@@ -189,11 +189,19 @@ class _ReadlineWrapper(object):
189189 saved_history_length = - 1
190190 startup_hook = None
191191 config = ReadlineConfig ()
192+ stdin = None
193+ stdout = None
194+ stderr = None
192195
193196 def __init__ (self , f_in = None , f_out = None ):
194197 self .f_in = f_in if f_in is not None else os .dup (0 )
195198 self .f_out = f_out if f_out is not None else os .dup (1 )
196199
200+ def setup_std_streams (self , stdin , stdout , stderr ):
201+ self .stdin = stdin
202+ self .stdout = stdout
203+ self .stderr = stderr
204+
197205 def get_reader (self ):
198206 if self .reader is None :
199207 console = UnixConsole (self .f_in , self .f_out , encoding = ENCODING )
@@ -208,6 +216,16 @@ def raw_input(self, prompt=''):
208216 return _old_raw_input (prompt )
209217 reader .ps1 = prompt
210218
219+ # the builtin raw_input calls PyOS_StdioReadline, which flushes
220+ # stdout/stderr before displaying the prompt. Try to mimic this
221+ # behavior: it seems to be the correct thing to do, and moreover it
222+ # mitigates this pytest issue:
223+ # https://github.com/pytest-dev/pytest/issues/5134
224+ if self .stdout and hasattr (self .stdout , 'flush' ):
225+ self .stdout .flush ()
226+ if self .stderr and hasattr (self .stderr , 'flush' ):
227+ self .stderr .flush ()
228+
211229 ret = reader .readline (startup_hook = self .startup_hook )
212230 if not PY3 :
213231 return ret
@@ -423,6 +441,7 @@ def _setup():
423441
424442 _wrapper .f_in = f_in
425443 _wrapper .f_out = f_out
444+ _wrapper .setup_std_streams (sys .stdin , sys .stdout , sys .stderr )
426445
427446 if '__pypy__' in sys .builtin_module_names : # PyPy
428447
0 commit comments