##// END OF EJS Templates
Make post-execution happen at the cell instead of the block level....
Fernando Perez -
Show More
@@ -1,2615 +1,2606
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__
21 21 import __future__
22 22 import abc
23 23 import ast
24 24 import atexit
25 25 import codeop
26 26 import inspect
27 27 import os
28 28 import re
29 29 import sys
30 30 import tempfile
31 31 import types
32 32 from contextlib import nested
33 33
34 34 from IPython.config.configurable import Configurable
35 35 from IPython.core import debugger, oinspect
36 36 from IPython.core import history as ipcorehist
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
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
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 TryNext, UsageError
49 49 from IPython.core.extensions import ExtensionManager
50 50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
51 51 from IPython.core.formatters import DisplayFormatter
52 52 from IPython.core.history import HistoryManager
53 53 from IPython.core.inputsplitter import IPythonInputSplitter
54 54 from IPython.core.logger import Logger
55 55 from IPython.core.macro import Macro
56 56 from IPython.core.magic import Magic
57 57 from IPython.core.payload import PayloadManager
58 58 from IPython.core.plugin import PluginManager
59 59 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
60 60 from IPython.external.Itpl import ItplNS
61 61 from IPython.utils import PyColorize
62 62 from IPython.utils import io
63 63 from IPython.utils.doctestreload import doctest_reload
64 64 from IPython.utils.io import ask_yes_no, rprint
65 65 from IPython.utils.ipstruct import Struct
66 66 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
67 67 from IPython.utils.pickleshare import PickleShareDB
68 68 from IPython.utils.process import system, getoutput
69 69 from IPython.utils.strdispatch import StrDispatch
70 70 from IPython.utils.syspathcontext import prepended_to_syspath
71 71 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
72 72 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
73 73 List, Unicode, Instance, Type)
74 74 from IPython.utils.warn import warn, error, fatal
75 75 import IPython.core.hooks
76 76
77 77 #-----------------------------------------------------------------------------
78 78 # Globals
79 79 #-----------------------------------------------------------------------------
80 80
81 81 # compiled regexps for autoindent management
82 82 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
83 83
84 84 #-----------------------------------------------------------------------------
85 85 # Utilities
86 86 #-----------------------------------------------------------------------------
87 87
88 88 # store the builtin raw_input globally, and use this always, in case user code
89 89 # overwrites it (like wx.py.PyShell does)
90 90 raw_input_original = raw_input
91 91
92 92 def softspace(file, newvalue):
93 93 """Copied from code.py, to remove the dependency"""
94 94
95 95 oldvalue = 0
96 96 try:
97 97 oldvalue = file.softspace
98 98 except AttributeError:
99 99 pass
100 100 try:
101 101 file.softspace = newvalue
102 102 except (AttributeError, TypeError):
103 103 # "attribute-less object" or "read-only attributes"
104 104 pass
105 105 return oldvalue
106 106
107 107
108 108 def no_op(*a, **kw): pass
109 109
110 110 class SpaceInInput(Exception): pass
111 111
112 112 class Bunch: pass
113 113
114 114
115 115 def get_default_colors():
116 116 if sys.platform=='darwin':
117 117 return "LightBG"
118 118 elif os.name=='nt':
119 119 return 'Linux'
120 120 else:
121 121 return 'Linux'
122 122
123 123
124 124 class SeparateStr(Str):
125 125 """A Str subclass to validate separate_in, separate_out, etc.
126 126
127 127 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
128 128 """
129 129
130 130 def validate(self, obj, value):
131 131 if value == '0': value = ''
132 132 value = value.replace('\\n','\n')
133 133 return super(SeparateStr, self).validate(obj, value)
134 134
135 135 class MultipleInstanceError(Exception):
136 136 pass
137 137
138 138 class ReadlineNoRecord(object):
139 139 """Context manager to execute some code, then reload readline history
140 140 so that interactive input to the code doesn't appear when pressing up."""
141 141 def __init__(self, shell):
142 142 self.shell = shell
143 143 self._nested_level = 0
144 144
145 145 def __enter__(self):
146 146 if self._nested_level == 0:
147 147 self.orig_length = self.current_length()
148 148 self.readline_tail = self.get_readline_tail()
149 149 self._nested_level += 1
150 150
151 151 def __exit__(self, type, value, traceback):
152 152 self._nested_level -= 1
153 153 if self._nested_level == 0:
154 154 # Try clipping the end if it's got longer
155 155 e = self.current_length() - self.orig_length
156 156 if e > 0:
157 157 for _ in range(e):
158 158 self.shell.readline.remove_history_item(self.orig_length)
159 159
160 160 # If it still doesn't match, just reload readline history.
161 161 if self.current_length() != self.orig_length \
162 162 or self.get_readline_tail() != self.readline_tail:
163 163 self.shell.refill_readline_hist()
164 164 # Returning False will cause exceptions to propagate
165 165 return False
166 166
167 167 def current_length(self):
168 168 return self.shell.readline.get_current_history_length()
169 169
170 170 def get_readline_tail(self, n=10):
171 171 """Get the last n items in readline history."""
172 172 end = self.shell.readline.get_current_history_length() + 1
173 173 start = max(end-n, 1)
174 174 ghi = self.shell.readline.get_history_item
175 175 return [ghi(x) for x in range(start, end)]
176 176
177 177
178 178 #-----------------------------------------------------------------------------
179 179 # Main IPython class
180 180 #-----------------------------------------------------------------------------
181 181
182 182 class InteractiveShell(Configurable, Magic):
183 183 """An enhanced, interactive shell for Python."""
184 184
185 185 _instance = None
186 186 autocall = Enum((0,1,2), default_value=1, config=True)
187 187 # TODO: remove all autoindent logic and put into frontends.
188 188 # We can't do this yet because even runlines uses the autoindent.
189 189 autoindent = CBool(True, config=True)
190 190 automagic = CBool(True, config=True)
191 191 cache_size = Int(1000, config=True)
192 192 color_info = CBool(True, config=True)
193 193 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
194 194 default_value=get_default_colors(), config=True)
195 195 debug = CBool(False, config=True)
196 196 deep_reload = CBool(False, config=True)
197 197 display_formatter = Instance(DisplayFormatter)
198 198 displayhook_class = Type(DisplayHook)
199 199 display_pub_class = Type(DisplayPublisher)
200 200
201 201 exit_now = CBool(False)
202 202 exiter = Instance(ExitAutocall)
203 203 def _exiter_default(self):
204 204 return ExitAutocall(self)
205 205 # Monotonically increasing execution counter
206 206 execution_count = Int(1)
207 207 filename = Unicode("<ipython console>")
208 208 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
209 209
210 210 # Input splitter, to split entire cells of input into either individual
211 211 # interactive statements or whole blocks.
212 212 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
213 213 (), {})
214 214 logstart = CBool(False, config=True)
215 215 logfile = Unicode('', config=True)
216 216 logappend = Unicode('', config=True)
217 217 object_info_string_level = Enum((0,1,2), default_value=0,
218 218 config=True)
219 219 pdb = CBool(False, config=True)
220 220
221 221 profile = Unicode('', config=True)
222 222 prompt_in1 = Str('In [\\#]: ', config=True)
223 223 prompt_in2 = Str(' .\\D.: ', config=True)
224 224 prompt_out = Str('Out[\\#]: ', config=True)
225 225 prompts_pad_left = CBool(True, config=True)
226 226 quiet = CBool(False, config=True)
227 227
228 228 history_length = Int(10000, config=True)
229 229
230 230 # The readline stuff will eventually be moved to the terminal subclass
231 231 # but for now, we can't do that as readline is welded in everywhere.
232 232 readline_use = CBool(True, config=True)
233 233 readline_merge_completions = CBool(True, config=True)
234 234 readline_omit__names = Enum((0,1,2), default_value=2, config=True)
235 235 readline_remove_delims = Str('-/~', config=True)
236 236 readline_parse_and_bind = List([
237 237 'tab: complete',
238 238 '"\C-l": clear-screen',
239 239 'set show-all-if-ambiguous on',
240 240 '"\C-o": tab-insert',
241 241 # See bug gh-58 - with \M-i enabled, chars 0x9000-0x9fff
242 242 # crash IPython.
243 243 '"\M-o": "\d\d\d\d"',
244 244 '"\M-I": "\d\d\d\d"',
245 245 '"\C-r": reverse-search-history',
246 246 '"\C-s": forward-search-history',
247 247 '"\C-p": history-search-backward',
248 248 '"\C-n": history-search-forward',
249 249 '"\e[A": history-search-backward',
250 250 '"\e[B": history-search-forward',
251 251 '"\C-k": kill-line',
252 252 '"\C-u": unix-line-discard',
253 253 ], allow_none=False, config=True)
254 254
255 255 # TODO: this part of prompt management should be moved to the frontends.
256 256 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
257 257 separate_in = SeparateStr('\n', config=True)
258 258 separate_out = SeparateStr('', config=True)
259 259 separate_out2 = SeparateStr('', config=True)
260 260 wildcards_case_sensitive = CBool(True, config=True)
261 261 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
262 262 default_value='Context', config=True)
263 263
264 264 # Subcomponents of InteractiveShell
265 265 alias_manager = Instance('IPython.core.alias.AliasManager')
266 266 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
267 267 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
268 268 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
269 269 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
270 270 plugin_manager = Instance('IPython.core.plugin.PluginManager')
271 271 payload_manager = Instance('IPython.core.payload.PayloadManager')
272 272 history_manager = Instance('IPython.core.history.HistoryManager')
273 273
274 274 # Private interface
275 _post_execute = set()
275 _post_execute = Instance(dict)
276 276
277 277 def __init__(self, config=None, ipython_dir=None,
278 278 user_ns=None, user_global_ns=None,
279 279 custom_exceptions=((), None)):
280 280
281 281 # This is where traits with a config_key argument are updated
282 282 # from the values on config.
283 283 super(InteractiveShell, self).__init__(config=config)
284 284
285 285 # These are relatively independent and stateless
286 286 self.init_ipython_dir(ipython_dir)
287 287 self.init_instance_attrs()
288 288 self.init_environment()
289 289
290 290 # Create namespaces (user_ns, user_global_ns, etc.)
291 291 self.init_create_namespaces(user_ns, user_global_ns)
292 292 # This has to be done after init_create_namespaces because it uses
293 293 # something in self.user_ns, but before init_sys_modules, which
294 294 # is the first thing to modify sys.
295 295 # TODO: When we override sys.stdout and sys.stderr before this class
296 296 # is created, we are saving the overridden ones here. Not sure if this
297 297 # is what we want to do.
298 298 self.save_sys_module_state()
299 299 self.init_sys_modules()
300 300
301 301 # While we're trying to have each part of the code directly access what
302 302 # it needs without keeping redundant references to objects, we have too
303 303 # much legacy code that expects ip.db to exist.
304 304 self.db = PickleShareDB(os.path.join(self.ipython_dir, 'db'))
305 305
306 306 self.init_history()
307 307 self.init_encoding()
308 308 self.init_prefilter()
309 309
310 310 Magic.__init__(self, self)
311 311
312 312 self.init_syntax_highlighting()
313 313 self.init_hooks()
314 314 self.init_pushd_popd_magic()
315 315 # self.init_traceback_handlers use to be here, but we moved it below
316 316 # because it and init_io have to come after init_readline.
317 317 self.init_user_ns()
318 318 self.init_logger()
319 319 self.init_alias()
320 320 self.init_builtins()
321 321
322 322 # pre_config_initialization
323 323
324 324 # The next section should contain everything that was in ipmaker.
325 325 self.init_logstart()
326 326
327 327 # The following was in post_config_initialization
328 328 self.init_inspector()
329 329 # init_readline() must come before init_io(), because init_io uses
330 330 # readline related things.
331 331 self.init_readline()
332 332 # init_completer must come after init_readline, because it needs to
333 333 # know whether readline is present or not system-wide to configure the
334 334 # completers, since the completion machinery can now operate
335 335 # independently of readline (e.g. over the network)
336 336 self.init_completer()
337 337 # TODO: init_io() needs to happen before init_traceback handlers
338 338 # because the traceback handlers hardcode the stdout/stderr streams.
339 339 # This logic in in debugger.Pdb and should eventually be changed.
340 340 self.init_io()
341 341 self.init_traceback_handlers(custom_exceptions)
342 342 self.init_prompts()
343 343 self.init_display_formatter()
344 344 self.init_display_pub()
345 345 self.init_displayhook()
346 346 self.init_reload_doctest()
347 347 self.init_magics()
348 348 self.init_pdb()
349 349 self.init_extension_manager()
350 350 self.init_plugin_manager()
351 351 self.init_payload()
352 352 self.hooks.late_startup_hook()
353 353 atexit.register(self.atexit_operations)
354 354
355 355 @classmethod
356 356 def instance(cls, *args, **kwargs):
357 357 """Returns a global InteractiveShell instance."""
358 358 if cls._instance is None:
359 359 inst = cls(*args, **kwargs)
360 360 # Now make sure that the instance will also be returned by
361 361 # the subclasses instance attribute.
362 362 for subclass in cls.mro():
363 363 if issubclass(cls, subclass) and \
364 364 issubclass(subclass, InteractiveShell):
365 365 subclass._instance = inst
366 366 else:
367 367 break
368 368 if isinstance(cls._instance, cls):
369 369 return cls._instance
370 370 else:
371 371 raise MultipleInstanceError(
372 372 'Multiple incompatible subclass instances of '
373 373 'InteractiveShell are being created.'
374 374 )
375 375
376 376 @classmethod
377 377 def initialized(cls):
378 378 return hasattr(cls, "_instance")
379 379
380 380 def get_ipython(self):
381 381 """Return the currently running IPython instance."""
382 382 return self
383 383
384 384 #-------------------------------------------------------------------------
385 385 # Trait changed handlers
386 386 #-------------------------------------------------------------------------
387 387
388 388 def _ipython_dir_changed(self, name, new):
389 389 if not os.path.isdir(new):
390 390 os.makedirs(new, mode = 0777)
391 391
392 392 def set_autoindent(self,value=None):
393 393 """Set the autoindent flag, checking for readline support.
394 394
395 395 If called with no arguments, it acts as a toggle."""
396 396
397 397 if not self.has_readline:
398 398 if os.name == 'posix':
399 399 warn("The auto-indent feature requires the readline library")
400 400 self.autoindent = 0
401 401 return
402 402 if value is None:
403 403 self.autoindent = not self.autoindent
404 404 else:
405 405 self.autoindent = value
406 406
407 407 #-------------------------------------------------------------------------
408 408 # init_* methods called by __init__
409 409 #-------------------------------------------------------------------------
410 410
411 411 def init_ipython_dir(self, ipython_dir):
412 412 if ipython_dir is not None:
413 413 self.ipython_dir = ipython_dir
414 414 self.config.Global.ipython_dir = self.ipython_dir
415 415 return
416 416
417 417 if hasattr(self.config.Global, 'ipython_dir'):
418 418 self.ipython_dir = self.config.Global.ipython_dir
419 419 else:
420 420 self.ipython_dir = get_ipython_dir()
421 421
422 422 # All children can just read this
423 423 self.config.Global.ipython_dir = self.ipython_dir
424 424
425 425 def init_instance_attrs(self):
426 426 self.more = False
427 427
428 428 # command compiler
429 429 self.compile = CachingCompiler()
430 430
431 431 # User input buffers
432 432 # NOTE: these variables are slated for full removal, once we are 100%
433 433 # sure that the new execution logic is solid. We will delte runlines,
434 434 # push_line and these buffers, as all input will be managed by the
435 435 # frontends via an inputsplitter instance.
436 436 self.buffer = []
437 437 self.buffer_raw = []
438 438
439 439 # Make an empty namespace, which extension writers can rely on both
440 440 # existing and NEVER being used by ipython itself. This gives them a
441 441 # convenient location for storing additional information and state
442 442 # their extensions may require, without fear of collisions with other
443 443 # ipython names that may develop later.
444 444 self.meta = Struct()
445 445
446 446 # Object variable to store code object waiting execution. This is
447 447 # used mainly by the multithreaded shells, but it can come in handy in
448 448 # other situations. No need to use a Queue here, since it's a single
449 449 # item which gets cleared once run.
450 450 self.code_to_run = None
451 451
452 452 # Temporary files used for various purposes. Deleted at exit.
453 453 self.tempfiles = []
454 454
455 455 # Keep track of readline usage (later set by init_readline)
456 456 self.has_readline = False
457 457
458 458 # keep track of where we started running (mainly for crash post-mortem)
459 459 # This is not being used anywhere currently.
460 460 self.starting_dir = os.getcwd()
461 461
462 462 # Indentation management
463 463 self.indent_current_nsp = 0
464 464
465 # Dict to track post-execution functions that have been registered
466 self._post_execute = {}
467
465 468 def init_environment(self):
466 469 """Any changes we need to make to the user's environment."""
467 470 pass
468 471
469 472 def init_encoding(self):
470 473 # Get system encoding at startup time. Certain terminals (like Emacs
471 474 # under Win32 have it set to None, and we need to have a known valid
472 475 # encoding to use in the raw_input() method
473 476 try:
474 477 self.stdin_encoding = sys.stdin.encoding or 'ascii'
475 478 except AttributeError:
476 479 self.stdin_encoding = 'ascii'
477 480
478 481 def init_syntax_highlighting(self):
479 482 # Python source parser/formatter for syntax highlighting
480 483 pyformat = PyColorize.Parser().format
481 484 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
482 485
483 486 def init_pushd_popd_magic(self):
484 487 # for pushd/popd management
485 488 try:
486 489 self.home_dir = get_home_dir()
487 490 except HomeDirError, msg:
488 491 fatal(msg)
489 492
490 493 self.dir_stack = []
491 494
492 495 def init_logger(self):
493 496 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
494 497 logmode='rotate')
495 498
496 499 def init_logstart(self):
497 500 """Initialize logging in case it was requested at the command line.
498 501 """
499 502 if self.logappend:
500 503 self.magic_logstart(self.logappend + ' append')
501 504 elif self.logfile:
502 505 self.magic_logstart(self.logfile)
503 506 elif self.logstart:
504 507 self.magic_logstart()
505 508
506 509 def init_builtins(self):
507 510 self.builtin_trap = BuiltinTrap(shell=self)
508 511
509 512 def init_inspector(self):
510 513 # Object inspector
511 514 self.inspector = oinspect.Inspector(oinspect.InspectColors,
512 515 PyColorize.ANSICodeColors,
513 516 'NoColor',
514 517 self.object_info_string_level)
515 518
516 519 def init_io(self):
517 520 # This will just use sys.stdout and sys.stderr. If you want to
518 521 # override sys.stdout and sys.stderr themselves, you need to do that
519 522 # *before* instantiating this class, because Term holds onto
520 523 # references to the underlying streams.
521 524 if sys.platform == 'win32' and self.has_readline:
522 525 Term = io.IOTerm(cout=self.readline._outputfile,
523 526 cerr=self.readline._outputfile)
524 527 else:
525 528 Term = io.IOTerm()
526 529 io.Term = Term
527 530
528 531 def init_prompts(self):
529 532 # TODO: This is a pass for now because the prompts are managed inside
530 533 # the DisplayHook. Once there is a separate prompt manager, this
531 534 # will initialize that object and all prompt related information.
532 535 pass
533 536
534 537 def init_display_formatter(self):
535 538 self.display_formatter = DisplayFormatter(config=self.config)
536 539
537 540 def init_display_pub(self):
538 541 self.display_pub = self.display_pub_class(config=self.config)
539 542
540 543 def init_displayhook(self):
541 544 # Initialize displayhook, set in/out prompts and printing system
542 545 self.displayhook = self.displayhook_class(
543 546 config=self.config,
544 547 shell=self,
545 548 cache_size=self.cache_size,
546 549 input_sep = self.separate_in,
547 550 output_sep = self.separate_out,
548 551 output_sep2 = self.separate_out2,
549 552 ps1 = self.prompt_in1,
550 553 ps2 = self.prompt_in2,
551 554 ps_out = self.prompt_out,
552 555 pad_left = self.prompts_pad_left
553 556 )
554 557 # This is a context manager that installs/revmoes the displayhook at
555 558 # the appropriate time.
556 559 self.display_trap = DisplayTrap(hook=self.displayhook)
557 560
558 561 def init_reload_doctest(self):
559 562 # Do a proper resetting of doctest, including the necessary displayhook
560 563 # monkeypatching
561 564 try:
562 565 doctest_reload()
563 566 except ImportError:
564 567 warn("doctest module does not exist.")
565 568
566 569 #-------------------------------------------------------------------------
567 570 # Things related to injections into the sys module
568 571 #-------------------------------------------------------------------------
569 572
570 573 def save_sys_module_state(self):
571 574 """Save the state of hooks in the sys module.
572 575
573 576 This has to be called after self.user_ns is created.
574 577 """
575 578 self._orig_sys_module_state = {}
576 579 self._orig_sys_module_state['stdin'] = sys.stdin
577 580 self._orig_sys_module_state['stdout'] = sys.stdout
578 581 self._orig_sys_module_state['stderr'] = sys.stderr
579 582 self._orig_sys_module_state['excepthook'] = sys.excepthook
580 583 try:
581 584 self._orig_sys_modules_main_name = self.user_ns['__name__']
582 585 except KeyError:
583 586 pass
584 587
585 588 def restore_sys_module_state(self):
586 589 """Restore the state of the sys module."""
587 590 try:
588 591 for k, v in self._orig_sys_module_state.iteritems():
589 592 setattr(sys, k, v)
590 593 except AttributeError:
591 594 pass
592 595 # Reset what what done in self.init_sys_modules
593 596 try:
594 597 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
595 598 except (AttributeError, KeyError):
596 599 pass
597 600
598 601 #-------------------------------------------------------------------------
599 602 # Things related to hooks
600 603 #-------------------------------------------------------------------------
601 604
602 605 def init_hooks(self):
603 606 # hooks holds pointers used for user-side customizations
604 607 self.hooks = Struct()
605 608
606 609 self.strdispatchers = {}
607 610
608 611 # Set all default hooks, defined in the IPython.hooks module.
609 612 hooks = IPython.core.hooks
610 613 for hook_name in hooks.__all__:
611 614 # default hooks have priority 100, i.e. low; user hooks should have
612 615 # 0-100 priority
613 616 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
614 617
615 618 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
616 619 """set_hook(name,hook) -> sets an internal IPython hook.
617 620
618 621 IPython exposes some of its internal API as user-modifiable hooks. By
619 622 adding your function to one of these hooks, you can modify IPython's
620 623 behavior to call at runtime your own routines."""
621 624
622 625 # At some point in the future, this should validate the hook before it
623 626 # accepts it. Probably at least check that the hook takes the number
624 627 # of args it's supposed to.
625 628
626 629 f = types.MethodType(hook,self)
627 630
628 631 # check if the hook is for strdispatcher first
629 632 if str_key is not None:
630 633 sdp = self.strdispatchers.get(name, StrDispatch())
631 634 sdp.add_s(str_key, f, priority )
632 635 self.strdispatchers[name] = sdp
633 636 return
634 637 if re_key is not None:
635 638 sdp = self.strdispatchers.get(name, StrDispatch())
636 639 sdp.add_re(re.compile(re_key), f, priority )
637 640 self.strdispatchers[name] = sdp
638 641 return
639 642
640 643 dp = getattr(self.hooks, name, None)
641 644 if name not in IPython.core.hooks.__all__:
642 645 print "Warning! Hook '%s' is not one of %s" % \
643 646 (name, IPython.core.hooks.__all__ )
644 647 if not dp:
645 648 dp = IPython.core.hooks.CommandChainDispatcher()
646 649
647 650 try:
648 651 dp.add(f,priority)
649 652 except AttributeError:
650 653 # it was not commandchain, plain old func - replace
651 654 dp = f
652 655
653 656 setattr(self.hooks,name, dp)
654 657
655 658 def register_post_execute(self, func):
656 659 """Register a function for calling after code execution.
657 660 """
658 661 if not callable(func):
659 662 raise ValueError('argument %s must be callable' % func)
660 self._post_execute.add(func)
663 self._post_execute[func] = True
661 664
662 665 #-------------------------------------------------------------------------
663 666 # Things related to the "main" module
664 667 #-------------------------------------------------------------------------
665 668
666 669 def new_main_mod(self,ns=None):
667 670 """Return a new 'main' module object for user code execution.
668 671 """
669 672 main_mod = self._user_main_module
670 673 init_fakemod_dict(main_mod,ns)
671 674 return main_mod
672 675
673 676 def cache_main_mod(self,ns,fname):
674 677 """Cache a main module's namespace.
675 678
676 679 When scripts are executed via %run, we must keep a reference to the
677 680 namespace of their __main__ module (a FakeModule instance) around so
678 681 that Python doesn't clear it, rendering objects defined therein
679 682 useless.
680 683
681 684 This method keeps said reference in a private dict, keyed by the
682 685 absolute path of the module object (which corresponds to the script
683 686 path). This way, for multiple executions of the same script we only
684 687 keep one copy of the namespace (the last one), thus preventing memory
685 688 leaks from old references while allowing the objects from the last
686 689 execution to be accessible.
687 690
688 691 Note: we can not allow the actual FakeModule instances to be deleted,
689 692 because of how Python tears down modules (it hard-sets all their
690 693 references to None without regard for reference counts). This method
691 694 must therefore make a *copy* of the given namespace, to allow the
692 695 original module's __dict__ to be cleared and reused.
693 696
694 697
695 698 Parameters
696 699 ----------
697 700 ns : a namespace (a dict, typically)
698 701
699 702 fname : str
700 703 Filename associated with the namespace.
701 704
702 705 Examples
703 706 --------
704 707
705 708 In [10]: import IPython
706 709
707 710 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
708 711
709 712 In [12]: IPython.__file__ in _ip._main_ns_cache
710 713 Out[12]: True
711 714 """
712 715 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
713 716
714 717 def clear_main_mod_cache(self):
715 718 """Clear the cache of main modules.
716 719
717 720 Mainly for use by utilities like %reset.
718 721
719 722 Examples
720 723 --------
721 724
722 725 In [15]: import IPython
723 726
724 727 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
725 728
726 729 In [17]: len(_ip._main_ns_cache) > 0
727 730 Out[17]: True
728 731
729 732 In [18]: _ip.clear_main_mod_cache()
730 733
731 734 In [19]: len(_ip._main_ns_cache) == 0
732 735 Out[19]: True
733 736 """
734 737 self._main_ns_cache.clear()
735 738
736 739 #-------------------------------------------------------------------------
737 740 # Things related to debugging
738 741 #-------------------------------------------------------------------------
739 742
740 743 def init_pdb(self):
741 744 # Set calling of pdb on exceptions
742 745 # self.call_pdb is a property
743 746 self.call_pdb = self.pdb
744 747
745 748 def _get_call_pdb(self):
746 749 return self._call_pdb
747 750
748 751 def _set_call_pdb(self,val):
749 752
750 753 if val not in (0,1,False,True):
751 754 raise ValueError,'new call_pdb value must be boolean'
752 755
753 756 # store value in instance
754 757 self._call_pdb = val
755 758
756 759 # notify the actual exception handlers
757 760 self.InteractiveTB.call_pdb = val
758 761
759 762 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
760 763 'Control auto-activation of pdb at exceptions')
761 764
762 765 def debugger(self,force=False):
763 766 """Call the pydb/pdb debugger.
764 767
765 768 Keywords:
766 769
767 770 - force(False): by default, this routine checks the instance call_pdb
768 771 flag and does not actually invoke the debugger if the flag is false.
769 772 The 'force' option forces the debugger to activate even if the flag
770 773 is false.
771 774 """
772 775
773 776 if not (force or self.call_pdb):
774 777 return
775 778
776 779 if not hasattr(sys,'last_traceback'):
777 780 error('No traceback has been produced, nothing to debug.')
778 781 return
779 782
780 783 # use pydb if available
781 784 if debugger.has_pydb:
782 785 from pydb import pm
783 786 else:
784 787 # fallback to our internal debugger
785 788 pm = lambda : self.InteractiveTB.debugger(force=True)
786 789
787 790 with self.readline_no_record:
788 791 pm()
789 792
790 793 #-------------------------------------------------------------------------
791 794 # Things related to IPython's various namespaces
792 795 #-------------------------------------------------------------------------
793 796
794 797 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
795 798 # Create the namespace where the user will operate. user_ns is
796 799 # normally the only one used, and it is passed to the exec calls as
797 800 # the locals argument. But we do carry a user_global_ns namespace
798 801 # given as the exec 'globals' argument, This is useful in embedding
799 802 # situations where the ipython shell opens in a context where the
800 803 # distinction between locals and globals is meaningful. For
801 804 # non-embedded contexts, it is just the same object as the user_ns dict.
802 805
803 806 # FIXME. For some strange reason, __builtins__ is showing up at user
804 807 # level as a dict instead of a module. This is a manual fix, but I
805 808 # should really track down where the problem is coming from. Alex
806 809 # Schmolck reported this problem first.
807 810
808 811 # A useful post by Alex Martelli on this topic:
809 812 # Re: inconsistent value from __builtins__
810 813 # Von: Alex Martelli <aleaxit@yahoo.com>
811 814 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
812 815 # Gruppen: comp.lang.python
813 816
814 817 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
815 818 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
816 819 # > <type 'dict'>
817 820 # > >>> print type(__builtins__)
818 821 # > <type 'module'>
819 822 # > Is this difference in return value intentional?
820 823
821 824 # Well, it's documented that '__builtins__' can be either a dictionary
822 825 # or a module, and it's been that way for a long time. Whether it's
823 826 # intentional (or sensible), I don't know. In any case, the idea is
824 827 # that if you need to access the built-in namespace directly, you
825 828 # should start with "import __builtin__" (note, no 's') which will
826 829 # definitely give you a module. Yeah, it's somewhat confusing:-(.
827 830
828 831 # These routines return properly built dicts as needed by the rest of
829 832 # the code, and can also be used by extension writers to generate
830 833 # properly initialized namespaces.
831 834 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
832 835 user_global_ns)
833 836
834 837 # Assign namespaces
835 838 # This is the namespace where all normal user variables live
836 839 self.user_ns = user_ns
837 840 self.user_global_ns = user_global_ns
838 841
839 842 # An auxiliary namespace that checks what parts of the user_ns were
840 843 # loaded at startup, so we can list later only variables defined in
841 844 # actual interactive use. Since it is always a subset of user_ns, it
842 845 # doesn't need to be separately tracked in the ns_table.
843 846 self.user_ns_hidden = {}
844 847
845 848 # A namespace to keep track of internal data structures to prevent
846 849 # them from cluttering user-visible stuff. Will be updated later
847 850 self.internal_ns = {}
848 851
849 852 # Now that FakeModule produces a real module, we've run into a nasty
850 853 # problem: after script execution (via %run), the module where the user
851 854 # code ran is deleted. Now that this object is a true module (needed
852 855 # so docetst and other tools work correctly), the Python module
853 856 # teardown mechanism runs over it, and sets to None every variable
854 857 # present in that module. Top-level references to objects from the
855 858 # script survive, because the user_ns is updated with them. However,
856 859 # calling functions defined in the script that use other things from
857 860 # the script will fail, because the function's closure had references
858 861 # to the original objects, which are now all None. So we must protect
859 862 # these modules from deletion by keeping a cache.
860 863 #
861 864 # To avoid keeping stale modules around (we only need the one from the
862 865 # last run), we use a dict keyed with the full path to the script, so
863 866 # only the last version of the module is held in the cache. Note,
864 867 # however, that we must cache the module *namespace contents* (their
865 868 # __dict__). Because if we try to cache the actual modules, old ones
866 869 # (uncached) could be destroyed while still holding references (such as
867 870 # those held by GUI objects that tend to be long-lived)>
868 871 #
869 872 # The %reset command will flush this cache. See the cache_main_mod()
870 873 # and clear_main_mod_cache() methods for details on use.
871 874
872 875 # This is the cache used for 'main' namespaces
873 876 self._main_ns_cache = {}
874 877 # And this is the single instance of FakeModule whose __dict__ we keep
875 878 # copying and clearing for reuse on each %run
876 879 self._user_main_module = FakeModule()
877 880
878 881 # A table holding all the namespaces IPython deals with, so that
879 882 # introspection facilities can search easily.
880 883 self.ns_table = {'user':user_ns,
881 884 'user_global':user_global_ns,
882 885 'internal':self.internal_ns,
883 886 'builtin':__builtin__.__dict__
884 887 }
885 888
886 889 # Similarly, track all namespaces where references can be held and that
887 890 # we can safely clear (so it can NOT include builtin). This one can be
888 891 # a simple list. Note that the main execution namespaces, user_ns and
889 892 # user_global_ns, can NOT be listed here, as clearing them blindly
890 893 # causes errors in object __del__ methods. Instead, the reset() method
891 894 # clears them manually and carefully.
892 895 self.ns_refs_table = [ self.user_ns_hidden,
893 896 self.internal_ns, self._main_ns_cache ]
894 897
895 898 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
896 899 """Return a valid local and global user interactive namespaces.
897 900
898 901 This builds a dict with the minimal information needed to operate as a
899 902 valid IPython user namespace, which you can pass to the various
900 903 embedding classes in ipython. The default implementation returns the
901 904 same dict for both the locals and the globals to allow functions to
902 905 refer to variables in the namespace. Customized implementations can
903 906 return different dicts. The locals dictionary can actually be anything
904 907 following the basic mapping protocol of a dict, but the globals dict
905 908 must be a true dict, not even a subclass. It is recommended that any
906 909 custom object for the locals namespace synchronize with the globals
907 910 dict somehow.
908 911
909 912 Raises TypeError if the provided globals namespace is not a true dict.
910 913
911 914 Parameters
912 915 ----------
913 916 user_ns : dict-like, optional
914 917 The current user namespace. The items in this namespace should
915 918 be included in the output. If None, an appropriate blank
916 919 namespace should be created.
917 920 user_global_ns : dict, optional
918 921 The current user global namespace. The items in this namespace
919 922 should be included in the output. If None, an appropriate
920 923 blank namespace should be created.
921 924
922 925 Returns
923 926 -------
924 927 A pair of dictionary-like object to be used as the local namespace
925 928 of the interpreter and a dict to be used as the global namespace.
926 929 """
927 930
928 931
929 932 # We must ensure that __builtin__ (without the final 's') is always
930 933 # available and pointing to the __builtin__ *module*. For more details:
931 934 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
932 935
933 936 if user_ns is None:
934 937 # Set __name__ to __main__ to better match the behavior of the
935 938 # normal interpreter.
936 939 user_ns = {'__name__' :'__main__',
937 940 '__builtin__' : __builtin__,
938 941 '__builtins__' : __builtin__,
939 942 }
940 943 else:
941 944 user_ns.setdefault('__name__','__main__')
942 945 user_ns.setdefault('__builtin__',__builtin__)
943 946 user_ns.setdefault('__builtins__',__builtin__)
944 947
945 948 if user_global_ns is None:
946 949 user_global_ns = user_ns
947 950 if type(user_global_ns) is not dict:
948 951 raise TypeError("user_global_ns must be a true dict; got %r"
949 952 % type(user_global_ns))
950 953
951 954 return user_ns, user_global_ns
952 955
953 956 def init_sys_modules(self):
954 957 # We need to insert into sys.modules something that looks like a
955 958 # module but which accesses the IPython namespace, for shelve and
956 959 # pickle to work interactively. Normally they rely on getting
957 960 # everything out of __main__, but for embedding purposes each IPython
958 961 # instance has its own private namespace, so we can't go shoving
959 962 # everything into __main__.
960 963
961 964 # note, however, that we should only do this for non-embedded
962 965 # ipythons, which really mimic the __main__.__dict__ with their own
963 966 # namespace. Embedded instances, on the other hand, should not do
964 967 # this because they need to manage the user local/global namespaces
965 968 # only, but they live within a 'normal' __main__ (meaning, they
966 969 # shouldn't overtake the execution environment of the script they're
967 970 # embedded in).
968 971
969 972 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
970 973
971 974 try:
972 975 main_name = self.user_ns['__name__']
973 976 except KeyError:
974 977 raise KeyError('user_ns dictionary MUST have a "__name__" key')
975 978 else:
976 979 sys.modules[main_name] = FakeModule(self.user_ns)
977 980
978 981 def init_user_ns(self):
979 982 """Initialize all user-visible namespaces to their minimum defaults.
980 983
981 984 Certain history lists are also initialized here, as they effectively
982 985 act as user namespaces.
983 986
984 987 Notes
985 988 -----
986 989 All data structures here are only filled in, they are NOT reset by this
987 990 method. If they were not empty before, data will simply be added to
988 991 therm.
989 992 """
990 993 # This function works in two parts: first we put a few things in
991 994 # user_ns, and we sync that contents into user_ns_hidden so that these
992 995 # initial variables aren't shown by %who. After the sync, we add the
993 996 # rest of what we *do* want the user to see with %who even on a new
994 997 # session (probably nothing, so theye really only see their own stuff)
995 998
996 999 # The user dict must *always* have a __builtin__ reference to the
997 1000 # Python standard __builtin__ namespace, which must be imported.
998 1001 # This is so that certain operations in prompt evaluation can be
999 1002 # reliably executed with builtins. Note that we can NOT use
1000 1003 # __builtins__ (note the 's'), because that can either be a dict or a
1001 1004 # module, and can even mutate at runtime, depending on the context
1002 1005 # (Python makes no guarantees on it). In contrast, __builtin__ is
1003 1006 # always a module object, though it must be explicitly imported.
1004 1007
1005 1008 # For more details:
1006 1009 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1007 1010 ns = dict(__builtin__ = __builtin__)
1008 1011
1009 1012 # Put 'help' in the user namespace
1010 1013 try:
1011 1014 from site import _Helper
1012 1015 ns['help'] = _Helper()
1013 1016 except ImportError:
1014 1017 warn('help() not available - check site.py')
1015 1018
1016 1019 # make global variables for user access to the histories
1017 1020 ns['_ih'] = self.history_manager.input_hist_parsed
1018 1021 ns['_oh'] = self.history_manager.output_hist
1019 1022 ns['_dh'] = self.history_manager.dir_hist
1020 1023
1021 1024 ns['_sh'] = shadowns
1022 1025
1023 1026 # user aliases to input and output histories. These shouldn't show up
1024 1027 # in %who, as they can have very large reprs.
1025 1028 ns['In'] = self.history_manager.input_hist_parsed
1026 1029 ns['Out'] = self.history_manager.output_hist
1027 1030
1028 1031 # Store myself as the public api!!!
1029 1032 ns['get_ipython'] = self.get_ipython
1030 1033
1031 1034 ns['exit'] = self.exiter
1032 1035 ns['quit'] = self.exiter
1033 1036
1034 1037 # Sync what we've added so far to user_ns_hidden so these aren't seen
1035 1038 # by %who
1036 1039 self.user_ns_hidden.update(ns)
1037 1040
1038 1041 # Anything put into ns now would show up in %who. Think twice before
1039 1042 # putting anything here, as we really want %who to show the user their
1040 1043 # stuff, not our variables.
1041 1044
1042 1045 # Finally, update the real user's namespace
1043 1046 self.user_ns.update(ns)
1044 1047
1045 1048 def reset(self, new_session=True):
1046 1049 """Clear all internal namespaces.
1047 1050
1048 1051 Note that this is much more aggressive than %reset, since it clears
1049 1052 fully all namespaces, as well as all input/output lists.
1050 1053
1051 1054 If new_session is True, a new history session will be opened.
1052 1055 """
1053 1056 # Clear histories
1054 1057 self.history_manager.reset(new_session)
1055 1058 # Reset counter used to index all histories
1056 1059 if new_session:
1057 1060 self.execution_count = 1
1058 1061
1059 1062 # Flush cached output items
1060 1063 self.displayhook.flush()
1061 1064
1062 1065 # Restore the user namespaces to minimal usability
1063 1066 for ns in self.ns_refs_table:
1064 1067 ns.clear()
1065 1068
1066 1069 # The main execution namespaces must be cleared very carefully,
1067 1070 # skipping the deletion of the builtin-related keys, because doing so
1068 1071 # would cause errors in many object's __del__ methods.
1069 1072 for ns in [self.user_ns, self.user_global_ns]:
1070 1073 drop_keys = set(ns.keys())
1071 1074 drop_keys.discard('__builtin__')
1072 1075 drop_keys.discard('__builtins__')
1073 1076 for k in drop_keys:
1074 1077 del ns[k]
1075 1078
1076 1079 # Restore the user namespaces to minimal usability
1077 1080 self.init_user_ns()
1078 1081
1079 1082 # Restore the default and user aliases
1080 1083 self.alias_manager.clear_aliases()
1081 1084 self.alias_manager.init_aliases()
1082 1085
1083 1086 # Flush the private list of module references kept for script
1084 1087 # execution protection
1085 1088 self.clear_main_mod_cache()
1086 1089
1087 1090 def reset_selective(self, regex=None):
1088 1091 """Clear selective variables from internal namespaces based on a
1089 1092 specified regular expression.
1090 1093
1091 1094 Parameters
1092 1095 ----------
1093 1096 regex : string or compiled pattern, optional
1094 1097 A regular expression pattern that will be used in searching
1095 1098 variable names in the users namespaces.
1096 1099 """
1097 1100 if regex is not None:
1098 1101 try:
1099 1102 m = re.compile(regex)
1100 1103 except TypeError:
1101 1104 raise TypeError('regex must be a string or compiled pattern')
1102 1105 # Search for keys in each namespace that match the given regex
1103 1106 # If a match is found, delete the key/value pair.
1104 1107 for ns in self.ns_refs_table:
1105 1108 for var in ns:
1106 1109 if m.search(var):
1107 1110 del ns[var]
1108 1111
1109 1112 def push(self, variables, interactive=True):
1110 1113 """Inject a group of variables into the IPython user namespace.
1111 1114
1112 1115 Parameters
1113 1116 ----------
1114 1117 variables : dict, str or list/tuple of str
1115 1118 The variables to inject into the user's namespace. If a dict, a
1116 1119 simple update is done. If a str, the string is assumed to have
1117 1120 variable names separated by spaces. A list/tuple of str can also
1118 1121 be used to give the variable names. If just the variable names are
1119 1122 give (list/tuple/str) then the variable values looked up in the
1120 1123 callers frame.
1121 1124 interactive : bool
1122 1125 If True (default), the variables will be listed with the ``who``
1123 1126 magic.
1124 1127 """
1125 1128 vdict = None
1126 1129
1127 1130 # We need a dict of name/value pairs to do namespace updates.
1128 1131 if isinstance(variables, dict):
1129 1132 vdict = variables
1130 1133 elif isinstance(variables, (basestring, list, tuple)):
1131 1134 if isinstance(variables, basestring):
1132 1135 vlist = variables.split()
1133 1136 else:
1134 1137 vlist = variables
1135 1138 vdict = {}
1136 1139 cf = sys._getframe(1)
1137 1140 for name in vlist:
1138 1141 try:
1139 1142 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1140 1143 except:
1141 1144 print ('Could not get variable %s from %s' %
1142 1145 (name,cf.f_code.co_name))
1143 1146 else:
1144 1147 raise ValueError('variables must be a dict/str/list/tuple')
1145 1148
1146 1149 # Propagate variables to user namespace
1147 1150 self.user_ns.update(vdict)
1148 1151
1149 1152 # And configure interactive visibility
1150 1153 config_ns = self.user_ns_hidden
1151 1154 if interactive:
1152 1155 for name, val in vdict.iteritems():
1153 1156 config_ns.pop(name, None)
1154 1157 else:
1155 1158 for name,val in vdict.iteritems():
1156 1159 config_ns[name] = val
1157 1160
1158 1161 #-------------------------------------------------------------------------
1159 1162 # Things related to object introspection
1160 1163 #-------------------------------------------------------------------------
1161 1164
1162 1165 def _ofind(self, oname, namespaces=None):
1163 1166 """Find an object in the available namespaces.
1164 1167
1165 1168 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1166 1169
1167 1170 Has special code to detect magic functions.
1168 1171 """
1169 1172 #oname = oname.strip()
1170 1173 #print '1- oname: <%r>' % oname # dbg
1171 1174 try:
1172 1175 oname = oname.strip().encode('ascii')
1173 1176 #print '2- oname: <%r>' % oname # dbg
1174 1177 except UnicodeEncodeError:
1175 1178 print 'Python identifiers can only contain ascii characters.'
1176 1179 return dict(found=False)
1177 1180
1178 1181 alias_ns = None
1179 1182 if namespaces is None:
1180 1183 # Namespaces to search in:
1181 1184 # Put them in a list. The order is important so that we
1182 1185 # find things in the same order that Python finds them.
1183 1186 namespaces = [ ('Interactive', self.user_ns),
1184 1187 ('IPython internal', self.internal_ns),
1185 1188 ('Python builtin', __builtin__.__dict__),
1186 1189 ('Alias', self.alias_manager.alias_table),
1187 1190 ]
1188 1191 alias_ns = self.alias_manager.alias_table
1189 1192
1190 1193 # initialize results to 'null'
1191 1194 found = False; obj = None; ospace = None; ds = None;
1192 1195 ismagic = False; isalias = False; parent = None
1193 1196
1194 1197 # We need to special-case 'print', which as of python2.6 registers as a
1195 1198 # function but should only be treated as one if print_function was
1196 1199 # loaded with a future import. In this case, just bail.
1197 1200 if (oname == 'print' and not (self.compile.compiler_flags &
1198 1201 __future__.CO_FUTURE_PRINT_FUNCTION)):
1199 1202 return {'found':found, 'obj':obj, 'namespace':ospace,
1200 1203 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1201 1204
1202 1205 # Look for the given name by splitting it in parts. If the head is
1203 1206 # found, then we look for all the remaining parts as members, and only
1204 1207 # declare success if we can find them all.
1205 1208 oname_parts = oname.split('.')
1206 1209 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1207 1210 for nsname,ns in namespaces:
1208 1211 try:
1209 1212 obj = ns[oname_head]
1210 1213 except KeyError:
1211 1214 continue
1212 1215 else:
1213 1216 #print 'oname_rest:', oname_rest # dbg
1214 1217 for part in oname_rest:
1215 1218 try:
1216 1219 parent = obj
1217 1220 obj = getattr(obj,part)
1218 1221 except:
1219 1222 # Blanket except b/c some badly implemented objects
1220 1223 # allow __getattr__ to raise exceptions other than
1221 1224 # AttributeError, which then crashes IPython.
1222 1225 break
1223 1226 else:
1224 1227 # If we finish the for loop (no break), we got all members
1225 1228 found = True
1226 1229 ospace = nsname
1227 1230 if ns == alias_ns:
1228 1231 isalias = True
1229 1232 break # namespace loop
1230 1233
1231 1234 # Try to see if it's magic
1232 1235 if not found:
1233 1236 if oname.startswith(ESC_MAGIC):
1234 1237 oname = oname[1:]
1235 1238 obj = getattr(self,'magic_'+oname,None)
1236 1239 if obj is not None:
1237 1240 found = True
1238 1241 ospace = 'IPython internal'
1239 1242 ismagic = True
1240 1243
1241 1244 # Last try: special-case some literals like '', [], {}, etc:
1242 1245 if not found and oname_head in ["''",'""','[]','{}','()']:
1243 1246 obj = eval(oname_head)
1244 1247 found = True
1245 1248 ospace = 'Interactive'
1246 1249
1247 1250 return {'found':found, 'obj':obj, 'namespace':ospace,
1248 1251 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1249 1252
1250 1253 def _ofind_property(self, oname, info):
1251 1254 """Second part of object finding, to look for property details."""
1252 1255 if info.found:
1253 1256 # Get the docstring of the class property if it exists.
1254 1257 path = oname.split('.')
1255 1258 root = '.'.join(path[:-1])
1256 1259 if info.parent is not None:
1257 1260 try:
1258 1261 target = getattr(info.parent, '__class__')
1259 1262 # The object belongs to a class instance.
1260 1263 try:
1261 1264 target = getattr(target, path[-1])
1262 1265 # The class defines the object.
1263 1266 if isinstance(target, property):
1264 1267 oname = root + '.__class__.' + path[-1]
1265 1268 info = Struct(self._ofind(oname))
1266 1269 except AttributeError: pass
1267 1270 except AttributeError: pass
1268 1271
1269 1272 # We return either the new info or the unmodified input if the object
1270 1273 # hadn't been found
1271 1274 return info
1272 1275
1273 1276 def _object_find(self, oname, namespaces=None):
1274 1277 """Find an object and return a struct with info about it."""
1275 1278 inf = Struct(self._ofind(oname, namespaces))
1276 1279 return Struct(self._ofind_property(oname, inf))
1277 1280
1278 1281 def _inspect(self, meth, oname, namespaces=None, **kw):
1279 1282 """Generic interface to the inspector system.
1280 1283
1281 1284 This function is meant to be called by pdef, pdoc & friends."""
1282 1285 info = self._object_find(oname)
1283 1286 if info.found:
1284 1287 pmethod = getattr(self.inspector, meth)
1285 1288 formatter = format_screen if info.ismagic else None
1286 1289 if meth == 'pdoc':
1287 1290 pmethod(info.obj, oname, formatter)
1288 1291 elif meth == 'pinfo':
1289 1292 pmethod(info.obj, oname, formatter, info, **kw)
1290 1293 else:
1291 1294 pmethod(info.obj, oname)
1292 1295 else:
1293 1296 print 'Object `%s` not found.' % oname
1294 1297 return 'not found' # so callers can take other action
1295 1298
1296 1299 def object_inspect(self, oname):
1297 1300 with self.builtin_trap:
1298 1301 info = self._object_find(oname)
1299 1302 if info.found:
1300 1303 return self.inspector.info(info.obj, oname, info=info)
1301 1304 else:
1302 1305 return oinspect.object_info(name=oname, found=False)
1303 1306
1304 1307 #-------------------------------------------------------------------------
1305 1308 # Things related to history management
1306 1309 #-------------------------------------------------------------------------
1307 1310
1308 1311 def init_history(self):
1309 1312 """Sets up the command history, and starts regular autosaves."""
1310 1313 self.history_manager = HistoryManager(shell=self, config=self.config)
1311 1314
1312 1315 #-------------------------------------------------------------------------
1313 1316 # Things related to exception handling and tracebacks (not debugging)
1314 1317 #-------------------------------------------------------------------------
1315 1318
1316 1319 def init_traceback_handlers(self, custom_exceptions):
1317 1320 # Syntax error handler.
1318 1321 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1319 1322
1320 1323 # The interactive one is initialized with an offset, meaning we always
1321 1324 # want to remove the topmost item in the traceback, which is our own
1322 1325 # internal code. Valid modes: ['Plain','Context','Verbose']
1323 1326 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1324 1327 color_scheme='NoColor',
1325 1328 tb_offset = 1,
1326 1329 check_cache=self.compile.check_cache)
1327 1330
1328 1331 # The instance will store a pointer to the system-wide exception hook,
1329 1332 # so that runtime code (such as magics) can access it. This is because
1330 1333 # during the read-eval loop, it may get temporarily overwritten.
1331 1334 self.sys_excepthook = sys.excepthook
1332 1335
1333 1336 # and add any custom exception handlers the user may have specified
1334 1337 self.set_custom_exc(*custom_exceptions)
1335 1338
1336 1339 # Set the exception mode
1337 1340 self.InteractiveTB.set_mode(mode=self.xmode)
1338 1341
1339 1342 def set_custom_exc(self, exc_tuple, handler):
1340 1343 """set_custom_exc(exc_tuple,handler)
1341 1344
1342 1345 Set a custom exception handler, which will be called if any of the
1343 1346 exceptions in exc_tuple occur in the mainloop (specifically, in the
1344 1347 run_code() method.
1345 1348
1346 1349 Inputs:
1347 1350
1348 1351 - exc_tuple: a *tuple* of valid exceptions to call the defined
1349 1352 handler for. It is very important that you use a tuple, and NOT A
1350 1353 LIST here, because of the way Python's except statement works. If
1351 1354 you only want to trap a single exception, use a singleton tuple:
1352 1355
1353 1356 exc_tuple == (MyCustomException,)
1354 1357
1355 1358 - handler: this must be defined as a function with the following
1356 1359 basic interface::
1357 1360
1358 1361 def my_handler(self, etype, value, tb, tb_offset=None)
1359 1362 ...
1360 1363 # The return value must be
1361 1364 return structured_traceback
1362 1365
1363 1366 This will be made into an instance method (via types.MethodType)
1364 1367 of IPython itself, and it will be called if any of the exceptions
1365 1368 listed in the exc_tuple are caught. If the handler is None, an
1366 1369 internal basic one is used, which just prints basic info.
1367 1370
1368 1371 WARNING: by putting in your own exception handler into IPython's main
1369 1372 execution loop, you run a very good chance of nasty crashes. This
1370 1373 facility should only be used if you really know what you are doing."""
1371 1374
1372 1375 assert type(exc_tuple)==type(()) , \
1373 1376 "The custom exceptions must be given AS A TUPLE."
1374 1377
1375 1378 def dummy_handler(self,etype,value,tb):
1376 1379 print '*** Simple custom exception handler ***'
1377 1380 print 'Exception type :',etype
1378 1381 print 'Exception value:',value
1379 1382 print 'Traceback :',tb
1380 1383 print 'Source code :','\n'.join(self.buffer)
1381 1384
1382 1385 if handler is None: handler = dummy_handler
1383 1386
1384 1387 self.CustomTB = types.MethodType(handler,self)
1385 1388 self.custom_exceptions = exc_tuple
1386 1389
1387 1390 def excepthook(self, etype, value, tb):
1388 1391 """One more defense for GUI apps that call sys.excepthook.
1389 1392
1390 1393 GUI frameworks like wxPython trap exceptions and call
1391 1394 sys.excepthook themselves. I guess this is a feature that
1392 1395 enables them to keep running after exceptions that would
1393 1396 otherwise kill their mainloop. This is a bother for IPython
1394 1397 which excepts to catch all of the program exceptions with a try:
1395 1398 except: statement.
1396 1399
1397 1400 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1398 1401 any app directly invokes sys.excepthook, it will look to the user like
1399 1402 IPython crashed. In order to work around this, we can disable the
1400 1403 CrashHandler and replace it with this excepthook instead, which prints a
1401 1404 regular traceback using our InteractiveTB. In this fashion, apps which
1402 1405 call sys.excepthook will generate a regular-looking exception from
1403 1406 IPython, and the CrashHandler will only be triggered by real IPython
1404 1407 crashes.
1405 1408
1406 1409 This hook should be used sparingly, only in places which are not likely
1407 1410 to be true IPython errors.
1408 1411 """
1409 1412 self.showtraceback((etype,value,tb),tb_offset=0)
1410 1413
1411 1414 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1412 1415 exception_only=False):
1413 1416 """Display the exception that just occurred.
1414 1417
1415 1418 If nothing is known about the exception, this is the method which
1416 1419 should be used throughout the code for presenting user tracebacks,
1417 1420 rather than directly invoking the InteractiveTB object.
1418 1421
1419 1422 A specific showsyntaxerror() also exists, but this method can take
1420 1423 care of calling it if needed, so unless you are explicitly catching a
1421 1424 SyntaxError exception, don't try to analyze the stack manually and
1422 1425 simply call this method."""
1423 1426
1424 1427 try:
1425 1428 if exc_tuple is None:
1426 1429 etype, value, tb = sys.exc_info()
1427 1430 else:
1428 1431 etype, value, tb = exc_tuple
1429 1432
1430 1433 if etype is None:
1431 1434 if hasattr(sys, 'last_type'):
1432 1435 etype, value, tb = sys.last_type, sys.last_value, \
1433 1436 sys.last_traceback
1434 1437 else:
1435 1438 self.write_err('No traceback available to show.\n')
1436 1439 return
1437 1440
1438 1441 if etype is SyntaxError:
1439 1442 # Though this won't be called by syntax errors in the input
1440 1443 # line, there may be SyntaxError cases whith imported code.
1441 1444 self.showsyntaxerror(filename)
1442 1445 elif etype is UsageError:
1443 1446 print "UsageError:", value
1444 1447 else:
1445 1448 # WARNING: these variables are somewhat deprecated and not
1446 1449 # necessarily safe to use in a threaded environment, but tools
1447 1450 # like pdb depend on their existence, so let's set them. If we
1448 1451 # find problems in the field, we'll need to revisit their use.
1449 1452 sys.last_type = etype
1450 1453 sys.last_value = value
1451 1454 sys.last_traceback = tb
1452 1455 if etype in self.custom_exceptions:
1453 1456 # FIXME: Old custom traceback objects may just return a
1454 1457 # string, in that case we just put it into a list
1455 1458 stb = self.CustomTB(etype, value, tb, tb_offset)
1456 1459 if isinstance(ctb, basestring):
1457 1460 stb = [stb]
1458 1461 else:
1459 1462 if exception_only:
1460 1463 stb = ['An exception has occurred, use %tb to see '
1461 1464 'the full traceback.\n']
1462 1465 stb.extend(self.InteractiveTB.get_exception_only(etype,
1463 1466 value))
1464 1467 else:
1465 1468 stb = self.InteractiveTB.structured_traceback(etype,
1466 1469 value, tb, tb_offset=tb_offset)
1467 1470
1468 1471 if self.call_pdb:
1469 1472 # drop into debugger
1470 1473 self.debugger(force=True)
1471 1474
1472 1475 # Actually show the traceback
1473 1476 self._showtraceback(etype, value, stb)
1474 1477
1475 1478 except KeyboardInterrupt:
1476 1479 self.write_err("\nKeyboardInterrupt\n")
1477 1480
1478 1481 def _showtraceback(self, etype, evalue, stb):
1479 1482 """Actually show a traceback.
1480 1483
1481 1484 Subclasses may override this method to put the traceback on a different
1482 1485 place, like a side channel.
1483 1486 """
1484 1487 print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
1485 1488
1486 1489 def showsyntaxerror(self, filename=None):
1487 1490 """Display the syntax error that just occurred.
1488 1491
1489 1492 This doesn't display a stack trace because there isn't one.
1490 1493
1491 1494 If a filename is given, it is stuffed in the exception instead
1492 1495 of what was there before (because Python's parser always uses
1493 1496 "<string>" when reading from a string).
1494 1497 """
1495 1498 etype, value, last_traceback = sys.exc_info()
1496 1499
1497 1500 # See note about these variables in showtraceback() above
1498 1501 sys.last_type = etype
1499 1502 sys.last_value = value
1500 1503 sys.last_traceback = last_traceback
1501 1504
1502 1505 if filename and etype is SyntaxError:
1503 1506 # Work hard to stuff the correct filename in the exception
1504 1507 try:
1505 1508 msg, (dummy_filename, lineno, offset, line) = value
1506 1509 except:
1507 1510 # Not the format we expect; leave it alone
1508 1511 pass
1509 1512 else:
1510 1513 # Stuff in the right filename
1511 1514 try:
1512 1515 # Assume SyntaxError is a class exception
1513 1516 value = SyntaxError(msg, (filename, lineno, offset, line))
1514 1517 except:
1515 1518 # If that failed, assume SyntaxError is a string
1516 1519 value = msg, (filename, lineno, offset, line)
1517 1520 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1518 1521 self._showtraceback(etype, value, stb)
1519 1522
1520 1523 #-------------------------------------------------------------------------
1521 1524 # Things related to readline
1522 1525 #-------------------------------------------------------------------------
1523 1526
1524 1527 def init_readline(self):
1525 1528 """Command history completion/saving/reloading."""
1526 1529
1527 1530 if self.readline_use:
1528 1531 import IPython.utils.rlineimpl as readline
1529 1532
1530 1533 self.rl_next_input = None
1531 1534 self.rl_do_indent = False
1532 1535
1533 1536 if not self.readline_use or not readline.have_readline:
1534 1537 self.has_readline = False
1535 1538 self.readline = None
1536 1539 # Set a number of methods that depend on readline to be no-op
1537 1540 self.set_readline_completer = no_op
1538 1541 self.set_custom_completer = no_op
1539 1542 self.set_completer_frame = no_op
1540 1543 warn('Readline services not available or not loaded.')
1541 1544 else:
1542 1545 self.has_readline = True
1543 1546 self.readline = readline
1544 1547 sys.modules['readline'] = readline
1545 1548
1546 1549 # Platform-specific configuration
1547 1550 if os.name == 'nt':
1548 1551 # FIXME - check with Frederick to see if we can harmonize
1549 1552 # naming conventions with pyreadline to avoid this
1550 1553 # platform-dependent check
1551 1554 self.readline_startup_hook = readline.set_pre_input_hook
1552 1555 else:
1553 1556 self.readline_startup_hook = readline.set_startup_hook
1554 1557
1555 1558 # Load user's initrc file (readline config)
1556 1559 # Or if libedit is used, load editrc.
1557 1560 inputrc_name = os.environ.get('INPUTRC')
1558 1561 if inputrc_name is None:
1559 1562 home_dir = get_home_dir()
1560 1563 if home_dir is not None:
1561 1564 inputrc_name = '.inputrc'
1562 1565 if readline.uses_libedit:
1563 1566 inputrc_name = '.editrc'
1564 1567 inputrc_name = os.path.join(home_dir, inputrc_name)
1565 1568 if os.path.isfile(inputrc_name):
1566 1569 try:
1567 1570 readline.read_init_file(inputrc_name)
1568 1571 except:
1569 1572 warn('Problems reading readline initialization file <%s>'
1570 1573 % inputrc_name)
1571 1574
1572 1575 # Configure readline according to user's prefs
1573 1576 # This is only done if GNU readline is being used. If libedit
1574 1577 # is being used (as on Leopard) the readline config is
1575 1578 # not run as the syntax for libedit is different.
1576 1579 if not readline.uses_libedit:
1577 1580 for rlcommand in self.readline_parse_and_bind:
1578 1581 #print "loading rl:",rlcommand # dbg
1579 1582 readline.parse_and_bind(rlcommand)
1580 1583
1581 1584 # Remove some chars from the delimiters list. If we encounter
1582 1585 # unicode chars, discard them.
1583 1586 delims = readline.get_completer_delims().encode("ascii", "ignore")
1584 1587 delims = delims.translate(None, self.readline_remove_delims)
1585 1588 delims = delims.replace(ESC_MAGIC, '')
1586 1589 readline.set_completer_delims(delims)
1587 1590 # otherwise we end up with a monster history after a while:
1588 1591 readline.set_history_length(self.history_length)
1589 1592
1590 1593 self.refill_readline_hist()
1591 1594 self.readline_no_record = ReadlineNoRecord(self)
1592 1595
1593 1596 # Configure auto-indent for all platforms
1594 1597 self.set_autoindent(self.autoindent)
1595 1598
1596 1599 def refill_readline_hist(self):
1597 1600 # Load the last 1000 lines from history
1598 1601 self.readline.clear_history()
1599 1602 stdin_encoding = sys.stdin.encoding or "utf-8"
1600 1603 for _, _, cell in self.history_manager.get_tail(1000,
1601 1604 include_latest=True):
1602 1605 if cell.strip(): # Ignore blank lines
1603 1606 for line in cell.splitlines():
1604 1607 self.readline.add_history(line.encode(stdin_encoding))
1605 1608
1606 1609 def set_next_input(self, s):
1607 1610 """ Sets the 'default' input string for the next command line.
1608 1611
1609 1612 Requires readline.
1610 1613
1611 1614 Example:
1612 1615
1613 1616 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1614 1617 [D:\ipython]|2> Hello Word_ # cursor is here
1615 1618 """
1616 1619
1617 1620 self.rl_next_input = s
1618 1621
1619 1622 # Maybe move this to the terminal subclass?
1620 1623 def pre_readline(self):
1621 1624 """readline hook to be used at the start of each line.
1622 1625
1623 1626 Currently it handles auto-indent only."""
1624 1627
1625 1628 if self.rl_do_indent:
1626 1629 self.readline.insert_text(self._indent_current_str())
1627 1630 if self.rl_next_input is not None:
1628 1631 self.readline.insert_text(self.rl_next_input)
1629 1632 self.rl_next_input = None
1630 1633
1631 1634 def _indent_current_str(self):
1632 1635 """return the current level of indentation as a string"""
1633 1636 return self.input_splitter.indent_spaces * ' '
1634 1637
1635 1638 #-------------------------------------------------------------------------
1636 1639 # Things related to text completion
1637 1640 #-------------------------------------------------------------------------
1638 1641
1639 1642 def init_completer(self):
1640 1643 """Initialize the completion machinery.
1641 1644
1642 1645 This creates completion machinery that can be used by client code,
1643 1646 either interactively in-process (typically triggered by the readline
1644 1647 library), programatically (such as in test suites) or out-of-prcess
1645 1648 (typically over the network by remote frontends).
1646 1649 """
1647 1650 from IPython.core.completer import IPCompleter
1648 1651 from IPython.core.completerlib import (module_completer,
1649 1652 magic_run_completer, cd_completer)
1650 1653
1651 1654 self.Completer = IPCompleter(self,
1652 1655 self.user_ns,
1653 1656 self.user_global_ns,
1654 1657 self.readline_omit__names,
1655 1658 self.alias_manager.alias_table,
1656 1659 self.has_readline)
1657 1660
1658 1661 # Add custom completers to the basic ones built into IPCompleter
1659 1662 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1660 1663 self.strdispatchers['complete_command'] = sdisp
1661 1664 self.Completer.custom_completers = sdisp
1662 1665
1663 1666 self.set_hook('complete_command', module_completer, str_key = 'import')
1664 1667 self.set_hook('complete_command', module_completer, str_key = 'from')
1665 1668 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1666 1669 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1667 1670
1668 1671 # Only configure readline if we truly are using readline. IPython can
1669 1672 # do tab-completion over the network, in GUIs, etc, where readline
1670 1673 # itself may be absent
1671 1674 if self.has_readline:
1672 1675 self.set_readline_completer()
1673 1676
1674 1677 def complete(self, text, line=None, cursor_pos=None):
1675 1678 """Return the completed text and a list of completions.
1676 1679
1677 1680 Parameters
1678 1681 ----------
1679 1682
1680 1683 text : string
1681 1684 A string of text to be completed on. It can be given as empty and
1682 1685 instead a line/position pair are given. In this case, the
1683 1686 completer itself will split the line like readline does.
1684 1687
1685 1688 line : string, optional
1686 1689 The complete line that text is part of.
1687 1690
1688 1691 cursor_pos : int, optional
1689 1692 The position of the cursor on the input line.
1690 1693
1691 1694 Returns
1692 1695 -------
1693 1696 text : string
1694 1697 The actual text that was completed.
1695 1698
1696 1699 matches : list
1697 1700 A sorted list with all possible completions.
1698 1701
1699 1702 The optional arguments allow the completion to take more context into
1700 1703 account, and are part of the low-level completion API.
1701 1704
1702 1705 This is a wrapper around the completion mechanism, similar to what
1703 1706 readline does at the command line when the TAB key is hit. By
1704 1707 exposing it as a method, it can be used by other non-readline
1705 1708 environments (such as GUIs) for text completion.
1706 1709
1707 1710 Simple usage example:
1708 1711
1709 1712 In [1]: x = 'hello'
1710 1713
1711 1714 In [2]: _ip.complete('x.l')
1712 1715 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1713 1716 """
1714 1717
1715 1718 # Inject names into __builtin__ so we can complete on the added names.
1716 1719 with self.builtin_trap:
1717 1720 return self.Completer.complete(text, line, cursor_pos)
1718 1721
1719 1722 def set_custom_completer(self, completer, pos=0):
1720 1723 """Adds a new custom completer function.
1721 1724
1722 1725 The position argument (defaults to 0) is the index in the completers
1723 1726 list where you want the completer to be inserted."""
1724 1727
1725 1728 newcomp = types.MethodType(completer,self.Completer)
1726 1729 self.Completer.matchers.insert(pos,newcomp)
1727 1730
1728 1731 def set_readline_completer(self):
1729 1732 """Reset readline's completer to be our own."""
1730 1733 self.readline.set_completer(self.Completer.rlcomplete)
1731 1734
1732 1735 def set_completer_frame(self, frame=None):
1733 1736 """Set the frame of the completer."""
1734 1737 if frame:
1735 1738 self.Completer.namespace = frame.f_locals
1736 1739 self.Completer.global_namespace = frame.f_globals
1737 1740 else:
1738 1741 self.Completer.namespace = self.user_ns
1739 1742 self.Completer.global_namespace = self.user_global_ns
1740 1743
1741 1744 #-------------------------------------------------------------------------
1742 1745 # Things related to magics
1743 1746 #-------------------------------------------------------------------------
1744 1747
1745 1748 def init_magics(self):
1746 1749 # FIXME: Move the color initialization to the DisplayHook, which
1747 1750 # should be split into a prompt manager and displayhook. We probably
1748 1751 # even need a centralize colors management object.
1749 1752 self.magic_colors(self.colors)
1750 1753 # History was moved to a separate module
1751 1754 from . import history
1752 1755 history.init_ipython(self)
1753 1756
1754 1757 def magic(self,arg_s):
1755 1758 """Call a magic function by name.
1756 1759
1757 1760 Input: a string containing the name of the magic function to call and
1758 1761 any additional arguments to be passed to the magic.
1759 1762
1760 1763 magic('name -opt foo bar') is equivalent to typing at the ipython
1761 1764 prompt:
1762 1765
1763 1766 In[1]: %name -opt foo bar
1764 1767
1765 1768 To call a magic without arguments, simply use magic('name').
1766 1769
1767 1770 This provides a proper Python function to call IPython's magics in any
1768 1771 valid Python code you can type at the interpreter, including loops and
1769 1772 compound statements.
1770 1773 """
1771 1774 args = arg_s.split(' ',1)
1772 1775 magic_name = args[0]
1773 1776 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1774 1777
1775 1778 try:
1776 1779 magic_args = args[1]
1777 1780 except IndexError:
1778 1781 magic_args = ''
1779 1782 fn = getattr(self,'magic_'+magic_name,None)
1780 1783 if fn is None:
1781 1784 error("Magic function `%s` not found." % magic_name)
1782 1785 else:
1783 1786 magic_args = self.var_expand(magic_args,1)
1784 1787 # Grab local namespace if we need it:
1785 1788 if getattr(fn, "needs_local_scope", False):
1786 1789 self._magic_locals = sys._getframe(1).f_locals
1787 with nested(self.builtin_trap,):
1790 with self.builtin_trap:
1788 1791 result = fn(magic_args)
1789 1792 # Ensure we're not keeping object references around:
1790 1793 self._magic_locals = {}
1791 1794 return result
1792 1795
1793 1796 def define_magic(self, magicname, func):
1794 1797 """Expose own function as magic function for ipython
1795 1798
1796 1799 def foo_impl(self,parameter_s=''):
1797 1800 'My very own magic!. (Use docstrings, IPython reads them).'
1798 1801 print 'Magic function. Passed parameter is between < >:'
1799 1802 print '<%s>' % parameter_s
1800 1803 print 'The self object is:',self
1801 1804
1802 1805 self.define_magic('foo',foo_impl)
1803 1806 """
1804 1807
1805 1808 import new
1806 1809 im = types.MethodType(func,self)
1807 1810 old = getattr(self, "magic_" + magicname, None)
1808 1811 setattr(self, "magic_" + magicname, im)
1809 1812 return old
1810 1813
1811 1814 #-------------------------------------------------------------------------
1812 1815 # Things related to macros
1813 1816 #-------------------------------------------------------------------------
1814 1817
1815 1818 def define_macro(self, name, themacro):
1816 1819 """Define a new macro
1817 1820
1818 1821 Parameters
1819 1822 ----------
1820 1823 name : str
1821 1824 The name of the macro.
1822 1825 themacro : str or Macro
1823 1826 The action to do upon invoking the macro. If a string, a new
1824 1827 Macro object is created by passing the string to it.
1825 1828 """
1826 1829
1827 1830 from IPython.core import macro
1828 1831
1829 1832 if isinstance(themacro, basestring):
1830 1833 themacro = macro.Macro(themacro)
1831 1834 if not isinstance(themacro, macro.Macro):
1832 1835 raise ValueError('A macro must be a string or a Macro instance.')
1833 1836 self.user_ns[name] = themacro
1834 1837
1835 1838 #-------------------------------------------------------------------------
1836 1839 # Things related to the running of system commands
1837 1840 #-------------------------------------------------------------------------
1838 1841
1839 1842 def system(self, cmd):
1840 1843 """Call the given cmd in a subprocess.
1841 1844
1842 1845 Parameters
1843 1846 ----------
1844 1847 cmd : str
1845 1848 Command to execute (can not end in '&', as bacground processes are
1846 1849 not supported.
1847 1850 """
1848 1851 # We do not support backgrounding processes because we either use
1849 1852 # pexpect or pipes to read from. Users can always just call
1850 1853 # os.system() if they really want a background process.
1851 1854 if cmd.endswith('&'):
1852 1855 raise OSError("Background processes not supported.")
1853 1856
1854 1857 return system(self.var_expand(cmd, depth=2))
1855 1858
1856 1859 def getoutput(self, cmd, split=True):
1857 1860 """Get output (possibly including stderr) from a subprocess.
1858 1861
1859 1862 Parameters
1860 1863 ----------
1861 1864 cmd : str
1862 1865 Command to execute (can not end in '&', as background processes are
1863 1866 not supported.
1864 1867 split : bool, optional
1865 1868
1866 1869 If True, split the output into an IPython SList. Otherwise, an
1867 1870 IPython LSString is returned. These are objects similar to normal
1868 1871 lists and strings, with a few convenience attributes for easier
1869 1872 manipulation of line-based output. You can use '?' on them for
1870 1873 details.
1871 1874 """
1872 1875 if cmd.endswith('&'):
1873 1876 raise OSError("Background processes not supported.")
1874 1877 out = getoutput(self.var_expand(cmd, depth=2))
1875 1878 if split:
1876 1879 out = SList(out.splitlines())
1877 1880 else:
1878 1881 out = LSString(out)
1879 1882 return out
1880 1883
1881 1884 #-------------------------------------------------------------------------
1882 1885 # Things related to aliases
1883 1886 #-------------------------------------------------------------------------
1884 1887
1885 1888 def init_alias(self):
1886 1889 self.alias_manager = AliasManager(shell=self, config=self.config)
1887 1890 self.ns_table['alias'] = self.alias_manager.alias_table,
1888 1891
1889 1892 #-------------------------------------------------------------------------
1890 1893 # Things related to extensions and plugins
1891 1894 #-------------------------------------------------------------------------
1892 1895
1893 1896 def init_extension_manager(self):
1894 1897 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1895 1898
1896 1899 def init_plugin_manager(self):
1897 1900 self.plugin_manager = PluginManager(config=self.config)
1898 1901
1899 1902 #-------------------------------------------------------------------------
1900 1903 # Things related to payloads
1901 1904 #-------------------------------------------------------------------------
1902 1905
1903 1906 def init_payload(self):
1904 1907 self.payload_manager = PayloadManager(config=self.config)
1905 1908
1906 1909 #-------------------------------------------------------------------------
1907 1910 # Things related to the prefilter
1908 1911 #-------------------------------------------------------------------------
1909 1912
1910 1913 def init_prefilter(self):
1911 1914 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1912 1915 # Ultimately this will be refactored in the new interpreter code, but
1913 1916 # for now, we should expose the main prefilter method (there's legacy
1914 1917 # code out there that may rely on this).
1915 1918 self.prefilter = self.prefilter_manager.prefilter_lines
1916 1919
1917 1920 def auto_rewrite_input(self, cmd):
1918 1921 """Print to the screen the rewritten form of the user's command.
1919 1922
1920 1923 This shows visual feedback by rewriting input lines that cause
1921 1924 automatic calling to kick in, like::
1922 1925
1923 1926 /f x
1924 1927
1925 1928 into::
1926 1929
1927 1930 ------> f(x)
1928 1931
1929 1932 after the user's input prompt. This helps the user understand that the
1930 1933 input line was transformed automatically by IPython.
1931 1934 """
1932 1935 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1933 1936
1934 1937 try:
1935 1938 # plain ascii works better w/ pyreadline, on some machines, so
1936 1939 # we use it and only print uncolored rewrite if we have unicode
1937 1940 rw = str(rw)
1938 1941 print >> IPython.utils.io.Term.cout, rw
1939 1942 except UnicodeEncodeError:
1940 1943 print "------> " + cmd
1941 1944
1942 1945 #-------------------------------------------------------------------------
1943 1946 # Things related to extracting values/expressions from kernel and user_ns
1944 1947 #-------------------------------------------------------------------------
1945 1948
1946 1949 def _simple_error(self):
1947 1950 etype, value = sys.exc_info()[:2]
1948 1951 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1949 1952
1950 1953 def user_variables(self, names):
1951 1954 """Get a list of variable names from the user's namespace.
1952 1955
1953 1956 Parameters
1954 1957 ----------
1955 1958 names : list of strings
1956 1959 A list of names of variables to be read from the user namespace.
1957 1960
1958 1961 Returns
1959 1962 -------
1960 1963 A dict, keyed by the input names and with the repr() of each value.
1961 1964 """
1962 1965 out = {}
1963 1966 user_ns = self.user_ns
1964 1967 for varname in names:
1965 1968 try:
1966 1969 value = repr(user_ns[varname])
1967 1970 except:
1968 1971 value = self._simple_error()
1969 1972 out[varname] = value
1970 1973 return out
1971 1974
1972 1975 def user_expressions(self, expressions):
1973 1976 """Evaluate a dict of expressions in the user's namespace.
1974 1977
1975 1978 Parameters
1976 1979 ----------
1977 1980 expressions : dict
1978 1981 A dict with string keys and string values. The expression values
1979 1982 should be valid Python expressions, each of which will be evaluated
1980 1983 in the user namespace.
1981 1984
1982 1985 Returns
1983 1986 -------
1984 1987 A dict, keyed like the input expressions dict, with the repr() of each
1985 1988 value.
1986 1989 """
1987 1990 out = {}
1988 1991 user_ns = self.user_ns
1989 1992 global_ns = self.user_global_ns
1990 1993 for key, expr in expressions.iteritems():
1991 1994 try:
1992 1995 value = repr(eval(expr, global_ns, user_ns))
1993 1996 except:
1994 1997 value = self._simple_error()
1995 1998 out[key] = value
1996 1999 return out
1997 2000
1998 2001 #-------------------------------------------------------------------------
1999 2002 # Things related to the running of code
2000 2003 #-------------------------------------------------------------------------
2001 2004
2002 2005 def ex(self, cmd):
2003 2006 """Execute a normal python statement in user namespace."""
2004 with nested(self.builtin_trap,):
2007 with self.builtin_trap:
2005 2008 exec cmd in self.user_global_ns, self.user_ns
2006 2009
2007 2010 def ev(self, expr):
2008 2011 """Evaluate python expression expr in user namespace.
2009 2012
2010 2013 Returns the result of evaluation
2011 2014 """
2012 with nested(self.builtin_trap,):
2015 with self.builtin_trap:
2013 2016 return eval(expr, self.user_global_ns, self.user_ns)
2014 2017
2015 2018 def safe_execfile(self, fname, *where, **kw):
2016 2019 """A safe version of the builtin execfile().
2017 2020
2018 2021 This version will never throw an exception, but instead print
2019 2022 helpful error messages to the screen. This only works on pure
2020 2023 Python files with the .py extension.
2021 2024
2022 2025 Parameters
2023 2026 ----------
2024 2027 fname : string
2025 2028 The name of the file to be executed.
2026 2029 where : tuple
2027 2030 One or two namespaces, passed to execfile() as (globals,locals).
2028 2031 If only one is given, it is passed as both.
2029 2032 exit_ignore : bool (False)
2030 2033 If True, then silence SystemExit for non-zero status (it is always
2031 2034 silenced for zero status, as it is so common).
2032 2035 """
2033 2036 kw.setdefault('exit_ignore', False)
2034 2037
2035 2038 fname = os.path.abspath(os.path.expanduser(fname))
2036 2039 # Make sure we have a .py file
2037 2040 if not fname.endswith('.py'):
2038 2041 warn('File must end with .py to be run using execfile: <%s>' % fname)
2039 2042
2040 2043 # Make sure we can open the file
2041 2044 try:
2042 2045 with open(fname) as thefile:
2043 2046 pass
2044 2047 except:
2045 2048 warn('Could not open file <%s> for safe execution.' % fname)
2046 2049 return
2047 2050
2048 2051 # Find things also in current directory. This is needed to mimic the
2049 2052 # behavior of running a script from the system command line, where
2050 2053 # Python inserts the script's directory into sys.path
2051 2054 dname = os.path.dirname(fname)
2052 2055
2053 2056 if isinstance(fname, unicode):
2054 2057 # execfile uses default encoding instead of filesystem encoding
2055 2058 # so unicode filenames will fail
2056 2059 fname = fname.encode(sys.getfilesystemencoding() or sys.getdefaultencoding())
2057 2060
2058 2061 with prepended_to_syspath(dname):
2059 2062 try:
2060 2063 execfile(fname,*where)
2061 2064 except SystemExit, status:
2062 2065 # If the call was made with 0 or None exit status (sys.exit(0)
2063 2066 # or sys.exit() ), don't bother showing a traceback, as both of
2064 2067 # these are considered normal by the OS:
2065 2068 # > python -c'import sys;sys.exit(0)'; echo $?
2066 2069 # 0
2067 2070 # > python -c'import sys;sys.exit()'; echo $?
2068 2071 # 0
2069 2072 # For other exit status, we show the exception unless
2070 2073 # explicitly silenced, but only in short form.
2071 2074 if status.code not in (0, None) and not kw['exit_ignore']:
2072 2075 self.showtraceback(exception_only=True)
2073 2076 except:
2074 2077 self.showtraceback()
2075 2078
2076 2079 def safe_execfile_ipy(self, fname):
2077 2080 """Like safe_execfile, but for .ipy files with IPython syntax.
2078 2081
2079 2082 Parameters
2080 2083 ----------
2081 2084 fname : str
2082 2085 The name of the file to execute. The filename must have a
2083 2086 .ipy extension.
2084 2087 """
2085 2088 fname = os.path.abspath(os.path.expanduser(fname))
2086 2089
2087 2090 # Make sure we have a .py file
2088 2091 if not fname.endswith('.ipy'):
2089 2092 warn('File must end with .py to be run using execfile: <%s>' % fname)
2090 2093
2091 2094 # Make sure we can open the file
2092 2095 try:
2093 2096 with open(fname) as thefile:
2094 2097 pass
2095 2098 except:
2096 2099 warn('Could not open file <%s> for safe execution.' % fname)
2097 2100 return
2098 2101
2099 2102 # Find things also in current directory. This is needed to mimic the
2100 2103 # behavior of running a script from the system command line, where
2101 2104 # Python inserts the script's directory into sys.path
2102 2105 dname = os.path.dirname(fname)
2103 2106
2104 2107 with prepended_to_syspath(dname):
2105 2108 try:
2106 2109 with open(fname) as thefile:
2107 2110 # self.run_cell currently captures all exceptions
2108 2111 # raised in user code. It would be nice if there were
2109 2112 # versions of runlines, execfile that did raise, so
2110 2113 # we could catch the errors.
2111 2114 self.run_cell(thefile.read(), store_history=False)
2112 2115 except:
2113 2116 self.showtraceback()
2114 2117 warn('Unknown failure executing file: <%s>' % fname)
2115 2118
2116 2119 def run_cell(self, raw_cell, store_history=True):
2117 2120 """Run a complete IPython cell.
2118 2121
2119 2122 Parameters
2120 2123 ----------
2121 2124 raw_cell : str
2122 2125 The code (including IPython code such as %magic functions) to run.
2123 2126 store_history : bool
2124 2127 If True, the raw and translated cell will be stored in IPython's
2125 2128 history. For user code calling back into IPython's machinery, this
2126 2129 should be set to False.
2127 2130 """
2128 2131 if (not raw_cell) or raw_cell.isspace():
2129 2132 return
2130 2133
2131 2134 for line in raw_cell.splitlines():
2132 2135 self.input_splitter.push(line)
2133 2136 cell = self.input_splitter.source_reset()
2134 2137
2135 2138 with self.builtin_trap:
2136 2139 if len(cell.splitlines()) == 1:
2137 2140 cell = self.prefilter_manager.prefilter_lines(cell)
2138 2141
2139 2142 # Store raw and processed history
2140 2143 if store_history:
2141 2144 self.history_manager.store_inputs(self.execution_count,
2142 2145 cell, raw_cell)
2143 2146
2144 2147 self.logger.log(cell, raw_cell)
2145 2148
2146 2149 cell_name = self.compile.cache(cell, self.execution_count)
2147 2150
2148 2151 with self.display_trap:
2149 2152 try:
2150 2153 code_ast = ast.parse(cell, filename=cell_name)
2151 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2152 # Case 1
2154 except (OverflowError, SyntaxError, ValueError, TypeError,
2155 MemoryError):
2153 2156 self.showsyntaxerror()
2154 2157 self.execution_count += 1
2155 2158 return None
2156 2159
2157 2160 interactivity = 'last' # Last node to be run interactive
2158 2161 if len(cell.splitlines()) == 1:
2159 2162 interactivity = 'all' # Single line; run fully interactive
2160 2163
2161 2164 self.run_ast_nodes(code_ast.body, cell_name, interactivity)
2165
2166 # Execute any registered post-execution functions.
2167 for func, status in self._post_execute.iteritems():
2168 if not status:
2169 continue
2170 try:
2171 func()
2172 except:
2173 self.showtraceback()
2174 # Deactivate failing function
2175 self._post_execute[func] = False
2162 2176
2163 2177 if store_history:
2164 2178 # Write output to the database. Does nothing unless
2165 2179 # history output logging is enabled.
2166 2180 self.history_manager.store_output(self.execution_count)
2167 2181 # Each cell is a *single* input, regardless of how many lines it has
2168 2182 self.execution_count += 1
2169 2183
2170 2184 def run_ast_nodes(self, nodelist, cell_name, interactivity='last'):
2171 2185 """Run a sequence of AST nodes. The execution mode depends on the
2172 2186 interactivity parameter.
2173 2187
2174 2188 Parameters
2175 2189 ----------
2176 2190 nodelist : list
2177 2191 A sequence of AST nodes to run.
2178 2192 cell_name : str
2179 2193 Will be passed to the compiler as the filename of the cell. Typically
2180 2194 the value returned by ip.compile.cache(cell).
2181 2195 interactivity : str
2182 2196 'all', 'last' or 'none', specifying which nodes should be run
2183 2197 interactively (displaying output from expressions). Other values for
2184 2198 this parameter will raise a ValueError.
2185 2199 """
2186 2200 if not nodelist:
2187 2201 return
2188 2202
2189 2203 if interactivity == 'none':
2190 2204 to_run_exec, to_run_interactive = nodelist, []
2191 2205 elif interactivity == 'last':
2192 2206 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2193 2207 elif interactivity == 'all':
2194 2208 to_run_exec, to_run_interactive = [], nodelist
2195 2209 else:
2196 2210 raise ValueError("Interactivity was %r" % interactivity)
2197 2211
2198 2212 exec_count = self.execution_count
2199 2213 if to_run_exec:
2200 2214 mod = ast.Module(to_run_exec)
2201 2215 self.code_to_run = code = self.compile(mod, cell_name, "exec")
2202 2216 if self.run_code(code) == 1:
2203 2217 return
2204 2218
2205 2219 if to_run_interactive:
2206 2220 mod = ast.Interactive(to_run_interactive)
2207 2221 self.code_to_run = code = self.compile(mod, cell_name, "single")
2208 2222 return self.run_code(code)
2209 2223
2210 2224
2211 2225 # PENDING REMOVAL: this method is slated for deletion, once our new
2212 2226 # input logic has been 100% moved to frontends and is stable.
2213 2227 def runlines(self, lines, clean=False):
2214 2228 """Run a string of one or more lines of source.
2215 2229
2216 2230 This method is capable of running a string containing multiple source
2217 2231 lines, as if they had been entered at the IPython prompt. Since it
2218 2232 exposes IPython's processing machinery, the given strings can contain
2219 2233 magic calls (%magic), special shell access (!cmd), etc.
2220 2234 """
2221 2235
2222 2236 if not isinstance(lines, (list, tuple)):
2223 2237 lines = lines.splitlines()
2224 2238
2225 2239 if clean:
2226 2240 lines = self._cleanup_ipy_script(lines)
2227 2241
2228 2242 # We must start with a clean buffer, in case this is run from an
2229 2243 # interactive IPython session (via a magic, for example).
2230 2244 self.reset_buffer()
2231 2245
2232 2246 # Since we will prefilter all lines, store the user's raw input too
2233 2247 # before we apply any transformations
2234 2248 self.buffer_raw[:] = [ l+'\n' for l in lines]
2235 2249
2236 2250 more = False
2237 2251 prefilter_lines = self.prefilter_manager.prefilter_lines
2238 2252 with nested(self.builtin_trap, self.display_trap):
2239 2253 for line in lines:
2240 2254 # skip blank lines so we don't mess up the prompt counter, but
2241 2255 # do NOT skip even a blank line if we are in a code block (more
2242 2256 # is true)
2243 2257
2244 2258 if line or more:
2245 2259 more = self.push_line(prefilter_lines(line, more))
2246 2260 # IPython's run_source returns None if there was an error
2247 2261 # compiling the code. This allows us to stop processing
2248 2262 # right away, so the user gets the error message at the
2249 2263 # right place.
2250 2264 if more is None:
2251 2265 break
2252 2266 # final newline in case the input didn't have it, so that the code
2253 2267 # actually does get executed
2254 2268 if more:
2255 2269 self.push_line('\n')
2256 2270
2257 def run_source(self, source, filename=None,
2258 symbol='single', post_execute=True):
2271 def run_source(self, source, filename=None, symbol='single'):
2259 2272 """Compile and run some source in the interpreter.
2260 2273
2261 2274 Arguments are as for compile_command().
2262 2275
2263 2276 One several things can happen:
2264 2277
2265 2278 1) The input is incorrect; compile_command() raised an
2266 2279 exception (SyntaxError or OverflowError). A syntax traceback
2267 2280 will be printed by calling the showsyntaxerror() method.
2268 2281
2269 2282 2) The input is incomplete, and more input is required;
2270 2283 compile_command() returned None. Nothing happens.
2271 2284
2272 2285 3) The input is complete; compile_command() returned a code
2273 2286 object. The code is executed by calling self.run_code() (which
2274 2287 also handles run-time exceptions, except for SystemExit).
2275 2288
2276 2289 The return value is:
2277 2290
2278 2291 - True in case 2
2279 2292
2280 2293 - False in the other cases, unless an exception is raised, where
2281 2294 None is returned instead. This can be used by external callers to
2282 2295 know whether to continue feeding input or not.
2283 2296
2284 2297 The return value can be used to decide whether to use sys.ps1 or
2285 2298 sys.ps2 to prompt the next line."""
2286 2299
2287 2300 # We need to ensure that the source is unicode from here on.
2288 2301 if type(source)==str:
2289 2302 usource = source.decode(self.stdin_encoding)
2290 2303 else:
2291 2304 usource = source
2292 2305
2293 if False: # dbg
2294 print 'Source:', repr(source) # dbg
2295 print 'USource:', repr(usource) # dbg
2296 print 'type:', type(source) # dbg
2297 print 'encoding', self.stdin_encoding # dbg
2298
2299 2306 try:
2300 2307 code_name = self.compile.cache(usource, self.execution_count)
2301 2308 code = self.compile(usource, code_name, symbol)
2302 2309 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2303 2310 # Case 1
2304 2311 self.showsyntaxerror(filename)
2305 2312 return None
2306 2313
2307 2314 if code is None:
2308 2315 # Case 2
2309 2316 return True
2310 2317
2311 2318 # Case 3
2312 2319 # We store the code object so that threaded shells and
2313 2320 # custom exception handlers can access all this info if needed.
2314 2321 # The source corresponding to this can be obtained from the
2315 2322 # buffer attribute as '\n'.join(self.buffer).
2316 2323 self.code_to_run = code
2317 2324 # now actually execute the code object
2318 if self.run_code(code, post_execute) == 0:
2325 if self.run_code(code) == 0:
2319 2326 return False
2320 2327 else:
2321 2328 return None
2322 2329
2323 2330 # For backwards compatibility
2324 2331 runsource = run_source
2325 2332
2326 def run_code(self, code_obj, post_execute=True):
2333 def run_code(self, code_obj):
2327 2334 """Execute a code object.
2328 2335
2329 2336 When an exception occurs, self.showtraceback() is called to display a
2330 2337 traceback.
2331 2338
2332 2339 Return value: a flag indicating whether the code to be run completed
2333 2340 successfully:
2334 2341
2335 2342 - 0: successful execution.
2336 2343 - 1: an error occurred.
2337 2344 """
2338 2345
2339 2346 # Set our own excepthook in case the user code tries to call it
2340 2347 # directly, so that the IPython crash handler doesn't get triggered
2341 2348 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2342 2349
2343 2350 # we save the original sys.excepthook in the instance, in case config
2344 2351 # code (such as magics) needs access to it.
2345 2352 self.sys_excepthook = old_excepthook
2346 2353 outflag = 1 # happens in more places, so it's easier as default
2347 2354 try:
2348 2355 try:
2349 2356 self.hooks.pre_run_code_hook()
2350 2357 #rprint('Running code', repr(code_obj)) # dbg
2351 2358 exec code_obj in self.user_global_ns, self.user_ns
2352 2359 finally:
2353 2360 # Reset our crash handler in place
2354 2361 sys.excepthook = old_excepthook
2355 2362 except SystemExit:
2356 2363 self.reset_buffer()
2357 2364 self.showtraceback(exception_only=True)
2358 2365 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2359 2366 except self.custom_exceptions:
2360 2367 etype,value,tb = sys.exc_info()
2361 2368 self.CustomTB(etype,value,tb)
2362 2369 except:
2363 2370 self.showtraceback()
2364 2371 else:
2365 2372 outflag = 0
2366 2373 if softspace(sys.stdout, 0):
2367 2374 print
2368 2375
2369 # Execute any registered post-execution functions. Here, any errors
2370 # are reported only minimally and just on the terminal, because the
2371 # main exception channel may be occupied with a user traceback.
2372 # FIXME: we need to think this mechanism a little more carefully.
2373 if post_execute:
2374 for func in self._post_execute:
2375 try:
2376 func()
2377 except:
2378 head = '[ ERROR ] Evaluating post_execute function: %s' % \
2379 func
2380 print >> io.Term.cout, head
2381 print >> io.Term.cout, self._simple_error()
2382 print >> io.Term.cout, 'Removing from post_execute'
2383 self._post_execute.remove(func)
2384
2385 2376 # Flush out code object which has been run (and source)
2386 2377 self.code_to_run = None
2387 2378 return outflag
2388 2379
2389 2380 # For backwards compatibility
2390 2381 runcode = run_code
2391 2382
2392 2383 # PENDING REMOVAL: this method is slated for deletion, once our new
2393 2384 # input logic has been 100% moved to frontends and is stable.
2394 2385 def push_line(self, line):
2395 2386 """Push a line to the interpreter.
2396 2387
2397 2388 The line should not have a trailing newline; it may have
2398 2389 internal newlines. The line is appended to a buffer and the
2399 2390 interpreter's run_source() method is called with the
2400 2391 concatenated contents of the buffer as source. If this
2401 2392 indicates that the command was executed or invalid, the buffer
2402 2393 is reset; otherwise, the command is incomplete, and the buffer
2403 2394 is left as it was after the line was appended. The return
2404 2395 value is 1 if more input is required, 0 if the line was dealt
2405 2396 with in some way (this is the same as run_source()).
2406 2397 """
2407 2398
2408 2399 # autoindent management should be done here, and not in the
2409 2400 # interactive loop, since that one is only seen by keyboard input. We
2410 2401 # need this done correctly even for code run via runlines (which uses
2411 2402 # push).
2412 2403
2413 2404 #print 'push line: <%s>' % line # dbg
2414 2405 self.buffer.append(line)
2415 2406 full_source = '\n'.join(self.buffer)
2416 2407 more = self.run_source(full_source, self.filename)
2417 2408 if not more:
2418 2409 self.history_manager.store_inputs(self.execution_count,
2419 2410 '\n'.join(self.buffer_raw), full_source)
2420 2411 self.reset_buffer()
2421 2412 self.execution_count += 1
2422 2413 return more
2423 2414
2424 2415 def reset_buffer(self):
2425 2416 """Reset the input buffer."""
2426 2417 self.buffer[:] = []
2427 2418 self.buffer_raw[:] = []
2428 2419 self.input_splitter.reset()
2429 2420
2430 2421 # For backwards compatibility
2431 2422 resetbuffer = reset_buffer
2432 2423
2433 2424 def _is_secondary_block_start(self, s):
2434 2425 if not s.endswith(':'):
2435 2426 return False
2436 2427 if (s.startswith('elif') or
2437 2428 s.startswith('else') or
2438 2429 s.startswith('except') or
2439 2430 s.startswith('finally')):
2440 2431 return True
2441 2432
2442 2433 def _cleanup_ipy_script(self, script):
2443 2434 """Make a script safe for self.runlines()
2444 2435
2445 2436 Currently, IPython is lines based, with blocks being detected by
2446 2437 empty lines. This is a problem for block based scripts that may
2447 2438 not have empty lines after blocks. This script adds those empty
2448 2439 lines to make scripts safe for running in the current line based
2449 2440 IPython.
2450 2441 """
2451 2442 res = []
2452 2443 lines = script.splitlines()
2453 2444 level = 0
2454 2445
2455 2446 for l in lines:
2456 2447 lstripped = l.lstrip()
2457 2448 stripped = l.strip()
2458 2449 if not stripped:
2459 2450 continue
2460 2451 newlevel = len(l) - len(lstripped)
2461 2452 if level > 0 and newlevel == 0 and \
2462 2453 not self._is_secondary_block_start(stripped):
2463 2454 # add empty line
2464 2455 res.append('')
2465 2456 res.append(l)
2466 2457 level = newlevel
2467 2458
2468 2459 return '\n'.join(res) + '\n'
2469 2460
2470 2461 #-------------------------------------------------------------------------
2471 2462 # Things related to GUI support and pylab
2472 2463 #-------------------------------------------------------------------------
2473 2464
2474 2465 def enable_pylab(self, gui=None):
2475 2466 raise NotImplementedError('Implement enable_pylab in a subclass')
2476 2467
2477 2468 #-------------------------------------------------------------------------
2478 2469 # Utilities
2479 2470 #-------------------------------------------------------------------------
2480 2471
2481 2472 def var_expand(self,cmd,depth=0):
2482 2473 """Expand python variables in a string.
2483 2474
2484 2475 The depth argument indicates how many frames above the caller should
2485 2476 be walked to look for the local namespace where to expand variables.
2486 2477
2487 2478 The global namespace for expansion is always the user's interactive
2488 2479 namespace.
2489 2480 """
2490 2481 res = ItplNS(cmd, self.user_ns, # globals
2491 2482 # Skip our own frame in searching for locals:
2492 2483 sys._getframe(depth+1).f_locals # locals
2493 2484 )
2494 2485 return str(res).decode(res.codec)
2495 2486
2496 2487 def mktempfile(self, data=None, prefix='ipython_edit_'):
2497 2488 """Make a new tempfile and return its filename.
2498 2489
2499 2490 This makes a call to tempfile.mktemp, but it registers the created
2500 2491 filename internally so ipython cleans it up at exit time.
2501 2492
2502 2493 Optional inputs:
2503 2494
2504 2495 - data(None): if data is given, it gets written out to the temp file
2505 2496 immediately, and the file is closed again."""
2506 2497
2507 2498 filename = tempfile.mktemp('.py', prefix)
2508 2499 self.tempfiles.append(filename)
2509 2500
2510 2501 if data:
2511 2502 tmp_file = open(filename,'w')
2512 2503 tmp_file.write(data)
2513 2504 tmp_file.close()
2514 2505 return filename
2515 2506
2516 2507 # TODO: This should be removed when Term is refactored.
2517 2508 def write(self,data):
2518 2509 """Write a string to the default output"""
2519 2510 io.Term.cout.write(data)
2520 2511
2521 2512 # TODO: This should be removed when Term is refactored.
2522 2513 def write_err(self,data):
2523 2514 """Write a string to the default error output"""
2524 2515 io.Term.cerr.write(data)
2525 2516
2526 2517 def ask_yes_no(self,prompt,default=True):
2527 2518 if self.quiet:
2528 2519 return True
2529 2520 return ask_yes_no(prompt,default)
2530 2521
2531 2522 def show_usage(self):
2532 2523 """Show a usage message"""
2533 2524 page.page(IPython.core.usage.interactive_usage)
2534 2525
2535 2526 def find_user_code(self, target, raw=True):
2536 2527 """Get a code string from history, file, or a string or macro.
2537 2528
2538 2529 This is mainly used by magic functions.
2539 2530
2540 2531 Parameters
2541 2532 ----------
2542 2533 target : str
2543 2534 A string specifying code to retrieve. This will be tried respectively
2544 2535 as: ranges of input history (see %history for syntax), a filename, or
2545 2536 an expression evaluating to a string or Macro in the user namespace.
2546 2537 raw : bool
2547 2538 If true (default), retrieve raw history. Has no effect on the other
2548 2539 retrieval mechanisms.
2549 2540
2550 2541 Returns
2551 2542 -------
2552 2543 A string of code.
2553 2544
2554 2545 ValueError is raised if nothing is found, and TypeError if it evaluates
2555 2546 to an object of another type. In each case, .args[0] is a printable
2556 2547 message.
2557 2548 """
2558 2549 code = self.extract_input_lines(target, raw=raw) # Grab history
2559 2550 if code:
2560 2551 return code
2561 2552 if os.path.isfile(target): # Read file
2562 2553 return open(target, "r").read()
2563 2554
2564 2555 try: # User namespace
2565 2556 codeobj = eval(target, self.user_ns)
2566 2557 except Exception:
2567 2558 raise ValueError(("'%s' was not found in history, as a file, nor in"
2568 2559 " the user namespace.") % target)
2569 2560 if isinstance(codeobj, basestring):
2570 2561 return codeobj
2571 2562 elif isinstance(codeobj, Macro):
2572 2563 return codeobj.value
2573 2564
2574 2565 raise TypeError("%s is neither a string nor a macro." % target,
2575 2566 codeobj)
2576 2567
2577 2568 #-------------------------------------------------------------------------
2578 2569 # Things related to IPython exiting
2579 2570 #-------------------------------------------------------------------------
2580 2571 def atexit_operations(self):
2581 2572 """This will be executed at the time of exit.
2582 2573
2583 2574 Cleanup operations and saving of persistent data that is done
2584 2575 unconditionally by IPython should be performed here.
2585 2576
2586 2577 For things that may depend on startup flags or platform specifics (such
2587 2578 as having readline or not), register a separate atexit function in the
2588 2579 code that has the appropriate information, rather than trying to
2589 2580 clutter
2590 2581 """
2591 2582 # Cleanup all tempfiles left around
2592 2583 for tfile in self.tempfiles:
2593 2584 try:
2594 2585 os.unlink(tfile)
2595 2586 except OSError:
2596 2587 pass
2597 2588
2598 2589 # Close the history session (this stores the end time and line count)
2599 2590 self.history_manager.end_session()
2600 2591
2601 2592 # Clear all user namespaces to release all references cleanly.
2602 2593 self.reset(new_session=False)
2603 2594
2604 2595 # Run user hooks
2605 2596 self.hooks.shutdown_hook()
2606 2597
2607 2598 def cleanup(self):
2608 2599 self.restore_sys_module_state()
2609 2600
2610 2601
2611 2602 class InteractiveShellABC(object):
2612 2603 """An abstract base class for InteractiveShell."""
2613 2604 __metaclass__ = abc.ABCMeta
2614 2605
2615 2606 InteractiveShellABC.register(InteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now