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