X Tutup
Skip to content

Commit 917c0eb

Browse files
committed
Merged in thomasballinger/bpython (pull request #25)
Possible fix to #257
2 parents a0b027f + 58c4be9 commit 917c0eb

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

bpython/cli.py

Lines changed: 17 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_current(func):
121+
@functools.wraps(func)
122+
def newfunc(self, *args, **kwargs):
123+
dest = self.get_dest()
124+
if self is dest:
125+
return func(self, *args, **kwargs)
126+
else:
127+
return getattr(self.get_dest(), newfunc.__name__)(*args, **kwargs)
128+
return newfunc
129+
119130

120131
class FakeStream(object):
121132
"""Provide a fake file object which calls functions on the interface
122133
provided."""
123134

124-
def __init__(self, interface):
135+
def __init__(self, interface, get_dest):
125136
self.encoding = getpreferredencoding()
126137
self.interface = interface
138+
self.get_dest = get_dest
127139

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

144+
@forward_if_not_current
131145
def writelines(self, l):
132146
for s in l:
133147
self.write(s)
@@ -1916,8 +1930,8 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
19161930
clirepl._C = cols
19171931

19181932
sys.stdin = FakeStdin(clirepl)
1919-
sys.stdout = FakeStream(clirepl)
1920-
sys.stderr = FakeStream(clirepl)
1933+
sys.stdout = FakeStream(clirepl, lambda: sys.stdout)
1934+
sys.stderr = FakeStream(clirepl, lambda: sys.stderr)
19211935

19221936
if args:
19231937
exit_value = ()

0 commit comments

Comments
 (0)
X Tutup