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