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