##// END OF EJS Templates
Merge pull request #9884 from denis-it/issue-8485...
Thomas Kluyver -
r22828:3ff50d6a merge
parent child Browse files
Show More
@@ -0,0 +1,3 b''
1 The `--deep-reload` flag and the corresponding options to inject `dreload` or
2 `reload` into the interactive namespace have been removed. You have to
3 explicitly import `reload` from `IPython.lib.deepreload` to use it.
@@ -1,114 +1,103 b''
1 1 """
2 2 A context manager for managing things injected into :mod:`__builtin__`.
3 3
4 4 Authors:
5 5
6 6 * Brian Granger
7 7 * Fernando Perez
8 8 """
9 9 #-----------------------------------------------------------------------------
10 10 # Copyright (C) 2010-2011 The IPython Development Team.
11 11 #
12 12 # Distributed under the terms of the BSD License.
13 13 #
14 14 # Complete license in the file COPYING.txt, distributed with this software.
15 15 #-----------------------------------------------------------------------------
16 16
17 17 #-----------------------------------------------------------------------------
18 18 # Imports
19 19 #-----------------------------------------------------------------------------
20 20
21 21 from traitlets.config.configurable import Configurable
22 22
23 23 from IPython.utils.py3compat import builtin_mod, iteritems
24 24 from traitlets import Instance
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Classes and functions
28 28 #-----------------------------------------------------------------------------
29 29
30 30 class __BuiltinUndefined(object): pass
31 31 BuiltinUndefined = __BuiltinUndefined()
32 32
33 33 class __HideBuiltin(object): pass
34 34 HideBuiltin = __HideBuiltin()
35 35
36 36
37 37 class BuiltinTrap(Configurable):
38 38
39 39 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
40 40 allow_none=True)
41 41
42 42 def __init__(self, shell=None):
43 43 super(BuiltinTrap, self).__init__(shell=shell, config=None)
44 44 self._orig_builtins = {}
45 45 # We define this to track if a single BuiltinTrap is nested.
46 46 # Only turn off the trap when the outermost call to __exit__ is made.
47 47 self._nested_level = 0
48 48 self.shell = shell
49 49 # builtins we always add - if set to HideBuiltin, they will just
50 50 # be removed instead of being replaced by something else
51 51 self.auto_builtins = {'exit': HideBuiltin,
52 52 'quit': HideBuiltin,
53 53 'get_ipython': self.shell.get_ipython,
54 54 }
55 # Recursive reload function
56 try:
57 from IPython.lib import deepreload
58 if self.shell.deep_reload:
59 from warnings import warn
60 warn("Automatically replacing builtin `reload` by `deepreload.reload` is deprecated since IPython 4.0, please import `reload` explicitly from `IPython.lib.deepreload", DeprecationWarning)
61 self.auto_builtins['reload'] = deepreload._dreload
62 else:
63 self.auto_builtins['dreload']= deepreload._dreload
64 except ImportError:
65 pass
66 55
67 56 def __enter__(self):
68 57 if self._nested_level == 0:
69 58 self.activate()
70 59 self._nested_level += 1
71 60 # I return self, so callers can use add_builtin in a with clause.
72 61 return self
73 62
74 63 def __exit__(self, type, value, traceback):
75 64 if self._nested_level == 1:
76 65 self.deactivate()
77 66 self._nested_level -= 1
78 67 # Returning False will cause exceptions to propagate
79 68 return False
80 69
81 70 def add_builtin(self, key, value):
82 71 """Add a builtin and save the original."""
83 72 bdict = builtin_mod.__dict__
84 73 orig = bdict.get(key, BuiltinUndefined)
85 74 if value is HideBuiltin:
86 75 if orig is not BuiltinUndefined: #same as 'key in bdict'
87 76 self._orig_builtins[key] = orig
88 77 del bdict[key]
89 78 else:
90 79 self._orig_builtins[key] = orig
91 80 bdict[key] = value
92 81
93 82 def remove_builtin(self, key, orig):
94 83 """Remove an added builtin and re-set the original."""
95 84 if orig is BuiltinUndefined:
96 85 del builtin_mod.__dict__[key]
97 86 else:
98 87 builtin_mod.__dict__[key] = orig
99 88
100 89 def activate(self):
101 90 """Store ipython references in the __builtin__ namespace."""
102 91
103 92 add_builtin = self.add_builtin
104 93 for name, func in iteritems(self.auto_builtins):
105 94 add_builtin(name, func)
106 95
107 96 def deactivate(self):
108 97 """Remove any builtins which might have been added by add_builtins, or
109 98 restore overwritten ones to their previous values."""
110 99 remove_builtin = self.remove_builtin
111 100 for key, val in iteritems(self._orig_builtins):
112 101 remove_builtin(key, val)
113 102 self._orig_builtins.clear()
114 103 self._builtins_added = False
@@ -1,3245 +1,3230 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 from __future__ import absolute_import, print_function
14 14
15 15 import __future__
16 16 import abc
17 17 import ast
18 18 import atexit
19 19 import functools
20 20 import os
21 21 import re
22 22 import runpy
23 23 import sys
24 24 import tempfile
25 25 import traceback
26 26 import types
27 27 import subprocess
28 28 import warnings
29 29 from io import open as io_open
30 30
31 31 from pickleshare import PickleShareDB
32 32
33 33 from traitlets.config.configurable import SingletonConfigurable
34 34 from IPython.core import oinspect
35 35 from IPython.core import magic
36 36 from IPython.core import page
37 37 from IPython.core import prefilter
38 38 from IPython.core import shadowns
39 39 from IPython.core import ultratb
40 40 from IPython.core.alias import Alias, AliasManager
41 41 from IPython.core.autocall import ExitAutocall
42 42 from IPython.core.builtin_trap import BuiltinTrap
43 43 from IPython.core.events import EventManager, available_events
44 44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
45 45 from IPython.core.debugger import Pdb
46 46 from IPython.core.display_trap import DisplayTrap
47 47 from IPython.core.displayhook import DisplayHook
48 48 from IPython.core.displaypub import DisplayPublisher
49 49 from IPython.core.error import InputRejected, UsageError
50 50 from IPython.core.extensions import ExtensionManager
51 51 from IPython.core.formatters import DisplayFormatter
52 52 from IPython.core.history import HistoryManager
53 53 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
54 54 from IPython.core.logger import Logger
55 55 from IPython.core.macro import Macro
56 56 from IPython.core.payload import PayloadManager
57 57 from IPython.core.prefilter import PrefilterManager
58 58 from IPython.core.profiledir import ProfileDir
59 59 from IPython.core.usage import default_banner
60 60 from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest
61 61 from IPython.utils import PyColorize
62 62 from IPython.utils import io
63 63 from IPython.utils import py3compat
64 64 from IPython.utils import openpy
65 65 from IPython.utils.decorators import undoc
66 66 from IPython.utils.io import ask_yes_no
67 67 from IPython.utils.ipstruct import Struct
68 68 from IPython.paths import get_ipython_dir
69 69 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
70 70 from IPython.utils.process import system, getoutput
71 71 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
72 72 with_metaclass, iteritems)
73 73 from IPython.utils.strdispatch import StrDispatch
74 74 from IPython.utils.syspathcontext import prepended_to_syspath
75 75 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
76 76 from IPython.utils.tempdir import TemporaryDirectory
77 77 from traitlets import (
78 78 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
79 79 observe, default,
80 80 )
81 81 from warnings import warn
82 82 from logging import error
83 83 import IPython.core.hooks
84 84
85 85 # NoOpContext is deprecated, but ipykernel imports it from here.
86 86 # See https://github.com/ipython/ipykernel/issues/157
87 87 from IPython.utils.contexts import NoOpContext
88 88
89 89 try:
90 90 import docrepr.sphinxify as sphx
91 91
92 92 def sphinxify(doc):
93 93 with TemporaryDirectory() as dirname:
94 94 return {
95 95 'text/html': sphx.sphinxify(doc, dirname),
96 96 'text/plain': doc
97 97 }
98 98 except ImportError:
99 99 sphinxify = None
100 100
101 101
102 102 class ProvisionalWarning(DeprecationWarning):
103 103 """
104 104 Warning class for unstable features
105 105 """
106 106 pass
107 107
108 108 #-----------------------------------------------------------------------------
109 109 # Globals
110 110 #-----------------------------------------------------------------------------
111 111
112 112 # compiled regexps for autoindent management
113 113 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
114 114
115 115 #-----------------------------------------------------------------------------
116 116 # Utilities
117 117 #-----------------------------------------------------------------------------
118 118
119 119 @undoc
120 120 def softspace(file, newvalue):
121 121 """Copied from code.py, to remove the dependency"""
122 122
123 123 oldvalue = 0
124 124 try:
125 125 oldvalue = file.softspace
126 126 except AttributeError:
127 127 pass
128 128 try:
129 129 file.softspace = newvalue
130 130 except (AttributeError, TypeError):
131 131 # "attribute-less object" or "read-only attributes"
132 132 pass
133 133 return oldvalue
134 134
135 135 @undoc
136 136 def no_op(*a, **kw): pass
137 137
138 138
139 139 class SpaceInInput(Exception): pass
140 140
141 141
142 142 def get_default_colors():
143 143 "DEPRECATED"
144 144 warn('get_default_color is Deprecated, and is `Neutral` on all platforms.',
145 145 DeprecationWarning, stacklevel=2)
146 146 return 'Neutral'
147 147
148 148
149 149 class SeparateUnicode(Unicode):
150 150 r"""A Unicode subclass to validate separate_in, separate_out, etc.
151 151
152 152 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
153 153 """
154 154
155 155 def validate(self, obj, value):
156 156 if value == '0': value = ''
157 157 value = value.replace('\\n','\n')
158 158 return super(SeparateUnicode, self).validate(obj, value)
159 159
160 160
161 161 @undoc
162 162 class DummyMod(object):
163 163 """A dummy module used for IPython's interactive module when
164 164 a namespace must be assigned to the module's __dict__."""
165 165 pass
166 166
167 167
168 168 class ExecutionResult(object):
169 169 """The result of a call to :meth:`InteractiveShell.run_cell`
170 170
171 171 Stores information about what took place.
172 172 """
173 173 execution_count = None
174 174 error_before_exec = None
175 175 error_in_exec = None
176 176 result = None
177 177
178 178 @property
179 179 def success(self):
180 180 return (self.error_before_exec is None) and (self.error_in_exec is None)
181 181
182 182 def raise_error(self):
183 183 """Reraises error if `success` is `False`, otherwise does nothing"""
184 184 if self.error_before_exec is not None:
185 185 raise self.error_before_exec
186 186 if self.error_in_exec is not None:
187 187 raise self.error_in_exec
188 188
189 189 def __repr__(self):
190 190 if sys.version_info > (3,):
191 191 name = self.__class__.__qualname__
192 192 else:
193 193 name = self.__class__.__name__
194 194 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\
195 195 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result))
196 196
197 197
198 198 class InteractiveShell(SingletonConfigurable):
199 199 """An enhanced, interactive shell for Python."""
200 200
201 201 _instance = None
202 202
203 203 ast_transformers = List([], help=
204 204 """
205 205 A list of ast.NodeTransformer subclass instances, which will be applied
206 206 to user input before code is run.
207 207 """
208 208 ).tag(config=True)
209 209
210 210 autocall = Enum((0,1,2), default_value=0, help=
211 211 """
212 212 Make IPython automatically call any callable object even if you didn't
213 213 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
214 214 automatically. The value can be '0' to disable the feature, '1' for
215 215 'smart' autocall, where it is not applied if there are no more
216 216 arguments on the line, and '2' for 'full' autocall, where all callable
217 217 objects are automatically called (even if no arguments are present).
218 218 """
219 219 ).tag(config=True)
220 220 # TODO: remove all autoindent logic and put into frontends.
221 221 # We can't do this yet because even runlines uses the autoindent.
222 222 autoindent = Bool(True, help=
223 223 """
224 224 Autoindent IPython code entered interactively.
225 225 """
226 226 ).tag(config=True)
227 227
228 228 automagic = Bool(True, help=
229 229 """
230 230 Enable magic commands to be called without the leading %.
231 231 """
232 232 ).tag(config=True)
233 233
234 234 banner1 = Unicode(default_banner,
235 235 help="""The part of the banner to be printed before the profile"""
236 236 ).tag(config=True)
237 237 banner2 = Unicode('',
238 238 help="""The part of the banner to be printed after the profile"""
239 239 ).tag(config=True)
240 240
241 241 cache_size = Integer(1000, help=
242 242 """
243 243 Set the size of the output cache. The default is 1000, you can
244 244 change it permanently in your config file. Setting it to 0 completely
245 245 disables the caching system, and the minimum value accepted is 20 (if
246 246 you provide a value less than 20, it is reset to 0 and a warning is
247 247 issued). This limit is defined because otherwise you'll spend more
248 248 time re-flushing a too small cache than working
249 249 """
250 250 ).tag(config=True)
251 251 color_info = Bool(True, help=
252 252 """
253 253 Use colors for displaying information about objects. Because this
254 254 information is passed through a pager (like 'less'), and some pagers
255 255 get confused with color codes, this capability can be turned off.
256 256 """
257 257 ).tag(config=True)
258 258 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
259 259 default_value='Neutral',
260 260 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
261 261 ).tag(config=True)
262 262 debug = Bool(False).tag(config=True)
263 deep_reload = Bool(False, help=
264 """
265 **Deprecated**
266
267 Will be removed in IPython 6.0
268
269 Enable deep (recursive) reloading by default. IPython can use the
270 deep_reload module which reloads changes in modules recursively (it
271 replaces the reload() function, so you don't need to change anything to
272 use it). `deep_reload` forces a full reload of modules whose code may
273 have changed, which the default reload() function does not. When
274 deep_reload is off, IPython will use the normal reload(), but
275 deep_reload will still be available as dreload().
276 """
277 ).tag(config=True)
278 263 disable_failing_post_execute = Bool(False,
279 264 help="Don't call post-execute functions that have failed in the past."
280 265 ).tag(config=True)
281 266 display_formatter = Instance(DisplayFormatter, allow_none=True)
282 267 displayhook_class = Type(DisplayHook)
283 268 display_pub_class = Type(DisplayPublisher)
284 269
285 270 sphinxify_docstring = Bool(False, help=
286 271 """
287 272 Enables rich html representation of docstrings. (This requires the
288 273 docrepr module).
289 274 """).tag(config=True)
290 275
291 276 @observe("sphinxify_docstring")
292 277 def _sphinxify_docstring_changed(self, change):
293 278 if change['new']:
294 279 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
295 280
296 281 enable_html_pager = Bool(False, help=
297 282 """
298 283 (Provisional API) enables html representation in mime bundles sent
299 284 to pagers.
300 285 """).tag(config=True)
301 286
302 287 @observe("enable_html_pager")
303 288 def _enable_html_pager_changed(self, change):
304 289 if change['new']:
305 290 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
306 291
307 292 data_pub_class = None
308 293
309 294 exit_now = Bool(False)
310 295 exiter = Instance(ExitAutocall)
311 296 @default('exiter')
312 297 def _exiter_default(self):
313 298 return ExitAutocall(self)
314 299 # Monotonically increasing execution counter
315 300 execution_count = Integer(1)
316 301 filename = Unicode("<ipython console>")
317 302 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
318 303
319 304 # Input splitter, to transform input line by line and detect when a block
320 305 # is ready to be executed.
321 306 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
322 307 (), {'line_input_checker': True})
323 308
324 309 # This InputSplitter instance is used to transform completed cells before
325 310 # running them. It allows cell magics to contain blank lines.
326 311 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
327 312 (), {'line_input_checker': False})
328 313
329 314 logstart = Bool(False, help=
330 315 """
331 316 Start logging to the default log file in overwrite mode.
332 317 Use `logappend` to specify a log file to **append** logs to.
333 318 """
334 319 ).tag(config=True)
335 320 logfile = Unicode('', help=
336 321 """
337 322 The name of the logfile to use.
338 323 """
339 324 ).tag(config=True)
340 325 logappend = Unicode('', help=
341 326 """
342 327 Start logging to the given file in append mode.
343 328 Use `logfile` to specify a log file to **overwrite** logs to.
344 329 """
345 330 ).tag(config=True)
346 331 object_info_string_level = Enum((0,1,2), default_value=0,
347 332 ).tag(config=True)
348 333 pdb = Bool(False, help=
349 334 """
350 335 Automatically call the pdb debugger after every exception.
351 336 """
352 337 ).tag(config=True)
353 338 display_page = Bool(False,
354 339 help="""If True, anything that would be passed to the pager
355 340 will be displayed as regular output instead."""
356 341 ).tag(config=True)
357 342
358 343 # deprecated prompt traits:
359 344
360 345 prompt_in1 = Unicode('In [\\#]: ',
361 346 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
362 347 ).tag(config=True)
363 348 prompt_in2 = Unicode(' .\\D.: ',
364 349 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
365 350 ).tag(config=True)
366 351 prompt_out = Unicode('Out[\\#]: ',
367 352 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
368 353 ).tag(config=True)
369 354 prompts_pad_left = Bool(True,
370 355 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
371 356 ).tag(config=True)
372 357
373 358 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
374 359 def _prompt_trait_changed(self, change):
375 360 name = change['name']
376 361 warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format(
377 362 name=name)
378 363 )
379 364 # protect against weird cases where self.config may not exist:
380 365
381 366 show_rewritten_input = Bool(True,
382 367 help="Show rewritten input, e.g. for autocall."
383 368 ).tag(config=True)
384 369
385 370 quiet = Bool(False).tag(config=True)
386 371
387 372 history_length = Integer(10000,
388 373 help='Total length of command history'
389 374 ).tag(config=True)
390 375
391 376 history_load_length = Integer(1000, help=
392 377 """
393 378 The number of saved history entries to be loaded
394 379 into the history buffer at startup.
395 380 """
396 381 ).tag(config=True)
397 382
398 383 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
399 384 default_value='last_expr',
400 385 help="""
401 386 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
402 387 run interactively (displaying output from expressions)."""
403 388 ).tag(config=True)
404 389
405 390 # TODO: this part of prompt management should be moved to the frontends.
406 391 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
407 392 separate_in = SeparateUnicode('\n').tag(config=True)
408 393 separate_out = SeparateUnicode('').tag(config=True)
409 394 separate_out2 = SeparateUnicode('').tag(config=True)
410 395 wildcards_case_sensitive = Bool(True).tag(config=True)
411 396 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
412 397 default_value='Context').tag(config=True)
413 398
414 399 # Subcomponents of InteractiveShell
415 400 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
416 401 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
417 402 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
418 403 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
419 404 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
420 405 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
421 406 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
422 407 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
423 408
424 409 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
425 410 @property
426 411 def profile(self):
427 412 if self.profile_dir is not None:
428 413 name = os.path.basename(self.profile_dir.location)
429 414 return name.replace('profile_','')
430 415
431 416
432 417 # Private interface
433 418 _post_execute = Dict()
434 419
435 420 # Tracks any GUI loop loaded for pylab
436 421 pylab_gui_select = None
437 422
438 423 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
439 424
440 425 def __init__(self, ipython_dir=None, profile_dir=None,
441 426 user_module=None, user_ns=None,
442 427 custom_exceptions=((), None), **kwargs):
443 428
444 429 # This is where traits with a config_key argument are updated
445 430 # from the values on config.
446 431 super(InteractiveShell, self).__init__(**kwargs)
447 432 if 'PromptManager' in self.config:
448 433 warn('As of IPython 5.0 `PromptManager` config will have no effect'
449 434 ' and has been replaced by TerminalInteractiveShell.prompts_class')
450 435 self.configurables = [self]
451 436
452 437 # These are relatively independent and stateless
453 438 self.init_ipython_dir(ipython_dir)
454 439 self.init_profile_dir(profile_dir)
455 440 self.init_instance_attrs()
456 441 self.init_environment()
457 442
458 443 # Check if we're in a virtualenv, and set up sys.path.
459 444 self.init_virtualenv()
460 445
461 446 # Create namespaces (user_ns, user_global_ns, etc.)
462 447 self.init_create_namespaces(user_module, user_ns)
463 448 # This has to be done after init_create_namespaces because it uses
464 449 # something in self.user_ns, but before init_sys_modules, which
465 450 # is the first thing to modify sys.
466 451 # TODO: When we override sys.stdout and sys.stderr before this class
467 452 # is created, we are saving the overridden ones here. Not sure if this
468 453 # is what we want to do.
469 454 self.save_sys_module_state()
470 455 self.init_sys_modules()
471 456
472 457 # While we're trying to have each part of the code directly access what
473 458 # it needs without keeping redundant references to objects, we have too
474 459 # much legacy code that expects ip.db to exist.
475 460 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
476 461
477 462 self.init_history()
478 463 self.init_encoding()
479 464 self.init_prefilter()
480 465
481 466 self.init_syntax_highlighting()
482 467 self.init_hooks()
483 468 self.init_events()
484 469 self.init_pushd_popd_magic()
485 470 self.init_user_ns()
486 471 self.init_logger()
487 472 self.init_builtins()
488 473
489 474 # The following was in post_config_initialization
490 475 self.init_inspector()
491 476 if py3compat.PY3:
492 477 self.raw_input_original = input
493 478 else:
494 479 self.raw_input_original = raw_input
495 480 self.init_completer()
496 481 # TODO: init_io() needs to happen before init_traceback handlers
497 482 # because the traceback handlers hardcode the stdout/stderr streams.
498 483 # This logic in in debugger.Pdb and should eventually be changed.
499 484 self.init_io()
500 485 self.init_traceback_handlers(custom_exceptions)
501 486 self.init_prompts()
502 487 self.init_display_formatter()
503 488 self.init_display_pub()
504 489 self.init_data_pub()
505 490 self.init_displayhook()
506 491 self.init_magics()
507 492 self.init_alias()
508 493 self.init_logstart()
509 494 self.init_pdb()
510 495 self.init_extension_manager()
511 496 self.init_payload()
512 497 self.init_deprecation_warnings()
513 498 self.hooks.late_startup_hook()
514 499 self.events.trigger('shell_initialized', self)
515 500 atexit.register(self.atexit_operations)
516 501
517 502 def get_ipython(self):
518 503 """Return the currently running IPython instance."""
519 504 return self
520 505
521 506 #-------------------------------------------------------------------------
522 507 # Trait changed handlers
523 508 #-------------------------------------------------------------------------
524 509 @observe('ipython_dir')
525 510 def _ipython_dir_changed(self, change):
526 511 ensure_dir_exists(change['new'])
527 512
528 513 def set_autoindent(self,value=None):
529 514 """Set the autoindent flag.
530 515
531 516 If called with no arguments, it acts as a toggle."""
532 517 if value is None:
533 518 self.autoindent = not self.autoindent
534 519 else:
535 520 self.autoindent = value
536 521
537 522 #-------------------------------------------------------------------------
538 523 # init_* methods called by __init__
539 524 #-------------------------------------------------------------------------
540 525
541 526 def init_ipython_dir(self, ipython_dir):
542 527 if ipython_dir is not None:
543 528 self.ipython_dir = ipython_dir
544 529 return
545 530
546 531 self.ipython_dir = get_ipython_dir()
547 532
548 533 def init_profile_dir(self, profile_dir):
549 534 if profile_dir is not None:
550 535 self.profile_dir = profile_dir
551 536 return
552 537 self.profile_dir =\
553 538 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
554 539
555 540 def init_instance_attrs(self):
556 541 self.more = False
557 542
558 543 # command compiler
559 544 self.compile = CachingCompiler()
560 545
561 546 # Make an empty namespace, which extension writers can rely on both
562 547 # existing and NEVER being used by ipython itself. This gives them a
563 548 # convenient location for storing additional information and state
564 549 # their extensions may require, without fear of collisions with other
565 550 # ipython names that may develop later.
566 551 self.meta = Struct()
567 552
568 553 # Temporary files used for various purposes. Deleted at exit.
569 554 self.tempfiles = []
570 555 self.tempdirs = []
571 556
572 557 # keep track of where we started running (mainly for crash post-mortem)
573 558 # This is not being used anywhere currently.
574 559 self.starting_dir = py3compat.getcwd()
575 560
576 561 # Indentation management
577 562 self.indent_current_nsp = 0
578 563
579 564 # Dict to track post-execution functions that have been registered
580 565 self._post_execute = {}
581 566
582 567 def init_environment(self):
583 568 """Any changes we need to make to the user's environment."""
584 569 pass
585 570
586 571 def init_encoding(self):
587 572 # Get system encoding at startup time. Certain terminals (like Emacs
588 573 # under Win32 have it set to None, and we need to have a known valid
589 574 # encoding to use in the raw_input() method
590 575 try:
591 576 self.stdin_encoding = sys.stdin.encoding or 'ascii'
592 577 except AttributeError:
593 578 self.stdin_encoding = 'ascii'
594 579
595 580 def init_syntax_highlighting(self):
596 581 # Python source parser/formatter for syntax highlighting
597 582 pyformat = PyColorize.Parser().format
598 583 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
599 584
600 585 def refresh_style(self):
601 586 # No-op here, used in subclass
602 587 pass
603 588
604 589 def init_pushd_popd_magic(self):
605 590 # for pushd/popd management
606 591 self.home_dir = get_home_dir()
607 592
608 593 self.dir_stack = []
609 594
610 595 def init_logger(self):
611 596 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
612 597 logmode='rotate')
613 598
614 599 def init_logstart(self):
615 600 """Initialize logging in case it was requested at the command line.
616 601 """
617 602 if self.logappend:
618 603 self.magic('logstart %s append' % self.logappend)
619 604 elif self.logfile:
620 605 self.magic('logstart %s' % self.logfile)
621 606 elif self.logstart:
622 607 self.magic('logstart')
623 608
624 609 def init_deprecation_warnings(self):
625 610 """
626 611 register default filter for deprecation warning.
627 612
628 613 This will allow deprecation warning of function used interactively to show
629 614 warning to users, and still hide deprecation warning from libraries import.
630 615 """
631 616 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
632 617
633 618 def init_builtins(self):
634 619 # A single, static flag that we set to True. Its presence indicates
635 620 # that an IPython shell has been created, and we make no attempts at
636 621 # removing on exit or representing the existence of more than one
637 622 # IPython at a time.
638 623 builtin_mod.__dict__['__IPYTHON__'] = True
639 624
640 625 self.builtin_trap = BuiltinTrap(shell=self)
641 626
642 627 def init_inspector(self):
643 628 # Object inspector
644 629 self.inspector = oinspect.Inspector(oinspect.InspectColors,
645 630 PyColorize.ANSICodeColors,
646 631 'NoColor',
647 632 self.object_info_string_level)
648 633
649 634 def init_io(self):
650 635 # This will just use sys.stdout and sys.stderr. If you want to
651 636 # override sys.stdout and sys.stderr themselves, you need to do that
652 637 # *before* instantiating this class, because io holds onto
653 638 # references to the underlying streams.
654 639 # io.std* are deprecated, but don't show our own deprecation warnings
655 640 # during initialization of the deprecated API.
656 641 with warnings.catch_warnings():
657 642 warnings.simplefilter('ignore', DeprecationWarning)
658 643 io.stdout = io.IOStream(sys.stdout)
659 644 io.stderr = io.IOStream(sys.stderr)
660 645
661 646 def init_prompts(self):
662 647 # Set system prompts, so that scripts can decide if they are running
663 648 # interactively.
664 649 sys.ps1 = 'In : '
665 650 sys.ps2 = '...: '
666 651 sys.ps3 = 'Out: '
667 652
668 653 def init_display_formatter(self):
669 654 self.display_formatter = DisplayFormatter(parent=self)
670 655 self.configurables.append(self.display_formatter)
671 656
672 657 def init_display_pub(self):
673 658 self.display_pub = self.display_pub_class(parent=self)
674 659 self.configurables.append(self.display_pub)
675 660
676 661 def init_data_pub(self):
677 662 if not self.data_pub_class:
678 663 self.data_pub = None
679 664 return
680 665 self.data_pub = self.data_pub_class(parent=self)
681 666 self.configurables.append(self.data_pub)
682 667
683 668 def init_displayhook(self):
684 669 # Initialize displayhook, set in/out prompts and printing system
685 670 self.displayhook = self.displayhook_class(
686 671 parent=self,
687 672 shell=self,
688 673 cache_size=self.cache_size,
689 674 )
690 675 self.configurables.append(self.displayhook)
691 676 # This is a context manager that installs/revmoes the displayhook at
692 677 # the appropriate time.
693 678 self.display_trap = DisplayTrap(hook=self.displayhook)
694 679
695 680 def init_virtualenv(self):
696 681 """Add a virtualenv to sys.path so the user can import modules from it.
697 682 This isn't perfect: it doesn't use the Python interpreter with which the
698 683 virtualenv was built, and it ignores the --no-site-packages option. A
699 684 warning will appear suggesting the user installs IPython in the
700 685 virtualenv, but for many cases, it probably works well enough.
701 686
702 687 Adapted from code snippets online.
703 688
704 689 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
705 690 """
706 691 if 'VIRTUAL_ENV' not in os.environ:
707 692 # Not in a virtualenv
708 693 return
709 694
710 695 # venv detection:
711 696 # stdlib venv may symlink sys.executable, so we can't use realpath.
712 697 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
713 698 # So we just check every item in the symlink tree (generally <= 3)
714 699 p = os.path.normcase(sys.executable)
715 700 paths = [p]
716 701 while os.path.islink(p):
717 702 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
718 703 paths.append(p)
719 704 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
720 705 if any(p.startswith(p_venv) for p in paths):
721 706 # Running properly in the virtualenv, don't need to do anything
722 707 return
723 708
724 709 warn("Attempting to work in a virtualenv. If you encounter problems, please "
725 710 "install IPython inside the virtualenv.")
726 711 if sys.platform == "win32":
727 712 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
728 713 else:
729 714 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
730 715 'python%d.%d' % sys.version_info[:2], 'site-packages')
731 716
732 717 import site
733 718 sys.path.insert(0, virtual_env)
734 719 site.addsitedir(virtual_env)
735 720
736 721 #-------------------------------------------------------------------------
737 722 # Things related to injections into the sys module
738 723 #-------------------------------------------------------------------------
739 724
740 725 def save_sys_module_state(self):
741 726 """Save the state of hooks in the sys module.
742 727
743 728 This has to be called after self.user_module is created.
744 729 """
745 730 self._orig_sys_module_state = {'stdin': sys.stdin,
746 731 'stdout': sys.stdout,
747 732 'stderr': sys.stderr,
748 733 'excepthook': sys.excepthook}
749 734 self._orig_sys_modules_main_name = self.user_module.__name__
750 735 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
751 736
752 737 def restore_sys_module_state(self):
753 738 """Restore the state of the sys module."""
754 739 try:
755 740 for k, v in iteritems(self._orig_sys_module_state):
756 741 setattr(sys, k, v)
757 742 except AttributeError:
758 743 pass
759 744 # Reset what what done in self.init_sys_modules
760 745 if self._orig_sys_modules_main_mod is not None:
761 746 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
762 747
763 748 #-------------------------------------------------------------------------
764 749 # Things related to the banner
765 750 #-------------------------------------------------------------------------
766 751
767 752 @property
768 753 def banner(self):
769 754 banner = self.banner1
770 755 if self.profile and self.profile != 'default':
771 756 banner += '\nIPython profile: %s\n' % self.profile
772 757 if self.banner2:
773 758 banner += '\n' + self.banner2
774 759 return banner
775 760
776 761 def show_banner(self, banner=None):
777 762 if banner is None:
778 763 banner = self.banner
779 764 sys.stdout.write(banner)
780 765
781 766 #-------------------------------------------------------------------------
782 767 # Things related to hooks
783 768 #-------------------------------------------------------------------------
784 769
785 770 def init_hooks(self):
786 771 # hooks holds pointers used for user-side customizations
787 772 self.hooks = Struct()
788 773
789 774 self.strdispatchers = {}
790 775
791 776 # Set all default hooks, defined in the IPython.hooks module.
792 777 hooks = IPython.core.hooks
793 778 for hook_name in hooks.__all__:
794 779 # default hooks have priority 100, i.e. low; user hooks should have
795 780 # 0-100 priority
796 781 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
797 782
798 783 if self.display_page:
799 784 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
800 785
801 786 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
802 787 _warn_deprecated=True):
803 788 """set_hook(name,hook) -> sets an internal IPython hook.
804 789
805 790 IPython exposes some of its internal API as user-modifiable hooks. By
806 791 adding your function to one of these hooks, you can modify IPython's
807 792 behavior to call at runtime your own routines."""
808 793
809 794 # At some point in the future, this should validate the hook before it
810 795 # accepts it. Probably at least check that the hook takes the number
811 796 # of args it's supposed to.
812 797
813 798 f = types.MethodType(hook,self)
814 799
815 800 # check if the hook is for strdispatcher first
816 801 if str_key is not None:
817 802 sdp = self.strdispatchers.get(name, StrDispatch())
818 803 sdp.add_s(str_key, f, priority )
819 804 self.strdispatchers[name] = sdp
820 805 return
821 806 if re_key is not None:
822 807 sdp = self.strdispatchers.get(name, StrDispatch())
823 808 sdp.add_re(re.compile(re_key), f, priority )
824 809 self.strdispatchers[name] = sdp
825 810 return
826 811
827 812 dp = getattr(self.hooks, name, None)
828 813 if name not in IPython.core.hooks.__all__:
829 814 print("Warning! Hook '%s' is not one of %s" % \
830 815 (name, IPython.core.hooks.__all__ ))
831 816
832 817 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
833 818 alternative = IPython.core.hooks.deprecated[name]
834 819 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
835 820
836 821 if not dp:
837 822 dp = IPython.core.hooks.CommandChainDispatcher()
838 823
839 824 try:
840 825 dp.add(f,priority)
841 826 except AttributeError:
842 827 # it was not commandchain, plain old func - replace
843 828 dp = f
844 829
845 830 setattr(self.hooks,name, dp)
846 831
847 832 #-------------------------------------------------------------------------
848 833 # Things related to events
849 834 #-------------------------------------------------------------------------
850 835
851 836 def init_events(self):
852 837 self.events = EventManager(self, available_events)
853 838
854 839 self.events.register("pre_execute", self._clear_warning_registry)
855 840
856 841 def register_post_execute(self, func):
857 842 """DEPRECATED: Use ip.events.register('post_run_cell', func)
858 843
859 844 Register a function for calling after code execution.
860 845 """
861 846 warn("ip.register_post_execute is deprecated, use "
862 847 "ip.events.register('post_run_cell', func) instead.")
863 848 self.events.register('post_run_cell', func)
864 849
865 850 def _clear_warning_registry(self):
866 851 # clear the warning registry, so that different code blocks with
867 852 # overlapping line number ranges don't cause spurious suppression of
868 853 # warnings (see gh-6611 for details)
869 854 if "__warningregistry__" in self.user_global_ns:
870 855 del self.user_global_ns["__warningregistry__"]
871 856
872 857 #-------------------------------------------------------------------------
873 858 # Things related to the "main" module
874 859 #-------------------------------------------------------------------------
875 860
876 861 def new_main_mod(self, filename, modname):
877 862 """Return a new 'main' module object for user code execution.
878 863
879 864 ``filename`` should be the path of the script which will be run in the
880 865 module. Requests with the same filename will get the same module, with
881 866 its namespace cleared.
882 867
883 868 ``modname`` should be the module name - normally either '__main__' or
884 869 the basename of the file without the extension.
885 870
886 871 When scripts are executed via %run, we must keep a reference to their
887 872 __main__ module around so that Python doesn't
888 873 clear it, rendering references to module globals useless.
889 874
890 875 This method keeps said reference in a private dict, keyed by the
891 876 absolute path of the script. This way, for multiple executions of the
892 877 same script we only keep one copy of the namespace (the last one),
893 878 thus preventing memory leaks from old references while allowing the
894 879 objects from the last execution to be accessible.
895 880 """
896 881 filename = os.path.abspath(filename)
897 882 try:
898 883 main_mod = self._main_mod_cache[filename]
899 884 except KeyError:
900 885 main_mod = self._main_mod_cache[filename] = types.ModuleType(
901 886 py3compat.cast_bytes_py2(modname),
902 887 doc="Module created for script run in IPython")
903 888 else:
904 889 main_mod.__dict__.clear()
905 890 main_mod.__name__ = modname
906 891
907 892 main_mod.__file__ = filename
908 893 # It seems pydoc (and perhaps others) needs any module instance to
909 894 # implement a __nonzero__ method
910 895 main_mod.__nonzero__ = lambda : True
911 896
912 897 return main_mod
913 898
914 899 def clear_main_mod_cache(self):
915 900 """Clear the cache of main modules.
916 901
917 902 Mainly for use by utilities like %reset.
918 903
919 904 Examples
920 905 --------
921 906
922 907 In [15]: import IPython
923 908
924 909 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
925 910
926 911 In [17]: len(_ip._main_mod_cache) > 0
927 912 Out[17]: True
928 913
929 914 In [18]: _ip.clear_main_mod_cache()
930 915
931 916 In [19]: len(_ip._main_mod_cache) == 0
932 917 Out[19]: True
933 918 """
934 919 self._main_mod_cache.clear()
935 920
936 921 #-------------------------------------------------------------------------
937 922 # Things related to debugging
938 923 #-------------------------------------------------------------------------
939 924
940 925 def init_pdb(self):
941 926 # Set calling of pdb on exceptions
942 927 # self.call_pdb is a property
943 928 self.call_pdb = self.pdb
944 929
945 930 def _get_call_pdb(self):
946 931 return self._call_pdb
947 932
948 933 def _set_call_pdb(self,val):
949 934
950 935 if val not in (0,1,False,True):
951 936 raise ValueError('new call_pdb value must be boolean')
952 937
953 938 # store value in instance
954 939 self._call_pdb = val
955 940
956 941 # notify the actual exception handlers
957 942 self.InteractiveTB.call_pdb = val
958 943
959 944 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
960 945 'Control auto-activation of pdb at exceptions')
961 946
962 947 def debugger(self,force=False):
963 948 """Call the pdb debugger.
964 949
965 950 Keywords:
966 951
967 952 - force(False): by default, this routine checks the instance call_pdb
968 953 flag and does not actually invoke the debugger if the flag is false.
969 954 The 'force' option forces the debugger to activate even if the flag
970 955 is false.
971 956 """
972 957
973 958 if not (force or self.call_pdb):
974 959 return
975 960
976 961 if not hasattr(sys,'last_traceback'):
977 962 error('No traceback has been produced, nothing to debug.')
978 963 return
979 964
980 965 self.InteractiveTB.debugger(force=True)
981 966
982 967 #-------------------------------------------------------------------------
983 968 # Things related to IPython's various namespaces
984 969 #-------------------------------------------------------------------------
985 970 default_user_namespaces = True
986 971
987 972 def init_create_namespaces(self, user_module=None, user_ns=None):
988 973 # Create the namespace where the user will operate. user_ns is
989 974 # normally the only one used, and it is passed to the exec calls as
990 975 # the locals argument. But we do carry a user_global_ns namespace
991 976 # given as the exec 'globals' argument, This is useful in embedding
992 977 # situations where the ipython shell opens in a context where the
993 978 # distinction between locals and globals is meaningful. For
994 979 # non-embedded contexts, it is just the same object as the user_ns dict.
995 980
996 981 # FIXME. For some strange reason, __builtins__ is showing up at user
997 982 # level as a dict instead of a module. This is a manual fix, but I
998 983 # should really track down where the problem is coming from. Alex
999 984 # Schmolck reported this problem first.
1000 985
1001 986 # A useful post by Alex Martelli on this topic:
1002 987 # Re: inconsistent value from __builtins__
1003 988 # Von: Alex Martelli <aleaxit@yahoo.com>
1004 989 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1005 990 # Gruppen: comp.lang.python
1006 991
1007 992 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1008 993 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1009 994 # > <type 'dict'>
1010 995 # > >>> print type(__builtins__)
1011 996 # > <type 'module'>
1012 997 # > Is this difference in return value intentional?
1013 998
1014 999 # Well, it's documented that '__builtins__' can be either a dictionary
1015 1000 # or a module, and it's been that way for a long time. Whether it's
1016 1001 # intentional (or sensible), I don't know. In any case, the idea is
1017 1002 # that if you need to access the built-in namespace directly, you
1018 1003 # should start with "import __builtin__" (note, no 's') which will
1019 1004 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1020 1005
1021 1006 # These routines return a properly built module and dict as needed by
1022 1007 # the rest of the code, and can also be used by extension writers to
1023 1008 # generate properly initialized namespaces.
1024 1009 if (user_ns is not None) or (user_module is not None):
1025 1010 self.default_user_namespaces = False
1026 1011 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1027 1012
1028 1013 # A record of hidden variables we have added to the user namespace, so
1029 1014 # we can list later only variables defined in actual interactive use.
1030 1015 self.user_ns_hidden = {}
1031 1016
1032 1017 # Now that FakeModule produces a real module, we've run into a nasty
1033 1018 # problem: after script execution (via %run), the module where the user
1034 1019 # code ran is deleted. Now that this object is a true module (needed
1035 1020 # so doctest and other tools work correctly), the Python module
1036 1021 # teardown mechanism runs over it, and sets to None every variable
1037 1022 # present in that module. Top-level references to objects from the
1038 1023 # script survive, because the user_ns is updated with them. However,
1039 1024 # calling functions defined in the script that use other things from
1040 1025 # the script will fail, because the function's closure had references
1041 1026 # to the original objects, which are now all None. So we must protect
1042 1027 # these modules from deletion by keeping a cache.
1043 1028 #
1044 1029 # To avoid keeping stale modules around (we only need the one from the
1045 1030 # last run), we use a dict keyed with the full path to the script, so
1046 1031 # only the last version of the module is held in the cache. Note,
1047 1032 # however, that we must cache the module *namespace contents* (their
1048 1033 # __dict__). Because if we try to cache the actual modules, old ones
1049 1034 # (uncached) could be destroyed while still holding references (such as
1050 1035 # those held by GUI objects that tend to be long-lived)>
1051 1036 #
1052 1037 # The %reset command will flush this cache. See the cache_main_mod()
1053 1038 # and clear_main_mod_cache() methods for details on use.
1054 1039
1055 1040 # This is the cache used for 'main' namespaces
1056 1041 self._main_mod_cache = {}
1057 1042
1058 1043 # A table holding all the namespaces IPython deals with, so that
1059 1044 # introspection facilities can search easily.
1060 1045 self.ns_table = {'user_global':self.user_module.__dict__,
1061 1046 'user_local':self.user_ns,
1062 1047 'builtin':builtin_mod.__dict__
1063 1048 }
1064 1049
1065 1050 @property
1066 1051 def user_global_ns(self):
1067 1052 return self.user_module.__dict__
1068 1053
1069 1054 def prepare_user_module(self, user_module=None, user_ns=None):
1070 1055 """Prepare the module and namespace in which user code will be run.
1071 1056
1072 1057 When IPython is started normally, both parameters are None: a new module
1073 1058 is created automatically, and its __dict__ used as the namespace.
1074 1059
1075 1060 If only user_module is provided, its __dict__ is used as the namespace.
1076 1061 If only user_ns is provided, a dummy module is created, and user_ns
1077 1062 becomes the global namespace. If both are provided (as they may be
1078 1063 when embedding), user_ns is the local namespace, and user_module
1079 1064 provides the global namespace.
1080 1065
1081 1066 Parameters
1082 1067 ----------
1083 1068 user_module : module, optional
1084 1069 The current user module in which IPython is being run. If None,
1085 1070 a clean module will be created.
1086 1071 user_ns : dict, optional
1087 1072 A namespace in which to run interactive commands.
1088 1073
1089 1074 Returns
1090 1075 -------
1091 1076 A tuple of user_module and user_ns, each properly initialised.
1092 1077 """
1093 1078 if user_module is None and user_ns is not None:
1094 1079 user_ns.setdefault("__name__", "__main__")
1095 1080 user_module = DummyMod()
1096 1081 user_module.__dict__ = user_ns
1097 1082
1098 1083 if user_module is None:
1099 1084 user_module = types.ModuleType("__main__",
1100 1085 doc="Automatically created module for IPython interactive environment")
1101 1086
1102 1087 # We must ensure that __builtin__ (without the final 's') is always
1103 1088 # available and pointing to the __builtin__ *module*. For more details:
1104 1089 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1105 1090 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1106 1091 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1107 1092
1108 1093 if user_ns is None:
1109 1094 user_ns = user_module.__dict__
1110 1095
1111 1096 return user_module, user_ns
1112 1097
1113 1098 def init_sys_modules(self):
1114 1099 # We need to insert into sys.modules something that looks like a
1115 1100 # module but which accesses the IPython namespace, for shelve and
1116 1101 # pickle to work interactively. Normally they rely on getting
1117 1102 # everything out of __main__, but for embedding purposes each IPython
1118 1103 # instance has its own private namespace, so we can't go shoving
1119 1104 # everything into __main__.
1120 1105
1121 1106 # note, however, that we should only do this for non-embedded
1122 1107 # ipythons, which really mimic the __main__.__dict__ with their own
1123 1108 # namespace. Embedded instances, on the other hand, should not do
1124 1109 # this because they need to manage the user local/global namespaces
1125 1110 # only, but they live within a 'normal' __main__ (meaning, they
1126 1111 # shouldn't overtake the execution environment of the script they're
1127 1112 # embedded in).
1128 1113
1129 1114 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1130 1115 main_name = self.user_module.__name__
1131 1116 sys.modules[main_name] = self.user_module
1132 1117
1133 1118 def init_user_ns(self):
1134 1119 """Initialize all user-visible namespaces to their minimum defaults.
1135 1120
1136 1121 Certain history lists are also initialized here, as they effectively
1137 1122 act as user namespaces.
1138 1123
1139 1124 Notes
1140 1125 -----
1141 1126 All data structures here are only filled in, they are NOT reset by this
1142 1127 method. If they were not empty before, data will simply be added to
1143 1128 therm.
1144 1129 """
1145 1130 # This function works in two parts: first we put a few things in
1146 1131 # user_ns, and we sync that contents into user_ns_hidden so that these
1147 1132 # initial variables aren't shown by %who. After the sync, we add the
1148 1133 # rest of what we *do* want the user to see with %who even on a new
1149 1134 # session (probably nothing, so they really only see their own stuff)
1150 1135
1151 1136 # The user dict must *always* have a __builtin__ reference to the
1152 1137 # Python standard __builtin__ namespace, which must be imported.
1153 1138 # This is so that certain operations in prompt evaluation can be
1154 1139 # reliably executed with builtins. Note that we can NOT use
1155 1140 # __builtins__ (note the 's'), because that can either be a dict or a
1156 1141 # module, and can even mutate at runtime, depending on the context
1157 1142 # (Python makes no guarantees on it). In contrast, __builtin__ is
1158 1143 # always a module object, though it must be explicitly imported.
1159 1144
1160 1145 # For more details:
1161 1146 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1162 1147 ns = dict()
1163 1148
1164 1149 # make global variables for user access to the histories
1165 1150 ns['_ih'] = self.history_manager.input_hist_parsed
1166 1151 ns['_oh'] = self.history_manager.output_hist
1167 1152 ns['_dh'] = self.history_manager.dir_hist
1168 1153
1169 1154 ns['_sh'] = shadowns
1170 1155
1171 1156 # user aliases to input and output histories. These shouldn't show up
1172 1157 # in %who, as they can have very large reprs.
1173 1158 ns['In'] = self.history_manager.input_hist_parsed
1174 1159 ns['Out'] = self.history_manager.output_hist
1175 1160
1176 1161 # Store myself as the public api!!!
1177 1162 ns['get_ipython'] = self.get_ipython
1178 1163
1179 1164 ns['exit'] = self.exiter
1180 1165 ns['quit'] = self.exiter
1181 1166
1182 1167 # Sync what we've added so far to user_ns_hidden so these aren't seen
1183 1168 # by %who
1184 1169 self.user_ns_hidden.update(ns)
1185 1170
1186 1171 # Anything put into ns now would show up in %who. Think twice before
1187 1172 # putting anything here, as we really want %who to show the user their
1188 1173 # stuff, not our variables.
1189 1174
1190 1175 # Finally, update the real user's namespace
1191 1176 self.user_ns.update(ns)
1192 1177
1193 1178 @property
1194 1179 def all_ns_refs(self):
1195 1180 """Get a list of references to all the namespace dictionaries in which
1196 1181 IPython might store a user-created object.
1197 1182
1198 1183 Note that this does not include the displayhook, which also caches
1199 1184 objects from the output."""
1200 1185 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1201 1186 [m.__dict__ for m in self._main_mod_cache.values()]
1202 1187
1203 1188 def reset(self, new_session=True):
1204 1189 """Clear all internal namespaces, and attempt to release references to
1205 1190 user objects.
1206 1191
1207 1192 If new_session is True, a new history session will be opened.
1208 1193 """
1209 1194 # Clear histories
1210 1195 self.history_manager.reset(new_session)
1211 1196 # Reset counter used to index all histories
1212 1197 if new_session:
1213 1198 self.execution_count = 1
1214 1199
1215 1200 # Flush cached output items
1216 1201 if self.displayhook.do_full_cache:
1217 1202 self.displayhook.flush()
1218 1203
1219 1204 # The main execution namespaces must be cleared very carefully,
1220 1205 # skipping the deletion of the builtin-related keys, because doing so
1221 1206 # would cause errors in many object's __del__ methods.
1222 1207 if self.user_ns is not self.user_global_ns:
1223 1208 self.user_ns.clear()
1224 1209 ns = self.user_global_ns
1225 1210 drop_keys = set(ns.keys())
1226 1211 drop_keys.discard('__builtin__')
1227 1212 drop_keys.discard('__builtins__')
1228 1213 drop_keys.discard('__name__')
1229 1214 for k in drop_keys:
1230 1215 del ns[k]
1231 1216
1232 1217 self.user_ns_hidden.clear()
1233 1218
1234 1219 # Restore the user namespaces to minimal usability
1235 1220 self.init_user_ns()
1236 1221
1237 1222 # Restore the default and user aliases
1238 1223 self.alias_manager.clear_aliases()
1239 1224 self.alias_manager.init_aliases()
1240 1225
1241 1226 # Flush the private list of module references kept for script
1242 1227 # execution protection
1243 1228 self.clear_main_mod_cache()
1244 1229
1245 1230 def del_var(self, varname, by_name=False):
1246 1231 """Delete a variable from the various namespaces, so that, as
1247 1232 far as possible, we're not keeping any hidden references to it.
1248 1233
1249 1234 Parameters
1250 1235 ----------
1251 1236 varname : str
1252 1237 The name of the variable to delete.
1253 1238 by_name : bool
1254 1239 If True, delete variables with the given name in each
1255 1240 namespace. If False (default), find the variable in the user
1256 1241 namespace, and delete references to it.
1257 1242 """
1258 1243 if varname in ('__builtin__', '__builtins__'):
1259 1244 raise ValueError("Refusing to delete %s" % varname)
1260 1245
1261 1246 ns_refs = self.all_ns_refs
1262 1247
1263 1248 if by_name: # Delete by name
1264 1249 for ns in ns_refs:
1265 1250 try:
1266 1251 del ns[varname]
1267 1252 except KeyError:
1268 1253 pass
1269 1254 else: # Delete by object
1270 1255 try:
1271 1256 obj = self.user_ns[varname]
1272 1257 except KeyError:
1273 1258 raise NameError("name '%s' is not defined" % varname)
1274 1259 # Also check in output history
1275 1260 ns_refs.append(self.history_manager.output_hist)
1276 1261 for ns in ns_refs:
1277 1262 to_delete = [n for n, o in iteritems(ns) if o is obj]
1278 1263 for name in to_delete:
1279 1264 del ns[name]
1280 1265
1281 1266 # displayhook keeps extra references, but not in a dictionary
1282 1267 for name in ('_', '__', '___'):
1283 1268 if getattr(self.displayhook, name) is obj:
1284 1269 setattr(self.displayhook, name, None)
1285 1270
1286 1271 def reset_selective(self, regex=None):
1287 1272 """Clear selective variables from internal namespaces based on a
1288 1273 specified regular expression.
1289 1274
1290 1275 Parameters
1291 1276 ----------
1292 1277 regex : string or compiled pattern, optional
1293 1278 A regular expression pattern that will be used in searching
1294 1279 variable names in the users namespaces.
1295 1280 """
1296 1281 if regex is not None:
1297 1282 try:
1298 1283 m = re.compile(regex)
1299 1284 except TypeError:
1300 1285 raise TypeError('regex must be a string or compiled pattern')
1301 1286 # Search for keys in each namespace that match the given regex
1302 1287 # If a match is found, delete the key/value pair.
1303 1288 for ns in self.all_ns_refs:
1304 1289 for var in ns:
1305 1290 if m.search(var):
1306 1291 del ns[var]
1307 1292
1308 1293 def push(self, variables, interactive=True):
1309 1294 """Inject a group of variables into the IPython user namespace.
1310 1295
1311 1296 Parameters
1312 1297 ----------
1313 1298 variables : dict, str or list/tuple of str
1314 1299 The variables to inject into the user's namespace. If a dict, a
1315 1300 simple update is done. If a str, the string is assumed to have
1316 1301 variable names separated by spaces. A list/tuple of str can also
1317 1302 be used to give the variable names. If just the variable names are
1318 1303 give (list/tuple/str) then the variable values looked up in the
1319 1304 callers frame.
1320 1305 interactive : bool
1321 1306 If True (default), the variables will be listed with the ``who``
1322 1307 magic.
1323 1308 """
1324 1309 vdict = None
1325 1310
1326 1311 # We need a dict of name/value pairs to do namespace updates.
1327 1312 if isinstance(variables, dict):
1328 1313 vdict = variables
1329 1314 elif isinstance(variables, string_types+(list, tuple)):
1330 1315 if isinstance(variables, string_types):
1331 1316 vlist = variables.split()
1332 1317 else:
1333 1318 vlist = variables
1334 1319 vdict = {}
1335 1320 cf = sys._getframe(1)
1336 1321 for name in vlist:
1337 1322 try:
1338 1323 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1339 1324 except:
1340 1325 print('Could not get variable %s from %s' %
1341 1326 (name,cf.f_code.co_name))
1342 1327 else:
1343 1328 raise ValueError('variables must be a dict/str/list/tuple')
1344 1329
1345 1330 # Propagate variables to user namespace
1346 1331 self.user_ns.update(vdict)
1347 1332
1348 1333 # And configure interactive visibility
1349 1334 user_ns_hidden = self.user_ns_hidden
1350 1335 if interactive:
1351 1336 for name in vdict:
1352 1337 user_ns_hidden.pop(name, None)
1353 1338 else:
1354 1339 user_ns_hidden.update(vdict)
1355 1340
1356 1341 def drop_by_id(self, variables):
1357 1342 """Remove a dict of variables from the user namespace, if they are the
1358 1343 same as the values in the dictionary.
1359 1344
1360 1345 This is intended for use by extensions: variables that they've added can
1361 1346 be taken back out if they are unloaded, without removing any that the
1362 1347 user has overwritten.
1363 1348
1364 1349 Parameters
1365 1350 ----------
1366 1351 variables : dict
1367 1352 A dictionary mapping object names (as strings) to the objects.
1368 1353 """
1369 1354 for name, obj in iteritems(variables):
1370 1355 if name in self.user_ns and self.user_ns[name] is obj:
1371 1356 del self.user_ns[name]
1372 1357 self.user_ns_hidden.pop(name, None)
1373 1358
1374 1359 #-------------------------------------------------------------------------
1375 1360 # Things related to object introspection
1376 1361 #-------------------------------------------------------------------------
1377 1362
1378 1363 def _ofind(self, oname, namespaces=None):
1379 1364 """Find an object in the available namespaces.
1380 1365
1381 1366 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1382 1367
1383 1368 Has special code to detect magic functions.
1384 1369 """
1385 1370 oname = oname.strip()
1386 1371 #print '1- oname: <%r>' % oname # dbg
1387 1372 if not oname.startswith(ESC_MAGIC) and \
1388 1373 not oname.startswith(ESC_MAGIC2) and \
1389 1374 not py3compat.isidentifier(oname, dotted=True):
1390 1375 return dict(found=False)
1391 1376
1392 1377 if namespaces is None:
1393 1378 # Namespaces to search in:
1394 1379 # Put them in a list. The order is important so that we
1395 1380 # find things in the same order that Python finds them.
1396 1381 namespaces = [ ('Interactive', self.user_ns),
1397 1382 ('Interactive (global)', self.user_global_ns),
1398 1383 ('Python builtin', builtin_mod.__dict__),
1399 1384 ]
1400 1385
1401 1386 # initialize results to 'null'
1402 1387 found = False; obj = None; ospace = None;
1403 1388 ismagic = False; isalias = False; parent = None
1404 1389
1405 1390 # We need to special-case 'print', which as of python2.6 registers as a
1406 1391 # function but should only be treated as one if print_function was
1407 1392 # loaded with a future import. In this case, just bail.
1408 1393 if (oname == 'print' and not py3compat.PY3 and not \
1409 1394 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1410 1395 return {'found':found, 'obj':obj, 'namespace':ospace,
1411 1396 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1412 1397
1413 1398 # Look for the given name by splitting it in parts. If the head is
1414 1399 # found, then we look for all the remaining parts as members, and only
1415 1400 # declare success if we can find them all.
1416 1401 oname_parts = oname.split('.')
1417 1402 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1418 1403 for nsname,ns in namespaces:
1419 1404 try:
1420 1405 obj = ns[oname_head]
1421 1406 except KeyError:
1422 1407 continue
1423 1408 else:
1424 1409 #print 'oname_rest:', oname_rest # dbg
1425 1410 for idx, part in enumerate(oname_rest):
1426 1411 try:
1427 1412 parent = obj
1428 1413 # The last part is looked up in a special way to avoid
1429 1414 # descriptor invocation as it may raise or have side
1430 1415 # effects.
1431 1416 if idx == len(oname_rest) - 1:
1432 1417 obj = self._getattr_property(obj, part)
1433 1418 else:
1434 1419 obj = getattr(obj, part)
1435 1420 except:
1436 1421 # Blanket except b/c some badly implemented objects
1437 1422 # allow __getattr__ to raise exceptions other than
1438 1423 # AttributeError, which then crashes IPython.
1439 1424 break
1440 1425 else:
1441 1426 # If we finish the for loop (no break), we got all members
1442 1427 found = True
1443 1428 ospace = nsname
1444 1429 break # namespace loop
1445 1430
1446 1431 # Try to see if it's magic
1447 1432 if not found:
1448 1433 obj = None
1449 1434 if oname.startswith(ESC_MAGIC2):
1450 1435 oname = oname.lstrip(ESC_MAGIC2)
1451 1436 obj = self.find_cell_magic(oname)
1452 1437 elif oname.startswith(ESC_MAGIC):
1453 1438 oname = oname.lstrip(ESC_MAGIC)
1454 1439 obj = self.find_line_magic(oname)
1455 1440 else:
1456 1441 # search without prefix, so run? will find %run?
1457 1442 obj = self.find_line_magic(oname)
1458 1443 if obj is None:
1459 1444 obj = self.find_cell_magic(oname)
1460 1445 if obj is not None:
1461 1446 found = True
1462 1447 ospace = 'IPython internal'
1463 1448 ismagic = True
1464 1449 isalias = isinstance(obj, Alias)
1465 1450
1466 1451 # Last try: special-case some literals like '', [], {}, etc:
1467 1452 if not found and oname_head in ["''",'""','[]','{}','()']:
1468 1453 obj = eval(oname_head)
1469 1454 found = True
1470 1455 ospace = 'Interactive'
1471 1456
1472 1457 return {'found':found, 'obj':obj, 'namespace':ospace,
1473 1458 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1474 1459
1475 1460 @staticmethod
1476 1461 def _getattr_property(obj, attrname):
1477 1462 """Property-aware getattr to use in object finding.
1478 1463
1479 1464 If attrname represents a property, return it unevaluated (in case it has
1480 1465 side effects or raises an error.
1481 1466
1482 1467 """
1483 1468 if not isinstance(obj, type):
1484 1469 try:
1485 1470 # `getattr(type(obj), attrname)` is not guaranteed to return
1486 1471 # `obj`, but does so for property:
1487 1472 #
1488 1473 # property.__get__(self, None, cls) -> self
1489 1474 #
1490 1475 # The universal alternative is to traverse the mro manually
1491 1476 # searching for attrname in class dicts.
1492 1477 attr = getattr(type(obj), attrname)
1493 1478 except AttributeError:
1494 1479 pass
1495 1480 else:
1496 1481 # This relies on the fact that data descriptors (with both
1497 1482 # __get__ & __set__ magic methods) take precedence over
1498 1483 # instance-level attributes:
1499 1484 #
1500 1485 # class A(object):
1501 1486 # @property
1502 1487 # def foobar(self): return 123
1503 1488 # a = A()
1504 1489 # a.__dict__['foobar'] = 345
1505 1490 # a.foobar # == 123
1506 1491 #
1507 1492 # So, a property may be returned right away.
1508 1493 if isinstance(attr, property):
1509 1494 return attr
1510 1495
1511 1496 # Nothing helped, fall back.
1512 1497 return getattr(obj, attrname)
1513 1498
1514 1499 def _object_find(self, oname, namespaces=None):
1515 1500 """Find an object and return a struct with info about it."""
1516 1501 return Struct(self._ofind(oname, namespaces))
1517 1502
1518 1503 def _inspect(self, meth, oname, namespaces=None, **kw):
1519 1504 """Generic interface to the inspector system.
1520 1505
1521 1506 This function is meant to be called by pdef, pdoc & friends.
1522 1507 """
1523 1508 info = self._object_find(oname, namespaces)
1524 1509 docformat = sphinxify if self.sphinxify_docstring else None
1525 1510 if info.found:
1526 1511 pmethod = getattr(self.inspector, meth)
1527 1512 # TODO: only apply format_screen to the plain/text repr of the mime
1528 1513 # bundle.
1529 1514 formatter = format_screen if info.ismagic else docformat
1530 1515 if meth == 'pdoc':
1531 1516 pmethod(info.obj, oname, formatter)
1532 1517 elif meth == 'pinfo':
1533 1518 pmethod(info.obj, oname, formatter, info,
1534 1519 enable_html_pager=self.enable_html_pager, **kw)
1535 1520 else:
1536 1521 pmethod(info.obj, oname)
1537 1522 else:
1538 1523 print('Object `%s` not found.' % oname)
1539 1524 return 'not found' # so callers can take other action
1540 1525
1541 1526 def object_inspect(self, oname, detail_level=0):
1542 1527 """Get object info about oname"""
1543 1528 with self.builtin_trap:
1544 1529 info = self._object_find(oname)
1545 1530 if info.found:
1546 1531 return self.inspector.info(info.obj, oname, info=info,
1547 1532 detail_level=detail_level
1548 1533 )
1549 1534 else:
1550 1535 return oinspect.object_info(name=oname, found=False)
1551 1536
1552 1537 def object_inspect_text(self, oname, detail_level=0):
1553 1538 """Get object info as formatted text"""
1554 1539 return self.object_inspect_mime(oname, detail_level)['text/plain']
1555 1540
1556 1541 def object_inspect_mime(self, oname, detail_level=0):
1557 1542 """Get object info as a mimebundle of formatted representations.
1558 1543
1559 1544 A mimebundle is a dictionary, keyed by mime-type.
1560 1545 It must always have the key `'text/plain'`.
1561 1546 """
1562 1547 with self.builtin_trap:
1563 1548 info = self._object_find(oname)
1564 1549 if info.found:
1565 1550 return self.inspector._get_info(info.obj, oname, info=info,
1566 1551 detail_level=detail_level
1567 1552 )
1568 1553 else:
1569 1554 raise KeyError(oname)
1570 1555
1571 1556 #-------------------------------------------------------------------------
1572 1557 # Things related to history management
1573 1558 #-------------------------------------------------------------------------
1574 1559
1575 1560 def init_history(self):
1576 1561 """Sets up the command history, and starts regular autosaves."""
1577 1562 self.history_manager = HistoryManager(shell=self, parent=self)
1578 1563 self.configurables.append(self.history_manager)
1579 1564
1580 1565 #-------------------------------------------------------------------------
1581 1566 # Things related to exception handling and tracebacks (not debugging)
1582 1567 #-------------------------------------------------------------------------
1583 1568
1584 1569 debugger_cls = Pdb
1585 1570
1586 1571 def init_traceback_handlers(self, custom_exceptions):
1587 1572 # Syntax error handler.
1588 1573 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1589 1574
1590 1575 # The interactive one is initialized with an offset, meaning we always
1591 1576 # want to remove the topmost item in the traceback, which is our own
1592 1577 # internal code. Valid modes: ['Plain','Context','Verbose']
1593 1578 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1594 1579 color_scheme='NoColor',
1595 1580 tb_offset = 1,
1596 1581 check_cache=check_linecache_ipython,
1597 1582 debugger_cls=self.debugger_cls)
1598 1583
1599 1584 # The instance will store a pointer to the system-wide exception hook,
1600 1585 # so that runtime code (such as magics) can access it. This is because
1601 1586 # during the read-eval loop, it may get temporarily overwritten.
1602 1587 self.sys_excepthook = sys.excepthook
1603 1588
1604 1589 # and add any custom exception handlers the user may have specified
1605 1590 self.set_custom_exc(*custom_exceptions)
1606 1591
1607 1592 # Set the exception mode
1608 1593 self.InteractiveTB.set_mode(mode=self.xmode)
1609 1594
1610 1595 def set_custom_exc(self, exc_tuple, handler):
1611 1596 """set_custom_exc(exc_tuple, handler)
1612 1597
1613 1598 Set a custom exception handler, which will be called if any of the
1614 1599 exceptions in exc_tuple occur in the mainloop (specifically, in the
1615 1600 run_code() method).
1616 1601
1617 1602 Parameters
1618 1603 ----------
1619 1604
1620 1605 exc_tuple : tuple of exception classes
1621 1606 A *tuple* of exception classes, for which to call the defined
1622 1607 handler. It is very important that you use a tuple, and NOT A
1623 1608 LIST here, because of the way Python's except statement works. If
1624 1609 you only want to trap a single exception, use a singleton tuple::
1625 1610
1626 1611 exc_tuple == (MyCustomException,)
1627 1612
1628 1613 handler : callable
1629 1614 handler must have the following signature::
1630 1615
1631 1616 def my_handler(self, etype, value, tb, tb_offset=None):
1632 1617 ...
1633 1618 return structured_traceback
1634 1619
1635 1620 Your handler must return a structured traceback (a list of strings),
1636 1621 or None.
1637 1622
1638 1623 This will be made into an instance method (via types.MethodType)
1639 1624 of IPython itself, and it will be called if any of the exceptions
1640 1625 listed in the exc_tuple are caught. If the handler is None, an
1641 1626 internal basic one is used, which just prints basic info.
1642 1627
1643 1628 To protect IPython from crashes, if your handler ever raises an
1644 1629 exception or returns an invalid result, it will be immediately
1645 1630 disabled.
1646 1631
1647 1632 WARNING: by putting in your own exception handler into IPython's main
1648 1633 execution loop, you run a very good chance of nasty crashes. This
1649 1634 facility should only be used if you really know what you are doing."""
1650 1635
1651 1636 assert type(exc_tuple)==type(()) , \
1652 1637 "The custom exceptions must be given AS A TUPLE."
1653 1638
1654 1639 def dummy_handler(self, etype, value, tb, tb_offset=None):
1655 1640 print('*** Simple custom exception handler ***')
1656 1641 print('Exception type :',etype)
1657 1642 print('Exception value:',value)
1658 1643 print('Traceback :',tb)
1659 1644 #print 'Source code :','\n'.join(self.buffer)
1660 1645
1661 1646 def validate_stb(stb):
1662 1647 """validate structured traceback return type
1663 1648
1664 1649 return type of CustomTB *should* be a list of strings, but allow
1665 1650 single strings or None, which are harmless.
1666 1651
1667 1652 This function will *always* return a list of strings,
1668 1653 and will raise a TypeError if stb is inappropriate.
1669 1654 """
1670 1655 msg = "CustomTB must return list of strings, not %r" % stb
1671 1656 if stb is None:
1672 1657 return []
1673 1658 elif isinstance(stb, string_types):
1674 1659 return [stb]
1675 1660 elif not isinstance(stb, list):
1676 1661 raise TypeError(msg)
1677 1662 # it's a list
1678 1663 for line in stb:
1679 1664 # check every element
1680 1665 if not isinstance(line, string_types):
1681 1666 raise TypeError(msg)
1682 1667 return stb
1683 1668
1684 1669 if handler is None:
1685 1670 wrapped = dummy_handler
1686 1671 else:
1687 1672 def wrapped(self,etype,value,tb,tb_offset=None):
1688 1673 """wrap CustomTB handler, to protect IPython from user code
1689 1674
1690 1675 This makes it harder (but not impossible) for custom exception
1691 1676 handlers to crash IPython.
1692 1677 """
1693 1678 try:
1694 1679 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1695 1680 return validate_stb(stb)
1696 1681 except:
1697 1682 # clear custom handler immediately
1698 1683 self.set_custom_exc((), None)
1699 1684 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1700 1685 # show the exception in handler first
1701 1686 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1702 1687 print(self.InteractiveTB.stb2text(stb))
1703 1688 print("The original exception:")
1704 1689 stb = self.InteractiveTB.structured_traceback(
1705 1690 (etype,value,tb), tb_offset=tb_offset
1706 1691 )
1707 1692 return stb
1708 1693
1709 1694 self.CustomTB = types.MethodType(wrapped,self)
1710 1695 self.custom_exceptions = exc_tuple
1711 1696
1712 1697 def excepthook(self, etype, value, tb):
1713 1698 """One more defense for GUI apps that call sys.excepthook.
1714 1699
1715 1700 GUI frameworks like wxPython trap exceptions and call
1716 1701 sys.excepthook themselves. I guess this is a feature that
1717 1702 enables them to keep running after exceptions that would
1718 1703 otherwise kill their mainloop. This is a bother for IPython
1719 1704 which excepts to catch all of the program exceptions with a try:
1720 1705 except: statement.
1721 1706
1722 1707 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1723 1708 any app directly invokes sys.excepthook, it will look to the user like
1724 1709 IPython crashed. In order to work around this, we can disable the
1725 1710 CrashHandler and replace it with this excepthook instead, which prints a
1726 1711 regular traceback using our InteractiveTB. In this fashion, apps which
1727 1712 call sys.excepthook will generate a regular-looking exception from
1728 1713 IPython, and the CrashHandler will only be triggered by real IPython
1729 1714 crashes.
1730 1715
1731 1716 This hook should be used sparingly, only in places which are not likely
1732 1717 to be true IPython errors.
1733 1718 """
1734 1719 self.showtraceback((etype, value, tb), tb_offset=0)
1735 1720
1736 1721 def _get_exc_info(self, exc_tuple=None):
1737 1722 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1738 1723
1739 1724 Ensures sys.last_type,value,traceback hold the exc_info we found,
1740 1725 from whichever source.
1741 1726
1742 1727 raises ValueError if none of these contain any information
1743 1728 """
1744 1729 if exc_tuple is None:
1745 1730 etype, value, tb = sys.exc_info()
1746 1731 else:
1747 1732 etype, value, tb = exc_tuple
1748 1733
1749 1734 if etype is None:
1750 1735 if hasattr(sys, 'last_type'):
1751 1736 etype, value, tb = sys.last_type, sys.last_value, \
1752 1737 sys.last_traceback
1753 1738
1754 1739 if etype is None:
1755 1740 raise ValueError("No exception to find")
1756 1741
1757 1742 # Now store the exception info in sys.last_type etc.
1758 1743 # WARNING: these variables are somewhat deprecated and not
1759 1744 # necessarily safe to use in a threaded environment, but tools
1760 1745 # like pdb depend on their existence, so let's set them. If we
1761 1746 # find problems in the field, we'll need to revisit their use.
1762 1747 sys.last_type = etype
1763 1748 sys.last_value = value
1764 1749 sys.last_traceback = tb
1765 1750
1766 1751 return etype, value, tb
1767 1752
1768 1753 def show_usage_error(self, exc):
1769 1754 """Show a short message for UsageErrors
1770 1755
1771 1756 These are special exceptions that shouldn't show a traceback.
1772 1757 """
1773 1758 print("UsageError: %s" % exc, file=sys.stderr)
1774 1759
1775 1760 def get_exception_only(self, exc_tuple=None):
1776 1761 """
1777 1762 Return as a string (ending with a newline) the exception that
1778 1763 just occurred, without any traceback.
1779 1764 """
1780 1765 etype, value, tb = self._get_exc_info(exc_tuple)
1781 1766 msg = traceback.format_exception_only(etype, value)
1782 1767 return ''.join(msg)
1783 1768
1784 1769 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1785 1770 exception_only=False):
1786 1771 """Display the exception that just occurred.
1787 1772
1788 1773 If nothing is known about the exception, this is the method which
1789 1774 should be used throughout the code for presenting user tracebacks,
1790 1775 rather than directly invoking the InteractiveTB object.
1791 1776
1792 1777 A specific showsyntaxerror() also exists, but this method can take
1793 1778 care of calling it if needed, so unless you are explicitly catching a
1794 1779 SyntaxError exception, don't try to analyze the stack manually and
1795 1780 simply call this method."""
1796 1781
1797 1782 try:
1798 1783 try:
1799 1784 etype, value, tb = self._get_exc_info(exc_tuple)
1800 1785 except ValueError:
1801 1786 print('No traceback available to show.', file=sys.stderr)
1802 1787 return
1803 1788
1804 1789 if issubclass(etype, SyntaxError):
1805 1790 # Though this won't be called by syntax errors in the input
1806 1791 # line, there may be SyntaxError cases with imported code.
1807 1792 self.showsyntaxerror(filename)
1808 1793 elif etype is UsageError:
1809 1794 self.show_usage_error(value)
1810 1795 else:
1811 1796 if exception_only:
1812 1797 stb = ['An exception has occurred, use %tb to see '
1813 1798 'the full traceback.\n']
1814 1799 stb.extend(self.InteractiveTB.get_exception_only(etype,
1815 1800 value))
1816 1801 else:
1817 1802 try:
1818 1803 # Exception classes can customise their traceback - we
1819 1804 # use this in IPython.parallel for exceptions occurring
1820 1805 # in the engines. This should return a list of strings.
1821 1806 stb = value._render_traceback_()
1822 1807 except Exception:
1823 1808 stb = self.InteractiveTB.structured_traceback(etype,
1824 1809 value, tb, tb_offset=tb_offset)
1825 1810
1826 1811 self._showtraceback(etype, value, stb)
1827 1812 if self.call_pdb:
1828 1813 # drop into debugger
1829 1814 self.debugger(force=True)
1830 1815 return
1831 1816
1832 1817 # Actually show the traceback
1833 1818 self._showtraceback(etype, value, stb)
1834 1819
1835 1820 except KeyboardInterrupt:
1836 1821 print('\n' + self.get_exception_only(), file=sys.stderr)
1837 1822
1838 1823 def _showtraceback(self, etype, evalue, stb):
1839 1824 """Actually show a traceback.
1840 1825
1841 1826 Subclasses may override this method to put the traceback on a different
1842 1827 place, like a side channel.
1843 1828 """
1844 1829 print(self.InteractiveTB.stb2text(stb))
1845 1830
1846 1831 def showsyntaxerror(self, filename=None):
1847 1832 """Display the syntax error that just occurred.
1848 1833
1849 1834 This doesn't display a stack trace because there isn't one.
1850 1835
1851 1836 If a filename is given, it is stuffed in the exception instead
1852 1837 of what was there before (because Python's parser always uses
1853 1838 "<string>" when reading from a string).
1854 1839 """
1855 1840 etype, value, last_traceback = self._get_exc_info()
1856 1841
1857 1842 if filename and issubclass(etype, SyntaxError):
1858 1843 try:
1859 1844 value.filename = filename
1860 1845 except:
1861 1846 # Not the format we expect; leave it alone
1862 1847 pass
1863 1848
1864 1849 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1865 1850 self._showtraceback(etype, value, stb)
1866 1851
1867 1852 # This is overridden in TerminalInteractiveShell to show a message about
1868 1853 # the %paste magic.
1869 1854 def showindentationerror(self):
1870 1855 """Called by run_cell when there's an IndentationError in code entered
1871 1856 at the prompt.
1872 1857
1873 1858 This is overridden in TerminalInteractiveShell to show a message about
1874 1859 the %paste magic."""
1875 1860 self.showsyntaxerror()
1876 1861
1877 1862 #-------------------------------------------------------------------------
1878 1863 # Things related to readline
1879 1864 #-------------------------------------------------------------------------
1880 1865
1881 1866 def init_readline(self):
1882 1867 """DEPRECATED
1883 1868
1884 1869 Moved to terminal subclass, here only to simplify the init logic."""
1885 1870 # Set a number of methods that depend on readline to be no-op
1886 1871 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1887 1872 DeprecationWarning, stacklevel=2)
1888 1873 self.set_custom_completer = no_op
1889 1874
1890 1875 @skip_doctest
1891 1876 def set_next_input(self, s, replace=False):
1892 1877 """ Sets the 'default' input string for the next command line.
1893 1878
1894 1879 Example::
1895 1880
1896 1881 In [1]: _ip.set_next_input("Hello Word")
1897 1882 In [2]: Hello Word_ # cursor is here
1898 1883 """
1899 1884 self.rl_next_input = py3compat.cast_bytes_py2(s)
1900 1885
1901 1886 def _indent_current_str(self):
1902 1887 """return the current level of indentation as a string"""
1903 1888 return self.input_splitter.indent_spaces * ' '
1904 1889
1905 1890 #-------------------------------------------------------------------------
1906 1891 # Things related to text completion
1907 1892 #-------------------------------------------------------------------------
1908 1893
1909 1894 def init_completer(self):
1910 1895 """Initialize the completion machinery.
1911 1896
1912 1897 This creates completion machinery that can be used by client code,
1913 1898 either interactively in-process (typically triggered by the readline
1914 1899 library), programmatically (such as in test suites) or out-of-process
1915 1900 (typically over the network by remote frontends).
1916 1901 """
1917 1902 from IPython.core.completer import IPCompleter
1918 1903 from IPython.core.completerlib import (module_completer,
1919 1904 magic_run_completer, cd_completer, reset_completer)
1920 1905
1921 1906 self.Completer = IPCompleter(shell=self,
1922 1907 namespace=self.user_ns,
1923 1908 global_namespace=self.user_global_ns,
1924 1909 use_readline=False,
1925 1910 parent=self,
1926 1911 )
1927 1912 self.configurables.append(self.Completer)
1928 1913
1929 1914 # Add custom completers to the basic ones built into IPCompleter
1930 1915 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1931 1916 self.strdispatchers['complete_command'] = sdisp
1932 1917 self.Completer.custom_completers = sdisp
1933 1918
1934 1919 self.set_hook('complete_command', module_completer, str_key = 'import')
1935 1920 self.set_hook('complete_command', module_completer, str_key = 'from')
1936 1921 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1937 1922 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1938 1923 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1939 1924 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1940 1925
1941 1926
1942 1927 @skip_doctest_py2
1943 1928 def complete(self, text, line=None, cursor_pos=None):
1944 1929 """Return the completed text and a list of completions.
1945 1930
1946 1931 Parameters
1947 1932 ----------
1948 1933
1949 1934 text : string
1950 1935 A string of text to be completed on. It can be given as empty and
1951 1936 instead a line/position pair are given. In this case, the
1952 1937 completer itself will split the line like readline does.
1953 1938
1954 1939 line : string, optional
1955 1940 The complete line that text is part of.
1956 1941
1957 1942 cursor_pos : int, optional
1958 1943 The position of the cursor on the input line.
1959 1944
1960 1945 Returns
1961 1946 -------
1962 1947 text : string
1963 1948 The actual text that was completed.
1964 1949
1965 1950 matches : list
1966 1951 A sorted list with all possible completions.
1967 1952
1968 1953 The optional arguments allow the completion to take more context into
1969 1954 account, and are part of the low-level completion API.
1970 1955
1971 1956 This is a wrapper around the completion mechanism, similar to what
1972 1957 readline does at the command line when the TAB key is hit. By
1973 1958 exposing it as a method, it can be used by other non-readline
1974 1959 environments (such as GUIs) for text completion.
1975 1960
1976 1961 Simple usage example:
1977 1962
1978 1963 In [1]: x = 'hello'
1979 1964
1980 1965 In [2]: _ip.complete('x.l')
1981 1966 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1982 1967 """
1983 1968
1984 1969 # Inject names into __builtin__ so we can complete on the added names.
1985 1970 with self.builtin_trap:
1986 1971 return self.Completer.complete(text, line, cursor_pos)
1987 1972
1988 1973 def set_custom_completer(self, completer, pos=0):
1989 1974 """Adds a new custom completer function.
1990 1975
1991 1976 The position argument (defaults to 0) is the index in the completers
1992 1977 list where you want the completer to be inserted."""
1993 1978
1994 1979 newcomp = types.MethodType(completer,self.Completer)
1995 1980 self.Completer.matchers.insert(pos,newcomp)
1996 1981
1997 1982 def set_completer_frame(self, frame=None):
1998 1983 """Set the frame of the completer."""
1999 1984 if frame:
2000 1985 self.Completer.namespace = frame.f_locals
2001 1986 self.Completer.global_namespace = frame.f_globals
2002 1987 else:
2003 1988 self.Completer.namespace = self.user_ns
2004 1989 self.Completer.global_namespace = self.user_global_ns
2005 1990
2006 1991 #-------------------------------------------------------------------------
2007 1992 # Things related to magics
2008 1993 #-------------------------------------------------------------------------
2009 1994
2010 1995 def init_magics(self):
2011 1996 from IPython.core import magics as m
2012 1997 self.magics_manager = magic.MagicsManager(shell=self,
2013 1998 parent=self,
2014 1999 user_magics=m.UserMagics(self))
2015 2000 self.configurables.append(self.magics_manager)
2016 2001
2017 2002 # Expose as public API from the magics manager
2018 2003 self.register_magics = self.magics_manager.register
2019 2004
2020 2005 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2021 2006 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2022 2007 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2023 2008 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2024 2009 )
2025 2010
2026 2011 # Register Magic Aliases
2027 2012 mman = self.magics_manager
2028 2013 # FIXME: magic aliases should be defined by the Magics classes
2029 2014 # or in MagicsManager, not here
2030 2015 mman.register_alias('ed', 'edit')
2031 2016 mman.register_alias('hist', 'history')
2032 2017 mman.register_alias('rep', 'recall')
2033 2018 mman.register_alias('SVG', 'svg', 'cell')
2034 2019 mman.register_alias('HTML', 'html', 'cell')
2035 2020 mman.register_alias('file', 'writefile', 'cell')
2036 2021
2037 2022 # FIXME: Move the color initialization to the DisplayHook, which
2038 2023 # should be split into a prompt manager and displayhook. We probably
2039 2024 # even need a centralize colors management object.
2040 2025 self.magic('colors %s' % self.colors)
2041 2026
2042 2027 # Defined here so that it's included in the documentation
2043 2028 @functools.wraps(magic.MagicsManager.register_function)
2044 2029 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2045 2030 self.magics_manager.register_function(func,
2046 2031 magic_kind=magic_kind, magic_name=magic_name)
2047 2032
2048 2033 def run_line_magic(self, magic_name, line):
2049 2034 """Execute the given line magic.
2050 2035
2051 2036 Parameters
2052 2037 ----------
2053 2038 magic_name : str
2054 2039 Name of the desired magic function, without '%' prefix.
2055 2040
2056 2041 line : str
2057 2042 The rest of the input line as a single string.
2058 2043 """
2059 2044 fn = self.find_line_magic(magic_name)
2060 2045 if fn is None:
2061 2046 cm = self.find_cell_magic(magic_name)
2062 2047 etpl = "Line magic function `%%%s` not found%s."
2063 2048 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2064 2049 'did you mean that instead?)' % magic_name )
2065 2050 error(etpl % (magic_name, extra))
2066 2051 else:
2067 2052 # Note: this is the distance in the stack to the user's frame.
2068 2053 # This will need to be updated if the internal calling logic gets
2069 2054 # refactored, or else we'll be expanding the wrong variables.
2070 2055 stack_depth = 2
2071 2056 magic_arg_s = self.var_expand(line, stack_depth)
2072 2057 # Put magic args in a list so we can call with f(*a) syntax
2073 2058 args = [magic_arg_s]
2074 2059 kwargs = {}
2075 2060 # Grab local namespace if we need it:
2076 2061 if getattr(fn, "needs_local_scope", False):
2077 2062 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2078 2063 with self.builtin_trap:
2079 2064 result = fn(*args,**kwargs)
2080 2065 return result
2081 2066
2082 2067 def run_cell_magic(self, magic_name, line, cell):
2083 2068 """Execute the given cell magic.
2084 2069
2085 2070 Parameters
2086 2071 ----------
2087 2072 magic_name : str
2088 2073 Name of the desired magic function, without '%' prefix.
2089 2074
2090 2075 line : str
2091 2076 The rest of the first input line as a single string.
2092 2077
2093 2078 cell : str
2094 2079 The body of the cell as a (possibly multiline) string.
2095 2080 """
2096 2081 fn = self.find_cell_magic(magic_name)
2097 2082 if fn is None:
2098 2083 lm = self.find_line_magic(magic_name)
2099 2084 etpl = "Cell magic `%%{0}` not found{1}."
2100 2085 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2101 2086 'did you mean that instead?)'.format(magic_name))
2102 2087 error(etpl.format(magic_name, extra))
2103 2088 elif cell == '':
2104 2089 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2105 2090 if self.find_line_magic(magic_name) is not None:
2106 2091 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2107 2092 raise UsageError(message)
2108 2093 else:
2109 2094 # Note: this is the distance in the stack to the user's frame.
2110 2095 # This will need to be updated if the internal calling logic gets
2111 2096 # refactored, or else we'll be expanding the wrong variables.
2112 2097 stack_depth = 2
2113 2098 magic_arg_s = self.var_expand(line, stack_depth)
2114 2099 with self.builtin_trap:
2115 2100 result = fn(magic_arg_s, cell)
2116 2101 return result
2117 2102
2118 2103 def find_line_magic(self, magic_name):
2119 2104 """Find and return a line magic by name.
2120 2105
2121 2106 Returns None if the magic isn't found."""
2122 2107 return self.magics_manager.magics['line'].get(magic_name)
2123 2108
2124 2109 def find_cell_magic(self, magic_name):
2125 2110 """Find and return a cell magic by name.
2126 2111
2127 2112 Returns None if the magic isn't found."""
2128 2113 return self.magics_manager.magics['cell'].get(magic_name)
2129 2114
2130 2115 def find_magic(self, magic_name, magic_kind='line'):
2131 2116 """Find and return a magic of the given type by name.
2132 2117
2133 2118 Returns None if the magic isn't found."""
2134 2119 return self.magics_manager.magics[magic_kind].get(magic_name)
2135 2120
2136 2121 def magic(self, arg_s):
2137 2122 """DEPRECATED. Use run_line_magic() instead.
2138 2123
2139 2124 Call a magic function by name.
2140 2125
2141 2126 Input: a string containing the name of the magic function to call and
2142 2127 any additional arguments to be passed to the magic.
2143 2128
2144 2129 magic('name -opt foo bar') is equivalent to typing at the ipython
2145 2130 prompt:
2146 2131
2147 2132 In[1]: %name -opt foo bar
2148 2133
2149 2134 To call a magic without arguments, simply use magic('name').
2150 2135
2151 2136 This provides a proper Python function to call IPython's magics in any
2152 2137 valid Python code you can type at the interpreter, including loops and
2153 2138 compound statements.
2154 2139 """
2155 2140 # TODO: should we issue a loud deprecation warning here?
2156 2141 magic_name, _, magic_arg_s = arg_s.partition(' ')
2157 2142 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2158 2143 return self.run_line_magic(magic_name, magic_arg_s)
2159 2144
2160 2145 #-------------------------------------------------------------------------
2161 2146 # Things related to macros
2162 2147 #-------------------------------------------------------------------------
2163 2148
2164 2149 def define_macro(self, name, themacro):
2165 2150 """Define a new macro
2166 2151
2167 2152 Parameters
2168 2153 ----------
2169 2154 name : str
2170 2155 The name of the macro.
2171 2156 themacro : str or Macro
2172 2157 The action to do upon invoking the macro. If a string, a new
2173 2158 Macro object is created by passing the string to it.
2174 2159 """
2175 2160
2176 2161 from IPython.core import macro
2177 2162
2178 2163 if isinstance(themacro, string_types):
2179 2164 themacro = macro.Macro(themacro)
2180 2165 if not isinstance(themacro, macro.Macro):
2181 2166 raise ValueError('A macro must be a string or a Macro instance.')
2182 2167 self.user_ns[name] = themacro
2183 2168
2184 2169 #-------------------------------------------------------------------------
2185 2170 # Things related to the running of system commands
2186 2171 #-------------------------------------------------------------------------
2187 2172
2188 2173 def system_piped(self, cmd):
2189 2174 """Call the given cmd in a subprocess, piping stdout/err
2190 2175
2191 2176 Parameters
2192 2177 ----------
2193 2178 cmd : str
2194 2179 Command to execute (can not end in '&', as background processes are
2195 2180 not supported. Should not be a command that expects input
2196 2181 other than simple text.
2197 2182 """
2198 2183 if cmd.rstrip().endswith('&'):
2199 2184 # this is *far* from a rigorous test
2200 2185 # We do not support backgrounding processes because we either use
2201 2186 # pexpect or pipes to read from. Users can always just call
2202 2187 # os.system() or use ip.system=ip.system_raw
2203 2188 # if they really want a background process.
2204 2189 raise OSError("Background processes not supported.")
2205 2190
2206 2191 # we explicitly do NOT return the subprocess status code, because
2207 2192 # a non-None value would trigger :func:`sys.displayhook` calls.
2208 2193 # Instead, we store the exit_code in user_ns.
2209 2194 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2210 2195
2211 2196 def system_raw(self, cmd):
2212 2197 """Call the given cmd in a subprocess using os.system on Windows or
2213 2198 subprocess.call using the system shell on other platforms.
2214 2199
2215 2200 Parameters
2216 2201 ----------
2217 2202 cmd : str
2218 2203 Command to execute.
2219 2204 """
2220 2205 cmd = self.var_expand(cmd, depth=1)
2221 2206 # protect os.system from UNC paths on Windows, which it can't handle:
2222 2207 if sys.platform == 'win32':
2223 2208 from IPython.utils._process_win32 import AvoidUNCPath
2224 2209 with AvoidUNCPath() as path:
2225 2210 if path is not None:
2226 2211 cmd = '"pushd %s &&"%s' % (path, cmd)
2227 2212 cmd = py3compat.unicode_to_str(cmd)
2228 2213 try:
2229 2214 ec = os.system(cmd)
2230 2215 except KeyboardInterrupt:
2231 2216 print('\n' + self.get_exception_only(), file=sys.stderr)
2232 2217 ec = -2
2233 2218 else:
2234 2219 cmd = py3compat.unicode_to_str(cmd)
2235 2220 # For posix the result of the subprocess.call() below is an exit
2236 2221 # code, which by convention is zero for success, positive for
2237 2222 # program failure. Exit codes above 128 are reserved for signals,
2238 2223 # and the formula for converting a signal to an exit code is usually
2239 2224 # signal_number+128. To more easily differentiate between exit
2240 2225 # codes and signals, ipython uses negative numbers. For instance
2241 2226 # since control-c is signal 2 but exit code 130, ipython's
2242 2227 # _exit_code variable will read -2. Note that some shells like
2243 2228 # csh and fish don't follow sh/bash conventions for exit codes.
2244 2229 executable = os.environ.get('SHELL', None)
2245 2230 try:
2246 2231 # Use env shell instead of default /bin/sh
2247 2232 ec = subprocess.call(cmd, shell=True, executable=executable)
2248 2233 except KeyboardInterrupt:
2249 2234 # intercept control-C; a long traceback is not useful here
2250 2235 print('\n' + self.get_exception_only(), file=sys.stderr)
2251 2236 ec = 130
2252 2237 if ec > 128:
2253 2238 ec = -(ec - 128)
2254 2239
2255 2240 # We explicitly do NOT return the subprocess status code, because
2256 2241 # a non-None value would trigger :func:`sys.displayhook` calls.
2257 2242 # Instead, we store the exit_code in user_ns. Note the semantics
2258 2243 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2259 2244 # but raising SystemExit(_exit_code) will give status 254!
2260 2245 self.user_ns['_exit_code'] = ec
2261 2246
2262 2247 # use piped system by default, because it is better behaved
2263 2248 system = system_piped
2264 2249
2265 2250 def getoutput(self, cmd, split=True, depth=0):
2266 2251 """Get output (possibly including stderr) from a subprocess.
2267 2252
2268 2253 Parameters
2269 2254 ----------
2270 2255 cmd : str
2271 2256 Command to execute (can not end in '&', as background processes are
2272 2257 not supported.
2273 2258 split : bool, optional
2274 2259 If True, split the output into an IPython SList. Otherwise, an
2275 2260 IPython LSString is returned. These are objects similar to normal
2276 2261 lists and strings, with a few convenience attributes for easier
2277 2262 manipulation of line-based output. You can use '?' on them for
2278 2263 details.
2279 2264 depth : int, optional
2280 2265 How many frames above the caller are the local variables which should
2281 2266 be expanded in the command string? The default (0) assumes that the
2282 2267 expansion variables are in the stack frame calling this function.
2283 2268 """
2284 2269 if cmd.rstrip().endswith('&'):
2285 2270 # this is *far* from a rigorous test
2286 2271 raise OSError("Background processes not supported.")
2287 2272 out = getoutput(self.var_expand(cmd, depth=depth+1))
2288 2273 if split:
2289 2274 out = SList(out.splitlines())
2290 2275 else:
2291 2276 out = LSString(out)
2292 2277 return out
2293 2278
2294 2279 #-------------------------------------------------------------------------
2295 2280 # Things related to aliases
2296 2281 #-------------------------------------------------------------------------
2297 2282
2298 2283 def init_alias(self):
2299 2284 self.alias_manager = AliasManager(shell=self, parent=self)
2300 2285 self.configurables.append(self.alias_manager)
2301 2286
2302 2287 #-------------------------------------------------------------------------
2303 2288 # Things related to extensions
2304 2289 #-------------------------------------------------------------------------
2305 2290
2306 2291 def init_extension_manager(self):
2307 2292 self.extension_manager = ExtensionManager(shell=self, parent=self)
2308 2293 self.configurables.append(self.extension_manager)
2309 2294
2310 2295 #-------------------------------------------------------------------------
2311 2296 # Things related to payloads
2312 2297 #-------------------------------------------------------------------------
2313 2298
2314 2299 def init_payload(self):
2315 2300 self.payload_manager = PayloadManager(parent=self)
2316 2301 self.configurables.append(self.payload_manager)
2317 2302
2318 2303 #-------------------------------------------------------------------------
2319 2304 # Things related to the prefilter
2320 2305 #-------------------------------------------------------------------------
2321 2306
2322 2307 def init_prefilter(self):
2323 2308 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2324 2309 self.configurables.append(self.prefilter_manager)
2325 2310 # Ultimately this will be refactored in the new interpreter code, but
2326 2311 # for now, we should expose the main prefilter method (there's legacy
2327 2312 # code out there that may rely on this).
2328 2313 self.prefilter = self.prefilter_manager.prefilter_lines
2329 2314
2330 2315 def auto_rewrite_input(self, cmd):
2331 2316 """Print to the screen the rewritten form of the user's command.
2332 2317
2333 2318 This shows visual feedback by rewriting input lines that cause
2334 2319 automatic calling to kick in, like::
2335 2320
2336 2321 /f x
2337 2322
2338 2323 into::
2339 2324
2340 2325 ------> f(x)
2341 2326
2342 2327 after the user's input prompt. This helps the user understand that the
2343 2328 input line was transformed automatically by IPython.
2344 2329 """
2345 2330 if not self.show_rewritten_input:
2346 2331 return
2347 2332
2348 2333 # This is overridden in TerminalInteractiveShell to use fancy prompts
2349 2334 print("------> " + cmd)
2350 2335
2351 2336 #-------------------------------------------------------------------------
2352 2337 # Things related to extracting values/expressions from kernel and user_ns
2353 2338 #-------------------------------------------------------------------------
2354 2339
2355 2340 def _user_obj_error(self):
2356 2341 """return simple exception dict
2357 2342
2358 2343 for use in user_expressions
2359 2344 """
2360 2345
2361 2346 etype, evalue, tb = self._get_exc_info()
2362 2347 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2363 2348
2364 2349 exc_info = {
2365 2350 u'status' : 'error',
2366 2351 u'traceback' : stb,
2367 2352 u'ename' : unicode_type(etype.__name__),
2368 2353 u'evalue' : py3compat.safe_unicode(evalue),
2369 2354 }
2370 2355
2371 2356 return exc_info
2372 2357
2373 2358 def _format_user_obj(self, obj):
2374 2359 """format a user object to display dict
2375 2360
2376 2361 for use in user_expressions
2377 2362 """
2378 2363
2379 2364 data, md = self.display_formatter.format(obj)
2380 2365 value = {
2381 2366 'status' : 'ok',
2382 2367 'data' : data,
2383 2368 'metadata' : md,
2384 2369 }
2385 2370 return value
2386 2371
2387 2372 def user_expressions(self, expressions):
2388 2373 """Evaluate a dict of expressions in the user's namespace.
2389 2374
2390 2375 Parameters
2391 2376 ----------
2392 2377 expressions : dict
2393 2378 A dict with string keys and string values. The expression values
2394 2379 should be valid Python expressions, each of which will be evaluated
2395 2380 in the user namespace.
2396 2381
2397 2382 Returns
2398 2383 -------
2399 2384 A dict, keyed like the input expressions dict, with the rich mime-typed
2400 2385 display_data of each value.
2401 2386 """
2402 2387 out = {}
2403 2388 user_ns = self.user_ns
2404 2389 global_ns = self.user_global_ns
2405 2390
2406 2391 for key, expr in iteritems(expressions):
2407 2392 try:
2408 2393 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2409 2394 except:
2410 2395 value = self._user_obj_error()
2411 2396 out[key] = value
2412 2397 return out
2413 2398
2414 2399 #-------------------------------------------------------------------------
2415 2400 # Things related to the running of code
2416 2401 #-------------------------------------------------------------------------
2417 2402
2418 2403 def ex(self, cmd):
2419 2404 """Execute a normal python statement in user namespace."""
2420 2405 with self.builtin_trap:
2421 2406 exec(cmd, self.user_global_ns, self.user_ns)
2422 2407
2423 2408 def ev(self, expr):
2424 2409 """Evaluate python expression expr in user namespace.
2425 2410
2426 2411 Returns the result of evaluation
2427 2412 """
2428 2413 with self.builtin_trap:
2429 2414 return eval(expr, self.user_global_ns, self.user_ns)
2430 2415
2431 2416 def safe_execfile(self, fname, *where, **kw):
2432 2417 """A safe version of the builtin execfile().
2433 2418
2434 2419 This version will never throw an exception, but instead print
2435 2420 helpful error messages to the screen. This only works on pure
2436 2421 Python files with the .py extension.
2437 2422
2438 2423 Parameters
2439 2424 ----------
2440 2425 fname : string
2441 2426 The name of the file to be executed.
2442 2427 where : tuple
2443 2428 One or two namespaces, passed to execfile() as (globals,locals).
2444 2429 If only one is given, it is passed as both.
2445 2430 exit_ignore : bool (False)
2446 2431 If True, then silence SystemExit for non-zero status (it is always
2447 2432 silenced for zero status, as it is so common).
2448 2433 raise_exceptions : bool (False)
2449 2434 If True raise exceptions everywhere. Meant for testing.
2450 2435 shell_futures : bool (False)
2451 2436 If True, the code will share future statements with the interactive
2452 2437 shell. It will both be affected by previous __future__ imports, and
2453 2438 any __future__ imports in the code will affect the shell. If False,
2454 2439 __future__ imports are not shared in either direction.
2455 2440
2456 2441 """
2457 2442 kw.setdefault('exit_ignore', False)
2458 2443 kw.setdefault('raise_exceptions', False)
2459 2444 kw.setdefault('shell_futures', False)
2460 2445
2461 2446 fname = os.path.abspath(os.path.expanduser(fname))
2462 2447
2463 2448 # Make sure we can open the file
2464 2449 try:
2465 2450 with open(fname):
2466 2451 pass
2467 2452 except:
2468 2453 warn('Could not open file <%s> for safe execution.' % fname)
2469 2454 return
2470 2455
2471 2456 # Find things also in current directory. This is needed to mimic the
2472 2457 # behavior of running a script from the system command line, where
2473 2458 # Python inserts the script's directory into sys.path
2474 2459 dname = os.path.dirname(fname)
2475 2460
2476 2461 with prepended_to_syspath(dname), self.builtin_trap:
2477 2462 try:
2478 2463 glob, loc = (where + (None, ))[:2]
2479 2464 py3compat.execfile(
2480 2465 fname, glob, loc,
2481 2466 self.compile if kw['shell_futures'] else None)
2482 2467 except SystemExit as status:
2483 2468 # If the call was made with 0 or None exit status (sys.exit(0)
2484 2469 # or sys.exit() ), don't bother showing a traceback, as both of
2485 2470 # these are considered normal by the OS:
2486 2471 # > python -c'import sys;sys.exit(0)'; echo $?
2487 2472 # 0
2488 2473 # > python -c'import sys;sys.exit()'; echo $?
2489 2474 # 0
2490 2475 # For other exit status, we show the exception unless
2491 2476 # explicitly silenced, but only in short form.
2492 2477 if status.code:
2493 2478 if kw['raise_exceptions']:
2494 2479 raise
2495 2480 if not kw['exit_ignore']:
2496 2481 self.showtraceback(exception_only=True)
2497 2482 except:
2498 2483 if kw['raise_exceptions']:
2499 2484 raise
2500 2485 # tb offset is 2 because we wrap execfile
2501 2486 self.showtraceback(tb_offset=2)
2502 2487
2503 2488 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2504 2489 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2505 2490
2506 2491 Parameters
2507 2492 ----------
2508 2493 fname : str
2509 2494 The name of the file to execute. The filename must have a
2510 2495 .ipy or .ipynb extension.
2511 2496 shell_futures : bool (False)
2512 2497 If True, the code will share future statements with the interactive
2513 2498 shell. It will both be affected by previous __future__ imports, and
2514 2499 any __future__ imports in the code will affect the shell. If False,
2515 2500 __future__ imports are not shared in either direction.
2516 2501 raise_exceptions : bool (False)
2517 2502 If True raise exceptions everywhere. Meant for testing.
2518 2503 """
2519 2504 fname = os.path.abspath(os.path.expanduser(fname))
2520 2505
2521 2506 # Make sure we can open the file
2522 2507 try:
2523 2508 with open(fname):
2524 2509 pass
2525 2510 except:
2526 2511 warn('Could not open file <%s> for safe execution.' % fname)
2527 2512 return
2528 2513
2529 2514 # Find things also in current directory. This is needed to mimic the
2530 2515 # behavior of running a script from the system command line, where
2531 2516 # Python inserts the script's directory into sys.path
2532 2517 dname = os.path.dirname(fname)
2533 2518
2534 2519 def get_cells():
2535 2520 """generator for sequence of code blocks to run"""
2536 2521 if fname.endswith('.ipynb'):
2537 2522 from nbformat import read
2538 2523 with io_open(fname) as f:
2539 2524 nb = read(f, as_version=4)
2540 2525 if not nb.cells:
2541 2526 return
2542 2527 for cell in nb.cells:
2543 2528 if cell.cell_type == 'code':
2544 2529 yield cell.source
2545 2530 else:
2546 2531 with open(fname) as f:
2547 2532 yield f.read()
2548 2533
2549 2534 with prepended_to_syspath(dname):
2550 2535 try:
2551 2536 for cell in get_cells():
2552 2537 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2553 2538 if raise_exceptions:
2554 2539 result.raise_error()
2555 2540 elif not result.success:
2556 2541 break
2557 2542 except:
2558 2543 if raise_exceptions:
2559 2544 raise
2560 2545 self.showtraceback()
2561 2546 warn('Unknown failure executing file: <%s>' % fname)
2562 2547
2563 2548 def safe_run_module(self, mod_name, where):
2564 2549 """A safe version of runpy.run_module().
2565 2550
2566 2551 This version will never throw an exception, but instead print
2567 2552 helpful error messages to the screen.
2568 2553
2569 2554 `SystemExit` exceptions with status code 0 or None are ignored.
2570 2555
2571 2556 Parameters
2572 2557 ----------
2573 2558 mod_name : string
2574 2559 The name of the module to be executed.
2575 2560 where : dict
2576 2561 The globals namespace.
2577 2562 """
2578 2563 try:
2579 2564 try:
2580 2565 where.update(
2581 2566 runpy.run_module(str(mod_name), run_name="__main__",
2582 2567 alter_sys=True)
2583 2568 )
2584 2569 except SystemExit as status:
2585 2570 if status.code:
2586 2571 raise
2587 2572 except:
2588 2573 self.showtraceback()
2589 2574 warn('Unknown failure executing module: <%s>' % mod_name)
2590 2575
2591 2576 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2592 2577 """Run a complete IPython cell.
2593 2578
2594 2579 Parameters
2595 2580 ----------
2596 2581 raw_cell : str
2597 2582 The code (including IPython code such as %magic functions) to run.
2598 2583 store_history : bool
2599 2584 If True, the raw and translated cell will be stored in IPython's
2600 2585 history. For user code calling back into IPython's machinery, this
2601 2586 should be set to False.
2602 2587 silent : bool
2603 2588 If True, avoid side-effects, such as implicit displayhooks and
2604 2589 and logging. silent=True forces store_history=False.
2605 2590 shell_futures : bool
2606 2591 If True, the code will share future statements with the interactive
2607 2592 shell. It will both be affected by previous __future__ imports, and
2608 2593 any __future__ imports in the code will affect the shell. If False,
2609 2594 __future__ imports are not shared in either direction.
2610 2595
2611 2596 Returns
2612 2597 -------
2613 2598 result : :class:`ExecutionResult`
2614 2599 """
2615 2600 result = ExecutionResult()
2616 2601
2617 2602 if (not raw_cell) or raw_cell.isspace():
2618 2603 self.last_execution_succeeded = True
2619 2604 return result
2620 2605
2621 2606 if silent:
2622 2607 store_history = False
2623 2608
2624 2609 if store_history:
2625 2610 result.execution_count = self.execution_count
2626 2611
2627 2612 def error_before_exec(value):
2628 2613 result.error_before_exec = value
2629 2614 self.last_execution_succeeded = False
2630 2615 return result
2631 2616
2632 2617 self.events.trigger('pre_execute')
2633 2618 if not silent:
2634 2619 self.events.trigger('pre_run_cell')
2635 2620
2636 2621 # If any of our input transformation (input_transformer_manager or
2637 2622 # prefilter_manager) raises an exception, we store it in this variable
2638 2623 # so that we can display the error after logging the input and storing
2639 2624 # it in the history.
2640 2625 preprocessing_exc_tuple = None
2641 2626 try:
2642 2627 # Static input transformations
2643 2628 cell = self.input_transformer_manager.transform_cell(raw_cell)
2644 2629 except SyntaxError:
2645 2630 preprocessing_exc_tuple = sys.exc_info()
2646 2631 cell = raw_cell # cell has to exist so it can be stored/logged
2647 2632 else:
2648 2633 if len(cell.splitlines()) == 1:
2649 2634 # Dynamic transformations - only applied for single line commands
2650 2635 with self.builtin_trap:
2651 2636 try:
2652 2637 # use prefilter_lines to handle trailing newlines
2653 2638 # restore trailing newline for ast.parse
2654 2639 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2655 2640 except Exception:
2656 2641 # don't allow prefilter errors to crash IPython
2657 2642 preprocessing_exc_tuple = sys.exc_info()
2658 2643
2659 2644 # Store raw and processed history
2660 2645 if store_history:
2661 2646 self.history_manager.store_inputs(self.execution_count,
2662 2647 cell, raw_cell)
2663 2648 if not silent:
2664 2649 self.logger.log(cell, raw_cell)
2665 2650
2666 2651 # Display the exception if input processing failed.
2667 2652 if preprocessing_exc_tuple is not None:
2668 2653 self.showtraceback(preprocessing_exc_tuple)
2669 2654 if store_history:
2670 2655 self.execution_count += 1
2671 2656 return error_before_exec(preprocessing_exc_tuple[2])
2672 2657
2673 2658 # Our own compiler remembers the __future__ environment. If we want to
2674 2659 # run code with a separate __future__ environment, use the default
2675 2660 # compiler
2676 2661 compiler = self.compile if shell_futures else CachingCompiler()
2677 2662
2678 2663 with self.builtin_trap:
2679 2664 cell_name = self.compile.cache(cell, self.execution_count)
2680 2665
2681 2666 with self.display_trap:
2682 2667 # Compile to bytecode
2683 2668 try:
2684 2669 code_ast = compiler.ast_parse(cell, filename=cell_name)
2685 2670 except self.custom_exceptions as e:
2686 2671 etype, value, tb = sys.exc_info()
2687 2672 self.CustomTB(etype, value, tb)
2688 2673 return error_before_exec(e)
2689 2674 except IndentationError as e:
2690 2675 self.showindentationerror()
2691 2676 if store_history:
2692 2677 self.execution_count += 1
2693 2678 return error_before_exec(e)
2694 2679 except (OverflowError, SyntaxError, ValueError, TypeError,
2695 2680 MemoryError) as e:
2696 2681 self.showsyntaxerror()
2697 2682 if store_history:
2698 2683 self.execution_count += 1
2699 2684 return error_before_exec(e)
2700 2685
2701 2686 # Apply AST transformations
2702 2687 try:
2703 2688 code_ast = self.transform_ast(code_ast)
2704 2689 except InputRejected as e:
2705 2690 self.showtraceback()
2706 2691 if store_history:
2707 2692 self.execution_count += 1
2708 2693 return error_before_exec(e)
2709 2694
2710 2695 # Give the displayhook a reference to our ExecutionResult so it
2711 2696 # can fill in the output value.
2712 2697 self.displayhook.exec_result = result
2713 2698
2714 2699 # Execute the user code
2715 2700 interactivity = "none" if silent else self.ast_node_interactivity
2716 2701 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2717 2702 interactivity=interactivity, compiler=compiler, result=result)
2718 2703
2719 2704 self.last_execution_succeeded = not has_raised
2720 2705
2721 2706 # Reset this so later displayed values do not modify the
2722 2707 # ExecutionResult
2723 2708 self.displayhook.exec_result = None
2724 2709
2725 2710 self.events.trigger('post_execute')
2726 2711 if not silent:
2727 2712 self.events.trigger('post_run_cell')
2728 2713
2729 2714 if store_history:
2730 2715 # Write output to the database. Does nothing unless
2731 2716 # history output logging is enabled.
2732 2717 self.history_manager.store_output(self.execution_count)
2733 2718 # Each cell is a *single* input, regardless of how many lines it has
2734 2719 self.execution_count += 1
2735 2720
2736 2721 return result
2737 2722
2738 2723 def transform_ast(self, node):
2739 2724 """Apply the AST transformations from self.ast_transformers
2740 2725
2741 2726 Parameters
2742 2727 ----------
2743 2728 node : ast.Node
2744 2729 The root node to be transformed. Typically called with the ast.Module
2745 2730 produced by parsing user input.
2746 2731
2747 2732 Returns
2748 2733 -------
2749 2734 An ast.Node corresponding to the node it was called with. Note that it
2750 2735 may also modify the passed object, so don't rely on references to the
2751 2736 original AST.
2752 2737 """
2753 2738 for transformer in self.ast_transformers:
2754 2739 try:
2755 2740 node = transformer.visit(node)
2756 2741 except InputRejected:
2757 2742 # User-supplied AST transformers can reject an input by raising
2758 2743 # an InputRejected. Short-circuit in this case so that we
2759 2744 # don't unregister the transform.
2760 2745 raise
2761 2746 except Exception:
2762 2747 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2763 2748 self.ast_transformers.remove(transformer)
2764 2749
2765 2750 if self.ast_transformers:
2766 2751 ast.fix_missing_locations(node)
2767 2752 return node
2768 2753
2769 2754
2770 2755 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2771 2756 compiler=compile, result=None):
2772 2757 """Run a sequence of AST nodes. The execution mode depends on the
2773 2758 interactivity parameter.
2774 2759
2775 2760 Parameters
2776 2761 ----------
2777 2762 nodelist : list
2778 2763 A sequence of AST nodes to run.
2779 2764 cell_name : str
2780 2765 Will be passed to the compiler as the filename of the cell. Typically
2781 2766 the value returned by ip.compile.cache(cell).
2782 2767 interactivity : str
2783 2768 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2784 2769 run interactively (displaying output from expressions). 'last_expr'
2785 2770 will run the last node interactively only if it is an expression (i.e.
2786 2771 expressions in loops or other blocks are not displayed. Other values
2787 2772 for this parameter will raise a ValueError.
2788 2773 compiler : callable
2789 2774 A function with the same interface as the built-in compile(), to turn
2790 2775 the AST nodes into code objects. Default is the built-in compile().
2791 2776 result : ExecutionResult, optional
2792 2777 An object to store exceptions that occur during execution.
2793 2778
2794 2779 Returns
2795 2780 -------
2796 2781 True if an exception occurred while running code, False if it finished
2797 2782 running.
2798 2783 """
2799 2784 if not nodelist:
2800 2785 return
2801 2786
2802 2787 if interactivity == 'last_expr':
2803 2788 if isinstance(nodelist[-1], ast.Expr):
2804 2789 interactivity = "last"
2805 2790 else:
2806 2791 interactivity = "none"
2807 2792
2808 2793 if interactivity == 'none':
2809 2794 to_run_exec, to_run_interactive = nodelist, []
2810 2795 elif interactivity == 'last':
2811 2796 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2812 2797 elif interactivity == 'all':
2813 2798 to_run_exec, to_run_interactive = [], nodelist
2814 2799 else:
2815 2800 raise ValueError("Interactivity was %r" % interactivity)
2816 2801
2817 2802 try:
2818 2803 for i, node in enumerate(to_run_exec):
2819 2804 mod = ast.Module([node])
2820 2805 code = compiler(mod, cell_name, "exec")
2821 2806 if self.run_code(code, result):
2822 2807 return True
2823 2808
2824 2809 for i, node in enumerate(to_run_interactive):
2825 2810 mod = ast.Interactive([node])
2826 2811 code = compiler(mod, cell_name, "single")
2827 2812 if self.run_code(code, result):
2828 2813 return True
2829 2814
2830 2815 # Flush softspace
2831 2816 if softspace(sys.stdout, 0):
2832 2817 print()
2833 2818
2834 2819 except:
2835 2820 # It's possible to have exceptions raised here, typically by
2836 2821 # compilation of odd code (such as a naked 'return' outside a
2837 2822 # function) that did parse but isn't valid. Typically the exception
2838 2823 # is a SyntaxError, but it's safest just to catch anything and show
2839 2824 # the user a traceback.
2840 2825
2841 2826 # We do only one try/except outside the loop to minimize the impact
2842 2827 # on runtime, and also because if any node in the node list is
2843 2828 # broken, we should stop execution completely.
2844 2829 if result:
2845 2830 result.error_before_exec = sys.exc_info()[1]
2846 2831 self.showtraceback()
2847 2832 return True
2848 2833
2849 2834 return False
2850 2835
2851 2836 def run_code(self, code_obj, result=None):
2852 2837 """Execute a code object.
2853 2838
2854 2839 When an exception occurs, self.showtraceback() is called to display a
2855 2840 traceback.
2856 2841
2857 2842 Parameters
2858 2843 ----------
2859 2844 code_obj : code object
2860 2845 A compiled code object, to be executed
2861 2846 result : ExecutionResult, optional
2862 2847 An object to store exceptions that occur during execution.
2863 2848
2864 2849 Returns
2865 2850 -------
2866 2851 False : successful execution.
2867 2852 True : an error occurred.
2868 2853 """
2869 2854 # Set our own excepthook in case the user code tries to call it
2870 2855 # directly, so that the IPython crash handler doesn't get triggered
2871 2856 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2872 2857
2873 2858 # we save the original sys.excepthook in the instance, in case config
2874 2859 # code (such as magics) needs access to it.
2875 2860 self.sys_excepthook = old_excepthook
2876 2861 outflag = 1 # happens in more places, so it's easier as default
2877 2862 try:
2878 2863 try:
2879 2864 self.hooks.pre_run_code_hook()
2880 2865 #rprint('Running code', repr(code_obj)) # dbg
2881 2866 exec(code_obj, self.user_global_ns, self.user_ns)
2882 2867 finally:
2883 2868 # Reset our crash handler in place
2884 2869 sys.excepthook = old_excepthook
2885 2870 except SystemExit as e:
2886 2871 if result is not None:
2887 2872 result.error_in_exec = e
2888 2873 self.showtraceback(exception_only=True)
2889 2874 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2890 2875 except self.custom_exceptions:
2891 2876 etype, value, tb = sys.exc_info()
2892 2877 if result is not None:
2893 2878 result.error_in_exec = value
2894 2879 self.CustomTB(etype, value, tb)
2895 2880 except:
2896 2881 if result is not None:
2897 2882 result.error_in_exec = sys.exc_info()[1]
2898 2883 self.showtraceback()
2899 2884 else:
2900 2885 outflag = 0
2901 2886 return outflag
2902 2887
2903 2888 # For backwards compatibility
2904 2889 runcode = run_code
2905 2890
2906 2891 #-------------------------------------------------------------------------
2907 2892 # Things related to GUI support and pylab
2908 2893 #-------------------------------------------------------------------------
2909 2894
2910 2895 def enable_gui(self, gui=None):
2911 2896 raise NotImplementedError('Implement enable_gui in a subclass')
2912 2897
2913 2898 def enable_matplotlib(self, gui=None):
2914 2899 """Enable interactive matplotlib and inline figure support.
2915 2900
2916 2901 This takes the following steps:
2917 2902
2918 2903 1. select the appropriate eventloop and matplotlib backend
2919 2904 2. set up matplotlib for interactive use with that backend
2920 2905 3. configure formatters for inline figure display
2921 2906 4. enable the selected gui eventloop
2922 2907
2923 2908 Parameters
2924 2909 ----------
2925 2910 gui : optional, string
2926 2911 If given, dictates the choice of matplotlib GUI backend to use
2927 2912 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2928 2913 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2929 2914 matplotlib (as dictated by the matplotlib build-time options plus the
2930 2915 user's matplotlibrc configuration file). Note that not all backends
2931 2916 make sense in all contexts, for example a terminal ipython can't
2932 2917 display figures inline.
2933 2918 """
2934 2919 from IPython.core import pylabtools as pt
2935 2920 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2936 2921
2937 2922 if gui != 'inline':
2938 2923 # If we have our first gui selection, store it
2939 2924 if self.pylab_gui_select is None:
2940 2925 self.pylab_gui_select = gui
2941 2926 # Otherwise if they are different
2942 2927 elif gui != self.pylab_gui_select:
2943 2928 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2944 2929 ' Using %s instead.' % (gui, self.pylab_gui_select))
2945 2930 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2946 2931
2947 2932 pt.activate_matplotlib(backend)
2948 2933 pt.configure_inline_support(self, backend)
2949 2934
2950 2935 # Now we must activate the gui pylab wants to use, and fix %run to take
2951 2936 # plot updates into account
2952 2937 self.enable_gui(gui)
2953 2938 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2954 2939 pt.mpl_runner(self.safe_execfile)
2955 2940
2956 2941 return gui, backend
2957 2942
2958 2943 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2959 2944 """Activate pylab support at runtime.
2960 2945
2961 2946 This turns on support for matplotlib, preloads into the interactive
2962 2947 namespace all of numpy and pylab, and configures IPython to correctly
2963 2948 interact with the GUI event loop. The GUI backend to be used can be
2964 2949 optionally selected with the optional ``gui`` argument.
2965 2950
2966 2951 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2967 2952
2968 2953 Parameters
2969 2954 ----------
2970 2955 gui : optional, string
2971 2956 If given, dictates the choice of matplotlib GUI backend to use
2972 2957 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2973 2958 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2974 2959 matplotlib (as dictated by the matplotlib build-time options plus the
2975 2960 user's matplotlibrc configuration file). Note that not all backends
2976 2961 make sense in all contexts, for example a terminal ipython can't
2977 2962 display figures inline.
2978 2963 import_all : optional, bool, default: True
2979 2964 Whether to do `from numpy import *` and `from pylab import *`
2980 2965 in addition to module imports.
2981 2966 welcome_message : deprecated
2982 2967 This argument is ignored, no welcome message will be displayed.
2983 2968 """
2984 2969 from IPython.core.pylabtools import import_pylab
2985 2970
2986 2971 gui, backend = self.enable_matplotlib(gui)
2987 2972
2988 2973 # We want to prevent the loading of pylab to pollute the user's
2989 2974 # namespace as shown by the %who* magics, so we execute the activation
2990 2975 # code in an empty namespace, and we update *both* user_ns and
2991 2976 # user_ns_hidden with this information.
2992 2977 ns = {}
2993 2978 import_pylab(ns, import_all)
2994 2979 # warn about clobbered names
2995 2980 ignored = {"__builtins__"}
2996 2981 both = set(ns).intersection(self.user_ns).difference(ignored)
2997 2982 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2998 2983 self.user_ns.update(ns)
2999 2984 self.user_ns_hidden.update(ns)
3000 2985 return gui, backend, clobbered
3001 2986
3002 2987 #-------------------------------------------------------------------------
3003 2988 # Utilities
3004 2989 #-------------------------------------------------------------------------
3005 2990
3006 2991 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3007 2992 """Expand python variables in a string.
3008 2993
3009 2994 The depth argument indicates how many frames above the caller should
3010 2995 be walked to look for the local namespace where to expand variables.
3011 2996
3012 2997 The global namespace for expansion is always the user's interactive
3013 2998 namespace.
3014 2999 """
3015 3000 ns = self.user_ns.copy()
3016 3001 try:
3017 3002 frame = sys._getframe(depth+1)
3018 3003 except ValueError:
3019 3004 # This is thrown if there aren't that many frames on the stack,
3020 3005 # e.g. if a script called run_line_magic() directly.
3021 3006 pass
3022 3007 else:
3023 3008 ns.update(frame.f_locals)
3024 3009
3025 3010 try:
3026 3011 # We have to use .vformat() here, because 'self' is a valid and common
3027 3012 # name, and expanding **ns for .format() would make it collide with
3028 3013 # the 'self' argument of the method.
3029 3014 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3030 3015 except Exception:
3031 3016 # if formatter couldn't format, just let it go untransformed
3032 3017 pass
3033 3018 return cmd
3034 3019
3035 3020 def mktempfile(self, data=None, prefix='ipython_edit_'):
3036 3021 """Make a new tempfile and return its filename.
3037 3022
3038 3023 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3039 3024 but it registers the created filename internally so ipython cleans it up
3040 3025 at exit time.
3041 3026
3042 3027 Optional inputs:
3043 3028
3044 3029 - data(None): if data is given, it gets written out to the temp file
3045 3030 immediately, and the file is closed again."""
3046 3031
3047 3032 dirname = tempfile.mkdtemp(prefix=prefix)
3048 3033 self.tempdirs.append(dirname)
3049 3034
3050 3035 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3051 3036 os.close(handle) # On Windows, there can only be one open handle on a file
3052 3037 self.tempfiles.append(filename)
3053 3038
3054 3039 if data:
3055 3040 tmp_file = open(filename,'w')
3056 3041 tmp_file.write(data)
3057 3042 tmp_file.close()
3058 3043 return filename
3059 3044
3060 3045 @undoc
3061 3046 def write(self,data):
3062 3047 """DEPRECATED: Write a string to the default output"""
3063 3048 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3064 3049 DeprecationWarning, stacklevel=2)
3065 3050 sys.stdout.write(data)
3066 3051
3067 3052 @undoc
3068 3053 def write_err(self,data):
3069 3054 """DEPRECATED: Write a string to the default error output"""
3070 3055 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3071 3056 DeprecationWarning, stacklevel=2)
3072 3057 sys.stderr.write(data)
3073 3058
3074 3059 def ask_yes_no(self, prompt, default=None, interrupt=None):
3075 3060 if self.quiet:
3076 3061 return True
3077 3062 return ask_yes_no(prompt,default,interrupt)
3078 3063
3079 3064 def show_usage(self):
3080 3065 """Show a usage message"""
3081 3066 page.page(IPython.core.usage.interactive_usage)
3082 3067
3083 3068 def extract_input_lines(self, range_str, raw=False):
3084 3069 """Return as a string a set of input history slices.
3085 3070
3086 3071 Parameters
3087 3072 ----------
3088 3073 range_str : string
3089 3074 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3090 3075 since this function is for use by magic functions which get their
3091 3076 arguments as strings. The number before the / is the session
3092 3077 number: ~n goes n back from the current session.
3093 3078
3094 3079 raw : bool, optional
3095 3080 By default, the processed input is used. If this is true, the raw
3096 3081 input history is used instead.
3097 3082
3098 3083 Notes
3099 3084 -----
3100 3085
3101 3086 Slices can be described with two notations:
3102 3087
3103 3088 * ``N:M`` -> standard python form, means including items N...(M-1).
3104 3089 * ``N-M`` -> include items N..M (closed endpoint).
3105 3090 """
3106 3091 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3107 3092 return "\n".join(x for _, _, x in lines)
3108 3093
3109 3094 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3110 3095 """Get a code string from history, file, url, or a string or macro.
3111 3096
3112 3097 This is mainly used by magic functions.
3113 3098
3114 3099 Parameters
3115 3100 ----------
3116 3101
3117 3102 target : str
3118 3103
3119 3104 A string specifying code to retrieve. This will be tried respectively
3120 3105 as: ranges of input history (see %history for syntax), url,
3121 3106 corresponding .py file, filename, or an expression evaluating to a
3122 3107 string or Macro in the user namespace.
3123 3108
3124 3109 raw : bool
3125 3110 If true (default), retrieve raw history. Has no effect on the other
3126 3111 retrieval mechanisms.
3127 3112
3128 3113 py_only : bool (default False)
3129 3114 Only try to fetch python code, do not try alternative methods to decode file
3130 3115 if unicode fails.
3131 3116
3132 3117 Returns
3133 3118 -------
3134 3119 A string of code.
3135 3120
3136 3121 ValueError is raised if nothing is found, and TypeError if it evaluates
3137 3122 to an object of another type. In each case, .args[0] is a printable
3138 3123 message.
3139 3124 """
3140 3125 code = self.extract_input_lines(target, raw=raw) # Grab history
3141 3126 if code:
3142 3127 return code
3143 3128 try:
3144 3129 if target.startswith(('http://', 'https://')):
3145 3130 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3146 3131 except UnicodeDecodeError:
3147 3132 if not py_only :
3148 3133 # Deferred import
3149 3134 try:
3150 3135 from urllib.request import urlopen # Py3
3151 3136 except ImportError:
3152 3137 from urllib import urlopen
3153 3138 response = urlopen(target)
3154 3139 return response.read().decode('latin1')
3155 3140 raise ValueError(("'%s' seem to be unreadable.") % target)
3156 3141
3157 3142 potential_target = [target]
3158 3143 try :
3159 3144 potential_target.insert(0,get_py_filename(target))
3160 3145 except IOError:
3161 3146 pass
3162 3147
3163 3148 for tgt in potential_target :
3164 3149 if os.path.isfile(tgt): # Read file
3165 3150 try :
3166 3151 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3167 3152 except UnicodeDecodeError :
3168 3153 if not py_only :
3169 3154 with io_open(tgt,'r', encoding='latin1') as f :
3170 3155 return f.read()
3171 3156 raise ValueError(("'%s' seem to be unreadable.") % target)
3172 3157 elif os.path.isdir(os.path.expanduser(tgt)):
3173 3158 raise ValueError("'%s' is a directory, not a regular file." % target)
3174 3159
3175 3160 if search_ns:
3176 3161 # Inspect namespace to load object source
3177 3162 object_info = self.object_inspect(target, detail_level=1)
3178 3163 if object_info['found'] and object_info['source']:
3179 3164 return object_info['source']
3180 3165
3181 3166 try: # User namespace
3182 3167 codeobj = eval(target, self.user_ns)
3183 3168 except Exception:
3184 3169 raise ValueError(("'%s' was not found in history, as a file, url, "
3185 3170 "nor in the user namespace.") % target)
3186 3171
3187 3172 if isinstance(codeobj, string_types):
3188 3173 return codeobj
3189 3174 elif isinstance(codeobj, Macro):
3190 3175 return codeobj.value
3191 3176
3192 3177 raise TypeError("%s is neither a string nor a macro." % target,
3193 3178 codeobj)
3194 3179
3195 3180 #-------------------------------------------------------------------------
3196 3181 # Things related to IPython exiting
3197 3182 #-------------------------------------------------------------------------
3198 3183 def atexit_operations(self):
3199 3184 """This will be executed at the time of exit.
3200 3185
3201 3186 Cleanup operations and saving of persistent data that is done
3202 3187 unconditionally by IPython should be performed here.
3203 3188
3204 3189 For things that may depend on startup flags or platform specifics (such
3205 3190 as having readline or not), register a separate atexit function in the
3206 3191 code that has the appropriate information, rather than trying to
3207 3192 clutter
3208 3193 """
3209 3194 # Close the history session (this stores the end time and line count)
3210 3195 # this must be *before* the tempfile cleanup, in case of temporary
3211 3196 # history db
3212 3197 self.history_manager.end_session()
3213 3198
3214 3199 # Cleanup all tempfiles and folders left around
3215 3200 for tfile in self.tempfiles:
3216 3201 try:
3217 3202 os.unlink(tfile)
3218 3203 except OSError:
3219 3204 pass
3220 3205
3221 3206 for tdir in self.tempdirs:
3222 3207 try:
3223 3208 os.rmdir(tdir)
3224 3209 except OSError:
3225 3210 pass
3226 3211
3227 3212 # Clear all user namespaces to release all references cleanly.
3228 3213 self.reset(new_session=False)
3229 3214
3230 3215 # Run user hooks
3231 3216 self.hooks.shutdown_hook()
3232 3217
3233 3218 def cleanup(self):
3234 3219 self.restore_sys_module_state()
3235 3220
3236 3221
3237 3222 # Overridden in terminal subclass to change prompts
3238 3223 def switch_doctest_mode(self, mode):
3239 3224 pass
3240 3225
3241 3226
3242 3227 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3243 3228 """An abstract base class for InteractiveShell."""
3244 3229
3245 3230 InteractiveShellABC.register(InteractiveShell)
@@ -1,361 +1,343 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Provides a reload() function that acts recursively.
4 4
5 5 Python's normal :func:`python:reload` function only reloads the module that it's
6 6 passed. The :func:`reload` function in this module also reloads everything
7 7 imported from that module, which is useful when you're changing files deep
8 8 inside a package.
9 9
10 10 To use this as your default reload function, type this for Python 2::
11 11
12 12 import __builtin__
13 13 from IPython.lib import deepreload
14 14 __builtin__.reload = deepreload.reload
15 15
16 16 Or this for Python 3::
17 17
18 18 import builtins
19 19 from IPython.lib import deepreload
20 20 builtins.reload = deepreload.reload
21 21
22 22 A reference to the original :func:`python:reload` is stored in this module as
23 23 :data:`original_reload`, so you can restore it later.
24 24
25 25 This code is almost entirely based on knee.py, which is a Python
26 26 re-implementation of hierarchical module import.
27 27 """
28 28 from __future__ import print_function
29 29 #*****************************************************************************
30 30 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
31 31 #
32 32 # Distributed under the terms of the BSD License. The full license is in
33 33 # the file COPYING, distributed as part of this software.
34 34 #*****************************************************************************
35 35
36 36 from contextlib import contextmanager
37 37 import imp
38 38 import sys
39 39
40 40 from types import ModuleType
41 41 from warnings import warn
42 42
43 43 from IPython.utils.py3compat import builtin_mod, builtin_mod_name
44 44
45 45 original_import = builtin_mod.__import__
46 46
47 47 @contextmanager
48 48 def replace_import_hook(new_import):
49 49 saved_import = builtin_mod.__import__
50 50 builtin_mod.__import__ = new_import
51 51 try:
52 52 yield
53 53 finally:
54 54 builtin_mod.__import__ = saved_import
55 55
56 56 def get_parent(globals, level):
57 57 """
58 58 parent, name = get_parent(globals, level)
59 59
60 60 Return the package that an import is being performed in. If globals comes
61 61 from the module foo.bar.bat (not itself a package), this returns the
62 62 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
63 63 the package's entry in sys.modules is returned.
64 64
65 65 If globals doesn't come from a package or a module in a package, or a
66 66 corresponding entry is not found in sys.modules, None is returned.
67 67 """
68 68 orig_level = level
69 69
70 70 if not level or not isinstance(globals, dict):
71 71 return None, ''
72 72
73 73 pkgname = globals.get('__package__', None)
74 74
75 75 if pkgname is not None:
76 76 # __package__ is set, so use it
77 77 if not hasattr(pkgname, 'rindex'):
78 78 raise ValueError('__package__ set to non-string')
79 79 if len(pkgname) == 0:
80 80 if level > 0:
81 81 raise ValueError('Attempted relative import in non-package')
82 82 return None, ''
83 83 name = pkgname
84 84 else:
85 85 # __package__ not set, so figure it out and set it
86 86 if '__name__' not in globals:
87 87 return None, ''
88 88 modname = globals['__name__']
89 89
90 90 if '__path__' in globals:
91 91 # __path__ is set, so modname is already the package name
92 92 globals['__package__'] = name = modname
93 93 else:
94 94 # Normal module, so work out the package name if any
95 95 lastdot = modname.rfind('.')
96 96 if lastdot < 0 < level:
97 97 raise ValueError("Attempted relative import in non-package")
98 98 if lastdot < 0:
99 99 globals['__package__'] = None
100 100 return None, ''
101 101 globals['__package__'] = name = modname[:lastdot]
102 102
103 103 dot = len(name)
104 104 for x in range(level, 1, -1):
105 105 try:
106 106 dot = name.rindex('.', 0, dot)
107 107 except ValueError:
108 108 raise ValueError("attempted relative import beyond top-level "
109 109 "package")
110 110 name = name[:dot]
111 111
112 112 try:
113 113 parent = sys.modules[name]
114 114 except:
115 115 if orig_level < 1:
116 116 warn("Parent module '%.200s' not found while handling absolute "
117 117 "import" % name)
118 118 parent = None
119 119 else:
120 120 raise SystemError("Parent module '%.200s' not loaded, cannot "
121 121 "perform relative import" % name)
122 122
123 123 # We expect, but can't guarantee, if parent != None, that:
124 124 # - parent.__name__ == name
125 125 # - parent.__dict__ is globals
126 126 # If this is violated... Who cares?
127 127 return parent, name
128 128
129 129 def load_next(mod, altmod, name, buf):
130 130 """
131 131 mod, name, buf = load_next(mod, altmod, name, buf)
132 132
133 133 altmod is either None or same as mod
134 134 """
135 135
136 136 if len(name) == 0:
137 137 # completely empty module name should only happen in
138 138 # 'from . import' (or '__import__("")')
139 139 return mod, None, buf
140 140
141 141 dot = name.find('.')
142 142 if dot == 0:
143 143 raise ValueError('Empty module name')
144 144
145 145 if dot < 0:
146 146 subname = name
147 147 next = None
148 148 else:
149 149 subname = name[:dot]
150 150 next = name[dot+1:]
151 151
152 152 if buf != '':
153 153 buf += '.'
154 154 buf += subname
155 155
156 156 result = import_submodule(mod, subname, buf)
157 157 if result is None and mod != altmod:
158 158 result = import_submodule(altmod, subname, subname)
159 159 if result is not None:
160 160 buf = subname
161 161
162 162 if result is None:
163 163 raise ImportError("No module named %.200s" % name)
164 164
165 165 return result, next, buf
166 166
167 167 # Need to keep track of what we've already reloaded to prevent cyclic evil
168 168 found_now = {}
169 169
170 170 def import_submodule(mod, subname, fullname):
171 171 """m = import_submodule(mod, subname, fullname)"""
172 172 # Require:
173 173 # if mod == None: subname == fullname
174 174 # else: mod.__name__ + "." + subname == fullname
175 175
176 176 global found_now
177 177 if fullname in found_now and fullname in sys.modules:
178 178 m = sys.modules[fullname]
179 179 else:
180 180 print('Reloading', fullname)
181 181 found_now[fullname] = 1
182 182 oldm = sys.modules.get(fullname, None)
183 183
184 184 if mod is None:
185 185 path = None
186 186 elif hasattr(mod, '__path__'):
187 187 path = mod.__path__
188 188 else:
189 189 return None
190 190
191 191 try:
192 192 # This appears to be necessary on Python 3, because imp.find_module()
193 193 # tries to import standard libraries (like io) itself, and we don't
194 194 # want them to be processed by our deep_import_hook.
195 195 with replace_import_hook(original_import):
196 196 fp, filename, stuff = imp.find_module(subname, path)
197 197 except ImportError:
198 198 return None
199 199
200 200 try:
201 201 m = imp.load_module(fullname, fp, filename, stuff)
202 202 except:
203 203 # load_module probably removed name from modules because of
204 204 # the error. Put back the original module object.
205 205 if oldm:
206 206 sys.modules[fullname] = oldm
207 207 raise
208 208 finally:
209 209 if fp: fp.close()
210 210
211 211 add_submodule(mod, m, fullname, subname)
212 212
213 213 return m
214 214
215 215 def add_submodule(mod, submod, fullname, subname):
216 216 """mod.{subname} = submod"""
217 217 if mod is None:
218 218 return #Nothing to do here.
219 219
220 220 if submod is None:
221 221 submod = sys.modules[fullname]
222 222
223 223 setattr(mod, subname, submod)
224 224
225 225 return
226 226
227 227 def ensure_fromlist(mod, fromlist, buf, recursive):
228 228 """Handle 'from module import a, b, c' imports."""
229 229 if not hasattr(mod, '__path__'):
230 230 return
231 231 for item in fromlist:
232 232 if not hasattr(item, 'rindex'):
233 233 raise TypeError("Item in ``from list'' not a string")
234 234 if item == '*':
235 235 if recursive:
236 236 continue # avoid endless recursion
237 237 try:
238 238 all = mod.__all__
239 239 except AttributeError:
240 240 pass
241 241 else:
242 242 ret = ensure_fromlist(mod, all, buf, 1)
243 243 if not ret:
244 244 return 0
245 245 elif not hasattr(mod, item):
246 246 import_submodule(mod, item, buf + '.' + item)
247 247
248 248 def deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1):
249 249 """Replacement for __import__()"""
250 250 parent, buf = get_parent(globals, level)
251 251
252 252 head, name, buf = load_next(parent, None if level < 0 else parent, name, buf)
253 253
254 254 tail = head
255 255 while name:
256 256 tail, name, buf = load_next(tail, tail, name, buf)
257 257
258 258 # If tail is None, both get_parent and load_next found
259 259 # an empty module name: someone called __import__("") or
260 260 # doctored faulty bytecode
261 261 if tail is None:
262 262 raise ValueError('Empty module name')
263 263
264 264 if not fromlist:
265 265 return head
266 266
267 267 ensure_fromlist(tail, fromlist, buf, 0)
268 268 return tail
269 269
270 270 modules_reloading = {}
271 271
272 272 def deep_reload_hook(m):
273 273 """Replacement for reload()."""
274 274 if not isinstance(m, ModuleType):
275 275 raise TypeError("reload() argument must be module")
276 276
277 277 name = m.__name__
278 278
279 279 if name not in sys.modules:
280 280 raise ImportError("reload(): module %.200s not in sys.modules" % name)
281 281
282 282 global modules_reloading
283 283 try:
284 284 return modules_reloading[name]
285 285 except:
286 286 modules_reloading[name] = m
287 287
288 288 dot = name.rfind('.')
289 289 if dot < 0:
290 290 subname = name
291 291 path = None
292 292 else:
293 293 try:
294 294 parent = sys.modules[name[:dot]]
295 295 except KeyError:
296 296 modules_reloading.clear()
297 297 raise ImportError("reload(): parent %.200s not in sys.modules" % name[:dot])
298 298 subname = name[dot+1:]
299 299 path = getattr(parent, "__path__", None)
300 300
301 301 try:
302 302 # This appears to be necessary on Python 3, because imp.find_module()
303 303 # tries to import standard libraries (like io) itself, and we don't
304 304 # want them to be processed by our deep_import_hook.
305 305 with replace_import_hook(original_import):
306 306 fp, filename, stuff = imp.find_module(subname, path)
307 307 finally:
308 308 modules_reloading.clear()
309 309
310 310 try:
311 311 newm = imp.load_module(name, fp, filename, stuff)
312 312 except:
313 313 # load_module probably removed name from modules because of
314 314 # the error. Put back the original module object.
315 315 sys.modules[name] = m
316 316 raise
317 317 finally:
318 318 if fp: fp.close()
319 319
320 320 modules_reloading.clear()
321 321 return newm
322 322
323 323 # Save the original hooks
324 324 try:
325 325 original_reload = builtin_mod.reload
326 326 except AttributeError:
327 327 original_reload = imp.reload # Python 3
328 328
329 329 # Replacement for reload()
330 330 def reload(module, exclude=('sys', 'os.path', builtin_mod_name, '__main__')):
331 331 """Recursively reload all modules used in the given module. Optionally
332 332 takes a list of modules to exclude from reloading. The default exclude
333 333 list contains sys, __main__, and __builtin__, to prevent, e.g., resetting
334 334 display, exception, and io hooks.
335 335 """
336 336 global found_now
337 337 for i in exclude:
338 338 found_now[i] = 1
339 339 try:
340 340 with replace_import_hook(deep_import_hook):
341 341 return deep_reload_hook(module)
342 342 finally:
343 343 found_now = {}
344
345
346 def _dreload(module, **kwargs):
347 """
348 **deprecated**
349
350 import reload explicitly from `IPython.lib.deepreload` to use it
351
352 """
353 # this was marked as deprecated and for 5.0 removal, but
354 # IPython.core_builtin_trap have a Deprecation warning for 6.0, so cannot
355 # remove that now.
356 warn("""
357 injecting `dreload` in interactive namespace is deprecated since IPython 4.0.
358 Please import `reload` explicitly from `IPython.lib.deepreload`.
359 """, DeprecationWarning, stacklevel=2)
360 reload(module, **kwargs)
361
General Comments 0
You need to be logged in to leave comments. Login now