Show More
@@ -1,196 +1,197 b'' | |||||
1 | """Compiler tools with improved interactive support. |
|
1 | """Compiler tools with improved interactive support. | |
2 |
|
2 | |||
3 | Provides compilation machinery similar to codeop, but with caching support so |
|
3 | Provides compilation machinery similar to codeop, but with caching support so | |
4 | we can provide interactive tracebacks. |
|
4 | we can provide interactive tracebacks. | |
5 |
|
5 | |||
6 | Authors |
|
6 | Authors | |
7 | ------- |
|
7 | ------- | |
8 | * Robert Kern |
|
8 | * Robert Kern | |
9 | * Fernando Perez |
|
9 | * Fernando Perez | |
10 | * Thomas Kluyver |
|
10 | * Thomas Kluyver | |
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | # Note: though it might be more natural to name this module 'compiler', that |
|
13 | # Note: though it might be more natural to name this module 'compiler', that | |
14 | # name is in the stdlib and name collisions with the stdlib tend to produce |
|
14 | # name is in the stdlib and name collisions with the stdlib tend to produce | |
15 | # weird problems (often with third-party tools). |
|
15 | # weird problems (often with third-party tools). | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Copyright (C) 2010-2011 The IPython Development Team. |
|
18 | # Copyright (C) 2010-2011 The IPython Development Team. | |
19 | # |
|
19 | # | |
20 | # Distributed under the terms of the BSD License. |
|
20 | # Distributed under the terms of the BSD License. | |
21 | # |
|
21 | # | |
22 | # The full license is in the file COPYING.txt, distributed with this software. |
|
22 | # The full license is in the file COPYING.txt, distributed with this software. | |
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | # Imports |
|
26 | # Imports | |
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 |
|
28 | |||
29 | # Stdlib imports |
|
29 | # Stdlib imports | |
30 | import __future__ |
|
30 | import __future__ | |
31 | from ast import PyCF_ONLY_AST |
|
31 | from ast import PyCF_ONLY_AST | |
32 | import codeop |
|
32 | import codeop | |
33 | import functools |
|
33 | import functools | |
34 | import hashlib |
|
34 | import hashlib | |
35 | import linecache |
|
35 | import linecache | |
36 | import operator |
|
36 | import operator | |
37 | import time |
|
37 | import time | |
38 | from contextlib import contextmanager |
|
38 | from contextlib import contextmanager | |
39 |
|
39 | |||
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 | # Constants |
|
41 | # Constants | |
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 |
|
43 | |||
44 | # Roughly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h, |
|
44 | # Roughly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h, | |
45 | # this is used as a bitmask to extract future-related code flags. |
|
45 | # this is used as a bitmask to extract future-related code flags. | |
46 | PyCF_MASK = functools.reduce(operator.or_, |
|
46 | PyCF_MASK = functools.reduce(operator.or_, | |
47 | (getattr(__future__, fname).compiler_flag |
|
47 | (getattr(__future__, fname).compiler_flag | |
48 | for fname in __future__.all_feature_names)) |
|
48 | for fname in __future__.all_feature_names)) | |
49 |
|
49 | |||
50 | #----------------------------------------------------------------------------- |
|
50 | #----------------------------------------------------------------------------- | |
51 | # Local utilities |
|
51 | # Local utilities | |
52 | #----------------------------------------------------------------------------- |
|
52 | #----------------------------------------------------------------------------- | |
53 |
|
53 | |||
54 | def code_name(code, number=0): |
|
54 | def code_name(code, number=0): | |
55 | """ Compute a (probably) unique name for code for caching. |
|
55 | """ Compute a (probably) unique name for code for caching. | |
56 |
|
56 | |||
57 | This now expects code to be unicode. |
|
57 | This now expects code to be unicode. | |
58 | """ |
|
58 | """ | |
59 | hash_digest = hashlib.sha1(code.encode("utf-8")).hexdigest() |
|
59 | hash_digest = hashlib.sha1(code.encode("utf-8")).hexdigest() | |
60 | # Include the number and 12 characters of the hash in the name. It's |
|
60 | # Include the number and 12 characters of the hash in the name. It's | |
61 | # pretty much impossible that in a single session we'll have collisions |
|
61 | # pretty much impossible that in a single session we'll have collisions | |
62 | # even with truncated hashes, and the full one makes tracebacks too long |
|
62 | # even with truncated hashes, and the full one makes tracebacks too long | |
63 | return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12]) |
|
63 | return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12]) | |
64 |
|
64 | |||
65 | #----------------------------------------------------------------------------- |
|
65 | #----------------------------------------------------------------------------- | |
66 | # Classes and functions |
|
66 | # Classes and functions | |
67 | #----------------------------------------------------------------------------- |
|
67 | #----------------------------------------------------------------------------- | |
68 |
|
68 | |||
69 | class CachingCompiler(codeop.Compile): |
|
69 | class CachingCompiler(codeop.Compile): | |
70 | """A compiler that caches code compiled from interactive statements. |
|
70 | """A compiler that caches code compiled from interactive statements. | |
71 | """ |
|
71 | """ | |
72 |
|
72 | |||
73 | def __init__(self): |
|
73 | def __init__(self): | |
74 | codeop.Compile.__init__(self) |
|
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 | # Caching a dictionary { filename: execution_count } for nicely |
|
76 | # Caching a dictionary { filename: execution_count } for nicely | |
96 | # rendered tracebacks. The filename corresponds to the filename |
|
77 | # rendered tracebacks. The filename corresponds to the filename | |
97 | # argument used for the builtins.compile function. |
|
78 | # argument used for the builtins.compile function. | |
98 | self._filename_map = {} |
|
79 | self._filename_map = {} | |
99 |
|
80 | |||
100 | def ast_parse(self, source, filename='<unknown>', symbol='exec'): |
|
81 | def ast_parse(self, source, filename='<unknown>', symbol='exec'): | |
101 | """Parse code to an AST with the current compiler flags active. |
|
82 | """Parse code to an AST with the current compiler flags active. | |
102 |
|
83 | |||
103 | Arguments are exactly the same as ast.parse (in the standard library), |
|
84 | Arguments are exactly the same as ast.parse (in the standard library), | |
104 | and are passed to the built-in compile function.""" |
|
85 | and are passed to the built-in compile function.""" | |
105 | return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1) |
|
86 | return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1) | |
106 |
|
87 | |||
107 | def reset_compiler_flags(self): |
|
88 | def reset_compiler_flags(self): | |
108 | """Reset compiler flags to default state.""" |
|
89 | """Reset compiler flags to default state.""" | |
109 | # This value is copied from codeop.Compile.__init__, so if that ever |
|
90 | # This value is copied from codeop.Compile.__init__, so if that ever | |
110 | # changes, it will need to be updated. |
|
91 | # changes, it will need to be updated. | |
111 | self.flags = codeop.PyCF_DONT_IMPLY_DEDENT |
|
92 | self.flags = codeop.PyCF_DONT_IMPLY_DEDENT | |
112 |
|
93 | |||
113 | @property |
|
94 | @property | |
114 | def compiler_flags(self): |
|
95 | def compiler_flags(self): | |
115 | """Flags currently active in the compilation process. |
|
96 | """Flags currently active in the compilation process. | |
116 | """ |
|
97 | """ | |
117 | return self.flags |
|
98 | return self.flags | |
118 |
|
99 | |||
119 | def get_code_name(self, raw_code, transformed_code, number): |
|
100 | def get_code_name(self, raw_code, transformed_code, number): | |
120 | """Compute filename given the code, and the cell number. |
|
101 | """Compute filename given the code, and the cell number. | |
121 |
|
102 | |||
122 | Parameters |
|
103 | Parameters | |
123 | ---------- |
|
104 | ---------- | |
124 | raw_code : str |
|
105 | raw_code : str | |
125 | The raw cell code. |
|
106 | The raw cell code. | |
126 | transformed_code : str |
|
107 | transformed_code : str | |
127 | The executable Python source code to cache and compile. |
|
108 | The executable Python source code to cache and compile. | |
128 | number : int |
|
109 | number : int | |
129 | A number which forms part of the code's name. Used for the execution |
|
110 | A number which forms part of the code's name. Used for the execution | |
130 | counter. |
|
111 | counter. | |
131 |
|
112 | |||
132 | Returns |
|
113 | Returns | |
133 | ------- |
|
114 | ------- | |
134 | The computed filename. |
|
115 | The computed filename. | |
135 | """ |
|
116 | """ | |
136 | return code_name(transformed_code, number) |
|
117 | return code_name(transformed_code, number) | |
137 |
|
118 | |||
138 | def cache(self, transformed_code, number=0, raw_code=None): |
|
119 | def cache(self, transformed_code, number=0, raw_code=None): | |
139 | """Make a name for a block of code, and cache the code. |
|
120 | """Make a name for a block of code, and cache the code. | |
140 |
|
121 | |||
141 | Parameters |
|
122 | Parameters | |
142 | ---------- |
|
123 | ---------- | |
143 | transformed_code : str |
|
124 | transformed_code : str | |
144 | The executable Python source code to cache and compile. |
|
125 | The executable Python source code to cache and compile. | |
145 | number : int |
|
126 | number : int | |
146 | A number which forms part of the code's name. Used for the execution |
|
127 | A number which forms part of the code's name. Used for the execution | |
147 | counter. |
|
128 | counter. | |
148 | raw_code : str |
|
129 | raw_code : str | |
149 | The raw code before transformation, if None, set to `transformed_code`. |
|
130 | The raw code before transformation, if None, set to `transformed_code`. | |
150 |
|
131 | |||
151 | Returns |
|
132 | Returns | |
152 | ------- |
|
133 | ------- | |
153 | The name of the cached code (as a string). Pass this as the filename |
|
134 | The name of the cached code (as a string). Pass this as the filename | |
154 | argument to compilation, so that tracebacks are correctly hooked up. |
|
135 | argument to compilation, so that tracebacks are correctly hooked up. | |
155 | """ |
|
136 | """ | |
156 | if raw_code is None: |
|
137 | if raw_code is None: | |
157 | raw_code = transformed_code |
|
138 | raw_code = transformed_code | |
158 |
|
139 | |||
159 | name = self.get_code_name(raw_code, transformed_code, number) |
|
140 | name = self.get_code_name(raw_code, transformed_code, number) | |
160 |
|
141 | |||
161 | # Save the execution count |
|
142 | # Save the execution count | |
162 | self._filename_map[name] = number |
|
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 | entry = ( |
|
156 | entry = ( | |
165 | len(transformed_code), |
|
157 | len(transformed_code), | |
166 |
|
|
158 | None, | |
167 | [line + "\n" for line in transformed_code.splitlines()], |
|
159 | [line + "\n" for line in transformed_code.splitlines()], | |
168 | name, |
|
160 | name, | |
169 | ) |
|
161 | ) | |
170 | linecache.cache[name] = entry |
|
162 | linecache.cache[name] = entry | |
171 | linecache._ipython_cache[name] = entry |
|
|||
172 | return name |
|
163 | return name | |
173 |
|
164 | |||
174 | @contextmanager |
|
165 | @contextmanager | |
175 | def extra_flags(self, flags): |
|
166 | def extra_flags(self, flags): | |
176 | ## bits that we'll set to 1 |
|
167 | ## bits that we'll set to 1 | |
177 | turn_on_bits = ~self.flags & flags |
|
168 | turn_on_bits = ~self.flags & flags | |
178 |
|
169 | |||
179 |
|
170 | |||
180 | self.flags = self.flags | flags |
|
171 | self.flags = self.flags | flags | |
181 | try: |
|
172 | try: | |
182 | yield |
|
173 | yield | |
183 | finally: |
|
174 | finally: | |
184 | # turn off only the bits we turned on so that something like |
|
175 | # turn off only the bits we turned on so that something like | |
185 | # __future__ that set flags stays. |
|
176 | # __future__ that set flags stays. | |
186 | self.flags &= ~turn_on_bits |
|
177 | self.flags &= ~turn_on_bits | |
187 |
|
178 | |||
188 |
|
179 | |||
189 | def check_linecache_ipython(*args): |
|
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 |
|
192 | import warnings | |
193 | linecache._checkcache_ori(*args) |
|
193 | warnings.warn( | |
194 | # Then, update back the cache with our data, so that tracebacks related |
|
194 | 'Just call linecache.checkcache() directly.', | |
195 | # to our compiled codes can be produced. |
|
195 | DeprecationWarning | |
196 | linecache.cache.update(linecache._ipython_cache) |
|
196 | ) | |
|
197 | linecache.checkcache() |
@@ -1,3825 +1,3824 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Main IPython class.""" |
|
2 | """Main IPython class.""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 |
|
13 | |||
14 | import abc |
|
14 | import abc | |
15 | import ast |
|
15 | import ast | |
16 | import atexit |
|
16 | import atexit | |
17 | import bdb |
|
17 | import bdb | |
18 | import builtins as builtin_mod |
|
18 | import builtins as builtin_mod | |
19 | import functools |
|
19 | import functools | |
20 | import inspect |
|
20 | import inspect | |
21 | import os |
|
21 | import os | |
22 | import re |
|
22 | import re | |
23 | import runpy |
|
23 | import runpy | |
24 | import subprocess |
|
24 | import subprocess | |
25 | import sys |
|
25 | import sys | |
26 | import tempfile |
|
26 | import tempfile | |
27 | import traceback |
|
27 | import traceback | |
28 | import types |
|
28 | import types | |
29 | import warnings |
|
29 | import warnings | |
30 | from ast import stmt |
|
30 | from ast import stmt | |
31 | from io import open as io_open |
|
31 | from io import open as io_open | |
32 | from logging import error |
|
32 | from logging import error | |
33 | from pathlib import Path |
|
33 | from pathlib import Path | |
34 | from typing import Callable |
|
34 | from typing import Callable | |
35 | from typing import List as ListType |
|
35 | from typing import List as ListType | |
36 | from typing import Optional, Tuple |
|
36 | from typing import Optional, Tuple | |
37 | from warnings import warn |
|
37 | from warnings import warn | |
38 |
|
38 | |||
39 | from pickleshare import PickleShareDB |
|
39 | from pickleshare import PickleShareDB | |
40 | from tempfile import TemporaryDirectory |
|
40 | from tempfile import TemporaryDirectory | |
41 | from traitlets import ( |
|
41 | from traitlets import ( | |
42 | Any, |
|
42 | Any, | |
43 | Bool, |
|
43 | Bool, | |
44 | CaselessStrEnum, |
|
44 | CaselessStrEnum, | |
45 | Dict, |
|
45 | Dict, | |
46 | Enum, |
|
46 | Enum, | |
47 | Instance, |
|
47 | Instance, | |
48 | Integer, |
|
48 | Integer, | |
49 | List, |
|
49 | List, | |
50 | Type, |
|
50 | Type, | |
51 | Unicode, |
|
51 | Unicode, | |
52 | default, |
|
52 | default, | |
53 | observe, |
|
53 | observe, | |
54 | validate, |
|
54 | validate, | |
55 | ) |
|
55 | ) | |
56 | from traitlets.config.configurable import SingletonConfigurable |
|
56 | from traitlets.config.configurable import SingletonConfigurable | |
57 | from traitlets.utils.importstring import import_item |
|
57 | from traitlets.utils.importstring import import_item | |
58 |
|
58 | |||
59 | import IPython.core.hooks |
|
59 | import IPython.core.hooks | |
60 | from IPython.core import magic, oinspect, page, prefilter, ultratb |
|
60 | from IPython.core import magic, oinspect, page, prefilter, ultratb | |
61 | from IPython.core.alias import Alias, AliasManager |
|
61 | from IPython.core.alias import Alias, AliasManager | |
62 | from IPython.core.autocall import ExitAutocall |
|
62 | from IPython.core.autocall import ExitAutocall | |
63 | from IPython.core.builtin_trap import BuiltinTrap |
|
63 | from IPython.core.builtin_trap import BuiltinTrap | |
64 |
from IPython.core.compilerop import CachingCompiler |
|
64 | from IPython.core.compilerop import CachingCompiler | |
65 | from IPython.core.debugger import InterruptiblePdb |
|
65 | from IPython.core.debugger import InterruptiblePdb | |
66 | from IPython.core.display_trap import DisplayTrap |
|
66 | from IPython.core.display_trap import DisplayTrap | |
67 | from IPython.core.displayhook import DisplayHook |
|
67 | from IPython.core.displayhook import DisplayHook | |
68 | from IPython.core.displaypub import DisplayPublisher |
|
68 | from IPython.core.displaypub import DisplayPublisher | |
69 | from IPython.core.error import InputRejected, UsageError |
|
69 | from IPython.core.error import InputRejected, UsageError | |
70 | from IPython.core.events import EventManager, available_events |
|
70 | from IPython.core.events import EventManager, available_events | |
71 | from IPython.core.extensions import ExtensionManager |
|
71 | from IPython.core.extensions import ExtensionManager | |
72 | from IPython.core.formatters import DisplayFormatter |
|
72 | from IPython.core.formatters import DisplayFormatter | |
73 | from IPython.core.history import HistoryManager |
|
73 | from IPython.core.history import HistoryManager | |
74 | from IPython.core.inputtransformer2 import ESC_MAGIC, ESC_MAGIC2 |
|
74 | from IPython.core.inputtransformer2 import ESC_MAGIC, ESC_MAGIC2 | |
75 | from IPython.core.logger import Logger |
|
75 | from IPython.core.logger import Logger | |
76 | from IPython.core.macro import Macro |
|
76 | from IPython.core.macro import Macro | |
77 | from IPython.core.payload import PayloadManager |
|
77 | from IPython.core.payload import PayloadManager | |
78 | from IPython.core.prefilter import PrefilterManager |
|
78 | from IPython.core.prefilter import PrefilterManager | |
79 | from IPython.core.profiledir import ProfileDir |
|
79 | from IPython.core.profiledir import ProfileDir | |
80 | from IPython.core.usage import default_banner |
|
80 | from IPython.core.usage import default_banner | |
81 | from IPython.display import display |
|
81 | from IPython.display import display | |
82 | from IPython.paths import get_ipython_dir |
|
82 | from IPython.paths import get_ipython_dir | |
83 | from IPython.testing.skipdoctest import skip_doctest |
|
83 | from IPython.testing.skipdoctest import skip_doctest | |
84 | from IPython.utils import PyColorize, io, openpy, py3compat |
|
84 | from IPython.utils import PyColorize, io, openpy, py3compat | |
85 | from IPython.utils.decorators import undoc |
|
85 | from IPython.utils.decorators import undoc | |
86 | from IPython.utils.io import ask_yes_no |
|
86 | from IPython.utils.io import ask_yes_no | |
87 | from IPython.utils.ipstruct import Struct |
|
87 | from IPython.utils.ipstruct import Struct | |
88 | from IPython.utils.path import ensure_dir_exists, get_home_dir, get_py_filename |
|
88 | from IPython.utils.path import ensure_dir_exists, get_home_dir, get_py_filename | |
89 | from IPython.utils.process import getoutput, system |
|
89 | from IPython.utils.process import getoutput, system | |
90 | from IPython.utils.strdispatch import StrDispatch |
|
90 | from IPython.utils.strdispatch import StrDispatch | |
91 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
91 | from IPython.utils.syspathcontext import prepended_to_syspath | |
92 | from IPython.utils.text import DollarFormatter, LSString, SList, format_screen |
|
92 | from IPython.utils.text import DollarFormatter, LSString, SList, format_screen | |
93 |
|
93 | |||
94 | sphinxify: Optional[Callable] |
|
94 | sphinxify: Optional[Callable] | |
95 |
|
95 | |||
96 | try: |
|
96 | try: | |
97 | import docrepr.sphinxify as sphx |
|
97 | import docrepr.sphinxify as sphx | |
98 |
|
98 | |||
99 | def sphinxify(oinfo): |
|
99 | def sphinxify(oinfo): | |
100 | wrapped_docstring = sphx.wrap_main_docstring(oinfo) |
|
100 | wrapped_docstring = sphx.wrap_main_docstring(oinfo) | |
101 |
|
101 | |||
102 | def sphinxify_docstring(docstring): |
|
102 | def sphinxify_docstring(docstring): | |
103 | with TemporaryDirectory() as dirname: |
|
103 | with TemporaryDirectory() as dirname: | |
104 | return { |
|
104 | return { | |
105 | "text/html": sphx.sphinxify(wrapped_docstring, dirname), |
|
105 | "text/html": sphx.sphinxify(wrapped_docstring, dirname), | |
106 | "text/plain": docstring, |
|
106 | "text/plain": docstring, | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | return sphinxify_docstring |
|
109 | return sphinxify_docstring | |
110 | except ImportError: |
|
110 | except ImportError: | |
111 | sphinxify = None |
|
111 | sphinxify = None | |
112 |
|
112 | |||
113 |
|
113 | |||
114 | class ProvisionalWarning(DeprecationWarning): |
|
114 | class ProvisionalWarning(DeprecationWarning): | |
115 | """ |
|
115 | """ | |
116 | Warning class for unstable features |
|
116 | Warning class for unstable features | |
117 | """ |
|
117 | """ | |
118 | pass |
|
118 | pass | |
119 |
|
119 | |||
120 | from ast import Module |
|
120 | from ast import Module | |
121 |
|
121 | |||
122 | _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign) |
|
122 | _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign) | |
123 | _single_targets_nodes = (ast.AugAssign, ast.AnnAssign) |
|
123 | _single_targets_nodes = (ast.AugAssign, ast.AnnAssign) | |
124 |
|
124 | |||
125 | #----------------------------------------------------------------------------- |
|
125 | #----------------------------------------------------------------------------- | |
126 | # Await Helpers |
|
126 | # Await Helpers | |
127 | #----------------------------------------------------------------------------- |
|
127 | #----------------------------------------------------------------------------- | |
128 |
|
128 | |||
129 | # we still need to run things using the asyncio eventloop, but there is no |
|
129 | # we still need to run things using the asyncio eventloop, but there is no | |
130 | # async integration |
|
130 | # async integration | |
131 | from .async_helpers import ( |
|
131 | from .async_helpers import ( | |
132 | _asyncio_runner, |
|
132 | _asyncio_runner, | |
133 | _curio_runner, |
|
133 | _curio_runner, | |
134 | _pseudo_sync_runner, |
|
134 | _pseudo_sync_runner, | |
135 | _should_be_async, |
|
135 | _should_be_async, | |
136 | _trio_runner, |
|
136 | _trio_runner, | |
137 | ) |
|
137 | ) | |
138 |
|
138 | |||
139 | #----------------------------------------------------------------------------- |
|
139 | #----------------------------------------------------------------------------- | |
140 | # Globals |
|
140 | # Globals | |
141 | #----------------------------------------------------------------------------- |
|
141 | #----------------------------------------------------------------------------- | |
142 |
|
142 | |||
143 | # compiled regexps for autoindent management |
|
143 | # compiled regexps for autoindent management | |
144 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
144 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
145 |
|
145 | |||
146 | #----------------------------------------------------------------------------- |
|
146 | #----------------------------------------------------------------------------- | |
147 | # Utilities |
|
147 | # Utilities | |
148 | #----------------------------------------------------------------------------- |
|
148 | #----------------------------------------------------------------------------- | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | def is_integer_string(s: str): |
|
151 | def is_integer_string(s: str): | |
152 | """ |
|
152 | """ | |
153 | Variant of "str.isnumeric()" that allow negative values and other ints. |
|
153 | Variant of "str.isnumeric()" that allow negative values and other ints. | |
154 | """ |
|
154 | """ | |
155 | try: |
|
155 | try: | |
156 | int(s) |
|
156 | int(s) | |
157 | return True |
|
157 | return True | |
158 | except ValueError: |
|
158 | except ValueError: | |
159 | return False |
|
159 | return False | |
160 | raise ValueError("Unexpected error") |
|
160 | raise ValueError("Unexpected error") | |
161 |
|
161 | |||
162 |
|
162 | |||
163 | @undoc |
|
163 | @undoc | |
164 | def softspace(file, newvalue): |
|
164 | def softspace(file, newvalue): | |
165 | """Copied from code.py, to remove the dependency""" |
|
165 | """Copied from code.py, to remove the dependency""" | |
166 |
|
166 | |||
167 | oldvalue = 0 |
|
167 | oldvalue = 0 | |
168 | try: |
|
168 | try: | |
169 | oldvalue = file.softspace |
|
169 | oldvalue = file.softspace | |
170 | except AttributeError: |
|
170 | except AttributeError: | |
171 | pass |
|
171 | pass | |
172 | try: |
|
172 | try: | |
173 | file.softspace = newvalue |
|
173 | file.softspace = newvalue | |
174 | except (AttributeError, TypeError): |
|
174 | except (AttributeError, TypeError): | |
175 | # "attribute-less object" or "read-only attributes" |
|
175 | # "attribute-less object" or "read-only attributes" | |
176 | pass |
|
176 | pass | |
177 | return oldvalue |
|
177 | return oldvalue | |
178 |
|
178 | |||
179 | @undoc |
|
179 | @undoc | |
180 | def no_op(*a, **kw): |
|
180 | def no_op(*a, **kw): | |
181 | pass |
|
181 | pass | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | class SpaceInInput(Exception): pass |
|
184 | class SpaceInInput(Exception): pass | |
185 |
|
185 | |||
186 |
|
186 | |||
187 | class SeparateUnicode(Unicode): |
|
187 | class SeparateUnicode(Unicode): | |
188 | r"""A Unicode subclass to validate separate_in, separate_out, etc. |
|
188 | r"""A Unicode subclass to validate separate_in, separate_out, etc. | |
189 |
|
189 | |||
190 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
190 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. | |
191 | """ |
|
191 | """ | |
192 |
|
192 | |||
193 | def validate(self, obj, value): |
|
193 | def validate(self, obj, value): | |
194 | if value == '0': value = '' |
|
194 | if value == '0': value = '' | |
195 | value = value.replace('\\n','\n') |
|
195 | value = value.replace('\\n','\n') | |
196 | return super(SeparateUnicode, self).validate(obj, value) |
|
196 | return super(SeparateUnicode, self).validate(obj, value) | |
197 |
|
197 | |||
198 |
|
198 | |||
199 | @undoc |
|
199 | @undoc | |
200 | class DummyMod(object): |
|
200 | class DummyMod(object): | |
201 | """A dummy module used for IPython's interactive module when |
|
201 | """A dummy module used for IPython's interactive module when | |
202 | a namespace must be assigned to the module's __dict__.""" |
|
202 | a namespace must be assigned to the module's __dict__.""" | |
203 | __spec__ = None |
|
203 | __spec__ = None | |
204 |
|
204 | |||
205 |
|
205 | |||
206 | class ExecutionInfo(object): |
|
206 | class ExecutionInfo(object): | |
207 | """The arguments used for a call to :meth:`InteractiveShell.run_cell` |
|
207 | """The arguments used for a call to :meth:`InteractiveShell.run_cell` | |
208 |
|
208 | |||
209 | Stores information about what is going to happen. |
|
209 | Stores information about what is going to happen. | |
210 | """ |
|
210 | """ | |
211 | raw_cell = None |
|
211 | raw_cell = None | |
212 | store_history = False |
|
212 | store_history = False | |
213 | silent = False |
|
213 | silent = False | |
214 | shell_futures = True |
|
214 | shell_futures = True | |
215 | cell_id = None |
|
215 | cell_id = None | |
216 |
|
216 | |||
217 | def __init__(self, raw_cell, store_history, silent, shell_futures, cell_id): |
|
217 | def __init__(self, raw_cell, store_history, silent, shell_futures, cell_id): | |
218 | self.raw_cell = raw_cell |
|
218 | self.raw_cell = raw_cell | |
219 | self.store_history = store_history |
|
219 | self.store_history = store_history | |
220 | self.silent = silent |
|
220 | self.silent = silent | |
221 | self.shell_futures = shell_futures |
|
221 | self.shell_futures = shell_futures | |
222 | self.cell_id = cell_id |
|
222 | self.cell_id = cell_id | |
223 |
|
223 | |||
224 | def __repr__(self): |
|
224 | def __repr__(self): | |
225 | name = self.__class__.__qualname__ |
|
225 | name = self.__class__.__qualname__ | |
226 | raw_cell = ( |
|
226 | raw_cell = ( | |
227 | (self.raw_cell[:50] + "..") if len(self.raw_cell) > 50 else self.raw_cell |
|
227 | (self.raw_cell[:50] + "..") if len(self.raw_cell) > 50 else self.raw_cell | |
228 | ) |
|
228 | ) | |
229 | return ( |
|
229 | return ( | |
230 | '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s cell_id=%s>' |
|
230 | '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s cell_id=%s>' | |
231 | % ( |
|
231 | % ( | |
232 | name, |
|
232 | name, | |
233 | id(self), |
|
233 | id(self), | |
234 | raw_cell, |
|
234 | raw_cell, | |
235 | self.store_history, |
|
235 | self.store_history, | |
236 | self.silent, |
|
236 | self.silent, | |
237 | self.shell_futures, |
|
237 | self.shell_futures, | |
238 | self.cell_id, |
|
238 | self.cell_id, | |
239 | ) |
|
239 | ) | |
240 | ) |
|
240 | ) | |
241 |
|
241 | |||
242 |
|
242 | |||
243 | class ExecutionResult(object): |
|
243 | class ExecutionResult(object): | |
244 | """The result of a call to :meth:`InteractiveShell.run_cell` |
|
244 | """The result of a call to :meth:`InteractiveShell.run_cell` | |
245 |
|
245 | |||
246 | Stores information about what took place. |
|
246 | Stores information about what took place. | |
247 | """ |
|
247 | """ | |
248 | execution_count = None |
|
248 | execution_count = None | |
249 | error_before_exec = None |
|
249 | error_before_exec = None | |
250 | error_in_exec: Optional[BaseException] = None |
|
250 | error_in_exec: Optional[BaseException] = None | |
251 | info = None |
|
251 | info = None | |
252 | result = None |
|
252 | result = None | |
253 |
|
253 | |||
254 | def __init__(self, info): |
|
254 | def __init__(self, info): | |
255 | self.info = info |
|
255 | self.info = info | |
256 |
|
256 | |||
257 | @property |
|
257 | @property | |
258 | def success(self): |
|
258 | def success(self): | |
259 | return (self.error_before_exec is None) and (self.error_in_exec is None) |
|
259 | return (self.error_before_exec is None) and (self.error_in_exec is None) | |
260 |
|
260 | |||
261 | def raise_error(self): |
|
261 | def raise_error(self): | |
262 | """Reraises error if `success` is `False`, otherwise does nothing""" |
|
262 | """Reraises error if `success` is `False`, otherwise does nothing""" | |
263 | if self.error_before_exec is not None: |
|
263 | if self.error_before_exec is not None: | |
264 | raise self.error_before_exec |
|
264 | raise self.error_before_exec | |
265 | if self.error_in_exec is not None: |
|
265 | if self.error_in_exec is not None: | |
266 | raise self.error_in_exec |
|
266 | raise self.error_in_exec | |
267 |
|
267 | |||
268 | def __repr__(self): |
|
268 | def __repr__(self): | |
269 | name = self.__class__.__qualname__ |
|
269 | name = self.__class__.__qualname__ | |
270 | return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\ |
|
270 | return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\ | |
271 | (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result)) |
|
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 | class InteractiveShell(SingletonConfigurable): |
|
274 | class InteractiveShell(SingletonConfigurable): | |
275 | """An enhanced, interactive shell for Python.""" |
|
275 | """An enhanced, interactive shell for Python.""" | |
276 |
|
276 | |||
277 | _instance = None |
|
277 | _instance = None | |
278 |
|
278 | |||
279 | ast_transformers = List([], help= |
|
279 | ast_transformers = List([], help= | |
280 | """ |
|
280 | """ | |
281 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
281 | A list of ast.NodeTransformer subclass instances, which will be applied | |
282 | to user input before code is run. |
|
282 | to user input before code is run. | |
283 | """ |
|
283 | """ | |
284 | ).tag(config=True) |
|
284 | ).tag(config=True) | |
285 |
|
285 | |||
286 | autocall = Enum((0,1,2), default_value=0, help= |
|
286 | autocall = Enum((0,1,2), default_value=0, help= | |
287 | """ |
|
287 | """ | |
288 | Make IPython automatically call any callable object even if you didn't |
|
288 | Make IPython automatically call any callable object even if you didn't | |
289 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
289 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' | |
290 | automatically. The value can be '0' to disable the feature, '1' for |
|
290 | automatically. The value can be '0' to disable the feature, '1' for | |
291 | 'smart' autocall, where it is not applied if there are no more |
|
291 | 'smart' autocall, where it is not applied if there are no more | |
292 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
292 | arguments on the line, and '2' for 'full' autocall, where all callable | |
293 | objects are automatically called (even if no arguments are present). |
|
293 | objects are automatically called (even if no arguments are present). | |
294 | """ |
|
294 | """ | |
295 | ).tag(config=True) |
|
295 | ).tag(config=True) | |
296 |
|
296 | |||
297 | autoindent = Bool(True, help= |
|
297 | autoindent = Bool(True, help= | |
298 | """ |
|
298 | """ | |
299 | Autoindent IPython code entered interactively. |
|
299 | Autoindent IPython code entered interactively. | |
300 | """ |
|
300 | """ | |
301 | ).tag(config=True) |
|
301 | ).tag(config=True) | |
302 |
|
302 | |||
303 | autoawait = Bool(True, help= |
|
303 | autoawait = Bool(True, help= | |
304 | """ |
|
304 | """ | |
305 | Automatically run await statement in the top level repl. |
|
305 | Automatically run await statement in the top level repl. | |
306 | """ |
|
306 | """ | |
307 | ).tag(config=True) |
|
307 | ).tag(config=True) | |
308 |
|
308 | |||
309 | loop_runner_map ={ |
|
309 | loop_runner_map ={ | |
310 | 'asyncio':(_asyncio_runner, True), |
|
310 | 'asyncio':(_asyncio_runner, True), | |
311 | 'curio':(_curio_runner, True), |
|
311 | 'curio':(_curio_runner, True), | |
312 | 'trio':(_trio_runner, True), |
|
312 | 'trio':(_trio_runner, True), | |
313 | 'sync': (_pseudo_sync_runner, False) |
|
313 | 'sync': (_pseudo_sync_runner, False) | |
314 | } |
|
314 | } | |
315 |
|
315 | |||
316 | loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner", |
|
316 | loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner", | |
317 | allow_none=True, |
|
317 | allow_none=True, | |
318 | help="""Select the loop runner that will be used to execute top-level asynchronous code""" |
|
318 | help="""Select the loop runner that will be used to execute top-level asynchronous code""" | |
319 | ).tag(config=True) |
|
319 | ).tag(config=True) | |
320 |
|
320 | |||
321 | @default('loop_runner') |
|
321 | @default('loop_runner') | |
322 | def _default_loop_runner(self): |
|
322 | def _default_loop_runner(self): | |
323 | return import_item("IPython.core.interactiveshell._asyncio_runner") |
|
323 | return import_item("IPython.core.interactiveshell._asyncio_runner") | |
324 |
|
324 | |||
325 | @validate('loop_runner') |
|
325 | @validate('loop_runner') | |
326 | def _import_runner(self, proposal): |
|
326 | def _import_runner(self, proposal): | |
327 | if isinstance(proposal.value, str): |
|
327 | if isinstance(proposal.value, str): | |
328 | if proposal.value in self.loop_runner_map: |
|
328 | if proposal.value in self.loop_runner_map: | |
329 | runner, autoawait = self.loop_runner_map[proposal.value] |
|
329 | runner, autoawait = self.loop_runner_map[proposal.value] | |
330 | self.autoawait = autoawait |
|
330 | self.autoawait = autoawait | |
331 | return runner |
|
331 | return runner | |
332 | runner = import_item(proposal.value) |
|
332 | runner = import_item(proposal.value) | |
333 | if not callable(runner): |
|
333 | if not callable(runner): | |
334 | raise ValueError('loop_runner must be callable') |
|
334 | raise ValueError('loop_runner must be callable') | |
335 | return runner |
|
335 | return runner | |
336 | if not callable(proposal.value): |
|
336 | if not callable(proposal.value): | |
337 | raise ValueError('loop_runner must be callable') |
|
337 | raise ValueError('loop_runner must be callable') | |
338 | return proposal.value |
|
338 | return proposal.value | |
339 |
|
339 | |||
340 | automagic = Bool(True, help= |
|
340 | automagic = Bool(True, help= | |
341 | """ |
|
341 | """ | |
342 | Enable magic commands to be called without the leading %. |
|
342 | Enable magic commands to be called without the leading %. | |
343 | """ |
|
343 | """ | |
344 | ).tag(config=True) |
|
344 | ).tag(config=True) | |
345 |
|
345 | |||
346 | banner1 = Unicode(default_banner, |
|
346 | banner1 = Unicode(default_banner, | |
347 | help="""The part of the banner to be printed before the profile""" |
|
347 | help="""The part of the banner to be printed before the profile""" | |
348 | ).tag(config=True) |
|
348 | ).tag(config=True) | |
349 | banner2 = Unicode('', |
|
349 | banner2 = Unicode('', | |
350 | help="""The part of the banner to be printed after the profile""" |
|
350 | help="""The part of the banner to be printed after the profile""" | |
351 | ).tag(config=True) |
|
351 | ).tag(config=True) | |
352 |
|
352 | |||
353 | cache_size = Integer(1000, help= |
|
353 | cache_size = Integer(1000, help= | |
354 | """ |
|
354 | """ | |
355 | Set the size of the output cache. The default is 1000, you can |
|
355 | Set the size of the output cache. The default is 1000, you can | |
356 | change it permanently in your config file. Setting it to 0 completely |
|
356 | change it permanently in your config file. Setting it to 0 completely | |
357 | disables the caching system, and the minimum value accepted is 3 (if |
|
357 | disables the caching system, and the minimum value accepted is 3 (if | |
358 | you provide a value less than 3, it is reset to 0 and a warning is |
|
358 | you provide a value less than 3, it is reset to 0 and a warning is | |
359 | issued). This limit is defined because otherwise you'll spend more |
|
359 | issued). This limit is defined because otherwise you'll spend more | |
360 | time re-flushing a too small cache than working |
|
360 | time re-flushing a too small cache than working | |
361 | """ |
|
361 | """ | |
362 | ).tag(config=True) |
|
362 | ).tag(config=True) | |
363 | color_info = Bool(True, help= |
|
363 | color_info = Bool(True, help= | |
364 | """ |
|
364 | """ | |
365 | Use colors for displaying information about objects. Because this |
|
365 | Use colors for displaying information about objects. Because this | |
366 | information is passed through a pager (like 'less'), and some pagers |
|
366 | information is passed through a pager (like 'less'), and some pagers | |
367 | get confused with color codes, this capability can be turned off. |
|
367 | get confused with color codes, this capability can be turned off. | |
368 | """ |
|
368 | """ | |
369 | ).tag(config=True) |
|
369 | ).tag(config=True) | |
370 | colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'), |
|
370 | colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'), | |
371 | default_value='Neutral', |
|
371 | default_value='Neutral', | |
372 | help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)." |
|
372 | help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)." | |
373 | ).tag(config=True) |
|
373 | ).tag(config=True) | |
374 | debug = Bool(False).tag(config=True) |
|
374 | debug = Bool(False).tag(config=True) | |
375 | disable_failing_post_execute = Bool(False, |
|
375 | disable_failing_post_execute = Bool(False, | |
376 | help="Don't call post-execute functions that have failed in the past." |
|
376 | help="Don't call post-execute functions that have failed in the past." | |
377 | ).tag(config=True) |
|
377 | ).tag(config=True) | |
378 | display_formatter = Instance(DisplayFormatter, allow_none=True) |
|
378 | display_formatter = Instance(DisplayFormatter, allow_none=True) | |
379 | displayhook_class = Type(DisplayHook) |
|
379 | displayhook_class = Type(DisplayHook) | |
380 | display_pub_class = Type(DisplayPublisher) |
|
380 | display_pub_class = Type(DisplayPublisher) | |
381 | compiler_class = Type(CachingCompiler) |
|
381 | compiler_class = Type(CachingCompiler) | |
382 |
|
382 | |||
383 | sphinxify_docstring = Bool(False, help= |
|
383 | sphinxify_docstring = Bool(False, help= | |
384 | """ |
|
384 | """ | |
385 | Enables rich html representation of docstrings. (This requires the |
|
385 | Enables rich html representation of docstrings. (This requires the | |
386 | docrepr module). |
|
386 | docrepr module). | |
387 | """).tag(config=True) |
|
387 | """).tag(config=True) | |
388 |
|
388 | |||
389 | @observe("sphinxify_docstring") |
|
389 | @observe("sphinxify_docstring") | |
390 | def _sphinxify_docstring_changed(self, change): |
|
390 | def _sphinxify_docstring_changed(self, change): | |
391 | if change['new']: |
|
391 | if change['new']: | |
392 | warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning) |
|
392 | warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning) | |
393 |
|
393 | |||
394 | enable_html_pager = Bool(False, help= |
|
394 | enable_html_pager = Bool(False, help= | |
395 | """ |
|
395 | """ | |
396 | (Provisional API) enables html representation in mime bundles sent |
|
396 | (Provisional API) enables html representation in mime bundles sent | |
397 | to pagers. |
|
397 | to pagers. | |
398 | """).tag(config=True) |
|
398 | """).tag(config=True) | |
399 |
|
399 | |||
400 | @observe("enable_html_pager") |
|
400 | @observe("enable_html_pager") | |
401 | def _enable_html_pager_changed(self, change): |
|
401 | def _enable_html_pager_changed(self, change): | |
402 | if change['new']: |
|
402 | if change['new']: | |
403 | warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning) |
|
403 | warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning) | |
404 |
|
404 | |||
405 | data_pub_class = None |
|
405 | data_pub_class = None | |
406 |
|
406 | |||
407 | exit_now = Bool(False) |
|
407 | exit_now = Bool(False) | |
408 | exiter = Instance(ExitAutocall) |
|
408 | exiter = Instance(ExitAutocall) | |
409 | @default('exiter') |
|
409 | @default('exiter') | |
410 | def _exiter_default(self): |
|
410 | def _exiter_default(self): | |
411 | return ExitAutocall(self) |
|
411 | return ExitAutocall(self) | |
412 | # Monotonically increasing execution counter |
|
412 | # Monotonically increasing execution counter | |
413 | execution_count = Integer(1) |
|
413 | execution_count = Integer(1) | |
414 | filename = Unicode("<ipython console>") |
|
414 | filename = Unicode("<ipython console>") | |
415 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ |
|
415 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ | |
416 |
|
416 | |||
417 | # Used to transform cells before running them, and check whether code is complete |
|
417 | # Used to transform cells before running them, and check whether code is complete | |
418 | input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager', |
|
418 | input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager', | |
419 | ()) |
|
419 | ()) | |
420 |
|
420 | |||
421 | @property |
|
421 | @property | |
422 | def input_transformers_cleanup(self): |
|
422 | def input_transformers_cleanup(self): | |
423 | return self.input_transformer_manager.cleanup_transforms |
|
423 | return self.input_transformer_manager.cleanup_transforms | |
424 |
|
424 | |||
425 | input_transformers_post = List([], |
|
425 | input_transformers_post = List([], | |
426 | help="A list of string input transformers, to be applied after IPython's " |
|
426 | help="A list of string input transformers, to be applied after IPython's " | |
427 | "own input transformations." |
|
427 | "own input transformations." | |
428 | ) |
|
428 | ) | |
429 |
|
429 | |||
430 | @property |
|
430 | @property | |
431 | def input_splitter(self): |
|
431 | def input_splitter(self): | |
432 | """Make this available for backward compatibility (pre-7.0 release) with existing code. |
|
432 | """Make this available for backward compatibility (pre-7.0 release) with existing code. | |
433 |
|
433 | |||
434 | For example, ipykernel ipykernel currently uses |
|
434 | For example, ipykernel ipykernel currently uses | |
435 | `shell.input_splitter.check_complete` |
|
435 | `shell.input_splitter.check_complete` | |
436 | """ |
|
436 | """ | |
437 | from warnings import warn |
|
437 | from warnings import warn | |
438 | warn("`input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`.", |
|
438 | warn("`input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`.", | |
439 | DeprecationWarning, stacklevel=2 |
|
439 | DeprecationWarning, stacklevel=2 | |
440 | ) |
|
440 | ) | |
441 | return self.input_transformer_manager |
|
441 | return self.input_transformer_manager | |
442 |
|
442 | |||
443 | logstart = Bool(False, help= |
|
443 | logstart = Bool(False, help= | |
444 | """ |
|
444 | """ | |
445 | Start logging to the default log file in overwrite mode. |
|
445 | Start logging to the default log file in overwrite mode. | |
446 | Use `logappend` to specify a log file to **append** logs to. |
|
446 | Use `logappend` to specify a log file to **append** logs to. | |
447 | """ |
|
447 | """ | |
448 | ).tag(config=True) |
|
448 | ).tag(config=True) | |
449 | logfile = Unicode('', help= |
|
449 | logfile = Unicode('', help= | |
450 | """ |
|
450 | """ | |
451 | The name of the logfile to use. |
|
451 | The name of the logfile to use. | |
452 | """ |
|
452 | """ | |
453 | ).tag(config=True) |
|
453 | ).tag(config=True) | |
454 | logappend = Unicode('', help= |
|
454 | logappend = Unicode('', help= | |
455 | """ |
|
455 | """ | |
456 | Start logging to the given file in append mode. |
|
456 | Start logging to the given file in append mode. | |
457 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
457 | Use `logfile` to specify a log file to **overwrite** logs to. | |
458 | """ |
|
458 | """ | |
459 | ).tag(config=True) |
|
459 | ).tag(config=True) | |
460 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
460 | object_info_string_level = Enum((0,1,2), default_value=0, | |
461 | ).tag(config=True) |
|
461 | ).tag(config=True) | |
462 | pdb = Bool(False, help= |
|
462 | pdb = Bool(False, help= | |
463 | """ |
|
463 | """ | |
464 | Automatically call the pdb debugger after every exception. |
|
464 | Automatically call the pdb debugger after every exception. | |
465 | """ |
|
465 | """ | |
466 | ).tag(config=True) |
|
466 | ).tag(config=True) | |
467 | display_page = Bool(False, |
|
467 | display_page = Bool(False, | |
468 | help="""If True, anything that would be passed to the pager |
|
468 | help="""If True, anything that would be passed to the pager | |
469 | will be displayed as regular output instead.""" |
|
469 | will be displayed as regular output instead.""" | |
470 | ).tag(config=True) |
|
470 | ).tag(config=True) | |
471 |
|
471 | |||
472 |
|
472 | |||
473 | show_rewritten_input = Bool(True, |
|
473 | show_rewritten_input = Bool(True, | |
474 | help="Show rewritten input, e.g. for autocall." |
|
474 | help="Show rewritten input, e.g. for autocall." | |
475 | ).tag(config=True) |
|
475 | ).tag(config=True) | |
476 |
|
476 | |||
477 | quiet = Bool(False).tag(config=True) |
|
477 | quiet = Bool(False).tag(config=True) | |
478 |
|
478 | |||
479 | history_length = Integer(10000, |
|
479 | history_length = Integer(10000, | |
480 | help='Total length of command history' |
|
480 | help='Total length of command history' | |
481 | ).tag(config=True) |
|
481 | ).tag(config=True) | |
482 |
|
482 | |||
483 | history_load_length = Integer(1000, help= |
|
483 | history_load_length = Integer(1000, help= | |
484 | """ |
|
484 | """ | |
485 | The number of saved history entries to be loaded |
|
485 | The number of saved history entries to be loaded | |
486 | into the history buffer at startup. |
|
486 | into the history buffer at startup. | |
487 | """ |
|
487 | """ | |
488 | ).tag(config=True) |
|
488 | ).tag(config=True) | |
489 |
|
489 | |||
490 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'], |
|
490 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'], | |
491 | default_value='last_expr', |
|
491 | default_value='last_expr', | |
492 | help=""" |
|
492 | help=""" | |
493 | 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying |
|
493 | 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying | |
494 | which nodes should be run interactively (displaying output from expressions). |
|
494 | which nodes should be run interactively (displaying output from expressions). | |
495 | """ |
|
495 | """ | |
496 | ).tag(config=True) |
|
496 | ).tag(config=True) | |
497 |
|
497 | |||
498 | warn_venv = Bool( |
|
498 | warn_venv = Bool( | |
499 | True, |
|
499 | True, | |
500 | help="Warn if running in a virtual environment with no IPython installed (so IPython from the global environment is used).", |
|
500 | help="Warn if running in a virtual environment with no IPython installed (so IPython from the global environment is used).", | |
501 | ).tag(config=True) |
|
501 | ).tag(config=True) | |
502 |
|
502 | |||
503 | # TODO: this part of prompt management should be moved to the frontends. |
|
503 | # TODO: this part of prompt management should be moved to the frontends. | |
504 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
504 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
505 | separate_in = SeparateUnicode('\n').tag(config=True) |
|
505 | separate_in = SeparateUnicode('\n').tag(config=True) | |
506 | separate_out = SeparateUnicode('').tag(config=True) |
|
506 | separate_out = SeparateUnicode('').tag(config=True) | |
507 | separate_out2 = SeparateUnicode('').tag(config=True) |
|
507 | separate_out2 = SeparateUnicode('').tag(config=True) | |
508 | wildcards_case_sensitive = Bool(True).tag(config=True) |
|
508 | wildcards_case_sensitive = Bool(True).tag(config=True) | |
509 | xmode = CaselessStrEnum(('Context', 'Plain', 'Verbose', 'Minimal'), |
|
509 | xmode = CaselessStrEnum(('Context', 'Plain', 'Verbose', 'Minimal'), | |
510 | default_value='Context', |
|
510 | default_value='Context', | |
511 | help="Switch modes for the IPython exception handlers." |
|
511 | help="Switch modes for the IPython exception handlers." | |
512 | ).tag(config=True) |
|
512 | ).tag(config=True) | |
513 |
|
513 | |||
514 | # Subcomponents of InteractiveShell |
|
514 | # Subcomponents of InteractiveShell | |
515 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) |
|
515 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) | |
516 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
516 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) | |
517 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) |
|
517 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) | |
518 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) |
|
518 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) | |
519 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) |
|
519 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) | |
520 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) |
|
520 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) | |
521 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) |
|
521 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) | |
522 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) |
|
522 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) | |
523 |
|
523 | |||
524 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) |
|
524 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) | |
525 | @property |
|
525 | @property | |
526 | def profile(self): |
|
526 | def profile(self): | |
527 | if self.profile_dir is not None: |
|
527 | if self.profile_dir is not None: | |
528 | name = os.path.basename(self.profile_dir.location) |
|
528 | name = os.path.basename(self.profile_dir.location) | |
529 | return name.replace('profile_','') |
|
529 | return name.replace('profile_','') | |
530 |
|
530 | |||
531 |
|
531 | |||
532 | # Private interface |
|
532 | # Private interface | |
533 | _post_execute = Dict() |
|
533 | _post_execute = Dict() | |
534 |
|
534 | |||
535 | # Tracks any GUI loop loaded for pylab |
|
535 | # Tracks any GUI loop loaded for pylab | |
536 | pylab_gui_select = None |
|
536 | pylab_gui_select = None | |
537 |
|
537 | |||
538 | last_execution_succeeded = Bool(True, help='Did last executed command succeeded') |
|
538 | last_execution_succeeded = Bool(True, help='Did last executed command succeeded') | |
539 |
|
539 | |||
540 | last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True) |
|
540 | last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True) | |
541 |
|
541 | |||
542 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
542 | def __init__(self, ipython_dir=None, profile_dir=None, | |
543 | user_module=None, user_ns=None, |
|
543 | user_module=None, user_ns=None, | |
544 | custom_exceptions=((), None), **kwargs): |
|
544 | custom_exceptions=((), None), **kwargs): | |
545 | # This is where traits with a config_key argument are updated |
|
545 | # This is where traits with a config_key argument are updated | |
546 | # from the values on config. |
|
546 | # from the values on config. | |
547 | super(InteractiveShell, self).__init__(**kwargs) |
|
547 | super(InteractiveShell, self).__init__(**kwargs) | |
548 | if 'PromptManager' in self.config: |
|
548 | if 'PromptManager' in self.config: | |
549 | warn('As of IPython 5.0 `PromptManager` config will have no effect' |
|
549 | warn('As of IPython 5.0 `PromptManager` config will have no effect' | |
550 | ' and has been replaced by TerminalInteractiveShell.prompts_class') |
|
550 | ' and has been replaced by TerminalInteractiveShell.prompts_class') | |
551 | self.configurables = [self] |
|
551 | self.configurables = [self] | |
552 |
|
552 | |||
553 | # These are relatively independent and stateless |
|
553 | # These are relatively independent and stateless | |
554 | self.init_ipython_dir(ipython_dir) |
|
554 | self.init_ipython_dir(ipython_dir) | |
555 | self.init_profile_dir(profile_dir) |
|
555 | self.init_profile_dir(profile_dir) | |
556 | self.init_instance_attrs() |
|
556 | self.init_instance_attrs() | |
557 | self.init_environment() |
|
557 | self.init_environment() | |
558 |
|
558 | |||
559 | # Check if we're in a virtualenv, and set up sys.path. |
|
559 | # Check if we're in a virtualenv, and set up sys.path. | |
560 | self.init_virtualenv() |
|
560 | self.init_virtualenv() | |
561 |
|
561 | |||
562 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
562 | # Create namespaces (user_ns, user_global_ns, etc.) | |
563 | self.init_create_namespaces(user_module, user_ns) |
|
563 | self.init_create_namespaces(user_module, user_ns) | |
564 | # This has to be done after init_create_namespaces because it uses |
|
564 | # This has to be done after init_create_namespaces because it uses | |
565 | # something in self.user_ns, but before init_sys_modules, which |
|
565 | # something in self.user_ns, but before init_sys_modules, which | |
566 | # is the first thing to modify sys. |
|
566 | # is the first thing to modify sys. | |
567 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
567 | # TODO: When we override sys.stdout and sys.stderr before this class | |
568 | # is created, we are saving the overridden ones here. Not sure if this |
|
568 | # is created, we are saving the overridden ones here. Not sure if this | |
569 | # is what we want to do. |
|
569 | # is what we want to do. | |
570 | self.save_sys_module_state() |
|
570 | self.save_sys_module_state() | |
571 | self.init_sys_modules() |
|
571 | self.init_sys_modules() | |
572 |
|
572 | |||
573 | # While we're trying to have each part of the code directly access what |
|
573 | # While we're trying to have each part of the code directly access what | |
574 | # it needs without keeping redundant references to objects, we have too |
|
574 | # it needs without keeping redundant references to objects, we have too | |
575 | # much legacy code that expects ip.db to exist. |
|
575 | # much legacy code that expects ip.db to exist. | |
576 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
576 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | |
577 |
|
577 | |||
578 | self.init_history() |
|
578 | self.init_history() | |
579 | self.init_encoding() |
|
579 | self.init_encoding() | |
580 | self.init_prefilter() |
|
580 | self.init_prefilter() | |
581 |
|
581 | |||
582 | self.init_syntax_highlighting() |
|
582 | self.init_syntax_highlighting() | |
583 | self.init_hooks() |
|
583 | self.init_hooks() | |
584 | self.init_events() |
|
584 | self.init_events() | |
585 | self.init_pushd_popd_magic() |
|
585 | self.init_pushd_popd_magic() | |
586 | self.init_user_ns() |
|
586 | self.init_user_ns() | |
587 | self.init_logger() |
|
587 | self.init_logger() | |
588 | self.init_builtins() |
|
588 | self.init_builtins() | |
589 |
|
589 | |||
590 | # The following was in post_config_initialization |
|
590 | # The following was in post_config_initialization | |
591 | self.init_inspector() |
|
591 | self.init_inspector() | |
592 | self.raw_input_original = input |
|
592 | self.raw_input_original = input | |
593 | self.init_completer() |
|
593 | self.init_completer() | |
594 | # TODO: init_io() needs to happen before init_traceback handlers |
|
594 | # TODO: init_io() needs to happen before init_traceback handlers | |
595 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
595 | # because the traceback handlers hardcode the stdout/stderr streams. | |
596 | # This logic in in debugger.Pdb and should eventually be changed. |
|
596 | # This logic in in debugger.Pdb and should eventually be changed. | |
597 | self.init_io() |
|
597 | self.init_io() | |
598 | self.init_traceback_handlers(custom_exceptions) |
|
598 | self.init_traceback_handlers(custom_exceptions) | |
599 | self.init_prompts() |
|
599 | self.init_prompts() | |
600 | self.init_display_formatter() |
|
600 | self.init_display_formatter() | |
601 | self.init_display_pub() |
|
601 | self.init_display_pub() | |
602 | self.init_data_pub() |
|
602 | self.init_data_pub() | |
603 | self.init_displayhook() |
|
603 | self.init_displayhook() | |
604 | self.init_magics() |
|
604 | self.init_magics() | |
605 | self.init_alias() |
|
605 | self.init_alias() | |
606 | self.init_logstart() |
|
606 | self.init_logstart() | |
607 | self.init_pdb() |
|
607 | self.init_pdb() | |
608 | self.init_extension_manager() |
|
608 | self.init_extension_manager() | |
609 | self.init_payload() |
|
609 | self.init_payload() | |
610 | self.events.trigger('shell_initialized', self) |
|
610 | self.events.trigger('shell_initialized', self) | |
611 | atexit.register(self.atexit_operations) |
|
611 | atexit.register(self.atexit_operations) | |
612 |
|
612 | |||
613 | # The trio runner is used for running Trio in the foreground thread. It |
|
613 | # The trio runner is used for running Trio in the foreground thread. It | |
614 | # is different from `_trio_runner(async_fn)` in `async_helpers.py` |
|
614 | # is different from `_trio_runner(async_fn)` in `async_helpers.py` | |
615 | # which calls `trio.run()` for every cell. This runner runs all cells |
|
615 | # which calls `trio.run()` for every cell. This runner runs all cells | |
616 | # inside a single Trio event loop. If used, it is set from |
|
616 | # inside a single Trio event loop. If used, it is set from | |
617 | # `ipykernel.kernelapp`. |
|
617 | # `ipykernel.kernelapp`. | |
618 | self.trio_runner = None |
|
618 | self.trio_runner = None | |
619 |
|
619 | |||
620 | def get_ipython(self): |
|
620 | def get_ipython(self): | |
621 | """Return the currently running IPython instance.""" |
|
621 | """Return the currently running IPython instance.""" | |
622 | return self |
|
622 | return self | |
623 |
|
623 | |||
624 | #------------------------------------------------------------------------- |
|
624 | #------------------------------------------------------------------------- | |
625 | # Trait changed handlers |
|
625 | # Trait changed handlers | |
626 | #------------------------------------------------------------------------- |
|
626 | #------------------------------------------------------------------------- | |
627 | @observe('ipython_dir') |
|
627 | @observe('ipython_dir') | |
628 | def _ipython_dir_changed(self, change): |
|
628 | def _ipython_dir_changed(self, change): | |
629 | ensure_dir_exists(change['new']) |
|
629 | ensure_dir_exists(change['new']) | |
630 |
|
630 | |||
631 | def set_autoindent(self,value=None): |
|
631 | def set_autoindent(self,value=None): | |
632 | """Set the autoindent flag. |
|
632 | """Set the autoindent flag. | |
633 |
|
633 | |||
634 | If called with no arguments, it acts as a toggle.""" |
|
634 | If called with no arguments, it acts as a toggle.""" | |
635 | if value is None: |
|
635 | if value is None: | |
636 | self.autoindent = not self.autoindent |
|
636 | self.autoindent = not self.autoindent | |
637 | else: |
|
637 | else: | |
638 | self.autoindent = value |
|
638 | self.autoindent = value | |
639 |
|
639 | |||
640 | def set_trio_runner(self, tr): |
|
640 | def set_trio_runner(self, tr): | |
641 | self.trio_runner = tr |
|
641 | self.trio_runner = tr | |
642 |
|
642 | |||
643 | #------------------------------------------------------------------------- |
|
643 | #------------------------------------------------------------------------- | |
644 | # init_* methods called by __init__ |
|
644 | # init_* methods called by __init__ | |
645 | #------------------------------------------------------------------------- |
|
645 | #------------------------------------------------------------------------- | |
646 |
|
646 | |||
647 | def init_ipython_dir(self, ipython_dir): |
|
647 | def init_ipython_dir(self, ipython_dir): | |
648 | if ipython_dir is not None: |
|
648 | if ipython_dir is not None: | |
649 | self.ipython_dir = ipython_dir |
|
649 | self.ipython_dir = ipython_dir | |
650 | return |
|
650 | return | |
651 |
|
651 | |||
652 | self.ipython_dir = get_ipython_dir() |
|
652 | self.ipython_dir = get_ipython_dir() | |
653 |
|
653 | |||
654 | def init_profile_dir(self, profile_dir): |
|
654 | def init_profile_dir(self, profile_dir): | |
655 | if profile_dir is not None: |
|
655 | if profile_dir is not None: | |
656 | self.profile_dir = profile_dir |
|
656 | self.profile_dir = profile_dir | |
657 | return |
|
657 | return | |
658 | self.profile_dir = ProfileDir.create_profile_dir_by_name( |
|
658 | self.profile_dir = ProfileDir.create_profile_dir_by_name( | |
659 | self.ipython_dir, "default" |
|
659 | self.ipython_dir, "default" | |
660 | ) |
|
660 | ) | |
661 |
|
661 | |||
662 | def init_instance_attrs(self): |
|
662 | def init_instance_attrs(self): | |
663 | self.more = False |
|
663 | self.more = False | |
664 |
|
664 | |||
665 | # command compiler |
|
665 | # command compiler | |
666 | self.compile = self.compiler_class() |
|
666 | self.compile = self.compiler_class() | |
667 |
|
667 | |||
668 | # Make an empty namespace, which extension writers can rely on both |
|
668 | # Make an empty namespace, which extension writers can rely on both | |
669 | # existing and NEVER being used by ipython itself. This gives them a |
|
669 | # existing and NEVER being used by ipython itself. This gives them a | |
670 | # convenient location for storing additional information and state |
|
670 | # convenient location for storing additional information and state | |
671 | # their extensions may require, without fear of collisions with other |
|
671 | # their extensions may require, without fear of collisions with other | |
672 | # ipython names that may develop later. |
|
672 | # ipython names that may develop later. | |
673 | self.meta = Struct() |
|
673 | self.meta = Struct() | |
674 |
|
674 | |||
675 | # Temporary files used for various purposes. Deleted at exit. |
|
675 | # Temporary files used for various purposes. Deleted at exit. | |
676 | # The files here are stored with Path from Pathlib |
|
676 | # The files here are stored with Path from Pathlib | |
677 | self.tempfiles = [] |
|
677 | self.tempfiles = [] | |
678 | self.tempdirs = [] |
|
678 | self.tempdirs = [] | |
679 |
|
679 | |||
680 | # keep track of where we started running (mainly for crash post-mortem) |
|
680 | # keep track of where we started running (mainly for crash post-mortem) | |
681 | # This is not being used anywhere currently. |
|
681 | # This is not being used anywhere currently. | |
682 | self.starting_dir = os.getcwd() |
|
682 | self.starting_dir = os.getcwd() | |
683 |
|
683 | |||
684 | # Indentation management |
|
684 | # Indentation management | |
685 | self.indent_current_nsp = 0 |
|
685 | self.indent_current_nsp = 0 | |
686 |
|
686 | |||
687 | # Dict to track post-execution functions that have been registered |
|
687 | # Dict to track post-execution functions that have been registered | |
688 | self._post_execute = {} |
|
688 | self._post_execute = {} | |
689 |
|
689 | |||
690 | def init_environment(self): |
|
690 | def init_environment(self): | |
691 | """Any changes we need to make to the user's environment.""" |
|
691 | """Any changes we need to make to the user's environment.""" | |
692 | pass |
|
692 | pass | |
693 |
|
693 | |||
694 | def init_encoding(self): |
|
694 | def init_encoding(self): | |
695 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
695 | # Get system encoding at startup time. Certain terminals (like Emacs | |
696 | # under Win32 have it set to None, and we need to have a known valid |
|
696 | # under Win32 have it set to None, and we need to have a known valid | |
697 | # encoding to use in the raw_input() method |
|
697 | # encoding to use in the raw_input() method | |
698 | try: |
|
698 | try: | |
699 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
699 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
700 | except AttributeError: |
|
700 | except AttributeError: | |
701 | self.stdin_encoding = 'ascii' |
|
701 | self.stdin_encoding = 'ascii' | |
702 |
|
702 | |||
703 |
|
703 | |||
704 | @observe('colors') |
|
704 | @observe('colors') | |
705 | def init_syntax_highlighting(self, changes=None): |
|
705 | def init_syntax_highlighting(self, changes=None): | |
706 | # Python source parser/formatter for syntax highlighting |
|
706 | # Python source parser/formatter for syntax highlighting | |
707 | pyformat = PyColorize.Parser(style=self.colors, parent=self).format |
|
707 | pyformat = PyColorize.Parser(style=self.colors, parent=self).format | |
708 | self.pycolorize = lambda src: pyformat(src,'str') |
|
708 | self.pycolorize = lambda src: pyformat(src,'str') | |
709 |
|
709 | |||
710 | def refresh_style(self): |
|
710 | def refresh_style(self): | |
711 | # No-op here, used in subclass |
|
711 | # No-op here, used in subclass | |
712 | pass |
|
712 | pass | |
713 |
|
713 | |||
714 | def init_pushd_popd_magic(self): |
|
714 | def init_pushd_popd_magic(self): | |
715 | # for pushd/popd management |
|
715 | # for pushd/popd management | |
716 | self.home_dir = get_home_dir() |
|
716 | self.home_dir = get_home_dir() | |
717 |
|
717 | |||
718 | self.dir_stack = [] |
|
718 | self.dir_stack = [] | |
719 |
|
719 | |||
720 | def init_logger(self): |
|
720 | def init_logger(self): | |
721 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
721 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
722 | logmode='rotate') |
|
722 | logmode='rotate') | |
723 |
|
723 | |||
724 | def init_logstart(self): |
|
724 | def init_logstart(self): | |
725 | """Initialize logging in case it was requested at the command line. |
|
725 | """Initialize logging in case it was requested at the command line. | |
726 | """ |
|
726 | """ | |
727 | if self.logappend: |
|
727 | if self.logappend: | |
728 | self.magic('logstart %s append' % self.logappend) |
|
728 | self.magic('logstart %s append' % self.logappend) | |
729 | elif self.logfile: |
|
729 | elif self.logfile: | |
730 | self.magic('logstart %s' % self.logfile) |
|
730 | self.magic('logstart %s' % self.logfile) | |
731 | elif self.logstart: |
|
731 | elif self.logstart: | |
732 | self.magic('logstart') |
|
732 | self.magic('logstart') | |
733 |
|
733 | |||
734 |
|
734 | |||
735 | def init_builtins(self): |
|
735 | def init_builtins(self): | |
736 | # A single, static flag that we set to True. Its presence indicates |
|
736 | # A single, static flag that we set to True. Its presence indicates | |
737 | # that an IPython shell has been created, and we make no attempts at |
|
737 | # that an IPython shell has been created, and we make no attempts at | |
738 | # removing on exit or representing the existence of more than one |
|
738 | # removing on exit or representing the existence of more than one | |
739 | # IPython at a time. |
|
739 | # IPython at a time. | |
740 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
740 | builtin_mod.__dict__['__IPYTHON__'] = True | |
741 | builtin_mod.__dict__['display'] = display |
|
741 | builtin_mod.__dict__['display'] = display | |
742 |
|
742 | |||
743 | self.builtin_trap = BuiltinTrap(shell=self) |
|
743 | self.builtin_trap = BuiltinTrap(shell=self) | |
744 |
|
744 | |||
745 | @observe('colors') |
|
745 | @observe('colors') | |
746 | def init_inspector(self, changes=None): |
|
746 | def init_inspector(self, changes=None): | |
747 | # Object inspector |
|
747 | # Object inspector | |
748 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
748 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
749 | PyColorize.ANSICodeColors, |
|
749 | PyColorize.ANSICodeColors, | |
750 | self.colors, |
|
750 | self.colors, | |
751 | self.object_info_string_level) |
|
751 | self.object_info_string_level) | |
752 |
|
752 | |||
753 | def init_io(self): |
|
753 | def init_io(self): | |
754 | # implemented in subclasses, TerminalInteractiveShell does call |
|
754 | # implemented in subclasses, TerminalInteractiveShell does call | |
755 | # colorama.init(). |
|
755 | # colorama.init(). | |
756 | pass |
|
756 | pass | |
757 |
|
757 | |||
758 | def init_prompts(self): |
|
758 | def init_prompts(self): | |
759 | # Set system prompts, so that scripts can decide if they are running |
|
759 | # Set system prompts, so that scripts can decide if they are running | |
760 | # interactively. |
|
760 | # interactively. | |
761 | sys.ps1 = 'In : ' |
|
761 | sys.ps1 = 'In : ' | |
762 | sys.ps2 = '...: ' |
|
762 | sys.ps2 = '...: ' | |
763 | sys.ps3 = 'Out: ' |
|
763 | sys.ps3 = 'Out: ' | |
764 |
|
764 | |||
765 | def init_display_formatter(self): |
|
765 | def init_display_formatter(self): | |
766 | self.display_formatter = DisplayFormatter(parent=self) |
|
766 | self.display_formatter = DisplayFormatter(parent=self) | |
767 | self.configurables.append(self.display_formatter) |
|
767 | self.configurables.append(self.display_formatter) | |
768 |
|
768 | |||
769 | def init_display_pub(self): |
|
769 | def init_display_pub(self): | |
770 | self.display_pub = self.display_pub_class(parent=self, shell=self) |
|
770 | self.display_pub = self.display_pub_class(parent=self, shell=self) | |
771 | self.configurables.append(self.display_pub) |
|
771 | self.configurables.append(self.display_pub) | |
772 |
|
772 | |||
773 | def init_data_pub(self): |
|
773 | def init_data_pub(self): | |
774 | if not self.data_pub_class: |
|
774 | if not self.data_pub_class: | |
775 | self.data_pub = None |
|
775 | self.data_pub = None | |
776 | return |
|
776 | return | |
777 | self.data_pub = self.data_pub_class(parent=self) |
|
777 | self.data_pub = self.data_pub_class(parent=self) | |
778 | self.configurables.append(self.data_pub) |
|
778 | self.configurables.append(self.data_pub) | |
779 |
|
779 | |||
780 | def init_displayhook(self): |
|
780 | def init_displayhook(self): | |
781 | # Initialize displayhook, set in/out prompts and printing system |
|
781 | # Initialize displayhook, set in/out prompts and printing system | |
782 | self.displayhook = self.displayhook_class( |
|
782 | self.displayhook = self.displayhook_class( | |
783 | parent=self, |
|
783 | parent=self, | |
784 | shell=self, |
|
784 | shell=self, | |
785 | cache_size=self.cache_size, |
|
785 | cache_size=self.cache_size, | |
786 | ) |
|
786 | ) | |
787 | self.configurables.append(self.displayhook) |
|
787 | self.configurables.append(self.displayhook) | |
788 | # This is a context manager that installs/revmoes the displayhook at |
|
788 | # This is a context manager that installs/revmoes the displayhook at | |
789 | # the appropriate time. |
|
789 | # the appropriate time. | |
790 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
790 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
791 |
|
791 | |||
792 | @staticmethod |
|
792 | @staticmethod | |
793 | def get_path_links(p: Path): |
|
793 | def get_path_links(p: Path): | |
794 | """Gets path links including all symlinks |
|
794 | """Gets path links including all symlinks | |
795 |
|
795 | |||
796 | Examples |
|
796 | Examples | |
797 | -------- |
|
797 | -------- | |
798 | In [1]: from IPython.core.interactiveshell import InteractiveShell |
|
798 | In [1]: from IPython.core.interactiveshell import InteractiveShell | |
799 |
|
799 | |||
800 | In [2]: import sys, pathlib |
|
800 | In [2]: import sys, pathlib | |
801 |
|
801 | |||
802 | In [3]: paths = InteractiveShell.get_path_links(pathlib.Path(sys.executable)) |
|
802 | In [3]: paths = InteractiveShell.get_path_links(pathlib.Path(sys.executable)) | |
803 |
|
803 | |||
804 | In [4]: len(paths) == len(set(paths)) |
|
804 | In [4]: len(paths) == len(set(paths)) | |
805 | Out[4]: True |
|
805 | Out[4]: True | |
806 |
|
806 | |||
807 | In [5]: bool(paths) |
|
807 | In [5]: bool(paths) | |
808 | Out[5]: True |
|
808 | Out[5]: True | |
809 | """ |
|
809 | """ | |
810 | paths = [p] |
|
810 | paths = [p] | |
811 | while p.is_symlink(): |
|
811 | while p.is_symlink(): | |
812 | new_path = Path(os.readlink(p)) |
|
812 | new_path = Path(os.readlink(p)) | |
813 | if not new_path.is_absolute(): |
|
813 | if not new_path.is_absolute(): | |
814 | new_path = p.parent / new_path |
|
814 | new_path = p.parent / new_path | |
815 | p = new_path |
|
815 | p = new_path | |
816 | paths.append(p) |
|
816 | paths.append(p) | |
817 | return paths |
|
817 | return paths | |
818 |
|
818 | |||
819 | def init_virtualenv(self): |
|
819 | def init_virtualenv(self): | |
820 | """Add the current virtualenv to sys.path so the user can import modules from it. |
|
820 | """Add the current virtualenv to sys.path so the user can import modules from it. | |
821 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
821 | This isn't perfect: it doesn't use the Python interpreter with which the | |
822 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
822 | virtualenv was built, and it ignores the --no-site-packages option. A | |
823 | warning will appear suggesting the user installs IPython in the |
|
823 | warning will appear suggesting the user installs IPython in the | |
824 | virtualenv, but for many cases, it probably works well enough. |
|
824 | virtualenv, but for many cases, it probably works well enough. | |
825 |
|
825 | |||
826 | Adapted from code snippets online. |
|
826 | Adapted from code snippets online. | |
827 |
|
827 | |||
828 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
828 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | |
829 | """ |
|
829 | """ | |
830 | if 'VIRTUAL_ENV' not in os.environ: |
|
830 | if 'VIRTUAL_ENV' not in os.environ: | |
831 | # Not in a virtualenv |
|
831 | # Not in a virtualenv | |
832 | return |
|
832 | return | |
833 | elif os.environ["VIRTUAL_ENV"] == "": |
|
833 | elif os.environ["VIRTUAL_ENV"] == "": | |
834 | warn("Virtual env path set to '', please check if this is intended.") |
|
834 | warn("Virtual env path set to '', please check if this is intended.") | |
835 | return |
|
835 | return | |
836 |
|
836 | |||
837 | p = Path(sys.executable) |
|
837 | p = Path(sys.executable) | |
838 | p_venv = Path(os.environ["VIRTUAL_ENV"]) |
|
838 | p_venv = Path(os.environ["VIRTUAL_ENV"]) | |
839 |
|
839 | |||
840 | # fallback venv detection: |
|
840 | # fallback venv detection: | |
841 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
841 | # stdlib venv may symlink sys.executable, so we can't use realpath. | |
842 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
842 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. | |
843 | # So we just check every item in the symlink tree (generally <= 3) |
|
843 | # So we just check every item in the symlink tree (generally <= 3) | |
844 | paths = self.get_path_links(p) |
|
844 | paths = self.get_path_links(p) | |
845 |
|
845 | |||
846 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible |
|
846 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible | |
847 | if p_venv.parts[1] == "cygdrive": |
|
847 | if p_venv.parts[1] == "cygdrive": | |
848 | drive_name = p_venv.parts[2] |
|
848 | drive_name = p_venv.parts[2] | |
849 | p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:]) |
|
849 | p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:]) | |
850 |
|
850 | |||
851 | if any(p_venv == p.parents[1] for p in paths): |
|
851 | if any(p_venv == p.parents[1] for p in paths): | |
852 | # Our exe is inside or has access to the virtualenv, don't need to do anything. |
|
852 | # Our exe is inside or has access to the virtualenv, don't need to do anything. | |
853 | return |
|
853 | return | |
854 |
|
854 | |||
855 | if sys.platform == "win32": |
|
855 | if sys.platform == "win32": | |
856 | virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages")) |
|
856 | virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages")) | |
857 | else: |
|
857 | else: | |
858 | virtual_env_path = Path( |
|
858 | virtual_env_path = Path( | |
859 | os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages" |
|
859 | os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages" | |
860 | ) |
|
860 | ) | |
861 | p_ver = sys.version_info[:2] |
|
861 | p_ver = sys.version_info[:2] | |
862 |
|
862 | |||
863 | # Predict version from py[thon]-x.x in the $VIRTUAL_ENV |
|
863 | # Predict version from py[thon]-x.x in the $VIRTUAL_ENV | |
864 | re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"]) |
|
864 | re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"]) | |
865 | if re_m: |
|
865 | if re_m: | |
866 | predicted_path = Path(str(virtual_env_path).format(*re_m.groups())) |
|
866 | predicted_path = Path(str(virtual_env_path).format(*re_m.groups())) | |
867 | if predicted_path.exists(): |
|
867 | if predicted_path.exists(): | |
868 | p_ver = re_m.groups() |
|
868 | p_ver = re_m.groups() | |
869 |
|
869 | |||
870 | virtual_env = str(virtual_env_path).format(*p_ver) |
|
870 | virtual_env = str(virtual_env_path).format(*p_ver) | |
871 | if self.warn_venv: |
|
871 | if self.warn_venv: | |
872 | warn( |
|
872 | warn( | |
873 | "Attempting to work in a virtualenv. If you encounter problems, " |
|
873 | "Attempting to work in a virtualenv. If you encounter problems, " | |
874 | "please install IPython inside the virtualenv." |
|
874 | "please install IPython inside the virtualenv." | |
875 | ) |
|
875 | ) | |
876 | import site |
|
876 | import site | |
877 | sys.path.insert(0, virtual_env) |
|
877 | sys.path.insert(0, virtual_env) | |
878 | site.addsitedir(virtual_env) |
|
878 | site.addsitedir(virtual_env) | |
879 |
|
879 | |||
880 | #------------------------------------------------------------------------- |
|
880 | #------------------------------------------------------------------------- | |
881 | # Things related to injections into the sys module |
|
881 | # Things related to injections into the sys module | |
882 | #------------------------------------------------------------------------- |
|
882 | #------------------------------------------------------------------------- | |
883 |
|
883 | |||
884 | def save_sys_module_state(self): |
|
884 | def save_sys_module_state(self): | |
885 | """Save the state of hooks in the sys module. |
|
885 | """Save the state of hooks in the sys module. | |
886 |
|
886 | |||
887 | This has to be called after self.user_module is created. |
|
887 | This has to be called after self.user_module is created. | |
888 | """ |
|
888 | """ | |
889 | self._orig_sys_module_state = {'stdin': sys.stdin, |
|
889 | self._orig_sys_module_state = {'stdin': sys.stdin, | |
890 | 'stdout': sys.stdout, |
|
890 | 'stdout': sys.stdout, | |
891 | 'stderr': sys.stderr, |
|
891 | 'stderr': sys.stderr, | |
892 | 'excepthook': sys.excepthook} |
|
892 | 'excepthook': sys.excepthook} | |
893 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
893 | self._orig_sys_modules_main_name = self.user_module.__name__ | |
894 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
894 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | |
895 |
|
895 | |||
896 | def restore_sys_module_state(self): |
|
896 | def restore_sys_module_state(self): | |
897 | """Restore the state of the sys module.""" |
|
897 | """Restore the state of the sys module.""" | |
898 | try: |
|
898 | try: | |
899 | for k, v in self._orig_sys_module_state.items(): |
|
899 | for k, v in self._orig_sys_module_state.items(): | |
900 | setattr(sys, k, v) |
|
900 | setattr(sys, k, v) | |
901 | except AttributeError: |
|
901 | except AttributeError: | |
902 | pass |
|
902 | pass | |
903 | # Reset what what done in self.init_sys_modules |
|
903 | # Reset what what done in self.init_sys_modules | |
904 | if self._orig_sys_modules_main_mod is not None: |
|
904 | if self._orig_sys_modules_main_mod is not None: | |
905 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
905 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | |
906 |
|
906 | |||
907 | #------------------------------------------------------------------------- |
|
907 | #------------------------------------------------------------------------- | |
908 | # Things related to the banner |
|
908 | # Things related to the banner | |
909 | #------------------------------------------------------------------------- |
|
909 | #------------------------------------------------------------------------- | |
910 |
|
910 | |||
911 | @property |
|
911 | @property | |
912 | def banner(self): |
|
912 | def banner(self): | |
913 | banner = self.banner1 |
|
913 | banner = self.banner1 | |
914 | if self.profile and self.profile != 'default': |
|
914 | if self.profile and self.profile != 'default': | |
915 | banner += '\nIPython profile: %s\n' % self.profile |
|
915 | banner += '\nIPython profile: %s\n' % self.profile | |
916 | if self.banner2: |
|
916 | if self.banner2: | |
917 | banner += '\n' + self.banner2 |
|
917 | banner += '\n' + self.banner2 | |
918 | return banner |
|
918 | return banner | |
919 |
|
919 | |||
920 | def show_banner(self, banner=None): |
|
920 | def show_banner(self, banner=None): | |
921 | if banner is None: |
|
921 | if banner is None: | |
922 | banner = self.banner |
|
922 | banner = self.banner | |
923 | sys.stdout.write(banner) |
|
923 | sys.stdout.write(banner) | |
924 |
|
924 | |||
925 | #------------------------------------------------------------------------- |
|
925 | #------------------------------------------------------------------------- | |
926 | # Things related to hooks |
|
926 | # Things related to hooks | |
927 | #------------------------------------------------------------------------- |
|
927 | #------------------------------------------------------------------------- | |
928 |
|
928 | |||
929 | def init_hooks(self): |
|
929 | def init_hooks(self): | |
930 | # hooks holds pointers used for user-side customizations |
|
930 | # hooks holds pointers used for user-side customizations | |
931 | self.hooks = Struct() |
|
931 | self.hooks = Struct() | |
932 |
|
932 | |||
933 | self.strdispatchers = {} |
|
933 | self.strdispatchers = {} | |
934 |
|
934 | |||
935 | # Set all default hooks, defined in the IPython.hooks module. |
|
935 | # Set all default hooks, defined in the IPython.hooks module. | |
936 | hooks = IPython.core.hooks |
|
936 | hooks = IPython.core.hooks | |
937 | for hook_name in hooks.__all__: |
|
937 | for hook_name in hooks.__all__: | |
938 | # default hooks have priority 100, i.e. low; user hooks should have |
|
938 | # default hooks have priority 100, i.e. low; user hooks should have | |
939 | # 0-100 priority |
|
939 | # 0-100 priority | |
940 | self.set_hook(hook_name, getattr(hooks, hook_name), 100) |
|
940 | self.set_hook(hook_name, getattr(hooks, hook_name), 100) | |
941 |
|
941 | |||
942 | if self.display_page: |
|
942 | if self.display_page: | |
943 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) |
|
943 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) | |
944 |
|
944 | |||
945 | def set_hook(self, name, hook, priority=50, str_key=None, re_key=None): |
|
945 | def set_hook(self, name, hook, priority=50, str_key=None, re_key=None): | |
946 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
946 | """set_hook(name,hook) -> sets an internal IPython hook. | |
947 |
|
947 | |||
948 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
948 | IPython exposes some of its internal API as user-modifiable hooks. By | |
949 | adding your function to one of these hooks, you can modify IPython's |
|
949 | adding your function to one of these hooks, you can modify IPython's | |
950 | behavior to call at runtime your own routines.""" |
|
950 | behavior to call at runtime your own routines.""" | |
951 |
|
951 | |||
952 | # At some point in the future, this should validate the hook before it |
|
952 | # At some point in the future, this should validate the hook before it | |
953 | # accepts it. Probably at least check that the hook takes the number |
|
953 | # accepts it. Probably at least check that the hook takes the number | |
954 | # of args it's supposed to. |
|
954 | # of args it's supposed to. | |
955 |
|
955 | |||
956 | f = types.MethodType(hook,self) |
|
956 | f = types.MethodType(hook,self) | |
957 |
|
957 | |||
958 | # check if the hook is for strdispatcher first |
|
958 | # check if the hook is for strdispatcher first | |
959 | if str_key is not None: |
|
959 | if str_key is not None: | |
960 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
960 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
961 | sdp.add_s(str_key, f, priority ) |
|
961 | sdp.add_s(str_key, f, priority ) | |
962 | self.strdispatchers[name] = sdp |
|
962 | self.strdispatchers[name] = sdp | |
963 | return |
|
963 | return | |
964 | if re_key is not None: |
|
964 | if re_key is not None: | |
965 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
965 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
966 | sdp.add_re(re.compile(re_key), f, priority ) |
|
966 | sdp.add_re(re.compile(re_key), f, priority ) | |
967 | self.strdispatchers[name] = sdp |
|
967 | self.strdispatchers[name] = sdp | |
968 | return |
|
968 | return | |
969 |
|
969 | |||
970 | dp = getattr(self.hooks, name, None) |
|
970 | dp = getattr(self.hooks, name, None) | |
971 | if name not in IPython.core.hooks.__all__: |
|
971 | if name not in IPython.core.hooks.__all__: | |
972 | print("Warning! Hook '%s' is not one of %s" % \ |
|
972 | print("Warning! Hook '%s' is not one of %s" % \ | |
973 | (name, IPython.core.hooks.__all__ )) |
|
973 | (name, IPython.core.hooks.__all__ )) | |
974 |
|
974 | |||
975 | if name in IPython.core.hooks.deprecated: |
|
975 | if name in IPython.core.hooks.deprecated: | |
976 | alternative = IPython.core.hooks.deprecated[name] |
|
976 | alternative = IPython.core.hooks.deprecated[name] | |
977 | raise ValueError( |
|
977 | raise ValueError( | |
978 | "Hook {} has been deprecated since IPython 5.0. Use {} instead.".format( |
|
978 | "Hook {} has been deprecated since IPython 5.0. Use {} instead.".format( | |
979 | name, alternative |
|
979 | name, alternative | |
980 | ) |
|
980 | ) | |
981 | ) |
|
981 | ) | |
982 |
|
982 | |||
983 | if not dp: |
|
983 | if not dp: | |
984 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
984 | dp = IPython.core.hooks.CommandChainDispatcher() | |
985 |
|
985 | |||
986 | try: |
|
986 | try: | |
987 | dp.add(f,priority) |
|
987 | dp.add(f,priority) | |
988 | except AttributeError: |
|
988 | except AttributeError: | |
989 | # it was not commandchain, plain old func - replace |
|
989 | # it was not commandchain, plain old func - replace | |
990 | dp = f |
|
990 | dp = f | |
991 |
|
991 | |||
992 | setattr(self.hooks,name, dp) |
|
992 | setattr(self.hooks,name, dp) | |
993 |
|
993 | |||
994 | #------------------------------------------------------------------------- |
|
994 | #------------------------------------------------------------------------- | |
995 | # Things related to events |
|
995 | # Things related to events | |
996 | #------------------------------------------------------------------------- |
|
996 | #------------------------------------------------------------------------- | |
997 |
|
997 | |||
998 | def init_events(self): |
|
998 | def init_events(self): | |
999 | self.events = EventManager(self, available_events) |
|
999 | self.events = EventManager(self, available_events) | |
1000 |
|
1000 | |||
1001 | self.events.register("pre_execute", self._clear_warning_registry) |
|
1001 | self.events.register("pre_execute", self._clear_warning_registry) | |
1002 |
|
1002 | |||
1003 | def register_post_execute(self, func): |
|
1003 | def register_post_execute(self, func): | |
1004 | """DEPRECATED: Use ip.events.register('post_run_cell', func) |
|
1004 | """DEPRECATED: Use ip.events.register('post_run_cell', func) | |
1005 |
|
1005 | |||
1006 | Register a function for calling after code execution. |
|
1006 | Register a function for calling after code execution. | |
1007 | """ |
|
1007 | """ | |
1008 | raise ValueError( |
|
1008 | raise ValueError( | |
1009 | "ip.register_post_execute is deprecated since IPython 1.0, use " |
|
1009 | "ip.register_post_execute is deprecated since IPython 1.0, use " | |
1010 | "ip.events.register('post_run_cell', func) instead." |
|
1010 | "ip.events.register('post_run_cell', func) instead." | |
1011 | ) |
|
1011 | ) | |
1012 |
|
1012 | |||
1013 | def _clear_warning_registry(self): |
|
1013 | def _clear_warning_registry(self): | |
1014 | # clear the warning registry, so that different code blocks with |
|
1014 | # clear the warning registry, so that different code blocks with | |
1015 | # overlapping line number ranges don't cause spurious suppression of |
|
1015 | # overlapping line number ranges don't cause spurious suppression of | |
1016 | # warnings (see gh-6611 for details) |
|
1016 | # warnings (see gh-6611 for details) | |
1017 | if "__warningregistry__" in self.user_global_ns: |
|
1017 | if "__warningregistry__" in self.user_global_ns: | |
1018 | del self.user_global_ns["__warningregistry__"] |
|
1018 | del self.user_global_ns["__warningregistry__"] | |
1019 |
|
1019 | |||
1020 | #------------------------------------------------------------------------- |
|
1020 | #------------------------------------------------------------------------- | |
1021 | # Things related to the "main" module |
|
1021 | # Things related to the "main" module | |
1022 | #------------------------------------------------------------------------- |
|
1022 | #------------------------------------------------------------------------- | |
1023 |
|
1023 | |||
1024 | def new_main_mod(self, filename, modname): |
|
1024 | def new_main_mod(self, filename, modname): | |
1025 | """Return a new 'main' module object for user code execution. |
|
1025 | """Return a new 'main' module object for user code execution. | |
1026 |
|
1026 | |||
1027 | ``filename`` should be the path of the script which will be run in the |
|
1027 | ``filename`` should be the path of the script which will be run in the | |
1028 | module. Requests with the same filename will get the same module, with |
|
1028 | module. Requests with the same filename will get the same module, with | |
1029 | its namespace cleared. |
|
1029 | its namespace cleared. | |
1030 |
|
1030 | |||
1031 | ``modname`` should be the module name - normally either '__main__' or |
|
1031 | ``modname`` should be the module name - normally either '__main__' or | |
1032 | the basename of the file without the extension. |
|
1032 | the basename of the file without the extension. | |
1033 |
|
1033 | |||
1034 | When scripts are executed via %run, we must keep a reference to their |
|
1034 | When scripts are executed via %run, we must keep a reference to their | |
1035 | __main__ module around so that Python doesn't |
|
1035 | __main__ module around so that Python doesn't | |
1036 | clear it, rendering references to module globals useless. |
|
1036 | clear it, rendering references to module globals useless. | |
1037 |
|
1037 | |||
1038 | This method keeps said reference in a private dict, keyed by the |
|
1038 | This method keeps said reference in a private dict, keyed by the | |
1039 | absolute path of the script. This way, for multiple executions of the |
|
1039 | absolute path of the script. This way, for multiple executions of the | |
1040 | same script we only keep one copy of the namespace (the last one), |
|
1040 | same script we only keep one copy of the namespace (the last one), | |
1041 | thus preventing memory leaks from old references while allowing the |
|
1041 | thus preventing memory leaks from old references while allowing the | |
1042 | objects from the last execution to be accessible. |
|
1042 | objects from the last execution to be accessible. | |
1043 | """ |
|
1043 | """ | |
1044 | filename = os.path.abspath(filename) |
|
1044 | filename = os.path.abspath(filename) | |
1045 | try: |
|
1045 | try: | |
1046 | main_mod = self._main_mod_cache[filename] |
|
1046 | main_mod = self._main_mod_cache[filename] | |
1047 | except KeyError: |
|
1047 | except KeyError: | |
1048 | main_mod = self._main_mod_cache[filename] = types.ModuleType( |
|
1048 | main_mod = self._main_mod_cache[filename] = types.ModuleType( | |
1049 | modname, |
|
1049 | modname, | |
1050 | doc="Module created for script run in IPython") |
|
1050 | doc="Module created for script run in IPython") | |
1051 | else: |
|
1051 | else: | |
1052 | main_mod.__dict__.clear() |
|
1052 | main_mod.__dict__.clear() | |
1053 | main_mod.__name__ = modname |
|
1053 | main_mod.__name__ = modname | |
1054 |
|
1054 | |||
1055 | main_mod.__file__ = filename |
|
1055 | main_mod.__file__ = filename | |
1056 | # It seems pydoc (and perhaps others) needs any module instance to |
|
1056 | # It seems pydoc (and perhaps others) needs any module instance to | |
1057 | # implement a __nonzero__ method |
|
1057 | # implement a __nonzero__ method | |
1058 | main_mod.__nonzero__ = lambda : True |
|
1058 | main_mod.__nonzero__ = lambda : True | |
1059 |
|
1059 | |||
1060 | return main_mod |
|
1060 | return main_mod | |
1061 |
|
1061 | |||
1062 | def clear_main_mod_cache(self): |
|
1062 | def clear_main_mod_cache(self): | |
1063 | """Clear the cache of main modules. |
|
1063 | """Clear the cache of main modules. | |
1064 |
|
1064 | |||
1065 | Mainly for use by utilities like %reset. |
|
1065 | Mainly for use by utilities like %reset. | |
1066 |
|
1066 | |||
1067 | Examples |
|
1067 | Examples | |
1068 | -------- |
|
1068 | -------- | |
1069 | In [15]: import IPython |
|
1069 | In [15]: import IPython | |
1070 |
|
1070 | |||
1071 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
1071 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | |
1072 |
|
1072 | |||
1073 | In [17]: len(_ip._main_mod_cache) > 0 |
|
1073 | In [17]: len(_ip._main_mod_cache) > 0 | |
1074 | Out[17]: True |
|
1074 | Out[17]: True | |
1075 |
|
1075 | |||
1076 | In [18]: _ip.clear_main_mod_cache() |
|
1076 | In [18]: _ip.clear_main_mod_cache() | |
1077 |
|
1077 | |||
1078 | In [19]: len(_ip._main_mod_cache) == 0 |
|
1078 | In [19]: len(_ip._main_mod_cache) == 0 | |
1079 | Out[19]: True |
|
1079 | Out[19]: True | |
1080 | """ |
|
1080 | """ | |
1081 | self._main_mod_cache.clear() |
|
1081 | self._main_mod_cache.clear() | |
1082 |
|
1082 | |||
1083 | #------------------------------------------------------------------------- |
|
1083 | #------------------------------------------------------------------------- | |
1084 | # Things related to debugging |
|
1084 | # Things related to debugging | |
1085 | #------------------------------------------------------------------------- |
|
1085 | #------------------------------------------------------------------------- | |
1086 |
|
1086 | |||
1087 | def init_pdb(self): |
|
1087 | def init_pdb(self): | |
1088 | # Set calling of pdb on exceptions |
|
1088 | # Set calling of pdb on exceptions | |
1089 | # self.call_pdb is a property |
|
1089 | # self.call_pdb is a property | |
1090 | self.call_pdb = self.pdb |
|
1090 | self.call_pdb = self.pdb | |
1091 |
|
1091 | |||
1092 | def _get_call_pdb(self): |
|
1092 | def _get_call_pdb(self): | |
1093 | return self._call_pdb |
|
1093 | return self._call_pdb | |
1094 |
|
1094 | |||
1095 | def _set_call_pdb(self,val): |
|
1095 | def _set_call_pdb(self,val): | |
1096 |
|
1096 | |||
1097 | if val not in (0,1,False,True): |
|
1097 | if val not in (0,1,False,True): | |
1098 | raise ValueError('new call_pdb value must be boolean') |
|
1098 | raise ValueError('new call_pdb value must be boolean') | |
1099 |
|
1099 | |||
1100 | # store value in instance |
|
1100 | # store value in instance | |
1101 | self._call_pdb = val |
|
1101 | self._call_pdb = val | |
1102 |
|
1102 | |||
1103 | # notify the actual exception handlers |
|
1103 | # notify the actual exception handlers | |
1104 | self.InteractiveTB.call_pdb = val |
|
1104 | self.InteractiveTB.call_pdb = val | |
1105 |
|
1105 | |||
1106 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
1106 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
1107 | 'Control auto-activation of pdb at exceptions') |
|
1107 | 'Control auto-activation of pdb at exceptions') | |
1108 |
|
1108 | |||
1109 | def debugger(self,force=False): |
|
1109 | def debugger(self,force=False): | |
1110 | """Call the pdb debugger. |
|
1110 | """Call the pdb debugger. | |
1111 |
|
1111 | |||
1112 | Keywords: |
|
1112 | Keywords: | |
1113 |
|
1113 | |||
1114 | - force(False): by default, this routine checks the instance call_pdb |
|
1114 | - force(False): by default, this routine checks the instance call_pdb | |
1115 | flag and does not actually invoke the debugger if the flag is false. |
|
1115 | flag and does not actually invoke the debugger if the flag is false. | |
1116 | The 'force' option forces the debugger to activate even if the flag |
|
1116 | The 'force' option forces the debugger to activate even if the flag | |
1117 | is false. |
|
1117 | is false. | |
1118 | """ |
|
1118 | """ | |
1119 |
|
1119 | |||
1120 | if not (force or self.call_pdb): |
|
1120 | if not (force or self.call_pdb): | |
1121 | return |
|
1121 | return | |
1122 |
|
1122 | |||
1123 | if not hasattr(sys,'last_traceback'): |
|
1123 | if not hasattr(sys,'last_traceback'): | |
1124 | error('No traceback has been produced, nothing to debug.') |
|
1124 | error('No traceback has been produced, nothing to debug.') | |
1125 | return |
|
1125 | return | |
1126 |
|
1126 | |||
1127 | self.InteractiveTB.debugger(force=True) |
|
1127 | self.InteractiveTB.debugger(force=True) | |
1128 |
|
1128 | |||
1129 | #------------------------------------------------------------------------- |
|
1129 | #------------------------------------------------------------------------- | |
1130 | # Things related to IPython's various namespaces |
|
1130 | # Things related to IPython's various namespaces | |
1131 | #------------------------------------------------------------------------- |
|
1131 | #------------------------------------------------------------------------- | |
1132 | default_user_namespaces = True |
|
1132 | default_user_namespaces = True | |
1133 |
|
1133 | |||
1134 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
1134 | def init_create_namespaces(self, user_module=None, user_ns=None): | |
1135 | # Create the namespace where the user will operate. user_ns is |
|
1135 | # Create the namespace where the user will operate. user_ns is | |
1136 | # normally the only one used, and it is passed to the exec calls as |
|
1136 | # normally the only one used, and it is passed to the exec calls as | |
1137 | # the locals argument. But we do carry a user_global_ns namespace |
|
1137 | # the locals argument. But we do carry a user_global_ns namespace | |
1138 | # given as the exec 'globals' argument, This is useful in embedding |
|
1138 | # given as the exec 'globals' argument, This is useful in embedding | |
1139 | # situations where the ipython shell opens in a context where the |
|
1139 | # situations where the ipython shell opens in a context where the | |
1140 | # distinction between locals and globals is meaningful. For |
|
1140 | # distinction between locals and globals is meaningful. For | |
1141 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
1141 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
1142 |
|
1142 | |||
1143 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
1143 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
1144 | # level as a dict instead of a module. This is a manual fix, but I |
|
1144 | # level as a dict instead of a module. This is a manual fix, but I | |
1145 | # should really track down where the problem is coming from. Alex |
|
1145 | # should really track down where the problem is coming from. Alex | |
1146 | # Schmolck reported this problem first. |
|
1146 | # Schmolck reported this problem first. | |
1147 |
|
1147 | |||
1148 | # A useful post by Alex Martelli on this topic: |
|
1148 | # A useful post by Alex Martelli on this topic: | |
1149 | # Re: inconsistent value from __builtins__ |
|
1149 | # Re: inconsistent value from __builtins__ | |
1150 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
1150 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
1151 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
1151 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
1152 | # Gruppen: comp.lang.python |
|
1152 | # Gruppen: comp.lang.python | |
1153 |
|
1153 | |||
1154 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
1154 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
1155 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
1155 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
1156 | # > <type 'dict'> |
|
1156 | # > <type 'dict'> | |
1157 | # > >>> print type(__builtins__) |
|
1157 | # > >>> print type(__builtins__) | |
1158 | # > <type 'module'> |
|
1158 | # > <type 'module'> | |
1159 | # > Is this difference in return value intentional? |
|
1159 | # > Is this difference in return value intentional? | |
1160 |
|
1160 | |||
1161 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
1161 | # Well, it's documented that '__builtins__' can be either a dictionary | |
1162 | # or a module, and it's been that way for a long time. Whether it's |
|
1162 | # or a module, and it's been that way for a long time. Whether it's | |
1163 | # intentional (or sensible), I don't know. In any case, the idea is |
|
1163 | # intentional (or sensible), I don't know. In any case, the idea is | |
1164 | # that if you need to access the built-in namespace directly, you |
|
1164 | # that if you need to access the built-in namespace directly, you | |
1165 | # should start with "import __builtin__" (note, no 's') which will |
|
1165 | # should start with "import __builtin__" (note, no 's') which will | |
1166 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
1166 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
1167 |
|
1167 | |||
1168 | # These routines return a properly built module and dict as needed by |
|
1168 | # These routines return a properly built module and dict as needed by | |
1169 | # the rest of the code, and can also be used by extension writers to |
|
1169 | # the rest of the code, and can also be used by extension writers to | |
1170 | # generate properly initialized namespaces. |
|
1170 | # generate properly initialized namespaces. | |
1171 | if (user_ns is not None) or (user_module is not None): |
|
1171 | if (user_ns is not None) or (user_module is not None): | |
1172 | self.default_user_namespaces = False |
|
1172 | self.default_user_namespaces = False | |
1173 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
1173 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | |
1174 |
|
1174 | |||
1175 | # A record of hidden variables we have added to the user namespace, so |
|
1175 | # A record of hidden variables we have added to the user namespace, so | |
1176 | # we can list later only variables defined in actual interactive use. |
|
1176 | # we can list later only variables defined in actual interactive use. | |
1177 | self.user_ns_hidden = {} |
|
1177 | self.user_ns_hidden = {} | |
1178 |
|
1178 | |||
1179 | # Now that FakeModule produces a real module, we've run into a nasty |
|
1179 | # Now that FakeModule produces a real module, we've run into a nasty | |
1180 | # problem: after script execution (via %run), the module where the user |
|
1180 | # problem: after script execution (via %run), the module where the user | |
1181 | # code ran is deleted. Now that this object is a true module (needed |
|
1181 | # code ran is deleted. Now that this object is a true module (needed | |
1182 | # so doctest and other tools work correctly), the Python module |
|
1182 | # so doctest and other tools work correctly), the Python module | |
1183 | # teardown mechanism runs over it, and sets to None every variable |
|
1183 | # teardown mechanism runs over it, and sets to None every variable | |
1184 | # present in that module. Top-level references to objects from the |
|
1184 | # present in that module. Top-level references to objects from the | |
1185 | # script survive, because the user_ns is updated with them. However, |
|
1185 | # script survive, because the user_ns is updated with them. However, | |
1186 | # calling functions defined in the script that use other things from |
|
1186 | # calling functions defined in the script that use other things from | |
1187 | # the script will fail, because the function's closure had references |
|
1187 | # the script will fail, because the function's closure had references | |
1188 | # to the original objects, which are now all None. So we must protect |
|
1188 | # to the original objects, which are now all None. So we must protect | |
1189 | # these modules from deletion by keeping a cache. |
|
1189 | # these modules from deletion by keeping a cache. | |
1190 | # |
|
1190 | # | |
1191 | # To avoid keeping stale modules around (we only need the one from the |
|
1191 | # To avoid keeping stale modules around (we only need the one from the | |
1192 | # last run), we use a dict keyed with the full path to the script, so |
|
1192 | # last run), we use a dict keyed with the full path to the script, so | |
1193 | # only the last version of the module is held in the cache. Note, |
|
1193 | # only the last version of the module is held in the cache. Note, | |
1194 | # however, that we must cache the module *namespace contents* (their |
|
1194 | # however, that we must cache the module *namespace contents* (their | |
1195 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1195 | # __dict__). Because if we try to cache the actual modules, old ones | |
1196 | # (uncached) could be destroyed while still holding references (such as |
|
1196 | # (uncached) could be destroyed while still holding references (such as | |
1197 | # those held by GUI objects that tend to be long-lived)> |
|
1197 | # those held by GUI objects that tend to be long-lived)> | |
1198 | # |
|
1198 | # | |
1199 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1199 | # The %reset command will flush this cache. See the cache_main_mod() | |
1200 | # and clear_main_mod_cache() methods for details on use. |
|
1200 | # and clear_main_mod_cache() methods for details on use. | |
1201 |
|
1201 | |||
1202 | # This is the cache used for 'main' namespaces |
|
1202 | # This is the cache used for 'main' namespaces | |
1203 | self._main_mod_cache = {} |
|
1203 | self._main_mod_cache = {} | |
1204 |
|
1204 | |||
1205 | # A table holding all the namespaces IPython deals with, so that |
|
1205 | # A table holding all the namespaces IPython deals with, so that | |
1206 | # introspection facilities can search easily. |
|
1206 | # introspection facilities can search easily. | |
1207 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1207 | self.ns_table = {'user_global':self.user_module.__dict__, | |
1208 | 'user_local':self.user_ns, |
|
1208 | 'user_local':self.user_ns, | |
1209 | 'builtin':builtin_mod.__dict__ |
|
1209 | 'builtin':builtin_mod.__dict__ | |
1210 | } |
|
1210 | } | |
1211 |
|
1211 | |||
1212 | @property |
|
1212 | @property | |
1213 | def user_global_ns(self): |
|
1213 | def user_global_ns(self): | |
1214 | return self.user_module.__dict__ |
|
1214 | return self.user_module.__dict__ | |
1215 |
|
1215 | |||
1216 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1216 | def prepare_user_module(self, user_module=None, user_ns=None): | |
1217 | """Prepare the module and namespace in which user code will be run. |
|
1217 | """Prepare the module and namespace in which user code will be run. | |
1218 |
|
1218 | |||
1219 | When IPython is started normally, both parameters are None: a new module |
|
1219 | When IPython is started normally, both parameters are None: a new module | |
1220 | is created automatically, and its __dict__ used as the namespace. |
|
1220 | is created automatically, and its __dict__ used as the namespace. | |
1221 |
|
1221 | |||
1222 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1222 | If only user_module is provided, its __dict__ is used as the namespace. | |
1223 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1223 | If only user_ns is provided, a dummy module is created, and user_ns | |
1224 | becomes the global namespace. If both are provided (as they may be |
|
1224 | becomes the global namespace. If both are provided (as they may be | |
1225 | when embedding), user_ns is the local namespace, and user_module |
|
1225 | when embedding), user_ns is the local namespace, and user_module | |
1226 | provides the global namespace. |
|
1226 | provides the global namespace. | |
1227 |
|
1227 | |||
1228 | Parameters |
|
1228 | Parameters | |
1229 | ---------- |
|
1229 | ---------- | |
1230 | user_module : module, optional |
|
1230 | user_module : module, optional | |
1231 | The current user module in which IPython is being run. If None, |
|
1231 | The current user module in which IPython is being run. If None, | |
1232 | a clean module will be created. |
|
1232 | a clean module will be created. | |
1233 | user_ns : dict, optional |
|
1233 | user_ns : dict, optional | |
1234 | A namespace in which to run interactive commands. |
|
1234 | A namespace in which to run interactive commands. | |
1235 |
|
1235 | |||
1236 | Returns |
|
1236 | Returns | |
1237 | ------- |
|
1237 | ------- | |
1238 | A tuple of user_module and user_ns, each properly initialised. |
|
1238 | A tuple of user_module and user_ns, each properly initialised. | |
1239 | """ |
|
1239 | """ | |
1240 | if user_module is None and user_ns is not None: |
|
1240 | if user_module is None and user_ns is not None: | |
1241 | user_ns.setdefault("__name__", "__main__") |
|
1241 | user_ns.setdefault("__name__", "__main__") | |
1242 | user_module = DummyMod() |
|
1242 | user_module = DummyMod() | |
1243 | user_module.__dict__ = user_ns |
|
1243 | user_module.__dict__ = user_ns | |
1244 |
|
1244 | |||
1245 | if user_module is None: |
|
1245 | if user_module is None: | |
1246 | user_module = types.ModuleType("__main__", |
|
1246 | user_module = types.ModuleType("__main__", | |
1247 | doc="Automatically created module for IPython interactive environment") |
|
1247 | doc="Automatically created module for IPython interactive environment") | |
1248 |
|
1248 | |||
1249 | # We must ensure that __builtin__ (without the final 's') is always |
|
1249 | # We must ensure that __builtin__ (without the final 's') is always | |
1250 | # available and pointing to the __builtin__ *module*. For more details: |
|
1250 | # available and pointing to the __builtin__ *module*. For more details: | |
1251 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1251 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1252 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1252 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | |
1253 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1253 | user_module.__dict__.setdefault('__builtins__', builtin_mod) | |
1254 |
|
1254 | |||
1255 | if user_ns is None: |
|
1255 | if user_ns is None: | |
1256 | user_ns = user_module.__dict__ |
|
1256 | user_ns = user_module.__dict__ | |
1257 |
|
1257 | |||
1258 | return user_module, user_ns |
|
1258 | return user_module, user_ns | |
1259 |
|
1259 | |||
1260 | def init_sys_modules(self): |
|
1260 | def init_sys_modules(self): | |
1261 | # We need to insert into sys.modules something that looks like a |
|
1261 | # We need to insert into sys.modules something that looks like a | |
1262 | # module but which accesses the IPython namespace, for shelve and |
|
1262 | # module but which accesses the IPython namespace, for shelve and | |
1263 | # pickle to work interactively. Normally they rely on getting |
|
1263 | # pickle to work interactively. Normally they rely on getting | |
1264 | # everything out of __main__, but for embedding purposes each IPython |
|
1264 | # everything out of __main__, but for embedding purposes each IPython | |
1265 | # instance has its own private namespace, so we can't go shoving |
|
1265 | # instance has its own private namespace, so we can't go shoving | |
1266 | # everything into __main__. |
|
1266 | # everything into __main__. | |
1267 |
|
1267 | |||
1268 | # note, however, that we should only do this for non-embedded |
|
1268 | # note, however, that we should only do this for non-embedded | |
1269 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1269 | # ipythons, which really mimic the __main__.__dict__ with their own | |
1270 | # namespace. Embedded instances, on the other hand, should not do |
|
1270 | # namespace. Embedded instances, on the other hand, should not do | |
1271 | # this because they need to manage the user local/global namespaces |
|
1271 | # this because they need to manage the user local/global namespaces | |
1272 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1272 | # only, but they live within a 'normal' __main__ (meaning, they | |
1273 | # shouldn't overtake the execution environment of the script they're |
|
1273 | # shouldn't overtake the execution environment of the script they're | |
1274 | # embedded in). |
|
1274 | # embedded in). | |
1275 |
|
1275 | |||
1276 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1276 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
1277 | main_name = self.user_module.__name__ |
|
1277 | main_name = self.user_module.__name__ | |
1278 | sys.modules[main_name] = self.user_module |
|
1278 | sys.modules[main_name] = self.user_module | |
1279 |
|
1279 | |||
1280 | def init_user_ns(self): |
|
1280 | def init_user_ns(self): | |
1281 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1281 | """Initialize all user-visible namespaces to their minimum defaults. | |
1282 |
|
1282 | |||
1283 | Certain history lists are also initialized here, as they effectively |
|
1283 | Certain history lists are also initialized here, as they effectively | |
1284 | act as user namespaces. |
|
1284 | act as user namespaces. | |
1285 |
|
1285 | |||
1286 | Notes |
|
1286 | Notes | |
1287 | ----- |
|
1287 | ----- | |
1288 | All data structures here are only filled in, they are NOT reset by this |
|
1288 | All data structures here are only filled in, they are NOT reset by this | |
1289 | method. If they were not empty before, data will simply be added to |
|
1289 | method. If they were not empty before, data will simply be added to | |
1290 | them. |
|
1290 | them. | |
1291 | """ |
|
1291 | """ | |
1292 | # This function works in two parts: first we put a few things in |
|
1292 | # This function works in two parts: first we put a few things in | |
1293 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1293 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
1294 | # initial variables aren't shown by %who. After the sync, we add the |
|
1294 | # initial variables aren't shown by %who. After the sync, we add the | |
1295 | # rest of what we *do* want the user to see with %who even on a new |
|
1295 | # rest of what we *do* want the user to see with %who even on a new | |
1296 | # session (probably nothing, so they really only see their own stuff) |
|
1296 | # session (probably nothing, so they really only see their own stuff) | |
1297 |
|
1297 | |||
1298 | # The user dict must *always* have a __builtin__ reference to the |
|
1298 | # The user dict must *always* have a __builtin__ reference to the | |
1299 | # Python standard __builtin__ namespace, which must be imported. |
|
1299 | # Python standard __builtin__ namespace, which must be imported. | |
1300 | # This is so that certain operations in prompt evaluation can be |
|
1300 | # This is so that certain operations in prompt evaluation can be | |
1301 | # reliably executed with builtins. Note that we can NOT use |
|
1301 | # reliably executed with builtins. Note that we can NOT use | |
1302 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1302 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1303 | # module, and can even mutate at runtime, depending on the context |
|
1303 | # module, and can even mutate at runtime, depending on the context | |
1304 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1304 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1305 | # always a module object, though it must be explicitly imported. |
|
1305 | # always a module object, though it must be explicitly imported. | |
1306 |
|
1306 | |||
1307 | # For more details: |
|
1307 | # For more details: | |
1308 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1308 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1309 | ns = {} |
|
1309 | ns = {} | |
1310 |
|
1310 | |||
1311 | # make global variables for user access to the histories |
|
1311 | # make global variables for user access to the histories | |
1312 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1312 | ns['_ih'] = self.history_manager.input_hist_parsed | |
1313 | ns['_oh'] = self.history_manager.output_hist |
|
1313 | ns['_oh'] = self.history_manager.output_hist | |
1314 | ns['_dh'] = self.history_manager.dir_hist |
|
1314 | ns['_dh'] = self.history_manager.dir_hist | |
1315 |
|
1315 | |||
1316 | # user aliases to input and output histories. These shouldn't show up |
|
1316 | # user aliases to input and output histories. These shouldn't show up | |
1317 | # in %who, as they can have very large reprs. |
|
1317 | # in %who, as they can have very large reprs. | |
1318 | ns['In'] = self.history_manager.input_hist_parsed |
|
1318 | ns['In'] = self.history_manager.input_hist_parsed | |
1319 | ns['Out'] = self.history_manager.output_hist |
|
1319 | ns['Out'] = self.history_manager.output_hist | |
1320 |
|
1320 | |||
1321 | # Store myself as the public api!!! |
|
1321 | # Store myself as the public api!!! | |
1322 | ns['get_ipython'] = self.get_ipython |
|
1322 | ns['get_ipython'] = self.get_ipython | |
1323 |
|
1323 | |||
1324 | ns['exit'] = self.exiter |
|
1324 | ns['exit'] = self.exiter | |
1325 | ns['quit'] = self.exiter |
|
1325 | ns['quit'] = self.exiter | |
1326 |
|
1326 | |||
1327 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1327 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1328 | # by %who |
|
1328 | # by %who | |
1329 | self.user_ns_hidden.update(ns) |
|
1329 | self.user_ns_hidden.update(ns) | |
1330 |
|
1330 | |||
1331 | # Anything put into ns now would show up in %who. Think twice before |
|
1331 | # Anything put into ns now would show up in %who. Think twice before | |
1332 | # putting anything here, as we really want %who to show the user their |
|
1332 | # putting anything here, as we really want %who to show the user their | |
1333 | # stuff, not our variables. |
|
1333 | # stuff, not our variables. | |
1334 |
|
1334 | |||
1335 | # Finally, update the real user's namespace |
|
1335 | # Finally, update the real user's namespace | |
1336 | self.user_ns.update(ns) |
|
1336 | self.user_ns.update(ns) | |
1337 |
|
1337 | |||
1338 | @property |
|
1338 | @property | |
1339 | def all_ns_refs(self): |
|
1339 | def all_ns_refs(self): | |
1340 | """Get a list of references to all the namespace dictionaries in which |
|
1340 | """Get a list of references to all the namespace dictionaries in which | |
1341 | IPython might store a user-created object. |
|
1341 | IPython might store a user-created object. | |
1342 |
|
1342 | |||
1343 | Note that this does not include the displayhook, which also caches |
|
1343 | Note that this does not include the displayhook, which also caches | |
1344 | objects from the output.""" |
|
1344 | objects from the output.""" | |
1345 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1345 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ | |
1346 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1346 | [m.__dict__ for m in self._main_mod_cache.values()] | |
1347 |
|
1347 | |||
1348 | def reset(self, new_session=True, aggressive=False): |
|
1348 | def reset(self, new_session=True, aggressive=False): | |
1349 | """Clear all internal namespaces, and attempt to release references to |
|
1349 | """Clear all internal namespaces, and attempt to release references to | |
1350 | user objects. |
|
1350 | user objects. | |
1351 |
|
1351 | |||
1352 | If new_session is True, a new history session will be opened. |
|
1352 | If new_session is True, a new history session will be opened. | |
1353 | """ |
|
1353 | """ | |
1354 | # Clear histories |
|
1354 | # Clear histories | |
1355 | self.history_manager.reset(new_session) |
|
1355 | self.history_manager.reset(new_session) | |
1356 | # Reset counter used to index all histories |
|
1356 | # Reset counter used to index all histories | |
1357 | if new_session: |
|
1357 | if new_session: | |
1358 | self.execution_count = 1 |
|
1358 | self.execution_count = 1 | |
1359 |
|
1359 | |||
1360 | # Reset last execution result |
|
1360 | # Reset last execution result | |
1361 | self.last_execution_succeeded = True |
|
1361 | self.last_execution_succeeded = True | |
1362 | self.last_execution_result = None |
|
1362 | self.last_execution_result = None | |
1363 |
|
1363 | |||
1364 | # Flush cached output items |
|
1364 | # Flush cached output items | |
1365 | if self.displayhook.do_full_cache: |
|
1365 | if self.displayhook.do_full_cache: | |
1366 | self.displayhook.flush() |
|
1366 | self.displayhook.flush() | |
1367 |
|
1367 | |||
1368 | # The main execution namespaces must be cleared very carefully, |
|
1368 | # The main execution namespaces must be cleared very carefully, | |
1369 | # skipping the deletion of the builtin-related keys, because doing so |
|
1369 | # skipping the deletion of the builtin-related keys, because doing so | |
1370 | # would cause errors in many object's __del__ methods. |
|
1370 | # would cause errors in many object's __del__ methods. | |
1371 | if self.user_ns is not self.user_global_ns: |
|
1371 | if self.user_ns is not self.user_global_ns: | |
1372 | self.user_ns.clear() |
|
1372 | self.user_ns.clear() | |
1373 | ns = self.user_global_ns |
|
1373 | ns = self.user_global_ns | |
1374 | drop_keys = set(ns.keys()) |
|
1374 | drop_keys = set(ns.keys()) | |
1375 | drop_keys.discard('__builtin__') |
|
1375 | drop_keys.discard('__builtin__') | |
1376 | drop_keys.discard('__builtins__') |
|
1376 | drop_keys.discard('__builtins__') | |
1377 | drop_keys.discard('__name__') |
|
1377 | drop_keys.discard('__name__') | |
1378 | for k in drop_keys: |
|
1378 | for k in drop_keys: | |
1379 | del ns[k] |
|
1379 | del ns[k] | |
1380 |
|
1380 | |||
1381 | self.user_ns_hidden.clear() |
|
1381 | self.user_ns_hidden.clear() | |
1382 |
|
1382 | |||
1383 | # Restore the user namespaces to minimal usability |
|
1383 | # Restore the user namespaces to minimal usability | |
1384 | self.init_user_ns() |
|
1384 | self.init_user_ns() | |
1385 | if aggressive and not hasattr(self, "_sys_modules_keys"): |
|
1385 | if aggressive and not hasattr(self, "_sys_modules_keys"): | |
1386 | print("Cannot restore sys.module, no snapshot") |
|
1386 | print("Cannot restore sys.module, no snapshot") | |
1387 | elif aggressive: |
|
1387 | elif aggressive: | |
1388 | print("culling sys module...") |
|
1388 | print("culling sys module...") | |
1389 | current_keys = set(sys.modules.keys()) |
|
1389 | current_keys = set(sys.modules.keys()) | |
1390 | for k in current_keys - self._sys_modules_keys: |
|
1390 | for k in current_keys - self._sys_modules_keys: | |
1391 | if k.startswith("multiprocessing"): |
|
1391 | if k.startswith("multiprocessing"): | |
1392 | continue |
|
1392 | continue | |
1393 | del sys.modules[k] |
|
1393 | del sys.modules[k] | |
1394 |
|
1394 | |||
1395 | # Restore the default and user aliases |
|
1395 | # Restore the default and user aliases | |
1396 | self.alias_manager.clear_aliases() |
|
1396 | self.alias_manager.clear_aliases() | |
1397 | self.alias_manager.init_aliases() |
|
1397 | self.alias_manager.init_aliases() | |
1398 |
|
1398 | |||
1399 | # Now define aliases that only make sense on the terminal, because they |
|
1399 | # Now define aliases that only make sense on the terminal, because they | |
1400 | # need direct access to the console in a way that we can't emulate in |
|
1400 | # need direct access to the console in a way that we can't emulate in | |
1401 | # GUI or web frontend |
|
1401 | # GUI or web frontend | |
1402 | if os.name == 'posix': |
|
1402 | if os.name == 'posix': | |
1403 | for cmd in ('clear', 'more', 'less', 'man'): |
|
1403 | for cmd in ('clear', 'more', 'less', 'man'): | |
1404 | if cmd not in self.magics_manager.magics['line']: |
|
1404 | if cmd not in self.magics_manager.magics['line']: | |
1405 | self.alias_manager.soft_define_alias(cmd, cmd) |
|
1405 | self.alias_manager.soft_define_alias(cmd, cmd) | |
1406 |
|
1406 | |||
1407 | # Flush the private list of module references kept for script |
|
1407 | # Flush the private list of module references kept for script | |
1408 | # execution protection |
|
1408 | # execution protection | |
1409 | self.clear_main_mod_cache() |
|
1409 | self.clear_main_mod_cache() | |
1410 |
|
1410 | |||
1411 | def del_var(self, varname, by_name=False): |
|
1411 | def del_var(self, varname, by_name=False): | |
1412 | """Delete a variable from the various namespaces, so that, as |
|
1412 | """Delete a variable from the various namespaces, so that, as | |
1413 | far as possible, we're not keeping any hidden references to it. |
|
1413 | far as possible, we're not keeping any hidden references to it. | |
1414 |
|
1414 | |||
1415 | Parameters |
|
1415 | Parameters | |
1416 | ---------- |
|
1416 | ---------- | |
1417 | varname : str |
|
1417 | varname : str | |
1418 | The name of the variable to delete. |
|
1418 | The name of the variable to delete. | |
1419 | by_name : bool |
|
1419 | by_name : bool | |
1420 | If True, delete variables with the given name in each |
|
1420 | If True, delete variables with the given name in each | |
1421 | namespace. If False (default), find the variable in the user |
|
1421 | namespace. If False (default), find the variable in the user | |
1422 | namespace, and delete references to it. |
|
1422 | namespace, and delete references to it. | |
1423 | """ |
|
1423 | """ | |
1424 | if varname in ('__builtin__', '__builtins__'): |
|
1424 | if varname in ('__builtin__', '__builtins__'): | |
1425 | raise ValueError("Refusing to delete %s" % varname) |
|
1425 | raise ValueError("Refusing to delete %s" % varname) | |
1426 |
|
1426 | |||
1427 | ns_refs = self.all_ns_refs |
|
1427 | ns_refs = self.all_ns_refs | |
1428 |
|
1428 | |||
1429 | if by_name: # Delete by name |
|
1429 | if by_name: # Delete by name | |
1430 | for ns in ns_refs: |
|
1430 | for ns in ns_refs: | |
1431 | try: |
|
1431 | try: | |
1432 | del ns[varname] |
|
1432 | del ns[varname] | |
1433 | except KeyError: |
|
1433 | except KeyError: | |
1434 | pass |
|
1434 | pass | |
1435 | else: # Delete by object |
|
1435 | else: # Delete by object | |
1436 | try: |
|
1436 | try: | |
1437 | obj = self.user_ns[varname] |
|
1437 | obj = self.user_ns[varname] | |
1438 | except KeyError as e: |
|
1438 | except KeyError as e: | |
1439 | raise NameError("name '%s' is not defined" % varname) from e |
|
1439 | raise NameError("name '%s' is not defined" % varname) from e | |
1440 | # Also check in output history |
|
1440 | # Also check in output history | |
1441 | ns_refs.append(self.history_manager.output_hist) |
|
1441 | ns_refs.append(self.history_manager.output_hist) | |
1442 | for ns in ns_refs: |
|
1442 | for ns in ns_refs: | |
1443 | to_delete = [n for n, o in ns.items() if o is obj] |
|
1443 | to_delete = [n for n, o in ns.items() if o is obj] | |
1444 | for name in to_delete: |
|
1444 | for name in to_delete: | |
1445 | del ns[name] |
|
1445 | del ns[name] | |
1446 |
|
1446 | |||
1447 | # Ensure it is removed from the last execution result |
|
1447 | # Ensure it is removed from the last execution result | |
1448 | if self.last_execution_result.result is obj: |
|
1448 | if self.last_execution_result.result is obj: | |
1449 | self.last_execution_result = None |
|
1449 | self.last_execution_result = None | |
1450 |
|
1450 | |||
1451 | # displayhook keeps extra references, but not in a dictionary |
|
1451 | # displayhook keeps extra references, but not in a dictionary | |
1452 | for name in ('_', '__', '___'): |
|
1452 | for name in ('_', '__', '___'): | |
1453 | if getattr(self.displayhook, name) is obj: |
|
1453 | if getattr(self.displayhook, name) is obj: | |
1454 | setattr(self.displayhook, name, None) |
|
1454 | setattr(self.displayhook, name, None) | |
1455 |
|
1455 | |||
1456 | def reset_selective(self, regex=None): |
|
1456 | def reset_selective(self, regex=None): | |
1457 | """Clear selective variables from internal namespaces based on a |
|
1457 | """Clear selective variables from internal namespaces based on a | |
1458 | specified regular expression. |
|
1458 | specified regular expression. | |
1459 |
|
1459 | |||
1460 | Parameters |
|
1460 | Parameters | |
1461 | ---------- |
|
1461 | ---------- | |
1462 | regex : string or compiled pattern, optional |
|
1462 | regex : string or compiled pattern, optional | |
1463 | A regular expression pattern that will be used in searching |
|
1463 | A regular expression pattern that will be used in searching | |
1464 | variable names in the users namespaces. |
|
1464 | variable names in the users namespaces. | |
1465 | """ |
|
1465 | """ | |
1466 | if regex is not None: |
|
1466 | if regex is not None: | |
1467 | try: |
|
1467 | try: | |
1468 | m = re.compile(regex) |
|
1468 | m = re.compile(regex) | |
1469 | except TypeError as e: |
|
1469 | except TypeError as e: | |
1470 | raise TypeError('regex must be a string or compiled pattern') from e |
|
1470 | raise TypeError('regex must be a string or compiled pattern') from e | |
1471 | # Search for keys in each namespace that match the given regex |
|
1471 | # Search for keys in each namespace that match the given regex | |
1472 | # If a match is found, delete the key/value pair. |
|
1472 | # If a match is found, delete the key/value pair. | |
1473 | for ns in self.all_ns_refs: |
|
1473 | for ns in self.all_ns_refs: | |
1474 | for var in ns: |
|
1474 | for var in ns: | |
1475 | if m.search(var): |
|
1475 | if m.search(var): | |
1476 | del ns[var] |
|
1476 | del ns[var] | |
1477 |
|
1477 | |||
1478 | def push(self, variables, interactive=True): |
|
1478 | def push(self, variables, interactive=True): | |
1479 | """Inject a group of variables into the IPython user namespace. |
|
1479 | """Inject a group of variables into the IPython user namespace. | |
1480 |
|
1480 | |||
1481 | Parameters |
|
1481 | Parameters | |
1482 | ---------- |
|
1482 | ---------- | |
1483 | variables : dict, str or list/tuple of str |
|
1483 | variables : dict, str or list/tuple of str | |
1484 | The variables to inject into the user's namespace. If a dict, a |
|
1484 | The variables to inject into the user's namespace. If a dict, a | |
1485 | simple update is done. If a str, the string is assumed to have |
|
1485 | simple update is done. If a str, the string is assumed to have | |
1486 | variable names separated by spaces. A list/tuple of str can also |
|
1486 | variable names separated by spaces. A list/tuple of str can also | |
1487 | be used to give the variable names. If just the variable names are |
|
1487 | be used to give the variable names. If just the variable names are | |
1488 | give (list/tuple/str) then the variable values looked up in the |
|
1488 | give (list/tuple/str) then the variable values looked up in the | |
1489 | callers frame. |
|
1489 | callers frame. | |
1490 | interactive : bool |
|
1490 | interactive : bool | |
1491 | If True (default), the variables will be listed with the ``who`` |
|
1491 | If True (default), the variables will be listed with the ``who`` | |
1492 | magic. |
|
1492 | magic. | |
1493 | """ |
|
1493 | """ | |
1494 | vdict = None |
|
1494 | vdict = None | |
1495 |
|
1495 | |||
1496 | # We need a dict of name/value pairs to do namespace updates. |
|
1496 | # We need a dict of name/value pairs to do namespace updates. | |
1497 | if isinstance(variables, dict): |
|
1497 | if isinstance(variables, dict): | |
1498 | vdict = variables |
|
1498 | vdict = variables | |
1499 | elif isinstance(variables, (str, list, tuple)): |
|
1499 | elif isinstance(variables, (str, list, tuple)): | |
1500 | if isinstance(variables, str): |
|
1500 | if isinstance(variables, str): | |
1501 | vlist = variables.split() |
|
1501 | vlist = variables.split() | |
1502 | else: |
|
1502 | else: | |
1503 | vlist = variables |
|
1503 | vlist = variables | |
1504 | vdict = {} |
|
1504 | vdict = {} | |
1505 | cf = sys._getframe(1) |
|
1505 | cf = sys._getframe(1) | |
1506 | for name in vlist: |
|
1506 | for name in vlist: | |
1507 | try: |
|
1507 | try: | |
1508 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1508 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1509 | except: |
|
1509 | except: | |
1510 | print('Could not get variable %s from %s' % |
|
1510 | print('Could not get variable %s from %s' % | |
1511 | (name,cf.f_code.co_name)) |
|
1511 | (name,cf.f_code.co_name)) | |
1512 | else: |
|
1512 | else: | |
1513 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1513 | raise ValueError('variables must be a dict/str/list/tuple') | |
1514 |
|
1514 | |||
1515 | # Propagate variables to user namespace |
|
1515 | # Propagate variables to user namespace | |
1516 | self.user_ns.update(vdict) |
|
1516 | self.user_ns.update(vdict) | |
1517 |
|
1517 | |||
1518 | # And configure interactive visibility |
|
1518 | # And configure interactive visibility | |
1519 | user_ns_hidden = self.user_ns_hidden |
|
1519 | user_ns_hidden = self.user_ns_hidden | |
1520 | if interactive: |
|
1520 | if interactive: | |
1521 | for name in vdict: |
|
1521 | for name in vdict: | |
1522 | user_ns_hidden.pop(name, None) |
|
1522 | user_ns_hidden.pop(name, None) | |
1523 | else: |
|
1523 | else: | |
1524 | user_ns_hidden.update(vdict) |
|
1524 | user_ns_hidden.update(vdict) | |
1525 |
|
1525 | |||
1526 | def drop_by_id(self, variables): |
|
1526 | def drop_by_id(self, variables): | |
1527 | """Remove a dict of variables from the user namespace, if they are the |
|
1527 | """Remove a dict of variables from the user namespace, if they are the | |
1528 | same as the values in the dictionary. |
|
1528 | same as the values in the dictionary. | |
1529 |
|
1529 | |||
1530 | This is intended for use by extensions: variables that they've added can |
|
1530 | This is intended for use by extensions: variables that they've added can | |
1531 | be taken back out if they are unloaded, without removing any that the |
|
1531 | be taken back out if they are unloaded, without removing any that the | |
1532 | user has overwritten. |
|
1532 | user has overwritten. | |
1533 |
|
1533 | |||
1534 | Parameters |
|
1534 | Parameters | |
1535 | ---------- |
|
1535 | ---------- | |
1536 | variables : dict |
|
1536 | variables : dict | |
1537 | A dictionary mapping object names (as strings) to the objects. |
|
1537 | A dictionary mapping object names (as strings) to the objects. | |
1538 | """ |
|
1538 | """ | |
1539 | for name, obj in variables.items(): |
|
1539 | for name, obj in variables.items(): | |
1540 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1540 | if name in self.user_ns and self.user_ns[name] is obj: | |
1541 | del self.user_ns[name] |
|
1541 | del self.user_ns[name] | |
1542 | self.user_ns_hidden.pop(name, None) |
|
1542 | self.user_ns_hidden.pop(name, None) | |
1543 |
|
1543 | |||
1544 | #------------------------------------------------------------------------- |
|
1544 | #------------------------------------------------------------------------- | |
1545 | # Things related to object introspection |
|
1545 | # Things related to object introspection | |
1546 | #------------------------------------------------------------------------- |
|
1546 | #------------------------------------------------------------------------- | |
1547 |
|
1547 | |||
1548 | def _ofind(self, oname, namespaces=None): |
|
1548 | def _ofind(self, oname, namespaces=None): | |
1549 | """Find an object in the available namespaces. |
|
1549 | """Find an object in the available namespaces. | |
1550 |
|
1550 | |||
1551 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1551 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1552 |
|
1552 | |||
1553 | Has special code to detect magic functions. |
|
1553 | Has special code to detect magic functions. | |
1554 | """ |
|
1554 | """ | |
1555 | oname = oname.strip() |
|
1555 | oname = oname.strip() | |
1556 | raw_parts = oname.split(".") |
|
1556 | raw_parts = oname.split(".") | |
1557 | parts = [] |
|
1557 | parts = [] | |
1558 | parts_ok = True |
|
1558 | parts_ok = True | |
1559 | for p in raw_parts: |
|
1559 | for p in raw_parts: | |
1560 | if p.endswith("]"): |
|
1560 | if p.endswith("]"): | |
1561 | var, *indices = p.split("[") |
|
1561 | var, *indices = p.split("[") | |
1562 | if not var.isidentifier(): |
|
1562 | if not var.isidentifier(): | |
1563 | parts_ok = False |
|
1563 | parts_ok = False | |
1564 | break |
|
1564 | break | |
1565 | parts.append(var) |
|
1565 | parts.append(var) | |
1566 | for ind in indices: |
|
1566 | for ind in indices: | |
1567 | if ind[-1] != "]" and not is_integer_string(ind[:-1]): |
|
1567 | if ind[-1] != "]" and not is_integer_string(ind[:-1]): | |
1568 | parts_ok = False |
|
1568 | parts_ok = False | |
1569 | break |
|
1569 | break | |
1570 | parts.append(ind[:-1]) |
|
1570 | parts.append(ind[:-1]) | |
1571 | continue |
|
1571 | continue | |
1572 |
|
1572 | |||
1573 | if not p.isidentifier(): |
|
1573 | if not p.isidentifier(): | |
1574 | parts_ok = False |
|
1574 | parts_ok = False | |
1575 | parts.append(p) |
|
1575 | parts.append(p) | |
1576 |
|
1576 | |||
1577 | if ( |
|
1577 | if ( | |
1578 | not oname.startswith(ESC_MAGIC) |
|
1578 | not oname.startswith(ESC_MAGIC) | |
1579 | and not oname.startswith(ESC_MAGIC2) |
|
1579 | and not oname.startswith(ESC_MAGIC2) | |
1580 | and not parts_ok |
|
1580 | and not parts_ok | |
1581 | ): |
|
1581 | ): | |
1582 | return {"found": False} |
|
1582 | return {"found": False} | |
1583 |
|
1583 | |||
1584 | if namespaces is None: |
|
1584 | if namespaces is None: | |
1585 | # Namespaces to search in: |
|
1585 | # Namespaces to search in: | |
1586 | # Put them in a list. The order is important so that we |
|
1586 | # Put them in a list. The order is important so that we | |
1587 | # find things in the same order that Python finds them. |
|
1587 | # find things in the same order that Python finds them. | |
1588 | namespaces = [ ('Interactive', self.user_ns), |
|
1588 | namespaces = [ ('Interactive', self.user_ns), | |
1589 | ('Interactive (global)', self.user_global_ns), |
|
1589 | ('Interactive (global)', self.user_global_ns), | |
1590 | ('Python builtin', builtin_mod.__dict__), |
|
1590 | ('Python builtin', builtin_mod.__dict__), | |
1591 | ] |
|
1591 | ] | |
1592 |
|
1592 | |||
1593 | ismagic = False |
|
1593 | ismagic = False | |
1594 | isalias = False |
|
1594 | isalias = False | |
1595 | found = False |
|
1595 | found = False | |
1596 | ospace = None |
|
1596 | ospace = None | |
1597 | parent = None |
|
1597 | parent = None | |
1598 | obj = None |
|
1598 | obj = None | |
1599 |
|
1599 | |||
1600 |
|
1600 | |||
1601 | # Look for the given name by splitting it in parts. If the head is |
|
1601 | # Look for the given name by splitting it in parts. If the head is | |
1602 | # found, then we look for all the remaining parts as members, and only |
|
1602 | # found, then we look for all the remaining parts as members, and only | |
1603 | # declare success if we can find them all. |
|
1603 | # declare success if we can find them all. | |
1604 | oname_parts = parts |
|
1604 | oname_parts = parts | |
1605 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1605 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1606 | for nsname,ns in namespaces: |
|
1606 | for nsname,ns in namespaces: | |
1607 | try: |
|
1607 | try: | |
1608 | obj = ns[oname_head] |
|
1608 | obj = ns[oname_head] | |
1609 | except KeyError: |
|
1609 | except KeyError: | |
1610 | continue |
|
1610 | continue | |
1611 | else: |
|
1611 | else: | |
1612 | for idx, part in enumerate(oname_rest): |
|
1612 | for idx, part in enumerate(oname_rest): | |
1613 | try: |
|
1613 | try: | |
1614 | parent = obj |
|
1614 | parent = obj | |
1615 | # The last part is looked up in a special way to avoid |
|
1615 | # The last part is looked up in a special way to avoid | |
1616 | # descriptor invocation as it may raise or have side |
|
1616 | # descriptor invocation as it may raise or have side | |
1617 | # effects. |
|
1617 | # effects. | |
1618 | if idx == len(oname_rest) - 1: |
|
1618 | if idx == len(oname_rest) - 1: | |
1619 | obj = self._getattr_property(obj, part) |
|
1619 | obj = self._getattr_property(obj, part) | |
1620 | else: |
|
1620 | else: | |
1621 | if is_integer_string(part): |
|
1621 | if is_integer_string(part): | |
1622 | obj = obj[int(part)] |
|
1622 | obj = obj[int(part)] | |
1623 | else: |
|
1623 | else: | |
1624 | obj = getattr(obj, part) |
|
1624 | obj = getattr(obj, part) | |
1625 | except: |
|
1625 | except: | |
1626 | # Blanket except b/c some badly implemented objects |
|
1626 | # Blanket except b/c some badly implemented objects | |
1627 | # allow __getattr__ to raise exceptions other than |
|
1627 | # allow __getattr__ to raise exceptions other than | |
1628 | # AttributeError, which then crashes IPython. |
|
1628 | # AttributeError, which then crashes IPython. | |
1629 | break |
|
1629 | break | |
1630 | else: |
|
1630 | else: | |
1631 | # If we finish the for loop (no break), we got all members |
|
1631 | # If we finish the for loop (no break), we got all members | |
1632 | found = True |
|
1632 | found = True | |
1633 | ospace = nsname |
|
1633 | ospace = nsname | |
1634 | break # namespace loop |
|
1634 | break # namespace loop | |
1635 |
|
1635 | |||
1636 | # Try to see if it's magic |
|
1636 | # Try to see if it's magic | |
1637 | if not found: |
|
1637 | if not found: | |
1638 | obj = None |
|
1638 | obj = None | |
1639 | if oname.startswith(ESC_MAGIC2): |
|
1639 | if oname.startswith(ESC_MAGIC2): | |
1640 | oname = oname.lstrip(ESC_MAGIC2) |
|
1640 | oname = oname.lstrip(ESC_MAGIC2) | |
1641 | obj = self.find_cell_magic(oname) |
|
1641 | obj = self.find_cell_magic(oname) | |
1642 | elif oname.startswith(ESC_MAGIC): |
|
1642 | elif oname.startswith(ESC_MAGIC): | |
1643 | oname = oname.lstrip(ESC_MAGIC) |
|
1643 | oname = oname.lstrip(ESC_MAGIC) | |
1644 | obj = self.find_line_magic(oname) |
|
1644 | obj = self.find_line_magic(oname) | |
1645 | else: |
|
1645 | else: | |
1646 | # search without prefix, so run? will find %run? |
|
1646 | # search without prefix, so run? will find %run? | |
1647 | obj = self.find_line_magic(oname) |
|
1647 | obj = self.find_line_magic(oname) | |
1648 | if obj is None: |
|
1648 | if obj is None: | |
1649 | obj = self.find_cell_magic(oname) |
|
1649 | obj = self.find_cell_magic(oname) | |
1650 | if obj is not None: |
|
1650 | if obj is not None: | |
1651 | found = True |
|
1651 | found = True | |
1652 | ospace = 'IPython internal' |
|
1652 | ospace = 'IPython internal' | |
1653 | ismagic = True |
|
1653 | ismagic = True | |
1654 | isalias = isinstance(obj, Alias) |
|
1654 | isalias = isinstance(obj, Alias) | |
1655 |
|
1655 | |||
1656 | # Last try: special-case some literals like '', [], {}, etc: |
|
1656 | # Last try: special-case some literals like '', [], {}, etc: | |
1657 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1657 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1658 | obj = eval(oname_head) |
|
1658 | obj = eval(oname_head) | |
1659 | found = True |
|
1659 | found = True | |
1660 | ospace = 'Interactive' |
|
1660 | ospace = 'Interactive' | |
1661 |
|
1661 | |||
1662 | return { |
|
1662 | return { | |
1663 | 'obj':obj, |
|
1663 | 'obj':obj, | |
1664 | 'found':found, |
|
1664 | 'found':found, | |
1665 | 'parent':parent, |
|
1665 | 'parent':parent, | |
1666 | 'ismagic':ismagic, |
|
1666 | 'ismagic':ismagic, | |
1667 | 'isalias':isalias, |
|
1667 | 'isalias':isalias, | |
1668 | 'namespace':ospace |
|
1668 | 'namespace':ospace | |
1669 | } |
|
1669 | } | |
1670 |
|
1670 | |||
1671 | @staticmethod |
|
1671 | @staticmethod | |
1672 | def _getattr_property(obj, attrname): |
|
1672 | def _getattr_property(obj, attrname): | |
1673 | """Property-aware getattr to use in object finding. |
|
1673 | """Property-aware getattr to use in object finding. | |
1674 |
|
1674 | |||
1675 | If attrname represents a property, return it unevaluated (in case it has |
|
1675 | If attrname represents a property, return it unevaluated (in case it has | |
1676 | side effects or raises an error. |
|
1676 | side effects or raises an error. | |
1677 |
|
1677 | |||
1678 | """ |
|
1678 | """ | |
1679 | if not isinstance(obj, type): |
|
1679 | if not isinstance(obj, type): | |
1680 | try: |
|
1680 | try: | |
1681 | # `getattr(type(obj), attrname)` is not guaranteed to return |
|
1681 | # `getattr(type(obj), attrname)` is not guaranteed to return | |
1682 | # `obj`, but does so for property: |
|
1682 | # `obj`, but does so for property: | |
1683 | # |
|
1683 | # | |
1684 | # property.__get__(self, None, cls) -> self |
|
1684 | # property.__get__(self, None, cls) -> self | |
1685 | # |
|
1685 | # | |
1686 | # The universal alternative is to traverse the mro manually |
|
1686 | # The universal alternative is to traverse the mro manually | |
1687 | # searching for attrname in class dicts. |
|
1687 | # searching for attrname in class dicts. | |
1688 | if is_integer_string(attrname): |
|
1688 | if is_integer_string(attrname): | |
1689 | return obj[int(attrname)] |
|
1689 | return obj[int(attrname)] | |
1690 | else: |
|
1690 | else: | |
1691 | attr = getattr(type(obj), attrname) |
|
1691 | attr = getattr(type(obj), attrname) | |
1692 | except AttributeError: |
|
1692 | except AttributeError: | |
1693 | pass |
|
1693 | pass | |
1694 | else: |
|
1694 | else: | |
1695 | # This relies on the fact that data descriptors (with both |
|
1695 | # This relies on the fact that data descriptors (with both | |
1696 | # __get__ & __set__ magic methods) take precedence over |
|
1696 | # __get__ & __set__ magic methods) take precedence over | |
1697 | # instance-level attributes: |
|
1697 | # instance-level attributes: | |
1698 | # |
|
1698 | # | |
1699 | # class A(object): |
|
1699 | # class A(object): | |
1700 | # @property |
|
1700 | # @property | |
1701 | # def foobar(self): return 123 |
|
1701 | # def foobar(self): return 123 | |
1702 | # a = A() |
|
1702 | # a = A() | |
1703 | # a.__dict__['foobar'] = 345 |
|
1703 | # a.__dict__['foobar'] = 345 | |
1704 | # a.foobar # == 123 |
|
1704 | # a.foobar # == 123 | |
1705 | # |
|
1705 | # | |
1706 | # So, a property may be returned right away. |
|
1706 | # So, a property may be returned right away. | |
1707 | if isinstance(attr, property): |
|
1707 | if isinstance(attr, property): | |
1708 | return attr |
|
1708 | return attr | |
1709 |
|
1709 | |||
1710 | # Nothing helped, fall back. |
|
1710 | # Nothing helped, fall back. | |
1711 | return getattr(obj, attrname) |
|
1711 | return getattr(obj, attrname) | |
1712 |
|
1712 | |||
1713 | def _object_find(self, oname, namespaces=None): |
|
1713 | def _object_find(self, oname, namespaces=None): | |
1714 | """Find an object and return a struct with info about it.""" |
|
1714 | """Find an object and return a struct with info about it.""" | |
1715 | return Struct(self._ofind(oname, namespaces)) |
|
1715 | return Struct(self._ofind(oname, namespaces)) | |
1716 |
|
1716 | |||
1717 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1717 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1718 | """Generic interface to the inspector system. |
|
1718 | """Generic interface to the inspector system. | |
1719 |
|
1719 | |||
1720 | This function is meant to be called by pdef, pdoc & friends. |
|
1720 | This function is meant to be called by pdef, pdoc & friends. | |
1721 | """ |
|
1721 | """ | |
1722 | info = self._object_find(oname, namespaces) |
|
1722 | info = self._object_find(oname, namespaces) | |
1723 | docformat = ( |
|
1723 | docformat = ( | |
1724 | sphinxify(self.object_inspect(oname)) if self.sphinxify_docstring else None |
|
1724 | sphinxify(self.object_inspect(oname)) if self.sphinxify_docstring else None | |
1725 | ) |
|
1725 | ) | |
1726 | if info.found: |
|
1726 | if info.found: | |
1727 | pmethod = getattr(self.inspector, meth) |
|
1727 | pmethod = getattr(self.inspector, meth) | |
1728 | # TODO: only apply format_screen to the plain/text repr of the mime |
|
1728 | # TODO: only apply format_screen to the plain/text repr of the mime | |
1729 | # bundle. |
|
1729 | # bundle. | |
1730 | formatter = format_screen if info.ismagic else docformat |
|
1730 | formatter = format_screen if info.ismagic else docformat | |
1731 | if meth == 'pdoc': |
|
1731 | if meth == 'pdoc': | |
1732 | pmethod(info.obj, oname, formatter) |
|
1732 | pmethod(info.obj, oname, formatter) | |
1733 | elif meth == 'pinfo': |
|
1733 | elif meth == 'pinfo': | |
1734 | pmethod( |
|
1734 | pmethod( | |
1735 | info.obj, |
|
1735 | info.obj, | |
1736 | oname, |
|
1736 | oname, | |
1737 | formatter, |
|
1737 | formatter, | |
1738 | info, |
|
1738 | info, | |
1739 | enable_html_pager=self.enable_html_pager, |
|
1739 | enable_html_pager=self.enable_html_pager, | |
1740 | **kw, |
|
1740 | **kw, | |
1741 | ) |
|
1741 | ) | |
1742 | else: |
|
1742 | else: | |
1743 | pmethod(info.obj, oname) |
|
1743 | pmethod(info.obj, oname) | |
1744 | else: |
|
1744 | else: | |
1745 | print('Object `%s` not found.' % oname) |
|
1745 | print('Object `%s` not found.' % oname) | |
1746 | return 'not found' # so callers can take other action |
|
1746 | return 'not found' # so callers can take other action | |
1747 |
|
1747 | |||
1748 | def object_inspect(self, oname, detail_level=0): |
|
1748 | def object_inspect(self, oname, detail_level=0): | |
1749 | """Get object info about oname""" |
|
1749 | """Get object info about oname""" | |
1750 | with self.builtin_trap: |
|
1750 | with self.builtin_trap: | |
1751 | info = self._object_find(oname) |
|
1751 | info = self._object_find(oname) | |
1752 | if info.found: |
|
1752 | if info.found: | |
1753 | return self.inspector.info(info.obj, oname, info=info, |
|
1753 | return self.inspector.info(info.obj, oname, info=info, | |
1754 | detail_level=detail_level |
|
1754 | detail_level=detail_level | |
1755 | ) |
|
1755 | ) | |
1756 | else: |
|
1756 | else: | |
1757 | return oinspect.object_info(name=oname, found=False) |
|
1757 | return oinspect.object_info(name=oname, found=False) | |
1758 |
|
1758 | |||
1759 | def object_inspect_text(self, oname, detail_level=0): |
|
1759 | def object_inspect_text(self, oname, detail_level=0): | |
1760 | """Get object info as formatted text""" |
|
1760 | """Get object info as formatted text""" | |
1761 | return self.object_inspect_mime(oname, detail_level)['text/plain'] |
|
1761 | return self.object_inspect_mime(oname, detail_level)['text/plain'] | |
1762 |
|
1762 | |||
1763 | def object_inspect_mime(self, oname, detail_level=0, omit_sections=()): |
|
1763 | def object_inspect_mime(self, oname, detail_level=0, omit_sections=()): | |
1764 | """Get object info as a mimebundle of formatted representations. |
|
1764 | """Get object info as a mimebundle of formatted representations. | |
1765 |
|
1765 | |||
1766 | A mimebundle is a dictionary, keyed by mime-type. |
|
1766 | A mimebundle is a dictionary, keyed by mime-type. | |
1767 | It must always have the key `'text/plain'`. |
|
1767 | It must always have the key `'text/plain'`. | |
1768 | """ |
|
1768 | """ | |
1769 | with self.builtin_trap: |
|
1769 | with self.builtin_trap: | |
1770 | info = self._object_find(oname) |
|
1770 | info = self._object_find(oname) | |
1771 | if info.found: |
|
1771 | if info.found: | |
1772 | docformat = ( |
|
1772 | docformat = ( | |
1773 | sphinxify(self.object_inspect(oname)) |
|
1773 | sphinxify(self.object_inspect(oname)) | |
1774 | if self.sphinxify_docstring |
|
1774 | if self.sphinxify_docstring | |
1775 | else None |
|
1775 | else None | |
1776 | ) |
|
1776 | ) | |
1777 | return self.inspector._get_info( |
|
1777 | return self.inspector._get_info( | |
1778 | info.obj, |
|
1778 | info.obj, | |
1779 | oname, |
|
1779 | oname, | |
1780 | info=info, |
|
1780 | info=info, | |
1781 | detail_level=detail_level, |
|
1781 | detail_level=detail_level, | |
1782 | formatter=docformat, |
|
1782 | formatter=docformat, | |
1783 | omit_sections=omit_sections, |
|
1783 | omit_sections=omit_sections, | |
1784 | ) |
|
1784 | ) | |
1785 | else: |
|
1785 | else: | |
1786 | raise KeyError(oname) |
|
1786 | raise KeyError(oname) | |
1787 |
|
1787 | |||
1788 | #------------------------------------------------------------------------- |
|
1788 | #------------------------------------------------------------------------- | |
1789 | # Things related to history management |
|
1789 | # Things related to history management | |
1790 | #------------------------------------------------------------------------- |
|
1790 | #------------------------------------------------------------------------- | |
1791 |
|
1791 | |||
1792 | def init_history(self): |
|
1792 | def init_history(self): | |
1793 | """Sets up the command history, and starts regular autosaves.""" |
|
1793 | """Sets up the command history, and starts regular autosaves.""" | |
1794 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1794 | self.history_manager = HistoryManager(shell=self, parent=self) | |
1795 | self.configurables.append(self.history_manager) |
|
1795 | self.configurables.append(self.history_manager) | |
1796 |
|
1796 | |||
1797 | #------------------------------------------------------------------------- |
|
1797 | #------------------------------------------------------------------------- | |
1798 | # Things related to exception handling and tracebacks (not debugging) |
|
1798 | # Things related to exception handling and tracebacks (not debugging) | |
1799 | #------------------------------------------------------------------------- |
|
1799 | #------------------------------------------------------------------------- | |
1800 |
|
1800 | |||
1801 | debugger_cls = InterruptiblePdb |
|
1801 | debugger_cls = InterruptiblePdb | |
1802 |
|
1802 | |||
1803 | def init_traceback_handlers(self, custom_exceptions): |
|
1803 | def init_traceback_handlers(self, custom_exceptions): | |
1804 | # Syntax error handler. |
|
1804 | # Syntax error handler. | |
1805 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self) |
|
1805 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self) | |
1806 |
|
1806 | |||
1807 | # The interactive one is initialized with an offset, meaning we always |
|
1807 | # The interactive one is initialized with an offset, meaning we always | |
1808 | # want to remove the topmost item in the traceback, which is our own |
|
1808 | # want to remove the topmost item in the traceback, which is our own | |
1809 | # internal code. Valid modes: ['Plain','Context','Verbose','Minimal'] |
|
1809 | # internal code. Valid modes: ['Plain','Context','Verbose','Minimal'] | |
1810 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1810 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1811 | color_scheme='NoColor', |
|
1811 | color_scheme='NoColor', | |
1812 | tb_offset = 1, |
|
1812 | tb_offset = 1, | |
1813 | check_cache=check_linecache_ipython, |
|
|||
1814 | debugger_cls=self.debugger_cls, parent=self) |
|
1813 | debugger_cls=self.debugger_cls, parent=self) | |
1815 |
|
1814 | |||
1816 | # The instance will store a pointer to the system-wide exception hook, |
|
1815 | # The instance will store a pointer to the system-wide exception hook, | |
1817 | # so that runtime code (such as magics) can access it. This is because |
|
1816 | # so that runtime code (such as magics) can access it. This is because | |
1818 | # during the read-eval loop, it may get temporarily overwritten. |
|
1817 | # during the read-eval loop, it may get temporarily overwritten. | |
1819 | self.sys_excepthook = sys.excepthook |
|
1818 | self.sys_excepthook = sys.excepthook | |
1820 |
|
1819 | |||
1821 | # and add any custom exception handlers the user may have specified |
|
1820 | # and add any custom exception handlers the user may have specified | |
1822 | self.set_custom_exc(*custom_exceptions) |
|
1821 | self.set_custom_exc(*custom_exceptions) | |
1823 |
|
1822 | |||
1824 | # Set the exception mode |
|
1823 | # Set the exception mode | |
1825 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1824 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1826 |
|
1825 | |||
1827 | def set_custom_exc(self, exc_tuple, handler): |
|
1826 | def set_custom_exc(self, exc_tuple, handler): | |
1828 | """set_custom_exc(exc_tuple, handler) |
|
1827 | """set_custom_exc(exc_tuple, handler) | |
1829 |
|
1828 | |||
1830 | Set a custom exception handler, which will be called if any of the |
|
1829 | Set a custom exception handler, which will be called if any of the | |
1831 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1830 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1832 | run_code() method). |
|
1831 | run_code() method). | |
1833 |
|
1832 | |||
1834 | Parameters |
|
1833 | Parameters | |
1835 | ---------- |
|
1834 | ---------- | |
1836 | exc_tuple : tuple of exception classes |
|
1835 | exc_tuple : tuple of exception classes | |
1837 | A *tuple* of exception classes, for which to call the defined |
|
1836 | A *tuple* of exception classes, for which to call the defined | |
1838 | handler. It is very important that you use a tuple, and NOT A |
|
1837 | handler. It is very important that you use a tuple, and NOT A | |
1839 | LIST here, because of the way Python's except statement works. If |
|
1838 | LIST here, because of the way Python's except statement works. If | |
1840 | you only want to trap a single exception, use a singleton tuple:: |
|
1839 | you only want to trap a single exception, use a singleton tuple:: | |
1841 |
|
1840 | |||
1842 | exc_tuple == (MyCustomException,) |
|
1841 | exc_tuple == (MyCustomException,) | |
1843 |
|
1842 | |||
1844 | handler : callable |
|
1843 | handler : callable | |
1845 | handler must have the following signature:: |
|
1844 | handler must have the following signature:: | |
1846 |
|
1845 | |||
1847 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1846 | def my_handler(self, etype, value, tb, tb_offset=None): | |
1848 | ... |
|
1847 | ... | |
1849 | return structured_traceback |
|
1848 | return structured_traceback | |
1850 |
|
1849 | |||
1851 | Your handler must return a structured traceback (a list of strings), |
|
1850 | Your handler must return a structured traceback (a list of strings), | |
1852 | or None. |
|
1851 | or None. | |
1853 |
|
1852 | |||
1854 | This will be made into an instance method (via types.MethodType) |
|
1853 | This will be made into an instance method (via types.MethodType) | |
1855 | of IPython itself, and it will be called if any of the exceptions |
|
1854 | of IPython itself, and it will be called if any of the exceptions | |
1856 | listed in the exc_tuple are caught. If the handler is None, an |
|
1855 | listed in the exc_tuple are caught. If the handler is None, an | |
1857 | internal basic one is used, which just prints basic info. |
|
1856 | internal basic one is used, which just prints basic info. | |
1858 |
|
1857 | |||
1859 | To protect IPython from crashes, if your handler ever raises an |
|
1858 | To protect IPython from crashes, if your handler ever raises an | |
1860 | exception or returns an invalid result, it will be immediately |
|
1859 | exception or returns an invalid result, it will be immediately | |
1861 | disabled. |
|
1860 | disabled. | |
1862 |
|
1861 | |||
1863 | Notes |
|
1862 | Notes | |
1864 | ----- |
|
1863 | ----- | |
1865 | WARNING: by putting in your own exception handler into IPython's main |
|
1864 | WARNING: by putting in your own exception handler into IPython's main | |
1866 | execution loop, you run a very good chance of nasty crashes. This |
|
1865 | execution loop, you run a very good chance of nasty crashes. This | |
1867 | facility should only be used if you really know what you are doing. |
|
1866 | facility should only be used if you really know what you are doing. | |
1868 | """ |
|
1867 | """ | |
1869 |
|
1868 | |||
1870 | if not isinstance(exc_tuple, tuple): |
|
1869 | if not isinstance(exc_tuple, tuple): | |
1871 | raise TypeError("The custom exceptions must be given as a tuple.") |
|
1870 | raise TypeError("The custom exceptions must be given as a tuple.") | |
1872 |
|
1871 | |||
1873 | def dummy_handler(self, etype, value, tb, tb_offset=None): |
|
1872 | def dummy_handler(self, etype, value, tb, tb_offset=None): | |
1874 | print('*** Simple custom exception handler ***') |
|
1873 | print('*** Simple custom exception handler ***') | |
1875 | print('Exception type :', etype) |
|
1874 | print('Exception type :', etype) | |
1876 | print('Exception value:', value) |
|
1875 | print('Exception value:', value) | |
1877 | print('Traceback :', tb) |
|
1876 | print('Traceback :', tb) | |
1878 |
|
1877 | |||
1879 | def validate_stb(stb): |
|
1878 | def validate_stb(stb): | |
1880 | """validate structured traceback return type |
|
1879 | """validate structured traceback return type | |
1881 |
|
1880 | |||
1882 | return type of CustomTB *should* be a list of strings, but allow |
|
1881 | return type of CustomTB *should* be a list of strings, but allow | |
1883 | single strings or None, which are harmless. |
|
1882 | single strings or None, which are harmless. | |
1884 |
|
1883 | |||
1885 | This function will *always* return a list of strings, |
|
1884 | This function will *always* return a list of strings, | |
1886 | and will raise a TypeError if stb is inappropriate. |
|
1885 | and will raise a TypeError if stb is inappropriate. | |
1887 | """ |
|
1886 | """ | |
1888 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1887 | msg = "CustomTB must return list of strings, not %r" % stb | |
1889 | if stb is None: |
|
1888 | if stb is None: | |
1890 | return [] |
|
1889 | return [] | |
1891 | elif isinstance(stb, str): |
|
1890 | elif isinstance(stb, str): | |
1892 | return [stb] |
|
1891 | return [stb] | |
1893 | elif not isinstance(stb, list): |
|
1892 | elif not isinstance(stb, list): | |
1894 | raise TypeError(msg) |
|
1893 | raise TypeError(msg) | |
1895 | # it's a list |
|
1894 | # it's a list | |
1896 | for line in stb: |
|
1895 | for line in stb: | |
1897 | # check every element |
|
1896 | # check every element | |
1898 | if not isinstance(line, str): |
|
1897 | if not isinstance(line, str): | |
1899 | raise TypeError(msg) |
|
1898 | raise TypeError(msg) | |
1900 | return stb |
|
1899 | return stb | |
1901 |
|
1900 | |||
1902 | if handler is None: |
|
1901 | if handler is None: | |
1903 | wrapped = dummy_handler |
|
1902 | wrapped = dummy_handler | |
1904 | else: |
|
1903 | else: | |
1905 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1904 | def wrapped(self,etype,value,tb,tb_offset=None): | |
1906 | """wrap CustomTB handler, to protect IPython from user code |
|
1905 | """wrap CustomTB handler, to protect IPython from user code | |
1907 |
|
1906 | |||
1908 | This makes it harder (but not impossible) for custom exception |
|
1907 | This makes it harder (but not impossible) for custom exception | |
1909 | handlers to crash IPython. |
|
1908 | handlers to crash IPython. | |
1910 | """ |
|
1909 | """ | |
1911 | try: |
|
1910 | try: | |
1912 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1911 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | |
1913 | return validate_stb(stb) |
|
1912 | return validate_stb(stb) | |
1914 | except: |
|
1913 | except: | |
1915 | # clear custom handler immediately |
|
1914 | # clear custom handler immediately | |
1916 | self.set_custom_exc((), None) |
|
1915 | self.set_custom_exc((), None) | |
1917 | print("Custom TB Handler failed, unregistering", file=sys.stderr) |
|
1916 | print("Custom TB Handler failed, unregistering", file=sys.stderr) | |
1918 | # show the exception in handler first |
|
1917 | # show the exception in handler first | |
1919 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1918 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | |
1920 | print(self.InteractiveTB.stb2text(stb)) |
|
1919 | print(self.InteractiveTB.stb2text(stb)) | |
1921 | print("The original exception:") |
|
1920 | print("The original exception:") | |
1922 | stb = self.InteractiveTB.structured_traceback( |
|
1921 | stb = self.InteractiveTB.structured_traceback( | |
1923 | (etype,value,tb), tb_offset=tb_offset |
|
1922 | (etype,value,tb), tb_offset=tb_offset | |
1924 | ) |
|
1923 | ) | |
1925 | return stb |
|
1924 | return stb | |
1926 |
|
1925 | |||
1927 | self.CustomTB = types.MethodType(wrapped,self) |
|
1926 | self.CustomTB = types.MethodType(wrapped,self) | |
1928 | self.custom_exceptions = exc_tuple |
|
1927 | self.custom_exceptions = exc_tuple | |
1929 |
|
1928 | |||
1930 | def excepthook(self, etype, value, tb): |
|
1929 | def excepthook(self, etype, value, tb): | |
1931 | """One more defense for GUI apps that call sys.excepthook. |
|
1930 | """One more defense for GUI apps that call sys.excepthook. | |
1932 |
|
1931 | |||
1933 | GUI frameworks like wxPython trap exceptions and call |
|
1932 | GUI frameworks like wxPython trap exceptions and call | |
1934 | sys.excepthook themselves. I guess this is a feature that |
|
1933 | sys.excepthook themselves. I guess this is a feature that | |
1935 | enables them to keep running after exceptions that would |
|
1934 | enables them to keep running after exceptions that would | |
1936 | otherwise kill their mainloop. This is a bother for IPython |
|
1935 | otherwise kill their mainloop. This is a bother for IPython | |
1937 | which expects to catch all of the program exceptions with a try: |
|
1936 | which expects to catch all of the program exceptions with a try: | |
1938 | except: statement. |
|
1937 | except: statement. | |
1939 |
|
1938 | |||
1940 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1939 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1941 | any app directly invokes sys.excepthook, it will look to the user like |
|
1940 | any app directly invokes sys.excepthook, it will look to the user like | |
1942 | IPython crashed. In order to work around this, we can disable the |
|
1941 | IPython crashed. In order to work around this, we can disable the | |
1943 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1942 | CrashHandler and replace it with this excepthook instead, which prints a | |
1944 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1943 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1945 | call sys.excepthook will generate a regular-looking exception from |
|
1944 | call sys.excepthook will generate a regular-looking exception from | |
1946 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1945 | IPython, and the CrashHandler will only be triggered by real IPython | |
1947 | crashes. |
|
1946 | crashes. | |
1948 |
|
1947 | |||
1949 | This hook should be used sparingly, only in places which are not likely |
|
1948 | This hook should be used sparingly, only in places which are not likely | |
1950 | to be true IPython errors. |
|
1949 | to be true IPython errors. | |
1951 | """ |
|
1950 | """ | |
1952 | self.showtraceback((etype, value, tb), tb_offset=0) |
|
1951 | self.showtraceback((etype, value, tb), tb_offset=0) | |
1953 |
|
1952 | |||
1954 | def _get_exc_info(self, exc_tuple=None): |
|
1953 | def _get_exc_info(self, exc_tuple=None): | |
1955 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1954 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | |
1956 |
|
1955 | |||
1957 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1956 | Ensures sys.last_type,value,traceback hold the exc_info we found, | |
1958 | from whichever source. |
|
1957 | from whichever source. | |
1959 |
|
1958 | |||
1960 | raises ValueError if none of these contain any information |
|
1959 | raises ValueError if none of these contain any information | |
1961 | """ |
|
1960 | """ | |
1962 | if exc_tuple is None: |
|
1961 | if exc_tuple is None: | |
1963 | etype, value, tb = sys.exc_info() |
|
1962 | etype, value, tb = sys.exc_info() | |
1964 | else: |
|
1963 | else: | |
1965 | etype, value, tb = exc_tuple |
|
1964 | etype, value, tb = exc_tuple | |
1966 |
|
1965 | |||
1967 | if etype is None: |
|
1966 | if etype is None: | |
1968 | if hasattr(sys, 'last_type'): |
|
1967 | if hasattr(sys, 'last_type'): | |
1969 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1968 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1970 | sys.last_traceback |
|
1969 | sys.last_traceback | |
1971 |
|
1970 | |||
1972 | if etype is None: |
|
1971 | if etype is None: | |
1973 | raise ValueError("No exception to find") |
|
1972 | raise ValueError("No exception to find") | |
1974 |
|
1973 | |||
1975 | # Now store the exception info in sys.last_type etc. |
|
1974 | # Now store the exception info in sys.last_type etc. | |
1976 | # WARNING: these variables are somewhat deprecated and not |
|
1975 | # WARNING: these variables are somewhat deprecated and not | |
1977 | # necessarily safe to use in a threaded environment, but tools |
|
1976 | # necessarily safe to use in a threaded environment, but tools | |
1978 | # like pdb depend on their existence, so let's set them. If we |
|
1977 | # like pdb depend on their existence, so let's set them. If we | |
1979 | # find problems in the field, we'll need to revisit their use. |
|
1978 | # find problems in the field, we'll need to revisit their use. | |
1980 | sys.last_type = etype |
|
1979 | sys.last_type = etype | |
1981 | sys.last_value = value |
|
1980 | sys.last_value = value | |
1982 | sys.last_traceback = tb |
|
1981 | sys.last_traceback = tb | |
1983 |
|
1982 | |||
1984 | return etype, value, tb |
|
1983 | return etype, value, tb | |
1985 |
|
1984 | |||
1986 | def show_usage_error(self, exc): |
|
1985 | def show_usage_error(self, exc): | |
1987 | """Show a short message for UsageErrors |
|
1986 | """Show a short message for UsageErrors | |
1988 |
|
1987 | |||
1989 | These are special exceptions that shouldn't show a traceback. |
|
1988 | These are special exceptions that shouldn't show a traceback. | |
1990 | """ |
|
1989 | """ | |
1991 | print("UsageError: %s" % exc, file=sys.stderr) |
|
1990 | print("UsageError: %s" % exc, file=sys.stderr) | |
1992 |
|
1991 | |||
1993 | def get_exception_only(self, exc_tuple=None): |
|
1992 | def get_exception_only(self, exc_tuple=None): | |
1994 | """ |
|
1993 | """ | |
1995 | Return as a string (ending with a newline) the exception that |
|
1994 | Return as a string (ending with a newline) the exception that | |
1996 | just occurred, without any traceback. |
|
1995 | just occurred, without any traceback. | |
1997 | """ |
|
1996 | """ | |
1998 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1997 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1999 | msg = traceback.format_exception_only(etype, value) |
|
1998 | msg = traceback.format_exception_only(etype, value) | |
2000 | return ''.join(msg) |
|
1999 | return ''.join(msg) | |
2001 |
|
2000 | |||
2002 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, |
|
2001 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, | |
2003 | exception_only=False, running_compiled_code=False): |
|
2002 | exception_only=False, running_compiled_code=False): | |
2004 | """Display the exception that just occurred. |
|
2003 | """Display the exception that just occurred. | |
2005 |
|
2004 | |||
2006 | If nothing is known about the exception, this is the method which |
|
2005 | If nothing is known about the exception, this is the method which | |
2007 | should be used throughout the code for presenting user tracebacks, |
|
2006 | should be used throughout the code for presenting user tracebacks, | |
2008 | rather than directly invoking the InteractiveTB object. |
|
2007 | rather than directly invoking the InteractiveTB object. | |
2009 |
|
2008 | |||
2010 | A specific showsyntaxerror() also exists, but this method can take |
|
2009 | A specific showsyntaxerror() also exists, but this method can take | |
2011 | care of calling it if needed, so unless you are explicitly catching a |
|
2010 | care of calling it if needed, so unless you are explicitly catching a | |
2012 | SyntaxError exception, don't try to analyze the stack manually and |
|
2011 | SyntaxError exception, don't try to analyze the stack manually and | |
2013 | simply call this method.""" |
|
2012 | simply call this method.""" | |
2014 |
|
2013 | |||
2015 | try: |
|
2014 | try: | |
2016 | try: |
|
2015 | try: | |
2017 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
2016 | etype, value, tb = self._get_exc_info(exc_tuple) | |
2018 | except ValueError: |
|
2017 | except ValueError: | |
2019 | print('No traceback available to show.', file=sys.stderr) |
|
2018 | print('No traceback available to show.', file=sys.stderr) | |
2020 | return |
|
2019 | return | |
2021 |
|
2020 | |||
2022 | if issubclass(etype, SyntaxError): |
|
2021 | if issubclass(etype, SyntaxError): | |
2023 | # Though this won't be called by syntax errors in the input |
|
2022 | # Though this won't be called by syntax errors in the input | |
2024 | # line, there may be SyntaxError cases with imported code. |
|
2023 | # line, there may be SyntaxError cases with imported code. | |
2025 | self.showsyntaxerror(filename, running_compiled_code) |
|
2024 | self.showsyntaxerror(filename, running_compiled_code) | |
2026 | elif etype is UsageError: |
|
2025 | elif etype is UsageError: | |
2027 | self.show_usage_error(value) |
|
2026 | self.show_usage_error(value) | |
2028 | else: |
|
2027 | else: | |
2029 | if exception_only: |
|
2028 | if exception_only: | |
2030 | stb = ['An exception has occurred, use %tb to see ' |
|
2029 | stb = ['An exception has occurred, use %tb to see ' | |
2031 | 'the full traceback.\n'] |
|
2030 | 'the full traceback.\n'] | |
2032 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
2031 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
2033 | value)) |
|
2032 | value)) | |
2034 | else: |
|
2033 | else: | |
2035 | try: |
|
2034 | try: | |
2036 | # Exception classes can customise their traceback - we |
|
2035 | # Exception classes can customise their traceback - we | |
2037 | # use this in IPython.parallel for exceptions occurring |
|
2036 | # use this in IPython.parallel for exceptions occurring | |
2038 | # in the engines. This should return a list of strings. |
|
2037 | # in the engines. This should return a list of strings. | |
2039 | if hasattr(value, "_render_traceback_"): |
|
2038 | if hasattr(value, "_render_traceback_"): | |
2040 | stb = value._render_traceback_() |
|
2039 | stb = value._render_traceback_() | |
2041 | else: |
|
2040 | else: | |
2042 | stb = self.InteractiveTB.structured_traceback( |
|
2041 | stb = self.InteractiveTB.structured_traceback( | |
2043 | etype, value, tb, tb_offset=tb_offset |
|
2042 | etype, value, tb, tb_offset=tb_offset | |
2044 | ) |
|
2043 | ) | |
2045 |
|
2044 | |||
2046 | except Exception: |
|
2045 | except Exception: | |
2047 | print( |
|
2046 | print( | |
2048 | "Unexpected exception formatting exception. Falling back to standard exception" |
|
2047 | "Unexpected exception formatting exception. Falling back to standard exception" | |
2049 | ) |
|
2048 | ) | |
2050 | traceback.print_exc() |
|
2049 | traceback.print_exc() | |
2051 | return None |
|
2050 | return None | |
2052 |
|
2051 | |||
2053 | self._showtraceback(etype, value, stb) |
|
2052 | self._showtraceback(etype, value, stb) | |
2054 | if self.call_pdb: |
|
2053 | if self.call_pdb: | |
2055 | # drop into debugger |
|
2054 | # drop into debugger | |
2056 | self.debugger(force=True) |
|
2055 | self.debugger(force=True) | |
2057 | return |
|
2056 | return | |
2058 |
|
2057 | |||
2059 | # Actually show the traceback |
|
2058 | # Actually show the traceback | |
2060 | self._showtraceback(etype, value, stb) |
|
2059 | self._showtraceback(etype, value, stb) | |
2061 |
|
2060 | |||
2062 | except KeyboardInterrupt: |
|
2061 | except KeyboardInterrupt: | |
2063 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2062 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2064 |
|
2063 | |||
2065 | def _showtraceback(self, etype, evalue, stb: str): |
|
2064 | def _showtraceback(self, etype, evalue, stb: str): | |
2066 | """Actually show a traceback. |
|
2065 | """Actually show a traceback. | |
2067 |
|
2066 | |||
2068 | Subclasses may override this method to put the traceback on a different |
|
2067 | Subclasses may override this method to put the traceback on a different | |
2069 | place, like a side channel. |
|
2068 | place, like a side channel. | |
2070 | """ |
|
2069 | """ | |
2071 | val = self.InteractiveTB.stb2text(stb) |
|
2070 | val = self.InteractiveTB.stb2text(stb) | |
2072 | try: |
|
2071 | try: | |
2073 | print(val) |
|
2072 | print(val) | |
2074 | except UnicodeEncodeError: |
|
2073 | except UnicodeEncodeError: | |
2075 | print(val.encode("utf-8", "backslashreplace").decode()) |
|
2074 | print(val.encode("utf-8", "backslashreplace").decode()) | |
2076 |
|
2075 | |||
2077 | def showsyntaxerror(self, filename=None, running_compiled_code=False): |
|
2076 | def showsyntaxerror(self, filename=None, running_compiled_code=False): | |
2078 | """Display the syntax error that just occurred. |
|
2077 | """Display the syntax error that just occurred. | |
2079 |
|
2078 | |||
2080 | This doesn't display a stack trace because there isn't one. |
|
2079 | This doesn't display a stack trace because there isn't one. | |
2081 |
|
2080 | |||
2082 | If a filename is given, it is stuffed in the exception instead |
|
2081 | If a filename is given, it is stuffed in the exception instead | |
2083 | of what was there before (because Python's parser always uses |
|
2082 | of what was there before (because Python's parser always uses | |
2084 | "<string>" when reading from a string). |
|
2083 | "<string>" when reading from a string). | |
2085 |
|
2084 | |||
2086 | If the syntax error occurred when running a compiled code (i.e. running_compile_code=True), |
|
2085 | If the syntax error occurred when running a compiled code (i.e. running_compile_code=True), | |
2087 | longer stack trace will be displayed. |
|
2086 | longer stack trace will be displayed. | |
2088 | """ |
|
2087 | """ | |
2089 | etype, value, last_traceback = self._get_exc_info() |
|
2088 | etype, value, last_traceback = self._get_exc_info() | |
2090 |
|
2089 | |||
2091 | if filename and issubclass(etype, SyntaxError): |
|
2090 | if filename and issubclass(etype, SyntaxError): | |
2092 | try: |
|
2091 | try: | |
2093 | value.filename = filename |
|
2092 | value.filename = filename | |
2094 | except: |
|
2093 | except: | |
2095 | # Not the format we expect; leave it alone |
|
2094 | # Not the format we expect; leave it alone | |
2096 | pass |
|
2095 | pass | |
2097 |
|
2096 | |||
2098 | # If the error occurred when executing compiled code, we should provide full stacktrace. |
|
2097 | # If the error occurred when executing compiled code, we should provide full stacktrace. | |
2099 | elist = traceback.extract_tb(last_traceback) if running_compiled_code else [] |
|
2098 | elist = traceback.extract_tb(last_traceback) if running_compiled_code else [] | |
2100 | stb = self.SyntaxTB.structured_traceback(etype, value, elist) |
|
2099 | stb = self.SyntaxTB.structured_traceback(etype, value, elist) | |
2101 | self._showtraceback(etype, value, stb) |
|
2100 | self._showtraceback(etype, value, stb) | |
2102 |
|
2101 | |||
2103 | # This is overridden in TerminalInteractiveShell to show a message about |
|
2102 | # This is overridden in TerminalInteractiveShell to show a message about | |
2104 | # the %paste magic. |
|
2103 | # the %paste magic. | |
2105 | def showindentationerror(self): |
|
2104 | def showindentationerror(self): | |
2106 | """Called by _run_cell when there's an IndentationError in code entered |
|
2105 | """Called by _run_cell when there's an IndentationError in code entered | |
2107 | at the prompt. |
|
2106 | at the prompt. | |
2108 |
|
2107 | |||
2109 | This is overridden in TerminalInteractiveShell to show a message about |
|
2108 | This is overridden in TerminalInteractiveShell to show a message about | |
2110 | the %paste magic.""" |
|
2109 | the %paste magic.""" | |
2111 | self.showsyntaxerror() |
|
2110 | self.showsyntaxerror() | |
2112 |
|
2111 | |||
2113 | @skip_doctest |
|
2112 | @skip_doctest | |
2114 | def set_next_input(self, s, replace=False): |
|
2113 | def set_next_input(self, s, replace=False): | |
2115 | """ Sets the 'default' input string for the next command line. |
|
2114 | """ Sets the 'default' input string for the next command line. | |
2116 |
|
2115 | |||
2117 | Example:: |
|
2116 | Example:: | |
2118 |
|
2117 | |||
2119 | In [1]: _ip.set_next_input("Hello Word") |
|
2118 | In [1]: _ip.set_next_input("Hello Word") | |
2120 | In [2]: Hello Word_ # cursor is here |
|
2119 | In [2]: Hello Word_ # cursor is here | |
2121 | """ |
|
2120 | """ | |
2122 | self.rl_next_input = s |
|
2121 | self.rl_next_input = s | |
2123 |
|
2122 | |||
2124 | def _indent_current_str(self): |
|
2123 | def _indent_current_str(self): | |
2125 | """return the current level of indentation as a string""" |
|
2124 | """return the current level of indentation as a string""" | |
2126 | return self.input_splitter.get_indent_spaces() * ' ' |
|
2125 | return self.input_splitter.get_indent_spaces() * ' ' | |
2127 |
|
2126 | |||
2128 | #------------------------------------------------------------------------- |
|
2127 | #------------------------------------------------------------------------- | |
2129 | # Things related to text completion |
|
2128 | # Things related to text completion | |
2130 | #------------------------------------------------------------------------- |
|
2129 | #------------------------------------------------------------------------- | |
2131 |
|
2130 | |||
2132 | def init_completer(self): |
|
2131 | def init_completer(self): | |
2133 | """Initialize the completion machinery. |
|
2132 | """Initialize the completion machinery. | |
2134 |
|
2133 | |||
2135 | This creates completion machinery that can be used by client code, |
|
2134 | This creates completion machinery that can be used by client code, | |
2136 | either interactively in-process (typically triggered by the readline |
|
2135 | either interactively in-process (typically triggered by the readline | |
2137 | library), programmatically (such as in test suites) or out-of-process |
|
2136 | library), programmatically (such as in test suites) or out-of-process | |
2138 | (typically over the network by remote frontends). |
|
2137 | (typically over the network by remote frontends). | |
2139 | """ |
|
2138 | """ | |
2140 | from IPython.core.completer import IPCompleter |
|
2139 | from IPython.core.completer import IPCompleter | |
2141 | from IPython.core.completerlib import ( |
|
2140 | from IPython.core.completerlib import ( | |
2142 | cd_completer, |
|
2141 | cd_completer, | |
2143 | magic_run_completer, |
|
2142 | magic_run_completer, | |
2144 | module_completer, |
|
2143 | module_completer, | |
2145 | reset_completer, |
|
2144 | reset_completer, | |
2146 | ) |
|
2145 | ) | |
2147 |
|
2146 | |||
2148 | self.Completer = IPCompleter(shell=self, |
|
2147 | self.Completer = IPCompleter(shell=self, | |
2149 | namespace=self.user_ns, |
|
2148 | namespace=self.user_ns, | |
2150 | global_namespace=self.user_global_ns, |
|
2149 | global_namespace=self.user_global_ns, | |
2151 | parent=self, |
|
2150 | parent=self, | |
2152 | ) |
|
2151 | ) | |
2153 | self.configurables.append(self.Completer) |
|
2152 | self.configurables.append(self.Completer) | |
2154 |
|
2153 | |||
2155 | # Add custom completers to the basic ones built into IPCompleter |
|
2154 | # Add custom completers to the basic ones built into IPCompleter | |
2156 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
2155 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
2157 | self.strdispatchers['complete_command'] = sdisp |
|
2156 | self.strdispatchers['complete_command'] = sdisp | |
2158 | self.Completer.custom_completers = sdisp |
|
2157 | self.Completer.custom_completers = sdisp | |
2159 |
|
2158 | |||
2160 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
2159 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
2161 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
2160 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
2162 | self.set_hook('complete_command', module_completer, str_key = '%aimport') |
|
2161 | self.set_hook('complete_command', module_completer, str_key = '%aimport') | |
2163 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
2162 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
2164 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
2163 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
2165 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
2164 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | |
2166 |
|
2165 | |||
2167 | @skip_doctest |
|
2166 | @skip_doctest | |
2168 | def complete(self, text, line=None, cursor_pos=None): |
|
2167 | def complete(self, text, line=None, cursor_pos=None): | |
2169 | """Return the completed text and a list of completions. |
|
2168 | """Return the completed text and a list of completions. | |
2170 |
|
2169 | |||
2171 | Parameters |
|
2170 | Parameters | |
2172 | ---------- |
|
2171 | ---------- | |
2173 | text : string |
|
2172 | text : string | |
2174 | A string of text to be completed on. It can be given as empty and |
|
2173 | A string of text to be completed on. It can be given as empty and | |
2175 | instead a line/position pair are given. In this case, the |
|
2174 | instead a line/position pair are given. In this case, the | |
2176 | completer itself will split the line like readline does. |
|
2175 | completer itself will split the line like readline does. | |
2177 | line : string, optional |
|
2176 | line : string, optional | |
2178 | The complete line that text is part of. |
|
2177 | The complete line that text is part of. | |
2179 | cursor_pos : int, optional |
|
2178 | cursor_pos : int, optional | |
2180 | The position of the cursor on the input line. |
|
2179 | The position of the cursor on the input line. | |
2181 |
|
2180 | |||
2182 | Returns |
|
2181 | Returns | |
2183 | ------- |
|
2182 | ------- | |
2184 | text : string |
|
2183 | text : string | |
2185 | The actual text that was completed. |
|
2184 | The actual text that was completed. | |
2186 | matches : list |
|
2185 | matches : list | |
2187 | A sorted list with all possible completions. |
|
2186 | A sorted list with all possible completions. | |
2188 |
|
2187 | |||
2189 | Notes |
|
2188 | Notes | |
2190 | ----- |
|
2189 | ----- | |
2191 | The optional arguments allow the completion to take more context into |
|
2190 | The optional arguments allow the completion to take more context into | |
2192 | account, and are part of the low-level completion API. |
|
2191 | account, and are part of the low-level completion API. | |
2193 |
|
2192 | |||
2194 | This is a wrapper around the completion mechanism, similar to what |
|
2193 | This is a wrapper around the completion mechanism, similar to what | |
2195 | readline does at the command line when the TAB key is hit. By |
|
2194 | readline does at the command line when the TAB key is hit. By | |
2196 | exposing it as a method, it can be used by other non-readline |
|
2195 | exposing it as a method, it can be used by other non-readline | |
2197 | environments (such as GUIs) for text completion. |
|
2196 | environments (such as GUIs) for text completion. | |
2198 |
|
2197 | |||
2199 | Examples |
|
2198 | Examples | |
2200 | -------- |
|
2199 | -------- | |
2201 | In [1]: x = 'hello' |
|
2200 | In [1]: x = 'hello' | |
2202 |
|
2201 | |||
2203 | In [2]: _ip.complete('x.l') |
|
2202 | In [2]: _ip.complete('x.l') | |
2204 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
2203 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
2205 | """ |
|
2204 | """ | |
2206 |
|
2205 | |||
2207 | # Inject names into __builtin__ so we can complete on the added names. |
|
2206 | # Inject names into __builtin__ so we can complete on the added names. | |
2208 | with self.builtin_trap: |
|
2207 | with self.builtin_trap: | |
2209 | return self.Completer.complete(text, line, cursor_pos) |
|
2208 | return self.Completer.complete(text, line, cursor_pos) | |
2210 |
|
2209 | |||
2211 | def set_custom_completer(self, completer, pos=0) -> None: |
|
2210 | def set_custom_completer(self, completer, pos=0) -> None: | |
2212 | """Adds a new custom completer function. |
|
2211 | """Adds a new custom completer function. | |
2213 |
|
2212 | |||
2214 | The position argument (defaults to 0) is the index in the completers |
|
2213 | The position argument (defaults to 0) is the index in the completers | |
2215 | list where you want the completer to be inserted. |
|
2214 | list where you want the completer to be inserted. | |
2216 |
|
2215 | |||
2217 | `completer` should have the following signature:: |
|
2216 | `completer` should have the following signature:: | |
2218 |
|
2217 | |||
2219 | def completion(self: Completer, text: string) -> List[str]: |
|
2218 | def completion(self: Completer, text: string) -> List[str]: | |
2220 | raise NotImplementedError |
|
2219 | raise NotImplementedError | |
2221 |
|
2220 | |||
2222 | It will be bound to the current Completer instance and pass some text |
|
2221 | It will be bound to the current Completer instance and pass some text | |
2223 | and return a list with current completions to suggest to the user. |
|
2222 | and return a list with current completions to suggest to the user. | |
2224 | """ |
|
2223 | """ | |
2225 |
|
2224 | |||
2226 | newcomp = types.MethodType(completer, self.Completer) |
|
2225 | newcomp = types.MethodType(completer, self.Completer) | |
2227 | self.Completer.custom_matchers.insert(pos,newcomp) |
|
2226 | self.Completer.custom_matchers.insert(pos,newcomp) | |
2228 |
|
2227 | |||
2229 | def set_completer_frame(self, frame=None): |
|
2228 | def set_completer_frame(self, frame=None): | |
2230 | """Set the frame of the completer.""" |
|
2229 | """Set the frame of the completer.""" | |
2231 | if frame: |
|
2230 | if frame: | |
2232 | self.Completer.namespace = frame.f_locals |
|
2231 | self.Completer.namespace = frame.f_locals | |
2233 | self.Completer.global_namespace = frame.f_globals |
|
2232 | self.Completer.global_namespace = frame.f_globals | |
2234 | else: |
|
2233 | else: | |
2235 | self.Completer.namespace = self.user_ns |
|
2234 | self.Completer.namespace = self.user_ns | |
2236 | self.Completer.global_namespace = self.user_global_ns |
|
2235 | self.Completer.global_namespace = self.user_global_ns | |
2237 |
|
2236 | |||
2238 | #------------------------------------------------------------------------- |
|
2237 | #------------------------------------------------------------------------- | |
2239 | # Things related to magics |
|
2238 | # Things related to magics | |
2240 | #------------------------------------------------------------------------- |
|
2239 | #------------------------------------------------------------------------- | |
2241 |
|
2240 | |||
2242 | def init_magics(self): |
|
2241 | def init_magics(self): | |
2243 | from IPython.core import magics as m |
|
2242 | from IPython.core import magics as m | |
2244 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2243 | self.magics_manager = magic.MagicsManager(shell=self, | |
2245 | parent=self, |
|
2244 | parent=self, | |
2246 | user_magics=m.UserMagics(self)) |
|
2245 | user_magics=m.UserMagics(self)) | |
2247 | self.configurables.append(self.magics_manager) |
|
2246 | self.configurables.append(self.magics_manager) | |
2248 |
|
2247 | |||
2249 | # Expose as public API from the magics manager |
|
2248 | # Expose as public API from the magics manager | |
2250 | self.register_magics = self.magics_manager.register |
|
2249 | self.register_magics = self.magics_manager.register | |
2251 |
|
2250 | |||
2252 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2251 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |
2253 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2252 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, | |
2254 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2253 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |
2255 | m.NamespaceMagics, m.OSMagics, m.PackagingMagics, |
|
2254 | m.NamespaceMagics, m.OSMagics, m.PackagingMagics, | |
2256 | m.PylabMagics, m.ScriptMagics, |
|
2255 | m.PylabMagics, m.ScriptMagics, | |
2257 | ) |
|
2256 | ) | |
2258 | self.register_magics(m.AsyncMagics) |
|
2257 | self.register_magics(m.AsyncMagics) | |
2259 |
|
2258 | |||
2260 | # Register Magic Aliases |
|
2259 | # Register Magic Aliases | |
2261 | mman = self.magics_manager |
|
2260 | mman = self.magics_manager | |
2262 | # FIXME: magic aliases should be defined by the Magics classes |
|
2261 | # FIXME: magic aliases should be defined by the Magics classes | |
2263 | # or in MagicsManager, not here |
|
2262 | # or in MagicsManager, not here | |
2264 | mman.register_alias('ed', 'edit') |
|
2263 | mman.register_alias('ed', 'edit') | |
2265 | mman.register_alias('hist', 'history') |
|
2264 | mman.register_alias('hist', 'history') | |
2266 | mman.register_alias('rep', 'recall') |
|
2265 | mman.register_alias('rep', 'recall') | |
2267 | mman.register_alias('SVG', 'svg', 'cell') |
|
2266 | mman.register_alias('SVG', 'svg', 'cell') | |
2268 | mman.register_alias('HTML', 'html', 'cell') |
|
2267 | mman.register_alias('HTML', 'html', 'cell') | |
2269 | mman.register_alias('file', 'writefile', 'cell') |
|
2268 | mman.register_alias('file', 'writefile', 'cell') | |
2270 |
|
2269 | |||
2271 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2270 | # FIXME: Move the color initialization to the DisplayHook, which | |
2272 | # should be split into a prompt manager and displayhook. We probably |
|
2271 | # should be split into a prompt manager and displayhook. We probably | |
2273 | # even need a centralize colors management object. |
|
2272 | # even need a centralize colors management object. | |
2274 | self.run_line_magic('colors', self.colors) |
|
2273 | self.run_line_magic('colors', self.colors) | |
2275 |
|
2274 | |||
2276 | # Defined here so that it's included in the documentation |
|
2275 | # Defined here so that it's included in the documentation | |
2277 | @functools.wraps(magic.MagicsManager.register_function) |
|
2276 | @functools.wraps(magic.MagicsManager.register_function) | |
2278 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2277 | def register_magic_function(self, func, magic_kind='line', magic_name=None): | |
2279 | self.magics_manager.register_function( |
|
2278 | self.magics_manager.register_function( | |
2280 | func, magic_kind=magic_kind, magic_name=magic_name |
|
2279 | func, magic_kind=magic_kind, magic_name=magic_name | |
2281 | ) |
|
2280 | ) | |
2282 |
|
2281 | |||
2283 | def _find_with_lazy_load(self, /, type_, magic_name: str): |
|
2282 | def _find_with_lazy_load(self, /, type_, magic_name: str): | |
2284 | """ |
|
2283 | """ | |
2285 | Try to find a magic potentially lazy-loading it. |
|
2284 | Try to find a magic potentially lazy-loading it. | |
2286 |
|
2285 | |||
2287 | Parameters |
|
2286 | Parameters | |
2288 | ---------- |
|
2287 | ---------- | |
2289 |
|
2288 | |||
2290 | type_: "line"|"cell" |
|
2289 | type_: "line"|"cell" | |
2291 | the type of magics we are trying to find/lazy load. |
|
2290 | the type of magics we are trying to find/lazy load. | |
2292 | magic_name: str |
|
2291 | magic_name: str | |
2293 | The name of the magic we are trying to find/lazy load |
|
2292 | The name of the magic we are trying to find/lazy load | |
2294 |
|
2293 | |||
2295 |
|
2294 | |||
2296 | Note that this may have any side effects |
|
2295 | Note that this may have any side effects | |
2297 | """ |
|
2296 | """ | |
2298 | finder = {"line": self.find_line_magic, "cell": self.find_cell_magic}[type_] |
|
2297 | finder = {"line": self.find_line_magic, "cell": self.find_cell_magic}[type_] | |
2299 | fn = finder(magic_name) |
|
2298 | fn = finder(magic_name) | |
2300 | if fn is not None: |
|
2299 | if fn is not None: | |
2301 | return fn |
|
2300 | return fn | |
2302 | lazy = self.magics_manager.lazy_magics.get(magic_name) |
|
2301 | lazy = self.magics_manager.lazy_magics.get(magic_name) | |
2303 | if lazy is None: |
|
2302 | if lazy is None: | |
2304 | return None |
|
2303 | return None | |
2305 |
|
2304 | |||
2306 | self.run_line_magic("load_ext", lazy) |
|
2305 | self.run_line_magic("load_ext", lazy) | |
2307 | res = finder(magic_name) |
|
2306 | res = finder(magic_name) | |
2308 | return res |
|
2307 | return res | |
2309 |
|
2308 | |||
2310 | def run_line_magic(self, magic_name: str, line, _stack_depth=1): |
|
2309 | def run_line_magic(self, magic_name: str, line, _stack_depth=1): | |
2311 | """Execute the given line magic. |
|
2310 | """Execute the given line magic. | |
2312 |
|
2311 | |||
2313 | Parameters |
|
2312 | Parameters | |
2314 | ---------- |
|
2313 | ---------- | |
2315 | magic_name : str |
|
2314 | magic_name : str | |
2316 | Name of the desired magic function, without '%' prefix. |
|
2315 | Name of the desired magic function, without '%' prefix. | |
2317 | line : str |
|
2316 | line : str | |
2318 | The rest of the input line as a single string. |
|
2317 | The rest of the input line as a single string. | |
2319 | _stack_depth : int |
|
2318 | _stack_depth : int | |
2320 | If run_line_magic() is called from magic() then _stack_depth=2. |
|
2319 | If run_line_magic() is called from magic() then _stack_depth=2. | |
2321 | This is added to ensure backward compatibility for use of 'get_ipython().magic()' |
|
2320 | This is added to ensure backward compatibility for use of 'get_ipython().magic()' | |
2322 | """ |
|
2321 | """ | |
2323 | fn = self._find_with_lazy_load("line", magic_name) |
|
2322 | fn = self._find_with_lazy_load("line", magic_name) | |
2324 | if fn is None: |
|
2323 | if fn is None: | |
2325 | lazy = self.magics_manager.lazy_magics.get(magic_name) |
|
2324 | lazy = self.magics_manager.lazy_magics.get(magic_name) | |
2326 | if lazy: |
|
2325 | if lazy: | |
2327 | self.run_line_magic("load_ext", lazy) |
|
2326 | self.run_line_magic("load_ext", lazy) | |
2328 | fn = self.find_line_magic(magic_name) |
|
2327 | fn = self.find_line_magic(magic_name) | |
2329 | if fn is None: |
|
2328 | if fn is None: | |
2330 | cm = self.find_cell_magic(magic_name) |
|
2329 | cm = self.find_cell_magic(magic_name) | |
2331 | etpl = "Line magic function `%%%s` not found%s." |
|
2330 | etpl = "Line magic function `%%%s` not found%s." | |
2332 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2331 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |
2333 | 'did you mean that instead?)' % magic_name ) |
|
2332 | 'did you mean that instead?)' % magic_name ) | |
2334 | raise UsageError(etpl % (magic_name, extra)) |
|
2333 | raise UsageError(etpl % (magic_name, extra)) | |
2335 | else: |
|
2334 | else: | |
2336 | # Note: this is the distance in the stack to the user's frame. |
|
2335 | # Note: this is the distance in the stack to the user's frame. | |
2337 | # This will need to be updated if the internal calling logic gets |
|
2336 | # This will need to be updated if the internal calling logic gets | |
2338 | # refactored, or else we'll be expanding the wrong variables. |
|
2337 | # refactored, or else we'll be expanding the wrong variables. | |
2339 |
|
2338 | |||
2340 | # Determine stack_depth depending on where run_line_magic() has been called |
|
2339 | # Determine stack_depth depending on where run_line_magic() has been called | |
2341 | stack_depth = _stack_depth |
|
2340 | stack_depth = _stack_depth | |
2342 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): |
|
2341 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): | |
2343 | # magic has opted out of var_expand |
|
2342 | # magic has opted out of var_expand | |
2344 | magic_arg_s = line |
|
2343 | magic_arg_s = line | |
2345 | else: |
|
2344 | else: | |
2346 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2345 | magic_arg_s = self.var_expand(line, stack_depth) | |
2347 | # Put magic args in a list so we can call with f(*a) syntax |
|
2346 | # Put magic args in a list so we can call with f(*a) syntax | |
2348 | args = [magic_arg_s] |
|
2347 | args = [magic_arg_s] | |
2349 | kwargs = {} |
|
2348 | kwargs = {} | |
2350 | # Grab local namespace if we need it: |
|
2349 | # Grab local namespace if we need it: | |
2351 | if getattr(fn, "needs_local_scope", False): |
|
2350 | if getattr(fn, "needs_local_scope", False): | |
2352 | kwargs['local_ns'] = self.get_local_scope(stack_depth) |
|
2351 | kwargs['local_ns'] = self.get_local_scope(stack_depth) | |
2353 | with self.builtin_trap: |
|
2352 | with self.builtin_trap: | |
2354 | result = fn(*args, **kwargs) |
|
2353 | result = fn(*args, **kwargs) | |
2355 | return result |
|
2354 | return result | |
2356 |
|
2355 | |||
2357 | def get_local_scope(self, stack_depth): |
|
2356 | def get_local_scope(self, stack_depth): | |
2358 | """Get local scope at given stack depth. |
|
2357 | """Get local scope at given stack depth. | |
2359 |
|
2358 | |||
2360 | Parameters |
|
2359 | Parameters | |
2361 | ---------- |
|
2360 | ---------- | |
2362 | stack_depth : int |
|
2361 | stack_depth : int | |
2363 | Depth relative to calling frame |
|
2362 | Depth relative to calling frame | |
2364 | """ |
|
2363 | """ | |
2365 | return sys._getframe(stack_depth + 1).f_locals |
|
2364 | return sys._getframe(stack_depth + 1).f_locals | |
2366 |
|
2365 | |||
2367 | def run_cell_magic(self, magic_name, line, cell): |
|
2366 | def run_cell_magic(self, magic_name, line, cell): | |
2368 | """Execute the given cell magic. |
|
2367 | """Execute the given cell magic. | |
2369 |
|
2368 | |||
2370 | Parameters |
|
2369 | Parameters | |
2371 | ---------- |
|
2370 | ---------- | |
2372 | magic_name : str |
|
2371 | magic_name : str | |
2373 | Name of the desired magic function, without '%' prefix. |
|
2372 | Name of the desired magic function, without '%' prefix. | |
2374 | line : str |
|
2373 | line : str | |
2375 | The rest of the first input line as a single string. |
|
2374 | The rest of the first input line as a single string. | |
2376 | cell : str |
|
2375 | cell : str | |
2377 | The body of the cell as a (possibly multiline) string. |
|
2376 | The body of the cell as a (possibly multiline) string. | |
2378 | """ |
|
2377 | """ | |
2379 | fn = self._find_with_lazy_load("cell", magic_name) |
|
2378 | fn = self._find_with_lazy_load("cell", magic_name) | |
2380 | if fn is None: |
|
2379 | if fn is None: | |
2381 | lm = self.find_line_magic(magic_name) |
|
2380 | lm = self.find_line_magic(magic_name) | |
2382 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2381 | etpl = "Cell magic `%%{0}` not found{1}." | |
2383 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2382 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' | |
2384 | 'did you mean that instead?)'.format(magic_name)) |
|
2383 | 'did you mean that instead?)'.format(magic_name)) | |
2385 | raise UsageError(etpl.format(magic_name, extra)) |
|
2384 | raise UsageError(etpl.format(magic_name, extra)) | |
2386 | elif cell == '': |
|
2385 | elif cell == '': | |
2387 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2386 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) | |
2388 | if self.find_line_magic(magic_name) is not None: |
|
2387 | if self.find_line_magic(magic_name) is not None: | |
2389 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2388 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) | |
2390 | raise UsageError(message) |
|
2389 | raise UsageError(message) | |
2391 | else: |
|
2390 | else: | |
2392 | # Note: this is the distance in the stack to the user's frame. |
|
2391 | # Note: this is the distance in the stack to the user's frame. | |
2393 | # This will need to be updated if the internal calling logic gets |
|
2392 | # This will need to be updated if the internal calling logic gets | |
2394 | # refactored, or else we'll be expanding the wrong variables. |
|
2393 | # refactored, or else we'll be expanding the wrong variables. | |
2395 | stack_depth = 2 |
|
2394 | stack_depth = 2 | |
2396 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): |
|
2395 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): | |
2397 | # magic has opted out of var_expand |
|
2396 | # magic has opted out of var_expand | |
2398 | magic_arg_s = line |
|
2397 | magic_arg_s = line | |
2399 | else: |
|
2398 | else: | |
2400 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2399 | magic_arg_s = self.var_expand(line, stack_depth) | |
2401 | kwargs = {} |
|
2400 | kwargs = {} | |
2402 | if getattr(fn, "needs_local_scope", False): |
|
2401 | if getattr(fn, "needs_local_scope", False): | |
2403 | kwargs['local_ns'] = self.user_ns |
|
2402 | kwargs['local_ns'] = self.user_ns | |
2404 |
|
2403 | |||
2405 | with self.builtin_trap: |
|
2404 | with self.builtin_trap: | |
2406 | args = (magic_arg_s, cell) |
|
2405 | args = (magic_arg_s, cell) | |
2407 | result = fn(*args, **kwargs) |
|
2406 | result = fn(*args, **kwargs) | |
2408 | return result |
|
2407 | return result | |
2409 |
|
2408 | |||
2410 | def find_line_magic(self, magic_name): |
|
2409 | def find_line_magic(self, magic_name): | |
2411 | """Find and return a line magic by name. |
|
2410 | """Find and return a line magic by name. | |
2412 |
|
2411 | |||
2413 | Returns None if the magic isn't found.""" |
|
2412 | Returns None if the magic isn't found.""" | |
2414 | return self.magics_manager.magics['line'].get(magic_name) |
|
2413 | return self.magics_manager.magics['line'].get(magic_name) | |
2415 |
|
2414 | |||
2416 | def find_cell_magic(self, magic_name): |
|
2415 | def find_cell_magic(self, magic_name): | |
2417 | """Find and return a cell magic by name. |
|
2416 | """Find and return a cell magic by name. | |
2418 |
|
2417 | |||
2419 | Returns None if the magic isn't found.""" |
|
2418 | Returns None if the magic isn't found.""" | |
2420 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2419 | return self.magics_manager.magics['cell'].get(magic_name) | |
2421 |
|
2420 | |||
2422 | def find_magic(self, magic_name, magic_kind='line'): |
|
2421 | def find_magic(self, magic_name, magic_kind='line'): | |
2423 | """Find and return a magic of the given type by name. |
|
2422 | """Find and return a magic of the given type by name. | |
2424 |
|
2423 | |||
2425 | Returns None if the magic isn't found.""" |
|
2424 | Returns None if the magic isn't found.""" | |
2426 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2425 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
2427 |
|
2426 | |||
2428 | def magic(self, arg_s): |
|
2427 | def magic(self, arg_s): | |
2429 | """ |
|
2428 | """ | |
2430 | DEPRECATED |
|
2429 | DEPRECATED | |
2431 |
|
2430 | |||
2432 | Deprecated since IPython 0.13 (warning added in |
|
2431 | Deprecated since IPython 0.13 (warning added in | |
2433 | 8.1), use run_line_magic(magic_name, parameter_s). |
|
2432 | 8.1), use run_line_magic(magic_name, parameter_s). | |
2434 |
|
2433 | |||
2435 | Call a magic function by name. |
|
2434 | Call a magic function by name. | |
2436 |
|
2435 | |||
2437 | Input: a string containing the name of the magic function to call and |
|
2436 | Input: a string containing the name of the magic function to call and | |
2438 | any additional arguments to be passed to the magic. |
|
2437 | any additional arguments to be passed to the magic. | |
2439 |
|
2438 | |||
2440 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2439 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
2441 | prompt: |
|
2440 | prompt: | |
2442 |
|
2441 | |||
2443 | In[1]: %name -opt foo bar |
|
2442 | In[1]: %name -opt foo bar | |
2444 |
|
2443 | |||
2445 | To call a magic without arguments, simply use magic('name'). |
|
2444 | To call a magic without arguments, simply use magic('name'). | |
2446 |
|
2445 | |||
2447 | This provides a proper Python function to call IPython's magics in any |
|
2446 | This provides a proper Python function to call IPython's magics in any | |
2448 | valid Python code you can type at the interpreter, including loops and |
|
2447 | valid Python code you can type at the interpreter, including loops and | |
2449 | compound statements. |
|
2448 | compound statements. | |
2450 | """ |
|
2449 | """ | |
2451 | warnings.warn( |
|
2450 | warnings.warn( | |
2452 | "`magic(...)` is deprecated since IPython 0.13 (warning added in " |
|
2451 | "`magic(...)` is deprecated since IPython 0.13 (warning added in " | |
2453 | "8.1), use run_line_magic(magic_name, parameter_s).", |
|
2452 | "8.1), use run_line_magic(magic_name, parameter_s).", | |
2454 | DeprecationWarning, |
|
2453 | DeprecationWarning, | |
2455 | stacklevel=2, |
|
2454 | stacklevel=2, | |
2456 | ) |
|
2455 | ) | |
2457 | # TODO: should we issue a loud deprecation warning here? |
|
2456 | # TODO: should we issue a loud deprecation warning here? | |
2458 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2457 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2459 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2458 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2460 | return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2) |
|
2459 | return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2) | |
2461 |
|
2460 | |||
2462 | #------------------------------------------------------------------------- |
|
2461 | #------------------------------------------------------------------------- | |
2463 | # Things related to macros |
|
2462 | # Things related to macros | |
2464 | #------------------------------------------------------------------------- |
|
2463 | #------------------------------------------------------------------------- | |
2465 |
|
2464 | |||
2466 | def define_macro(self, name, themacro): |
|
2465 | def define_macro(self, name, themacro): | |
2467 | """Define a new macro |
|
2466 | """Define a new macro | |
2468 |
|
2467 | |||
2469 | Parameters |
|
2468 | Parameters | |
2470 | ---------- |
|
2469 | ---------- | |
2471 | name : str |
|
2470 | name : str | |
2472 | The name of the macro. |
|
2471 | The name of the macro. | |
2473 | themacro : str or Macro |
|
2472 | themacro : str or Macro | |
2474 | The action to do upon invoking the macro. If a string, a new |
|
2473 | The action to do upon invoking the macro. If a string, a new | |
2475 | Macro object is created by passing the string to it. |
|
2474 | Macro object is created by passing the string to it. | |
2476 | """ |
|
2475 | """ | |
2477 |
|
2476 | |||
2478 | from IPython.core import macro |
|
2477 | from IPython.core import macro | |
2479 |
|
2478 | |||
2480 | if isinstance(themacro, str): |
|
2479 | if isinstance(themacro, str): | |
2481 | themacro = macro.Macro(themacro) |
|
2480 | themacro = macro.Macro(themacro) | |
2482 | if not isinstance(themacro, macro.Macro): |
|
2481 | if not isinstance(themacro, macro.Macro): | |
2483 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2482 | raise ValueError('A macro must be a string or a Macro instance.') | |
2484 | self.user_ns[name] = themacro |
|
2483 | self.user_ns[name] = themacro | |
2485 |
|
2484 | |||
2486 | #------------------------------------------------------------------------- |
|
2485 | #------------------------------------------------------------------------- | |
2487 | # Things related to the running of system commands |
|
2486 | # Things related to the running of system commands | |
2488 | #------------------------------------------------------------------------- |
|
2487 | #------------------------------------------------------------------------- | |
2489 |
|
2488 | |||
2490 | def system_piped(self, cmd): |
|
2489 | def system_piped(self, cmd): | |
2491 | """Call the given cmd in a subprocess, piping stdout/err |
|
2490 | """Call the given cmd in a subprocess, piping stdout/err | |
2492 |
|
2491 | |||
2493 | Parameters |
|
2492 | Parameters | |
2494 | ---------- |
|
2493 | ---------- | |
2495 | cmd : str |
|
2494 | cmd : str | |
2496 | Command to execute (can not end in '&', as background processes are |
|
2495 | Command to execute (can not end in '&', as background processes are | |
2497 | not supported. Should not be a command that expects input |
|
2496 | not supported. Should not be a command that expects input | |
2498 | other than simple text. |
|
2497 | other than simple text. | |
2499 | """ |
|
2498 | """ | |
2500 | if cmd.rstrip().endswith('&'): |
|
2499 | if cmd.rstrip().endswith('&'): | |
2501 | # this is *far* from a rigorous test |
|
2500 | # this is *far* from a rigorous test | |
2502 | # We do not support backgrounding processes because we either use |
|
2501 | # We do not support backgrounding processes because we either use | |
2503 | # pexpect or pipes to read from. Users can always just call |
|
2502 | # pexpect or pipes to read from. Users can always just call | |
2504 | # os.system() or use ip.system=ip.system_raw |
|
2503 | # os.system() or use ip.system=ip.system_raw | |
2505 | # if they really want a background process. |
|
2504 | # if they really want a background process. | |
2506 | raise OSError("Background processes not supported.") |
|
2505 | raise OSError("Background processes not supported.") | |
2507 |
|
2506 | |||
2508 | # we explicitly do NOT return the subprocess status code, because |
|
2507 | # we explicitly do NOT return the subprocess status code, because | |
2509 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2508 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2510 | # Instead, we store the exit_code in user_ns. |
|
2509 | # Instead, we store the exit_code in user_ns. | |
2511 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2510 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | |
2512 |
|
2511 | |||
2513 | def system_raw(self, cmd): |
|
2512 | def system_raw(self, cmd): | |
2514 | """Call the given cmd in a subprocess using os.system on Windows or |
|
2513 | """Call the given cmd in a subprocess using os.system on Windows or | |
2515 | subprocess.call using the system shell on other platforms. |
|
2514 | subprocess.call using the system shell on other platforms. | |
2516 |
|
2515 | |||
2517 | Parameters |
|
2516 | Parameters | |
2518 | ---------- |
|
2517 | ---------- | |
2519 | cmd : str |
|
2518 | cmd : str | |
2520 | Command to execute. |
|
2519 | Command to execute. | |
2521 | """ |
|
2520 | """ | |
2522 | cmd = self.var_expand(cmd, depth=1) |
|
2521 | cmd = self.var_expand(cmd, depth=1) | |
2523 | # warn if there is an IPython magic alternative. |
|
2522 | # warn if there is an IPython magic alternative. | |
2524 | main_cmd = cmd.split()[0] |
|
2523 | main_cmd = cmd.split()[0] | |
2525 | has_magic_alternatives = ("pip", "conda", "cd") |
|
2524 | has_magic_alternatives = ("pip", "conda", "cd") | |
2526 |
|
2525 | |||
2527 | if main_cmd in has_magic_alternatives: |
|
2526 | if main_cmd in has_magic_alternatives: | |
2528 | warnings.warn( |
|
2527 | warnings.warn( | |
2529 | ( |
|
2528 | ( | |
2530 | "You executed the system command !{0} which may not work " |
|
2529 | "You executed the system command !{0} which may not work " | |
2531 | "as expected. Try the IPython magic %{0} instead." |
|
2530 | "as expected. Try the IPython magic %{0} instead." | |
2532 | ).format(main_cmd) |
|
2531 | ).format(main_cmd) | |
2533 | ) |
|
2532 | ) | |
2534 |
|
2533 | |||
2535 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2534 | # protect os.system from UNC paths on Windows, which it can't handle: | |
2536 | if sys.platform == 'win32': |
|
2535 | if sys.platform == 'win32': | |
2537 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2536 | from IPython.utils._process_win32 import AvoidUNCPath | |
2538 | with AvoidUNCPath() as path: |
|
2537 | with AvoidUNCPath() as path: | |
2539 | if path is not None: |
|
2538 | if path is not None: | |
2540 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2539 | cmd = '"pushd %s &&"%s' % (path, cmd) | |
2541 | try: |
|
2540 | try: | |
2542 | ec = os.system(cmd) |
|
2541 | ec = os.system(cmd) | |
2543 | except KeyboardInterrupt: |
|
2542 | except KeyboardInterrupt: | |
2544 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2543 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2545 | ec = -2 |
|
2544 | ec = -2 | |
2546 | else: |
|
2545 | else: | |
2547 | # For posix the result of the subprocess.call() below is an exit |
|
2546 | # For posix the result of the subprocess.call() below is an exit | |
2548 | # code, which by convention is zero for success, positive for |
|
2547 | # code, which by convention is zero for success, positive for | |
2549 | # program failure. Exit codes above 128 are reserved for signals, |
|
2548 | # program failure. Exit codes above 128 are reserved for signals, | |
2550 | # and the formula for converting a signal to an exit code is usually |
|
2549 | # and the formula for converting a signal to an exit code is usually | |
2551 | # signal_number+128. To more easily differentiate between exit |
|
2550 | # signal_number+128. To more easily differentiate between exit | |
2552 | # codes and signals, ipython uses negative numbers. For instance |
|
2551 | # codes and signals, ipython uses negative numbers. For instance | |
2553 | # since control-c is signal 2 but exit code 130, ipython's |
|
2552 | # since control-c is signal 2 but exit code 130, ipython's | |
2554 | # _exit_code variable will read -2. Note that some shells like |
|
2553 | # _exit_code variable will read -2. Note that some shells like | |
2555 | # csh and fish don't follow sh/bash conventions for exit codes. |
|
2554 | # csh and fish don't follow sh/bash conventions for exit codes. | |
2556 | executable = os.environ.get('SHELL', None) |
|
2555 | executable = os.environ.get('SHELL', None) | |
2557 | try: |
|
2556 | try: | |
2558 | # Use env shell instead of default /bin/sh |
|
2557 | # Use env shell instead of default /bin/sh | |
2559 | ec = subprocess.call(cmd, shell=True, executable=executable) |
|
2558 | ec = subprocess.call(cmd, shell=True, executable=executable) | |
2560 | except KeyboardInterrupt: |
|
2559 | except KeyboardInterrupt: | |
2561 | # intercept control-C; a long traceback is not useful here |
|
2560 | # intercept control-C; a long traceback is not useful here | |
2562 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2561 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2563 | ec = 130 |
|
2562 | ec = 130 | |
2564 | if ec > 128: |
|
2563 | if ec > 128: | |
2565 | ec = -(ec - 128) |
|
2564 | ec = -(ec - 128) | |
2566 |
|
2565 | |||
2567 | # We explicitly do NOT return the subprocess status code, because |
|
2566 | # We explicitly do NOT return the subprocess status code, because | |
2568 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2567 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2569 | # Instead, we store the exit_code in user_ns. Note the semantics |
|
2568 | # Instead, we store the exit_code in user_ns. Note the semantics | |
2570 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
|
2569 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, | |
2571 | # but raising SystemExit(_exit_code) will give status 254! |
|
2570 | # but raising SystemExit(_exit_code) will give status 254! | |
2572 | self.user_ns['_exit_code'] = ec |
|
2571 | self.user_ns['_exit_code'] = ec | |
2573 |
|
2572 | |||
2574 | # use piped system by default, because it is better behaved |
|
2573 | # use piped system by default, because it is better behaved | |
2575 | system = system_piped |
|
2574 | system = system_piped | |
2576 |
|
2575 | |||
2577 | def getoutput(self, cmd, split=True, depth=0): |
|
2576 | def getoutput(self, cmd, split=True, depth=0): | |
2578 | """Get output (possibly including stderr) from a subprocess. |
|
2577 | """Get output (possibly including stderr) from a subprocess. | |
2579 |
|
2578 | |||
2580 | Parameters |
|
2579 | Parameters | |
2581 | ---------- |
|
2580 | ---------- | |
2582 | cmd : str |
|
2581 | cmd : str | |
2583 | Command to execute (can not end in '&', as background processes are |
|
2582 | Command to execute (can not end in '&', as background processes are | |
2584 | not supported. |
|
2583 | not supported. | |
2585 | split : bool, optional |
|
2584 | split : bool, optional | |
2586 | If True, split the output into an IPython SList. Otherwise, an |
|
2585 | If True, split the output into an IPython SList. Otherwise, an | |
2587 | IPython LSString is returned. These are objects similar to normal |
|
2586 | IPython LSString is returned. These are objects similar to normal | |
2588 | lists and strings, with a few convenience attributes for easier |
|
2587 | lists and strings, with a few convenience attributes for easier | |
2589 | manipulation of line-based output. You can use '?' on them for |
|
2588 | manipulation of line-based output. You can use '?' on them for | |
2590 | details. |
|
2589 | details. | |
2591 | depth : int, optional |
|
2590 | depth : int, optional | |
2592 | How many frames above the caller are the local variables which should |
|
2591 | How many frames above the caller are the local variables which should | |
2593 | be expanded in the command string? The default (0) assumes that the |
|
2592 | be expanded in the command string? The default (0) assumes that the | |
2594 | expansion variables are in the stack frame calling this function. |
|
2593 | expansion variables are in the stack frame calling this function. | |
2595 | """ |
|
2594 | """ | |
2596 | if cmd.rstrip().endswith('&'): |
|
2595 | if cmd.rstrip().endswith('&'): | |
2597 | # this is *far* from a rigorous test |
|
2596 | # this is *far* from a rigorous test | |
2598 | raise OSError("Background processes not supported.") |
|
2597 | raise OSError("Background processes not supported.") | |
2599 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2598 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | |
2600 | if split: |
|
2599 | if split: | |
2601 | out = SList(out.splitlines()) |
|
2600 | out = SList(out.splitlines()) | |
2602 | else: |
|
2601 | else: | |
2603 | out = LSString(out) |
|
2602 | out = LSString(out) | |
2604 | return out |
|
2603 | return out | |
2605 |
|
2604 | |||
2606 | #------------------------------------------------------------------------- |
|
2605 | #------------------------------------------------------------------------- | |
2607 | # Things related to aliases |
|
2606 | # Things related to aliases | |
2608 | #------------------------------------------------------------------------- |
|
2607 | #------------------------------------------------------------------------- | |
2609 |
|
2608 | |||
2610 | def init_alias(self): |
|
2609 | def init_alias(self): | |
2611 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2610 | self.alias_manager = AliasManager(shell=self, parent=self) | |
2612 | self.configurables.append(self.alias_manager) |
|
2611 | self.configurables.append(self.alias_manager) | |
2613 |
|
2612 | |||
2614 | #------------------------------------------------------------------------- |
|
2613 | #------------------------------------------------------------------------- | |
2615 | # Things related to extensions |
|
2614 | # Things related to extensions | |
2616 | #------------------------------------------------------------------------- |
|
2615 | #------------------------------------------------------------------------- | |
2617 |
|
2616 | |||
2618 | def init_extension_manager(self): |
|
2617 | def init_extension_manager(self): | |
2619 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2618 | self.extension_manager = ExtensionManager(shell=self, parent=self) | |
2620 | self.configurables.append(self.extension_manager) |
|
2619 | self.configurables.append(self.extension_manager) | |
2621 |
|
2620 | |||
2622 | #------------------------------------------------------------------------- |
|
2621 | #------------------------------------------------------------------------- | |
2623 | # Things related to payloads |
|
2622 | # Things related to payloads | |
2624 | #------------------------------------------------------------------------- |
|
2623 | #------------------------------------------------------------------------- | |
2625 |
|
2624 | |||
2626 | def init_payload(self): |
|
2625 | def init_payload(self): | |
2627 | self.payload_manager = PayloadManager(parent=self) |
|
2626 | self.payload_manager = PayloadManager(parent=self) | |
2628 | self.configurables.append(self.payload_manager) |
|
2627 | self.configurables.append(self.payload_manager) | |
2629 |
|
2628 | |||
2630 | #------------------------------------------------------------------------- |
|
2629 | #------------------------------------------------------------------------- | |
2631 | # Things related to the prefilter |
|
2630 | # Things related to the prefilter | |
2632 | #------------------------------------------------------------------------- |
|
2631 | #------------------------------------------------------------------------- | |
2633 |
|
2632 | |||
2634 | def init_prefilter(self): |
|
2633 | def init_prefilter(self): | |
2635 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2634 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) | |
2636 | self.configurables.append(self.prefilter_manager) |
|
2635 | self.configurables.append(self.prefilter_manager) | |
2637 | # Ultimately this will be refactored in the new interpreter code, but |
|
2636 | # Ultimately this will be refactored in the new interpreter code, but | |
2638 | # for now, we should expose the main prefilter method (there's legacy |
|
2637 | # for now, we should expose the main prefilter method (there's legacy | |
2639 | # code out there that may rely on this). |
|
2638 | # code out there that may rely on this). | |
2640 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2639 | self.prefilter = self.prefilter_manager.prefilter_lines | |
2641 |
|
2640 | |||
2642 | def auto_rewrite_input(self, cmd): |
|
2641 | def auto_rewrite_input(self, cmd): | |
2643 | """Print to the screen the rewritten form of the user's command. |
|
2642 | """Print to the screen the rewritten form of the user's command. | |
2644 |
|
2643 | |||
2645 | This shows visual feedback by rewriting input lines that cause |
|
2644 | This shows visual feedback by rewriting input lines that cause | |
2646 | automatic calling to kick in, like:: |
|
2645 | automatic calling to kick in, like:: | |
2647 |
|
2646 | |||
2648 | /f x |
|
2647 | /f x | |
2649 |
|
2648 | |||
2650 | into:: |
|
2649 | into:: | |
2651 |
|
2650 | |||
2652 | ------> f(x) |
|
2651 | ------> f(x) | |
2653 |
|
2652 | |||
2654 | after the user's input prompt. This helps the user understand that the |
|
2653 | after the user's input prompt. This helps the user understand that the | |
2655 | input line was transformed automatically by IPython. |
|
2654 | input line was transformed automatically by IPython. | |
2656 | """ |
|
2655 | """ | |
2657 | if not self.show_rewritten_input: |
|
2656 | if not self.show_rewritten_input: | |
2658 | return |
|
2657 | return | |
2659 |
|
2658 | |||
2660 | # This is overridden in TerminalInteractiveShell to use fancy prompts |
|
2659 | # This is overridden in TerminalInteractiveShell to use fancy prompts | |
2661 | print("------> " + cmd) |
|
2660 | print("------> " + cmd) | |
2662 |
|
2661 | |||
2663 | #------------------------------------------------------------------------- |
|
2662 | #------------------------------------------------------------------------- | |
2664 | # Things related to extracting values/expressions from kernel and user_ns |
|
2663 | # Things related to extracting values/expressions from kernel and user_ns | |
2665 | #------------------------------------------------------------------------- |
|
2664 | #------------------------------------------------------------------------- | |
2666 |
|
2665 | |||
2667 | def _user_obj_error(self): |
|
2666 | def _user_obj_error(self): | |
2668 | """return simple exception dict |
|
2667 | """return simple exception dict | |
2669 |
|
2668 | |||
2670 | for use in user_expressions |
|
2669 | for use in user_expressions | |
2671 | """ |
|
2670 | """ | |
2672 |
|
2671 | |||
2673 | etype, evalue, tb = self._get_exc_info() |
|
2672 | etype, evalue, tb = self._get_exc_info() | |
2674 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2673 | stb = self.InteractiveTB.get_exception_only(etype, evalue) | |
2675 |
|
2674 | |||
2676 | exc_info = { |
|
2675 | exc_info = { | |
2677 | "status": "error", |
|
2676 | "status": "error", | |
2678 | "traceback": stb, |
|
2677 | "traceback": stb, | |
2679 | "ename": etype.__name__, |
|
2678 | "ename": etype.__name__, | |
2680 | "evalue": py3compat.safe_unicode(evalue), |
|
2679 | "evalue": py3compat.safe_unicode(evalue), | |
2681 | } |
|
2680 | } | |
2682 |
|
2681 | |||
2683 | return exc_info |
|
2682 | return exc_info | |
2684 |
|
2683 | |||
2685 | def _format_user_obj(self, obj): |
|
2684 | def _format_user_obj(self, obj): | |
2686 | """format a user object to display dict |
|
2685 | """format a user object to display dict | |
2687 |
|
2686 | |||
2688 | for use in user_expressions |
|
2687 | for use in user_expressions | |
2689 | """ |
|
2688 | """ | |
2690 |
|
2689 | |||
2691 | data, md = self.display_formatter.format(obj) |
|
2690 | data, md = self.display_formatter.format(obj) | |
2692 | value = { |
|
2691 | value = { | |
2693 | 'status' : 'ok', |
|
2692 | 'status' : 'ok', | |
2694 | 'data' : data, |
|
2693 | 'data' : data, | |
2695 | 'metadata' : md, |
|
2694 | 'metadata' : md, | |
2696 | } |
|
2695 | } | |
2697 | return value |
|
2696 | return value | |
2698 |
|
2697 | |||
2699 | def user_expressions(self, expressions): |
|
2698 | def user_expressions(self, expressions): | |
2700 | """Evaluate a dict of expressions in the user's namespace. |
|
2699 | """Evaluate a dict of expressions in the user's namespace. | |
2701 |
|
2700 | |||
2702 | Parameters |
|
2701 | Parameters | |
2703 | ---------- |
|
2702 | ---------- | |
2704 | expressions : dict |
|
2703 | expressions : dict | |
2705 | A dict with string keys and string values. The expression values |
|
2704 | A dict with string keys and string values. The expression values | |
2706 | should be valid Python expressions, each of which will be evaluated |
|
2705 | should be valid Python expressions, each of which will be evaluated | |
2707 | in the user namespace. |
|
2706 | in the user namespace. | |
2708 |
|
2707 | |||
2709 | Returns |
|
2708 | Returns | |
2710 | ------- |
|
2709 | ------- | |
2711 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2710 | A dict, keyed like the input expressions dict, with the rich mime-typed | |
2712 | display_data of each value. |
|
2711 | display_data of each value. | |
2713 | """ |
|
2712 | """ | |
2714 | out = {} |
|
2713 | out = {} | |
2715 | user_ns = self.user_ns |
|
2714 | user_ns = self.user_ns | |
2716 | global_ns = self.user_global_ns |
|
2715 | global_ns = self.user_global_ns | |
2717 |
|
2716 | |||
2718 | for key, expr in expressions.items(): |
|
2717 | for key, expr in expressions.items(): | |
2719 | try: |
|
2718 | try: | |
2720 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2719 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) | |
2721 | except: |
|
2720 | except: | |
2722 | value = self._user_obj_error() |
|
2721 | value = self._user_obj_error() | |
2723 | out[key] = value |
|
2722 | out[key] = value | |
2724 | return out |
|
2723 | return out | |
2725 |
|
2724 | |||
2726 | #------------------------------------------------------------------------- |
|
2725 | #------------------------------------------------------------------------- | |
2727 | # Things related to the running of code |
|
2726 | # Things related to the running of code | |
2728 | #------------------------------------------------------------------------- |
|
2727 | #------------------------------------------------------------------------- | |
2729 |
|
2728 | |||
2730 | def ex(self, cmd): |
|
2729 | def ex(self, cmd): | |
2731 | """Execute a normal python statement in user namespace.""" |
|
2730 | """Execute a normal python statement in user namespace.""" | |
2732 | with self.builtin_trap: |
|
2731 | with self.builtin_trap: | |
2733 | exec(cmd, self.user_global_ns, self.user_ns) |
|
2732 | exec(cmd, self.user_global_ns, self.user_ns) | |
2734 |
|
2733 | |||
2735 | def ev(self, expr): |
|
2734 | def ev(self, expr): | |
2736 | """Evaluate python expression expr in user namespace. |
|
2735 | """Evaluate python expression expr in user namespace. | |
2737 |
|
2736 | |||
2738 | Returns the result of evaluation |
|
2737 | Returns the result of evaluation | |
2739 | """ |
|
2738 | """ | |
2740 | with self.builtin_trap: |
|
2739 | with self.builtin_trap: | |
2741 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2740 | return eval(expr, self.user_global_ns, self.user_ns) | |
2742 |
|
2741 | |||
2743 | def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False): |
|
2742 | def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False): | |
2744 | """A safe version of the builtin execfile(). |
|
2743 | """A safe version of the builtin execfile(). | |
2745 |
|
2744 | |||
2746 | This version will never throw an exception, but instead print |
|
2745 | This version will never throw an exception, but instead print | |
2747 | helpful error messages to the screen. This only works on pure |
|
2746 | helpful error messages to the screen. This only works on pure | |
2748 | Python files with the .py extension. |
|
2747 | Python files with the .py extension. | |
2749 |
|
2748 | |||
2750 | Parameters |
|
2749 | Parameters | |
2751 | ---------- |
|
2750 | ---------- | |
2752 | fname : string |
|
2751 | fname : string | |
2753 | The name of the file to be executed. |
|
2752 | The name of the file to be executed. | |
2754 | *where : tuple |
|
2753 | *where : tuple | |
2755 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2754 | One or two namespaces, passed to execfile() as (globals,locals). | |
2756 | If only one is given, it is passed as both. |
|
2755 | If only one is given, it is passed as both. | |
2757 | exit_ignore : bool (False) |
|
2756 | exit_ignore : bool (False) | |
2758 | If True, then silence SystemExit for non-zero status (it is always |
|
2757 | If True, then silence SystemExit for non-zero status (it is always | |
2759 | silenced for zero status, as it is so common). |
|
2758 | silenced for zero status, as it is so common). | |
2760 | raise_exceptions : bool (False) |
|
2759 | raise_exceptions : bool (False) | |
2761 | If True raise exceptions everywhere. Meant for testing. |
|
2760 | If True raise exceptions everywhere. Meant for testing. | |
2762 | shell_futures : bool (False) |
|
2761 | shell_futures : bool (False) | |
2763 | If True, the code will share future statements with the interactive |
|
2762 | If True, the code will share future statements with the interactive | |
2764 | shell. It will both be affected by previous __future__ imports, and |
|
2763 | shell. It will both be affected by previous __future__ imports, and | |
2765 | any __future__ imports in the code will affect the shell. If False, |
|
2764 | any __future__ imports in the code will affect the shell. If False, | |
2766 | __future__ imports are not shared in either direction. |
|
2765 | __future__ imports are not shared in either direction. | |
2767 |
|
2766 | |||
2768 | """ |
|
2767 | """ | |
2769 | fname = Path(fname).expanduser().resolve() |
|
2768 | fname = Path(fname).expanduser().resolve() | |
2770 |
|
2769 | |||
2771 | # Make sure we can open the file |
|
2770 | # Make sure we can open the file | |
2772 | try: |
|
2771 | try: | |
2773 | with fname.open("rb"): |
|
2772 | with fname.open("rb"): | |
2774 | pass |
|
2773 | pass | |
2775 | except: |
|
2774 | except: | |
2776 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2775 | warn('Could not open file <%s> for safe execution.' % fname) | |
2777 | return |
|
2776 | return | |
2778 |
|
2777 | |||
2779 | # Find things also in current directory. This is needed to mimic the |
|
2778 | # Find things also in current directory. This is needed to mimic the | |
2780 | # behavior of running a script from the system command line, where |
|
2779 | # behavior of running a script from the system command line, where | |
2781 | # Python inserts the script's directory into sys.path |
|
2780 | # Python inserts the script's directory into sys.path | |
2782 | dname = str(fname.parent) |
|
2781 | dname = str(fname.parent) | |
2783 |
|
2782 | |||
2784 | with prepended_to_syspath(dname), self.builtin_trap: |
|
2783 | with prepended_to_syspath(dname), self.builtin_trap: | |
2785 | try: |
|
2784 | try: | |
2786 | glob, loc = (where + (None, ))[:2] |
|
2785 | glob, loc = (where + (None, ))[:2] | |
2787 | py3compat.execfile( |
|
2786 | py3compat.execfile( | |
2788 | fname, glob, loc, |
|
2787 | fname, glob, loc, | |
2789 | self.compile if shell_futures else None) |
|
2788 | self.compile if shell_futures else None) | |
2790 | except SystemExit as status: |
|
2789 | except SystemExit as status: | |
2791 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2790 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2792 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2791 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2793 | # these are considered normal by the OS: |
|
2792 | # these are considered normal by the OS: | |
2794 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2793 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2795 | # 0 |
|
2794 | # 0 | |
2796 | # > python -c'import sys;sys.exit()'; echo $? |
|
2795 | # > python -c'import sys;sys.exit()'; echo $? | |
2797 | # 0 |
|
2796 | # 0 | |
2798 | # For other exit status, we show the exception unless |
|
2797 | # For other exit status, we show the exception unless | |
2799 | # explicitly silenced, but only in short form. |
|
2798 | # explicitly silenced, but only in short form. | |
2800 | if status.code: |
|
2799 | if status.code: | |
2801 | if raise_exceptions: |
|
2800 | if raise_exceptions: | |
2802 | raise |
|
2801 | raise | |
2803 | if not exit_ignore: |
|
2802 | if not exit_ignore: | |
2804 | self.showtraceback(exception_only=True) |
|
2803 | self.showtraceback(exception_only=True) | |
2805 | except: |
|
2804 | except: | |
2806 | if raise_exceptions: |
|
2805 | if raise_exceptions: | |
2807 | raise |
|
2806 | raise | |
2808 | # tb offset is 2 because we wrap execfile |
|
2807 | # tb offset is 2 because we wrap execfile | |
2809 | self.showtraceback(tb_offset=2) |
|
2808 | self.showtraceback(tb_offset=2) | |
2810 |
|
2809 | |||
2811 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): |
|
2810 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): | |
2812 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. |
|
2811 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. | |
2813 |
|
2812 | |||
2814 | Parameters |
|
2813 | Parameters | |
2815 | ---------- |
|
2814 | ---------- | |
2816 | fname : str |
|
2815 | fname : str | |
2817 | The name of the file to execute. The filename must have a |
|
2816 | The name of the file to execute. The filename must have a | |
2818 | .ipy or .ipynb extension. |
|
2817 | .ipy or .ipynb extension. | |
2819 | shell_futures : bool (False) |
|
2818 | shell_futures : bool (False) | |
2820 | If True, the code will share future statements with the interactive |
|
2819 | If True, the code will share future statements with the interactive | |
2821 | shell. It will both be affected by previous __future__ imports, and |
|
2820 | shell. It will both be affected by previous __future__ imports, and | |
2822 | any __future__ imports in the code will affect the shell. If False, |
|
2821 | any __future__ imports in the code will affect the shell. If False, | |
2823 | __future__ imports are not shared in either direction. |
|
2822 | __future__ imports are not shared in either direction. | |
2824 | raise_exceptions : bool (False) |
|
2823 | raise_exceptions : bool (False) | |
2825 | If True raise exceptions everywhere. Meant for testing. |
|
2824 | If True raise exceptions everywhere. Meant for testing. | |
2826 | """ |
|
2825 | """ | |
2827 | fname = Path(fname).expanduser().resolve() |
|
2826 | fname = Path(fname).expanduser().resolve() | |
2828 |
|
2827 | |||
2829 | # Make sure we can open the file |
|
2828 | # Make sure we can open the file | |
2830 | try: |
|
2829 | try: | |
2831 | with fname.open("rb"): |
|
2830 | with fname.open("rb"): | |
2832 | pass |
|
2831 | pass | |
2833 | except: |
|
2832 | except: | |
2834 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2833 | warn('Could not open file <%s> for safe execution.' % fname) | |
2835 | return |
|
2834 | return | |
2836 |
|
2835 | |||
2837 | # Find things also in current directory. This is needed to mimic the |
|
2836 | # Find things also in current directory. This is needed to mimic the | |
2838 | # behavior of running a script from the system command line, where |
|
2837 | # behavior of running a script from the system command line, where | |
2839 | # Python inserts the script's directory into sys.path |
|
2838 | # Python inserts the script's directory into sys.path | |
2840 | dname = str(fname.parent) |
|
2839 | dname = str(fname.parent) | |
2841 |
|
2840 | |||
2842 | def get_cells(): |
|
2841 | def get_cells(): | |
2843 | """generator for sequence of code blocks to run""" |
|
2842 | """generator for sequence of code blocks to run""" | |
2844 | if fname.suffix == ".ipynb": |
|
2843 | if fname.suffix == ".ipynb": | |
2845 | from nbformat import read |
|
2844 | from nbformat import read | |
2846 | nb = read(fname, as_version=4) |
|
2845 | nb = read(fname, as_version=4) | |
2847 | if not nb.cells: |
|
2846 | if not nb.cells: | |
2848 | return |
|
2847 | return | |
2849 | for cell in nb.cells: |
|
2848 | for cell in nb.cells: | |
2850 | if cell.cell_type == 'code': |
|
2849 | if cell.cell_type == 'code': | |
2851 | yield cell.source |
|
2850 | yield cell.source | |
2852 | else: |
|
2851 | else: | |
2853 | yield fname.read_text(encoding="utf-8") |
|
2852 | yield fname.read_text(encoding="utf-8") | |
2854 |
|
2853 | |||
2855 | with prepended_to_syspath(dname): |
|
2854 | with prepended_to_syspath(dname): | |
2856 | try: |
|
2855 | try: | |
2857 | for cell in get_cells(): |
|
2856 | for cell in get_cells(): | |
2858 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) |
|
2857 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) | |
2859 | if raise_exceptions: |
|
2858 | if raise_exceptions: | |
2860 | result.raise_error() |
|
2859 | result.raise_error() | |
2861 | elif not result.success: |
|
2860 | elif not result.success: | |
2862 | break |
|
2861 | break | |
2863 | except: |
|
2862 | except: | |
2864 | if raise_exceptions: |
|
2863 | if raise_exceptions: | |
2865 | raise |
|
2864 | raise | |
2866 | self.showtraceback() |
|
2865 | self.showtraceback() | |
2867 | warn('Unknown failure executing file: <%s>' % fname) |
|
2866 | warn('Unknown failure executing file: <%s>' % fname) | |
2868 |
|
2867 | |||
2869 | def safe_run_module(self, mod_name, where): |
|
2868 | def safe_run_module(self, mod_name, where): | |
2870 | """A safe version of runpy.run_module(). |
|
2869 | """A safe version of runpy.run_module(). | |
2871 |
|
2870 | |||
2872 | This version will never throw an exception, but instead print |
|
2871 | This version will never throw an exception, but instead print | |
2873 | helpful error messages to the screen. |
|
2872 | helpful error messages to the screen. | |
2874 |
|
2873 | |||
2875 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2874 | `SystemExit` exceptions with status code 0 or None are ignored. | |
2876 |
|
2875 | |||
2877 | Parameters |
|
2876 | Parameters | |
2878 | ---------- |
|
2877 | ---------- | |
2879 | mod_name : string |
|
2878 | mod_name : string | |
2880 | The name of the module to be executed. |
|
2879 | The name of the module to be executed. | |
2881 | where : dict |
|
2880 | where : dict | |
2882 | The globals namespace. |
|
2881 | The globals namespace. | |
2883 | """ |
|
2882 | """ | |
2884 | try: |
|
2883 | try: | |
2885 | try: |
|
2884 | try: | |
2886 | where.update( |
|
2885 | where.update( | |
2887 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2886 | runpy.run_module(str(mod_name), run_name="__main__", | |
2888 | alter_sys=True) |
|
2887 | alter_sys=True) | |
2889 | ) |
|
2888 | ) | |
2890 | except SystemExit as status: |
|
2889 | except SystemExit as status: | |
2891 | if status.code: |
|
2890 | if status.code: | |
2892 | raise |
|
2891 | raise | |
2893 | except: |
|
2892 | except: | |
2894 | self.showtraceback() |
|
2893 | self.showtraceback() | |
2895 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2894 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2896 |
|
2895 | |||
2897 | def run_cell( |
|
2896 | def run_cell( | |
2898 | self, |
|
2897 | self, | |
2899 | raw_cell, |
|
2898 | raw_cell, | |
2900 | store_history=False, |
|
2899 | store_history=False, | |
2901 | silent=False, |
|
2900 | silent=False, | |
2902 | shell_futures=True, |
|
2901 | shell_futures=True, | |
2903 | cell_id=None, |
|
2902 | cell_id=None, | |
2904 | ): |
|
2903 | ): | |
2905 | """Run a complete IPython cell. |
|
2904 | """Run a complete IPython cell. | |
2906 |
|
2905 | |||
2907 | Parameters |
|
2906 | Parameters | |
2908 | ---------- |
|
2907 | ---------- | |
2909 | raw_cell : str |
|
2908 | raw_cell : str | |
2910 | The code (including IPython code such as %magic functions) to run. |
|
2909 | The code (including IPython code such as %magic functions) to run. | |
2911 | store_history : bool |
|
2910 | store_history : bool | |
2912 | If True, the raw and translated cell will be stored in IPython's |
|
2911 | If True, the raw and translated cell will be stored in IPython's | |
2913 | history. For user code calling back into IPython's machinery, this |
|
2912 | history. For user code calling back into IPython's machinery, this | |
2914 | should be set to False. |
|
2913 | should be set to False. | |
2915 | silent : bool |
|
2914 | silent : bool | |
2916 | If True, avoid side-effects, such as implicit displayhooks and |
|
2915 | If True, avoid side-effects, such as implicit displayhooks and | |
2917 | and logging. silent=True forces store_history=False. |
|
2916 | and logging. silent=True forces store_history=False. | |
2918 | shell_futures : bool |
|
2917 | shell_futures : bool | |
2919 | If True, the code will share future statements with the interactive |
|
2918 | If True, the code will share future statements with the interactive | |
2920 | shell. It will both be affected by previous __future__ imports, and |
|
2919 | shell. It will both be affected by previous __future__ imports, and | |
2921 | any __future__ imports in the code will affect the shell. If False, |
|
2920 | any __future__ imports in the code will affect the shell. If False, | |
2922 | __future__ imports are not shared in either direction. |
|
2921 | __future__ imports are not shared in either direction. | |
2923 |
|
2922 | |||
2924 | Returns |
|
2923 | Returns | |
2925 | ------- |
|
2924 | ------- | |
2926 | result : :class:`ExecutionResult` |
|
2925 | result : :class:`ExecutionResult` | |
2927 | """ |
|
2926 | """ | |
2928 | result = None |
|
2927 | result = None | |
2929 | try: |
|
2928 | try: | |
2930 | result = self._run_cell( |
|
2929 | result = self._run_cell( | |
2931 | raw_cell, store_history, silent, shell_futures, cell_id |
|
2930 | raw_cell, store_history, silent, shell_futures, cell_id | |
2932 | ) |
|
2931 | ) | |
2933 | finally: |
|
2932 | finally: | |
2934 | self.events.trigger('post_execute') |
|
2933 | self.events.trigger('post_execute') | |
2935 | if not silent: |
|
2934 | if not silent: | |
2936 | self.events.trigger('post_run_cell', result) |
|
2935 | self.events.trigger('post_run_cell', result) | |
2937 | return result |
|
2936 | return result | |
2938 |
|
2937 | |||
2939 | def _run_cell( |
|
2938 | def _run_cell( | |
2940 | self, |
|
2939 | self, | |
2941 | raw_cell: str, |
|
2940 | raw_cell: str, | |
2942 | store_history: bool, |
|
2941 | store_history: bool, | |
2943 | silent: bool, |
|
2942 | silent: bool, | |
2944 | shell_futures: bool, |
|
2943 | shell_futures: bool, | |
2945 | cell_id: str, |
|
2944 | cell_id: str, | |
2946 | ) -> ExecutionResult: |
|
2945 | ) -> ExecutionResult: | |
2947 | """Internal method to run a complete IPython cell.""" |
|
2946 | """Internal method to run a complete IPython cell.""" | |
2948 |
|
2947 | |||
2949 | # we need to avoid calling self.transform_cell multiple time on the same thing |
|
2948 | # we need to avoid calling self.transform_cell multiple time on the same thing | |
2950 | # so we need to store some results: |
|
2949 | # so we need to store some results: | |
2951 | preprocessing_exc_tuple = None |
|
2950 | preprocessing_exc_tuple = None | |
2952 | try: |
|
2951 | try: | |
2953 | transformed_cell = self.transform_cell(raw_cell) |
|
2952 | transformed_cell = self.transform_cell(raw_cell) | |
2954 | except Exception: |
|
2953 | except Exception: | |
2955 | transformed_cell = raw_cell |
|
2954 | transformed_cell = raw_cell | |
2956 | preprocessing_exc_tuple = sys.exc_info() |
|
2955 | preprocessing_exc_tuple = sys.exc_info() | |
2957 |
|
2956 | |||
2958 | assert transformed_cell is not None |
|
2957 | assert transformed_cell is not None | |
2959 | coro = self.run_cell_async( |
|
2958 | coro = self.run_cell_async( | |
2960 | raw_cell, |
|
2959 | raw_cell, | |
2961 | store_history=store_history, |
|
2960 | store_history=store_history, | |
2962 | silent=silent, |
|
2961 | silent=silent, | |
2963 | shell_futures=shell_futures, |
|
2962 | shell_futures=shell_futures, | |
2964 | transformed_cell=transformed_cell, |
|
2963 | transformed_cell=transformed_cell, | |
2965 | preprocessing_exc_tuple=preprocessing_exc_tuple, |
|
2964 | preprocessing_exc_tuple=preprocessing_exc_tuple, | |
2966 | cell_id=cell_id, |
|
2965 | cell_id=cell_id, | |
2967 | ) |
|
2966 | ) | |
2968 |
|
2967 | |||
2969 | # run_cell_async is async, but may not actually need an eventloop. |
|
2968 | # run_cell_async is async, but may not actually need an eventloop. | |
2970 | # when this is the case, we want to run it using the pseudo_sync_runner |
|
2969 | # when this is the case, we want to run it using the pseudo_sync_runner | |
2971 | # so that code can invoke eventloops (for example via the %run , and |
|
2970 | # so that code can invoke eventloops (for example via the %run , and | |
2972 | # `%paste` magic. |
|
2971 | # `%paste` magic. | |
2973 | if self.trio_runner: |
|
2972 | if self.trio_runner: | |
2974 | runner = self.trio_runner |
|
2973 | runner = self.trio_runner | |
2975 | elif self.should_run_async( |
|
2974 | elif self.should_run_async( | |
2976 | raw_cell, |
|
2975 | raw_cell, | |
2977 | transformed_cell=transformed_cell, |
|
2976 | transformed_cell=transformed_cell, | |
2978 | preprocessing_exc_tuple=preprocessing_exc_tuple, |
|
2977 | preprocessing_exc_tuple=preprocessing_exc_tuple, | |
2979 | ): |
|
2978 | ): | |
2980 | runner = self.loop_runner |
|
2979 | runner = self.loop_runner | |
2981 | else: |
|
2980 | else: | |
2982 | runner = _pseudo_sync_runner |
|
2981 | runner = _pseudo_sync_runner | |
2983 |
|
2982 | |||
2984 | try: |
|
2983 | try: | |
2985 | return runner(coro) |
|
2984 | return runner(coro) | |
2986 | except BaseException as e: |
|
2985 | except BaseException as e: | |
2987 | info = ExecutionInfo( |
|
2986 | info = ExecutionInfo( | |
2988 | raw_cell, store_history, silent, shell_futures, cell_id |
|
2987 | raw_cell, store_history, silent, shell_futures, cell_id | |
2989 | ) |
|
2988 | ) | |
2990 | result = ExecutionResult(info) |
|
2989 | result = ExecutionResult(info) | |
2991 | result.error_in_exec = e |
|
2990 | result.error_in_exec = e | |
2992 | self.showtraceback(running_compiled_code=True) |
|
2991 | self.showtraceback(running_compiled_code=True) | |
2993 | return result |
|
2992 | return result | |
2994 |
|
2993 | |||
2995 | def should_run_async( |
|
2994 | def should_run_async( | |
2996 | self, raw_cell: str, *, transformed_cell=None, preprocessing_exc_tuple=None |
|
2995 | self, raw_cell: str, *, transformed_cell=None, preprocessing_exc_tuple=None | |
2997 | ) -> bool: |
|
2996 | ) -> bool: | |
2998 | """Return whether a cell should be run asynchronously via a coroutine runner |
|
2997 | """Return whether a cell should be run asynchronously via a coroutine runner | |
2999 |
|
2998 | |||
3000 | Parameters |
|
2999 | Parameters | |
3001 | ---------- |
|
3000 | ---------- | |
3002 | raw_cell : str |
|
3001 | raw_cell : str | |
3003 | The code to be executed |
|
3002 | The code to be executed | |
3004 |
|
3003 | |||
3005 | Returns |
|
3004 | Returns | |
3006 | ------- |
|
3005 | ------- | |
3007 | result: bool |
|
3006 | result: bool | |
3008 | Whether the code needs to be run with a coroutine runner or not |
|
3007 | Whether the code needs to be run with a coroutine runner or not | |
3009 | .. versionadded:: 7.0 |
|
3008 | .. versionadded:: 7.0 | |
3010 | """ |
|
3009 | """ | |
3011 | if not self.autoawait: |
|
3010 | if not self.autoawait: | |
3012 | return False |
|
3011 | return False | |
3013 | if preprocessing_exc_tuple is not None: |
|
3012 | if preprocessing_exc_tuple is not None: | |
3014 | return False |
|
3013 | return False | |
3015 | assert preprocessing_exc_tuple is None |
|
3014 | assert preprocessing_exc_tuple is None | |
3016 | if transformed_cell is None: |
|
3015 | if transformed_cell is None: | |
3017 | warnings.warn( |
|
3016 | warnings.warn( | |
3018 | "`should_run_async` will not call `transform_cell`" |
|
3017 | "`should_run_async` will not call `transform_cell`" | |
3019 | " automatically in the future. Please pass the result to" |
|
3018 | " automatically in the future. Please pass the result to" | |
3020 | " `transformed_cell` argument and any exception that happen" |
|
3019 | " `transformed_cell` argument and any exception that happen" | |
3021 | " during the" |
|
3020 | " during the" | |
3022 | "transform in `preprocessing_exc_tuple` in" |
|
3021 | "transform in `preprocessing_exc_tuple` in" | |
3023 | " IPython 7.17 and above.", |
|
3022 | " IPython 7.17 and above.", | |
3024 | DeprecationWarning, |
|
3023 | DeprecationWarning, | |
3025 | stacklevel=2, |
|
3024 | stacklevel=2, | |
3026 | ) |
|
3025 | ) | |
3027 | try: |
|
3026 | try: | |
3028 | cell = self.transform_cell(raw_cell) |
|
3027 | cell = self.transform_cell(raw_cell) | |
3029 | except Exception: |
|
3028 | except Exception: | |
3030 | # any exception during transform will be raised |
|
3029 | # any exception during transform will be raised | |
3031 | # prior to execution |
|
3030 | # prior to execution | |
3032 | return False |
|
3031 | return False | |
3033 | else: |
|
3032 | else: | |
3034 | cell = transformed_cell |
|
3033 | cell = transformed_cell | |
3035 | return _should_be_async(cell) |
|
3034 | return _should_be_async(cell) | |
3036 |
|
3035 | |||
3037 | async def run_cell_async( |
|
3036 | async def run_cell_async( | |
3038 | self, |
|
3037 | self, | |
3039 | raw_cell: str, |
|
3038 | raw_cell: str, | |
3040 | store_history=False, |
|
3039 | store_history=False, | |
3041 | silent=False, |
|
3040 | silent=False, | |
3042 | shell_futures=True, |
|
3041 | shell_futures=True, | |
3043 | *, |
|
3042 | *, | |
3044 | transformed_cell: Optional[str] = None, |
|
3043 | transformed_cell: Optional[str] = None, | |
3045 | preprocessing_exc_tuple: Optional[Any] = None, |
|
3044 | preprocessing_exc_tuple: Optional[Any] = None, | |
3046 | cell_id=None, |
|
3045 | cell_id=None, | |
3047 | ) -> ExecutionResult: |
|
3046 | ) -> ExecutionResult: | |
3048 | """Run a complete IPython cell asynchronously. |
|
3047 | """Run a complete IPython cell asynchronously. | |
3049 |
|
3048 | |||
3050 | Parameters |
|
3049 | Parameters | |
3051 | ---------- |
|
3050 | ---------- | |
3052 | raw_cell : str |
|
3051 | raw_cell : str | |
3053 | The code (including IPython code such as %magic functions) to run. |
|
3052 | The code (including IPython code such as %magic functions) to run. | |
3054 | store_history : bool |
|
3053 | store_history : bool | |
3055 | If True, the raw and translated cell will be stored in IPython's |
|
3054 | If True, the raw and translated cell will be stored in IPython's | |
3056 | history. For user code calling back into IPython's machinery, this |
|
3055 | history. For user code calling back into IPython's machinery, this | |
3057 | should be set to False. |
|
3056 | should be set to False. | |
3058 | silent : bool |
|
3057 | silent : bool | |
3059 | If True, avoid side-effects, such as implicit displayhooks and |
|
3058 | If True, avoid side-effects, such as implicit displayhooks and | |
3060 | and logging. silent=True forces store_history=False. |
|
3059 | and logging. silent=True forces store_history=False. | |
3061 | shell_futures : bool |
|
3060 | shell_futures : bool | |
3062 | If True, the code will share future statements with the interactive |
|
3061 | If True, the code will share future statements with the interactive | |
3063 | shell. It will both be affected by previous __future__ imports, and |
|
3062 | shell. It will both be affected by previous __future__ imports, and | |
3064 | any __future__ imports in the code will affect the shell. If False, |
|
3063 | any __future__ imports in the code will affect the shell. If False, | |
3065 | __future__ imports are not shared in either direction. |
|
3064 | __future__ imports are not shared in either direction. | |
3066 | transformed_cell: str |
|
3065 | transformed_cell: str | |
3067 | cell that was passed through transformers |
|
3066 | cell that was passed through transformers | |
3068 | preprocessing_exc_tuple: |
|
3067 | preprocessing_exc_tuple: | |
3069 | trace if the transformation failed. |
|
3068 | trace if the transformation failed. | |
3070 |
|
3069 | |||
3071 | Returns |
|
3070 | Returns | |
3072 | ------- |
|
3071 | ------- | |
3073 | result : :class:`ExecutionResult` |
|
3072 | result : :class:`ExecutionResult` | |
3074 |
|
3073 | |||
3075 | .. versionadded:: 7.0 |
|
3074 | .. versionadded:: 7.0 | |
3076 | """ |
|
3075 | """ | |
3077 | info = ExecutionInfo(raw_cell, store_history, silent, shell_futures, cell_id) |
|
3076 | info = ExecutionInfo(raw_cell, store_history, silent, shell_futures, cell_id) | |
3078 | result = ExecutionResult(info) |
|
3077 | result = ExecutionResult(info) | |
3079 |
|
3078 | |||
3080 | if (not raw_cell) or raw_cell.isspace(): |
|
3079 | if (not raw_cell) or raw_cell.isspace(): | |
3081 | self.last_execution_succeeded = True |
|
3080 | self.last_execution_succeeded = True | |
3082 | self.last_execution_result = result |
|
3081 | self.last_execution_result = result | |
3083 | return result |
|
3082 | return result | |
3084 |
|
3083 | |||
3085 | if silent: |
|
3084 | if silent: | |
3086 | store_history = False |
|
3085 | store_history = False | |
3087 |
|
3086 | |||
3088 | if store_history: |
|
3087 | if store_history: | |
3089 | result.execution_count = self.execution_count |
|
3088 | result.execution_count = self.execution_count | |
3090 |
|
3089 | |||
3091 | def error_before_exec(value): |
|
3090 | def error_before_exec(value): | |
3092 | if store_history: |
|
3091 | if store_history: | |
3093 | self.execution_count += 1 |
|
3092 | self.execution_count += 1 | |
3094 | result.error_before_exec = value |
|
3093 | result.error_before_exec = value | |
3095 | self.last_execution_succeeded = False |
|
3094 | self.last_execution_succeeded = False | |
3096 | self.last_execution_result = result |
|
3095 | self.last_execution_result = result | |
3097 | return result |
|
3096 | return result | |
3098 |
|
3097 | |||
3099 | self.events.trigger('pre_execute') |
|
3098 | self.events.trigger('pre_execute') | |
3100 | if not silent: |
|
3099 | if not silent: | |
3101 | self.events.trigger('pre_run_cell', info) |
|
3100 | self.events.trigger('pre_run_cell', info) | |
3102 |
|
3101 | |||
3103 | if transformed_cell is None: |
|
3102 | if transformed_cell is None: | |
3104 | warnings.warn( |
|
3103 | warnings.warn( | |
3105 | "`run_cell_async` will not call `transform_cell`" |
|
3104 | "`run_cell_async` will not call `transform_cell`" | |
3106 | " automatically in the future. Please pass the result to" |
|
3105 | " automatically in the future. Please pass the result to" | |
3107 | " `transformed_cell` argument and any exception that happen" |
|
3106 | " `transformed_cell` argument and any exception that happen" | |
3108 | " during the" |
|
3107 | " during the" | |
3109 | "transform in `preprocessing_exc_tuple` in" |
|
3108 | "transform in `preprocessing_exc_tuple` in" | |
3110 | " IPython 7.17 and above.", |
|
3109 | " IPython 7.17 and above.", | |
3111 | DeprecationWarning, |
|
3110 | DeprecationWarning, | |
3112 | stacklevel=2, |
|
3111 | stacklevel=2, | |
3113 | ) |
|
3112 | ) | |
3114 | # If any of our input transformation (input_transformer_manager or |
|
3113 | # If any of our input transformation (input_transformer_manager or | |
3115 | # prefilter_manager) raises an exception, we store it in this variable |
|
3114 | # prefilter_manager) raises an exception, we store it in this variable | |
3116 | # so that we can display the error after logging the input and storing |
|
3115 | # so that we can display the error after logging the input and storing | |
3117 | # it in the history. |
|
3116 | # it in the history. | |
3118 | try: |
|
3117 | try: | |
3119 | cell = self.transform_cell(raw_cell) |
|
3118 | cell = self.transform_cell(raw_cell) | |
3120 | except Exception: |
|
3119 | except Exception: | |
3121 | preprocessing_exc_tuple = sys.exc_info() |
|
3120 | preprocessing_exc_tuple = sys.exc_info() | |
3122 | cell = raw_cell # cell has to exist so it can be stored/logged |
|
3121 | cell = raw_cell # cell has to exist so it can be stored/logged | |
3123 | else: |
|
3122 | else: | |
3124 | preprocessing_exc_tuple = None |
|
3123 | preprocessing_exc_tuple = None | |
3125 | else: |
|
3124 | else: | |
3126 | if preprocessing_exc_tuple is None: |
|
3125 | if preprocessing_exc_tuple is None: | |
3127 | cell = transformed_cell |
|
3126 | cell = transformed_cell | |
3128 | else: |
|
3127 | else: | |
3129 | cell = raw_cell |
|
3128 | cell = raw_cell | |
3130 |
|
3129 | |||
3131 | # Store raw and processed history |
|
3130 | # Store raw and processed history | |
3132 | if store_history and raw_cell.strip(" %") != "paste": |
|
3131 | if store_history and raw_cell.strip(" %") != "paste": | |
3133 | self.history_manager.store_inputs(self.execution_count, cell, raw_cell) |
|
3132 | self.history_manager.store_inputs(self.execution_count, cell, raw_cell) | |
3134 | if not silent: |
|
3133 | if not silent: | |
3135 | self.logger.log(cell, raw_cell) |
|
3134 | self.logger.log(cell, raw_cell) | |
3136 |
|
3135 | |||
3137 | # Display the exception if input processing failed. |
|
3136 | # Display the exception if input processing failed. | |
3138 | if preprocessing_exc_tuple is not None: |
|
3137 | if preprocessing_exc_tuple is not None: | |
3139 | self.showtraceback(preprocessing_exc_tuple) |
|
3138 | self.showtraceback(preprocessing_exc_tuple) | |
3140 | if store_history: |
|
3139 | if store_history: | |
3141 | self.execution_count += 1 |
|
3140 | self.execution_count += 1 | |
3142 | return error_before_exec(preprocessing_exc_tuple[1]) |
|
3141 | return error_before_exec(preprocessing_exc_tuple[1]) | |
3143 |
|
3142 | |||
3144 | # Our own compiler remembers the __future__ environment. If we want to |
|
3143 | # Our own compiler remembers the __future__ environment. If we want to | |
3145 | # run code with a separate __future__ environment, use the default |
|
3144 | # run code with a separate __future__ environment, use the default | |
3146 | # compiler |
|
3145 | # compiler | |
3147 | compiler = self.compile if shell_futures else self.compiler_class() |
|
3146 | compiler = self.compile if shell_futures else self.compiler_class() | |
3148 |
|
3147 | |||
3149 | _run_async = False |
|
3148 | _run_async = False | |
3150 |
|
3149 | |||
3151 | with self.builtin_trap: |
|
3150 | with self.builtin_trap: | |
3152 | cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell) |
|
3151 | cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell) | |
3153 |
|
3152 | |||
3154 | with self.display_trap: |
|
3153 | with self.display_trap: | |
3155 | # Compile to bytecode |
|
3154 | # Compile to bytecode | |
3156 | try: |
|
3155 | try: | |
3157 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
3156 | code_ast = compiler.ast_parse(cell, filename=cell_name) | |
3158 | except self.custom_exceptions as e: |
|
3157 | except self.custom_exceptions as e: | |
3159 | etype, value, tb = sys.exc_info() |
|
3158 | etype, value, tb = sys.exc_info() | |
3160 | self.CustomTB(etype, value, tb) |
|
3159 | self.CustomTB(etype, value, tb) | |
3161 | return error_before_exec(e) |
|
3160 | return error_before_exec(e) | |
3162 | except IndentationError as e: |
|
3161 | except IndentationError as e: | |
3163 | self.showindentationerror() |
|
3162 | self.showindentationerror() | |
3164 | return error_before_exec(e) |
|
3163 | return error_before_exec(e) | |
3165 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
3164 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
3166 | MemoryError) as e: |
|
3165 | MemoryError) as e: | |
3167 | self.showsyntaxerror() |
|
3166 | self.showsyntaxerror() | |
3168 | return error_before_exec(e) |
|
3167 | return error_before_exec(e) | |
3169 |
|
3168 | |||
3170 | # Apply AST transformations |
|
3169 | # Apply AST transformations | |
3171 | try: |
|
3170 | try: | |
3172 | code_ast = self.transform_ast(code_ast) |
|
3171 | code_ast = self.transform_ast(code_ast) | |
3173 | except InputRejected as e: |
|
3172 | except InputRejected as e: | |
3174 | self.showtraceback() |
|
3173 | self.showtraceback() | |
3175 | return error_before_exec(e) |
|
3174 | return error_before_exec(e) | |
3176 |
|
3175 | |||
3177 | # Give the displayhook a reference to our ExecutionResult so it |
|
3176 | # Give the displayhook a reference to our ExecutionResult so it | |
3178 | # can fill in the output value. |
|
3177 | # can fill in the output value. | |
3179 | self.displayhook.exec_result = result |
|
3178 | self.displayhook.exec_result = result | |
3180 |
|
3179 | |||
3181 | # Execute the user code |
|
3180 | # Execute the user code | |
3182 | interactivity = "none" if silent else self.ast_node_interactivity |
|
3181 | interactivity = "none" if silent else self.ast_node_interactivity | |
3183 |
|
3182 | |||
3184 | has_raised = await self.run_ast_nodes(code_ast.body, cell_name, |
|
3183 | has_raised = await self.run_ast_nodes(code_ast.body, cell_name, | |
3185 | interactivity=interactivity, compiler=compiler, result=result) |
|
3184 | interactivity=interactivity, compiler=compiler, result=result) | |
3186 |
|
3185 | |||
3187 | self.last_execution_succeeded = not has_raised |
|
3186 | self.last_execution_succeeded = not has_raised | |
3188 | self.last_execution_result = result |
|
3187 | self.last_execution_result = result | |
3189 |
|
3188 | |||
3190 | # Reset this so later displayed values do not modify the |
|
3189 | # Reset this so later displayed values do not modify the | |
3191 | # ExecutionResult |
|
3190 | # ExecutionResult | |
3192 | self.displayhook.exec_result = None |
|
3191 | self.displayhook.exec_result = None | |
3193 |
|
3192 | |||
3194 | if store_history: |
|
3193 | if store_history: | |
3195 | # Write output to the database. Does nothing unless |
|
3194 | # Write output to the database. Does nothing unless | |
3196 | # history output logging is enabled. |
|
3195 | # history output logging is enabled. | |
3197 | self.history_manager.store_output(self.execution_count) |
|
3196 | self.history_manager.store_output(self.execution_count) | |
3198 | # Each cell is a *single* input, regardless of how many lines it has |
|
3197 | # Each cell is a *single* input, regardless of how many lines it has | |
3199 | self.execution_count += 1 |
|
3198 | self.execution_count += 1 | |
3200 |
|
3199 | |||
3201 | return result |
|
3200 | return result | |
3202 |
|
3201 | |||
3203 | def transform_cell(self, raw_cell): |
|
3202 | def transform_cell(self, raw_cell): | |
3204 | """Transform an input cell before parsing it. |
|
3203 | """Transform an input cell before parsing it. | |
3205 |
|
3204 | |||
3206 | Static transformations, implemented in IPython.core.inputtransformer2, |
|
3205 | Static transformations, implemented in IPython.core.inputtransformer2, | |
3207 | deal with things like ``%magic`` and ``!system`` commands. |
|
3206 | deal with things like ``%magic`` and ``!system`` commands. | |
3208 | These run on all input. |
|
3207 | These run on all input. | |
3209 | Dynamic transformations, for things like unescaped magics and the exit |
|
3208 | Dynamic transformations, for things like unescaped magics and the exit | |
3210 | autocall, depend on the state of the interpreter. |
|
3209 | autocall, depend on the state of the interpreter. | |
3211 | These only apply to single line inputs. |
|
3210 | These only apply to single line inputs. | |
3212 |
|
3211 | |||
3213 | These string-based transformations are followed by AST transformations; |
|
3212 | These string-based transformations are followed by AST transformations; | |
3214 | see :meth:`transform_ast`. |
|
3213 | see :meth:`transform_ast`. | |
3215 | """ |
|
3214 | """ | |
3216 | # Static input transformations |
|
3215 | # Static input transformations | |
3217 | cell = self.input_transformer_manager.transform_cell(raw_cell) |
|
3216 | cell = self.input_transformer_manager.transform_cell(raw_cell) | |
3218 |
|
3217 | |||
3219 | if len(cell.splitlines()) == 1: |
|
3218 | if len(cell.splitlines()) == 1: | |
3220 | # Dynamic transformations - only applied for single line commands |
|
3219 | # Dynamic transformations - only applied for single line commands | |
3221 | with self.builtin_trap: |
|
3220 | with self.builtin_trap: | |
3222 | # use prefilter_lines to handle trailing newlines |
|
3221 | # use prefilter_lines to handle trailing newlines | |
3223 | # restore trailing newline for ast.parse |
|
3222 | # restore trailing newline for ast.parse | |
3224 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
3223 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | |
3225 |
|
3224 | |||
3226 | lines = cell.splitlines(keepends=True) |
|
3225 | lines = cell.splitlines(keepends=True) | |
3227 | for transform in self.input_transformers_post: |
|
3226 | for transform in self.input_transformers_post: | |
3228 | lines = transform(lines) |
|
3227 | lines = transform(lines) | |
3229 | cell = ''.join(lines) |
|
3228 | cell = ''.join(lines) | |
3230 |
|
3229 | |||
3231 | return cell |
|
3230 | return cell | |
3232 |
|
3231 | |||
3233 | def transform_ast(self, node): |
|
3232 | def transform_ast(self, node): | |
3234 | """Apply the AST transformations from self.ast_transformers |
|
3233 | """Apply the AST transformations from self.ast_transformers | |
3235 |
|
3234 | |||
3236 | Parameters |
|
3235 | Parameters | |
3237 | ---------- |
|
3236 | ---------- | |
3238 | node : ast.Node |
|
3237 | node : ast.Node | |
3239 | The root node to be transformed. Typically called with the ast.Module |
|
3238 | The root node to be transformed. Typically called with the ast.Module | |
3240 | produced by parsing user input. |
|
3239 | produced by parsing user input. | |
3241 |
|
3240 | |||
3242 | Returns |
|
3241 | Returns | |
3243 | ------- |
|
3242 | ------- | |
3244 | An ast.Node corresponding to the node it was called with. Note that it |
|
3243 | An ast.Node corresponding to the node it was called with. Note that it | |
3245 | may also modify the passed object, so don't rely on references to the |
|
3244 | may also modify the passed object, so don't rely on references to the | |
3246 | original AST. |
|
3245 | original AST. | |
3247 | """ |
|
3246 | """ | |
3248 | for transformer in self.ast_transformers: |
|
3247 | for transformer in self.ast_transformers: | |
3249 | try: |
|
3248 | try: | |
3250 | node = transformer.visit(node) |
|
3249 | node = transformer.visit(node) | |
3251 | except InputRejected: |
|
3250 | except InputRejected: | |
3252 | # User-supplied AST transformers can reject an input by raising |
|
3251 | # User-supplied AST transformers can reject an input by raising | |
3253 | # an InputRejected. Short-circuit in this case so that we |
|
3252 | # an InputRejected. Short-circuit in this case so that we | |
3254 | # don't unregister the transform. |
|
3253 | # don't unregister the transform. | |
3255 | raise |
|
3254 | raise | |
3256 | except Exception: |
|
3255 | except Exception: | |
3257 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
3256 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) | |
3258 | self.ast_transformers.remove(transformer) |
|
3257 | self.ast_transformers.remove(transformer) | |
3259 |
|
3258 | |||
3260 | if self.ast_transformers: |
|
3259 | if self.ast_transformers: | |
3261 | ast.fix_missing_locations(node) |
|
3260 | ast.fix_missing_locations(node) | |
3262 | return node |
|
3261 | return node | |
3263 |
|
3262 | |||
3264 | async def run_ast_nodes( |
|
3263 | async def run_ast_nodes( | |
3265 | self, |
|
3264 | self, | |
3266 | nodelist: ListType[stmt], |
|
3265 | nodelist: ListType[stmt], | |
3267 | cell_name: str, |
|
3266 | cell_name: str, | |
3268 | interactivity="last_expr", |
|
3267 | interactivity="last_expr", | |
3269 | compiler=compile, |
|
3268 | compiler=compile, | |
3270 | result=None, |
|
3269 | result=None, | |
3271 | ): |
|
3270 | ): | |
3272 | """Run a sequence of AST nodes. The execution mode depends on the |
|
3271 | """Run a sequence of AST nodes. The execution mode depends on the | |
3273 | interactivity parameter. |
|
3272 | interactivity parameter. | |
3274 |
|
3273 | |||
3275 | Parameters |
|
3274 | Parameters | |
3276 | ---------- |
|
3275 | ---------- | |
3277 | nodelist : list |
|
3276 | nodelist : list | |
3278 | A sequence of AST nodes to run. |
|
3277 | A sequence of AST nodes to run. | |
3279 | cell_name : str |
|
3278 | cell_name : str | |
3280 | Will be passed to the compiler as the filename of the cell. Typically |
|
3279 | Will be passed to the compiler as the filename of the cell. Typically | |
3281 | the value returned by ip.compile.cache(cell). |
|
3280 | the value returned by ip.compile.cache(cell). | |
3282 | interactivity : str |
|
3281 | interactivity : str | |
3283 | 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none', |
|
3282 | 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none', | |
3284 | specifying which nodes should be run interactively (displaying output |
|
3283 | specifying which nodes should be run interactively (displaying output | |
3285 | from expressions). 'last_expr' will run the last node interactively |
|
3284 | from expressions). 'last_expr' will run the last node interactively | |
3286 | only if it is an expression (i.e. expressions in loops or other blocks |
|
3285 | only if it is an expression (i.e. expressions in loops or other blocks | |
3287 | are not displayed) 'last_expr_or_assign' will run the last expression |
|
3286 | are not displayed) 'last_expr_or_assign' will run the last expression | |
3288 | or the last assignment. Other values for this parameter will raise a |
|
3287 | or the last assignment. Other values for this parameter will raise a | |
3289 | ValueError. |
|
3288 | ValueError. | |
3290 |
|
3289 | |||
3291 | compiler : callable |
|
3290 | compiler : callable | |
3292 | A function with the same interface as the built-in compile(), to turn |
|
3291 | A function with the same interface as the built-in compile(), to turn | |
3293 | the AST nodes into code objects. Default is the built-in compile(). |
|
3292 | the AST nodes into code objects. Default is the built-in compile(). | |
3294 | result : ExecutionResult, optional |
|
3293 | result : ExecutionResult, optional | |
3295 | An object to store exceptions that occur during execution. |
|
3294 | An object to store exceptions that occur during execution. | |
3296 |
|
3295 | |||
3297 | Returns |
|
3296 | Returns | |
3298 | ------- |
|
3297 | ------- | |
3299 | True if an exception occurred while running code, False if it finished |
|
3298 | True if an exception occurred while running code, False if it finished | |
3300 | running. |
|
3299 | running. | |
3301 | """ |
|
3300 | """ | |
3302 | if not nodelist: |
|
3301 | if not nodelist: | |
3303 | return |
|
3302 | return | |
3304 |
|
3303 | |||
3305 |
|
3304 | |||
3306 | if interactivity == 'last_expr_or_assign': |
|
3305 | if interactivity == 'last_expr_or_assign': | |
3307 | if isinstance(nodelist[-1], _assign_nodes): |
|
3306 | if isinstance(nodelist[-1], _assign_nodes): | |
3308 | asg = nodelist[-1] |
|
3307 | asg = nodelist[-1] | |
3309 | if isinstance(asg, ast.Assign) and len(asg.targets) == 1: |
|
3308 | if isinstance(asg, ast.Assign) and len(asg.targets) == 1: | |
3310 | target = asg.targets[0] |
|
3309 | target = asg.targets[0] | |
3311 | elif isinstance(asg, _single_targets_nodes): |
|
3310 | elif isinstance(asg, _single_targets_nodes): | |
3312 | target = asg.target |
|
3311 | target = asg.target | |
3313 | else: |
|
3312 | else: | |
3314 | target = None |
|
3313 | target = None | |
3315 | if isinstance(target, ast.Name): |
|
3314 | if isinstance(target, ast.Name): | |
3316 | nnode = ast.Expr(ast.Name(target.id, ast.Load())) |
|
3315 | nnode = ast.Expr(ast.Name(target.id, ast.Load())) | |
3317 | ast.fix_missing_locations(nnode) |
|
3316 | ast.fix_missing_locations(nnode) | |
3318 | nodelist.append(nnode) |
|
3317 | nodelist.append(nnode) | |
3319 | interactivity = 'last_expr' |
|
3318 | interactivity = 'last_expr' | |
3320 |
|
3319 | |||
3321 | _async = False |
|
3320 | _async = False | |
3322 | if interactivity == 'last_expr': |
|
3321 | if interactivity == 'last_expr': | |
3323 | if isinstance(nodelist[-1], ast.Expr): |
|
3322 | if isinstance(nodelist[-1], ast.Expr): | |
3324 | interactivity = "last" |
|
3323 | interactivity = "last" | |
3325 | else: |
|
3324 | else: | |
3326 | interactivity = "none" |
|
3325 | interactivity = "none" | |
3327 |
|
3326 | |||
3328 | if interactivity == 'none': |
|
3327 | if interactivity == 'none': | |
3329 | to_run_exec, to_run_interactive = nodelist, [] |
|
3328 | to_run_exec, to_run_interactive = nodelist, [] | |
3330 | elif interactivity == 'last': |
|
3329 | elif interactivity == 'last': | |
3331 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
3330 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | |
3332 | elif interactivity == 'all': |
|
3331 | elif interactivity == 'all': | |
3333 | to_run_exec, to_run_interactive = [], nodelist |
|
3332 | to_run_exec, to_run_interactive = [], nodelist | |
3334 | else: |
|
3333 | else: | |
3335 | raise ValueError("Interactivity was %r" % interactivity) |
|
3334 | raise ValueError("Interactivity was %r" % interactivity) | |
3336 |
|
3335 | |||
3337 | try: |
|
3336 | try: | |
3338 |
|
3337 | |||
3339 | def compare(code): |
|
3338 | def compare(code): | |
3340 | is_async = inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE |
|
3339 | is_async = inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE | |
3341 | return is_async |
|
3340 | return is_async | |
3342 |
|
3341 | |||
3343 | # refactor that to just change the mod constructor. |
|
3342 | # refactor that to just change the mod constructor. | |
3344 | to_run = [] |
|
3343 | to_run = [] | |
3345 | for node in to_run_exec: |
|
3344 | for node in to_run_exec: | |
3346 | to_run.append((node, "exec")) |
|
3345 | to_run.append((node, "exec")) | |
3347 |
|
3346 | |||
3348 | for node in to_run_interactive: |
|
3347 | for node in to_run_interactive: | |
3349 | to_run.append((node, "single")) |
|
3348 | to_run.append((node, "single")) | |
3350 |
|
3349 | |||
3351 | for node, mode in to_run: |
|
3350 | for node, mode in to_run: | |
3352 | if mode == "exec": |
|
3351 | if mode == "exec": | |
3353 | mod = Module([node], []) |
|
3352 | mod = Module([node], []) | |
3354 | elif mode == "single": |
|
3353 | elif mode == "single": | |
3355 | mod = ast.Interactive([node]) |
|
3354 | mod = ast.Interactive([node]) | |
3356 | with compiler.extra_flags( |
|
3355 | with compiler.extra_flags( | |
3357 | getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0) |
|
3356 | getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0) | |
3358 | if self.autoawait |
|
3357 | if self.autoawait | |
3359 | else 0x0 |
|
3358 | else 0x0 | |
3360 | ): |
|
3359 | ): | |
3361 | code = compiler(mod, cell_name, mode) |
|
3360 | code = compiler(mod, cell_name, mode) | |
3362 | asy = compare(code) |
|
3361 | asy = compare(code) | |
3363 | if await self.run_code(code, result, async_=asy): |
|
3362 | if await self.run_code(code, result, async_=asy): | |
3364 | return True |
|
3363 | return True | |
3365 |
|
3364 | |||
3366 | # Flush softspace |
|
3365 | # Flush softspace | |
3367 | if softspace(sys.stdout, 0): |
|
3366 | if softspace(sys.stdout, 0): | |
3368 | print() |
|
3367 | print() | |
3369 |
|
3368 | |||
3370 | except: |
|
3369 | except: | |
3371 | # It's possible to have exceptions raised here, typically by |
|
3370 | # It's possible to have exceptions raised here, typically by | |
3372 | # compilation of odd code (such as a naked 'return' outside a |
|
3371 | # compilation of odd code (such as a naked 'return' outside a | |
3373 | # function) that did parse but isn't valid. Typically the exception |
|
3372 | # function) that did parse but isn't valid. Typically the exception | |
3374 | # is a SyntaxError, but it's safest just to catch anything and show |
|
3373 | # is a SyntaxError, but it's safest just to catch anything and show | |
3375 | # the user a traceback. |
|
3374 | # the user a traceback. | |
3376 |
|
3375 | |||
3377 | # We do only one try/except outside the loop to minimize the impact |
|
3376 | # We do only one try/except outside the loop to minimize the impact | |
3378 | # on runtime, and also because if any node in the node list is |
|
3377 | # on runtime, and also because if any node in the node list is | |
3379 | # broken, we should stop execution completely. |
|
3378 | # broken, we should stop execution completely. | |
3380 | if result: |
|
3379 | if result: | |
3381 | result.error_before_exec = sys.exc_info()[1] |
|
3380 | result.error_before_exec = sys.exc_info()[1] | |
3382 | self.showtraceback() |
|
3381 | self.showtraceback() | |
3383 | return True |
|
3382 | return True | |
3384 |
|
3383 | |||
3385 | return False |
|
3384 | return False | |
3386 |
|
3385 | |||
3387 | async def run_code(self, code_obj, result=None, *, async_=False): |
|
3386 | async def run_code(self, code_obj, result=None, *, async_=False): | |
3388 | """Execute a code object. |
|
3387 | """Execute a code object. | |
3389 |
|
3388 | |||
3390 | When an exception occurs, self.showtraceback() is called to display a |
|
3389 | When an exception occurs, self.showtraceback() is called to display a | |
3391 | traceback. |
|
3390 | traceback. | |
3392 |
|
3391 | |||
3393 | Parameters |
|
3392 | Parameters | |
3394 | ---------- |
|
3393 | ---------- | |
3395 | code_obj : code object |
|
3394 | code_obj : code object | |
3396 | A compiled code object, to be executed |
|
3395 | A compiled code object, to be executed | |
3397 | result : ExecutionResult, optional |
|
3396 | result : ExecutionResult, optional | |
3398 | An object to store exceptions that occur during execution. |
|
3397 | An object to store exceptions that occur during execution. | |
3399 | async_ : Bool (Experimental) |
|
3398 | async_ : Bool (Experimental) | |
3400 | Attempt to run top-level asynchronous code in a default loop. |
|
3399 | Attempt to run top-level asynchronous code in a default loop. | |
3401 |
|
3400 | |||
3402 | Returns |
|
3401 | Returns | |
3403 | ------- |
|
3402 | ------- | |
3404 | False : successful execution. |
|
3403 | False : successful execution. | |
3405 | True : an error occurred. |
|
3404 | True : an error occurred. | |
3406 | """ |
|
3405 | """ | |
3407 | # special value to say that anything above is IPython and should be |
|
3406 | # special value to say that anything above is IPython and should be | |
3408 | # hidden. |
|
3407 | # hidden. | |
3409 | __tracebackhide__ = "__ipython_bottom__" |
|
3408 | __tracebackhide__ = "__ipython_bottom__" | |
3410 | # Set our own excepthook in case the user code tries to call it |
|
3409 | # Set our own excepthook in case the user code tries to call it | |
3411 | # directly, so that the IPython crash handler doesn't get triggered |
|
3410 | # directly, so that the IPython crash handler doesn't get triggered | |
3412 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook |
|
3411 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook | |
3413 |
|
3412 | |||
3414 | # we save the original sys.excepthook in the instance, in case config |
|
3413 | # we save the original sys.excepthook in the instance, in case config | |
3415 | # code (such as magics) needs access to it. |
|
3414 | # code (such as magics) needs access to it. | |
3416 | self.sys_excepthook = old_excepthook |
|
3415 | self.sys_excepthook = old_excepthook | |
3417 | outflag = True # happens in more places, so it's easier as default |
|
3416 | outflag = True # happens in more places, so it's easier as default | |
3418 | try: |
|
3417 | try: | |
3419 | try: |
|
3418 | try: | |
3420 | if async_: |
|
3419 | if async_: | |
3421 | await eval(code_obj, self.user_global_ns, self.user_ns) |
|
3420 | await eval(code_obj, self.user_global_ns, self.user_ns) | |
3422 | else: |
|
3421 | else: | |
3423 | exec(code_obj, self.user_global_ns, self.user_ns) |
|
3422 | exec(code_obj, self.user_global_ns, self.user_ns) | |
3424 | finally: |
|
3423 | finally: | |
3425 | # Reset our crash handler in place |
|
3424 | # Reset our crash handler in place | |
3426 | sys.excepthook = old_excepthook |
|
3425 | sys.excepthook = old_excepthook | |
3427 | except SystemExit as e: |
|
3426 | except SystemExit as e: | |
3428 | if result is not None: |
|
3427 | if result is not None: | |
3429 | result.error_in_exec = e |
|
3428 | result.error_in_exec = e | |
3430 | self.showtraceback(exception_only=True) |
|
3429 | self.showtraceback(exception_only=True) | |
3431 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) |
|
3430 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) | |
3432 | except bdb.BdbQuit: |
|
3431 | except bdb.BdbQuit: | |
3433 | etype, value, tb = sys.exc_info() |
|
3432 | etype, value, tb = sys.exc_info() | |
3434 | if result is not None: |
|
3433 | if result is not None: | |
3435 | result.error_in_exec = value |
|
3434 | result.error_in_exec = value | |
3436 | # the BdbQuit stops here |
|
3435 | # the BdbQuit stops here | |
3437 | except self.custom_exceptions: |
|
3436 | except self.custom_exceptions: | |
3438 | etype, value, tb = sys.exc_info() |
|
3437 | etype, value, tb = sys.exc_info() | |
3439 | if result is not None: |
|
3438 | if result is not None: | |
3440 | result.error_in_exec = value |
|
3439 | result.error_in_exec = value | |
3441 | self.CustomTB(etype, value, tb) |
|
3440 | self.CustomTB(etype, value, tb) | |
3442 | except: |
|
3441 | except: | |
3443 | if result is not None: |
|
3442 | if result is not None: | |
3444 | result.error_in_exec = sys.exc_info()[1] |
|
3443 | result.error_in_exec = sys.exc_info()[1] | |
3445 | self.showtraceback(running_compiled_code=True) |
|
3444 | self.showtraceback(running_compiled_code=True) | |
3446 | else: |
|
3445 | else: | |
3447 | outflag = False |
|
3446 | outflag = False | |
3448 | return outflag |
|
3447 | return outflag | |
3449 |
|
3448 | |||
3450 | # For backwards compatibility |
|
3449 | # For backwards compatibility | |
3451 | runcode = run_code |
|
3450 | runcode = run_code | |
3452 |
|
3451 | |||
3453 | def check_complete(self, code: str) -> Tuple[str, str]: |
|
3452 | def check_complete(self, code: str) -> Tuple[str, str]: | |
3454 | """Return whether a block of code is ready to execute, or should be continued |
|
3453 | """Return whether a block of code is ready to execute, or should be continued | |
3455 |
|
3454 | |||
3456 | Parameters |
|
3455 | Parameters | |
3457 | ---------- |
|
3456 | ---------- | |
3458 | code : string |
|
3457 | code : string | |
3459 | Python input code, which can be multiline. |
|
3458 | Python input code, which can be multiline. | |
3460 |
|
3459 | |||
3461 | Returns |
|
3460 | Returns | |
3462 | ------- |
|
3461 | ------- | |
3463 | status : str |
|
3462 | status : str | |
3464 | One of 'complete', 'incomplete', or 'invalid' if source is not a |
|
3463 | One of 'complete', 'incomplete', or 'invalid' if source is not a | |
3465 | prefix of valid code. |
|
3464 | prefix of valid code. | |
3466 | indent : str |
|
3465 | indent : str | |
3467 | When status is 'incomplete', this is some whitespace to insert on |
|
3466 | When status is 'incomplete', this is some whitespace to insert on | |
3468 | the next line of the prompt. |
|
3467 | the next line of the prompt. | |
3469 | """ |
|
3468 | """ | |
3470 | status, nspaces = self.input_transformer_manager.check_complete(code) |
|
3469 | status, nspaces = self.input_transformer_manager.check_complete(code) | |
3471 | return status, ' ' * (nspaces or 0) |
|
3470 | return status, ' ' * (nspaces or 0) | |
3472 |
|
3471 | |||
3473 | #------------------------------------------------------------------------- |
|
3472 | #------------------------------------------------------------------------- | |
3474 | # Things related to GUI support and pylab |
|
3473 | # Things related to GUI support and pylab | |
3475 | #------------------------------------------------------------------------- |
|
3474 | #------------------------------------------------------------------------- | |
3476 |
|
3475 | |||
3477 | active_eventloop = None |
|
3476 | active_eventloop = None | |
3478 |
|
3477 | |||
3479 | def enable_gui(self, gui=None): |
|
3478 | def enable_gui(self, gui=None): | |
3480 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
3479 | raise NotImplementedError('Implement enable_gui in a subclass') | |
3481 |
|
3480 | |||
3482 | def enable_matplotlib(self, gui=None): |
|
3481 | def enable_matplotlib(self, gui=None): | |
3483 | """Enable interactive matplotlib and inline figure support. |
|
3482 | """Enable interactive matplotlib and inline figure support. | |
3484 |
|
3483 | |||
3485 | This takes the following steps: |
|
3484 | This takes the following steps: | |
3486 |
|
3485 | |||
3487 | 1. select the appropriate eventloop and matplotlib backend |
|
3486 | 1. select the appropriate eventloop and matplotlib backend | |
3488 | 2. set up matplotlib for interactive use with that backend |
|
3487 | 2. set up matplotlib for interactive use with that backend | |
3489 | 3. configure formatters for inline figure display |
|
3488 | 3. configure formatters for inline figure display | |
3490 | 4. enable the selected gui eventloop |
|
3489 | 4. enable the selected gui eventloop | |
3491 |
|
3490 | |||
3492 | Parameters |
|
3491 | Parameters | |
3493 | ---------- |
|
3492 | ---------- | |
3494 | gui : optional, string |
|
3493 | gui : optional, string | |
3495 | If given, dictates the choice of matplotlib GUI backend to use |
|
3494 | If given, dictates the choice of matplotlib GUI backend to use | |
3496 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3495 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
3497 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3496 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
3498 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3497 | matplotlib (as dictated by the matplotlib build-time options plus the | |
3499 | user's matplotlibrc configuration file). Note that not all backends |
|
3498 | user's matplotlibrc configuration file). Note that not all backends | |
3500 | make sense in all contexts, for example a terminal ipython can't |
|
3499 | make sense in all contexts, for example a terminal ipython can't | |
3501 | display figures inline. |
|
3500 | display figures inline. | |
3502 | """ |
|
3501 | """ | |
3503 | from matplotlib_inline.backend_inline import configure_inline_support |
|
3502 | from matplotlib_inline.backend_inline import configure_inline_support | |
3504 |
|
3503 | |||
3505 | from IPython.core import pylabtools as pt |
|
3504 | from IPython.core import pylabtools as pt | |
3506 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
3505 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) | |
3507 |
|
3506 | |||
3508 | if gui != 'inline': |
|
3507 | if gui != 'inline': | |
3509 | # If we have our first gui selection, store it |
|
3508 | # If we have our first gui selection, store it | |
3510 | if self.pylab_gui_select is None: |
|
3509 | if self.pylab_gui_select is None: | |
3511 | self.pylab_gui_select = gui |
|
3510 | self.pylab_gui_select = gui | |
3512 | # Otherwise if they are different |
|
3511 | # Otherwise if they are different | |
3513 | elif gui != self.pylab_gui_select: |
|
3512 | elif gui != self.pylab_gui_select: | |
3514 | print('Warning: Cannot change to a different GUI toolkit: %s.' |
|
3513 | print('Warning: Cannot change to a different GUI toolkit: %s.' | |
3515 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
3514 | ' Using %s instead.' % (gui, self.pylab_gui_select)) | |
3516 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
3515 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) | |
3517 |
|
3516 | |||
3518 | pt.activate_matplotlib(backend) |
|
3517 | pt.activate_matplotlib(backend) | |
3519 | configure_inline_support(self, backend) |
|
3518 | configure_inline_support(self, backend) | |
3520 |
|
3519 | |||
3521 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
3520 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
3522 | # plot updates into account |
|
3521 | # plot updates into account | |
3523 | self.enable_gui(gui) |
|
3522 | self.enable_gui(gui) | |
3524 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
3523 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
3525 | pt.mpl_runner(self.safe_execfile) |
|
3524 | pt.mpl_runner(self.safe_execfile) | |
3526 |
|
3525 | |||
3527 | return gui, backend |
|
3526 | return gui, backend | |
3528 |
|
3527 | |||
3529 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
3528 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): | |
3530 | """Activate pylab support at runtime. |
|
3529 | """Activate pylab support at runtime. | |
3531 |
|
3530 | |||
3532 | This turns on support for matplotlib, preloads into the interactive |
|
3531 | This turns on support for matplotlib, preloads into the interactive | |
3533 | namespace all of numpy and pylab, and configures IPython to correctly |
|
3532 | namespace all of numpy and pylab, and configures IPython to correctly | |
3534 | interact with the GUI event loop. The GUI backend to be used can be |
|
3533 | interact with the GUI event loop. The GUI backend to be used can be | |
3535 | optionally selected with the optional ``gui`` argument. |
|
3534 | optionally selected with the optional ``gui`` argument. | |
3536 |
|
3535 | |||
3537 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
3536 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. | |
3538 |
|
3537 | |||
3539 | Parameters |
|
3538 | Parameters | |
3540 | ---------- |
|
3539 | ---------- | |
3541 | gui : optional, string |
|
3540 | gui : optional, string | |
3542 | If given, dictates the choice of matplotlib GUI backend to use |
|
3541 | If given, dictates the choice of matplotlib GUI backend to use | |
3543 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3542 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
3544 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3543 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
3545 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3544 | matplotlib (as dictated by the matplotlib build-time options plus the | |
3546 | user's matplotlibrc configuration file). Note that not all backends |
|
3545 | user's matplotlibrc configuration file). Note that not all backends | |
3547 | make sense in all contexts, for example a terminal ipython can't |
|
3546 | make sense in all contexts, for example a terminal ipython can't | |
3548 | display figures inline. |
|
3547 | display figures inline. | |
3549 | import_all : optional, bool, default: True |
|
3548 | import_all : optional, bool, default: True | |
3550 | Whether to do `from numpy import *` and `from pylab import *` |
|
3549 | Whether to do `from numpy import *` and `from pylab import *` | |
3551 | in addition to module imports. |
|
3550 | in addition to module imports. | |
3552 | welcome_message : deprecated |
|
3551 | welcome_message : deprecated | |
3553 | This argument is ignored, no welcome message will be displayed. |
|
3552 | This argument is ignored, no welcome message will be displayed. | |
3554 | """ |
|
3553 | """ | |
3555 | from IPython.core.pylabtools import import_pylab |
|
3554 | from IPython.core.pylabtools import import_pylab | |
3556 |
|
3555 | |||
3557 | gui, backend = self.enable_matplotlib(gui) |
|
3556 | gui, backend = self.enable_matplotlib(gui) | |
3558 |
|
3557 | |||
3559 | # We want to prevent the loading of pylab to pollute the user's |
|
3558 | # We want to prevent the loading of pylab to pollute the user's | |
3560 | # namespace as shown by the %who* magics, so we execute the activation |
|
3559 | # namespace as shown by the %who* magics, so we execute the activation | |
3561 | # code in an empty namespace, and we update *both* user_ns and |
|
3560 | # code in an empty namespace, and we update *both* user_ns and | |
3562 | # user_ns_hidden with this information. |
|
3561 | # user_ns_hidden with this information. | |
3563 | ns = {} |
|
3562 | ns = {} | |
3564 | import_pylab(ns, import_all) |
|
3563 | import_pylab(ns, import_all) | |
3565 | # warn about clobbered names |
|
3564 | # warn about clobbered names | |
3566 | ignored = {"__builtins__"} |
|
3565 | ignored = {"__builtins__"} | |
3567 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
3566 | both = set(ns).intersection(self.user_ns).difference(ignored) | |
3568 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
3567 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] | |
3569 | self.user_ns.update(ns) |
|
3568 | self.user_ns.update(ns) | |
3570 | self.user_ns_hidden.update(ns) |
|
3569 | self.user_ns_hidden.update(ns) | |
3571 | return gui, backend, clobbered |
|
3570 | return gui, backend, clobbered | |
3572 |
|
3571 | |||
3573 | #------------------------------------------------------------------------- |
|
3572 | #------------------------------------------------------------------------- | |
3574 | # Utilities |
|
3573 | # Utilities | |
3575 | #------------------------------------------------------------------------- |
|
3574 | #------------------------------------------------------------------------- | |
3576 |
|
3575 | |||
3577 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
3576 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | |
3578 | """Expand python variables in a string. |
|
3577 | """Expand python variables in a string. | |
3579 |
|
3578 | |||
3580 | The depth argument indicates how many frames above the caller should |
|
3579 | The depth argument indicates how many frames above the caller should | |
3581 | be walked to look for the local namespace where to expand variables. |
|
3580 | be walked to look for the local namespace where to expand variables. | |
3582 |
|
3581 | |||
3583 | The global namespace for expansion is always the user's interactive |
|
3582 | The global namespace for expansion is always the user's interactive | |
3584 | namespace. |
|
3583 | namespace. | |
3585 | """ |
|
3584 | """ | |
3586 | ns = self.user_ns.copy() |
|
3585 | ns = self.user_ns.copy() | |
3587 | try: |
|
3586 | try: | |
3588 | frame = sys._getframe(depth+1) |
|
3587 | frame = sys._getframe(depth+1) | |
3589 | except ValueError: |
|
3588 | except ValueError: | |
3590 | # This is thrown if there aren't that many frames on the stack, |
|
3589 | # This is thrown if there aren't that many frames on the stack, | |
3591 | # e.g. if a script called run_line_magic() directly. |
|
3590 | # e.g. if a script called run_line_magic() directly. | |
3592 | pass |
|
3591 | pass | |
3593 | else: |
|
3592 | else: | |
3594 | ns.update(frame.f_locals) |
|
3593 | ns.update(frame.f_locals) | |
3595 |
|
3594 | |||
3596 | try: |
|
3595 | try: | |
3597 | # We have to use .vformat() here, because 'self' is a valid and common |
|
3596 | # We have to use .vformat() here, because 'self' is a valid and common | |
3598 | # name, and expanding **ns for .format() would make it collide with |
|
3597 | # name, and expanding **ns for .format() would make it collide with | |
3599 | # the 'self' argument of the method. |
|
3598 | # the 'self' argument of the method. | |
3600 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
3599 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) | |
3601 | except Exception: |
|
3600 | except Exception: | |
3602 | # if formatter couldn't format, just let it go untransformed |
|
3601 | # if formatter couldn't format, just let it go untransformed | |
3603 | pass |
|
3602 | pass | |
3604 | return cmd |
|
3603 | return cmd | |
3605 |
|
3604 | |||
3606 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
3605 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
3607 | """Make a new tempfile and return its filename. |
|
3606 | """Make a new tempfile and return its filename. | |
3608 |
|
3607 | |||
3609 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3608 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), | |
3610 | but it registers the created filename internally so ipython cleans it up |
|
3609 | but it registers the created filename internally so ipython cleans it up | |
3611 | at exit time. |
|
3610 | at exit time. | |
3612 |
|
3611 | |||
3613 | Optional inputs: |
|
3612 | Optional inputs: | |
3614 |
|
3613 | |||
3615 | - data(None): if data is given, it gets written out to the temp file |
|
3614 | - data(None): if data is given, it gets written out to the temp file | |
3616 | immediately, and the file is closed again.""" |
|
3615 | immediately, and the file is closed again.""" | |
3617 |
|
3616 | |||
3618 | dir_path = Path(tempfile.mkdtemp(prefix=prefix)) |
|
3617 | dir_path = Path(tempfile.mkdtemp(prefix=prefix)) | |
3619 | self.tempdirs.append(dir_path) |
|
3618 | self.tempdirs.append(dir_path) | |
3620 |
|
3619 | |||
3621 | handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path)) |
|
3620 | handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path)) | |
3622 | os.close(handle) # On Windows, there can only be one open handle on a file |
|
3621 | os.close(handle) # On Windows, there can only be one open handle on a file | |
3623 |
|
3622 | |||
3624 | file_path = Path(filename) |
|
3623 | file_path = Path(filename) | |
3625 | self.tempfiles.append(file_path) |
|
3624 | self.tempfiles.append(file_path) | |
3626 |
|
3625 | |||
3627 | if data: |
|
3626 | if data: | |
3628 | file_path.write_text(data, encoding="utf-8") |
|
3627 | file_path.write_text(data, encoding="utf-8") | |
3629 | return filename |
|
3628 | return filename | |
3630 |
|
3629 | |||
3631 | def ask_yes_no(self, prompt, default=None, interrupt=None): |
|
3630 | def ask_yes_no(self, prompt, default=None, interrupt=None): | |
3632 | if self.quiet: |
|
3631 | if self.quiet: | |
3633 | return True |
|
3632 | return True | |
3634 | return ask_yes_no(prompt,default,interrupt) |
|
3633 | return ask_yes_no(prompt,default,interrupt) | |
3635 |
|
3634 | |||
3636 | def show_usage(self): |
|
3635 | def show_usage(self): | |
3637 | """Show a usage message""" |
|
3636 | """Show a usage message""" | |
3638 | page.page(IPython.core.usage.interactive_usage) |
|
3637 | page.page(IPython.core.usage.interactive_usage) | |
3639 |
|
3638 | |||
3640 | def extract_input_lines(self, range_str, raw=False): |
|
3639 | def extract_input_lines(self, range_str, raw=False): | |
3641 | """Return as a string a set of input history slices. |
|
3640 | """Return as a string a set of input history slices. | |
3642 |
|
3641 | |||
3643 | Parameters |
|
3642 | Parameters | |
3644 | ---------- |
|
3643 | ---------- | |
3645 | range_str : str |
|
3644 | range_str : str | |
3646 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3645 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |
3647 | since this function is for use by magic functions which get their |
|
3646 | since this function is for use by magic functions which get their | |
3648 | arguments as strings. The number before the / is the session |
|
3647 | arguments as strings. The number before the / is the session | |
3649 | number: ~n goes n back from the current session. |
|
3648 | number: ~n goes n back from the current session. | |
3650 |
|
3649 | |||
3651 | If empty string is given, returns history of current session |
|
3650 | If empty string is given, returns history of current session | |
3652 | without the last input. |
|
3651 | without the last input. | |
3653 |
|
3652 | |||
3654 | raw : bool, optional |
|
3653 | raw : bool, optional | |
3655 | By default, the processed input is used. If this is true, the raw |
|
3654 | By default, the processed input is used. If this is true, the raw | |
3656 | input history is used instead. |
|
3655 | input history is used instead. | |
3657 |
|
3656 | |||
3658 | Notes |
|
3657 | Notes | |
3659 | ----- |
|
3658 | ----- | |
3660 | Slices can be described with two notations: |
|
3659 | Slices can be described with two notations: | |
3661 |
|
3660 | |||
3662 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3661 | * ``N:M`` -> standard python form, means including items N...(M-1). | |
3663 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3662 | * ``N-M`` -> include items N..M (closed endpoint). | |
3664 | """ |
|
3663 | """ | |
3665 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3664 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
3666 | text = "\n".join(x for _, _, x in lines) |
|
3665 | text = "\n".join(x for _, _, x in lines) | |
3667 |
|
3666 | |||
3668 | # Skip the last line, as it's probably the magic that called this |
|
3667 | # Skip the last line, as it's probably the magic that called this | |
3669 | if not range_str: |
|
3668 | if not range_str: | |
3670 | if "\n" not in text: |
|
3669 | if "\n" not in text: | |
3671 | text = "" |
|
3670 | text = "" | |
3672 | else: |
|
3671 | else: | |
3673 | text = text[: text.rfind("\n")] |
|
3672 | text = text[: text.rfind("\n")] | |
3674 |
|
3673 | |||
3675 | return text |
|
3674 | return text | |
3676 |
|
3675 | |||
3677 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): |
|
3676 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): | |
3678 | """Get a code string from history, file, url, or a string or macro. |
|
3677 | """Get a code string from history, file, url, or a string or macro. | |
3679 |
|
3678 | |||
3680 | This is mainly used by magic functions. |
|
3679 | This is mainly used by magic functions. | |
3681 |
|
3680 | |||
3682 | Parameters |
|
3681 | Parameters | |
3683 | ---------- |
|
3682 | ---------- | |
3684 | target : str |
|
3683 | target : str | |
3685 | A string specifying code to retrieve. This will be tried respectively |
|
3684 | A string specifying code to retrieve. This will be tried respectively | |
3686 | as: ranges of input history (see %history for syntax), url, |
|
3685 | as: ranges of input history (see %history for syntax), url, | |
3687 | corresponding .py file, filename, or an expression evaluating to a |
|
3686 | corresponding .py file, filename, or an expression evaluating to a | |
3688 | string or Macro in the user namespace. |
|
3687 | string or Macro in the user namespace. | |
3689 |
|
3688 | |||
3690 | If empty string is given, returns complete history of current |
|
3689 | If empty string is given, returns complete history of current | |
3691 | session, without the last line. |
|
3690 | session, without the last line. | |
3692 |
|
3691 | |||
3693 | raw : bool |
|
3692 | raw : bool | |
3694 | If true (default), retrieve raw history. Has no effect on the other |
|
3693 | If true (default), retrieve raw history. Has no effect on the other | |
3695 | retrieval mechanisms. |
|
3694 | retrieval mechanisms. | |
3696 |
|
3695 | |||
3697 | py_only : bool (default False) |
|
3696 | py_only : bool (default False) | |
3698 | Only try to fetch python code, do not try alternative methods to decode file |
|
3697 | Only try to fetch python code, do not try alternative methods to decode file | |
3699 | if unicode fails. |
|
3698 | if unicode fails. | |
3700 |
|
3699 | |||
3701 | Returns |
|
3700 | Returns | |
3702 | ------- |
|
3701 | ------- | |
3703 | A string of code. |
|
3702 | A string of code. | |
3704 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3703 | ValueError is raised if nothing is found, and TypeError if it evaluates | |
3705 | to an object of another type. In each case, .args[0] is a printable |
|
3704 | to an object of another type. In each case, .args[0] is a printable | |
3706 | message. |
|
3705 | message. | |
3707 | """ |
|
3706 | """ | |
3708 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3707 | code = self.extract_input_lines(target, raw=raw) # Grab history | |
3709 | if code: |
|
3708 | if code: | |
3710 | return code |
|
3709 | return code | |
3711 | try: |
|
3710 | try: | |
3712 | if target.startswith(('http://', 'https://')): |
|
3711 | if target.startswith(('http://', 'https://')): | |
3713 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) |
|
3712 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) | |
3714 | except UnicodeDecodeError as e: |
|
3713 | except UnicodeDecodeError as e: | |
3715 | if not py_only : |
|
3714 | if not py_only : | |
3716 | # Deferred import |
|
3715 | # Deferred import | |
3717 | from urllib.request import urlopen |
|
3716 | from urllib.request import urlopen | |
3718 | response = urlopen(target) |
|
3717 | response = urlopen(target) | |
3719 | return response.read().decode('latin1') |
|
3718 | return response.read().decode('latin1') | |
3720 | raise ValueError(("'%s' seem to be unreadable.") % target) from e |
|
3719 | raise ValueError(("'%s' seem to be unreadable.") % target) from e | |
3721 |
|
3720 | |||
3722 | potential_target = [target] |
|
3721 | potential_target = [target] | |
3723 | try : |
|
3722 | try : | |
3724 | potential_target.insert(0,get_py_filename(target)) |
|
3723 | potential_target.insert(0,get_py_filename(target)) | |
3725 | except IOError: |
|
3724 | except IOError: | |
3726 | pass |
|
3725 | pass | |
3727 |
|
3726 | |||
3728 | for tgt in potential_target : |
|
3727 | for tgt in potential_target : | |
3729 | if os.path.isfile(tgt): # Read file |
|
3728 | if os.path.isfile(tgt): # Read file | |
3730 | try : |
|
3729 | try : | |
3731 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3730 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) | |
3732 | except UnicodeDecodeError as e: |
|
3731 | except UnicodeDecodeError as e: | |
3733 | if not py_only : |
|
3732 | if not py_only : | |
3734 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3733 | with io_open(tgt,'r', encoding='latin1') as f : | |
3735 | return f.read() |
|
3734 | return f.read() | |
3736 | raise ValueError(("'%s' seem to be unreadable.") % target) from e |
|
3735 | raise ValueError(("'%s' seem to be unreadable.") % target) from e | |
3737 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3736 | elif os.path.isdir(os.path.expanduser(tgt)): | |
3738 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3737 | raise ValueError("'%s' is a directory, not a regular file." % target) | |
3739 |
|
3738 | |||
3740 | if search_ns: |
|
3739 | if search_ns: | |
3741 | # Inspect namespace to load object source |
|
3740 | # Inspect namespace to load object source | |
3742 | object_info = self.object_inspect(target, detail_level=1) |
|
3741 | object_info = self.object_inspect(target, detail_level=1) | |
3743 | if object_info['found'] and object_info['source']: |
|
3742 | if object_info['found'] and object_info['source']: | |
3744 | return object_info['source'] |
|
3743 | return object_info['source'] | |
3745 |
|
3744 | |||
3746 | try: # User namespace |
|
3745 | try: # User namespace | |
3747 | codeobj = eval(target, self.user_ns) |
|
3746 | codeobj = eval(target, self.user_ns) | |
3748 | except Exception as e: |
|
3747 | except Exception as e: | |
3749 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3748 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
3750 | "nor in the user namespace.") % target) from e |
|
3749 | "nor in the user namespace.") % target) from e | |
3751 |
|
3750 | |||
3752 | if isinstance(codeobj, str): |
|
3751 | if isinstance(codeobj, str): | |
3753 | return codeobj |
|
3752 | return codeobj | |
3754 | elif isinstance(codeobj, Macro): |
|
3753 | elif isinstance(codeobj, Macro): | |
3755 | return codeobj.value |
|
3754 | return codeobj.value | |
3756 |
|
3755 | |||
3757 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3756 | raise TypeError("%s is neither a string nor a macro." % target, | |
3758 | codeobj) |
|
3757 | codeobj) | |
3759 |
|
3758 | |||
3760 | def _atexit_once(self): |
|
3759 | def _atexit_once(self): | |
3761 | """ |
|
3760 | """ | |
3762 | At exist operation that need to be called at most once. |
|
3761 | At exist operation that need to be called at most once. | |
3763 | Second call to this function per instance will do nothing. |
|
3762 | Second call to this function per instance will do nothing. | |
3764 | """ |
|
3763 | """ | |
3765 |
|
3764 | |||
3766 | if not getattr(self, "_atexit_once_called", False): |
|
3765 | if not getattr(self, "_atexit_once_called", False): | |
3767 | self._atexit_once_called = True |
|
3766 | self._atexit_once_called = True | |
3768 | # Clear all user namespaces to release all references cleanly. |
|
3767 | # Clear all user namespaces to release all references cleanly. | |
3769 | self.reset(new_session=False) |
|
3768 | self.reset(new_session=False) | |
3770 | # Close the history session (this stores the end time and line count) |
|
3769 | # Close the history session (this stores the end time and line count) | |
3771 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3770 | # this must be *before* the tempfile cleanup, in case of temporary | |
3772 | # history db |
|
3771 | # history db | |
3773 | self.history_manager.end_session() |
|
3772 | self.history_manager.end_session() | |
3774 | self.history_manager = None |
|
3773 | self.history_manager = None | |
3775 |
|
3774 | |||
3776 | #------------------------------------------------------------------------- |
|
3775 | #------------------------------------------------------------------------- | |
3777 | # Things related to IPython exiting |
|
3776 | # Things related to IPython exiting | |
3778 | #------------------------------------------------------------------------- |
|
3777 | #------------------------------------------------------------------------- | |
3779 | def atexit_operations(self): |
|
3778 | def atexit_operations(self): | |
3780 | """This will be executed at the time of exit. |
|
3779 | """This will be executed at the time of exit. | |
3781 |
|
3780 | |||
3782 | Cleanup operations and saving of persistent data that is done |
|
3781 | Cleanup operations and saving of persistent data that is done | |
3783 | unconditionally by IPython should be performed here. |
|
3782 | unconditionally by IPython should be performed here. | |
3784 |
|
3783 | |||
3785 | For things that may depend on startup flags or platform specifics (such |
|
3784 | For things that may depend on startup flags or platform specifics (such | |
3786 | as having readline or not), register a separate atexit function in the |
|
3785 | as having readline or not), register a separate atexit function in the | |
3787 | code that has the appropriate information, rather than trying to |
|
3786 | code that has the appropriate information, rather than trying to | |
3788 | clutter |
|
3787 | clutter | |
3789 | """ |
|
3788 | """ | |
3790 | self._atexit_once() |
|
3789 | self._atexit_once() | |
3791 |
|
3790 | |||
3792 | # Cleanup all tempfiles and folders left around |
|
3791 | # Cleanup all tempfiles and folders left around | |
3793 | for tfile in self.tempfiles: |
|
3792 | for tfile in self.tempfiles: | |
3794 | try: |
|
3793 | try: | |
3795 | tfile.unlink() |
|
3794 | tfile.unlink() | |
3796 | self.tempfiles.remove(tfile) |
|
3795 | self.tempfiles.remove(tfile) | |
3797 | except FileNotFoundError: |
|
3796 | except FileNotFoundError: | |
3798 | pass |
|
3797 | pass | |
3799 | del self.tempfiles |
|
3798 | del self.tempfiles | |
3800 | for tdir in self.tempdirs: |
|
3799 | for tdir in self.tempdirs: | |
3801 | try: |
|
3800 | try: | |
3802 | tdir.rmdir() |
|
3801 | tdir.rmdir() | |
3803 | self.tempdirs.remove(tdir) |
|
3802 | self.tempdirs.remove(tdir) | |
3804 | except FileNotFoundError: |
|
3803 | except FileNotFoundError: | |
3805 | pass |
|
3804 | pass | |
3806 | del self.tempdirs |
|
3805 | del self.tempdirs | |
3807 |
|
3806 | |||
3808 | # Restore user's cursor |
|
3807 | # Restore user's cursor | |
3809 | if hasattr(self, "editing_mode") and self.editing_mode == "vi": |
|
3808 | if hasattr(self, "editing_mode") and self.editing_mode == "vi": | |
3810 | sys.stdout.write("\x1b[0 q") |
|
3809 | sys.stdout.write("\x1b[0 q") | |
3811 | sys.stdout.flush() |
|
3810 | sys.stdout.flush() | |
3812 |
|
3811 | |||
3813 | def cleanup(self): |
|
3812 | def cleanup(self): | |
3814 | self.restore_sys_module_state() |
|
3813 | self.restore_sys_module_state() | |
3815 |
|
3814 | |||
3816 |
|
3815 | |||
3817 | # Overridden in terminal subclass to change prompts |
|
3816 | # Overridden in terminal subclass to change prompts | |
3818 | def switch_doctest_mode(self, mode): |
|
3817 | def switch_doctest_mode(self, mode): | |
3819 | pass |
|
3818 | pass | |
3820 |
|
3819 | |||
3821 |
|
3820 | |||
3822 | class InteractiveShellABC(metaclass=abc.ABCMeta): |
|
3821 | class InteractiveShellABC(metaclass=abc.ABCMeta): | |
3823 | """An abstract base class for InteractiveShell.""" |
|
3822 | """An abstract base class for InteractiveShell.""" | |
3824 |
|
3823 | |||
3825 | InteractiveShellABC.register(InteractiveShell) |
|
3824 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,1203 +1,1201 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Verbose and colourful traceback formatting. |
|
3 | Verbose and colourful traceback formatting. | |
4 |
|
4 | |||
5 | **ColorTB** |
|
5 | **ColorTB** | |
6 |
|
6 | |||
7 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
7 | I've always found it a bit hard to visually parse tracebacks in Python. The | |
8 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
8 | ColorTB class is a solution to that problem. It colors the different parts of a | |
9 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
9 | traceback in a manner similar to what you would expect from a syntax-highlighting | |
10 | text editor. |
|
10 | text editor. | |
11 |
|
11 | |||
12 | Installation instructions for ColorTB:: |
|
12 | Installation instructions for ColorTB:: | |
13 |
|
13 | |||
14 | import sys,ultratb |
|
14 | import sys,ultratb | |
15 | sys.excepthook = ultratb.ColorTB() |
|
15 | sys.excepthook = ultratb.ColorTB() | |
16 |
|
16 | |||
17 | **VerboseTB** |
|
17 | **VerboseTB** | |
18 |
|
18 | |||
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds | |
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML | |
21 | and intended it for CGI programmers, but why should they have all the fun? I |
|
21 | and intended it for CGI programmers, but why should they have all the fun? I | |
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, | |
23 | but kind of neat, and maybe useful for long-running programs that you believe |
|
23 | but kind of neat, and maybe useful for long-running programs that you believe | |
24 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
24 | are bug-free. If a crash *does* occur in that type of program you want details. | |
25 | Give it a shot--you'll love it or you'll hate it. |
|
25 | Give it a shot--you'll love it or you'll hate it. | |
26 |
|
26 | |||
27 | .. note:: |
|
27 | .. note:: | |
28 |
|
28 | |||
29 | The Verbose mode prints the variables currently visible where the exception |
|
29 | The Verbose mode prints the variables currently visible where the exception | |
30 | happened (shortening their strings if too long). This can potentially be |
|
30 | happened (shortening their strings if too long). This can potentially be | |
31 | very slow, if you happen to have a huge data structure whose string |
|
31 | very slow, if you happen to have a huge data structure whose string | |
32 | representation is complex to compute. Your computer may appear to freeze for |
|
32 | representation is complex to compute. Your computer may appear to freeze for | |
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback | |
34 | with Ctrl-C (maybe hitting it more than once). |
|
34 | with Ctrl-C (maybe hitting it more than once). | |
35 |
|
35 | |||
36 | If you encounter this kind of situation often, you may want to use the |
|
36 | If you encounter this kind of situation often, you may want to use the | |
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting | |
38 | variables (but otherwise includes the information and context given by |
|
38 | variables (but otherwise includes the information and context given by | |
39 | Verbose). |
|
39 | Verbose). | |
40 |
|
40 | |||
41 | .. note:: |
|
41 | .. note:: | |
42 |
|
42 | |||
43 | The verbose mode print all variables in the stack, which means it can |
|
43 | The verbose mode print all variables in the stack, which means it can | |
44 | potentially leak sensitive information like access keys, or unencrypted |
|
44 | potentially leak sensitive information like access keys, or unencrypted | |
45 | password. |
|
45 | password. | |
46 |
|
46 | |||
47 | Installation instructions for VerboseTB:: |
|
47 | Installation instructions for VerboseTB:: | |
48 |
|
48 | |||
49 | import sys,ultratb |
|
49 | import sys,ultratb | |
50 | sys.excepthook = ultratb.VerboseTB() |
|
50 | sys.excepthook = ultratb.VerboseTB() | |
51 |
|
51 | |||
52 | Note: Much of the code in this module was lifted verbatim from the standard |
|
52 | Note: Much of the code in this module was lifted verbatim from the standard | |
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. | |
54 |
|
54 | |||
55 | Color schemes |
|
55 | Color schemes | |
56 | ------------- |
|
56 | ------------- | |
57 |
|
57 | |||
58 | The colors are defined in the class TBTools through the use of the |
|
58 | The colors are defined in the class TBTools through the use of the | |
59 | ColorSchemeTable class. Currently the following exist: |
|
59 | ColorSchemeTable class. Currently the following exist: | |
60 |
|
60 | |||
61 | - NoColor: allows all of this module to be used in any terminal (the color |
|
61 | - NoColor: allows all of this module to be used in any terminal (the color | |
62 | escapes are just dummy blank strings). |
|
62 | escapes are just dummy blank strings). | |
63 |
|
63 | |||
64 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
64 | - Linux: is meant to look good in a terminal like the Linux console (black | |
65 | or very dark background). |
|
65 | or very dark background). | |
66 |
|
66 | |||
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable | |
68 | in light background terminals. |
|
68 | in light background terminals. | |
69 |
|
69 | |||
70 | - Neutral: a neutral color scheme that should be readable on both light and |
|
70 | - Neutral: a neutral color scheme that should be readable on both light and | |
71 | dark background |
|
71 | dark background | |
72 |
|
72 | |||
73 | You can implement other color schemes easily, the syntax is fairly |
|
73 | You can implement other color schemes easily, the syntax is fairly | |
74 | self-explanatory. Please send back new schemes you develop to the author for |
|
74 | self-explanatory. Please send back new schemes you develop to the author for | |
75 | possible inclusion in future releases. |
|
75 | possible inclusion in future releases. | |
76 |
|
76 | |||
77 | Inheritance diagram: |
|
77 | Inheritance diagram: | |
78 |
|
78 | |||
79 | .. inheritance-diagram:: IPython.core.ultratb |
|
79 | .. inheritance-diagram:: IPython.core.ultratb | |
80 | :parts: 3 |
|
80 | :parts: 3 | |
81 | """ |
|
81 | """ | |
82 |
|
82 | |||
83 | #***************************************************************************** |
|
83 | #***************************************************************************** | |
84 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
84 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
85 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
85 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
86 | # |
|
86 | # | |
87 | # Distributed under the terms of the BSD License. The full license is in |
|
87 | # Distributed under the terms of the BSD License. The full license is in | |
88 | # the file COPYING, distributed as part of this software. |
|
88 | # the file COPYING, distributed as part of this software. | |
89 | #***************************************************************************** |
|
89 | #***************************************************************************** | |
90 |
|
90 | |||
91 |
|
91 | |||
92 | import inspect |
|
92 | import inspect | |
93 | import linecache |
|
93 | import linecache | |
94 | import pydoc |
|
94 | import pydoc | |
95 | import sys |
|
95 | import sys | |
96 | import time |
|
96 | import time | |
97 | import traceback |
|
97 | import traceback | |
98 | from types import TracebackType |
|
98 | from types import TracebackType | |
99 | from typing import Tuple, List, Any, Optional |
|
99 | from typing import Tuple, List, Any, Optional | |
100 |
|
100 | |||
101 | import stack_data |
|
101 | import stack_data | |
102 | from pygments.formatters.terminal256 import Terminal256Formatter |
|
102 | from pygments.formatters.terminal256 import Terminal256Formatter | |
103 | from pygments.styles import get_style_by_name |
|
103 | from pygments.styles import get_style_by_name | |
104 |
|
104 | |||
105 | # IPython's own modules |
|
105 | # IPython's own modules | |
106 | from IPython import get_ipython |
|
106 | from IPython import get_ipython | |
107 | from IPython.core import debugger |
|
107 | from IPython.core import debugger | |
108 | from IPython.core.display_trap import DisplayTrap |
|
108 | from IPython.core.display_trap import DisplayTrap | |
109 | from IPython.core.excolors import exception_colors |
|
109 | from IPython.core.excolors import exception_colors | |
110 | from IPython.utils import path as util_path |
|
110 | from IPython.utils import path as util_path | |
111 | from IPython.utils import py3compat |
|
111 | from IPython.utils import py3compat | |
112 | from IPython.utils.terminal import get_terminal_size |
|
112 | from IPython.utils.terminal import get_terminal_size | |
113 |
|
113 | |||
114 | import IPython.utils.colorable as colorable |
|
114 | import IPython.utils.colorable as colorable | |
115 |
|
115 | |||
116 | # Globals |
|
116 | # Globals | |
117 | # amount of space to put line numbers before verbose tracebacks |
|
117 | # amount of space to put line numbers before verbose tracebacks | |
118 | INDENT_SIZE = 8 |
|
118 | INDENT_SIZE = 8 | |
119 |
|
119 | |||
120 | # Default color scheme. This is used, for example, by the traceback |
|
120 | # Default color scheme. This is used, for example, by the traceback | |
121 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
121 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
122 | # value is used, but having a module global makes this functionality available |
|
122 | # value is used, but having a module global makes this functionality available | |
123 | # to users of ultratb who are NOT running inside ipython. |
|
123 | # to users of ultratb who are NOT running inside ipython. | |
124 | DEFAULT_SCHEME = 'NoColor' |
|
124 | DEFAULT_SCHEME = 'NoColor' | |
125 |
|
125 | |||
126 | # --------------------------------------------------------------------------- |
|
126 | # --------------------------------------------------------------------------- | |
127 | # Code begins |
|
127 | # Code begins | |
128 |
|
128 | |||
129 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
129 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
130 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
130 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
131 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
131 | # can be recognized properly by ipython.el's py-traceback-line-re | |
132 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
132 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
133 |
|
133 | |||
134 |
|
134 | |||
135 | def _format_traceback_lines(lines, Colors, has_colors: bool, lvals): |
|
135 | def _format_traceback_lines(lines, Colors, has_colors: bool, lvals): | |
136 | """ |
|
136 | """ | |
137 | Format tracebacks lines with pointing arrow, leading numbers... |
|
137 | Format tracebacks lines with pointing arrow, leading numbers... | |
138 |
|
138 | |||
139 | Parameters |
|
139 | Parameters | |
140 | ---------- |
|
140 | ---------- | |
141 | lines : list[Line] |
|
141 | lines : list[Line] | |
142 | Colors |
|
142 | Colors | |
143 | ColorScheme used. |
|
143 | ColorScheme used. | |
144 | lvals : str |
|
144 | lvals : str | |
145 | Values of local variables, already colored, to inject just after the error line. |
|
145 | Values of local variables, already colored, to inject just after the error line. | |
146 | """ |
|
146 | """ | |
147 | numbers_width = INDENT_SIZE - 1 |
|
147 | numbers_width = INDENT_SIZE - 1 | |
148 | res = [] |
|
148 | res = [] | |
149 |
|
149 | |||
150 | for stack_line in lines: |
|
150 | for stack_line in lines: | |
151 | if stack_line is stack_data.LINE_GAP: |
|
151 | if stack_line is stack_data.LINE_GAP: | |
152 | res.append('%s (...)%s\n' % (Colors.linenoEm, Colors.Normal)) |
|
152 | res.append('%s (...)%s\n' % (Colors.linenoEm, Colors.Normal)) | |
153 | continue |
|
153 | continue | |
154 |
|
154 | |||
155 | line = stack_line.render(pygmented=has_colors).rstrip('\n') + '\n' |
|
155 | line = stack_line.render(pygmented=has_colors).rstrip('\n') + '\n' | |
156 | lineno = stack_line.lineno |
|
156 | lineno = stack_line.lineno | |
157 | if stack_line.is_current: |
|
157 | if stack_line.is_current: | |
158 | # This is the line with the error |
|
158 | # This is the line with the error | |
159 | pad = numbers_width - len(str(lineno)) |
|
159 | pad = numbers_width - len(str(lineno)) | |
160 | num = '%s%s' % (debugger.make_arrow(pad), str(lineno)) |
|
160 | num = '%s%s' % (debugger.make_arrow(pad), str(lineno)) | |
161 | start_color = Colors.linenoEm |
|
161 | start_color = Colors.linenoEm | |
162 | else: |
|
162 | else: | |
163 | num = '%*s' % (numbers_width, lineno) |
|
163 | num = '%*s' % (numbers_width, lineno) | |
164 | start_color = Colors.lineno |
|
164 | start_color = Colors.lineno | |
165 |
|
165 | |||
166 | line = '%s%s%s %s' % (start_color, num, Colors.Normal, line) |
|
166 | line = '%s%s%s %s' % (start_color, num, Colors.Normal, line) | |
167 |
|
167 | |||
168 | res.append(line) |
|
168 | res.append(line) | |
169 | if lvals and stack_line.is_current: |
|
169 | if lvals and stack_line.is_current: | |
170 | res.append(lvals + '\n') |
|
170 | res.append(lvals + '\n') | |
171 | return res |
|
171 | return res | |
172 |
|
172 | |||
173 |
|
173 | |||
174 | def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None): |
|
174 | def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None): | |
175 | """ |
|
175 | """ | |
176 | Format filename lines with `In [n]` if it's the nth code cell or `File *.py` if it's a module. |
|
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 | Parameters |
|
178 | Parameters | |
179 | ---------- |
|
179 | ---------- | |
180 | file : str |
|
180 | file : str | |
181 | ColorFilename |
|
181 | ColorFilename | |
182 | ColorScheme's filename coloring to be used. |
|
182 | ColorScheme's filename coloring to be used. | |
183 | ColorNormal |
|
183 | ColorNormal | |
184 | ColorScheme's normal coloring to be used. |
|
184 | ColorScheme's normal coloring to be used. | |
185 | """ |
|
185 | """ | |
186 | ipinst = get_ipython() |
|
186 | ipinst = get_ipython() | |
187 |
|
187 | |||
188 | if ipinst is not None and file in ipinst.compile._filename_map: |
|
188 | if ipinst is not None and file in ipinst.compile._filename_map: | |
189 | file = "[%s]" % ipinst.compile._filename_map[file] |
|
189 | file = "[%s]" % ipinst.compile._filename_map[file] | |
190 | if lineno is None: |
|
190 | if lineno is None: | |
191 | tpl_link = f"Cell {ColorFilename}In {{file}}{ColorNormal}" |
|
191 | tpl_link = f"Cell {ColorFilename}In {{file}}{ColorNormal}" | |
192 | else: |
|
192 | else: | |
193 | tpl_link = f"Cell {ColorFilename}In {{file}}, line {{lineno}}{ColorNormal}" |
|
193 | tpl_link = f"Cell {ColorFilename}In {{file}}, line {{lineno}}{ColorNormal}" | |
194 | else: |
|
194 | else: | |
195 | file = util_path.compress_user( |
|
195 | file = util_path.compress_user( | |
196 | py3compat.cast_unicode(file, util_path.fs_encoding) |
|
196 | py3compat.cast_unicode(file, util_path.fs_encoding) | |
197 | ) |
|
197 | ) | |
198 | if lineno is None: |
|
198 | if lineno is None: | |
199 | tpl_link = f"File {ColorFilename}{{file}}{ColorNormal}" |
|
199 | tpl_link = f"File {ColorFilename}{{file}}{ColorNormal}" | |
200 | else: |
|
200 | else: | |
201 | tpl_link = f"File {ColorFilename}{{file}}:{{lineno}}{ColorNormal}" |
|
201 | tpl_link = f"File {ColorFilename}{{file}}:{{lineno}}{ColorNormal}" | |
202 |
|
202 | |||
203 | return tpl_link.format(file=file, lineno=lineno) |
|
203 | return tpl_link.format(file=file, lineno=lineno) | |
204 |
|
204 | |||
205 | #--------------------------------------------------------------------------- |
|
205 | #--------------------------------------------------------------------------- | |
206 | # Module classes |
|
206 | # Module classes | |
207 | class TBTools(colorable.Colorable): |
|
207 | class TBTools(colorable.Colorable): | |
208 | """Basic tools used by all traceback printer classes.""" |
|
208 | """Basic tools used by all traceback printer classes.""" | |
209 |
|
209 | |||
210 | # Number of frames to skip when reporting tracebacks |
|
210 | # Number of frames to skip when reporting tracebacks | |
211 | tb_offset = 0 |
|
211 | tb_offset = 0 | |
212 |
|
212 | |||
213 | def __init__( |
|
213 | def __init__( | |
214 | self, |
|
214 | self, | |
215 | color_scheme="NoColor", |
|
215 | color_scheme="NoColor", | |
216 | call_pdb=False, |
|
216 | call_pdb=False, | |
217 | ostream=None, |
|
217 | ostream=None, | |
218 | parent=None, |
|
218 | parent=None, | |
219 | config=None, |
|
219 | config=None, | |
220 | *, |
|
220 | *, | |
221 | debugger_cls=None, |
|
221 | debugger_cls=None, | |
222 | ): |
|
222 | ): | |
223 | # Whether to call the interactive pdb debugger after printing |
|
223 | # Whether to call the interactive pdb debugger after printing | |
224 | # tracebacks or not |
|
224 | # tracebacks or not | |
225 | super(TBTools, self).__init__(parent=parent, config=config) |
|
225 | super(TBTools, self).__init__(parent=parent, config=config) | |
226 | self.call_pdb = call_pdb |
|
226 | self.call_pdb = call_pdb | |
227 |
|
227 | |||
228 | # Output stream to write to. Note that we store the original value in |
|
228 | # Output stream to write to. Note that we store the original value in | |
229 | # a private attribute and then make the public ostream a property, so |
|
229 | # a private attribute and then make the public ostream a property, so | |
230 | # that we can delay accessing sys.stdout until runtime. The way |
|
230 | # that we can delay accessing sys.stdout until runtime. The way | |
231 | # things are written now, the sys.stdout object is dynamically managed |
|
231 | # things are written now, the sys.stdout object is dynamically managed | |
232 | # so a reference to it should NEVER be stored statically. This |
|
232 | # so a reference to it should NEVER be stored statically. This | |
233 | # property approach confines this detail to a single location, and all |
|
233 | # property approach confines this detail to a single location, and all | |
234 | # subclasses can simply access self.ostream for writing. |
|
234 | # subclasses can simply access self.ostream for writing. | |
235 | self._ostream = ostream |
|
235 | self._ostream = ostream | |
236 |
|
236 | |||
237 | # Create color table |
|
237 | # Create color table | |
238 | self.color_scheme_table = exception_colors() |
|
238 | self.color_scheme_table = exception_colors() | |
239 |
|
239 | |||
240 | self.set_colors(color_scheme) |
|
240 | self.set_colors(color_scheme) | |
241 | self.old_scheme = color_scheme # save initial value for toggles |
|
241 | self.old_scheme = color_scheme # save initial value for toggles | |
242 | self.debugger_cls = debugger_cls or debugger.Pdb |
|
242 | self.debugger_cls = debugger_cls or debugger.Pdb | |
243 |
|
243 | |||
244 | if call_pdb: |
|
244 | if call_pdb: | |
245 | self.pdb = self.debugger_cls() |
|
245 | self.pdb = self.debugger_cls() | |
246 | else: |
|
246 | else: | |
247 | self.pdb = None |
|
247 | self.pdb = None | |
248 |
|
248 | |||
249 | def _get_ostream(self): |
|
249 | def _get_ostream(self): | |
250 | """Output stream that exceptions are written to. |
|
250 | """Output stream that exceptions are written to. | |
251 |
|
251 | |||
252 | Valid values are: |
|
252 | Valid values are: | |
253 |
|
253 | |||
254 | - None: the default, which means that IPython will dynamically resolve |
|
254 | - None: the default, which means that IPython will dynamically resolve | |
255 | to sys.stdout. This ensures compatibility with most tools, including |
|
255 | to sys.stdout. This ensures compatibility with most tools, including | |
256 | Windows (where plain stdout doesn't recognize ANSI escapes). |
|
256 | Windows (where plain stdout doesn't recognize ANSI escapes). | |
257 |
|
257 | |||
258 | - Any object with 'write' and 'flush' attributes. |
|
258 | - Any object with 'write' and 'flush' attributes. | |
259 | """ |
|
259 | """ | |
260 | return sys.stdout if self._ostream is None else self._ostream |
|
260 | return sys.stdout if self._ostream is None else self._ostream | |
261 |
|
261 | |||
262 | def _set_ostream(self, val): |
|
262 | def _set_ostream(self, val): | |
263 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) |
|
263 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) | |
264 | self._ostream = val |
|
264 | self._ostream = val | |
265 |
|
265 | |||
266 | ostream = property(_get_ostream, _set_ostream) |
|
266 | ostream = property(_get_ostream, _set_ostream) | |
267 |
|
267 | |||
268 | @staticmethod |
|
268 | @staticmethod | |
269 | def _get_chained_exception(exception_value): |
|
269 | def _get_chained_exception(exception_value): | |
270 | cause = getattr(exception_value, "__cause__", None) |
|
270 | cause = getattr(exception_value, "__cause__", None) | |
271 | if cause: |
|
271 | if cause: | |
272 | return cause |
|
272 | return cause | |
273 | if getattr(exception_value, "__suppress_context__", False): |
|
273 | if getattr(exception_value, "__suppress_context__", False): | |
274 | return None |
|
274 | return None | |
275 | return getattr(exception_value, "__context__", None) |
|
275 | return getattr(exception_value, "__context__", None) | |
276 |
|
276 | |||
277 | def get_parts_of_chained_exception( |
|
277 | def get_parts_of_chained_exception( | |
278 | self, evalue |
|
278 | self, evalue | |
279 | ) -> Optional[Tuple[type, BaseException, TracebackType]]: |
|
279 | ) -> Optional[Tuple[type, BaseException, TracebackType]]: | |
280 |
|
280 | |||
281 | chained_evalue = self._get_chained_exception(evalue) |
|
281 | chained_evalue = self._get_chained_exception(evalue) | |
282 |
|
282 | |||
283 | if chained_evalue: |
|
283 | if chained_evalue: | |
284 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ |
|
284 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ | |
285 | return None |
|
285 | return None | |
286 |
|
286 | |||
287 | def prepare_chained_exception_message(self, cause) -> List[Any]: |
|
287 | def prepare_chained_exception_message(self, cause) -> List[Any]: | |
288 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" |
|
288 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" | |
289 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" |
|
289 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" | |
290 |
|
290 | |||
291 | if cause: |
|
291 | if cause: | |
292 | message = [[direct_cause]] |
|
292 | message = [[direct_cause]] | |
293 | else: |
|
293 | else: | |
294 | message = [[exception_during_handling]] |
|
294 | message = [[exception_during_handling]] | |
295 | return message |
|
295 | return message | |
296 |
|
296 | |||
297 | @property |
|
297 | @property | |
298 | def has_colors(self) -> bool: |
|
298 | def has_colors(self) -> bool: | |
299 | return self.color_scheme_table.active_scheme_name.lower() != "nocolor" |
|
299 | return self.color_scheme_table.active_scheme_name.lower() != "nocolor" | |
300 |
|
300 | |||
301 | def set_colors(self, *args, **kw): |
|
301 | def set_colors(self, *args, **kw): | |
302 | """Shorthand access to the color table scheme selector method.""" |
|
302 | """Shorthand access to the color table scheme selector method.""" | |
303 |
|
303 | |||
304 | # Set own color table |
|
304 | # Set own color table | |
305 | self.color_scheme_table.set_active_scheme(*args, **kw) |
|
305 | self.color_scheme_table.set_active_scheme(*args, **kw) | |
306 | # for convenience, set Colors to the active scheme |
|
306 | # for convenience, set Colors to the active scheme | |
307 | self.Colors = self.color_scheme_table.active_colors |
|
307 | self.Colors = self.color_scheme_table.active_colors | |
308 | # Also set colors of debugger |
|
308 | # Also set colors of debugger | |
309 | if hasattr(self, 'pdb') and self.pdb is not None: |
|
309 | if hasattr(self, 'pdb') and self.pdb is not None: | |
310 | self.pdb.set_colors(*args, **kw) |
|
310 | self.pdb.set_colors(*args, **kw) | |
311 |
|
311 | |||
312 | def color_toggle(self): |
|
312 | def color_toggle(self): | |
313 | """Toggle between the currently active color scheme and NoColor.""" |
|
313 | """Toggle between the currently active color scheme and NoColor.""" | |
314 |
|
314 | |||
315 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
315 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
316 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
316 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
317 | self.Colors = self.color_scheme_table.active_colors |
|
317 | self.Colors = self.color_scheme_table.active_colors | |
318 | else: |
|
318 | else: | |
319 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
319 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
320 | self.color_scheme_table.set_active_scheme('NoColor') |
|
320 | self.color_scheme_table.set_active_scheme('NoColor') | |
321 | self.Colors = self.color_scheme_table.active_colors |
|
321 | self.Colors = self.color_scheme_table.active_colors | |
322 |
|
322 | |||
323 | def stb2text(self, stb): |
|
323 | def stb2text(self, stb): | |
324 | """Convert a structured traceback (a list) to a string.""" |
|
324 | """Convert a structured traceback (a list) to a string.""" | |
325 | return '\n'.join(stb) |
|
325 | return '\n'.join(stb) | |
326 |
|
326 | |||
327 | def text(self, etype, value, tb, tb_offset: Optional[int] = None, context=5): |
|
327 | def text(self, etype, value, tb, tb_offset: Optional[int] = None, context=5): | |
328 | """Return formatted traceback. |
|
328 | """Return formatted traceback. | |
329 |
|
329 | |||
330 | Subclasses may override this if they add extra arguments. |
|
330 | Subclasses may override this if they add extra arguments. | |
331 | """ |
|
331 | """ | |
332 | tb_list = self.structured_traceback(etype, value, tb, |
|
332 | tb_list = self.structured_traceback(etype, value, tb, | |
333 | tb_offset, context) |
|
333 | tb_offset, context) | |
334 | return self.stb2text(tb_list) |
|
334 | return self.stb2text(tb_list) | |
335 |
|
335 | |||
336 | def structured_traceback( |
|
336 | def structured_traceback( | |
337 | self, etype, evalue, tb, tb_offset: Optional[int] = None, context=5, mode=None |
|
337 | self, etype, evalue, tb, tb_offset: Optional[int] = None, context=5, mode=None | |
338 | ): |
|
338 | ): | |
339 | """Return a list of traceback frames. |
|
339 | """Return a list of traceback frames. | |
340 |
|
340 | |||
341 | Must be implemented by each class. |
|
341 | Must be implemented by each class. | |
342 | """ |
|
342 | """ | |
343 | raise NotImplementedError() |
|
343 | raise NotImplementedError() | |
344 |
|
344 | |||
345 |
|
345 | |||
346 | #--------------------------------------------------------------------------- |
|
346 | #--------------------------------------------------------------------------- | |
347 | class ListTB(TBTools): |
|
347 | class ListTB(TBTools): | |
348 | """Print traceback information from a traceback list, with optional color. |
|
348 | """Print traceback information from a traceback list, with optional color. | |
349 |
|
349 | |||
350 | Calling requires 3 arguments: (etype, evalue, elist) |
|
350 | Calling requires 3 arguments: (etype, evalue, elist) | |
351 | as would be obtained by:: |
|
351 | as would be obtained by:: | |
352 |
|
352 | |||
353 | etype, evalue, tb = sys.exc_info() |
|
353 | etype, evalue, tb = sys.exc_info() | |
354 | if tb: |
|
354 | if tb: | |
355 | elist = traceback.extract_tb(tb) |
|
355 | elist = traceback.extract_tb(tb) | |
356 | else: |
|
356 | else: | |
357 | elist = None |
|
357 | elist = None | |
358 |
|
358 | |||
359 | It can thus be used by programs which need to process the traceback before |
|
359 | It can thus be used by programs which need to process the traceback before | |
360 | printing (such as console replacements based on the code module from the |
|
360 | printing (such as console replacements based on the code module from the | |
361 | standard library). |
|
361 | standard library). | |
362 |
|
362 | |||
363 | Because they are meant to be called without a full traceback (only a |
|
363 | Because they are meant to be called without a full traceback (only a | |
364 | list), instances of this class can't call the interactive pdb debugger.""" |
|
364 | list), instances of this class can't call the interactive pdb debugger.""" | |
365 |
|
365 | |||
366 |
|
366 | |||
367 | def __call__(self, etype, value, elist): |
|
367 | def __call__(self, etype, value, elist): | |
368 | self.ostream.flush() |
|
368 | self.ostream.flush() | |
369 | self.ostream.write(self.text(etype, value, elist)) |
|
369 | self.ostream.write(self.text(etype, value, elist)) | |
370 | self.ostream.write('\n') |
|
370 | self.ostream.write('\n') | |
371 |
|
371 | |||
372 | def _extract_tb(self, tb): |
|
372 | def _extract_tb(self, tb): | |
373 | if tb: |
|
373 | if tb: | |
374 | return traceback.extract_tb(tb) |
|
374 | return traceback.extract_tb(tb) | |
375 | else: |
|
375 | else: | |
376 | return None |
|
376 | return None | |
377 |
|
377 | |||
378 | def structured_traceback( |
|
378 | def structured_traceback( | |
379 | self, |
|
379 | self, | |
380 | etype: type, |
|
380 | etype: type, | |
381 | evalue: BaseException, |
|
381 | evalue: BaseException, | |
382 | etb: Optional[TracebackType] = None, |
|
382 | etb: Optional[TracebackType] = None, | |
383 | tb_offset: Optional[int] = None, |
|
383 | tb_offset: Optional[int] = None, | |
384 | context=5, |
|
384 | context=5, | |
385 | ): |
|
385 | ): | |
386 | """Return a color formatted string with the traceback info. |
|
386 | """Return a color formatted string with the traceback info. | |
387 |
|
387 | |||
388 | Parameters |
|
388 | Parameters | |
389 | ---------- |
|
389 | ---------- | |
390 | etype : exception type |
|
390 | etype : exception type | |
391 | Type of the exception raised. |
|
391 | Type of the exception raised. | |
392 | evalue : object |
|
392 | evalue : object | |
393 | Data stored in the exception |
|
393 | Data stored in the exception | |
394 | etb : list | TracebackType | None |
|
394 | etb : list | TracebackType | None | |
395 | If list: List of frames, see class docstring for details. |
|
395 | If list: List of frames, see class docstring for details. | |
396 | If Traceback: Traceback of the exception. |
|
396 | If Traceback: Traceback of the exception. | |
397 | tb_offset : int, optional |
|
397 | tb_offset : int, optional | |
398 | Number of frames in the traceback to skip. If not given, the |
|
398 | Number of frames in the traceback to skip. If not given, the | |
399 | instance evalue is used (set in constructor). |
|
399 | instance evalue is used (set in constructor). | |
400 | context : int, optional |
|
400 | context : int, optional | |
401 | Number of lines of context information to print. |
|
401 | Number of lines of context information to print. | |
402 |
|
402 | |||
403 | Returns |
|
403 | Returns | |
404 | ------- |
|
404 | ------- | |
405 | String with formatted exception. |
|
405 | String with formatted exception. | |
406 | """ |
|
406 | """ | |
407 | # This is a workaround to get chained_exc_ids in recursive calls |
|
407 | # This is a workaround to get chained_exc_ids in recursive calls | |
408 | # etb should not be a tuple if structured_traceback is not recursive |
|
408 | # etb should not be a tuple if structured_traceback is not recursive | |
409 | if isinstance(etb, tuple): |
|
409 | if isinstance(etb, tuple): | |
410 | etb, chained_exc_ids = etb |
|
410 | etb, chained_exc_ids = etb | |
411 | else: |
|
411 | else: | |
412 | chained_exc_ids = set() |
|
412 | chained_exc_ids = set() | |
413 |
|
413 | |||
414 | if isinstance(etb, list): |
|
414 | if isinstance(etb, list): | |
415 | elist = etb |
|
415 | elist = etb | |
416 | elif etb is not None: |
|
416 | elif etb is not None: | |
417 | elist = self._extract_tb(etb) |
|
417 | elist = self._extract_tb(etb) | |
418 | else: |
|
418 | else: | |
419 | elist = [] |
|
419 | elist = [] | |
420 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
420 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
421 | assert isinstance(tb_offset, int) |
|
421 | assert isinstance(tb_offset, int) | |
422 | Colors = self.Colors |
|
422 | Colors = self.Colors | |
423 | out_list = [] |
|
423 | out_list = [] | |
424 | if elist: |
|
424 | if elist: | |
425 |
|
425 | |||
426 | if tb_offset and len(elist) > tb_offset: |
|
426 | if tb_offset and len(elist) > tb_offset: | |
427 | elist = elist[tb_offset:] |
|
427 | elist = elist[tb_offset:] | |
428 |
|
428 | |||
429 | out_list.append('Traceback %s(most recent call last)%s:' % |
|
429 | out_list.append('Traceback %s(most recent call last)%s:' % | |
430 | (Colors.normalEm, Colors.Normal) + '\n') |
|
430 | (Colors.normalEm, Colors.Normal) + '\n') | |
431 | out_list.extend(self._format_list(elist)) |
|
431 | out_list.extend(self._format_list(elist)) | |
432 | # The exception info should be a single entry in the list. |
|
432 | # The exception info should be a single entry in the list. | |
433 | lines = ''.join(self._format_exception_only(etype, evalue)) |
|
433 | lines = ''.join(self._format_exception_only(etype, evalue)) | |
434 | out_list.append(lines) |
|
434 | out_list.append(lines) | |
435 |
|
435 | |||
436 | exception = self.get_parts_of_chained_exception(evalue) |
|
436 | exception = self.get_parts_of_chained_exception(evalue) | |
437 |
|
437 | |||
438 | if exception and not id(exception[1]) in chained_exc_ids: |
|
438 | if exception and not id(exception[1]) in chained_exc_ids: | |
439 | chained_exception_message = self.prepare_chained_exception_message( |
|
439 | chained_exception_message = self.prepare_chained_exception_message( | |
440 | evalue.__cause__)[0] |
|
440 | evalue.__cause__)[0] | |
441 | etype, evalue, etb = exception |
|
441 | etype, evalue, etb = exception | |
442 | # Trace exception to avoid infinite 'cause' loop |
|
442 | # Trace exception to avoid infinite 'cause' loop | |
443 | chained_exc_ids.add(id(exception[1])) |
|
443 | chained_exc_ids.add(id(exception[1])) | |
444 | chained_exceptions_tb_offset = 0 |
|
444 | chained_exceptions_tb_offset = 0 | |
445 | out_list = ( |
|
445 | out_list = ( | |
446 | self.structured_traceback( |
|
446 | self.structured_traceback( | |
447 | etype, evalue, (etb, chained_exc_ids), |
|
447 | etype, evalue, (etb, chained_exc_ids), | |
448 | chained_exceptions_tb_offset, context) |
|
448 | chained_exceptions_tb_offset, context) | |
449 | + chained_exception_message |
|
449 | + chained_exception_message | |
450 | + out_list) |
|
450 | + out_list) | |
451 |
|
451 | |||
452 | return out_list |
|
452 | return out_list | |
453 |
|
453 | |||
454 | def _format_list(self, extracted_list): |
|
454 | def _format_list(self, extracted_list): | |
455 | """Format a list of traceback entry tuples for printing. |
|
455 | """Format a list of traceback entry tuples for printing. | |
456 |
|
456 | |||
457 | Given a list of tuples as returned by extract_tb() or |
|
457 | Given a list of tuples as returned by extract_tb() or | |
458 | extract_stack(), return a list of strings ready for printing. |
|
458 | extract_stack(), return a list of strings ready for printing. | |
459 | Each string in the resulting list corresponds to the item with the |
|
459 | Each string in the resulting list corresponds to the item with the | |
460 | same index in the argument list. Each string ends in a newline; |
|
460 | same index in the argument list. Each string ends in a newline; | |
461 | the strings may contain internal newlines as well, for those items |
|
461 | the strings may contain internal newlines as well, for those items | |
462 | whose source text line is not None. |
|
462 | whose source text line is not None. | |
463 |
|
463 | |||
464 | Lifted almost verbatim from traceback.py |
|
464 | Lifted almost verbatim from traceback.py | |
465 | """ |
|
465 | """ | |
466 |
|
466 | |||
467 | Colors = self.Colors |
|
467 | Colors = self.Colors | |
468 | list = [] |
|
468 | list = [] | |
469 | for ind, (filename, lineno, name, line) in enumerate(extracted_list): |
|
469 | for ind, (filename, lineno, name, line) in enumerate(extracted_list): | |
470 | normalCol, nameCol, fileCol, lineCol = ( |
|
470 | normalCol, nameCol, fileCol, lineCol = ( | |
471 | # Emphasize the last entry |
|
471 | # Emphasize the last entry | |
472 | (Colors.normalEm, Colors.nameEm, Colors.filenameEm, Colors.line) |
|
472 | (Colors.normalEm, Colors.nameEm, Colors.filenameEm, Colors.line) | |
473 | if ind == len(extracted_list) - 1 |
|
473 | if ind == len(extracted_list) - 1 | |
474 | else (Colors.Normal, Colors.name, Colors.filename, "") |
|
474 | else (Colors.Normal, Colors.name, Colors.filename, "") | |
475 | ) |
|
475 | ) | |
476 |
|
476 | |||
477 | fns = _format_filename(filename, fileCol, normalCol, lineno=lineno) |
|
477 | fns = _format_filename(filename, fileCol, normalCol, lineno=lineno) | |
478 | item = f"{normalCol} {fns}" |
|
478 | item = f"{normalCol} {fns}" | |
479 |
|
479 | |||
480 | if name != "<module>": |
|
480 | if name != "<module>": | |
481 | item += f" in {nameCol}{name}{normalCol}\n" |
|
481 | item += f" in {nameCol}{name}{normalCol}\n" | |
482 | else: |
|
482 | else: | |
483 | item += "\n" |
|
483 | item += "\n" | |
484 | if line: |
|
484 | if line: | |
485 | item += f"{lineCol} {line.strip()}{normalCol}\n" |
|
485 | item += f"{lineCol} {line.strip()}{normalCol}\n" | |
486 | list.append(item) |
|
486 | list.append(item) | |
487 |
|
487 | |||
488 | return list |
|
488 | return list | |
489 |
|
489 | |||
490 | def _format_exception_only(self, etype, value): |
|
490 | def _format_exception_only(self, etype, value): | |
491 | """Format the exception part of a traceback. |
|
491 | """Format the exception part of a traceback. | |
492 |
|
492 | |||
493 | The arguments are the exception type and value such as given by |
|
493 | The arguments are the exception type and value such as given by | |
494 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
494 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
495 | in a newline. Normally, the list contains a single string; however, |
|
495 | in a newline. Normally, the list contains a single string; however, | |
496 | for SyntaxError exceptions, it contains several lines that (when |
|
496 | for SyntaxError exceptions, it contains several lines that (when | |
497 | printed) display detailed information about where the syntax error |
|
497 | printed) display detailed information about where the syntax error | |
498 | occurred. The message indicating which exception occurred is the |
|
498 | occurred. The message indicating which exception occurred is the | |
499 | always last string in the list. |
|
499 | always last string in the list. | |
500 |
|
500 | |||
501 | Also lifted nearly verbatim from traceback.py |
|
501 | Also lifted nearly verbatim from traceback.py | |
502 | """ |
|
502 | """ | |
503 | have_filedata = False |
|
503 | have_filedata = False | |
504 | Colors = self.Colors |
|
504 | Colors = self.Colors | |
505 | list = [] |
|
505 | list = [] | |
506 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) |
|
506 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) | |
507 | if value is None: |
|
507 | if value is None: | |
508 | # Not sure if this can still happen in Python 2.6 and above |
|
508 | # Not sure if this can still happen in Python 2.6 and above | |
509 | list.append(stype + '\n') |
|
509 | list.append(stype + '\n') | |
510 | else: |
|
510 | else: | |
511 | if issubclass(etype, SyntaxError): |
|
511 | if issubclass(etype, SyntaxError): | |
512 | have_filedata = True |
|
512 | have_filedata = True | |
513 | if not value.filename: value.filename = "<string>" |
|
513 | if not value.filename: value.filename = "<string>" | |
514 | if value.lineno: |
|
514 | if value.lineno: | |
515 | lineno = value.lineno |
|
515 | lineno = value.lineno | |
516 | textline = linecache.getline(value.filename, value.lineno) |
|
516 | textline = linecache.getline(value.filename, value.lineno) | |
517 | else: |
|
517 | else: | |
518 | lineno = "unknown" |
|
518 | lineno = "unknown" | |
519 | textline = "" |
|
519 | textline = "" | |
520 | list.append( |
|
520 | list.append( | |
521 | "%s %s%s\n" |
|
521 | "%s %s%s\n" | |
522 | % ( |
|
522 | % ( | |
523 | Colors.normalEm, |
|
523 | Colors.normalEm, | |
524 | _format_filename( |
|
524 | _format_filename( | |
525 | value.filename, |
|
525 | value.filename, | |
526 | Colors.filenameEm, |
|
526 | Colors.filenameEm, | |
527 | Colors.normalEm, |
|
527 | Colors.normalEm, | |
528 | lineno=(None if lineno == "unknown" else lineno), |
|
528 | lineno=(None if lineno == "unknown" else lineno), | |
529 | ), |
|
529 | ), | |
530 | Colors.Normal, |
|
530 | Colors.Normal, | |
531 | ) |
|
531 | ) | |
532 | ) |
|
532 | ) | |
533 | if textline == "": |
|
533 | if textline == "": | |
534 | textline = py3compat.cast_unicode(value.text, "utf-8") |
|
534 | textline = py3compat.cast_unicode(value.text, "utf-8") | |
535 |
|
535 | |||
536 | if textline is not None: |
|
536 | if textline is not None: | |
537 | i = 0 |
|
537 | i = 0 | |
538 | while i < len(textline) and textline[i].isspace(): |
|
538 | while i < len(textline) and textline[i].isspace(): | |
539 | i += 1 |
|
539 | i += 1 | |
540 | list.append('%s %s%s\n' % (Colors.line, |
|
540 | list.append('%s %s%s\n' % (Colors.line, | |
541 | textline.strip(), |
|
541 | textline.strip(), | |
542 | Colors.Normal)) |
|
542 | Colors.Normal)) | |
543 | if value.offset is not None: |
|
543 | if value.offset is not None: | |
544 | s = ' ' |
|
544 | s = ' ' | |
545 | for c in textline[i:value.offset - 1]: |
|
545 | for c in textline[i:value.offset - 1]: | |
546 | if c.isspace(): |
|
546 | if c.isspace(): | |
547 | s += c |
|
547 | s += c | |
548 | else: |
|
548 | else: | |
549 | s += ' ' |
|
549 | s += ' ' | |
550 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
550 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
551 | Colors.Normal)) |
|
551 | Colors.Normal)) | |
552 |
|
552 | |||
553 | try: |
|
553 | try: | |
554 | s = value.msg |
|
554 | s = value.msg | |
555 | except Exception: |
|
555 | except Exception: | |
556 | s = self._some_str(value) |
|
556 | s = self._some_str(value) | |
557 | if s: |
|
557 | if s: | |
558 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, |
|
558 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, | |
559 | Colors.Normal, s)) |
|
559 | Colors.Normal, s)) | |
560 | else: |
|
560 | else: | |
561 | list.append('%s\n' % stype) |
|
561 | list.append('%s\n' % stype) | |
562 |
|
562 | |||
563 | # sync with user hooks |
|
563 | # sync with user hooks | |
564 | if have_filedata: |
|
564 | if have_filedata: | |
565 | ipinst = get_ipython() |
|
565 | ipinst = get_ipython() | |
566 | if ipinst is not None: |
|
566 | if ipinst is not None: | |
567 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) |
|
567 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) | |
568 |
|
568 | |||
569 | return list |
|
569 | return list | |
570 |
|
570 | |||
571 | def get_exception_only(self, etype, value): |
|
571 | def get_exception_only(self, etype, value): | |
572 | """Only print the exception type and message, without a traceback. |
|
572 | """Only print the exception type and message, without a traceback. | |
573 |
|
573 | |||
574 | Parameters |
|
574 | Parameters | |
575 | ---------- |
|
575 | ---------- | |
576 | etype : exception type |
|
576 | etype : exception type | |
577 | value : exception value |
|
577 | value : exception value | |
578 | """ |
|
578 | """ | |
579 | return ListTB.structured_traceback(self, etype, value) |
|
579 | return ListTB.structured_traceback(self, etype, value) | |
580 |
|
580 | |||
581 | def show_exception_only(self, etype, evalue): |
|
581 | def show_exception_only(self, etype, evalue): | |
582 | """Only print the exception type and message, without a traceback. |
|
582 | """Only print the exception type and message, without a traceback. | |
583 |
|
583 | |||
584 | Parameters |
|
584 | Parameters | |
585 | ---------- |
|
585 | ---------- | |
586 | etype : exception type |
|
586 | etype : exception type | |
587 | evalue : exception value |
|
587 | evalue : exception value | |
588 | """ |
|
588 | """ | |
589 | # This method needs to use __call__ from *this* class, not the one from |
|
589 | # This method needs to use __call__ from *this* class, not the one from | |
590 | # a subclass whose signature or behavior may be different |
|
590 | # a subclass whose signature or behavior may be different | |
591 | ostream = self.ostream |
|
591 | ostream = self.ostream | |
592 | ostream.flush() |
|
592 | ostream.flush() | |
593 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) |
|
593 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) | |
594 | ostream.flush() |
|
594 | ostream.flush() | |
595 |
|
595 | |||
596 | def _some_str(self, value): |
|
596 | def _some_str(self, value): | |
597 | # Lifted from traceback.py |
|
597 | # Lifted from traceback.py | |
598 | try: |
|
598 | try: | |
599 | return py3compat.cast_unicode(str(value)) |
|
599 | return py3compat.cast_unicode(str(value)) | |
600 | except: |
|
600 | except: | |
601 | return u'<unprintable %s object>' % type(value).__name__ |
|
601 | return u'<unprintable %s object>' % type(value).__name__ | |
602 |
|
602 | |||
603 |
|
603 | |||
604 | #---------------------------------------------------------------------------- |
|
604 | #---------------------------------------------------------------------------- | |
605 | class VerboseTB(TBTools): |
|
605 | class VerboseTB(TBTools): | |
606 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
606 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
607 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
607 | of HTML. Requires inspect and pydoc. Crazy, man. | |
608 |
|
608 | |||
609 | Modified version which optionally strips the topmost entries from the |
|
609 | Modified version which optionally strips the topmost entries from the | |
610 | traceback, to be used with alternate interpreters (because their own code |
|
610 | traceback, to be used with alternate interpreters (because their own code | |
611 | would appear in the traceback).""" |
|
611 | would appear in the traceback).""" | |
612 |
|
612 | |||
613 | _tb_highlight = "bg:ansiyellow" |
|
613 | _tb_highlight = "bg:ansiyellow" | |
614 |
|
614 | |||
615 | def __init__( |
|
615 | def __init__( | |
616 | self, |
|
616 | self, | |
617 | color_scheme: str = "Linux", |
|
617 | color_scheme: str = "Linux", | |
618 | call_pdb: bool = False, |
|
618 | call_pdb: bool = False, | |
619 | ostream=None, |
|
619 | ostream=None, | |
620 | tb_offset: int = 0, |
|
620 | tb_offset: int = 0, | |
621 | long_header: bool = False, |
|
621 | long_header: bool = False, | |
622 | include_vars: bool = True, |
|
622 | include_vars: bool = True, | |
623 | check_cache=None, |
|
623 | check_cache=None, | |
624 | debugger_cls=None, |
|
624 | debugger_cls=None, | |
625 | parent=None, |
|
625 | parent=None, | |
626 | config=None, |
|
626 | config=None, | |
627 | ): |
|
627 | ): | |
628 | """Specify traceback offset, headers and color scheme. |
|
628 | """Specify traceback offset, headers and color scheme. | |
629 |
|
629 | |||
630 | Define how many frames to drop from the tracebacks. Calling it with |
|
630 | Define how many frames to drop from the tracebacks. Calling it with | |
631 | tb_offset=1 allows use of this handler in interpreters which will have |
|
631 | tb_offset=1 allows use of this handler in interpreters which will have | |
632 | their own code at the top of the traceback (VerboseTB will first |
|
632 | their own code at the top of the traceback (VerboseTB will first | |
633 | remove that frame before printing the traceback info).""" |
|
633 | remove that frame before printing the traceback info).""" | |
634 | TBTools.__init__( |
|
634 | TBTools.__init__( | |
635 | self, |
|
635 | self, | |
636 | color_scheme=color_scheme, |
|
636 | color_scheme=color_scheme, | |
637 | call_pdb=call_pdb, |
|
637 | call_pdb=call_pdb, | |
638 | ostream=ostream, |
|
638 | ostream=ostream, | |
639 | parent=parent, |
|
639 | parent=parent, | |
640 | config=config, |
|
640 | config=config, | |
641 | debugger_cls=debugger_cls, |
|
641 | debugger_cls=debugger_cls, | |
642 | ) |
|
642 | ) | |
643 | self.tb_offset = tb_offset |
|
643 | self.tb_offset = tb_offset | |
644 | self.long_header = long_header |
|
644 | self.long_header = long_header | |
645 | self.include_vars = include_vars |
|
645 | self.include_vars = include_vars | |
646 | # By default we use linecache.checkcache, but the user can provide a |
|
646 | # By default we use linecache.checkcache, but the user can provide a | |
647 |
# different check_cache implementation. This |
|
647 | # different check_cache implementation. This was formerly used by the | |
648 |
# |
|
648 | # IPython kernel for interactive code, but is no longer necessary. | |
649 | # by a compiler instance that flushes the linecache but preserves its |
|
|||
650 | # own code cache. |
|
|||
651 | if check_cache is None: |
|
649 | if check_cache is None: | |
652 | check_cache = linecache.checkcache |
|
650 | check_cache = linecache.checkcache | |
653 | self.check_cache = check_cache |
|
651 | self.check_cache = check_cache | |
654 |
|
652 | |||
655 | self.skip_hidden = True |
|
653 | self.skip_hidden = True | |
656 |
|
654 | |||
657 | def format_record(self, frame_info): |
|
655 | def format_record(self, frame_info): | |
658 | """Format a single stack frame""" |
|
656 | """Format a single stack frame""" | |
659 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
657 | Colors = self.Colors # just a shorthand + quicker name lookup | |
660 | ColorsNormal = Colors.Normal # used a lot |
|
658 | ColorsNormal = Colors.Normal # used a lot | |
661 |
|
659 | |||
662 | if isinstance(frame_info, stack_data.RepeatedFrames): |
|
660 | if isinstance(frame_info, stack_data.RepeatedFrames): | |
663 | return ' %s[... skipping similar frames: %s]%s\n' % ( |
|
661 | return ' %s[... skipping similar frames: %s]%s\n' % ( | |
664 | Colors.excName, frame_info.description, ColorsNormal) |
|
662 | Colors.excName, frame_info.description, ColorsNormal) | |
665 |
|
663 | |||
666 | indent = " " * INDENT_SIZE |
|
664 | indent = " " * INDENT_SIZE | |
667 | em_normal = "%s\n%s%s" % (Colors.valEm, indent, ColorsNormal) |
|
665 | em_normal = "%s\n%s%s" % (Colors.valEm, indent, ColorsNormal) | |
668 | tpl_call = f"in {Colors.vName}{{file}}{Colors.valEm}{{scope}}{ColorsNormal}" |
|
666 | tpl_call = f"in {Colors.vName}{{file}}{Colors.valEm}{{scope}}{ColorsNormal}" | |
669 | tpl_call_fail = "in %s%%s%s(***failed resolving arguments***)%s" % ( |
|
667 | tpl_call_fail = "in %s%%s%s(***failed resolving arguments***)%s" % ( | |
670 | Colors.vName, |
|
668 | Colors.vName, | |
671 | Colors.valEm, |
|
669 | Colors.valEm, | |
672 | ColorsNormal, |
|
670 | ColorsNormal, | |
673 | ) |
|
671 | ) | |
674 | tpl_name_val = "%%s %s= %%s%s" % (Colors.valEm, ColorsNormal) |
|
672 | tpl_name_val = "%%s %s= %%s%s" % (Colors.valEm, ColorsNormal) | |
675 |
|
673 | |||
676 | link = _format_filename( |
|
674 | link = _format_filename( | |
677 | frame_info.filename, |
|
675 | frame_info.filename, | |
678 | Colors.filenameEm, |
|
676 | Colors.filenameEm, | |
679 | ColorsNormal, |
|
677 | ColorsNormal, | |
680 | lineno=frame_info.lineno, |
|
678 | lineno=frame_info.lineno, | |
681 | ) |
|
679 | ) | |
682 | args, varargs, varkw, locals_ = inspect.getargvalues(frame_info.frame) |
|
680 | args, varargs, varkw, locals_ = inspect.getargvalues(frame_info.frame) | |
683 |
|
681 | |||
684 | func = frame_info.executing.code_qualname() |
|
682 | func = frame_info.executing.code_qualname() | |
685 | if func == "<module>": |
|
683 | if func == "<module>": | |
686 | call = "" |
|
684 | call = "" | |
687 | else: |
|
685 | else: | |
688 | # Decide whether to include variable details or not |
|
686 | # Decide whether to include variable details or not | |
689 | var_repr = eqrepr if self.include_vars else nullrepr |
|
687 | var_repr = eqrepr if self.include_vars else nullrepr | |
690 | try: |
|
688 | try: | |
691 | scope = inspect.formatargvalues( |
|
689 | scope = inspect.formatargvalues( | |
692 | args, varargs, varkw, locals_, formatvalue=var_repr |
|
690 | args, varargs, varkw, locals_, formatvalue=var_repr | |
693 | ) |
|
691 | ) | |
694 | call = tpl_call.format(file=func, scope=scope) |
|
692 | call = tpl_call.format(file=func, scope=scope) | |
695 | except KeyError: |
|
693 | except KeyError: | |
696 | # This happens in situations like errors inside generator |
|
694 | # This happens in situations like errors inside generator | |
697 | # expressions, where local variables are listed in the |
|
695 | # expressions, where local variables are listed in the | |
698 | # line, but can't be extracted from the frame. I'm not |
|
696 | # line, but can't be extracted from the frame. I'm not | |
699 | # 100% sure this isn't actually a bug in inspect itself, |
|
697 | # 100% sure this isn't actually a bug in inspect itself, | |
700 | # but since there's no info for us to compute with, the |
|
698 | # but since there's no info for us to compute with, the | |
701 | # best we can do is report the failure and move on. Here |
|
699 | # best we can do is report the failure and move on. Here | |
702 | # we must *not* call any traceback construction again, |
|
700 | # we must *not* call any traceback construction again, | |
703 | # because that would mess up use of %debug later on. So we |
|
701 | # because that would mess up use of %debug later on. So we | |
704 | # simply report the failure and move on. The only |
|
702 | # simply report the failure and move on. The only | |
705 | # limitation will be that this frame won't have locals |
|
703 | # limitation will be that this frame won't have locals | |
706 | # listed in the call signature. Quite subtle problem... |
|
704 | # listed in the call signature. Quite subtle problem... | |
707 | # I can't think of a good way to validate this in a unit |
|
705 | # I can't think of a good way to validate this in a unit | |
708 | # test, but running a script consisting of: |
|
706 | # test, but running a script consisting of: | |
709 | # dict( (k,v.strip()) for (k,v) in range(10) ) |
|
707 | # dict( (k,v.strip()) for (k,v) in range(10) ) | |
710 | # will illustrate the error, if this exception catch is |
|
708 | # will illustrate the error, if this exception catch is | |
711 | # disabled. |
|
709 | # disabled. | |
712 | call = tpl_call_fail % func |
|
710 | call = tpl_call_fail % func | |
713 |
|
711 | |||
714 | lvals = '' |
|
712 | lvals = '' | |
715 | lvals_list = [] |
|
713 | lvals_list = [] | |
716 | if self.include_vars: |
|
714 | if self.include_vars: | |
717 | try: |
|
715 | try: | |
718 | # we likely want to fix stackdata at some point, but |
|
716 | # we likely want to fix stackdata at some point, but | |
719 | # still need a workaround. |
|
717 | # still need a workaround. | |
720 | fibp = frame_info.variables_in_executing_piece |
|
718 | fibp = frame_info.variables_in_executing_piece | |
721 | for var in fibp: |
|
719 | for var in fibp: | |
722 | lvals_list.append(tpl_name_val % (var.name, repr(var.value))) |
|
720 | lvals_list.append(tpl_name_val % (var.name, repr(var.value))) | |
723 | except Exception: |
|
721 | except Exception: | |
724 | lvals_list.append( |
|
722 | lvals_list.append( | |
725 | "Exception trying to inspect frame. No more locals available." |
|
723 | "Exception trying to inspect frame. No more locals available." | |
726 | ) |
|
724 | ) | |
727 | if lvals_list: |
|
725 | if lvals_list: | |
728 | lvals = '%s%s' % (indent, em_normal.join(lvals_list)) |
|
726 | lvals = '%s%s' % (indent, em_normal.join(lvals_list)) | |
729 |
|
727 | |||
730 | result = f'{link}{", " if call else ""}{call}\n' |
|
728 | result = f'{link}{", " if call else ""}{call}\n' | |
731 |
|
729 | |||
732 | result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals)) |
|
730 | result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals)) | |
733 | return result |
|
731 | return result | |
734 |
|
732 | |||
735 | def prepare_header(self, etype, long_version=False): |
|
733 | def prepare_header(self, etype, long_version=False): | |
736 | colors = self.Colors # just a shorthand + quicker name lookup |
|
734 | colors = self.Colors # just a shorthand + quicker name lookup | |
737 | colorsnormal = colors.Normal # used a lot |
|
735 | colorsnormal = colors.Normal # used a lot | |
738 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) |
|
736 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) | |
739 | width = min(75, get_terminal_size()[0]) |
|
737 | width = min(75, get_terminal_size()[0]) | |
740 | if long_version: |
|
738 | if long_version: | |
741 | # Header with the exception type, python version, and date |
|
739 | # Header with the exception type, python version, and date | |
742 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
|
740 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable | |
743 | date = time.ctime(time.time()) |
|
741 | date = time.ctime(time.time()) | |
744 |
|
742 | |||
745 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, |
|
743 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, | |
746 | exc, ' ' * (width - len(str(etype)) - len(pyver)), |
|
744 | exc, ' ' * (width - len(str(etype)) - len(pyver)), | |
747 | pyver, date.rjust(width) ) |
|
745 | pyver, date.rjust(width) ) | |
748 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ |
|
746 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ | |
749 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
747 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
750 | else: |
|
748 | else: | |
751 | # Simplified header |
|
749 | # Simplified header | |
752 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ |
|
750 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ | |
753 | rjust(width - len(str(etype))) ) |
|
751 | rjust(width - len(str(etype))) ) | |
754 |
|
752 | |||
755 | return head |
|
753 | return head | |
756 |
|
754 | |||
757 | def format_exception(self, etype, evalue): |
|
755 | def format_exception(self, etype, evalue): | |
758 | colors = self.Colors # just a shorthand + quicker name lookup |
|
756 | colors = self.Colors # just a shorthand + quicker name lookup | |
759 | colorsnormal = colors.Normal # used a lot |
|
757 | colorsnormal = colors.Normal # used a lot | |
760 | # Get (safely) a string form of the exception info |
|
758 | # Get (safely) a string form of the exception info | |
761 | try: |
|
759 | try: | |
762 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
760 | etype_str, evalue_str = map(str, (etype, evalue)) | |
763 | except: |
|
761 | except: | |
764 | # User exception is improperly defined. |
|
762 | # User exception is improperly defined. | |
765 | etype, evalue = str, sys.exc_info()[:2] |
|
763 | etype, evalue = str, sys.exc_info()[:2] | |
766 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
764 | etype_str, evalue_str = map(str, (etype, evalue)) | |
767 | # ... and format it |
|
765 | # ... and format it | |
768 | return ['%s%s%s: %s' % (colors.excName, etype_str, |
|
766 | return ['%s%s%s: %s' % (colors.excName, etype_str, | |
769 | colorsnormal, py3compat.cast_unicode(evalue_str))] |
|
767 | colorsnormal, py3compat.cast_unicode(evalue_str))] | |
770 |
|
768 | |||
771 | def format_exception_as_a_whole( |
|
769 | def format_exception_as_a_whole( | |
772 | self, |
|
770 | self, | |
773 | etype: type, |
|
771 | etype: type, | |
774 | evalue: BaseException, |
|
772 | evalue: BaseException, | |
775 | etb: Optional[TracebackType], |
|
773 | etb: Optional[TracebackType], | |
776 | number_of_lines_of_context, |
|
774 | number_of_lines_of_context, | |
777 | tb_offset: Optional[int], |
|
775 | tb_offset: Optional[int], | |
778 | ): |
|
776 | ): | |
779 | """Formats the header, traceback and exception message for a single exception. |
|
777 | """Formats the header, traceback and exception message for a single exception. | |
780 |
|
778 | |||
781 | This may be called multiple times by Python 3 exception chaining |
|
779 | This may be called multiple times by Python 3 exception chaining | |
782 | (PEP 3134). |
|
780 | (PEP 3134). | |
783 | """ |
|
781 | """ | |
784 | # some locals |
|
782 | # some locals | |
785 | orig_etype = etype |
|
783 | orig_etype = etype | |
786 | try: |
|
784 | try: | |
787 | etype = etype.__name__ |
|
785 | etype = etype.__name__ | |
788 | except AttributeError: |
|
786 | except AttributeError: | |
789 | pass |
|
787 | pass | |
790 |
|
788 | |||
791 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
789 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
792 | assert isinstance(tb_offset, int) |
|
790 | assert isinstance(tb_offset, int) | |
793 | head = self.prepare_header(etype, self.long_header) |
|
791 | head = self.prepare_header(etype, self.long_header) | |
794 | records = ( |
|
792 | records = ( | |
795 | self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else [] |
|
793 | self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else [] | |
796 | ) |
|
794 | ) | |
797 |
|
795 | |||
798 | frames = [] |
|
796 | frames = [] | |
799 | skipped = 0 |
|
797 | skipped = 0 | |
800 | lastrecord = len(records) - 1 |
|
798 | lastrecord = len(records) - 1 | |
801 | for i, r in enumerate(records): |
|
799 | for i, r in enumerate(records): | |
802 | if not isinstance(r, stack_data.RepeatedFrames) and self.skip_hidden: |
|
800 | if not isinstance(r, stack_data.RepeatedFrames) and self.skip_hidden: | |
803 | if r.frame.f_locals.get("__tracebackhide__", 0) and i != lastrecord: |
|
801 | if r.frame.f_locals.get("__tracebackhide__", 0) and i != lastrecord: | |
804 | skipped += 1 |
|
802 | skipped += 1 | |
805 | continue |
|
803 | continue | |
806 | if skipped: |
|
804 | if skipped: | |
807 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
805 | Colors = self.Colors # just a shorthand + quicker name lookup | |
808 | ColorsNormal = Colors.Normal # used a lot |
|
806 | ColorsNormal = Colors.Normal # used a lot | |
809 | frames.append( |
|
807 | frames.append( | |
810 | " %s[... skipping hidden %s frame]%s\n" |
|
808 | " %s[... skipping hidden %s frame]%s\n" | |
811 | % (Colors.excName, skipped, ColorsNormal) |
|
809 | % (Colors.excName, skipped, ColorsNormal) | |
812 | ) |
|
810 | ) | |
813 | skipped = 0 |
|
811 | skipped = 0 | |
814 | frames.append(self.format_record(r)) |
|
812 | frames.append(self.format_record(r)) | |
815 | if skipped: |
|
813 | if skipped: | |
816 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
814 | Colors = self.Colors # just a shorthand + quicker name lookup | |
817 | ColorsNormal = Colors.Normal # used a lot |
|
815 | ColorsNormal = Colors.Normal # used a lot | |
818 | frames.append( |
|
816 | frames.append( | |
819 | " %s[... skipping hidden %s frame]%s\n" |
|
817 | " %s[... skipping hidden %s frame]%s\n" | |
820 | % (Colors.excName, skipped, ColorsNormal) |
|
818 | % (Colors.excName, skipped, ColorsNormal) | |
821 | ) |
|
819 | ) | |
822 |
|
820 | |||
823 | formatted_exception = self.format_exception(etype, evalue) |
|
821 | formatted_exception = self.format_exception(etype, evalue) | |
824 | if records: |
|
822 | if records: | |
825 | frame_info = records[-1] |
|
823 | frame_info = records[-1] | |
826 | ipinst = get_ipython() |
|
824 | ipinst = get_ipython() | |
827 | if ipinst is not None: |
|
825 | if ipinst is not None: | |
828 | ipinst.hooks.synchronize_with_editor(frame_info.filename, frame_info.lineno, 0) |
|
826 | ipinst.hooks.synchronize_with_editor(frame_info.filename, frame_info.lineno, 0) | |
829 |
|
827 | |||
830 | return [[head] + frames + [''.join(formatted_exception[0])]] |
|
828 | return [[head] + frames + [''.join(formatted_exception[0])]] | |
831 |
|
829 | |||
832 | def get_records( |
|
830 | def get_records( | |
833 | self, etb: TracebackType, number_of_lines_of_context: int, tb_offset: int |
|
831 | self, etb: TracebackType, number_of_lines_of_context: int, tb_offset: int | |
834 | ): |
|
832 | ): | |
835 | assert etb is not None |
|
833 | assert etb is not None | |
836 | context = number_of_lines_of_context - 1 |
|
834 | context = number_of_lines_of_context - 1 | |
837 | after = context // 2 |
|
835 | after = context // 2 | |
838 | before = context - after |
|
836 | before = context - after | |
839 | if self.has_colors: |
|
837 | if self.has_colors: | |
840 | style = get_style_by_name("default") |
|
838 | style = get_style_by_name("default") | |
841 | style = stack_data.style_with_executing_node(style, self._tb_highlight) |
|
839 | style = stack_data.style_with_executing_node(style, self._tb_highlight) | |
842 | formatter = Terminal256Formatter(style=style) |
|
840 | formatter = Terminal256Formatter(style=style) | |
843 | else: |
|
841 | else: | |
844 | formatter = None |
|
842 | formatter = None | |
845 | options = stack_data.Options( |
|
843 | options = stack_data.Options( | |
846 | before=before, |
|
844 | before=before, | |
847 | after=after, |
|
845 | after=after, | |
848 | pygments_formatter=formatter, |
|
846 | pygments_formatter=formatter, | |
849 | ) |
|
847 | ) | |
850 | return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:] |
|
848 | return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:] | |
851 |
|
849 | |||
852 | def structured_traceback( |
|
850 | def structured_traceback( | |
853 | self, |
|
851 | self, | |
854 | etype: type, |
|
852 | etype: type, | |
855 | evalue: Optional[BaseException], |
|
853 | evalue: Optional[BaseException], | |
856 | etb: Optional[TracebackType], |
|
854 | etb: Optional[TracebackType], | |
857 | tb_offset: Optional[int] = None, |
|
855 | tb_offset: Optional[int] = None, | |
858 | number_of_lines_of_context: int = 5, |
|
856 | number_of_lines_of_context: int = 5, | |
859 | ): |
|
857 | ): | |
860 | """Return a nice text document describing the traceback.""" |
|
858 | """Return a nice text document describing the traceback.""" | |
861 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, |
|
859 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, | |
862 | tb_offset) |
|
860 | tb_offset) | |
863 |
|
861 | |||
864 | colors = self.Colors # just a shorthand + quicker name lookup |
|
862 | colors = self.Colors # just a shorthand + quicker name lookup | |
865 | colorsnormal = colors.Normal # used a lot |
|
863 | colorsnormal = colors.Normal # used a lot | |
866 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) |
|
864 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) | |
867 | structured_traceback_parts = [head] |
|
865 | structured_traceback_parts = [head] | |
868 | chained_exceptions_tb_offset = 0 |
|
866 | chained_exceptions_tb_offset = 0 | |
869 | lines_of_context = 3 |
|
867 | lines_of_context = 3 | |
870 | formatted_exceptions = formatted_exception |
|
868 | formatted_exceptions = formatted_exception | |
871 | exception = self.get_parts_of_chained_exception(evalue) |
|
869 | exception = self.get_parts_of_chained_exception(evalue) | |
872 | if exception: |
|
870 | if exception: | |
873 | assert evalue is not None |
|
871 | assert evalue is not None | |
874 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
872 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
875 | etype, evalue, etb = exception |
|
873 | etype, evalue, etb = exception | |
876 | else: |
|
874 | else: | |
877 | evalue = None |
|
875 | evalue = None | |
878 | chained_exc_ids = set() |
|
876 | chained_exc_ids = set() | |
879 | while evalue: |
|
877 | while evalue: | |
880 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, |
|
878 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, | |
881 | chained_exceptions_tb_offset) |
|
879 | chained_exceptions_tb_offset) | |
882 | exception = self.get_parts_of_chained_exception(evalue) |
|
880 | exception = self.get_parts_of_chained_exception(evalue) | |
883 |
|
881 | |||
884 | if exception and not id(exception[1]) in chained_exc_ids: |
|
882 | if exception and not id(exception[1]) in chained_exc_ids: | |
885 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop |
|
883 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop | |
886 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
884 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
887 | etype, evalue, etb = exception |
|
885 | etype, evalue, etb = exception | |
888 | else: |
|
886 | else: | |
889 | evalue = None |
|
887 | evalue = None | |
890 |
|
888 | |||
891 | # we want to see exceptions in a reversed order: |
|
889 | # we want to see exceptions in a reversed order: | |
892 | # the first exception should be on top |
|
890 | # the first exception should be on top | |
893 | for formatted_exception in reversed(formatted_exceptions): |
|
891 | for formatted_exception in reversed(formatted_exceptions): | |
894 | structured_traceback_parts += formatted_exception |
|
892 | structured_traceback_parts += formatted_exception | |
895 |
|
893 | |||
896 | return structured_traceback_parts |
|
894 | return structured_traceback_parts | |
897 |
|
895 | |||
898 | def debugger(self, force: bool = False): |
|
896 | def debugger(self, force: bool = False): | |
899 | """Call up the pdb debugger if desired, always clean up the tb |
|
897 | """Call up the pdb debugger if desired, always clean up the tb | |
900 | reference. |
|
898 | reference. | |
901 |
|
899 | |||
902 | Keywords: |
|
900 | Keywords: | |
903 |
|
901 | |||
904 | - force(False): by default, this routine checks the instance call_pdb |
|
902 | - force(False): by default, this routine checks the instance call_pdb | |
905 | flag and does not actually invoke the debugger if the flag is false. |
|
903 | flag and does not actually invoke the debugger if the flag is false. | |
906 | The 'force' option forces the debugger to activate even if the flag |
|
904 | The 'force' option forces the debugger to activate even if the flag | |
907 | is false. |
|
905 | is false. | |
908 |
|
906 | |||
909 | If the call_pdb flag is set, the pdb interactive debugger is |
|
907 | If the call_pdb flag is set, the pdb interactive debugger is | |
910 | invoked. In all cases, the self.tb reference to the current traceback |
|
908 | invoked. In all cases, the self.tb reference to the current traceback | |
911 | is deleted to prevent lingering references which hamper memory |
|
909 | is deleted to prevent lingering references which hamper memory | |
912 | management. |
|
910 | management. | |
913 |
|
911 | |||
914 | Note that each call to pdb() does an 'import readline', so if your app |
|
912 | Note that each call to pdb() does an 'import readline', so if your app | |
915 | requires a special setup for the readline completers, you'll have to |
|
913 | requires a special setup for the readline completers, you'll have to | |
916 | fix that by hand after invoking the exception handler.""" |
|
914 | fix that by hand after invoking the exception handler.""" | |
917 |
|
915 | |||
918 | if force or self.call_pdb: |
|
916 | if force or self.call_pdb: | |
919 | if self.pdb is None: |
|
917 | if self.pdb is None: | |
920 | self.pdb = self.debugger_cls() |
|
918 | self.pdb = self.debugger_cls() | |
921 | # the system displayhook may have changed, restore the original |
|
919 | # the system displayhook may have changed, restore the original | |
922 | # for pdb |
|
920 | # for pdb | |
923 | display_trap = DisplayTrap(hook=sys.__displayhook__) |
|
921 | display_trap = DisplayTrap(hook=sys.__displayhook__) | |
924 | with display_trap: |
|
922 | with display_trap: | |
925 | self.pdb.reset() |
|
923 | self.pdb.reset() | |
926 | # Find the right frame so we don't pop up inside ipython itself |
|
924 | # Find the right frame so we don't pop up inside ipython itself | |
927 | if hasattr(self, 'tb') and self.tb is not None: |
|
925 | if hasattr(self, 'tb') and self.tb is not None: | |
928 | etb = self.tb |
|
926 | etb = self.tb | |
929 | else: |
|
927 | else: | |
930 | etb = self.tb = sys.last_traceback |
|
928 | etb = self.tb = sys.last_traceback | |
931 | while self.tb is not None and self.tb.tb_next is not None: |
|
929 | while self.tb is not None and self.tb.tb_next is not None: | |
932 | assert self.tb.tb_next is not None |
|
930 | assert self.tb.tb_next is not None | |
933 | self.tb = self.tb.tb_next |
|
931 | self.tb = self.tb.tb_next | |
934 | if etb and etb.tb_next: |
|
932 | if etb and etb.tb_next: | |
935 | etb = etb.tb_next |
|
933 | etb = etb.tb_next | |
936 | self.pdb.botframe = etb.tb_frame |
|
934 | self.pdb.botframe = etb.tb_frame | |
937 | self.pdb.interaction(None, etb) |
|
935 | self.pdb.interaction(None, etb) | |
938 |
|
936 | |||
939 | if hasattr(self, 'tb'): |
|
937 | if hasattr(self, 'tb'): | |
940 | del self.tb |
|
938 | del self.tb | |
941 |
|
939 | |||
942 | def handler(self, info=None): |
|
940 | def handler(self, info=None): | |
943 | (etype, evalue, etb) = info or sys.exc_info() |
|
941 | (etype, evalue, etb) = info or sys.exc_info() | |
944 | self.tb = etb |
|
942 | self.tb = etb | |
945 | ostream = self.ostream |
|
943 | ostream = self.ostream | |
946 | ostream.flush() |
|
944 | ostream.flush() | |
947 | ostream.write(self.text(etype, evalue, etb)) |
|
945 | ostream.write(self.text(etype, evalue, etb)) | |
948 | ostream.write('\n') |
|
946 | ostream.write('\n') | |
949 | ostream.flush() |
|
947 | ostream.flush() | |
950 |
|
948 | |||
951 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
949 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
952 | # out the right info on its own. |
|
950 | # out the right info on its own. | |
953 | def __call__(self, etype=None, evalue=None, etb=None): |
|
951 | def __call__(self, etype=None, evalue=None, etb=None): | |
954 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
952 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
955 | if etb is None: |
|
953 | if etb is None: | |
956 | self.handler() |
|
954 | self.handler() | |
957 | else: |
|
955 | else: | |
958 | self.handler((etype, evalue, etb)) |
|
956 | self.handler((etype, evalue, etb)) | |
959 | try: |
|
957 | try: | |
960 | self.debugger() |
|
958 | self.debugger() | |
961 | except KeyboardInterrupt: |
|
959 | except KeyboardInterrupt: | |
962 | print("\nKeyboardInterrupt") |
|
960 | print("\nKeyboardInterrupt") | |
963 |
|
961 | |||
964 |
|
962 | |||
965 | #---------------------------------------------------------------------------- |
|
963 | #---------------------------------------------------------------------------- | |
966 | class FormattedTB(VerboseTB, ListTB): |
|
964 | class FormattedTB(VerboseTB, ListTB): | |
967 | """Subclass ListTB but allow calling with a traceback. |
|
965 | """Subclass ListTB but allow calling with a traceback. | |
968 |
|
966 | |||
969 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
967 | It can thus be used as a sys.excepthook for Python > 2.1. | |
970 |
|
968 | |||
971 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
969 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
972 |
|
970 | |||
973 | Allows a tb_offset to be specified. This is useful for situations where |
|
971 | Allows a tb_offset to be specified. This is useful for situations where | |
974 | one needs to remove a number of topmost frames from the traceback (such as |
|
972 | one needs to remove a number of topmost frames from the traceback (such as | |
975 | occurs with python programs that themselves execute other python code, |
|
973 | occurs with python programs that themselves execute other python code, | |
976 | like Python shells). """ |
|
974 | like Python shells). """ | |
977 |
|
975 | |||
978 | mode: str |
|
976 | mode: str | |
979 |
|
977 | |||
980 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, |
|
978 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, | |
981 | ostream=None, |
|
979 | ostream=None, | |
982 | tb_offset=0, long_header=False, include_vars=False, |
|
980 | tb_offset=0, long_header=False, include_vars=False, | |
983 | check_cache=None, debugger_cls=None, |
|
981 | check_cache=None, debugger_cls=None, | |
984 | parent=None, config=None): |
|
982 | parent=None, config=None): | |
985 |
|
983 | |||
986 | # NEVER change the order of this list. Put new modes at the end: |
|
984 | # NEVER change the order of this list. Put new modes at the end: | |
987 | self.valid_modes = ['Plain', 'Context', 'Verbose', 'Minimal'] |
|
985 | self.valid_modes = ['Plain', 'Context', 'Verbose', 'Minimal'] | |
988 | self.verbose_modes = self.valid_modes[1:3] |
|
986 | self.verbose_modes = self.valid_modes[1:3] | |
989 |
|
987 | |||
990 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
988 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
991 | ostream=ostream, tb_offset=tb_offset, |
|
989 | ostream=ostream, tb_offset=tb_offset, | |
992 | long_header=long_header, include_vars=include_vars, |
|
990 | long_header=long_header, include_vars=include_vars, | |
993 | check_cache=check_cache, debugger_cls=debugger_cls, |
|
991 | check_cache=check_cache, debugger_cls=debugger_cls, | |
994 | parent=parent, config=config) |
|
992 | parent=parent, config=config) | |
995 |
|
993 | |||
996 | # Different types of tracebacks are joined with different separators to |
|
994 | # Different types of tracebacks are joined with different separators to | |
997 | # form a single string. They are taken from this dict |
|
995 | # form a single string. They are taken from this dict | |
998 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n', |
|
996 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n', | |
999 | Minimal='') |
|
997 | Minimal='') | |
1000 | # set_mode also sets the tb_join_char attribute |
|
998 | # set_mode also sets the tb_join_char attribute | |
1001 | self.set_mode(mode) |
|
999 | self.set_mode(mode) | |
1002 |
|
1000 | |||
1003 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): |
|
1001 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): | |
1004 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1002 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1005 | mode = self.mode |
|
1003 | mode = self.mode | |
1006 | if mode in self.verbose_modes: |
|
1004 | if mode in self.verbose_modes: | |
1007 | # Verbose modes need a full traceback |
|
1005 | # Verbose modes need a full traceback | |
1008 | return VerboseTB.structured_traceback( |
|
1006 | return VerboseTB.structured_traceback( | |
1009 | self, etype, value, tb, tb_offset, number_of_lines_of_context |
|
1007 | self, etype, value, tb, tb_offset, number_of_lines_of_context | |
1010 | ) |
|
1008 | ) | |
1011 | elif mode == 'Minimal': |
|
1009 | elif mode == 'Minimal': | |
1012 | return ListTB.get_exception_only(self, etype, value) |
|
1010 | return ListTB.get_exception_only(self, etype, value) | |
1013 | else: |
|
1011 | else: | |
1014 | # We must check the source cache because otherwise we can print |
|
1012 | # We must check the source cache because otherwise we can print | |
1015 | # out-of-date source code. |
|
1013 | # out-of-date source code. | |
1016 | self.check_cache() |
|
1014 | self.check_cache() | |
1017 | # Now we can extract and format the exception |
|
1015 | # Now we can extract and format the exception | |
1018 | return ListTB.structured_traceback( |
|
1016 | return ListTB.structured_traceback( | |
1019 | self, etype, value, tb, tb_offset, number_of_lines_of_context |
|
1017 | self, etype, value, tb, tb_offset, number_of_lines_of_context | |
1020 | ) |
|
1018 | ) | |
1021 |
|
1019 | |||
1022 | def stb2text(self, stb): |
|
1020 | def stb2text(self, stb): | |
1023 | """Convert a structured traceback (a list) to a string.""" |
|
1021 | """Convert a structured traceback (a list) to a string.""" | |
1024 | return self.tb_join_char.join(stb) |
|
1022 | return self.tb_join_char.join(stb) | |
1025 |
|
1023 | |||
1026 | def set_mode(self, mode: Optional[str] = None): |
|
1024 | def set_mode(self, mode: Optional[str] = None): | |
1027 | """Switch to the desired mode. |
|
1025 | """Switch to the desired mode. | |
1028 |
|
1026 | |||
1029 | If mode is not specified, cycles through the available modes.""" |
|
1027 | If mode is not specified, cycles through the available modes.""" | |
1030 |
|
1028 | |||
1031 | if not mode: |
|
1029 | if not mode: | |
1032 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ |
|
1030 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ | |
1033 | len(self.valid_modes) |
|
1031 | len(self.valid_modes) | |
1034 | self.mode = self.valid_modes[new_idx] |
|
1032 | self.mode = self.valid_modes[new_idx] | |
1035 | elif mode not in self.valid_modes: |
|
1033 | elif mode not in self.valid_modes: | |
1036 | raise ValueError( |
|
1034 | raise ValueError( | |
1037 | "Unrecognized mode in FormattedTB: <" + mode + ">\n" |
|
1035 | "Unrecognized mode in FormattedTB: <" + mode + ">\n" | |
1038 | "Valid modes: " + str(self.valid_modes) |
|
1036 | "Valid modes: " + str(self.valid_modes) | |
1039 | ) |
|
1037 | ) | |
1040 | else: |
|
1038 | else: | |
1041 | assert isinstance(mode, str) |
|
1039 | assert isinstance(mode, str) | |
1042 | self.mode = mode |
|
1040 | self.mode = mode | |
1043 | # include variable details only in 'Verbose' mode |
|
1041 | # include variable details only in 'Verbose' mode | |
1044 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
1042 | self.include_vars = (self.mode == self.valid_modes[2]) | |
1045 | # Set the join character for generating text tracebacks |
|
1043 | # Set the join character for generating text tracebacks | |
1046 | self.tb_join_char = self._join_chars[self.mode] |
|
1044 | self.tb_join_char = self._join_chars[self.mode] | |
1047 |
|
1045 | |||
1048 | # some convenient shortcuts |
|
1046 | # some convenient shortcuts | |
1049 | def plain(self): |
|
1047 | def plain(self): | |
1050 | self.set_mode(self.valid_modes[0]) |
|
1048 | self.set_mode(self.valid_modes[0]) | |
1051 |
|
1049 | |||
1052 | def context(self): |
|
1050 | def context(self): | |
1053 | self.set_mode(self.valid_modes[1]) |
|
1051 | self.set_mode(self.valid_modes[1]) | |
1054 |
|
1052 | |||
1055 | def verbose(self): |
|
1053 | def verbose(self): | |
1056 | self.set_mode(self.valid_modes[2]) |
|
1054 | self.set_mode(self.valid_modes[2]) | |
1057 |
|
1055 | |||
1058 | def minimal(self): |
|
1056 | def minimal(self): | |
1059 | self.set_mode(self.valid_modes[3]) |
|
1057 | self.set_mode(self.valid_modes[3]) | |
1060 |
|
1058 | |||
1061 |
|
1059 | |||
1062 | #---------------------------------------------------------------------------- |
|
1060 | #---------------------------------------------------------------------------- | |
1063 | class AutoFormattedTB(FormattedTB): |
|
1061 | class AutoFormattedTB(FormattedTB): | |
1064 | """A traceback printer which can be called on the fly. |
|
1062 | """A traceback printer which can be called on the fly. | |
1065 |
|
1063 | |||
1066 | It will find out about exceptions by itself. |
|
1064 | It will find out about exceptions by itself. | |
1067 |
|
1065 | |||
1068 | A brief example:: |
|
1066 | A brief example:: | |
1069 |
|
1067 | |||
1070 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
1068 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
1071 | try: |
|
1069 | try: | |
1072 | ... |
|
1070 | ... | |
1073 | except: |
|
1071 | except: | |
1074 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
1072 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
1075 | """ |
|
1073 | """ | |
1076 |
|
1074 | |||
1077 | def __call__(self, etype=None, evalue=None, etb=None, |
|
1075 | def __call__(self, etype=None, evalue=None, etb=None, | |
1078 | out=None, tb_offset=None): |
|
1076 | out=None, tb_offset=None): | |
1079 | """Print out a formatted exception traceback. |
|
1077 | """Print out a formatted exception traceback. | |
1080 |
|
1078 | |||
1081 | Optional arguments: |
|
1079 | Optional arguments: | |
1082 | - out: an open file-like object to direct output to. |
|
1080 | - out: an open file-like object to direct output to. | |
1083 |
|
1081 | |||
1084 | - tb_offset: the number of frames to skip over in the stack, on a |
|
1082 | - tb_offset: the number of frames to skip over in the stack, on a | |
1085 | per-call basis (this overrides temporarily the instance's tb_offset |
|
1083 | per-call basis (this overrides temporarily the instance's tb_offset | |
1086 | given at initialization time.""" |
|
1084 | given at initialization time.""" | |
1087 |
|
1085 | |||
1088 | if out is None: |
|
1086 | if out is None: | |
1089 | out = self.ostream |
|
1087 | out = self.ostream | |
1090 | out.flush() |
|
1088 | out.flush() | |
1091 | out.write(self.text(etype, evalue, etb, tb_offset)) |
|
1089 | out.write(self.text(etype, evalue, etb, tb_offset)) | |
1092 | out.write('\n') |
|
1090 | out.write('\n') | |
1093 | out.flush() |
|
1091 | out.flush() | |
1094 | # FIXME: we should remove the auto pdb behavior from here and leave |
|
1092 | # FIXME: we should remove the auto pdb behavior from here and leave | |
1095 | # that to the clients. |
|
1093 | # that to the clients. | |
1096 | try: |
|
1094 | try: | |
1097 | self.debugger() |
|
1095 | self.debugger() | |
1098 | except KeyboardInterrupt: |
|
1096 | except KeyboardInterrupt: | |
1099 | print("\nKeyboardInterrupt") |
|
1097 | print("\nKeyboardInterrupt") | |
1100 |
|
1098 | |||
1101 | def structured_traceback(self, etype=None, value=None, tb=None, |
|
1099 | def structured_traceback(self, etype=None, value=None, tb=None, | |
1102 | tb_offset=None, number_of_lines_of_context=5): |
|
1100 | tb_offset=None, number_of_lines_of_context=5): | |
1103 |
|
1101 | |||
1104 | etype: type |
|
1102 | etype: type | |
1105 | value: BaseException |
|
1103 | value: BaseException | |
1106 | # tb: TracebackType or tupleof tb types ? |
|
1104 | # tb: TracebackType or tupleof tb types ? | |
1107 | if etype is None: |
|
1105 | if etype is None: | |
1108 | etype, value, tb = sys.exc_info() |
|
1106 | etype, value, tb = sys.exc_info() | |
1109 | if isinstance(tb, tuple): |
|
1107 | if isinstance(tb, tuple): | |
1110 | # tb is a tuple if this is a chained exception. |
|
1108 | # tb is a tuple if this is a chained exception. | |
1111 | self.tb = tb[0] |
|
1109 | self.tb = tb[0] | |
1112 | else: |
|
1110 | else: | |
1113 | self.tb = tb |
|
1111 | self.tb = tb | |
1114 | return FormattedTB.structured_traceback( |
|
1112 | return FormattedTB.structured_traceback( | |
1115 | self, etype, value, tb, tb_offset, number_of_lines_of_context) |
|
1113 | self, etype, value, tb, tb_offset, number_of_lines_of_context) | |
1116 |
|
1114 | |||
1117 |
|
1115 | |||
1118 | #--------------------------------------------------------------------------- |
|
1116 | #--------------------------------------------------------------------------- | |
1119 |
|
1117 | |||
1120 | # A simple class to preserve Nathan's original functionality. |
|
1118 | # A simple class to preserve Nathan's original functionality. | |
1121 | class ColorTB(FormattedTB): |
|
1119 | class ColorTB(FormattedTB): | |
1122 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
1120 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1123 |
|
1121 | |||
1124 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): |
|
1122 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): | |
1125 | FormattedTB.__init__(self, color_scheme=color_scheme, |
|
1123 | FormattedTB.__init__(self, color_scheme=color_scheme, | |
1126 | call_pdb=call_pdb, **kwargs) |
|
1124 | call_pdb=call_pdb, **kwargs) | |
1127 |
|
1125 | |||
1128 |
|
1126 | |||
1129 | class SyntaxTB(ListTB): |
|
1127 | class SyntaxTB(ListTB): | |
1130 | """Extension which holds some state: the last exception value""" |
|
1128 | """Extension which holds some state: the last exception value""" | |
1131 |
|
1129 | |||
1132 | def __init__(self, color_scheme='NoColor', parent=None, config=None): |
|
1130 | def __init__(self, color_scheme='NoColor', parent=None, config=None): | |
1133 | ListTB.__init__(self, color_scheme, parent=parent, config=config) |
|
1131 | ListTB.__init__(self, color_scheme, parent=parent, config=config) | |
1134 | self.last_syntax_error = None |
|
1132 | self.last_syntax_error = None | |
1135 |
|
1133 | |||
1136 | def __call__(self, etype, value, elist): |
|
1134 | def __call__(self, etype, value, elist): | |
1137 | self.last_syntax_error = value |
|
1135 | self.last_syntax_error = value | |
1138 |
|
1136 | |||
1139 | ListTB.__call__(self, etype, value, elist) |
|
1137 | ListTB.__call__(self, etype, value, elist) | |
1140 |
|
1138 | |||
1141 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
1139 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
1142 | context=5): |
|
1140 | context=5): | |
1143 | # If the source file has been edited, the line in the syntax error can |
|
1141 | # If the source file has been edited, the line in the syntax error can | |
1144 | # be wrong (retrieved from an outdated cache). This replaces it with |
|
1142 | # be wrong (retrieved from an outdated cache). This replaces it with | |
1145 | # the current value. |
|
1143 | # the current value. | |
1146 | if isinstance(value, SyntaxError) \ |
|
1144 | if isinstance(value, SyntaxError) \ | |
1147 | and isinstance(value.filename, str) \ |
|
1145 | and isinstance(value.filename, str) \ | |
1148 | and isinstance(value.lineno, int): |
|
1146 | and isinstance(value.lineno, int): | |
1149 | linecache.checkcache(value.filename) |
|
1147 | linecache.checkcache(value.filename) | |
1150 | newtext = linecache.getline(value.filename, value.lineno) |
|
1148 | newtext = linecache.getline(value.filename, value.lineno) | |
1151 | if newtext: |
|
1149 | if newtext: | |
1152 | value.text = newtext |
|
1150 | value.text = newtext | |
1153 | self.last_syntax_error = value |
|
1151 | self.last_syntax_error = value | |
1154 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, |
|
1152 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, | |
1155 | tb_offset=tb_offset, context=context) |
|
1153 | tb_offset=tb_offset, context=context) | |
1156 |
|
1154 | |||
1157 | def clear_err_state(self): |
|
1155 | def clear_err_state(self): | |
1158 | """Return the current error state and clear it""" |
|
1156 | """Return the current error state and clear it""" | |
1159 | e = self.last_syntax_error |
|
1157 | e = self.last_syntax_error | |
1160 | self.last_syntax_error = None |
|
1158 | self.last_syntax_error = None | |
1161 | return e |
|
1159 | return e | |
1162 |
|
1160 | |||
1163 | def stb2text(self, stb): |
|
1161 | def stb2text(self, stb): | |
1164 | """Convert a structured traceback (a list) to a string.""" |
|
1162 | """Convert a structured traceback (a list) to a string.""" | |
1165 | return ''.join(stb) |
|
1163 | return ''.join(stb) | |
1166 |
|
1164 | |||
1167 |
|
1165 | |||
1168 | # some internal-use functions |
|
1166 | # some internal-use functions | |
1169 | def text_repr(value): |
|
1167 | def text_repr(value): | |
1170 | """Hopefully pretty robust repr equivalent.""" |
|
1168 | """Hopefully pretty robust repr equivalent.""" | |
1171 | # this is pretty horrible but should always return *something* |
|
1169 | # this is pretty horrible but should always return *something* | |
1172 | try: |
|
1170 | try: | |
1173 | return pydoc.text.repr(value) |
|
1171 | return pydoc.text.repr(value) | |
1174 | except KeyboardInterrupt: |
|
1172 | except KeyboardInterrupt: | |
1175 | raise |
|
1173 | raise | |
1176 | except: |
|
1174 | except: | |
1177 | try: |
|
1175 | try: | |
1178 | return repr(value) |
|
1176 | return repr(value) | |
1179 | except KeyboardInterrupt: |
|
1177 | except KeyboardInterrupt: | |
1180 | raise |
|
1178 | raise | |
1181 | except: |
|
1179 | except: | |
1182 | try: |
|
1180 | try: | |
1183 | # all still in an except block so we catch |
|
1181 | # all still in an except block so we catch | |
1184 | # getattr raising |
|
1182 | # getattr raising | |
1185 | name = getattr(value, '__name__', None) |
|
1183 | name = getattr(value, '__name__', None) | |
1186 | if name: |
|
1184 | if name: | |
1187 | # ick, recursion |
|
1185 | # ick, recursion | |
1188 | return text_repr(name) |
|
1186 | return text_repr(name) | |
1189 | klass = getattr(value, '__class__', None) |
|
1187 | klass = getattr(value, '__class__', None) | |
1190 | if klass: |
|
1188 | if klass: | |
1191 | return '%s instance' % text_repr(klass) |
|
1189 | return '%s instance' % text_repr(klass) | |
1192 | except KeyboardInterrupt: |
|
1190 | except KeyboardInterrupt: | |
1193 | raise |
|
1191 | raise | |
1194 | except: |
|
1192 | except: | |
1195 | return 'UNRECOVERABLE REPR FAILURE' |
|
1193 | return 'UNRECOVERABLE REPR FAILURE' | |
1196 |
|
1194 | |||
1197 |
|
1195 | |||
1198 | def eqrepr(value, repr=text_repr): |
|
1196 | def eqrepr(value, repr=text_repr): | |
1199 | return '=%s' % repr(value) |
|
1197 | return '=%s' % repr(value) | |
1200 |
|
1198 | |||
1201 |
|
1199 | |||
1202 | def nullrepr(value, repr=text_repr): |
|
1200 | def nullrepr(value, repr=text_repr): | |
1203 | return '' |
|
1201 | return '' |
General Comments 0
You need to be logged in to leave comments.
Login now