Skip to content

Commit cc87037

Browse files
committed
Simplify and remove unused context manager
1 parent 4501fd9 commit cc87037

1 file changed

Lines changed: 4 additions & 14 deletions

File tree

ipykernel/zmqshell.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import threading
2121
import typing
2222
import warnings
23-
from contextlib import contextmanager
2423
from pathlib import Path
2524
from subprocess import CalledProcessError
2625

@@ -766,30 +765,21 @@ def get_parent(self):
766765
def set_thread_parent(self, parent):
767766
"""Set the parent header for only the current thread associating output with its triggering input"""
768767
tokens = [(self._parent_header.reset, self._parent_header.set(parent))]
769-
objs: list[typing.Any] = [self.displayhook, self.display_pub]
768+
objs = [self.displayhook, self.display_pub, sys.stdout, sys.stderr]
770769
if hasattr(self, "_data_pub"):
771770
objs.append(self.data_pub)
772-
objs += [sys.stdout, sys.stderr]
773771
for obj in objs:
774772
set_thread = getattr(obj, "set_thread_parent", None)
775-
if set_thread is not None:
776-
tokens.append((obj.reset_thread_parent, set_thread(parent)))
773+
reset_thread = getattr(obj, "reset_thread_parent", None)
774+
if set_thread is not None and reset_thread is not None:
775+
tokens.append((reset_thread, set_thread(parent)))
777776
return tuple(tokens)
778777

779778
def reset_thread_parent(self, tokens):
780779
"""Reset the parent header to undo the set_thread_parent call that returned the token."""
781780
for reset, token in reversed(tokens):
782781
reset(token)
783782

784-
@contextmanager
785-
def parent_override(self, parent):
786-
"""Context manager to temporarily set the thread's parent header"""
787-
tokens = self.set_thread_parent(parent)
788-
try:
789-
yield
790-
finally:
791-
self.reset_thread_parent(tokens)
792-
793783
def init_magics(self):
794784
"""Initialize magics."""
795785
super().init_magics()

0 commit comments

Comments
 (0)