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