Skip to content

Commit e59905b

Browse files
Fix fd encodings (#629)
* Use real stdin encoding for fake stdin encoding. this is both more accurate and corrects the spelling from UTF8 to UTF-8
1 parent 5bdb173 commit e59905b

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

bpython/curtsiesfrontend/coderunner.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def request_from_main_context(self, force_refresh=False):
205205

206206

207207
class FakeOutput(object):
208-
def __init__(self, coderunner, on_write, fileno=1):
208+
def __init__(self, coderunner, on_write, real_fileobj):
209209
"""Fakes sys.stdout or sys.stderr
210210
211211
on_write should always take unicode
@@ -215,7 +215,7 @@ def __init__(self, coderunner, on_write, fileno=1):
215215
"""
216216
self.coderunner = coderunner
217217
self.on_write = on_write
218-
self.real_fileno = fileno
218+
self._real_fileobj = real_fileobj
219219

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

232232
def writelines(self, l):
233233
for s in l:
@@ -238,3 +238,7 @@ def flush(self):
238238

239239
def isatty(self):
240240
return True
241+
242+
@property
243+
def encoding(self):
244+
return self._real_fileobj.encoding

bpython/curtsiesfrontend/repl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def close(self):
223223

224224
@property
225225
def encoding(self):
226-
return "UTF8"
226+
return sys.__stdin__.encoding
227227

228228
# TODO write a read() method?
229229

@@ -447,12 +447,12 @@ def __init__(
447447
self.stdout = FakeOutput(
448448
self.coderunner,
449449
self.send_to_stdouterr,
450-
fileno=sys.__stdout__.fileno(),
450+
real_fileobj=sys.__stdout__,
451451
)
452452
self.stderr = FakeOutput(
453453
self.coderunner,
454454
self.send_to_stdouterr,
455-
fileno=sys.__stderr__.fileno(),
455+
real_fileobj=sys.__stderr__,
456456
)
457457
self.stdin = FakeStdin(self.coderunner, self, self.edit_keys)
458458

bpython/test/test_curtsies_coderunner.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def test_simple(self):
2020
request_refresh=lambda: self.orig_stdout.flush()
2121
or self.orig_stderr.flush()
2222
)
23-
stdout = FakeOutput(c, lambda *args, **kwargs: None)
24-
stderr = FakeOutput(c, lambda *args, **kwargs: None)
23+
stdout = FakeOutput(c, lambda *args, **kwargs: None, None)
24+
stderr = FakeOutput(c, lambda *args, **kwargs: None, None)
2525
sys.stdout = stdout
2626
sys.stdout = stderr
2727
c.load_code("1 + 1")
@@ -38,8 +38,8 @@ def test_exception(self):
3838
def ctrlc():
3939
raise KeyboardInterrupt()
4040

41-
stdout = FakeOutput(c, lambda x: ctrlc())
42-
stderr = FakeOutput(c, lambda *args, **kwargs: None)
41+
stdout = FakeOutput(c, lambda x: ctrlc(), None)
42+
stderr = FakeOutput(c, lambda *args, **kwargs: None, None)
4343
sys.stdout = stdout
4444
sys.stderr = stderr
4545
c.load_code("1 + 1")
@@ -51,5 +51,5 @@ def assert_unicode(self, s):
5151
self.assertIsInstance(s, type(u""))
5252

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

0 commit comments

Comments
 (0)