|
10 | 10 | import signal |
11 | 11 | import traceback |
12 | 12 | import logging |
| 13 | +from io import TextIOWrapper, FileIO |
| 14 | +from logging import StreamHandler |
13 | 15 |
|
14 | 16 | import tornado |
15 | 17 | from tornado import ioloop |
@@ -414,9 +416,24 @@ def init_io(self): |
414 | 416 | echo=e_stdout) |
415 | 417 | if sys.stderr is not None: |
416 | 418 | sys.stderr.flush() |
417 | | - sys.stderr = outstream_factory(self.session, self.iopub_thread, |
418 | | - 'stderr', |
419 | | - echo=e_stderr) |
| 419 | + sys.stderr = outstream_factory( |
| 420 | + self.session, self.iopub_thread, "stderr", echo=e_stderr |
| 421 | + ) |
| 422 | + self.log.error("this %s", hasattr(sys.stderr, "_original_stdstream_copy")) |
| 423 | + if hasattr(sys.stderr, "_original_stdstream_copy"): |
| 424 | + |
| 425 | + for handler in self.log.handlers: |
| 426 | + if isinstance(handler, StreamHandler) and ( |
| 427 | + handler.stream.buffer.fileno() == 2 |
| 428 | + ): |
| 429 | + self.log.debug( |
| 430 | + "Seeing logger to stderr, rerouting to raw filedescriptor." |
| 431 | + ) |
| 432 | + |
| 433 | + handler.stream = TextIOWrapper( |
| 434 | + FileIO(sys.stderr._original_stdstream_copy, "w") |
| 435 | + ) |
| 436 | + self.log.error("Redirected to raw FD.") |
420 | 437 | if self.displayhook_class: |
421 | 438 | displayhook_factory = import_item(str(self.displayhook_class)) |
422 | 439 | self.displayhook = displayhook_factory(self.session, self.iopub_socket) |
|
0 commit comments