|
20 | 20 | import threading |
21 | 21 | import typing |
22 | 22 | import warnings |
23 | | -from contextlib import contextmanager |
24 | 23 | from pathlib import Path |
25 | 24 | from subprocess import CalledProcessError |
26 | 25 |
|
@@ -766,30 +765,21 @@ def get_parent(self): |
766 | 765 | def set_thread_parent(self, parent): |
767 | 766 | """Set the parent header for only the current thread associating output with its triggering input""" |
768 | 767 | 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] |
770 | 769 | if hasattr(self, "_data_pub"): |
771 | 770 | objs.append(self.data_pub) |
772 | | - objs += [sys.stdout, sys.stderr] |
773 | 771 | for obj in objs: |
774 | 772 | 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))) |
777 | 776 | return tuple(tokens) |
778 | 777 |
|
779 | 778 | def reset_thread_parent(self, tokens): |
780 | 779 | """Reset the parent header to undo the set_thread_parent call that returned the token.""" |
781 | 780 | for reset, token in reversed(tokens): |
782 | 781 | reset(token) |
783 | 782 |
|
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 | | - |
793 | 783 | def init_magics(self): |
794 | 784 | """Initialize magics.""" |
795 | 785 | super().init_magics() |
|
0 commit comments