##// END OF EJS Templates
Allow custom tracebacks for exception types with a _render_traceback_() method.
Thomas Kluyver -
Show More
@@ -1,3032 +1,3018
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 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19 from __future__ import print_function
20 20
21 21 import __builtin__ as builtin_mod
22 22 import __future__
23 23 import abc
24 24 import ast
25 25 import atexit
26 26 import os
27 27 import re
28 28 import runpy
29 29 import sys
30 30 import tempfile
31 31 import types
32 32
33 33 # We need to use nested to support python 2.6, once we move to >=2.7, we can
34 34 # use the with keyword's new builtin support for nested managers
35 35 try:
36 36 from contextlib import nested
37 37 except:
38 38 from IPython.utils.nested_context import nested
39 39
40 40 from IPython.config.configurable import SingletonConfigurable
41 41 from IPython.core import debugger, oinspect
42 42 from IPython.core import history as ipcorehist
43 43 from IPython.core import magic
44 44 from IPython.core import page
45 45 from IPython.core import prefilter
46 46 from IPython.core import shadowns
47 47 from IPython.core import ultratb
48 48 from IPython.core.alias import AliasManager, AliasError
49 49 from IPython.core.autocall import ExitAutocall
50 50 from IPython.core.builtin_trap import BuiltinTrap
51 51 from IPython.core.compilerop import CachingCompiler
52 52 from IPython.core.display_trap import DisplayTrap
53 53 from IPython.core.displayhook import DisplayHook
54 54 from IPython.core.displaypub import DisplayPublisher
55 55 from IPython.core.error import UsageError
56 56 from IPython.core.extensions import ExtensionManager
57 57 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
58 58 from IPython.core.formatters import DisplayFormatter
59 59 from IPython.core.history import HistoryManager
60 60 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
61 61 from IPython.core.logger import Logger
62 62 from IPython.core.macro import Macro
63 63 from IPython.core.payload import PayloadManager
64 64 from IPython.core.plugin import PluginManager
65 65 from IPython.core.prefilter import PrefilterManager
66 66 from IPython.core.profiledir import ProfileDir
67 67 from IPython.core.pylabtools import pylab_activate
68 68 from IPython.core.prompts import PromptManager
69 69 from IPython.lib.latextools import LaTeXTool
70 70 from IPython.utils import PyColorize
71 71 from IPython.utils import io
72 72 from IPython.utils import py3compat
73 73 from IPython.utils import openpy
74 74 from IPython.utils.doctestreload import doctest_reload
75 75 from IPython.utils.io import ask_yes_no
76 76 from IPython.utils.ipstruct import Struct
77 77 from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename
78 78 from IPython.utils.pickleshare import PickleShareDB
79 79 from IPython.utils.process import system, getoutput
80 80 from IPython.utils.strdispatch import StrDispatch
81 81 from IPython.utils.syspathcontext import prepended_to_syspath
82 82 from IPython.utils.text import (format_screen, LSString, SList,
83 83 DollarFormatter)
84 84 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
85 85 List, Unicode, Instance, Type)
86 86 from IPython.utils.warn import warn, error
87 87 import IPython.core.hooks
88 88
89 # FIXME: do this in a function to avoid circular dependencies
90 # A better solution is to remove IPython.parallel.error,
91 # and place those classes in IPython.core.error.
92
93 class RemoteError(Exception):
94 pass
95
96 def _import_remote_error():
97 global RemoteError
98 try:
99 from IPython.parallel.error import RemoteError
100 except:
101 pass
102
103 _import_remote_error()
104
105 89 #-----------------------------------------------------------------------------
106 90 # Globals
107 91 #-----------------------------------------------------------------------------
108 92
109 93 # compiled regexps for autoindent management
110 94 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
111 95
112 96 #-----------------------------------------------------------------------------
113 97 # Utilities
114 98 #-----------------------------------------------------------------------------
115 99
116 100 def softspace(file, newvalue):
117 101 """Copied from code.py, to remove the dependency"""
118 102
119 103 oldvalue = 0
120 104 try:
121 105 oldvalue = file.softspace
122 106 except AttributeError:
123 107 pass
124 108 try:
125 109 file.softspace = newvalue
126 110 except (AttributeError, TypeError):
127 111 # "attribute-less object" or "read-only attributes"
128 112 pass
129 113 return oldvalue
130 114
131 115
132 116 def no_op(*a, **kw): pass
133 117
134 118 class NoOpContext(object):
135 119 def __enter__(self): pass
136 120 def __exit__(self, type, value, traceback): pass
137 121 no_op_context = NoOpContext()
138 122
139 123 class SpaceInInput(Exception): pass
140 124
141 125 class Bunch: pass
142 126
143 127
144 128 def get_default_colors():
145 129 if sys.platform=='darwin':
146 130 return "LightBG"
147 131 elif os.name=='nt':
148 132 return 'Linux'
149 133 else:
150 134 return 'Linux'
151 135
152 136
153 137 class SeparateUnicode(Unicode):
154 138 """A Unicode subclass to validate separate_in, separate_out, etc.
155 139
156 140 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
157 141 """
158 142
159 143 def validate(self, obj, value):
160 144 if value == '0': value = ''
161 145 value = value.replace('\\n','\n')
162 146 return super(SeparateUnicode, self).validate(obj, value)
163 147
164 148
165 149 class ReadlineNoRecord(object):
166 150 """Context manager to execute some code, then reload readline history
167 151 so that interactive input to the code doesn't appear when pressing up."""
168 152 def __init__(self, shell):
169 153 self.shell = shell
170 154 self._nested_level = 0
171 155
172 156 def __enter__(self):
173 157 if self._nested_level == 0:
174 158 try:
175 159 self.orig_length = self.current_length()
176 160 self.readline_tail = self.get_readline_tail()
177 161 except (AttributeError, IndexError): # Can fail with pyreadline
178 162 self.orig_length, self.readline_tail = 999999, []
179 163 self._nested_level += 1
180 164
181 165 def __exit__(self, type, value, traceback):
182 166 self._nested_level -= 1
183 167 if self._nested_level == 0:
184 168 # Try clipping the end if it's got longer
185 169 try:
186 170 e = self.current_length() - self.orig_length
187 171 if e > 0:
188 172 for _ in range(e):
189 173 self.shell.readline.remove_history_item(self.orig_length)
190 174
191 175 # If it still doesn't match, just reload readline history.
192 176 if self.current_length() != self.orig_length \
193 177 or self.get_readline_tail() != self.readline_tail:
194 178 self.shell.refill_readline_hist()
195 179 except (AttributeError, IndexError):
196 180 pass
197 181 # Returning False will cause exceptions to propagate
198 182 return False
199 183
200 184 def current_length(self):
201 185 return self.shell.readline.get_current_history_length()
202 186
203 187 def get_readline_tail(self, n=10):
204 188 """Get the last n items in readline history."""
205 189 end = self.shell.readline.get_current_history_length() + 1
206 190 start = max(end-n, 1)
207 191 ghi = self.shell.readline.get_history_item
208 192 return [ghi(x) for x in range(start, end)]
209 193
210 194 #-----------------------------------------------------------------------------
211 195 # Main IPython class
212 196 #-----------------------------------------------------------------------------
213 197
214 198 class InteractiveShell(SingletonConfigurable):
215 199 """An enhanced, interactive shell for Python."""
216 200
217 201 _instance = None
218 202
219 203 autocall = Enum((0,1,2), default_value=0, config=True, help=
220 204 """
221 205 Make IPython automatically call any callable object even if you didn't
222 206 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
223 207 automatically. The value can be '0' to disable the feature, '1' for
224 208 'smart' autocall, where it is not applied if there are no more
225 209 arguments on the line, and '2' for 'full' autocall, where all callable
226 210 objects are automatically called (even if no arguments are present).
227 211 """
228 212 )
229 213 # TODO: remove all autoindent logic and put into frontends.
230 214 # We can't do this yet because even runlines uses the autoindent.
231 215 autoindent = CBool(True, config=True, help=
232 216 """
233 217 Autoindent IPython code entered interactively.
234 218 """
235 219 )
236 220 automagic = CBool(True, config=True, help=
237 221 """
238 222 Enable magic commands to be called without the leading %.
239 223 """
240 224 )
241 225 cache_size = Integer(1000, config=True, help=
242 226 """
243 227 Set the size of the output cache. The default is 1000, you can
244 228 change it permanently in your config file. Setting it to 0 completely
245 229 disables the caching system, and the minimum value accepted is 20 (if
246 230 you provide a value less than 20, it is reset to 0 and a warning is
247 231 issued). This limit is defined because otherwise you'll spend more
248 232 time re-flushing a too small cache than working
249 233 """
250 234 )
251 235 color_info = CBool(True, config=True, help=
252 236 """
253 237 Use colors for displaying information about objects. Because this
254 238 information is passed through a pager (like 'less'), and some pagers
255 239 get confused with color codes, this capability can be turned off.
256 240 """
257 241 )
258 242 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
259 243 default_value=get_default_colors(), config=True,
260 244 help="Set the color scheme (NoColor, Linux, or LightBG)."
261 245 )
262 246 colors_force = CBool(False, help=
263 247 """
264 248 Force use of ANSI color codes, regardless of OS and readline
265 249 availability.
266 250 """
267 251 # FIXME: This is essentially a hack to allow ZMQShell to show colors
268 252 # without readline on Win32. When the ZMQ formatting system is
269 253 # refactored, this should be removed.
270 254 )
271 255 debug = CBool(False, config=True)
272 256 deep_reload = CBool(False, config=True, help=
273 257 """
274 258 Enable deep (recursive) reloading by default. IPython can use the
275 259 deep_reload module which reloads changes in modules recursively (it
276 260 replaces the reload() function, so you don't need to change anything to
277 261 use it). deep_reload() forces a full reload of modules whose code may
278 262 have changed, which the default reload() function does not. When
279 263 deep_reload is off, IPython will use the normal reload(), but
280 264 deep_reload will still be available as dreload().
281 265 """
282 266 )
283 267 disable_failing_post_execute = CBool(False, config=True,
284 268 help="Don't call post-execute functions that have failed in the past."
285 269 )
286 270 display_formatter = Instance(DisplayFormatter)
287 271 displayhook_class = Type(DisplayHook)
288 272 display_pub_class = Type(DisplayPublisher)
289 273
290 274 exit_now = CBool(False)
291 275 exiter = Instance(ExitAutocall)
292 276 def _exiter_default(self):
293 277 return ExitAutocall(self)
294 278 # Monotonically increasing execution counter
295 279 execution_count = Integer(1)
296 280 filename = Unicode("<ipython console>")
297 281 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
298 282
299 283 # Input splitter, to split entire cells of input into either individual
300 284 # interactive statements or whole blocks.
301 285 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
302 286 (), {})
303 287 logstart = CBool(False, config=True, help=
304 288 """
305 289 Start logging to the default log file.
306 290 """
307 291 )
308 292 logfile = Unicode('', config=True, help=
309 293 """
310 294 The name of the logfile to use.
311 295 """
312 296 )
313 297 logappend = Unicode('', config=True, help=
314 298 """
315 299 Start logging to the given file in append mode.
316 300 """
317 301 )
318 302 object_info_string_level = Enum((0,1,2), default_value=0,
319 303 config=True)
320 304 pdb = CBool(False, config=True, help=
321 305 """
322 306 Automatically call the pdb debugger after every exception.
323 307 """
324 308 )
325 309 multiline_history = CBool(sys.platform != 'win32', config=True,
326 310 help="Save multi-line entries as one entry in readline history"
327 311 )
328 312
329 313 # deprecated prompt traits:
330 314
331 315 prompt_in1 = Unicode('In [\\#]: ', config=True,
332 316 help="Deprecated, use PromptManager.in_template")
333 317 prompt_in2 = Unicode(' .\\D.: ', config=True,
334 318 help="Deprecated, use PromptManager.in2_template")
335 319 prompt_out = Unicode('Out[\\#]: ', config=True,
336 320 help="Deprecated, use PromptManager.out_template")
337 321 prompts_pad_left = CBool(True, config=True,
338 322 help="Deprecated, use PromptManager.justify")
339 323
340 324 def _prompt_trait_changed(self, name, old, new):
341 325 table = {
342 326 'prompt_in1' : 'in_template',
343 327 'prompt_in2' : 'in2_template',
344 328 'prompt_out' : 'out_template',
345 329 'prompts_pad_left' : 'justify',
346 330 }
347 331 warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}\n".format(
348 332 name=name, newname=table[name])
349 333 )
350 334 # protect against weird cases where self.config may not exist:
351 335 if self.config is not None:
352 336 # propagate to corresponding PromptManager trait
353 337 setattr(self.config.PromptManager, table[name], new)
354 338
355 339 _prompt_in1_changed = _prompt_trait_changed
356 340 _prompt_in2_changed = _prompt_trait_changed
357 341 _prompt_out_changed = _prompt_trait_changed
358 342 _prompt_pad_left_changed = _prompt_trait_changed
359 343
360 344 show_rewritten_input = CBool(True, config=True,
361 345 help="Show rewritten input, e.g. for autocall."
362 346 )
363 347
364 348 quiet = CBool(False, config=True)
365 349
366 350 history_length = Integer(10000, config=True)
367 351
368 352 # The readline stuff will eventually be moved to the terminal subclass
369 353 # but for now, we can't do that as readline is welded in everywhere.
370 354 readline_use = CBool(True, config=True)
371 355 readline_remove_delims = Unicode('-/~', config=True)
372 356 # don't use \M- bindings by default, because they
373 357 # conflict with 8-bit encodings. See gh-58,gh-88
374 358 readline_parse_and_bind = List([
375 359 'tab: complete',
376 360 '"\C-l": clear-screen',
377 361 'set show-all-if-ambiguous on',
378 362 '"\C-o": tab-insert',
379 363 '"\C-r": reverse-search-history',
380 364 '"\C-s": forward-search-history',
381 365 '"\C-p": history-search-backward',
382 366 '"\C-n": history-search-forward',
383 367 '"\e[A": history-search-backward',
384 368 '"\e[B": history-search-forward',
385 369 '"\C-k": kill-line',
386 370 '"\C-u": unix-line-discard',
387 371 ], allow_none=False, config=True)
388 372
389 373 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
390 374 default_value='last_expr', config=True,
391 375 help="""
392 376 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
393 377 run interactively (displaying output from expressions).""")
394 378
395 379 # TODO: this part of prompt management should be moved to the frontends.
396 380 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
397 381 separate_in = SeparateUnicode('\n', config=True)
398 382 separate_out = SeparateUnicode('', config=True)
399 383 separate_out2 = SeparateUnicode('', config=True)
400 384 wildcards_case_sensitive = CBool(True, config=True)
401 385 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
402 386 default_value='Context', config=True)
403 387
404 388 # Subcomponents of InteractiveShell
405 389 alias_manager = Instance('IPython.core.alias.AliasManager')
406 390 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
407 391 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
408 392 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
409 393 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
410 394 plugin_manager = Instance('IPython.core.plugin.PluginManager')
411 395 payload_manager = Instance('IPython.core.payload.PayloadManager')
412 396 history_manager = Instance('IPython.core.history.HistoryManager')
413 397 magics_manager = Instance('IPython.core.magic.MagicsManager')
414 398
415 399 profile_dir = Instance('IPython.core.application.ProfileDir')
416 400 @property
417 401 def profile(self):
418 402 if self.profile_dir is not None:
419 403 name = os.path.basename(self.profile_dir.location)
420 404 return name.replace('profile_','')
421 405
422 406
423 407 # Private interface
424 408 _post_execute = Instance(dict)
425 409
426 410 # Tracks any GUI loop loaded for pylab
427 411 pylab_gui_select = None
428 412
429 413 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
430 414 user_module=None, user_ns=None,
431 415 custom_exceptions=((), None)):
432 416
433 417 # This is where traits with a config_key argument are updated
434 418 # from the values on config.
435 419 super(InteractiveShell, self).__init__(config=config)
436 420 self.configurables = [self]
437 421
438 422 # These are relatively independent and stateless
439 423 self.init_ipython_dir(ipython_dir)
440 424 self.init_profile_dir(profile_dir)
441 425 self.init_instance_attrs()
442 426 self.init_environment()
443 427
444 428 # Check if we're in a virtualenv, and set up sys.path.
445 429 self.init_virtualenv()
446 430
447 431 # Create namespaces (user_ns, user_global_ns, etc.)
448 432 self.init_create_namespaces(user_module, user_ns)
449 433 # This has to be done after init_create_namespaces because it uses
450 434 # something in self.user_ns, but before init_sys_modules, which
451 435 # is the first thing to modify sys.
452 436 # TODO: When we override sys.stdout and sys.stderr before this class
453 437 # is created, we are saving the overridden ones here. Not sure if this
454 438 # is what we want to do.
455 439 self.save_sys_module_state()
456 440 self.init_sys_modules()
457 441
458 442 # While we're trying to have each part of the code directly access what
459 443 # it needs without keeping redundant references to objects, we have too
460 444 # much legacy code that expects ip.db to exist.
461 445 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
462 446
463 447 self.init_history()
464 448 self.init_encoding()
465 449 self.init_prefilter()
466 450
467 451 self.init_syntax_highlighting()
468 452 self.init_hooks()
469 453 self.init_pushd_popd_magic()
470 454 # self.init_traceback_handlers use to be here, but we moved it below
471 455 # because it and init_io have to come after init_readline.
472 456 self.init_user_ns()
473 457 self.init_logger()
474 458 self.init_alias()
475 459 self.init_builtins()
476 460
477 461 # The following was in post_config_initialization
478 462 self.init_inspector()
479 463 # init_readline() must come before init_io(), because init_io uses
480 464 # readline related things.
481 465 self.init_readline()
482 466 # We save this here in case user code replaces raw_input, but it needs
483 467 # to be after init_readline(), because PyPy's readline works by replacing
484 468 # raw_input.
485 469 if py3compat.PY3:
486 470 self.raw_input_original = input
487 471 else:
488 472 self.raw_input_original = raw_input
489 473 # init_completer must come after init_readline, because it needs to
490 474 # know whether readline is present or not system-wide to configure the
491 475 # completers, since the completion machinery can now operate
492 476 # independently of readline (e.g. over the network)
493 477 self.init_completer()
494 478 # TODO: init_io() needs to happen before init_traceback handlers
495 479 # because the traceback handlers hardcode the stdout/stderr streams.
496 480 # This logic in in debugger.Pdb and should eventually be changed.
497 481 self.init_io()
498 482 self.init_traceback_handlers(custom_exceptions)
499 483 self.init_prompts()
500 484 self.init_display_formatter()
501 485 self.init_display_pub()
502 486 self.init_displayhook()
503 487 self.init_reload_doctest()
504 488 self.init_latextool()
505 489 self.init_magics()
506 490 self.init_logstart()
507 491 self.init_pdb()
508 492 self.init_extension_manager()
509 493 self.init_plugin_manager()
510 494 self.init_payload()
511 495 self.hooks.late_startup_hook()
512 496 atexit.register(self.atexit_operations)
513 497
514 498 def get_ipython(self):
515 499 """Return the currently running IPython instance."""
516 500 return self
517 501
518 502 #-------------------------------------------------------------------------
519 503 # Trait changed handlers
520 504 #-------------------------------------------------------------------------
521 505
522 506 def _ipython_dir_changed(self, name, new):
523 507 if not os.path.isdir(new):
524 508 os.makedirs(new, mode = 0777)
525 509
526 510 def set_autoindent(self,value=None):
527 511 """Set the autoindent flag, checking for readline support.
528 512
529 513 If called with no arguments, it acts as a toggle."""
530 514
531 515 if value != 0 and not self.has_readline:
532 516 if os.name == 'posix':
533 517 warn("The auto-indent feature requires the readline library")
534 518 self.autoindent = 0
535 519 return
536 520 if value is None:
537 521 self.autoindent = not self.autoindent
538 522 else:
539 523 self.autoindent = value
540 524
541 525 #-------------------------------------------------------------------------
542 526 # init_* methods called by __init__
543 527 #-------------------------------------------------------------------------
544 528
545 529 def init_ipython_dir(self, ipython_dir):
546 530 if ipython_dir is not None:
547 531 self.ipython_dir = ipython_dir
548 532 return
549 533
550 534 self.ipython_dir = get_ipython_dir()
551 535
552 536 def init_profile_dir(self, profile_dir):
553 537 if profile_dir is not None:
554 538 self.profile_dir = profile_dir
555 539 return
556 540 self.profile_dir =\
557 541 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
558 542
559 543 def init_instance_attrs(self):
560 544 self.more = False
561 545
562 546 # command compiler
563 547 self.compile = CachingCompiler()
564 548
565 549 # Make an empty namespace, which extension writers can rely on both
566 550 # existing and NEVER being used by ipython itself. This gives them a
567 551 # convenient location for storing additional information and state
568 552 # their extensions may require, without fear of collisions with other
569 553 # ipython names that may develop later.
570 554 self.meta = Struct()
571 555
572 556 # Temporary files used for various purposes. Deleted at exit.
573 557 self.tempfiles = []
574 558
575 559 # Keep track of readline usage (later set by init_readline)
576 560 self.has_readline = False
577 561
578 562 # keep track of where we started running (mainly for crash post-mortem)
579 563 # This is not being used anywhere currently.
580 564 self.starting_dir = os.getcwdu()
581 565
582 566 # Indentation management
583 567 self.indent_current_nsp = 0
584 568
585 569 # Dict to track post-execution functions that have been registered
586 570 self._post_execute = {}
587 571
588 572 def init_environment(self):
589 573 """Any changes we need to make to the user's environment."""
590 574 pass
591 575
592 576 def init_encoding(self):
593 577 # Get system encoding at startup time. Certain terminals (like Emacs
594 578 # under Win32 have it set to None, and we need to have a known valid
595 579 # encoding to use in the raw_input() method
596 580 try:
597 581 self.stdin_encoding = sys.stdin.encoding or 'ascii'
598 582 except AttributeError:
599 583 self.stdin_encoding = 'ascii'
600 584
601 585 def init_syntax_highlighting(self):
602 586 # Python source parser/formatter for syntax highlighting
603 587 pyformat = PyColorize.Parser().format
604 588 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
605 589
606 590 def init_pushd_popd_magic(self):
607 591 # for pushd/popd management
608 592 self.home_dir = get_home_dir()
609 593
610 594 self.dir_stack = []
611 595
612 596 def init_logger(self):
613 597 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
614 598 logmode='rotate')
615 599
616 600 def init_logstart(self):
617 601 """Initialize logging in case it was requested at the command line.
618 602 """
619 603 if self.logappend:
620 604 self.magic('logstart %s append' % self.logappend)
621 605 elif self.logfile:
622 606 self.magic('logstart %s' % self.logfile)
623 607 elif self.logstart:
624 608 self.magic('logstart')
625 609
626 610 def init_builtins(self):
627 611 # A single, static flag that we set to True. Its presence indicates
628 612 # that an IPython shell has been created, and we make no attempts at
629 613 # removing on exit or representing the existence of more than one
630 614 # IPython at a time.
631 615 builtin_mod.__dict__['__IPYTHON__'] = True
632 616
633 617 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
634 618 # manage on enter/exit, but with all our shells it's virtually
635 619 # impossible to get all the cases right. We're leaving the name in for
636 620 # those who adapted their codes to check for this flag, but will
637 621 # eventually remove it after a few more releases.
638 622 builtin_mod.__dict__['__IPYTHON__active'] = \
639 623 'Deprecated, check for __IPYTHON__'
640 624
641 625 self.builtin_trap = BuiltinTrap(shell=self)
642 626
643 627 def init_inspector(self):
644 628 # Object inspector
645 629 self.inspector = oinspect.Inspector(oinspect.InspectColors,
646 630 PyColorize.ANSICodeColors,
647 631 'NoColor',
648 632 self.object_info_string_level)
649 633
650 634 def init_io(self):
651 635 # This will just use sys.stdout and sys.stderr. If you want to
652 636 # override sys.stdout and sys.stderr themselves, you need to do that
653 637 # *before* instantiating this class, because io holds onto
654 638 # references to the underlying streams.
655 639 if sys.platform == 'win32' and self.has_readline:
656 640 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
657 641 else:
658 642 io.stdout = io.IOStream(sys.stdout)
659 643 io.stderr = io.IOStream(sys.stderr)
660 644
661 645 def init_prompts(self):
662 646 self.prompt_manager = PromptManager(shell=self, config=self.config)
663 647 self.configurables.append(self.prompt_manager)
664 648 # Set system prompts, so that scripts can decide if they are running
665 649 # interactively.
666 650 sys.ps1 = 'In : '
667 651 sys.ps2 = '...: '
668 652 sys.ps3 = 'Out: '
669 653
670 654 def init_display_formatter(self):
671 655 self.display_formatter = DisplayFormatter(config=self.config)
672 656 self.configurables.append(self.display_formatter)
673 657
674 658 def init_display_pub(self):
675 659 self.display_pub = self.display_pub_class(config=self.config)
676 660 self.configurables.append(self.display_pub)
677 661
678 662 def init_displayhook(self):
679 663 # Initialize displayhook, set in/out prompts and printing system
680 664 self.displayhook = self.displayhook_class(
681 665 config=self.config,
682 666 shell=self,
683 667 cache_size=self.cache_size,
684 668 )
685 669 self.configurables.append(self.displayhook)
686 670 # This is a context manager that installs/revmoes the displayhook at
687 671 # the appropriate time.
688 672 self.display_trap = DisplayTrap(hook=self.displayhook)
689 673
690 674 def init_reload_doctest(self):
691 675 # Do a proper resetting of doctest, including the necessary displayhook
692 676 # monkeypatching
693 677 try:
694 678 doctest_reload()
695 679 except ImportError:
696 680 warn("doctest module does not exist.")
697 681
698 682 def init_latextool(self):
699 683 """Configure LaTeXTool."""
700 684 cfg = LaTeXTool.instance(config=self.config)
701 685 if cfg not in self.configurables:
702 686 self.configurables.append(cfg)
703 687
704 688 def init_virtualenv(self):
705 689 """Add a virtualenv to sys.path so the user can import modules from it.
706 690 This isn't perfect: it doesn't use the Python interpreter with which the
707 691 virtualenv was built, and it ignores the --no-site-packages option. A
708 692 warning will appear suggesting the user installs IPython in the
709 693 virtualenv, but for many cases, it probably works well enough.
710 694
711 695 Adapted from code snippets online.
712 696
713 697 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
714 698 """
715 699 if 'VIRTUAL_ENV' not in os.environ:
716 700 # Not in a virtualenv
717 701 return
718 702
719 703 if sys.executable.startswith(os.environ['VIRTUAL_ENV']):
720 704 # Running properly in the virtualenv, don't need to do anything
721 705 return
722 706
723 707 warn("Attempting to work in a virtualenv. If you encounter problems, please "
724 708 "install IPython inside the virtualenv.\n")
725 709 if sys.platform == "win32":
726 710 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
727 711 else:
728 712 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
729 713 'python%d.%d' % sys.version_info[:2], 'site-packages')
730 714
731 715 import site
732 716 sys.path.insert(0, virtual_env)
733 717 site.addsitedir(virtual_env)
734 718
735 719 #-------------------------------------------------------------------------
736 720 # Things related to injections into the sys module
737 721 #-------------------------------------------------------------------------
738 722
739 723 def save_sys_module_state(self):
740 724 """Save the state of hooks in the sys module.
741 725
742 726 This has to be called after self.user_module is created.
743 727 """
744 728 self._orig_sys_module_state = {}
745 729 self._orig_sys_module_state['stdin'] = sys.stdin
746 730 self._orig_sys_module_state['stdout'] = sys.stdout
747 731 self._orig_sys_module_state['stderr'] = sys.stderr
748 732 self._orig_sys_module_state['excepthook'] = sys.excepthook
749 733 self._orig_sys_modules_main_name = self.user_module.__name__
750 734 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
751 735
752 736 def restore_sys_module_state(self):
753 737 """Restore the state of the sys module."""
754 738 try:
755 739 for k, v in self._orig_sys_module_state.iteritems():
756 740 setattr(sys, k, v)
757 741 except AttributeError:
758 742 pass
759 743 # Reset what what done in self.init_sys_modules
760 744 if self._orig_sys_modules_main_mod is not None:
761 745 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
762 746
763 747 #-------------------------------------------------------------------------
764 748 # Things related to hooks
765 749 #-------------------------------------------------------------------------
766 750
767 751 def init_hooks(self):
768 752 # hooks holds pointers used for user-side customizations
769 753 self.hooks = Struct()
770 754
771 755 self.strdispatchers = {}
772 756
773 757 # Set all default hooks, defined in the IPython.hooks module.
774 758 hooks = IPython.core.hooks
775 759 for hook_name in hooks.__all__:
776 760 # default hooks have priority 100, i.e. low; user hooks should have
777 761 # 0-100 priority
778 762 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
779 763
780 764 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
781 765 """set_hook(name,hook) -> sets an internal IPython hook.
782 766
783 767 IPython exposes some of its internal API as user-modifiable hooks. By
784 768 adding your function to one of these hooks, you can modify IPython's
785 769 behavior to call at runtime your own routines."""
786 770
787 771 # At some point in the future, this should validate the hook before it
788 772 # accepts it. Probably at least check that the hook takes the number
789 773 # of args it's supposed to.
790 774
791 775 f = types.MethodType(hook,self)
792 776
793 777 # check if the hook is for strdispatcher first
794 778 if str_key is not None:
795 779 sdp = self.strdispatchers.get(name, StrDispatch())
796 780 sdp.add_s(str_key, f, priority )
797 781 self.strdispatchers[name] = sdp
798 782 return
799 783 if re_key is not None:
800 784 sdp = self.strdispatchers.get(name, StrDispatch())
801 785 sdp.add_re(re.compile(re_key), f, priority )
802 786 self.strdispatchers[name] = sdp
803 787 return
804 788
805 789 dp = getattr(self.hooks, name, None)
806 790 if name not in IPython.core.hooks.__all__:
807 791 print("Warning! Hook '%s' is not one of %s" % \
808 792 (name, IPython.core.hooks.__all__ ))
809 793 if not dp:
810 794 dp = IPython.core.hooks.CommandChainDispatcher()
811 795
812 796 try:
813 797 dp.add(f,priority)
814 798 except AttributeError:
815 799 # it was not commandchain, plain old func - replace
816 800 dp = f
817 801
818 802 setattr(self.hooks,name, dp)
819 803
820 804 def register_post_execute(self, func):
821 805 """Register a function for calling after code execution.
822 806 """
823 807 if not callable(func):
824 808 raise ValueError('argument %s must be callable' % func)
825 809 self._post_execute[func] = True
826 810
827 811 #-------------------------------------------------------------------------
828 812 # Things related to the "main" module
829 813 #-------------------------------------------------------------------------
830 814
831 815 def new_main_mod(self,ns=None):
832 816 """Return a new 'main' module object for user code execution.
833 817 """
834 818 main_mod = self._user_main_module
835 819 init_fakemod_dict(main_mod,ns)
836 820 return main_mod
837 821
838 822 def cache_main_mod(self,ns,fname):
839 823 """Cache a main module's namespace.
840 824
841 825 When scripts are executed via %run, we must keep a reference to the
842 826 namespace of their __main__ module (a FakeModule instance) around so
843 827 that Python doesn't clear it, rendering objects defined therein
844 828 useless.
845 829
846 830 This method keeps said reference in a private dict, keyed by the
847 831 absolute path of the module object (which corresponds to the script
848 832 path). This way, for multiple executions of the same script we only
849 833 keep one copy of the namespace (the last one), thus preventing memory
850 834 leaks from old references while allowing the objects from the last
851 835 execution to be accessible.
852 836
853 837 Note: we can not allow the actual FakeModule instances to be deleted,
854 838 because of how Python tears down modules (it hard-sets all their
855 839 references to None without regard for reference counts). This method
856 840 must therefore make a *copy* of the given namespace, to allow the
857 841 original module's __dict__ to be cleared and reused.
858 842
859 843
860 844 Parameters
861 845 ----------
862 846 ns : a namespace (a dict, typically)
863 847
864 848 fname : str
865 849 Filename associated with the namespace.
866 850
867 851 Examples
868 852 --------
869 853
870 854 In [10]: import IPython
871 855
872 856 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
873 857
874 858 In [12]: IPython.__file__ in _ip._main_ns_cache
875 859 Out[12]: True
876 860 """
877 861 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
878 862
879 863 def clear_main_mod_cache(self):
880 864 """Clear the cache of main modules.
881 865
882 866 Mainly for use by utilities like %reset.
883 867
884 868 Examples
885 869 --------
886 870
887 871 In [15]: import IPython
888 872
889 873 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
890 874
891 875 In [17]: len(_ip._main_ns_cache) > 0
892 876 Out[17]: True
893 877
894 878 In [18]: _ip.clear_main_mod_cache()
895 879
896 880 In [19]: len(_ip._main_ns_cache) == 0
897 881 Out[19]: True
898 882 """
899 883 self._main_ns_cache.clear()
900 884
901 885 #-------------------------------------------------------------------------
902 886 # Things related to debugging
903 887 #-------------------------------------------------------------------------
904 888
905 889 def init_pdb(self):
906 890 # Set calling of pdb on exceptions
907 891 # self.call_pdb is a property
908 892 self.call_pdb = self.pdb
909 893
910 894 def _get_call_pdb(self):
911 895 return self._call_pdb
912 896
913 897 def _set_call_pdb(self,val):
914 898
915 899 if val not in (0,1,False,True):
916 900 raise ValueError('new call_pdb value must be boolean')
917 901
918 902 # store value in instance
919 903 self._call_pdb = val
920 904
921 905 # notify the actual exception handlers
922 906 self.InteractiveTB.call_pdb = val
923 907
924 908 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
925 909 'Control auto-activation of pdb at exceptions')
926 910
927 911 def debugger(self,force=False):
928 912 """Call the pydb/pdb debugger.
929 913
930 914 Keywords:
931 915
932 916 - force(False): by default, this routine checks the instance call_pdb
933 917 flag and does not actually invoke the debugger if the flag is false.
934 918 The 'force' option forces the debugger to activate even if the flag
935 919 is false.
936 920 """
937 921
938 922 if not (force or self.call_pdb):
939 923 return
940 924
941 925 if not hasattr(sys,'last_traceback'):
942 926 error('No traceback has been produced, nothing to debug.')
943 927 return
944 928
945 929 # use pydb if available
946 930 if debugger.has_pydb:
947 931 from pydb import pm
948 932 else:
949 933 # fallback to our internal debugger
950 934 pm = lambda : self.InteractiveTB.debugger(force=True)
951 935
952 936 with self.readline_no_record:
953 937 pm()
954 938
955 939 #-------------------------------------------------------------------------
956 940 # Things related to IPython's various namespaces
957 941 #-------------------------------------------------------------------------
958 942 default_user_namespaces = True
959 943
960 944 def init_create_namespaces(self, user_module=None, user_ns=None):
961 945 # Create the namespace where the user will operate. user_ns is
962 946 # normally the only one used, and it is passed to the exec calls as
963 947 # the locals argument. But we do carry a user_global_ns namespace
964 948 # given as the exec 'globals' argument, This is useful in embedding
965 949 # situations where the ipython shell opens in a context where the
966 950 # distinction between locals and globals is meaningful. For
967 951 # non-embedded contexts, it is just the same object as the user_ns dict.
968 952
969 953 # FIXME. For some strange reason, __builtins__ is showing up at user
970 954 # level as a dict instead of a module. This is a manual fix, but I
971 955 # should really track down where the problem is coming from. Alex
972 956 # Schmolck reported this problem first.
973 957
974 958 # A useful post by Alex Martelli on this topic:
975 959 # Re: inconsistent value from __builtins__
976 960 # Von: Alex Martelli <aleaxit@yahoo.com>
977 961 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
978 962 # Gruppen: comp.lang.python
979 963
980 964 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
981 965 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
982 966 # > <type 'dict'>
983 967 # > >>> print type(__builtins__)
984 968 # > <type 'module'>
985 969 # > Is this difference in return value intentional?
986 970
987 971 # Well, it's documented that '__builtins__' can be either a dictionary
988 972 # or a module, and it's been that way for a long time. Whether it's
989 973 # intentional (or sensible), I don't know. In any case, the idea is
990 974 # that if you need to access the built-in namespace directly, you
991 975 # should start with "import __builtin__" (note, no 's') which will
992 976 # definitely give you a module. Yeah, it's somewhat confusing:-(.
993 977
994 978 # These routines return a properly built module and dict as needed by
995 979 # the rest of the code, and can also be used by extension writers to
996 980 # generate properly initialized namespaces.
997 981 if (user_ns is not None) or (user_module is not None):
998 982 self.default_user_namespaces = False
999 983 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1000 984
1001 985 # A record of hidden variables we have added to the user namespace, so
1002 986 # we can list later only variables defined in actual interactive use.
1003 987 self.user_ns_hidden = set()
1004 988
1005 989 # Now that FakeModule produces a real module, we've run into a nasty
1006 990 # problem: after script execution (via %run), the module where the user
1007 991 # code ran is deleted. Now that this object is a true module (needed
1008 992 # so docetst and other tools work correctly), the Python module
1009 993 # teardown mechanism runs over it, and sets to None every variable
1010 994 # present in that module. Top-level references to objects from the
1011 995 # script survive, because the user_ns is updated with them. However,
1012 996 # calling functions defined in the script that use other things from
1013 997 # the script will fail, because the function's closure had references
1014 998 # to the original objects, which are now all None. So we must protect
1015 999 # these modules from deletion by keeping a cache.
1016 1000 #
1017 1001 # To avoid keeping stale modules around (we only need the one from the
1018 1002 # last run), we use a dict keyed with the full path to the script, so
1019 1003 # only the last version of the module is held in the cache. Note,
1020 1004 # however, that we must cache the module *namespace contents* (their
1021 1005 # __dict__). Because if we try to cache the actual modules, old ones
1022 1006 # (uncached) could be destroyed while still holding references (such as
1023 1007 # those held by GUI objects that tend to be long-lived)>
1024 1008 #
1025 1009 # The %reset command will flush this cache. See the cache_main_mod()
1026 1010 # and clear_main_mod_cache() methods for details on use.
1027 1011
1028 1012 # This is the cache used for 'main' namespaces
1029 1013 self._main_ns_cache = {}
1030 1014 # And this is the single instance of FakeModule whose __dict__ we keep
1031 1015 # copying and clearing for reuse on each %run
1032 1016 self._user_main_module = FakeModule()
1033 1017
1034 1018 # A table holding all the namespaces IPython deals with, so that
1035 1019 # introspection facilities can search easily.
1036 1020 self.ns_table = {'user_global':self.user_module.__dict__,
1037 1021 'user_local':self.user_ns,
1038 1022 'builtin':builtin_mod.__dict__
1039 1023 }
1040 1024
1041 1025 @property
1042 1026 def user_global_ns(self):
1043 1027 return self.user_module.__dict__
1044 1028
1045 1029 def prepare_user_module(self, user_module=None, user_ns=None):
1046 1030 """Prepare the module and namespace in which user code will be run.
1047 1031
1048 1032 When IPython is started normally, both parameters are None: a new module
1049 1033 is created automatically, and its __dict__ used as the namespace.
1050 1034
1051 1035 If only user_module is provided, its __dict__ is used as the namespace.
1052 1036 If only user_ns is provided, a dummy module is created, and user_ns
1053 1037 becomes the global namespace. If both are provided (as they may be
1054 1038 when embedding), user_ns is the local namespace, and user_module
1055 1039 provides the global namespace.
1056 1040
1057 1041 Parameters
1058 1042 ----------
1059 1043 user_module : module, optional
1060 1044 The current user module in which IPython is being run. If None,
1061 1045 a clean module will be created.
1062 1046 user_ns : dict, optional
1063 1047 A namespace in which to run interactive commands.
1064 1048
1065 1049 Returns
1066 1050 -------
1067 1051 A tuple of user_module and user_ns, each properly initialised.
1068 1052 """
1069 1053 if user_module is None and user_ns is not None:
1070 1054 user_ns.setdefault("__name__", "__main__")
1071 1055 class DummyMod(object):
1072 1056 "A dummy module used for IPython's interactive namespace."
1073 1057 pass
1074 1058 user_module = DummyMod()
1075 1059 user_module.__dict__ = user_ns
1076 1060
1077 1061 if user_module is None:
1078 1062 user_module = types.ModuleType("__main__",
1079 1063 doc="Automatically created module for IPython interactive environment")
1080 1064
1081 1065 # We must ensure that __builtin__ (without the final 's') is always
1082 1066 # available and pointing to the __builtin__ *module*. For more details:
1083 1067 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1084 1068 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1085 1069 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1086 1070
1087 1071 if user_ns is None:
1088 1072 user_ns = user_module.__dict__
1089 1073
1090 1074 return user_module, user_ns
1091 1075
1092 1076 def init_sys_modules(self):
1093 1077 # We need to insert into sys.modules something that looks like a
1094 1078 # module but which accesses the IPython namespace, for shelve and
1095 1079 # pickle to work interactively. Normally they rely on getting
1096 1080 # everything out of __main__, but for embedding purposes each IPython
1097 1081 # instance has its own private namespace, so we can't go shoving
1098 1082 # everything into __main__.
1099 1083
1100 1084 # note, however, that we should only do this for non-embedded
1101 1085 # ipythons, which really mimic the __main__.__dict__ with their own
1102 1086 # namespace. Embedded instances, on the other hand, should not do
1103 1087 # this because they need to manage the user local/global namespaces
1104 1088 # only, but they live within a 'normal' __main__ (meaning, they
1105 1089 # shouldn't overtake the execution environment of the script they're
1106 1090 # embedded in).
1107 1091
1108 1092 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1109 1093 main_name = self.user_module.__name__
1110 1094 sys.modules[main_name] = self.user_module
1111 1095
1112 1096 def init_user_ns(self):
1113 1097 """Initialize all user-visible namespaces to their minimum defaults.
1114 1098
1115 1099 Certain history lists are also initialized here, as they effectively
1116 1100 act as user namespaces.
1117 1101
1118 1102 Notes
1119 1103 -----
1120 1104 All data structures here are only filled in, they are NOT reset by this
1121 1105 method. If they were not empty before, data will simply be added to
1122 1106 therm.
1123 1107 """
1124 1108 # This function works in two parts: first we put a few things in
1125 1109 # user_ns, and we sync that contents into user_ns_hidden so that these
1126 1110 # initial variables aren't shown by %who. After the sync, we add the
1127 1111 # rest of what we *do* want the user to see with %who even on a new
1128 1112 # session (probably nothing, so theye really only see their own stuff)
1129 1113
1130 1114 # The user dict must *always* have a __builtin__ reference to the
1131 1115 # Python standard __builtin__ namespace, which must be imported.
1132 1116 # This is so that certain operations in prompt evaluation can be
1133 1117 # reliably executed with builtins. Note that we can NOT use
1134 1118 # __builtins__ (note the 's'), because that can either be a dict or a
1135 1119 # module, and can even mutate at runtime, depending on the context
1136 1120 # (Python makes no guarantees on it). In contrast, __builtin__ is
1137 1121 # always a module object, though it must be explicitly imported.
1138 1122
1139 1123 # For more details:
1140 1124 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1141 1125 ns = dict()
1142 1126
1143 1127 # Put 'help' in the user namespace
1144 1128 try:
1145 1129 from site import _Helper
1146 1130 ns['help'] = _Helper()
1147 1131 except ImportError:
1148 1132 warn('help() not available - check site.py')
1149 1133
1150 1134 # make global variables for user access to the histories
1151 1135 ns['_ih'] = self.history_manager.input_hist_parsed
1152 1136 ns['_oh'] = self.history_manager.output_hist
1153 1137 ns['_dh'] = self.history_manager.dir_hist
1154 1138
1155 1139 ns['_sh'] = shadowns
1156 1140
1157 1141 # user aliases to input and output histories. These shouldn't show up
1158 1142 # in %who, as they can have very large reprs.
1159 1143 ns['In'] = self.history_manager.input_hist_parsed
1160 1144 ns['Out'] = self.history_manager.output_hist
1161 1145
1162 1146 # Store myself as the public api!!!
1163 1147 ns['get_ipython'] = self.get_ipython
1164 1148
1165 1149 ns['exit'] = self.exiter
1166 1150 ns['quit'] = self.exiter
1167 1151
1168 1152 # Sync what we've added so far to user_ns_hidden so these aren't seen
1169 1153 # by %who
1170 1154 self.user_ns_hidden.update(ns)
1171 1155
1172 1156 # Anything put into ns now would show up in %who. Think twice before
1173 1157 # putting anything here, as we really want %who to show the user their
1174 1158 # stuff, not our variables.
1175 1159
1176 1160 # Finally, update the real user's namespace
1177 1161 self.user_ns.update(ns)
1178 1162
1179 1163 @property
1180 1164 def all_ns_refs(self):
1181 1165 """Get a list of references to all the namespace dictionaries in which
1182 1166 IPython might store a user-created object.
1183 1167
1184 1168 Note that this does not include the displayhook, which also caches
1185 1169 objects from the output."""
1186 1170 return [self.user_ns, self.user_global_ns,
1187 1171 self._user_main_module.__dict__] + self._main_ns_cache.values()
1188 1172
1189 1173 def reset(self, new_session=True):
1190 1174 """Clear all internal namespaces, and attempt to release references to
1191 1175 user objects.
1192 1176
1193 1177 If new_session is True, a new history session will be opened.
1194 1178 """
1195 1179 # Clear histories
1196 1180 self.history_manager.reset(new_session)
1197 1181 # Reset counter used to index all histories
1198 1182 if new_session:
1199 1183 self.execution_count = 1
1200 1184
1201 1185 # Flush cached output items
1202 1186 if self.displayhook.do_full_cache:
1203 1187 self.displayhook.flush()
1204 1188
1205 1189 # The main execution namespaces must be cleared very carefully,
1206 1190 # skipping the deletion of the builtin-related keys, because doing so
1207 1191 # would cause errors in many object's __del__ methods.
1208 1192 if self.user_ns is not self.user_global_ns:
1209 1193 self.user_ns.clear()
1210 1194 ns = self.user_global_ns
1211 1195 drop_keys = set(ns.keys())
1212 1196 drop_keys.discard('__builtin__')
1213 1197 drop_keys.discard('__builtins__')
1214 1198 drop_keys.discard('__name__')
1215 1199 for k in drop_keys:
1216 1200 del ns[k]
1217 1201
1218 1202 self.user_ns_hidden.clear()
1219 1203
1220 1204 # Restore the user namespaces to minimal usability
1221 1205 self.init_user_ns()
1222 1206
1223 1207 # Restore the default and user aliases
1224 1208 self.alias_manager.clear_aliases()
1225 1209 self.alias_manager.init_aliases()
1226 1210
1227 1211 # Flush the private list of module references kept for script
1228 1212 # execution protection
1229 1213 self.clear_main_mod_cache()
1230 1214
1231 1215 # Clear out the namespace from the last %run
1232 1216 self.new_main_mod()
1233 1217
1234 1218 def del_var(self, varname, by_name=False):
1235 1219 """Delete a variable from the various namespaces, so that, as
1236 1220 far as possible, we're not keeping any hidden references to it.
1237 1221
1238 1222 Parameters
1239 1223 ----------
1240 1224 varname : str
1241 1225 The name of the variable to delete.
1242 1226 by_name : bool
1243 1227 If True, delete variables with the given name in each
1244 1228 namespace. If False (default), find the variable in the user
1245 1229 namespace, and delete references to it.
1246 1230 """
1247 1231 if varname in ('__builtin__', '__builtins__'):
1248 1232 raise ValueError("Refusing to delete %s" % varname)
1249 1233
1250 1234 ns_refs = self.all_ns_refs
1251 1235
1252 1236 if by_name: # Delete by name
1253 1237 for ns in ns_refs:
1254 1238 try:
1255 1239 del ns[varname]
1256 1240 except KeyError:
1257 1241 pass
1258 1242 else: # Delete by object
1259 1243 try:
1260 1244 obj = self.user_ns[varname]
1261 1245 except KeyError:
1262 1246 raise NameError("name '%s' is not defined" % varname)
1263 1247 # Also check in output history
1264 1248 ns_refs.append(self.history_manager.output_hist)
1265 1249 for ns in ns_refs:
1266 1250 to_delete = [n for n, o in ns.iteritems() if o is obj]
1267 1251 for name in to_delete:
1268 1252 del ns[name]
1269 1253
1270 1254 # displayhook keeps extra references, but not in a dictionary
1271 1255 for name in ('_', '__', '___'):
1272 1256 if getattr(self.displayhook, name) is obj:
1273 1257 setattr(self.displayhook, name, None)
1274 1258
1275 1259 def reset_selective(self, regex=None):
1276 1260 """Clear selective variables from internal namespaces based on a
1277 1261 specified regular expression.
1278 1262
1279 1263 Parameters
1280 1264 ----------
1281 1265 regex : string or compiled pattern, optional
1282 1266 A regular expression pattern that will be used in searching
1283 1267 variable names in the users namespaces.
1284 1268 """
1285 1269 if regex is not None:
1286 1270 try:
1287 1271 m = re.compile(regex)
1288 1272 except TypeError:
1289 1273 raise TypeError('regex must be a string or compiled pattern')
1290 1274 # Search for keys in each namespace that match the given regex
1291 1275 # If a match is found, delete the key/value pair.
1292 1276 for ns in self.all_ns_refs:
1293 1277 for var in ns:
1294 1278 if m.search(var):
1295 1279 del ns[var]
1296 1280
1297 1281 def push(self, variables, interactive=True):
1298 1282 """Inject a group of variables into the IPython user namespace.
1299 1283
1300 1284 Parameters
1301 1285 ----------
1302 1286 variables : dict, str or list/tuple of str
1303 1287 The variables to inject into the user's namespace. If a dict, a
1304 1288 simple update is done. If a str, the string is assumed to have
1305 1289 variable names separated by spaces. A list/tuple of str can also
1306 1290 be used to give the variable names. If just the variable names are
1307 1291 give (list/tuple/str) then the variable values looked up in the
1308 1292 callers frame.
1309 1293 interactive : bool
1310 1294 If True (default), the variables will be listed with the ``who``
1311 1295 magic.
1312 1296 """
1313 1297 vdict = None
1314 1298
1315 1299 # We need a dict of name/value pairs to do namespace updates.
1316 1300 if isinstance(variables, dict):
1317 1301 vdict = variables
1318 1302 elif isinstance(variables, (basestring, list, tuple)):
1319 1303 if isinstance(variables, basestring):
1320 1304 vlist = variables.split()
1321 1305 else:
1322 1306 vlist = variables
1323 1307 vdict = {}
1324 1308 cf = sys._getframe(1)
1325 1309 for name in vlist:
1326 1310 try:
1327 1311 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1328 1312 except:
1329 1313 print('Could not get variable %s from %s' %
1330 1314 (name,cf.f_code.co_name))
1331 1315 else:
1332 1316 raise ValueError('variables must be a dict/str/list/tuple')
1333 1317
1334 1318 # Propagate variables to user namespace
1335 1319 self.user_ns.update(vdict)
1336 1320
1337 1321 # And configure interactive visibility
1338 1322 user_ns_hidden = self.user_ns_hidden
1339 1323 if interactive:
1340 1324 user_ns_hidden.difference_update(vdict)
1341 1325 else:
1342 1326 user_ns_hidden.update(vdict)
1343 1327
1344 1328 def drop_by_id(self, variables):
1345 1329 """Remove a dict of variables from the user namespace, if they are the
1346 1330 same as the values in the dictionary.
1347 1331
1348 1332 This is intended for use by extensions: variables that they've added can
1349 1333 be taken back out if they are unloaded, without removing any that the
1350 1334 user has overwritten.
1351 1335
1352 1336 Parameters
1353 1337 ----------
1354 1338 variables : dict
1355 1339 A dictionary mapping object names (as strings) to the objects.
1356 1340 """
1357 1341 for name, obj in variables.iteritems():
1358 1342 if name in self.user_ns and self.user_ns[name] is obj:
1359 1343 del self.user_ns[name]
1360 1344 self.user_ns_hidden.discard(name)
1361 1345
1362 1346 #-------------------------------------------------------------------------
1363 1347 # Things related to object introspection
1364 1348 #-------------------------------------------------------------------------
1365 1349
1366 1350 def _ofind(self, oname, namespaces=None):
1367 1351 """Find an object in the available namespaces.
1368 1352
1369 1353 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1370 1354
1371 1355 Has special code to detect magic functions.
1372 1356 """
1373 1357 oname = oname.strip()
1374 1358 #print '1- oname: <%r>' % oname # dbg
1375 1359 if not oname.startswith(ESC_MAGIC) and \
1376 1360 not oname.startswith(ESC_MAGIC2) and \
1377 1361 not py3compat.isidentifier(oname, dotted=True):
1378 1362 return dict(found=False)
1379 1363
1380 1364 alias_ns = None
1381 1365 if namespaces is None:
1382 1366 # Namespaces to search in:
1383 1367 # Put them in a list. The order is important so that we
1384 1368 # find things in the same order that Python finds them.
1385 1369 namespaces = [ ('Interactive', self.user_ns),
1386 1370 ('Interactive (global)', self.user_global_ns),
1387 1371 ('Python builtin', builtin_mod.__dict__),
1388 1372 ('Alias', self.alias_manager.alias_table),
1389 1373 ]
1390 1374 alias_ns = self.alias_manager.alias_table
1391 1375
1392 1376 # initialize results to 'null'
1393 1377 found = False; obj = None; ospace = None; ds = None;
1394 1378 ismagic = False; isalias = False; parent = None
1395 1379
1396 1380 # We need to special-case 'print', which as of python2.6 registers as a
1397 1381 # function but should only be treated as one if print_function was
1398 1382 # loaded with a future import. In this case, just bail.
1399 1383 if (oname == 'print' and not py3compat.PY3 and not \
1400 1384 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1401 1385 return {'found':found, 'obj':obj, 'namespace':ospace,
1402 1386 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1403 1387
1404 1388 # Look for the given name by splitting it in parts. If the head is
1405 1389 # found, then we look for all the remaining parts as members, and only
1406 1390 # declare success if we can find them all.
1407 1391 oname_parts = oname.split('.')
1408 1392 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1409 1393 for nsname,ns in namespaces:
1410 1394 try:
1411 1395 obj = ns[oname_head]
1412 1396 except KeyError:
1413 1397 continue
1414 1398 else:
1415 1399 #print 'oname_rest:', oname_rest # dbg
1416 1400 for part in oname_rest:
1417 1401 try:
1418 1402 parent = obj
1419 1403 obj = getattr(obj,part)
1420 1404 except:
1421 1405 # Blanket except b/c some badly implemented objects
1422 1406 # allow __getattr__ to raise exceptions other than
1423 1407 # AttributeError, which then crashes IPython.
1424 1408 break
1425 1409 else:
1426 1410 # If we finish the for loop (no break), we got all members
1427 1411 found = True
1428 1412 ospace = nsname
1429 1413 if ns == alias_ns:
1430 1414 isalias = True
1431 1415 break # namespace loop
1432 1416
1433 1417 # Try to see if it's magic
1434 1418 if not found:
1435 1419 obj = None
1436 1420 if oname.startswith(ESC_MAGIC2):
1437 1421 oname = oname.lstrip(ESC_MAGIC2)
1438 1422 obj = self.find_cell_magic(oname)
1439 1423 elif oname.startswith(ESC_MAGIC):
1440 1424 oname = oname.lstrip(ESC_MAGIC)
1441 1425 obj = self.find_line_magic(oname)
1442 1426 else:
1443 1427 # search without prefix, so run? will find %run?
1444 1428 obj = self.find_line_magic(oname)
1445 1429 if obj is None:
1446 1430 obj = self.find_cell_magic(oname)
1447 1431 if obj is not None:
1448 1432 found = True
1449 1433 ospace = 'IPython internal'
1450 1434 ismagic = True
1451 1435
1452 1436 # Last try: special-case some literals like '', [], {}, etc:
1453 1437 if not found and oname_head in ["''",'""','[]','{}','()']:
1454 1438 obj = eval(oname_head)
1455 1439 found = True
1456 1440 ospace = 'Interactive'
1457 1441
1458 1442 return {'found':found, 'obj':obj, 'namespace':ospace,
1459 1443 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1460 1444
1461 1445 def _ofind_property(self, oname, info):
1462 1446 """Second part of object finding, to look for property details."""
1463 1447 if info.found:
1464 1448 # Get the docstring of the class property if it exists.
1465 1449 path = oname.split('.')
1466 1450 root = '.'.join(path[:-1])
1467 1451 if info.parent is not None:
1468 1452 try:
1469 1453 target = getattr(info.parent, '__class__')
1470 1454 # The object belongs to a class instance.
1471 1455 try:
1472 1456 target = getattr(target, path[-1])
1473 1457 # The class defines the object.
1474 1458 if isinstance(target, property):
1475 1459 oname = root + '.__class__.' + path[-1]
1476 1460 info = Struct(self._ofind(oname))
1477 1461 except AttributeError: pass
1478 1462 except AttributeError: pass
1479 1463
1480 1464 # We return either the new info or the unmodified input if the object
1481 1465 # hadn't been found
1482 1466 return info
1483 1467
1484 1468 def _object_find(self, oname, namespaces=None):
1485 1469 """Find an object and return a struct with info about it."""
1486 1470 inf = Struct(self._ofind(oname, namespaces))
1487 1471 return Struct(self._ofind_property(oname, inf))
1488 1472
1489 1473 def _inspect(self, meth, oname, namespaces=None, **kw):
1490 1474 """Generic interface to the inspector system.
1491 1475
1492 1476 This function is meant to be called by pdef, pdoc & friends."""
1493 1477 info = self._object_find(oname, namespaces)
1494 1478 if info.found:
1495 1479 pmethod = getattr(self.inspector, meth)
1496 1480 formatter = format_screen if info.ismagic else None
1497 1481 if meth == 'pdoc':
1498 1482 pmethod(info.obj, oname, formatter)
1499 1483 elif meth == 'pinfo':
1500 1484 pmethod(info.obj, oname, formatter, info, **kw)
1501 1485 else:
1502 1486 pmethod(info.obj, oname)
1503 1487 else:
1504 1488 print('Object `%s` not found.' % oname)
1505 1489 return 'not found' # so callers can take other action
1506 1490
1507 1491 def object_inspect(self, oname, detail_level=0):
1508 1492 with self.builtin_trap:
1509 1493 info = self._object_find(oname)
1510 1494 if info.found:
1511 1495 return self.inspector.info(info.obj, oname, info=info,
1512 1496 detail_level=detail_level
1513 1497 )
1514 1498 else:
1515 1499 return oinspect.object_info(name=oname, found=False)
1516 1500
1517 1501 #-------------------------------------------------------------------------
1518 1502 # Things related to history management
1519 1503 #-------------------------------------------------------------------------
1520 1504
1521 1505 def init_history(self):
1522 1506 """Sets up the command history, and starts regular autosaves."""
1523 1507 self.history_manager = HistoryManager(shell=self, config=self.config)
1524 1508 self.configurables.append(self.history_manager)
1525 1509
1526 1510 #-------------------------------------------------------------------------
1527 1511 # Things related to exception handling and tracebacks (not debugging)
1528 1512 #-------------------------------------------------------------------------
1529 1513
1530 1514 def init_traceback_handlers(self, custom_exceptions):
1531 1515 # Syntax error handler.
1532 1516 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1533 1517
1534 1518 # The interactive one is initialized with an offset, meaning we always
1535 1519 # want to remove the topmost item in the traceback, which is our own
1536 1520 # internal code. Valid modes: ['Plain','Context','Verbose']
1537 1521 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1538 1522 color_scheme='NoColor',
1539 1523 tb_offset = 1,
1540 1524 check_cache=self.compile.check_cache)
1541 1525
1542 1526 # The instance will store a pointer to the system-wide exception hook,
1543 1527 # so that runtime code (such as magics) can access it. This is because
1544 1528 # during the read-eval loop, it may get temporarily overwritten.
1545 1529 self.sys_excepthook = sys.excepthook
1546 1530
1547 1531 # and add any custom exception handlers the user may have specified
1548 1532 self.set_custom_exc(*custom_exceptions)
1549 1533
1550 1534 # Set the exception mode
1551 1535 self.InteractiveTB.set_mode(mode=self.xmode)
1552 1536
1553 1537 def set_custom_exc(self, exc_tuple, handler):
1554 1538 """set_custom_exc(exc_tuple,handler)
1555 1539
1556 1540 Set a custom exception handler, which will be called if any of the
1557 1541 exceptions in exc_tuple occur in the mainloop (specifically, in the
1558 1542 run_code() method).
1559 1543
1560 1544 Parameters
1561 1545 ----------
1562 1546
1563 1547 exc_tuple : tuple of exception classes
1564 1548 A *tuple* of exception classes, for which to call the defined
1565 1549 handler. It is very important that you use a tuple, and NOT A
1566 1550 LIST here, because of the way Python's except statement works. If
1567 1551 you only want to trap a single exception, use a singleton tuple::
1568 1552
1569 1553 exc_tuple == (MyCustomException,)
1570 1554
1571 1555 handler : callable
1572 1556 handler must have the following signature::
1573 1557
1574 1558 def my_handler(self, etype, value, tb, tb_offset=None):
1575 1559 ...
1576 1560 return structured_traceback
1577 1561
1578 1562 Your handler must return a structured traceback (a list of strings),
1579 1563 or None.
1580 1564
1581 1565 This will be made into an instance method (via types.MethodType)
1582 1566 of IPython itself, and it will be called if any of the exceptions
1583 1567 listed in the exc_tuple are caught. If the handler is None, an
1584 1568 internal basic one is used, which just prints basic info.
1585 1569
1586 1570 To protect IPython from crashes, if your handler ever raises an
1587 1571 exception or returns an invalid result, it will be immediately
1588 1572 disabled.
1589 1573
1590 1574 WARNING: by putting in your own exception handler into IPython's main
1591 1575 execution loop, you run a very good chance of nasty crashes. This
1592 1576 facility should only be used if you really know what you are doing."""
1593 1577
1594 1578 assert type(exc_tuple)==type(()) , \
1595 1579 "The custom exceptions must be given AS A TUPLE."
1596 1580
1597 1581 def dummy_handler(self,etype,value,tb,tb_offset=None):
1598 1582 print('*** Simple custom exception handler ***')
1599 1583 print('Exception type :',etype)
1600 1584 print('Exception value:',value)
1601 1585 print('Traceback :',tb)
1602 1586 #print 'Source code :','\n'.join(self.buffer)
1603 1587
1604 1588 def validate_stb(stb):
1605 1589 """validate structured traceback return type
1606 1590
1607 1591 return type of CustomTB *should* be a list of strings, but allow
1608 1592 single strings or None, which are harmless.
1609 1593
1610 1594 This function will *always* return a list of strings,
1611 1595 and will raise a TypeError if stb is inappropriate.
1612 1596 """
1613 1597 msg = "CustomTB must return list of strings, not %r" % stb
1614 1598 if stb is None:
1615 1599 return []
1616 1600 elif isinstance(stb, basestring):
1617 1601 return [stb]
1618 1602 elif not isinstance(stb, list):
1619 1603 raise TypeError(msg)
1620 1604 # it's a list
1621 1605 for line in stb:
1622 1606 # check every element
1623 1607 if not isinstance(line, basestring):
1624 1608 raise TypeError(msg)
1625 1609 return stb
1626 1610
1627 1611 if handler is None:
1628 1612 wrapped = dummy_handler
1629 1613 else:
1630 1614 def wrapped(self,etype,value,tb,tb_offset=None):
1631 1615 """wrap CustomTB handler, to protect IPython from user code
1632 1616
1633 1617 This makes it harder (but not impossible) for custom exception
1634 1618 handlers to crash IPython.
1635 1619 """
1636 1620 try:
1637 1621 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1638 1622 return validate_stb(stb)
1639 1623 except:
1640 1624 # clear custom handler immediately
1641 1625 self.set_custom_exc((), None)
1642 1626 print("Custom TB Handler failed, unregistering", file=io.stderr)
1643 1627 # show the exception in handler first
1644 1628 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1645 1629 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1646 1630 print("The original exception:", file=io.stdout)
1647 1631 stb = self.InteractiveTB.structured_traceback(
1648 1632 (etype,value,tb), tb_offset=tb_offset
1649 1633 )
1650 1634 return stb
1651 1635
1652 1636 self.CustomTB = types.MethodType(wrapped,self)
1653 1637 self.custom_exceptions = exc_tuple
1654 1638
1655 1639 def excepthook(self, etype, value, tb):
1656 1640 """One more defense for GUI apps that call sys.excepthook.
1657 1641
1658 1642 GUI frameworks like wxPython trap exceptions and call
1659 1643 sys.excepthook themselves. I guess this is a feature that
1660 1644 enables them to keep running after exceptions that would
1661 1645 otherwise kill their mainloop. This is a bother for IPython
1662 1646 which excepts to catch all of the program exceptions with a try:
1663 1647 except: statement.
1664 1648
1665 1649 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1666 1650 any app directly invokes sys.excepthook, it will look to the user like
1667 1651 IPython crashed. In order to work around this, we can disable the
1668 1652 CrashHandler and replace it with this excepthook instead, which prints a
1669 1653 regular traceback using our InteractiveTB. In this fashion, apps which
1670 1654 call sys.excepthook will generate a regular-looking exception from
1671 1655 IPython, and the CrashHandler will only be triggered by real IPython
1672 1656 crashes.
1673 1657
1674 1658 This hook should be used sparingly, only in places which are not likely
1675 1659 to be true IPython errors.
1676 1660 """
1677 1661 self.showtraceback((etype,value,tb),tb_offset=0)
1678 1662
1679 1663 def _get_exc_info(self, exc_tuple=None):
1680 1664 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1681 1665
1682 1666 Ensures sys.last_type,value,traceback hold the exc_info we found,
1683 1667 from whichever source.
1684 1668
1685 1669 raises ValueError if none of these contain any information
1686 1670 """
1687 1671 if exc_tuple is None:
1688 1672 etype, value, tb = sys.exc_info()
1689 1673 else:
1690 1674 etype, value, tb = exc_tuple
1691 1675
1692 1676 if etype is None:
1693 1677 if hasattr(sys, 'last_type'):
1694 1678 etype, value, tb = sys.last_type, sys.last_value, \
1695 1679 sys.last_traceback
1696 1680
1697 1681 if etype is None:
1698 1682 raise ValueError("No exception to find")
1699 1683
1700 1684 # Now store the exception info in sys.last_type etc.
1701 1685 # WARNING: these variables are somewhat deprecated and not
1702 1686 # necessarily safe to use in a threaded environment, but tools
1703 1687 # like pdb depend on their existence, so let's set them. If we
1704 1688 # find problems in the field, we'll need to revisit their use.
1705 1689 sys.last_type = etype
1706 1690 sys.last_value = value
1707 1691 sys.last_traceback = tb
1708 1692
1709 1693 return etype, value, tb
1710 1694
1711 1695
1712 1696 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1713 1697 exception_only=False):
1714 1698 """Display the exception that just occurred.
1715 1699
1716 1700 If nothing is known about the exception, this is the method which
1717 1701 should be used throughout the code for presenting user tracebacks,
1718 1702 rather than directly invoking the InteractiveTB object.
1719 1703
1720 1704 A specific showsyntaxerror() also exists, but this method can take
1721 1705 care of calling it if needed, so unless you are explicitly catching a
1722 1706 SyntaxError exception, don't try to analyze the stack manually and
1723 1707 simply call this method."""
1724 1708
1725 1709 try:
1726 1710 try:
1727 1711 etype, value, tb = self._get_exc_info(exc_tuple)
1728 1712 except ValueError:
1729 1713 self.write_err('No traceback available to show.\n')
1730 1714 return
1731 1715
1732 1716 if etype is SyntaxError:
1733 1717 # Though this won't be called by syntax errors in the input
1734 1718 # line, there may be SyntaxError cases with imported code.
1735 1719 self.showsyntaxerror(filename)
1736 1720 elif etype is UsageError:
1737 1721 self.write_err("UsageError: %s" % value)
1738 elif issubclass(etype, RemoteError):
1739 # IPython.parallel remote exceptions.
1740 # Draw the remote traceback, not the local one.
1741 self._showtraceback(etype, value, value.render_traceback())
1742 1722 else:
1743 1723 if exception_only:
1744 1724 stb = ['An exception has occurred, use %tb to see '
1745 1725 'the full traceback.\n']
1746 1726 stb.extend(self.InteractiveTB.get_exception_only(etype,
1747 1727 value))
1748 1728 else:
1749 stb = self.InteractiveTB.structured_traceback(etype,
1729 try:
1730 # Exception classes can customise their traceback - we
1731 # use this in IPython.parallel for exceptions occurring
1732 # in the engines. This should return a list of strings.
1733 stb = value._render_traceback_()
1734 except Exception:
1735 stb = self.InteractiveTB.structured_traceback(etype,
1750 1736 value, tb, tb_offset=tb_offset)
1751 1737
1752 1738 self._showtraceback(etype, value, stb)
1753 1739 if self.call_pdb:
1754 1740 # drop into debugger
1755 1741 self.debugger(force=True)
1756 1742 return
1757 1743
1758 1744 # Actually show the traceback
1759 1745 self._showtraceback(etype, value, stb)
1760 1746
1761 1747 except KeyboardInterrupt:
1762 1748 self.write_err("\nKeyboardInterrupt\n")
1763 1749
1764 1750 def _showtraceback(self, etype, evalue, stb):
1765 1751 """Actually show a traceback.
1766 1752
1767 1753 Subclasses may override this method to put the traceback on a different
1768 1754 place, like a side channel.
1769 1755 """
1770 1756 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1771 1757
1772 1758 def showsyntaxerror(self, filename=None):
1773 1759 """Display the syntax error that just occurred.
1774 1760
1775 1761 This doesn't display a stack trace because there isn't one.
1776 1762
1777 1763 If a filename is given, it is stuffed in the exception instead
1778 1764 of what was there before (because Python's parser always uses
1779 1765 "<string>" when reading from a string).
1780 1766 """
1781 1767 etype, value, last_traceback = self._get_exc_info()
1782 1768
1783 1769 if filename and etype is SyntaxError:
1784 1770 try:
1785 1771 value.filename = filename
1786 1772 except:
1787 1773 # Not the format we expect; leave it alone
1788 1774 pass
1789 1775
1790 1776 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1791 1777 self._showtraceback(etype, value, stb)
1792 1778
1793 1779 # This is overridden in TerminalInteractiveShell to show a message about
1794 1780 # the %paste magic.
1795 1781 def showindentationerror(self):
1796 1782 """Called by run_cell when there's an IndentationError in code entered
1797 1783 at the prompt.
1798 1784
1799 1785 This is overridden in TerminalInteractiveShell to show a message about
1800 1786 the %paste magic."""
1801 1787 self.showsyntaxerror()
1802 1788
1803 1789 #-------------------------------------------------------------------------
1804 1790 # Things related to readline
1805 1791 #-------------------------------------------------------------------------
1806 1792
1807 1793 def init_readline(self):
1808 1794 """Command history completion/saving/reloading."""
1809 1795
1810 1796 if self.readline_use:
1811 1797 import IPython.utils.rlineimpl as readline
1812 1798
1813 1799 self.rl_next_input = None
1814 1800 self.rl_do_indent = False
1815 1801
1816 1802 if not self.readline_use or not readline.have_readline:
1817 1803 self.has_readline = False
1818 1804 self.readline = None
1819 1805 # Set a number of methods that depend on readline to be no-op
1820 1806 self.readline_no_record = no_op_context
1821 1807 self.set_readline_completer = no_op
1822 1808 self.set_custom_completer = no_op
1823 1809 if self.readline_use:
1824 1810 warn('Readline services not available or not loaded.')
1825 1811 else:
1826 1812 self.has_readline = True
1827 1813 self.readline = readline
1828 1814 sys.modules['readline'] = readline
1829 1815
1830 1816 # Platform-specific configuration
1831 1817 if os.name == 'nt':
1832 1818 # FIXME - check with Frederick to see if we can harmonize
1833 1819 # naming conventions with pyreadline to avoid this
1834 1820 # platform-dependent check
1835 1821 self.readline_startup_hook = readline.set_pre_input_hook
1836 1822 else:
1837 1823 self.readline_startup_hook = readline.set_startup_hook
1838 1824
1839 1825 # Load user's initrc file (readline config)
1840 1826 # Or if libedit is used, load editrc.
1841 1827 inputrc_name = os.environ.get('INPUTRC')
1842 1828 if inputrc_name is None:
1843 1829 inputrc_name = '.inputrc'
1844 1830 if readline.uses_libedit:
1845 1831 inputrc_name = '.editrc'
1846 1832 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1847 1833 if os.path.isfile(inputrc_name):
1848 1834 try:
1849 1835 readline.read_init_file(inputrc_name)
1850 1836 except:
1851 1837 warn('Problems reading readline initialization file <%s>'
1852 1838 % inputrc_name)
1853 1839
1854 1840 # Configure readline according to user's prefs
1855 1841 # This is only done if GNU readline is being used. If libedit
1856 1842 # is being used (as on Leopard) the readline config is
1857 1843 # not run as the syntax for libedit is different.
1858 1844 if not readline.uses_libedit:
1859 1845 for rlcommand in self.readline_parse_and_bind:
1860 1846 #print "loading rl:",rlcommand # dbg
1861 1847 readline.parse_and_bind(rlcommand)
1862 1848
1863 1849 # Remove some chars from the delimiters list. If we encounter
1864 1850 # unicode chars, discard them.
1865 1851 delims = readline.get_completer_delims()
1866 1852 if not py3compat.PY3:
1867 1853 delims = delims.encode("ascii", "ignore")
1868 1854 for d in self.readline_remove_delims:
1869 1855 delims = delims.replace(d, "")
1870 1856 delims = delims.replace(ESC_MAGIC, '')
1871 1857 readline.set_completer_delims(delims)
1872 1858 # otherwise we end up with a monster history after a while:
1873 1859 readline.set_history_length(self.history_length)
1874 1860
1875 1861 self.refill_readline_hist()
1876 1862 self.readline_no_record = ReadlineNoRecord(self)
1877 1863
1878 1864 # Configure auto-indent for all platforms
1879 1865 self.set_autoindent(self.autoindent)
1880 1866
1881 1867 def refill_readline_hist(self):
1882 1868 # Load the last 1000 lines from history
1883 1869 self.readline.clear_history()
1884 1870 stdin_encoding = sys.stdin.encoding or "utf-8"
1885 1871 last_cell = u""
1886 1872 for _, _, cell in self.history_manager.get_tail(1000,
1887 1873 include_latest=True):
1888 1874 # Ignore blank lines and consecutive duplicates
1889 1875 cell = cell.rstrip()
1890 1876 if cell and (cell != last_cell):
1891 1877 if self.multiline_history:
1892 1878 self.readline.add_history(py3compat.unicode_to_str(cell,
1893 1879 stdin_encoding))
1894 1880 else:
1895 1881 for line in cell.splitlines():
1896 1882 self.readline.add_history(py3compat.unicode_to_str(line,
1897 1883 stdin_encoding))
1898 1884 last_cell = cell
1899 1885
1900 1886 def set_next_input(self, s):
1901 1887 """ Sets the 'default' input string for the next command line.
1902 1888
1903 1889 Requires readline.
1904 1890
1905 1891 Example:
1906 1892
1907 1893 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1908 1894 [D:\ipython]|2> Hello Word_ # cursor is here
1909 1895 """
1910 1896 self.rl_next_input = py3compat.cast_bytes_py2(s)
1911 1897
1912 1898 # Maybe move this to the terminal subclass?
1913 1899 def pre_readline(self):
1914 1900 """readline hook to be used at the start of each line.
1915 1901
1916 1902 Currently it handles auto-indent only."""
1917 1903
1918 1904 if self.rl_do_indent:
1919 1905 self.readline.insert_text(self._indent_current_str())
1920 1906 if self.rl_next_input is not None:
1921 1907 self.readline.insert_text(self.rl_next_input)
1922 1908 self.rl_next_input = None
1923 1909
1924 1910 def _indent_current_str(self):
1925 1911 """return the current level of indentation as a string"""
1926 1912 return self.input_splitter.indent_spaces * ' '
1927 1913
1928 1914 #-------------------------------------------------------------------------
1929 1915 # Things related to text completion
1930 1916 #-------------------------------------------------------------------------
1931 1917
1932 1918 def init_completer(self):
1933 1919 """Initialize the completion machinery.
1934 1920
1935 1921 This creates completion machinery that can be used by client code,
1936 1922 either interactively in-process (typically triggered by the readline
1937 1923 library), programatically (such as in test suites) or out-of-prcess
1938 1924 (typically over the network by remote frontends).
1939 1925 """
1940 1926 from IPython.core.completer import IPCompleter
1941 1927 from IPython.core.completerlib import (module_completer,
1942 1928 magic_run_completer, cd_completer, reset_completer)
1943 1929
1944 1930 self.Completer = IPCompleter(shell=self,
1945 1931 namespace=self.user_ns,
1946 1932 global_namespace=self.user_global_ns,
1947 1933 alias_table=self.alias_manager.alias_table,
1948 1934 use_readline=self.has_readline,
1949 1935 config=self.config,
1950 1936 )
1951 1937 self.configurables.append(self.Completer)
1952 1938
1953 1939 # Add custom completers to the basic ones built into IPCompleter
1954 1940 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1955 1941 self.strdispatchers['complete_command'] = sdisp
1956 1942 self.Completer.custom_completers = sdisp
1957 1943
1958 1944 self.set_hook('complete_command', module_completer, str_key = 'import')
1959 1945 self.set_hook('complete_command', module_completer, str_key = 'from')
1960 1946 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1961 1947 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1962 1948 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1963 1949
1964 1950 # Only configure readline if we truly are using readline. IPython can
1965 1951 # do tab-completion over the network, in GUIs, etc, where readline
1966 1952 # itself may be absent
1967 1953 if self.has_readline:
1968 1954 self.set_readline_completer()
1969 1955
1970 1956 def complete(self, text, line=None, cursor_pos=None):
1971 1957 """Return the completed text and a list of completions.
1972 1958
1973 1959 Parameters
1974 1960 ----------
1975 1961
1976 1962 text : string
1977 1963 A string of text to be completed on. It can be given as empty and
1978 1964 instead a line/position pair are given. In this case, the
1979 1965 completer itself will split the line like readline does.
1980 1966
1981 1967 line : string, optional
1982 1968 The complete line that text is part of.
1983 1969
1984 1970 cursor_pos : int, optional
1985 1971 The position of the cursor on the input line.
1986 1972
1987 1973 Returns
1988 1974 -------
1989 1975 text : string
1990 1976 The actual text that was completed.
1991 1977
1992 1978 matches : list
1993 1979 A sorted list with all possible completions.
1994 1980
1995 1981 The optional arguments allow the completion to take more context into
1996 1982 account, and are part of the low-level completion API.
1997 1983
1998 1984 This is a wrapper around the completion mechanism, similar to what
1999 1985 readline does at the command line when the TAB key is hit. By
2000 1986 exposing it as a method, it can be used by other non-readline
2001 1987 environments (such as GUIs) for text completion.
2002 1988
2003 1989 Simple usage example:
2004 1990
2005 1991 In [1]: x = 'hello'
2006 1992
2007 1993 In [2]: _ip.complete('x.l')
2008 1994 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
2009 1995 """
2010 1996
2011 1997 # Inject names into __builtin__ so we can complete on the added names.
2012 1998 with self.builtin_trap:
2013 1999 return self.Completer.complete(text, line, cursor_pos)
2014 2000
2015 2001 def set_custom_completer(self, completer, pos=0):
2016 2002 """Adds a new custom completer function.
2017 2003
2018 2004 The position argument (defaults to 0) is the index in the completers
2019 2005 list where you want the completer to be inserted."""
2020 2006
2021 2007 newcomp = types.MethodType(completer,self.Completer)
2022 2008 self.Completer.matchers.insert(pos,newcomp)
2023 2009
2024 2010 def set_readline_completer(self):
2025 2011 """Reset readline's completer to be our own."""
2026 2012 self.readline.set_completer(self.Completer.rlcomplete)
2027 2013
2028 2014 def set_completer_frame(self, frame=None):
2029 2015 """Set the frame of the completer."""
2030 2016 if frame:
2031 2017 self.Completer.namespace = frame.f_locals
2032 2018 self.Completer.global_namespace = frame.f_globals
2033 2019 else:
2034 2020 self.Completer.namespace = self.user_ns
2035 2021 self.Completer.global_namespace = self.user_global_ns
2036 2022
2037 2023 #-------------------------------------------------------------------------
2038 2024 # Things related to magics
2039 2025 #-------------------------------------------------------------------------
2040 2026
2041 2027 def init_magics(self):
2042 2028 from IPython.core import magics as m
2043 2029 self.magics_manager = magic.MagicsManager(shell=self,
2044 2030 confg=self.config,
2045 2031 user_magics=m.UserMagics(self))
2046 2032 self.configurables.append(self.magics_manager)
2047 2033
2048 2034 # Expose as public API from the magics manager
2049 2035 self.register_magics = self.magics_manager.register
2050 2036 self.register_magic_function = self.magics_manager.register_function
2051 2037 self.define_magic = self.magics_manager.define_magic
2052 2038
2053 2039 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2054 2040 m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics,
2055 2041 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2056 2042 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2057 2043 )
2058 2044
2059 2045 # Register Magic Aliases
2060 2046 mman = self.magics_manager
2061 2047 mman.register_alias('ed', 'edit')
2062 2048 mman.register_alias('hist', 'history')
2063 2049 mman.register_alias('rep', 'recall')
2064 2050
2065 2051 # FIXME: Move the color initialization to the DisplayHook, which
2066 2052 # should be split into a prompt manager and displayhook. We probably
2067 2053 # even need a centralize colors management object.
2068 2054 self.magic('colors %s' % self.colors)
2069 2055
2070 2056 def run_line_magic(self, magic_name, line):
2071 2057 """Execute the given line magic.
2072 2058
2073 2059 Parameters
2074 2060 ----------
2075 2061 magic_name : str
2076 2062 Name of the desired magic function, without '%' prefix.
2077 2063
2078 2064 line : str
2079 2065 The rest of the input line as a single string.
2080 2066 """
2081 2067 fn = self.find_line_magic(magic_name)
2082 2068 if fn is None:
2083 2069 cm = self.find_cell_magic(magic_name)
2084 2070 etpl = "Line magic function `%%%s` not found%s."
2085 2071 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2086 2072 'did you mean that instead?)' % magic_name )
2087 2073 error(etpl % (magic_name, extra))
2088 2074 else:
2089 2075 # Note: this is the distance in the stack to the user's frame.
2090 2076 # This will need to be updated if the internal calling logic gets
2091 2077 # refactored, or else we'll be expanding the wrong variables.
2092 2078 stack_depth = 2
2093 2079 magic_arg_s = self.var_expand(line, stack_depth)
2094 2080 # Put magic args in a list so we can call with f(*a) syntax
2095 2081 args = [magic_arg_s]
2096 2082 # Grab local namespace if we need it:
2097 2083 if getattr(fn, "needs_local_scope", False):
2098 2084 args.append(sys._getframe(stack_depth).f_locals)
2099 2085 with self.builtin_trap:
2100 2086 result = fn(*args)
2101 2087 return result
2102 2088
2103 2089 def run_cell_magic(self, magic_name, line, cell):
2104 2090 """Execute the given cell magic.
2105 2091
2106 2092 Parameters
2107 2093 ----------
2108 2094 magic_name : str
2109 2095 Name of the desired magic function, without '%' prefix.
2110 2096
2111 2097 line : str
2112 2098 The rest of the first input line as a single string.
2113 2099
2114 2100 cell : str
2115 2101 The body of the cell as a (possibly multiline) string.
2116 2102 """
2117 2103 fn = self.find_cell_magic(magic_name)
2118 2104 if fn is None:
2119 2105 lm = self.find_line_magic(magic_name)
2120 2106 etpl = "Cell magic function `%%%%%s` not found%s."
2121 2107 extra = '' if lm is None else (' (But line magic `%%%s` exists, '
2122 2108 'did you mean that instead?)' % magic_name )
2123 2109 error(etpl % (magic_name, extra))
2124 2110 else:
2125 2111 # Note: this is the distance in the stack to the user's frame.
2126 2112 # This will need to be updated if the internal calling logic gets
2127 2113 # refactored, or else we'll be expanding the wrong variables.
2128 2114 stack_depth = 2
2129 2115 magic_arg_s = self.var_expand(line, stack_depth)
2130 2116 with self.builtin_trap:
2131 2117 result = fn(magic_arg_s, cell)
2132 2118 return result
2133 2119
2134 2120 def find_line_magic(self, magic_name):
2135 2121 """Find and return a line magic by name.
2136 2122
2137 2123 Returns None if the magic isn't found."""
2138 2124 return self.magics_manager.magics['line'].get(magic_name)
2139 2125
2140 2126 def find_cell_magic(self, magic_name):
2141 2127 """Find and return a cell magic by name.
2142 2128
2143 2129 Returns None if the magic isn't found."""
2144 2130 return self.magics_manager.magics['cell'].get(magic_name)
2145 2131
2146 2132 def find_magic(self, magic_name, magic_kind='line'):
2147 2133 """Find and return a magic of the given type by name.
2148 2134
2149 2135 Returns None if the magic isn't found."""
2150 2136 return self.magics_manager.magics[magic_kind].get(magic_name)
2151 2137
2152 2138 def magic(self, arg_s):
2153 2139 """DEPRECATED. Use run_line_magic() instead.
2154 2140
2155 2141 Call a magic function by name.
2156 2142
2157 2143 Input: a string containing the name of the magic function to call and
2158 2144 any additional arguments to be passed to the magic.
2159 2145
2160 2146 magic('name -opt foo bar') is equivalent to typing at the ipython
2161 2147 prompt:
2162 2148
2163 2149 In[1]: %name -opt foo bar
2164 2150
2165 2151 To call a magic without arguments, simply use magic('name').
2166 2152
2167 2153 This provides a proper Python function to call IPython's magics in any
2168 2154 valid Python code you can type at the interpreter, including loops and
2169 2155 compound statements.
2170 2156 """
2171 2157 # TODO: should we issue a loud deprecation warning here?
2172 2158 magic_name, _, magic_arg_s = arg_s.partition(' ')
2173 2159 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2174 2160 return self.run_line_magic(magic_name, magic_arg_s)
2175 2161
2176 2162 #-------------------------------------------------------------------------
2177 2163 # Things related to macros
2178 2164 #-------------------------------------------------------------------------
2179 2165
2180 2166 def define_macro(self, name, themacro):
2181 2167 """Define a new macro
2182 2168
2183 2169 Parameters
2184 2170 ----------
2185 2171 name : str
2186 2172 The name of the macro.
2187 2173 themacro : str or Macro
2188 2174 The action to do upon invoking the macro. If a string, a new
2189 2175 Macro object is created by passing the string to it.
2190 2176 """
2191 2177
2192 2178 from IPython.core import macro
2193 2179
2194 2180 if isinstance(themacro, basestring):
2195 2181 themacro = macro.Macro(themacro)
2196 2182 if not isinstance(themacro, macro.Macro):
2197 2183 raise ValueError('A macro must be a string or a Macro instance.')
2198 2184 self.user_ns[name] = themacro
2199 2185
2200 2186 #-------------------------------------------------------------------------
2201 2187 # Things related to the running of system commands
2202 2188 #-------------------------------------------------------------------------
2203 2189
2204 2190 def system_piped(self, cmd):
2205 2191 """Call the given cmd in a subprocess, piping stdout/err
2206 2192
2207 2193 Parameters
2208 2194 ----------
2209 2195 cmd : str
2210 2196 Command to execute (can not end in '&', as background processes are
2211 2197 not supported. Should not be a command that expects input
2212 2198 other than simple text.
2213 2199 """
2214 2200 if cmd.rstrip().endswith('&'):
2215 2201 # this is *far* from a rigorous test
2216 2202 # We do not support backgrounding processes because we either use
2217 2203 # pexpect or pipes to read from. Users can always just call
2218 2204 # os.system() or use ip.system=ip.system_raw
2219 2205 # if they really want a background process.
2220 2206 raise OSError("Background processes not supported.")
2221 2207
2222 2208 # we explicitly do NOT return the subprocess status code, because
2223 2209 # a non-None value would trigger :func:`sys.displayhook` calls.
2224 2210 # Instead, we store the exit_code in user_ns.
2225 2211 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2226 2212
2227 2213 def system_raw(self, cmd):
2228 2214 """Call the given cmd in a subprocess using os.system
2229 2215
2230 2216 Parameters
2231 2217 ----------
2232 2218 cmd : str
2233 2219 Command to execute.
2234 2220 """
2235 2221 cmd = self.var_expand(cmd, depth=1)
2236 2222 # protect os.system from UNC paths on Windows, which it can't handle:
2237 2223 if sys.platform == 'win32':
2238 2224 from IPython.utils._process_win32 import AvoidUNCPath
2239 2225 with AvoidUNCPath() as path:
2240 2226 if path is not None:
2241 2227 cmd = '"pushd %s &&"%s' % (path, cmd)
2242 2228 cmd = py3compat.unicode_to_str(cmd)
2243 2229 ec = os.system(cmd)
2244 2230 else:
2245 2231 cmd = py3compat.unicode_to_str(cmd)
2246 2232 ec = os.system(cmd)
2247 2233
2248 2234 # We explicitly do NOT return the subprocess status code, because
2249 2235 # a non-None value would trigger :func:`sys.displayhook` calls.
2250 2236 # Instead, we store the exit_code in user_ns.
2251 2237 self.user_ns['_exit_code'] = ec
2252 2238
2253 2239 # use piped system by default, because it is better behaved
2254 2240 system = system_piped
2255 2241
2256 2242 def getoutput(self, cmd, split=True, depth=0):
2257 2243 """Get output (possibly including stderr) from a subprocess.
2258 2244
2259 2245 Parameters
2260 2246 ----------
2261 2247 cmd : str
2262 2248 Command to execute (can not end in '&', as background processes are
2263 2249 not supported.
2264 2250 split : bool, optional
2265 2251 If True, split the output into an IPython SList. Otherwise, an
2266 2252 IPython LSString is returned. These are objects similar to normal
2267 2253 lists and strings, with a few convenience attributes for easier
2268 2254 manipulation of line-based output. You can use '?' on them for
2269 2255 details.
2270 2256 depth : int, optional
2271 2257 How many frames above the caller are the local variables which should
2272 2258 be expanded in the command string? The default (0) assumes that the
2273 2259 expansion variables are in the stack frame calling this function.
2274 2260 """
2275 2261 if cmd.rstrip().endswith('&'):
2276 2262 # this is *far* from a rigorous test
2277 2263 raise OSError("Background processes not supported.")
2278 2264 out = getoutput(self.var_expand(cmd, depth=depth+1))
2279 2265 if split:
2280 2266 out = SList(out.splitlines())
2281 2267 else:
2282 2268 out = LSString(out)
2283 2269 return out
2284 2270
2285 2271 #-------------------------------------------------------------------------
2286 2272 # Things related to aliases
2287 2273 #-------------------------------------------------------------------------
2288 2274
2289 2275 def init_alias(self):
2290 2276 self.alias_manager = AliasManager(shell=self, config=self.config)
2291 2277 self.configurables.append(self.alias_manager)
2292 2278 self.ns_table['alias'] = self.alias_manager.alias_table,
2293 2279
2294 2280 #-------------------------------------------------------------------------
2295 2281 # Things related to extensions and plugins
2296 2282 #-------------------------------------------------------------------------
2297 2283
2298 2284 def init_extension_manager(self):
2299 2285 self.extension_manager = ExtensionManager(shell=self, config=self.config)
2300 2286 self.configurables.append(self.extension_manager)
2301 2287
2302 2288 def init_plugin_manager(self):
2303 2289 self.plugin_manager = PluginManager(config=self.config)
2304 2290 self.configurables.append(self.plugin_manager)
2305 2291
2306 2292
2307 2293 #-------------------------------------------------------------------------
2308 2294 # Things related to payloads
2309 2295 #-------------------------------------------------------------------------
2310 2296
2311 2297 def init_payload(self):
2312 2298 self.payload_manager = PayloadManager(config=self.config)
2313 2299 self.configurables.append(self.payload_manager)
2314 2300
2315 2301 #-------------------------------------------------------------------------
2316 2302 # Things related to the prefilter
2317 2303 #-------------------------------------------------------------------------
2318 2304
2319 2305 def init_prefilter(self):
2320 2306 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
2321 2307 self.configurables.append(self.prefilter_manager)
2322 2308 # Ultimately this will be refactored in the new interpreter code, but
2323 2309 # for now, we should expose the main prefilter method (there's legacy
2324 2310 # code out there that may rely on this).
2325 2311 self.prefilter = self.prefilter_manager.prefilter_lines
2326 2312
2327 2313 def auto_rewrite_input(self, cmd):
2328 2314 """Print to the screen the rewritten form of the user's command.
2329 2315
2330 2316 This shows visual feedback by rewriting input lines that cause
2331 2317 automatic calling to kick in, like::
2332 2318
2333 2319 /f x
2334 2320
2335 2321 into::
2336 2322
2337 2323 ------> f(x)
2338 2324
2339 2325 after the user's input prompt. This helps the user understand that the
2340 2326 input line was transformed automatically by IPython.
2341 2327 """
2342 2328 if not self.show_rewritten_input:
2343 2329 return
2344 2330
2345 2331 rw = self.prompt_manager.render('rewrite') + cmd
2346 2332
2347 2333 try:
2348 2334 # plain ascii works better w/ pyreadline, on some machines, so
2349 2335 # we use it and only print uncolored rewrite if we have unicode
2350 2336 rw = str(rw)
2351 2337 print(rw, file=io.stdout)
2352 2338 except UnicodeEncodeError:
2353 2339 print("------> " + cmd)
2354 2340
2355 2341 #-------------------------------------------------------------------------
2356 2342 # Things related to extracting values/expressions from kernel and user_ns
2357 2343 #-------------------------------------------------------------------------
2358 2344
2359 2345 def _simple_error(self):
2360 2346 etype, value = sys.exc_info()[:2]
2361 2347 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
2362 2348
2363 2349 def user_variables(self, names):
2364 2350 """Get a list of variable names from the user's namespace.
2365 2351
2366 2352 Parameters
2367 2353 ----------
2368 2354 names : list of strings
2369 2355 A list of names of variables to be read from the user namespace.
2370 2356
2371 2357 Returns
2372 2358 -------
2373 2359 A dict, keyed by the input names and with the repr() of each value.
2374 2360 """
2375 2361 out = {}
2376 2362 user_ns = self.user_ns
2377 2363 for varname in names:
2378 2364 try:
2379 2365 value = repr(user_ns[varname])
2380 2366 except:
2381 2367 value = self._simple_error()
2382 2368 out[varname] = value
2383 2369 return out
2384 2370
2385 2371 def user_expressions(self, expressions):
2386 2372 """Evaluate a dict of expressions in the user's namespace.
2387 2373
2388 2374 Parameters
2389 2375 ----------
2390 2376 expressions : dict
2391 2377 A dict with string keys and string values. The expression values
2392 2378 should be valid Python expressions, each of which will be evaluated
2393 2379 in the user namespace.
2394 2380
2395 2381 Returns
2396 2382 -------
2397 2383 A dict, keyed like the input expressions dict, with the repr() of each
2398 2384 value.
2399 2385 """
2400 2386 out = {}
2401 2387 user_ns = self.user_ns
2402 2388 global_ns = self.user_global_ns
2403 2389 for key, expr in expressions.iteritems():
2404 2390 try:
2405 2391 value = repr(eval(expr, global_ns, user_ns))
2406 2392 except:
2407 2393 value = self._simple_error()
2408 2394 out[key] = value
2409 2395 return out
2410 2396
2411 2397 #-------------------------------------------------------------------------
2412 2398 # Things related to the running of code
2413 2399 #-------------------------------------------------------------------------
2414 2400
2415 2401 def ex(self, cmd):
2416 2402 """Execute a normal python statement in user namespace."""
2417 2403 with self.builtin_trap:
2418 2404 exec cmd in self.user_global_ns, self.user_ns
2419 2405
2420 2406 def ev(self, expr):
2421 2407 """Evaluate python expression expr in user namespace.
2422 2408
2423 2409 Returns the result of evaluation
2424 2410 """
2425 2411 with self.builtin_trap:
2426 2412 return eval(expr, self.user_global_ns, self.user_ns)
2427 2413
2428 2414 def safe_execfile(self, fname, *where, **kw):
2429 2415 """A safe version of the builtin execfile().
2430 2416
2431 2417 This version will never throw an exception, but instead print
2432 2418 helpful error messages to the screen. This only works on pure
2433 2419 Python files with the .py extension.
2434 2420
2435 2421 Parameters
2436 2422 ----------
2437 2423 fname : string
2438 2424 The name of the file to be executed.
2439 2425 where : tuple
2440 2426 One or two namespaces, passed to execfile() as (globals,locals).
2441 2427 If only one is given, it is passed as both.
2442 2428 exit_ignore : bool (False)
2443 2429 If True, then silence SystemExit for non-zero status (it is always
2444 2430 silenced for zero status, as it is so common).
2445 2431 raise_exceptions : bool (False)
2446 2432 If True raise exceptions everywhere. Meant for testing.
2447 2433
2448 2434 """
2449 2435 kw.setdefault('exit_ignore', False)
2450 2436 kw.setdefault('raise_exceptions', False)
2451 2437
2452 2438 fname = os.path.abspath(os.path.expanduser(fname))
2453 2439
2454 2440 # Make sure we can open the file
2455 2441 try:
2456 2442 with open(fname) as thefile:
2457 2443 pass
2458 2444 except:
2459 2445 warn('Could not open file <%s> for safe execution.' % fname)
2460 2446 return
2461 2447
2462 2448 # Find things also in current directory. This is needed to mimic the
2463 2449 # behavior of running a script from the system command line, where
2464 2450 # Python inserts the script's directory into sys.path
2465 2451 dname = os.path.dirname(fname)
2466 2452
2467 2453 with prepended_to_syspath(dname):
2468 2454 # Ensure that __file__ is always defined to match Python behavior
2469 2455 save_fname = self.user_ns.get('__file__',None)
2470 2456 self.user_ns['__file__'] = fname
2471 2457 try:
2472 2458 py3compat.execfile(fname,*where)
2473 2459 except SystemExit as status:
2474 2460 # If the call was made with 0 or None exit status (sys.exit(0)
2475 2461 # or sys.exit() ), don't bother showing a traceback, as both of
2476 2462 # these are considered normal by the OS:
2477 2463 # > python -c'import sys;sys.exit(0)'; echo $?
2478 2464 # 0
2479 2465 # > python -c'import sys;sys.exit()'; echo $?
2480 2466 # 0
2481 2467 # For other exit status, we show the exception unless
2482 2468 # explicitly silenced, but only in short form.
2483 2469 if kw['raise_exceptions']:
2484 2470 raise
2485 2471 if status.code not in (0, None) and not kw['exit_ignore']:
2486 2472 self.showtraceback(exception_only=True)
2487 2473 except:
2488 2474 if kw['raise_exceptions']:
2489 2475 raise
2490 2476 self.showtraceback()
2491 2477 finally:
2492 2478 self.user_ns['__file__'] = save_fname
2493 2479
2494 2480 def safe_execfile_ipy(self, fname):
2495 2481 """Like safe_execfile, but for .ipy files with IPython syntax.
2496 2482
2497 2483 Parameters
2498 2484 ----------
2499 2485 fname : str
2500 2486 The name of the file to execute. The filename must have a
2501 2487 .ipy extension.
2502 2488 """
2503 2489 fname = os.path.abspath(os.path.expanduser(fname))
2504 2490
2505 2491 # Make sure we can open the file
2506 2492 try:
2507 2493 with open(fname) as thefile:
2508 2494 pass
2509 2495 except:
2510 2496 warn('Could not open file <%s> for safe execution.' % fname)
2511 2497 return
2512 2498
2513 2499 # Find things also in current directory. This is needed to mimic the
2514 2500 # behavior of running a script from the system command line, where
2515 2501 # Python inserts the script's directory into sys.path
2516 2502 dname = os.path.dirname(fname)
2517 2503
2518 2504 with prepended_to_syspath(dname):
2519 2505 # Ensure that __file__ is always defined to match Python behavior
2520 2506 save_fname = self.user_ns.get('__file__',None)
2521 2507 self.user_ns['__file__'] = fname
2522 2508 try:
2523 2509 with open(fname) as thefile:
2524 2510 # self.run_cell currently captures all exceptions
2525 2511 # raised in user code. It would be nice if there were
2526 2512 # versions of runlines, execfile that did raise, so
2527 2513 # we could catch the errors.
2528 2514 self.run_cell(thefile.read(), store_history=False)
2529 2515 except:
2530 2516 self.showtraceback()
2531 2517 warn('Unknown failure executing file: <%s>' % fname)
2532 2518 finally:
2533 2519 self.user_ns['__file__'] = save_fname
2534 2520
2535 2521 def safe_run_module(self, mod_name, where):
2536 2522 """A safe version of runpy.run_module().
2537 2523
2538 2524 This version will never throw an exception, but instead print
2539 2525 helpful error messages to the screen.
2540 2526
2541 2527 Parameters
2542 2528 ----------
2543 2529 mod_name : string
2544 2530 The name of the module to be executed.
2545 2531 where : dict
2546 2532 The globals namespace.
2547 2533 """
2548 2534 try:
2549 2535 where.update(
2550 2536 runpy.run_module(str(mod_name), run_name="__main__",
2551 2537 alter_sys=True)
2552 2538 )
2553 2539 except:
2554 2540 self.showtraceback()
2555 2541 warn('Unknown failure executing module: <%s>' % mod_name)
2556 2542
2557 2543 def _run_cached_cell_magic(self, magic_name, line):
2558 2544 """Special method to call a cell magic with the data stored in self.
2559 2545 """
2560 2546 cell = self._current_cell_magic_body
2561 2547 self._current_cell_magic_body = None
2562 2548 return self.run_cell_magic(magic_name, line, cell)
2563 2549
2564 2550 def run_cell(self, raw_cell, store_history=False, silent=False):
2565 2551 """Run a complete IPython cell.
2566 2552
2567 2553 Parameters
2568 2554 ----------
2569 2555 raw_cell : str
2570 2556 The code (including IPython code such as %magic functions) to run.
2571 2557 store_history : bool
2572 2558 If True, the raw and translated cell will be stored in IPython's
2573 2559 history. For user code calling back into IPython's machinery, this
2574 2560 should be set to False.
2575 2561 silent : bool
2576 2562 If True, avoid side-effets, such as implicit displayhooks, history,
2577 2563 and logging. silent=True forces store_history=False.
2578 2564 """
2579 2565 if (not raw_cell) or raw_cell.isspace():
2580 2566 return
2581 2567
2582 2568 if silent:
2583 2569 store_history = False
2584 2570
2585 2571 self.input_splitter.push(raw_cell)
2586 2572
2587 2573 # Check for cell magics, which leave state behind. This interface is
2588 2574 # ugly, we need to do something cleaner later... Now the logic is
2589 2575 # simply that the input_splitter remembers if there was a cell magic,
2590 2576 # and in that case we grab the cell body.
2591 2577 if self.input_splitter.cell_magic_parts:
2592 2578 self._current_cell_magic_body = \
2593 2579 ''.join(self.input_splitter.cell_magic_parts)
2594 2580 cell = self.input_splitter.source_reset()
2595 2581
2596 2582 with self.builtin_trap:
2597 2583 prefilter_failed = False
2598 2584 if len(cell.splitlines()) == 1:
2599 2585 try:
2600 2586 # use prefilter_lines to handle trailing newlines
2601 2587 # restore trailing newline for ast.parse
2602 2588 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2603 2589 except AliasError as e:
2604 2590 error(e)
2605 2591 prefilter_failed = True
2606 2592 except Exception:
2607 2593 # don't allow prefilter errors to crash IPython
2608 2594 self.showtraceback()
2609 2595 prefilter_failed = True
2610 2596
2611 2597 # Store raw and processed history
2612 2598 if store_history:
2613 2599 self.history_manager.store_inputs(self.execution_count,
2614 2600 cell, raw_cell)
2615 2601 if not silent:
2616 2602 self.logger.log(cell, raw_cell)
2617 2603
2618 2604 if not prefilter_failed:
2619 2605 # don't run if prefilter failed
2620 2606 cell_name = self.compile.cache(cell, self.execution_count)
2621 2607
2622 2608 with self.display_trap:
2623 2609 try:
2624 2610 code_ast = self.compile.ast_parse(cell,
2625 2611 filename=cell_name)
2626 2612 except IndentationError:
2627 2613 self.showindentationerror()
2628 2614 if store_history:
2629 2615 self.execution_count += 1
2630 2616 return None
2631 2617 except (OverflowError, SyntaxError, ValueError, TypeError,
2632 2618 MemoryError):
2633 2619 self.showsyntaxerror()
2634 2620 if store_history:
2635 2621 self.execution_count += 1
2636 2622 return None
2637 2623
2638 2624 interactivity = "none" if silent else self.ast_node_interactivity
2639 2625 self.run_ast_nodes(code_ast.body, cell_name,
2640 2626 interactivity=interactivity)
2641 2627
2642 2628 # Execute any registered post-execution functions.
2643 2629 # unless we are silent
2644 2630 post_exec = [] if silent else self._post_execute.iteritems()
2645 2631
2646 2632 for func, status in post_exec:
2647 2633 if self.disable_failing_post_execute and not status:
2648 2634 continue
2649 2635 try:
2650 2636 func()
2651 2637 except KeyboardInterrupt:
2652 2638 print("\nKeyboardInterrupt", file=io.stderr)
2653 2639 except Exception:
2654 2640 # register as failing:
2655 2641 self._post_execute[func] = False
2656 2642 self.showtraceback()
2657 2643 print('\n'.join([
2658 2644 "post-execution function %r produced an error." % func,
2659 2645 "If this problem persists, you can disable failing post-exec functions with:",
2660 2646 "",
2661 2647 " get_ipython().disable_failing_post_execute = True"
2662 2648 ]), file=io.stderr)
2663 2649
2664 2650 if store_history:
2665 2651 # Write output to the database. Does nothing unless
2666 2652 # history output logging is enabled.
2667 2653 self.history_manager.store_output(self.execution_count)
2668 2654 # Each cell is a *single* input, regardless of how many lines it has
2669 2655 self.execution_count += 1
2670 2656
2671 2657 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr'):
2672 2658 """Run a sequence of AST nodes. The execution mode depends on the
2673 2659 interactivity parameter.
2674 2660
2675 2661 Parameters
2676 2662 ----------
2677 2663 nodelist : list
2678 2664 A sequence of AST nodes to run.
2679 2665 cell_name : str
2680 2666 Will be passed to the compiler as the filename of the cell. Typically
2681 2667 the value returned by ip.compile.cache(cell).
2682 2668 interactivity : str
2683 2669 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2684 2670 run interactively (displaying output from expressions). 'last_expr'
2685 2671 will run the last node interactively only if it is an expression (i.e.
2686 2672 expressions in loops or other blocks are not displayed. Other values
2687 2673 for this parameter will raise a ValueError.
2688 2674 """
2689 2675 if not nodelist:
2690 2676 return
2691 2677
2692 2678 if interactivity == 'last_expr':
2693 2679 if isinstance(nodelist[-1], ast.Expr):
2694 2680 interactivity = "last"
2695 2681 else:
2696 2682 interactivity = "none"
2697 2683
2698 2684 if interactivity == 'none':
2699 2685 to_run_exec, to_run_interactive = nodelist, []
2700 2686 elif interactivity == 'last':
2701 2687 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2702 2688 elif interactivity == 'all':
2703 2689 to_run_exec, to_run_interactive = [], nodelist
2704 2690 else:
2705 2691 raise ValueError("Interactivity was %r" % interactivity)
2706 2692
2707 2693 exec_count = self.execution_count
2708 2694
2709 2695 try:
2710 2696 for i, node in enumerate(to_run_exec):
2711 2697 mod = ast.Module([node])
2712 2698 code = self.compile(mod, cell_name, "exec")
2713 2699 if self.run_code(code):
2714 2700 return True
2715 2701
2716 2702 for i, node in enumerate(to_run_interactive):
2717 2703 mod = ast.Interactive([node])
2718 2704 code = self.compile(mod, cell_name, "single")
2719 2705 if self.run_code(code):
2720 2706 return True
2721 2707
2722 2708 # Flush softspace
2723 2709 if softspace(sys.stdout, 0):
2724 2710 print()
2725 2711
2726 2712 except:
2727 2713 # It's possible to have exceptions raised here, typically by
2728 2714 # compilation of odd code (such as a naked 'return' outside a
2729 2715 # function) that did parse but isn't valid. Typically the exception
2730 2716 # is a SyntaxError, but it's safest just to catch anything and show
2731 2717 # the user a traceback.
2732 2718
2733 2719 # We do only one try/except outside the loop to minimize the impact
2734 2720 # on runtime, and also because if any node in the node list is
2735 2721 # broken, we should stop execution completely.
2736 2722 self.showtraceback()
2737 2723
2738 2724 return False
2739 2725
2740 2726 def run_code(self, code_obj):
2741 2727 """Execute a code object.
2742 2728
2743 2729 When an exception occurs, self.showtraceback() is called to display a
2744 2730 traceback.
2745 2731
2746 2732 Parameters
2747 2733 ----------
2748 2734 code_obj : code object
2749 2735 A compiled code object, to be executed
2750 2736
2751 2737 Returns
2752 2738 -------
2753 2739 False : successful execution.
2754 2740 True : an error occurred.
2755 2741 """
2756 2742
2757 2743 # Set our own excepthook in case the user code tries to call it
2758 2744 # directly, so that the IPython crash handler doesn't get triggered
2759 2745 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2760 2746
2761 2747 # we save the original sys.excepthook in the instance, in case config
2762 2748 # code (such as magics) needs access to it.
2763 2749 self.sys_excepthook = old_excepthook
2764 2750 outflag = 1 # happens in more places, so it's easier as default
2765 2751 try:
2766 2752 try:
2767 2753 self.hooks.pre_run_code_hook()
2768 2754 #rprint('Running code', repr(code_obj)) # dbg
2769 2755 exec code_obj in self.user_global_ns, self.user_ns
2770 2756 finally:
2771 2757 # Reset our crash handler in place
2772 2758 sys.excepthook = old_excepthook
2773 2759 except SystemExit:
2774 2760 self.showtraceback(exception_only=True)
2775 2761 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2776 2762 except self.custom_exceptions:
2777 2763 etype,value,tb = sys.exc_info()
2778 2764 self.CustomTB(etype,value,tb)
2779 2765 except:
2780 2766 self.showtraceback()
2781 2767 else:
2782 2768 outflag = 0
2783 2769 return outflag
2784 2770
2785 2771 # For backwards compatibility
2786 2772 runcode = run_code
2787 2773
2788 2774 #-------------------------------------------------------------------------
2789 2775 # Things related to GUI support and pylab
2790 2776 #-------------------------------------------------------------------------
2791 2777
2792 2778 def enable_gui(self, gui=None):
2793 2779 raise NotImplementedError('Implement enable_gui in a subclass')
2794 2780
2795 2781 def enable_pylab(self, gui=None, import_all=True):
2796 2782 """Activate pylab support at runtime.
2797 2783
2798 2784 This turns on support for matplotlib, preloads into the interactive
2799 2785 namespace all of numpy and pylab, and configures IPython to correctly
2800 2786 interact with the GUI event loop. The GUI backend to be used can be
2801 2787 optionally selected with the optional :param:`gui` argument.
2802 2788
2803 2789 Parameters
2804 2790 ----------
2805 2791 gui : optional, string
2806 2792
2807 2793 If given, dictates the choice of matplotlib GUI backend to use
2808 2794 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2809 2795 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2810 2796 matplotlib (as dictated by the matplotlib build-time options plus the
2811 2797 user's matplotlibrc configuration file). Note that not all backends
2812 2798 make sense in all contexts, for example a terminal ipython can't
2813 2799 display figures inline.
2814 2800 """
2815 2801 from IPython.core.pylabtools import mpl_runner
2816 2802 # We want to prevent the loading of pylab to pollute the user's
2817 2803 # namespace as shown by the %who* magics, so we execute the activation
2818 2804 # code in an empty namespace, and we update *both* user_ns and
2819 2805 # user_ns_hidden with this information.
2820 2806 ns = {}
2821 2807 try:
2822 2808 gui = pylab_activate(ns, gui, import_all, self)
2823 2809 except KeyError:
2824 2810 error("Backend %r not supported" % gui)
2825 2811 return
2826 2812 self.user_ns.update(ns)
2827 2813 self.user_ns_hidden.update(ns)
2828 2814 # Now we must activate the gui pylab wants to use, and fix %run to take
2829 2815 # plot updates into account
2830 2816 self.enable_gui(gui)
2831 2817 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2832 2818 mpl_runner(self.safe_execfile)
2833 2819
2834 2820 #-------------------------------------------------------------------------
2835 2821 # Utilities
2836 2822 #-------------------------------------------------------------------------
2837 2823
2838 2824 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2839 2825 """Expand python variables in a string.
2840 2826
2841 2827 The depth argument indicates how many frames above the caller should
2842 2828 be walked to look for the local namespace where to expand variables.
2843 2829
2844 2830 The global namespace for expansion is always the user's interactive
2845 2831 namespace.
2846 2832 """
2847 2833 ns = self.user_ns.copy()
2848 2834 ns.update(sys._getframe(depth+1).f_locals)
2849 2835 ns.pop('self', None)
2850 2836 try:
2851 2837 cmd = formatter.format(cmd, **ns)
2852 2838 except Exception:
2853 2839 # if formatter couldn't format, just let it go untransformed
2854 2840 pass
2855 2841 return cmd
2856 2842
2857 2843 def mktempfile(self, data=None, prefix='ipython_edit_'):
2858 2844 """Make a new tempfile and return its filename.
2859 2845
2860 2846 This makes a call to tempfile.mktemp, but it registers the created
2861 2847 filename internally so ipython cleans it up at exit time.
2862 2848
2863 2849 Optional inputs:
2864 2850
2865 2851 - data(None): if data is given, it gets written out to the temp file
2866 2852 immediately, and the file is closed again."""
2867 2853
2868 2854 filename = tempfile.mktemp('.py', prefix)
2869 2855 self.tempfiles.append(filename)
2870 2856
2871 2857 if data:
2872 2858 tmp_file = open(filename,'w')
2873 2859 tmp_file.write(data)
2874 2860 tmp_file.close()
2875 2861 return filename
2876 2862
2877 2863 # TODO: This should be removed when Term is refactored.
2878 2864 def write(self,data):
2879 2865 """Write a string to the default output"""
2880 2866 io.stdout.write(data)
2881 2867
2882 2868 # TODO: This should be removed when Term is refactored.
2883 2869 def write_err(self,data):
2884 2870 """Write a string to the default error output"""
2885 2871 io.stderr.write(data)
2886 2872
2887 2873 def ask_yes_no(self, prompt, default=None):
2888 2874 if self.quiet:
2889 2875 return True
2890 2876 return ask_yes_no(prompt,default)
2891 2877
2892 2878 def show_usage(self):
2893 2879 """Show a usage message"""
2894 2880 page.page(IPython.core.usage.interactive_usage)
2895 2881
2896 2882 def extract_input_lines(self, range_str, raw=False):
2897 2883 """Return as a string a set of input history slices.
2898 2884
2899 2885 Parameters
2900 2886 ----------
2901 2887 range_str : string
2902 2888 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
2903 2889 since this function is for use by magic functions which get their
2904 2890 arguments as strings. The number before the / is the session
2905 2891 number: ~n goes n back from the current session.
2906 2892
2907 2893 Optional Parameters:
2908 2894 - raw(False): by default, the processed input is used. If this is
2909 2895 true, the raw input history is used instead.
2910 2896
2911 2897 Note that slices can be called with two notations:
2912 2898
2913 2899 N:M -> standard python form, means including items N...(M-1).
2914 2900
2915 2901 N-M -> include items N..M (closed endpoint)."""
2916 2902 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
2917 2903 return "\n".join(x for _, _, x in lines)
2918 2904
2919 2905 def find_user_code(self, target, raw=True, py_only=False):
2920 2906 """Get a code string from history, file, url, or a string or macro.
2921 2907
2922 2908 This is mainly used by magic functions.
2923 2909
2924 2910 Parameters
2925 2911 ----------
2926 2912
2927 2913 target : str
2928 2914
2929 2915 A string specifying code to retrieve. This will be tried respectively
2930 2916 as: ranges of input history (see %history for syntax), url,
2931 2917 correspnding .py file, filename, or an expression evaluating to a
2932 2918 string or Macro in the user namespace.
2933 2919
2934 2920 raw : bool
2935 2921 If true (default), retrieve raw history. Has no effect on the other
2936 2922 retrieval mechanisms.
2937 2923
2938 2924 py_only : bool (default False)
2939 2925 Only try to fetch python code, do not try alternative methods to decode file
2940 2926 if unicode fails.
2941 2927
2942 2928 Returns
2943 2929 -------
2944 2930 A string of code.
2945 2931
2946 2932 ValueError is raised if nothing is found, and TypeError if it evaluates
2947 2933 to an object of another type. In each case, .args[0] is a printable
2948 2934 message.
2949 2935 """
2950 2936 code = self.extract_input_lines(target, raw=raw) # Grab history
2951 2937 if code:
2952 2938 return code
2953 2939 utarget = unquote_filename(target)
2954 2940 try:
2955 2941 if utarget.startswith(('http://', 'https://')):
2956 2942 return openpy.read_py_url(utarget, skip_encoding_cookie=True)
2957 2943 except UnicodeDecodeError:
2958 2944 if not py_only :
2959 2945 response = urllib.urlopen(target)
2960 2946 return response.read().decode('latin1')
2961 2947 raise ValueError(("'%s' seem to be unreadable.") % utarget)
2962 2948
2963 2949 potential_target = [target]
2964 2950 try :
2965 2951 potential_target.insert(0,get_py_filename(target))
2966 2952 except IOError:
2967 2953 pass
2968 2954
2969 2955 for tgt in potential_target :
2970 2956 if os.path.isfile(tgt): # Read file
2971 2957 try :
2972 2958 return openpy.read_py_file(tgt, skip_encoding_cookie=True)
2973 2959 except UnicodeDecodeError :
2974 2960 if not py_only :
2975 2961 with io_open(tgt,'r', encoding='latin1') as f :
2976 2962 return f.read()
2977 2963 raise ValueError(("'%s' seem to be unreadable.") % target)
2978 2964
2979 2965 try: # User namespace
2980 2966 codeobj = eval(target, self.user_ns)
2981 2967 except Exception:
2982 2968 raise ValueError(("'%s' was not found in history, as a file, url, "
2983 2969 "nor in the user namespace.") % target)
2984 2970 if isinstance(codeobj, basestring):
2985 2971 return codeobj
2986 2972 elif isinstance(codeobj, Macro):
2987 2973 return codeobj.value
2988 2974
2989 2975 raise TypeError("%s is neither a string nor a macro." % target,
2990 2976 codeobj)
2991 2977
2992 2978 #-------------------------------------------------------------------------
2993 2979 # Things related to IPython exiting
2994 2980 #-------------------------------------------------------------------------
2995 2981 def atexit_operations(self):
2996 2982 """This will be executed at the time of exit.
2997 2983
2998 2984 Cleanup operations and saving of persistent data that is done
2999 2985 unconditionally by IPython should be performed here.
3000 2986
3001 2987 For things that may depend on startup flags or platform specifics (such
3002 2988 as having readline or not), register a separate atexit function in the
3003 2989 code that has the appropriate information, rather than trying to
3004 2990 clutter
3005 2991 """
3006 2992 # Close the history session (this stores the end time and line count)
3007 2993 # this must be *before* the tempfile cleanup, in case of temporary
3008 2994 # history db
3009 2995 self.history_manager.end_session()
3010 2996
3011 2997 # Cleanup all tempfiles left around
3012 2998 for tfile in self.tempfiles:
3013 2999 try:
3014 3000 os.unlink(tfile)
3015 3001 except OSError:
3016 3002 pass
3017 3003
3018 3004 # Clear all user namespaces to release all references cleanly.
3019 3005 self.reset(new_session=False)
3020 3006
3021 3007 # Run user hooks
3022 3008 self.hooks.shutdown_hook()
3023 3009
3024 3010 def cleanup(self):
3025 3011 self.restore_sys_module_state()
3026 3012
3027 3013
3028 3014 class InteractiveShellABC(object):
3029 3015 """An abstract base class for InteractiveShell."""
3030 3016 __metaclass__ = abc.ABCMeta
3031 3017
3032 3018 InteractiveShellABC.register(InteractiveShell)
@@ -1,332 +1,335
1 1 # encoding: utf-8
2 2
3 3 """Classes and functions for kernel related errors and exceptions.
4 4
5 5 Authors:
6 6
7 7 * Brian Granger
8 8 * Min RK
9 9 """
10 10 from __future__ import print_function
11 11
12 12 import sys
13 13 import traceback
14 14
15 15 __docformat__ = "restructuredtext en"
16 16
17 17 # Tell nose to skip this module
18 18 __test__ = {}
19 19
20 20 #-------------------------------------------------------------------------------
21 21 # Copyright (C) 2008-2011 The IPython Development Team
22 22 #
23 23 # Distributed under the terms of the BSD License. The full license is in
24 24 # the file COPYING, distributed as part of this software.
25 25 #-------------------------------------------------------------------------------
26 26
27 27 #-------------------------------------------------------------------------------
28 28 # Error classes
29 29 #-------------------------------------------------------------------------------
30 30 class IPythonError(Exception):
31 31 """Base exception that all of our exceptions inherit from.
32 32
33 33 This can be raised by code that doesn't have any more specific
34 34 information."""
35 35
36 36 pass
37 37
38 38 # Exceptions associated with the controller objects
39 39 class ControllerError(IPythonError): pass
40 40
41 41 class ControllerCreationError(ControllerError): pass
42 42
43 43
44 44 # Exceptions associated with the Engines
45 45 class EngineError(IPythonError): pass
46 46
47 47 class EngineCreationError(EngineError): pass
48 48
49 49 class KernelError(IPythonError):
50 50 pass
51 51
52 52 class NotDefined(KernelError):
53 53 def __init__(self, name):
54 54 self.name = name
55 55 self.args = (name,)
56 56
57 57 def __repr__(self):
58 58 return '<NotDefined: %s>' % self.name
59 59
60 60 __str__ = __repr__
61 61
62 62
63 63 class QueueCleared(KernelError):
64 64 pass
65 65
66 66
67 67 class IdInUse(KernelError):
68 68 pass
69 69
70 70
71 71 class ProtocolError(KernelError):
72 72 pass
73 73
74 74
75 75 class ConnectionError(KernelError):
76 76 pass
77 77
78 78
79 79 class InvalidEngineID(KernelError):
80 80 pass
81 81
82 82
83 83 class NoEnginesRegistered(KernelError):
84 84 pass
85 85
86 86
87 87 class InvalidClientID(KernelError):
88 88 pass
89 89
90 90
91 91 class InvalidDeferredID(KernelError):
92 92 pass
93 93
94 94
95 95 class SerializationError(KernelError):
96 96 pass
97 97
98 98
99 99 class MessageSizeError(KernelError):
100 100 pass
101 101
102 102
103 103 class PBMessageSizeError(MessageSizeError):
104 104 pass
105 105
106 106
107 107 class ResultNotCompleted(KernelError):
108 108 pass
109 109
110 110
111 111 class ResultAlreadyRetrieved(KernelError):
112 112 pass
113 113
114 114 class ClientError(KernelError):
115 115 pass
116 116
117 117
118 118 class TaskAborted(KernelError):
119 119 pass
120 120
121 121
122 122 class TaskTimeout(KernelError):
123 123 pass
124 124
125 125
126 126 class NotAPendingResult(KernelError):
127 127 pass
128 128
129 129
130 130 class UnpickleableException(KernelError):
131 131 pass
132 132
133 133
134 134 class AbortedPendingDeferredError(KernelError):
135 135 pass
136 136
137 137
138 138 class InvalidProperty(KernelError):
139 139 pass
140 140
141 141
142 142 class MissingBlockArgument(KernelError):
143 143 pass
144 144
145 145
146 146 class StopLocalExecution(KernelError):
147 147 pass
148 148
149 149
150 150 class SecurityError(KernelError):
151 151 pass
152 152
153 153
154 154 class FileTimeoutError(KernelError):
155 155 pass
156 156
157 157 class TimeoutError(KernelError):
158 158 pass
159 159
160 160 class UnmetDependency(KernelError):
161 161 pass
162 162
163 163 class ImpossibleDependency(UnmetDependency):
164 164 pass
165 165
166 166 class DependencyTimeout(ImpossibleDependency):
167 167 pass
168 168
169 169 class InvalidDependency(ImpossibleDependency):
170 170 pass
171 171
172 172 class RemoteError(KernelError):
173 173 """Error raised elsewhere"""
174 174 ename=None
175 175 evalue=None
176 176 traceback=None
177 177 engine_info=None
178 178
179 179 def __init__(self, ename, evalue, traceback, engine_info=None):
180 180 self.ename=ename
181 181 self.evalue=evalue
182 182 self.traceback=traceback
183 183 self.engine_info=engine_info or {}
184 184 self.args=(ename, evalue)
185 185
186 186 def __repr__(self):
187 187 engineid = self.engine_info.get('engine_id', ' ')
188 188 return "<Remote[%s]:%s(%s)>"%(engineid, self.ename, self.evalue)
189 189
190 190 def __str__(self):
191 191 return "%s(%s)" % (self.ename, self.evalue)
192 192
193 def render_traceback(self):
193 def _render_traceback_(self):
194 194 """render traceback to a list of lines"""
195 195 return (self.traceback or "No traceback available").splitlines()
196
197 # Previous name retained for backwards compatibility.
198 render_traceback = _render_traceback_
196 199
197 200 def print_traceback(self, excid=None):
198 201 """print my traceback"""
199 202 print('\n'.join(self.render_traceback()))
200 203
201 204
202 205
203 206
204 207 class TaskRejectError(KernelError):
205 208 """Exception to raise when a task should be rejected by an engine.
206 209
207 210 This exception can be used to allow a task running on an engine to test
208 211 if the engine (or the user's namespace on the engine) has the needed
209 212 task dependencies. If not, the task should raise this exception. For
210 213 the task to be retried on another engine, the task should be created
211 214 with the `retries` argument > 1.
212 215
213 216 The advantage of this approach over our older properties system is that
214 217 tasks have full access to the user's namespace on the engines and the
215 218 properties don't have to be managed or tested by the controller.
216 219 """
217 220
218 221
219 222 class CompositeError(RemoteError):
220 223 """Error for representing possibly multiple errors on engines"""
221 224 def __init__(self, message, elist):
222 225 Exception.__init__(self, *(message, elist))
223 226 # Don't use pack_exception because it will conflict with the .message
224 227 # attribute that is being deprecated in 2.6 and beyond.
225 228 self.msg = message
226 229 self.elist = elist
227 230 self.args = [ e[0] for e in elist ]
228 231
229 232 def _get_engine_str(self, ei):
230 233 if not ei:
231 234 return '[Engine Exception]'
232 235 else:
233 236 return '[%s:%s]: ' % (ei['engine_id'], ei['method'])
234 237
235 238 def _get_traceback(self, ev):
236 239 try:
237 240 tb = ev._ipython_traceback_text
238 241 except AttributeError:
239 242 return 'No traceback available'
240 243 else:
241 244 return tb
242 245
243 246 def __str__(self):
244 247 s = str(self.msg)
245 248 for en, ev, etb, ei in self.elist:
246 249 engine_str = self._get_engine_str(ei)
247 250 s = s + '\n' + engine_str + en + ': ' + str(ev)
248 251 return s
249 252
250 253 def __repr__(self):
251 254 return "CompositeError(%i)"%len(self.elist)
252 255
253 256 def render_traceback(self, excid=None):
254 257 """render one or all of my tracebacks to a list of lines"""
255 258 lines = []
256 259 if excid is None:
257 260 for (en,ev,etb,ei) in self.elist:
258 261 lines.append(self._get_engine_str(ei))
259 262 lines.extend((etb or 'No traceback available').splitlines())
260 263 lines.append('')
261 264 else:
262 265 try:
263 266 en,ev,etb,ei = self.elist[excid]
264 267 except:
265 268 raise IndexError("an exception with index %i does not exist"%excid)
266 269 else:
267 270 lines.append(self._get_engine_str(ei))
268 271 lines.extend((etb or 'No traceback available').splitlines())
269 272
270 273 return lines
271 274
272 275 def print_traceback(self, excid=None):
273 276 print('\n'.join(self.render_traceback(excid)))
274 277
275 278 def raise_exception(self, excid=0):
276 279 try:
277 280 en,ev,etb,ei = self.elist[excid]
278 281 except:
279 282 raise IndexError("an exception with index %i does not exist"%excid)
280 283 else:
281 284 raise RemoteError(en, ev, etb, ei)
282 285
283 286
284 287 def collect_exceptions(rdict_or_list, method='unspecified'):
285 288 """check a result dict for errors, and raise CompositeError if any exist.
286 289 Passthrough otherwise."""
287 290 elist = []
288 291 if isinstance(rdict_or_list, dict):
289 292 rlist = rdict_or_list.values()
290 293 else:
291 294 rlist = rdict_or_list
292 295 for r in rlist:
293 296 if isinstance(r, RemoteError):
294 297 en, ev, etb, ei = r.ename, r.evalue, r.traceback, r.engine_info
295 298 # Sometimes we could have CompositeError in our list. Just take
296 299 # the errors out of them and put them in our new list. This
297 300 # has the effect of flattening lists of CompositeErrors into one
298 301 # CompositeError
299 302 if en=='CompositeError':
300 303 for e in ev.elist:
301 304 elist.append(e)
302 305 else:
303 306 elist.append((en, ev, etb, ei))
304 307 if len(elist)==0:
305 308 return rdict_or_list
306 309 else:
307 310 msg = "one or more exceptions from call to method: %s" % (method)
308 311 # This silliness is needed so the debugger has access to the exception
309 312 # instance (e in this case)
310 313 try:
311 314 raise CompositeError(msg, elist)
312 315 except CompositeError as e:
313 316 raise e
314 317
315 318 def wrap_exception(engine_info={}):
316 319 etype, evalue, tb = sys.exc_info()
317 320 stb = traceback.format_exception(etype, evalue, tb)
318 321 exc_content = {
319 322 'status' : 'error',
320 323 'traceback' : stb,
321 324 'ename' : unicode(etype.__name__),
322 325 'evalue' : unicode(evalue),
323 326 'engine_info' : engine_info
324 327 }
325 328 return exc_content
326 329
327 330 def unwrap_exception(content):
328 331 err = RemoteError(content['ename'], content['evalue'],
329 332 ''.join(content['traceback']),
330 333 content.get('engine_info', {}))
331 334 return err
332 335
General Comments 0
You need to be logged in to leave comments. Login now