Skip to content

Prevent pyreadline from bugging out in --pdb mode with capture #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ Eduardo Schettino
Elizaveta Shashkova
Eric Hunsberger
Eric Siegerman
Erik M. Bray
Florian Bruhin
Floris Bruynooghe
Gabriel Reis
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -5,6 +5,9 @@
this was a regression failing plugins combinations
like pytest-pep8 + pytest-flakes

- Workaround for exception that occurs in pyreadline when using
``--pdb`` with standard I/O capture enabled.

2.8.5
-----

26 changes: 26 additions & 0 deletions _pytest/capture.py
Original file line number Diff line number Diff line change
@@ -308,6 +308,7 @@ class FDCapture:
""" Capture IO to/from a given os-level filedescriptor. """

def __init__(self, targetfd, tmpfile=None):
readline_workaround()
self.targetfd = targetfd
try:
self.targetfd_save = os.dup(self.targetfd)
@@ -442,3 +443,28 @@ def isatty(self):

def close(self):
pass


def readline_workaround():
"""
Ensure readline is imported so that it attaches to the correct stdio
handles on Windows.
Pdb uses readline support where available--when not running from the Python
prompt, the readline module is not imported until running the pdb REPL. If
running py.test with the --pdb option this means the readline module is not
imported until after I/O capture has been started.
This is a problem for pyreadline, which is often used to implement readline
support on Windows, as it does not attach to the correct handles for stdout
and/or stdin if they have been redirected by the FDCapture mechanism. This
workaround ensures that readline is imported before I/O capture is setup so
that it can attach to the actual stdin/out for the console.
See https://github.com/pytest-dev/pytest/pull/1281
"""

try:
import readline # noqa
except ImportError:
pass