Skip to content

Commit d00ff33

Browse files
committed
Modify all log handlers to route messages directly screen.
Bypass the original fd 2 for stderr, and use the new piped one.
1 parent 637c35c commit d00ff33

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

ipykernel/kernelapp.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import signal
1111
import traceback
1212
import logging
13+
from io import TextIOWrapper, FileIO
14+
from logging import StreamHandler
1315

1416
import tornado
1517
from tornado import ioloop
@@ -414,9 +416,24 @@ def init_io(self):
414416
echo=e_stdout)
415417
if sys.stderr is not None:
416418
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.")
420437
if self.displayhook_class:
421438
displayhook_factory = import_item(str(self.displayhook_class))
422439
self.displayhook = displayhook_factory(self.session, self.iopub_socket)

0 commit comments

Comments
 (0)