MCPcopy Create free account
hub / github.com/ipython/ipython / DisplayTrap

Class DisplayTrap

IPython/core/display_trap.py:32–69  ·  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 def set(self):
62 """Set the hook."""
63 if sys.displayhook is not self.hook:
64 self.old_hook = sys.displayhook
65 sys.displayhook = self.hook
66
67 def unset(self):
68 """Unset the hook."""
69 sys.displayhook = self.old_hook
70

Callers 2

debuggerMethod · 0.90
init_displayhookMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected