|
48 | 48 | import math |
49 | 49 | import re |
50 | 50 | import time |
| 51 | +import functools |
51 | 52 |
|
52 | 53 | import struct |
53 | 54 | if platform.system() != 'Windows': |
@@ -116,18 +117,31 @@ def calculate_screen_lines(tokens, width, cursor=0): |
116 | 117 | pos %= width |
117 | 118 | return lines |
118 | 119 |
|
| 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 | + |
119 | 129 |
|
120 | 130 | class FakeStream(object): |
121 | 131 | """Provide a fake file object which calls functions on the interface |
122 | 132 | provided.""" |
123 | 133 |
|
124 | | - def __init__(self, interface): |
| 134 | + def __init__(self, interface, get_dest): |
125 | 135 | self.encoding = getpreferredencoding() |
126 | 136 | self.interface = interface |
| 137 | + self.active = True |
| 138 | + self.get_dest = get_dest |
127 | 139 |
|
| 140 | + @forward_if_not_active |
128 | 141 | def write(self, s): |
129 | 142 | self.interface.write(s) |
130 | 143 |
|
| 144 | + @forward_if_not_active |
131 | 145 | def writelines(self, l): |
132 | 146 | for s in l: |
133 | 147 | self.write(s) |
@@ -1873,8 +1887,8 @@ def main_curses(scr, args, config, interactive=True, locals_=None, |
1873 | 1887 | clirepl._C = cols |
1874 | 1888 |
|
1875 | 1889 | 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) |
1878 | 1892 |
|
1879 | 1893 | if args: |
1880 | 1894 | exit_value = () |
@@ -1927,6 +1941,8 @@ def main(args=None, locals_=None, banner=None): |
1927 | 1941 | main_curses, exec_args, config, options.interactive, locals_, |
1928 | 1942 | banner=banner) |
1929 | 1943 | finally: |
| 1944 | + sys.stderr.active = False |
| 1945 | + sys.stdout.active = False |
1930 | 1946 | sys.stdin = orig_stdin |
1931 | 1947 | sys.stderr = orig_stderr |
1932 | 1948 | sys.stdout = orig_stdout |
|
0 commit comments