##// END OF EJS Templates
Override terminal size in doctests to standardise traceback format...
Override terminal size in doctests to standardise traceback format Should fix test failures on Jenkins

File last commit:

r21889:0cb1da81
r22157:ca5c9233
Show More
builtin_trap.py
114 lines | 4.2 KiB | text/x-python | PythonLexer
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 """
A context manager for managing things injected into :mod:`__builtin__`.
Authors:
* Brian Granger
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 * Fernando Perez
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 """
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2010-2011 The IPython Development Team.
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 #
# Distributed under the terms of the BSD License.
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 #
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 # Complete license in the file COPYING.txt, distributed with this software.
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 #-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Min RK
update dependency imports...
r21253 from traitlets.config.configurable import Configurable
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 from IPython.utils.py3compat import builtin_mod, iteritems
Min RK
update dependency imports...
r21253 from traitlets import Instance
Brian Granger
A number of changes to how traitlets and components work....
r2229
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 #-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
Fernando Perez
Small fixes so the docs build....
r2404 class __BuiltinUndefined(object): pass
BuiltinUndefined = __BuiltinUndefined()
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 class __HideBuiltin(object): pass
HideBuiltin = __HideBuiltin()
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Brian Granger
First draft of refactored Component->Configurable.
r2731 class BuiltinTrap(Configurable):
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Sylvain Corlay
allow_none=False by default for Type and Instance
r20940 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
allow_none=True)
Brian Granger
First draft of refactored Component->Configurable.
r2731
Brian Granger
Adding support for HasTraits to take keyword arguments.
r2740 def __init__(self, shell=None):
super(BuiltinTrap, self).__init__(shell=shell, config=None)
Brian Granger
More work on refactoring things into components....
r2244 self._orig_builtins = {}
Brian Granger
Massive refactoring of of the core....
r2245 # 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
Brian Granger
First draft of refactored Component->Configurable.
r2731 self.shell = shell
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 # 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,
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 'get_ipython': self.shell.get_ipython,
}
# Recursive reload function
try:
from IPython.lib import deepreload
if self.shell.deep_reload:
Matthias Bussonnier
deprecate auto-injection of deep-reload
r21407 from warnings import warn
Matthias Bussonnier
Explicits deprecation warnings removal versions....
r21889 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)
Matthias Bussonnier
deprecate auto-injection of deep-reload
r21407 self.auto_builtins['reload'] = deepreload._dreload
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 else:
Matthias Bussonnier
deprecate auto-injection of deep-reload
r21407 self.auto_builtins['dreload']= deepreload._dreload
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 except ImportError:
pass
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
def __enter__(self):
Brian Granger
Massive refactoring of of the core....
r2245 if self._nested_level == 0:
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 self.activate()
Brian Granger
Massive refactoring of of the core....
r2245 self._nested_level += 1
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 # I return self, so callers can use add_builtin in a with clause.
return self
def __exit__(self, type, value, traceback):
Brian Granger
Massive refactoring of of the core....
r2245 if self._nested_level == 1:
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 self.deactivate()
Brian Granger
Massive refactoring of of the core....
r2245 self._nested_level -= 1
Brian Granger
The pretty.py extension has been ported to the new extension API....
r2281 # Returning False will cause exceptions to propagate
return False
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
def add_builtin(self, key, value):
"""Add a builtin and save the original."""
Thomas Kluyver
Fix imports of builtins module
r13351 bdict = builtin_mod.__dict__
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 orig = bdict.get(key, BuiltinUndefined)
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 if value is HideBuiltin:
Erik Tollerud
fixed bug introduced into builtin_trap due to 'quit' being removed twice - test suit now passes
r3191 if orig is not BuiltinUndefined: #same as 'key in bdict'
self._orig_builtins[key] = orig
del bdict[key]
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 else:
Erik Tollerud
fixed bug introduced into builtin_trap due to 'quit' being removed twice - test suit now passes
r3191 self._orig_builtins[key] = orig
Erik Tollerud
__builtin__.exit and quit are now hidden - exit magic is now the only exit command
r3185 bdict[key] = value
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Fernando Perez
Simplify logic (semantics unchanged) in builtins trap.
r5491 def remove_builtin(self, key, orig):
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 """Remove an added builtin and re-set the original."""
Fernando Perez
Simplify logic (semantics unchanged) in builtins trap.
r5491 if orig is BuiltinUndefined:
Thomas Kluyver
Fix imports of builtins module
r13351 del builtin_mod.__dict__[key]
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 else:
Thomas Kluyver
Fix imports of builtins module
r13351 builtin_mod.__dict__[key] = orig
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 def activate(self):
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 """Store ipython references in the __builtin__ namespace."""
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 add_builtin = self.add_builtin
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 for name, func in iteritems(self.auto_builtins):
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 add_builtin(name, func)
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 def deactivate(self):
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 """Remove any builtins which might have been added by add_builtins, or
restore overwritten ones to their previous values."""
Fernando Perez
Speedup builtin_trap enter/exit by reducing object creation....
r2957 remove_builtin = self.remove_builtin
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 for key, val in iteritems(self._orig_builtins):
Fernando Perez
Simplify logic (semantics unchanged) in builtins trap.
r5491 remove_builtin(key, val)
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 self._orig_builtins.clear()
self._builtins_added = False