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