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