##// END OF EJS Templates
fixed bug introduced into builtin_trap due to 'quit' being removed twice - test suit now passes
Erik Tollerud -
Show More
@@ -1,126 +1,128 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 24
25 25 from IPython.utils.traitlets import Instance
26 26
27 27 #-----------------------------------------------------------------------------
28 28 # Classes and functions
29 29 #-----------------------------------------------------------------------------
30 30
31 31 class __BuiltinUndefined(object): pass
32 32 BuiltinUndefined = __BuiltinUndefined()
33 33
34 34 class __HideBuiltin(object): pass
35 35 HideBuiltin = __HideBuiltin()
36 36
37 37
38 38 class BuiltinTrap(Configurable):
39 39
40 40 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
41 41
42 42 def __init__(self, shell=None):
43 43 super(BuiltinTrap, self).__init__(shell=shell, config=None)
44 44 self._orig_builtins = {}
45 45 # We define this to track if a single BuiltinTrap is nested.
46 46 # Only turn off the trap when the outermost call to __exit__ is made.
47 47 self._nested_level = 0
48 48 self.shell = shell
49 49 # builtins we always add - if set to HideBuiltin, they will just
50 50 # be removed instead of being replaced by something else
51 51 self.auto_builtins = {'exit': HideBuiltin,
52 52 'quit': HideBuiltin,
53 53 'get_ipython': self.shell.get_ipython,
54 54 }
55 55 # Recursive reload function
56 56 try:
57 57 from IPython.lib import deepreload
58 58 if self.shell.deep_reload:
59 59 self.auto_builtins['reload'] = deepreload.reload
60 60 else:
61 61 self.auto_builtins['dreload']= deepreload.reload
62 62 except ImportError:
63 63 pass
64 64
65 65 def __enter__(self):
66 66 if self._nested_level == 0:
67 67 self.activate()
68 68 self._nested_level += 1
69 69 # I return self, so callers can use add_builtin in a with clause.
70 70 return self
71 71
72 72 def __exit__(self, type, value, traceback):
73 73 if self._nested_level == 1:
74 74 self.deactivate()
75 75 self._nested_level -= 1
76 76 # Returning False will cause exceptions to propagate
77 77 return False
78 78
79 79 def add_builtin(self, key, value):
80 80 """Add a builtin and save the original."""
81 81 bdict = __builtin__.__dict__
82 82 orig = bdict.get(key, BuiltinUndefined)
83 self._orig_builtins[key] = orig
84 83 if value is HideBuiltin:
85 del bdict[key]
84 if orig is not BuiltinUndefined: #same as 'key in bdict'
85 self._orig_builtins[key] = orig
86 del bdict[key]
86 87 else:
88 self._orig_builtins[key] = orig
87 89 bdict[key] = value
88 90
89 91 def remove_builtin(self, key):
90 92 """Remove an added builtin and re-set the original."""
91 93 try:
92 94 orig = self._orig_builtins.pop(key)
93 95 except KeyError:
94 96 pass
95 97 else:
96 98 if orig is BuiltinUndefined:
97 99 del __builtin__.__dict__[key]
98 100 else:
99 101 __builtin__.__dict__[key] = orig
100 102
101 103 def activate(self):
102 104 """Store ipython references in the __builtin__ namespace."""
103 105
104 106 add_builtin = self.add_builtin
105 107 for name, func in self.auto_builtins.iteritems():
106 108 add_builtin(name, func)
107 109
108 110 # Keep in the builtins a flag for when IPython is active. We set it
109 111 # with setdefault so that multiple nested IPythons don't clobber one
110 112 # another.
111 113 __builtin__.__dict__.setdefault('__IPYTHON__active', 0)
112 114
113 115 def deactivate(self):
114 116 """Remove any builtins which might have been added by add_builtins, or
115 117 restore overwritten ones to their previous values."""
116 118 # Note: must iterate over a static keys() list because we'll be
117 119 # mutating the dict itself
118 120 remove_builtin = self.remove_builtin
119 121 for key in self._orig_builtins.keys():
120 122 remove_builtin(key)
121 123 self._orig_builtins.clear()
122 124 self._builtins_added = False
123 125 try:
124 126 del __builtin__.__dict__['__IPYTHON__active']
125 127 except KeyError:
126 128 pass
General Comments 0
You need to be logged in to leave comments. Login now