interactiveshell.py
3217 lines
| 128.3 KiB
| text/x-python
|
PythonLexer
|
r1032 | # -*- coding: utf-8 -*- | ||
|
r2731 | """Main IPython class.""" | ||
|
r1032 | |||
|
r2202 | #----------------------------------------------------------------------------- | ||
# Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | ||||
# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | ||||
|
r3297 | # Copyright (C) 2008-2011 The IPython Development Team | ||
|
r1032 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
|
r2202 | #----------------------------------------------------------------------------- | ||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r2421 | from __future__ import absolute_import | ||
|
r7817 | from __future__ import print_function | ||
|
r2227 | |||
|
r2927 | import __future__ | ||
|
r2738 | import abc | ||
|
r3527 | import ast | ||
|
r2953 | import atexit | ||
|
r12297 | import functools | ||
|
r1032 | import os | ||
import re | ||||
|
r6029 | import runpy | ||
|
r1032 | import sys | ||
import tempfile | ||||
|
r3158 | import types | ||
|
r12217 | import subprocess | ||
|
r8372 | from io import open as io_open | ||
|
r1032 | |||
|
r3793 | from IPython.config.configurable import SingletonConfigurable | ||
|
r2037 | from IPython.core import debugger, oinspect | ||
|
r6923 | from IPython.core import magic | ||
|
r2876 | from IPython.core import page | ||
|
r2202 | from IPython.core import prefilter | ||
|
r2363 | from IPython.core import shadowns | ||
from IPython.core import ultratb | ||||
|
r3822 | from IPython.core.alias import AliasManager, AliasError | ||
|
r3721 | from IPython.core.autocall import ExitAutocall | ||
|
r2227 | from IPython.core.builtin_trap import BuiltinTrap | ||
|
r15605 | from IPython.core.events import EventManager, available_events | ||
|
r9140 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython | ||
|
r2231 | from IPython.core.display_trap import DisplayTrap | ||
|
r2838 | from IPython.core.displayhook import DisplayHook | ||
|
r3277 | from IPython.core.displaypub import DisplayPublisher | ||
|
r6776 | from IPython.core.error import UsageError | ||
|
r2731 | from IPython.core.extensions import ExtensionManager | ||
|
r3278 | from IPython.core.formatters import DisplayFormatter | ||
|
r3122 | from IPython.core.history import HistoryManager | ||
|
r7437 | from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2 | ||
|
r2032 | from IPython.core.logger import Logger | ||
|
r3509 | from IPython.core.macro import Macro | ||
|
r2812 | from IPython.core.payload import PayloadManager | ||
|
r7437 | from IPython.core.prefilter import PrefilterManager | ||
|
r4024 | from IPython.core.profiledir import ProfileDir | ||
|
r5495 | from IPython.core.prompts import PromptManager | ||
|
r7861 | from IPython.lib.latextools import LaTeXTool | ||
|
r9246 | from IPython.testing.skipdoctest import skip_doctest | ||
|
r2202 | from IPython.utils import PyColorize | ||
|
r2838 | from IPython.utils import io | ||
|
r4731 | from IPython.utils import py3compat | ||
|
r6761 | from IPython.utils import openpy | ||
|
r8793 | from IPython.utils.decorators import undoc | ||
|
r6776 | from IPython.utils.io import ask_yes_no | ||
|
r2363 | from IPython.utils.ipstruct import Struct | ||
|
r6776 | from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename | ||
|
r3388 | from IPython.utils.pickleshare import PickleShareDB | ||
|
r2908 | from IPython.utils.process import system, getoutput | ||
|
r13359 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, | ||
|
r13361 | with_metaclass, iteritems) | ||
|
r2252 | from IPython.utils.strdispatch import StrDispatch | ||
from IPython.utils.syspathcontext import prepended_to_syspath | ||||
|
r6776 | from IPython.utils.text import (format_screen, LSString, SList, | ||
|
r5355 | DollarFormatter) | ||
|
r5344 | from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, | ||
|
r2838 | List, Unicode, Instance, Type) | ||
|
r6776 | from IPython.utils.warn import warn, error | ||
|
r2838 | import IPython.core.hooks | ||
|
r2202 | |||
#----------------------------------------------------------------------------- | ||||
|
r1032 | # Globals | ||
|
r2202 | #----------------------------------------------------------------------------- | ||
|
r1032 | # compiled regexps for autoindent management | ||
dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | ||||
|
r2202 | #----------------------------------------------------------------------------- | ||
# Utilities | ||||
#----------------------------------------------------------------------------- | ||||
|
r8793 | @undoc | ||
|
r1032 | def softspace(file, newvalue): | ||
"""Copied from code.py, to remove the dependency""" | ||||
oldvalue = 0 | ||||
try: | ||||
oldvalue = file.softspace | ||||
except AttributeError: | ||||
pass | ||||
try: | ||||
file.softspace = newvalue | ||||
except (AttributeError, TypeError): | ||||
# "attribute-less object" or "read-only attributes" | ||||
pass | ||||
return oldvalue | ||||
|
r8793 | @undoc | ||
|
r2377 | def no_op(*a, **kw): pass | ||
|
r8793 | @undoc | ||
|
r4816 | class NoOpContext(object): | ||
def __enter__(self): pass | ||||
def __exit__(self, type, value, traceback): pass | ||||
no_op_context = NoOpContext() | ||||
|
r3158 | class SpaceInInput(Exception): pass | ||
|
r1032 | |||
|
r8793 | @undoc | ||
|
r1032 | class Bunch: pass | ||
|
r2226 | |||
|
r2319 | def get_default_colors(): | ||
if sys.platform=='darwin': | ||||
return "LightBG" | ||||
elif os.name=='nt': | ||||
return 'Linux' | ||||
else: | ||||
return 'Linux' | ||||
|
r4046 | class SeparateUnicode(Unicode): | ||
|
r13597 | r"""A Unicode subclass to validate separate_in, separate_out, etc. | ||
|
r2766 | |||
|
r13597 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. | ||
|
r2766 | """ | ||
def validate(self, obj, value): | ||||
if value == '0': value = '' | ||||
value = value.replace('\\n','\n') | ||||
|
r4046 | return super(SeparateUnicode, self).validate(obj, value) | ||
|
r2766 | |||
|
r3793 | |||
|
r3474 | class ReadlineNoRecord(object): | ||
"""Context manager to execute some code, then reload readline history | ||||
so that interactive input to the code doesn't appear when pressing up.""" | ||||
def __init__(self, shell): | ||||
self.shell = shell | ||||
self._nested_level = 0 | ||||
|
r4872 | |||
|
r3474 | def __enter__(self): | ||
|
r3481 | if self._nested_level == 0: | ||
|
r3760 | try: | ||
self.orig_length = self.current_length() | ||||
self.readline_tail = self.get_readline_tail() | ||||
except (AttributeError, IndexError): # Can fail with pyreadline | ||||
self.orig_length, self.readline_tail = 999999, [] | ||||
|
r3474 | self._nested_level += 1 | ||
|
r4872 | |||
|
r3474 | def __exit__(self, type, value, traceback): | ||
self._nested_level -= 1 | ||||
if self._nested_level == 0: | ||||
|
r3496 | # Try clipping the end if it's got longer | ||
|
r3760 | try: | ||
e = self.current_length() - self.orig_length | ||||
if e > 0: | ||||
for _ in range(e): | ||||
self.shell.readline.remove_history_item(self.orig_length) | ||||
|
r4872 | |||
|
r3760 | # If it still doesn't match, just reload readline history. | ||
if self.current_length() != self.orig_length \ | ||||
or self.get_readline_tail() != self.readline_tail: | ||||
self.shell.refill_readline_hist() | ||||
except (AttributeError, IndexError): | ||||
pass | ||||
|
r3474 | # Returning False will cause exceptions to propagate | ||
return False | ||||
|
r4872 | |||
|
r3496 | def current_length(self): | ||
return self.shell.readline.get_current_history_length() | ||||
|
r4872 | |||
|
r3481 | def get_readline_tail(self, n=10): | ||
"""Get the last n items in readline history.""" | ||||
end = self.shell.readline.get_current_history_length() + 1 | ||||
start = max(end-n, 1) | ||||
ghi = self.shell.readline.get_history_item | ||||
return [ghi(x) for x in range(start, end)] | ||||
|
r2799 | |||
|
r12470 | |||
|
r12491 | @undoc | ||
|
r12470 | class DummyMod(object): | ||
|
r12491 | """A dummy module used for IPython's interactive module when | ||
a namespace must be assigned to the module's __dict__.""" | ||||
|
r12470 | pass | ||
|
r2202 | #----------------------------------------------------------------------------- | ||
|
r1032 | # Main IPython class | ||
|
r2202 | #----------------------------------------------------------------------------- | ||
|
r1032 | |||
|
r6906 | class InteractiveShell(SingletonConfigurable): | ||
|
r2226 | """An enhanced, interactive shell for Python.""" | ||
|
r1032 | |||
|
r2802 | _instance = None | ||
|
r8220 | |||
ast_transformers = List([], config=True, help= | ||||
""" | ||||
A list of ast.NodeTransformer subclass instances, which will be applied | ||||
to user input before code is run. | ||||
""" | ||||
) | ||||
|
r3788 | |||
|
r5624 | autocall = Enum((0,1,2), default_value=0, config=True, help= | ||
|
r3788 | """ | ||
Make IPython automatically call any callable object even if you didn't | ||||
type explicit parentheses. For example, 'str 43' becomes 'str(43)' | ||||
automatically. The value can be '0' to disable the feature, '1' for | ||||
'smart' autocall, where it is not applied if there are no more | ||||
|
r3787 | arguments on the line, and '2' for 'full' autocall, where all callable | ||
objects are automatically called (even if no arguments are present). | ||||
|
r3788 | """ | ||
|
r3787 | ) | ||
|
r2766 | # TODO: remove all autoindent logic and put into frontends. | ||
# We can't do this yet because even runlines uses the autoindent. | ||||
|
r3788 | autoindent = CBool(True, config=True, help= | ||
""" | ||||
|
r3789 | Autoindent IPython code entered interactively. | ||
|
r3788 | """ | ||
) | ||||
automagic = CBool(True, config=True, help= | ||||
""" | ||||
Enable magic commands to be called without the leading %. | ||||
""" | ||||
) | ||||
|
r5344 | cache_size = Integer(1000, config=True, help= | ||
|
r3789 | """ | ||
Set the size of the output cache. The default is 1000, you can | ||||
change it permanently in your config file. Setting it to 0 completely | ||||
disables the caching system, and the minimum value accepted is 20 (if | ||||
you provide a value less than 20, it is reset to 0 and a warning is | ||||
issued). This limit is defined because otherwise you'll spend more | ||||
time re-flushing a too small cache than working | ||||
""" | ||||
) | ||||
color_info = CBool(True, config=True, help= | ||||
""" | ||||
Use colors for displaying information about objects. Because this | ||||
information is passed through a pager (like 'less'), and some pagers | ||||
get confused with color codes, this capability can be turned off. | ||||
""" | ||||
) | ||||
|
r4872 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | ||
|
r3963 | default_value=get_default_colors(), config=True, | ||
|
r3967 | help="Set the color scheme (NoColor, Linux, or LightBG)." | ||
|
r3963 | ) | ||
|
r4816 | colors_force = CBool(False, help= | ||
""" | ||||
Force use of ANSI color codes, regardless of OS and readline | ||||
availability. | ||||
""" | ||||
# FIXME: This is essentially a hack to allow ZMQShell to show colors | ||||
# without readline on Win32. When the ZMQ formatting system is | ||||
# refactored, this should be removed. | ||||
) | ||||
|
r2245 | debug = CBool(False, config=True) | ||
|
r3789 | deep_reload = CBool(False, config=True, help= | ||
""" | ||||
Enable deep (recursive) reloading by default. IPython can use the | ||||
deep_reload module which reloads changes in modules recursively (it | ||||
replaces the reload() function, so you don't need to change anything to | ||||
use it). deep_reload() forces a full reload of modules whose code may | ||||
have changed, which the default reload() function does not. When | ||||
deep_reload is off, IPython will use the normal reload(), but | ||||
deep_reload will still be available as dreload(). | ||||
""" | ||||
) | ||||
|
r5734 | disable_failing_post_execute = CBool(False, config=True, | ||
|
r7363 | help="Don't call post-execute functions that have failed in the past." | ||
|
r5734 | ) | ||
|
r3278 | display_formatter = Instance(DisplayFormatter) | ||
|
r2786 | displayhook_class = Type(DisplayHook) | ||
|
r3277 | display_pub_class = Type(DisplayPublisher) | ||
|
r8102 | data_pub_class = None | ||
|
r3277 | |||
|
r2950 | exit_now = CBool(False) | ||
|
r3724 | exiter = Instance(ExitAutocall) | ||
def _exiter_default(self): | ||||
return ExitAutocall(self) | ||||
|
r3136 | # Monotonically increasing execution counter | ||
|
r5344 | execution_count = Integer(1) | ||
|
r3464 | filename = Unicode("<ipython console>") | ||
|
r2322 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ | ||
|
r3049 | |||
|
r10254 | # Input splitter, to transform input line by line and detect when a block | ||
# is ready to be executed. | ||||
|
r3049 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | ||
|
r10254 | (), {'line_input_checker': True}) | ||
# This InputSplitter instance is used to transform completed cells before | ||||
# running them. It allows cell magics to contain blank lines. | ||||
input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | ||||
(), {'line_input_checker': False}) | ||||
|
r3789 | logstart = CBool(False, config=True, help= | ||
""" | ||||
Start logging to the default log file. | ||||
""" | ||||
) | ||||
logfile = Unicode('', config=True, help= | ||||
""" | ||||
The name of the logfile to use. | ||||
""" | ||||
) | ||||
logappend = Unicode('', config=True, help= | ||||
""" | ||||
Start logging to the given file in append mode. | ||||
""" | ||||
) | ||||
|
r2204 | object_info_string_level = Enum((0,1,2), default_value=0, | ||
|
r2245 | config=True) | ||
|
r3789 | pdb = CBool(False, config=True, help= | ||
""" | ||||
Automatically call the pdb debugger after every exception. | ||||
""" | ||||
) | ||||
|
r5096 | multiline_history = CBool(sys.platform != 'win32', config=True, | ||
|
r5178 | help="Save multi-line entries as one entry in readline history" | ||
|
r5011 | ) | ||
|
r2987 | |||
|
r5548 | # deprecated prompt traits: | ||
prompt_in1 = Unicode('In [\\#]: ', config=True, | ||||
help="Deprecated, use PromptManager.in_template") | ||||
prompt_in2 = Unicode(' .\\D.: ', config=True, | ||||
help="Deprecated, use PromptManager.in2_template") | ||||
prompt_out = Unicode('Out[\\#]: ', config=True, | ||||
help="Deprecated, use PromptManager.out_template") | ||||
prompts_pad_left = CBool(True, config=True, | ||||
help="Deprecated, use PromptManager.justify") | ||||
def _prompt_trait_changed(self, name, old, new): | ||||
table = { | ||||
'prompt_in1' : 'in_template', | ||||
'prompt_in2' : 'in2_template', | ||||
'prompt_out' : 'out_template', | ||||
'prompts_pad_left' : 'justify', | ||||
} | ||||
|
r8223 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( | ||
|
r5548 | name=name, newname=table[name]) | ||
) | ||||
# protect against weird cases where self.config may not exist: | ||||
if self.config is not None: | ||||
# propagate to corresponding PromptManager trait | ||||
setattr(self.config.PromptManager, table[name], new) | ||||
_prompt_in1_changed = _prompt_trait_changed | ||||
_prompt_in2_changed = _prompt_trait_changed | ||||
_prompt_out_changed = _prompt_trait_changed | ||||
_prompt_pad_left_changed = _prompt_trait_changed | ||||
|
r5555 | show_rewritten_input = CBool(True, config=True, | ||
help="Show rewritten input, e.g. for autocall." | ||||
) | ||||
|
r2245 | quiet = CBool(False, config=True) | ||
|
r5344 | history_length = Integer(10000, config=True) | ||
|
r4872 | |||
|
r2761 | # The readline stuff will eventually be moved to the terminal subclass | ||
# but for now, we can't do that as readline is welded in everywhere. | ||||
|
r2245 | readline_use = CBool(True, config=True) | ||
|
r4046 | readline_remove_delims = Unicode('-/~', config=True) | ||
|
r9017 | readline_delims = Unicode() # set by init_readline() | ||
|
r3842 | # don't use \M- bindings by default, because they | ||
# conflict with 8-bit encodings. See gh-58,gh-88 | ||||
|
r2204 | readline_parse_and_bind = List([ | ||
'tab: complete', | ||||
|
r2567 | '"\C-l": clear-screen', | ||
|
r2204 | 'set show-all-if-ambiguous on', | ||
'"\C-o": tab-insert', | ||||
'"\C-r": reverse-search-history', | ||||
'"\C-s": forward-search-history', | ||||
'"\C-p": history-search-backward', | ||||
'"\C-n": history-search-forward', | ||||
'"\e[A": history-search-backward', | ||||
'"\e[B": history-search-forward', | ||||
'"\C-k": kill-line', | ||||
'"\C-u": unix-line-discard', | ||||
|
r2245 | ], allow_none=False, config=True) | ||
|
r2202 | |||
|
r7034 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], | ||
default_value='last_expr', config=True, | ||||
help=""" | ||||
|
r7550 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | ||
|
r7034 | run interactively (displaying output from expressions).""") | ||
|
r7033 | |||
|
r2766 | # TODO: this part of prompt management should be moved to the frontends. | ||
# Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | ||||
|
r4046 | separate_in = SeparateUnicode('\n', config=True) | ||
separate_out = SeparateUnicode('', config=True) | ||||
separate_out2 = SeparateUnicode('', config=True) | ||||
|
r2245 | wildcards_case_sensitive = CBool(True, config=True) | ||
|
r4872 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | ||
|
r2245 | default_value='Context', config=True) | ||
|
r2204 | |||
|
r2731 | # Subcomponents of InteractiveShell | ||
alias_manager = Instance('IPython.core.alias.AliasManager') | ||||
prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') | ||||
builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') | ||||
display_trap = Instance('IPython.core.display_trap.DisplayTrap') | ||||
extension_manager = Instance('IPython.core.extensions.ExtensionManager') | ||||
|
r2812 | payload_manager = Instance('IPython.core.payload.PayloadManager') | ||
|
r3122 | history_manager = Instance('IPython.core.history.HistoryManager') | ||
|
r6917 | magics_manager = Instance('IPython.core.magic.MagicsManager') | ||
|
r2731 | |||
|
r4023 | profile_dir = Instance('IPython.core.application.ProfileDir') | ||
|
r3963 | @property | ||
def profile(self): | ||||
if self.profile_dir is not None: | ||||
name = os.path.basename(self.profile_dir.location) | ||||
return name.replace('profile_','') | ||||
|
r2987 | # Private interface | ||
|
r3732 | _post_execute = Instance(dict) | ||
|
r2987 | |||
|
r7965 | # Tracks any GUI loop loaded for pylab | ||
pylab_gui_select = None | ||||
|
r11064 | def __init__(self, ipython_dir=None, profile_dir=None, | ||
|
r5453 | user_module=None, user_ns=None, | ||
|
r10331 | custom_exceptions=((), None), **kwargs): | ||
|
r1032 | |||
|
r2385 | # This is where traits with a config_key argument are updated | ||
|
r2203 | # from the values on config. | ||
|
r11064 | super(InteractiveShell, self).__init__(**kwargs) | ||
|
r5225 | self.configurables = [self] | ||
|
r1032 | |||
|
r2226 | # These are relatively independent and stateless | ||
|
r2322 | self.init_ipython_dir(ipython_dir) | ||
|
r3963 | self.init_profile_dir(profile_dir) | ||
|
r2202 | self.init_instance_attrs() | ||
|
r3005 | self.init_environment() | ||
|
r6085 | |||
# Check if we're in a virtualenv, and set up sys.path. | ||||
self.init_virtualenv() | ||||
|
r2226 | |||
|
r2245 | # Create namespaces (user_ns, user_global_ns, etc.) | ||
|
r5453 | self.init_create_namespaces(user_module, user_ns) | ||
|
r2226 | # This has to be done after init_create_namespaces because it uses | ||
# something in self.user_ns, but before init_sys_modules, which | ||||
# is the first thing to modify sys. | ||||
|
r2804 | # TODO: When we override sys.stdout and sys.stderr before this class | ||
# is created, we are saving the overridden ones here. Not sure if this | ||||
# is what we want to do. | ||||
|
r2226 | self.save_sys_module_state() | ||
self.init_sys_modules() | ||||
|
r4872 | |||
|
r3388 | # While we're trying to have each part of the code directly access what | ||
# it needs without keeping redundant references to objects, we have too | ||||
# much legacy code that expects ip.db to exist. | ||||
|
r3963 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | ||
|
r2226 | |||
|
r2202 | self.init_history() | ||
self.init_encoding() | ||||
|
r2244 | self.init_prefilter() | ||
|
r1032 | |||
|
r2202 | self.init_syntax_highlighting() | ||
self.init_hooks() | ||||
|
r15605 | self.init_events() | ||
|
r2202 | self.init_pushd_popd_magic() | ||
|
r2804 | # self.init_traceback_handlers use to be here, but we moved it below | ||
# because it and init_io have to come after init_readline. | ||||
|
r2226 | self.init_user_ns() | ||
|
r2202 | self.init_logger() | ||
self.init_builtins() | ||||
|
r2767 | |||
|
r2203 | # The following was in post_config_initialization | ||
self.init_inspector() | ||||
|
r2804 | # init_readline() must come before init_io(), because init_io uses | ||
# readline related things. | ||||
|
r2203 | self.init_readline() | ||
|
r4662 | # We save this here in case user code replaces raw_input, but it needs | ||
# to be after init_readline(), because PyPy's readline works by replacing | ||||
# raw_input. | ||||
|
r4753 | if py3compat.PY3: | ||
self.raw_input_original = input | ||||
else: | ||||
self.raw_input_original = raw_input | ||||
|
r2956 | # init_completer must come after init_readline, because it needs to | ||
# know whether readline is present or not system-wide to configure the | ||||
# completers, since the completion machinery can now operate | ||||
# independently of readline (e.g. over the network) | ||||
self.init_completer() | ||||
|
r2804 | # TODO: init_io() needs to happen before init_traceback handlers | ||
# because the traceback handlers hardcode the stdout/stderr streams. | ||||
# This logic in in debugger.Pdb and should eventually be changed. | ||||
self.init_io() | ||||
self.init_traceback_handlers(custom_exceptions) | ||||
|
r2203 | self.init_prompts() | ||
|
r3278 | self.init_display_formatter() | ||
|
r3277 | self.init_display_pub() | ||
|
r8102 | self.init_data_pub() | ||
|
r2203 | self.init_displayhook() | ||
|
r7861 | self.init_latextool() | ||
|
r2203 | self.init_magics() | ||
|
r12595 | self.init_alias() | ||
|
r7819 | self.init_logstart() | ||
|
r2203 | self.init_pdb() | ||
|
r2731 | self.init_extension_manager() | ||
|
r2810 | self.init_payload() | ||
|
r13218 | self.init_comms() | ||
|
r2203 | self.hooks.late_startup_hook() | ||
|
r15612 | self.events.trigger('shell_initialized', self) | ||
|
r2953 | atexit.register(self.atexit_operations) | ||
|
r2203 | |||
|
r2245 | def get_ipython(self): | ||
|
r2400 | """Return the currently running IPython instance.""" | ||
|
r2245 | return self | ||
|
r2203 | #------------------------------------------------------------------------- | ||
|
r2385 | # Trait changed handlers | ||
|
r2203 | #------------------------------------------------------------------------- | ||
|
r2322 | def _ipython_dir_changed(self, name, new): | ||
|
r2245 | if not os.path.isdir(new): | ||
|
r8490 | os.makedirs(new, mode = 0o777) | ||
|
r2245 | |||
|
r2242 | def set_autoindent(self,value=None): | ||
"""Set the autoindent flag, checking for readline support. | ||||
If called with no arguments, it acts as a toggle.""" | ||||
|
r4828 | if value != 0 and not self.has_readline: | ||
|
r2242 | if os.name == 'posix': | ||
warn("The auto-indent feature requires the readline library") | ||||
self.autoindent = 0 | ||||
return | ||||
if value is None: | ||||
self.autoindent = not self.autoindent | ||||
else: | ||||
self.autoindent = value | ||||
|
r2203 | #------------------------------------------------------------------------- | ||
# init_* methods called by __init__ | ||||
#------------------------------------------------------------------------- | ||||
|
r2202 | |||
|
r2322 | def init_ipython_dir(self, ipython_dir): | ||
if ipython_dir is not None: | ||||
self.ipython_dir = ipython_dir | ||||
|
r2223 | return | ||
|
r3963 | self.ipython_dir = get_ipython_dir() | ||
|
r2226 | |||
|
r3963 | def init_profile_dir(self, profile_dir): | ||
if profile_dir is not None: | ||||
self.profile_dir = profile_dir | ||||
return | ||||
self.profile_dir =\ | ||||
ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') | ||||
|
r2223 | |||
|
r2202 | def init_instance_attrs(self): | ||
self.more = False | ||||
|
r1032 | |||
# command compiler | ||||
|
r3172 | self.compile = CachingCompiler() | ||
|
r1032 | |||
# Make an empty namespace, which extension writers can rely on both | ||||
# existing and NEVER being used by ipython itself. This gives them a | ||||
# convenient location for storing additional information and state | ||||
# their extensions may require, without fear of collisions with other | ||||
# ipython names that may develop later. | ||||
self.meta = Struct() | ||||
|
r2202 | # Temporary files used for various purposes. Deleted at exit. | ||
self.tempfiles = [] | ||||
|
r14683 | self.tempdirs = [] | ||
|
r2202 | |||
# Keep track of readline usage (later set by init_readline) | ||||
self.has_readline = False | ||||
# keep track of where we started running (mainly for crash post-mortem) | ||||
# This is not being used anywhere currently. | ||||
|
r13447 | self.starting_dir = py3compat.getcwd() | ||
|
r2202 | |||
# Indentation management | ||||
self.indent_current_nsp = 0 | ||||
|
r3732 | # Dict to track post-execution functions that have been registered | ||
self._post_execute = {} | ||||
|
r3005 | def init_environment(self): | ||
"""Any changes we need to make to the user's environment.""" | ||||
pass | ||||
|
r2242 | def init_encoding(self): | ||
# Get system encoding at startup time. Certain terminals (like Emacs | ||||
# under Win32 have it set to None, and we need to have a known valid | ||||
# encoding to use in the raw_input() method | ||||
try: | ||||
self.stdin_encoding = sys.stdin.encoding or 'ascii' | ||||
except AttributeError: | ||||
self.stdin_encoding = 'ascii' | ||||
|
r1032 | |||
|
r2242 | def init_syntax_highlighting(self): | ||
# Python source parser/formatter for syntax highlighting | ||||
pyformat = PyColorize.Parser().format | ||||
self.pycolorize = lambda src: pyformat(src,'str',self.colors) | ||||
|
r1032 | |||
|
r2242 | def init_pushd_popd_magic(self): | ||
# for pushd/popd management | ||||
|
r5384 | self.home_dir = get_home_dir() | ||
|
r1032 | |||
|
r2242 | self.dir_stack = [] | ||
|
r1419 | |||
|
r2242 | def init_logger(self): | ||
|
r3122 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | ||
logmode='rotate') | ||||
|
r1859 | |||
|
r2242 | def init_logstart(self): | ||
|
r3122 | """Initialize logging in case it was requested at the command line. | ||
""" | ||||
|
r2265 | if self.logappend: | ||
|
r6906 | self.magic('logstart %s append' % self.logappend) | ||
|
r2252 | elif self.logfile: | ||
|
r7819 | self.magic('logstart %s' % self.logfile) | ||
|
r2242 | elif self.logstart: | ||
|
r6906 | self.magic('logstart') | ||
|
r1859 | |||
|
r2242 | def init_builtins(self): | ||
|
r5492 | # A single, static flag that we set to True. Its presence indicates | ||
# that an IPython shell has been created, and we make no attempts at | ||||
# removing on exit or representing the existence of more than one | ||||
# IPython at a time. | ||||
builtin_mod.__dict__['__IPYTHON__'] = True | ||||
# In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to | ||||
# manage on enter/exit, but with all our shells it's virtually | ||||
# impossible to get all the cases right. We're leaving the name in for | ||||
# those who adapted their codes to check for this flag, but will | ||||
# eventually remove it after a few more releases. | ||||
builtin_mod.__dict__['__IPYTHON__active'] = \ | ||||
'Deprecated, check for __IPYTHON__' | ||||
|
r2740 | self.builtin_trap = BuiltinTrap(shell=self) | ||
|
r1032 | |||
|
r2242 | def init_inspector(self): | ||
# Object inspector | ||||
self.inspector = oinspect.Inspector(oinspect.InspectColors, | ||||
PyColorize.ANSICodeColors, | ||||
'NoColor', | ||||
self.object_info_string_level) | ||||
|
r1032 | |||
|
r2775 | def init_io(self): | ||
|
r2974 | # This will just use sys.stdout and sys.stderr. If you want to | ||
# override sys.stdout and sys.stderr themselves, you need to do that | ||||
|
r4872 | # *before* instantiating this class, because io holds onto | ||
|
r2974 | # references to the underlying streams. | ||
|
r8573 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: | ||
|
r3800 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) | ||
|
r2775 | else: | ||
|
r3800 | io.stdout = io.IOStream(sys.stdout) | ||
io.stderr = io.IOStream(sys.stderr) | ||||
|
r2775 | |||
|
r2242 | def init_prompts(self): | ||
|
r11064 | self.prompt_manager = PromptManager(shell=self, parent=self) | ||
|
r5511 | self.configurables.append(self.prompt_manager) | ||
|
r6749 | # Set system prompts, so that scripts can decide if they are running | ||
# interactively. | ||||
sys.ps1 = 'In : ' | ||||
sys.ps2 = '...: ' | ||||
sys.ps3 = 'Out: ' | ||||
|
r2781 | |||
|
r3278 | def init_display_formatter(self): | ||
|
r11064 | self.display_formatter = DisplayFormatter(parent=self) | ||
|
r5225 | self.configurables.append(self.display_formatter) | ||
|
r3278 | |||
|
r3277 | def init_display_pub(self): | ||
|
r11064 | self.display_pub = self.display_pub_class(parent=self) | ||
|
r5225 | self.configurables.append(self.display_pub) | ||
|
r3277 | |||
|
r8102 | def init_data_pub(self): | ||
if not self.data_pub_class: | ||||
self.data_pub = None | ||||
return | ||||
|
r11064 | self.data_pub = self.data_pub_class(parent=self) | ||
|
r8102 | self.configurables.append(self.data_pub) | ||
|
r2781 | def init_displayhook(self): | ||
# Initialize displayhook, set in/out prompts and printing system | ||||
|
r2786 | self.displayhook = self.displayhook_class( | ||
|
r11064 | parent=self, | ||
|
r2786 | shell=self, | ||
cache_size=self.cache_size, | ||||
) | ||||
|
r5225 | self.configurables.append(self.displayhook) | ||
|
r2781 | # This is a context manager that installs/revmoes the displayhook at | ||
# the appropriate time. | ||||
self.display_trap = DisplayTrap(hook=self.displayhook) | ||||
|
r1032 | |||
|
r7861 | def init_latextool(self): | ||
"""Configure LaTeXTool.""" | ||||
|
r11064 | cfg = LaTeXTool.instance(parent=self) | ||
|
r7861 | if cfg not in self.configurables: | ||
self.configurables.append(cfg) | ||||
|
r6085 | def init_virtualenv(self): | ||
"""Add a virtualenv to sys.path so the user can import modules from it. | ||||
This isn't perfect: it doesn't use the Python interpreter with which the | ||||
virtualenv was built, and it ignores the --no-site-packages option. A | ||||
warning will appear suggesting the user installs IPython in the | ||||
virtualenv, but for many cases, it probably works well enough. | ||||
Adapted from code snippets online. | ||||
http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | ||||
""" | ||||
if 'VIRTUAL_ENV' not in os.environ: | ||||
# Not in a virtualenv | ||||
return | ||||
|
r13944 | # venv detection: | ||
# stdlib venv may symlink sys.executable, so we can't use realpath. | ||||
# but others can symlink *to* the venv Python, so we can't just use sys.executable. | ||||
# So we just check every item in the symlink tree (generally <= 3) | ||||
p = sys.executable | ||||
paths = [p] | ||||
while os.path.islink(p): | ||||
p = os.path.join(os.path.dirname(p), os.readlink(p)) | ||||
paths.append(p) | ||||
if any(p.startswith(os.environ['VIRTUAL_ENV']) for p in paths): | ||||
|
r6085 | # Running properly in the virtualenv, don't need to do anything | ||
return | ||||
warn("Attempting to work in a virtualenv. If you encounter problems, please " | ||||
|
r8223 | "install IPython inside the virtualenv.") | ||
|
r6085 | if sys.platform == "win32": | ||
virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') | ||||
else: | ||||
virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', | ||||
'python%d.%d' % sys.version_info[:2], 'site-packages') | ||||
import site | ||||
sys.path.insert(0, virtual_env) | ||||
site.addsitedir(virtual_env) | ||||
|
r1032 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to injections into the sys module | ||||
#------------------------------------------------------------------------- | ||||
|
r1032 | |||
|
r2242 | def save_sys_module_state(self): | ||
"""Save the state of hooks in the sys module. | ||||
|
r2226 | |||
|
r5453 | This has to be called after self.user_module is created. | ||
|
r2242 | """ | ||
self._orig_sys_module_state = {} | ||||
self._orig_sys_module_state['stdin'] = sys.stdin | ||||
self._orig_sys_module_state['stdout'] = sys.stdout | ||||
self._orig_sys_module_state['stderr'] = sys.stderr | ||||
self._orig_sys_module_state['excepthook'] = sys.excepthook | ||||
|
r5453 | self._orig_sys_modules_main_name = self.user_module.__name__ | ||
|
r6829 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | ||
|
r2202 | |||
|
r2242 | def restore_sys_module_state(self): | ||
"""Restore the state of the sys module.""" | ||||
try: | ||||
|
r13361 | for k, v in iteritems(self._orig_sys_module_state): | ||
|
r2242 | setattr(sys, k, v) | ||
except AttributeError: | ||||
pass | ||||
# Reset what what done in self.init_sys_modules | ||||
|
r6829 | if self._orig_sys_modules_main_mod is not None: | ||
sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | ||||
|
r2205 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to hooks | ||||
#------------------------------------------------------------------------- | ||||
|
r2205 | |||
|
r2242 | def init_hooks(self): | ||
# hooks holds pointers used for user-side customizations | ||||
self.hooks = Struct() | ||||
|
r2205 | |||
|
r2242 | self.strdispatchers = {} | ||
|
r2205 | |||
|
r2242 | # Set all default hooks, defined in the IPython.hooks module. | ||
hooks = IPython.core.hooks | ||||
for hook_name in hooks.__all__: | ||||
# default hooks have priority 100, i.e. low; user hooks should have | ||||
# 0-100 priority | ||||
|
r15602 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) | ||
|
r2205 | |||
|
r15602 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, | ||
_warn_deprecated=True): | ||||
|
r2242 | """set_hook(name,hook) -> sets an internal IPython hook. | ||
|
r2205 | |||
|
r2242 | IPython exposes some of its internal API as user-modifiable hooks. By | ||
|
r4872 | adding your function to one of these hooks, you can modify IPython's | ||
|
r2242 | behavior to call at runtime your own routines.""" | ||
|
r2205 | |||
|
r2242 | # At some point in the future, this should validate the hook before it | ||
# accepts it. Probably at least check that the hook takes the number | ||||
# of args it's supposed to. | ||||
|
r4872 | |||
|
r3158 | f = types.MethodType(hook,self) | ||
|
r1032 | |||
|
r2242 | # check if the hook is for strdispatcher first | ||
if str_key is not None: | ||||
sdp = self.strdispatchers.get(name, StrDispatch()) | ||||
sdp.add_s(str_key, f, priority ) | ||||
self.strdispatchers[name] = sdp | ||||
return | ||||
if re_key is not None: | ||||
sdp = self.strdispatchers.get(name, StrDispatch()) | ||||
sdp.add_re(re.compile(re_key), f, priority ) | ||||
self.strdispatchers[name] = sdp | ||||
return | ||||
|
r4872 | |||
|
r2242 | dp = getattr(self.hooks, name, None) | ||
if name not in IPython.core.hooks.__all__: | ||||
|
r7817 | print("Warning! Hook '%s' is not one of %s" % \ | ||
(name, IPython.core.hooks.__all__ )) | ||||
|
r15602 | |||
if _warn_deprecated and (name in IPython.core.hooks.deprecated): | ||||
alternative = IPython.core.hooks.deprecated[name] | ||||
warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) | ||||
|
r2242 | if not dp: | ||
dp = IPython.core.hooks.CommandChainDispatcher() | ||||
|
r4872 | |||
|
r1032 | try: | ||
|
r2242 | dp.add(f,priority) | ||
except AttributeError: | ||||
# it was not commandchain, plain old func - replace | ||||
dp = f | ||||
|
r1032 | |||
|
r2242 | setattr(self.hooks,name, dp) | ||
|
r1032 | |||
|
r15597 | #------------------------------------------------------------------------- | ||
|
r15665 | # Things related to events | ||
|
r15597 | #------------------------------------------------------------------------- | ||
|
r15605 | def init_events(self): | ||
self.events = EventManager(self, available_events) | ||||
|
r15597 | |||
|
r2987 | def register_post_execute(self, func): | ||
|
r15611 | """DEPRECATED: Use ip.events.register('post_run_cell', func) | ||
|
r15597 | |||
Register a function for calling after code execution. | ||||
|
r2987 | """ | ||
|
r15597 | warn("ip.register_post_execute is deprecated, use " | ||
|
r15611 | "ip.events.register('post_run_cell', func) instead.") | ||
|
r15607 | self.events.register('post_run_cell', func) | ||
|
r5734 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to the "main" module | ||||
#------------------------------------------------------------------------- | ||||
|
r2202 | |||
|
r12562 | def new_main_mod(self, filename, modname): | ||
|
r2242 | """Return a new 'main' module object for user code execution. | ||
|
r11207 | |||
|
r11275 | ``filename`` should be the path of the script which will be run in the | ||
module. Requests with the same filename will get the same module, with | ||||
its namespace cleared. | ||||
|
r12562 | ``modname`` should be the module name - normally either '__main__' or | ||
the basename of the file without the extension. | ||||
|
r11275 | When scripts are executed via %run, we must keep a reference to their | ||
|
r12562 | __main__ module around so that Python doesn't | ||
|
r11275 | clear it, rendering references to module globals useless. | ||
|
r1032 | |||
|
r2242 | This method keeps said reference in a private dict, keyed by the | ||
|
r11275 | absolute path of the script. This way, for multiple executions of the | ||
same script we only keep one copy of the namespace (the last one), | ||||
thus preventing memory leaks from old references while allowing the | ||||
objects from the last execution to be accessible. | ||||
|
r2242 | """ | ||
|
r11207 | filename = os.path.abspath(filename) | ||
try: | ||||
main_mod = self._main_mod_cache[filename] | ||||
except KeyError: | ||||
|
r12562 | main_mod = self._main_mod_cache[filename] = types.ModuleType(modname, | ||
doc="Module created for script run in IPython") | ||||
|
r11207 | else: | ||
|
r12562 | main_mod.__dict__.clear() | ||
main_mod.__name__ = modname | ||||
main_mod.__file__ = filename | ||||
# It seems pydoc (and perhaps others) needs any module instance to | ||||
# implement a __nonzero__ method | ||||
main_mod.__nonzero__ = lambda : True | ||||
|
r11207 | |||
return main_mod | ||||
|
r1032 | |||
|
r2242 | def clear_main_mod_cache(self): | ||
"""Clear the cache of main modules. | ||||
|
r2202 | |||
|
r2242 | Mainly for use by utilities like %reset. | ||
|
r2202 | |||
|
r2242 | Examples | ||
-------- | ||||
|
r1032 | |||
|
r2242 | In [15]: import IPython | ||
|
r1032 | |||
|
r12562 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | ||
|
r1032 | |||
|
r11207 | In [17]: len(_ip._main_mod_cache) > 0 | ||
|
r2242 | Out[17]: True | ||
|
r1032 | |||
|
r2242 | In [18]: _ip.clear_main_mod_cache() | ||
|
r1856 | |||
|
r11207 | In [19]: len(_ip._main_mod_cache) == 0 | ||
|
r2242 | Out[19]: True | ||
""" | ||||
|
r11207 | self._main_mod_cache.clear() | ||
|
r2203 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to debugging | ||||
#------------------------------------------------------------------------- | ||||
|
r2203 | |||
|
r2242 | def init_pdb(self): | ||
# Set calling of pdb on exceptions | ||||
# self.call_pdb is a property | ||||
self.call_pdb = self.pdb | ||||
|
r1032 | |||
|
r2242 | def _get_call_pdb(self): | ||
return self._call_pdb | ||||
def _set_call_pdb(self,val): | ||||
if val not in (0,1,False,True): | ||||
|
r7843 | raise ValueError('new call_pdb value must be boolean') | ||
|
r2242 | |||
# store value in instance | ||||
self._call_pdb = val | ||||
# notify the actual exception handlers | ||||
self.InteractiveTB.call_pdb = val | ||||
call_pdb = property(_get_call_pdb,_set_call_pdb,None, | ||||
'Control auto-activation of pdb at exceptions') | ||||
def debugger(self,force=False): | ||||
"""Call the pydb/pdb debugger. | ||||
Keywords: | ||||
- force(False): by default, this routine checks the instance call_pdb | ||||
|
r12553 | flag and does not actually invoke the debugger if the flag is false. | ||
The 'force' option forces the debugger to activate even if the flag | ||||
is false. | ||||
|
r2242 | """ | ||
if not (force or self.call_pdb): | ||||
|
r2203 | return | ||
|
r2242 | if not hasattr(sys,'last_traceback'): | ||
error('No traceback has been produced, nothing to debug.') | ||||
return | ||||
# use pydb if available | ||||
if debugger.has_pydb: | ||||
from pydb import pm | ||||
|
r2203 | else: | ||
|
r2242 | # fallback to our internal debugger | ||
pm = lambda : self.InteractiveTB.debugger(force=True) | ||||
|
r4872 | |||
|
r3474 | with self.readline_no_record: | ||
pm() | ||||
|
r2203 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to IPython's various namespaces | ||||
#------------------------------------------------------------------------- | ||||
|
r5669 | default_user_namespaces = True | ||
|
r2203 | |||
|
r5453 | def init_create_namespaces(self, user_module=None, user_ns=None): | ||
|
r2242 | # Create the namespace where the user will operate. user_ns is | ||
# normally the only one used, and it is passed to the exec calls as | ||||
# the locals argument. But we do carry a user_global_ns namespace | ||||
# given as the exec 'globals' argument, This is useful in embedding | ||||
# situations where the ipython shell opens in a context where the | ||||
# distinction between locals and globals is meaningful. For | ||||
# non-embedded contexts, it is just the same object as the user_ns dict. | ||||
|
r2203 | |||
|
r2242 | # FIXME. For some strange reason, __builtins__ is showing up at user | ||
# level as a dict instead of a module. This is a manual fix, but I | ||||
# should really track down where the problem is coming from. Alex | ||||
# Schmolck reported this problem first. | ||||
|
r2203 | |||
|
r2242 | # A useful post by Alex Martelli on this topic: | ||
# Re: inconsistent value from __builtins__ | ||||
# Von: Alex Martelli <aleaxit@yahoo.com> | ||||
# Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | ||||
# Gruppen: comp.lang.python | ||||
|
r2203 | |||
|
r2242 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | ||
# > >>> print type(builtin_check.get_global_binding('__builtins__')) | ||||
# > <type 'dict'> | ||||
# > >>> print type(__builtins__) | ||||
# > <type 'module'> | ||||
# > Is this difference in return value intentional? | ||||
# Well, it's documented that '__builtins__' can be either a dictionary | ||||
# or a module, and it's been that way for a long time. Whether it's | ||||
# intentional (or sensible), I don't know. In any case, the idea is | ||||
# that if you need to access the built-in namespace directly, you | ||||
# should start with "import __builtin__" (note, no 's') which will | ||||
# definitely give you a module. Yeah, it's somewhat confusing:-(. | ||||
|
r5456 | # These routines return a properly built module and dict as needed by | ||
# the rest of the code, and can also be used by extension writers to | ||||
# generate properly initialized namespaces. | ||||
|
r5669 | if (user_ns is not None) or (user_module is not None): | ||
self.default_user_namespaces = False | ||||
|
r5456 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | ||
|
r2242 | |||
|
r5456 | # A record of hidden variables we have added to the user namespace, so | ||
# we can list later only variables defined in actual interactive use. | ||||
|
r12257 | self.user_ns_hidden = {} | ||
|
r2242 | |||
# Now that FakeModule produces a real module, we've run into a nasty | ||||
# problem: after script execution (via %run), the module where the user | ||||
# code ran is deleted. Now that this object is a true module (needed | ||||
# so docetst and other tools work correctly), the Python module | ||||
# teardown mechanism runs over it, and sets to None every variable | ||||
# present in that module. Top-level references to objects from the | ||||
# script survive, because the user_ns is updated with them. However, | ||||
# calling functions defined in the script that use other things from | ||||
# the script will fail, because the function's closure had references | ||||
# to the original objects, which are now all None. So we must protect | ||||
# these modules from deletion by keeping a cache. | ||||
|
r4872 | # | ||
|
r2242 | # To avoid keeping stale modules around (we only need the one from the | ||
# last run), we use a dict keyed with the full path to the script, so | ||||
# only the last version of the module is held in the cache. Note, | ||||
# however, that we must cache the module *namespace contents* (their | ||||
# __dict__). Because if we try to cache the actual modules, old ones | ||||
# (uncached) could be destroyed while still holding references (such as | ||||
# those held by GUI objects that tend to be long-lived)> | ||||
|
r4872 | # | ||
|
r2242 | # The %reset command will flush this cache. See the cache_main_mod() | ||
# and clear_main_mod_cache() methods for details on use. | ||||
# This is the cache used for 'main' namespaces | ||||
|
r11207 | self._main_mod_cache = {} | ||
|
r2242 | |||
# A table holding all the namespaces IPython deals with, so that | ||||
# introspection facilities can search easily. | ||||
|
r5453 | self.ns_table = {'user_global':self.user_module.__dict__, | ||
|
r5550 | 'user_local':self.user_ns, | ||
|
r4734 | 'builtin':builtin_mod.__dict__ | ||
|
r2242 | } | ||
|
r5452 | |||
@property | ||||
def user_global_ns(self): | ||||
return self.user_module.__dict__ | ||||
|
r2499 | |||
|
r5456 | def prepare_user_module(self, user_module=None, user_ns=None): | ||
"""Prepare the module and namespace in which user code will be run. | ||||
When IPython is started normally, both parameters are None: a new module | ||||
is created automatically, and its __dict__ used as the namespace. | ||||
If only user_module is provided, its __dict__ is used as the namespace. | ||||
If only user_ns is provided, a dummy module is created, and user_ns | ||||
becomes the global namespace. If both are provided (as they may be | ||||
when embedding), user_ns is the local namespace, and user_module | ||||
provides the global namespace. | ||||
|
r2499 | |||
Parameters | ||||
---------- | ||||
|
r5452 | user_module : module, optional | ||
The current user module in which IPython is being run. If None, | ||||
a clean module will be created. | ||||
|
r5456 | user_ns : dict, optional | ||
A namespace in which to run interactive commands. | ||||
|
r2499 | |||
Returns | ||||
------- | ||||
|
r5456 | A tuple of user_module and user_ns, each properly initialised. | ||
|
r2499 | """ | ||
|
r5456 | if user_module is None and user_ns is not None: | ||
user_ns.setdefault("__name__", "__main__") | ||||
user_module = DummyMod() | ||||
user_module.__dict__ = user_ns | ||||
|
r5452 | if user_module is None: | ||
user_module = types.ModuleType("__main__", | ||||
doc="Automatically created module for IPython interactive environment") | ||||
|
r2499 | # We must ensure that __builtin__ (without the final 's') is always | ||
# available and pointing to the __builtin__ *module*. For more details: | ||||
# http://mail.python.org/pipermail/python-dev/2001-April/014068.html | ||||
|
r5460 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | ||
user_module.__dict__.setdefault('__builtins__', builtin_mod) | ||||
|
r5456 | |||
if user_ns is None: | ||||
user_ns = user_module.__dict__ | ||||
|
r2499 | |||
|
r5456 | return user_module, user_ns | ||
|
r2499 | |||
|
r2242 | def init_sys_modules(self): | ||
# We need to insert into sys.modules something that looks like a | ||||
# module but which accesses the IPython namespace, for shelve and | ||||
# pickle to work interactively. Normally they rely on getting | ||||
# everything out of __main__, but for embedding purposes each IPython | ||||
# instance has its own private namespace, so we can't go shoving | ||||
# everything into __main__. | ||||
# note, however, that we should only do this for non-embedded | ||||
# ipythons, which really mimic the __main__.__dict__ with their own | ||||
# namespace. Embedded instances, on the other hand, should not do | ||||
# this because they need to manage the user local/global namespaces | ||||
# only, but they live within a 'normal' __main__ (meaning, they | ||||
# shouldn't overtake the execution environment of the script they're | ||||
# embedded in). | ||||
|
r2203 | |||
|
r2242 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | ||
|
r5452 | main_name = self.user_module.__name__ | ||
sys.modules[main_name] = self.user_module | ||||
|
r1032 | |||
|
r2226 | def init_user_ns(self): | ||
|
r1859 | """Initialize all user-visible namespaces to their minimum defaults. | ||
Certain history lists are also initialized here, as they effectively | ||||
act as user namespaces. | ||||
|
r2131 | Notes | ||
----- | ||||
|
r1859 | All data structures here are only filled in, they are NOT reset by this | ||
method. If they were not empty before, data will simply be added to | ||||
therm. | ||||
""" | ||||
|
r2398 | # This function works in two parts: first we put a few things in | ||
|
r2509 | # user_ns, and we sync that contents into user_ns_hidden so that these | ||
|
r2398 | # initial variables aren't shown by %who. After the sync, we add the | ||
# rest of what we *do* want the user to see with %who even on a new | ||||
|
r2485 | # session (probably nothing, so theye really only see their own stuff) | ||
# The user dict must *always* have a __builtin__ reference to the | ||||
# Python standard __builtin__ namespace, which must be imported. | ||||
# This is so that certain operations in prompt evaluation can be | ||||
# reliably executed with builtins. Note that we can NOT use | ||||
# __builtins__ (note the 's'), because that can either be a dict or a | ||||
# module, and can even mutate at runtime, depending on the context | ||||
# (Python makes no guarantees on it). In contrast, __builtin__ is | ||||
# always a module object, though it must be explicitly imported. | ||||
|
r4872 | |||
|
r2485 | # For more details: | ||
# http://mail.python.org/pipermail/python-dev/2001-April/014068.html | ||||
|
r5452 | ns = dict() | ||
|
r1859 | # make global variables for user access to the histories | ||
|
r3240 | ns['_ih'] = self.history_manager.input_hist_parsed | ||
ns['_oh'] = self.history_manager.output_hist | ||||
ns['_dh'] = self.history_manager.dir_hist | ||||
|
r2398 | |||
ns['_sh'] = shadowns | ||||
|
r2472 | # user aliases to input and output histories. These shouldn't show up | ||
# in %who, as they can have very large reprs. | ||||
|
r3240 | ns['In'] = self.history_manager.input_hist_parsed | ||
ns['Out'] = self.history_manager.output_hist | ||||
|
r1859 | |||
|
r2398 | # Store myself as the public api!!! | ||
ns['get_ipython'] = self.get_ipython | ||||
|
r5457 | |||
|
r3725 | ns['exit'] = self.exiter | ||
ns['quit'] = self.exiter | ||||
|
r2472 | |||
|
r2509 | # Sync what we've added so far to user_ns_hidden so these aren't seen | ||
|
r2472 | # by %who | ||
|
r2509 | self.user_ns_hidden.update(ns) | ||
|
r2472 | |||
# Anything put into ns now would show up in %who. Think twice before | ||||
# putting anything here, as we really want %who to show the user their | ||||
# stuff, not our variables. | ||||
|
r4872 | |||
|
r2472 | # Finally, update the real user's namespace | ||
|
r5453 | self.user_ns.update(ns) | ||
|
r5458 | |||
@property | ||||
def all_ns_refs(self): | ||||
"""Get a list of references to all the namespace dictionaries in which | ||||
IPython might store a user-created object. | ||||
Note that this does not include the displayhook, which also caches | ||||
objects from the output.""" | ||||
|
r12259 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ | ||
|
r11207 | [m.__dict__ for m in self._main_mod_cache.values()] | ||
|
r1859 | |||
|
r3397 | def reset(self, new_session=True): | ||
|
r3762 | """Clear all internal namespaces, and attempt to release references to | ||
user objects. | ||||
|
r4872 | |||
|
r3397 | If new_session is True, a new history session will be opened. | ||
|
r2242 | """ | ||
|
r3122 | # Clear histories | ||
|
r3397 | self.history_manager.reset(new_session) | ||
|
r3703 | # Reset counter used to index all histories | ||
if new_session: | ||||
self.execution_count = 1 | ||||
|
r4872 | |||
|
r3521 | # Flush cached output items | ||
|
r3810 | if self.displayhook.do_full_cache: | ||
self.displayhook.flush() | ||||
|
r4872 | |||
|
r3157 | # The main execution namespaces must be cleared very carefully, | ||
# skipping the deletion of the builtin-related keys, because doing so | ||||
# would cause errors in many object's __del__ methods. | ||||
|
r5456 | if self.user_ns is not self.user_global_ns: | ||
self.user_ns.clear() | ||||
ns = self.user_global_ns | ||||
drop_keys = set(ns.keys()) | ||||
drop_keys.discard('__builtin__') | ||||
drop_keys.discard('__builtins__') | ||||
drop_keys.discard('__name__') | ||||
for k in drop_keys: | ||||
del ns[k] | ||||
|
r5458 | self.user_ns_hidden.clear() | ||
|
r3157 | # Restore the user namespaces to minimal usability | ||
|
r2242 | self.init_user_ns() | ||
|
r3122 | |||
|
r2245 | # Restore the default and user aliases | ||
|
r3122 | self.alias_manager.clear_aliases() | ||
|
r2245 | self.alias_manager.init_aliases() | ||
|
r4872 | |||
|
r3521 | # Flush the private list of module references kept for script | ||
# execution protection | ||||
self.clear_main_mod_cache() | ||||
|
r4872 | |||
|
r3823 | def del_var(self, varname, by_name=False): | ||
"""Delete a variable from the various namespaces, so that, as | ||||
far as possible, we're not keeping any hidden references to it. | ||||
|
r4872 | |||
|
r3823 | Parameters | ||
---------- | ||||
varname : str | ||||
The name of the variable to delete. | ||||
by_name : bool | ||||
If True, delete variables with the given name in each | ||||
namespace. If False (default), find the variable in the user | ||||
namespace, and delete references to it. | ||||
""" | ||||
if varname in ('__builtin__', '__builtins__'): | ||||
raise ValueError("Refusing to delete %s" % varname) | ||||
|
r4872 | |||
|
r5458 | ns_refs = self.all_ns_refs | ||
|
r5454 | |||
|
r3823 | if by_name: # Delete by name | ||
for ns in ns_refs: | ||||
try: | ||||
del ns[varname] | ||||
except KeyError: | ||||
pass | ||||
else: # Delete by object | ||||
try: | ||||
obj = self.user_ns[varname] | ||||
except KeyError: | ||||
raise NameError("name '%s' is not defined" % varname) | ||||
# Also check in output history | ||||
ns_refs.append(self.history_manager.output_hist) | ||||
for ns in ns_refs: | ||||
|
r13361 | to_delete = [n for n, o in iteritems(ns) if o is obj] | ||
|
r3823 | for name in to_delete: | ||
del ns[name] | ||||
|
r4872 | |||
|
r3823 | # displayhook keeps extra references, but not in a dictionary | ||
for name in ('_', '__', '___'): | ||||
if getattr(self.displayhook, name) is obj: | ||||
setattr(self.displayhook, name, None) | ||||
|
r4872 | |||
|
r2577 | def reset_selective(self, regex=None): | ||
|
r2955 | """Clear selective variables from internal namespaces based on a | ||
specified regular expression. | ||||
|
r2577 | |||
Parameters | ||||
---------- | ||||
regex : string or compiled pattern, optional | ||||
|
r2955 | A regular expression pattern that will be used in searching | ||
variable names in the users namespaces. | ||||
|
r2577 | """ | ||
if regex is not None: | ||||
try: | ||||
m = re.compile(regex) | ||||
except TypeError: | ||||
raise TypeError('regex must be a string or compiled pattern') | ||||
# Search for keys in each namespace that match the given regex | ||||
# If a match is found, delete the key/value pair. | ||||
|
r5458 | for ns in self.all_ns_refs: | ||
|
r2577 | for var in ns: | ||
if m.search(var): | ||||
|
r4872 | del ns[var] | ||
|
r2242 | def push(self, variables, interactive=True): | ||
"""Inject a group of variables into the IPython user namespace. | ||||
Parameters | ||||
---------- | ||||
variables : dict, str or list/tuple of str | ||||
|
r2955 | The variables to inject into the user's namespace. If a dict, a | ||
simple update is done. If a str, the string is assumed to have | ||||
variable names separated by spaces. A list/tuple of str can also | ||||
be used to give the variable names. If just the variable names are | ||||
give (list/tuple/str) then the variable values looked up in the | ||||
callers frame. | ||||
|
r2242 | interactive : bool | ||
If True (default), the variables will be listed with the ``who`` | ||||
magic. | ||||
""" | ||||
vdict = None | ||||
# We need a dict of name/value pairs to do namespace updates. | ||||
if isinstance(variables, dict): | ||||
vdict = variables | ||||
|
r13353 | elif isinstance(variables, string_types+(list, tuple)): | ||
if isinstance(variables, string_types): | ||||
|
r2242 | vlist = variables.split() | ||
else: | ||||
vlist = variables | ||||
vdict = {} | ||||
cf = sys._getframe(1) | ||||
for name in vlist: | ||||
try: | ||||
vdict[name] = eval(name, cf.f_globals, cf.f_locals) | ||||
except: | ||||
|
r7817 | print('Could not get variable %s from %s' % | ||
|
r2242 | (name,cf.f_code.co_name)) | ||
else: | ||||
raise ValueError('variables must be a dict/str/list/tuple') | ||||
|
r4872 | |||
|
r2242 | # Propagate variables to user namespace | ||
self.user_ns.update(vdict) | ||||
# And configure interactive visibility | ||||
|
r5452 | user_ns_hidden = self.user_ns_hidden | ||
|
r2242 | if interactive: | ||
|
r12257 | for name in vdict: | ||
user_ns_hidden.pop(name, None) | ||||
|
r2242 | else: | ||
|
r5458 | user_ns_hidden.update(vdict) | ||
|
r5068 | |||
def drop_by_id(self, variables): | ||||
"""Remove a dict of variables from the user namespace, if they are the | ||||
same as the values in the dictionary. | ||||
This is intended for use by extensions: variables that they've added can | ||||
be taken back out if they are unloaded, without removing any that the | ||||
user has overwritten. | ||||
Parameters | ||||
---------- | ||||
variables : dict | ||||
A dictionary mapping object names (as strings) to the objects. | ||||
""" | ||||
|
r13361 | for name, obj in iteritems(variables): | ||
|
r5068 | if name in self.user_ns and self.user_ns[name] is obj: | ||
del self.user_ns[name] | ||||
|
r12259 | self.user_ns_hidden.pop(name, None) | ||
|
r2242 | |||
#------------------------------------------------------------------------- | ||||
|
r2927 | # Things related to object introspection | ||
#------------------------------------------------------------------------- | ||||
|
r3049 | |||
|
r2927 | def _ofind(self, oname, namespaces=None): | ||
"""Find an object in the available namespaces. | ||||
self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | ||||
Has special code to detect magic functions. | ||||
""" | ||||
|
r4731 | oname = oname.strip() | ||
|
r2929 | #print '1- oname: <%r>' % oname # dbg | ||
|
r7435 | if not oname.startswith(ESC_MAGIC) and \ | ||
|
r7437 | not oname.startswith(ESC_MAGIC2) and \ | ||
|
r7435 | not py3compat.isidentifier(oname, dotted=True): | ||
|
r4740 | return dict(found=False) | ||
|
r2929 | |||
|
r2927 | alias_ns = None | ||
if namespaces is None: | ||||
# Namespaces to search in: | ||||
# Put them in a list. The order is important so that we | ||||
# find things in the same order that Python finds them. | ||||
|
r2928 | namespaces = [ ('Interactive', self.user_ns), | ||
|
r5454 | ('Interactive (global)', self.user_global_ns), | ||
|
r4734 | ('Python builtin', builtin_mod.__dict__), | ||
|
r2927 | ] | ||
# initialize results to 'null' | ||||
found = False; obj = None; ospace = None; ds = None; | ||||
ismagic = False; isalias = False; parent = None | ||||
# We need to special-case 'print', which as of python2.6 registers as a | ||||
# function but should only be treated as one if print_function was | ||||
# loaded with a future import. In this case, just bail. | ||||
|
r4741 | if (oname == 'print' and not py3compat.PY3 and not \ | ||
(self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): | ||||
|
r2927 | return {'found':found, 'obj':obj, 'namespace':ospace, | ||
'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | ||||
# Look for the given name by splitting it in parts. If the head is | ||||
# found, then we look for all the remaining parts as members, and only | ||||
# declare success if we can find them all. | ||||
oname_parts = oname.split('.') | ||||
oname_head, oname_rest = oname_parts[0],oname_parts[1:] | ||||
for nsname,ns in namespaces: | ||||
try: | ||||
obj = ns[oname_head] | ||||
except KeyError: | ||||
continue | ||||
else: | ||||
#print 'oname_rest:', oname_rest # dbg | ||||
for part in oname_rest: | ||||
try: | ||||
parent = obj | ||||
obj = getattr(obj,part) | ||||
except: | ||||
# Blanket except b/c some badly implemented objects | ||||
# allow __getattr__ to raise exceptions other than | ||||
# AttributeError, which then crashes IPython. | ||||
break | ||||
else: | ||||
# If we finish the for loop (no break), we got all members | ||||
found = True | ||||
ospace = nsname | ||||
break # namespace loop | ||||
# Try to see if it's magic | ||||
if not found: | ||||
|
r7435 | obj = None | ||
|
r7437 | if oname.startswith(ESC_MAGIC2): | ||
oname = oname.lstrip(ESC_MAGIC2) | ||||
|
r6997 | obj = self.find_cell_magic(oname) | ||
|
r7435 | elif oname.startswith(ESC_MAGIC): | ||
oname = oname.lstrip(ESC_MAGIC) | ||||
obj = self.find_line_magic(oname) | ||||
else: | ||||
# search without prefix, so run? will find %run? | ||||
obj = self.find_line_magic(oname) | ||||
if obj is None: | ||||
obj = self.find_cell_magic(oname) | ||||
|
r2927 | if obj is not None: | ||
found = True | ||||
ospace = 'IPython internal' | ||||
ismagic = True | ||||
# Last try: special-case some literals like '', [], {}, etc: | ||||
if not found and oname_head in ["''",'""','[]','{}','()']: | ||||
obj = eval(oname_head) | ||||
found = True | ||||
ospace = 'Interactive' | ||||
|
r2929 | |||
|
r2927 | return {'found':found, 'obj':obj, 'namespace':ospace, | ||
'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | ||||
|
r2929 | def _ofind_property(self, oname, info): | ||
"""Second part of object finding, to look for property details.""" | ||||
|
r2927 | if info.found: | ||
# Get the docstring of the class property if it exists. | ||||
path = oname.split('.') | ||||
root = '.'.join(path[:-1]) | ||||
if info.parent is not None: | ||||
try: | ||||
|
r4872 | target = getattr(info.parent, '__class__') | ||
# The object belongs to a class instance. | ||||
try: | ||||
|
r2927 | target = getattr(target, path[-1]) | ||
|
r4872 | # The class defines the object. | ||
|
r2927 | if isinstance(target, property): | ||
oname = root + '.__class__.' + path[-1] | ||||
info = Struct(self._ofind(oname)) | ||||
except AttributeError: pass | ||||
except AttributeError: pass | ||||
|
r2929 | |||
# We return either the new info or the unmodified input if the object | ||||
# hadn't been found | ||||
return info | ||||
def _object_find(self, oname, namespaces=None): | ||||
"""Find an object and return a struct with info about it.""" | ||||
inf = Struct(self._ofind(oname, namespaces)) | ||||
return Struct(self._ofind_property(oname, inf)) | ||||
|
r4872 | |||
|
r2929 | def _inspect(self, meth, oname, namespaces=None, **kw): | ||
"""Generic interface to the inspector system. | ||||
This function is meant to be called by pdef, pdoc & friends.""" | ||||
|
r7905 | info = self._object_find(oname, namespaces) | ||
|
r2929 | if info.found: | ||
pmethod = getattr(self.inspector, meth) | ||||
formatter = format_screen if info.ismagic else None | ||||
|
r2927 | if meth == 'pdoc': | ||
|
r2929 | pmethod(info.obj, oname, formatter) | ||
|
r2927 | elif meth == 'pinfo': | ||
|
r2929 | pmethod(info.obj, oname, formatter, info, **kw) | ||
|
r2927 | else: | ||
|
r2929 | pmethod(info.obj, oname) | ||
|
r2927 | else: | ||
|
r7817 | print('Object `%s` not found.' % oname) | ||
|
r2927 | return 'not found' # so callers can take other action | ||
|
r2929 | |||
|
r6556 | def object_inspect(self, oname, detail_level=0): | ||
|
r3685 | with self.builtin_trap: | ||
info = self._object_find(oname) | ||||
if info.found: | ||||
|
r6556 | return self.inspector.info(info.obj, oname, info=info, | ||
detail_level=detail_level | ||||
) | ||||
|
r3685 | else: | ||
return oinspect.object_info(name=oname, found=False) | ||||
|
r3053 | |||
|
r2927 | #------------------------------------------------------------------------- | ||
|
r2242 | # Things related to history management | ||
#------------------------------------------------------------------------- | ||||
def init_history(self): | ||||
|
r3259 | """Sets up the command history, and starts regular autosaves.""" | ||
|
r11064 | self.history_manager = HistoryManager(shell=self, parent=self) | ||
|
r5225 | self.configurables.append(self.history_manager) | ||
|
r2242 | |||
#------------------------------------------------------------------------- | ||||
# Things related to exception handling and tracebacks (not debugging) | ||||
#------------------------------------------------------------------------- | ||||
|
r1032 | |||
|
r2242 | def init_traceback_handlers(self, custom_exceptions): | ||
# Syntax error handler. | ||||
|
r2792 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | ||
|
r4872 | |||
|
r2242 | # The interactive one is initialized with an offset, meaning we always | ||
# want to remove the topmost item in the traceback, which is our own | ||||
# internal code. Valid modes: ['Plain','Context','Verbose'] | ||||
self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | ||||
color_scheme='NoColor', | ||||
|
r3175 | tb_offset = 1, | ||
|
r9140 | check_cache=check_linecache_ipython) | ||
|
r1032 | |||
|
r2403 | # The instance will store a pointer to the system-wide exception hook, | ||
# so that runtime code (such as magics) can access it. This is because | ||||
# during the read-eval loop, it may get temporarily overwritten. | ||||
self.sys_excepthook = sys.excepthook | ||||
|
r1032 | |||
|
r2242 | # and add any custom exception handlers the user may have specified | ||
self.set_custom_exc(*custom_exceptions) | ||||
|
r1032 | |||
|
r2498 | # Set the exception mode | ||
self.InteractiveTB.set_mode(mode=self.xmode) | ||||
|
r2838 | def set_custom_exc(self, exc_tuple, handler): | ||
|
r1032 | """set_custom_exc(exc_tuple,handler) | ||
Set a custom exception handler, which will be called if any of the | ||||
exceptions in exc_tuple occur in the mainloop (specifically, in the | ||||
|
r5000 | run_code() method). | ||
|
r1032 | |||
|
r5000 | Parameters | ||
---------- | ||||
exc_tuple : tuple of exception classes | ||||
A *tuple* of exception classes, for which to call the defined | ||||
handler. It is very important that you use a tuple, and NOT A | ||||
LIST here, because of the way Python's except statement works. If | ||||
you only want to trap a single exception, use a singleton tuple:: | ||||
|
r1032 | |||
|
r5000 | exc_tuple == (MyCustomException,) | ||
|
r1032 | |||
|
r5000 | handler : callable | ||
handler must have the following signature:: | ||||
|
r1032 | |||
|
r5000 | def my_handler(self, etype, value, tb, tb_offset=None): | ||
... | ||||
return structured_traceback | ||||
|
r2838 | |||
|
r5000 | Your handler must return a structured traceback (a list of strings), | ||
or None. | ||||
|
r1032 | |||
|
r5000 | This will be made into an instance method (via types.MethodType) | ||
of IPython itself, and it will be called if any of the exceptions | ||||
listed in the exc_tuple are caught. If the handler is None, an | ||||
internal basic one is used, which just prints basic info. | ||||
|
r2061 | |||
|
r5000 | To protect IPython from crashes, if your handler ever raises an | ||
exception or returns an invalid result, it will be immediately | ||||
disabled. | ||||
|
r1032 | |||
WARNING: by putting in your own exception handler into IPython's main | ||||
execution loop, you run a very good chance of nasty crashes. This | ||||
facility should only be used if you really know what you are doing.""" | ||||
assert type(exc_tuple)==type(()) , \ | ||||
"The custom exceptions must be given AS A TUPLE." | ||||
|
r4991 | def dummy_handler(self,etype,value,tb,tb_offset=None): | ||
|
r7817 | print('*** Simple custom exception handler ***') | ||
print('Exception type :',etype) | ||||
print('Exception value:',value) | ||||
print('Traceback :',tb) | ||||
|
r3756 | #print 'Source code :','\n'.join(self.buffer) | ||
|
r4999 | |||
def validate_stb(stb): | ||||
"""validate structured traceback return type | ||||
return type of CustomTB *should* be a list of strings, but allow | ||||
single strings or None, which are harmless. | ||||
This function will *always* return a list of strings, | ||||
and will raise a TypeError if stb is inappropriate. | ||||
""" | ||||
msg = "CustomTB must return list of strings, not %r" % stb | ||||
if stb is None: | ||||
return [] | ||||
|
r13353 | elif isinstance(stb, string_types): | ||
|
r4999 | return [stb] | ||
elif not isinstance(stb, list): | ||||
raise TypeError(msg) | ||||
# it's a list | ||||
for line in stb: | ||||
# check every element | ||||
|
r13353 | if not isinstance(line, string_types): | ||
|
r4999 | raise TypeError(msg) | ||
return stb | ||||
|
r2061 | |||
|
r4991 | if handler is None: | ||
wrapped = dummy_handler | ||||
else: | ||||
def wrapped(self,etype,value,tb,tb_offset=None): | ||||
|
r4999 | """wrap CustomTB handler, to protect IPython from user code | ||
This makes it harder (but not impossible) for custom exception | ||||
handlers to crash IPython. | ||||
""" | ||||
|
r4991 | try: | ||
|
r4999 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | ||
return validate_stb(stb) | ||||
|
r4991 | except: | ||
# clear custom handler immediately | ||||
self.set_custom_exc((), None) | ||||
|
r7817 | print("Custom TB Handler failed, unregistering", file=io.stderr) | ||
|
r4991 | # show the exception in handler first | ||
stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | ||||
|
r7817 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) | ||
print("The original exception:", file=io.stdout) | ||||
|
r4999 | stb = self.InteractiveTB.structured_traceback( | ||
(etype,value,tb), tb_offset=tb_offset | ||||
) | ||||
return stb | ||||
|
r4991 | |||
self.CustomTB = types.MethodType(wrapped,self) | ||||
|
r1032 | self.custom_exceptions = exc_tuple | ||
|
r2242 | def excepthook(self, etype, value, tb): | ||
"""One more defense for GUI apps that call sys.excepthook. | ||||
|
r1032 | |||
|
r2242 | GUI frameworks like wxPython trap exceptions and call | ||
sys.excepthook themselves. I guess this is a feature that | ||||
enables them to keep running after exceptions that would | ||||
otherwise kill their mainloop. This is a bother for IPython | ||||
which excepts to catch all of the program exceptions with a try: | ||||
except: statement. | ||||
|
r1032 | |||
|
r2242 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | ||
any app directly invokes sys.excepthook, it will look to the user like | ||||
IPython crashed. In order to work around this, we can disable the | ||||
CrashHandler and replace it with this excepthook instead, which prints a | ||||
regular traceback using our InteractiveTB. In this fashion, apps which | ||||
call sys.excepthook will generate a regular-looking exception from | ||||
IPython, and the CrashHandler will only be triggered by real IPython | ||||
crashes. | ||||
|
r1032 | |||
|
r2242 | This hook should be used sparingly, only in places which are not likely | ||
to be true IPython errors. | ||||
""" | ||||
self.showtraceback((etype,value,tb),tb_offset=0) | ||||
|
r1032 | |||
|
r6109 | def _get_exc_info(self, exc_tuple=None): | ||
"""get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | ||||
Ensures sys.last_type,value,traceback hold the exc_info we found, | ||||
from whichever source. | ||||
raises ValueError if none of these contain any information | ||||
""" | ||||
if exc_tuple is None: | ||||
etype, value, tb = sys.exc_info() | ||||
else: | ||||
etype, value, tb = exc_tuple | ||||
if etype is None: | ||||
if hasattr(sys, 'last_type'): | ||||
etype, value, tb = sys.last_type, sys.last_value, \ | ||||
sys.last_traceback | ||||
if etype is None: | ||||
raise ValueError("No exception to find") | ||||
# Now store the exception info in sys.last_type etc. | ||||
# WARNING: these variables are somewhat deprecated and not | ||||
# necessarily safe to use in a threaded environment, but tools | ||||
# like pdb depend on their existence, so let's set them. If we | ||||
# find problems in the field, we'll need to revisit their use. | ||||
sys.last_type = etype | ||||
sys.last_value = value | ||||
sys.last_traceback = tb | ||||
return etype, value, tb | ||||
|
r11466 | def show_usage_error(self, exc): | ||
"""Show a short message for UsageErrors | ||||
These are special exceptions that shouldn't show a traceback. | ||||
""" | ||||
self.write_err("UsageError: %s" % exc) | ||||
|
r2440 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, | ||
exception_only=False): | ||||
|
r2242 | """Display the exception that just occurred. | ||
|
r1032 | |||
|
r2242 | If nothing is known about the exception, this is the method which | ||
should be used throughout the code for presenting user tracebacks, | ||||
rather than directly invoking the InteractiveTB object. | ||||
|
r1032 | |||
|
r2242 | A specific showsyntaxerror() also exists, but this method can take | ||
care of calling it if needed, so unless you are explicitly catching a | ||||
SyntaxError exception, don't try to analyze the stack manually and | ||||
simply call this method.""" | ||||
|
r4872 | |||
|
r1032 | try: | ||
|
r6109 | try: | ||
etype, value, tb = self._get_exc_info(exc_tuple) | ||||
except ValueError: | ||||
self.write_err('No traceback available to show.\n') | ||||
return | ||||
|
r7563 | |||
|
r8417 | if issubclass(etype, SyntaxError): | ||
|
r2440 | # Though this won't be called by syntax errors in the input | ||
|
r4741 | # line, there may be SyntaxError cases with imported code. | ||
|
r2242 | self.showsyntaxerror(filename) | ||
elif etype is UsageError: | ||||
|
r11466 | self.show_usage_error(value) | ||
|
r2242 | else: | ||
|
r7109 | if exception_only: | ||
stb = ['An exception has occurred, use %tb to see ' | ||||
'the full traceback.\n'] | ||||
stb.extend(self.InteractiveTB.get_exception_only(etype, | ||||
value)) | ||||
|
r2242 | else: | ||
|
r8082 | try: | ||
# Exception classes can customise their traceback - we | ||||
# use this in IPython.parallel for exceptions occurring | ||||
# in the engines. This should return a list of strings. | ||||
stb = value._render_traceback_() | ||||
except Exception: | ||||
stb = self.InteractiveTB.structured_traceback(etype, | ||||
|
r7109 | value, tb, tb_offset=tb_offset) | ||
self._showtraceback(etype, value, stb) | ||||
if self.call_pdb: | ||||
# drop into debugger | ||||
self.debugger(force=True) | ||||
return | ||||
|
r2838 | |||
# Actually show the traceback | ||||
self._showtraceback(etype, value, stb) | ||||
|
r4872 | |||
|
r2242 | except KeyboardInterrupt: | ||
|
r2838 | self.write_err("\nKeyboardInterrupt\n") | ||
def _showtraceback(self, etype, evalue, stb): | ||||
"""Actually show a traceback. | ||||
Subclasses may override this method to put the traceback on a different | ||||
place, like a side channel. | ||||
""" | ||||
|
r7817 | print(self.InteractiveTB.stb2text(stb), file=io.stdout) | ||
|
r2205 | |||
|
r2242 | def showsyntaxerror(self, filename=None): | ||
"""Display the syntax error that just occurred. | ||||
|
r2205 | |||
|
r2242 | This doesn't display a stack trace because there isn't one. | ||
|
r2202 | |||
|
r2242 | If a filename is given, it is stuffed in the exception instead | ||
of what was there before (because Python's parser always uses | ||||
"<string>" when reading from a string). | ||||
""" | ||||
|
r6109 | etype, value, last_traceback = self._get_exc_info() | ||
|
r4872 | |||
|
r8417 | if filename and issubclass(etype, SyntaxError): | ||
|
r2242 | try: | ||
|
r5827 | value.filename = filename | ||
|
r2242 | except: | ||
# Not the format we expect; leave it alone | ||||
pass | ||||
|
r5827 | |||
|
r2838 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | ||
self._showtraceback(etype, value, stb) | ||||
|
r4872 | |||
|
r4226 | # This is overridden in TerminalInteractiveShell to show a message about | ||
# the %paste magic. | ||||
|
r4227 | def showindentationerror(self): | ||
"""Called by run_cell when there's an IndentationError in code entered | ||||
at the prompt. | ||||
|
r4872 | |||
|
r4227 | This is overridden in TerminalInteractiveShell to show a message about | ||
the %paste magic.""" | ||||
self.showsyntaxerror() | ||||
|
r1032 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to readline | ||||
#------------------------------------------------------------------------- | ||||
def init_readline(self): | ||||
"""Command history completion/saving/reloading.""" | ||||
|
r2377 | if self.readline_use: | ||
import IPython.utils.rlineimpl as readline | ||||
|
r2808 | |||
|
r2242 | self.rl_next_input = None | ||
self.rl_do_indent = False | ||||
|
r2377 | if not self.readline_use or not readline.have_readline: | ||
self.has_readline = False | ||||
|
r2242 | self.readline = None | ||
|
r2377 | # Set a number of methods that depend on readline to be no-op | ||
|
r4816 | self.readline_no_record = no_op_context | ||
|
r2952 | self.set_readline_completer = no_op | ||
|
r2377 | self.set_custom_completer = no_op | ||
|
r4816 | if self.readline_use: | ||
warn('Readline services not available or not loaded.') | ||||
|
r2242 | else: | ||
|
r2377 | self.has_readline = True | ||
self.readline = readline | ||||
|
r2242 | sys.modules['readline'] = readline | ||
|
r4872 | |||
|
r2242 | # Platform-specific configuration | ||
if os.name == 'nt': | ||||
|
r2953 | # FIXME - check with Frederick to see if we can harmonize | ||
# naming conventions with pyreadline to avoid this | ||||
# platform-dependent check | ||||
|
r2242 | self.readline_startup_hook = readline.set_pre_input_hook | ||
else: | ||||
self.readline_startup_hook = readline.set_startup_hook | ||||
# Load user's initrc file (readline config) | ||||
# Or if libedit is used, load editrc. | ||||
inputrc_name = os.environ.get('INPUTRC') | ||||
if inputrc_name is None: | ||||
|
r5384 | inputrc_name = '.inputrc' | ||
if readline.uses_libedit: | ||||
inputrc_name = '.editrc' | ||||
inputrc_name = os.path.join(self.home_dir, inputrc_name) | ||||
|
r2242 | if os.path.isfile(inputrc_name): | ||
try: | ||||
readline.read_init_file(inputrc_name) | ||||
except: | ||||
warn('Problems reading readline initialization file <%s>' | ||||
% inputrc_name) | ||||
|
r4872 | |||
|
r2242 | # Configure readline according to user's prefs | ||
# This is only done if GNU readline is being used. If libedit | ||||
# is being used (as on Leopard) the readline config is | ||||
# not run as the syntax for libedit is different. | ||||
if not readline.uses_libedit: | ||||
for rlcommand in self.readline_parse_and_bind: | ||||
#print "loading rl:",rlcommand # dbg | ||||
readline.parse_and_bind(rlcommand) | ||||
# Remove some chars from the delimiters list. If we encounter | ||||
# unicode chars, discard them. | ||||
|
r4742 | delims = readline.get_completer_delims() | ||
if not py3compat.PY3: | ||||
delims = delims.encode("ascii", "ignore") | ||||
|
r4046 | for d in self.readline_remove_delims: | ||
delims = delims.replace(d, "") | ||||
|
r2956 | delims = delims.replace(ESC_MAGIC, '') | ||
|
r2242 | readline.set_completer_delims(delims) | ||
|
r9017 | # Store these so we can restore them if something like rpy2 modifies | ||
# them. | ||||
self.readline_delims = delims | ||||
|
r2242 | # otherwise we end up with a monster history after a while: | ||
|
r3240 | readline.set_history_length(self.history_length) | ||
|
r4872 | |||
|
r3474 | self.refill_readline_hist() | ||
self.readline_no_record = ReadlineNoRecord(self) | ||||
|
r2242 | |||
# Configure auto-indent for all platforms | ||||
self.set_autoindent(self.autoindent) | ||||
|
r4872 | |||
|
r3474 | def refill_readline_hist(self): | ||
# Load the last 1000 lines from history | ||||
self.readline.clear_history() | ||||
stdin_encoding = sys.stdin.encoding or "utf-8" | ||||
|
r5307 | last_cell = u"" | ||
|
r3474 | for _, _, cell in self.history_manager.get_tail(1000, | ||
include_latest=True): | ||||
|
r5307 | # Ignore blank lines and consecutive duplicates | ||
|
r5311 | cell = cell.rstrip() | ||
if cell and (cell != last_cell): | ||||
|
r11881 | try: | ||
if self.multiline_history: | ||||
self.readline.add_history(py3compat.unicode_to_str(cell, | ||||
stdin_encoding)) | ||||
else: | ||||
for line in cell.splitlines(): | ||||
self.readline.add_history(py3compat.unicode_to_str(line, | ||||
stdin_encoding)) | ||||
last_cell = cell | ||||
except TypeError: | ||||
# The history DB can get corrupted so it returns strings | ||||
# containing null bytes, which readline objects to. | ||||
continue | ||||
|
r2205 | |||
|
r9246 | @skip_doctest | ||
|
r2205 | def set_next_input(self, s): | ||
""" Sets the 'default' input string for the next command line. | ||||
|
r4872 | |||
|
r2205 | Requires readline. | ||
|
r4872 | |||
|
r9244 | Example:: | ||
|
r4872 | |||
|
r9244 | In [1]: _ip.set_next_input("Hello Word") | ||
In [2]: Hello Word_ # cursor is here | ||||
|
r2205 | """ | ||
|
r5308 | self.rl_next_input = py3compat.cast_bytes_py2(s) | ||
|
r2205 | |||
|
r2761 | # Maybe move this to the terminal subclass? | ||
|
r2242 | def pre_readline(self): | ||
"""readline hook to be used at the start of each line. | ||||
|
r1032 | |||
|
r2242 | Currently it handles auto-indent only.""" | ||
|
r1032 | |||
|
r2242 | if self.rl_do_indent: | ||
self.readline.insert_text(self._indent_current_str()) | ||||
if self.rl_next_input is not None: | ||||
self.readline.insert_text(self.rl_next_input) | ||||
self.rl_next_input = None | ||||
|
r1032 | |||
|
r2242 | def _indent_current_str(self): | ||
"""return the current level of indentation as a string""" | ||||
|
r3122 | return self.input_splitter.indent_spaces * ' ' | ||
|
r1032 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
|
r2956 | # Things related to text completion | ||
#------------------------------------------------------------------------- | ||||
def init_completer(self): | ||||
"""Initialize the completion machinery. | ||||
This creates completion machinery that can be used by client code, | ||||
either interactively in-process (typically triggered by the readline | ||||
library), programatically (such as in test suites) or out-of-prcess | ||||
(typically over the network by remote frontends). | ||||
""" | ||||
from IPython.core.completer import IPCompleter | ||||
|
r2959 | from IPython.core.completerlib import (module_completer, | ||
|
r5965 | magic_run_completer, cd_completer, reset_completer) | ||
|
r4872 | |||
|
r4825 | self.Completer = IPCompleter(shell=self, | ||
namespace=self.user_ns, | ||||
global_namespace=self.user_global_ns, | ||||
use_readline=self.has_readline, | ||||
|
r11064 | parent=self, | ||
|
r4825 | ) | ||
|
r5225 | self.configurables.append(self.Completer) | ||
|
r4872 | |||
|
r2959 | # Add custom completers to the basic ones built into IPCompleter | ||
|
r2956 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | ||
self.strdispatchers['complete_command'] = sdisp | ||||
self.Completer.custom_completers = sdisp | ||||
|
r2959 | self.set_hook('complete_command', module_completer, str_key = 'import') | ||
self.set_hook('complete_command', module_completer, str_key = 'from') | ||||
self.set_hook('complete_command', magic_run_completer, str_key = '%run') | ||||
self.set_hook('complete_command', cd_completer, str_key = '%cd') | ||||
|
r5965 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | ||
|
r2959 | |||
# Only configure readline if we truly are using readline. IPython can | ||||
# do tab-completion over the network, in GUIs, etc, where readline | ||||
# itself may be absent | ||||
|
r2956 | if self.has_readline: | ||
self.set_readline_completer() | ||||
def complete(self, text, line=None, cursor_pos=None): | ||||
"""Return the completed text and a list of completions. | ||||
Parameters | ||||
---------- | ||||
text : string | ||||
A string of text to be completed on. It can be given as empty and | ||||
instead a line/position pair are given. In this case, the | ||||
completer itself will split the line like readline does. | ||||
line : string, optional | ||||
The complete line that text is part of. | ||||
cursor_pos : int, optional | ||||
The position of the cursor on the input line. | ||||
Returns | ||||
------- | ||||
text : string | ||||
The actual text that was completed. | ||||
matches : list | ||||
A sorted list with all possible completions. | ||||
The optional arguments allow the completion to take more context into | ||||
account, and are part of the low-level completion API. | ||||
|
r4872 | |||
|
r2956 | This is a wrapper around the completion mechanism, similar to what | ||
readline does at the command line when the TAB key is hit. By | ||||
exposing it as a method, it can be used by other non-readline | ||||
environments (such as GUIs) for text completion. | ||||
Simple usage example: | ||||
In [1]: x = 'hello' | ||||
In [2]: _ip.complete('x.l') | ||||
Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | ||||
""" | ||||
# Inject names into __builtin__ so we can complete on the added names. | ||||
with self.builtin_trap: | ||||
return self.Completer.complete(text, line, cursor_pos) | ||||
def set_custom_completer(self, completer, pos=0): | ||||
"""Adds a new custom completer function. | ||||
The position argument (defaults to 0) is the index in the completers | ||||
list where you want the completer to be inserted.""" | ||||
|
r3158 | newcomp = types.MethodType(completer,self.Completer) | ||
|
r2956 | self.Completer.matchers.insert(pos,newcomp) | ||
def set_readline_completer(self): | ||||
"""Reset readline's completer to be our own.""" | ||||
self.readline.set_completer(self.Completer.rlcomplete) | ||||
def set_completer_frame(self, frame=None): | ||||
"""Set the frame of the completer.""" | ||||
if frame: | ||||
self.Completer.namespace = frame.f_locals | ||||
self.Completer.global_namespace = frame.f_globals | ||||
else: | ||||
self.Completer.namespace = self.user_ns | ||||
self.Completer.global_namespace = self.user_global_ns | ||||
#------------------------------------------------------------------------- | ||||
|
r2242 | # Things related to magics | ||
#------------------------------------------------------------------------- | ||||
|
r1032 | |||
|
r2242 | def init_magics(self): | ||
|
r6956 | from IPython.core import magics as m | ||
|
r6923 | self.magics_manager = magic.MagicsManager(shell=self, | ||
|
r11064 | parent=self, | ||
|
r6957 | user_magics=m.UserMagics(self)) | ||
|
r6923 | self.configurables.append(self.magics_manager) | ||
|
r6936 | # Expose as public API from the magics manager | ||
|
r6933 | self.register_magics = self.magics_manager.register | ||
|
r6954 | self.define_magic = self.magics_manager.define_magic | ||
|
r6933 | |||
|
r6964 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | ||
|
r7946 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, | ||
|
r6967 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | ||
|
r7299 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, | ||
) | ||||
|
r6923 | |||
|
r7933 | # Register Magic Aliases | ||
mman = self.magics_manager | ||||
|
r10655 | # FIXME: magic aliases should be defined by the Magics classes | ||
# or in MagicsManager, not here | ||||
|
r7933 | mman.register_alias('ed', 'edit') | ||
mman.register_alias('hist', 'history') | ||||
mman.register_alias('rep', 'recall') | ||||
|
r10000 | mman.register_alias('SVG', 'svg', 'cell') | ||
mman.register_alias('HTML', 'html', 'cell') | ||||
|
r10634 | mman.register_alias('file', 'writefile', 'cell') | ||
|
r7933 | |||
|
r2781 | # FIXME: Move the color initialization to the DisplayHook, which | ||
# should be split into a prompt manager and displayhook. We probably | ||||
# even need a centralize colors management object. | ||||
|
r6925 | self.magic('colors %s' % self.colors) | ||
|
r12297 | |||
# Defined here so that it's included in the documentation | ||||
@functools.wraps(magic.MagicsManager.register_function) | ||||
def register_magic_function(self, func, magic_kind='line', magic_name=None): | ||||
self.magics_manager.register_function(func, | ||||
magic_kind=magic_kind, magic_name=magic_name) | ||||
|
r1032 | |||
|
r7003 | def run_line_magic(self, magic_name, line): | ||
|
r6953 | """Execute the given line magic. | ||
|
r1859 | |||
|
r6953 | Parameters | ||
---------- | ||||
magic_name : str | ||||
Name of the desired magic function, without '%' prefix. | ||||
|
r1859 | |||
|
r6953 | line : str | ||
The rest of the input line as a single string. | ||||
|
r2242 | """ | ||
|
r6953 | fn = self.find_line_magic(magic_name) | ||
|
r2242 | if fn is None: | ||
|
r6978 | cm = self.find_cell_magic(magic_name) | ||
|
r6983 | etpl = "Line magic function `%%%s` not found%s." | ||
extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | ||||
'did you mean that instead?)' % magic_name ) | ||||
error(etpl % (magic_name, extra)) | ||||
|
r2242 | else: | ||
|
r6953 | # Note: this is the distance in the stack to the user's frame. | ||
# This will need to be updated if the internal calling logic gets | ||||
# refactored, or else we'll be expanding the wrong variables. | ||||
stack_depth = 2 | ||||
magic_arg_s = self.var_expand(line, stack_depth) | ||||
|
r6907 | # Put magic args in a list so we can call with f(*a) syntax | ||
args = [magic_arg_s] | ||||
|
r8262 | kwargs = {} | ||
|
r3479 | # Grab local namespace if we need it: | ||
if getattr(fn, "needs_local_scope", False): | ||||
|
r8262 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals | ||
|
r3732 | with self.builtin_trap: | ||
|
r8262 | result = fn(*args,**kwargs) | ||
|
r3479 | return result | ||
|
r1032 | |||
|
r7003 | def run_cell_magic(self, magic_name, line, cell): | ||
|
r6953 | """Execute the given cell magic. | ||
|
r7003 | |||
Parameters | ||||
---------- | ||||
magic_name : str | ||||
Name of the desired magic function, without '%' prefix. | ||||
line : str | ||||
The rest of the first input line as a single string. | ||||
cell : str | ||||
The body of the cell as a (possibly multiline) string. | ||||
|
r6953 | """ | ||
fn = self.find_cell_magic(magic_name) | ||||
if fn is None: | ||||
|
r6983 | lm = self.find_line_magic(magic_name) | ||
|
r10227 | etpl = "Cell magic `%%{0}` not found{1}." | ||
|
r9469 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' | ||
'did you mean that instead?)'.format(magic_name)) | ||||
error(etpl.format(magic_name, extra)) | ||||
|
r9466 | elif cell == '': | ||
|
r10227 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) | ||
if self.find_line_magic(magic_name) is not None: | ||||
message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) | ||||
raise UsageError(message) | ||||
|
r6953 | else: | ||
# Note: this is the distance in the stack to the user's frame. | ||||
# This will need to be updated if the internal calling logic gets | ||||
# refactored, or else we'll be expanding the wrong variables. | ||||
stack_depth = 2 | ||||
magic_arg_s = self.var_expand(line, stack_depth) | ||||
with self.builtin_trap: | ||||
|
r7902 | result = fn(magic_arg_s, cell) | ||
|
r6953 | return result | ||
|
r6923 | def find_line_magic(self, magic_name): | ||
|
r6953 | """Find and return a line magic by name. | ||
Returns None if the magic isn't found.""" | ||||
|
r6923 | return self.magics_manager.magics['line'].get(magic_name) | ||
def find_cell_magic(self, magic_name): | ||||
|
r6953 | """Find and return a cell magic by name. | ||
Returns None if the magic isn't found.""" | ||||
|
r6923 | return self.magics_manager.magics['cell'].get(magic_name) | ||
|
r6974 | def find_magic(self, magic_name, magic_kind='line'): | ||
|
r6953 | """Find and return a magic of the given type by name. | ||
Returns None if the magic isn't found.""" | ||||
|
r6974 | return self.magics_manager.magics[magic_kind].get(magic_name) | ||
|
r6906 | |||
|
r6974 | def magic(self, arg_s): | ||
|
r7003 | """DEPRECATED. Use run_line_magic() instead. | ||
|
r6953 | |||
Call a magic function by name. | ||||
Input: a string containing the name of the magic function to call and | ||||
any additional arguments to be passed to the magic. | ||||
magic('name -opt foo bar') is equivalent to typing at the ipython | ||||
prompt: | ||||
In[1]: %name -opt foo bar | ||||
To call a magic without arguments, simply use magic('name'). | ||||
This provides a proper Python function to call IPython's magics in any | ||||
valid Python code you can type at the interpreter, including loops and | ||||
compound statements. | ||||
""" | ||||
# TODO: should we issue a loud deprecation warning here? | ||||
magic_name, _, magic_arg_s = arg_s.partition(' ') | ||||
magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | ||||
|
r7003 | return self.run_line_magic(magic_name, magic_arg_s) | ||
|
r6953 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to macros | ||||
#------------------------------------------------------------------------- | ||||
|
r1032 | |||
|
r2242 | def define_macro(self, name, themacro): | ||
"""Define a new macro | ||||
|
r1856 | |||
|
r2242 | Parameters | ||
---------- | ||||
name : str | ||||
The name of the macro. | ||||
themacro : str or Macro | ||||
|
r4872 | The action to do upon invoking the macro. If a string, a new | ||
|
r2242 | Macro object is created by passing the string to it. | ||
|
r1916 | """ | ||
|
r4872 | |||
|
r2242 | from IPython.core import macro | ||
|
r1916 | |||
|
r13353 | if isinstance(themacro, string_types): | ||
|
r2242 | themacro = macro.Macro(themacro) | ||
if not isinstance(themacro, macro.Macro): | ||||
raise ValueError('A macro must be a string or a Macro instance.') | ||||
self.user_ns[name] = themacro | ||||
|
r1856 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to the running of system commands | ||||
#------------------------------------------------------------------------- | ||||
|
r1856 | |||
|
r3909 | def system_piped(self, cmd): | ||
"""Call the given cmd in a subprocess, piping stdout/err | ||||
|
r3002 | |||
Parameters | ||||
---------- | ||||
cmd : str | ||||
|
r3909 | Command to execute (can not end in '&', as background processes are | ||
not supported. Should not be a command that expects input | ||||
other than simple text. | ||||
|
r3002 | """ | ||
|
r3909 | if cmd.rstrip().endswith('&'): | ||
# this is *far* from a rigorous test | ||||
# We do not support backgrounding processes because we either use | ||||
# pexpect or pipes to read from. Users can always just call | ||||
# os.system() or use ip.system=ip.system_raw | ||||
# if they really want a background process. | ||||
|
r2905 | raise OSError("Background processes not supported.") | ||
|
r4872 | |||
|
r3910 | # we explicitly do NOT return the subprocess status code, because | ||
# a non-None value would trigger :func:`sys.displayhook` calls. | ||||
# Instead, we store the exit_code in user_ns. | ||||
|
r7332 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | ||
|
r4872 | |||
|
r3909 | def system_raw(self, cmd): | ||
|
r12366 | """Call the given cmd in a subprocess using os.system on Windows or | ||
subprocess.call using the system shell on other platforms. | ||||
|
r4872 | |||
|
r3909 | Parameters | ||
---------- | ||||
cmd : str | ||||
Command to execute. | ||||
""" | ||||
|
r7332 | cmd = self.var_expand(cmd, depth=1) | ||
|
r5277 | # protect os.system from UNC paths on Windows, which it can't handle: | ||
if sys.platform == 'win32': | ||||
from IPython.utils._process_win32 import AvoidUNCPath | ||||
with AvoidUNCPath() as path: | ||||
if path is not None: | ||||
cmd = '"pushd %s &&"%s' % (path, cmd) | ||||
|
r5313 | cmd = py3compat.unicode_to_str(cmd) | ||
|
r5277 | ec = os.system(cmd) | ||
else: | ||||
|
r5314 | cmd = py3compat.unicode_to_str(cmd) | ||
|
r12366 | # Call the cmd using the OS shell, instead of the default /bin/sh, if set. | ||
ec = subprocess.call(cmd, shell=True, executable=os.environ.get('SHELL', None)) | ||||
|
r12766 | # exit code is positive for program failure, or negative for | ||
# terminating signal number. | ||||
|
r5277 | |||
|
r3910 | # We explicitly do NOT return the subprocess status code, because | ||
# a non-None value would trigger :func:`sys.displayhook` calls. | ||||
# Instead, we store the exit_code in user_ns. | ||||
|
r5277 | self.user_ns['_exit_code'] = ec | ||
|
r4872 | |||
|
r3909 | # use piped system by default, because it is better behaved | ||
system = system_piped | ||||
|
r2905 | |||
|
r7400 | def getoutput(self, cmd, split=True, depth=0): | ||
|
r3002 | """Get output (possibly including stderr) from a subprocess. | ||
Parameters | ||||
---------- | ||||
cmd : str | ||||
|
r3005 | Command to execute (can not end in '&', as background processes are | ||
|
r3002 | not supported. | ||
split : bool, optional | ||||
If True, split the output into an IPython SList. Otherwise, an | ||||
IPython LSString is returned. These are objects similar to normal | ||||
lists and strings, with a few convenience attributes for easier | ||||
manipulation of line-based output. You can use '?' on them for | ||||
details. | ||||
|
r7400 | depth : int, optional | ||
How many frames above the caller are the local variables which should | ||||
be expanded in the command string? The default (0) assumes that the | ||||
expansion variables are in the stack frame calling this function. | ||||
""" | ||||
|
r3909 | if cmd.rstrip().endswith('&'): | ||
# this is *far* from a rigorous test | ||||
|
r2905 | raise OSError("Background processes not supported.") | ||
|
r7400 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | ||
|
r3002 | if split: | ||
out = SList(out.splitlines()) | ||||
else: | ||||
out = LSString(out) | ||||
return out | ||||
|
r2905 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
# Things related to aliases | ||||
#------------------------------------------------------------------------- | ||||
|
r2243 | def init_alias(self): | ||
|
r11064 | self.alias_manager = AliasManager(shell=self, parent=self) | ||
|
r5225 | self.configurables.append(self.alias_manager) | ||
|
r1032 | |||
|
r2242 | #------------------------------------------------------------------------- | ||
|
r8197 | # Things related to extensions | ||
|
r2731 | #------------------------------------------------------------------------- | ||
def init_extension_manager(self): | ||||
|
r11064 | self.extension_manager = ExtensionManager(shell=self, parent=self) | ||
|
r5225 | self.configurables.append(self.extension_manager) | ||
|
r2731 | |||
#------------------------------------------------------------------------- | ||||
|
r2810 | # Things related to payloads | ||
#------------------------------------------------------------------------- | ||||
def init_payload(self): | ||||
|
r11064 | self.payload_manager = PayloadManager(parent=self) | ||
|
r5225 | self.configurables.append(self.payload_manager) | ||
|
r13188 | |||
#------------------------------------------------------------------------- | ||||
# Things related to widgets | ||||
#------------------------------------------------------------------------- | ||||
|
r13218 | def init_comms(self): | ||
|
r13188 | # not implemented in the base class | ||
pass | ||||
|
r2810 | #------------------------------------------------------------------------- | ||
|
r2761 | # Things related to the prefilter | ||
#------------------------------------------------------------------------- | ||||
def init_prefilter(self): | ||||
|
r11064 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) | ||
|
r5225 | self.configurables.append(self.prefilter_manager) | ||
|
r2761 | # Ultimately this will be refactored in the new interpreter code, but | ||
# for now, we should expose the main prefilter method (there's legacy | ||||
# code out there that may rely on this). | ||||
self.prefilter = self.prefilter_manager.prefilter_lines | ||||
|
r2951 | def auto_rewrite_input(self, cmd): | ||
"""Print to the screen the rewritten form of the user's command. | ||||
|
r2954 | This shows visual feedback by rewriting input lines that cause | ||
automatic calling to kick in, like:: | ||||
/f x | ||||
into:: | ||||
|
r2951 | |||
|
r2954 | ------> f(x) | ||
|
r4872 | |||
|
r2954 | after the user's input prompt. This helps the user understand that the | ||
input line was transformed automatically by IPython. | ||||
|
r2951 | """ | ||
|
r5555 | if not self.show_rewritten_input: | ||
return | ||||
|
r5495 | rw = self.prompt_manager.render('rewrite') + cmd | ||
|
r2951 | |||
try: | ||||
# plain ascii works better w/ pyreadline, on some machines, so | ||||
# we use it and only print uncolored rewrite if we have unicode | ||||
rw = str(rw) | ||||
|
r7817 | print(rw, file=io.stdout) | ||
|
r2951 | except UnicodeEncodeError: | ||
|
r7817 | print("------> " + cmd) | ||
|
r4872 | |||
|
r2761 | #------------------------------------------------------------------------- | ||
|
r2926 | # Things related to extracting values/expressions from kernel and user_ns | ||
#------------------------------------------------------------------------- | ||||
|
r10636 | def _user_obj_error(self): | ||
"""return simple exception dict | ||||
for use in user_variables / expressions | ||||
""" | ||||
etype, evalue, tb = self._get_exc_info() | ||||
stb = self.InteractiveTB.get_exception_only(etype, evalue) | ||||
exc_info = { | ||||
u'status' : 'error', | ||||
u'traceback' : stb, | ||||
|
r13353 | u'ename' : unicode_type(etype.__name__), | ||
|
r10636 | u'evalue' : py3compat.safe_unicode(evalue), | ||
} | ||||
|
r2926 | |||
|
r10636 | return exc_info | ||
def _format_user_obj(self, obj): | ||||
"""format a user object to display dict | ||||
for use in user_expressions / variables | ||||
""" | ||||
data, md = self.display_formatter.format(obj) | ||||
value = { | ||||
'status' : 'ok', | ||||
'data' : data, | ||||
'metadata' : md, | ||||
} | ||||
return value | ||||
|
r3050 | def user_variables(self, names): | ||
|
r2926 | """Get a list of variable names from the user's namespace. | ||
|
r3050 | Parameters | ||
---------- | ||||
names : list of strings | ||||
A list of names of variables to be read from the user namespace. | ||||
Returns | ||||
------- | ||||
|
r10636 | A dict, keyed by the input names and with the rich mime-type repr(s) of each value. | ||
Each element will be a sub-dict of the same form as a display_data message. | ||||
|
r2926 | """ | ||
out = {} | ||||
user_ns = self.user_ns | ||||
|
r10636 | |||
|
r2926 | for varname in names: | ||
try: | ||||
|
r10636 | value = self._format_user_obj(user_ns[varname]) | ||
|
r2926 | except: | ||
|
r10636 | value = self._user_obj_error() | ||
|
r2926 | out[varname] = value | ||
return out | ||||
|
r4872 | |||
|
r3050 | def user_expressions(self, expressions): | ||
|
r2926 | """Evaluate a dict of expressions in the user's namespace. | ||
|
r3050 | Parameters | ||
---------- | ||||
expressions : dict | ||||
A dict with string keys and string values. The expression values | ||||
should be valid Python expressions, each of which will be evaluated | ||||
in the user namespace. | ||||
|
r4872 | |||
|
r3050 | Returns | ||
------- | ||||
|
r10636 | A dict, keyed like the input expressions dict, with the rich mime-typed | ||
display_data of each value. | ||||
|
r2926 | """ | ||
out = {} | ||||
user_ns = self.user_ns | ||||
global_ns = self.user_global_ns | ||||
|
r10636 | |||
|
r13361 | for key, expr in iteritems(expressions): | ||
|
r2926 | try: | ||
|
r10636 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) | ||
|
r2926 | except: | ||
|
r10636 | value = self._user_obj_error() | ||
|
r2926 | out[key] = value | ||
return out | ||||
#------------------------------------------------------------------------- | ||||
|
r2242 | # Things related to the running of code | ||
#------------------------------------------------------------------------- | ||||
|
r1032 | |||
|
r2242 | def ex(self, cmd): | ||
"""Execute a normal python statement in user namespace.""" | ||||
|
r3732 | with self.builtin_trap: | ||
|
r13350 | exec(cmd, self.user_global_ns, self.user_ns) | ||
|
r1032 | |||
|
r2242 | def ev(self, expr): | ||
"""Evaluate python expression expr in user namespace. | ||||
|
r1032 | |||
|
r2242 | Returns the result of evaluation | ||
""" | ||||
|
r3732 | with self.builtin_trap: | ||
|
r2242 | return eval(expr, self.user_global_ns, self.user_ns) | ||
|
r1032 | |||
|
r2252 | def safe_execfile(self, fname, *where, **kw): | ||
|
r2242 | """A safe version of the builtin execfile(). | ||
|
r1032 | |||
|
r2252 | This version will never throw an exception, but instead print | ||
helpful error messages to the screen. This only works on pure | ||||
Python files with the .py extension. | ||||
|
r2205 | |||
|
r2252 | Parameters | ||
---------- | ||||
fname : string | ||||
The name of the file to be executed. | ||||
where : tuple | ||||
|
r2242 | One or two namespaces, passed to execfile() as (globals,locals). | ||
If only one is given, it is passed as both. | ||||
|
r2252 | exit_ignore : bool (False) | ||
|
r2440 | If True, then silence SystemExit for non-zero status (it is always | ||
silenced for zero status, as it is so common). | ||||
|
r5094 | raise_exceptions : bool (False) | ||
If True raise exceptions everywhere. Meant for testing. | ||||
|
r2252 | """ | ||
kw.setdefault('exit_ignore', False) | ||||
|
r5094 | kw.setdefault('raise_exceptions', False) | ||
|
r1032 | |||
|
r2252 | fname = os.path.abspath(os.path.expanduser(fname)) | ||
|
r1032 | |||
|
r2252 | # Make sure we can open the file | ||
try: | ||||
with open(fname) as thefile: | ||||
pass | ||||
except: | ||||
warn('Could not open file <%s> for safe execution.' % fname) | ||||
return | ||||
|
r2205 | |||
|
r2242 | # Find things also in current directory. This is needed to mimic the | ||
# behavior of running a script from the system command line, where | ||||
# Python inserts the script's directory into sys.path | ||||
|
r2252 | dname = os.path.dirname(fname) | ||
|
r2242 | |||
|
r2252 | with prepended_to_syspath(dname): | ||
|
r2242 | try: | ||
|
r4731 | py3compat.execfile(fname,*where) | ||
|
r7787 | except SystemExit as status: | ||
|
r2440 | # If the call was made with 0 or None exit status (sys.exit(0) | ||
# or sys.exit() ), don't bother showing a traceback, as both of | ||||
# these are considered normal by the OS: | ||||
# > python -c'import sys;sys.exit(0)'; echo $? | ||||
# 0 | ||||
# > python -c'import sys;sys.exit()'; echo $? | ||||
# 0 | ||||
# For other exit status, we show the exception unless | ||||
# explicitly silenced, but only in short form. | ||||
|
r5094 | if kw['raise_exceptions']: | ||
raise | ||||
|
r9679 | if status.code and not kw['exit_ignore']: | ||
|
r2440 | self.showtraceback(exception_only=True) | ||
|
r2242 | except: | ||
|
r5094 | if kw['raise_exceptions']: | ||
raise | ||||
|
r14730 | # tb offset is 2 because we wrap execfile | ||
self.showtraceback(tb_offset=2) | ||||
|
r2242 | |||
|
r2252 | def safe_execfile_ipy(self, fname): | ||
|
r13644 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. | ||
|
r2252 | |||
Parameters | ||||
---------- | ||||
fname : str | ||||
The name of the file to execute. The filename must have a | ||||
|
r13644 | .ipy or .ipynb extension. | ||
|
r2252 | """ | ||
fname = os.path.abspath(os.path.expanduser(fname)) | ||||
# Make sure we can open the file | ||||
try: | ||||
with open(fname) as thefile: | ||||
pass | ||||
except: | ||||
warn('Could not open file <%s> for safe execution.' % fname) | ||||
return | ||||
# Find things also in current directory. This is needed to mimic the | ||||
# behavior of running a script from the system command line, where | ||||
# Python inserts the script's directory into sys.path | ||||
dname = os.path.dirname(fname) | ||||
|
r13644 | |||
def get_cells(): | ||||
"""generator for sequence of code blocks to run""" | ||||
if fname.endswith('.ipynb'): | ||||
from IPython.nbformat import current | ||||
with open(fname) as f: | ||||
nb = current.read(f, 'json') | ||||
if not nb.worksheets: | ||||
return | ||||
for cell in nb.worksheets[0].cells: | ||||
|
r13747 | if cell.cell_type == 'code': | ||
yield cell.input | ||||
|
r13644 | else: | ||
with open(fname) as f: | ||||
yield f.read() | ||||
|
r2252 | |||
with prepended_to_syspath(dname): | ||||
try: | ||||
|
r13644 | for cell in get_cells(): | ||
|
r3122 | # self.run_cell currently captures all exceptions | ||
# raised in user code. It would be nice if there were | ||||
|
r13644 | # versions of run_cell that did raise, so | ||
|
r2252 | # we could catch the errors. | ||
|
r13644 | self.run_cell(cell, store_history=False, shell_futures=False) | ||
|
r2252 | except: | ||
self.showtraceback() | ||||
warn('Unknown failure executing file: <%s>' % fname) | ||||
|
r4872 | |||
|
r6029 | def safe_run_module(self, mod_name, where): | ||
"""A safe version of runpy.run_module(). | ||||
This version will never throw an exception, but instead print | ||||
helpful error messages to the screen. | ||||
|
r9679 | `SystemExit` exceptions with status code 0 or None are ignored. | ||
|
r6029 | Parameters | ||
---------- | ||||
mod_name : string | ||||
The name of the module to be executed. | ||||
where : dict | ||||
The globals namespace. | ||||
""" | ||||
try: | ||||
|
r9679 | try: | ||
where.update( | ||||
runpy.run_module(str(mod_name), run_name="__main__", | ||||
alter_sys=True) | ||||
) | ||||
except SystemExit as status: | ||||
if status.code: | ||||
raise | ||||
|
r6029 | except: | ||
self.showtraceback() | ||||
warn('Unknown failure executing module: <%s>' % mod_name) | ||||
|
r7003 | def _run_cached_cell_magic(self, magic_name, line): | ||
|
r6978 | """Special method to call a cell magic with the data stored in self. | ||
""" | ||||
|
r6976 | cell = self._current_cell_magic_body | ||
self._current_cell_magic_body = None | ||||
|
r7003 | return self.run_cell_magic(magic_name, line, cell) | ||
|
r6976 | |||
|
r9081 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): | ||
|
r3527 | """Run a complete IPython cell. | ||
|
r4872 | |||
|
r3527 | Parameters | ||
---------- | ||||
|
r3709 | raw_cell : str | ||
|
r3527 | The code (including IPython code such as %magic functions) to run. | ||
store_history : bool | ||||
If True, the raw and translated cell will be stored in IPython's | ||||
history. For user code calling back into IPython's machinery, this | ||||
should be set to False. | ||||
|
r6802 | silent : bool | ||
|
r7999 | If True, avoid side-effects, such as implicit displayhooks and | ||
|
r6802 | and logging. silent=True forces store_history=False. | ||
|
r9081 | shell_futures : bool | ||
If True, the code will share future statements with the interactive | ||||
shell. It will both be affected by previous __future__ imports, and | ||||
any __future__ imports in the code will affect the shell. If False, | ||||
__future__ imports are not shared in either direction. | ||||
|
r3527 | """ | ||
|
r3710 | if (not raw_cell) or raw_cell.isspace(): | ||
|
r3706 | return | ||
|
r6802 | |||
if silent: | ||||
store_history = False | ||||
|
r11467 | |||
|
r15605 | self.events.trigger('pre_execute') | ||
|
r15597 | if not silent: | ||
|
r15607 | self.events.trigger('pre_run_cell') | ||
|
r15597 | |||
|
r14989 | # If any of our input transformation (input_transformer_manager or | ||
# prefilter_manager) raises an exception, we store it in this variable | ||||
# so that we can display the error after logging the input and storing | ||||
# it in the history. | ||||
|
r13526 | preprocessing_exc_tuple = None | ||
|
r13525 | try: | ||
|
r13526 | # Static input transformations | ||
|
r13912 | cell = self.input_transformer_manager.transform_cell(raw_cell) | ||
|
r13525 | except SyntaxError: | ||
|
r13526 | preprocessing_exc_tuple = sys.exc_info() | ||
cell = raw_cell # cell has to exist so it can be stored/logged | ||||
else: | ||||
if len(cell.splitlines()) == 1: | ||||
# Dynamic transformations - only applied for single line commands | ||||
with self.builtin_trap: | ||||
try: | ||||
# use prefilter_lines to handle trailing newlines | ||||
# restore trailing newline for ast.parse | ||||
cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | ||||
except Exception: | ||||
# don't allow prefilter errors to crash IPython | ||||
preprocessing_exc_tuple = sys.exc_info() | ||||
# Store raw and processed history | ||||
if store_history: | ||||
self.history_manager.store_inputs(self.execution_count, | ||||
cell, raw_cell) | ||||
if not silent: | ||||
self.logger.log(cell, raw_cell) | ||||
# Display the exception if input processing failed. | ||||
if preprocessing_exc_tuple is not None: | ||||
self.showtraceback(preprocessing_exc_tuple) | ||||
if store_history: | ||||
self.execution_count += 1 | ||||
return | ||||
|
r11467 | |||
|
r9081 | # Our own compiler remembers the __future__ environment. If we want to | ||
# run code with a separate __future__ environment, use the default | ||||
# compiler | ||||
|
r9140 | compiler = self.compile if shell_futures else CachingCompiler() | ||
|
r4872 | |||
|
r3527 | with self.builtin_trap: | ||
|
r13526 | cell_name = self.compile.cache(cell, self.execution_count) | ||
|
r4872 | |||
|
r13526 | with self.display_trap: | ||
# Compile to bytecode | ||||
try: | ||||
code_ast = compiler.ast_parse(cell, filename=cell_name) | ||||
except IndentationError: | ||||
self.showindentationerror() | ||||
if store_history: | ||||
self.execution_count += 1 | ||||
return None | ||||
except (OverflowError, SyntaxError, ValueError, TypeError, | ||||
MemoryError): | ||||
self.showsyntaxerror() | ||||
if store_history: | ||||
self.execution_count += 1 | ||||
return None | ||||
# Apply AST transformations | ||||
code_ast = self.transform_ast(code_ast) | ||||
# Execute the user code | ||||
interactivity = "none" if silent else self.ast_node_interactivity | ||||
self.run_ast_nodes(code_ast.body, cell_name, | ||||
interactivity=interactivity, compiler=compiler) | ||||
|
r15597 | |||
|
r15605 | self.events.trigger('post_execute') | ||
|
r15597 | if not silent: | ||
|
r15607 | self.events.trigger('post_run_cell') | ||
|
r4872 | |||
|
r3527 | if store_history: | ||
# Write output to the database. Does nothing unless | ||||
# history output logging is enabled. | ||||
self.history_manager.store_output(self.execution_count) | ||||
# Each cell is a *single* input, regardless of how many lines it has | ||||
self.execution_count += 1 | ||||
|
r8220 | |||
def transform_ast(self, node): | ||||
"""Apply the AST transformations from self.ast_transformers | ||||
Parameters | ||||
---------- | ||||
node : ast.Node | ||||
The root node to be transformed. Typically called with the ast.Module | ||||
produced by parsing user input. | ||||
Returns | ||||
------- | ||||
An ast.Node corresponding to the node it was called with. Note that it | ||||
may also modify the passed object, so don't rely on references to the | ||||
original AST. | ||||
""" | ||||
for transformer in self.ast_transformers: | ||||
try: | ||||
node = transformer.visit(node) | ||||
except Exception: | ||||
|
r8222 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) | ||
|
r8220 | self.ast_transformers.remove(transformer) | ||
|
r9482 | if self.ast_transformers: | ||
ast.fix_missing_locations(node) | ||||
return node | ||||
|
r8220 | |||
|
r4872 | |||
|
r9081 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', | ||
compiler=compile): | ||||
|
r3527 | """Run a sequence of AST nodes. The execution mode depends on the | ||
interactivity parameter. | ||||
|
r4872 | |||
|
r3527 | Parameters | ||
---------- | ||||
nodelist : list | ||||
A sequence of AST nodes to run. | ||||
|
r3533 | cell_name : str | ||
Will be passed to the compiler as the filename of the cell. Typically | ||||
the value returned by ip.compile.cache(cell). | ||||
interactivity : str | ||||
|
r3740 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | ||
run interactively (displaying output from expressions). 'last_expr' | ||||
will run the last node interactively only if it is an expression (i.e. | ||||
expressions in loops or other blocks are not displayed. Other values | ||||
for this parameter will raise a ValueError. | ||||
|
r9081 | compiler : callable | ||
A function with the same interface as the built-in compile(), to turn | ||||
the AST nodes into code objects. Default is the built-in compile(). | ||||
|
r3527 | """ | ||
if not nodelist: | ||||
return | ||||
|
r4872 | |||
|
r3740 | if interactivity == 'last_expr': | ||
if isinstance(nodelist[-1], ast.Expr): | ||||
interactivity = "last" | ||||
else: | ||||
interactivity = "none" | ||||
|
r4872 | |||
|
r3533 | if interactivity == 'none': | ||
|
r3527 | to_run_exec, to_run_interactive = nodelist, [] | ||
|
r3533 | elif interactivity == 'last': | ||
|
r3527 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | ||
|
r3533 | elif interactivity == 'all': | ||
|
r3527 | to_run_exec, to_run_interactive = [], nodelist | ||
|
r3533 | else: | ||
raise ValueError("Interactivity was %r" % interactivity) | ||||
|
r4872 | |||
|
r3529 | exec_count = self.execution_count | ||
|
r4566 | |||
try: | ||||
for i, node in enumerate(to_run_exec): | ||||
mod = ast.Module([node]) | ||||
|
r9081 | code = compiler(mod, cell_name, "exec") | ||
|
r6644 | if self.run_code(code): | ||
|
r4566 | return True | ||
for i, node in enumerate(to_run_interactive): | ||||
mod = ast.Interactive([node]) | ||||
|
r9081 | code = compiler(mod, cell_name, "single") | ||
|
r6644 | if self.run_code(code): | ||
|
r4566 | return True | ||
|
r6642 | |||
# Flush softspace | ||||
if softspace(sys.stdout, 0): | ||||
|
r7817 | print() | ||
|
r6642 | |||
|
r4566 | except: | ||
# It's possible to have exceptions raised here, typically by | ||||
# compilation of odd code (such as a naked 'return' outside a | ||||
# function) that did parse but isn't valid. Typically the exception | ||||
# is a SyntaxError, but it's safest just to catch anything and show | ||||
# the user a traceback. | ||||
# We do only one try/except outside the loop to minimize the impact | ||||
# on runtime, and also because if any node in the node list is | ||||
# broken, we should stop execution completely. | ||||
self.showtraceback() | ||||
|
r3737 | |||
return False | ||||
|
r4872 | |||
|
r6644 | def run_code(self, code_obj): | ||
|
r1032 | """Execute a code object. | ||
When an exception occurs, self.showtraceback() is called to display a | ||||
traceback. | ||||
|
r3735 | Parameters | ||
---------- | ||||
code_obj : code object | ||||
A compiled code object, to be executed | ||||
|
r1032 | |||
|
r3735 | Returns | ||
------- | ||||
|
r3737 | False : successful execution. | ||
True : an error occurred. | ||||
|
r1032 | """ | ||
# Set our own excepthook in case the user code tries to call it | ||||
# directly, so that the IPython crash handler doesn't get triggered | ||||
old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | ||||
# we save the original sys.excepthook in the instance, in case config | ||||
# code (such as magics) needs access to it. | ||||
self.sys_excepthook = old_excepthook | ||||
outflag = 1 # happens in more places, so it's easier as default | ||||
try: | ||||
try: | ||||
|
r3122 | self.hooks.pre_run_code_hook() | ||
|
r3297 | #rprint('Running code', repr(code_obj)) # dbg | ||
|
r13350 | exec(code_obj, self.user_global_ns, self.user_ns) | ||
|
r1032 | finally: | ||
# Reset our crash handler in place | ||||
sys.excepthook = old_excepthook | ||||
except SystemExit: | ||||
|
r2440 | self.showtraceback(exception_only=True) | ||
|
r3758 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) | ||
|
r1032 | except self.custom_exceptions: | ||
etype,value,tb = sys.exc_info() | ||||
self.CustomTB(etype,value,tb) | ||||
except: | ||||
self.showtraceback() | ||||
else: | ||||
outflag = 0 | ||||
return outflag | ||||
|
r4872 | |||
|
r3122 | # For backwards compatibility | ||
runcode = run_code | ||||
|
r3139 | |||
|
r2252 | #------------------------------------------------------------------------- | ||
|
r2761 | # Things related to GUI support and pylab | ||
|
r2242 | #------------------------------------------------------------------------- | ||
|
r1032 | |||
|
r5469 | def enable_gui(self, gui=None): | ||
raise NotImplementedError('Implement enable_gui in a subclass') | ||||
|
r11328 | |||
def enable_matplotlib(self, gui=None): | ||||
"""Enable interactive matplotlib and inline figure support. | ||||
This takes the following steps: | ||||
1. select the appropriate eventloop and matplotlib backend | ||||
2. set up matplotlib for interactive use with that backend | ||||
3. configure formatters for inline figure display | ||||
4. enable the selected gui eventloop | ||||
Parameters | ||||
---------- | ||||
gui : optional, string | ||||
If given, dictates the choice of matplotlib GUI backend to use | ||||
(should be one of IPython's supported backends, 'qt', 'osx', 'tk', | ||||
'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | ||||
matplotlib (as dictated by the matplotlib build-time options plus the | ||||
user's matplotlibrc configuration file). Note that not all backends | ||||
make sense in all contexts, for example a terminal ipython can't | ||||
display figures inline. | ||||
""" | ||||
from IPython.core import pylabtools as pt | ||||
gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) | ||||
if gui != 'inline': | ||||
# If we have our first gui selection, store it | ||||
if self.pylab_gui_select is None: | ||||
self.pylab_gui_select = gui | ||||
# Otherwise if they are different | ||||
elif gui != self.pylab_gui_select: | ||||
print ('Warning: Cannot change to a different GUI toolkit: %s.' | ||||
' Using %s instead.' % (gui, self.pylab_gui_select)) | ||||
gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) | ||||
pt.activate_matplotlib(backend) | ||||
pt.configure_inline_support(self, backend) | ||||
# Now we must activate the gui pylab wants to use, and fix %run to take | ||||
# plot updates into account | ||||
self.enable_gui(gui) | ||||
self.magics_manager.registry['ExecutionMagics'].default_runner = \ | ||||
pt.mpl_runner(self.safe_execfile) | ||||
return gui, backend | ||||
|
r5469 | |||
|
r8229 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): | ||
|
r5469 | """Activate pylab support at runtime. | ||
This turns on support for matplotlib, preloads into the interactive | ||||
namespace all of numpy and pylab, and configures IPython to correctly | ||||
interact with the GUI event loop. The GUI backend to be used can be | ||||
|
r9244 | optionally selected with the optional ``gui`` argument. | ||
|
r11328 | |||
This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. | ||||
|
r5469 | |||
Parameters | ||||
---------- | ||||
gui : optional, string | ||||
If given, dictates the choice of matplotlib GUI backend to use | ||||
(should be one of IPython's supported backends, 'qt', 'osx', 'tk', | ||||
'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | ||||
matplotlib (as dictated by the matplotlib build-time options plus the | ||||
user's matplotlibrc configuration file). Note that not all backends | ||||
make sense in all contexts, for example a terminal ipython can't | ||||
display figures inline. | ||||
|
r11328 | import_all : optional, bool, default: True | ||
Whether to do `from numpy import *` and `from pylab import *` | ||||
in addition to module imports. | ||||
welcome_message : deprecated | ||||
This argument is ignored, no welcome message will be displayed. | ||||
|
r5469 | """ | ||
|
r11328 | from IPython.core.pylabtools import import_pylab | ||
gui, backend = self.enable_matplotlib(gui) | ||||
|
r5469 | # We want to prevent the loading of pylab to pollute the user's | ||
# namespace as shown by the %who* magics, so we execute the activation | ||||
# code in an empty namespace, and we update *both* user_ns and | ||||
# user_ns_hidden with this information. | ||||
ns = {} | ||||
|
r11328 | import_pylab(ns, import_all) | ||
|
r11325 | # warn about clobbered names | ||
ignored = set(["__builtins__"]) | ||||
both = set(ns).intersection(self.user_ns).difference(ignored) | ||||
clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] | ||||
|
r5469 | self.user_ns.update(ns) | ||
self.user_ns_hidden.update(ns) | ||||
|
r11328 | return gui, backend, clobbered | ||
|
r2242 | |||
#------------------------------------------------------------------------- | ||||
# Utilities | ||||
#------------------------------------------------------------------------- | ||||
|
r5355 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | ||
|
r2202 | """Expand python variables in a string. | ||
The depth argument indicates how many frames above the caller should | ||||
be walked to look for the local namespace where to expand variables. | ||||
The global namespace for expansion is always the user's interactive | ||||
namespace. | ||||
""" | ||||
|
r5355 | ns = self.user_ns.copy() | ||
ns.update(sys._getframe(depth+1).f_locals) | ||||
|
r6124 | try: | ||
|
r8225 | # We have to use .vformat() here, because 'self' is a valid and common | ||
# name, and expanding **ns for .format() would make it collide with | ||||
# the 'self' argument of the method. | ||||
cmd = formatter.vformat(cmd, args=[], kwargs=ns) | ||||
|
r6124 | except Exception: | ||
# if formatter couldn't format, just let it go untransformed | ||||
pass | ||||
return cmd | ||||
|
r1032 | |||
|
r3175 | def mktempfile(self, data=None, prefix='ipython_edit_'): | ||
|
r1032 | """Make a new tempfile and return its filename. | ||
|
r14683 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), | ||
but it registers the created filename internally so ipython cleans it up | ||||
at exit time. | ||||
|
r1032 | |||
Optional inputs: | ||||
- data(None): if data is given, it gets written out to the temp file | ||||
|
r12553 | immediately, and the file is closed again.""" | ||
|
r1032 | |||
|
r14683 | dirname = tempfile.mkdtemp(prefix=prefix) | ||
self.tempdirs.append(dirname) | ||||
handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) | ||||
|
r1032 | self.tempfiles.append(filename) | ||
|
r4872 | |||
|
r1032 | if data: | ||
tmp_file = open(filename,'w') | ||||
tmp_file.write(data) | ||||
tmp_file.close() | ||||
return filename | ||||
|
r2761 | # TODO: This should be removed when Term is refactored. | ||
|
r1032 | def write(self,data): | ||
"""Write a string to the default output""" | ||||
|
r3800 | io.stdout.write(data) | ||
|
r1032 | |||
|
r2761 | # TODO: This should be removed when Term is refactored. | ||
|
r1032 | def write_err(self,data): | ||
"""Write a string to the default error output""" | ||||
|
r3800 | io.stderr.write(data) | ||
|
r1032 | |||
|
r5370 | def ask_yes_no(self, prompt, default=None): | ||
|
r2242 | if self.quiet: | ||
return True | ||||
return ask_yes_no(prompt,default) | ||||
|
r4872 | |||
|
r2876 | def show_usage(self): | ||
"""Show a usage message""" | ||||
page.page(IPython.core.usage.interactive_usage) | ||||
|
r4872 | |||
|
r6914 | def extract_input_lines(self, range_str, raw=False): | ||
"""Return as a string a set of input history slices. | ||||
Parameters | ||||
---------- | ||||
range_str : string | ||||
The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | ||||
since this function is for use by magic functions which get their | ||||
arguments as strings. The number before the / is the session | ||||
number: ~n goes n back from the current session. | ||||
|
r13587 | raw : bool, optional | ||
By default, the processed input is used. If this is true, the raw | ||||
input history is used instead. | ||||
|
r6914 | |||
|
r13587 | Notes | ||
----- | ||||
|
r6914 | |||
|
r13587 | Slices can be described with two notations: | ||
|
r6914 | |||
|
r13587 | * ``N:M`` -> standard python form, means including items N...(M-1). | ||
* ``N-M`` -> include items N..M (closed endpoint). | ||||
|
r12553 | """ | ||
|
r6914 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | ||
return "\n".join(x for _, _, x in lines) | ||||
|
r8334 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True): | ||
|
r6774 | """Get a code string from history, file, url, or a string or macro. | ||
|
r4872 | |||
This is mainly used by magic functions. | ||||
|
r3509 | Parameters | ||
---------- | ||||
|
r6761 | |||
|
r3509 | target : str | ||
|
r6761 | |||
|
r3509 | A string specifying code to retrieve. This will be tried respectively | ||
|
r6761 | as: ranges of input history (see %history for syntax), url, | ||
|
r6773 | correspnding .py file, filename, or an expression evaluating to a | ||
|
r6761 | string or Macro in the user namespace. | ||
|
r3509 | raw : bool | ||
If true (default), retrieve raw history. Has no effect on the other | ||||
retrieval mechanisms. | ||||
|
r4872 | |||
|
r6775 | py_only : bool (default False) | ||
|
r6774 | Only try to fetch python code, do not try alternative methods to decode file | ||
if unicode fails. | ||||
|
r3509 | Returns | ||
------- | ||||
A string of code. | ||||
|
r4872 | |||
|
r3509 | ValueError is raised if nothing is found, and TypeError if it evaluates | ||
to an object of another type. In each case, .args[0] is a printable | ||||
message. | ||||
""" | ||||
|
r4872 | code = self.extract_input_lines(target, raw=raw) # Grab history | ||
|
r3509 | if code: | ||
return code | ||||
|
r6774 | utarget = unquote_filename(target) | ||
|
r6763 | try: | ||
if utarget.startswith(('http://', 'https://')): | ||||
|
r8334 | return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie) | ||
|
r6773 | except UnicodeDecodeError: | ||
|
r6774 | if not py_only : | ||
|
r13640 | # Deferred import | ||
try: | ||||
from urllib.request import urlopen # Py3 | ||||
except ImportError: | ||||
from urllib import urlopen | ||||
|
r9389 | response = urlopen(target) | ||
|
r6895 | return response.read().decode('latin1') | ||
|
r6774 | raise ValueError(("'%s' seem to be unreadable.") % utarget) | ||
|
r6761 | |||
|
r6778 | potential_target = [target] | ||
|
r6761 | try : | ||
|
r6778 | potential_target.insert(0,get_py_filename(target)) | ||
|
r6761 | except IOError: | ||
pass | ||||
|
r6778 | |||
for tgt in potential_target : | ||||
if os.path.isfile(tgt): # Read file | ||||
try : | ||||
|
r8334 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) | ||
|
r6778 | except UnicodeDecodeError : | ||
if not py_only : | ||||
with io_open(tgt,'r', encoding='latin1') as f : | ||||
return f.read() | ||||
raise ValueError(("'%s' seem to be unreadable.") % target) | ||||
|
r10348 | elif os.path.isdir(os.path.expanduser(tgt)): | ||
raise ValueError("'%s' is a directory, not a regular file." % target) | ||||
|
r4872 | |||
|
r3509 | try: # User namespace | ||
codeobj = eval(target, self.user_ns) | ||||
except Exception: | ||||
|
r6778 | raise ValueError(("'%s' was not found in history, as a file, url, " | ||
|
r6761 | "nor in the user namespace.") % target) | ||
|
r13353 | if isinstance(codeobj, string_types): | ||
|
r3509 | return codeobj | ||
elif isinstance(codeobj, Macro): | ||||
return codeobj.value | ||||
|
r4872 | |||
|
r3509 | raise TypeError("%s is neither a string nor a macro." % target, | ||
codeobj) | ||||
|
r2242 | |||
#------------------------------------------------------------------------- | ||||
# Things related to IPython exiting | ||||
#------------------------------------------------------------------------- | ||||
def atexit_operations(self): | ||||
"""This will be executed at the time of exit. | ||||
|
r1032 | |||
|
r2953 | Cleanup operations and saving of persistent data that is done | ||
unconditionally by IPython should be performed here. | ||||
|
r1032 | |||
|
r2953 | For things that may depend on startup flags or platform specifics (such | ||
as having readline or not), register a separate atexit function in the | ||||
code that has the appropriate information, rather than trying to | ||||
|
r4872 | clutter | ||
|
r2953 | """ | ||
|
r4602 | # Close the history session (this stores the end time and line count) | ||
# this must be *before* the tempfile cleanup, in case of temporary | ||||
# history db | ||||
self.history_manager.end_session() | ||||
|
r4872 | |||
|
r14683 | # Cleanup all tempfiles and folders left around | ||
|
r2242 | for tfile in self.tempfiles: | ||
try: | ||||
os.unlink(tfile) | ||||
except OSError: | ||||
pass | ||||
|
r4872 | |||
|
r14683 | for tdir in self.tempdirs: | ||
try: | ||||
os.rmdir(tdir) | ||||
except OSError: | ||||
pass | ||||
|
r2242 | # Clear all user namespaces to release all references cleanly. | ||
|
r3397 | self.reset(new_session=False) | ||
|
r1032 | |||
|
r2242 | # Run user hooks | ||
self.hooks.shutdown_hook() | ||||
|
r1032 | |||
|
r2242 | def cleanup(self): | ||
self.restore_sys_module_state() | ||||
|
r1032 | |||
|
r13359 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): | ||
|
r2738 | """An abstract base class for InteractiveShell.""" | ||
InteractiveShellABC.register(InteractiveShell) | ||||