From 7b56472b77a7cb0705ed15dde11fd4cc0d32e495 Mon Sep 17 00:00:00 2001 From: Ilya Sherstyuk Date: Thu, 12 Jan 2023 18:16:58 -0800 Subject: [PATCH] Fix Exception in OutStream.close() This bug fixes an Exception which was thrown in OutStream.close() whenever the OutStream was constructed with watchfd=False. A regression test was also added to test_io.py. --- ipykernel/iostream.py | 3 ++- ipykernel/tests/test_io.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ipykernel/iostream.py b/ipykernel/iostream.py index 92df61aea..32829f6bd 100644 --- a/ipykernel/iostream.py +++ b/ipykernel/iostream.py @@ -401,6 +401,7 @@ def __init__( self._buffer = StringIO() self.echo = None self._isatty = bool(isatty) + self._should_watch = False if ( watchfd @@ -449,7 +450,7 @@ def set_parent(self, parent): def close(self): """Close the stream.""" - if sys.platform.startswith("linux") or sys.platform.startswith("darwin"): + if self._should_watch: self._should_watch = False self.watch_fd_thread.join() if self._exc: diff --git a/ipykernel/tests/test_io.py b/ipykernel/tests/test_io.py index f7fc8b3f4..221af1f8b 100644 --- a/ipykernel/tests/test_io.py +++ b/ipykernel/tests/test_io.py @@ -98,6 +98,9 @@ def test_outstream(): stream = OutStream(session, pub, "stdout") stream = OutStream(session, thread, "stdout", pipe=object()) + stream = OutStream(session, thread, "stdout", watchfd=False) + stream.close() + stream = OutStream(session, thread, "stdout", isatty=True, echo=io.StringIO()) with pytest.raises(io.UnsupportedOperation): stream.fileno()