Skip to content
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

readline: pick up current stdin/stdout #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 28 additions & 19 deletions pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,30 @@ class _ReadlineWrapper(object):
saved_history_length = -1
startup_hook = None
config = ReadlineConfig()
stdin = None
stdout = None
stderr = None

def __init__(self, f_in=None, f_out=None):
self.f_in = f_in if f_in is not None else os.dup(0)
self.f_out = f_out if f_out is not None else os.dup(1)
# Forced input/output, otherwise sys.stdin/sys.stdout will be used.
f_in = None
f_out = None

def setup_std_streams(self, stdin, stdout, stderr):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
def __init__(self, f_in=None, f_out=None):
if f_in is not None:
self.f_in = f_in
if f_out is not None:
self.f_out = f_out

def get_reader(self):
if self.reader is None:
console = UnixConsole(self.f_in, self.f_out, encoding=ENCODING)
if self.f_in is None:
fd_in = sys.stdin.fileno()
else:
fd_in = self.f_in
if self.f_out is None:
fd_out = sys.stdout.fileno()
else:
fd_out = self.f_out
if (self.reader is None
or fd_in != self.reader.console.input_fd
or fd_out != self.reader.console.output_fd):
console = UnixConsole(fd_in, fd_out, encoding=ENCODING)
self.reader = ReadlineAlikeReader(console)
self.reader.config = self.config
return self.reader
Expand All @@ -221,10 +229,14 @@ def raw_input(self, prompt=''):
# behavior: it seems to be the correct thing to do, and moreover it
# mitigates this pytest issue:
# https://github.com/pytest-dev/pytest/issues/5134
if self.stdout and hasattr(self.stdout, 'flush'):
self.stdout.flush()
if self.stderr and hasattr(self.stderr, 'flush'):
self.stderr.flush()
try:
sys.stdout.flush()
except AttributeError:
pass
try:
sys.stderr.flush()
except AttributeError:
pass

ret = reader.readline(startup_hook=self.startup_hook)
if not PY3:
Expand Down Expand Up @@ -447,9 +459,6 @@ def _setup():
if not os.isatty(f_in) or not os.isatty(f_out):
return

_wrapper.f_in = f_in
_wrapper.f_out = f_out
_wrapper.setup_std_streams(sys.stdin, sys.stdout, sys.stderr)

if '__pypy__' in sys.builtin_module_names: # PyPy

Expand Down
38 changes: 38 additions & 0 deletions testing/test_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from pyrepl.readline import _ReadlineWrapper


pytest_plugins = ["pytester"]


@pytest.fixture
def readline_wrapper():
master, slave = pty.openpty()
Expand Down Expand Up @@ -103,3 +106,38 @@ def replace(cls, *args):
readline_wrapper.write_history_file(str(histfile))

assert open(str(histfile), "r").readlines() == ["foo\n", "bar\n"]


@pytest.mark.parametrize("use_pyrepl", (True, False))
def test_uses_current_stdin(use_pyrepl, testdir):
p1 = testdir.makepyfile("""
import io, os, sys

assert os.isatty(0)
dup_stdin = io.FileIO(os.dup(0))
assert os.isatty(dup_stdin.fileno())

old_stdin = sys.stdin
sys.stdin = dup_stdin

if {use_pyrepl!r}:
import pyrepl.readline

print("input1:", input())
dup_stdin.close()

sys.stdin = old_stdin
print("input2:", input())
""".format(use_pyrepl=use_pyrepl))

child = testdir.spawn(sys.executable + " " + str(p1), expect_timeout=1)
child.sendline("line1")
if use_pyrepl:
child.expect_exact("input1: line1\r\n")
else:
child.expect_exact("input1: b'line1'\r\n")
child.sendline("line2")
if use_pyrepl:
child.expect_exact("input2: line2\r\n")
else:
child.expect_exact(b'line2\r\ninput2: line2\r\n')
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ commands =
passenv =
TERM
setenv =
coverage: PYTEST_ADDOPTS=--cov {env:PYTEST_ADDOPTS:}
coverage: PYTEST_ADDOPTS=--cov --cov-config={toxinidir}/tox.ini {env:PYTEST_ADDOPTS:}

[testenv:qa]
deps =
Expand Down