##// END OF EJS Templates
Stop monkeypatching `linecache`...
Ben Longbons -
Show More
@@ -1,196 +1,197 b''
1 1 """Compiler tools with improved interactive support.
2 2
3 3 Provides compilation machinery similar to codeop, but with caching support so
4 4 we can provide interactive tracebacks.
5 5
6 6 Authors
7 7 -------
8 8 * Robert Kern
9 9 * Fernando Perez
10 10 * Thomas Kluyver
11 11 """
12 12
13 13 # Note: though it might be more natural to name this module 'compiler', that
14 14 # name is in the stdlib and name collisions with the stdlib tend to produce
15 15 # weird problems (often with third-party tools).
16 16
17 17 #-----------------------------------------------------------------------------
18 18 # Copyright (C) 2010-2011 The IPython Development Team.
19 19 #
20 20 # Distributed under the terms of the BSD License.
21 21 #
22 22 # The full license is in the file COPYING.txt, distributed with this software.
23 23 #-----------------------------------------------------------------------------
24 24
25 25 #-----------------------------------------------------------------------------
26 26 # Imports
27 27 #-----------------------------------------------------------------------------
28 28
29 29 # Stdlib imports
30 30 import __future__
31 31 from ast import PyCF_ONLY_AST
32 32 import codeop
33 33 import functools
34 34 import hashlib
35 35 import linecache
36 36 import operator
37 37 import time
38 38 from contextlib import contextmanager
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Constants
42 42 #-----------------------------------------------------------------------------
43 43
44 44 # Roughly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h,
45 45 # this is used as a bitmask to extract future-related code flags.
46 46 PyCF_MASK = functools.reduce(operator.or_,
47 47 (getattr(__future__, fname).compiler_flag
48 48 for fname in __future__.all_feature_names))
49 49
50 50 #-----------------------------------------------------------------------------
51 51 # Local utilities
52 52 #-----------------------------------------------------------------------------
53 53
54 54 def code_name(code, number=0):
55 55 """ Compute a (probably) unique name for code for caching.
56 56
57 57 This now expects code to be unicode.
58 58 """
59 59 hash_digest = hashlib.sha1(code.encode("utf-8")).hexdigest()
60 60 # Include the number and 12 characters of the hash in the name. It's
61 61 # pretty much impossible that in a single session we'll have collisions
62 62 # even with truncated hashes, and the full one makes tracebacks too long
63 63 return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12])
64 64
65 65 #-----------------------------------------------------------------------------
66 66 # Classes and functions
67 67 #-----------------------------------------------------------------------------
68 68
69 69 class CachingCompiler(codeop.Compile):
70 70 """A compiler that caches code compiled from interactive statements.
71 71 """
72 72
73 73 def __init__(self):
74 74 codeop.Compile.__init__(self)
75 75
76 # This is ugly, but it must be done this way to allow multiple
77 # simultaneous ipython instances to coexist. Since Python itself
78 # directly accesses the data structures in the linecache module, and
79 # the cache therein is global, we must work with that data structure.
80 # We must hold a reference to the original checkcache routine and call
81 # that in our own check_cache() below, but the special IPython cache
82 # must also be shared by all IPython instances. If we were to hold
83 # separate caches (one in each CachingCompiler instance), any call made
84 # by Python itself to linecache.checkcache() would obliterate the
85 # cached data from the other IPython instances.
86 if not hasattr(linecache, '_ipython_cache'):
87 linecache._ipython_cache = {}
88 if not hasattr(linecache, '_checkcache_ori'):
89 linecache._checkcache_ori = linecache.checkcache
90 # Now, we must monkeypatch the linecache directly so that parts of the
91 # stdlib that call it outside our control go through our codepath
92 # (otherwise we'd lose our tracebacks).
93 linecache.checkcache = check_linecache_ipython
94
95 76 # Caching a dictionary { filename: execution_count } for nicely
96 77 # rendered tracebacks. The filename corresponds to the filename
97 78 # argument used for the builtins.compile function.
98 79 self._filename_map = {}
99 80
100 81 def ast_parse(self, source, filename='<unknown>', symbol='exec'):
101 82 """Parse code to an AST with the current compiler flags active.
102 83
103 84 Arguments are exactly the same as ast.parse (in the standard library),
104 85 and are passed to the built-in compile function."""
105 86 return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
106 87
107 88 def reset_compiler_flags(self):
108 89 """Reset compiler flags to default state."""
109 90 # This value is copied from codeop.Compile.__init__, so if that ever
110 91 # changes, it will need to be updated.
111 92 self.flags = codeop.PyCF_DONT_IMPLY_DEDENT
112 93
113 94 @property
114 95 def compiler_flags(self):
115 96 """Flags currently active in the compilation process.
116 97 """
117 98 return self.flags
118 99
119 100 def get_code_name(self, raw_code, transformed_code, number):
120 101 """Compute filename given the code, and the cell number.
121 102
122 103 Parameters
123 104 ----------
124 105 raw_code : str
125 106 The raw cell code.
126 107 transformed_code : str
127 108 The executable Python source code to cache and compile.
128 109 number : int
129 110 A number which forms part of the code's name. Used for the execution
130 111 counter.
131 112
132 113 Returns
133 114 -------
134 115 The computed filename.
135 116 """
136 117 return code_name(transformed_code, number)
137 118
138 119 def cache(self, transformed_code, number=0, raw_code=None):
139 120 """Make a name for a block of code, and cache the code.
140 121
141 122 Parameters
142 123 ----------
143 124 transformed_code : str
144 125 The executable Python source code to cache and compile.
145 126 number : int
146 127 A number which forms part of the code's name. Used for the execution
147 128 counter.
148 129 raw_code : str
149 130 The raw code before transformation, if None, set to `transformed_code`.
150 131
151 132 Returns
152 133 -------
153 134 The name of the cached code (as a string). Pass this as the filename
154 135 argument to compilation, so that tracebacks are correctly hooked up.
155 136 """
156 137 if raw_code is None:
157 138 raw_code = transformed_code
158 139
159 140 name = self.get_code_name(raw_code, transformed_code, number)
160 141
161 142 # Save the execution count
162 143 self._filename_map[name] = number
163 144
145 # Since Python 2.5, setting mtime to `None` means the lines will
146 # never be removed by `linecache.checkcache`. This means all the
147 # monkeypatching has *never* been necessary, since this code was
148 # only added in 2010, at which point IPython had already stopped
149 # supporting Python 2.4.
150 #
151 # Note that `linecache.clearcache` and `linecache.updatecache` may
152 # still remove our code from the cache, but those show explicit
153 # intent, and we should not try to interfere. Normally the former
154 # is never called except when out of memory, and the latter is only
155 # called for lines *not* in the cache.
164 156 entry = (
165 157 len(transformed_code),
166 time.time(),
158 None,
167 159 [line + "\n" for line in transformed_code.splitlines()],
168 160 name,
169 161 )
170 162 linecache.cache[name] = entry
171 linecache._ipython_cache[name] = entry
172 163 return name
173 164
174 165 @contextmanager
175 166 def extra_flags(self, flags):
176 167 ## bits that we'll set to 1
177 168 turn_on_bits = ~self.flags & flags
178 169
179 170
180 171 self.flags = self.flags | flags
181 172 try:
182 173 yield
183 174 finally:
184 175 # turn off only the bits we turned on so that something like
185 176 # __future__ that set flags stays.
186 177 self.flags &= ~turn_on_bits
187 178
188 179
189 180 def check_linecache_ipython(*args):
190 """Call linecache.checkcache() safely protecting our cached values.
181 """Deprecated since IPython 8.6. Call linecache.checkcache() directly.
182
183 It was already not necessary to call this function directly. If no
184 CachingCompiler had been created, this function would fail badly. If
185 an instance had been created, this function would've been monkeypatched
186 into place.
187
188 As of IPython 8.6, the monkeypatching has gone away entirely. But there
189 were still internal callers of this function, so maybe external callers
190 also existed?
191 191 """
192 # First call the original checkcache as intended
193 linecache._checkcache_ori(*args)
194 # Then, update back the cache with our data, so that tracebacks related
195 # to our compiled codes can be produced.
196 linecache.cache.update(linecache._ipython_cache)
192 import warnings
193 warnings.warn(
194 'Just call linecache.checkcache() directly.',
195 DeprecationWarning
196 )
197 linecache.checkcache()
@@ -1,3825 +1,3824 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
14 14 import abc
15 15 import ast
16 16 import atexit
17 17 import bdb
18 18 import builtins as builtin_mod
19 19 import functools
20 20 import inspect
21 21 import os
22 22 import re
23 23 import runpy
24 24 import subprocess
25 25 import sys
26 26 import tempfile
27 27 import traceback
28 28 import types
29 29 import warnings
30 30 from ast import stmt
31 31 from io import open as io_open
32 32 from logging import error
33 33 from pathlib import Path
34 34 from typing import Callable
35 35 from typing import List as ListType
36 36 from typing import Optional, Tuple
37 37 from warnings import warn
38 38
39 39 from pickleshare import PickleShareDB
40 40 from tempfile import TemporaryDirectory
41 41 from traitlets import (
42 42 Any,
43 43 Bool,
44 44 CaselessStrEnum,
45 45 Dict,
46 46 Enum,
47 47 Instance,
48 48 Integer,
49 49 List,
50 50 Type,
51 51 Unicode,
52 52 default,
53 53 observe,
54 54 validate,
55 55 )
56 56 from traitlets.config.configurable import SingletonConfigurable
57 57 from traitlets.utils.importstring import import_item
58 58
59 59 import IPython.core.hooks
60 60 from IPython.core import magic, oinspect, page, prefilter, ultratb
61 61 from IPython.core.alias import Alias, AliasManager
62 62 from IPython.core.autocall import ExitAutocall
63 63 from IPython.core.builtin_trap import BuiltinTrap
64 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
64 from IPython.core.compilerop import CachingCompiler
65 65 from IPython.core.debugger import InterruptiblePdb
66 66 from IPython.core.display_trap import DisplayTrap
67 67 from IPython.core.displayhook import DisplayHook
68 68 from IPython.core.displaypub import DisplayPublisher
69 69 from IPython.core.error import InputRejected, UsageError
70 70 from IPython.core.events import EventManager, available_events
71 71 from IPython.core.extensions import ExtensionManager
72 72 from IPython.core.formatters import DisplayFormatter
73 73 from IPython.core.history import HistoryManager
74 74 from IPython.core.inputtransformer2 import ESC_MAGIC, ESC_MAGIC2
75 75 from IPython.core.logger import Logger
76 76 from IPython.core.macro import Macro
77 77 from IPython.core.payload import PayloadManager
78 78 from IPython.core.prefilter import PrefilterManager
79 79 from IPython.core.profiledir import ProfileDir
80 80 from IPython.core.usage import default_banner
81 81 from IPython.display import display
82 82 from IPython.paths import get_ipython_dir
83 83 from IPython.testing.skipdoctest import skip_doctest
84 84 from IPython.utils import PyColorize, io, openpy, py3compat
85 85 from IPython.utils.decorators import undoc
86 86 from IPython.utils.io import ask_yes_no
87 87 from IPython.utils.ipstruct import Struct
88 88 from IPython.utils.path import ensure_dir_exists, get_home_dir, get_py_filename
89 89 from IPython.utils.process import getoutput, system
90 90 from IPython.utils.strdispatch import StrDispatch
91 91 from IPython.utils.syspathcontext import prepended_to_syspath
92 92 from IPython.utils.text import DollarFormatter, LSString, SList, format_screen
93 93
94 94 sphinxify: Optional[Callable]
95 95
96 96 try:
97 97 import docrepr.sphinxify as sphx
98 98
99 99 def sphinxify(oinfo):
100 100 wrapped_docstring = sphx.wrap_main_docstring(oinfo)
101 101
102 102 def sphinxify_docstring(docstring):
103 103 with TemporaryDirectory() as dirname:
104 104 return {
105 105 "text/html": sphx.sphinxify(wrapped_docstring, dirname),
106 106 "text/plain": docstring,
107 107 }
108 108
109 109 return sphinxify_docstring
110 110 except ImportError:
111 111 sphinxify = None
112 112
113 113
114 114 class ProvisionalWarning(DeprecationWarning):
115 115 """
116 116 Warning class for unstable features
117 117 """
118 118 pass
119 119
120 120 from ast import Module
121 121
122 122 _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign)
123 123 _single_targets_nodes = (ast.AugAssign, ast.AnnAssign)
124 124
125 125 #-----------------------------------------------------------------------------
126 126 # Await Helpers
127 127 #-----------------------------------------------------------------------------
128 128
129 129 # we still need to run things using the asyncio eventloop, but there is no
130 130 # async integration
131 131 from .async_helpers import (
132 132 _asyncio_runner,
133 133 _curio_runner,
134 134 _pseudo_sync_runner,
135 135 _should_be_async,
136 136 _trio_runner,
137 137 )
138 138
139 139 #-----------------------------------------------------------------------------
140 140 # Globals
141 141 #-----------------------------------------------------------------------------
142 142
143 143 # compiled regexps for autoindent management
144 144 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
145 145
146 146 #-----------------------------------------------------------------------------
147 147 # Utilities
148 148 #-----------------------------------------------------------------------------
149 149
150 150
151 151 def is_integer_string(s: str):
152 152 """
153 153 Variant of "str.isnumeric()" that allow negative values and other ints.
154 154 """
155 155 try:
156 156 int(s)
157 157 return True
158 158 except ValueError:
159 159 return False
160 160 raise ValueError("Unexpected error")
161 161
162 162
163 163 @undoc
164 164 def softspace(file, newvalue):
165 165 """Copied from code.py, to remove the dependency"""
166 166
167 167 oldvalue = 0
168 168 try:
169 169 oldvalue = file.softspace
170 170 except AttributeError:
171 171 pass
172 172 try:
173 173 file.softspace = newvalue
174 174 except (AttributeError, TypeError):
175 175 # "attribute-less object" or "read-only attributes"
176 176 pass
177 177 return oldvalue
178 178
179 179 @undoc
180 180 def no_op(*a, **kw):
181 181 pass
182 182
183 183
184 184 class SpaceInInput(Exception): pass
185 185
186 186
187 187 class SeparateUnicode(Unicode):
188 188 r"""A Unicode subclass to validate separate_in, separate_out, etc.
189 189
190 190 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
191 191 """
192 192
193 193 def validate(self, obj, value):
194 194 if value == '0': value = ''
195 195 value = value.replace('\\n','\n')
196 196 return super(SeparateUnicode, self).validate(obj, value)
197 197
198 198
199 199 @undoc
200 200 class DummyMod(object):
201 201 """A dummy module used for IPython's interactive module when
202 202 a namespace must be assigned to the module's __dict__."""
203 203 __spec__ = None
204 204
205 205
206 206 class ExecutionInfo(object):
207 207 """The arguments used for a call to :meth:`InteractiveShell.run_cell`
208 208
209 209 Stores information about what is going to happen.
210 210 """
211 211 raw_cell = None
212 212 store_history = False
213 213 silent = False
214 214 shell_futures = True
215 215 cell_id = None
216 216
217 217 def __init__(self, raw_cell, store_history, silent, shell_futures, cell_id):
218 218 self.raw_cell = raw_cell
219 219 self.store_history = store_history
220 220 self.silent = silent
221 221 self.shell_futures = shell_futures
222 222 self.cell_id = cell_id
223 223
224 224 def __repr__(self):
225 225 name = self.__class__.__qualname__
226 226 raw_cell = (
227 227 (self.raw_cell[:50] + "..") if len(self.raw_cell) > 50 else self.raw_cell
228 228 )
229 229 return (
230 230 '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s cell_id=%s>'
231 231 % (
232 232 name,
233 233 id(self),
234 234 raw_cell,
235 235 self.store_history,
236 236 self.silent,
237 237 self.shell_futures,
238 238 self.cell_id,
239 239 )
240 240 )
241 241
242 242
243 243 class ExecutionResult(object):
244 244 """The result of a call to :meth:`InteractiveShell.run_cell`
245 245
246 246 Stores information about what took place.
247 247 """
248 248 execution_count = None
249 249 error_before_exec = None
250 250 error_in_exec: Optional[BaseException] = None
251 251 info = None
252 252 result = None
253 253
254 254 def __init__(self, info):
255 255 self.info = info
256 256
257 257 @property
258 258 def success(self):
259 259 return (self.error_before_exec is None) and (self.error_in_exec is None)
260 260
261 261 def raise_error(self):
262 262 """Reraises error if `success` is `False`, otherwise does nothing"""
263 263 if self.error_before_exec is not None:
264 264 raise self.error_before_exec
265 265 if self.error_in_exec is not None:
266 266 raise self.error_in_exec
267 267
268 268 def __repr__(self):
269 269 name = self.__class__.__qualname__
270 270 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\
271 271 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result))
272 272
273 273
274 274 class InteractiveShell(SingletonConfigurable):
275 275 """An enhanced, interactive shell for Python."""
276 276
277 277 _instance = None
278 278
279 279 ast_transformers = List([], help=
280 280 """
281 281 A list of ast.NodeTransformer subclass instances, which will be applied
282 282 to user input before code is run.
283 283 """
284 284 ).tag(config=True)
285 285
286 286 autocall = Enum((0,1,2), default_value=0, help=
287 287 """
288 288 Make IPython automatically call any callable object even if you didn't
289 289 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
290 290 automatically. The value can be '0' to disable the feature, '1' for
291 291 'smart' autocall, where it is not applied if there are no more
292 292 arguments on the line, and '2' for 'full' autocall, where all callable
293 293 objects are automatically called (even if no arguments are present).
294 294 """
295 295 ).tag(config=True)
296 296
297 297 autoindent = Bool(True, help=
298 298 """
299 299 Autoindent IPython code entered interactively.
300 300 """
301 301 ).tag(config=True)
302 302
303 303 autoawait = Bool(True, help=
304 304 """
305 305 Automatically run await statement in the top level repl.
306 306 """
307 307 ).tag(config=True)
308 308
309 309 loop_runner_map ={
310 310 'asyncio':(_asyncio_runner, True),
311 311 'curio':(_curio_runner, True),
312 312 'trio':(_trio_runner, True),
313 313 'sync': (_pseudo_sync_runner, False)
314 314 }
315 315
316 316 loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner",
317 317 allow_none=True,
318 318 help="""Select the loop runner that will be used to execute top-level asynchronous code"""
319 319 ).tag(config=True)
320 320
321 321 @default('loop_runner')
322 322 def _default_loop_runner(self):
323 323 return import_item("IPython.core.interactiveshell._asyncio_runner")
324 324
325 325 @validate('loop_runner')
326 326 def _import_runner(self, proposal):
327 327 if isinstance(proposal.value, str):
328 328 if proposal.value in self.loop_runner_map:
329 329 runner, autoawait = self.loop_runner_map[proposal.value]
330 330 self.autoawait = autoawait
331 331 return runner
332 332 runner = import_item(proposal.value)
333 333 if not callable(runner):
334 334 raise ValueError('loop_runner must be callable')
335 335 return runner
336 336 if not callable(proposal.value):
337 337 raise ValueError('loop_runner must be callable')
338 338 return proposal.value
339 339
340 340 automagic = Bool(True, help=
341 341 """
342 342 Enable magic commands to be called without the leading %.
343 343 """
344 344 ).tag(config=True)
345 345
346 346 banner1 = Unicode(default_banner,
347 347 help="""The part of the banner to be printed before the profile"""
348 348 ).tag(config=True)
349 349 banner2 = Unicode('',
350 350 help="""The part of the banner to be printed after the profile"""
351 351 ).tag(config=True)
352 352
353 353 cache_size = Integer(1000, help=
354 354 """
355 355 Set the size of the output cache. The default is 1000, you can
356 356 change it permanently in your config file. Setting it to 0 completely
357 357 disables the caching system, and the minimum value accepted is 3 (if
358 358 you provide a value less than 3, it is reset to 0 and a warning is
359 359 issued). This limit is defined because otherwise you'll spend more
360 360 time re-flushing a too small cache than working
361 361 """
362 362 ).tag(config=True)
363 363 color_info = Bool(True, help=
364 364 """
365 365 Use colors for displaying information about objects. Because this
366 366 information is passed through a pager (like 'less'), and some pagers
367 367 get confused with color codes, this capability can be turned off.
368 368 """
369 369 ).tag(config=True)
370 370 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
371 371 default_value='Neutral',
372 372 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
373 373 ).tag(config=True)
374 374 debug = Bool(False).tag(config=True)
375 375 disable_failing_post_execute = Bool(False,
376 376 help="Don't call post-execute functions that have failed in the past."
377 377 ).tag(config=True)
378 378 display_formatter = Instance(DisplayFormatter, allow_none=True)
379 379 displayhook_class = Type(DisplayHook)
380 380 display_pub_class = Type(DisplayPublisher)
381 381 compiler_class = Type(CachingCompiler)
382 382
383 383 sphinxify_docstring = Bool(False, help=
384 384 """
385 385 Enables rich html representation of docstrings. (This requires the
386 386 docrepr module).
387 387 """).tag(config=True)
388 388
389 389 @observe("sphinxify_docstring")
390 390 def _sphinxify_docstring_changed(self, change):
391 391 if change['new']:
392 392 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
393 393
394 394 enable_html_pager = Bool(False, help=
395 395 """
396 396 (Provisional API) enables html representation in mime bundles sent
397 397 to pagers.
398 398 """).tag(config=True)
399 399
400 400 @observe("enable_html_pager")
401 401 def _enable_html_pager_changed(self, change):
402 402 if change['new']:
403 403 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
404 404
405 405 data_pub_class = None
406 406
407 407 exit_now = Bool(False)
408 408 exiter = Instance(ExitAutocall)
409 409 @default('exiter')
410 410 def _exiter_default(self):
411 411 return ExitAutocall(self)
412 412 # Monotonically increasing execution counter
413 413 execution_count = Integer(1)
414 414 filename = Unicode("<ipython console>")
415 415 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
416 416
417 417 # Used to transform cells before running them, and check whether code is complete
418 418 input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager',
419 419 ())
420 420
421 421 @property
422 422 def input_transformers_cleanup(self):
423 423 return self.input_transformer_manager.cleanup_transforms
424 424
425 425 input_transformers_post = List([],
426 426 help="A list of string input transformers, to be applied after IPython's "
427 427 "own input transformations."
428 428 )
429 429
430 430 @property
431 431 def input_splitter(self):
432 432 """Make this available for backward compatibility (pre-7.0 release) with existing code.
433 433
434 434 For example, ipykernel ipykernel currently uses
435 435 `shell.input_splitter.check_complete`
436 436 """
437 437 from warnings import warn
438 438 warn("`input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`.",
439 439 DeprecationWarning, stacklevel=2
440 440 )
441 441 return self.input_transformer_manager
442 442
443 443 logstart = Bool(False, help=
444 444 """
445 445 Start logging to the default log file in overwrite mode.
446 446 Use `logappend` to specify a log file to **append** logs to.
447 447 """
448 448 ).tag(config=True)
449 449 logfile = Unicode('', help=
450 450 """
451 451 The name of the logfile to use.
452 452 """
453 453 ).tag(config=True)
454 454 logappend = Unicode('', help=
455 455 """
456 456 Start logging to the given file in append mode.
457 457 Use `logfile` to specify a log file to **overwrite** logs to.
458 458 """
459 459 ).tag(config=True)
460 460 object_info_string_level = Enum((0,1,2), default_value=0,
461 461 ).tag(config=True)
462 462 pdb = Bool(False, help=
463 463 """
464 464 Automatically call the pdb debugger after every exception.
465 465 """
466 466 ).tag(config=True)
467 467 display_page = Bool(False,
468 468 help="""If True, anything that would be passed to the pager
469 469 will be displayed as regular output instead."""
470 470 ).tag(config=True)
471 471
472 472
473 473 show_rewritten_input = Bool(True,
474 474 help="Show rewritten input, e.g. for autocall."
475 475 ).tag(config=True)
476 476
477 477 quiet = Bool(False).tag(config=True)
478 478
479 479 history_length = Integer(10000,
480 480 help='Total length of command history'
481 481 ).tag(config=True)
482 482
483 483 history_load_length = Integer(1000, help=
484 484 """
485 485 The number of saved history entries to be loaded
486 486 into the history buffer at startup.
487 487 """
488 488 ).tag(config=True)
489 489
490 490 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'],
491 491 default_value='last_expr',
492 492 help="""
493 493 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying
494 494 which nodes should be run interactively (displaying output from expressions).
495 495 """
496 496 ).tag(config=True)
497 497
498 498 warn_venv = Bool(
499 499 True,
500 500 help="Warn if running in a virtual environment with no IPython installed (so IPython from the global environment is used).",
501 501 ).tag(config=True)
502 502
503 503 # TODO: this part of prompt management should be moved to the frontends.
504 504 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
505 505 separate_in = SeparateUnicode('\n').tag(config=True)
506 506 separate_out = SeparateUnicode('').tag(config=True)
507 507 separate_out2 = SeparateUnicode('').tag(config=True)
508 508 wildcards_case_sensitive = Bool(True).tag(config=True)
509 509 xmode = CaselessStrEnum(('Context', 'Plain', 'Verbose', 'Minimal'),
510 510 default_value='Context',
511 511 help="Switch modes for the IPython exception handlers."
512 512 ).tag(config=True)
513 513
514 514 # Subcomponents of InteractiveShell
515 515 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
516 516 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
517 517 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
518 518 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
519 519 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
520 520 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
521 521 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
522 522 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
523 523
524 524 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
525 525 @property
526 526 def profile(self):
527 527 if self.profile_dir is not None:
528 528 name = os.path.basename(self.profile_dir.location)
529 529 return name.replace('profile_','')
530 530
531 531
532 532 # Private interface
533 533 _post_execute = Dict()
534 534
535 535 # Tracks any GUI loop loaded for pylab
536 536 pylab_gui_select = None
537 537
538 538 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
539 539
540 540 last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True)
541 541
542 542 def __init__(self, ipython_dir=None, profile_dir=None,
543 543 user_module=None, user_ns=None,
544 544 custom_exceptions=((), None), **kwargs):
545 545 # This is where traits with a config_key argument are updated
546 546 # from the values on config.
547 547 super(InteractiveShell, self).__init__(**kwargs)
548 548 if 'PromptManager' in self.config:
549 549 warn('As of IPython 5.0 `PromptManager` config will have no effect'
550 550 ' and has been replaced by TerminalInteractiveShell.prompts_class')
551 551 self.configurables = [self]
552 552
553 553 # These are relatively independent and stateless
554 554 self.init_ipython_dir(ipython_dir)
555 555 self.init_profile_dir(profile_dir)
556 556 self.init_instance_attrs()
557 557 self.init_environment()
558 558
559 559 # Check if we're in a virtualenv, and set up sys.path.
560 560 self.init_virtualenv()
561 561
562 562 # Create namespaces (user_ns, user_global_ns, etc.)
563 563 self.init_create_namespaces(user_module, user_ns)
564 564 # This has to be done after init_create_namespaces because it uses
565 565 # something in self.user_ns, but before init_sys_modules, which
566 566 # is the first thing to modify sys.
567 567 # TODO: When we override sys.stdout and sys.stderr before this class
568 568 # is created, we are saving the overridden ones here. Not sure if this
569 569 # is what we want to do.
570 570 self.save_sys_module_state()
571 571 self.init_sys_modules()
572 572
573 573 # While we're trying to have each part of the code directly access what
574 574 # it needs without keeping redundant references to objects, we have too
575 575 # much legacy code that expects ip.db to exist.
576 576 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
577 577
578 578 self.init_history()
579 579 self.init_encoding()
580 580 self.init_prefilter()
581 581
582 582 self.init_syntax_highlighting()
583 583 self.init_hooks()
584 584 self.init_events()
585 585 self.init_pushd_popd_magic()
586 586 self.init_user_ns()
587 587 self.init_logger()
588 588 self.init_builtins()
589 589
590 590 # The following was in post_config_initialization
591 591 self.init_inspector()
592 592 self.raw_input_original = input
593 593 self.init_completer()
594 594 # TODO: init_io() needs to happen before init_traceback handlers
595 595 # because the traceback handlers hardcode the stdout/stderr streams.
596 596 # This logic in in debugger.Pdb and should eventually be changed.
597 597 self.init_io()
598 598 self.init_traceback_handlers(custom_exceptions)
599 599 self.init_prompts()
600 600 self.init_display_formatter()
601 601 self.init_display_pub()
602 602 self.init_data_pub()
603 603 self.init_displayhook()
604 604 self.init_magics()
605 605 self.init_alias()
606 606 self.init_logstart()
607 607 self.init_pdb()
608 608 self.init_extension_manager()
609 609 self.init_payload()
610 610 self.events.trigger('shell_initialized', self)
611 611 atexit.register(self.atexit_operations)
612 612
613 613 # The trio runner is used for running Trio in the foreground thread. It
614 614 # is different from `_trio_runner(async_fn)` in `async_helpers.py`
615 615 # which calls `trio.run()` for every cell. This runner runs all cells
616 616 # inside a single Trio event loop. If used, it is set from
617 617 # `ipykernel.kernelapp`.
618 618 self.trio_runner = None
619 619
620 620 def get_ipython(self):
621 621 """Return the currently running IPython instance."""
622 622 return self
623 623
624 624 #-------------------------------------------------------------------------
625 625 # Trait changed handlers
626 626 #-------------------------------------------------------------------------
627 627 @observe('ipython_dir')
628 628 def _ipython_dir_changed(self, change):
629 629 ensure_dir_exists(change['new'])
630 630
631 631 def set_autoindent(self,value=None):
632 632 """Set the autoindent flag.
633 633
634 634 If called with no arguments, it acts as a toggle."""
635 635 if value is None:
636 636 self.autoindent = not self.autoindent
637 637 else:
638 638 self.autoindent = value
639 639
640 640 def set_trio_runner(self, tr):
641 641 self.trio_runner = tr
642 642
643 643 #-------------------------------------------------------------------------
644 644 # init_* methods called by __init__
645 645 #-------------------------------------------------------------------------
646 646
647 647 def init_ipython_dir(self, ipython_dir):
648 648 if ipython_dir is not None:
649 649 self.ipython_dir = ipython_dir
650 650 return
651 651
652 652 self.ipython_dir = get_ipython_dir()
653 653
654 654 def init_profile_dir(self, profile_dir):
655 655 if profile_dir is not None:
656 656 self.profile_dir = profile_dir
657 657 return
658 658 self.profile_dir = ProfileDir.create_profile_dir_by_name(
659 659 self.ipython_dir, "default"
660 660 )
661 661
662 662 def init_instance_attrs(self):
663 663 self.more = False
664 664
665 665 # command compiler
666 666 self.compile = self.compiler_class()
667 667
668 668 # Make an empty namespace, which extension writers can rely on both
669 669 # existing and NEVER being used by ipython itself. This gives them a
670 670 # convenient location for storing additional information and state
671 671 # their extensions may require, without fear of collisions with other
672 672 # ipython names that may develop later.
673 673 self.meta = Struct()
674 674
675 675 # Temporary files used for various purposes. Deleted at exit.
676 676 # The files here are stored with Path from Pathlib
677 677 self.tempfiles = []
678 678 self.tempdirs = []
679 679
680 680 # keep track of where we started running (mainly for crash post-mortem)
681 681 # This is not being used anywhere currently.
682 682 self.starting_dir = os.getcwd()
683 683
684 684 # Indentation management
685 685 self.indent_current_nsp = 0
686 686
687 687 # Dict to track post-execution functions that have been registered
688 688 self._post_execute = {}
689 689
690 690 def init_environment(self):
691 691 """Any changes we need to make to the user's environment."""
692 692 pass
693 693
694 694 def init_encoding(self):
695 695 # Get system encoding at startup time. Certain terminals (like Emacs
696 696 # under Win32 have it set to None, and we need to have a known valid
697 697 # encoding to use in the raw_input() method
698 698 try:
699 699 self.stdin_encoding = sys.stdin.encoding or 'ascii'
700 700 except AttributeError:
701 701 self.stdin_encoding = 'ascii'
702 702
703 703
704 704 @observe('colors')
705 705 def init_syntax_highlighting(self, changes=None):
706 706 # Python source parser/formatter for syntax highlighting
707 707 pyformat = PyColorize.Parser(style=self.colors, parent=self).format
708 708 self.pycolorize = lambda src: pyformat(src,'str')
709 709
710 710 def refresh_style(self):
711 711 # No-op here, used in subclass
712 712 pass
713 713
714 714 def init_pushd_popd_magic(self):
715 715 # for pushd/popd management
716 716 self.home_dir = get_home_dir()
717 717
718 718 self.dir_stack = []
719 719
720 720 def init_logger(self):
721 721 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
722 722 logmode='rotate')
723 723
724 724 def init_logstart(self):
725 725 """Initialize logging in case it was requested at the command line.
726 726 """
727 727 if self.logappend:
728 728 self.magic('logstart %s append' % self.logappend)
729 729 elif self.logfile:
730 730 self.magic('logstart %s' % self.logfile)
731 731 elif self.logstart:
732 732 self.magic('logstart')
733 733
734 734
735 735 def init_builtins(self):
736 736 # A single, static flag that we set to True. Its presence indicates
737 737 # that an IPython shell has been created, and we make no attempts at
738 738 # removing on exit or representing the existence of more than one
739 739 # IPython at a time.
740 740 builtin_mod.__dict__['__IPYTHON__'] = True
741 741 builtin_mod.__dict__['display'] = display
742 742
743 743 self.builtin_trap = BuiltinTrap(shell=self)
744 744
745 745 @observe('colors')
746 746 def init_inspector(self, changes=None):
747 747 # Object inspector
748 748 self.inspector = oinspect.Inspector(oinspect.InspectColors,
749 749 PyColorize.ANSICodeColors,
750 750 self.colors,
751 751 self.object_info_string_level)
752 752
753 753 def init_io(self):
754 754 # implemented in subclasses, TerminalInteractiveShell does call
755 755 # colorama.init().
756 756 pass
757 757
758 758 def init_prompts(self):
759 759 # Set system prompts, so that scripts can decide if they are running
760 760 # interactively.
761 761 sys.ps1 = 'In : '
762 762 sys.ps2 = '...: '
763 763 sys.ps3 = 'Out: '
764 764
765 765 def init_display_formatter(self):
766 766 self.display_formatter = DisplayFormatter(parent=self)
767 767 self.configurables.append(self.display_formatter)
768 768
769 769 def init_display_pub(self):
770 770 self.display_pub = self.display_pub_class(parent=self, shell=self)
771 771 self.configurables.append(self.display_pub)
772 772
773 773 def init_data_pub(self):
774 774 if not self.data_pub_class:
775 775 self.data_pub = None
776 776 return
777 777 self.data_pub = self.data_pub_class(parent=self)
778 778 self.configurables.append(self.data_pub)
779 779
780 780 def init_displayhook(self):
781 781 # Initialize displayhook, set in/out prompts and printing system
782 782 self.displayhook = self.displayhook_class(
783 783 parent=self,
784 784 shell=self,
785 785 cache_size=self.cache_size,
786 786 )
787 787 self.configurables.append(self.displayhook)
788 788 # This is a context manager that installs/revmoes the displayhook at
789 789 # the appropriate time.
790 790 self.display_trap = DisplayTrap(hook=self.displayhook)
791 791
792 792 @staticmethod
793 793 def get_path_links(p: Path):
794 794 """Gets path links including all symlinks
795 795
796 796 Examples
797 797 --------
798 798 In [1]: from IPython.core.interactiveshell import InteractiveShell
799 799
800 800 In [2]: import sys, pathlib
801 801
802 802 In [3]: paths = InteractiveShell.get_path_links(pathlib.Path(sys.executable))
803 803
804 804 In [4]: len(paths) == len(set(paths))
805 805 Out[4]: True
806 806
807 807 In [5]: bool(paths)
808 808 Out[5]: True
809 809 """
810 810 paths = [p]
811 811 while p.is_symlink():
812 812 new_path = Path(os.readlink(p))
813 813 if not new_path.is_absolute():
814 814 new_path = p.parent / new_path
815 815 p = new_path
816 816 paths.append(p)
817 817 return paths
818 818
819 819 def init_virtualenv(self):
820 820 """Add the current virtualenv to sys.path so the user can import modules from it.
821 821 This isn't perfect: it doesn't use the Python interpreter with which the
822 822 virtualenv was built, and it ignores the --no-site-packages option. A
823 823 warning will appear suggesting the user installs IPython in the
824 824 virtualenv, but for many cases, it probably works well enough.
825 825
826 826 Adapted from code snippets online.
827 827
828 828 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
829 829 """
830 830 if 'VIRTUAL_ENV' not in os.environ:
831 831 # Not in a virtualenv
832 832 return
833 833 elif os.environ["VIRTUAL_ENV"] == "":
834 834 warn("Virtual env path set to '', please check if this is intended.")
835 835 return
836 836
837 837 p = Path(sys.executable)
838 838 p_venv = Path(os.environ["VIRTUAL_ENV"])
839 839
840 840 # fallback venv detection:
841 841 # stdlib venv may symlink sys.executable, so we can't use realpath.
842 842 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
843 843 # So we just check every item in the symlink tree (generally <= 3)
844 844 paths = self.get_path_links(p)
845 845
846 846 # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
847 847 if p_venv.parts[1] == "cygdrive":
848 848 drive_name = p_venv.parts[2]
849 849 p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:])
850 850
851 851 if any(p_venv == p.parents[1] for p in paths):
852 852 # Our exe is inside or has access to the virtualenv, don't need to do anything.
853 853 return
854 854
855 855 if sys.platform == "win32":
856 856 virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages"))
857 857 else:
858 858 virtual_env_path = Path(
859 859 os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages"
860 860 )
861 861 p_ver = sys.version_info[:2]
862 862
863 863 # Predict version from py[thon]-x.x in the $VIRTUAL_ENV
864 864 re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"])
865 865 if re_m:
866 866 predicted_path = Path(str(virtual_env_path).format(*re_m.groups()))
867 867 if predicted_path.exists():
868 868 p_ver = re_m.groups()
869 869
870 870 virtual_env = str(virtual_env_path).format(*p_ver)
871 871 if self.warn_venv:
872 872 warn(
873 873 "Attempting to work in a virtualenv. If you encounter problems, "
874 874 "please install IPython inside the virtualenv."
875 875 )
876 876 import site
877 877 sys.path.insert(0, virtual_env)
878 878 site.addsitedir(virtual_env)
879 879
880 880 #-------------------------------------------------------------------------
881 881 # Things related to injections into the sys module
882 882 #-------------------------------------------------------------------------
883 883
884 884 def save_sys_module_state(self):
885 885 """Save the state of hooks in the sys module.
886 886
887 887 This has to be called after self.user_module is created.
888 888 """
889 889 self._orig_sys_module_state = {'stdin': sys.stdin,
890 890 'stdout': sys.stdout,
891 891 'stderr': sys.stderr,
892 892 'excepthook': sys.excepthook}
893 893 self._orig_sys_modules_main_name = self.user_module.__name__
894 894 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
895 895
896 896 def restore_sys_module_state(self):
897 897 """Restore the state of the sys module."""
898 898 try:
899 899 for k, v in self._orig_sys_module_state.items():
900 900 setattr(sys, k, v)
901 901 except AttributeError:
902 902 pass
903 903 # Reset what what done in self.init_sys_modules
904 904 if self._orig_sys_modules_main_mod is not None:
905 905 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
906 906
907 907 #-------------------------------------------------------------------------
908 908 # Things related to the banner
909 909 #-------------------------------------------------------------------------
910 910
911 911 @property
912 912 def banner(self):
913 913 banner = self.banner1
914 914 if self.profile and self.profile != 'default':
915 915 banner += '\nIPython profile: %s\n' % self.profile
916 916 if self.banner2:
917 917 banner += '\n' + self.banner2
918 918 return banner
919 919
920 920 def show_banner(self, banner=None):
921 921 if banner is None:
922 922 banner = self.banner
923 923 sys.stdout.write(banner)
924 924
925 925 #-------------------------------------------------------------------------
926 926 # Things related to hooks
927 927 #-------------------------------------------------------------------------
928 928
929 929 def init_hooks(self):
930 930 # hooks holds pointers used for user-side customizations
931 931 self.hooks = Struct()
932 932
933 933 self.strdispatchers = {}
934 934
935 935 # Set all default hooks, defined in the IPython.hooks module.
936 936 hooks = IPython.core.hooks
937 937 for hook_name in hooks.__all__:
938 938 # default hooks have priority 100, i.e. low; user hooks should have
939 939 # 0-100 priority
940 940 self.set_hook(hook_name, getattr(hooks, hook_name), 100)
941 941
942 942 if self.display_page:
943 943 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
944 944
945 945 def set_hook(self, name, hook, priority=50, str_key=None, re_key=None):
946 946 """set_hook(name,hook) -> sets an internal IPython hook.
947 947
948 948 IPython exposes some of its internal API as user-modifiable hooks. By
949 949 adding your function to one of these hooks, you can modify IPython's
950 950 behavior to call at runtime your own routines."""
951 951
952 952 # At some point in the future, this should validate the hook before it
953 953 # accepts it. Probably at least check that the hook takes the number
954 954 # of args it's supposed to.
955 955
956 956 f = types.MethodType(hook,self)
957 957
958 958 # check if the hook is for strdispatcher first
959 959 if str_key is not None:
960 960 sdp = self.strdispatchers.get(name, StrDispatch())
961 961 sdp.add_s(str_key, f, priority )
962 962 self.strdispatchers[name] = sdp
963 963 return
964 964 if re_key is not None:
965 965 sdp = self.strdispatchers.get(name, StrDispatch())
966 966 sdp.add_re(re.compile(re_key), f, priority )
967 967 self.strdispatchers[name] = sdp
968 968 return
969 969
970 970 dp = getattr(self.hooks, name, None)
971 971 if name not in IPython.core.hooks.__all__:
972 972 print("Warning! Hook '%s' is not one of %s" % \
973 973 (name, IPython.core.hooks.__all__ ))
974 974
975 975 if name in IPython.core.hooks.deprecated:
976 976 alternative = IPython.core.hooks.deprecated[name]
977 977 raise ValueError(
978 978 "Hook {} has been deprecated since IPython 5.0. Use {} instead.".format(
979 979 name, alternative
980 980 )
981 981 )
982 982
983 983 if not dp:
984 984 dp = IPython.core.hooks.CommandChainDispatcher()
985 985
986 986 try:
987 987 dp.add(f,priority)
988 988 except AttributeError:
989 989 # it was not commandchain, plain old func - replace
990 990 dp = f
991 991
992 992 setattr(self.hooks,name, dp)
993 993
994 994 #-------------------------------------------------------------------------
995 995 # Things related to events
996 996 #-------------------------------------------------------------------------
997 997
998 998 def init_events(self):
999 999 self.events = EventManager(self, available_events)
1000 1000
1001 1001 self.events.register("pre_execute", self._clear_warning_registry)
1002 1002
1003 1003 def register_post_execute(self, func):
1004 1004 """DEPRECATED: Use ip.events.register('post_run_cell', func)
1005 1005
1006 1006 Register a function for calling after code execution.
1007 1007 """
1008 1008 raise ValueError(
1009 1009 "ip.register_post_execute is deprecated since IPython 1.0, use "
1010 1010 "ip.events.register('post_run_cell', func) instead."
1011 1011 )
1012 1012
1013 1013 def _clear_warning_registry(self):
1014 1014 # clear the warning registry, so that different code blocks with
1015 1015 # overlapping line number ranges don't cause spurious suppression of
1016 1016 # warnings (see gh-6611 for details)
1017 1017 if "__warningregistry__" in self.user_global_ns:
1018 1018 del self.user_global_ns["__warningregistry__"]
1019 1019
1020 1020 #-------------------------------------------------------------------------
1021 1021 # Things related to the "main" module
1022 1022 #-------------------------------------------------------------------------
1023 1023
1024 1024 def new_main_mod(self, filename, modname):
1025 1025 """Return a new 'main' module object for user code execution.
1026 1026
1027 1027 ``filename`` should be the path of the script which will be run in the
1028 1028 module. Requests with the same filename will get the same module, with
1029 1029 its namespace cleared.
1030 1030
1031 1031 ``modname`` should be the module name - normally either '__main__' or
1032 1032 the basename of the file without the extension.
1033 1033
1034 1034 When scripts are executed via %run, we must keep a reference to their
1035 1035 __main__ module around so that Python doesn't
1036 1036 clear it, rendering references to module globals useless.
1037 1037
1038 1038 This method keeps said reference in a private dict, keyed by the
1039 1039 absolute path of the script. This way, for multiple executions of the
1040 1040 same script we only keep one copy of the namespace (the last one),
1041 1041 thus preventing memory leaks from old references while allowing the
1042 1042 objects from the last execution to be accessible.
1043 1043 """
1044 1044 filename = os.path.abspath(filename)
1045 1045 try:
1046 1046 main_mod = self._main_mod_cache[filename]
1047 1047 except KeyError:
1048 1048 main_mod = self._main_mod_cache[filename] = types.ModuleType(
1049 1049 modname,
1050 1050 doc="Module created for script run in IPython")
1051 1051 else:
1052 1052 main_mod.__dict__.clear()
1053 1053 main_mod.__name__ = modname
1054 1054
1055 1055 main_mod.__file__ = filename
1056 1056 # It seems pydoc (and perhaps others) needs any module instance to
1057 1057 # implement a __nonzero__ method
1058 1058 main_mod.__nonzero__ = lambda : True
1059 1059
1060 1060 return main_mod
1061 1061
1062 1062 def clear_main_mod_cache(self):
1063 1063 """Clear the cache of main modules.
1064 1064
1065 1065 Mainly for use by utilities like %reset.
1066 1066
1067 1067 Examples
1068 1068 --------
1069 1069 In [15]: import IPython
1070 1070
1071 1071 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
1072 1072
1073 1073 In [17]: len(_ip._main_mod_cache) > 0
1074 1074 Out[17]: True
1075 1075
1076 1076 In [18]: _ip.clear_main_mod_cache()
1077 1077
1078 1078 In [19]: len(_ip._main_mod_cache) == 0
1079 1079 Out[19]: True
1080 1080 """
1081 1081 self._main_mod_cache.clear()
1082 1082
1083 1083 #-------------------------------------------------------------------------
1084 1084 # Things related to debugging
1085 1085 #-------------------------------------------------------------------------
1086 1086
1087 1087 def init_pdb(self):
1088 1088 # Set calling of pdb on exceptions
1089 1089 # self.call_pdb is a property
1090 1090 self.call_pdb = self.pdb
1091 1091
1092 1092 def _get_call_pdb(self):
1093 1093 return self._call_pdb
1094 1094
1095 1095 def _set_call_pdb(self,val):
1096 1096
1097 1097 if val not in (0,1,False,True):
1098 1098 raise ValueError('new call_pdb value must be boolean')
1099 1099
1100 1100 # store value in instance
1101 1101 self._call_pdb = val
1102 1102
1103 1103 # notify the actual exception handlers
1104 1104 self.InteractiveTB.call_pdb = val
1105 1105
1106 1106 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
1107 1107 'Control auto-activation of pdb at exceptions')
1108 1108
1109 1109 def debugger(self,force=False):
1110 1110 """Call the pdb debugger.
1111 1111
1112 1112 Keywords:
1113 1113
1114 1114 - force(False): by default, this routine checks the instance call_pdb
1115 1115 flag and does not actually invoke the debugger if the flag is false.
1116 1116 The 'force' option forces the debugger to activate even if the flag
1117 1117 is false.
1118 1118 """
1119 1119
1120 1120 if not (force or self.call_pdb):
1121 1121 return
1122 1122
1123 1123 if not hasattr(sys,'last_traceback'):
1124 1124 error('No traceback has been produced, nothing to debug.')
1125 1125 return
1126 1126
1127 1127 self.InteractiveTB.debugger(force=True)
1128 1128
1129 1129 #-------------------------------------------------------------------------
1130 1130 # Things related to IPython's various namespaces
1131 1131 #-------------------------------------------------------------------------
1132 1132 default_user_namespaces = True
1133 1133
1134 1134 def init_create_namespaces(self, user_module=None, user_ns=None):
1135 1135 # Create the namespace where the user will operate. user_ns is
1136 1136 # normally the only one used, and it is passed to the exec calls as
1137 1137 # the locals argument. But we do carry a user_global_ns namespace
1138 1138 # given as the exec 'globals' argument, This is useful in embedding
1139 1139 # situations where the ipython shell opens in a context where the
1140 1140 # distinction between locals and globals is meaningful. For
1141 1141 # non-embedded contexts, it is just the same object as the user_ns dict.
1142 1142
1143 1143 # FIXME. For some strange reason, __builtins__ is showing up at user
1144 1144 # level as a dict instead of a module. This is a manual fix, but I
1145 1145 # should really track down where the problem is coming from. Alex
1146 1146 # Schmolck reported this problem first.
1147 1147
1148 1148 # A useful post by Alex Martelli on this topic:
1149 1149 # Re: inconsistent value from __builtins__
1150 1150 # Von: Alex Martelli <aleaxit@yahoo.com>
1151 1151 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1152 1152 # Gruppen: comp.lang.python
1153 1153
1154 1154 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1155 1155 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1156 1156 # > <type 'dict'>
1157 1157 # > >>> print type(__builtins__)
1158 1158 # > <type 'module'>
1159 1159 # > Is this difference in return value intentional?
1160 1160
1161 1161 # Well, it's documented that '__builtins__' can be either a dictionary
1162 1162 # or a module, and it's been that way for a long time. Whether it's
1163 1163 # intentional (or sensible), I don't know. In any case, the idea is
1164 1164 # that if you need to access the built-in namespace directly, you
1165 1165 # should start with "import __builtin__" (note, no 's') which will
1166 1166 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1167 1167
1168 1168 # These routines return a properly built module and dict as needed by
1169 1169 # the rest of the code, and can also be used by extension writers to
1170 1170 # generate properly initialized namespaces.
1171 1171 if (user_ns is not None) or (user_module is not None):
1172 1172 self.default_user_namespaces = False
1173 1173 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1174 1174
1175 1175 # A record of hidden variables we have added to the user namespace, so
1176 1176 # we can list later only variables defined in actual interactive use.
1177 1177 self.user_ns_hidden = {}
1178 1178
1179 1179 # Now that FakeModule produces a real module, we've run into a nasty
1180 1180 # problem: after script execution (via %run), the module where the user
1181 1181 # code ran is deleted. Now that this object is a true module (needed
1182 1182 # so doctest and other tools work correctly), the Python module
1183 1183 # teardown mechanism runs over it, and sets to None every variable
1184 1184 # present in that module. Top-level references to objects from the
1185 1185 # script survive, because the user_ns is updated with them. However,
1186 1186 # calling functions defined in the script that use other things from
1187 1187 # the script will fail, because the function's closure had references
1188 1188 # to the original objects, which are now all None. So we must protect
1189 1189 # these modules from deletion by keeping a cache.
1190 1190 #
1191 1191 # To avoid keeping stale modules around (we only need the one from the
1192 1192 # last run), we use a dict keyed with the full path to the script, so
1193 1193 # only the last version of the module is held in the cache. Note,
1194 1194 # however, that we must cache the module *namespace contents* (their
1195 1195 # __dict__). Because if we try to cache the actual modules, old ones
1196 1196 # (uncached) could be destroyed while still holding references (such as
1197 1197 # those held by GUI objects that tend to be long-lived)>
1198 1198 #
1199 1199 # The %reset command will flush this cache. See the cache_main_mod()
1200 1200 # and clear_main_mod_cache() methods for details on use.
1201 1201
1202 1202 # This is the cache used for 'main' namespaces
1203 1203 self._main_mod_cache = {}
1204 1204
1205 1205 # A table holding all the namespaces IPython deals with, so that
1206 1206 # introspection facilities can search easily.
1207 1207 self.ns_table = {'user_global':self.user_module.__dict__,
1208 1208 'user_local':self.user_ns,
1209 1209 'builtin':builtin_mod.__dict__
1210 1210 }
1211 1211
1212 1212 @property
1213 1213 def user_global_ns(self):
1214 1214 return self.user_module.__dict__
1215 1215
1216 1216 def prepare_user_module(self, user_module=None, user_ns=None):
1217 1217 """Prepare the module and namespace in which user code will be run.
1218 1218
1219 1219 When IPython is started normally, both parameters are None: a new module
1220 1220 is created automatically, and its __dict__ used as the namespace.
1221 1221
1222 1222 If only user_module is provided, its __dict__ is used as the namespace.
1223 1223 If only user_ns is provided, a dummy module is created, and user_ns
1224 1224 becomes the global namespace. If both are provided (as they may be
1225 1225 when embedding), user_ns is the local namespace, and user_module
1226 1226 provides the global namespace.
1227 1227
1228 1228 Parameters
1229 1229 ----------
1230 1230 user_module : module, optional
1231 1231 The current user module in which IPython is being run. If None,
1232 1232 a clean module will be created.
1233 1233 user_ns : dict, optional
1234 1234 A namespace in which to run interactive commands.
1235 1235
1236 1236 Returns
1237 1237 -------
1238 1238 A tuple of user_module and user_ns, each properly initialised.
1239 1239 """
1240 1240 if user_module is None and user_ns is not None:
1241 1241 user_ns.setdefault("__name__", "__main__")
1242 1242 user_module = DummyMod()
1243 1243 user_module.__dict__ = user_ns
1244 1244
1245 1245 if user_module is None:
1246 1246 user_module = types.ModuleType("__main__",
1247 1247 doc="Automatically created module for IPython interactive environment")
1248 1248
1249 1249 # We must ensure that __builtin__ (without the final 's') is always
1250 1250 # available and pointing to the __builtin__ *module*. For more details:
1251 1251 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1252 1252 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1253 1253 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1254 1254
1255 1255 if user_ns is None:
1256 1256 user_ns = user_module.__dict__
1257 1257
1258 1258 return user_module, user_ns
1259 1259
1260 1260 def init_sys_modules(self):
1261 1261 # We need to insert into sys.modules something that looks like a
1262 1262 # module but which accesses the IPython namespace, for shelve and
1263 1263 # pickle to work interactively. Normally they rely on getting
1264 1264 # everything out of __main__, but for embedding purposes each IPython
1265 1265 # instance has its own private namespace, so we can't go shoving
1266 1266 # everything into __main__.
1267 1267
1268 1268 # note, however, that we should only do this for non-embedded
1269 1269 # ipythons, which really mimic the __main__.__dict__ with their own
1270 1270 # namespace. Embedded instances, on the other hand, should not do
1271 1271 # this because they need to manage the user local/global namespaces
1272 1272 # only, but they live within a 'normal' __main__ (meaning, they
1273 1273 # shouldn't overtake the execution environment of the script they're
1274 1274 # embedded in).
1275 1275
1276 1276 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1277 1277 main_name = self.user_module.__name__
1278 1278 sys.modules[main_name] = self.user_module
1279 1279
1280 1280 def init_user_ns(self):
1281 1281 """Initialize all user-visible namespaces to their minimum defaults.
1282 1282
1283 1283 Certain history lists are also initialized here, as they effectively
1284 1284 act as user namespaces.
1285 1285
1286 1286 Notes
1287 1287 -----
1288 1288 All data structures here are only filled in, they are NOT reset by this
1289 1289 method. If they were not empty before, data will simply be added to
1290 1290 them.
1291 1291 """
1292 1292 # This function works in two parts: first we put a few things in
1293 1293 # user_ns, and we sync that contents into user_ns_hidden so that these
1294 1294 # initial variables aren't shown by %who. After the sync, we add the
1295 1295 # rest of what we *do* want the user to see with %who even on a new
1296 1296 # session (probably nothing, so they really only see their own stuff)
1297 1297
1298 1298 # The user dict must *always* have a __builtin__ reference to the
1299 1299 # Python standard __builtin__ namespace, which must be imported.
1300 1300 # This is so that certain operations in prompt evaluation can be
1301 1301 # reliably executed with builtins. Note that we can NOT use
1302 1302 # __builtins__ (note the 's'), because that can either be a dict or a
1303 1303 # module, and can even mutate at runtime, depending on the context
1304 1304 # (Python makes no guarantees on it). In contrast, __builtin__ is
1305 1305 # always a module object, though it must be explicitly imported.
1306 1306
1307 1307 # For more details:
1308 1308 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1309 1309 ns = {}
1310 1310
1311 1311 # make global variables for user access to the histories
1312 1312 ns['_ih'] = self.history_manager.input_hist_parsed
1313 1313 ns['_oh'] = self.history_manager.output_hist
1314 1314 ns['_dh'] = self.history_manager.dir_hist
1315 1315
1316 1316 # user aliases to input and output histories. These shouldn't show up
1317 1317 # in %who, as they can have very large reprs.
1318 1318 ns['In'] = self.history_manager.input_hist_parsed
1319 1319 ns['Out'] = self.history_manager.output_hist
1320 1320
1321 1321 # Store myself as the public api!!!
1322 1322 ns['get_ipython'] = self.get_ipython
1323 1323
1324 1324 ns['exit'] = self.exiter
1325 1325 ns['quit'] = self.exiter
1326 1326
1327 1327 # Sync what we've added so far to user_ns_hidden so these aren't seen
1328 1328 # by %who
1329 1329 self.user_ns_hidden.update(ns)
1330 1330
1331 1331 # Anything put into ns now would show up in %who. Think twice before
1332 1332 # putting anything here, as we really want %who to show the user their
1333 1333 # stuff, not our variables.
1334 1334
1335 1335 # Finally, update the real user's namespace
1336 1336 self.user_ns.update(ns)
1337 1337
1338 1338 @property
1339 1339 def all_ns_refs(self):
1340 1340 """Get a list of references to all the namespace dictionaries in which
1341 1341 IPython might store a user-created object.
1342 1342
1343 1343 Note that this does not include the displayhook, which also caches
1344 1344 objects from the output."""
1345 1345 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1346 1346 [m.__dict__ for m in self._main_mod_cache.values()]
1347 1347
1348 1348 def reset(self, new_session=True, aggressive=False):
1349 1349 """Clear all internal namespaces, and attempt to release references to
1350 1350 user objects.
1351 1351
1352 1352 If new_session is True, a new history session will be opened.
1353 1353 """
1354 1354 # Clear histories
1355 1355 self.history_manager.reset(new_session)
1356 1356 # Reset counter used to index all histories
1357 1357 if new_session:
1358 1358 self.execution_count = 1
1359 1359
1360 1360 # Reset last execution result
1361 1361 self.last_execution_succeeded = True
1362 1362 self.last_execution_result = None
1363 1363
1364 1364 # Flush cached output items
1365 1365 if self.displayhook.do_full_cache:
1366 1366 self.displayhook.flush()
1367 1367
1368 1368 # The main execution namespaces must be cleared very carefully,
1369 1369 # skipping the deletion of the builtin-related keys, because doing so
1370 1370 # would cause errors in many object's __del__ methods.
1371 1371 if self.user_ns is not self.user_global_ns:
1372 1372 self.user_ns.clear()
1373 1373 ns = self.user_global_ns
1374 1374 drop_keys = set(ns.keys())
1375 1375 drop_keys.discard('__builtin__')
1376 1376 drop_keys.discard('__builtins__')
1377 1377 drop_keys.discard('__name__')
1378 1378 for k in drop_keys:
1379 1379 del ns[k]
1380 1380
1381 1381 self.user_ns_hidden.clear()
1382 1382
1383 1383 # Restore the user namespaces to minimal usability
1384 1384 self.init_user_ns()
1385 1385 if aggressive and not hasattr(self, "_sys_modules_keys"):
1386 1386 print("Cannot restore sys.module, no snapshot")
1387 1387 elif aggressive:
1388 1388 print("culling sys module...")
1389 1389 current_keys = set(sys.modules.keys())
1390 1390 for k in current_keys - self._sys_modules_keys:
1391 1391 if k.startswith("multiprocessing"):
1392 1392 continue
1393 1393 del sys.modules[k]
1394 1394
1395 1395 # Restore the default and user aliases
1396 1396 self.alias_manager.clear_aliases()
1397 1397 self.alias_manager.init_aliases()
1398 1398
1399 1399 # Now define aliases that only make sense on the terminal, because they
1400 1400 # need direct access to the console in a way that we can't emulate in
1401 1401 # GUI or web frontend
1402 1402 if os.name == 'posix':
1403 1403 for cmd in ('clear', 'more', 'less', 'man'):
1404 1404 if cmd not in self.magics_manager.magics['line']:
1405 1405 self.alias_manager.soft_define_alias(cmd, cmd)
1406 1406
1407 1407 # Flush the private list of module references kept for script
1408 1408 # execution protection
1409 1409 self.clear_main_mod_cache()
1410 1410
1411 1411 def del_var(self, varname, by_name=False):
1412 1412 """Delete a variable from the various namespaces, so that, as
1413 1413 far as possible, we're not keeping any hidden references to it.
1414 1414
1415 1415 Parameters
1416 1416 ----------
1417 1417 varname : str
1418 1418 The name of the variable to delete.
1419 1419 by_name : bool
1420 1420 If True, delete variables with the given name in each
1421 1421 namespace. If False (default), find the variable in the user
1422 1422 namespace, and delete references to it.
1423 1423 """
1424 1424 if varname in ('__builtin__', '__builtins__'):
1425 1425 raise ValueError("Refusing to delete %s" % varname)
1426 1426
1427 1427 ns_refs = self.all_ns_refs
1428 1428
1429 1429 if by_name: # Delete by name
1430 1430 for ns in ns_refs:
1431 1431 try:
1432 1432 del ns[varname]
1433 1433 except KeyError:
1434 1434 pass
1435 1435 else: # Delete by object
1436 1436 try:
1437 1437 obj = self.user_ns[varname]
1438 1438 except KeyError as e:
1439 1439 raise NameError("name '%s' is not defined" % varname) from e
1440 1440 # Also check in output history
1441 1441 ns_refs.append(self.history_manager.output_hist)
1442 1442 for ns in ns_refs:
1443 1443 to_delete = [n for n, o in ns.items() if o is obj]
1444 1444 for name in to_delete:
1445 1445 del ns[name]
1446 1446
1447 1447 # Ensure it is removed from the last execution result
1448 1448 if self.last_execution_result.result is obj:
1449 1449 self.last_execution_result = None
1450 1450
1451 1451 # displayhook keeps extra references, but not in a dictionary
1452 1452 for name in ('_', '__', '___'):
1453 1453 if getattr(self.displayhook, name) is obj:
1454 1454 setattr(self.displayhook, name, None)
1455 1455
1456 1456 def reset_selective(self, regex=None):
1457 1457 """Clear selective variables from internal namespaces based on a
1458 1458 specified regular expression.
1459 1459
1460 1460 Parameters
1461 1461 ----------
1462 1462 regex : string or compiled pattern, optional
1463 1463 A regular expression pattern that will be used in searching
1464 1464 variable names in the users namespaces.
1465 1465 """
1466 1466 if regex is not None:
1467 1467 try:
1468 1468 m = re.compile(regex)
1469 1469 except TypeError as e:
1470 1470 raise TypeError('regex must be a string or compiled pattern') from e
1471 1471 # Search for keys in each namespace that match the given regex
1472 1472 # If a match is found, delete the key/value pair.
1473 1473 for ns in self.all_ns_refs:
1474 1474 for var in ns:
1475 1475 if m.search(var):
1476 1476 del ns[var]
1477 1477
1478 1478 def push(self, variables, interactive=True):
1479 1479 """Inject a group of variables into the IPython user namespace.
1480 1480
1481 1481 Parameters
1482 1482 ----------
1483 1483 variables : dict, str or list/tuple of str
1484 1484 The variables to inject into the user's namespace. If a dict, a
1485 1485 simple update is done. If a str, the string is assumed to have
1486 1486 variable names separated by spaces. A list/tuple of str can also
1487 1487 be used to give the variable names. If just the variable names are
1488 1488 give (list/tuple/str) then the variable values looked up in the
1489 1489 callers frame.
1490 1490 interactive : bool
1491 1491 If True (default), the variables will be listed with the ``who``
1492 1492 magic.
1493 1493 """
1494 1494 vdict = None
1495 1495
1496 1496 # We need a dict of name/value pairs to do namespace updates.
1497 1497 if isinstance(variables, dict):
1498 1498 vdict = variables
1499 1499 elif isinstance(variables, (str, list, tuple)):
1500 1500 if isinstance(variables, str):
1501 1501 vlist = variables.split()
1502 1502 else:
1503 1503 vlist = variables
1504 1504 vdict = {}
1505 1505 cf = sys._getframe(1)
1506 1506 for name in vlist:
1507 1507 try:
1508 1508 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1509 1509 except:
1510 1510 print('Could not get variable %s from %s' %
1511 1511 (name,cf.f_code.co_name))
1512 1512 else:
1513 1513 raise ValueError('variables must be a dict/str/list/tuple')
1514 1514
1515 1515 # Propagate variables to user namespace
1516 1516 self.user_ns.update(vdict)
1517 1517
1518 1518 # And configure interactive visibility
1519 1519 user_ns_hidden = self.user_ns_hidden
1520 1520 if interactive:
1521 1521 for name in vdict:
1522 1522 user_ns_hidden.pop(name, None)
1523 1523 else:
1524 1524 user_ns_hidden.update(vdict)
1525 1525
1526 1526 def drop_by_id(self, variables):
1527 1527 """Remove a dict of variables from the user namespace, if they are the
1528 1528 same as the values in the dictionary.
1529 1529
1530 1530 This is intended for use by extensions: variables that they've added can
1531 1531 be taken back out if they are unloaded, without removing any that the
1532 1532 user has overwritten.
1533 1533
1534 1534 Parameters
1535 1535 ----------
1536 1536 variables : dict
1537 1537 A dictionary mapping object names (as strings) to the objects.
1538 1538 """
1539 1539 for name, obj in variables.items():
1540 1540 if name in self.user_ns and self.user_ns[name] is obj:
1541 1541 del self.user_ns[name]
1542 1542 self.user_ns_hidden.pop(name, None)
1543 1543
1544 1544 #-------------------------------------------------------------------------
1545 1545 # Things related to object introspection
1546 1546 #-------------------------------------------------------------------------
1547 1547
1548 1548 def _ofind(self, oname, namespaces=None):
1549 1549 """Find an object in the available namespaces.
1550 1550
1551 1551 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1552 1552
1553 1553 Has special code to detect magic functions.
1554 1554 """
1555 1555 oname = oname.strip()
1556 1556 raw_parts = oname.split(".")
1557 1557 parts = []
1558 1558 parts_ok = True
1559 1559 for p in raw_parts:
1560 1560 if p.endswith("]"):
1561 1561 var, *indices = p.split("[")
1562 1562 if not var.isidentifier():
1563 1563 parts_ok = False
1564 1564 break
1565 1565 parts.append(var)
1566 1566 for ind in indices:
1567 1567 if ind[-1] != "]" and not is_integer_string(ind[:-1]):
1568 1568 parts_ok = False
1569 1569 break
1570 1570 parts.append(ind[:-1])
1571 1571 continue
1572 1572
1573 1573 if not p.isidentifier():
1574 1574 parts_ok = False
1575 1575 parts.append(p)
1576 1576
1577 1577 if (
1578 1578 not oname.startswith(ESC_MAGIC)
1579 1579 and not oname.startswith(ESC_MAGIC2)
1580 1580 and not parts_ok
1581 1581 ):
1582 1582 return {"found": False}
1583 1583
1584 1584 if namespaces is None:
1585 1585 # Namespaces to search in:
1586 1586 # Put them in a list. The order is important so that we
1587 1587 # find things in the same order that Python finds them.
1588 1588 namespaces = [ ('Interactive', self.user_ns),
1589 1589 ('Interactive (global)', self.user_global_ns),
1590 1590 ('Python builtin', builtin_mod.__dict__),
1591 1591 ]
1592 1592
1593 1593 ismagic = False
1594 1594 isalias = False
1595 1595 found = False
1596 1596 ospace = None
1597 1597 parent = None
1598 1598 obj = None
1599 1599
1600 1600
1601 1601 # Look for the given name by splitting it in parts. If the head is
1602 1602 # found, then we look for all the remaining parts as members, and only
1603 1603 # declare success if we can find them all.
1604 1604 oname_parts = parts
1605 1605 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1606 1606 for nsname,ns in namespaces:
1607 1607 try:
1608 1608 obj = ns[oname_head]
1609 1609 except KeyError:
1610 1610 continue
1611 1611 else:
1612 1612 for idx, part in enumerate(oname_rest):
1613 1613 try:
1614 1614 parent = obj
1615 1615 # The last part is looked up in a special way to avoid
1616 1616 # descriptor invocation as it may raise or have side
1617 1617 # effects.
1618 1618 if idx == len(oname_rest) - 1:
1619 1619 obj = self._getattr_property(obj, part)
1620 1620 else:
1621 1621 if is_integer_string(part):
1622 1622 obj = obj[int(part)]
1623 1623 else:
1624 1624 obj = getattr(obj, part)
1625 1625 except:
1626 1626 # Blanket except b/c some badly implemented objects
1627 1627 # allow __getattr__ to raise exceptions other than
1628 1628 # AttributeError, which then crashes IPython.
1629 1629 break
1630 1630 else:
1631 1631 # If we finish the for loop (no break), we got all members
1632 1632 found = True
1633 1633 ospace = nsname
1634 1634 break # namespace loop
1635 1635
1636 1636 # Try to see if it's magic
1637 1637 if not found:
1638 1638 obj = None
1639 1639 if oname.startswith(ESC_MAGIC2):
1640 1640 oname = oname.lstrip(ESC_MAGIC2)
1641 1641 obj = self.find_cell_magic(oname)
1642 1642 elif oname.startswith(ESC_MAGIC):
1643 1643 oname = oname.lstrip(ESC_MAGIC)
1644 1644 obj = self.find_line_magic(oname)
1645 1645 else:
1646 1646 # search without prefix, so run? will find %run?
1647 1647 obj = self.find_line_magic(oname)
1648 1648 if obj is None:
1649 1649 obj = self.find_cell_magic(oname)
1650 1650 if obj is not None:
1651 1651 found = True
1652 1652 ospace = 'IPython internal'
1653 1653 ismagic = True
1654 1654 isalias = isinstance(obj, Alias)
1655 1655
1656 1656 # Last try: special-case some literals like '', [], {}, etc:
1657 1657 if not found and oname_head in ["''",'""','[]','{}','()']:
1658 1658 obj = eval(oname_head)
1659 1659 found = True
1660 1660 ospace = 'Interactive'
1661 1661
1662 1662 return {
1663 1663 'obj':obj,
1664 1664 'found':found,
1665 1665 'parent':parent,
1666 1666 'ismagic':ismagic,
1667 1667 'isalias':isalias,
1668 1668 'namespace':ospace
1669 1669 }
1670 1670
1671 1671 @staticmethod
1672 1672 def _getattr_property(obj, attrname):
1673 1673 """Property-aware getattr to use in object finding.
1674 1674
1675 1675 If attrname represents a property, return it unevaluated (in case it has
1676 1676 side effects or raises an error.
1677 1677
1678 1678 """
1679 1679 if not isinstance(obj, type):
1680 1680 try:
1681 1681 # `getattr(type(obj), attrname)` is not guaranteed to return
1682 1682 # `obj`, but does so for property:
1683 1683 #
1684 1684 # property.__get__(self, None, cls) -> self
1685 1685 #
1686 1686 # The universal alternative is to traverse the mro manually
1687 1687 # searching for attrname in class dicts.
1688 1688 if is_integer_string(attrname):
1689 1689 return obj[int(attrname)]
1690 1690 else:
1691 1691 attr = getattr(type(obj), attrname)
1692 1692 except AttributeError:
1693 1693 pass
1694 1694 else:
1695 1695 # This relies on the fact that data descriptors (with both
1696 1696 # __get__ & __set__ magic methods) take precedence over
1697 1697 # instance-level attributes:
1698 1698 #
1699 1699 # class A(object):
1700 1700 # @property
1701 1701 # def foobar(self): return 123
1702 1702 # a = A()
1703 1703 # a.__dict__['foobar'] = 345
1704 1704 # a.foobar # == 123
1705 1705 #
1706 1706 # So, a property may be returned right away.
1707 1707 if isinstance(attr, property):
1708 1708 return attr
1709 1709
1710 1710 # Nothing helped, fall back.
1711 1711 return getattr(obj, attrname)
1712 1712
1713 1713 def _object_find(self, oname, namespaces=None):
1714 1714 """Find an object and return a struct with info about it."""
1715 1715 return Struct(self._ofind(oname, namespaces))
1716 1716
1717 1717 def _inspect(self, meth, oname, namespaces=None, **kw):
1718 1718 """Generic interface to the inspector system.
1719 1719
1720 1720 This function is meant to be called by pdef, pdoc & friends.
1721 1721 """
1722 1722 info = self._object_find(oname, namespaces)
1723 1723 docformat = (
1724 1724 sphinxify(self.object_inspect(oname)) if self.sphinxify_docstring else None
1725 1725 )
1726 1726 if info.found:
1727 1727 pmethod = getattr(self.inspector, meth)
1728 1728 # TODO: only apply format_screen to the plain/text repr of the mime
1729 1729 # bundle.
1730 1730 formatter = format_screen if info.ismagic else docformat
1731 1731 if meth == 'pdoc':
1732 1732 pmethod(info.obj, oname, formatter)
1733 1733 elif meth == 'pinfo':
1734 1734 pmethod(
1735 1735 info.obj,
1736 1736 oname,
1737 1737 formatter,
1738 1738 info,
1739 1739 enable_html_pager=self.enable_html_pager,
1740 1740 **kw,
1741 1741 )
1742 1742 else:
1743 1743 pmethod(info.obj, oname)
1744 1744 else:
1745 1745 print('Object `%s` not found.' % oname)
1746 1746 return 'not found' # so callers can take other action
1747 1747
1748 1748 def object_inspect(self, oname, detail_level=0):
1749 1749 """Get object info about oname"""
1750 1750 with self.builtin_trap:
1751 1751 info = self._object_find(oname)
1752 1752 if info.found:
1753 1753 return self.inspector.info(info.obj, oname, info=info,
1754 1754 detail_level=detail_level
1755 1755 )
1756 1756 else:
1757 1757 return oinspect.object_info(name=oname, found=False)
1758 1758
1759 1759 def object_inspect_text(self, oname, detail_level=0):
1760 1760 """Get object info as formatted text"""
1761 1761 return self.object_inspect_mime(oname, detail_level)['text/plain']
1762 1762
1763 1763 def object_inspect_mime(self, oname, detail_level=0, omit_sections=()):
1764 1764 """Get object info as a mimebundle of formatted representations.
1765 1765
1766 1766 A mimebundle is a dictionary, keyed by mime-type.
1767 1767 It must always have the key `'text/plain'`.
1768 1768 """
1769 1769 with self.builtin_trap:
1770 1770 info = self._object_find(oname)
1771 1771 if info.found:
1772 1772 docformat = (
1773 1773 sphinxify(self.object_inspect(oname))
1774 1774 if self.sphinxify_docstring
1775 1775 else None
1776 1776 )
1777 1777 return self.inspector._get_info(
1778 1778 info.obj,
1779 1779 oname,
1780 1780 info=info,
1781 1781 detail_level=detail_level,
1782 1782 formatter=docformat,
1783 1783 omit_sections=omit_sections,
1784 1784 )
1785 1785 else:
1786 1786 raise KeyError(oname)
1787 1787
1788 1788 #-------------------------------------------------------------------------
1789 1789 # Things related to history management
1790 1790 #-------------------------------------------------------------------------
1791 1791
1792 1792 def init_history(self):
1793 1793 """Sets up the command history, and starts regular autosaves."""
1794 1794 self.history_manager = HistoryManager(shell=self, parent=self)
1795 1795 self.configurables.append(self.history_manager)
1796 1796
1797 1797 #-------------------------------------------------------------------------
1798 1798 # Things related to exception handling and tracebacks (not debugging)
1799 1799 #-------------------------------------------------------------------------
1800 1800
1801 1801 debugger_cls = InterruptiblePdb
1802 1802
1803 1803 def init_traceback_handlers(self, custom_exceptions):
1804 1804 # Syntax error handler.
1805 1805 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self)
1806 1806
1807 1807 # The interactive one is initialized with an offset, meaning we always
1808 1808 # want to remove the topmost item in the traceback, which is our own
1809 1809 # internal code. Valid modes: ['Plain','Context','Verbose','Minimal']
1810 1810 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1811 1811 color_scheme='NoColor',
1812 1812 tb_offset = 1,
1813 check_cache=check_linecache_ipython,
1814 1813 debugger_cls=self.debugger_cls, parent=self)
1815 1814
1816 1815 # The instance will store a pointer to the system-wide exception hook,
1817 1816 # so that runtime code (such as magics) can access it. This is because
1818 1817 # during the read-eval loop, it may get temporarily overwritten.
1819 1818 self.sys_excepthook = sys.excepthook
1820 1819
1821 1820 # and add any custom exception handlers the user may have specified
1822 1821 self.set_custom_exc(*custom_exceptions)
1823 1822
1824 1823 # Set the exception mode
1825 1824 self.InteractiveTB.set_mode(mode=self.xmode)
1826 1825
1827 1826 def set_custom_exc(self, exc_tuple, handler):
1828 1827 """set_custom_exc(exc_tuple, handler)
1829 1828
1830 1829 Set a custom exception handler, which will be called if any of the
1831 1830 exceptions in exc_tuple occur in the mainloop (specifically, in the
1832 1831 run_code() method).
1833 1832
1834 1833 Parameters
1835 1834 ----------
1836 1835 exc_tuple : tuple of exception classes
1837 1836 A *tuple* of exception classes, for which to call the defined
1838 1837 handler. It is very important that you use a tuple, and NOT A
1839 1838 LIST here, because of the way Python's except statement works. If
1840 1839 you only want to trap a single exception, use a singleton tuple::
1841 1840
1842 1841 exc_tuple == (MyCustomException,)
1843 1842
1844 1843 handler : callable
1845 1844 handler must have the following signature::
1846 1845
1847 1846 def my_handler(self, etype, value, tb, tb_offset=None):
1848 1847 ...
1849 1848 return structured_traceback
1850 1849
1851 1850 Your handler must return a structured traceback (a list of strings),
1852 1851 or None.
1853 1852
1854 1853 This will be made into an instance method (via types.MethodType)
1855 1854 of IPython itself, and it will be called if any of the exceptions
1856 1855 listed in the exc_tuple are caught. If the handler is None, an
1857 1856 internal basic one is used, which just prints basic info.
1858 1857
1859 1858 To protect IPython from crashes, if your handler ever raises an
1860 1859 exception or returns an invalid result, it will be immediately
1861 1860 disabled.
1862 1861
1863 1862 Notes
1864 1863 -----
1865 1864 WARNING: by putting in your own exception handler into IPython's main
1866 1865 execution loop, you run a very good chance of nasty crashes. This
1867 1866 facility should only be used if you really know what you are doing.
1868 1867 """
1869 1868
1870 1869 if not isinstance(exc_tuple, tuple):
1871 1870 raise TypeError("The custom exceptions must be given as a tuple.")
1872 1871
1873 1872 def dummy_handler(self, etype, value, tb, tb_offset=None):
1874 1873 print('*** Simple custom exception handler ***')
1875 1874 print('Exception type :', etype)
1876 1875 print('Exception value:', value)
1877 1876 print('Traceback :', tb)
1878 1877
1879 1878 def validate_stb(stb):
1880 1879 """validate structured traceback return type
1881 1880
1882 1881 return type of CustomTB *should* be a list of strings, but allow
1883 1882 single strings or None, which are harmless.
1884 1883
1885 1884 This function will *always* return a list of strings,
1886 1885 and will raise a TypeError if stb is inappropriate.
1887 1886 """
1888 1887 msg = "CustomTB must return list of strings, not %r" % stb
1889 1888 if stb is None:
1890 1889 return []
1891 1890 elif isinstance(stb, str):
1892 1891 return [stb]
1893 1892 elif not isinstance(stb, list):
1894 1893 raise TypeError(msg)
1895 1894 # it's a list
1896 1895 for line in stb:
1897 1896 # check every element
1898 1897 if not isinstance(line, str):
1899 1898 raise TypeError(msg)
1900 1899 return stb
1901 1900
1902 1901 if handler is None:
1903 1902 wrapped = dummy_handler
1904 1903 else:
1905 1904 def wrapped(self,etype,value,tb,tb_offset=None):
1906 1905 """wrap CustomTB handler, to protect IPython from user code
1907 1906
1908 1907 This makes it harder (but not impossible) for custom exception
1909 1908 handlers to crash IPython.
1910 1909 """
1911 1910 try:
1912 1911 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1913 1912 return validate_stb(stb)
1914 1913 except:
1915 1914 # clear custom handler immediately
1916 1915 self.set_custom_exc((), None)
1917 1916 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1918 1917 # show the exception in handler first
1919 1918 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1920 1919 print(self.InteractiveTB.stb2text(stb))
1921 1920 print("The original exception:")
1922 1921 stb = self.InteractiveTB.structured_traceback(
1923 1922 (etype,value,tb), tb_offset=tb_offset
1924 1923 )
1925 1924 return stb
1926 1925
1927 1926 self.CustomTB = types.MethodType(wrapped,self)
1928 1927 self.custom_exceptions = exc_tuple
1929 1928
1930 1929 def excepthook(self, etype, value, tb):
1931 1930 """One more defense for GUI apps that call sys.excepthook.
1932 1931
1933 1932 GUI frameworks like wxPython trap exceptions and call
1934 1933 sys.excepthook themselves. I guess this is a feature that
1935 1934 enables them to keep running after exceptions that would
1936 1935 otherwise kill their mainloop. This is a bother for IPython
1937 1936 which expects to catch all of the program exceptions with a try:
1938 1937 except: statement.
1939 1938
1940 1939 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1941 1940 any app directly invokes sys.excepthook, it will look to the user like
1942 1941 IPython crashed. In order to work around this, we can disable the
1943 1942 CrashHandler and replace it with this excepthook instead, which prints a
1944 1943 regular traceback using our InteractiveTB. In this fashion, apps which
1945 1944 call sys.excepthook will generate a regular-looking exception from
1946 1945 IPython, and the CrashHandler will only be triggered by real IPython
1947 1946 crashes.
1948 1947
1949 1948 This hook should be used sparingly, only in places which are not likely
1950 1949 to be true IPython errors.
1951 1950 """
1952 1951 self.showtraceback((etype, value, tb), tb_offset=0)
1953 1952
1954 1953 def _get_exc_info(self, exc_tuple=None):
1955 1954 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1956 1955
1957 1956 Ensures sys.last_type,value,traceback hold the exc_info we found,
1958 1957 from whichever source.
1959 1958
1960 1959 raises ValueError if none of these contain any information
1961 1960 """
1962 1961 if exc_tuple is None:
1963 1962 etype, value, tb = sys.exc_info()
1964 1963 else:
1965 1964 etype, value, tb = exc_tuple
1966 1965
1967 1966 if etype is None:
1968 1967 if hasattr(sys, 'last_type'):
1969 1968 etype, value, tb = sys.last_type, sys.last_value, \
1970 1969 sys.last_traceback
1971 1970
1972 1971 if etype is None:
1973 1972 raise ValueError("No exception to find")
1974 1973
1975 1974 # Now store the exception info in sys.last_type etc.
1976 1975 # WARNING: these variables are somewhat deprecated and not
1977 1976 # necessarily safe to use in a threaded environment, but tools
1978 1977 # like pdb depend on their existence, so let's set them. If we
1979 1978 # find problems in the field, we'll need to revisit their use.
1980 1979 sys.last_type = etype
1981 1980 sys.last_value = value
1982 1981 sys.last_traceback = tb
1983 1982
1984 1983 return etype, value, tb
1985 1984
1986 1985 def show_usage_error(self, exc):
1987 1986 """Show a short message for UsageErrors
1988 1987
1989 1988 These are special exceptions that shouldn't show a traceback.
1990 1989 """
1991 1990 print("UsageError: %s" % exc, file=sys.stderr)
1992 1991
1993 1992 def get_exception_only(self, exc_tuple=None):
1994 1993 """
1995 1994 Return as a string (ending with a newline) the exception that
1996 1995 just occurred, without any traceback.
1997 1996 """
1998 1997 etype, value, tb = self._get_exc_info(exc_tuple)
1999 1998 msg = traceback.format_exception_only(etype, value)
2000 1999 return ''.join(msg)
2001 2000
2002 2001 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
2003 2002 exception_only=False, running_compiled_code=False):
2004 2003 """Display the exception that just occurred.
2005 2004
2006 2005 If nothing is known about the exception, this is the method which
2007 2006 should be used throughout the code for presenting user tracebacks,
2008 2007 rather than directly invoking the InteractiveTB object.
2009 2008
2010 2009 A specific showsyntaxerror() also exists, but this method can take
2011 2010 care of calling it if needed, so unless you are explicitly catching a
2012 2011 SyntaxError exception, don't try to analyze the stack manually and
2013 2012 simply call this method."""
2014 2013
2015 2014 try:
2016 2015 try:
2017 2016 etype, value, tb = self._get_exc_info(exc_tuple)
2018 2017 except ValueError:
2019 2018 print('No traceback available to show.', file=sys.stderr)
2020 2019 return
2021 2020
2022 2021 if issubclass(etype, SyntaxError):
2023 2022 # Though this won't be called by syntax errors in the input
2024 2023 # line, there may be SyntaxError cases with imported code.
2025 2024 self.showsyntaxerror(filename, running_compiled_code)
2026 2025 elif etype is UsageError:
2027 2026 self.show_usage_error(value)
2028 2027 else:
2029 2028 if exception_only:
2030 2029 stb = ['An exception has occurred, use %tb to see '
2031 2030 'the full traceback.\n']
2032 2031 stb.extend(self.InteractiveTB.get_exception_only(etype,
2033 2032 value))
2034 2033 else:
2035 2034 try:
2036 2035 # Exception classes can customise their traceback - we
2037 2036 # use this in IPython.parallel for exceptions occurring
2038 2037 # in the engines. This should return a list of strings.
2039 2038 if hasattr(value, "_render_traceback_"):
2040 2039 stb = value._render_traceback_()
2041 2040 else:
2042 2041 stb = self.InteractiveTB.structured_traceback(
2043 2042 etype, value, tb, tb_offset=tb_offset
2044 2043 )
2045 2044
2046 2045 except Exception:
2047 2046 print(
2048 2047 "Unexpected exception formatting exception. Falling back to standard exception"
2049 2048 )
2050 2049 traceback.print_exc()
2051 2050 return None
2052 2051
2053 2052 self._showtraceback(etype, value, stb)
2054 2053 if self.call_pdb:
2055 2054 # drop into debugger
2056 2055 self.debugger(force=True)
2057 2056 return
2058 2057
2059 2058 # Actually show the traceback
2060 2059 self._showtraceback(etype, value, stb)
2061 2060
2062 2061 except KeyboardInterrupt:
2063 2062 print('\n' + self.get_exception_only(), file=sys.stderr)
2064 2063
2065 2064 def _showtraceback(self, etype, evalue, stb: str):
2066 2065 """Actually show a traceback.
2067 2066
2068 2067 Subclasses may override this method to put the traceback on a different
2069 2068 place, like a side channel.
2070 2069 """
2071 2070 val = self.InteractiveTB.stb2text(stb)
2072 2071 try:
2073 2072 print(val)
2074 2073 except UnicodeEncodeError:
2075 2074 print(val.encode("utf-8", "backslashreplace").decode())
2076 2075
2077 2076 def showsyntaxerror(self, filename=None, running_compiled_code=False):
2078 2077 """Display the syntax error that just occurred.
2079 2078
2080 2079 This doesn't display a stack trace because there isn't one.
2081 2080
2082 2081 If a filename is given, it is stuffed in the exception instead
2083 2082 of what was there before (because Python's parser always uses
2084 2083 "<string>" when reading from a string).
2085 2084
2086 2085 If the syntax error occurred when running a compiled code (i.e. running_compile_code=True),
2087 2086 longer stack trace will be displayed.
2088 2087 """
2089 2088 etype, value, last_traceback = self._get_exc_info()
2090 2089
2091 2090 if filename and issubclass(etype, SyntaxError):
2092 2091 try:
2093 2092 value.filename = filename
2094 2093 except:
2095 2094 # Not the format we expect; leave it alone
2096 2095 pass
2097 2096
2098 2097 # If the error occurred when executing compiled code, we should provide full stacktrace.
2099 2098 elist = traceback.extract_tb(last_traceback) if running_compiled_code else []
2100 2099 stb = self.SyntaxTB.structured_traceback(etype, value, elist)
2101 2100 self._showtraceback(etype, value, stb)
2102 2101
2103 2102 # This is overridden in TerminalInteractiveShell to show a message about
2104 2103 # the %paste magic.
2105 2104 def showindentationerror(self):
2106 2105 """Called by _run_cell when there's an IndentationError in code entered
2107 2106 at the prompt.
2108 2107
2109 2108 This is overridden in TerminalInteractiveShell to show a message about
2110 2109 the %paste magic."""
2111 2110 self.showsyntaxerror()
2112 2111
2113 2112 @skip_doctest
2114 2113 def set_next_input(self, s, replace=False):
2115 2114 """ Sets the 'default' input string for the next command line.
2116 2115
2117 2116 Example::
2118 2117
2119 2118 In [1]: _ip.set_next_input("Hello Word")
2120 2119 In [2]: Hello Word_ # cursor is here
2121 2120 """
2122 2121 self.rl_next_input = s
2123 2122
2124 2123 def _indent_current_str(self):
2125 2124 """return the current level of indentation as a string"""
2126 2125 return self.input_splitter.get_indent_spaces() * ' '
2127 2126
2128 2127 #-------------------------------------------------------------------------
2129 2128 # Things related to text completion
2130 2129 #-------------------------------------------------------------------------
2131 2130
2132 2131 def init_completer(self):
2133 2132 """Initialize the completion machinery.
2134 2133
2135 2134 This creates completion machinery that can be used by client code,
2136 2135 either interactively in-process (typically triggered by the readline
2137 2136 library), programmatically (such as in test suites) or out-of-process
2138 2137 (typically over the network by remote frontends).
2139 2138 """
2140 2139 from IPython.core.completer import IPCompleter
2141 2140 from IPython.core.completerlib import (
2142 2141 cd_completer,
2143 2142 magic_run_completer,
2144 2143 module_completer,
2145 2144 reset_completer,
2146 2145 )
2147 2146
2148 2147 self.Completer = IPCompleter(shell=self,
2149 2148 namespace=self.user_ns,
2150 2149 global_namespace=self.user_global_ns,
2151 2150 parent=self,
2152 2151 )
2153 2152 self.configurables.append(self.Completer)
2154 2153
2155 2154 # Add custom completers to the basic ones built into IPCompleter
2156 2155 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
2157 2156 self.strdispatchers['complete_command'] = sdisp
2158 2157 self.Completer.custom_completers = sdisp
2159 2158
2160 2159 self.set_hook('complete_command', module_completer, str_key = 'import')
2161 2160 self.set_hook('complete_command', module_completer, str_key = 'from')
2162 2161 self.set_hook('complete_command', module_completer, str_key = '%aimport')
2163 2162 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
2164 2163 self.set_hook('complete_command', cd_completer, str_key = '%cd')
2165 2164 self.set_hook('complete_command', reset_completer, str_key = '%reset')
2166 2165
2167 2166 @skip_doctest
2168 2167 def complete(self, text, line=None, cursor_pos=None):
2169 2168 """Return the completed text and a list of completions.
2170 2169
2171 2170 Parameters
2172 2171 ----------
2173 2172 text : string
2174 2173 A string of text to be completed on. It can be given as empty and
2175 2174 instead a line/position pair are given. In this case, the
2176 2175 completer itself will split the line like readline does.
2177 2176 line : string, optional
2178 2177 The complete line that text is part of.
2179 2178 cursor_pos : int, optional
2180 2179 The position of the cursor on the input line.
2181 2180
2182 2181 Returns
2183 2182 -------
2184 2183 text : string
2185 2184 The actual text that was completed.
2186 2185 matches : list
2187 2186 A sorted list with all possible completions.
2188 2187
2189 2188 Notes
2190 2189 -----
2191 2190 The optional arguments allow the completion to take more context into
2192 2191 account, and are part of the low-level completion API.
2193 2192
2194 2193 This is a wrapper around the completion mechanism, similar to what
2195 2194 readline does at the command line when the TAB key is hit. By
2196 2195 exposing it as a method, it can be used by other non-readline
2197 2196 environments (such as GUIs) for text completion.
2198 2197
2199 2198 Examples
2200 2199 --------
2201 2200 In [1]: x = 'hello'
2202 2201
2203 2202 In [2]: _ip.complete('x.l')
2204 2203 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
2205 2204 """
2206 2205
2207 2206 # Inject names into __builtin__ so we can complete on the added names.
2208 2207 with self.builtin_trap:
2209 2208 return self.Completer.complete(text, line, cursor_pos)
2210 2209
2211 2210 def set_custom_completer(self, completer, pos=0) -> None:
2212 2211 """Adds a new custom completer function.
2213 2212
2214 2213 The position argument (defaults to 0) is the index in the completers
2215 2214 list where you want the completer to be inserted.
2216 2215
2217 2216 `completer` should have the following signature::
2218 2217
2219 2218 def completion(self: Completer, text: string) -> List[str]:
2220 2219 raise NotImplementedError
2221 2220
2222 2221 It will be bound to the current Completer instance and pass some text
2223 2222 and return a list with current completions to suggest to the user.
2224 2223 """
2225 2224
2226 2225 newcomp = types.MethodType(completer, self.Completer)
2227 2226 self.Completer.custom_matchers.insert(pos,newcomp)
2228 2227
2229 2228 def set_completer_frame(self, frame=None):
2230 2229 """Set the frame of the completer."""
2231 2230 if frame:
2232 2231 self.Completer.namespace = frame.f_locals
2233 2232 self.Completer.global_namespace = frame.f_globals
2234 2233 else:
2235 2234 self.Completer.namespace = self.user_ns
2236 2235 self.Completer.global_namespace = self.user_global_ns
2237 2236
2238 2237 #-------------------------------------------------------------------------
2239 2238 # Things related to magics
2240 2239 #-------------------------------------------------------------------------
2241 2240
2242 2241 def init_magics(self):
2243 2242 from IPython.core import magics as m
2244 2243 self.magics_manager = magic.MagicsManager(shell=self,
2245 2244 parent=self,
2246 2245 user_magics=m.UserMagics(self))
2247 2246 self.configurables.append(self.magics_manager)
2248 2247
2249 2248 # Expose as public API from the magics manager
2250 2249 self.register_magics = self.magics_manager.register
2251 2250
2252 2251 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2253 2252 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2254 2253 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2255 2254 m.NamespaceMagics, m.OSMagics, m.PackagingMagics,
2256 2255 m.PylabMagics, m.ScriptMagics,
2257 2256 )
2258 2257 self.register_magics(m.AsyncMagics)
2259 2258
2260 2259 # Register Magic Aliases
2261 2260 mman = self.magics_manager
2262 2261 # FIXME: magic aliases should be defined by the Magics classes
2263 2262 # or in MagicsManager, not here
2264 2263 mman.register_alias('ed', 'edit')
2265 2264 mman.register_alias('hist', 'history')
2266 2265 mman.register_alias('rep', 'recall')
2267 2266 mman.register_alias('SVG', 'svg', 'cell')
2268 2267 mman.register_alias('HTML', 'html', 'cell')
2269 2268 mman.register_alias('file', 'writefile', 'cell')
2270 2269
2271 2270 # FIXME: Move the color initialization to the DisplayHook, which
2272 2271 # should be split into a prompt manager and displayhook. We probably
2273 2272 # even need a centralize colors management object.
2274 2273 self.run_line_magic('colors', self.colors)
2275 2274
2276 2275 # Defined here so that it's included in the documentation
2277 2276 @functools.wraps(magic.MagicsManager.register_function)
2278 2277 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2279 2278 self.magics_manager.register_function(
2280 2279 func, magic_kind=magic_kind, magic_name=magic_name
2281 2280 )
2282 2281
2283 2282 def _find_with_lazy_load(self, /, type_, magic_name: str):
2284 2283 """
2285 2284 Try to find a magic potentially lazy-loading it.
2286 2285
2287 2286 Parameters
2288 2287 ----------
2289 2288
2290 2289 type_: "line"|"cell"
2291 2290 the type of magics we are trying to find/lazy load.
2292 2291 magic_name: str
2293 2292 The name of the magic we are trying to find/lazy load
2294 2293
2295 2294
2296 2295 Note that this may have any side effects
2297 2296 """
2298 2297 finder = {"line": self.find_line_magic, "cell": self.find_cell_magic}[type_]
2299 2298 fn = finder(magic_name)
2300 2299 if fn is not None:
2301 2300 return fn
2302 2301 lazy = self.magics_manager.lazy_magics.get(magic_name)
2303 2302 if lazy is None:
2304 2303 return None
2305 2304
2306 2305 self.run_line_magic("load_ext", lazy)
2307 2306 res = finder(magic_name)
2308 2307 return res
2309 2308
2310 2309 def run_line_magic(self, magic_name: str, line, _stack_depth=1):
2311 2310 """Execute the given line magic.
2312 2311
2313 2312 Parameters
2314 2313 ----------
2315 2314 magic_name : str
2316 2315 Name of the desired magic function, without '%' prefix.
2317 2316 line : str
2318 2317 The rest of the input line as a single string.
2319 2318 _stack_depth : int
2320 2319 If run_line_magic() is called from magic() then _stack_depth=2.
2321 2320 This is added to ensure backward compatibility for use of 'get_ipython().magic()'
2322 2321 """
2323 2322 fn = self._find_with_lazy_load("line", magic_name)
2324 2323 if fn is None:
2325 2324 lazy = self.magics_manager.lazy_magics.get(magic_name)
2326 2325 if lazy:
2327 2326 self.run_line_magic("load_ext", lazy)
2328 2327 fn = self.find_line_magic(magic_name)
2329 2328 if fn is None:
2330 2329 cm = self.find_cell_magic(magic_name)
2331 2330 etpl = "Line magic function `%%%s` not found%s."
2332 2331 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2333 2332 'did you mean that instead?)' % magic_name )
2334 2333 raise UsageError(etpl % (magic_name, extra))
2335 2334 else:
2336 2335 # Note: this is the distance in the stack to the user's frame.
2337 2336 # This will need to be updated if the internal calling logic gets
2338 2337 # refactored, or else we'll be expanding the wrong variables.
2339 2338
2340 2339 # Determine stack_depth depending on where run_line_magic() has been called
2341 2340 stack_depth = _stack_depth
2342 2341 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2343 2342 # magic has opted out of var_expand
2344 2343 magic_arg_s = line
2345 2344 else:
2346 2345 magic_arg_s = self.var_expand(line, stack_depth)
2347 2346 # Put magic args in a list so we can call with f(*a) syntax
2348 2347 args = [magic_arg_s]
2349 2348 kwargs = {}
2350 2349 # Grab local namespace if we need it:
2351 2350 if getattr(fn, "needs_local_scope", False):
2352 2351 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2353 2352 with self.builtin_trap:
2354 2353 result = fn(*args, **kwargs)
2355 2354 return result
2356 2355
2357 2356 def get_local_scope(self, stack_depth):
2358 2357 """Get local scope at given stack depth.
2359 2358
2360 2359 Parameters
2361 2360 ----------
2362 2361 stack_depth : int
2363 2362 Depth relative to calling frame
2364 2363 """
2365 2364 return sys._getframe(stack_depth + 1).f_locals
2366 2365
2367 2366 def run_cell_magic(self, magic_name, line, cell):
2368 2367 """Execute the given cell magic.
2369 2368
2370 2369 Parameters
2371 2370 ----------
2372 2371 magic_name : str
2373 2372 Name of the desired magic function, without '%' prefix.
2374 2373 line : str
2375 2374 The rest of the first input line as a single string.
2376 2375 cell : str
2377 2376 The body of the cell as a (possibly multiline) string.
2378 2377 """
2379 2378 fn = self._find_with_lazy_load("cell", magic_name)
2380 2379 if fn is None:
2381 2380 lm = self.find_line_magic(magic_name)
2382 2381 etpl = "Cell magic `%%{0}` not found{1}."
2383 2382 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2384 2383 'did you mean that instead?)'.format(magic_name))
2385 2384 raise UsageError(etpl.format(magic_name, extra))
2386 2385 elif cell == '':
2387 2386 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2388 2387 if self.find_line_magic(magic_name) is not None:
2389 2388 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2390 2389 raise UsageError(message)
2391 2390 else:
2392 2391 # Note: this is the distance in the stack to the user's frame.
2393 2392 # This will need to be updated if the internal calling logic gets
2394 2393 # refactored, or else we'll be expanding the wrong variables.
2395 2394 stack_depth = 2
2396 2395 if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False):
2397 2396 # magic has opted out of var_expand
2398 2397 magic_arg_s = line
2399 2398 else:
2400 2399 magic_arg_s = self.var_expand(line, stack_depth)
2401 2400 kwargs = {}
2402 2401 if getattr(fn, "needs_local_scope", False):
2403 2402 kwargs['local_ns'] = self.user_ns
2404 2403
2405 2404 with self.builtin_trap:
2406 2405 args = (magic_arg_s, cell)
2407 2406 result = fn(*args, **kwargs)
2408 2407 return result
2409 2408
2410 2409 def find_line_magic(self, magic_name):
2411 2410 """Find and return a line magic by name.
2412 2411
2413 2412 Returns None if the magic isn't found."""
2414 2413 return self.magics_manager.magics['line'].get(magic_name)
2415 2414
2416 2415 def find_cell_magic(self, magic_name):
2417 2416 """Find and return a cell magic by name.
2418 2417
2419 2418 Returns None if the magic isn't found."""
2420 2419 return self.magics_manager.magics['cell'].get(magic_name)
2421 2420
2422 2421 def find_magic(self, magic_name, magic_kind='line'):
2423 2422 """Find and return a magic of the given type by name.
2424 2423
2425 2424 Returns None if the magic isn't found."""
2426 2425 return self.magics_manager.magics[magic_kind].get(magic_name)
2427 2426
2428 2427 def magic(self, arg_s):
2429 2428 """
2430 2429 DEPRECATED
2431 2430
2432 2431 Deprecated since IPython 0.13 (warning added in
2433 2432 8.1), use run_line_magic(magic_name, parameter_s).
2434 2433
2435 2434 Call a magic function by name.
2436 2435
2437 2436 Input: a string containing the name of the magic function to call and
2438 2437 any additional arguments to be passed to the magic.
2439 2438
2440 2439 magic('name -opt foo bar') is equivalent to typing at the ipython
2441 2440 prompt:
2442 2441
2443 2442 In[1]: %name -opt foo bar
2444 2443
2445 2444 To call a magic without arguments, simply use magic('name').
2446 2445
2447 2446 This provides a proper Python function to call IPython's magics in any
2448 2447 valid Python code you can type at the interpreter, including loops and
2449 2448 compound statements.
2450 2449 """
2451 2450 warnings.warn(
2452 2451 "`magic(...)` is deprecated since IPython 0.13 (warning added in "
2453 2452 "8.1), use run_line_magic(magic_name, parameter_s).",
2454 2453 DeprecationWarning,
2455 2454 stacklevel=2,
2456 2455 )
2457 2456 # TODO: should we issue a loud deprecation warning here?
2458 2457 magic_name, _, magic_arg_s = arg_s.partition(' ')
2459 2458 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2460 2459 return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
2461 2460
2462 2461 #-------------------------------------------------------------------------
2463 2462 # Things related to macros
2464 2463 #-------------------------------------------------------------------------
2465 2464
2466 2465 def define_macro(self, name, themacro):
2467 2466 """Define a new macro
2468 2467
2469 2468 Parameters
2470 2469 ----------
2471 2470 name : str
2472 2471 The name of the macro.
2473 2472 themacro : str or Macro
2474 2473 The action to do upon invoking the macro. If a string, a new
2475 2474 Macro object is created by passing the string to it.
2476 2475 """
2477 2476
2478 2477 from IPython.core import macro
2479 2478
2480 2479 if isinstance(themacro, str):
2481 2480 themacro = macro.Macro(themacro)
2482 2481 if not isinstance(themacro, macro.Macro):
2483 2482 raise ValueError('A macro must be a string or a Macro instance.')
2484 2483 self.user_ns[name] = themacro
2485 2484
2486 2485 #-------------------------------------------------------------------------
2487 2486 # Things related to the running of system commands
2488 2487 #-------------------------------------------------------------------------
2489 2488
2490 2489 def system_piped(self, cmd):
2491 2490 """Call the given cmd in a subprocess, piping stdout/err
2492 2491
2493 2492 Parameters
2494 2493 ----------
2495 2494 cmd : str
2496 2495 Command to execute (can not end in '&', as background processes are
2497 2496 not supported. Should not be a command that expects input
2498 2497 other than simple text.
2499 2498 """
2500 2499 if cmd.rstrip().endswith('&'):
2501 2500 # this is *far* from a rigorous test
2502 2501 # We do not support backgrounding processes because we either use
2503 2502 # pexpect or pipes to read from. Users can always just call
2504 2503 # os.system() or use ip.system=ip.system_raw
2505 2504 # if they really want a background process.
2506 2505 raise OSError("Background processes not supported.")
2507 2506
2508 2507 # we explicitly do NOT return the subprocess status code, because
2509 2508 # a non-None value would trigger :func:`sys.displayhook` calls.
2510 2509 # Instead, we store the exit_code in user_ns.
2511 2510 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2512 2511
2513 2512 def system_raw(self, cmd):
2514 2513 """Call the given cmd in a subprocess using os.system on Windows or
2515 2514 subprocess.call using the system shell on other platforms.
2516 2515
2517 2516 Parameters
2518 2517 ----------
2519 2518 cmd : str
2520 2519 Command to execute.
2521 2520 """
2522 2521 cmd = self.var_expand(cmd, depth=1)
2523 2522 # warn if there is an IPython magic alternative.
2524 2523 main_cmd = cmd.split()[0]
2525 2524 has_magic_alternatives = ("pip", "conda", "cd")
2526 2525
2527 2526 if main_cmd in has_magic_alternatives:
2528 2527 warnings.warn(
2529 2528 (
2530 2529 "You executed the system command !{0} which may not work "
2531 2530 "as expected. Try the IPython magic %{0} instead."
2532 2531 ).format(main_cmd)
2533 2532 )
2534 2533
2535 2534 # protect os.system from UNC paths on Windows, which it can't handle:
2536 2535 if sys.platform == 'win32':
2537 2536 from IPython.utils._process_win32 import AvoidUNCPath
2538 2537 with AvoidUNCPath() as path:
2539 2538 if path is not None:
2540 2539 cmd = '"pushd %s &&"%s' % (path, cmd)
2541 2540 try:
2542 2541 ec = os.system(cmd)
2543 2542 except KeyboardInterrupt:
2544 2543 print('\n' + self.get_exception_only(), file=sys.stderr)
2545 2544 ec = -2
2546 2545 else:
2547 2546 # For posix the result of the subprocess.call() below is an exit
2548 2547 # code, which by convention is zero for success, positive for
2549 2548 # program failure. Exit codes above 128 are reserved for signals,
2550 2549 # and the formula for converting a signal to an exit code is usually
2551 2550 # signal_number+128. To more easily differentiate between exit
2552 2551 # codes and signals, ipython uses negative numbers. For instance
2553 2552 # since control-c is signal 2 but exit code 130, ipython's
2554 2553 # _exit_code variable will read -2. Note that some shells like
2555 2554 # csh and fish don't follow sh/bash conventions for exit codes.
2556 2555 executable = os.environ.get('SHELL', None)
2557 2556 try:
2558 2557 # Use env shell instead of default /bin/sh
2559 2558 ec = subprocess.call(cmd, shell=True, executable=executable)
2560 2559 except KeyboardInterrupt:
2561 2560 # intercept control-C; a long traceback is not useful here
2562 2561 print('\n' + self.get_exception_only(), file=sys.stderr)
2563 2562 ec = 130
2564 2563 if ec > 128:
2565 2564 ec = -(ec - 128)
2566 2565
2567 2566 # We explicitly do NOT return the subprocess status code, because
2568 2567 # a non-None value would trigger :func:`sys.displayhook` calls.
2569 2568 # Instead, we store the exit_code in user_ns. Note the semantics
2570 2569 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2571 2570 # but raising SystemExit(_exit_code) will give status 254!
2572 2571 self.user_ns['_exit_code'] = ec
2573 2572
2574 2573 # use piped system by default, because it is better behaved
2575 2574 system = system_piped
2576 2575
2577 2576 def getoutput(self, cmd, split=True, depth=0):
2578 2577 """Get output (possibly including stderr) from a subprocess.
2579 2578
2580 2579 Parameters
2581 2580 ----------
2582 2581 cmd : str
2583 2582 Command to execute (can not end in '&', as background processes are
2584 2583 not supported.
2585 2584 split : bool, optional
2586 2585 If True, split the output into an IPython SList. Otherwise, an
2587 2586 IPython LSString is returned. These are objects similar to normal
2588 2587 lists and strings, with a few convenience attributes for easier
2589 2588 manipulation of line-based output. You can use '?' on them for
2590 2589 details.
2591 2590 depth : int, optional
2592 2591 How many frames above the caller are the local variables which should
2593 2592 be expanded in the command string? The default (0) assumes that the
2594 2593 expansion variables are in the stack frame calling this function.
2595 2594 """
2596 2595 if cmd.rstrip().endswith('&'):
2597 2596 # this is *far* from a rigorous test
2598 2597 raise OSError("Background processes not supported.")
2599 2598 out = getoutput(self.var_expand(cmd, depth=depth+1))
2600 2599 if split:
2601 2600 out = SList(out.splitlines())
2602 2601 else:
2603 2602 out = LSString(out)
2604 2603 return out
2605 2604
2606 2605 #-------------------------------------------------------------------------
2607 2606 # Things related to aliases
2608 2607 #-------------------------------------------------------------------------
2609 2608
2610 2609 def init_alias(self):
2611 2610 self.alias_manager = AliasManager(shell=self, parent=self)
2612 2611 self.configurables.append(self.alias_manager)
2613 2612
2614 2613 #-------------------------------------------------------------------------
2615 2614 # Things related to extensions
2616 2615 #-------------------------------------------------------------------------
2617 2616
2618 2617 def init_extension_manager(self):
2619 2618 self.extension_manager = ExtensionManager(shell=self, parent=self)
2620 2619 self.configurables.append(self.extension_manager)
2621 2620
2622 2621 #-------------------------------------------------------------------------
2623 2622 # Things related to payloads
2624 2623 #-------------------------------------------------------------------------
2625 2624
2626 2625 def init_payload(self):
2627 2626 self.payload_manager = PayloadManager(parent=self)
2628 2627 self.configurables.append(self.payload_manager)
2629 2628
2630 2629 #-------------------------------------------------------------------------
2631 2630 # Things related to the prefilter
2632 2631 #-------------------------------------------------------------------------
2633 2632
2634 2633 def init_prefilter(self):
2635 2634 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2636 2635 self.configurables.append(self.prefilter_manager)
2637 2636 # Ultimately this will be refactored in the new interpreter code, but
2638 2637 # for now, we should expose the main prefilter method (there's legacy
2639 2638 # code out there that may rely on this).
2640 2639 self.prefilter = self.prefilter_manager.prefilter_lines
2641 2640
2642 2641 def auto_rewrite_input(self, cmd):
2643 2642 """Print to the screen the rewritten form of the user's command.
2644 2643
2645 2644 This shows visual feedback by rewriting input lines that cause
2646 2645 automatic calling to kick in, like::
2647 2646
2648 2647 /f x
2649 2648
2650 2649 into::
2651 2650
2652 2651 ------> f(x)
2653 2652
2654 2653 after the user's input prompt. This helps the user understand that the
2655 2654 input line was transformed automatically by IPython.
2656 2655 """
2657 2656 if not self.show_rewritten_input:
2658 2657 return
2659 2658
2660 2659 # This is overridden in TerminalInteractiveShell to use fancy prompts
2661 2660 print("------> " + cmd)
2662 2661
2663 2662 #-------------------------------------------------------------------------
2664 2663 # Things related to extracting values/expressions from kernel and user_ns
2665 2664 #-------------------------------------------------------------------------
2666 2665
2667 2666 def _user_obj_error(self):
2668 2667 """return simple exception dict
2669 2668
2670 2669 for use in user_expressions
2671 2670 """
2672 2671
2673 2672 etype, evalue, tb = self._get_exc_info()
2674 2673 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2675 2674
2676 2675 exc_info = {
2677 2676 "status": "error",
2678 2677 "traceback": stb,
2679 2678 "ename": etype.__name__,
2680 2679 "evalue": py3compat.safe_unicode(evalue),
2681 2680 }
2682 2681
2683 2682 return exc_info
2684 2683
2685 2684 def _format_user_obj(self, obj):
2686 2685 """format a user object to display dict
2687 2686
2688 2687 for use in user_expressions
2689 2688 """
2690 2689
2691 2690 data, md = self.display_formatter.format(obj)
2692 2691 value = {
2693 2692 'status' : 'ok',
2694 2693 'data' : data,
2695 2694 'metadata' : md,
2696 2695 }
2697 2696 return value
2698 2697
2699 2698 def user_expressions(self, expressions):
2700 2699 """Evaluate a dict of expressions in the user's namespace.
2701 2700
2702 2701 Parameters
2703 2702 ----------
2704 2703 expressions : dict
2705 2704 A dict with string keys and string values. The expression values
2706 2705 should be valid Python expressions, each of which will be evaluated
2707 2706 in the user namespace.
2708 2707
2709 2708 Returns
2710 2709 -------
2711 2710 A dict, keyed like the input expressions dict, with the rich mime-typed
2712 2711 display_data of each value.
2713 2712 """
2714 2713 out = {}
2715 2714 user_ns = self.user_ns
2716 2715 global_ns = self.user_global_ns
2717 2716
2718 2717 for key, expr in expressions.items():
2719 2718 try:
2720 2719 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2721 2720 except:
2722 2721 value = self._user_obj_error()
2723 2722 out[key] = value
2724 2723 return out
2725 2724
2726 2725 #-------------------------------------------------------------------------
2727 2726 # Things related to the running of code
2728 2727 #-------------------------------------------------------------------------
2729 2728
2730 2729 def ex(self, cmd):
2731 2730 """Execute a normal python statement in user namespace."""
2732 2731 with self.builtin_trap:
2733 2732 exec(cmd, self.user_global_ns, self.user_ns)
2734 2733
2735 2734 def ev(self, expr):
2736 2735 """Evaluate python expression expr in user namespace.
2737 2736
2738 2737 Returns the result of evaluation
2739 2738 """
2740 2739 with self.builtin_trap:
2741 2740 return eval(expr, self.user_global_ns, self.user_ns)
2742 2741
2743 2742 def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False):
2744 2743 """A safe version of the builtin execfile().
2745 2744
2746 2745 This version will never throw an exception, but instead print
2747 2746 helpful error messages to the screen. This only works on pure
2748 2747 Python files with the .py extension.
2749 2748
2750 2749 Parameters
2751 2750 ----------
2752 2751 fname : string
2753 2752 The name of the file to be executed.
2754 2753 *where : tuple
2755 2754 One or two namespaces, passed to execfile() as (globals,locals).
2756 2755 If only one is given, it is passed as both.
2757 2756 exit_ignore : bool (False)
2758 2757 If True, then silence SystemExit for non-zero status (it is always
2759 2758 silenced for zero status, as it is so common).
2760 2759 raise_exceptions : bool (False)
2761 2760 If True raise exceptions everywhere. Meant for testing.
2762 2761 shell_futures : bool (False)
2763 2762 If True, the code will share future statements with the interactive
2764 2763 shell. It will both be affected by previous __future__ imports, and
2765 2764 any __future__ imports in the code will affect the shell. If False,
2766 2765 __future__ imports are not shared in either direction.
2767 2766
2768 2767 """
2769 2768 fname = Path(fname).expanduser().resolve()
2770 2769
2771 2770 # Make sure we can open the file
2772 2771 try:
2773 2772 with fname.open("rb"):
2774 2773 pass
2775 2774 except:
2776 2775 warn('Could not open file <%s> for safe execution.' % fname)
2777 2776 return
2778 2777
2779 2778 # Find things also in current directory. This is needed to mimic the
2780 2779 # behavior of running a script from the system command line, where
2781 2780 # Python inserts the script's directory into sys.path
2782 2781 dname = str(fname.parent)
2783 2782
2784 2783 with prepended_to_syspath(dname), self.builtin_trap:
2785 2784 try:
2786 2785 glob, loc = (where + (None, ))[:2]
2787 2786 py3compat.execfile(
2788 2787 fname, glob, loc,
2789 2788 self.compile if shell_futures else None)
2790 2789 except SystemExit as status:
2791 2790 # If the call was made with 0 or None exit status (sys.exit(0)
2792 2791 # or sys.exit() ), don't bother showing a traceback, as both of
2793 2792 # these are considered normal by the OS:
2794 2793 # > python -c'import sys;sys.exit(0)'; echo $?
2795 2794 # 0
2796 2795 # > python -c'import sys;sys.exit()'; echo $?
2797 2796 # 0
2798 2797 # For other exit status, we show the exception unless
2799 2798 # explicitly silenced, but only in short form.
2800 2799 if status.code:
2801 2800 if raise_exceptions:
2802 2801 raise
2803 2802 if not exit_ignore:
2804 2803 self.showtraceback(exception_only=True)
2805 2804 except:
2806 2805 if raise_exceptions:
2807 2806 raise
2808 2807 # tb offset is 2 because we wrap execfile
2809 2808 self.showtraceback(tb_offset=2)
2810 2809
2811 2810 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2812 2811 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2813 2812
2814 2813 Parameters
2815 2814 ----------
2816 2815 fname : str
2817 2816 The name of the file to execute. The filename must have a
2818 2817 .ipy or .ipynb extension.
2819 2818 shell_futures : bool (False)
2820 2819 If True, the code will share future statements with the interactive
2821 2820 shell. It will both be affected by previous __future__ imports, and
2822 2821 any __future__ imports in the code will affect the shell. If False,
2823 2822 __future__ imports are not shared in either direction.
2824 2823 raise_exceptions : bool (False)
2825 2824 If True raise exceptions everywhere. Meant for testing.
2826 2825 """
2827 2826 fname = Path(fname).expanduser().resolve()
2828 2827
2829 2828 # Make sure we can open the file
2830 2829 try:
2831 2830 with fname.open("rb"):
2832 2831 pass
2833 2832 except:
2834 2833 warn('Could not open file <%s> for safe execution.' % fname)
2835 2834 return
2836 2835
2837 2836 # Find things also in current directory. This is needed to mimic the
2838 2837 # behavior of running a script from the system command line, where
2839 2838 # Python inserts the script's directory into sys.path
2840 2839 dname = str(fname.parent)
2841 2840
2842 2841 def get_cells():
2843 2842 """generator for sequence of code blocks to run"""
2844 2843 if fname.suffix == ".ipynb":
2845 2844 from nbformat import read
2846 2845 nb = read(fname, as_version=4)
2847 2846 if not nb.cells:
2848 2847 return
2849 2848 for cell in nb.cells:
2850 2849 if cell.cell_type == 'code':
2851 2850 yield cell.source
2852 2851 else:
2853 2852 yield fname.read_text(encoding="utf-8")
2854 2853
2855 2854 with prepended_to_syspath(dname):
2856 2855 try:
2857 2856 for cell in get_cells():
2858 2857 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2859 2858 if raise_exceptions:
2860 2859 result.raise_error()
2861 2860 elif not result.success:
2862 2861 break
2863 2862 except:
2864 2863 if raise_exceptions:
2865 2864 raise
2866 2865 self.showtraceback()
2867 2866 warn('Unknown failure executing file: <%s>' % fname)
2868 2867
2869 2868 def safe_run_module(self, mod_name, where):
2870 2869 """A safe version of runpy.run_module().
2871 2870
2872 2871 This version will never throw an exception, but instead print
2873 2872 helpful error messages to the screen.
2874 2873
2875 2874 `SystemExit` exceptions with status code 0 or None are ignored.
2876 2875
2877 2876 Parameters
2878 2877 ----------
2879 2878 mod_name : string
2880 2879 The name of the module to be executed.
2881 2880 where : dict
2882 2881 The globals namespace.
2883 2882 """
2884 2883 try:
2885 2884 try:
2886 2885 where.update(
2887 2886 runpy.run_module(str(mod_name), run_name="__main__",
2888 2887 alter_sys=True)
2889 2888 )
2890 2889 except SystemExit as status:
2891 2890 if status.code:
2892 2891 raise
2893 2892 except:
2894 2893 self.showtraceback()
2895 2894 warn('Unknown failure executing module: <%s>' % mod_name)
2896 2895
2897 2896 def run_cell(
2898 2897 self,
2899 2898 raw_cell,
2900 2899 store_history=False,
2901 2900 silent=False,
2902 2901 shell_futures=True,
2903 2902 cell_id=None,
2904 2903 ):
2905 2904 """Run a complete IPython cell.
2906 2905
2907 2906 Parameters
2908 2907 ----------
2909 2908 raw_cell : str
2910 2909 The code (including IPython code such as %magic functions) to run.
2911 2910 store_history : bool
2912 2911 If True, the raw and translated cell will be stored in IPython's
2913 2912 history. For user code calling back into IPython's machinery, this
2914 2913 should be set to False.
2915 2914 silent : bool
2916 2915 If True, avoid side-effects, such as implicit displayhooks and
2917 2916 and logging. silent=True forces store_history=False.
2918 2917 shell_futures : bool
2919 2918 If True, the code will share future statements with the interactive
2920 2919 shell. It will both be affected by previous __future__ imports, and
2921 2920 any __future__ imports in the code will affect the shell. If False,
2922 2921 __future__ imports are not shared in either direction.
2923 2922
2924 2923 Returns
2925 2924 -------
2926 2925 result : :class:`ExecutionResult`
2927 2926 """
2928 2927 result = None
2929 2928 try:
2930 2929 result = self._run_cell(
2931 2930 raw_cell, store_history, silent, shell_futures, cell_id
2932 2931 )
2933 2932 finally:
2934 2933 self.events.trigger('post_execute')
2935 2934 if not silent:
2936 2935 self.events.trigger('post_run_cell', result)
2937 2936 return result
2938 2937
2939 2938 def _run_cell(
2940 2939 self,
2941 2940 raw_cell: str,
2942 2941 store_history: bool,
2943 2942 silent: bool,
2944 2943 shell_futures: bool,
2945 2944 cell_id: str,
2946 2945 ) -> ExecutionResult:
2947 2946 """Internal method to run a complete IPython cell."""
2948 2947
2949 2948 # we need to avoid calling self.transform_cell multiple time on the same thing
2950 2949 # so we need to store some results:
2951 2950 preprocessing_exc_tuple = None
2952 2951 try:
2953 2952 transformed_cell = self.transform_cell(raw_cell)
2954 2953 except Exception:
2955 2954 transformed_cell = raw_cell
2956 2955 preprocessing_exc_tuple = sys.exc_info()
2957 2956
2958 2957 assert transformed_cell is not None
2959 2958 coro = self.run_cell_async(
2960 2959 raw_cell,
2961 2960 store_history=store_history,
2962 2961 silent=silent,
2963 2962 shell_futures=shell_futures,
2964 2963 transformed_cell=transformed_cell,
2965 2964 preprocessing_exc_tuple=preprocessing_exc_tuple,
2966 2965 cell_id=cell_id,
2967 2966 )
2968 2967
2969 2968 # run_cell_async is async, but may not actually need an eventloop.
2970 2969 # when this is the case, we want to run it using the pseudo_sync_runner
2971 2970 # so that code can invoke eventloops (for example via the %run , and
2972 2971 # `%paste` magic.
2973 2972 if self.trio_runner:
2974 2973 runner = self.trio_runner
2975 2974 elif self.should_run_async(
2976 2975 raw_cell,
2977 2976 transformed_cell=transformed_cell,
2978 2977 preprocessing_exc_tuple=preprocessing_exc_tuple,
2979 2978 ):
2980 2979 runner = self.loop_runner
2981 2980 else:
2982 2981 runner = _pseudo_sync_runner
2983 2982
2984 2983 try:
2985 2984 return runner(coro)
2986 2985 except BaseException as e:
2987 2986 info = ExecutionInfo(
2988 2987 raw_cell, store_history, silent, shell_futures, cell_id
2989 2988 )
2990 2989 result = ExecutionResult(info)
2991 2990 result.error_in_exec = e
2992 2991 self.showtraceback(running_compiled_code=True)
2993 2992 return result
2994 2993
2995 2994 def should_run_async(
2996 2995 self, raw_cell: str, *, transformed_cell=None, preprocessing_exc_tuple=None
2997 2996 ) -> bool:
2998 2997 """Return whether a cell should be run asynchronously via a coroutine runner
2999 2998
3000 2999 Parameters
3001 3000 ----------
3002 3001 raw_cell : str
3003 3002 The code to be executed
3004 3003
3005 3004 Returns
3006 3005 -------
3007 3006 result: bool
3008 3007 Whether the code needs to be run with a coroutine runner or not
3009 3008 .. versionadded:: 7.0
3010 3009 """
3011 3010 if not self.autoawait:
3012 3011 return False
3013 3012 if preprocessing_exc_tuple is not None:
3014 3013 return False
3015 3014 assert preprocessing_exc_tuple is None
3016 3015 if transformed_cell is None:
3017 3016 warnings.warn(
3018 3017 "`should_run_async` will not call `transform_cell`"
3019 3018 " automatically in the future. Please pass the result to"
3020 3019 " `transformed_cell` argument and any exception that happen"
3021 3020 " during the"
3022 3021 "transform in `preprocessing_exc_tuple` in"
3023 3022 " IPython 7.17 and above.",
3024 3023 DeprecationWarning,
3025 3024 stacklevel=2,
3026 3025 )
3027 3026 try:
3028 3027 cell = self.transform_cell(raw_cell)
3029 3028 except Exception:
3030 3029 # any exception during transform will be raised
3031 3030 # prior to execution
3032 3031 return False
3033 3032 else:
3034 3033 cell = transformed_cell
3035 3034 return _should_be_async(cell)
3036 3035
3037 3036 async def run_cell_async(
3038 3037 self,
3039 3038 raw_cell: str,
3040 3039 store_history=False,
3041 3040 silent=False,
3042 3041 shell_futures=True,
3043 3042 *,
3044 3043 transformed_cell: Optional[str] = None,
3045 3044 preprocessing_exc_tuple: Optional[Any] = None,
3046 3045 cell_id=None,
3047 3046 ) -> ExecutionResult:
3048 3047 """Run a complete IPython cell asynchronously.
3049 3048
3050 3049 Parameters
3051 3050 ----------
3052 3051 raw_cell : str
3053 3052 The code (including IPython code such as %magic functions) to run.
3054 3053 store_history : bool
3055 3054 If True, the raw and translated cell will be stored in IPython's
3056 3055 history. For user code calling back into IPython's machinery, this
3057 3056 should be set to False.
3058 3057 silent : bool
3059 3058 If True, avoid side-effects, such as implicit displayhooks and
3060 3059 and logging. silent=True forces store_history=False.
3061 3060 shell_futures : bool
3062 3061 If True, the code will share future statements with the interactive
3063 3062 shell. It will both be affected by previous __future__ imports, and
3064 3063 any __future__ imports in the code will affect the shell. If False,
3065 3064 __future__ imports are not shared in either direction.
3066 3065 transformed_cell: str
3067 3066 cell that was passed through transformers
3068 3067 preprocessing_exc_tuple:
3069 3068 trace if the transformation failed.
3070 3069
3071 3070 Returns
3072 3071 -------
3073 3072 result : :class:`ExecutionResult`
3074 3073
3075 3074 .. versionadded:: 7.0
3076 3075 """
3077 3076 info = ExecutionInfo(raw_cell, store_history, silent, shell_futures, cell_id)
3078 3077 result = ExecutionResult(info)
3079 3078
3080 3079 if (not raw_cell) or raw_cell.isspace():
3081 3080 self.last_execution_succeeded = True
3082 3081 self.last_execution_result = result
3083 3082 return result
3084 3083
3085 3084 if silent:
3086 3085 store_history = False
3087 3086
3088 3087 if store_history:
3089 3088 result.execution_count = self.execution_count
3090 3089
3091 3090 def error_before_exec(value):
3092 3091 if store_history:
3093 3092 self.execution_count += 1
3094 3093 result.error_before_exec = value
3095 3094 self.last_execution_succeeded = False
3096 3095 self.last_execution_result = result
3097 3096 return result
3098 3097
3099 3098 self.events.trigger('pre_execute')
3100 3099 if not silent:
3101 3100 self.events.trigger('pre_run_cell', info)
3102 3101
3103 3102 if transformed_cell is None:
3104 3103 warnings.warn(
3105 3104 "`run_cell_async` will not call `transform_cell`"
3106 3105 " automatically in the future. Please pass the result to"
3107 3106 " `transformed_cell` argument and any exception that happen"
3108 3107 " during the"
3109 3108 "transform in `preprocessing_exc_tuple` in"
3110 3109 " IPython 7.17 and above.",
3111 3110 DeprecationWarning,
3112 3111 stacklevel=2,
3113 3112 )
3114 3113 # If any of our input transformation (input_transformer_manager or
3115 3114 # prefilter_manager) raises an exception, we store it in this variable
3116 3115 # so that we can display the error after logging the input and storing
3117 3116 # it in the history.
3118 3117 try:
3119 3118 cell = self.transform_cell(raw_cell)
3120 3119 except Exception:
3121 3120 preprocessing_exc_tuple = sys.exc_info()
3122 3121 cell = raw_cell # cell has to exist so it can be stored/logged
3123 3122 else:
3124 3123 preprocessing_exc_tuple = None
3125 3124 else:
3126 3125 if preprocessing_exc_tuple is None:
3127 3126 cell = transformed_cell
3128 3127 else:
3129 3128 cell = raw_cell
3130 3129
3131 3130 # Store raw and processed history
3132 3131 if store_history and raw_cell.strip(" %") != "paste":
3133 3132 self.history_manager.store_inputs(self.execution_count, cell, raw_cell)
3134 3133 if not silent:
3135 3134 self.logger.log(cell, raw_cell)
3136 3135
3137 3136 # Display the exception if input processing failed.
3138 3137 if preprocessing_exc_tuple is not None:
3139 3138 self.showtraceback(preprocessing_exc_tuple)
3140 3139 if store_history:
3141 3140 self.execution_count += 1
3142 3141 return error_before_exec(preprocessing_exc_tuple[1])
3143 3142
3144 3143 # Our own compiler remembers the __future__ environment. If we want to
3145 3144 # run code with a separate __future__ environment, use the default
3146 3145 # compiler
3147 3146 compiler = self.compile if shell_futures else self.compiler_class()
3148 3147
3149 3148 _run_async = False
3150 3149
3151 3150 with self.builtin_trap:
3152 3151 cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell)
3153 3152
3154 3153 with self.display_trap:
3155 3154 # Compile to bytecode
3156 3155 try:
3157 3156 code_ast = compiler.ast_parse(cell, filename=cell_name)
3158 3157 except self.custom_exceptions as e:
3159 3158 etype, value, tb = sys.exc_info()
3160 3159 self.CustomTB(etype, value, tb)
3161 3160 return error_before_exec(e)
3162 3161 except IndentationError as e:
3163 3162 self.showindentationerror()
3164 3163 return error_before_exec(e)
3165 3164 except (OverflowError, SyntaxError, ValueError, TypeError,
3166 3165 MemoryError) as e:
3167 3166 self.showsyntaxerror()
3168 3167 return error_before_exec(e)
3169 3168
3170 3169 # Apply AST transformations
3171 3170 try:
3172 3171 code_ast = self.transform_ast(code_ast)
3173 3172 except InputRejected as e:
3174 3173 self.showtraceback()
3175 3174 return error_before_exec(e)
3176 3175
3177 3176 # Give the displayhook a reference to our ExecutionResult so it
3178 3177 # can fill in the output value.
3179 3178 self.displayhook.exec_result = result
3180 3179
3181 3180 # Execute the user code
3182 3181 interactivity = "none" if silent else self.ast_node_interactivity
3183 3182
3184 3183 has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
3185 3184 interactivity=interactivity, compiler=compiler, result=result)
3186 3185
3187 3186 self.last_execution_succeeded = not has_raised
3188 3187 self.last_execution_result = result
3189 3188
3190 3189 # Reset this so later displayed values do not modify the
3191 3190 # ExecutionResult
3192 3191 self.displayhook.exec_result = None
3193 3192
3194 3193 if store_history:
3195 3194 # Write output to the database. Does nothing unless
3196 3195 # history output logging is enabled.
3197 3196 self.history_manager.store_output(self.execution_count)
3198 3197 # Each cell is a *single* input, regardless of how many lines it has
3199 3198 self.execution_count += 1
3200 3199
3201 3200 return result
3202 3201
3203 3202 def transform_cell(self, raw_cell):
3204 3203 """Transform an input cell before parsing it.
3205 3204
3206 3205 Static transformations, implemented in IPython.core.inputtransformer2,
3207 3206 deal with things like ``%magic`` and ``!system`` commands.
3208 3207 These run on all input.
3209 3208 Dynamic transformations, for things like unescaped magics and the exit
3210 3209 autocall, depend on the state of the interpreter.
3211 3210 These only apply to single line inputs.
3212 3211
3213 3212 These string-based transformations are followed by AST transformations;
3214 3213 see :meth:`transform_ast`.
3215 3214 """
3216 3215 # Static input transformations
3217 3216 cell = self.input_transformer_manager.transform_cell(raw_cell)
3218 3217
3219 3218 if len(cell.splitlines()) == 1:
3220 3219 # Dynamic transformations - only applied for single line commands
3221 3220 with self.builtin_trap:
3222 3221 # use prefilter_lines to handle trailing newlines
3223 3222 # restore trailing newline for ast.parse
3224 3223 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
3225 3224
3226 3225 lines = cell.splitlines(keepends=True)
3227 3226 for transform in self.input_transformers_post:
3228 3227 lines = transform(lines)
3229 3228 cell = ''.join(lines)
3230 3229
3231 3230 return cell
3232 3231
3233 3232 def transform_ast(self, node):
3234 3233 """Apply the AST transformations from self.ast_transformers
3235 3234
3236 3235 Parameters
3237 3236 ----------
3238 3237 node : ast.Node
3239 3238 The root node to be transformed. Typically called with the ast.Module
3240 3239 produced by parsing user input.
3241 3240
3242 3241 Returns
3243 3242 -------
3244 3243 An ast.Node corresponding to the node it was called with. Note that it
3245 3244 may also modify the passed object, so don't rely on references to the
3246 3245 original AST.
3247 3246 """
3248 3247 for transformer in self.ast_transformers:
3249 3248 try:
3250 3249 node = transformer.visit(node)
3251 3250 except InputRejected:
3252 3251 # User-supplied AST transformers can reject an input by raising
3253 3252 # an InputRejected. Short-circuit in this case so that we
3254 3253 # don't unregister the transform.
3255 3254 raise
3256 3255 except Exception:
3257 3256 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
3258 3257 self.ast_transformers.remove(transformer)
3259 3258
3260 3259 if self.ast_transformers:
3261 3260 ast.fix_missing_locations(node)
3262 3261 return node
3263 3262
3264 3263 async def run_ast_nodes(
3265 3264 self,
3266 3265 nodelist: ListType[stmt],
3267 3266 cell_name: str,
3268 3267 interactivity="last_expr",
3269 3268 compiler=compile,
3270 3269 result=None,
3271 3270 ):
3272 3271 """Run a sequence of AST nodes. The execution mode depends on the
3273 3272 interactivity parameter.
3274 3273
3275 3274 Parameters
3276 3275 ----------
3277 3276 nodelist : list
3278 3277 A sequence of AST nodes to run.
3279 3278 cell_name : str
3280 3279 Will be passed to the compiler as the filename of the cell. Typically
3281 3280 the value returned by ip.compile.cache(cell).
3282 3281 interactivity : str
3283 3282 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none',
3284 3283 specifying which nodes should be run interactively (displaying output
3285 3284 from expressions). 'last_expr' will run the last node interactively
3286 3285 only if it is an expression (i.e. expressions in loops or other blocks
3287 3286 are not displayed) 'last_expr_or_assign' will run the last expression
3288 3287 or the last assignment. Other values for this parameter will raise a
3289 3288 ValueError.
3290 3289
3291 3290 compiler : callable
3292 3291 A function with the same interface as the built-in compile(), to turn
3293 3292 the AST nodes into code objects. Default is the built-in compile().
3294 3293 result : ExecutionResult, optional
3295 3294 An object to store exceptions that occur during execution.
3296 3295
3297 3296 Returns
3298 3297 -------
3299 3298 True if an exception occurred while running code, False if it finished
3300 3299 running.
3301 3300 """
3302 3301 if not nodelist:
3303 3302 return
3304 3303
3305 3304
3306 3305 if interactivity == 'last_expr_or_assign':
3307 3306 if isinstance(nodelist[-1], _assign_nodes):
3308 3307 asg = nodelist[-1]
3309 3308 if isinstance(asg, ast.Assign) and len(asg.targets) == 1:
3310 3309 target = asg.targets[0]
3311 3310 elif isinstance(asg, _single_targets_nodes):
3312 3311 target = asg.target
3313 3312 else:
3314 3313 target = None
3315 3314 if isinstance(target, ast.Name):
3316 3315 nnode = ast.Expr(ast.Name(target.id, ast.Load()))
3317 3316 ast.fix_missing_locations(nnode)
3318 3317 nodelist.append(nnode)
3319 3318 interactivity = 'last_expr'
3320 3319
3321 3320 _async = False
3322 3321 if interactivity == 'last_expr':
3323 3322 if isinstance(nodelist[-1], ast.Expr):
3324 3323 interactivity = "last"
3325 3324 else:
3326 3325 interactivity = "none"
3327 3326
3328 3327 if interactivity == 'none':
3329 3328 to_run_exec, to_run_interactive = nodelist, []
3330 3329 elif interactivity == 'last':
3331 3330 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
3332 3331 elif interactivity == 'all':
3333 3332 to_run_exec, to_run_interactive = [], nodelist
3334 3333 else:
3335 3334 raise ValueError("Interactivity was %r" % interactivity)
3336 3335
3337 3336 try:
3338 3337
3339 3338 def compare(code):
3340 3339 is_async = inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE
3341 3340 return is_async
3342 3341
3343 3342 # refactor that to just change the mod constructor.
3344 3343 to_run = []
3345 3344 for node in to_run_exec:
3346 3345 to_run.append((node, "exec"))
3347 3346
3348 3347 for node in to_run_interactive:
3349 3348 to_run.append((node, "single"))
3350 3349
3351 3350 for node, mode in to_run:
3352 3351 if mode == "exec":
3353 3352 mod = Module([node], [])
3354 3353 elif mode == "single":
3355 3354 mod = ast.Interactive([node])
3356 3355 with compiler.extra_flags(
3357 3356 getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0)
3358 3357 if self.autoawait
3359 3358 else 0x0
3360 3359 ):
3361 3360 code = compiler(mod, cell_name, mode)
3362 3361 asy = compare(code)
3363 3362 if await self.run_code(code, result, async_=asy):
3364 3363 return True
3365 3364
3366 3365 # Flush softspace
3367 3366 if softspace(sys.stdout, 0):
3368 3367 print()
3369 3368
3370 3369 except:
3371 3370 # It's possible to have exceptions raised here, typically by
3372 3371 # compilation of odd code (such as a naked 'return' outside a
3373 3372 # function) that did parse but isn't valid. Typically the exception
3374 3373 # is a SyntaxError, but it's safest just to catch anything and show
3375 3374 # the user a traceback.
3376 3375
3377 3376 # We do only one try/except outside the loop to minimize the impact
3378 3377 # on runtime, and also because if any node in the node list is
3379 3378 # broken, we should stop execution completely.
3380 3379 if result:
3381 3380 result.error_before_exec = sys.exc_info()[1]
3382 3381 self.showtraceback()
3383 3382 return True
3384 3383
3385 3384 return False
3386 3385
3387 3386 async def run_code(self, code_obj, result=None, *, async_=False):
3388 3387 """Execute a code object.
3389 3388
3390 3389 When an exception occurs, self.showtraceback() is called to display a
3391 3390 traceback.
3392 3391
3393 3392 Parameters
3394 3393 ----------
3395 3394 code_obj : code object
3396 3395 A compiled code object, to be executed
3397 3396 result : ExecutionResult, optional
3398 3397 An object to store exceptions that occur during execution.
3399 3398 async_ : Bool (Experimental)
3400 3399 Attempt to run top-level asynchronous code in a default loop.
3401 3400
3402 3401 Returns
3403 3402 -------
3404 3403 False : successful execution.
3405 3404 True : an error occurred.
3406 3405 """
3407 3406 # special value to say that anything above is IPython and should be
3408 3407 # hidden.
3409 3408 __tracebackhide__ = "__ipython_bottom__"
3410 3409 # Set our own excepthook in case the user code tries to call it
3411 3410 # directly, so that the IPython crash handler doesn't get triggered
3412 3411 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
3413 3412
3414 3413 # we save the original sys.excepthook in the instance, in case config
3415 3414 # code (such as magics) needs access to it.
3416 3415 self.sys_excepthook = old_excepthook
3417 3416 outflag = True # happens in more places, so it's easier as default
3418 3417 try:
3419 3418 try:
3420 3419 if async_:
3421 3420 await eval(code_obj, self.user_global_ns, self.user_ns)
3422 3421 else:
3423 3422 exec(code_obj, self.user_global_ns, self.user_ns)
3424 3423 finally:
3425 3424 # Reset our crash handler in place
3426 3425 sys.excepthook = old_excepthook
3427 3426 except SystemExit as e:
3428 3427 if result is not None:
3429 3428 result.error_in_exec = e
3430 3429 self.showtraceback(exception_only=True)
3431 3430 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
3432 3431 except bdb.BdbQuit:
3433 3432 etype, value, tb = sys.exc_info()
3434 3433 if result is not None:
3435 3434 result.error_in_exec = value
3436 3435 # the BdbQuit stops here
3437 3436 except self.custom_exceptions:
3438 3437 etype, value, tb = sys.exc_info()
3439 3438 if result is not None:
3440 3439 result.error_in_exec = value
3441 3440 self.CustomTB(etype, value, tb)
3442 3441 except:
3443 3442 if result is not None:
3444 3443 result.error_in_exec = sys.exc_info()[1]
3445 3444 self.showtraceback(running_compiled_code=True)
3446 3445 else:
3447 3446 outflag = False
3448 3447 return outflag
3449 3448
3450 3449 # For backwards compatibility
3451 3450 runcode = run_code
3452 3451
3453 3452 def check_complete(self, code: str) -> Tuple[str, str]:
3454 3453 """Return whether a block of code is ready to execute, or should be continued
3455 3454
3456 3455 Parameters
3457 3456 ----------
3458 3457 code : string
3459 3458 Python input code, which can be multiline.
3460 3459
3461 3460 Returns
3462 3461 -------
3463 3462 status : str
3464 3463 One of 'complete', 'incomplete', or 'invalid' if source is not a
3465 3464 prefix of valid code.
3466 3465 indent : str
3467 3466 When status is 'incomplete', this is some whitespace to insert on
3468 3467 the next line of the prompt.
3469 3468 """
3470 3469 status, nspaces = self.input_transformer_manager.check_complete(code)
3471 3470 return status, ' ' * (nspaces or 0)
3472 3471
3473 3472 #-------------------------------------------------------------------------
3474 3473 # Things related to GUI support and pylab
3475 3474 #-------------------------------------------------------------------------
3476 3475
3477 3476 active_eventloop = None
3478 3477
3479 3478 def enable_gui(self, gui=None):
3480 3479 raise NotImplementedError('Implement enable_gui in a subclass')
3481 3480
3482 3481 def enable_matplotlib(self, gui=None):
3483 3482 """Enable interactive matplotlib and inline figure support.
3484 3483
3485 3484 This takes the following steps:
3486 3485
3487 3486 1. select the appropriate eventloop and matplotlib backend
3488 3487 2. set up matplotlib for interactive use with that backend
3489 3488 3. configure formatters for inline figure display
3490 3489 4. enable the selected gui eventloop
3491 3490
3492 3491 Parameters
3493 3492 ----------
3494 3493 gui : optional, string
3495 3494 If given, dictates the choice of matplotlib GUI backend to use
3496 3495 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3497 3496 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3498 3497 matplotlib (as dictated by the matplotlib build-time options plus the
3499 3498 user's matplotlibrc configuration file). Note that not all backends
3500 3499 make sense in all contexts, for example a terminal ipython can't
3501 3500 display figures inline.
3502 3501 """
3503 3502 from matplotlib_inline.backend_inline import configure_inline_support
3504 3503
3505 3504 from IPython.core import pylabtools as pt
3506 3505 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3507 3506
3508 3507 if gui != 'inline':
3509 3508 # If we have our first gui selection, store it
3510 3509 if self.pylab_gui_select is None:
3511 3510 self.pylab_gui_select = gui
3512 3511 # Otherwise if they are different
3513 3512 elif gui != self.pylab_gui_select:
3514 3513 print('Warning: Cannot change to a different GUI toolkit: %s.'
3515 3514 ' Using %s instead.' % (gui, self.pylab_gui_select))
3516 3515 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
3517 3516
3518 3517 pt.activate_matplotlib(backend)
3519 3518 configure_inline_support(self, backend)
3520 3519
3521 3520 # Now we must activate the gui pylab wants to use, and fix %run to take
3522 3521 # plot updates into account
3523 3522 self.enable_gui(gui)
3524 3523 self.magics_manager.registry['ExecutionMagics'].default_runner = \
3525 3524 pt.mpl_runner(self.safe_execfile)
3526 3525
3527 3526 return gui, backend
3528 3527
3529 3528 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
3530 3529 """Activate pylab support at runtime.
3531 3530
3532 3531 This turns on support for matplotlib, preloads into the interactive
3533 3532 namespace all of numpy and pylab, and configures IPython to correctly
3534 3533 interact with the GUI event loop. The GUI backend to be used can be
3535 3534 optionally selected with the optional ``gui`` argument.
3536 3535
3537 3536 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
3538 3537
3539 3538 Parameters
3540 3539 ----------
3541 3540 gui : optional, string
3542 3541 If given, dictates the choice of matplotlib GUI backend to use
3543 3542 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3544 3543 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3545 3544 matplotlib (as dictated by the matplotlib build-time options plus the
3546 3545 user's matplotlibrc configuration file). Note that not all backends
3547 3546 make sense in all contexts, for example a terminal ipython can't
3548 3547 display figures inline.
3549 3548 import_all : optional, bool, default: True
3550 3549 Whether to do `from numpy import *` and `from pylab import *`
3551 3550 in addition to module imports.
3552 3551 welcome_message : deprecated
3553 3552 This argument is ignored, no welcome message will be displayed.
3554 3553 """
3555 3554 from IPython.core.pylabtools import import_pylab
3556 3555
3557 3556 gui, backend = self.enable_matplotlib(gui)
3558 3557
3559 3558 # We want to prevent the loading of pylab to pollute the user's
3560 3559 # namespace as shown by the %who* magics, so we execute the activation
3561 3560 # code in an empty namespace, and we update *both* user_ns and
3562 3561 # user_ns_hidden with this information.
3563 3562 ns = {}
3564 3563 import_pylab(ns, import_all)
3565 3564 # warn about clobbered names
3566 3565 ignored = {"__builtins__"}
3567 3566 both = set(ns).intersection(self.user_ns).difference(ignored)
3568 3567 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
3569 3568 self.user_ns.update(ns)
3570 3569 self.user_ns_hidden.update(ns)
3571 3570 return gui, backend, clobbered
3572 3571
3573 3572 #-------------------------------------------------------------------------
3574 3573 # Utilities
3575 3574 #-------------------------------------------------------------------------
3576 3575
3577 3576 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3578 3577 """Expand python variables in a string.
3579 3578
3580 3579 The depth argument indicates how many frames above the caller should
3581 3580 be walked to look for the local namespace where to expand variables.
3582 3581
3583 3582 The global namespace for expansion is always the user's interactive
3584 3583 namespace.
3585 3584 """
3586 3585 ns = self.user_ns.copy()
3587 3586 try:
3588 3587 frame = sys._getframe(depth+1)
3589 3588 except ValueError:
3590 3589 # This is thrown if there aren't that many frames on the stack,
3591 3590 # e.g. if a script called run_line_magic() directly.
3592 3591 pass
3593 3592 else:
3594 3593 ns.update(frame.f_locals)
3595 3594
3596 3595 try:
3597 3596 # We have to use .vformat() here, because 'self' is a valid and common
3598 3597 # name, and expanding **ns for .format() would make it collide with
3599 3598 # the 'self' argument of the method.
3600 3599 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3601 3600 except Exception:
3602 3601 # if formatter couldn't format, just let it go untransformed
3603 3602 pass
3604 3603 return cmd
3605 3604
3606 3605 def mktempfile(self, data=None, prefix='ipython_edit_'):
3607 3606 """Make a new tempfile and return its filename.
3608 3607
3609 3608 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3610 3609 but it registers the created filename internally so ipython cleans it up
3611 3610 at exit time.
3612 3611
3613 3612 Optional inputs:
3614 3613
3615 3614 - data(None): if data is given, it gets written out to the temp file
3616 3615 immediately, and the file is closed again."""
3617 3616
3618 3617 dir_path = Path(tempfile.mkdtemp(prefix=prefix))
3619 3618 self.tempdirs.append(dir_path)
3620 3619
3621 3620 handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path))
3622 3621 os.close(handle) # On Windows, there can only be one open handle on a file
3623 3622
3624 3623 file_path = Path(filename)
3625 3624 self.tempfiles.append(file_path)
3626 3625
3627 3626 if data:
3628 3627 file_path.write_text(data, encoding="utf-8")
3629 3628 return filename
3630 3629
3631 3630 def ask_yes_no(self, prompt, default=None, interrupt=None):
3632 3631 if self.quiet:
3633 3632 return True
3634 3633 return ask_yes_no(prompt,default,interrupt)
3635 3634
3636 3635 def show_usage(self):
3637 3636 """Show a usage message"""
3638 3637 page.page(IPython.core.usage.interactive_usage)
3639 3638
3640 3639 def extract_input_lines(self, range_str, raw=False):
3641 3640 """Return as a string a set of input history slices.
3642 3641
3643 3642 Parameters
3644 3643 ----------
3645 3644 range_str : str
3646 3645 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3647 3646 since this function is for use by magic functions which get their
3648 3647 arguments as strings. The number before the / is the session
3649 3648 number: ~n goes n back from the current session.
3650 3649
3651 3650 If empty string is given, returns history of current session
3652 3651 without the last input.
3653 3652
3654 3653 raw : bool, optional
3655 3654 By default, the processed input is used. If this is true, the raw
3656 3655 input history is used instead.
3657 3656
3658 3657 Notes
3659 3658 -----
3660 3659 Slices can be described with two notations:
3661 3660
3662 3661 * ``N:M`` -> standard python form, means including items N...(M-1).
3663 3662 * ``N-M`` -> include items N..M (closed endpoint).
3664 3663 """
3665 3664 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3666 3665 text = "\n".join(x for _, _, x in lines)
3667 3666
3668 3667 # Skip the last line, as it's probably the magic that called this
3669 3668 if not range_str:
3670 3669 if "\n" not in text:
3671 3670 text = ""
3672 3671 else:
3673 3672 text = text[: text.rfind("\n")]
3674 3673
3675 3674 return text
3676 3675
3677 3676 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3678 3677 """Get a code string from history, file, url, or a string or macro.
3679 3678
3680 3679 This is mainly used by magic functions.
3681 3680
3682 3681 Parameters
3683 3682 ----------
3684 3683 target : str
3685 3684 A string specifying code to retrieve. This will be tried respectively
3686 3685 as: ranges of input history (see %history for syntax), url,
3687 3686 corresponding .py file, filename, or an expression evaluating to a
3688 3687 string or Macro in the user namespace.
3689 3688
3690 3689 If empty string is given, returns complete history of current
3691 3690 session, without the last line.
3692 3691
3693 3692 raw : bool
3694 3693 If true (default), retrieve raw history. Has no effect on the other
3695 3694 retrieval mechanisms.
3696 3695
3697 3696 py_only : bool (default False)
3698 3697 Only try to fetch python code, do not try alternative methods to decode file
3699 3698 if unicode fails.
3700 3699
3701 3700 Returns
3702 3701 -------
3703 3702 A string of code.
3704 3703 ValueError is raised if nothing is found, and TypeError if it evaluates
3705 3704 to an object of another type. In each case, .args[0] is a printable
3706 3705 message.
3707 3706 """
3708 3707 code = self.extract_input_lines(target, raw=raw) # Grab history
3709 3708 if code:
3710 3709 return code
3711 3710 try:
3712 3711 if target.startswith(('http://', 'https://')):
3713 3712 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3714 3713 except UnicodeDecodeError as e:
3715 3714 if not py_only :
3716 3715 # Deferred import
3717 3716 from urllib.request import urlopen
3718 3717 response = urlopen(target)
3719 3718 return response.read().decode('latin1')
3720 3719 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3721 3720
3722 3721 potential_target = [target]
3723 3722 try :
3724 3723 potential_target.insert(0,get_py_filename(target))
3725 3724 except IOError:
3726 3725 pass
3727 3726
3728 3727 for tgt in potential_target :
3729 3728 if os.path.isfile(tgt): # Read file
3730 3729 try :
3731 3730 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3732 3731 except UnicodeDecodeError as e:
3733 3732 if not py_only :
3734 3733 with io_open(tgt,'r', encoding='latin1') as f :
3735 3734 return f.read()
3736 3735 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3737 3736 elif os.path.isdir(os.path.expanduser(tgt)):
3738 3737 raise ValueError("'%s' is a directory, not a regular file." % target)
3739 3738
3740 3739 if search_ns:
3741 3740 # Inspect namespace to load object source
3742 3741 object_info = self.object_inspect(target, detail_level=1)
3743 3742 if object_info['found'] and object_info['source']:
3744 3743 return object_info['source']
3745 3744
3746 3745 try: # User namespace
3747 3746 codeobj = eval(target, self.user_ns)
3748 3747 except Exception as e:
3749 3748 raise ValueError(("'%s' was not found in history, as a file, url, "
3750 3749 "nor in the user namespace.") % target) from e
3751 3750
3752 3751 if isinstance(codeobj, str):
3753 3752 return codeobj
3754 3753 elif isinstance(codeobj, Macro):
3755 3754 return codeobj.value
3756 3755
3757 3756 raise TypeError("%s is neither a string nor a macro." % target,
3758 3757 codeobj)
3759 3758
3760 3759 def _atexit_once(self):
3761 3760 """
3762 3761 At exist operation that need to be called at most once.
3763 3762 Second call to this function per instance will do nothing.
3764 3763 """
3765 3764
3766 3765 if not getattr(self, "_atexit_once_called", False):
3767 3766 self._atexit_once_called = True
3768 3767 # Clear all user namespaces to release all references cleanly.
3769 3768 self.reset(new_session=False)
3770 3769 # Close the history session (this stores the end time and line count)
3771 3770 # this must be *before* the tempfile cleanup, in case of temporary
3772 3771 # history db
3773 3772 self.history_manager.end_session()
3774 3773 self.history_manager = None
3775 3774
3776 3775 #-------------------------------------------------------------------------
3777 3776 # Things related to IPython exiting
3778 3777 #-------------------------------------------------------------------------
3779 3778 def atexit_operations(self):
3780 3779 """This will be executed at the time of exit.
3781 3780
3782 3781 Cleanup operations and saving of persistent data that is done
3783 3782 unconditionally by IPython should be performed here.
3784 3783
3785 3784 For things that may depend on startup flags or platform specifics (such
3786 3785 as having readline or not), register a separate atexit function in the
3787 3786 code that has the appropriate information, rather than trying to
3788 3787 clutter
3789 3788 """
3790 3789 self._atexit_once()
3791 3790
3792 3791 # Cleanup all tempfiles and folders left around
3793 3792 for tfile in self.tempfiles:
3794 3793 try:
3795 3794 tfile.unlink()
3796 3795 self.tempfiles.remove(tfile)
3797 3796 except FileNotFoundError:
3798 3797 pass
3799 3798 del self.tempfiles
3800 3799 for tdir in self.tempdirs:
3801 3800 try:
3802 3801 tdir.rmdir()
3803 3802 self.tempdirs.remove(tdir)
3804 3803 except FileNotFoundError:
3805 3804 pass
3806 3805 del self.tempdirs
3807 3806
3808 3807 # Restore user's cursor
3809 3808 if hasattr(self, "editing_mode") and self.editing_mode == "vi":
3810 3809 sys.stdout.write("\x1b[0 q")
3811 3810 sys.stdout.flush()
3812 3811
3813 3812 def cleanup(self):
3814 3813 self.restore_sys_module_state()
3815 3814
3816 3815
3817 3816 # Overridden in terminal subclass to change prompts
3818 3817 def switch_doctest_mode(self, mode):
3819 3818 pass
3820 3819
3821 3820
3822 3821 class InteractiveShellABC(metaclass=abc.ABCMeta):
3823 3822 """An abstract base class for InteractiveShell."""
3824 3823
3825 3824 InteractiveShellABC.register(InteractiveShell)
@@ -1,1203 +1,1201 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Verbose and colourful traceback formatting.
4 4
5 5 **ColorTB**
6 6
7 7 I've always found it a bit hard to visually parse tracebacks in Python. The
8 8 ColorTB class is a solution to that problem. It colors the different parts of a
9 9 traceback in a manner similar to what you would expect from a syntax-highlighting
10 10 text editor.
11 11
12 12 Installation instructions for ColorTB::
13 13
14 14 import sys,ultratb
15 15 sys.excepthook = ultratb.ColorTB()
16 16
17 17 **VerboseTB**
18 18
19 19 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
20 20 of useful info when a traceback occurs. Ping originally had it spit out HTML
21 21 and intended it for CGI programmers, but why should they have all the fun? I
22 22 altered it to spit out colored text to the terminal. It's a bit overwhelming,
23 23 but kind of neat, and maybe useful for long-running programs that you believe
24 24 are bug-free. If a crash *does* occur in that type of program you want details.
25 25 Give it a shot--you'll love it or you'll hate it.
26 26
27 27 .. note::
28 28
29 29 The Verbose mode prints the variables currently visible where the exception
30 30 happened (shortening their strings if too long). This can potentially be
31 31 very slow, if you happen to have a huge data structure whose string
32 32 representation is complex to compute. Your computer may appear to freeze for
33 33 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
34 34 with Ctrl-C (maybe hitting it more than once).
35 35
36 36 If you encounter this kind of situation often, you may want to use the
37 37 Verbose_novars mode instead of the regular Verbose, which avoids formatting
38 38 variables (but otherwise includes the information and context given by
39 39 Verbose).
40 40
41 41 .. note::
42 42
43 43 The verbose mode print all variables in the stack, which means it can
44 44 potentially leak sensitive information like access keys, or unencrypted
45 45 password.
46 46
47 47 Installation instructions for VerboseTB::
48 48
49 49 import sys,ultratb
50 50 sys.excepthook = ultratb.VerboseTB()
51 51
52 52 Note: Much of the code in this module was lifted verbatim from the standard
53 53 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
54 54
55 55 Color schemes
56 56 -------------
57 57
58 58 The colors are defined in the class TBTools through the use of the
59 59 ColorSchemeTable class. Currently the following exist:
60 60
61 61 - NoColor: allows all of this module to be used in any terminal (the color
62 62 escapes are just dummy blank strings).
63 63
64 64 - Linux: is meant to look good in a terminal like the Linux console (black
65 65 or very dark background).
66 66
67 67 - LightBG: similar to Linux but swaps dark/light colors to be more readable
68 68 in light background terminals.
69 69
70 70 - Neutral: a neutral color scheme that should be readable on both light and
71 71 dark background
72 72
73 73 You can implement other color schemes easily, the syntax is fairly
74 74 self-explanatory. Please send back new schemes you develop to the author for
75 75 possible inclusion in future releases.
76 76
77 77 Inheritance diagram:
78 78
79 79 .. inheritance-diagram:: IPython.core.ultratb
80 80 :parts: 3
81 81 """
82 82
83 83 #*****************************************************************************
84 84 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
85 85 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
86 86 #
87 87 # Distributed under the terms of the BSD License. The full license is in
88 88 # the file COPYING, distributed as part of this software.
89 89 #*****************************************************************************
90 90
91 91
92 92 import inspect
93 93 import linecache
94 94 import pydoc
95 95 import sys
96 96 import time
97 97 import traceback
98 98 from types import TracebackType
99 99 from typing import Tuple, List, Any, Optional
100 100
101 101 import stack_data
102 102 from pygments.formatters.terminal256 import Terminal256Formatter
103 103 from pygments.styles import get_style_by_name
104 104
105 105 # IPython's own modules
106 106 from IPython import get_ipython
107 107 from IPython.core import debugger
108 108 from IPython.core.display_trap import DisplayTrap
109 109 from IPython.core.excolors import exception_colors
110 110 from IPython.utils import path as util_path
111 111 from IPython.utils import py3compat
112 112 from IPython.utils.terminal import get_terminal_size
113 113
114 114 import IPython.utils.colorable as colorable
115 115
116 116 # Globals
117 117 # amount of space to put line numbers before verbose tracebacks
118 118 INDENT_SIZE = 8
119 119
120 120 # Default color scheme. This is used, for example, by the traceback
121 121 # formatter. When running in an actual IPython instance, the user's rc.colors
122 122 # value is used, but having a module global makes this functionality available
123 123 # to users of ultratb who are NOT running inside ipython.
124 124 DEFAULT_SCHEME = 'NoColor'
125 125
126 126 # ---------------------------------------------------------------------------
127 127 # Code begins
128 128
129 129 # Helper function -- largely belongs to VerboseTB, but we need the same
130 130 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
131 131 # can be recognized properly by ipython.el's py-traceback-line-re
132 132 # (SyntaxErrors have to be treated specially because they have no traceback)
133 133
134 134
135 135 def _format_traceback_lines(lines, Colors, has_colors: bool, lvals):
136 136 """
137 137 Format tracebacks lines with pointing arrow, leading numbers...
138 138
139 139 Parameters
140 140 ----------
141 141 lines : list[Line]
142 142 Colors
143 143 ColorScheme used.
144 144 lvals : str
145 145 Values of local variables, already colored, to inject just after the error line.
146 146 """
147 147 numbers_width = INDENT_SIZE - 1
148 148 res = []
149 149
150 150 for stack_line in lines:
151 151 if stack_line is stack_data.LINE_GAP:
152 152 res.append('%s (...)%s\n' % (Colors.linenoEm, Colors.Normal))
153 153 continue
154 154
155 155 line = stack_line.render(pygmented=has_colors).rstrip('\n') + '\n'
156 156 lineno = stack_line.lineno
157 157 if stack_line.is_current:
158 158 # This is the line with the error
159 159 pad = numbers_width - len(str(lineno))
160 160 num = '%s%s' % (debugger.make_arrow(pad), str(lineno))
161 161 start_color = Colors.linenoEm
162 162 else:
163 163 num = '%*s' % (numbers_width, lineno)
164 164 start_color = Colors.lineno
165 165
166 166 line = '%s%s%s %s' % (start_color, num, Colors.Normal, line)
167 167
168 168 res.append(line)
169 169 if lvals and stack_line.is_current:
170 170 res.append(lvals + '\n')
171 171 return res
172 172
173 173
174 174 def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):
175 175 """
176 176 Format filename lines with `In [n]` if it's the nth code cell or `File *.py` if it's a module.
177 177
178 178 Parameters
179 179 ----------
180 180 file : str
181 181 ColorFilename
182 182 ColorScheme's filename coloring to be used.
183 183 ColorNormal
184 184 ColorScheme's normal coloring to be used.
185 185 """
186 186 ipinst = get_ipython()
187 187
188 188 if ipinst is not None and file in ipinst.compile._filename_map:
189 189 file = "[%s]" % ipinst.compile._filename_map[file]
190 190 if lineno is None:
191 191 tpl_link = f"Cell {ColorFilename}In {{file}}{ColorNormal}"
192 192 else:
193 193 tpl_link = f"Cell {ColorFilename}In {{file}}, line {{lineno}}{ColorNormal}"
194 194 else:
195 195 file = util_path.compress_user(
196 196 py3compat.cast_unicode(file, util_path.fs_encoding)
197 197 )
198 198 if lineno is None:
199 199 tpl_link = f"File {ColorFilename}{{file}}{ColorNormal}"
200 200 else:
201 201 tpl_link = f"File {ColorFilename}{{file}}:{{lineno}}{ColorNormal}"
202 202
203 203 return tpl_link.format(file=file, lineno=lineno)
204 204
205 205 #---------------------------------------------------------------------------
206 206 # Module classes
207 207 class TBTools(colorable.Colorable):
208 208 """Basic tools used by all traceback printer classes."""
209 209
210 210 # Number of frames to skip when reporting tracebacks
211 211 tb_offset = 0
212 212
213 213 def __init__(
214 214 self,
215 215 color_scheme="NoColor",
216 216 call_pdb=False,
217 217 ostream=None,
218 218 parent=None,
219 219 config=None,
220 220 *,
221 221 debugger_cls=None,
222 222 ):
223 223 # Whether to call the interactive pdb debugger after printing
224 224 # tracebacks or not
225 225 super(TBTools, self).__init__(parent=parent, config=config)
226 226 self.call_pdb = call_pdb
227 227
228 228 # Output stream to write to. Note that we store the original value in
229 229 # a private attribute and then make the public ostream a property, so
230 230 # that we can delay accessing sys.stdout until runtime. The way
231 231 # things are written now, the sys.stdout object is dynamically managed
232 232 # so a reference to it should NEVER be stored statically. This
233 233 # property approach confines this detail to a single location, and all
234 234 # subclasses can simply access self.ostream for writing.
235 235 self._ostream = ostream
236 236
237 237 # Create color table
238 238 self.color_scheme_table = exception_colors()
239 239
240 240 self.set_colors(color_scheme)
241 241 self.old_scheme = color_scheme # save initial value for toggles
242 242 self.debugger_cls = debugger_cls or debugger.Pdb
243 243
244 244 if call_pdb:
245 245 self.pdb = self.debugger_cls()
246 246 else:
247 247 self.pdb = None
248 248
249 249 def _get_ostream(self):
250 250 """Output stream that exceptions are written to.
251 251
252 252 Valid values are:
253 253
254 254 - None: the default, which means that IPython will dynamically resolve
255 255 to sys.stdout. This ensures compatibility with most tools, including
256 256 Windows (where plain stdout doesn't recognize ANSI escapes).
257 257
258 258 - Any object with 'write' and 'flush' attributes.
259 259 """
260 260 return sys.stdout if self._ostream is None else self._ostream
261 261
262 262 def _set_ostream(self, val):
263 263 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
264 264 self._ostream = val
265 265
266 266 ostream = property(_get_ostream, _set_ostream)
267 267
268 268 @staticmethod
269 269 def _get_chained_exception(exception_value):
270 270 cause = getattr(exception_value, "__cause__", None)
271 271 if cause:
272 272 return cause
273 273 if getattr(exception_value, "__suppress_context__", False):
274 274 return None
275 275 return getattr(exception_value, "__context__", None)
276 276
277 277 def get_parts_of_chained_exception(
278 278 self, evalue
279 279 ) -> Optional[Tuple[type, BaseException, TracebackType]]:
280 280
281 281 chained_evalue = self._get_chained_exception(evalue)
282 282
283 283 if chained_evalue:
284 284 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
285 285 return None
286 286
287 287 def prepare_chained_exception_message(self, cause) -> List[Any]:
288 288 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
289 289 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
290 290
291 291 if cause:
292 292 message = [[direct_cause]]
293 293 else:
294 294 message = [[exception_during_handling]]
295 295 return message
296 296
297 297 @property
298 298 def has_colors(self) -> bool:
299 299 return self.color_scheme_table.active_scheme_name.lower() != "nocolor"
300 300
301 301 def set_colors(self, *args, **kw):
302 302 """Shorthand access to the color table scheme selector method."""
303 303
304 304 # Set own color table
305 305 self.color_scheme_table.set_active_scheme(*args, **kw)
306 306 # for convenience, set Colors to the active scheme
307 307 self.Colors = self.color_scheme_table.active_colors
308 308 # Also set colors of debugger
309 309 if hasattr(self, 'pdb') and self.pdb is not None:
310 310 self.pdb.set_colors(*args, **kw)
311 311
312 312 def color_toggle(self):
313 313 """Toggle between the currently active color scheme and NoColor."""
314 314
315 315 if self.color_scheme_table.active_scheme_name == 'NoColor':
316 316 self.color_scheme_table.set_active_scheme(self.old_scheme)
317 317 self.Colors = self.color_scheme_table.active_colors
318 318 else:
319 319 self.old_scheme = self.color_scheme_table.active_scheme_name
320 320 self.color_scheme_table.set_active_scheme('NoColor')
321 321 self.Colors = self.color_scheme_table.active_colors
322 322
323 323 def stb2text(self, stb):
324 324 """Convert a structured traceback (a list) to a string."""
325 325 return '\n'.join(stb)
326 326
327 327 def text(self, etype, value, tb, tb_offset: Optional[int] = None, context=5):
328 328 """Return formatted traceback.
329 329
330 330 Subclasses may override this if they add extra arguments.
331 331 """
332 332 tb_list = self.structured_traceback(etype, value, tb,
333 333 tb_offset, context)
334 334 return self.stb2text(tb_list)
335 335
336 336 def structured_traceback(
337 337 self, etype, evalue, tb, tb_offset: Optional[int] = None, context=5, mode=None
338 338 ):
339 339 """Return a list of traceback frames.
340 340
341 341 Must be implemented by each class.
342 342 """
343 343 raise NotImplementedError()
344 344
345 345
346 346 #---------------------------------------------------------------------------
347 347 class ListTB(TBTools):
348 348 """Print traceback information from a traceback list, with optional color.
349 349
350 350 Calling requires 3 arguments: (etype, evalue, elist)
351 351 as would be obtained by::
352 352
353 353 etype, evalue, tb = sys.exc_info()
354 354 if tb:
355 355 elist = traceback.extract_tb(tb)
356 356 else:
357 357 elist = None
358 358
359 359 It can thus be used by programs which need to process the traceback before
360 360 printing (such as console replacements based on the code module from the
361 361 standard library).
362 362
363 363 Because they are meant to be called without a full traceback (only a
364 364 list), instances of this class can't call the interactive pdb debugger."""
365 365
366 366
367 367 def __call__(self, etype, value, elist):
368 368 self.ostream.flush()
369 369 self.ostream.write(self.text(etype, value, elist))
370 370 self.ostream.write('\n')
371 371
372 372 def _extract_tb(self, tb):
373 373 if tb:
374 374 return traceback.extract_tb(tb)
375 375 else:
376 376 return None
377 377
378 378 def structured_traceback(
379 379 self,
380 380 etype: type,
381 381 evalue: BaseException,
382 382 etb: Optional[TracebackType] = None,
383 383 tb_offset: Optional[int] = None,
384 384 context=5,
385 385 ):
386 386 """Return a color formatted string with the traceback info.
387 387
388 388 Parameters
389 389 ----------
390 390 etype : exception type
391 391 Type of the exception raised.
392 392 evalue : object
393 393 Data stored in the exception
394 394 etb : list | TracebackType | None
395 395 If list: List of frames, see class docstring for details.
396 396 If Traceback: Traceback of the exception.
397 397 tb_offset : int, optional
398 398 Number of frames in the traceback to skip. If not given, the
399 399 instance evalue is used (set in constructor).
400 400 context : int, optional
401 401 Number of lines of context information to print.
402 402
403 403 Returns
404 404 -------
405 405 String with formatted exception.
406 406 """
407 407 # This is a workaround to get chained_exc_ids in recursive calls
408 408 # etb should not be a tuple if structured_traceback is not recursive
409 409 if isinstance(etb, tuple):
410 410 etb, chained_exc_ids = etb
411 411 else:
412 412 chained_exc_ids = set()
413 413
414 414 if isinstance(etb, list):
415 415 elist = etb
416 416 elif etb is not None:
417 417 elist = self._extract_tb(etb)
418 418 else:
419 419 elist = []
420 420 tb_offset = self.tb_offset if tb_offset is None else tb_offset
421 421 assert isinstance(tb_offset, int)
422 422 Colors = self.Colors
423 423 out_list = []
424 424 if elist:
425 425
426 426 if tb_offset and len(elist) > tb_offset:
427 427 elist = elist[tb_offset:]
428 428
429 429 out_list.append('Traceback %s(most recent call last)%s:' %
430 430 (Colors.normalEm, Colors.Normal) + '\n')
431 431 out_list.extend(self._format_list(elist))
432 432 # The exception info should be a single entry in the list.
433 433 lines = ''.join(self._format_exception_only(etype, evalue))
434 434 out_list.append(lines)
435 435
436 436 exception = self.get_parts_of_chained_exception(evalue)
437 437
438 438 if exception and not id(exception[1]) in chained_exc_ids:
439 439 chained_exception_message = self.prepare_chained_exception_message(
440 440 evalue.__cause__)[0]
441 441 etype, evalue, etb = exception
442 442 # Trace exception to avoid infinite 'cause' loop
443 443 chained_exc_ids.add(id(exception[1]))
444 444 chained_exceptions_tb_offset = 0
445 445 out_list = (
446 446 self.structured_traceback(
447 447 etype, evalue, (etb, chained_exc_ids),
448 448 chained_exceptions_tb_offset, context)
449 449 + chained_exception_message
450 450 + out_list)
451 451
452 452 return out_list
453 453
454 454 def _format_list(self, extracted_list):
455 455 """Format a list of traceback entry tuples for printing.
456 456
457 457 Given a list of tuples as returned by extract_tb() or
458 458 extract_stack(), return a list of strings ready for printing.
459 459 Each string in the resulting list corresponds to the item with the
460 460 same index in the argument list. Each string ends in a newline;
461 461 the strings may contain internal newlines as well, for those items
462 462 whose source text line is not None.
463 463
464 464 Lifted almost verbatim from traceback.py
465 465 """
466 466
467 467 Colors = self.Colors
468 468 list = []
469 469 for ind, (filename, lineno, name, line) in enumerate(extracted_list):
470 470 normalCol, nameCol, fileCol, lineCol = (
471 471 # Emphasize the last entry
472 472 (Colors.normalEm, Colors.nameEm, Colors.filenameEm, Colors.line)
473 473 if ind == len(extracted_list) - 1
474 474 else (Colors.Normal, Colors.name, Colors.filename, "")
475 475 )
476 476
477 477 fns = _format_filename(filename, fileCol, normalCol, lineno=lineno)
478 478 item = f"{normalCol} {fns}"
479 479
480 480 if name != "<module>":
481 481 item += f" in {nameCol}{name}{normalCol}\n"
482 482 else:
483 483 item += "\n"
484 484 if line:
485 485 item += f"{lineCol} {line.strip()}{normalCol}\n"
486 486 list.append(item)
487 487
488 488 return list
489 489
490 490 def _format_exception_only(self, etype, value):
491 491 """Format the exception part of a traceback.
492 492
493 493 The arguments are the exception type and value such as given by
494 494 sys.exc_info()[:2]. The return value is a list of strings, each ending
495 495 in a newline. Normally, the list contains a single string; however,
496 496 for SyntaxError exceptions, it contains several lines that (when
497 497 printed) display detailed information about where the syntax error
498 498 occurred. The message indicating which exception occurred is the
499 499 always last string in the list.
500 500
501 501 Also lifted nearly verbatim from traceback.py
502 502 """
503 503 have_filedata = False
504 504 Colors = self.Colors
505 505 list = []
506 506 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
507 507 if value is None:
508 508 # Not sure if this can still happen in Python 2.6 and above
509 509 list.append(stype + '\n')
510 510 else:
511 511 if issubclass(etype, SyntaxError):
512 512 have_filedata = True
513 513 if not value.filename: value.filename = "<string>"
514 514 if value.lineno:
515 515 lineno = value.lineno
516 516 textline = linecache.getline(value.filename, value.lineno)
517 517 else:
518 518 lineno = "unknown"
519 519 textline = ""
520 520 list.append(
521 521 "%s %s%s\n"
522 522 % (
523 523 Colors.normalEm,
524 524 _format_filename(
525 525 value.filename,
526 526 Colors.filenameEm,
527 527 Colors.normalEm,
528 528 lineno=(None if lineno == "unknown" else lineno),
529 529 ),
530 530 Colors.Normal,
531 531 )
532 532 )
533 533 if textline == "":
534 534 textline = py3compat.cast_unicode(value.text, "utf-8")
535 535
536 536 if textline is not None:
537 537 i = 0
538 538 while i < len(textline) and textline[i].isspace():
539 539 i += 1
540 540 list.append('%s %s%s\n' % (Colors.line,
541 541 textline.strip(),
542 542 Colors.Normal))
543 543 if value.offset is not None:
544 544 s = ' '
545 545 for c in textline[i:value.offset - 1]:
546 546 if c.isspace():
547 547 s += c
548 548 else:
549 549 s += ' '
550 550 list.append('%s%s^%s\n' % (Colors.caret, s,
551 551 Colors.Normal))
552 552
553 553 try:
554 554 s = value.msg
555 555 except Exception:
556 556 s = self._some_str(value)
557 557 if s:
558 558 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
559 559 Colors.Normal, s))
560 560 else:
561 561 list.append('%s\n' % stype)
562 562
563 563 # sync with user hooks
564 564 if have_filedata:
565 565 ipinst = get_ipython()
566 566 if ipinst is not None:
567 567 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
568 568
569 569 return list
570 570
571 571 def get_exception_only(self, etype, value):
572 572 """Only print the exception type and message, without a traceback.
573 573
574 574 Parameters
575 575 ----------
576 576 etype : exception type
577 577 value : exception value
578 578 """
579 579 return ListTB.structured_traceback(self, etype, value)
580 580
581 581 def show_exception_only(self, etype, evalue):
582 582 """Only print the exception type and message, without a traceback.
583 583
584 584 Parameters
585 585 ----------
586 586 etype : exception type
587 587 evalue : exception value
588 588 """
589 589 # This method needs to use __call__ from *this* class, not the one from
590 590 # a subclass whose signature or behavior may be different
591 591 ostream = self.ostream
592 592 ostream.flush()
593 593 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
594 594 ostream.flush()
595 595
596 596 def _some_str(self, value):
597 597 # Lifted from traceback.py
598 598 try:
599 599 return py3compat.cast_unicode(str(value))
600 600 except:
601 601 return u'<unprintable %s object>' % type(value).__name__
602 602
603 603
604 604 #----------------------------------------------------------------------------
605 605 class VerboseTB(TBTools):
606 606 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
607 607 of HTML. Requires inspect and pydoc. Crazy, man.
608 608
609 609 Modified version which optionally strips the topmost entries from the
610 610 traceback, to be used with alternate interpreters (because their own code
611 611 would appear in the traceback)."""
612 612
613 613 _tb_highlight = "bg:ansiyellow"
614 614
615 615 def __init__(
616 616 self,
617 617 color_scheme: str = "Linux",
618 618 call_pdb: bool = False,
619 619 ostream=None,
620 620 tb_offset: int = 0,
621 621 long_header: bool = False,
622 622 include_vars: bool = True,
623 623 check_cache=None,
624 624 debugger_cls=None,
625 625 parent=None,
626 626 config=None,
627 627 ):
628 628 """Specify traceback offset, headers and color scheme.
629 629
630 630 Define how many frames to drop from the tracebacks. Calling it with
631 631 tb_offset=1 allows use of this handler in interpreters which will have
632 632 their own code at the top of the traceback (VerboseTB will first
633 633 remove that frame before printing the traceback info)."""
634 634 TBTools.__init__(
635 635 self,
636 636 color_scheme=color_scheme,
637 637 call_pdb=call_pdb,
638 638 ostream=ostream,
639 639 parent=parent,
640 640 config=config,
641 641 debugger_cls=debugger_cls,
642 642 )
643 643 self.tb_offset = tb_offset
644 644 self.long_header = long_header
645 645 self.include_vars = include_vars
646 646 # By default we use linecache.checkcache, but the user can provide a
647 # different check_cache implementation. This is used by the IPython
648 # kernel to provide tracebacks for interactive code that is cached,
649 # by a compiler instance that flushes the linecache but preserves its
650 # own code cache.
647 # different check_cache implementation. This was formerly used by the
648 # IPython kernel for interactive code, but is no longer necessary.
651 649 if check_cache is None:
652 650 check_cache = linecache.checkcache
653 651 self.check_cache = check_cache
654 652
655 653 self.skip_hidden = True
656 654
657 655 def format_record(self, frame_info):
658 656 """Format a single stack frame"""
659 657 Colors = self.Colors # just a shorthand + quicker name lookup
660 658 ColorsNormal = Colors.Normal # used a lot
661 659
662 660 if isinstance(frame_info, stack_data.RepeatedFrames):
663 661 return ' %s[... skipping similar frames: %s]%s\n' % (
664 662 Colors.excName, frame_info.description, ColorsNormal)
665 663
666 664 indent = " " * INDENT_SIZE
667 665 em_normal = "%s\n%s%s" % (Colors.valEm, indent, ColorsNormal)
668 666 tpl_call = f"in {Colors.vName}{{file}}{Colors.valEm}{{scope}}{ColorsNormal}"
669 667 tpl_call_fail = "in %s%%s%s(***failed resolving arguments***)%s" % (
670 668 Colors.vName,
671 669 Colors.valEm,
672 670 ColorsNormal,
673 671 )
674 672 tpl_name_val = "%%s %s= %%s%s" % (Colors.valEm, ColorsNormal)
675 673
676 674 link = _format_filename(
677 675 frame_info.filename,
678 676 Colors.filenameEm,
679 677 ColorsNormal,
680 678 lineno=frame_info.lineno,
681 679 )
682 680 args, varargs, varkw, locals_ = inspect.getargvalues(frame_info.frame)
683 681
684 682 func = frame_info.executing.code_qualname()
685 683 if func == "<module>":
686 684 call = ""
687 685 else:
688 686 # Decide whether to include variable details or not
689 687 var_repr = eqrepr if self.include_vars else nullrepr
690 688 try:
691 689 scope = inspect.formatargvalues(
692 690 args, varargs, varkw, locals_, formatvalue=var_repr
693 691 )
694 692 call = tpl_call.format(file=func, scope=scope)
695 693 except KeyError:
696 694 # This happens in situations like errors inside generator
697 695 # expressions, where local variables are listed in the
698 696 # line, but can't be extracted from the frame. I'm not
699 697 # 100% sure this isn't actually a bug in inspect itself,
700 698 # but since there's no info for us to compute with, the
701 699 # best we can do is report the failure and move on. Here
702 700 # we must *not* call any traceback construction again,
703 701 # because that would mess up use of %debug later on. So we
704 702 # simply report the failure and move on. The only
705 703 # limitation will be that this frame won't have locals
706 704 # listed in the call signature. Quite subtle problem...
707 705 # I can't think of a good way to validate this in a unit
708 706 # test, but running a script consisting of:
709 707 # dict( (k,v.strip()) for (k,v) in range(10) )
710 708 # will illustrate the error, if this exception catch is
711 709 # disabled.
712 710 call = tpl_call_fail % func
713 711
714 712 lvals = ''
715 713 lvals_list = []
716 714 if self.include_vars:
717 715 try:
718 716 # we likely want to fix stackdata at some point, but
719 717 # still need a workaround.
720 718 fibp = frame_info.variables_in_executing_piece
721 719 for var in fibp:
722 720 lvals_list.append(tpl_name_val % (var.name, repr(var.value)))
723 721 except Exception:
724 722 lvals_list.append(
725 723 "Exception trying to inspect frame. No more locals available."
726 724 )
727 725 if lvals_list:
728 726 lvals = '%s%s' % (indent, em_normal.join(lvals_list))
729 727
730 728 result = f'{link}{", " if call else ""}{call}\n'
731 729
732 730 result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
733 731 return result
734 732
735 733 def prepare_header(self, etype, long_version=False):
736 734 colors = self.Colors # just a shorthand + quicker name lookup
737 735 colorsnormal = colors.Normal # used a lot
738 736 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
739 737 width = min(75, get_terminal_size()[0])
740 738 if long_version:
741 739 # Header with the exception type, python version, and date
742 740 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
743 741 date = time.ctime(time.time())
744 742
745 743 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
746 744 exc, ' ' * (width - len(str(etype)) - len(pyver)),
747 745 pyver, date.rjust(width) )
748 746 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
749 747 "\ncalls leading up to the error, with the most recent (innermost) call last."
750 748 else:
751 749 # Simplified header
752 750 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
753 751 rjust(width - len(str(etype))) )
754 752
755 753 return head
756 754
757 755 def format_exception(self, etype, evalue):
758 756 colors = self.Colors # just a shorthand + quicker name lookup
759 757 colorsnormal = colors.Normal # used a lot
760 758 # Get (safely) a string form of the exception info
761 759 try:
762 760 etype_str, evalue_str = map(str, (etype, evalue))
763 761 except:
764 762 # User exception is improperly defined.
765 763 etype, evalue = str, sys.exc_info()[:2]
766 764 etype_str, evalue_str = map(str, (etype, evalue))
767 765 # ... and format it
768 766 return ['%s%s%s: %s' % (colors.excName, etype_str,
769 767 colorsnormal, py3compat.cast_unicode(evalue_str))]
770 768
771 769 def format_exception_as_a_whole(
772 770 self,
773 771 etype: type,
774 772 evalue: BaseException,
775 773 etb: Optional[TracebackType],
776 774 number_of_lines_of_context,
777 775 tb_offset: Optional[int],
778 776 ):
779 777 """Formats the header, traceback and exception message for a single exception.
780 778
781 779 This may be called multiple times by Python 3 exception chaining
782 780 (PEP 3134).
783 781 """
784 782 # some locals
785 783 orig_etype = etype
786 784 try:
787 785 etype = etype.__name__
788 786 except AttributeError:
789 787 pass
790 788
791 789 tb_offset = self.tb_offset if tb_offset is None else tb_offset
792 790 assert isinstance(tb_offset, int)
793 791 head = self.prepare_header(etype, self.long_header)
794 792 records = (
795 793 self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
796 794 )
797 795
798 796 frames = []
799 797 skipped = 0
800 798 lastrecord = len(records) - 1
801 799 for i, r in enumerate(records):
802 800 if not isinstance(r, stack_data.RepeatedFrames) and self.skip_hidden:
803 801 if r.frame.f_locals.get("__tracebackhide__", 0) and i != lastrecord:
804 802 skipped += 1
805 803 continue
806 804 if skipped:
807 805 Colors = self.Colors # just a shorthand + quicker name lookup
808 806 ColorsNormal = Colors.Normal # used a lot
809 807 frames.append(
810 808 " %s[... skipping hidden %s frame]%s\n"
811 809 % (Colors.excName, skipped, ColorsNormal)
812 810 )
813 811 skipped = 0
814 812 frames.append(self.format_record(r))
815 813 if skipped:
816 814 Colors = self.Colors # just a shorthand + quicker name lookup
817 815 ColorsNormal = Colors.Normal # used a lot
818 816 frames.append(
819 817 " %s[... skipping hidden %s frame]%s\n"
820 818 % (Colors.excName, skipped, ColorsNormal)
821 819 )
822 820
823 821 formatted_exception = self.format_exception(etype, evalue)
824 822 if records:
825 823 frame_info = records[-1]
826 824 ipinst = get_ipython()
827 825 if ipinst is not None:
828 826 ipinst.hooks.synchronize_with_editor(frame_info.filename, frame_info.lineno, 0)
829 827
830 828 return [[head] + frames + [''.join(formatted_exception[0])]]
831 829
832 830 def get_records(
833 831 self, etb: TracebackType, number_of_lines_of_context: int, tb_offset: int
834 832 ):
835 833 assert etb is not None
836 834 context = number_of_lines_of_context - 1
837 835 after = context // 2
838 836 before = context - after
839 837 if self.has_colors:
840 838 style = get_style_by_name("default")
841 839 style = stack_data.style_with_executing_node(style, self._tb_highlight)
842 840 formatter = Terminal256Formatter(style=style)
843 841 else:
844 842 formatter = None
845 843 options = stack_data.Options(
846 844 before=before,
847 845 after=after,
848 846 pygments_formatter=formatter,
849 847 )
850 848 return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:]
851 849
852 850 def structured_traceback(
853 851 self,
854 852 etype: type,
855 853 evalue: Optional[BaseException],
856 854 etb: Optional[TracebackType],
857 855 tb_offset: Optional[int] = None,
858 856 number_of_lines_of_context: int = 5,
859 857 ):
860 858 """Return a nice text document describing the traceback."""
861 859 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
862 860 tb_offset)
863 861
864 862 colors = self.Colors # just a shorthand + quicker name lookup
865 863 colorsnormal = colors.Normal # used a lot
866 864 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
867 865 structured_traceback_parts = [head]
868 866 chained_exceptions_tb_offset = 0
869 867 lines_of_context = 3
870 868 formatted_exceptions = formatted_exception
871 869 exception = self.get_parts_of_chained_exception(evalue)
872 870 if exception:
873 871 assert evalue is not None
874 872 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
875 873 etype, evalue, etb = exception
876 874 else:
877 875 evalue = None
878 876 chained_exc_ids = set()
879 877 while evalue:
880 878 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
881 879 chained_exceptions_tb_offset)
882 880 exception = self.get_parts_of_chained_exception(evalue)
883 881
884 882 if exception and not id(exception[1]) in chained_exc_ids:
885 883 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
886 884 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
887 885 etype, evalue, etb = exception
888 886 else:
889 887 evalue = None
890 888
891 889 # we want to see exceptions in a reversed order:
892 890 # the first exception should be on top
893 891 for formatted_exception in reversed(formatted_exceptions):
894 892 structured_traceback_parts += formatted_exception
895 893
896 894 return structured_traceback_parts
897 895
898 896 def debugger(self, force: bool = False):
899 897 """Call up the pdb debugger if desired, always clean up the tb
900 898 reference.
901 899
902 900 Keywords:
903 901
904 902 - force(False): by default, this routine checks the instance call_pdb
905 903 flag and does not actually invoke the debugger if the flag is false.
906 904 The 'force' option forces the debugger to activate even if the flag
907 905 is false.
908 906
909 907 If the call_pdb flag is set, the pdb interactive debugger is
910 908 invoked. In all cases, the self.tb reference to the current traceback
911 909 is deleted to prevent lingering references which hamper memory
912 910 management.
913 911
914 912 Note that each call to pdb() does an 'import readline', so if your app
915 913 requires a special setup for the readline completers, you'll have to
916 914 fix that by hand after invoking the exception handler."""
917 915
918 916 if force or self.call_pdb:
919 917 if self.pdb is None:
920 918 self.pdb = self.debugger_cls()
921 919 # the system displayhook may have changed, restore the original
922 920 # for pdb
923 921 display_trap = DisplayTrap(hook=sys.__displayhook__)
924 922 with display_trap:
925 923 self.pdb.reset()
926 924 # Find the right frame so we don't pop up inside ipython itself
927 925 if hasattr(self, 'tb') and self.tb is not None:
928 926 etb = self.tb
929 927 else:
930 928 etb = self.tb = sys.last_traceback
931 929 while self.tb is not None and self.tb.tb_next is not None:
932 930 assert self.tb.tb_next is not None
933 931 self.tb = self.tb.tb_next
934 932 if etb and etb.tb_next:
935 933 etb = etb.tb_next
936 934 self.pdb.botframe = etb.tb_frame
937 935 self.pdb.interaction(None, etb)
938 936
939 937 if hasattr(self, 'tb'):
940 938 del self.tb
941 939
942 940 def handler(self, info=None):
943 941 (etype, evalue, etb) = info or sys.exc_info()
944 942 self.tb = etb
945 943 ostream = self.ostream
946 944 ostream.flush()
947 945 ostream.write(self.text(etype, evalue, etb))
948 946 ostream.write('\n')
949 947 ostream.flush()
950 948
951 949 # Changed so an instance can just be called as VerboseTB_inst() and print
952 950 # out the right info on its own.
953 951 def __call__(self, etype=None, evalue=None, etb=None):
954 952 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
955 953 if etb is None:
956 954 self.handler()
957 955 else:
958 956 self.handler((etype, evalue, etb))
959 957 try:
960 958 self.debugger()
961 959 except KeyboardInterrupt:
962 960 print("\nKeyboardInterrupt")
963 961
964 962
965 963 #----------------------------------------------------------------------------
966 964 class FormattedTB(VerboseTB, ListTB):
967 965 """Subclass ListTB but allow calling with a traceback.
968 966
969 967 It can thus be used as a sys.excepthook for Python > 2.1.
970 968
971 969 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
972 970
973 971 Allows a tb_offset to be specified. This is useful for situations where
974 972 one needs to remove a number of topmost frames from the traceback (such as
975 973 occurs with python programs that themselves execute other python code,
976 974 like Python shells). """
977 975
978 976 mode: str
979 977
980 978 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
981 979 ostream=None,
982 980 tb_offset=0, long_header=False, include_vars=False,
983 981 check_cache=None, debugger_cls=None,
984 982 parent=None, config=None):
985 983
986 984 # NEVER change the order of this list. Put new modes at the end:
987 985 self.valid_modes = ['Plain', 'Context', 'Verbose', 'Minimal']
988 986 self.verbose_modes = self.valid_modes[1:3]
989 987
990 988 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
991 989 ostream=ostream, tb_offset=tb_offset,
992 990 long_header=long_header, include_vars=include_vars,
993 991 check_cache=check_cache, debugger_cls=debugger_cls,
994 992 parent=parent, config=config)
995 993
996 994 # Different types of tracebacks are joined with different separators to
997 995 # form a single string. They are taken from this dict
998 996 self._join_chars = dict(Plain='', Context='\n', Verbose='\n',
999 997 Minimal='')
1000 998 # set_mode also sets the tb_join_char attribute
1001 999 self.set_mode(mode)
1002 1000
1003 1001 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1004 1002 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1005 1003 mode = self.mode
1006 1004 if mode in self.verbose_modes:
1007 1005 # Verbose modes need a full traceback
1008 1006 return VerboseTB.structured_traceback(
1009 1007 self, etype, value, tb, tb_offset, number_of_lines_of_context
1010 1008 )
1011 1009 elif mode == 'Minimal':
1012 1010 return ListTB.get_exception_only(self, etype, value)
1013 1011 else:
1014 1012 # We must check the source cache because otherwise we can print
1015 1013 # out-of-date source code.
1016 1014 self.check_cache()
1017 1015 # Now we can extract and format the exception
1018 1016 return ListTB.structured_traceback(
1019 1017 self, etype, value, tb, tb_offset, number_of_lines_of_context
1020 1018 )
1021 1019
1022 1020 def stb2text(self, stb):
1023 1021 """Convert a structured traceback (a list) to a string."""
1024 1022 return self.tb_join_char.join(stb)
1025 1023
1026 1024 def set_mode(self, mode: Optional[str] = None):
1027 1025 """Switch to the desired mode.
1028 1026
1029 1027 If mode is not specified, cycles through the available modes."""
1030 1028
1031 1029 if not mode:
1032 1030 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1033 1031 len(self.valid_modes)
1034 1032 self.mode = self.valid_modes[new_idx]
1035 1033 elif mode not in self.valid_modes:
1036 1034 raise ValueError(
1037 1035 "Unrecognized mode in FormattedTB: <" + mode + ">\n"
1038 1036 "Valid modes: " + str(self.valid_modes)
1039 1037 )
1040 1038 else:
1041 1039 assert isinstance(mode, str)
1042 1040 self.mode = mode
1043 1041 # include variable details only in 'Verbose' mode
1044 1042 self.include_vars = (self.mode == self.valid_modes[2])
1045 1043 # Set the join character for generating text tracebacks
1046 1044 self.tb_join_char = self._join_chars[self.mode]
1047 1045
1048 1046 # some convenient shortcuts
1049 1047 def plain(self):
1050 1048 self.set_mode(self.valid_modes[0])
1051 1049
1052 1050 def context(self):
1053 1051 self.set_mode(self.valid_modes[1])
1054 1052
1055 1053 def verbose(self):
1056 1054 self.set_mode(self.valid_modes[2])
1057 1055
1058 1056 def minimal(self):
1059 1057 self.set_mode(self.valid_modes[3])
1060 1058
1061 1059
1062 1060 #----------------------------------------------------------------------------
1063 1061 class AutoFormattedTB(FormattedTB):
1064 1062 """A traceback printer which can be called on the fly.
1065 1063
1066 1064 It will find out about exceptions by itself.
1067 1065
1068 1066 A brief example::
1069 1067
1070 1068 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1071 1069 try:
1072 1070 ...
1073 1071 except:
1074 1072 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1075 1073 """
1076 1074
1077 1075 def __call__(self, etype=None, evalue=None, etb=None,
1078 1076 out=None, tb_offset=None):
1079 1077 """Print out a formatted exception traceback.
1080 1078
1081 1079 Optional arguments:
1082 1080 - out: an open file-like object to direct output to.
1083 1081
1084 1082 - tb_offset: the number of frames to skip over in the stack, on a
1085 1083 per-call basis (this overrides temporarily the instance's tb_offset
1086 1084 given at initialization time."""
1087 1085
1088 1086 if out is None:
1089 1087 out = self.ostream
1090 1088 out.flush()
1091 1089 out.write(self.text(etype, evalue, etb, tb_offset))
1092 1090 out.write('\n')
1093 1091 out.flush()
1094 1092 # FIXME: we should remove the auto pdb behavior from here and leave
1095 1093 # that to the clients.
1096 1094 try:
1097 1095 self.debugger()
1098 1096 except KeyboardInterrupt:
1099 1097 print("\nKeyboardInterrupt")
1100 1098
1101 1099 def structured_traceback(self, etype=None, value=None, tb=None,
1102 1100 tb_offset=None, number_of_lines_of_context=5):
1103 1101
1104 1102 etype: type
1105 1103 value: BaseException
1106 1104 # tb: TracebackType or tupleof tb types ?
1107 1105 if etype is None:
1108 1106 etype, value, tb = sys.exc_info()
1109 1107 if isinstance(tb, tuple):
1110 1108 # tb is a tuple if this is a chained exception.
1111 1109 self.tb = tb[0]
1112 1110 else:
1113 1111 self.tb = tb
1114 1112 return FormattedTB.structured_traceback(
1115 1113 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1116 1114
1117 1115
1118 1116 #---------------------------------------------------------------------------
1119 1117
1120 1118 # A simple class to preserve Nathan's original functionality.
1121 1119 class ColorTB(FormattedTB):
1122 1120 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1123 1121
1124 1122 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1125 1123 FormattedTB.__init__(self, color_scheme=color_scheme,
1126 1124 call_pdb=call_pdb, **kwargs)
1127 1125
1128 1126
1129 1127 class SyntaxTB(ListTB):
1130 1128 """Extension which holds some state: the last exception value"""
1131 1129
1132 1130 def __init__(self, color_scheme='NoColor', parent=None, config=None):
1133 1131 ListTB.__init__(self, color_scheme, parent=parent, config=config)
1134 1132 self.last_syntax_error = None
1135 1133
1136 1134 def __call__(self, etype, value, elist):
1137 1135 self.last_syntax_error = value
1138 1136
1139 1137 ListTB.__call__(self, etype, value, elist)
1140 1138
1141 1139 def structured_traceback(self, etype, value, elist, tb_offset=None,
1142 1140 context=5):
1143 1141 # If the source file has been edited, the line in the syntax error can
1144 1142 # be wrong (retrieved from an outdated cache). This replaces it with
1145 1143 # the current value.
1146 1144 if isinstance(value, SyntaxError) \
1147 1145 and isinstance(value.filename, str) \
1148 1146 and isinstance(value.lineno, int):
1149 1147 linecache.checkcache(value.filename)
1150 1148 newtext = linecache.getline(value.filename, value.lineno)
1151 1149 if newtext:
1152 1150 value.text = newtext
1153 1151 self.last_syntax_error = value
1154 1152 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1155 1153 tb_offset=tb_offset, context=context)
1156 1154
1157 1155 def clear_err_state(self):
1158 1156 """Return the current error state and clear it"""
1159 1157 e = self.last_syntax_error
1160 1158 self.last_syntax_error = None
1161 1159 return e
1162 1160
1163 1161 def stb2text(self, stb):
1164 1162 """Convert a structured traceback (a list) to a string."""
1165 1163 return ''.join(stb)
1166 1164
1167 1165
1168 1166 # some internal-use functions
1169 1167 def text_repr(value):
1170 1168 """Hopefully pretty robust repr equivalent."""
1171 1169 # this is pretty horrible but should always return *something*
1172 1170 try:
1173 1171 return pydoc.text.repr(value)
1174 1172 except KeyboardInterrupt:
1175 1173 raise
1176 1174 except:
1177 1175 try:
1178 1176 return repr(value)
1179 1177 except KeyboardInterrupt:
1180 1178 raise
1181 1179 except:
1182 1180 try:
1183 1181 # all still in an except block so we catch
1184 1182 # getattr raising
1185 1183 name = getattr(value, '__name__', None)
1186 1184 if name:
1187 1185 # ick, recursion
1188 1186 return text_repr(name)
1189 1187 klass = getattr(value, '__class__', None)
1190 1188 if klass:
1191 1189 return '%s instance' % text_repr(klass)
1192 1190 except KeyboardInterrupt:
1193 1191 raise
1194 1192 except:
1195 1193 return 'UNRECOVERABLE REPR FAILURE'
1196 1194
1197 1195
1198 1196 def eqrepr(value, repr=text_repr):
1199 1197 return '=%s' % repr(value)
1200 1198
1201 1199
1202 1200 def nullrepr(value, repr=text_repr):
1203 1201 return ''
General Comments 0
You need to be logged in to leave comments. Login now