diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index 44e3bafa..c7a1d193 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -506,13 +506,13 @@ def init_io(self): if hasattr(sys.stderr, "_original_stdstream_copy"): for handler in self.log.handlers: - if (isinstance(handler, StreamHandler) - and (buffer := getattr(handler.stream, "buffer")) - and (fileno := getattr(buffer, "fileno")) - and fileno() == sys.stderr._original_stdstream_fd): - self.log.debug( - "Seeing logger to stderr, rerouting to raw filedescriptor." - ) + if ( + isinstance(handler, StreamHandler) + and (buffer := handler.stream.buffer) + and (fileno := buffer.fileno) + and fileno() == sys.stderr._original_stdstream_fd + ): + self.log.debug("Seeing logger to stderr, rerouting to raw filedescriptor.") handler.stream = TextIOWrapper( FileIO( @@ -534,18 +534,17 @@ def reset_io(self): """ stdout, stderr, displayhook = sys.stdout, sys.stderr, sys.displayhook sys.stdout, sys.stderr, sys.displayhook = self._original_io - if (finish_displayhook := getattr(displayhook, "finish_displayhook", - None)): + if finish_displayhook := getattr(displayhook, "finish_displayhook", None): finish_displayhook() if hasattr(sys.stderr, "_original_stdstream_copy"): for handler in self.log.handlers: - if (isinstance(handler, StreamHandler) - and (buffer := getattr(handler.stream, "buffer")) - and (fileno := getattr(buffer, "fileno")) - and fileno() == sys.stderr._original_stdstream_copy): - self.log.debug( - "Seeing logger to raw filedescriptor, rerouting back to stderr" - ) + if ( + isinstance(handler, StreamHandler) + and (buffer := handler.stream.buffer) + and (fileno := buffer.fileno) + and fileno() == sys.stderr._original_stdstream_copy + ): + self.log.debug("Seeing logger to raw filedescriptor, rerouting back to stderr") handler.stream = TextIOWrapper( FileIO(