Show More
@@ -227,6 +227,14 b' class InteractiveShellEmbed(InteractiveShell):' | |||||
227 | for var in local_varnames: |
|
227 | for var in local_varnames: | |
228 | delvar(var,None) |
|
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 | _embedded_shell = None |
|
239 | _embedded_shell = None | |
232 |
|
240 |
This diff has been collapsed as it changes many lines, (2836 lines changed) Show them Hide them | |||||
@@ -322,9 +322,6 b' class InteractiveShell(Component, Magic):' | |||||
322 | self.init_pdb() |
|
322 | self.init_pdb() | |
323 | self.hooks.late_startup_hook() |
|
323 | self.hooks.late_startup_hook() | |
324 |
|
324 | |||
325 | def cleanup(self): |
|
|||
326 | self.restore_sys_module_state() |
|
|||
327 |
|
||||
328 | #------------------------------------------------------------------------- |
|
325 | #------------------------------------------------------------------------- | |
329 | # Traitlet changed handlers |
|
326 | # Traitlet changed handlers | |
330 | #------------------------------------------------------------------------- |
|
327 | #------------------------------------------------------------------------- | |
@@ -346,6 +343,21 b' class InteractiveShell(Component, Magic):' | |||||
346 | def _term_title_changed(self, name, new_value): |
|
343 | def _term_title_changed(self, name, new_value): | |
347 | self.init_term_title() |
|
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 | # init_* methods called by __init__ |
|
362 | # init_* methods called by __init__ | |
351 | #------------------------------------------------------------------------- |
|
363 | #------------------------------------------------------------------------- | |
@@ -439,578 +451,498 b' class InteractiveShell(Component, Magic):' | |||||
439 | if self.banner2: |
|
451 | if self.banner2: | |
440 |
self.banner += '\n' + self.banner2 + '\n' |
|
452 | self.banner += '\n' + self.banner2 + '\n' | |
441 |
|
453 | |||
442 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
454 | def init_encoding(self): | |
443 | # Create the namespace where the user will operate. user_ns is |
|
455 | # Get system encoding at startup time. Certain terminals (like Emacs | |
444 | # normally the only one used, and it is passed to the exec calls as |
|
456 | # under Win32 have it set to None, and we need to have a known valid | |
445 | # the locals argument. But we do carry a user_global_ns namespace |
|
457 | # encoding to use in the raw_input() method | |
446 | # given as the exec 'globals' argument, This is useful in embedding |
|
458 | try: | |
447 | # situations where the ipython shell opens in a context where the |
|
459 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
448 | # distinction between locals and globals is meaningful. For |
|
460 | except AttributeError: | |
449 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
461 | self.stdin_encoding = 'ascii' | |
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. |
|
|||
455 |
|
||||
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 |
|
|||
461 |
|
||||
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? |
|
|||
468 |
|
462 | |||
469 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
463 | def init_syntax_highlighting(self): | |
470 | # or a module, and it's been that way for a long time. Whether it's |
|
464 | # Python source parser/formatter for syntax highlighting | |
471 | # intentional (or sensible), I don't know. In any case, the idea is |
|
465 | pyformat = PyColorize.Parser().format | |
472 | # that if you need to access the built-in namespace directly, you |
|
466 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
473 | # should start with "import __builtin__" (note, no 's') which will |
|
|||
474 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
|||
475 |
|
467 | |||
476 | # These routines return properly built dicts as needed by the rest of |
|
468 | def init_pushd_popd_magic(self): | |
477 | # the code, and can also be used by extension writers to generate |
|
469 | # for pushd/popd management | |
478 | # properly initialized namespaces. |
|
470 | try: | |
479 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, |
|
471 | self.home_dir = get_home_dir() | |
480 | user_global_ns) |
|
472 | except HomeDirError, msg: | |
|
473 | fatal(msg) | |||
481 |
|
474 | |||
482 | # Assign namespaces |
|
475 | self.dir_stack = [] | |
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 |
|
|||
486 |
|
476 | |||
487 | # An auxiliary namespace that checks what parts of the user_ns were |
|
477 | def init_logger(self): | |
488 | # loaded at startup, so we can list later only variables defined in |
|
478 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') | |
489 | # actual interactive use. Since it is always a subset of user_ns, it |
|
479 | # local shortcut, this is used a LOT | |
490 | # doesn't need to be seaparately tracked in the ns_table |
|
480 | self.log = self.logger.log | |
491 | self.user_config_ns = {} |
|
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 | """ | |||
492 |
|
491 | |||
493 | # A namespace to keep track of internal data structures to prevent |
|
492 | def init_logstart(self): | |
494 | # them from cluttering user-visible stuff. Will be updated later |
|
493 | if self.logplay: | |
495 | self.internal_ns = {} |
|
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() | |||
496 |
|
499 | |||
497 | # Namespace of system aliases. Each entry in the alias |
|
500 | def init_builtins(self): | |
498 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
501 | self.builtin_trap = BuiltinTrap(self) | |
499 | # of positional arguments of the alias. |
|
|||
500 | self.alias_table = {} |
|
|||
501 |
|
502 | |||
502 | # Now that FakeModule produces a real module, we've run into a nasty |
|
503 | def init_inspector(self): | |
503 | # problem: after script execution (via %run), the module where the user |
|
504 | # Object inspector | |
504 | # code ran is deleted. Now that this object is a true module (needed |
|
505 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
505 | # so docetst and other tools work correctly), the Python module |
|
506 | PyColorize.ANSICodeColors, | |
506 | # teardown mechanism runs over it, and sets to None every variable |
|
507 | 'NoColor', | |
507 | # present in that module. Top-level references to objects from the |
|
508 | self.object_info_string_level) | |
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. |
|
|||
524 |
|
509 | |||
525 | # This is the cache used for 'main' namespaces |
|
510 | def init_prompts(self): | |
526 | self._main_ns_cache = {} |
|
511 | # Initialize cache, set in/out prompts and printing system | |
527 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
512 | self.outputcache = CachedOutput(self, | |
528 | # copying and clearing for reuse on each %run |
|
513 | self.cache_size, | |
529 | self._user_main_module = FakeModule() |
|
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) | |||
530 |
|
522 | |||
531 | # A table holding all the namespaces IPython deals with, so that |
|
523 | # user may have over-ridden the default print hook: | |
532 | # introspection facilities can search easily. |
|
524 | try: | |
533 | self.ns_table = {'user':user_ns, |
|
525 | self.outputcache.__class__.display = self.hooks.display | |
534 | 'user_global':user_global_ns, |
|
526 | except AttributeError: | |
535 | 'alias':self.alias_table, |
|
527 | pass | |
536 | 'internal':self.internal_ns, |
|
|||
537 | 'builtin':__builtin__.__dict__ |
|
|||
538 | } |
|
|||
539 |
|
528 | |||
540 | # Similarly, track all namespaces where references can be held and that |
|
529 | def init_displayhook(self): | |
541 | # we can safely clear (so it can NOT include builtin). This one can be |
|
530 | self.display_trap = DisplayTrap(self, self.outputcache) | |
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 |
|
531 | |||
547 |
def init_ |
|
532 | def init_reload_doctest(self): | |
548 | # We need to insert into sys.modules something that looks like a |
|
533 | # Do a proper resetting of doctest, including the necessary displayhook | |
549 | # module but which accesses the IPython namespace, for shelve and |
|
534 | # monkeypatching | |
550 | # pickle to work interactively. Normally they rely on getting |
|
535 | try: | |
551 | # everything out of __main__, but for embedding purposes each IPython |
|
536 | doctest_reload() | |
552 | # instance has its own private namespace, so we can't go shoving |
|
537 | except ImportError: | |
553 | # everything into __main__. |
|
538 | warn("doctest module does not exist.") | |
554 |
|
539 | |||
555 | # note, however, that we should only do this for non-embedded |
|
540 | #------------------------------------------------------------------------- | |
556 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
541 | # Things related to injections into the sys module | |
557 | # namespace. Embedded instances, on the other hand, should not do |
|
542 | #------------------------------------------------------------------------- | |
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). |
|
|||
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 | try: |
|
554 | try: | |
566 | main_name = self.user_ns['__name__'] |
|
555 | self._orig_sys_modules_main_name = self.user_ns['__name__'] | |
567 | except KeyError: |
|
556 | except KeyError: | |
568 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
557 | pass | |
569 | else: |
|
|||
570 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
|||
571 |
|
558 | |||
572 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
559 | def restore_sys_module_state(self): | |
573 | """Return a valid local and global user interactive namespaces. |
|
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 | #------------------------------------------------------------------------- | |
576 | valid IPython user namespace, which you can pass to the various |
|
577 | # Things related to hooks | |
577 | embedding classes in ipython. The default implementation returns the |
|
578 | #------------------------------------------------------------------------- | |
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. |
|
|||
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: |
|
584 | self.strdispatchers = {} | |
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 |
|
585 | |||
598 | :Returns: |
|
586 | # Set all default hooks, defined in the IPython.hooks module. | |
599 | A tuple pair of dictionary-like object to be used as the local namespace |
|
587 | import IPython.core.hooks | |
600 | of the interpreter and a dict to be used as the global namespace. |
|
588 | hooks = IPython.core.hooks | |
601 | """ |
|
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) | |||
602 |
|
593 | |||
603 | if user_ns is None: |
|
594 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
604 | # Set __name__ to __main__ to better match the behavior of the |
|
595 | """set_hook(name,hook) -> sets an internal IPython hook. | |
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__) |
|
|||
612 |
|
|
596 | ||
613 | if user_global_ns is None: |
|
597 | IPython exposes some of its internal API as user-modifiable hooks. By | |
614 | user_global_ns = user_ns |
|
598 | adding your function to one of these hooks, you can modify IPython's | |
615 | if type(user_global_ns) is not dict: |
|
599 | behavior to call at runtime your own routines.""" | |
616 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
|||
617 | % type(user_global_ns)) |
|
|||
618 |
|
600 | |||
619 | return user_ns, user_global_ns |
|
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. | |||
620 |
|
604 | |||
621 | def init_history(self): |
|
605 | f = new.instancemethod(hook,self,self.__class__) | |
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() |
|
|||
628 |
|
606 | |||
629 | # list of visited directories |
|
607 | # check if the hook is for strdispatcher first | |
630 | try: |
|
608 | if str_key is not None: | |
631 | self.dir_hist = [os.getcwd()] |
|
609 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
632 | except OSError: |
|
610 | sdp.add_s(str_key, f, priority ) | |
633 |
self. |
|
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 | |||
634 |
|
618 | |||
635 | # dict of output history |
|
619 | dp = getattr(self.hooks, name, None) | |
636 | self.output_hist = {} |
|
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() | |||
637 |
|
624 | |||
638 | # Now the history file |
|
|||
639 | try: |
|
625 | try: | |
640 | histfname = 'history-%s' % self.profile |
|
626 | dp.add(f,priority) | |
641 | except AttributeError: |
|
627 | except AttributeError: | |
642 | histfname = 'history' |
|
628 | # it was not commandchain, plain old func - replace | |
643 | self.histfile = os.path.join(self.config.IPYTHONDIR, histfname) |
|
629 | dp = f | |
644 |
|
630 | |||
645 | # Fill the history zero entry, user counter starts at 1 |
|
631 | setattr(self.hooks,name, dp) | |
646 | self.input_hist.append('\n') |
|
|||
647 | self.input_hist_raw.append('\n') |
|
|||
648 |
|
632 | |||
649 | def init_encoding(self): |
|
633 | #------------------------------------------------------------------------- | |
650 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
634 | # Things related to the "main" module | |
651 | # under Win32 have it set to None, and we need to have a known valid |
|
635 | #------------------------------------------------------------------------- | |
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' |
|
|||
657 |
|
636 | |||
658 |
def |
|
637 | def new_main_mod(self,ns=None): | |
659 | # escapes for automatic behavior on the command line |
|
638 | """Return a new 'main' module object for user code execution. | |
660 | self.ESC_SHELL = '!' |
|
639 | """ | |
661 | self.ESC_SH_CAP = '!!' |
|
640 | main_mod = self._user_main_module | |
662 | self.ESC_HELP = '?' |
|
641 | init_fakemod_dict(main_mod,ns) | |
663 | self.ESC_MAGIC = '%' |
|
642 | return main_mod | |
664 | self.ESC_QUOTE = ',' |
|
|||
665 | self.ESC_QUOTE2 = ';' |
|
|||
666 | self.ESC_PAREN = '/' |
|
|||
667 |
|
643 | |||
668 | # And their associated handlers |
|
644 | def cache_main_mod(self,ns,fname): | |
669 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
645 | """Cache a main module's namespace. | |
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 | } |
|
|||
677 |
|
|
646 | ||
678 | def init_syntax_highlighting(self): |
|
647 | When scripts are executed via %run, we must keep a reference to the | |
679 | # Python source parser/formatter for syntax highlighting |
|
648 | namespace of their __main__ module (a FakeModule instance) around so | |
680 | pyformat = PyColorize.Parser().format |
|
649 | that Python doesn't clear it, rendering objects defined therein | |
681 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
650 | useless. | |
682 |
|
|
651 | ||
683 | def init_hooks(self): |
|
652 | This method keeps said reference in a private dict, keyed by the | |
684 | # hooks holds pointers used for user-side customizations |
|
653 | absolute path of the module object (which corresponds to the script | |
685 | self.hooks = Struct() |
|
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. | |||
686 |
|
658 | |||
687 | self.strdispatchers = {} |
|
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. | |||
688 |
|
664 | |||
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) |
|
|||
696 |
|
665 | |||
697 | def init_pushd_popd_magic(self): |
|
666 | Parameters | |
698 | # for pushd/popd management |
|
667 | ---------- | |
699 | try: |
|
668 | ns : a namespace (a dict, typically) | |
700 | self.home_dir = get_home_dir() |
|
|||
701 | except HomeDirError, msg: |
|
|||
702 | fatal(msg) |
|
|||
703 |
|
|
669 | ||
704 | self.dir_stack = [] |
|
670 | fname : str | |
|
671 | Filename associated with the namespace. | |||
705 |
|
|
672 | ||
706 | def init_traceback_handlers(self, custom_exceptions): |
|
673 | Examples | |
707 | # Syntax error handler. |
|
674 | -------- | |
708 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
|||
709 |
|
675 | |||
710 | # The interactive one is initialized with an offset, meaning we always |
|
676 | In [10]: import IPython | |
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) |
|
|||
716 |
|
|
677 | ||
717 | # IPython itself shouldn't crash. This will produce a detailed |
|
678 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
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) |
|
|||
729 |
|
|
679 | ||
730 | # and add any custom exception handlers the user may have specified |
|
680 | In [12]: IPython.__file__ in _ip._main_ns_cache | |
731 | self.set_custom_exc(*custom_exceptions) |
|
681 | Out[12]: True | |
|
682 | """ | |||
|
683 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() | |||
732 |
|
684 | |||
733 |
def |
|
685 | def clear_main_mod_cache(self): | |
734 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') |
|
686 | """Clear the cache of main modules. | |
735 | # local shortcut, this is used a LOT |
|
687 | ||
736 | self.log = self.logger.log |
|
688 | Mainly for use by utilities like %reset. | |
737 | # template for logfile headers. It gets resolved at runtime by the |
|
689 | ||
738 | # logstart method. |
|
690 | Examples | |
739 | self.loghead_tpl = \ |
|
691 | -------- | |
740 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
692 | ||
741 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
693 | In [15]: import IPython | |
742 | #log# opts = %s |
|
694 | ||
743 | #log# args = %s |
|
695 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
744 | #log# It is safe to make manual edits below here. |
|
696 | ||
745 | #log#----------------------------------------------------------------------- |
|
697 | In [17]: len(_ip._main_ns_cache) > 0 | |
|
698 | Out[17]: True | |||
|
699 | ||||
|
700 | In [18]: _ip.clear_main_mod_cache() | |||
|
701 | ||||
|
702 | In [19]: len(_ip._main_ns_cache) == 0 | |||
|
703 | Out[19]: True | |||
746 | """ |
|
704 | """ | |
|
705 | self._main_ns_cache.clear() | |||
747 |
|
706 | |||
748 | def init_logstart(self): |
|
707 | #------------------------------------------------------------------------- | |
749 | if self.logplay: |
|
708 | # Things related to debugging | |
750 | self.magic_logstart(self.logplay + ' append') |
|
709 | #------------------------------------------------------------------------- | |
751 | elif self.logfile: |
|
|||
752 | self.magic_logstart(self.logfile) |
|
|||
753 | elif self.logstart: |
|
|||
754 | self.magic_logstart() |
|
|||
755 |
|
710 | |||
756 |
def init_ |
|
711 | def init_pdb(self): | |
757 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
712 | # Set calling of pdb on exceptions | |
758 | no_alias = {} |
|
713 | # self.call_pdb is a property | |
759 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
714 | self.call_pdb = self.pdb | |
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 |
|
|||
764 |
|
715 | |||
765 | # Make some aliases automatically |
|
716 | def _get_call_pdb(self): | |
766 | # Prepare list of shell aliases to auto-define |
|
717 | return self._call_pdb | |
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 |
|
718 | |||
810 | # Load default aliases |
|
719 | def _set_call_pdb(self,val): | |
811 | for alias, cmd in self.auto_alias: |
|
|||
812 | self.define_alias(alias,cmd) |
|
|||
813 |
|
720 | |||
814 | # Load user aliases |
|
721 | if val not in (0,1,False,True): | |
815 | for alias in self.alias: |
|
722 | raise ValueError,'new call_pdb value must be boolean' | |
816 | self.magic_alias(alias) |
|
|||
817 |
|
723 | |||
818 | def init_builtins(self): |
|
724 | # store value in instance | |
819 | self.builtin_trap = BuiltinTrap(self) |
|
725 | self._call_pdb = val | |
820 |
|
726 | |||
821 | def init_shadow_hist(self): |
|
727 | # notify the actual exception handlers | |
|
728 | self.InteractiveTB.call_pdb = val | |||
|
729 | if self.isthreaded: | |||
822 | try: |
|
730 | try: | |
823 | self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db") |
|
731 | self.sys_excepthook.call_pdb = val | |
824 | except exceptions.UnicodeDecodeError: |
|
732 | except: | |
825 | print "Your ipythondir can't be decoded to unicode!" |
|
733 | warn('Failed to activate pdb for threaded exception handler') | |
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) |
|
|||
831 |
|
734 | |||
832 | def init_inspector(self): |
|
735 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
833 | # Object inspector |
|
736 | 'Control auto-activation of pdb at exceptions') | |
834 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
|||
835 | PyColorize.ANSICodeColors, |
|
|||
836 | 'NoColor', |
|
|||
837 | self.object_info_string_level) |
|
|||
838 |
|
737 | |||
839 | def init_readline(self): |
|
738 | def debugger(self,force=False): | |
840 | """Command history completion/saving/reloading.""" |
|
739 | """Call the pydb/pdb debugger. | |
841 |
|
|
740 | ||
842 | self.rl_next_input = None |
|
741 | Keywords: | |
843 | self.rl_do_indent = False |
|
|||
844 |
|
|
742 | ||
845 | if not self.readline_use: |
|
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 | return |
|
750 | return | |
847 |
|
751 | |||
848 | import IPython.utils.rlineimpl as readline |
|
752 | if not hasattr(sys,'last_traceback'): | |
|
753 | error('No traceback has been produced, nothing to debug.') | |||
|
754 | return | |||
849 |
|
755 | |||
850 | if not readline.have_readline: |
|
756 | # use pydb if available | |
851 | self.has_readline = 0 |
|
757 | if debugger.has_pydb: | |
852 | self.readline = None |
|
758 | from pydb import pm | |
853 | # no point in bugging windows users with this every time: |
|
|||
854 | warn('Readline services not available on this platform.') |
|
|||
855 | 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 |
|
|
759 | 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) |
|
764 | #------------------------------------------------------------------------- | |
874 | # Or if libedit is used, load editrc. |
|
765 | # Things related to IPython's various namespaces | |
875 | inputrc_name = os.environ.get('INPUTRC') |
|
766 | #------------------------------------------------------------------------- | |
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 |
|
767 | |||
890 | self.has_readline = 1 |
|
768 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): | |
891 | self.readline = readline |
|
769 | # Create the namespace where the user will operate. user_ns is | |
892 | # save this in sys so embedded copies can restore it properly |
|
770 | # normally the only one used, and it is passed to the exec calls as | |
893 | sys.ipcompleter = self.Completer.complete |
|
771 | # the locals argument. But we do carry a user_global_ns namespace | |
894 | self.set_completer() |
|
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. | |||
895 |
|
776 | |||
896 | # Configure readline according to user's prefs |
|
777 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
897 | # This is only done if GNU readline is being used. If libedit |
|
778 | # level as a dict instead of a module. This is a manual fix, but I | |
898 | # is being used (as on Leopard) the readline config is |
|
779 | # should really track down where the problem is coming from. Alex | |
899 | # not run as the syntax for libedit is different. |
|
780 | # Schmolck reported this problem first. | |
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) |
|
|||
904 |
|
781 | |||
905 | # Remove some chars from the delimiters list. If we encounter |
|
782 | # A useful post by Alex Martelli on this topic: | |
906 | # unicode chars, discard them. |
|
783 | # Re: inconsistent value from __builtins__ | |
907 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
784 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
908 | delims = delims.translate(string._idmap, |
|
785 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
909 | self.readline_remove_delims) |
|
786 | # Gruppen: comp.lang.python | |
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. |
|
|||
918 |
|
787 | |||
919 | atexit.register(self.atexit_operations) |
|
788 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
920 | del atexit |
|
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? | |||
921 |
|
794 | |||
922 | # Configure auto-indent for all platforms |
|
795 | # Well, it's documented that '__builtins__' can be either a dictionary | |
923 | self.set_autoindent(self.autoindent) |
|
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:-(. | |||
924 |
|
801 | |||
925 | def init_prompts(self): |
|
802 | # These routines return properly built dicts as needed by the rest of | |
926 | # Initialize cache, set in/out prompts and printing system |
|
803 | # the code, and can also be used by extension writers to generate | |
927 | self.outputcache = CachedOutput(self, |
|
804 | # properly initialized namespaces. | |
928 | self.cache_size, |
|
805 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, | |
929 | self.pprint, |
|
806 | user_global_ns) | |
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) |
|
|||
937 |
|
807 | |||
938 | # user may have over-ridden the default print hook: |
|
808 | # Assign namespaces | |
939 | try: |
|
809 | # This is the namespace where all normal user variables live | |
940 | self.outputcache.__class__.display = self.hooks.display |
|
810 | self.user_ns = user_ns | |
941 | except AttributeError: |
|
811 | self.user_global_ns = user_global_ns | |
942 | pass |
|
|||
943 |
|
812 | |||
944 | def init_displayhook(self): |
|
813 | # An auxiliary namespace that checks what parts of the user_ns were | |
945 | self.display_trap = DisplayTrap(self, self.outputcache) |
|
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). | |||
|
888 | ||||
|
889 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |||
946 |
|
890 | |||
947 | def init_reload_doctest(self): |
|
|||
948 | # Do a proper resetting of doctest, including the necessary displayhook |
|
|||
949 | # monkeypatching |
|
|||
950 | try: |
|
891 | try: | |
951 | doctest_reload() |
|
892 | main_name = self.user_ns['__name__'] | |
952 |
except |
|
893 | except KeyError: | |
953 | warn("doctest module does not exist.") |
|
894 | raise KeyError('user_ns dictionary MUST have a "__name__" key') | |
|
895 | else: | |||
|
896 | sys.modules[main_name] = FakeModule(self.user_ns) | |||
954 |
|
897 | |||
955 | def init_magics(self): |
|
898 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): | |
956 | # Set user colors (don't do it in the constructor above so that it |
|
899 | """Return a valid local and global user interactive namespaces. | |
957 | # doesn't crash if colors option is invalid) |
|
|||
958 | self.magic_colors(self.colors) |
|
|||
959 |
|
|
900 | ||
960 | def init_pdb(self): |
|
901 | This builds a dict with the minimal information needed to operate as a | |
961 | # Set calling of pdb on exceptions |
|
902 | valid IPython user namespace, which you can pass to the various | |
962 | # self.call_pdb is a property |
|
903 | embedding classes in ipython. The default implementation returns the | |
963 | self.call_pdb = self.pdb |
|
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. | |||
964 |
|
|
911 | ||
965 | # def init_exec_commands(self): |
|
912 | Raises TypeError if the provided globals namespace is not a true dict. | |
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() |
|
|||
982 |
|
|
913 | ||
983 | # def load(self, mod): |
|
914 | :Parameters: | |
984 | # """ Load an extension. |
|
915 | user_ns : dict-like, optional | |
985 | # |
|
916 | The current user namespace. The items in this namespace should | |
986 | # Some modules should (or must) be 'load()':ed, rather than just imported. |
|
917 | be included in the output. If None, an appropriate blank | |
987 | # |
|
918 | namespace should be created. | |
988 | # Loading will do: |
|
919 | user_global_ns : dict, optional | |
989 | # |
|
920 | The current user global namespace. The items in this namespace | |
990 | # - run init_ipython(ip) |
|
921 | should be included in the output. If None, an appropriate | |
991 | # - run ipython_firstrun(ip) |
|
922 | blank namespace should be created. | |
992 | # """ |
|
923 | ||
993 | # |
|
924 | :Returns: | |
994 | # if mod in self.extensions: |
|
925 | A tuple pair of dictionary-like object to be used as the local namespace | |
995 | # # just to make sure we don't init it twice |
|
926 | of the interpreter and a dict to be used as the global namespace. | |
996 | # # note that if you 'load' a module that has already been |
|
927 | """ | |
997 | # # imported, init_ipython gets run anyway |
|
928 | ||
998 | # |
|
929 | if user_ns is None: | |
999 | # return self.extensions[mod] |
|
930 | # Set __name__ to __main__ to better match the behavior of the | |
1000 | # __import__(mod) |
|
931 | # normal interpreter. | |
1001 | # m = sys.modules[mod] |
|
932 | user_ns = {'__name__' :'__main__', | |
1002 | # if hasattr(m,'init_ipython'): |
|
933 | '__builtins__' : __builtin__, | |
1003 | # m.init_ipython(self) |
|
934 | } | |
1004 | # |
|
935 | else: | |
1005 | # if hasattr(m,'ipython_firstrun'): |
|
936 | user_ns.setdefault('__name__','__main__') | |
1006 | # already_loaded = self.db.get('firstrun_done', set()) |
|
937 | user_ns.setdefault('__builtins__',__builtin__) | |
1007 | # if mod not in already_loaded: |
|
938 | ||
1008 | # m.ipython_firstrun(self) |
|
939 | if user_global_ns is None: | |
1009 | # already_loaded.add(mod) |
|
940 | user_global_ns = user_ns | |
1010 | # self.db['firstrun_done'] = already_loaded |
|
941 | if type(user_global_ns) is not dict: | |
1011 | # |
|
942 | raise TypeError("user_global_ns must be a true dict; got %r" | |
1012 | # self.extensions[mod] = m |
|
943 | % type(user_global_ns)) | |
1013 | # return m |
|
944 | ||
|
945 | return user_ns, user_global_ns | |||
1014 |
|
946 | |||
1015 | def init_user_ns(self): |
|
947 | def init_user_ns(self): | |
1016 | """Initialize all user-visible namespaces to their minimum defaults. |
|
948 | """Initialize all user-visible namespaces to their minimum defaults. | |
@@ -1018,106 +950,213 b' class InteractiveShell(Component, Magic):' | |||||
1018 | Certain history lists are also initialized here, as they effectively |
|
950 | Certain history lists are also initialized here, as they effectively | |
1019 | act as user namespaces. |
|
951 | act as user namespaces. | |
1020 |
|
952 | |||
1021 | Notes |
|
953 | Notes | |
1022 | ----- |
|
954 | ----- | |
1023 | All data structures here are only filled in, they are NOT reset by this |
|
955 | All data structures here are only filled in, they are NOT reset by this | |
1024 | method. If they were not empty before, data will simply be added to |
|
956 | method. If they were not empty before, data will simply be added to | |
1025 | therm. |
|
957 | therm. | |
1026 | """ |
|
958 | """ | |
1027 | # The user namespace MUST have a pointer to the shell itself. |
|
959 | # The user namespace MUST have a pointer to the shell itself. | |
1028 | self.user_ns[self.name] = self |
|
960 | self.user_ns[self.name] = self | |
|
961 | ||||
|
962 | # Store myself as the public api!!! | |||
|
963 | self.user_ns['_ip'] = self | |||
|
964 | ||||
|
965 | # make global variables for user access to the histories | |||
|
966 | self.user_ns['_ih'] = self.input_hist | |||
|
967 | self.user_ns['_oh'] = self.output_hist | |||
|
968 | self.user_ns['_dh'] = self.dir_hist | |||
|
969 | ||||
|
970 | # user aliases to input and output histories | |||
|
971 | self.user_ns['In'] = self.input_hist | |||
|
972 | self.user_ns['Out'] = self.output_hist | |||
|
973 | ||||
|
974 | self.user_ns['_sh'] = shadowns | |||
|
975 | ||||
|
976 | # Put 'help' in the user namespace | |||
|
977 | try: | |||
|
978 | from site import _Helper | |||
|
979 | self.user_ns['help'] = _Helper() | |||
|
980 | except ImportError: | |||
|
981 | warn('help() not available - check site.py') | |||
|
982 | ||||
|
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 | |||
1029 |
|
1047 | |||
1030 | # Store myself as the public api!!! |
|
1048 | #------------------------------------------------------------------------- | |
1031 | self.user_ns['_ip'] = self |
|
1049 | # Things related to history management | |
|
1050 | #------------------------------------------------------------------------- | |||
1032 |
|
1051 | |||
1033 | # make global variables for user access to the histories |
|
1052 | def init_history(self): | |
1034 | self.user_ns['_ih'] = self.input_hist |
|
1053 | # List of input with multi-line handling. | |
1035 | self.user_ns['_oh'] = self.output_hist |
|
1054 | self.input_hist = InputList() | |
1036 | self.user_ns['_dh'] = self.dir_hist |
|
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() | |||
1037 |
|
1059 | |||
1038 | # user aliases to input and output histories |
|
1060 | # list of visited directories | |
1039 | self.user_ns['In'] = self.input_hist |
|
1061 | try: | |
1040 | self.user_ns['Out'] = self.output_hist |
|
1062 | self.dir_hist = [os.getcwd()] | |
|
1063 | except OSError: | |||
|
1064 | self.dir_hist = [] | |||
1041 |
|
1065 | |||
1042 | self.user_ns['_sh'] = shadowns |
|
1066 | # dict of output history | |
|
1067 | self.output_hist = {} | |||
1043 |
|
1068 | |||
1044 | # Put 'help' in the user namespace |
|
1069 | # Now the history file | |
1045 | try: |
|
1070 | try: | |
1046 | from site import _Helper |
|
1071 | histfname = 'history-%s' % self.profile | |
1047 | self.user_ns['help'] = _Helper() |
|
1072 | except AttributeError: | |
1048 | except ImportError: |
|
1073 | histfname = 'history' | |
1049 | warn('help() not available - check site.py') |
|
1074 | self.histfile = os.path.join(self.config.IPYTHONDIR, histfname) | |
1050 |
|
1075 | |||
1051 | def save_sys_module_state(self): |
|
1076 | # Fill the history zero entry, user counter starts at 1 | |
1052 | """Save the state of hooks in the sys module. |
|
1077 | self.input_hist.append('\n') | |
|
1078 | self.input_hist_raw.append('\n') | |||
1053 |
|
1079 | |||
1054 | This has to be called after self.user_ns is created. |
|
1080 | def init_shadow_hist(self): | |
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: |
|
1081 | try: | |
1062 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
1082 | self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db") | |
1063 |
except |
|
1083 | except exceptions.UnicodeDecodeError: | |
1064 | pass |
|
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 | |||
1065 |
|
1096 | |||
1066 | def restore_sys_module_state(self): |
|
|||
1067 | """Restore the state of the sys module.""" |
|
|||
1068 | try: |
|
1097 | try: | |
1069 | for k, v in self._orig_sys_module_state.items(): |
|
1098 | self.readline.write_history_file(self.histfile) | |
1070 | setattr(sys, k, v) |
|
1099 | except: | |
1071 | except AttributeError: |
|
1100 | print 'Unable to save IPython command history to file: ' + \ | |
1072 | pass |
|
1101 | `self.histfile` | |
|
1102 | ||||
|
1103 | def reloadhist(self): | |||
|
1104 | """Reload the input history from disk file.""" | |||
|
1105 | ||||
|
1106 | if self.has_readline: | |||
1073 | try: |
|
1107 | try: | |
1074 | delattr(sys, 'ipcompleter') |
|
1108 | self.readline.clear_history() | |
|
1109 | self.readline.read_history_file(self.shell.histfile) | |||
1075 | except AttributeError: |
|
1110 | except AttributeError: | |
1076 | pass |
|
1111 | 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 |
|
|||
1082 |
|
1112 | |||
1083 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
1113 | def history_saving_wrapper(self, func): | |
1084 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
1114 | """ Wrap func for readline history saving | |
1085 |
|
1115 | |||
1086 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
1116 | Convert func into callable that saves & restores | |
1087 | adding your function to one of these hooks, you can modify IPython's |
|
1117 | history around the call """ | |
1088 | behavior to call at runtime your own routines.""" |
|
|||
1089 |
|
1118 | |||
1090 | # At some point in the future, this should validate the hook before it |
|
1119 | if not self.has_readline: | |
1091 | # accepts it. Probably at least check that the hook takes the number |
|
1120 | return func | |
1092 | # of args it's supposed to. |
|
|||
1093 |
|
1121 | |||
1094 | f = new.instancemethod(hook,self,self.__class__) |
|
1122 | def wrapper(): | |
|
1123 | self.savehist() | |||
|
1124 | try: | |||
|
1125 | func() | |||
|
1126 | finally: | |||
|
1127 | readline.read_history_file(self.histfile) | |||
|
1128 | return wrapper | |||
1095 |
|
1129 | |||
1096 | # check if the hook is for strdispatcher first |
|
1130 | #------------------------------------------------------------------------- | |
1097 | if str_key is not None: |
|
1131 | # Things related to exception handling and tracebacks (not debugging) | |
1098 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
1132 | #------------------------------------------------------------------------- | |
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 |
|
1133 | |||
1108 | dp = getattr(self.hooks, name, None) |
|
1134 | def init_traceback_handlers(self, custom_exceptions): | |
1109 | if name not in IPython.core.hooks.__all__: |
|
1135 | # Syntax error handler. | |
1110 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) |
|
1136 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') | |
1111 | if not dp: |
|
|||
1112 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
|||
1113 |
|
1137 | |||
1114 | try: |
|
1138 | # The interactive one is initialized with an offset, meaning we always | |
1115 | dp.add(f,priority) |
|
1139 | # want to remove the topmost item in the traceback, which is our own | |
1116 | except AttributeError: |
|
1140 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1117 | # it was not commandchain, plain old func - replace |
|
1141 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1118 | dp = f |
|
1142 | color_scheme='NoColor', | |
|
1143 | tb_offset = 1) | |||
1119 |
|
1144 | |||
1120 | setattr(self.hooks,name, dp) |
|
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) | |||
|
1157 | ||||
|
1158 | # and add any custom exception handlers the user may have specified | |||
|
1159 | self.set_custom_exc(*custom_exceptions) | |||
1121 |
|
1160 | |||
1122 | def set_crash_handler(self, crashHandler): |
|
1161 | def set_crash_handler(self, crashHandler): | |
1123 | """Set the IPython crash handler. |
|
1162 | """Set the IPython crash handler. | |
@@ -1177,192 +1216,169 b' class InteractiveShell(Component, Magic):' | |||||
1177 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
1216 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
1178 | self.custom_exceptions = exc_tuple |
|
1217 | self.custom_exceptions = exc_tuple | |
1179 |
|
1218 | |||
1180 | def set_custom_completer(self,completer,pos=0): |
|
1219 | def excepthook(self, etype, value, tb): | |
1181 | """set_custom_completer(completer,pos=0) |
|
1220 | """One more defense for GUI apps that call sys.excepthook. | |
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. |
|
|||
1220 |
|
||||
1221 | Input: a string containing the name of the magic function to call and any |
|
|||
1222 | additional arguments to be passed to the magic. |
|
|||
1223 |
|
||||
1224 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
|||
1225 | prompt: |
|
|||
1226 |
|
||||
1227 | In[1]: %name -opt foo bar |
|
|||
1228 |
|
||||
1229 | To call a magic without arguments, simply use magic('name'). |
|
|||
1230 |
|
||||
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 | """ |
|
|||
1235 |
|
||||
1236 | args = arg_s.split(' ',1) |
|
|||
1237 | magic_name = args[0] |
|
|||
1238 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
|||
1239 |
|
||||
1240 | 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 |
|
|
1221 | ||
1253 | def define_magic(self, magicname, func): |
|
1222 | GUI frameworks like wxPython trap exceptions and call | |
1254 | """Expose own function as magic function for ipython |
|
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. | |||
1255 |
|
1228 | |||
1256 | def foo_impl(self,parameter_s=''): |
|
1229 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1257 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1230 | any app directly invokes sys.excepthook, it will look to the user like | |
1258 | print 'Magic function. Passed parameter is between < >:' |
|
1231 | IPython crashed. In order to work around this, we can disable the | |
1259 | print '<%s>' % parameter_s |
|
1232 | CrashHandler and replace it with this excepthook instead, which prints a | |
1260 | print 'The self object is:',self |
|
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. | |||
1261 |
|
1237 | |||
1262 | self.define_magic('foo',foo_impl) |
|
1238 | This hook should be used sparingly, only in places which are not likely | |
|
1239 | to be true IPython errors. | |||
1263 |
|
|
1240 | """ | |
|
1241 | self.showtraceback((etype,value,tb),tb_offset=0) | |||
1264 |
|
1242 | |||
1265 | import new |
|
1243 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): | |
1266 | im = new.instancemethod(func,self, self.__class__) |
|
1244 | """Display the exception that just occurred. | |
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 |
|
1245 | |||
1274 | Parameters |
|
1246 | If nothing is known about the exception, this is the method which | |
1275 | ---------- |
|
1247 | should be used throughout the code for presenting user tracebacks, | |
1276 | name : str |
|
1248 | rather than directly invoking the InteractiveTB object. | |
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 |
|
1249 | |||
1283 | from IPython.core import macro |
|
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.""" | |||
1284 |
|
1254 | |||
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 |
|
|||
1290 |
|
1255 | |||
1291 | def define_alias(self, name, cmd): |
|
1256 | # Though this won't be called by syntax errors in the input line, | |
1292 | """ Define a new alias.""" |
|
1257 | # there may be SyntaxError cases whith imported code. | |
1293 |
|
1258 | |||
1294 | if callable(cmd): |
|
1259 | try: | |
1295 | self.alias_table[name] = cmd |
|
1260 | if exc_tuple is None: | |
1296 | from IPython.core import shadowns |
|
1261 | etype, value, tb = sys.exc_info() | |
1297 | setattr(shadowns, name, cmd) |
|
1262 | else: | |
1298 | return |
|
1263 | etype, value, tb = exc_tuple | |
1299 |
|
1264 | |||
1300 | if isinstance(cmd, basestring): |
|
1265 | if etype is SyntaxError: | |
1301 | nargs = cmd.count('%s') |
|
1266 | self.showsyntaxerror(filename) | |
1302 | if nargs>0 and cmd.find('%l')>=0: |
|
1267 | elif etype is UsageError: | |
1303 | raise Exception('The %s and %l specifiers are mutually ' |
|
1268 | print "UsageError:", value | |
1304 | 'exclusive in alias definitions.') |
|
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 | |||
1305 |
|
1277 | |||
1306 | self.alias_table[name] = (nargs,cmd) |
|
1278 | if etype in self.custom_exceptions: | |
1307 | return |
|
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") | |||
1308 |
|
1287 | |||
1309 | self.alias_table[name] = cmd |
|
1288 | def showsyntaxerror(self, filename=None): | |
|
1289 | """Display the syntax error that just occurred. | |||
1310 |
|
|
1290 | ||
1311 | def ipalias(self,arg_s): |
|
1291 | This doesn't display a stack trace because there isn't one. | |
1312 | """Call an alias by name. |
|
|||
1313 |
|
1292 | |||
1314 | Input: a string containing the name of the alias to call and any |
|
1293 | If a filename is given, it is stuffed in the exception instead | |
1315 | additional arguments to be passed to the magic. |
|
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() | |||
1316 |
|
1298 | |||
1317 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
1299 | # See note about these variables in showtraceback() below | |
1318 | prompt: |
|
1300 | sys.last_type = etype | |
|
1301 | sys.last_value = value | |||
|
1302 | sys.last_traceback = last_traceback | |||
1319 |
|
1303 | |||
1320 | In[1]: name -opt foo bar |
|
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,[]) | |||
1321 |
|
1320 | |||
1322 | To call an alias without arguments, simply use ipalias('name'). |
|
1321 | def edit_syntax_error(self): | |
|
1322 | """The bottom half of the syntax error handler called in the main loop. | |||
1323 |
|
1323 | |||
1324 | This provides a proper Python function to call IPython's aliases in any |
|
1324 | Loop until syntax error is fixed or user cancels. | |
1325 | valid Python code you can type at the interpreter, including loops and |
|
1325 | """ | |
1326 | compound statements. It is added by IPython to the Python builtin |
|
|||
1327 | namespace upon initialization.""" |
|
|||
1328 |
|
1326 | |||
1329 | args = arg_s.split(' ',1) |
|
1327 | while self.SyntaxTB.last_syntax_error: | |
1330 | alias_name = args[0] |
|
1328 | # copy and clear last_syntax_error | |
|
1329 | err = self.SyntaxTB.clear_err_state() | |||
|
1330 | if not self._should_recompile(err): | |||
|
1331 | return | |||
1331 | try: |
|
1332 | try: | |
1332 | alias_args = args[1] |
|
1333 | # may set last_syntax_error again if a SyntaxError is raised | |
1333 | except IndexError: |
|
1334 | self.safe_execfile(err.filename,self.user_ns) | |
1334 | alias_args = '' |
|
1335 | except: | |
1335 | if alias_name in self.alias_table: |
|
1336 | self.showtraceback() | |
1336 | self.call_alias(alias_name,alias_args) |
|
|||
1337 | else: |
|
1337 | else: | |
1338 | error("Alias `%s` not found." % alias_name) |
|
1338 | try: | |
1339 |
|
1339 | f = file(err.filename) | ||
1340 | def system(self, cmd): |
|
1340 | try: | |
1341 | """Make a system call, using IPython.""" |
|
1341 | # This should be inside a display_trap block and I | |
1342 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) |
|
1342 | # think it is. | |
|
1343 | sys.displayhook(f.read()) | |||
|
1344 | finally: | |||
|
1345 | f.close() | |||
|
1346 | except: | |||
|
1347 | self.showtraceback() | |||
1343 |
|
1348 | |||
1344 |
def |
|
1349 | def _should_recompile(self,e): | |
1345 | """Execute a normal python statement in user namespace.""" |
|
1350 | """Utility routine for edit_syntax_error""" | |
1346 | with nested(self.builtin_trap, self.display_trap): |
|
|||
1347 | exec cmd in self.user_global_ns, self.user_ns |
|
|||
1348 |
|
1351 | |||
1349 | def ev(self, expr): |
|
1352 | if e.filename in ('<ipython console>','<input>','<string>', | |
1350 | """Evaluate python expression expr in user namespace. |
|
1353 | '<console>','<BackgroundJob compilation>', | |
|
1354 | None): | |||
1351 |
|
1355 | |||
1352 | Returns the result of evaluation |
|
1356 | return False | |
1353 |
|
|
1357 | try: | |
1354 | with nested(self.builtin_trap, self.display_trap): |
|
1358 | if (self.autoedit_syntax and | |
1355 | return eval(expr, self.user_global_ns, self.user_ns) |
|
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): |
|
1365 | def int0(x): | |
1358 | return getoutput(self.var_expand(cmd,depth=2), |
|
1366 | try: | |
1359 | header=self.system_header, |
|
1367 | return int(x) | |
1360 | verbose=self.system_verbose) |
|
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): |
|
1379 | #------------------------------------------------------------------------- | |
1363 | return getoutputerror(self.var_expand(cmd,depth=2), |
|
1380 | # Things related to tab completion | |
1364 | header=self.system_header, |
|
1381 | #------------------------------------------------------------------------- | |
1365 | verbose=self.system_verbose) |
|
|||
1366 |
|
1382 | |||
1367 | def complete(self, text): |
|
1383 | def complete(self, text): | |
1368 | """Return a sorted list of all possible completions on text. |
|
1384 | """Return a sorted list of all possible completions on text. | |
@@ -1410,34 +1426,111 b' class InteractiveShell(Component, Magic):' | |||||
1410 | #print "vars:",self.user_ns.keys() |
|
1426 | #print "vars:",self.user_ns.keys() | |
1411 | return outcomps |
|
1427 | return outcomps | |
1412 |
|
1428 | |||
1413 |
def set_completer |
|
1429 | def set_custom_completer(self,completer,pos=0): | |
1414 | if frame: |
|
1430 | """set_custom_completer(completer,pos=0) | |
1415 | self.Completer.namespace = frame.f_locals |
|
1431 | ||
1416 | self.Completer.global_namespace = frame.f_globals |
|
1432 | Adds a new custom completer function. | |
|
1433 | ||||
|
1434 | The position argument (defaults to 0) is the index in the completers | |||
|
1435 | list where you want the completer to be inserted.""" | |||
|
1436 | ||||
|
1437 | newcomp = new.instancemethod(completer,self.Completer, | |||
|
1438 | self.Completer.__class__) | |||
|
1439 | self.Completer.matchers.insert(pos,newcomp) | |||
|
1440 | ||||
|
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 | |||
1417 | else: |
|
1480 | else: | |
1418 | self.Completer.namespace = self.user_ns |
|
1481 | self.readline_startup_hook = readline.set_startup_hook | |
1419 | self.Completer.global_namespace = self.user_global_ns |
|
|||
1420 |
|
1482 | |||
1421 | def init_auto_alias(self): |
|
1483 | # Load user's initrc file (readline config) | |
1422 | """Define some aliases automatically. |
|
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) | |||
1423 |
|
1499 | |||
1424 | These are ALL parameter-less aliases""" |
|
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() | |||
1425 |
|
1505 | |||
1426 | for alias,cmd in self.auto_alias: |
|
1506 | # Configure readline according to user's prefs | |
1427 | self.define_alias(alias,cmd) |
|
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) | |||
1428 |
|
1514 | |||
1429 | def alias_table_validate(self,verbose=0): |
|
1515 | # Remove some chars from the delimiters list. If we encounter | |
1430 | """Update information about the alias table. |
|
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. | |||
1431 |
|
1528 | |||
1432 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1529 | atexit.register(self.atexit_operations) | |
|
1530 | del atexit | |||
1433 |
|
1531 | |||
1434 | no_alias = self.no_alias |
|
1532 | # Configure auto-indent for all platforms | |
1435 | for k in self.alias_table.keys(): |
|
1533 | self.set_autoindent(self.autoindent) | |
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) |
|
|||
1441 |
|
1534 | |||
1442 | def set_next_input(self, s): |
|
1535 | def set_next_input(self, s): | |
1443 | """ Sets the 'default' input string for the next command line. |
|
1536 | """ Sets the 'default' input string for the next command line. | |
@@ -1452,348 +1545,369 b' class InteractiveShell(Component, Magic):' | |||||
1452 |
|
1545 | |||
1453 | self.rl_next_input = s |
|
1546 | self.rl_next_input = s | |
1454 |
|
1547 | |||
1455 |
def |
|
1548 | def pre_readline(self): | |
1456 | """Set the autoindent flag, checking for readline support. |
|
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: |
|
1553 | #debugx('self.indent_current_nsp','pre_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 |
|
|||
1469 |
|
1554 | |||
1470 | def atexit_operations(self): |
|
1555 | if self.rl_do_indent: | |
1471 | """This will be executed at the time of exit. |
|
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 |
|
1565 | #------------------------------------------------------------------------- | |
1476 | # input history |
|
1566 | # Things related to magics | |
1477 | self.savehist() |
|
1567 | #------------------------------------------------------------------------- | |
1478 |
|
1568 | |||
1479 | # Cleanup all tempfiles left around |
|
1569 | def init_magics(self): | |
1480 | for tfile in self.tempfiles: |
|
1570 | # Set user colors (don't do it in the constructor above so that it | |
1481 | try: |
|
1571 | # doesn't crash if colors option is invalid) | |
1482 | os.unlink(tfile) |
|
1572 | self.magic_colors(self.colors) | |
1483 | except OSError: |
|
|||
1484 | pass |
|
|||
1485 |
|
1573 | |||
1486 | # Clear all user namespaces to release all references cleanly. |
|
1574 | def magic(self,arg_s): | |
1487 | self.reset() |
|
1575 | """Call a magic function by name. | |
1488 |
|
|
1576 | ||
1489 | # Run user hooks |
|
1577 | Input: a string containing the name of the magic function to call and any | |
1490 | self.hooks.shutdown_hook() |
|
1578 | additional arguments to be passed to the magic. | |
1491 |
|
|
1579 | ||
1492 | def reset(self): |
|
1580 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
1493 | """Clear all internal namespaces. |
|
1581 | prompt: | |
1494 |
|
1582 | |||
1495 | Note that this is much more aggressive than %reset, since it clears |
|
1583 | In[1]: %name -opt foo bar | |
1496 | fully all namespaces, as well as all input/output lists. |
|
|||
1497 | """ |
|
|||
1498 | for ns in self.ns_refs_table: |
|
|||
1499 | ns.clear() |
|
|||
1500 |
|
|
1584 | ||
1501 | # Clear input and output histories |
|
1585 | To call a magic without arguments, simply use magic('name'). | |
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 |
|
1586 | |||
1508 | def savehist(self): |
|
1587 | This provides a proper Python function to call IPython's magics in any | |
1509 | """Save input history to a file (via readline library).""" |
|
1588 | valid Python code you can type at the interpreter, including loops and | |
|
1589 | compound statements. | |||
|
1590 | """ | |||
1510 |
|
1591 | |||
1511 | if not self.has_readline: |
|
1592 | args = arg_s.split(' ',1) | |
1512 | return |
|
1593 | magic_name = args[0] | |
|
1594 | magic_name = magic_name.lstrip(self.ESC_MAGIC) | |||
1513 |
|
1595 | |||
1514 | try: |
|
1596 | try: | |
1515 | self.readline.write_history_file(self.histfile) |
|
1597 | magic_args = args[1] | |
1516 | except: |
|
1598 | except IndexError: | |
1517 | print 'Unable to save IPython command history to file: ' + \ |
|
1599 | magic_args = '' | |
1518 | `self.histfile` |
|
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 | |||
1519 |
|
1608 | |||
1520 | def reloadhist(self): |
|
1609 | def define_magic(self, magicname, func): | |
1521 | """Reload the input history from disk file.""" |
|
1610 | """Expose own function as magic function for ipython | |
1522 |
|
1611 | |||
1523 | if self.has_readline: |
|
1612 | def foo_impl(self,parameter_s=''): | |
1524 | try: |
|
1613 | 'My very own magic!. (Use docstrings, IPython reads them).' | |
1525 | self.readline.clear_history() |
|
1614 | print 'Magic function. Passed parameter is between < >:' | |
1526 | self.readline.read_history_file(self.shell.histfile) |
|
1615 | print '<%s>' % parameter_s | |
1527 | except AttributeError: |
|
1616 | print 'The self object is:',self | |
1528 | pass |
|
|||
1529 |
|
1617 | |||
|
1618 | self.define_magic('foo',foo_impl) | |||
|
1619 | """ | |||
1530 |
|
1620 | |||
1531 | def history_saving_wrapper(self, func): |
|
1621 | import new | |
1532 | """ Wrap func for readline history saving |
|
1622 | im = new.instancemethod(func,self, self.__class__) | |
|
1623 | old = getattr(self, "magic_" + magicname, None) | |||
|
1624 | setattr(self, "magic_" + magicname, im) | |||
|
1625 | return old | |||
1533 |
|
1626 | |||
1534 | Convert func into callable that saves & restores |
|
1627 | #------------------------------------------------------------------------- | |
1535 | history around the call """ |
|
1628 | # Things related to macros | |
|
1629 | #------------------------------------------------------------------------- | |||
1536 |
|
1630 | |||
1537 | if not self.has_readline: |
|
1631 | def define_macro(self, name, themacro): | |
1538 | return func |
|
1632 | """Define a new macro | |
1539 |
|
1633 | |||
1540 |
|
|
1634 | Parameters | |
1541 | self.savehist() |
|
1635 | ---------- | |
1542 |
|
|
1636 | name : str | |
1543 |
|
|
1637 | The name of the macro. | |
1544 | finally: |
|
1638 | themacro : str or Macro | |
1545 | readline.read_history_file(self.histfile) |
|
1639 | The action to do upon invoking the macro. If a string, a new | |
1546 | return wrapper |
|
1640 | Macro object is created by passing the string to it. | |
|
1641 | """ | |||
1547 |
|
|
1642 | ||
1548 | def pre_readline(self): |
|
1643 | from IPython.core import macro | |
1549 | """readline hook to be used at the start of each line. |
|
|||
1550 |
|
1644 | |||
1551 | Currently it handles auto-indent only.""" |
|
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 | |||
1552 |
|
1650 | |||
1553 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1651 | #------------------------------------------------------------------------- | |
|
1652 | # Things related to the running of system commands | |||
|
1653 | #------------------------------------------------------------------------- | |||
1554 |
|
1654 | |||
1555 | if self.rl_do_indent: |
|
1655 | def system(self, cmd): | |
1556 | self.readline.insert_text(self.indent_current_str()) |
|
1656 | """Make a system call, using IPython.""" | |
1557 | if self.rl_next_input is not None: |
|
1657 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) | |
1558 | self.readline.insert_text(self.rl_next_input) |
|
|||
1559 | self.rl_next_input = None |
|
|||
1560 |
|
1658 | |||
1561 | def ask_yes_no(self,prompt,default=True): |
|
1659 | #------------------------------------------------------------------------- | |
1562 | if self.quiet: |
|
1660 | # Things related to aliases | |
1563 | return True |
|
1661 | #------------------------------------------------------------------------- | |
1564 | return ask_yes_no(prompt,default) |
|
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 | |||
|
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] | |||
1565 |
|
1716 | |||
1566 | def new_main_mod(self,ns=None): |
|
1717 | # Load default aliases | |
1567 | """Return a new 'main' module object for user code execution. |
|
1718 | for alias, cmd in self.auto_alias: | |
1568 | """ |
|
1719 | self.define_alias(alias,cmd) | |
1569 | main_mod = self._user_main_module |
|
|||
1570 | init_fakemod_dict(main_mod,ns) |
|
|||
1571 | return main_mod |
|
|||
1572 |
|
1720 | |||
1573 | def cache_main_mod(self,ns,fname): |
|
1721 | # Load user aliases | |
1574 | """Cache a main module's namespace. |
|
1722 | for alias in self.alias: | |
|
1723 | self.magic_alias(alias) | |||
1575 |
|
1724 | |||
1576 | When scripts are executed via %run, we must keep a reference to the |
|
1725 | def call_alias(self,alias,rest=''): | |
1577 | namespace of their __main__ module (a FakeModule instance) around so |
|
1726 | """Call an alias given its name and the rest of the line. | |
1578 | that Python doesn't clear it, rendering objects defined therein |
|
|||
1579 | useless. |
|
|||
1580 |
|
1727 | |||
1581 | This method keeps said reference in a private dict, keyed by the |
|
1728 | This is only used to provide backwards compatibility for users of | |
1582 | absolute path of the module object (which corresponds to the script |
|
1729 | ipalias(), use of which is not recommended for anymore.""" | |
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. |
|
|||
1587 |
|
1730 | |||
1588 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
1731 | # Now call the macro, evaluating in the user's namespace | |
1589 | because of how Python tears down modules (it hard-sets all their |
|
1732 | cmd = self.transform_alias(alias, rest) | |
1590 | references to None without regard for reference counts). This method |
|
1733 | try: | |
1591 | must therefore make a *copy* of the given namespace, to allow the |
|
1734 | self.system(cmd) | |
1592 | original module's __dict__ to be cleared and reused. |
|
1735 | except: | |
|
1736 | self.showtraceback() | |||
1593 |
|
1737 | |||
|
1738 | def define_alias(self, name, cmd): | |||
|
1739 | """ Define a new alias.""" | |||
1594 |
|
1740 | |||
1595 | Parameters |
|
1741 | if callable(cmd): | |
1596 | ---------- |
|
1742 | self.alias_table[name] = cmd | |
1597 | ns : a namespace (a dict, typically) |
|
1743 | from IPython.core import shadowns | |
|
1744 | setattr(shadowns, name, cmd) | |||
|
1745 | return | |||
1598 |
|
1746 | |||
1599 | fname : str |
|
1747 | if isinstance(cmd, basestring): | |
1600 | Filename associated with the namespace. |
|
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.') | |||
1601 |
|
1752 | |||
1602 | Examples |
|
1753 | self.alias_table[name] = (nargs,cmd) | |
1603 | -------- |
|
1754 | return | |
1604 |
|
1755 | |||
1605 | In [10]: import IPython |
|
1756 | self.alias_table[name] = cmd | |
1606 |
|
1757 | |||
1607 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
1758 | def ipalias(self,arg_s): | |
|
1759 | """Call an alias by name. | |||
1608 |
|
1760 | |||
1609 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
1761 | Input: a string containing the name of the alias to call and any | |
1610 | Out[12]: True |
|
1762 | additional arguments to be passed to the magic. | |
1611 | """ |
|
|||
1612 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
|||
1613 |
|
|
1763 | ||
1614 | def clear_main_mod_cache(self): |
|
1764 | ipalias('name -opt foo bar') is equivalent to typing at the ipython | |
1615 | """Clear the cache of main modules. |
|
1765 | prompt: | |
1616 |
|
1766 | |||
1617 | Mainly for use by utilities like %reset. |
|
1767 | In[1]: name -opt foo bar | |
1618 |
|
1768 | |||
1619 | Examples |
|
1769 | To call an alias without arguments, simply use ipalias('name'). | |
1620 | -------- |
|
|||
1621 |
|
1770 | |||
1622 | In [15]: import IPython |
|
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.""" | |||
1623 |
|
1775 | |||
1624 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
1776 | args = arg_s.split(' ',1) | |
|
1777 | alias_name = args[0] | |||
|
1778 | try: | |||
|
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) | |||
1625 |
|
1786 | |||
1626 | In [17]: len(_ip._main_ns_cache) > 0 |
|
1787 | def expand_alias(self, line): | |
1627 | Out[17]: True |
|
1788 | """ Expand an alias in the command line | |
1628 |
|
1789 | |||
1629 | In [18]: _ip.clear_main_mod_cache() |
|
1790 | Returns the provided command line, possibly with the first word | |
|
1791 | (command) translated according to alias expansion rules. | |||
1630 |
|
1792 | |||
1631 | In [19]: len(_ip._main_ns_cache) == 0 |
|
1793 | [ipython]|16> _ip.expand_aliases("np myfile.txt") | |
1632 | Out[19]: True |
|
1794 | <16> 'q:/opt/np/notepad++.exe myfile.txt' | |
1633 | """ |
|
1795 | """ | |
1634 | self._main_ns_cache.clear() |
|
|||
1635 |
|
||||
1636 | def _should_recompile(self,e): |
|
|||
1637 | """Utility routine for edit_syntax_error""" |
|
|||
1638 |
|
||||
1639 | if e.filename in ('<ipython console>','<input>','<string>', |
|
|||
1640 | '<console>','<BackgroundJob compilation>', |
|
|||
1641 | None): |
|
|||
1642 |
|
||||
1643 | return False |
|
|||
1644 | 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 |
|
|||
1651 |
|
1796 | |||
1652 | def int0(x): |
|
1797 | pre,fn,rest = self.split_user_input(line) | |
1653 | try: |
|
1798 | res = pre + self.expand_aliases(fn, rest) | |
1654 |
|
|
1799 | return res | |
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 |
|
|||
1665 |
|
1800 | |||
1666 | def edit_syntax_error(self): |
|
1801 | def expand_aliases(self, fn, rest): | |
1667 | """The bottom half of the syntax error handler called in the main loop. |
|
1802 | """Expand multiple levels of aliases: | |
1668 |
|
1803 | |||
1669 | Loop until syntax error is fixed or user cancels. |
|
1804 | if: | |
1670 | """ |
|
|||
1671 |
|
1805 | |||
1672 | while self.SyntaxTB.last_syntax_error: |
|
1806 | alias foo bar /tmp | |
1673 | # copy and clear last_syntax_error |
|
1807 | alias baz foo | |
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() |
|
|||
1682 | 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 |
|
1808 | |||
1694 | def showsyntaxerror(self, filename=None): |
|
1809 | then: | |
1695 | """Display the syntax error that just occurred. |
|
|||
1696 |
|
1810 | |||
1697 | This doesn't display a stack trace because there isn't one. |
|
1811 | baz huhhahhei -> bar /tmp huhhahhei | |
1698 |
|
1812 | |||
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). |
|
|||
1702 | """ |
|
1813 | """ | |
1703 | etype, value, last_traceback = sys.exc_info() |
|
1814 | line = fn + " " + rest | |
1704 |
|
1815 | |||
1705 | # See note about these variables in showtraceback() below |
|
1816 | done = set() | |
1706 | sys.last_type = etype |
|
1817 | while 1: | |
1707 | sys.last_value = value |
|
1818 | pre,fn,rest = prefilter.splitUserInput(line, | |
1708 | sys.last_traceback = last_traceback |
|
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) | |||
1709 |
|
1825 | |||
1710 | if filename and etype is SyntaxError: |
|
1826 | l2 = self.transform_alias(fn,rest) | |
1711 | # Work hard to stuff the correct filename in the exception |
|
1827 | # dir -> dir | |
1712 | try: |
|
1828 | # print "alias",line, "->",l2 #dbg | |
1713 | msg, (dummy_filename, lineno, offset, line) = value |
|
1829 | if l2 == line: | |
1714 | except: |
|
1830 | break | |
1715 | # Not the format we expect; leave it alone |
|
1831 | # ls -> ls -F should not recurse forever | |
1716 | pass |
|
1832 | if l2.split(None,1)[0] == line.split(None,1)[0]: | |
1717 |
|
|
1833 | line = l2 | |
1718 |
|
|
1834 | break | |
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,[]) |
|
|||
1726 |
|
1835 | |||
1727 | def debugger(self,force=False): |
|
1836 | line=l2 | |
1728 | """Call the pydb/pdb debugger. |
|
|||
1729 |
|
1837 | |||
1730 | Keywords: |
|
|||
1731 |
|
1838 | |||
1732 | - force(False): by default, this routine checks the instance call_pdb |
|
1839 | # print "al expand to",line #dbg | |
1733 | flag and does not actually invoke the debugger if the flag is false. |
|
1840 | else: | |
1734 | The 'force' option forces the debugger to activate even if the flag |
|
1841 | break | |
1735 | is false. |
|
|||
1736 | """ |
|
|||
1737 |
|
1842 | |||
1738 | if not (force or self.call_pdb): |
|
1843 | return line | |
1739 | return |
|
|||
1740 |
|
1844 | |||
1741 | if not hasattr(sys,'last_traceback'): |
|
1845 | def transform_alias(self, alias,rest=''): | |
1742 | error('No traceback has been produced, nothing to debug.') |
|
1846 | """ Transform alias to system command string. | |
1743 | return |
|
1847 | """ | |
|
1848 | trg = self.alias_table[alias] | |||
1744 |
|
1849 | |||
1745 | # use pydb if available |
|
1850 | nargs,cmd = trg | |
1746 | if debugger.has_pydb: |
|
1851 | # print trg #dbg | |
1747 | from pydb import pm |
|
1852 | if ' ' in cmd and os.path.isfile(cmd): | |
|
1853 | cmd = '"%s"' % cmd | |||
|
1854 | ||||
|
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) | |||
1748 | else: |
|
1862 | else: | |
1749 | # fallback to our internal debugger |
|
1863 | # Handle aliases with positional arguments | |
1750 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
1864 | args = rest.split(None,nargs) | |
1751 | self.history_saving_wrapper(pm)() |
|
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 | |||
1752 |
|
1873 | |||
1753 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1874 | def init_auto_alias(self): | |
1754 | """Display the exception that just occurred. |
|
1875 | """Define some aliases automatically. | |
1755 |
|
1876 | |||
1756 | If nothing is known about the exception, this is the method which |
|
1877 | These are ALL parameter-less aliases""" | |
1757 | should be used throughout the code for presenting user tracebacks, |
|
|||
1758 | rather than directly invoking the InteractiveTB object. |
|
|||
1759 |
|
1878 | |||
1760 | A specific showsyntaxerror() also exists, but this method can take |
|
1879 | for alias,cmd in self.auto_alias: | |
1761 | care of calling it if needed, so unless you are explicitly catching a |
|
1880 | self.define_alias(alias,cmd) | |
1762 | SyntaxError exception, don't try to analyze the stack manually and |
|
|||
1763 | simply call this method.""" |
|
|||
1764 |
|
1881 | |||
|
1882 | def alias_table_validate(self,verbose=0): | |||
|
1883 | """Update information about the alias table. | |||
1765 |
|
1884 | |||
1766 | # Though this won't be called by syntax errors in the input line, |
|
1885 | In particular, make sure no Python keywords/builtins are in it.""" | |
1767 | # there may be SyntaxError cases whith imported code. |
|
|||
1768 |
|
1886 | |||
1769 | try: |
|
1887 | no_alias = self.no_alias | |
1770 | if exc_tuple is None: |
|
1888 | for k in self.alias_table.keys(): | |
1771 | etype, value, tb = sys.exc_info() |
|
1889 | if k in no_alias: | |
1772 | else: |
|
1890 | del self.alias_table[k] | |
1773 | etype, value, tb = exc_tuple |
|
1891 | if verbose: | |
|
1892 | print ("Deleting alias <%s>, it's a Python " | |||
|
1893 | "keyword or builtin." % k) | |||
1774 |
|
1894 | |||
1775 | if etype is SyntaxError: |
|
1895 | #------------------------------------------------------------------------- | |
1776 | self.showsyntaxerror(filename) |
|
1896 | # Things related to the running of code | |
1777 | elif etype is UsageError: |
|
1897 | #------------------------------------------------------------------------- | |
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 |
|
1898 | |||
1788 | if etype in self.custom_exceptions: |
|
1899 | def ex(self, cmd): | |
1789 | self.CustomTB(etype,value,tb) |
|
1900 | """Execute a normal python statement in user namespace.""" | |
1790 | else: |
|
1901 | with nested(self.builtin_trap, self.display_trap): | |
1791 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1902 | exec cmd in self.user_global_ns, self.user_ns | |
1792 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1903 | ||
1793 | # pdb mucks up readline, fix it back |
|
1904 | def ev(self, expr): | |
1794 | self.set_completer() |
|
1905 | """Evaluate python expression expr in user namespace. | |
1795 | except KeyboardInterrupt: |
|
1906 | ||
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 | def mainloop(self, banner=None): |
|
1912 | def mainloop(self, banner=None): | |
1799 | """Start the mainloop. |
|
1913 | """Start the mainloop. | |
@@ -1978,200 +2092,170 b' class InteractiveShell(Component, Magic):' | |||||
1978 | # We are off again... |
|
2092 | # We are off again... | |
1979 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
2093 | __builtin__.__dict__['__IPYTHON__active'] -= 1 | |
1980 |
|
2094 | |||
1981 | def excepthook(self, etype, value, tb): |
|
2095 | def safe_execfile(self,fname,*where,**kw): | |
1982 | """One more defense for GUI apps that call sys.excepthook. |
|
2096 | """A safe version of the builtin execfile(). | |
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) |
|
|||
2004 |
|
||||
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 |
|
|||
2018 |
|
|
2097 | ||
2019 | def expand_aliases(self, fn, rest): |
|
2098 | This version will never throw an exception, and knows how to handle | |
2020 | """Expand multiple levels of aliases: |
|
2099 | ipython logs as well. | |
2021 |
|
2100 | |||
2022 | if: |
|
2101 | :Parameters: | |
|
2102 | fname : string | |||
|
2103 | Name of the file to be executed. | |||
2023 |
|
2104 | |||
2024 | alias foo bar /tmp |
|
2105 | where : tuple | |
2025 | alias baz foo |
|
2106 | One or two namespaces, passed to execfile() as (globals,locals). | |
|
2107 | If only one is given, it is passed as both. | |||
2026 |
|
2108 | |||
2027 |
|
|
2109 | :Keywords: | |
|
2110 | islog : boolean (False) | |||
2028 |
|
2111 | |||
2029 | baz huhhahhei -> bar /tmp huhhahhei |
|
2112 | quiet : boolean (True) | |
2030 |
|
2113 | |||
|
2114 | exit_ignore : boolean (False) | |||
2031 | """ |
|
2115 | """ | |
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) |
|
|||
2043 |
|
||||
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 |
|
2116 | |||
2054 | line=l2 |
|
2117 | def syspath_cleanup(): | |
2055 |
|
2118 | """Internal cleanup routine for sys.path.""" | ||
2056 |
|
|
2119 | if add_dname: | |
2057 | # print "al expand to",line #dbg |
|
2120 | try: | |
2058 | else: |
|
2121 | sys.path.remove(dname) | |
2059 |
|
|
2122 | except ValueError: | |
2060 |
|
2123 | # For some reason the user has already removed it, ignore. | ||
2061 | return line |
|
2124 | pass | |
2062 |
|
2125 | |||
2063 | def transform_alias(self, alias,rest=''): |
|
2126 | fname = os.path.expanduser(fname) | |
2064 | """ Transform alias to system command string. |
|
|||
2065 | """ |
|
|||
2066 | trg = self.alias_table[alias] |
|
|||
2067 |
|
2127 | |||
2068 | nargs,cmd = trg |
|
2128 | # Find things also in current directory. This is needed to mimic the | |
2069 | # print trg #dbg |
|
2129 | # behavior of running a script from the system command line, where | |
2070 | if ' ' in cmd and os.path.isfile(cmd): |
|
2130 | # Python inserts the script's directory into sys.path | |
2071 | cmd = '"%s"' % cmd |
|
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 | |||
2072 |
|
2136 | |||
2073 | # Expand the %l special to be the user's input line |
|
2137 | try: | |
2074 | if cmd.find('%l') >= 0: |
|
2138 | xfile = open(fname) | |
2075 | cmd = cmd.replace('%l',rest) |
|
2139 | except: | |
2076 | rest = '' |
|
2140 | print >> Term.cerr, \ | |
2077 | if nargs==0: |
|
2141 | 'Could not open file <%s> for safe execution.' % fname | |
2078 | # Simple, argument-less aliases |
|
2142 | syspath_cleanup() | |
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 |
|
|
2143 | 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 |
|
|||
2091 |
|
2144 | |||
2092 | def call_alias(self,alias,rest=''): |
|
2145 | kw.setdefault('islog',0) | |
2093 | """Call an alias given its name and the rest of the line. |
|
2146 | kw.setdefault('quiet',1) | |
2094 |
|
2147 | kw.setdefault('exit_ignore',0) | ||
2095 | This is only used to provide backwards compatibility for users of |
|
|||
2096 | ipalias(), use of which is not recommended for anymore.""" |
|
|||
2097 |
|
2148 | |||
2098 | # Now call the macro, evaluating in the user's namespace |
|
2149 | first = xfile.readline() | |
2099 | cmd = self.transform_alias(alias, rest) |
|
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() | |||
2100 | try: |
|
2158 | try: | |
2101 | self.system(cmd) |
|
2159 | globs,locs = where[0:2] | |
2102 | except: |
|
2160 | except: | |
2103 | self.showtraceback() |
|
2161 | try: | |
2104 |
|
2162 | globs = locs = where[0] | ||
2105 | def indent_current_str(self): |
|
2163 | except: | |
2106 | """return the current level of indentation as a string""" |
|
2164 | globs = locs = globals() | |
2107 | return self.indent_current_nsp * ' ' |
|
2165 | badblocks = [] | |
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 |
|
2166 | |||
2120 | if line[-1] == ':': |
|
2167 | # we also need to identify indented blocks of code when replaying | |
2121 | self.indent_current_nsp += 4 |
|
2168 | # logs and put them together before passing them to an exec | |
2122 | elif dedent_re.match(line): |
|
2169 | # statement. This takes a bit of regexp and look-ahead work in the | |
2123 | self.indent_current_nsp -= 4 |
|
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 | |||
2124 | else: |
|
2185 | else: | |
2125 | self.indent_current_nsp = 0 |
|
2186 | # build a block of code (maybe a single line) for execution | |
2126 |
|
2187 | block = line | ||
2127 | def push(self, variables, interactive=True): |
|
2188 | try: | |
2128 | """Inject a group of variables into the IPython user namespace. |
|
2189 | next = filelines[lnum] # lnum has already incremented | |
2129 |
|
2190 | except: | ||
2130 | Parameters |
|
2191 | next = None | |
2131 | ---------- |
|
2192 | while next and indent_re.match(next): | |
2132 | variables : dict, str or list/tuple of str |
|
2193 | block += next | |
2133 | The variables to inject into the user's namespace. If a dict, |
|
2194 | lnum += 1 | |
2134 | a simple update is done. If a str, the string is assumed to |
|
2195 | try: | |
2135 | have variable names separated by spaces. A list/tuple of str |
|
2196 | next = filelines[lnum] | |
2136 | can also be used to give the variable names. If just the variable |
|
2197 | except: | |
2137 | names are give (list/tuple/str) then the variable values looked |
|
2198 | next = None | |
2138 | up in the callers frame. |
|
2199 | # now execute the block of one or more lines | |
2139 | interactive : bool |
|
2200 | try: | |
2140 | If True (default), the variables will be listed with the ``who`` |
|
2201 | exec block in globs,locs | |
2141 | magic. |
|
2202 | except SystemExit: | |
2142 | """ |
|
2203 | pass | |
2143 | vdict = None |
|
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) | |||
2144 |
|
2213 | |||
2145 | # We need a dict of name/value pairs to do namespace updates. |
|
2214 | for badline in badblocks: | |
2146 | if isinstance(variables, dict): |
|
2215 | print >> sys.stderr, badline | |
2147 | vdict = variables |
|
2216 | else: # regular file execution | |
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: |
|
|||
2156 |
|
|
2217 | try: | |
2157 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
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] | |||
2158 | except: |
|
2225 | except: | |
2159 | print ('Could not get variable %s from %s' % |
|
2226 | try: | |
2160 | (name,cf.f_code.co_name)) |
|
2227 | globs = locs = where[0] | |
|
2228 | except: | |||
|
2229 | globs = locs = globals() | |||
|
2230 | exec file(fname) in globs,locs | |||
2161 | else: |
|
2231 | else: | |
2162 | raise ValueError('variables must be a dict/str/list/tuple') |
|
2232 | execfile(fname,*where) | |
2163 |
|
2233 | except SyntaxError: | ||
2164 | # Propagate variables to user namespace |
|
2234 | self.showsyntaxerror() | |
2165 | self.user_ns.update(vdict) |
|
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 | |||
2166 |
|
2244 | |||
2167 | # And configure interactive visibility |
|
2245 | if sys.version_info[:2] > (2,5): | |
2168 | config_ns = self.user_config_ns |
|
2246 | if status.message!=0 and not kw['exit_ignore']: | |
2169 | if interactive: |
|
2247 | show = True | |
2170 | for name, val in vdict.iteritems(): |
|
|||
2171 | config_ns.pop(name, None) |
|
|||
2172 | else: |
|
2248 | else: | |
2173 | for name,val in vdict.iteritems(): |
|
2249 | if status.code and not kw['exit_ignore']: | |
2174 | config_ns[name] = val |
|
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 | def cleanup_ipy_script(self, script): |
|
2260 | def cleanup_ipy_script(self, script): | |
2177 | """Make a script safe for self.runlines() |
|
2261 | """Make a script safe for self.runlines() | |
@@ -2383,13 +2467,31 b' class InteractiveShell(Component, Magic):' | |||||
2383 |
|
2467 | |||
2384 | #print 'push line: <%s>' % line # dbg |
|
2468 | #print 'push line: <%s>' % line # dbg | |
2385 | for subline in line.splitlines(): |
|
2469 | for subline in line.splitlines(): | |
2386 | self.autoindent_update(subline) |
|
2470 | self._autoindent_update(subline) | |
2387 | self.buffer.append(line) |
|
2471 | self.buffer.append(line) | |
2388 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
2472 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
2389 | if not more: |
|
2473 | if not more: | |
2390 | self.resetbuffer() |
|
2474 | self.resetbuffer() | |
2391 | return more |
|
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 | def split_user_input(self, line): |
|
2495 | def split_user_input(self, line): | |
2394 | # This is really a hold-over to support ipapi and some extensions |
|
2496 | # This is really a hold-over to support ipapi and some extensions | |
2395 | return prefilter.splitUserInput(line) |
|
2497 | return prefilter.splitUserInput(line) | |
@@ -2425,47 +2527,121 b' class InteractiveShell(Component, Magic):' | |||||
2425 | self.ask_exit() |
|
2527 | self.ask_exit() | |
2426 | return "" |
|
2528 | return "" | |
2427 |
|
2529 | |||
2428 | # Try to be reasonably smart about not re-indenting pasted input more |
|
2530 | # 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 |
|
2531 | # than necessary. We do this by trimming out the auto-indent initial | |
2430 | # spaces, if the user's actual input started itself with whitespace. |
|
2532 | # spaces, if the user's actual input started itself with whitespace. | |
2431 | #debugx('self.buffer[-1]') |
|
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: |
|
2626 | def init_handlers(self): | |
2434 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
2627 | # escapes for automatic behavior on the command line | |
2435 | line = line[self.indent_current_nsp:] |
|
2628 | self.ESC_SHELL = '!' | |
2436 | self.indent_current_nsp = 0 |
|
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 = '/' | |||
2437 |
|
2635 | |||
2438 | # store the unfiltered input before the user has any chance to modify |
|
2636 | # And their associated handlers | |
2439 | # it. |
|
2637 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, | |
2440 | if line.strip(): |
|
2638 | self.ESC_QUOTE : self.handle_auto, | |
2441 | if continue_prompt: |
|
2639 | self.ESC_QUOTE2 : self.handle_auto, | |
2442 | self.input_hist_raw[-1] += '%s\n' % line |
|
2640 | self.ESC_MAGIC : self.handle_magic, | |
2443 | if self.has_readline: # and some config option is set? |
|
2641 | self.ESC_HELP : self.handle_help, | |
2444 | try: |
|
2642 | self.ESC_SHELL : self.handle_shell_escape, | |
2445 | histlen = self.readline.get_current_history_length() |
|
2643 | self.ESC_SH_CAP : self.handle_shell_escape, | |
2446 |
|
|
2644 | } | |
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 |
|
|||
2469 |
|
2645 | |||
2470 | def _prefilter(self, line, continue_prompt): |
|
2646 | def _prefilter(self, line, continue_prompt): | |
2471 | """Calls different preprocessors, depending on the form of line.""" |
|
2647 | """Calls different preprocessors, depending on the form of line.""" | |
@@ -2732,6 +2908,20 b' class InteractiveShell(Component, Magic):' | |||||
2732 | # The input cache shouldn't be updated |
|
2908 | # The input cache shouldn't be updated | |
2733 | return line_info.line |
|
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 | def var_expand(self,cmd,depth=0): |
|
2925 | def var_expand(self,cmd,depth=0): | |
2736 | """Expand python variables in a string. |
|
2926 | """Expand python variables in a string. | |
2737 |
|
2927 | |||
@@ -2776,6 +2966,15 b' class InteractiveShell(Component, Magic):' | |||||
2776 | """Write a string to the default error output""" |
|
2966 | """Write a string to the default error output""" | |
2777 | Term.cerr.write(data) |
|
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 | def ask_exit(self): |
|
2978 | def ask_exit(self): | |
2780 | """ Call for exiting. Can be overiden and used as a callback. """ |
|
2979 | """ Call for exiting. Can be overiden and used as a callback. """ | |
2781 | self.exit_now = True |
|
2980 | self.exit_now = True | |
@@ -2790,169 +2989,30 b' class InteractiveShell(Component, Magic):' | |||||
2790 | else: |
|
2989 | else: | |
2791 | self.ask_exit() |
|
2990 | self.ask_exit() | |
2792 |
|
2991 | |||
2793 | def safe_execfile(self,fname,*where,**kw): |
|
2992 | def atexit_operations(self): | |
2794 | """A safe version of the builtin execfile(). |
|
2993 | """This will be executed at the time of exit. | |
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) |
|
|||
2809 |
|
||||
2810 | quiet : boolean (True) |
|
|||
2811 |
|
2994 | |||
2812 | exit_ignore : boolean (False) |
|
2995 | Saving of persistent data should be performed here. | |
2813 |
|
|
2996 | """ | |
|
2997 | self.savehist() | |||
2814 |
|
2998 | |||
2815 | def syspath_cleanup(): |
|
2999 | # Cleanup all tempfiles left around | |
2816 | """Internal cleanup routine for sys.path.""" |
|
3000 | for tfile in self.tempfiles: | |
2817 | if add_dname: |
|
|||
2818 |
|
|
3001 | try: | |
2819 | sys.path.remove(dname) |
|
3002 | os.unlink(tfile) | |
2820 |
|
|
3003 | except OSError: | |
2821 | # For some reason the user has already removed it, ignore. |
|
|||
2822 |
|
|
3004 | pass | |
2823 |
|
3005 | |||
2824 | fname = os.path.expanduser(fname) |
|
3006 | # Clear all user namespaces to release all references cleanly. | |
2825 |
|
3007 | self.reset() | ||
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 |
|
|||
2834 |
|
||||
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 |
|
|||
2842 |
|
3008 | |||
2843 | kw.setdefault('islog',0) |
|
3009 | # Run user hooks | |
2844 | kw.setdefault('quiet',1) |
|
3010 | self.hooks.shutdown_hook() | |
2845 | kw.setdefault('exit_ignore',0) |
|
|||
2846 |
|
3011 | |||
2847 | first = xfile.readline() |
|
3012 | def cleanup(self): | |
2848 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
3013 | self.restore_sys_module_state() | |
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 |
|
3014 | |||
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 |
|
3015 | |||
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