Skip to content

Commit b30efb8

Browse files
Merge remote-tracking branch 'caleb/add-fileno-to-fakeoutput'
2 parents f21a68b + c2c9e84 commit b30efb8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

bpython/curtsiesfrontend/coderunner.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,30 @@ def request_from_main_greenlet(self, force_refresh=False):
195195

196196

197197
class FakeOutput(object):
198-
def __init__(self, coderunner, on_write):
198+
def __init__(self, coderunner, on_write, fileno=1):
199199
"""Fakes sys.stdout or sys.stderr
200200
201201
on_write should always take unicode
202+
203+
fileno should be the fileno that on_write will
204+
output to (e.g. 1 for standard output).
202205
"""
203206
self.coderunner = coderunner
204207
self.on_write = on_write
208+
self.real_fileno = fileno
205209

206210
def write(self, s, *args, **kwargs):
207211
if not py3 and isinstance(s, str):
208212
s = s.decode(getpreferredencoding(), 'ignore')
209213
self.on_write(s, *args, **kwargs)
210214
return self.coderunner.request_from_main_greenlet(force_refresh=True)
211215

216+
# Some applications which use curses require that sys.stdout
217+
# have a method called fileno. One example is pwntools. This
218+
# is not a widespread issue, but is annoying.
219+
def fileno(self):
220+
return self.real_fileno
221+
212222
def writelines(self, l):
213223
for s in l:
214224
self.write(s)

bpython/curtsiesfrontend/repl.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,13 @@ def __init__(self,
389389
self.orig_tcattrs = orig_tcattrs
390390

391391
self.coderunner = CodeRunner(self.interp, self.request_refresh)
392-
self.stdout = FakeOutput(self.coderunner, self.send_to_stdout)
393-
self.stderr = FakeOutput(self.coderunner, self.send_to_stderr)
392+
393+
# filenos match the backing device for libs that expect it,
394+
# but writing to them will do weird things to the display
395+
self.stdout = FakeOutput(self.coderunner, self.send_to_stdout,
396+
fileno=sys.__stdout__.fileno())
397+
self.stderr = FakeOutput(self.coderunner, self.send_to_stderr,
398+
fileno=sys.__stderr__.fileno())
394399
self.stdin = FakeStdin(self.coderunner, self, self.edit_keys)
395400

396401
# next paint should clear screen

0 commit comments

Comments
 (0)