Skip to content

Commit 3ff3cfa

Browse files
committed
make sure to flush stdout/stderr when calling readline()/raw_input(). It helps to mitigates this pytest issue:
pytest-dev/pytest#5134 Prior to this fix, pdb++ failed to display the output when you run it under the conditions described in the issue above.
1 parent 9a9da69 commit 3ff3cfa

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

pyrepl/readline.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,19 @@ class _ReadlineWrapper(object):
181181
saved_history_length = -1
182182
startup_hook = None
183183
config = ReadlineConfig()
184+
stdin = None
185+
stdout = None
186+
stderr = None
184187

185188
def __init__(self, f_in=None, f_out=None):
186189
self.f_in = f_in if f_in is not None else os.dup(0)
187190
self.f_out = f_out if f_out is not None else os.dup(1)
188191

192+
def setup_std_streams(self, stdin, stdout, stderr):
193+
self.stdin = stdin
194+
self.stdout = stdout
195+
self.stderr = stderr
196+
189197
def get_reader(self):
190198
if self.reader is None:
191199
console = UnixConsole(self.f_in, self.f_out, encoding=ENCODING)
@@ -198,7 +206,18 @@ def raw_input(self, prompt=''):
198206
reader = self.get_reader()
199207
except _error:
200208
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
202221
return reader.readline(startup_hook=self.startup_hook)
203222

204223
def multiline_input(self, more_lines, ps1, ps2, returns_unicode=False):
@@ -405,6 +424,7 @@ def _setup():
405424

406425
_wrapper.f_in = f_in
407426
_wrapper.f_out = f_out
427+
_wrapper.setup_std_streams(sys.stdin, sys.stdout, sys.stderr)
408428

409429
if '__pypy__' in sys.builtin_module_names: # PyPy
410430

0 commit comments

Comments
 (0)