@@ -189,11 +189,19 @@ class _ReadlineWrapper(object):
189
189
saved_history_length = - 1
190
190
startup_hook = None
191
191
config = ReadlineConfig ()
192
+ stdin = None
193
+ stdout = None
194
+ stderr = None
192
195
193
196
def __init__ (self , f_in = None , f_out = None ):
194
197
self .f_in = f_in if f_in is not None else os .dup (0 )
195
198
self .f_out = f_out if f_out is not None else os .dup (1 )
196
199
200
+ def setup_std_streams (self , stdin , stdout , stderr ):
201
+ self .stdin = stdin
202
+ self .stdout = stdout
203
+ self .stderr = stderr
204
+
197
205
def get_reader (self ):
198
206
if self .reader is None :
199
207
console = UnixConsole (self .f_in , self .f_out , encoding = ENCODING )
@@ -208,6 +216,16 @@ def raw_input(self, prompt=''):
208
216
return _old_raw_input (prompt )
209
217
reader .ps1 = prompt
210
218
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
+
211
229
ret = reader .readline (startup_hook = self .startup_hook )
212
230
if not PY3 :
213
231
return ret
@@ -423,6 +441,7 @@ def _setup():
423
441
424
442
_wrapper .f_in = f_in
425
443
_wrapper .f_out = f_out
444
+ _wrapper .setup_std_streams (sys .stdin , sys .stdout , sys .stderr )
426
445
427
446
if '__pypy__' in sys .builtin_module_names : # PyPy
428
447
0 commit comments