|
|
# -*- coding: utf-8 -*-
|
|
|
"""Main IPython class."""
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
|
|
|
# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
|
|
|
# Copyright (C) 2008-2011 The IPython Development Team
|
|
|
#
|
|
|
# Distributed under the terms of the BSD License. The full license is in
|
|
|
# the file COPYING, distributed as part of this software.
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
import __future__
|
|
|
import abc
|
|
|
import ast
|
|
|
import atexit
|
|
|
import functools
|
|
|
import os
|
|
|
import re
|
|
|
import runpy
|
|
|
import sys
|
|
|
import tempfile
|
|
|
import traceback
|
|
|
import types
|
|
|
import subprocess
|
|
|
import warnings
|
|
|
from io import open as io_open
|
|
|
|
|
|
from pickleshare import PickleShareDB
|
|
|
|
|
|
from traitlets.config.configurable import SingletonConfigurable
|
|
|
from IPython.core import oinspect
|
|
|
from IPython.core import magic
|
|
|
from IPython.core import page
|
|
|
from IPython.core import prefilter
|
|
|
from IPython.core import shadowns
|
|
|
from IPython.core import ultratb
|
|
|
from IPython.core.alias import Alias, AliasManager
|
|
|
from IPython.core.autocall import ExitAutocall
|
|
|
from IPython.core.builtin_trap import BuiltinTrap
|
|
|
from IPython.core.events import EventManager, available_events
|
|
|
from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
|
|
|
from IPython.core.debugger import Pdb
|
|
|
from IPython.core.display_trap import DisplayTrap
|
|
|
from IPython.core.displayhook import DisplayHook
|
|
|
from IPython.core.displaypub import DisplayPublisher
|
|
|
from IPython.core.error import InputRejected, UsageError
|
|
|
from IPython.core.extensions import ExtensionManager
|
|
|
from IPython.core.formatters import DisplayFormatter
|
|
|
from IPython.core.history import HistoryManager
|
|
|
from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
|
|
|
from IPython.core.logger import Logger
|
|
|
from IPython.core.macro import Macro
|
|
|
from IPython.core.payload import PayloadManager
|
|
|
from IPython.core.prefilter import PrefilterManager
|
|
|
from IPython.core.profiledir import ProfileDir
|
|
|
from IPython.core.usage import default_banner
|
|
|
from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest
|
|
|
from IPython.utils import PyColorize
|
|
|
from IPython.utils import io
|
|
|
from IPython.utils import py3compat
|
|
|
from IPython.utils import openpy
|
|
|
from IPython.utils.decorators import undoc
|
|
|
from IPython.utils.io import ask_yes_no
|
|
|
from IPython.utils.ipstruct import Struct
|
|
|
from IPython.paths import get_ipython_dir
|
|
|
from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
|
|
|
from IPython.utils.process import system, getoutput
|
|
|
from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
|
|
|
with_metaclass, iteritems)
|
|
|
from IPython.utils.strdispatch import StrDispatch
|
|
|
from IPython.utils.syspathcontext import prepended_to_syspath
|
|
|
from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
|
|
|
from IPython.utils.tempdir import TemporaryDirectory
|
|
|
from traitlets import (
|
|
|
Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
|
|
|
observe, default,
|
|
|
)
|
|
|
from warnings import warn
|
|
|
from logging import error
|
|
|
import IPython.core.hooks
|
|
|
|
|
|
# NoOpContext is deprecated, but ipykernel imports it from here.
|
|
|
# See https://github.com/ipython/ipykernel/issues/157
|
|
|
from IPython.utils.contexts import NoOpContext
|
|
|
|
|
|
try:
|
|
|
import docrepr.sphinxify as sphx
|
|
|
|
|
|
def sphinxify(doc):
|
|
|
with TemporaryDirectory() as dirname:
|
|
|
return {
|
|
|
'text/html': sphx.sphinxify(doc, dirname),
|
|
|
'text/plain': doc
|
|
|
}
|
|
|
except ImportError:
|
|
|
sphinxify = None
|
|
|
|
|
|
|
|
|
class ProvisionalWarning(DeprecationWarning):
|
|
|
"""
|
|
|
Warning class for unstable features
|
|
|
"""
|
|
|
pass
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# Globals
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
# compiled regexps for autoindent management
|
|
|
dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# Utilities
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
@undoc
|
|
|
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
|
|
|
|
|
|
@undoc
|
|
|
def no_op(*a, **kw): pass
|
|
|
|
|
|
|
|
|
class SpaceInInput(Exception): pass
|
|
|
|
|
|
|
|
|
def get_default_colors():
|
|
|
"DEPRECATED"
|
|
|
warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
return 'Neutral'
|
|
|
|
|
|
|
|
|
class SeparateUnicode(Unicode):
|
|
|
r"""A Unicode subclass to validate separate_in, separate_out, etc.
|
|
|
|
|
|
This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
|
|
|
"""
|
|
|
|
|
|
def validate(self, obj, value):
|
|
|
if value == '0': value = ''
|
|
|
value = value.replace('\\n','\n')
|
|
|
return super(SeparateUnicode, self).validate(obj, value)
|
|
|
|
|
|
|
|
|
@undoc
|
|
|
class DummyMod(object):
|
|
|
"""A dummy module used for IPython's interactive module when
|
|
|
a namespace must be assigned to the module's __dict__."""
|
|
|
pass
|
|
|
|
|
|
|
|
|
class ExecutionResult(object):
|
|
|
"""The result of a call to :meth:`InteractiveShell.run_cell`
|
|
|
|
|
|
Stores information about what took place.
|
|
|
"""
|
|
|
execution_count = None
|
|
|
error_before_exec = None
|
|
|
error_in_exec = None
|
|
|
result = None
|
|
|
|
|
|
@property
|
|
|
def success(self):
|
|
|
return (self.error_before_exec is None) and (self.error_in_exec is None)
|
|
|
|
|
|
def raise_error(self):
|
|
|
"""Reraises error if `success` is `False`, otherwise does nothing"""
|
|
|
if self.error_before_exec is not None:
|
|
|
raise self.error_before_exec
|
|
|
if self.error_in_exec is not None:
|
|
|
raise self.error_in_exec
|
|
|
|
|
|
def __repr__(self):
|
|
|
if sys.version_info > (3,):
|
|
|
name = self.__class__.__qualname__
|
|
|
else:
|
|
|
name = self.__class__.__name__
|
|
|
return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
|
|
|
(name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
|
|
|
|
|
|
|
|
|
class InteractiveShell(SingletonConfigurable):
|
|
|
"""An enhanced, interactive shell for Python."""
|
|
|
|
|
|
_instance = None
|
|
|
|
|
|
ast_transformers = List([], help=
|
|
|
"""
|
|
|
A list of ast.NodeTransformer subclass instances, which will be applied
|
|
|
to user input before code is run.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
|
|
|
autocall = Enum((0,1,2), default_value=0, help=
|
|
|
"""
|
|
|
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
|
|
|
arguments on the line, and '2' for 'full' autocall, where all callable
|
|
|
objects are automatically called (even if no arguments are present).
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
# TODO: remove all autoindent logic and put into frontends.
|
|
|
# We can't do this yet because even runlines uses the autoindent.
|
|
|
autoindent = Bool(True, help=
|
|
|
"""
|
|
|
Autoindent IPython code entered interactively.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
|
|
|
automagic = Bool(True, help=
|
|
|
"""
|
|
|
Enable magic commands to be called without the leading %.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
|
|
|
banner1 = Unicode(default_banner,
|
|
|
help="""The part of the banner to be printed before the profile"""
|
|
|
).tag(config=True)
|
|
|
banner2 = Unicode('',
|
|
|
help="""The part of the banner to be printed after the profile"""
|
|
|
).tag(config=True)
|
|
|
|
|
|
cache_size = Integer(1000, help=
|
|
|
"""
|
|
|
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
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
color_info = Bool(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.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
|
|
|
default_value='Neutral',
|
|
|
help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
|
|
|
).tag(config=True)
|
|
|
debug = Bool(False).tag(config=True)
|
|
|
disable_failing_post_execute = Bool(False,
|
|
|
help="Don't call post-execute functions that have failed in the past."
|
|
|
).tag(config=True)
|
|
|
display_formatter = Instance(DisplayFormatter, allow_none=True)
|
|
|
displayhook_class = Type(DisplayHook)
|
|
|
display_pub_class = Type(DisplayPublisher)
|
|
|
|
|
|
sphinxify_docstring = Bool(False, help=
|
|
|
"""
|
|
|
Enables rich html representation of docstrings. (This requires the
|
|
|
docrepr module).
|
|
|
""").tag(config=True)
|
|
|
|
|
|
@observe("sphinxify_docstring")
|
|
|
def _sphinxify_docstring_changed(self, change):
|
|
|
if change['new']:
|
|
|
warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
|
|
|
|
|
|
enable_html_pager = Bool(False, help=
|
|
|
"""
|
|
|
(Provisional API) enables html representation in mime bundles sent
|
|
|
to pagers.
|
|
|
""").tag(config=True)
|
|
|
|
|
|
@observe("enable_html_pager")
|
|
|
def _enable_html_pager_changed(self, change):
|
|
|
if change['new']:
|
|
|
warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
|
|
|
|
|
|
data_pub_class = None
|
|
|
|
|
|
exit_now = Bool(False)
|
|
|
exiter = Instance(ExitAutocall)
|
|
|
@default('exiter')
|
|
|
def _exiter_default(self):
|
|
|
return ExitAutocall(self)
|
|
|
# Monotonically increasing execution counter
|
|
|
execution_count = Integer(1)
|
|
|
filename = Unicode("<ipython console>")
|
|
|
ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
|
|
|
|
|
|
# Input splitter, to transform input line by line and detect when a block
|
|
|
# is ready to be executed.
|
|
|
input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
|
|
|
(), {'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})
|
|
|
|
|
|
logstart = Bool(False, help=
|
|
|
"""
|
|
|
Start logging to the default log file in overwrite mode.
|
|
|
Use `logappend` to specify a log file to **append** logs to.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
logfile = Unicode('', help=
|
|
|
"""
|
|
|
The name of the logfile to use.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
logappend = Unicode('', help=
|
|
|
"""
|
|
|
Start logging to the given file in append mode.
|
|
|
Use `logfile` to specify a log file to **overwrite** logs to.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
object_info_string_level = Enum((0,1,2), default_value=0,
|
|
|
).tag(config=True)
|
|
|
pdb = Bool(False, help=
|
|
|
"""
|
|
|
Automatically call the pdb debugger after every exception.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
display_page = Bool(False,
|
|
|
help="""If True, anything that would be passed to the pager
|
|
|
will be displayed as regular output instead."""
|
|
|
).tag(config=True)
|
|
|
|
|
|
# deprecated prompt traits:
|
|
|
|
|
|
prompt_in1 = Unicode('In [\\#]: ',
|
|
|
help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
|
|
|
).tag(config=True)
|
|
|
prompt_in2 = Unicode(' .\\D.: ',
|
|
|
help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
|
|
|
).tag(config=True)
|
|
|
prompt_out = Unicode('Out[\\#]: ',
|
|
|
help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
|
|
|
).tag(config=True)
|
|
|
prompts_pad_left = Bool(True,
|
|
|
help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
|
|
|
).tag(config=True)
|
|
|
|
|
|
@observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
|
|
|
def _prompt_trait_changed(self, change):
|
|
|
name = change['name']
|
|
|
warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format(
|
|
|
name=name)
|
|
|
)
|
|
|
# protect against weird cases where self.config may not exist:
|
|
|
|
|
|
show_rewritten_input = Bool(True,
|
|
|
help="Show rewritten input, e.g. for autocall."
|
|
|
).tag(config=True)
|
|
|
|
|
|
quiet = Bool(False).tag(config=True)
|
|
|
|
|
|
history_length = Integer(10000,
|
|
|
help='Total length of command history'
|
|
|
).tag(config=True)
|
|
|
|
|
|
history_load_length = Integer(1000, help=
|
|
|
"""
|
|
|
The number of saved history entries to be loaded
|
|
|
into the history buffer at startup.
|
|
|
"""
|
|
|
).tag(config=True)
|
|
|
|
|
|
ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
|
|
|
default_value='last_expr',
|
|
|
help="""
|
|
|
'all', 'last', 'last_expr' or 'none', specifying which nodes should be
|
|
|
run interactively (displaying output from expressions)."""
|
|
|
).tag(config=True)
|
|
|
|
|
|
# TODO: this part of prompt management should be moved to the frontends.
|
|
|
# Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
|
|
|
separate_in = SeparateUnicode('\n').tag(config=True)
|
|
|
separate_out = SeparateUnicode('').tag(config=True)
|
|
|
separate_out2 = SeparateUnicode('').tag(config=True)
|
|
|
wildcards_case_sensitive = Bool(True).tag(config=True)
|
|
|
xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
|
|
|
default_value='Context').tag(config=True)
|
|
|
|
|
|
# Subcomponents of InteractiveShell
|
|
|
alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
|
|
|
prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
|
|
|
builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
|
|
|
display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
|
|
|
extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
|
|
|
payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
|
|
|
history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
|
|
|
magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
|
|
|
|
|
|
profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
|
|
|
@property
|
|
|
def profile(self):
|
|
|
if self.profile_dir is not None:
|
|
|
name = os.path.basename(self.profile_dir.location)
|
|
|
return name.replace('profile_','')
|
|
|
|
|
|
|
|
|
# Private interface
|
|
|
_post_execute = Dict()
|
|
|
|
|
|
# Tracks any GUI loop loaded for pylab
|
|
|
pylab_gui_select = None
|
|
|
|
|
|
last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
|
|
|
|
|
|
def __init__(self, ipython_dir=None, profile_dir=None,
|
|
|
user_module=None, user_ns=None,
|
|
|
custom_exceptions=((), None), **kwargs):
|
|
|
|
|
|
# This is where traits with a config_key argument are updated
|
|
|
# from the values on config.
|
|
|
super(InteractiveShell, self).__init__(**kwargs)
|
|
|
if 'PromptManager' in self.config:
|
|
|
warn('As of IPython 5.0 `PromptManager` config will have no effect'
|
|
|
' and has been replaced by TerminalInteractiveShell.prompts_class')
|
|
|
self.configurables = [self]
|
|
|
|
|
|
# These are relatively independent and stateless
|
|
|
self.init_ipython_dir(ipython_dir)
|
|
|
self.init_profile_dir(profile_dir)
|
|
|
self.init_instance_attrs()
|
|
|
self.init_environment()
|
|
|
|
|
|
# Check if we're in a virtualenv, and set up sys.path.
|
|
|
self.init_virtualenv()
|
|
|
|
|
|
# Create namespaces (user_ns, user_global_ns, etc.)
|
|
|
self.init_create_namespaces(user_module, user_ns)
|
|
|
# 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.
|
|
|
# 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.
|
|
|
self.save_sys_module_state()
|
|
|
self.init_sys_modules()
|
|
|
|
|
|
# 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.
|
|
|
self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
|
|
|
|
|
|
self.init_history()
|
|
|
self.init_encoding()
|
|
|
self.init_prefilter()
|
|
|
|
|
|
self.init_syntax_highlighting()
|
|
|
self.init_hooks()
|
|
|
self.init_events()
|
|
|
self.init_pushd_popd_magic()
|
|
|
self.init_user_ns()
|
|
|
self.init_logger()
|
|
|
self.init_builtins()
|
|
|
|
|
|
# The following was in post_config_initialization
|
|
|
self.init_inspector()
|
|
|
self.raw_input_original = input
|
|
|
self.init_completer()
|
|
|
# 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)
|
|
|
self.init_prompts()
|
|
|
self.init_display_formatter()
|
|
|
self.init_display_pub()
|
|
|
self.init_data_pub()
|
|
|
self.init_displayhook()
|
|
|
self.init_magics()
|
|
|
self.init_alias()
|
|
|
self.init_logstart()
|
|
|
self.init_pdb()
|
|
|
self.init_extension_manager()
|
|
|
self.init_payload()
|
|
|
self.init_deprecation_warnings()
|
|
|
self.hooks.late_startup_hook()
|
|
|
self.events.trigger('shell_initialized', self)
|
|
|
atexit.register(self.atexit_operations)
|
|
|
|
|
|
def get_ipython(self):
|
|
|
"""Return the currently running IPython instance."""
|
|
|
return self
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Trait changed handlers
|
|
|
#-------------------------------------------------------------------------
|
|
|
@observe('ipython_dir')
|
|
|
def _ipython_dir_changed(self, change):
|
|
|
ensure_dir_exists(change['new'])
|
|
|
|
|
|
def set_autoindent(self,value=None):
|
|
|
"""Set the autoindent flag.
|
|
|
|
|
|
If called with no arguments, it acts as a toggle."""
|
|
|
if value is None:
|
|
|
self.autoindent = not self.autoindent
|
|
|
else:
|
|
|
self.autoindent = value
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# init_* methods called by __init__
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def init_ipython_dir(self, ipython_dir):
|
|
|
if ipython_dir is not None:
|
|
|
self.ipython_dir = ipython_dir
|
|
|
return
|
|
|
|
|
|
self.ipython_dir = get_ipython_dir()
|
|
|
|
|
|
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')
|
|
|
|
|
|
def init_instance_attrs(self):
|
|
|
self.more = False
|
|
|
|
|
|
# command compiler
|
|
|
self.compile = CachingCompiler()
|
|
|
|
|
|
# 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()
|
|
|
|
|
|
# Temporary files used for various purposes. Deleted at exit.
|
|
|
self.tempfiles = []
|
|
|
self.tempdirs = []
|
|
|
|
|
|
# keep track of where we started running (mainly for crash post-mortem)
|
|
|
# This is not being used anywhere currently.
|
|
|
self.starting_dir = py3compat.getcwd()
|
|
|
|
|
|
# Indentation management
|
|
|
self.indent_current_nsp = 0
|
|
|
|
|
|
# Dict to track post-execution functions that have been registered
|
|
|
self._post_execute = {}
|
|
|
|
|
|
def init_environment(self):
|
|
|
"""Any changes we need to make to the user's environment."""
|
|
|
pass
|
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
|
@observe('colors')
|
|
|
def init_syntax_highlighting(self, changes=None):
|
|
|
# Python source parser/formatter for syntax highlighting
|
|
|
pyformat = PyColorize.Parser(style=self.colors, parent=self).format
|
|
|
self.pycolorize = lambda src: pyformat(src,'str')
|
|
|
|
|
|
def refresh_style(self):
|
|
|
# No-op here, used in subclass
|
|
|
pass
|
|
|
|
|
|
def init_pushd_popd_magic(self):
|
|
|
# for pushd/popd management
|
|
|
self.home_dir = get_home_dir()
|
|
|
|
|
|
self.dir_stack = []
|
|
|
|
|
|
def init_logger(self):
|
|
|
self.logger = Logger(self.home_dir, logfname='ipython_log.py',
|
|
|
logmode='rotate')
|
|
|
|
|
|
def init_logstart(self):
|
|
|
"""Initialize logging in case it was requested at the command line.
|
|
|
"""
|
|
|
if self.logappend:
|
|
|
self.magic('logstart %s append' % self.logappend)
|
|
|
elif self.logfile:
|
|
|
self.magic('logstart %s' % self.logfile)
|
|
|
elif self.logstart:
|
|
|
self.magic('logstart')
|
|
|
|
|
|
def init_deprecation_warnings(self):
|
|
|
"""
|
|
|
register default filter for deprecation warning.
|
|
|
|
|
|
This will allow deprecation warning of function used interactively to show
|
|
|
warning to users, and still hide deprecation warning from libraries import.
|
|
|
"""
|
|
|
warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
|
|
|
|
|
|
def init_builtins(self):
|
|
|
# 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
|
|
|
|
|
|
self.builtin_trap = BuiltinTrap(shell=self)
|
|
|
|
|
|
def init_inspector(self):
|
|
|
# Object inspector
|
|
|
self.inspector = oinspect.Inspector(oinspect.InspectColors,
|
|
|
PyColorize.ANSICodeColors,
|
|
|
'NoColor',
|
|
|
self.object_info_string_level)
|
|
|
|
|
|
def init_io(self):
|
|
|
# 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
|
|
|
# *before* instantiating this class, because io holds onto
|
|
|
# references to the underlying streams.
|
|
|
# io.std* are deprecated, but don't show our own deprecation warnings
|
|
|
# during initialization of the deprecated API.
|
|
|
with warnings.catch_warnings():
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
io.stdout = io.IOStream(sys.stdout)
|
|
|
io.stderr = io.IOStream(sys.stderr)
|
|
|
|
|
|
def init_prompts(self):
|
|
|
# Set system prompts, so that scripts can decide if they are running
|
|
|
# interactively.
|
|
|
sys.ps1 = 'In : '
|
|
|
sys.ps2 = '...: '
|
|
|
sys.ps3 = 'Out: '
|
|
|
|
|
|
def init_display_formatter(self):
|
|
|
self.display_formatter = DisplayFormatter(parent=self)
|
|
|
self.configurables.append(self.display_formatter)
|
|
|
|
|
|
def init_display_pub(self):
|
|
|
self.display_pub = self.display_pub_class(parent=self)
|
|
|
self.configurables.append(self.display_pub)
|
|
|
|
|
|
def init_data_pub(self):
|
|
|
if not self.data_pub_class:
|
|
|
self.data_pub = None
|
|
|
return
|
|
|
self.data_pub = self.data_pub_class(parent=self)
|
|
|
self.configurables.append(self.data_pub)
|
|
|
|
|
|
def init_displayhook(self):
|
|
|
# Initialize displayhook, set in/out prompts and printing system
|
|
|
self.displayhook = self.displayhook_class(
|
|
|
parent=self,
|
|
|
shell=self,
|
|
|
cache_size=self.cache_size,
|
|
|
)
|
|
|
self.configurables.append(self.displayhook)
|
|
|
# This is a context manager that installs/revmoes the displayhook at
|
|
|
# the appropriate time.
|
|
|
self.display_trap = DisplayTrap(hook=self.displayhook)
|
|
|
|
|
|
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
|
|
|
|
|
|
# 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 = os.path.normcase(sys.executable)
|
|
|
paths = [p]
|
|
|
while os.path.islink(p):
|
|
|
p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
|
|
|
paths.append(p)
|
|
|
p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
|
|
|
if any(p.startswith(p_venv) for p in paths):
|
|
|
# Running properly in the virtualenv, don't need to do anything
|
|
|
return
|
|
|
|
|
|
warn("Attempting to work in a virtualenv. If you encounter problems, please "
|
|
|
"install IPython inside the virtualenv.")
|
|
|
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)
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to injections into the sys module
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def save_sys_module_state(self):
|
|
|
"""Save the state of hooks in the sys module.
|
|
|
|
|
|
This has to be called after self.user_module is created.
|
|
|
"""
|
|
|
self._orig_sys_module_state = {'stdin': sys.stdin,
|
|
|
'stdout': sys.stdout,
|
|
|
'stderr': sys.stderr,
|
|
|
'excepthook': sys.excepthook}
|
|
|
self._orig_sys_modules_main_name = self.user_module.__name__
|
|
|
self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
|
|
|
|
|
|
def restore_sys_module_state(self):
|
|
|
"""Restore the state of the sys module."""
|
|
|
try:
|
|
|
for k, v in iteritems(self._orig_sys_module_state):
|
|
|
setattr(sys, k, v)
|
|
|
except AttributeError:
|
|
|
pass
|
|
|
# Reset what what done in self.init_sys_modules
|
|
|
if self._orig_sys_modules_main_mod is not None:
|
|
|
sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to the banner
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
@property
|
|
|
def banner(self):
|
|
|
banner = self.banner1
|
|
|
if self.profile and self.profile != 'default':
|
|
|
banner += '\nIPython profile: %s\n' % self.profile
|
|
|
if self.banner2:
|
|
|
banner += '\n' + self.banner2
|
|
|
return banner
|
|
|
|
|
|
def show_banner(self, banner=None):
|
|
|
if banner is None:
|
|
|
banner = self.banner
|
|
|
sys.stdout.write(banner)
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to hooks
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def init_hooks(self):
|
|
|
# hooks holds pointers used for user-side customizations
|
|
|
self.hooks = Struct()
|
|
|
|
|
|
self.strdispatchers = {}
|
|
|
|
|
|
# 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
|
|
|
self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
|
|
|
|
|
|
if self.display_page:
|
|
|
self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
|
|
|
|
|
|
def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
|
|
|
_warn_deprecated=True):
|
|
|
"""set_hook(name,hook) -> sets an internal IPython hook.
|
|
|
|
|
|
IPython exposes some of its internal API as user-modifiable hooks. By
|
|
|
adding your function to one of these hooks, you can modify IPython's
|
|
|
behavior to call at runtime your own routines."""
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
f = types.MethodType(hook,self)
|
|
|
|
|
|
# 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
|
|
|
|
|
|
dp = getattr(self.hooks, name, None)
|
|
|
if name not in IPython.core.hooks.__all__:
|
|
|
print("Warning! Hook '%s' is not one of %s" % \
|
|
|
(name, IPython.core.hooks.__all__ ))
|
|
|
|
|
|
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))
|
|
|
|
|
|
if not dp:
|
|
|
dp = IPython.core.hooks.CommandChainDispatcher()
|
|
|
|
|
|
try:
|
|
|
dp.add(f,priority)
|
|
|
except AttributeError:
|
|
|
# it was not commandchain, plain old func - replace
|
|
|
dp = f
|
|
|
|
|
|
setattr(self.hooks,name, dp)
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to events
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def init_events(self):
|
|
|
self.events = EventManager(self, available_events)
|
|
|
|
|
|
self.events.register("pre_execute", self._clear_warning_registry)
|
|
|
|
|
|
def register_post_execute(self, func):
|
|
|
"""DEPRECATED: Use ip.events.register('post_run_cell', func)
|
|
|
|
|
|
Register a function for calling after code execution.
|
|
|
"""
|
|
|
warn("ip.register_post_execute is deprecated, use "
|
|
|
"ip.events.register('post_run_cell', func) instead.")
|
|
|
self.events.register('post_run_cell', func)
|
|
|
|
|
|
def _clear_warning_registry(self):
|
|
|
# clear the warning registry, so that different code blocks with
|
|
|
# overlapping line number ranges don't cause spurious suppression of
|
|
|
# warnings (see gh-6611 for details)
|
|
|
if "__warningregistry__" in self.user_global_ns:
|
|
|
del self.user_global_ns["__warningregistry__"]
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to the "main" module
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def new_main_mod(self, filename, modname):
|
|
|
"""Return a new 'main' module object for user code execution.
|
|
|
|
|
|
``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.
|
|
|
|
|
|
``modname`` should be the module name - normally either '__main__' or
|
|
|
the basename of the file without the extension.
|
|
|
|
|
|
When scripts are executed via %run, we must keep a reference to their
|
|
|
__main__ module around so that Python doesn't
|
|
|
clear it, rendering references to module globals useless.
|
|
|
|
|
|
This method keeps said reference in a private dict, keyed by the
|
|
|
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.
|
|
|
"""
|
|
|
filename = os.path.abspath(filename)
|
|
|
try:
|
|
|
main_mod = self._main_mod_cache[filename]
|
|
|
except KeyError:
|
|
|
main_mod = self._main_mod_cache[filename] = types.ModuleType(
|
|
|
py3compat.cast_bytes_py2(modname),
|
|
|
doc="Module created for script run in IPython")
|
|
|
else:
|
|
|
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
|
|
|
|
|
|
return main_mod
|
|
|
|
|
|
def clear_main_mod_cache(self):
|
|
|
"""Clear the cache of main modules.
|
|
|
|
|
|
Mainly for use by utilities like %reset.
|
|
|
|
|
|
Examples
|
|
|
--------
|
|
|
|
|
|
In [15]: import IPython
|
|
|
|
|
|
In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
|
|
|
|
|
|
In [17]: len(_ip._main_mod_cache) > 0
|
|
|
Out[17]: True
|
|
|
|
|
|
In [18]: _ip.clear_main_mod_cache()
|
|
|
|
|
|
In [19]: len(_ip._main_mod_cache) == 0
|
|
|
Out[19]: True
|
|
|
"""
|
|
|
self._main_mod_cache.clear()
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to debugging
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def init_pdb(self):
|
|
|
# Set calling of pdb on exceptions
|
|
|
# self.call_pdb is a property
|
|
|
self.call_pdb = self.pdb
|
|
|
|
|
|
def _get_call_pdb(self):
|
|
|
return self._call_pdb
|
|
|
|
|
|
def _set_call_pdb(self,val):
|
|
|
|
|
|
if val not in (0,1,False,True):
|
|
|
raise ValueError('new call_pdb value must be boolean')
|
|
|
|
|
|
# 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 pdb debugger.
|
|
|
|
|
|
Keywords:
|
|
|
|
|
|
- force(False): by default, this routine checks the instance call_pdb
|
|
|
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.
|
|
|
"""
|
|
|
|
|
|
if not (force or self.call_pdb):
|
|
|
return
|
|
|
|
|
|
if not hasattr(sys,'last_traceback'):
|
|
|
error('No traceback has been produced, nothing to debug.')
|
|
|
return
|
|
|
|
|
|
self.InteractiveTB.debugger(force=True)
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to IPython's various namespaces
|
|
|
#-------------------------------------------------------------------------
|
|
|
default_user_namespaces = True
|
|
|
|
|
|
def init_create_namespaces(self, user_module=None, user_ns=None):
|
|
|
# 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.
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
# 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
|
|
|
|
|
|
# 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:-(.
|
|
|
|
|
|
# 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.
|
|
|
if (user_ns is not None) or (user_module is not None):
|
|
|
self.default_user_namespaces = False
|
|
|
self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
|
|
|
|
|
|
# A record of hidden variables we have added to the user namespace, so
|
|
|
# we can list later only variables defined in actual interactive use.
|
|
|
self.user_ns_hidden = {}
|
|
|
|
|
|
# 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 doctest 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.
|
|
|
#
|
|
|
# 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)>
|
|
|
#
|
|
|
# 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
|
|
|
self._main_mod_cache = {}
|
|
|
|
|
|
# A table holding all the namespaces IPython deals with, so that
|
|
|
# introspection facilities can search easily.
|
|
|
self.ns_table = {'user_global':self.user_module.__dict__,
|
|
|
'user_local':self.user_ns,
|
|
|
'builtin':builtin_mod.__dict__
|
|
|
}
|
|
|
|
|
|
@property
|
|
|
def user_global_ns(self):
|
|
|
return self.user_module.__dict__
|
|
|
|
|
|
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.
|
|
|
|
|
|
Parameters
|
|
|
----------
|
|
|
user_module : module, optional
|
|
|
The current user module in which IPython is being run. If None,
|
|
|
a clean module will be created.
|
|
|
user_ns : dict, optional
|
|
|
A namespace in which to run interactive commands.
|
|
|
|
|
|
Returns
|
|
|
-------
|
|
|
A tuple of user_module and user_ns, each properly initialised.
|
|
|
"""
|
|
|
if user_module is None and user_ns is not None:
|
|
|
user_ns.setdefault("__name__", "__main__")
|
|
|
user_module = DummyMod()
|
|
|
user_module.__dict__ = user_ns
|
|
|
|
|
|
if user_module is None:
|
|
|
user_module = types.ModuleType("__main__",
|
|
|
doc="Automatically created module for IPython interactive environment")
|
|
|
|
|
|
# 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
|
|
|
user_module.__dict__.setdefault('__builtin__', builtin_mod)
|
|
|
user_module.__dict__.setdefault('__builtins__', builtin_mod)
|
|
|
|
|
|
if user_ns is None:
|
|
|
user_ns = user_module.__dict__
|
|
|
|
|
|
return user_module, user_ns
|
|
|
|
|
|
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).
|
|
|
|
|
|
# This is overridden in the InteractiveShellEmbed subclass to a no-op.
|
|
|
main_name = self.user_module.__name__
|
|
|
sys.modules[main_name] = self.user_module
|
|
|
|
|
|
def init_user_ns(self):
|
|
|
"""Initialize all user-visible namespaces to their minimum defaults.
|
|
|
|
|
|
Certain history lists are also initialized here, as they effectively
|
|
|
act as user namespaces.
|
|
|
|
|
|
Notes
|
|
|
-----
|
|
|
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.
|
|
|
"""
|
|
|
# This function works in two parts: first we put a few things in
|
|
|
# user_ns, and we sync that contents into user_ns_hidden so that these
|
|
|
# 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
|
|
|
# session (probably nothing, so they 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.
|
|
|
|
|
|
# For more details:
|
|
|
# http://mail.python.org/pipermail/python-dev/2001-April/014068.html
|
|
|
ns = dict()
|
|
|
|
|
|
# make global variables for user access to the histories
|
|
|
ns['_ih'] = self.history_manager.input_hist_parsed
|
|
|
ns['_oh'] = self.history_manager.output_hist
|
|
|
ns['_dh'] = self.history_manager.dir_hist
|
|
|
|
|
|
ns['_sh'] = shadowns
|
|
|
|
|
|
# user aliases to input and output histories. These shouldn't show up
|
|
|
# in %who, as they can have very large reprs.
|
|
|
ns['In'] = self.history_manager.input_hist_parsed
|
|
|
ns['Out'] = self.history_manager.output_hist
|
|
|
|
|
|
# Store myself as the public api!!!
|
|
|
ns['get_ipython'] = self.get_ipython
|
|
|
|
|
|
ns['exit'] = self.exiter
|
|
|
ns['quit'] = self.exiter
|
|
|
|
|
|
# Sync what we've added so far to user_ns_hidden so these aren't seen
|
|
|
# by %who
|
|
|
self.user_ns_hidden.update(ns)
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
# Finally, update the real user's namespace
|
|
|
self.user_ns.update(ns)
|
|
|
|
|
|
@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."""
|
|
|
return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
|
|
|
[m.__dict__ for m in self._main_mod_cache.values()]
|
|
|
|
|
|
def reset(self, new_session=True):
|
|
|
"""Clear all internal namespaces, and attempt to release references to
|
|
|
user objects.
|
|
|
|
|
|
If new_session is True, a new history session will be opened.
|
|
|
"""
|
|
|
# Clear histories
|
|
|
self.history_manager.reset(new_session)
|
|
|
# Reset counter used to index all histories
|
|
|
if new_session:
|
|
|
self.execution_count = 1
|
|
|
|
|
|
# Flush cached output items
|
|
|
if self.displayhook.do_full_cache:
|
|
|
self.displayhook.flush()
|
|
|
|
|
|
# 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.
|
|
|
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]
|
|
|
|
|
|
self.user_ns_hidden.clear()
|
|
|
|
|
|
# Restore the user namespaces to minimal usability
|
|
|
self.init_user_ns()
|
|
|
|
|
|
# Restore the default and user aliases
|
|
|
self.alias_manager.clear_aliases()
|
|
|
self.alias_manager.init_aliases()
|
|
|
|
|
|
# Flush the private list of module references kept for script
|
|
|
# execution protection
|
|
|
self.clear_main_mod_cache()
|
|
|
|
|
|
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.
|
|
|
|
|
|
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)
|
|
|
|
|
|
ns_refs = self.all_ns_refs
|
|
|
|
|
|
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:
|
|
|
to_delete = [n for n, o in iteritems(ns) if o is obj]
|
|
|
for name in to_delete:
|
|
|
del ns[name]
|
|
|
|
|
|
# displayhook keeps extra references, but not in a dictionary
|
|
|
for name in ('_', '__', '___'):
|
|
|
if getattr(self.displayhook, name) is obj:
|
|
|
setattr(self.displayhook, name, None)
|
|
|
|
|
|
def reset_selective(self, regex=None):
|
|
|
"""Clear selective variables from internal namespaces based on a
|
|
|
specified regular expression.
|
|
|
|
|
|
Parameters
|
|
|
----------
|
|
|
regex : string or compiled pattern, optional
|
|
|
A regular expression pattern that will be used in searching
|
|
|
variable names in the users namespaces.
|
|
|
"""
|
|
|
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.
|
|
|
for ns in self.all_ns_refs:
|
|
|
for var in ns:
|
|
|
if m.search(var):
|
|
|
del ns[var]
|
|
|
|
|
|
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
|
|
|
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.
|
|
|
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
|
|
|
elif isinstance(variables, string_types+(list, tuple)):
|
|
|
if isinstance(variables, string_types):
|
|
|
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:
|
|
|
print('Could not get variable %s from %s' %
|
|
|
(name,cf.f_code.co_name))
|
|
|
else:
|
|
|
raise ValueError('variables must be a dict/str/list/tuple')
|
|
|
|
|
|
# Propagate variables to user namespace
|
|
|
self.user_ns.update(vdict)
|
|
|
|
|
|
# And configure interactive visibility
|
|
|
user_ns_hidden = self.user_ns_hidden
|
|
|
if interactive:
|
|
|
for name in vdict:
|
|
|
user_ns_hidden.pop(name, None)
|
|
|
else:
|
|
|
user_ns_hidden.update(vdict)
|
|
|
|
|
|
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.
|
|
|
"""
|
|
|
for name, obj in iteritems(variables):
|
|
|
if name in self.user_ns and self.user_ns[name] is obj:
|
|
|
del self.user_ns[name]
|
|
|
self.user_ns_hidden.pop(name, None)
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to object introspection
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
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.
|
|
|
"""
|
|
|
oname = oname.strip()
|
|
|
#print '1- oname: <%r>' % oname # dbg
|
|
|
if not oname.startswith(ESC_MAGIC) and \
|
|
|
not oname.startswith(ESC_MAGIC2) and \
|
|
|
not py3compat.isidentifier(oname, dotted=True):
|
|
|
return dict(found=False)
|
|
|
|
|
|
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.
|
|
|
namespaces = [ ('Interactive', self.user_ns),
|
|
|
('Interactive (global)', self.user_global_ns),
|
|
|
('Python builtin', builtin_mod.__dict__),
|
|
|
]
|
|
|
|
|
|
# initialize results to 'null'
|
|
|
found = False; obj = None; ospace = 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.
|
|
|
if (oname == 'print' and not py3compat.PY3 and not \
|
|
|
(self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
|
|
|
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 idx, part in enumerate(oname_rest):
|
|
|
try:
|
|
|
parent = obj
|
|
|
# The last part is looked up in a special way to avoid
|
|
|
# descriptor invocation as it may raise or have side
|
|
|
# effects.
|
|
|
if idx == len(oname_rest) - 1:
|
|
|
obj = self._getattr_property(obj, part)
|
|
|
else:
|
|
|
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:
|
|
|
obj = None
|
|
|
if oname.startswith(ESC_MAGIC2):
|
|
|
oname = oname.lstrip(ESC_MAGIC2)
|
|
|
obj = self.find_cell_magic(oname)
|
|
|
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)
|
|
|
if obj is not None:
|
|
|
found = True
|
|
|
ospace = 'IPython internal'
|
|
|
ismagic = True
|
|
|
isalias = isinstance(obj, Alias)
|
|
|
|
|
|
# Last try: special-case some literals like '', [], {}, etc:
|
|
|
if not found and oname_head in ["''",'""','[]','{}','()']:
|
|
|
obj = eval(oname_head)
|
|
|
found = True
|
|
|
ospace = 'Interactive'
|
|
|
|
|
|
return {'found':found, 'obj':obj, 'namespace':ospace,
|
|
|
'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
|
|
|
|
|
|
@staticmethod
|
|
|
def _getattr_property(obj, attrname):
|
|
|
"""Property-aware getattr to use in object finding.
|
|
|
|
|
|
If attrname represents a property, return it unevaluated (in case it has
|
|
|
side effects or raises an error.
|
|
|
|
|
|
"""
|
|
|
if not isinstance(obj, type):
|
|
|
try:
|
|
|
# `getattr(type(obj), attrname)` is not guaranteed to return
|
|
|
# `obj`, but does so for property:
|
|
|
#
|
|
|
# property.__get__(self, None, cls) -> self
|
|
|
#
|
|
|
# The universal alternative is to traverse the mro manually
|
|
|
# searching for attrname in class dicts.
|
|
|
attr = getattr(type(obj), attrname)
|
|
|
except AttributeError:
|
|
|
pass
|
|
|
else:
|
|
|
# This relies on the fact that data descriptors (with both
|
|
|
# __get__ & __set__ magic methods) take precedence over
|
|
|
# instance-level attributes:
|
|
|
#
|
|
|
# class A(object):
|
|
|
# @property
|
|
|
# def foobar(self): return 123
|
|
|
# a = A()
|
|
|
# a.__dict__['foobar'] = 345
|
|
|
# a.foobar # == 123
|
|
|
#
|
|
|
# So, a property may be returned right away.
|
|
|
if isinstance(attr, property):
|
|
|
return attr
|
|
|
|
|
|
# Nothing helped, fall back.
|
|
|
return getattr(obj, attrname)
|
|
|
|
|
|
def _object_find(self, oname, namespaces=None):
|
|
|
"""Find an object and return a struct with info about it."""
|
|
|
return Struct(self._ofind(oname, namespaces))
|
|
|
|
|
|
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.
|
|
|
"""
|
|
|
info = self._object_find(oname, namespaces)
|
|
|
docformat = sphinxify if self.sphinxify_docstring else None
|
|
|
if info.found:
|
|
|
pmethod = getattr(self.inspector, meth)
|
|
|
# TODO: only apply format_screen to the plain/text repr of the mime
|
|
|
# bundle.
|
|
|
formatter = format_screen if info.ismagic else docformat
|
|
|
if meth == 'pdoc':
|
|
|
pmethod(info.obj, oname, formatter)
|
|
|
elif meth == 'pinfo':
|
|
|
pmethod(info.obj, oname, formatter, info,
|
|
|
enable_html_pager=self.enable_html_pager, **kw)
|
|
|
else:
|
|
|
pmethod(info.obj, oname)
|
|
|
else:
|
|
|
print('Object `%s` not found.' % oname)
|
|
|
return 'not found' # so callers can take other action
|
|
|
|
|
|
def object_inspect(self, oname, detail_level=0):
|
|
|
"""Get object info about oname"""
|
|
|
with self.builtin_trap:
|
|
|
info = self._object_find(oname)
|
|
|
if info.found:
|
|
|
return self.inspector.info(info.obj, oname, info=info,
|
|
|
detail_level=detail_level
|
|
|
)
|
|
|
else:
|
|
|
return oinspect.object_info(name=oname, found=False)
|
|
|
|
|
|
def object_inspect_text(self, oname, detail_level=0):
|
|
|
"""Get object info as formatted text"""
|
|
|
return self.object_inspect_mime(oname, detail_level)['text/plain']
|
|
|
|
|
|
def object_inspect_mime(self, oname, detail_level=0):
|
|
|
"""Get object info as a mimebundle of formatted representations.
|
|
|
|
|
|
A mimebundle is a dictionary, keyed by mime-type.
|
|
|
It must always have the key `'text/plain'`.
|
|
|
"""
|
|
|
with self.builtin_trap:
|
|
|
info = self._object_find(oname)
|
|
|
if info.found:
|
|
|
return self.inspector._get_info(info.obj, oname, info=info,
|
|
|
detail_level=detail_level
|
|
|
)
|
|
|
else:
|
|
|
raise KeyError(oname)
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to history management
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def init_history(self):
|
|
|
"""Sets up the command history, and starts regular autosaves."""
|
|
|
self.history_manager = HistoryManager(shell=self, parent=self)
|
|
|
self.configurables.append(self.history_manager)
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to exception handling and tracebacks (not debugging)
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
debugger_cls = Pdb
|
|
|
|
|
|
def init_traceback_handlers(self, custom_exceptions):
|
|
|
# Syntax error handler.
|
|
|
self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self)
|
|
|
|
|
|
# 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',
|
|
|
tb_offset = 1,
|
|
|
check_cache=check_linecache_ipython,
|
|
|
debugger_cls=self.debugger_cls, parent=self)
|
|
|
|
|
|
# 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
|
|
|
|
|
|
# and add any custom exception handlers the user may have specified
|
|
|
self.set_custom_exc(*custom_exceptions)
|
|
|
|
|
|
# Set the exception mode
|
|
|
self.InteractiveTB.set_mode(mode=self.xmode)
|
|
|
|
|
|
def set_custom_exc(self, exc_tuple, handler):
|
|
|
"""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
|
|
|
run_code() method).
|
|
|
|
|
|
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::
|
|
|
|
|
|
exc_tuple == (MyCustomException,)
|
|
|
|
|
|
handler : callable
|
|
|
handler must have the following signature::
|
|
|
|
|
|
def my_handler(self, etype, value, tb, tb_offset=None):
|
|
|
...
|
|
|
return structured_traceback
|
|
|
|
|
|
Your handler must return a structured traceback (a list of strings),
|
|
|
or None.
|
|
|
|
|
|
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.
|
|
|
|
|
|
To protect IPython from crashes, if your handler ever raises an
|
|
|
exception or returns an invalid result, it will be immediately
|
|
|
disabled.
|
|
|
|
|
|
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."
|
|
|
|
|
|
def dummy_handler(self, etype, value, tb, tb_offset=None):
|
|
|
print('*** Simple custom exception handler ***')
|
|
|
print('Exception type :',etype)
|
|
|
print('Exception value:',value)
|
|
|
print('Traceback :',tb)
|
|
|
#print 'Source code :','\n'.join(self.buffer)
|
|
|
|
|
|
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 []
|
|
|
elif isinstance(stb, string_types):
|
|
|
return [stb]
|
|
|
elif not isinstance(stb, list):
|
|
|
raise TypeError(msg)
|
|
|
# it's a list
|
|
|
for line in stb:
|
|
|
# check every element
|
|
|
if not isinstance(line, string_types):
|
|
|
raise TypeError(msg)
|
|
|
return stb
|
|
|
|
|
|
if handler is None:
|
|
|
wrapped = dummy_handler
|
|
|
else:
|
|
|
def wrapped(self,etype,value,tb,tb_offset=None):
|
|
|
"""wrap CustomTB handler, to protect IPython from user code
|
|
|
|
|
|
This makes it harder (but not impossible) for custom exception
|
|
|
handlers to crash IPython.
|
|
|
"""
|
|
|
try:
|
|
|
stb = handler(self,etype,value,tb,tb_offset=tb_offset)
|
|
|
return validate_stb(stb)
|
|
|
except:
|
|
|
# clear custom handler immediately
|
|
|
self.set_custom_exc((), None)
|
|
|
print("Custom TB Handler failed, unregistering", file=sys.stderr)
|
|
|
# show the exception in handler first
|
|
|
stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
|
|
|
print(self.InteractiveTB.stb2text(stb))
|
|
|
print("The original exception:")
|
|
|
stb = self.InteractiveTB.structured_traceback(
|
|
|
(etype,value,tb), tb_offset=tb_offset
|
|
|
)
|
|
|
return stb
|
|
|
|
|
|
self.CustomTB = types.MethodType(wrapped,self)
|
|
|
self.custom_exceptions = exc_tuple
|
|
|
|
|
|
def excepthook(self, etype, value, tb):
|
|
|
"""One more defense for GUI apps that call sys.excepthook.
|
|
|
|
|
|
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.
|
|
|
|
|
|
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.
|
|
|
|
|
|
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)
|
|
|
|
|
|
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
|
|
|
|
|
|
def show_usage_error(self, exc):
|
|
|
"""Show a short message for UsageErrors
|
|
|
|
|
|
These are special exceptions that shouldn't show a traceback.
|
|
|
"""
|
|
|
print("UsageError: %s" % exc, file=sys.stderr)
|
|
|
|
|
|
def get_exception_only(self, exc_tuple=None):
|
|
|
"""
|
|
|
Return as a string (ending with a newline) the exception that
|
|
|
just occurred, without any traceback.
|
|
|
"""
|
|
|
etype, value, tb = self._get_exc_info(exc_tuple)
|
|
|
msg = traceback.format_exception_only(etype, value)
|
|
|
return ''.join(msg)
|
|
|
|
|
|
def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
|
|
|
exception_only=False):
|
|
|
"""Display the exception that just occurred.
|
|
|
|
|
|
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.
|
|
|
|
|
|
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."""
|
|
|
|
|
|
try:
|
|
|
try:
|
|
|
etype, value, tb = self._get_exc_info(exc_tuple)
|
|
|
except ValueError:
|
|
|
print('No traceback available to show.', file=sys.stderr)
|
|
|
return
|
|
|
|
|
|
if issubclass(etype, SyntaxError):
|
|
|
# Though this won't be called by syntax errors in the input
|
|
|
# line, there may be SyntaxError cases with imported code.
|
|
|
self.showsyntaxerror(filename)
|
|
|
elif etype is UsageError:
|
|
|
self.show_usage_error(value)
|
|
|
else:
|
|
|
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))
|
|
|
else:
|
|
|
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,
|
|
|
value, tb, tb_offset=tb_offset)
|
|
|
|
|
|
self._showtraceback(etype, value, stb)
|
|
|
if self.call_pdb:
|
|
|
# drop into debugger
|
|
|
self.debugger(force=True)
|
|
|
return
|
|
|
|
|
|
# Actually show the traceback
|
|
|
self._showtraceback(etype, value, stb)
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
print('\n' + self.get_exception_only(), file=sys.stderr)
|
|
|
|
|
|
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.
|
|
|
"""
|
|
|
print(self.InteractiveTB.stb2text(stb))
|
|
|
|
|
|
def showsyntaxerror(self, filename=None):
|
|
|
"""Display the syntax error that just occurred.
|
|
|
|
|
|
This doesn't display a stack trace because there isn't one.
|
|
|
|
|
|
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).
|
|
|
"""
|
|
|
etype, value, last_traceback = self._get_exc_info()
|
|
|
|
|
|
if filename and issubclass(etype, SyntaxError):
|
|
|
try:
|
|
|
value.filename = filename
|
|
|
except:
|
|
|
# Not the format we expect; leave it alone
|
|
|
pass
|
|
|
|
|
|
stb = self.SyntaxTB.structured_traceback(etype, value, [])
|
|
|
self._showtraceback(etype, value, stb)
|
|
|
|
|
|
# This is overridden in TerminalInteractiveShell to show a message about
|
|
|
# the %paste magic.
|
|
|
def showindentationerror(self):
|
|
|
"""Called by run_cell when there's an IndentationError in code entered
|
|
|
at the prompt.
|
|
|
|
|
|
This is overridden in TerminalInteractiveShell to show a message about
|
|
|
the %paste magic."""
|
|
|
self.showsyntaxerror()
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
# Things related to readline
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
def init_readline(self):
|
|
|
"""DEPRECATED
|
|
|
|
|
|
Moved to terminal subclass, here only to simplify the init logic."""
|
|
|
# Set a number of methods that depend on readline to be no-op
|
|
|
warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
self.set_custom_completer = no_op
|
|
|
|
|
|
@skip_doctest
|
|
|
def set_next_input(self, s, replace=False):
|
|
|
""" Sets the 'default' input string for the next command line.
|
|
|
|
|
|
Example::
|
|
|
|
|
|
In [1]: _ip.set_next_input("Hello Word")
|
|
|
In [2]: Hello Word_ # cursor is here
|
|
|
"""
|
|
|
self.rl_next_input = py3compat.cast_bytes_py2(s)
|
|
|
|
|
|
def _indent_current_str(self):
|
|
|
"""return the current level of indentation as a string"""
|
|
|
return self.input_splitter.indent_spaces * ' '
|
|
|