| 132 | fd.flush() |
| 133 | |
| 134 | def init(): |
| 135 | atexit.register(resetterm) |
| 136 | signal.signal(signal.SIGWINCH, handler_sigwinch) |
| 137 | signal.signal(signal.SIGTSTP, handler_sigstop) |
| 138 | signal.signal(signal.SIGCONT, handler_sigcont) |
| 139 | class Wrapper: |
| 140 | def __init__(self, fd): |
| 141 | self._fd = fd |
| 142 | def write(self, s): |
| 143 | return output(s, frozen=True) |
| 144 | def __getattr__(self, k): |
| 145 | return getattr(self._fd, k) |
| 146 | if sys.stdout.isatty(): |
| 147 | sys.stdout = Wrapper(sys.stdout) |
| 148 | if sys.stderr.isatty(): |
| 149 | sys.stderr = Wrapper(sys.stderr) |
| 150 | |
| 151 | console = ContextType.defaults['log_console'] |
| 152 | if console.isatty(): |
| 153 | ContextType.defaults['log_console'] = Wrapper(console) |
| 154 | |
| 155 | # freeze all cells if an exception is thrown |
| 156 | orig_hook = sys.excepthook |
| 157 | def hook(*args): |
| 158 | sys.stderr = sys.__stderr__ |
| 159 | resetterm() |
| 160 | cells.clear() |
| 161 | if orig_hook: |
| 162 | orig_hook(*args) |
| 163 | else: |
| 164 | traceback.print_exception(*args) |
| 165 | sys.excepthook = hook |
| 166 | |
| 167 | tmap = {c: '\\x{:02x}'.format(c) for c in set(range(0x20)) - {0x09, 0x0a, 0x0d, 0x1b} | {0x7f}} |
| 168 | |