@@ -181,11 +181,19 @@ class _ReadlineWrapper(object):
181
181
saved_history_length = - 1
182
182
startup_hook = None
183
183
config = ReadlineConfig ()
184
+ stdin = None
185
+ stdout = None
186
+ stderr = None
184
187
185
188
def __init__ (self , f_in = None , f_out = None ):
186
189
self .f_in = f_in if f_in is not None else os .dup (0 )
187
190
self .f_out = f_out if f_out is not None else os .dup (1 )
188
191
192
+ def setup_std_streams (self , stdin , stdout , stderr ):
193
+ self .stdin = stdin
194
+ self .stdout = stdout
195
+ self .stderr = stderr
196
+
189
197
def get_reader (self ):
190
198
if self .reader is None :
191
199
console = UnixConsole (self .f_in , self .f_out , encoding = ENCODING )
@@ -198,7 +206,18 @@ def raw_input(self, prompt=''):
198
206
reader = self .get_reader ()
199
207
except _error :
200
208
return _old_raw_input (prompt )
201
- reader .ps1 = prompt
209
+
210
+ # the builtin raw_input calls PyOS_StdioReadline, which flushes
211
+ # stdout/stderr before displaying the prompt. Try to mimic this
212
+ # behavior: it seems to be the correct thing to do, and moreover it
213
+ # mitigates this pytest issue:
214
+ # https://github.com/pytest-dev/pytest/issues/5134
215
+ if self .stdout and hasattr (self .stdout , 'flush' ):
216
+ self .stdout .flush ()
217
+ if self .stderr and hasattr (self .stderr , 'flush' ):
218
+ self .stderr .flush ()
219
+
220
+ reader .ps1 = prompt
202
221
return reader .readline (startup_hook = self .startup_hook )
203
222
204
223
def multiline_input (self , more_lines , ps1 , ps2 , returns_unicode = False ):
@@ -405,6 +424,7 @@ def _setup():
405
424
406
425
_wrapper .f_in = f_in
407
426
_wrapper .f_out = f_out
427
+ _wrapper .setup_std_streams (sys .stdin , sys .stdout , sys .stderr )
408
428
409
429
if '__pypy__' in sys .builtin_module_names : # PyPy
410
430
0 commit comments