Skip to content

Commit

Permalink
Fixed error accessing sys.stdout/sys.stderr when those are None (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-shklover authored Jul 1, 2024
1 parent 5641346 commit eb10a0d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
6 changes: 4 additions & 2 deletions ipykernel/inprocess/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ def _abort_queues(self):
def _input_request(self, prompt, ident, parent, password=False):
# Flush output before making the request.
self.raw_input_str = None
sys.stderr.flush()
sys.stdout.flush()
if sys.stdout is not None:
sys.stdout.flush()
if sys.stderr is not None:
sys.stderr.flush()

# Send the input request.
content = json_clean(dict(prompt=prompt, password=password))
Expand Down
30 changes: 20 additions & 10 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ async def process_control_message(self, msg=None):
except Exception:
self.log.error("Exception in control handler:", exc_info=True) # noqa: G201

sys.stdout.flush()
sys.stderr.flush()
if sys.stdout is not None:
sys.stdout.flush()
if sys.stderr is not None:
sys.stderr.flush()
self._publish_status("idle", "control")

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

sys.stdout.flush()
sys.stderr.flush()
if sys.stdout is not None:
sys.stdout.flush()
if sys.stderr is not None:
sys.stderr.flush()
self._publish_status("idle", "shell")

async def control_main(self):
Expand Down Expand Up @@ -665,8 +669,10 @@ async def execute_request(self, socket, ident, parent):
reply_content = await reply_content

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

# flush i/o
sys.stdout.flush()
sys.stderr.flush()
if sys.stdout is not None:
sys.stdout.flush()
if sys.stderr is not None:
sys.stderr.flush()

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

def _input_request(self, prompt, ident, parent, password=False):
# Flush output before making the request.
sys.stderr.flush()
sys.stdout.flush()
if sys.stdout is not None:
sys.stdout.flush()
if sys.stderr is not None:
sys.stderr.flush()

# flush the stdin socket, to purge stale replies
while True:
Expand Down

0 comments on commit eb10a0d

Please sign in to comment.