##// END OF EJS Templates
Merge pull request #1059 from fperez/__IPYTHON__...
Fernando Perez -
r5572:487b6b96 merge
parent child Browse files
Show More
@@ -88,17 +88,12 b' class BuiltinTrap(Configurable):'
88 88 self._orig_builtins[key] = orig
89 89 bdict[key] = value
90 90
91 def remove_builtin(self, key):
91 def remove_builtin(self, key, orig):
92 92 """Remove an added builtin and re-set the original."""
93 try:
94 orig = self._orig_builtins.pop(key)
95 except KeyError:
96 pass
93 if orig is BuiltinUndefined:
94 del __builtin__.__dict__[key]
97 95 else:
98 if orig is BuiltinUndefined:
99 del __builtin__.__dict__[key]
100 else:
101 __builtin__.__dict__[key] = orig
96 __builtin__.__dict__[key] = orig
102 97
103 98 def activate(self):
104 99 """Store ipython references in the __builtin__ namespace."""
@@ -107,22 +102,11 b' class BuiltinTrap(Configurable):'
107 102 for name, func in self.auto_builtins.iteritems():
108 103 add_builtin(name, func)
109 104
110 # Keep in the builtins a flag for when IPython is active. We set it
111 # with setdefault so that multiple nested IPythons don't clobber one
112 # another.
113 __builtin__.__dict__.setdefault('__IPYTHON__active', 0)
114
115 105 def deactivate(self):
116 106 """Remove any builtins which might have been added by add_builtins, or
117 107 restore overwritten ones to their previous values."""
118 # Note: must iterate over a static keys() list because we'll be
119 # mutating the dict itself
120 108 remove_builtin = self.remove_builtin
121 for key in self._orig_builtins.keys():
122 remove_builtin(key)
109 for key, val in self._orig_builtins.iteritems():
110 remove_builtin(key, val)
123 111 self._orig_builtins.clear()
124 112 self._builtins_added = False
125 try:
126 del __builtin__.__dict__['__IPYTHON__active']
127 except KeyError:
128 pass
@@ -605,6 +605,20 b' class InteractiveShell(SingletonConfigurable, Magic):'
605 605 self.magic_logstart()
606 606
607 607 def init_builtins(self):
608 # A single, static flag that we set to True. Its presence indicates
609 # that an IPython shell has been created, and we make no attempts at
610 # removing on exit or representing the existence of more than one
611 # IPython at a time.
612 builtin_mod.__dict__['__IPYTHON__'] = True
613
614 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
615 # manage on enter/exit, but with all our shells it's virtually
616 # impossible to get all the cases right. We're leaving the name in for
617 # those who adapted their codes to check for this flag, but will
618 # eventually remove it after a few more releases.
619 builtin_mod.__dict__['__IPYTHON__active'] = \
620 'Deprecated, check for __IPYTHON__'
621
608 622 self.builtin_trap = BuiltinTrap(shell=self)
609 623
610 624 def init_inspector(self):
@@ -271,3 +271,8 b' class TestSystemRaw(unittest.TestCase):'
271 271 """
272 272 cmd = ur'''python -c "'åäö'" '''
273 273 _ip.shell.system_raw(cmd)
274
275
276 def test__IPYTHON__():
277 # This shouldn't raise a NameError, that's all
278 __IPYTHON__
@@ -341,9 +341,6 b' class TerminalInteractiveShell(InteractiveShell):'
341 341
342 342 more = False
343 343
344 # Mark activity in the builtins
345 __builtin__.__dict__['__IPYTHON__active'] += 1
346
347 344 if self.has_readline:
348 345 self.readline_startup_hook(self.pre_readline)
349 346 hlen_b4_cell = self.readline.get_current_history_length()
@@ -413,9 +410,6 b' class TerminalInteractiveShell(InteractiveShell):'
413 410 hlen_b4_cell = \
414 411 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
415 412
416 # We are off again...
417 __builtin__.__dict__['__IPYTHON__active'] -= 1
418
419 413 # Turn off the exit flag, so the mainloop can be restarted if desired
420 414 self.exit_now = False
421 415
General Comments 0
You need to be logged in to leave comments. Login now