@@ -70,6 +70,39 @@ def __exit__(self, *args):
7070 sys .stdout = self ._stdout
7171
7272
73+ # Custom mock for sys.stdin that supports buffer attribute
74+ class MockStdinWithBuffer :
75+ def __init__ (self , inputs : str ):
76+ self .inputs = inputs
77+ self ._stringio = StringIO (inputs )
78+ self .buffer = MockBuffer (inputs )
79+
80+ def read (self , * args ):
81+ return self .inputs
82+
83+ def readline (self , * args ):
84+ return self ._stringio .readline (* args )
85+
86+ def readlines (self , * args ):
87+ return self .inputs .split ("\n " )
88+
89+ def __getattr__ (self , name ):
90+ # Delegate other attributes to StringIO
91+ return getattr (self ._stringio , name )
92+
93+
94+ class MockBuffer :
95+ def __init__ (self , inputs : str ):
96+ self .inputs = inputs .encode ("utf-8" ) # Convert to bytes
97+
98+ def read (self , * args ):
99+ # Return as byte strings that can be split
100+ return self .inputs
101+
102+ def readline (self , * args ):
103+ return self .inputs .split (b"\n " )[0 ] + b"\n "
104+
105+
73106def clean_if_name (code : str ) -> str :
74107 try :
75108 astree = ast .parse (code )
@@ -125,11 +158,14 @@ def call_method(method, inputs):
125158
126159 inputs_line_iterator = iter (inputs .split ("\n " ))
127160
161+ # Create custom stdin mock with buffer support
162+ mock_stdin = MockStdinWithBuffer (inputs )
163+
128164 # sys.setrecursionlimit(10000)
129165
130166 # @patch('builtins.input', side_effect=inputs.split("\n"))
131167 @patch ("builtins.open" , mock_open (read_data = inputs ))
132- @patch ("sys.stdin" , StringIO ( inputs ))
168+ @patch ("sys.stdin" , mock_stdin ) # Use our custom mock instead of StringIO
133169 @patch ("sys.stdin.readline" , lambda * args : next (inputs_line_iterator ))
134170 @patch ("sys.stdin.readlines" , lambda * args : inputs .split ("\n " ))
135171 @patch ("sys.stdin.read" , lambda * args : inputs )
0 commit comments