Skip to content

Fix fd encodings #629

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 4 commits into from
Oct 11, 2020
Merged
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
10 changes: 7 additions & 3 deletions bpython/curtsiesfrontend/coderunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def request_from_main_context(self, force_refresh=False):


class FakeOutput(object):
def __init__(self, coderunner, on_write, fileno=1):
def __init__(self, coderunner, on_write, real_fileobj):
"""Fakes sys.stdout or sys.stderr

on_write should always take unicode
Expand All @@ -215,7 +215,7 @@ def __init__(self, coderunner, on_write, fileno=1):
"""
self.coderunner = coderunner
self.on_write = on_write
self.real_fileno = fileno
self._real_fileobj = real_fileobj

def write(self, s, *args, **kwargs):
if not py3 and isinstance(s, str):
Expand All @@ -227,7 +227,7 @@ def write(self, s, *args, **kwargs):
# have a method called fileno. One example is pwntools. This
# is not a widespread issue, but is annoying.
def fileno(self):
return self.real_fileno
return self._real_fileobj.fileno()

def writelines(self, l):
for s in l:
Expand All @@ -238,3 +238,7 @@ def flush(self):

def isatty(self):
return True

@property
def encoding(self):
return self._real_fileobj.encoding
6 changes: 3 additions & 3 deletions bpython/curtsiesfrontend/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def close(self):

@property
def encoding(self):
return "UTF8"
return sys.__stdin__.encoding

# TODO write a read() method?

Expand Down Expand Up @@ -421,12 +421,12 @@ def __init__(
self.stdout = FakeOutput(
self.coderunner,
self.send_to_stdouterr,
fileno=sys.__stdout__.fileno(),
real_fileobj=sys.__stdout__,
)
self.stderr = FakeOutput(
self.coderunner,
self.send_to_stdouterr,
fileno=sys.__stderr__.fileno(),
real_fileobj=sys.__stderr__,
)
self.stdin = FakeStdin(self.coderunner, self, self.edit_keys)

Expand Down
10 changes: 5 additions & 5 deletions bpython/test/test_curtsies_coderunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def test_simple(self):
request_refresh=lambda: self.orig_stdout.flush()
or self.orig_stderr.flush()
)
stdout = FakeOutput(c, lambda *args, **kwargs: None)
stderr = FakeOutput(c, lambda *args, **kwargs: None)
stdout = FakeOutput(c, lambda *args, **kwargs: None, None)
stderr = FakeOutput(c, lambda *args, **kwargs: None, None)
sys.stdout = stdout
sys.stdout = stderr
c.load_code("1 + 1")
Expand All @@ -38,8 +38,8 @@ def test_exception(self):
def ctrlc():
raise KeyboardInterrupt()

stdout = FakeOutput(c, lambda x: ctrlc())
stderr = FakeOutput(c, lambda *args, **kwargs: None)
stdout = FakeOutput(c, lambda x: ctrlc(), None)
stderr = FakeOutput(c, lambda *args, **kwargs: None, None)
sys.stdout = stdout
sys.stderr = stderr
c.load_code("1 + 1")
Expand All @@ -51,5 +51,5 @@ def assert_unicode(self, s):
self.assertIsInstance(s, type(u""))

def test_bytes(self):
out = FakeOutput(mock.Mock(), self.assert_unicode)
out = FakeOutput(mock.Mock(), self.assert_unicode, None)
out.write("native string type")