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