##// END OF EJS Templates
More re-organization of InteractiveShell.
Brian Granger -
Show More
@@ -227,6 +227,14 b' class InteractiveShellEmbed(InteractiveShell):'
227 227 for var in local_varnames:
228 228 delvar(var,None)
229 229
230 def set_completer_frame(self, frame=None):
231 if frame:
232 self.Completer.namespace = frame.f_locals
233 self.Completer.global_namespace = frame.f_globals
234 else:
235 self.Completer.namespace = self.user_ns
236 self.Completer.global_namespace = self.user_global_ns
237
230 238
231 239 _embedded_shell = None
232 240
This diff has been collapsed as it changes many lines, (2896 lines changed) Show them Hide them
@@ -322,9 +322,6 b' class InteractiveShell(Component, Magic):'
322 322 self.init_pdb()
323 323 self.hooks.late_startup_hook()
324 324
325 def cleanup(self):
326 self.restore_sys_module_state()
327
328 325 #-------------------------------------------------------------------------
329 326 # Traitlet changed handlers
330 327 #-------------------------------------------------------------------------
@@ -346,6 +343,21 b' class InteractiveShell(Component, Magic):'
346 343 def _term_title_changed(self, name, new_value):
347 344 self.init_term_title()
348 345
346 def set_autoindent(self,value=None):
347 """Set the autoindent flag, checking for readline support.
348
349 If called with no arguments, it acts as a toggle."""
350
351 if not self.has_readline:
352 if os.name == 'posix':
353 warn("The auto-indent feature requires the readline library")
354 self.autoindent = 0
355 return
356 if value is None:
357 self.autoindent = not self.autoindent
358 else:
359 self.autoindent = value
360
349 361 #-------------------------------------------------------------------------
350 362 # init_* methods called by __init__
351 363 #-------------------------------------------------------------------------
@@ -437,580 +449,500 b' class InteractiveShell(Component, Magic):'
437 449 if self.profile:
438 450 self.banner += '\nIPython profile: %s\n' % self.profile
439 451 if self.banner2:
440 self.banner += '\n' + self.banner2 + '\n'
441
442 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
443 # Create the namespace where the user will operate. user_ns is
444 # normally the only one used, and it is passed to the exec calls as
445 # the locals argument. But we do carry a user_global_ns namespace
446 # given as the exec 'globals' argument, This is useful in embedding
447 # situations where the ipython shell opens in a context where the
448 # distinction between locals and globals is meaningful. For
449 # non-embedded contexts, it is just the same object as the user_ns dict.
450
451 # FIXME. For some strange reason, __builtins__ is showing up at user
452 # level as a dict instead of a module. This is a manual fix, but I
453 # should really track down where the problem is coming from. Alex
454 # Schmolck reported this problem first.
452 self.banner += '\n' + self.banner2 + '\n'
455 453
456 # A useful post by Alex Martelli on this topic:
457 # Re: inconsistent value from __builtins__
458 # Von: Alex Martelli <aleaxit@yahoo.com>
459 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
460 # Gruppen: comp.lang.python
454 def init_encoding(self):
455 # Get system encoding at startup time. Certain terminals (like Emacs
456 # under Win32 have it set to None, and we need to have a known valid
457 # encoding to use in the raw_input() method
458 try:
459 self.stdin_encoding = sys.stdin.encoding or 'ascii'
460 except AttributeError:
461 self.stdin_encoding = 'ascii'
461 462
462 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
463 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
464 # > <type 'dict'>
465 # > >>> print type(__builtins__)
466 # > <type 'module'>
467 # > Is this difference in return value intentional?
463 def init_syntax_highlighting(self):
464 # Python source parser/formatter for syntax highlighting
465 pyformat = PyColorize.Parser().format
466 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
468 467
469 # Well, it's documented that '__builtins__' can be either a dictionary
470 # or a module, and it's been that way for a long time. Whether it's
471 # intentional (or sensible), I don't know. In any case, the idea is
472 # that if you need to access the built-in namespace directly, you
473 # should start with "import __builtin__" (note, no 's') which will
474 # definitely give you a module. Yeah, it's somewhat confusing:-(.
468 def init_pushd_popd_magic(self):
469 # for pushd/popd management
470 try:
471 self.home_dir = get_home_dir()
472 except HomeDirError, msg:
473 fatal(msg)
475 474
476 # These routines return properly built dicts as needed by the rest of
477 # the code, and can also be used by extension writers to generate
478 # properly initialized namespaces.
479 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
480 user_global_ns)
475 self.dir_stack = []
481 476
482 # Assign namespaces
483 # This is the namespace where all normal user variables live
484 self.user_ns = user_ns
485 self.user_global_ns = user_global_ns
477 def init_logger(self):
478 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
479 # local shortcut, this is used a LOT
480 self.log = self.logger.log
481 # template for logfile headers. It gets resolved at runtime by the
482 # logstart method.
483 self.loghead_tpl = \
484 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
485 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
486 #log# opts = %s
487 #log# args = %s
488 #log# It is safe to make manual edits below here.
489 #log#-----------------------------------------------------------------------
490 """
486 491
487 # An auxiliary namespace that checks what parts of the user_ns were
488 # loaded at startup, so we can list later only variables defined in
489 # actual interactive use. Since it is always a subset of user_ns, it
490 # doesn't need to be seaparately tracked in the ns_table
491 self.user_config_ns = {}
492 def init_logstart(self):
493 if self.logplay:
494 self.magic_logstart(self.logplay + ' append')
495 elif self.logfile:
496 self.magic_logstart(self.logfile)
497 elif self.logstart:
498 self.magic_logstart()
492 499
493 # A namespace to keep track of internal data structures to prevent
494 # them from cluttering user-visible stuff. Will be updated later
495 self.internal_ns = {}
500 def init_builtins(self):
501 self.builtin_trap = BuiltinTrap(self)
496 502
497 # Namespace of system aliases. Each entry in the alias
498 # table must be a 2-tuple of the form (N,name), where N is the number
499 # of positional arguments of the alias.
500 self.alias_table = {}
503 def init_inspector(self):
504 # Object inspector
505 self.inspector = oinspect.Inspector(oinspect.InspectColors,
506 PyColorize.ANSICodeColors,
507 'NoColor',
508 self.object_info_string_level)
501 509
502 # Now that FakeModule produces a real module, we've run into a nasty
503 # problem: after script execution (via %run), the module where the user
504 # code ran is deleted. Now that this object is a true module (needed
505 # so docetst and other tools work correctly), the Python module
506 # teardown mechanism runs over it, and sets to None every variable
507 # present in that module. Top-level references to objects from the
508 # script survive, because the user_ns is updated with them. However,
509 # calling functions defined in the script that use other things from
510 # the script will fail, because the function's closure had references
511 # to the original objects, which are now all None. So we must protect
512 # these modules from deletion by keeping a cache.
513 #
514 # To avoid keeping stale modules around (we only need the one from the
515 # last run), we use a dict keyed with the full path to the script, so
516 # only the last version of the module is held in the cache. Note,
517 # however, that we must cache the module *namespace contents* (their
518 # __dict__). Because if we try to cache the actual modules, old ones
519 # (uncached) could be destroyed while still holding references (such as
520 # those held by GUI objects that tend to be long-lived)>
521 #
522 # The %reset command will flush this cache. See the cache_main_mod()
523 # and clear_main_mod_cache() methods for details on use.
510 def init_prompts(self):
511 # Initialize cache, set in/out prompts and printing system
512 self.outputcache = CachedOutput(self,
513 self.cache_size,
514 self.pprint,
515 input_sep = self.separate_in,
516 output_sep = self.separate_out,
517 output_sep2 = self.separate_out2,
518 ps1 = self.prompt_in1,
519 ps2 = self.prompt_in2,
520 ps_out = self.prompt_out,
521 pad_left = self.prompts_pad_left)
524 522
525 # This is the cache used for 'main' namespaces
526 self._main_ns_cache = {}
527 # And this is the single instance of FakeModule whose __dict__ we keep
528 # copying and clearing for reuse on each %run
529 self._user_main_module = FakeModule()
523 # user may have over-ridden the default print hook:
524 try:
525 self.outputcache.__class__.display = self.hooks.display
526 except AttributeError:
527 pass
530 528
531 # A table holding all the namespaces IPython deals with, so that
532 # introspection facilities can search easily.
533 self.ns_table = {'user':user_ns,
534 'user_global':user_global_ns,
535 'alias':self.alias_table,
536 'internal':self.internal_ns,
537 'builtin':__builtin__.__dict__
538 }
529 def init_displayhook(self):
530 self.display_trap = DisplayTrap(self, self.outputcache)
539 531
540 # Similarly, track all namespaces where references can be held and that
541 # we can safely clear (so it can NOT include builtin). This one can be
542 # a simple list.
543 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
544 self.alias_table, self.internal_ns,
545 self._main_ns_cache ]
546
547 def init_sys_modules(self):
548 # We need to insert into sys.modules something that looks like a
549 # module but which accesses the IPython namespace, for shelve and
550 # pickle to work interactively. Normally they rely on getting
551 # everything out of __main__, but for embedding purposes each IPython
552 # instance has its own private namespace, so we can't go shoving
553 # everything into __main__.
532 def init_reload_doctest(self):
533 # Do a proper resetting of doctest, including the necessary displayhook
534 # monkeypatching
535 try:
536 doctest_reload()
537 except ImportError:
538 warn("doctest module does not exist.")
554 539
555 # note, however, that we should only do this for non-embedded
556 # ipythons, which really mimic the __main__.__dict__ with their own
557 # namespace. Embedded instances, on the other hand, should not do
558 # this because they need to manage the user local/global namespaces
559 # only, but they live within a 'normal' __main__ (meaning, they
560 # shouldn't overtake the execution environment of the script they're
561 # embedded in).
540 #-------------------------------------------------------------------------
541 # Things related to injections into the sys module
542 #-------------------------------------------------------------------------
562 543
563 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
544 def save_sys_module_state(self):
545 """Save the state of hooks in the sys module.
564 546
547 This has to be called after self.user_ns is created.
548 """
549 self._orig_sys_module_state = {}
550 self._orig_sys_module_state['stdin'] = sys.stdin
551 self._orig_sys_module_state['stdout'] = sys.stdout
552 self._orig_sys_module_state['stderr'] = sys.stderr
553 self._orig_sys_module_state['excepthook'] = sys.excepthook
565 554 try:
566 main_name = self.user_ns['__name__']
555 self._orig_sys_modules_main_name = self.user_ns['__name__']
567 556 except KeyError:
568 raise KeyError('user_ns dictionary MUST have a "__name__" key')
569 else:
570 sys.modules[main_name] = FakeModule(self.user_ns)
557 pass
571 558
572 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
573 """Return a valid local and global user interactive namespaces.
559 def restore_sys_module_state(self):
560 """Restore the state of the sys module."""
561 try:
562 for k, v in self._orig_sys_module_state.items():
563 setattr(sys, k, v)
564 except AttributeError:
565 pass
566 try:
567 delattr(sys, 'ipcompleter')
568 except AttributeError:
569 pass
570 # Reset what what done in self.init_sys_modules
571 try:
572 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
573 except (AttributeError, KeyError):
574 pass
574 575
575 This builds a dict with the minimal information needed to operate as a
576 valid IPython user namespace, which you can pass to the various
577 embedding classes in ipython. The default implementation returns the
578 same dict for both the locals and the globals to allow functions to
579 refer to variables in the namespace. Customized implementations can
580 return different dicts. The locals dictionary can actually be anything
581 following the basic mapping protocol of a dict, but the globals dict
582 must be a true dict, not even a subclass. It is recommended that any
583 custom object for the locals namespace synchronize with the globals
584 dict somehow.
576 #-------------------------------------------------------------------------
577 # Things related to hooks
578 #-------------------------------------------------------------------------
585 579
586 Raises TypeError if the provided globals namespace is not a true dict.
580 def init_hooks(self):
581 # hooks holds pointers used for user-side customizations
582 self.hooks = Struct()
587 583
588 :Parameters:
589 user_ns : dict-like, optional
590 The current user namespace. The items in this namespace should
591 be included in the output. If None, an appropriate blank
592 namespace should be created.
593 user_global_ns : dict, optional
594 The current user global namespace. The items in this namespace
595 should be included in the output. If None, an appropriate
596 blank namespace should be created.
597
598 :Returns:
599 A tuple pair of dictionary-like object to be used as the local namespace
600 of the interpreter and a dict to be used as the global namespace.
601 """
584 self.strdispatchers = {}
602 585
603 if user_ns is None:
604 # Set __name__ to __main__ to better match the behavior of the
605 # normal interpreter.
606 user_ns = {'__name__' :'__main__',
607 '__builtins__' : __builtin__,
608 }
609 else:
610 user_ns.setdefault('__name__','__main__')
611 user_ns.setdefault('__builtins__',__builtin__)
586 # Set all default hooks, defined in the IPython.hooks module.
587 import IPython.core.hooks
588 hooks = IPython.core.hooks
589 for hook_name in hooks.__all__:
590 # default hooks have priority 100, i.e. low; user hooks should have
591 # 0-100 priority
592 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
612 593
613 if user_global_ns is None:
614 user_global_ns = user_ns
615 if type(user_global_ns) is not dict:
616 raise TypeError("user_global_ns must be a true dict; got %r"
617 % type(user_global_ns))
594 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
595 """set_hook(name,hook) -> sets an internal IPython hook.
618 596
619 return user_ns, user_global_ns
597 IPython exposes some of its internal API as user-modifiable hooks. By
598 adding your function to one of these hooks, you can modify IPython's
599 behavior to call at runtime your own routines."""
620 600
621 def init_history(self):
622 # List of input with multi-line handling.
623 self.input_hist = InputList()
624 # This one will hold the 'raw' input history, without any
625 # pre-processing. This will allow users to retrieve the input just as
626 # it was exactly typed in by the user, with %hist -r.
627 self.input_hist_raw = InputList()
601 # At some point in the future, this should validate the hook before it
602 # accepts it. Probably at least check that the hook takes the number
603 # of args it's supposed to.
604
605 f = new.instancemethod(hook,self,self.__class__)
628 606
629 # list of visited directories
607 # check if the hook is for strdispatcher first
608 if str_key is not None:
609 sdp = self.strdispatchers.get(name, StrDispatch())
610 sdp.add_s(str_key, f, priority )
611 self.strdispatchers[name] = sdp
612 return
613 if re_key is not None:
614 sdp = self.strdispatchers.get(name, StrDispatch())
615 sdp.add_re(re.compile(re_key), f, priority )
616 self.strdispatchers[name] = sdp
617 return
618
619 dp = getattr(self.hooks, name, None)
620 if name not in IPython.core.hooks.__all__:
621 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
622 if not dp:
623 dp = IPython.core.hooks.CommandChainDispatcher()
624
630 625 try:
631 self.dir_hist = [os.getcwd()]
632 except OSError:
633 self.dir_hist = []
626 dp.add(f,priority)
627 except AttributeError:
628 # it was not commandchain, plain old func - replace
629 dp = f
634 630
635 # dict of output history
636 self.output_hist = {}
631 setattr(self.hooks,name, dp)
637 632
638 # Now the history file
639 try:
640 histfname = 'history-%s' % self.profile
641 except AttributeError:
642 histfname = 'history'
643 self.histfile = os.path.join(self.config.IPYTHONDIR, histfname)
633 #-------------------------------------------------------------------------
634 # Things related to the "main" module
635 #-------------------------------------------------------------------------
644 636
645 # Fill the history zero entry, user counter starts at 1
646 self.input_hist.append('\n')
647 self.input_hist_raw.append('\n')
637 def new_main_mod(self,ns=None):
638 """Return a new 'main' module object for user code execution.
639 """
640 main_mod = self._user_main_module
641 init_fakemod_dict(main_mod,ns)
642 return main_mod
648 643
649 def init_encoding(self):
650 # Get system encoding at startup time. Certain terminals (like Emacs
651 # under Win32 have it set to None, and we need to have a known valid
652 # encoding to use in the raw_input() method
653 try:
654 self.stdin_encoding = sys.stdin.encoding or 'ascii'
655 except AttributeError:
656 self.stdin_encoding = 'ascii'
644 def cache_main_mod(self,ns,fname):
645 """Cache a main module's namespace.
657 646
658 def init_handlers(self):
659 # escapes for automatic behavior on the command line
660 self.ESC_SHELL = '!'
661 self.ESC_SH_CAP = '!!'
662 self.ESC_HELP = '?'
663 self.ESC_MAGIC = '%'
664 self.ESC_QUOTE = ','
665 self.ESC_QUOTE2 = ';'
666 self.ESC_PAREN = '/'
647 When scripts are executed via %run, we must keep a reference to the
648 namespace of their __main__ module (a FakeModule instance) around so
649 that Python doesn't clear it, rendering objects defined therein
650 useless.
667 651
668 # And their associated handlers
669 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
670 self.ESC_QUOTE : self.handle_auto,
671 self.ESC_QUOTE2 : self.handle_auto,
672 self.ESC_MAGIC : self.handle_magic,
673 self.ESC_HELP : self.handle_help,
674 self.ESC_SHELL : self.handle_shell_escape,
675 self.ESC_SH_CAP : self.handle_shell_escape,
676 }
652 This method keeps said reference in a private dict, keyed by the
653 absolute path of the module object (which corresponds to the script
654 path). This way, for multiple executions of the same script we only
655 keep one copy of the namespace (the last one), thus preventing memory
656 leaks from old references while allowing the objects from the last
657 execution to be accessible.
677 658
678 def init_syntax_highlighting(self):
679 # Python source parser/formatter for syntax highlighting
680 pyformat = PyColorize.Parser().format
681 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
659 Note: we can not allow the actual FakeModule instances to be deleted,
660 because of how Python tears down modules (it hard-sets all their
661 references to None without regard for reference counts). This method
662 must therefore make a *copy* of the given namespace, to allow the
663 original module's __dict__ to be cleared and reused.
682 664
683 def init_hooks(self):
684 # hooks holds pointers used for user-side customizations
685 self.hooks = Struct()
686
687 self.strdispatchers = {}
688 665
689 # Set all default hooks, defined in the IPython.hooks module.
690 import IPython.core.hooks
691 hooks = IPython.core.hooks
692 for hook_name in hooks.__all__:
693 # default hooks have priority 100, i.e. low; user hooks should have
694 # 0-100 priority
695 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
666 Parameters
667 ----------
668 ns : a namespace (a dict, typically)
696 669
697 def init_pushd_popd_magic(self):
698 # for pushd/popd management
699 try:
700 self.home_dir = get_home_dir()
701 except HomeDirError, msg:
702 fatal(msg)
670 fname : str
671 Filename associated with the namespace.
703 672
704 self.dir_stack = []
673 Examples
674 --------
705 675
706 def init_traceback_handlers(self, custom_exceptions):
707 # Syntax error handler.
708 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
709
710 # The interactive one is initialized with an offset, meaning we always
711 # want to remove the topmost item in the traceback, which is our own
712 # internal code. Valid modes: ['Plain','Context','Verbose']
713 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
714 color_scheme='NoColor',
715 tb_offset = 1)
676 In [10]: import IPython
716 677
717 # IPython itself shouldn't crash. This will produce a detailed
718 # post-mortem if it does. But we only install the crash handler for
719 # non-threaded shells, the threaded ones use a normal verbose reporter
720 # and lose the crash handler. This is because exceptions in the main
721 # thread (such as in GUI code) propagate directly to sys.excepthook,
722 # and there's no point in printing crash dumps for every user exception.
723 if self.isthreaded:
724 ipCrashHandler = ultratb.FormattedTB()
725 else:
726 from IPython.core import crashhandler
727 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
728 self.set_crash_handler(ipCrashHandler)
678 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
729 679
730 # and add any custom exception handlers the user may have specified
731 self.set_custom_exc(*custom_exceptions)
680 In [12]: IPython.__file__ in _ip._main_ns_cache
681 Out[12]: True
682 """
683 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
732 684
733 def init_logger(self):
734 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
735 # local shortcut, this is used a LOT
736 self.log = self.logger.log
737 # template for logfile headers. It gets resolved at runtime by the
738 # logstart method.
739 self.loghead_tpl = \
740 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
741 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
742 #log# opts = %s
743 #log# args = %s
744 #log# It is safe to make manual edits below here.
745 #log#-----------------------------------------------------------------------
746 """
685 def clear_main_mod_cache(self):
686 """Clear the cache of main modules.
747 687
748 def init_logstart(self):
749 if self.logplay:
750 self.magic_logstart(self.logplay + ' append')
751 elif self.logfile:
752 self.magic_logstart(self.logfile)
753 elif self.logstart:
754 self.magic_logstart()
688 Mainly for use by utilities like %reset.
755 689
756 def init_aliases(self):
757 # dict of things NOT to alias (keywords, builtins and some magics)
758 no_alias = {}
759 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
760 for key in keyword.kwlist + no_alias_magics:
761 no_alias[key] = 1
762 no_alias.update(__builtin__.__dict__)
763 self.no_alias = no_alias
690 Examples
691 --------
764 692
765 # Make some aliases automatically
766 # Prepare list of shell aliases to auto-define
767 if os.name == 'posix':
768 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
769 'mv mv -i','rm rm -i','cp cp -i',
770 'cat cat','less less','clear clear',
771 # a better ls
772 'ls ls -F',
773 # long ls
774 'll ls -lF')
775 # Extra ls aliases with color, which need special treatment on BSD
776 # variants
777 ls_extra = ( # color ls
778 'lc ls -F -o --color',
779 # ls normal files only
780 'lf ls -F -o --color %l | grep ^-',
781 # ls symbolic links
782 'lk ls -F -o --color %l | grep ^l',
783 # directories or links to directories,
784 'ldir ls -F -o --color %l | grep /$',
785 # things which are executable
786 'lx ls -F -o --color %l | grep ^-..x',
787 )
788 # The BSDs don't ship GNU ls, so they don't understand the
789 # --color switch out of the box
790 if 'bsd' in sys.platform:
791 ls_extra = ( # ls normal files only
792 'lf ls -lF | grep ^-',
793 # ls symbolic links
794 'lk ls -lF | grep ^l',
795 # directories or links to directories,
796 'ldir ls -lF | grep /$',
797 # things which are executable
798 'lx ls -lF | grep ^-..x',
799 )
800 auto_alias = auto_alias + ls_extra
801 elif os.name in ['nt','dos']:
802 auto_alias = ('ls dir /on',
803 'ddir dir /ad /on', 'ldir dir /ad /on',
804 'mkdir mkdir','rmdir rmdir','echo echo',
805 'ren ren','cls cls','copy copy')
806 else:
807 auto_alias = ()
808 self.auto_alias = [s.split(None,1) for s in auto_alias]
809
810 # Load default aliases
811 for alias, cmd in self.auto_alias:
812 self.define_alias(alias,cmd)
693 In [15]: import IPython
813 694
814 # Load user aliases
815 for alias in self.alias:
816 self.magic_alias(alias)
695 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
817 696
818 def init_builtins(self):
819 self.builtin_trap = BuiltinTrap(self)
697 In [17]: len(_ip._main_ns_cache) > 0
698 Out[17]: True
820 699
821 def init_shadow_hist(self):
822 try:
823 self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db")
824 except exceptions.UnicodeDecodeError:
825 print "Your ipythondir can't be decoded to unicode!"
826 print "Please set HOME environment variable to something that"
827 print r"only has ASCII characters, e.g. c:\home"
828 print "Now it is", self.config.IPYTHONDIR
829 sys.exit()
830 self.shadowhist = ipcorehist.ShadowHist(self.db)
700 In [18]: _ip.clear_main_mod_cache()
831 701
832 def init_inspector(self):
833 # Object inspector
834 self.inspector = oinspect.Inspector(oinspect.InspectColors,
835 PyColorize.ANSICodeColors,
836 'NoColor',
837 self.object_info_string_level)
702 In [19]: len(_ip._main_ns_cache) == 0
703 Out[19]: True
704 """
705 self._main_ns_cache.clear()
838 706
839 def init_readline(self):
840 """Command history completion/saving/reloading."""
707 #-------------------------------------------------------------------------
708 # Things related to debugging
709 #-------------------------------------------------------------------------
841 710
842 self.rl_next_input = None
843 self.rl_do_indent = False
711 def init_pdb(self):
712 # Set calling of pdb on exceptions
713 # self.call_pdb is a property
714 self.call_pdb = self.pdb
844 715
845 if not self.readline_use:
716 def _get_call_pdb(self):
717 return self._call_pdb
718
719 def _set_call_pdb(self,val):
720
721 if val not in (0,1,False,True):
722 raise ValueError,'new call_pdb value must be boolean'
723
724 # store value in instance
725 self._call_pdb = val
726
727 # notify the actual exception handlers
728 self.InteractiveTB.call_pdb = val
729 if self.isthreaded:
730 try:
731 self.sys_excepthook.call_pdb = val
732 except:
733 warn('Failed to activate pdb for threaded exception handler')
734
735 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
736 'Control auto-activation of pdb at exceptions')
737
738 def debugger(self,force=False):
739 """Call the pydb/pdb debugger.
740
741 Keywords:
742
743 - force(False): by default, this routine checks the instance call_pdb
744 flag and does not actually invoke the debugger if the flag is false.
745 The 'force' option forces the debugger to activate even if the flag
746 is false.
747 """
748
749 if not (force or self.call_pdb):
846 750 return
847 751
848 import IPython.utils.rlineimpl as readline
849
850 if not readline.have_readline:
851 self.has_readline = 0
852 self.readline = None
853 # no point in bugging windows users with this every time:
854 warn('Readline services not available on this platform.')
752 if not hasattr(sys,'last_traceback'):
753 error('No traceback has been produced, nothing to debug.')
754 return
755
756 # use pydb if available
757 if debugger.has_pydb:
758 from pydb import pm
855 759 else:
856 sys.modules['readline'] = readline
857 import atexit
858 from IPython.core.completer import IPCompleter
859 self.Completer = IPCompleter(self,
860 self.user_ns,
861 self.user_global_ns,
862 self.readline_omit__names,
863 self.alias_table)
864 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
865 self.strdispatchers['complete_command'] = sdisp
866 self.Completer.custom_completers = sdisp
867 # Platform-specific configuration
868 if os.name == 'nt':
869 self.readline_startup_hook = readline.set_pre_input_hook
870 else:
871 self.readline_startup_hook = readline.set_startup_hook
760 # fallback to our internal debugger
761 pm = lambda : self.InteractiveTB.debugger(force=True)
762 self.history_saving_wrapper(pm)()
872 763
873 # Load user's initrc file (readline config)
874 # Or if libedit is used, load editrc.
875 inputrc_name = os.environ.get('INPUTRC')
876 if inputrc_name is None:
877 home_dir = get_home_dir()
878 if home_dir is not None:
879 inputrc_name = '.inputrc'
880 if readline.uses_libedit:
881 inputrc_name = '.editrc'
882 inputrc_name = os.path.join(home_dir, inputrc_name)
883 if os.path.isfile(inputrc_name):
884 try:
885 readline.read_init_file(inputrc_name)
886 except:
887 warn('Problems reading readline initialization file <%s>'
888 % inputrc_name)
889
890 self.has_readline = 1
891 self.readline = readline
892 # save this in sys so embedded copies can restore it properly
893 sys.ipcompleter = self.Completer.complete
894 self.set_completer()
764 #-------------------------------------------------------------------------
765 # Things related to IPython's various namespaces
766 #-------------------------------------------------------------------------
895 767
896 # Configure readline according to user's prefs
897 # This is only done if GNU readline is being used. If libedit
898 # is being used (as on Leopard) the readline config is
899 # not run as the syntax for libedit is different.
900 if not readline.uses_libedit:
901 for rlcommand in self.readline_parse_and_bind:
902 #print "loading rl:",rlcommand # dbg
903 readline.parse_and_bind(rlcommand)
768 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
769 # Create the namespace where the user will operate. user_ns is
770 # normally the only one used, and it is passed to the exec calls as
771 # the locals argument. But we do carry a user_global_ns namespace
772 # given as the exec 'globals' argument, This is useful in embedding
773 # situations where the ipython shell opens in a context where the
774 # distinction between locals and globals is meaningful. For
775 # non-embedded contexts, it is just the same object as the user_ns dict.
904 776
905 # Remove some chars from the delimiters list. If we encounter
906 # unicode chars, discard them.
907 delims = readline.get_completer_delims().encode("ascii", "ignore")
908 delims = delims.translate(string._idmap,
909 self.readline_remove_delims)
910 readline.set_completer_delims(delims)
911 # otherwise we end up with a monster history after a while:
912 readline.set_history_length(1000)
913 try:
914 #print '*** Reading readline history' # dbg
915 readline.read_history_file(self.histfile)
916 except IOError:
917 pass # It doesn't exist yet.
777 # FIXME. For some strange reason, __builtins__ is showing up at user
778 # level as a dict instead of a module. This is a manual fix, but I
779 # should really track down where the problem is coming from. Alex
780 # Schmolck reported this problem first.
918 781
919 atexit.register(self.atexit_operations)
920 del atexit
782 # A useful post by Alex Martelli on this topic:
783 # Re: inconsistent value from __builtins__
784 # Von: Alex Martelli <aleaxit@yahoo.com>
785 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
786 # Gruppen: comp.lang.python
921 787
922 # Configure auto-indent for all platforms
923 self.set_autoindent(self.autoindent)
788 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
789 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
790 # > <type 'dict'>
791 # > >>> print type(__builtins__)
792 # > <type 'module'>
793 # > Is this difference in return value intentional?
794
795 # Well, it's documented that '__builtins__' can be either a dictionary
796 # or a module, and it's been that way for a long time. Whether it's
797 # intentional (or sensible), I don't know. In any case, the idea is
798 # that if you need to access the built-in namespace directly, you
799 # should start with "import __builtin__" (note, no 's') which will
800 # definitely give you a module. Yeah, it's somewhat confusing:-(.
801
802 # These routines return properly built dicts as needed by the rest of
803 # the code, and can also be used by extension writers to generate
804 # properly initialized namespaces.
805 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
806 user_global_ns)
807
808 # Assign namespaces
809 # This is the namespace where all normal user variables live
810 self.user_ns = user_ns
811 self.user_global_ns = user_global_ns
812
813 # An auxiliary namespace that checks what parts of the user_ns were
814 # loaded at startup, so we can list later only variables defined in
815 # actual interactive use. Since it is always a subset of user_ns, it
816 # doesn't need to be seaparately tracked in the ns_table
817 self.user_config_ns = {}
818
819 # A namespace to keep track of internal data structures to prevent
820 # them from cluttering user-visible stuff. Will be updated later
821 self.internal_ns = {}
822
823 # Namespace of system aliases. Each entry in the alias
824 # table must be a 2-tuple of the form (N,name), where N is the number
825 # of positional arguments of the alias.
826 self.alias_table = {}
827
828 # Now that FakeModule produces a real module, we've run into a nasty
829 # problem: after script execution (via %run), the module where the user
830 # code ran is deleted. Now that this object is a true module (needed
831 # so docetst and other tools work correctly), the Python module
832 # teardown mechanism runs over it, and sets to None every variable
833 # present in that module. Top-level references to objects from the
834 # script survive, because the user_ns is updated with them. However,
835 # calling functions defined in the script that use other things from
836 # the script will fail, because the function's closure had references
837 # to the original objects, which are now all None. So we must protect
838 # these modules from deletion by keeping a cache.
839 #
840 # To avoid keeping stale modules around (we only need the one from the
841 # last run), we use a dict keyed with the full path to the script, so
842 # only the last version of the module is held in the cache. Note,
843 # however, that we must cache the module *namespace contents* (their
844 # __dict__). Because if we try to cache the actual modules, old ones
845 # (uncached) could be destroyed while still holding references (such as
846 # those held by GUI objects that tend to be long-lived)>
847 #
848 # The %reset command will flush this cache. See the cache_main_mod()
849 # and clear_main_mod_cache() methods for details on use.
850
851 # This is the cache used for 'main' namespaces
852 self._main_ns_cache = {}
853 # And this is the single instance of FakeModule whose __dict__ we keep
854 # copying and clearing for reuse on each %run
855 self._user_main_module = FakeModule()
856
857 # A table holding all the namespaces IPython deals with, so that
858 # introspection facilities can search easily.
859 self.ns_table = {'user':user_ns,
860 'user_global':user_global_ns,
861 'alias':self.alias_table,
862 'internal':self.internal_ns,
863 'builtin':__builtin__.__dict__
864 }
865
866 # Similarly, track all namespaces where references can be held and that
867 # we can safely clear (so it can NOT include builtin). This one can be
868 # a simple list.
869 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
870 self.alias_table, self.internal_ns,
871 self._main_ns_cache ]
872
873 def init_sys_modules(self):
874 # We need to insert into sys.modules something that looks like a
875 # module but which accesses the IPython namespace, for shelve and
876 # pickle to work interactively. Normally they rely on getting
877 # everything out of __main__, but for embedding purposes each IPython
878 # instance has its own private namespace, so we can't go shoving
879 # everything into __main__.
880
881 # note, however, that we should only do this for non-embedded
882 # ipythons, which really mimic the __main__.__dict__ with their own
883 # namespace. Embedded instances, on the other hand, should not do
884 # this because they need to manage the user local/global namespaces
885 # only, but they live within a 'normal' __main__ (meaning, they
886 # shouldn't overtake the execution environment of the script they're
887 # embedded in).
924 888
925 def init_prompts(self):
926 # Initialize cache, set in/out prompts and printing system
927 self.outputcache = CachedOutput(self,
928 self.cache_size,
929 self.pprint,
930 input_sep = self.separate_in,
931 output_sep = self.separate_out,
932 output_sep2 = self.separate_out2,
933 ps1 = self.prompt_in1,
934 ps2 = self.prompt_in2,
935 ps_out = self.prompt_out,
936 pad_left = self.prompts_pad_left)
889 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
937 890
938 # user may have over-ridden the default print hook:
939 891 try:
940 self.outputcache.__class__.display = self.hooks.display
941 except AttributeError:
942 pass
892 main_name = self.user_ns['__name__']
893 except KeyError:
894 raise KeyError('user_ns dictionary MUST have a "__name__" key')
895 else:
896 sys.modules[main_name] = FakeModule(self.user_ns)
943 897
944 def init_displayhook(self):
945 self.display_trap = DisplayTrap(self, self.outputcache)
898 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
899 """Return a valid local and global user interactive namespaces.
946 900
947 def init_reload_doctest(self):
948 # Do a proper resetting of doctest, including the necessary displayhook
949 # monkeypatching
950 try:
951 doctest_reload()
952 except ImportError:
953 warn("doctest module does not exist.")
901 This builds a dict with the minimal information needed to operate as a
902 valid IPython user namespace, which you can pass to the various
903 embedding classes in ipython. The default implementation returns the
904 same dict for both the locals and the globals to allow functions to
905 refer to variables in the namespace. Customized implementations can
906 return different dicts. The locals dictionary can actually be anything
907 following the basic mapping protocol of a dict, but the globals dict
908 must be a true dict, not even a subclass. It is recommended that any
909 custom object for the locals namespace synchronize with the globals
910 dict somehow.
954 911
955 def init_magics(self):
956 # Set user colors (don't do it in the constructor above so that it
957 # doesn't crash if colors option is invalid)
958 self.magic_colors(self.colors)
912 Raises TypeError if the provided globals namespace is not a true dict.
959 913
960 def init_pdb(self):
961 # Set calling of pdb on exceptions
962 # self.call_pdb is a property
963 self.call_pdb = self.pdb
914 :Parameters:
915 user_ns : dict-like, optional
916 The current user namespace. The items in this namespace should
917 be included in the output. If None, an appropriate blank
918 namespace should be created.
919 user_global_ns : dict, optional
920 The current user global namespace. The items in this namespace
921 should be included in the output. If None, an appropriate
922 blank namespace should be created.
964 923
965 # def init_exec_commands(self):
966 # for cmd in self.config.EXECUTE:
967 # print "execute:", cmd
968 # self.api.runlines(cmd)
969 #
970 # batchrun = False
971 # if self.config.has_key('EXECFILE'):
972 # for batchfile in [path(arg) for arg in self.config.EXECFILE
973 # if arg.lower().endswith('.ipy')]:
974 # if not batchfile.isfile():
975 # print "No such batch file:", batchfile
976 # continue
977 # self.api.runlines(batchfile.text())
978 # batchrun = True
979 # # without -i option, exit after running the batch file
980 # if batchrun and not self.interactive:
981 # self.ask_exit()
924 :Returns:
925 A tuple pair of dictionary-like object to be used as the local namespace
926 of the interpreter and a dict to be used as the global namespace.
927 """
982 928
983 # def load(self, mod):
984 # """ Load an extension.
985 #
986 # Some modules should (or must) be 'load()':ed, rather than just imported.
987 #
988 # Loading will do:
989 #
990 # - run init_ipython(ip)
991 # - run ipython_firstrun(ip)
992 # """
993 #
994 # if mod in self.extensions:
995 # # just to make sure we don't init it twice
996 # # note that if you 'load' a module that has already been
997 # # imported, init_ipython gets run anyway
998 #
999 # return self.extensions[mod]
1000 # __import__(mod)
1001 # m = sys.modules[mod]
1002 # if hasattr(m,'init_ipython'):
1003 # m.init_ipython(self)
1004 #
1005 # if hasattr(m,'ipython_firstrun'):
1006 # already_loaded = self.db.get('firstrun_done', set())
1007 # if mod not in already_loaded:
1008 # m.ipython_firstrun(self)
1009 # already_loaded.add(mod)
1010 # self.db['firstrun_done'] = already_loaded
1011 #
1012 # self.extensions[mod] = m
1013 # return m
929 if user_ns is None:
930 # Set __name__ to __main__ to better match the behavior of the
931 # normal interpreter.
932 user_ns = {'__name__' :'__main__',
933 '__builtins__' : __builtin__,
934 }
935 else:
936 user_ns.setdefault('__name__','__main__')
937 user_ns.setdefault('__builtins__',__builtin__)
938
939 if user_global_ns is None:
940 user_global_ns = user_ns
941 if type(user_global_ns) is not dict:
942 raise TypeError("user_global_ns must be a true dict; got %r"
943 % type(user_global_ns))
944
945 return user_ns, user_global_ns
1014 946
1015 947 def init_user_ns(self):
1016 948 """Initialize all user-visible namespaces to their minimum defaults.
@@ -1048,76 +980,183 b' class InteractiveShell(Component, Magic):'
1048 980 except ImportError:
1049 981 warn('help() not available - check site.py')
1050 982
1051 def save_sys_module_state(self):
1052 """Save the state of hooks in the sys module.
983 def reset(self):
984 """Clear all internal namespaces.
985
986 Note that this is much more aggressive than %reset, since it clears
987 fully all namespaces, as well as all input/output lists.
988 """
989 for ns in self.ns_refs_table:
990 ns.clear()
991
992 # Clear input and output histories
993 self.input_hist[:] = []
994 self.input_hist_raw[:] = []
995 self.output_hist.clear()
996 # Restore the user namespaces to minimal usability
997 self.init_user_ns()
998
999 def push(self, variables, interactive=True):
1000 """Inject a group of variables into the IPython user namespace.
1001
1002 Parameters
1003 ----------
1004 variables : dict, str or list/tuple of str
1005 The variables to inject into the user's namespace. If a dict,
1006 a simple update is done. If a str, the string is assumed to
1007 have variable names separated by spaces. A list/tuple of str
1008 can also be used to give the variable names. If just the variable
1009 names are give (list/tuple/str) then the variable values looked
1010 up in the callers frame.
1011 interactive : bool
1012 If True (default), the variables will be listed with the ``who``
1013 magic.
1014 """
1015 vdict = None
1016
1017 # We need a dict of name/value pairs to do namespace updates.
1018 if isinstance(variables, dict):
1019 vdict = variables
1020 elif isinstance(variables, (basestring, list, tuple)):
1021 if isinstance(variables, basestring):
1022 vlist = variables.split()
1023 else:
1024 vlist = variables
1025 vdict = {}
1026 cf = sys._getframe(1)
1027 for name in vlist:
1028 try:
1029 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1030 except:
1031 print ('Could not get variable %s from %s' %
1032 (name,cf.f_code.co_name))
1033 else:
1034 raise ValueError('variables must be a dict/str/list/tuple')
1035
1036 # Propagate variables to user namespace
1037 self.user_ns.update(vdict)
1038
1039 # And configure interactive visibility
1040 config_ns = self.user_config_ns
1041 if interactive:
1042 for name, val in vdict.iteritems():
1043 config_ns.pop(name, None)
1044 else:
1045 for name,val in vdict.iteritems():
1046 config_ns[name] = val
1047
1048 #-------------------------------------------------------------------------
1049 # Things related to history management
1050 #-------------------------------------------------------------------------
1051
1052 def init_history(self):
1053 # List of input with multi-line handling.
1054 self.input_hist = InputList()
1055 # This one will hold the 'raw' input history, without any
1056 # pre-processing. This will allow users to retrieve the input just as
1057 # it was exactly typed in by the user, with %hist -r.
1058 self.input_hist_raw = InputList()
1059
1060 # list of visited directories
1061 try:
1062 self.dir_hist = [os.getcwd()]
1063 except OSError:
1064 self.dir_hist = []
1065
1066 # dict of output history
1067 self.output_hist = {}
1068
1069 # Now the history file
1070 try:
1071 histfname = 'history-%s' % self.profile
1072 except AttributeError:
1073 histfname = 'history'
1074 self.histfile = os.path.join(self.config.IPYTHONDIR, histfname)
1075
1076 # Fill the history zero entry, user counter starts at 1
1077 self.input_hist.append('\n')
1078 self.input_hist_raw.append('\n')
1079
1080 def init_shadow_hist(self):
1081 try:
1082 self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db")
1083 except exceptions.UnicodeDecodeError:
1084 print "Your ipythondir can't be decoded to unicode!"
1085 print "Please set HOME environment variable to something that"
1086 print r"only has ASCII characters, e.g. c:\home"
1087 print "Now it is", self.config.IPYTHONDIR
1088 sys.exit()
1089 self.shadowhist = ipcorehist.ShadowHist(self.db)
1090
1091 def savehist(self):
1092 """Save input history to a file (via readline library)."""
1093
1094 if not self.has_readline:
1095 return
1096
1097 try:
1098 self.readline.write_history_file(self.histfile)
1099 except:
1100 print 'Unable to save IPython command history to file: ' + \
1101 `self.histfile`
1102
1103 def reloadhist(self):
1104 """Reload the input history from disk file."""
1105
1106 if self.has_readline:
1107 try:
1108 self.readline.clear_history()
1109 self.readline.read_history_file(self.shell.histfile)
1110 except AttributeError:
1111 pass
1112
1113 def history_saving_wrapper(self, func):
1114 """ Wrap func for readline history saving
1053 1115
1054 This has to be called after self.user_ns is created.
1055 """
1056 self._orig_sys_module_state = {}
1057 self._orig_sys_module_state['stdin'] = sys.stdin
1058 self._orig_sys_module_state['stdout'] = sys.stdout
1059 self._orig_sys_module_state['stderr'] = sys.stderr
1060 self._orig_sys_module_state['excepthook'] = sys.excepthook
1061 try:
1062 self._orig_sys_modules_main_name = self.user_ns['__name__']
1063 except KeyError:
1064 pass
1116 Convert func into callable that saves & restores
1117 history around the call """
1065 1118
1066 def restore_sys_module_state(self):
1067 """Restore the state of the sys module."""
1068 try:
1069 for k, v in self._orig_sys_module_state.items():
1070 setattr(sys, k, v)
1071 except AttributeError:
1072 pass
1073 try:
1074 delattr(sys, 'ipcompleter')
1075 except AttributeError:
1076 pass
1077 # Reset what what done in self.init_sys_modules
1078 try:
1079 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
1080 except (AttributeError, KeyError):
1081 pass
1119 if not self.has_readline:
1120 return func
1082 1121
1083 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
1084 """set_hook(name,hook) -> sets an internal IPython hook.
1122 def wrapper():
1123 self.savehist()
1124 try:
1125 func()
1126 finally:
1127 readline.read_history_file(self.histfile)
1128 return wrapper
1085 1129
1086 IPython exposes some of its internal API as user-modifiable hooks. By
1087 adding your function to one of these hooks, you can modify IPython's
1088 behavior to call at runtime your own routines."""
1130 #-------------------------------------------------------------------------
1131 # Things related to exception handling and tracebacks (not debugging)
1132 #-------------------------------------------------------------------------
1089 1133
1090 # At some point in the future, this should validate the hook before it
1091 # accepts it. Probably at least check that the hook takes the number
1092 # of args it's supposed to.
1134 def init_traceback_handlers(self, custom_exceptions):
1135 # Syntax error handler.
1136 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1093 1137
1094 f = new.instancemethod(hook,self,self.__class__)
1138 # The interactive one is initialized with an offset, meaning we always
1139 # want to remove the topmost item in the traceback, which is our own
1140 # internal code. Valid modes: ['Plain','Context','Verbose']
1141 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1142 color_scheme='NoColor',
1143 tb_offset = 1)
1095 1144
1096 # check if the hook is for strdispatcher first
1097 if str_key is not None:
1098 sdp = self.strdispatchers.get(name, StrDispatch())
1099 sdp.add_s(str_key, f, priority )
1100 self.strdispatchers[name] = sdp
1101 return
1102 if re_key is not None:
1103 sdp = self.strdispatchers.get(name, StrDispatch())
1104 sdp.add_re(re.compile(re_key), f, priority )
1105 self.strdispatchers[name] = sdp
1106 return
1107
1108 dp = getattr(self.hooks, name, None)
1109 if name not in IPython.core.hooks.__all__:
1110 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
1111 if not dp:
1112 dp = IPython.core.hooks.CommandChainDispatcher()
1113
1114 try:
1115 dp.add(f,priority)
1116 except AttributeError:
1117 # it was not commandchain, plain old func - replace
1118 dp = f
1145 # IPython itself shouldn't crash. This will produce a detailed
1146 # post-mortem if it does. But we only install the crash handler for
1147 # non-threaded shells, the threaded ones use a normal verbose reporter
1148 # and lose the crash handler. This is because exceptions in the main
1149 # thread (such as in GUI code) propagate directly to sys.excepthook,
1150 # and there's no point in printing crash dumps for every user exception.
1151 if self.isthreaded:
1152 ipCrashHandler = ultratb.FormattedTB()
1153 else:
1154 from IPython.core import crashhandler
1155 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1156 self.set_crash_handler(ipCrashHandler)
1119 1157
1120 setattr(self.hooks,name, dp)
1158 # and add any custom exception handlers the user may have specified
1159 self.set_custom_exc(*custom_exceptions)
1121 1160
1122 1161 def set_crash_handler(self, crashHandler):
1123 1162 """Set the IPython crash handler.
@@ -1177,192 +1216,169 b' class InteractiveShell(Component, Magic):'
1177 1216 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1178 1217 self.custom_exceptions = exc_tuple
1179 1218
1180 def set_custom_completer(self,completer,pos=0):
1181 """set_custom_completer(completer,pos=0)
1182
1183 Adds a new custom completer function.
1184
1185 The position argument (defaults to 0) is the index in the completers
1186 list where you want the completer to be inserted."""
1187
1188 newcomp = new.instancemethod(completer,self.Completer,
1189 self.Completer.__class__)
1190 self.Completer.matchers.insert(pos,newcomp)
1191
1192 def set_completer(self):
1193 """reset readline's completer to be our own."""
1194 self.readline.set_completer(self.Completer.complete)
1195
1196 def _get_call_pdb(self):
1197 return self._call_pdb
1198
1199 def _set_call_pdb(self,val):
1200
1201 if val not in (0,1,False,True):
1202 raise ValueError,'new call_pdb value must be boolean'
1203
1204 # store value in instance
1205 self._call_pdb = val
1206
1207 # notify the actual exception handlers
1208 self.InteractiveTB.call_pdb = val
1209 if self.isthreaded:
1210 try:
1211 self.sys_excepthook.call_pdb = val
1212 except:
1213 warn('Failed to activate pdb for threaded exception handler')
1214
1215 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
1216 'Control auto-activation of pdb at exceptions')
1217
1218 def magic(self,arg_s):
1219 """Call a magic function by name.
1219 def excepthook(self, etype, value, tb):
1220 """One more defense for GUI apps that call sys.excepthook.
1220 1221
1221 Input: a string containing the name of the magic function to call and any
1222 additional arguments to be passed to the magic.
1222 GUI frameworks like wxPython trap exceptions and call
1223 sys.excepthook themselves. I guess this is a feature that
1224 enables them to keep running after exceptions that would
1225 otherwise kill their mainloop. This is a bother for IPython
1226 which excepts to catch all of the program exceptions with a try:
1227 except: statement.
1223 1228
1224 magic('name -opt foo bar') is equivalent to typing at the ipython
1225 prompt:
1229 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1230 any app directly invokes sys.excepthook, it will look to the user like
1231 IPython crashed. In order to work around this, we can disable the
1232 CrashHandler and replace it with this excepthook instead, which prints a
1233 regular traceback using our InteractiveTB. In this fashion, apps which
1234 call sys.excepthook will generate a regular-looking exception from
1235 IPython, and the CrashHandler will only be triggered by real IPython
1236 crashes.
1226 1237
1227 In[1]: %name -opt foo bar
1238 This hook should be used sparingly, only in places which are not likely
1239 to be true IPython errors.
1240 """
1241 self.showtraceback((etype,value,tb),tb_offset=0)
1228 1242
1229 To call a magic without arguments, simply use magic('name').
1243 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1244 """Display the exception that just occurred.
1230 1245
1231 This provides a proper Python function to call IPython's magics in any
1232 valid Python code you can type at the interpreter, including loops and
1233 compound statements.
1234 """
1246 If nothing is known about the exception, this is the method which
1247 should be used throughout the code for presenting user tracebacks,
1248 rather than directly invoking the InteractiveTB object.
1235 1249
1236 args = arg_s.split(' ',1)
1237 magic_name = args[0]
1238 magic_name = magic_name.lstrip(self.ESC_MAGIC)
1250 A specific showsyntaxerror() also exists, but this method can take
1251 care of calling it if needed, so unless you are explicitly catching a
1252 SyntaxError exception, don't try to analyze the stack manually and
1253 simply call this method."""
1239 1254
1255
1256 # Though this won't be called by syntax errors in the input line,
1257 # there may be SyntaxError cases whith imported code.
1258
1240 1259 try:
1241 magic_args = args[1]
1242 except IndexError:
1243 magic_args = ''
1244 fn = getattr(self,'magic_'+magic_name,None)
1245 if fn is None:
1246 error("Magic function `%s` not found." % magic_name)
1247 else:
1248 magic_args = self.var_expand(magic_args,1)
1249 with nested(self.builtin_trap, self.display_trap):
1250 return fn(magic_args)
1251 # return result
1252
1253 def define_magic(self, magicname, func):
1254 """Expose own function as magic function for ipython
1260 if exc_tuple is None:
1261 etype, value, tb = sys.exc_info()
1262 else:
1263 etype, value, tb = exc_tuple
1255 1264
1256 def foo_impl(self,parameter_s=''):
1257 'My very own magic!. (Use docstrings, IPython reads them).'
1258 print 'Magic function. Passed parameter is between < >:'
1259 print '<%s>' % parameter_s
1260 print 'The self object is:',self
1265 if etype is SyntaxError:
1266 self.showsyntaxerror(filename)
1267 elif etype is UsageError:
1268 print "UsageError:", value
1269 else:
1270 # WARNING: these variables are somewhat deprecated and not
1271 # necessarily safe to use in a threaded environment, but tools
1272 # like pdb depend on their existence, so let's set them. If we
1273 # find problems in the field, we'll need to revisit their use.
1274 sys.last_type = etype
1275 sys.last_value = value
1276 sys.last_traceback = tb
1261 1277
1262 self.define_magic('foo',foo_impl)
1263 """
1264
1265 import new
1266 im = new.instancemethod(func,self, self.__class__)
1267 old = getattr(self, "magic_" + magicname, None)
1268 setattr(self, "magic_" + magicname, im)
1269 return old
1270
1271 def define_macro(self, name, themacro):
1272 """Define a new macro
1273
1274 Parameters
1275 ----------
1276 name : str
1277 The name of the macro.
1278 themacro : str or Macro
1279 The action to do upon invoking the macro. If a string, a new
1280 Macro object is created by passing the string to it.
1281 """
1282
1283 from IPython.core import macro
1278 if etype in self.custom_exceptions:
1279 self.CustomTB(etype,value,tb)
1280 else:
1281 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1282 if self.InteractiveTB.call_pdb and self.has_readline:
1283 # pdb mucks up readline, fix it back
1284 self.set_completer()
1285 except KeyboardInterrupt:
1286 self.write("\nKeyboardInterrupt\n")
1284 1287
1285 if isinstance(themacro, basestring):
1286 themacro = macro.Macro(themacro)
1287 if not isinstance(themacro, macro.Macro):
1288 raise ValueError('A macro must be a string or a Macro instance.')
1289 self.user_ns[name] = themacro
1288 def showsyntaxerror(self, filename=None):
1289 """Display the syntax error that just occurred.
1290 1290
1291 def define_alias(self, name, cmd):
1292 """ Define a new alias."""
1291 This doesn't display a stack trace because there isn't one.
1293 1292
1294 if callable(cmd):
1295 self.alias_table[name] = cmd
1296 from IPython.core import shadowns
1297 setattr(shadowns, name, cmd)
1298 return
1293 If a filename is given, it is stuffed in the exception instead
1294 of what was there before (because Python's parser always uses
1295 "<string>" when reading from a string).
1296 """
1297 etype, value, last_traceback = sys.exc_info()
1299 1298
1300 if isinstance(cmd, basestring):
1301 nargs = cmd.count('%s')
1302 if nargs>0 and cmd.find('%l')>=0:
1303 raise Exception('The %s and %l specifiers are mutually '
1304 'exclusive in alias definitions.')
1305
1306 self.alias_table[name] = (nargs,cmd)
1307 return
1299 # See note about these variables in showtraceback() below
1300 sys.last_type = etype
1301 sys.last_value = value
1302 sys.last_traceback = last_traceback
1308 1303
1309 self.alias_table[name] = cmd
1310
1311 def ipalias(self,arg_s):
1312 """Call an alias by name.
1313
1314 Input: a string containing the name of the alias to call and any
1315 additional arguments to be passed to the magic.
1304 if filename and etype is SyntaxError:
1305 # Work hard to stuff the correct filename in the exception
1306 try:
1307 msg, (dummy_filename, lineno, offset, line) = value
1308 except:
1309 # Not the format we expect; leave it alone
1310 pass
1311 else:
1312 # Stuff in the right filename
1313 try:
1314 # Assume SyntaxError is a class exception
1315 value = SyntaxError(msg, (filename, lineno, offset, line))
1316 except:
1317 # If that failed, assume SyntaxError is a string
1318 value = msg, (filename, lineno, offset, line)
1319 self.SyntaxTB(etype,value,[])
1316 1320
1317 ipalias('name -opt foo bar') is equivalent to typing at the ipython
1318 prompt:
1321 def edit_syntax_error(self):
1322 """The bottom half of the syntax error handler called in the main loop.
1319 1323
1320 In[1]: name -opt foo bar
1324 Loop until syntax error is fixed or user cancels.
1325 """
1321 1326
1322 To call an alias without arguments, simply use ipalias('name').
1327 while self.SyntaxTB.last_syntax_error:
1328 # copy and clear last_syntax_error
1329 err = self.SyntaxTB.clear_err_state()
1330 if not self._should_recompile(err):
1331 return
1332 try:
1333 # may set last_syntax_error again if a SyntaxError is raised
1334 self.safe_execfile(err.filename,self.user_ns)
1335 except:
1336 self.showtraceback()
1337 else:
1338 try:
1339 f = file(err.filename)
1340 try:
1341 # This should be inside a display_trap block and I
1342 # think it is.
1343 sys.displayhook(f.read())
1344 finally:
1345 f.close()
1346 except:
1347 self.showtraceback()
1323 1348
1324 This provides a proper Python function to call IPython's aliases in any
1325 valid Python code you can type at the interpreter, including loops and
1326 compound statements. It is added by IPython to the Python builtin
1327 namespace upon initialization."""
1349 def _should_recompile(self,e):
1350 """Utility routine for edit_syntax_error"""
1328 1351
1329 args = arg_s.split(' ',1)
1330 alias_name = args[0]
1352 if e.filename in ('<ipython console>','<input>','<string>',
1353 '<console>','<BackgroundJob compilation>',
1354 None):
1355
1356 return False
1331 1357 try:
1332 alias_args = args[1]
1333 except IndexError:
1334 alias_args = ''
1335 if alias_name in self.alias_table:
1336 self.call_alias(alias_name,alias_args)
1337 else:
1338 error("Alias `%s` not found." % alias_name)
1339
1340 def system(self, cmd):
1341 """Make a system call, using IPython."""
1342 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1343
1344 def ex(self, cmd):
1345 """Execute a normal python statement in user namespace."""
1346 with nested(self.builtin_trap, self.display_trap):
1347 exec cmd in self.user_global_ns, self.user_ns
1348
1349 def ev(self, expr):
1350 """Evaluate python expression expr in user namespace.
1351
1352 Returns the result of evaluation
1353 """
1354 with nested(self.builtin_trap, self.display_trap):
1355 return eval(expr, self.user_global_ns, self.user_ns)
1358 if (self.autoedit_syntax and
1359 not self.ask_yes_no('Return to editor to correct syntax error? '
1360 '[Y/n] ','y')):
1361 return False
1362 except EOFError:
1363 return False
1356 1364
1357 def getoutput(self, cmd):
1358 return getoutput(self.var_expand(cmd,depth=2),
1359 header=self.system_header,
1360 verbose=self.system_verbose)
1365 def int0(x):
1366 try:
1367 return int(x)
1368 except TypeError:
1369 return 0
1370 # always pass integer line and offset values to editor hook
1371 try:
1372 self.hooks.fix_error_editor(e.filename,
1373 int0(e.lineno),int0(e.offset),e.msg)
1374 except TryNext:
1375 warn('Could not open editor')
1376 return False
1377 return True
1361 1378
1362 def getoutputerror(self, cmd):
1363 return getoutputerror(self.var_expand(cmd,depth=2),
1364 header=self.system_header,
1365 verbose=self.system_verbose)
1379 #-------------------------------------------------------------------------
1380 # Things related to tab completion
1381 #-------------------------------------------------------------------------
1366 1382
1367 1383 def complete(self, text):
1368 1384 """Return a sorted list of all possible completions on text.
@@ -1409,35 +1425,112 b' class InteractiveShell(Component, Magic):'
1409 1425 #print "T:",text,"OC:",outcomps # dbg
1410 1426 #print "vars:",self.user_ns.keys()
1411 1427 return outcomps
1412
1413 def set_completer_frame(self, frame=None):
1414 if frame:
1415 self.Completer.namespace = frame.f_locals
1416 self.Completer.global_namespace = frame.f_globals
1417 else:
1418 self.Completer.namespace = self.user_ns
1419 self.Completer.global_namespace = self.user_global_ns
1420 1428
1421 def init_auto_alias(self):
1422 """Define some aliases automatically.
1429 def set_custom_completer(self,completer,pos=0):
1430 """set_custom_completer(completer,pos=0)
1423 1431
1424 These are ALL parameter-less aliases"""
1432 Adds a new custom completer function.
1425 1433
1426 for alias,cmd in self.auto_alias:
1427 self.define_alias(alias,cmd)
1434 The position argument (defaults to 0) is the index in the completers
1435 list where you want the completer to be inserted."""
1428 1436
1429 def alias_table_validate(self,verbose=0):
1430 """Update information about the alias table.
1437 newcomp = new.instancemethod(completer,self.Completer,
1438 self.Completer.__class__)
1439 self.Completer.matchers.insert(pos,newcomp)
1431 1440
1432 In particular, make sure no Python keywords/builtins are in it."""
1441 def set_completer(self):
1442 """reset readline's completer to be our own."""
1443 self.readline.set_completer(self.Completer.complete)
1444
1445 #-------------------------------------------------------------------------
1446 # Things related to readline
1447 #-------------------------------------------------------------------------
1448
1449 def init_readline(self):
1450 """Command history completion/saving/reloading."""
1451
1452 self.rl_next_input = None
1453 self.rl_do_indent = False
1454
1455 if not self.readline_use:
1456 return
1457
1458 import IPython.utils.rlineimpl as readline
1459
1460 if not readline.have_readline:
1461 self.has_readline = 0
1462 self.readline = None
1463 # no point in bugging windows users with this every time:
1464 warn('Readline services not available on this platform.')
1465 else:
1466 sys.modules['readline'] = readline
1467 import atexit
1468 from IPython.core.completer import IPCompleter
1469 self.Completer = IPCompleter(self,
1470 self.user_ns,
1471 self.user_global_ns,
1472 self.readline_omit__names,
1473 self.alias_table)
1474 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1475 self.strdispatchers['complete_command'] = sdisp
1476 self.Completer.custom_completers = sdisp
1477 # Platform-specific configuration
1478 if os.name == 'nt':
1479 self.readline_startup_hook = readline.set_pre_input_hook
1480 else:
1481 self.readline_startup_hook = readline.set_startup_hook
1482
1483 # Load user's initrc file (readline config)
1484 # Or if libedit is used, load editrc.
1485 inputrc_name = os.environ.get('INPUTRC')
1486 if inputrc_name is None:
1487 home_dir = get_home_dir()
1488 if home_dir is not None:
1489 inputrc_name = '.inputrc'
1490 if readline.uses_libedit:
1491 inputrc_name = '.editrc'
1492 inputrc_name = os.path.join(home_dir, inputrc_name)
1493 if os.path.isfile(inputrc_name):
1494 try:
1495 readline.read_init_file(inputrc_name)
1496 except:
1497 warn('Problems reading readline initialization file <%s>'
1498 % inputrc_name)
1499
1500 self.has_readline = 1
1501 self.readline = readline
1502 # save this in sys so embedded copies can restore it properly
1503 sys.ipcompleter = self.Completer.complete
1504 self.set_completer()
1505
1506 # Configure readline according to user's prefs
1507 # This is only done if GNU readline is being used. If libedit
1508 # is being used (as on Leopard) the readline config is
1509 # not run as the syntax for libedit is different.
1510 if not readline.uses_libedit:
1511 for rlcommand in self.readline_parse_and_bind:
1512 #print "loading rl:",rlcommand # dbg
1513 readline.parse_and_bind(rlcommand)
1514
1515 # Remove some chars from the delimiters list. If we encounter
1516 # unicode chars, discard them.
1517 delims = readline.get_completer_delims().encode("ascii", "ignore")
1518 delims = delims.translate(string._idmap,
1519 self.readline_remove_delims)
1520 readline.set_completer_delims(delims)
1521 # otherwise we end up with a monster history after a while:
1522 readline.set_history_length(1000)
1523 try:
1524 #print '*** Reading readline history' # dbg
1525 readline.read_history_file(self.histfile)
1526 except IOError:
1527 pass # It doesn't exist yet.
1528
1529 atexit.register(self.atexit_operations)
1530 del atexit
1433 1531
1434 no_alias = self.no_alias
1435 for k in self.alias_table.keys():
1436 if k in no_alias:
1437 del self.alias_table[k]
1438 if verbose:
1439 print ("Deleting alias <%s>, it's a Python "
1440 "keyword or builtin." % k)
1532 # Configure auto-indent for all platforms
1533 self.set_autoindent(self.autoindent)
1441 1534
1442 1535 def set_next_input(self, s):
1443 1536 """ Sets the 'default' input string for the next command line.
@@ -1452,348 +1545,369 b' class InteractiveShell(Component, Magic):'
1452 1545
1453 1546 self.rl_next_input = s
1454 1547
1455 def set_autoindent(self,value=None):
1456 """Set the autoindent flag, checking for readline support.
1548 def pre_readline(self):
1549 """readline hook to be used at the start of each line.
1457 1550
1458 If called with no arguments, it acts as a toggle."""
1551 Currently it handles auto-indent only."""
1459 1552
1460 if not self.has_readline:
1461 if os.name == 'posix':
1462 warn("The auto-indent feature requires the readline library")
1463 self.autoindent = 0
1464 return
1465 if value is None:
1466 self.autoindent = not self.autoindent
1467 else:
1468 self.autoindent = value
1553 #debugx('self.indent_current_nsp','pre_readline:')
1469 1554
1470 def atexit_operations(self):
1471 """This will be executed at the time of exit.
1555 if self.rl_do_indent:
1556 self.readline.insert_text(self._indent_current_str())
1557 if self.rl_next_input is not None:
1558 self.readline.insert_text(self.rl_next_input)
1559 self.rl_next_input = None
1472 1560
1473 Saving of persistent data should be performed here. """
1561 def _indent_current_str(self):
1562 """return the current level of indentation as a string"""
1563 return self.indent_current_nsp * ' '
1474 1564
1475 #print '*** IPython exit cleanup ***' # dbg
1476 # input history
1477 self.savehist()
1565 #-------------------------------------------------------------------------
1566 # Things related to magics
1567 #-------------------------------------------------------------------------
1478 1568
1479 # Cleanup all tempfiles left around
1480 for tfile in self.tempfiles:
1481 try:
1482 os.unlink(tfile)
1483 except OSError:
1484 pass
1569 def init_magics(self):
1570 # Set user colors (don't do it in the constructor above so that it
1571 # doesn't crash if colors option is invalid)
1572 self.magic_colors(self.colors)
1485 1573
1486 # Clear all user namespaces to release all references cleanly.
1487 self.reset()
1574 def magic(self,arg_s):
1575 """Call a magic function by name.
1488 1576
1489 # Run user hooks
1490 self.hooks.shutdown_hook()
1577 Input: a string containing the name of the magic function to call and any
1578 additional arguments to be passed to the magic.
1491 1579
1492 def reset(self):
1493 """Clear all internal namespaces.
1580 magic('name -opt foo bar') is equivalent to typing at the ipython
1581 prompt:
1494 1582
1495 Note that this is much more aggressive than %reset, since it clears
1496 fully all namespaces, as well as all input/output lists.
1497 """
1498 for ns in self.ns_refs_table:
1499 ns.clear()
1583 In[1]: %name -opt foo bar
1500 1584
1501 # Clear input and output histories
1502 self.input_hist[:] = []
1503 self.input_hist_raw[:] = []
1504 self.output_hist.clear()
1505 # Restore the user namespaces to minimal usability
1506 self.init_user_ns()
1507
1508 def savehist(self):
1509 """Save input history to a file (via readline library)."""
1585 To call a magic without arguments, simply use magic('name').
1510 1586
1511 if not self.has_readline:
1512 return
1513
1514 try:
1515 self.readline.write_history_file(self.histfile)
1516 except:
1517 print 'Unable to save IPython command history to file: ' + \
1518 `self.histfile`
1587 This provides a proper Python function to call IPython's magics in any
1588 valid Python code you can type at the interpreter, including loops and
1589 compound statements.
1590 """
1519 1591
1520 def reloadhist(self):
1521 """Reload the input history from disk file."""
1592 args = arg_s.split(' ',1)
1593 magic_name = args[0]
1594 magic_name = magic_name.lstrip(self.ESC_MAGIC)
1522 1595
1523 if self.has_readline:
1524 try:
1525 self.readline.clear_history()
1526 self.readline.read_history_file(self.shell.histfile)
1527 except AttributeError:
1528 pass
1529
1596 try:
1597 magic_args = args[1]
1598 except IndexError:
1599 magic_args = ''
1600 fn = getattr(self,'magic_'+magic_name,None)
1601 if fn is None:
1602 error("Magic function `%s` not found." % magic_name)
1603 else:
1604 magic_args = self.var_expand(magic_args,1)
1605 with nested(self.builtin_trap, self.display_trap):
1606 return fn(magic_args)
1607 # return result
1530 1608
1531 def history_saving_wrapper(self, func):
1532 """ Wrap func for readline history saving
1533
1534 Convert func into callable that saves & restores
1535 history around the call """
1536
1537 if not self.has_readline:
1538 return func
1609 def define_magic(self, magicname, func):
1610 """Expose own function as magic function for ipython
1611
1612 def foo_impl(self,parameter_s=''):
1613 'My very own magic!. (Use docstrings, IPython reads them).'
1614 print 'Magic function. Passed parameter is between < >:'
1615 print '<%s>' % parameter_s
1616 print 'The self object is:',self
1617
1618 self.define_magic('foo',foo_impl)
1619 """
1539 1620
1540 def wrapper():
1541 self.savehist()
1542 try:
1543 func()
1544 finally:
1545 readline.read_history_file(self.histfile)
1546 return wrapper
1547
1548 def pre_readline(self):
1549 """readline hook to be used at the start of each line.
1550
1551 Currently it handles auto-indent only."""
1552
1553 #debugx('self.indent_current_nsp','pre_readline:')
1621 import new
1622 im = new.instancemethod(func,self, self.__class__)
1623 old = getattr(self, "magic_" + magicname, None)
1624 setattr(self, "magic_" + magicname, im)
1625 return old
1554 1626
1555 if self.rl_do_indent:
1556 self.readline.insert_text(self.indent_current_str())
1557 if self.rl_next_input is not None:
1558 self.readline.insert_text(self.rl_next_input)
1559 self.rl_next_input = None
1627 #-------------------------------------------------------------------------
1628 # Things related to macros
1629 #-------------------------------------------------------------------------
1560 1630
1561 def ask_yes_no(self,prompt,default=True):
1562 if self.quiet:
1563 return True
1564 return ask_yes_no(prompt,default)
1631 def define_macro(self, name, themacro):
1632 """Define a new macro
1565 1633
1566 def new_main_mod(self,ns=None):
1567 """Return a new 'main' module object for user code execution.
1634 Parameters
1635 ----------
1636 name : str
1637 The name of the macro.
1638 themacro : str or Macro
1639 The action to do upon invoking the macro. If a string, a new
1640 Macro object is created by passing the string to it.
1568 1641 """
1569 main_mod = self._user_main_module
1570 init_fakemod_dict(main_mod,ns)
1571 return main_mod
1642
1643 from IPython.core import macro
1572 1644
1573 def cache_main_mod(self,ns,fname):
1574 """Cache a main module's namespace.
1645 if isinstance(themacro, basestring):
1646 themacro = macro.Macro(themacro)
1647 if not isinstance(themacro, macro.Macro):
1648 raise ValueError('A macro must be a string or a Macro instance.')
1649 self.user_ns[name] = themacro
1575 1650
1576 When scripts are executed via %run, we must keep a reference to the
1577 namespace of their __main__ module (a FakeModule instance) around so
1578 that Python doesn't clear it, rendering objects defined therein
1579 useless.
1651 #-------------------------------------------------------------------------
1652 # Things related to the running of system commands
1653 #-------------------------------------------------------------------------
1580 1654
1581 This method keeps said reference in a private dict, keyed by the
1582 absolute path of the module object (which corresponds to the script
1583 path). This way, for multiple executions of the same script we only
1584 keep one copy of the namespace (the last one), thus preventing memory
1585 leaks from old references while allowing the objects from the last
1586 execution to be accessible.
1655 def system(self, cmd):
1656 """Make a system call, using IPython."""
1657 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1587 1658
1588 Note: we can not allow the actual FakeModule instances to be deleted,
1589 because of how Python tears down modules (it hard-sets all their
1590 references to None without regard for reference counts). This method
1591 must therefore make a *copy* of the given namespace, to allow the
1592 original module's __dict__ to be cleared and reused.
1659 #-------------------------------------------------------------------------
1660 # Things related to aliases
1661 #-------------------------------------------------------------------------
1662
1663 def init_aliases(self):
1664 # dict of things NOT to alias (keywords, builtins and some magics)
1665 no_alias = {}
1666 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
1667 for key in keyword.kwlist + no_alias_magics:
1668 no_alias[key] = 1
1669 no_alias.update(__builtin__.__dict__)
1670 self.no_alias = no_alias
1593 1671
1672 # Make some aliases automatically
1673 # Prepare list of shell aliases to auto-define
1674 if os.name == 'posix':
1675 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
1676 'mv mv -i','rm rm -i','cp cp -i',
1677 'cat cat','less less','clear clear',
1678 # a better ls
1679 'ls ls -F',
1680 # long ls
1681 'll ls -lF')
1682 # Extra ls aliases with color, which need special treatment on BSD
1683 # variants
1684 ls_extra = ( # color ls
1685 'lc ls -F -o --color',
1686 # ls normal files only
1687 'lf ls -F -o --color %l | grep ^-',
1688 # ls symbolic links
1689 'lk ls -F -o --color %l | grep ^l',
1690 # directories or links to directories,
1691 'ldir ls -F -o --color %l | grep /$',
1692 # things which are executable
1693 'lx ls -F -o --color %l | grep ^-..x',
1694 )
1695 # The BSDs don't ship GNU ls, so they don't understand the
1696 # --color switch out of the box
1697 if 'bsd' in sys.platform:
1698 ls_extra = ( # ls normal files only
1699 'lf ls -lF | grep ^-',
1700 # ls symbolic links
1701 'lk ls -lF | grep ^l',
1702 # directories or links to directories,
1703 'ldir ls -lF | grep /$',
1704 # things which are executable
1705 'lx ls -lF | grep ^-..x',
1706 )
1707 auto_alias = auto_alias + ls_extra
1708 elif os.name in ['nt','dos']:
1709 auto_alias = ('ls dir /on',
1710 'ddir dir /ad /on', 'ldir dir /ad /on',
1711 'mkdir mkdir','rmdir rmdir','echo echo',
1712 'ren ren','cls cls','copy copy')
1713 else:
1714 auto_alias = ()
1715 self.auto_alias = [s.split(None,1) for s in auto_alias]
1594 1716
1595 Parameters
1596 ----------
1597 ns : a namespace (a dict, typically)
1598
1599 fname : str
1600 Filename associated with the namespace.
1717 # Load default aliases
1718 for alias, cmd in self.auto_alias:
1719 self.define_alias(alias,cmd)
1601 1720
1602 Examples
1603 --------
1721 # Load user aliases
1722 for alias in self.alias:
1723 self.magic_alias(alias)
1604 1724
1605 In [10]: import IPython
1725 def call_alias(self,alias,rest=''):
1726 """Call an alias given its name and the rest of the line.
1606 1727
1607 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
1728 This is only used to provide backwards compatibility for users of
1729 ipalias(), use of which is not recommended for anymore."""
1608 1730
1609 In [12]: IPython.__file__ in _ip._main_ns_cache
1610 Out[12]: True
1611 """
1612 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
1731 # Now call the macro, evaluating in the user's namespace
1732 cmd = self.transform_alias(alias, rest)
1733 try:
1734 self.system(cmd)
1735 except:
1736 self.showtraceback()
1613 1737
1614 def clear_main_mod_cache(self):
1615 """Clear the cache of main modules.
1738 def define_alias(self, name, cmd):
1739 """ Define a new alias."""
1616 1740
1617 Mainly for use by utilities like %reset.
1741 if callable(cmd):
1742 self.alias_table[name] = cmd
1743 from IPython.core import shadowns
1744 setattr(shadowns, name, cmd)
1745 return
1618 1746
1619 Examples
1620 --------
1747 if isinstance(cmd, basestring):
1748 nargs = cmd.count('%s')
1749 if nargs>0 and cmd.find('%l')>=0:
1750 raise Exception('The %s and %l specifiers are mutually '
1751 'exclusive in alias definitions.')
1752
1753 self.alias_table[name] = (nargs,cmd)
1754 return
1755
1756 self.alias_table[name] = cmd
1621 1757
1622 In [15]: import IPython
1758 def ipalias(self,arg_s):
1759 """Call an alias by name.
1623 1760
1624 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
1761 Input: a string containing the name of the alias to call and any
1762 additional arguments to be passed to the magic.
1625 1763
1626 In [17]: len(_ip._main_ns_cache) > 0
1627 Out[17]: True
1764 ipalias('name -opt foo bar') is equivalent to typing at the ipython
1765 prompt:
1628 1766
1629 In [18]: _ip.clear_main_mod_cache()
1767 In[1]: name -opt foo bar
1630 1768
1631 In [19]: len(_ip._main_ns_cache) == 0
1632 Out[19]: True
1633 """
1634 self._main_ns_cache.clear()
1769 To call an alias without arguments, simply use ipalias('name').
1635 1770
1636 def _should_recompile(self,e):
1637 """Utility routine for edit_syntax_error"""
1771 This provides a proper Python function to call IPython's aliases in any
1772 valid Python code you can type at the interpreter, including loops and
1773 compound statements. It is added by IPython to the Python builtin
1774 namespace upon initialization."""
1638 1775
1639 if e.filename in ('<ipython console>','<input>','<string>',
1640 '<console>','<BackgroundJob compilation>',
1641 None):
1642
1643 return False
1776 args = arg_s.split(' ',1)
1777 alias_name = args[0]
1644 1778 try:
1645 if (self.autoedit_syntax and
1646 not self.ask_yes_no('Return to editor to correct syntax error? '
1647 '[Y/n] ','y')):
1648 return False
1649 except EOFError:
1650 return False
1779 alias_args = args[1]
1780 except IndexError:
1781 alias_args = ''
1782 if alias_name in self.alias_table:
1783 self.call_alias(alias_name,alias_args)
1784 else:
1785 error("Alias `%s` not found." % alias_name)
1651 1786
1652 def int0(x):
1653 try:
1654 return int(x)
1655 except TypeError:
1656 return 0
1657 # always pass integer line and offset values to editor hook
1658 try:
1659 self.hooks.fix_error_editor(e.filename,
1660 int0(e.lineno),int0(e.offset),e.msg)
1661 except TryNext:
1662 warn('Could not open editor')
1663 return False
1664 return True
1787 def expand_alias(self, line):
1788 """ Expand an alias in the command line
1665 1789
1666 def edit_syntax_error(self):
1667 """The bottom half of the syntax error handler called in the main loop.
1790 Returns the provided command line, possibly with the first word
1791 (command) translated according to alias expansion rules.
1792
1793 [ipython]|16> _ip.expand_aliases("np myfile.txt")
1794 <16> 'q:/opt/np/notepad++.exe myfile.txt'
1795 """
1796
1797 pre,fn,rest = self.split_user_input(line)
1798 res = pre + self.expand_aliases(fn, rest)
1799 return res
1668 1800
1669 Loop until syntax error is fixed or user cancels.
1801 def expand_aliases(self, fn, rest):
1802 """Expand multiple levels of aliases:
1803
1804 if:
1805
1806 alias foo bar /tmp
1807 alias baz foo
1808
1809 then:
1810
1811 baz huhhahhei -> bar /tmp huhhahhei
1812
1670 1813 """
1814 line = fn + " " + rest
1815
1816 done = set()
1817 while 1:
1818 pre,fn,rest = prefilter.splitUserInput(line,
1819 prefilter.shell_line_split)
1820 if fn in self.alias_table:
1821 if fn in done:
1822 warn("Cyclic alias definition, repeated '%s'" % fn)
1823 return ""
1824 done.add(fn)
1671 1825
1672 while self.SyntaxTB.last_syntax_error:
1673 # copy and clear last_syntax_error
1674 err = self.SyntaxTB.clear_err_state()
1675 if not self._should_recompile(err):
1676 return
1677 try:
1678 # may set last_syntax_error again if a SyntaxError is raised
1679 self.safe_execfile(err.filename,self.user_ns)
1680 except:
1681 self.showtraceback()
1826 l2 = self.transform_alias(fn,rest)
1827 # dir -> dir
1828 # print "alias",line, "->",l2 #dbg
1829 if l2 == line:
1830 break
1831 # ls -> ls -F should not recurse forever
1832 if l2.split(None,1)[0] == line.split(None,1)[0]:
1833 line = l2
1834 break
1835
1836 line=l2
1837
1838
1839 # print "al expand to",line #dbg
1682 1840 else:
1683 try:
1684 f = file(err.filename)
1685 try:
1686 # This should be inside a display_trap block and I
1687 # think it is.
1688 sys.displayhook(f.read())
1689 finally:
1690 f.close()
1691 except:
1692 self.showtraceback()
1693
1694 def showsyntaxerror(self, filename=None):
1695 """Display the syntax error that just occurred.
1696
1697 This doesn't display a stack trace because there isn't one.
1841 break
1842
1843 return line
1698 1844
1699 If a filename is given, it is stuffed in the exception instead
1700 of what was there before (because Python's parser always uses
1701 "<string>" when reading from a string).
1845 def transform_alias(self, alias,rest=''):
1846 """ Transform alias to system command string.
1702 1847 """
1703 etype, value, last_traceback = sys.exc_info()
1848 trg = self.alias_table[alias]
1704 1849
1705 # See note about these variables in showtraceback() below
1706 sys.last_type = etype
1707 sys.last_value = value
1708 sys.last_traceback = last_traceback
1709
1710 if filename and etype is SyntaxError:
1711 # Work hard to stuff the correct filename in the exception
1712 try:
1713 msg, (dummy_filename, lineno, offset, line) = value
1714 except:
1715 # Not the format we expect; leave it alone
1716 pass
1717 else:
1718 # Stuff in the right filename
1719 try:
1720 # Assume SyntaxError is a class exception
1721 value = SyntaxError(msg, (filename, lineno, offset, line))
1722 except:
1723 # If that failed, assume SyntaxError is a string
1724 value = msg, (filename, lineno, offset, line)
1725 self.SyntaxTB(etype,value,[])
1850 nargs,cmd = trg
1851 # print trg #dbg
1852 if ' ' in cmd and os.path.isfile(cmd):
1853 cmd = '"%s"' % cmd
1726 1854
1727 def debugger(self,force=False):
1728 """Call the pydb/pdb debugger.
1855 # Expand the %l special to be the user's input line
1856 if cmd.find('%l') >= 0:
1857 cmd = cmd.replace('%l',rest)
1858 rest = ''
1859 if nargs==0:
1860 # Simple, argument-less aliases
1861 cmd = '%s %s' % (cmd,rest)
1862 else:
1863 # Handle aliases with positional arguments
1864 args = rest.split(None,nargs)
1865 if len(args)< nargs:
1866 error('Alias <%s> requires %s arguments, %s given.' %
1867 (alias,nargs,len(args)))
1868 return None
1869 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1870 # Now call the macro, evaluating in the user's namespace
1871 #print 'new command: <%r>' % cmd # dbg
1872 return cmd
1729 1873
1730 Keywords:
1874 def init_auto_alias(self):
1875 """Define some aliases automatically.
1731 1876
1732 - force(False): by default, this routine checks the instance call_pdb
1733 flag and does not actually invoke the debugger if the flag is false.
1734 The 'force' option forces the debugger to activate even if the flag
1735 is false.
1736 """
1877 These are ALL parameter-less aliases"""
1737 1878
1738 if not (force or self.call_pdb):
1739 return
1879 for alias,cmd in self.auto_alias:
1880 self.define_alias(alias,cmd)
1881
1882 def alias_table_validate(self,verbose=0):
1883 """Update information about the alias table.
1740 1884
1741 if not hasattr(sys,'last_traceback'):
1742 error('No traceback has been produced, nothing to debug.')
1743 return
1885 In particular, make sure no Python keywords/builtins are in it."""
1744 1886
1745 # use pydb if available
1746 if debugger.has_pydb:
1747 from pydb import pm
1748 else:
1749 # fallback to our internal debugger
1750 pm = lambda : self.InteractiveTB.debugger(force=True)
1751 self.history_saving_wrapper(pm)()
1887 no_alias = self.no_alias
1888 for k in self.alias_table.keys():
1889 if k in no_alias:
1890 del self.alias_table[k]
1891 if verbose:
1892 print ("Deleting alias <%s>, it's a Python "
1893 "keyword or builtin." % k)
1752 1894
1753 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1754 """Display the exception that just occurred.
1895 #-------------------------------------------------------------------------
1896 # Things related to the running of code
1897 #-------------------------------------------------------------------------
1755 1898
1756 If nothing is known about the exception, this is the method which
1757 should be used throughout the code for presenting user tracebacks,
1758 rather than directly invoking the InteractiveTB object.
1899 def ex(self, cmd):
1900 """Execute a normal python statement in user namespace."""
1901 with nested(self.builtin_trap, self.display_trap):
1902 exec cmd in self.user_global_ns, self.user_ns
1759 1903
1760 A specific showsyntaxerror() also exists, but this method can take
1761 care of calling it if needed, so unless you are explicitly catching a
1762 SyntaxError exception, don't try to analyze the stack manually and
1763 simply call this method."""
1904 def ev(self, expr):
1905 """Evaluate python expression expr in user namespace.
1764 1906
1765
1766 # Though this won't be called by syntax errors in the input line,
1767 # there may be SyntaxError cases whith imported code.
1768
1769 try:
1770 if exc_tuple is None:
1771 etype, value, tb = sys.exc_info()
1772 else:
1773 etype, value, tb = exc_tuple
1774
1775 if etype is SyntaxError:
1776 self.showsyntaxerror(filename)
1777 elif etype is UsageError:
1778 print "UsageError:", value
1779 else:
1780 # WARNING: these variables are somewhat deprecated and not
1781 # necessarily safe to use in a threaded environment, but tools
1782 # like pdb depend on their existence, so let's set them. If we
1783 # find problems in the field, we'll need to revisit their use.
1784 sys.last_type = etype
1785 sys.last_value = value
1786 sys.last_traceback = tb
1787
1788 if etype in self.custom_exceptions:
1789 self.CustomTB(etype,value,tb)
1790 else:
1791 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1792 if self.InteractiveTB.call_pdb and self.has_readline:
1793 # pdb mucks up readline, fix it back
1794 self.set_completer()
1795 except KeyboardInterrupt:
1796 self.write("\nKeyboardInterrupt\n")
1907 Returns the result of evaluation
1908 """
1909 with nested(self.builtin_trap, self.display_trap):
1910 return eval(expr, self.user_global_ns, self.user_ns)
1797 1911
1798 1912 def mainloop(self, banner=None):
1799 1913 """Start the mainloop.
@@ -1978,200 +2092,170 b' class InteractiveShell(Component, Magic):'
1978 2092 # We are off again...
1979 2093 __builtin__.__dict__['__IPYTHON__active'] -= 1
1980 2094
1981 def excepthook(self, etype, value, tb):
1982 """One more defense for GUI apps that call sys.excepthook.
1983
1984 GUI frameworks like wxPython trap exceptions and call
1985 sys.excepthook themselves. I guess this is a feature that
1986 enables them to keep running after exceptions that would
1987 otherwise kill their mainloop. This is a bother for IPython
1988 which excepts to catch all of the program exceptions with a try:
1989 except: statement.
1990
1991 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1992 any app directly invokes sys.excepthook, it will look to the user like
1993 IPython crashed. In order to work around this, we can disable the
1994 CrashHandler and replace it with this excepthook instead, which prints a
1995 regular traceback using our InteractiveTB. In this fashion, apps which
1996 call sys.excepthook will generate a regular-looking exception from
1997 IPython, and the CrashHandler will only be triggered by real IPython
1998 crashes.
1999
2000 This hook should be used sparingly, only in places which are not likely
2001 to be true IPython errors.
2002 """
2003 self.showtraceback((etype,value,tb),tb_offset=0)
2095 def safe_execfile(self,fname,*where,**kw):
2096 """A safe version of the builtin execfile().
2004 2097
2005 def expand_alias(self, line):
2006 """ Expand an alias in the command line
2007
2008 Returns the provided command line, possibly with the first word
2009 (command) translated according to alias expansion rules.
2010
2011 [ipython]|16> _ip.expand_aliases("np myfile.txt")
2012 <16> 'q:/opt/np/notepad++.exe myfile.txt'
2013 """
2014
2015 pre,fn,rest = self.split_user_input(line)
2016 res = pre + self.expand_aliases(fn, rest)
2017 return res
2098 This version will never throw an exception, and knows how to handle
2099 ipython logs as well.
2018 2100
2019 def expand_aliases(self, fn, rest):
2020 """Expand multiple levels of aliases:
2021
2022 if:
2023
2024 alias foo bar /tmp
2025 alias baz foo
2026
2027 then:
2028
2029 baz huhhahhei -> bar /tmp huhhahhei
2030
2031 """
2032 line = fn + " " + rest
2033
2034 done = set()
2035 while 1:
2036 pre,fn,rest = prefilter.splitUserInput(line,
2037 prefilter.shell_line_split)
2038 if fn in self.alias_table:
2039 if fn in done:
2040 warn("Cyclic alias definition, repeated '%s'" % fn)
2041 return ""
2042 done.add(fn)
2101 :Parameters:
2102 fname : string
2103 Name of the file to be executed.
2104
2105 where : tuple
2106 One or two namespaces, passed to execfile() as (globals,locals).
2107 If only one is given, it is passed as both.
2043 2108
2044 l2 = self.transform_alias(fn,rest)
2045 # dir -> dir
2046 # print "alias",line, "->",l2 #dbg
2047 if l2 == line:
2048 break
2049 # ls -> ls -F should not recurse forever
2050 if l2.split(None,1)[0] == line.split(None,1)[0]:
2051 line = l2
2052 break
2053
2054 line=l2
2055
2056
2057 # print "al expand to",line #dbg
2058 else:
2059 break
2060
2061 return line
2109 :Keywords:
2110 islog : boolean (False)
2062 2111
2063 def transform_alias(self, alias,rest=''):
2064 """ Transform alias to system command string.
2065 """
2066 trg = self.alias_table[alias]
2112 quiet : boolean (True)
2067 2113
2068 nargs,cmd = trg
2069 # print trg #dbg
2070 if ' ' in cmd and os.path.isfile(cmd):
2071 cmd = '"%s"' % cmd
2114 exit_ignore : boolean (False)
2115 """
2072 2116
2073 # Expand the %l special to be the user's input line
2074 if cmd.find('%l') >= 0:
2075 cmd = cmd.replace('%l',rest)
2076 rest = ''
2077 if nargs==0:
2078 # Simple, argument-less aliases
2079 cmd = '%s %s' % (cmd,rest)
2080 else:
2081 # Handle aliases with positional arguments
2082 args = rest.split(None,nargs)
2083 if len(args)< nargs:
2084 error('Alias <%s> requires %s arguments, %s given.' %
2085 (alias,nargs,len(args)))
2086 return None
2087 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
2088 # Now call the macro, evaluating in the user's namespace
2089 #print 'new command: <%r>' % cmd # dbg
2090 return cmd
2117 def syspath_cleanup():
2118 """Internal cleanup routine for sys.path."""
2119 if add_dname:
2120 try:
2121 sys.path.remove(dname)
2122 except ValueError:
2123 # For some reason the user has already removed it, ignore.
2124 pass
2091 2125
2092 def call_alias(self,alias,rest=''):
2093 """Call an alias given its name and the rest of the line.
2094
2095 This is only used to provide backwards compatibility for users of
2096 ipalias(), use of which is not recommended for anymore."""
2097
2098 # Now call the macro, evaluating in the user's namespace
2099 cmd = self.transform_alias(alias, rest)
2100 try:
2101 self.system(cmd)
2102 except:
2103 self.showtraceback()
2104
2105 def indent_current_str(self):
2106 """return the current level of indentation as a string"""
2107 return self.indent_current_nsp * ' '
2108
2109 def autoindent_update(self,line):
2110 """Keep track of the indent level."""
2111
2112 #debugx('line')
2113 #debugx('self.indent_current_nsp')
2114 if self.autoindent:
2115 if line:
2116 inisp = num_ini_spaces(line)
2117 if inisp < self.indent_current_nsp:
2118 self.indent_current_nsp = inisp
2119
2120 if line[-1] == ':':
2121 self.indent_current_nsp += 4
2122 elif dedent_re.match(line):
2123 self.indent_current_nsp -= 4
2124 else:
2125 self.indent_current_nsp = 0
2126
2127 def push(self, variables, interactive=True):
2128 """Inject a group of variables into the IPython user namespace.
2126 fname = os.path.expanduser(fname)
2129 2127
2130 Parameters
2131 ----------
2132 variables : dict, str or list/tuple of str
2133 The variables to inject into the user's namespace. If a dict,
2134 a simple update is done. If a str, the string is assumed to
2135 have variable names separated by spaces. A list/tuple of str
2136 can also be used to give the variable names. If just the variable
2137 names are give (list/tuple/str) then the variable values looked
2138 up in the callers frame.
2139 interactive : bool
2140 If True (default), the variables will be listed with the ``who``
2141 magic.
2142 """
2143 vdict = None
2128 # Find things also in current directory. This is needed to mimic the
2129 # behavior of running a script from the system command line, where
2130 # Python inserts the script's directory into sys.path
2131 dname = os.path.dirname(os.path.abspath(fname))
2132 add_dname = False
2133 if dname not in sys.path:
2134 sys.path.insert(0,dname)
2135 add_dname = True
2144 2136
2145 # We need a dict of name/value pairs to do namespace updates.
2146 if isinstance(variables, dict):
2147 vdict = variables
2148 elif isinstance(variables, (basestring, list, tuple)):
2149 if isinstance(variables, basestring):
2150 vlist = variables.split()
2151 else:
2152 vlist = variables
2153 vdict = {}
2154 cf = sys._getframe(1)
2155 for name in vlist:
2137 try:
2138 xfile = open(fname)
2139 except:
2140 print >> Term.cerr, \
2141 'Could not open file <%s> for safe execution.' % fname
2142 syspath_cleanup()
2143 return None
2144
2145 kw.setdefault('islog',0)
2146 kw.setdefault('quiet',1)
2147 kw.setdefault('exit_ignore',0)
2148
2149 first = xfile.readline()
2150 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2151 xfile.close()
2152 # line by line execution
2153 if first.startswith(loghead) or kw['islog']:
2154 print 'Loading log file <%s> one line at a time...' % fname
2155 if kw['quiet']:
2156 stdout_save = sys.stdout
2157 sys.stdout = StringIO.StringIO()
2158 try:
2159 globs,locs = where[0:2]
2160 except:
2156 2161 try:
2157 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
2162 globs = locs = where[0]
2158 2163 except:
2159 print ('Could not get variable %s from %s' %
2160 (name,cf.f_code.co_name))
2161 else:
2162 raise ValueError('variables must be a dict/str/list/tuple')
2163
2164 # Propagate variables to user namespace
2165 self.user_ns.update(vdict)
2164 globs = locs = globals()
2165 badblocks = []
2166 2166
2167 # And configure interactive visibility
2168 config_ns = self.user_config_ns
2169 if interactive:
2170 for name, val in vdict.iteritems():
2171 config_ns.pop(name, None)
2172 else:
2173 for name,val in vdict.iteritems():
2174 config_ns[name] = val
2167 # we also need to identify indented blocks of code when replaying
2168 # logs and put them together before passing them to an exec
2169 # statement. This takes a bit of regexp and look-ahead work in the
2170 # file. It's easiest if we swallow the whole thing in memory
2171 # first, and manually walk through the lines list moving the
2172 # counter ourselves.
2173 indent_re = re.compile('\s+\S')
2174 xfile = open(fname)
2175 filelines = xfile.readlines()
2176 xfile.close()
2177 nlines = len(filelines)
2178 lnum = 0
2179 while lnum < nlines:
2180 line = filelines[lnum]
2181 lnum += 1
2182 # don't re-insert logger status info into cache
2183 if line.startswith('#log#'):
2184 continue
2185 else:
2186 # build a block of code (maybe a single line) for execution
2187 block = line
2188 try:
2189 next = filelines[lnum] # lnum has already incremented
2190 except:
2191 next = None
2192 while next and indent_re.match(next):
2193 block += next
2194 lnum += 1
2195 try:
2196 next = filelines[lnum]
2197 except:
2198 next = None
2199 # now execute the block of one or more lines
2200 try:
2201 exec block in globs,locs
2202 except SystemExit:
2203 pass
2204 except:
2205 badblocks.append(block.rstrip())
2206 if kw['quiet']: # restore stdout
2207 sys.stdout.close()
2208 sys.stdout = stdout_save
2209 print 'Finished replaying log file <%s>' % fname
2210 if badblocks:
2211 print >> sys.stderr, ('\nThe following lines/blocks in file '
2212 '<%s> reported errors:' % fname)
2213
2214 for badline in badblocks:
2215 print >> sys.stderr, badline
2216 else: # regular file execution
2217 try:
2218 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2219 # Work around a bug in Python for Windows. The bug was
2220 # fixed in in Python 2.5 r54159 and 54158, but that's still
2221 # SVN Python as of March/07. For details, see:
2222 # http://projects.scipy.org/ipython/ipython/ticket/123
2223 try:
2224 globs,locs = where[0:2]
2225 except:
2226 try:
2227 globs = locs = where[0]
2228 except:
2229 globs = locs = globals()
2230 exec file(fname) in globs,locs
2231 else:
2232 execfile(fname,*where)
2233 except SyntaxError:
2234 self.showsyntaxerror()
2235 warn('Failure executing file: <%s>' % fname)
2236 except SystemExit,status:
2237 # Code that correctly sets the exit status flag to success (0)
2238 # shouldn't be bothered with a traceback. Note that a plain
2239 # sys.exit() does NOT set the message to 0 (it's empty) so that
2240 # will still get a traceback. Note that the structure of the
2241 # SystemExit exception changed between Python 2.4 and 2.5, so
2242 # the checks must be done in a version-dependent way.
2243 show = False
2244
2245 if sys.version_info[:2] > (2,5):
2246 if status.message!=0 and not kw['exit_ignore']:
2247 show = True
2248 else:
2249 if status.code and not kw['exit_ignore']:
2250 show = True
2251 if show:
2252 self.showtraceback()
2253 warn('Failure executing file: <%s>' % fname)
2254 except:
2255 self.showtraceback()
2256 warn('Failure executing file: <%s>' % fname)
2257
2258 syspath_cleanup()
2175 2259
2176 2260 def cleanup_ipy_script(self, script):
2177 2261 """Make a script safe for self.runlines()
@@ -2383,13 +2467,31 b' class InteractiveShell(Component, Magic):'
2383 2467
2384 2468 #print 'push line: <%s>' % line # dbg
2385 2469 for subline in line.splitlines():
2386 self.autoindent_update(subline)
2470 self._autoindent_update(subline)
2387 2471 self.buffer.append(line)
2388 2472 more = self.runsource('\n'.join(self.buffer), self.filename)
2389 2473 if not more:
2390 2474 self.resetbuffer()
2391 2475 return more
2392 2476
2477 def _autoindent_update(self,line):
2478 """Keep track of the indent level."""
2479
2480 #debugx('line')
2481 #debugx('self.indent_current_nsp')
2482 if self.autoindent:
2483 if line:
2484 inisp = num_ini_spaces(line)
2485 if inisp < self.indent_current_nsp:
2486 self.indent_current_nsp = inisp
2487
2488 if line[-1] == ':':
2489 self.indent_current_nsp += 4
2490 elif dedent_re.match(line):
2491 self.indent_current_nsp -= 4
2492 else:
2493 self.indent_current_nsp = 0
2494
2393 2495 def split_user_input(self, line):
2394 2496 # This is really a hold-over to support ipapi and some extensions
2395 2497 return prefilter.splitUserInput(line)
@@ -2425,47 +2527,121 b' class InteractiveShell(Component, Magic):'
2425 2527 self.ask_exit()
2426 2528 return ""
2427 2529
2428 # Try to be reasonably smart about not re-indenting pasted input more
2429 # than necessary. We do this by trimming out the auto-indent initial
2430 # spaces, if the user's actual input started itself with whitespace.
2431 #debugx('self.buffer[-1]')
2530 # Try to be reasonably smart about not re-indenting pasted input more
2531 # than necessary. We do this by trimming out the auto-indent initial
2532 # spaces, if the user's actual input started itself with whitespace.
2533 #debugx('self.buffer[-1]')
2534
2535 if self.autoindent:
2536 if num_ini_spaces(line) > self.indent_current_nsp:
2537 line = line[self.indent_current_nsp:]
2538 self.indent_current_nsp = 0
2539
2540 # store the unfiltered input before the user has any chance to modify
2541 # it.
2542 if line.strip():
2543 if continue_prompt:
2544 self.input_hist_raw[-1] += '%s\n' % line
2545 if self.has_readline: # and some config option is set?
2546 try:
2547 histlen = self.readline.get_current_history_length()
2548 if histlen > 1:
2549 newhist = self.input_hist_raw[-1].rstrip()
2550 self.readline.remove_history_item(histlen-1)
2551 self.readline.replace_history_item(histlen-2,
2552 newhist.encode(self.stdin_encoding))
2553 except AttributeError:
2554 pass # re{move,place}_history_item are new in 2.4.
2555 else:
2556 self.input_hist_raw.append('%s\n' % line)
2557 # only entries starting at first column go to shadow history
2558 if line.lstrip() == line:
2559 self.shadowhist.add(line.strip())
2560 elif not continue_prompt:
2561 self.input_hist_raw.append('\n')
2562 try:
2563 lineout = self.prefilter(line,continue_prompt)
2564 except:
2565 # blanket except, in case a user-defined prefilter crashes, so it
2566 # can't take all of ipython with it.
2567 self.showtraceback()
2568 return ''
2569 else:
2570 return lineout
2571
2572 # def init_exec_commands(self):
2573 # for cmd in self.config.EXECUTE:
2574 # print "execute:", cmd
2575 # self.api.runlines(cmd)
2576 #
2577 # batchrun = False
2578 # if self.config.has_key('EXECFILE'):
2579 # for batchfile in [path(arg) for arg in self.config.EXECFILE
2580 # if arg.lower().endswith('.ipy')]:
2581 # if not batchfile.isfile():
2582 # print "No such batch file:", batchfile
2583 # continue
2584 # self.api.runlines(batchfile.text())
2585 # batchrun = True
2586 # # without -i option, exit after running the batch file
2587 # if batchrun and not self.interactive:
2588 # self.ask_exit()
2589
2590 # def load(self, mod):
2591 # """ Load an extension.
2592 #
2593 # Some modules should (or must) be 'load()':ed, rather than just imported.
2594 #
2595 # Loading will do:
2596 #
2597 # - run init_ipython(ip)
2598 # - run ipython_firstrun(ip)
2599 # """
2600 #
2601 # if mod in self.extensions:
2602 # # just to make sure we don't init it twice
2603 # # note that if you 'load' a module that has already been
2604 # # imported, init_ipython gets run anyway
2605 #
2606 # return self.extensions[mod]
2607 # __import__(mod)
2608 # m = sys.modules[mod]
2609 # if hasattr(m,'init_ipython'):
2610 # m.init_ipython(self)
2611 #
2612 # if hasattr(m,'ipython_firstrun'):
2613 # already_loaded = self.db.get('firstrun_done', set())
2614 # if mod not in already_loaded:
2615 # m.ipython_firstrun(self)
2616 # already_loaded.add(mod)
2617 # self.db['firstrun_done'] = already_loaded
2618 #
2619 # self.extensions[mod] = m
2620 # return m
2621
2622 #-------------------------------------------------------------------------
2623 # Things related to the prefilter
2624 #-------------------------------------------------------------------------
2432 2625
2433 if self.autoindent:
2434 if num_ini_spaces(line) > self.indent_current_nsp:
2435 line = line[self.indent_current_nsp:]
2436 self.indent_current_nsp = 0
2437
2438 # store the unfiltered input before the user has any chance to modify
2439 # it.
2440 if line.strip():
2441 if continue_prompt:
2442 self.input_hist_raw[-1] += '%s\n' % line
2443 if self.has_readline: # and some config option is set?
2444 try:
2445 histlen = self.readline.get_current_history_length()
2446 if histlen > 1:
2447 newhist = self.input_hist_raw[-1].rstrip()
2448 self.readline.remove_history_item(histlen-1)
2449 self.readline.replace_history_item(histlen-2,
2450 newhist.encode(self.stdin_encoding))
2451 except AttributeError:
2452 pass # re{move,place}_history_item are new in 2.4.
2453 else:
2454 self.input_hist_raw.append('%s\n' % line)
2455 # only entries starting at first column go to shadow history
2456 if line.lstrip() == line:
2457 self.shadowhist.add(line.strip())
2458 elif not continue_prompt:
2459 self.input_hist_raw.append('\n')
2460 try:
2461 lineout = self.prefilter(line,continue_prompt)
2462 except:
2463 # blanket except, in case a user-defined prefilter crashes, so it
2464 # can't take all of ipython with it.
2465 self.showtraceback()
2466 return ''
2467 else:
2468 return lineout
2626 def init_handlers(self):
2627 # escapes for automatic behavior on the command line
2628 self.ESC_SHELL = '!'
2629 self.ESC_SH_CAP = '!!'
2630 self.ESC_HELP = '?'
2631 self.ESC_MAGIC = '%'
2632 self.ESC_QUOTE = ','
2633 self.ESC_QUOTE2 = ';'
2634 self.ESC_PAREN = '/'
2635
2636 # And their associated handlers
2637 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
2638 self.ESC_QUOTE : self.handle_auto,
2639 self.ESC_QUOTE2 : self.handle_auto,
2640 self.ESC_MAGIC : self.handle_magic,
2641 self.ESC_HELP : self.handle_help,
2642 self.ESC_SHELL : self.handle_shell_escape,
2643 self.ESC_SH_CAP : self.handle_shell_escape,
2644 }
2469 2645
2470 2646 def _prefilter(self, line, continue_prompt):
2471 2647 """Calls different preprocessors, depending on the form of line."""
@@ -2731,7 +2907,21 b' class InteractiveShell(Component, Magic):'
2731 2907
2732 2908 # The input cache shouldn't be updated
2733 2909 return line_info.line
2734
2910
2911 #-------------------------------------------------------------------------
2912 # Utilities
2913 #-------------------------------------------------------------------------
2914
2915 def getoutput(self, cmd):
2916 return getoutput(self.var_expand(cmd,depth=2),
2917 header=self.system_header,
2918 verbose=self.system_verbose)
2919
2920 def getoutputerror(self, cmd):
2921 return getoutputerror(self.var_expand(cmd,depth=2),
2922 header=self.system_header,
2923 verbose=self.system_verbose)
2924
2735 2925 def var_expand(self,cmd,depth=0):
2736 2926 """Expand python variables in a string.
2737 2927
@@ -2776,6 +2966,15 b' class InteractiveShell(Component, Magic):'
2776 2966 """Write a string to the default error output"""
2777 2967 Term.cerr.write(data)
2778 2968
2969 def ask_yes_no(self,prompt,default=True):
2970 if self.quiet:
2971 return True
2972 return ask_yes_no(prompt,default)
2973
2974 #-------------------------------------------------------------------------
2975 # Things related to IPython exiting
2976 #-------------------------------------------------------------------------
2977
2779 2978 def ask_exit(self):
2780 2979 """ Call for exiting. Can be overiden and used as a callback. """
2781 2980 self.exit_now = True
@@ -2790,169 +2989,30 b' class InteractiveShell(Component, Magic):'
2790 2989 else:
2791 2990 self.ask_exit()
2792 2991
2793 def safe_execfile(self,fname,*where,**kw):
2794 """A safe version of the builtin execfile().
2795
2796 This version will never throw an exception, and knows how to handle
2797 ipython logs as well.
2798
2799 :Parameters:
2800 fname : string
2801 Name of the file to be executed.
2802
2803 where : tuple
2804 One or two namespaces, passed to execfile() as (globals,locals).
2805 If only one is given, it is passed as both.
2806
2807 :Keywords:
2808 islog : boolean (False)
2992 def atexit_operations(self):
2993 """This will be executed at the time of exit.
2809 2994
2810 quiet : boolean (True)
2995 Saving of persistent data should be performed here.
2996 """
2997 self.savehist()
2811 2998
2812 exit_ignore : boolean (False)
2813 """
2999 # Cleanup all tempfiles left around
3000 for tfile in self.tempfiles:
3001 try:
3002 os.unlink(tfile)
3003 except OSError:
3004 pass
2814 3005
2815 def syspath_cleanup():
2816 """Internal cleanup routine for sys.path."""
2817 if add_dname:
2818 try:
2819 sys.path.remove(dname)
2820 except ValueError:
2821 # For some reason the user has already removed it, ignore.
2822 pass
2823
2824 fname = os.path.expanduser(fname)
3006 # Clear all user namespaces to release all references cleanly.
3007 self.reset()
2825 3008
2826 # Find things also in current directory. This is needed to mimic the
2827 # behavior of running a script from the system command line, where
2828 # Python inserts the script's directory into sys.path
2829 dname = os.path.dirname(os.path.abspath(fname))
2830 add_dname = False
2831 if dname not in sys.path:
2832 sys.path.insert(0,dname)
2833 add_dname = True
3009 # Run user hooks
3010 self.hooks.shutdown_hook()
2834 3011
2835 try:
2836 xfile = open(fname)
2837 except:
2838 print >> Term.cerr, \
2839 'Could not open file <%s> for safe execution.' % fname
2840 syspath_cleanup()
2841 return None
3012 def cleanup(self):
3013 self.restore_sys_module_state()
2842 3014
2843 kw.setdefault('islog',0)
2844 kw.setdefault('quiet',1)
2845 kw.setdefault('exit_ignore',0)
2846
2847 first = xfile.readline()
2848 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2849 xfile.close()
2850 # line by line execution
2851 if first.startswith(loghead) or kw['islog']:
2852 print 'Loading log file <%s> one line at a time...' % fname
2853 if kw['quiet']:
2854 stdout_save = sys.stdout
2855 sys.stdout = StringIO.StringIO()
2856 try:
2857 globs,locs = where[0:2]
2858 except:
2859 try:
2860 globs = locs = where[0]
2861 except:
2862 globs = locs = globals()
2863 badblocks = []
2864 3015
2865 # we also need to identify indented blocks of code when replaying
2866 # logs and put them together before passing them to an exec
2867 # statement. This takes a bit of regexp and look-ahead work in the
2868 # file. It's easiest if we swallow the whole thing in memory
2869 # first, and manually walk through the lines list moving the
2870 # counter ourselves.
2871 indent_re = re.compile('\s+\S')
2872 xfile = open(fname)
2873 filelines = xfile.readlines()
2874 xfile.close()
2875 nlines = len(filelines)
2876 lnum = 0
2877 while lnum < nlines:
2878 line = filelines[lnum]
2879 lnum += 1
2880 # don't re-insert logger status info into cache
2881 if line.startswith('#log#'):
2882 continue
2883 else:
2884 # build a block of code (maybe a single line) for execution
2885 block = line
2886 try:
2887 next = filelines[lnum] # lnum has already incremented
2888 except:
2889 next = None
2890 while next and indent_re.match(next):
2891 block += next
2892 lnum += 1
2893 try:
2894 next = filelines[lnum]
2895 except:
2896 next = None
2897 # now execute the block of one or more lines
2898 try:
2899 exec block in globs,locs
2900 except SystemExit:
2901 pass
2902 except:
2903 badblocks.append(block.rstrip())
2904 if kw['quiet']: # restore stdout
2905 sys.stdout.close()
2906 sys.stdout = stdout_save
2907 print 'Finished replaying log file <%s>' % fname
2908 if badblocks:
2909 print >> sys.stderr, ('\nThe following lines/blocks in file '
2910 '<%s> reported errors:' % fname)
2911
2912 for badline in badblocks:
2913 print >> sys.stderr, badline
2914 else: # regular file execution
2915 try:
2916 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2917 # Work around a bug in Python for Windows. The bug was
2918 # fixed in in Python 2.5 r54159 and 54158, but that's still
2919 # SVN Python as of March/07. For details, see:
2920 # http://projects.scipy.org/ipython/ipython/ticket/123
2921 try:
2922 globs,locs = where[0:2]
2923 except:
2924 try:
2925 globs = locs = where[0]
2926 except:
2927 globs = locs = globals()
2928 exec file(fname) in globs,locs
2929 else:
2930 execfile(fname,*where)
2931 except SyntaxError:
2932 self.showsyntaxerror()
2933 warn('Failure executing file: <%s>' % fname)
2934 except SystemExit,status:
2935 # Code that correctly sets the exit status flag to success (0)
2936 # shouldn't be bothered with a traceback. Note that a plain
2937 # sys.exit() does NOT set the message to 0 (it's empty) so that
2938 # will still get a traceback. Note that the structure of the
2939 # SystemExit exception changed between Python 2.4 and 2.5, so
2940 # the checks must be done in a version-dependent way.
2941 show = False
2942 3016
2943 if sys.version_info[:2] > (2,5):
2944 if status.message!=0 and not kw['exit_ignore']:
2945 show = True
2946 else:
2947 if status.code and not kw['exit_ignore']:
2948 show = True
2949 if show:
2950 self.showtraceback()
2951 warn('Failure executing file: <%s>' % fname)
2952 except:
2953 self.showtraceback()
2954 warn('Failure executing file: <%s>' % fname)
2955 3017
2956 syspath_cleanup()
2957 3018
2958 #************************* end of file <iplib.py> *****************************
General Comments 0
You need to be logged in to leave comments. Login now