Debian Codegraph @

IPython/core/interactiveshell.py

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