MCPcopy Index your code
hub / github.com/ipython/ipython / DisplayTrap

Class DisplayTrap

IPython/core/display_trap.py:32–73  ·  view source on GitHub ↗

Object to manage sys.displayhook. This came from IPython.core.kernel.display_hook, but is simplified (no callbacks or formatters) until more of the core is refactored.

Source from the content-addressed store, hash-verified

30
31
32class DisplayTrap(Configurable):
33 """Object to manage sys.displayhook.
34
35 This came from IPython.core.kernel.display_hook, but is simplified
36 (no callbacks or formatters) until more of the core is refactored.
37 """
38
39 hook = Any()
40
41 def __init__(self, hook=None):
42 super(DisplayTrap, self).__init__(hook=hook, config=None)
43 self.old_hook = None
44 # We define this to track if a single BuiltinTrap is nested.
45 # Only turn off the trap when the outermost call to __exit__ is made.
46 self._nested_level = 0
47
48 def __enter__(self):
49 if self._nested_level == 0:
50 self.set()
51 self._nested_level += 1
52 return self
53
54 def __exit__(self, type, value, traceback):
55 if self._nested_level == 1:
56 self.unset()
57 self._nested_level -= 1
58 # Returning False will cause exceptions to propagate
59 return False
60
61 @property
62 def is_active(self) -> bool:
63 return self._nested_level != 0
64
65 def set(self):
66 """Set the hook."""
67 if sys.displayhook is not self.hook:
68 self.old_hook = sys.displayhook
69 sys.displayhook = self.hook
70
71 def unset(self):
72 """Unset the hook."""
73 sys.displayhook = self.old_hook
74

Callers 2

init_displayhookMethod · 0.90
debuggerMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…