X Tutup
Skip to content

Commit 2e3cd64

Browse files
proof of concept fix for #257
1 parent b4bef26 commit 2e3cd64

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

bpython/cli.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import math
4949
import re
5050
import time
51+
import functools
5152

5253
import struct
5354
if platform.system() != 'Windows':
@@ -116,18 +117,31 @@ def calculate_screen_lines(tokens, width, cursor=0):
116117
pos %= width
117118
return lines
118119

120+
def forward_if_not_active(func):
121+
@functools.wraps(func)
122+
def newfunc(self, *args, **kwargs):
123+
if self.active:
124+
return func(self, *args, **kwargs)
125+
else:
126+
return getattr(self.get_dest(), newfunc.__name__)(*args, **kwargs)
127+
return newfunc
128+
119129

120130
class FakeStream(object):
121131
"""Provide a fake file object which calls functions on the interface
122132
provided."""
123133

124-
def __init__(self, interface):
134+
def __init__(self, interface, get_dest):
125135
self.encoding = getpreferredencoding()
126136
self.interface = interface
137+
self.active = True
138+
self.get_dest = get_dest
127139

140+
@forward_if_not_active
128141
def write(self, s):
129142
self.interface.write(s)
130143

144+
@forward_if_not_active
131145
def writelines(self, l):
132146
for s in l:
133147
self.write(s)
@@ -1873,8 +1887,8 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
18731887
clirepl._C = cols
18741888

18751889
sys.stdin = FakeStdin(clirepl)
1876-
sys.stdout = FakeStream(clirepl)
1877-
sys.stderr = FakeStream(clirepl)
1890+
sys.stdout = FakeStream(clirepl, lambda: sys.stdout)
1891+
sys.stderr = FakeStream(clirepl, lambda: sys.stderr)
18781892

18791893
if args:
18801894
exit_value = ()
@@ -1927,6 +1941,8 @@ def main(args=None, locals_=None, banner=None):
19271941
main_curses, exec_args, config, options.interactive, locals_,
19281942
banner=banner)
19291943
finally:
1944+
sys.stderr.active = False
1945+
sys.stdout.active = False
19301946
sys.stdin = orig_stdin
19311947
sys.stderr = orig_stderr
19321948
sys.stdout = orig_stdout

0 commit comments

Comments
 (0)
X Tutup