Skip to content

Commit eb10a0d

Browse files
Fixed error accessing sys.stdout/sys.stderr when those are None (#1247)
1 parent 5641346 commit eb10a0d

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

ipykernel/inprocess/ipkernel.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ def _abort_queues(self):
9797
def _input_request(self, prompt, ident, parent, password=False):
9898
# Flush output before making the request.
9999
self.raw_input_str = None
100-
sys.stderr.flush()
101-
sys.stdout.flush()
100+
if sys.stdout is not None:
101+
sys.stdout.flush()
102+
if sys.stderr is not None:
103+
sys.stderr.flush()
102104

103105
# Send the input request.
104106
content = json_clean(dict(prompt=prompt, password=password))

ipykernel/kernelbase.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,10 @@ async def process_control_message(self, msg=None):
296296
except Exception:
297297
self.log.error("Exception in control handler:", exc_info=True) # noqa: G201
298298

299-
sys.stdout.flush()
300-
sys.stderr.flush()
299+
if sys.stdout is not None:
300+
sys.stdout.flush()
301+
if sys.stderr is not None:
302+
sys.stderr.flush()
301303
self._publish_status("idle", "control")
302304

303305
async def should_handle(self, stream, msg, idents):
@@ -439,8 +441,10 @@ async def process_shell_message(self, msg=None):
439441
except Exception:
440442
self.log.debug("Unable to signal in post_handler_hook:", exc_info=True)
441443

442-
sys.stdout.flush()
443-
sys.stderr.flush()
444+
if sys.stdout is not None:
445+
sys.stdout.flush()
446+
if sys.stderr is not None:
447+
sys.stderr.flush()
444448
self._publish_status("idle", "shell")
445449

446450
async def control_main(self):
@@ -665,8 +669,10 @@ async def execute_request(self, socket, ident, parent):
665669
reply_content = await reply_content
666670

667671
# Flush output before sending the reply.
668-
sys.stdout.flush()
669-
sys.stderr.flush()
672+
if sys.stdout is not None:
673+
sys.stdout.flush()
674+
if sys.stderr is not None:
675+
sys.stderr.flush()
670676
# FIXME: on rare occasions, the flush doesn't seem to make it to the
671677
# clients... This seems to mitigate the problem, but we definitely need
672678
# to better understand what's going on.
@@ -997,8 +1003,10 @@ async def apply_request(self, socket, ident, parent): # pragma: no cover
9971003
reply_content, result_buf = self.do_apply(content, bufs, msg_id, md)
9981004

9991005
# flush i/o
1000-
sys.stdout.flush()
1001-
sys.stderr.flush()
1006+
if sys.stdout is not None:
1007+
sys.stdout.flush()
1008+
if sys.stderr is not None:
1009+
sys.stderr.flush()
10021010

10031011
md = self.finish_metadata(parent, md, reply_content)
10041012
if not self.session:
@@ -1136,8 +1144,10 @@ def raw_input(self, prompt=""):
11361144

11371145
def _input_request(self, prompt, ident, parent, password=False):
11381146
# Flush output before making the request.
1139-
sys.stderr.flush()
1140-
sys.stdout.flush()
1147+
if sys.stdout is not None:
1148+
sys.stdout.flush()
1149+
if sys.stderr is not None:
1150+
sys.stderr.flush()
11411151

11421152
# flush the stdin socket, to purge stale replies
11431153
while True:

0 commit comments

Comments
 (0)