##// END OF EJS Templates
Merge pull request #11006 from Carreau/fix-ipython-spelling...
Merge pull request #11006 from Carreau/fix-ipython-spelling Fix IPython spelling

File last commit:

r23842:4189eb77
r24144:cc353b25 merge
Show More
embed.py
396 lines | 14.9 KiB | text/x-python | PythonLexer
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 # encoding: utf-8
"""
An embedded IPython shell.
"""
Paul Ivanov
fix IPython embed documentation...
r17134 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 import sys
Thomas Kluyver
Add global_ns argument to embedding, for backwards compatibility.
r5675 import warnings
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Bradley M. Froehle
embed(): Default to the future compile flags of the calling frame.
r7894 from IPython.core import ultratb, compilerop
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 from IPython.core import magic_arguments
Fernando Perez
Renamed @register_magics to @magics_class to avoid confusion....
r6973 from IPython.core.magic import Magics, magics_class, line_magic
Min RK
undeprecate terminal.interactiveshell...
r22549 from IPython.core.interactiveshell import DummyMod, InteractiveShell
from IPython.terminal.interactiveshell import TerminalInteractiveShell
Fernando Perez
Fix imports for plain terminal ipython and use a proper warn() call.
r11020 from IPython.terminal.ipapp import load_default_config
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Min RK
update dependency imports...
r21253 from traitlets import Bool, CBool, Unicode
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 from IPython.utils.io import ask_yes_no
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Jean Cruypenynck
Fix typo
r23804 class KillEmbedded(Exception):pass
Brian Granger
Massive refactoring of of the core....
r2245
Matthias Bussonnier
Reexpose a KillEmbeded alias for backward compat
r23842 # kept for backward compatibility as IPython 6 was released with
# the typo. See https://github.com/ipython/ipython/pull/10706
KillEmbeded = KillEmbedded
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 # This is an additional magic that is exposed in embedded shells.
Fernando Perez
Renamed @register_magics to @magics_class to avoid confusion....
r6973 @magics_class
Fernando Perez
Update embedded shell to new magics API.
r6941 class EmbeddedMagics(Magics):
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Fernando Perez
Update embedded shell to new magics API.
r6941 @line_magic
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 @magic_arguments.magic_arguments()
@magic_arguments.argument('-i', '--instance', action='store_true',
help='Kill instance instead of call location')
@magic_arguments.argument('-x', '--exit', action='store_true',
help='Also exit the current session')
@magic_arguments.argument('-y', '--yes', action='store_true',
help='Do not ask confirmation')
Fernando Perez
Update embedded shell to new magics API.
r6941 def kill_embedded(self, parameter_s=''):
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 """%kill_embedded : deactivate for good the current embedded IPython
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update embedded shell to new magics API.
r6941 This function (after asking for confirmation) sets an internal flag so
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 that an embedded IPython will never activate again for the given call
location. This is useful to permanently disable a shell that is being
called inside a loop: once you've figured out what you needed from it,
you may then kill it and the program will then continue to run without
the interactive shell interfering again.
Matthias Bussonnier
Fix a couple of warnings/errors in doc builds.
r23477 Kill Instance Option:
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257
Matthias Bussonnier
Fix a couple of warnings/errors in doc builds.
r23477 If for some reasons you need to kill the location where the instance
is created and not called, for example if you create a single
instance in one place and debug in many locations, you can use the
``--instance`` option to kill this specific instance. Like for the
``call location`` killing an "instance" should work even if it is
recreated within a loop.
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257
.. note::
This was the default behavior before IPython 5.2
Fernando Perez
Update embedded shell to new magics API.
r6941 """
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 args = magic_arguments.parse_argstring(self.kill_embedded, parameter_s)
print(args)
if args.instance:
# let no ask
if not args.yes:
kill = ask_yes_no(
"Are you sure you want to kill this embedded instance? [y/N] ", 'n')
else:
kill = True
if kill:
self.shell._disable_init_location()
print("This embedded IPython instance will not reactivate anymore "
"once you exit.")
else:
if not args.yes:
kill = ask_yes_no(
"Are you sure you want to kill this embedded call_location? [y/N] ", 'n')
else:
kill = True
if kill:
self.shell.embedded_active = False
print("This embedded IPython call location will not reactivate anymore "
"once you exit.")
if args.exit:
# Ask-exit does not really ask, it just set internals flags to exit
# on next loop.
self.shell.ask_exit()
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Matthias Bussonnier
Refactor to exit and raise immediately on exit....
r21987
@line_magic
def exit_raise(self, parameter_s=''):
"""%exit_raise Make the current embedded kernel exit and raise and exception.
Matthias Bussonnier
Update docs and error message
r21998 This function sets an internal flag so that an embedded IPython will
Jean Cruypenynck
Fix typo
r23804 raise a `IPython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is
Matthias Bussonnier
Update docs and error message
r21998 useful to permanently exit a loop that create IPython embed instance.
Matthias Bussonnier
Refactor to exit and raise immediately on exit....
r21987 """
self.shell.should_raise = True
self.shell.ask_exit()
Matthias Bussonnier
Allow Embeded instances to raise on exit....
r21960
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Brian Granger
Complete reorganization of InteractiveShell....
r2761 class InteractiveShellEmbed(TerminalInteractiveShell):
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
dummy_mode = Bool(False)
MinRK
fix at least some command-line unicode options
r3464 exit_msg = Unicode('')
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 embedded = CBool(True)
Matthias Bussonnier
Allow Embeded instances to raise on exit....
r21960 should_raise = CBool(False)
Brian Granger
Work on startup related things....
r2252 # Like the base class display_banner is not configurable, but here it
# is True by default.
display_banner = CBool(True)
MinRK
move banner to base InteractiveShell class
r16581 exit_msg = Unicode()
Matthias Bussonnier
Restore `%kill_embedded` for multiple IPython embed call....
r22294
Thomas Kluyver
Don't change terminal title for embedded IPython...
r22715 # When embedding, by default we don't change the terminal title
term_title = Bool(False,
help="Automatically set the terminal title"
).tag(config=True)
Matthias Bussonnier
Restore `%kill_embedded` for multiple IPython embed call....
r22294 _inactive_locations = set()
@property
def embedded_active(self):
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 return (self._call_location_id not in InteractiveShellEmbed._inactive_locations)\
and (self._init_location_id not in InteractiveShellEmbed._inactive_locations)
def _disable_init_location(self):
"""Disable the current Instance creation location"""
InteractiveShellEmbed._inactive_locations.add(self._init_location_id)
Matthias Bussonnier
Restore `%kill_embedded` for multiple IPython embed call....
r22294
@embedded_active.setter
def embedded_active(self, value):
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 if value:
InteractiveShellEmbed._inactive_locations.discard(
self._call_location_id)
InteractiveShellEmbed._inactive_locations.discard(
self._init_location_id)
Matthias Bussonnier
Restore `%kill_embedded` for multiple IPython embed call....
r22294 else:
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 InteractiveShellEmbed._inactive_locations.add(
self._call_location_id)
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
MinRK
move banner to base InteractiveShell class
r16581 def __init__(self, **kw):
if kw.get('user_global_ns', None) is not None:
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 raise DeprecationWarning(
"Key word argument `user_global_ns` has been replaced by `user_module` since IPython 4.0.")
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 clid = kw.pop('_init_location_id', None)
if not clid:
frame = sys._getframe(1)
clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
self._init_location_id = clid
Matthias Bussonnier
Restore `%kill_embedded` for multiple IPython embed call....
r22294
MinRK
move banner to base InteractiveShell class
r16581 super(InteractiveShellEmbed,self).__init__(**kw)
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
# don't use the ipython crash handler so that user exceptions aren't
# trapped
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
mode=self.xmode,
call_pdb=self.pdb)
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 def init_sys_modules(self):
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 """
Thomas Kluyver
Fix a couple of rst roles causing Sphinx warnings
r23428 Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing.
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 """
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 pass
Fernando Perez
Update embedded shell to new magics API.
r6941 def init_magics(self):
super(InteractiveShellEmbed, self).init_magics()
self.register_magics(EmbeddedMagics)
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459 def __call__(self, header='', local_ns=None, module=None, dummy=None,
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 stack_depth=1, global_ns=None, compile_flags=None, **kw):
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 """Activate the interactive interpreter.
Thomas Kluyver
Update docs about embedding.
r5684 __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 the interpreter shell with the given local and global namespaces, and
optionally print a header string at startup.
The shell can be globally activated/deactivated using the
Thomas Kluyver
Update docs about embedding.
r5684 dummy_mode attribute. This allows you to turn off a shell used
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 for debugging globally.
However, *each* time you call the shell you can override the current
state of dummy_mode with the optional keyword parameter 'dummy'. For
Thomas Kluyver
Update docs about embedding.
r5684 example, if you set dummy mode on with IPShell.dummy_mode = True, you
can still have a specific call work by making it as IPShell(dummy=False).
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 """
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 # we are called, set the underlying interactiveshell not to exit.
self.keep_running = True
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 # If the user has turned it off, go away
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 clid = kw.pop('_call_location_id', None)
if not clid:
frame = sys._getframe(1)
clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
self._call_location_id = clid
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 if not self.embedded_active:
return
# Normal exits from interactive mode set this flag, so the shell can't
# re-enter (it checks this variable at the start of interactive mode).
self.exit_now = False
# Allow the dummy parameter to override the global __dummy_mode
if dummy or (dummy != 0 and self.dummy_mode):
return
Brian Granger
Work on startup related things....
r2252 # self.banner is auto computed
if header:
self.old_banner2 = self.banner2
self.banner2 = self.banner2 + '\n' + header + '\n'
Fernando Perez
Fix small error on exit from embedded shells.
r2583 else:
self.old_banner2 = ''
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Thomas Kluyver
Switch embedded IPython over to prompt_toolkit shell...
r22229 if self.display_banner:
self.show_banner()
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206 # Call the embedding code with a stack depth of 1 so it can skip over
# our call and get the original caller's namespaces.
Bradley M. Froehle
Allow embed(..., compile_flags=...)
r7895 self.mainloop(local_ns, module, stack_depth=stack_depth,
global_ns=global_ns, compile_flags=compile_flags)
Brian Granger
Work on startup related things....
r2252
self.banner2 = self.old_banner2
Brian Granger
Removed shell.py entirely and made the embedded shell a proper subclass....
r2206
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 if self.exit_msg is not None:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print(self.exit_msg)
Brian Granger
sys.displayhook is now managed dynamically by display_trap.
r2231
Matthias Bussonnier
Allow Embeded instances to raise on exit....
r21960 if self.should_raise:
Jean Cruypenynck
Fix typo
r23804 raise KillEmbedded('Embedded IPython raising error, as user requested.')
Matthias Bussonnier
Allow Embeded instances to raise on exit....
r21960
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459 def mainloop(self, local_ns=None, module=None, stack_depth=0,
Bradley M. Froehle
embed(): Default to the future compile flags of the calling frame.
r7894 display_banner=None, global_ns=None, compile_flags=None):
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 """Embeds IPython into a running python program.
Thomas Kluyver
Clean up numpydoc section headers
r13587 Parameters
----------
local_ns, module
Working local namespace (a dict) and module (a module or similar
object). If given as None, they are automatically taken from the scope
where the shell was called, so that program variables become visible.
stack_depth : int
How many levels in the stack to go to looking for namespaces (when
local_ns or module is None). This allows an intermediate caller to
make sure that this function gets the namespace from the intended
level in the stack. By default (0) it will get its locals and globals
from the immediate caller.
compile_flags
A bit field identifying the __future__ features
that are enabled, as passed to the builtin :func:`compile` function.
If given as None, they are automatically taken from the scope where
the shell was called.
"""
Thomas Kluyver
Add global_ns argument to embedding, for backwards compatibility.
r5675
if (global_ns is not None) and (module is None):
Matthias Bussonnier
Turn marked deprecation warnings into errors for 5.0
r22405 raise DeprecationWarning("'global_ns' keyword argument is deprecated, and has been removed in IPython 5.0 use `module` keyword argument instead.")
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Thomas Kluyver
Switch embedded IPython over to prompt_toolkit shell...
r22229 if (display_banner is not None):
Matthias Bussonnier
Turn marked deprecation warnings into errors for 5.0
r22405 warnings.warn("The display_banner parameter is deprecated since IPython 4.0", DeprecationWarning)
Thomas Kluyver
Switch embedded IPython over to prompt_toolkit shell...
r22229
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 # Get locals and globals from caller
Bradley M. Froehle
Fix regression in embed() from pull-request #2096....
r8130 if ((local_ns is None or module is None or compile_flags is None)
and self.default_user_namespaces):
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 call_frame = sys._getframe(stack_depth).f_back
Thomas Kluyver
Simplify logic for deciding when to auto-detect local namespace and module.
r5669 if local_ns is None:
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 local_ns = call_frame.f_locals
Thomas Kluyver
Simplify logic for deciding when to auto-detect local namespace and module.
r5669 if module is None:
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 global_ns = call_frame.f_globals
Min RK
handle failure to get module for globals...
r22235 try:
module = sys.modules[global_ns['__name__']]
except KeyError:
warnings.warn("Failed to get module %s" % \
global_ns.get('__name__', 'unknown module')
)
module = DummyMod()
module.__dict__ = global_ns
Bradley M. Froehle
embed(): Default to the future compile flags of the calling frame.
r7894 if compile_flags is None:
compile_flags = (call_frame.f_code.co_flags &
compilerop.PyCF_MASK)
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459
# Save original namespace and module so we can restore them after
# embedding; otherwise the shell doesn't shut down correctly.
orig_user_module = self.user_module
orig_user_ns = self.user_ns
Bradley M. Froehle
embed(): Default to the future compile flags of the calling frame.
r7894 orig_compile_flags = self.compile.flags
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 # Update namespaces and fire up interpreter
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 # The global one is easy, we can just throw it in
Thomas Kluyver
Don't override passed user_ns in embed()
r5667 if module is not None:
self.user_module = module
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459 # But the user/local one is tricky: ipython needs it to store internal
# data, but we also need the locals. We'll throw our hidden variables
# like _ih and get_ipython() into the local namespace, but delete them
# later.
Thomas Kluyver
Don't override passed user_ns in embed()
r5667 if local_ns is not None:
Jason Newton
Make embed nestable...
r18762 reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
self.user_ns = reentrant_local_ns
Thomas Kluyver
Don't override passed user_ns in embed()
r5667 self.init_user_ns()
Brian Granger
Created context manager for the things injected into __builtin__.
r2227
Bradley M. Froehle
embed(): Default to the future compile flags of the calling frame.
r7894 # Compiler flags
Bradley M. Froehle
Fix regression in embed() from pull-request #2096....
r8130 if compile_flags is not None:
self.compile.flags = compile_flags
Bradley M. Froehle
embed(): Default to the future compile flags of the calling frame.
r7894
Brian Granger
Created context manager for the things injected into __builtin__.
r2227 # make sure the tab-completer has the correct frame information, so it
# actually completes using the frame's locals/globals
self.set_completer_frame()
Mike McKerns
Revert "converted multi-context with-statements to nested (2.6 compatibility)"...
r12471 with self.builtin_trap, self.display_trap:
Thomas Kluyver
Switch embedded IPython over to prompt_toolkit shell...
r22229 self.interact()
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459
# now, purge out the local namespace of IPython's hidden variables.
Thomas Kluyver
Don't override passed user_ns in embed()
r5667 if local_ns is not None:
Jason Newton
Make embed nestable...
r18762 local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})
Thomas Kluyver
Fix embedding terminal IPython with new namespace model.
r5459
# Restore original namespace so shell can shut down when we exit.
self.user_module = orig_user_module
self.user_ns = orig_user_ns
Bradley M. Froehle
embed(): Default to the future compile flags of the calling frame.
r7894 self.compile.flags = orig_compile_flags
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Brian Granger
Complete reorganization of InteractiveShell....
r2761 def embed(**kwargs):
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 """Call this to embed IPython at the current point in your program.
The first invocation of this will create an :class:`InteractiveShellEmbed`
instance and then call it. Consecutive calls just call the already
created instance.
MinRK
expand start_ipython / embed docstrings...
r11174 If you don't want the kernel to initialize the namespace
from the scope of the surrounding function,
and/or you want to load full IPython configuration,
you probably want `IPython.start_ipython()` instead.
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 Here is a simple example::
from IPython import embed
a = 10
b = 20
Paul Ivanov
fix IPython embed documentation...
r17134 embed(header='First time')
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 c = 30
d = 40
Paul Ivanov
fix IPython embed documentation...
r17134 embed()
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
MinRK
use Singleton.instance() for embed() instead of manual global...
r8385 Full customization can be done by passing a :class:`Config` in as the
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226 config argument.
"""
Brian Granger
Complete reorganization of InteractiveShell....
r2761 config = kwargs.get('config')
header = kwargs.pop('header', u'')
Bradley M. Froehle
Allow embed(..., compile_flags=...)
r7895 compile_flags = kwargs.pop('compile_flags', None)
Brian Granger
Massive refactoring of of the core....
r2245 if config is None:
config = load_default_config()
Brian Granger
Complete reorganization of InteractiveShell....
r2761 config.InteractiveShellEmbed = config.TerminalInteractiveShell
muzuiget
embed() doesn't load default config...
r3914 kwargs['config'] = config
Jason Newton
save and restore ps1/ps2
r18763 #save ps1/ps2 if defined
ps1 = None
ps2 = None
try:
ps1 = sys.ps1
ps2 = sys.ps2
except AttributeError:
pass
Jason Newton
Make embed nestable...
r18762 #save previous instance
saved_shell_instance = InteractiveShell._instance
if saved_shell_instance is not None:
cls = type(saved_shell_instance)
cls.clear_instance()
Matthias Bussonnier
Restore `%kill_embedded` for multiple IPython embed call....
r22294 frame = sys._getframe(1)
Matthias Bussonnier
Fix deactivation of embedded instance....
r23257 shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % (
frame.f_code.co_filename, frame.f_lineno), **kwargs)
shell(header=header, stack_depth=2, compile_flags=compile_flags,
_call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
Jason Newton
Make embed nestable...
r18762 InteractiveShellEmbed.clear_instance()
#restore previous instance
if saved_shell_instance is not None:
cls = type(saved_shell_instance)
cls.clear_instance()
for subclass in cls._walk_mro():
subclass._instance = saved_shell_instance
Jason Newton
save and restore ps1/ps2
r18763 if ps1 is not None:
sys.ps1 = ps1
sys.ps2 = ps2