##// END OF EJS Templates
moved Quitter to deathrow as it is no longer used by anything
Erik Tollerud -
Show More
@@ -1,127 +1,126 b''
1 1 """
2 2 A context manager for managing things injected into :mod:`__builtin__`.
3 3
4 4 Authors:
5 5
6 6 * Brian Granger
7 7 * Fernando Perez
8 8 """
9 9 #-----------------------------------------------------------------------------
10 10 # Copyright (C) 2010 The IPython Development Team.
11 11 #
12 12 # Distributed under the terms of the BSD License.
13 13 #
14 14 # Complete license in the file COPYING.txt, distributed with this software.
15 15 #-----------------------------------------------------------------------------
16 16
17 17 #-----------------------------------------------------------------------------
18 18 # Imports
19 19 #-----------------------------------------------------------------------------
20 20
21 21 import __builtin__
22 22
23 23 from IPython.config.configurable import Configurable
24 from IPython.core.quitter import Quitter
25 24
26 25 from IPython.utils.traitlets import Instance
27 26
28 27 #-----------------------------------------------------------------------------
29 28 # Classes and functions
30 29 #-----------------------------------------------------------------------------
31 30
32 31 class __BuiltinUndefined(object): pass
33 32 BuiltinUndefined = __BuiltinUndefined()
34 33
35 34 class __HideBuiltin(object): pass
36 35 HideBuiltin = __HideBuiltin()
37 36
38 37
39 38 class BuiltinTrap(Configurable):
40 39
41 40 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
42 41
43 42 def __init__(self, shell=None):
44 43 super(BuiltinTrap, self).__init__(shell=shell, config=None)
45 44 self._orig_builtins = {}
46 45 # We define this to track if a single BuiltinTrap is nested.
47 46 # Only turn off the trap when the outermost call to __exit__ is made.
48 47 self._nested_level = 0
49 48 self.shell = shell
50 49 # builtins we always add - if set to HideBuiltin, they will just
51 50 # be removed instead of being replaced by something else
52 51 self.auto_builtins = {'exit': HideBuiltin,
53 52 'quit': HideBuiltin,
54 53 'get_ipython': self.shell.get_ipython,
55 54 }
56 55 # Recursive reload function
57 56 try:
58 57 from IPython.lib import deepreload
59 58 if self.shell.deep_reload:
60 59 self.auto_builtins['reload'] = deepreload.reload
61 60 else:
62 61 self.auto_builtins['dreload']= deepreload.reload
63 62 except ImportError:
64 63 pass
65 64
66 65 def __enter__(self):
67 66 if self._nested_level == 0:
68 67 self.activate()
69 68 self._nested_level += 1
70 69 # I return self, so callers can use add_builtin in a with clause.
71 70 return self
72 71
73 72 def __exit__(self, type, value, traceback):
74 73 if self._nested_level == 1:
75 74 self.deactivate()
76 75 self._nested_level -= 1
77 76 # Returning False will cause exceptions to propagate
78 77 return False
79 78
80 79 def add_builtin(self, key, value):
81 80 """Add a builtin and save the original."""
82 81 bdict = __builtin__.__dict__
83 82 orig = bdict.get(key, BuiltinUndefined)
84 83 self._orig_builtins[key] = orig
85 84 if value is HideBuiltin:
86 85 del bdict[key]
87 86 else:
88 87 bdict[key] = value
89 88
90 89 def remove_builtin(self, key):
91 90 """Remove an added builtin and re-set the original."""
92 91 try:
93 92 orig = self._orig_builtins.pop(key)
94 93 except KeyError:
95 94 pass
96 95 else:
97 96 if orig is BuiltinUndefined:
98 97 del __builtin__.__dict__[key]
99 98 else:
100 99 __builtin__.__dict__[key] = orig
101 100
102 101 def activate(self):
103 102 """Store ipython references in the __builtin__ namespace."""
104 103
105 104 add_builtin = self.add_builtin
106 105 for name, func in self.auto_builtins.iteritems():
107 106 add_builtin(name, func)
108 107
109 108 # Keep in the builtins a flag for when IPython is active. We set it
110 109 # with setdefault so that multiple nested IPythons don't clobber one
111 110 # another.
112 111 __builtin__.__dict__.setdefault('__IPYTHON__active', 0)
113 112
114 113 def deactivate(self):
115 114 """Remove any builtins which might have been added by add_builtins, or
116 115 restore overwritten ones to their previous values."""
117 116 # Note: must iterate over a static keys() list because we'll be
118 117 # mutating the dict itself
119 118 remove_builtin = self.remove_builtin
120 119 for key in self._orig_builtins.keys():
121 120 remove_builtin(key)
122 121 self._orig_builtins.clear()
123 122 self._builtins_added = False
124 123 try:
125 124 del __builtin__.__dict__['__IPYTHON__active']
126 125 except KeyError:
127 126 pass
1 NO CONTENT: file renamed from IPython/core/quitter.py to IPython/deathrow/quitter.py
General Comments 0
You need to be logged in to leave comments. Login now