Skip to content

Commit 6e170a4

Browse files
committed
* Moved workaround to its own function, mostly for the sake of adding
a more descriptive docstring (the workaround itself is just to import readline earlier). I removed the conditional on targetfd from the workaround since it doesn't really matter. * Added # noqa marker. * Added changelog entry, and self to authors.
1 parent 924a966 commit 6e170a4

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Eduardo Schettino
3131
Elizaveta Shashkova
3232
Eric Hunsberger
3333
Eric Siegerman
34+
Erik M. Bray
3435
Florian Bruhin
3536
Floris Bruynooghe
3637
Gabriel Reis

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
this was a regression failing plugins combinations
66
like pytest-pep8 + pytest-flakes
77

8+
- Workaround for exception that occurs in pyreadline when using
9+
``--pdb`` with standard I/O capture enabled.
10+
811
2.8.5
912
-----
1013

_pytest/capture.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,7 @@ class FDCapture:
308308
""" Capture IO to/from a given os-level filedescriptor. """
309309

310310
def __init__(self, targetfd, tmpfile=None):
311-
# ensure readline is imported so that it attaches to the correct
312-
# stdio handles on Windows
313-
if targetfd in (0, 1, 2):
314-
try:
315-
import readline
316-
except ImportError:
317-
pass
318-
311+
readline_workaround()
319312
self.targetfd = targetfd
320313
try:
321314
self.targetfd_save = os.dup(self.targetfd)
@@ -450,3 +443,28 @@ def isatty(self):
450443

451444
def close(self):
452445
pass
446+
447+
448+
def readline_workaround():
449+
"""
450+
Ensure readline is imported so that it attaches to the correct stdio
451+
handles on Windows.
452+
453+
Pdb uses readline support where available--when not running from the Python
454+
prompt, the readline module is not imported until running the pdb REPL. If
455+
running py.test with the --pdb option this means the readline module is not
456+
imported until after I/O capture has been started.
457+
458+
This is a problem for pyreadline, which is often used to implement readline
459+
support on Windows, as it does not attach to the correct handles for stdout
460+
and/or stdin if they have been redirected by the FDCapture mechanism. This
461+
workaround ensures that readline is imported before I/O capture is setup so
462+
that it can attach to the actual stdin/out for the console.
463+
464+
See https://github.com/pytest-dev/pytest/pull/1281
465+
"""
466+
467+
try:
468+
import readline # noqa
469+
except ImportError:
470+
pass

0 commit comments

Comments
 (0)