You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# We define this to track if a single BuiltinTrap is nested.
# Only turn off the trap when the outermost call to __exit__ is made.
self._nested_level=0
self.shell=shell
# builtins we always add - if set to HideBuiltin, they will just
# be removed instead of being replaced by something else
self.auto_builtins= {'exit': HideBuiltin,
'quit': HideBuiltin,
'get_ipython': self.shell.get_ipython,
}
# Recursive reload function
try:
fromIPython.libimportdeepreload
ifself.shell.deep_reload:
fromwarningsimportwarn
warn("Automatically replacing builtin `reload` by `deepreload.reload` is deprecated and will be removed in IPython 6.0, please import `reload` explicitly from `IPython.lib.deeprelaod", DeprecationWarning)
self.auto_builtins['reload'] =deepreload._dreload
else:
self.auto_builtins['dreload']=deepreload._dreload
exceptImportError:
pass
def__enter__(self):
ifself._nested_level==0:
self.activate()
self._nested_level+=1
# I return self, so callers can use add_builtin in a with clause.
returnself
def__exit__(self, type, value, traceback):
ifself._nested_level==1:
self.deactivate()
self._nested_level-=1
# Returning False will cause exceptions to propagate
returnFalse
defadd_builtin(self, key, value):
"""Add a builtin and save the original."""
bdict=builtin_mod.__dict__
orig=bdict.get(key, BuiltinUndefined)
ifvalueisHideBuiltin:
iforigisnotBuiltinUndefined: #same as 'key in bdict'
self._orig_builtins[key] =orig
delbdict[key]
else:
self._orig_builtins[key] =orig
bdict[key] =value
defremove_builtin(self, key, orig):
"""Remove an added builtin and re-set the original."""
iforigisBuiltinUndefined:
delbuiltin_mod.__dict__[key]
else:
builtin_mod.__dict__[key] =orig
defactivate(self):
"""Store ipython references in the __builtin__ namespace."""
add_builtin=self.add_builtin
forname, funciniteritems(self.auto_builtins):
add_builtin(name, func)
defdeactivate(self):
"""Remove any builtins which might have been added by add_builtins, or
restore overwritten ones to their previous values."""