Show More
@@ -0,0 +1,77 b'' | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | # encoding: utf-8 | |||
|
3 | """ | |||
|
4 | Simple utility for splitting user input. | |||
|
5 | ||||
|
6 | Authors: | |||
|
7 | ||||
|
8 | * Brian Granger | |||
|
9 | * Fernando Perez | |||
|
10 | """ | |||
|
11 | ||||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | # Copyright (C) 2008-2009 The IPython Development Team | |||
|
14 | # | |||
|
15 | # Distributed under the terms of the BSD License. The full license is in | |||
|
16 | # the file COPYING, distributed as part of this software. | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | #----------------------------------------------------------------------------- | |||
|
20 | # Imports | |||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | ||||
|
23 | import re | |||
|
24 | ||||
|
25 | #----------------------------------------------------------------------------- | |||
|
26 | # Main function | |||
|
27 | #----------------------------------------------------------------------------- | |||
|
28 | ||||
|
29 | ||||
|
30 | # RegExp for splitting line contents into pre-char//first word-method//rest. | |||
|
31 | # For clarity, each group in on one line. | |||
|
32 | ||||
|
33 | # WARNING: update the regexp if the escapes in iplib are changed, as they | |||
|
34 | # are hardwired in. | |||
|
35 | ||||
|
36 | # Although it's not solely driven by the regex, note that: | |||
|
37 | # ,;/% only trigger if they are the first character on the line | |||
|
38 | # ! and !! trigger if they are first char(s) *or* follow an indent | |||
|
39 | # ? triggers as first or last char. | |||
|
40 | ||||
|
41 | # The three parts of the regex are: | |||
|
42 | # 1) pre: pre_char *or* initial whitespace | |||
|
43 | # 2) ifun: first word/method (mix of \w and '.') | |||
|
44 | # 3) the_rest: rest of line (separated from ifun by space if non-empty) | |||
|
45 | line_split = re.compile(r'^([,;/%?]|!!?|\s*)' | |||
|
46 | r'\s*([\w\.]+)' | |||
|
47 | r'(\s+.*$|$)') | |||
|
48 | ||||
|
49 | ||||
|
50 | def split_user_input(line, pattern=None): | |||
|
51 | """Split user input into pre-char/whitespace, function part and rest.""" | |||
|
52 | ||||
|
53 | if pattern is None: | |||
|
54 | pattern = line_split | |||
|
55 | match = pattern.match(line) | |||
|
56 | if not match: | |||
|
57 | #print "match failed for line '%s'" % line | |||
|
58 | try: | |||
|
59 | ifun, the_rest = line.split(None,1) | |||
|
60 | except ValueError: | |||
|
61 | #print "split failed for line '%s'" % line | |||
|
62 | ifun, the_rest = line,'' | |||
|
63 | pre = re.match('^(\s*)(.*)',line).groups()[0] | |||
|
64 | else: | |||
|
65 | pre,ifun,the_rest = match.groups() | |||
|
66 | ||||
|
67 | # ifun has to be a valid python identifier, so it better be only pure | |||
|
68 | # ascii, no unicode: | |||
|
69 | try: | |||
|
70 | ifun = ifun.encode('ascii') | |||
|
71 | except UnicodeEncodeError: | |||
|
72 | the_rest = ifun + u' ' + the_rest | |||
|
73 | ifun = u'' | |||
|
74 | ||||
|
75 | #print 'line:<%s>' % line # dbg | |||
|
76 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg | |||
|
77 | return pre, ifun.strip(), the_rest.lstrip() |
@@ -0,0 +1,162 b'' | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | # encoding: utf-8 | |||
|
3 | """Descriptor support for NIPY. | |||
|
4 | ||||
|
5 | Utilities to support special Python descriptors [1,2], in particular the use of | |||
|
6 | a useful pattern for properties we call 'one time properties'. These are | |||
|
7 | object attributes which are declared as properties, but become regular | |||
|
8 | attributes once they've been read the first time. They can thus be evaluated | |||
|
9 | later in the object's life cycle, but once evaluated they become normal, static | |||
|
10 | attributes with no function call overhead on access or any other constraints. | |||
|
11 | ||||
|
12 | A special ResetMixin class is provided to add a .reset() method to users who | |||
|
13 | may want to have their objects capable of resetting these computed properties | |||
|
14 | to their 'untriggered' state. | |||
|
15 | ||||
|
16 | References | |||
|
17 | ---------- | |||
|
18 | [1] How-To Guide for Descriptors, Raymond | |||
|
19 | Hettinger. http://users.rcn.com/python/download/Descriptor.htm | |||
|
20 | ||||
|
21 | [2] Python data model, http://docs.python.org/reference/datamodel.html | |||
|
22 | ||||
|
23 | Notes | |||
|
24 | ----- | |||
|
25 | This module is taken from the NiPy project | |||
|
26 | (http://neuroimaging.scipy.org/site/index.html), and is BSD licensed. | |||
|
27 | """ | |||
|
28 | ||||
|
29 | #----------------------------------------------------------------------------- | |||
|
30 | # Classes and Functions | |||
|
31 | #----------------------------------------------------------------------------- | |||
|
32 | ||||
|
33 | class ResetMixin(object): | |||
|
34 | """A Mixin class to add a .reset() method to users of OneTimeProperty. | |||
|
35 | ||||
|
36 | By default, auto attributes once computed, become static. If they happen to | |||
|
37 | depend on other parts of an object and those parts change, their values may | |||
|
38 | now be invalid. | |||
|
39 | ||||
|
40 | This class offers a .reset() method that users can call *explicitly* when | |||
|
41 | they know the state of their objects may have changed and they want to | |||
|
42 | ensure that *all* their special attributes should be invalidated. Once | |||
|
43 | reset() is called, all their auto attributes are reset to their | |||
|
44 | OneTimeProperty descriptors, and their accessor functions will be triggered | |||
|
45 | again. | |||
|
46 | ||||
|
47 | Example | |||
|
48 | ------- | |||
|
49 | ||||
|
50 | >>> class A(ResetMixin): | |||
|
51 | ... def __init__(self,x=1.0): | |||
|
52 | ... self.x = x | |||
|
53 | ... | |||
|
54 | ... @auto_attr | |||
|
55 | ... def y(self): | |||
|
56 | ... print '*** y computation executed ***' | |||
|
57 | ... return self.x / 2.0 | |||
|
58 | ... | |||
|
59 | ||||
|
60 | >>> a = A(10) | |||
|
61 | ||||
|
62 | About to access y twice, the second time no computation is done: | |||
|
63 | >>> a.y | |||
|
64 | *** y computation executed *** | |||
|
65 | 5.0 | |||
|
66 | >>> a.y | |||
|
67 | 5.0 | |||
|
68 | ||||
|
69 | Changing x | |||
|
70 | >>> a.x = 20 | |||
|
71 | ||||
|
72 | a.y doesn't change to 10, since it is a static attribute: | |||
|
73 | >>> a.y | |||
|
74 | 5.0 | |||
|
75 | ||||
|
76 | We now reset a, and this will then force all auto attributes to recompute | |||
|
77 | the next time we access them: | |||
|
78 | >>> a.reset() | |||
|
79 | ||||
|
80 | About to access y twice again after reset(): | |||
|
81 | >>> a.y | |||
|
82 | *** y computation executed *** | |||
|
83 | 10.0 | |||
|
84 | >>> a.y | |||
|
85 | 10.0 | |||
|
86 | """ | |||
|
87 | ||||
|
88 | def reset(self): | |||
|
89 | """Reset all OneTimeProperty attributes that may have fired already.""" | |||
|
90 | instdict = self.__dict__ | |||
|
91 | classdict = self.__class__.__dict__ | |||
|
92 | # To reset them, we simply remove them from the instance dict. At that | |||
|
93 | # point, it's as if they had never been computed. On the next access, | |||
|
94 | # the accessor function from the parent class will be called, simply | |||
|
95 | # because that's how the python descriptor protocol works. | |||
|
96 | for mname, mval in classdict.items(): | |||
|
97 | if mname in instdict and isinstance(mval, OneTimeProperty): | |||
|
98 | delattr(self, mname) | |||
|
99 | ||||
|
100 | ||||
|
101 | class OneTimeProperty(object): | |||
|
102 | """A descriptor to make special properties that become normal attributes. | |||
|
103 | ||||
|
104 | This is meant to be used mostly by the auto_attr decorator in this module. | |||
|
105 | """ | |||
|
106 | def __init__(self,func): | |||
|
107 | """Create a OneTimeProperty instance. | |||
|
108 | ||||
|
109 | Parameters | |||
|
110 | ---------- | |||
|
111 | func : method | |||
|
112 | ||||
|
113 | The method that will be called the first time to compute a value. | |||
|
114 | Afterwards, the method's name will be a standard attribute holding | |||
|
115 | the value of this computation. | |||
|
116 | """ | |||
|
117 | self.getter = func | |||
|
118 | self.name = func.func_name | |||
|
119 | ||||
|
120 | def __get__(self,obj,type=None): | |||
|
121 | """This will be called on attribute access on the class or instance. """ | |||
|
122 | ||||
|
123 | if obj is None: | |||
|
124 | # Being called on the class, return the original function. This way, | |||
|
125 | # introspection works on the class. | |||
|
126 | #return func | |||
|
127 | return self.getter | |||
|
128 | ||||
|
129 | val = self.getter(obj) | |||
|
130 | #print "** auto_attr - loading '%s'" % self.name # dbg | |||
|
131 | setattr(obj, self.name, val) | |||
|
132 | return val | |||
|
133 | ||||
|
134 | ||||
|
135 | def auto_attr(func): | |||
|
136 | """Decorator to create OneTimeProperty attributes. | |||
|
137 | ||||
|
138 | Parameters | |||
|
139 | ---------- | |||
|
140 | func : method | |||
|
141 | The method that will be called the first time to compute a value. | |||
|
142 | Afterwards, the method's name will be a standard attribute holding the | |||
|
143 | value of this computation. | |||
|
144 | ||||
|
145 | Examples | |||
|
146 | -------- | |||
|
147 | >>> class MagicProp(object): | |||
|
148 | ... @auto_attr | |||
|
149 | ... def a(self): | |||
|
150 | ... return 99 | |||
|
151 | ... | |||
|
152 | >>> x = MagicProp() | |||
|
153 | >>> 'a' in x.__dict__ | |||
|
154 | False | |||
|
155 | >>> x.a | |||
|
156 | 99 | |||
|
157 | >>> 'a' in x.__dict__ | |||
|
158 | True | |||
|
159 | """ | |||
|
160 | return OneTimeProperty(func) | |||
|
161 | ||||
|
162 |
@@ -1,191 +1,257 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | IPython's alias component |
|
4 | IPython's alias component | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Brian Granger |
|
8 | * Brian Granger | |
9 | """ |
|
9 | """ | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Copyright (C) 2008-2009 The IPython Development Team |
|
12 | # Copyright (C) 2008-2009 The IPython Development Team | |
13 | # |
|
13 | # | |
14 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | # Distributed under the terms of the BSD License. The full license is in | |
15 | # the file COPYING, distributed as part of this software. |
|
15 | # the file COPYING, distributed as part of this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | import keyword |
|
23 | import keyword | |
24 | import os |
|
24 | import os | |
|
25 | import re | |||
25 | import sys |
|
26 | import sys | |
26 |
|
27 | |||
27 | from IPython.core.component import Component |
|
28 | from IPython.core.component import Component | |
|
29 | from IPython.core.splitinput import split_user_input | |||
28 |
|
30 | |||
29 | from IPython.utils.traitlets import CBool, List, Instance |
|
31 | from IPython.utils.traitlets import CBool, List, Instance | |
30 | from IPython.utils.genutils import error |
|
32 | from IPython.utils.genutils import error | |
|
33 | from IPython.utils.autoattr import auto_attr | |||
31 |
|
34 | |||
32 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
33 | # Functions and classes |
|
36 | # Utilities | |
34 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
35 |
|
38 | |||
|
39 | # This is used as the pattern for calls to split_user_input. | |||
|
40 | shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)') | |||
|
41 | ||||
36 | def default_aliases(): |
|
42 | def default_aliases(): | |
37 | # Make some aliases automatically |
|
43 | # Make some aliases automatically | |
38 | # Prepare list of shell aliases to auto-define |
|
44 | # Prepare list of shell aliases to auto-define | |
39 | if os.name == 'posix': |
|
45 | if os.name == 'posix': | |
40 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
46 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', | |
41 | 'mv mv -i','rm rm -i','cp cp -i', |
|
47 | 'mv mv -i','rm rm -i','cp cp -i', | |
42 | 'cat cat','less less','clear clear', |
|
48 | 'cat cat','less less','clear clear', | |
43 | # a better ls |
|
49 | # a better ls | |
44 | 'ls ls -F', |
|
50 | 'ls ls -F', | |
45 | # long ls |
|
51 | # long ls | |
46 | 'll ls -lF') |
|
52 | 'll ls -lF') | |
47 | # Extra ls aliases with color, which need special treatment on BSD |
|
53 | # Extra ls aliases with color, which need special treatment on BSD | |
48 | # variants |
|
54 | # variants | |
49 | ls_extra = ( # color ls |
|
55 | ls_extra = ( # color ls | |
50 | 'lc ls -F -o --color', |
|
56 | 'lc ls -F -o --color', | |
51 | # ls normal files only |
|
57 | # ls normal files only | |
52 | 'lf ls -F -o --color %l | grep ^-', |
|
58 | 'lf ls -F -o --color %l | grep ^-', | |
53 | # ls symbolic links |
|
59 | # ls symbolic links | |
54 | 'lk ls -F -o --color %l | grep ^l', |
|
60 | 'lk ls -F -o --color %l | grep ^l', | |
55 | # directories or links to directories, |
|
61 | # directories or links to directories, | |
56 | 'ldir ls -F -o --color %l | grep /$', |
|
62 | 'ldir ls -F -o --color %l | grep /$', | |
57 | # things which are executable |
|
63 | # things which are executable | |
58 | 'lx ls -F -o --color %l | grep ^-..x', |
|
64 | 'lx ls -F -o --color %l | grep ^-..x', | |
59 | ) |
|
65 | ) | |
60 | # The BSDs don't ship GNU ls, so they don't understand the |
|
66 | # The BSDs don't ship GNU ls, so they don't understand the | |
61 | # --color switch out of the box |
|
67 | # --color switch out of the box | |
62 | if 'bsd' in sys.platform: |
|
68 | if 'bsd' in sys.platform: | |
63 | ls_extra = ( # ls normal files only |
|
69 | ls_extra = ( # ls normal files only | |
64 | 'lf ls -lF | grep ^-', |
|
70 | 'lf ls -lF | grep ^-', | |
65 | # ls symbolic links |
|
71 | # ls symbolic links | |
66 | 'lk ls -lF | grep ^l', |
|
72 | 'lk ls -lF | grep ^l', | |
67 | # directories or links to directories, |
|
73 | # directories or links to directories, | |
68 | 'ldir ls -lF | grep /$', |
|
74 | 'ldir ls -lF | grep /$', | |
69 | # things which are executable |
|
75 | # things which are executable | |
70 | 'lx ls -lF | grep ^-..x', |
|
76 | 'lx ls -lF | grep ^-..x', | |
71 | ) |
|
77 | ) | |
72 | auto_alias = auto_alias + ls_extra |
|
78 | auto_alias = auto_alias + ls_extra | |
73 | elif os.name in ['nt','dos']: |
|
79 | elif os.name in ['nt','dos']: | |
74 | auto_alias = ('ls dir /on', |
|
80 | auto_alias = ('ls dir /on', | |
75 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
81 | 'ddir dir /ad /on', 'ldir dir /ad /on', | |
76 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
82 | 'mkdir mkdir','rmdir rmdir','echo echo', | |
77 | 'ren ren','cls cls','copy copy') |
|
83 | 'ren ren','cls cls','copy copy') | |
78 | else: |
|
84 | else: | |
79 | auto_alias = () |
|
85 | auto_alias = () | |
80 | return [s.split(None,1) for s in auto_alias] |
|
86 | return [s.split(None,1) for s in auto_alias] | |
81 |
|
87 | |||
82 |
|
88 | |||
83 | class AliasError(Exception): |
|
89 | class AliasError(Exception): | |
84 | pass |
|
90 | pass | |
85 |
|
91 | |||
86 |
|
92 | |||
87 | class InvalidAliasError(AliasError): |
|
93 | class InvalidAliasError(AliasError): | |
88 | pass |
|
94 | pass | |
89 |
|
95 | |||
90 |
|
96 | |||
|
97 | #----------------------------------------------------------------------------- | |||
|
98 | # Main AliasManager class | |||
|
99 | #----------------------------------------------------------------------------- | |||
|
100 | ||||
|
101 | ||||
91 | class AliasManager(Component): |
|
102 | class AliasManager(Component): | |
92 |
|
103 | |||
93 | auto_alias = List(default_aliases()) |
|
104 | auto_alias = List(default_aliases()) | |
94 | user_alias = List(default_value=[], config_key='USER_ALIAS') |
|
105 | user_alias = List(default_value=[], config_key='USER_ALIAS') | |
95 |
|
106 | |||
96 | def __init__(self, parent, config=None): |
|
107 | def __init__(self, parent, config=None): | |
97 | super(AliasManager, self).__init__(parent, config=config) |
|
108 | super(AliasManager, self).__init__(parent, config=config) | |
98 | self.shell = Component.get_instances( |
|
|||
99 | root=self.root, |
|
|||
100 | klass='IPython.core.iplib.InteractiveShell' |
|
|||
101 | )[0] |
|
|||
102 | self.alias_table = {} |
|
109 | self.alias_table = {} | |
103 | self.exclude_aliases() |
|
110 | self.exclude_aliases() | |
104 | self.init_aliases() |
|
111 | self.init_aliases() | |
105 |
|
112 | |||
|
113 | @auto_attr | |||
|
114 | def shell(self): | |||
|
115 | shell = Component.get_instances( | |||
|
116 | root=self.root, | |||
|
117 | klass='IPython.core.iplib.InteractiveShell' | |||
|
118 | )[0] | |||
|
119 | return shell | |||
|
120 | ||||
106 | def __contains__(self, name): |
|
121 | def __contains__(self, name): | |
107 | if name in self.alias_table: |
|
122 | if name in self.alias_table: | |
108 | return True |
|
123 | return True | |
109 | else: |
|
124 | else: | |
110 | return False |
|
125 | return False | |
111 |
|
126 | |||
112 | @property |
|
127 | @property | |
113 | def aliases(self): |
|
128 | def aliases(self): | |
114 | return [(item[0], item[1][1]) for item in self.alias_table.iteritems()] |
|
129 | return [(item[0], item[1][1]) for item in self.alias_table.iteritems()] | |
115 |
|
130 | |||
116 | def exclude_aliases(self): |
|
131 | def exclude_aliases(self): | |
117 | # set of things NOT to alias (keywords, builtins and some magics) |
|
132 | # set of things NOT to alias (keywords, builtins and some magics) | |
118 | no_alias = set(['cd','popd','pushd','dhist','alias','unalias']) |
|
133 | no_alias = set(['cd','popd','pushd','dhist','alias','unalias']) | |
119 | no_alias.update(set(keyword.kwlist)) |
|
134 | no_alias.update(set(keyword.kwlist)) | |
120 | no_alias.update(set(__builtin__.__dict__.keys())) |
|
135 | no_alias.update(set(__builtin__.__dict__.keys())) | |
121 | self.no_alias = no_alias |
|
136 | self.no_alias = no_alias | |
122 |
|
137 | |||
123 | def init_aliases(self): |
|
138 | def init_aliases(self): | |
124 | # Load default aliases |
|
139 | # Load default aliases | |
125 | for name, cmd in self.auto_alias: |
|
140 | for name, cmd in self.auto_alias: | |
126 | self.soft_define_alias(name, cmd) |
|
141 | self.soft_define_alias(name, cmd) | |
127 |
|
142 | |||
128 | # Load user aliases |
|
143 | # Load user aliases | |
129 | for name, cmd in self.user_alias: |
|
144 | for name, cmd in self.user_alias: | |
130 | self.soft_define_alias(name, cmd) |
|
145 | self.soft_define_alias(name, cmd) | |
131 |
|
146 | |||
132 | def soft_define_alias(self, name, cmd): |
|
147 | def soft_define_alias(self, name, cmd): | |
133 | """Define an alias, but don't raise on an AliasError.""" |
|
148 | """Define an alias, but don't raise on an AliasError.""" | |
134 | try: |
|
149 | try: | |
135 | self.define_alias(name, cmd) |
|
150 | self.define_alias(name, cmd) | |
136 | except AliasError, e: |
|
151 | except AliasError, e: | |
137 | error("Invalid alias: %s" % e) |
|
152 | error("Invalid alias: %s" % e) | |
138 |
|
153 | |||
139 | def define_alias(self, name, cmd): |
|
154 | def define_alias(self, name, cmd): | |
140 | """Define a new alias after validating it. |
|
155 | """Define a new alias after validating it. | |
141 |
|
156 | |||
142 | This will raise an :exc:`AliasError` if there are validation |
|
157 | This will raise an :exc:`AliasError` if there are validation | |
143 | problems. |
|
158 | problems. | |
144 | """ |
|
159 | """ | |
145 | nargs = self.validate_alias(name, cmd) |
|
160 | nargs = self.validate_alias(name, cmd) | |
146 | self.alias_table[name] = (nargs, cmd) |
|
161 | self.alias_table[name] = (nargs, cmd) | |
147 |
|
162 | |||
148 | def validate_alias(self, name, cmd): |
|
163 | def validate_alias(self, name, cmd): | |
149 | """Validate an alias and return the its number of arguments.""" |
|
164 | """Validate an alias and return the its number of arguments.""" | |
150 | if name in self.no_alias: |
|
165 | if name in self.no_alias: | |
151 | raise InvalidAliasError("The name %s can't be aliased " |
|
166 | raise InvalidAliasError("The name %s can't be aliased " | |
152 | "because it is a keyword or builtin." % name) |
|
167 | "because it is a keyword or builtin." % name) | |
153 | if not (isinstance(cmd, basestring)): |
|
168 | if not (isinstance(cmd, basestring)): | |
154 | raise InvalidAliasError("An alias command must be a string, " |
|
169 | raise InvalidAliasError("An alias command must be a string, " | |
155 | "got: %r" % name) |
|
170 | "got: %r" % name) | |
156 | nargs = cmd.count('%s') |
|
171 | nargs = cmd.count('%s') | |
157 | if nargs>0 and cmd.find('%l')>=0: |
|
172 | if nargs>0 and cmd.find('%l')>=0: | |
158 | raise InvalidAliasError('The %s and %l specifiers are mutually ' |
|
173 | raise InvalidAliasError('The %s and %l specifiers are mutually ' | |
159 | 'exclusive in alias definitions.') |
|
174 | 'exclusive in alias definitions.') | |
160 | return nargs |
|
175 | return nargs | |
161 |
|
176 | |||
162 | def call_alias(self, alias, rest=''): |
|
177 | def call_alias(self, alias, rest=''): | |
163 | """Call an alias given its name and the rest of the line.""" |
|
178 | """Call an alias given its name and the rest of the line.""" | |
164 | cmd = self.transform_alias(alias, rest) |
|
179 | cmd = self.transform_alias(alias, rest) | |
165 | try: |
|
180 | try: | |
166 | self.shell.system(cmd) |
|
181 | self.shell.system(cmd) | |
167 | except: |
|
182 | except: | |
168 | self.shell.showtraceback() |
|
183 | self.shell.showtraceback() | |
169 |
|
184 | |||
170 | def transform_alias(self, alias,rest=''): |
|
185 | def transform_alias(self, alias,rest=''): | |
171 | """Transform alias to system command string.""" |
|
186 | """Transform alias to system command string.""" | |
172 | nargs, cmd = self.alias_table[alias] |
|
187 | nargs, cmd = self.alias_table[alias] | |
173 |
|
188 | |||
174 | if ' ' in cmd and os.path.isfile(cmd): |
|
189 | if ' ' in cmd and os.path.isfile(cmd): | |
175 | cmd = '"%s"' % cmd |
|
190 | cmd = '"%s"' % cmd | |
176 |
|
191 | |||
177 | # Expand the %l special to be the user's input line |
|
192 | # Expand the %l special to be the user's input line | |
178 | if cmd.find('%l') >= 0: |
|
193 | if cmd.find('%l') >= 0: | |
179 | cmd = cmd.replace('%l', rest) |
|
194 | cmd = cmd.replace('%l', rest) | |
180 | rest = '' |
|
195 | rest = '' | |
181 | if nargs==0: |
|
196 | if nargs==0: | |
182 | # Simple, argument-less aliases |
|
197 | # Simple, argument-less aliases | |
183 | cmd = '%s %s' % (cmd, rest) |
|
198 | cmd = '%s %s' % (cmd, rest) | |
184 | else: |
|
199 | else: | |
185 | # Handle aliases with positional arguments |
|
200 | # Handle aliases with positional arguments | |
186 | args = rest.split(None, nargs) |
|
201 | args = rest.split(None, nargs) | |
187 | if len(args) < nargs: |
|
202 | if len(args) < nargs: | |
188 | raise AliasError('Alias <%s> requires %s arguments, %s given.' % |
|
203 | raise AliasError('Alias <%s> requires %s arguments, %s given.' % | |
189 | (alias, nargs, len(args))) |
|
204 | (alias, nargs, len(args))) | |
190 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
205 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) | |
191 | return cmd |
|
206 | return cmd | |
|
207 | ||||
|
208 | def expand_alias(self, line): | |||
|
209 | """ Expand an alias in the command line | |||
|
210 | ||||
|
211 | Returns the provided command line, possibly with the first word | |||
|
212 | (command) translated according to alias expansion rules. | |||
|
213 | ||||
|
214 | [ipython]|16> _ip.expand_aliases("np myfile.txt") | |||
|
215 | <16> 'q:/opt/np/notepad++.exe myfile.txt' | |||
|
216 | """ | |||
|
217 | ||||
|
218 | pre,fn,rest = split_user_input(line) | |||
|
219 | res = pre + self.expand_aliases(fn, rest) | |||
|
220 | return res | |||
|
221 | ||||
|
222 | def expand_aliases(self, fn, rest): | |||
|
223 | """Expand multiple levels of aliases: | |||
|
224 | ||||
|
225 | if: | |||
|
226 | ||||
|
227 | alias foo bar /tmp | |||
|
228 | alias baz foo | |||
|
229 | ||||
|
230 | then: | |||
|
231 | ||||
|
232 | baz huhhahhei -> bar /tmp huhhahhei | |||
|
233 | ||||
|
234 | """ | |||
|
235 | line = fn + " " + rest | |||
|
236 | ||||
|
237 | done = set() | |||
|
238 | while 1: | |||
|
239 | pre,fn,rest = split_user_input(line, shell_line_split) | |||
|
240 | if fn in self.alias_table: | |||
|
241 | if fn in done: | |||
|
242 | warn("Cyclic alias definition, repeated '%s'" % fn) | |||
|
243 | return "" | |||
|
244 | done.add(fn) | |||
|
245 | ||||
|
246 | l2 = self.transform_alias(fn, rest) | |||
|
247 | if l2 == line: | |||
|
248 | break | |||
|
249 | # ls -> ls -F should not recurse forever | |||
|
250 | if l2.split(None,1)[0] == line.split(None,1)[0]: | |||
|
251 | line = l2 | |||
|
252 | break | |||
|
253 | line=l2 | |||
|
254 | else: | |||
|
255 | break | |||
|
256 | ||||
|
257 | return line |
@@ -1,108 +1,111 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | A context manager for managing things injected into :mod:`__builtin__`. |
|
4 | A context manager for managing things injected into :mod:`__builtin__`. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Brian Granger |
|
8 | * Brian Granger | |
9 | """ |
|
9 | """ | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Copyright (C) 2008-2009 The IPython Development Team |
|
12 | # Copyright (C) 2008-2009 The IPython Development Team | |
13 | # |
|
13 | # | |
14 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | # Distributed under the terms of the BSD License. The full license is in | |
15 | # the file COPYING, distributed as part of this software. |
|
15 | # the file COPYING, distributed as part of this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 |
|
23 | |||
24 | from IPython.core.component import Component |
|
24 | from IPython.core.component import Component | |
25 | from IPython.core.quitter import Quitter |
|
25 | from IPython.core.quitter import Quitter | |
26 |
|
26 | |||
27 |
from IPython.utils. |
|
27 | from IPython.utils.autoattr import auto_attr | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Classes and functions |
|
30 | # Classes and functions | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | class BuiltinUndefined(object): pass |
|
34 | class BuiltinUndefined(object): pass | |
35 | BuiltinUndefined = BuiltinUndefined() |
|
35 | BuiltinUndefined = BuiltinUndefined() | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class BuiltinTrap(Component): |
|
38 | class BuiltinTrap(Component): | |
39 |
|
39 | |||
40 | def __init__(self, parent): |
|
40 | def __init__(self, parent): | |
41 | super(BuiltinTrap, self).__init__(parent, None, None) |
|
41 | super(BuiltinTrap, self).__init__(parent, None, None) | |
42 | # Don't just grab parent!!! |
|
42 | self._orig_builtins = {} | |
43 | self.shell = Component.get_instances( |
|
43 | ||
|
44 | @auto_attr | |||
|
45 | def shell(self): | |||
|
46 | shell = Component.get_instances( | |||
44 | root=self.root, |
|
47 | root=self.root, | |
45 | klass='IPython.core.iplib.InteractiveShell' |
|
48 | klass='IPython.core.iplib.InteractiveShell' | |
46 | )[0] |
|
49 | )[0] | |
47 | self._orig_builtins = {} |
|
50 | return shell | |
48 |
|
51 | |||
49 | def __enter__(self): |
|
52 | def __enter__(self): | |
50 | self.set() |
|
53 | self.set() | |
51 | # I return self, so callers can use add_builtin in a with clause. |
|
54 | # I return self, so callers can use add_builtin in a with clause. | |
52 | return self |
|
55 | return self | |
53 |
|
56 | |||
54 | def __exit__(self, type, value, traceback): |
|
57 | def __exit__(self, type, value, traceback): | |
55 | self.unset() |
|
58 | self.unset() | |
56 | return True |
|
59 | return True | |
57 |
|
60 | |||
58 | def add_builtin(self, key, value): |
|
61 | def add_builtin(self, key, value): | |
59 | """Add a builtin and save the original.""" |
|
62 | """Add a builtin and save the original.""" | |
60 | orig = __builtin__.__dict__.get(key, BuiltinUndefined) |
|
63 | orig = __builtin__.__dict__.get(key, BuiltinUndefined) | |
61 | self._orig_builtins[key] = orig |
|
64 | self._orig_builtins[key] = orig | |
62 | __builtin__.__dict__[key] = value |
|
65 | __builtin__.__dict__[key] = value | |
63 |
|
66 | |||
64 | def remove_builtin(self, key): |
|
67 | def remove_builtin(self, key): | |
65 | """Remove an added builtin and re-set the original.""" |
|
68 | """Remove an added builtin and re-set the original.""" | |
66 | try: |
|
69 | try: | |
67 | orig = self._orig_builtins.pop(key) |
|
70 | orig = self._orig_builtins.pop(key) | |
68 | except KeyError: |
|
71 | except KeyError: | |
69 | pass |
|
72 | pass | |
70 | else: |
|
73 | else: | |
71 | if orig is BuiltinUndefined: |
|
74 | if orig is BuiltinUndefined: | |
72 | del __builtin__.__dict__[key] |
|
75 | del __builtin__.__dict__[key] | |
73 | else: |
|
76 | else: | |
74 | __builtin__.__dict__[key] = orig |
|
77 | __builtin__.__dict__[key] = orig | |
75 |
|
78 | |||
76 | def set(self): |
|
79 | def set(self): | |
77 | """Store ipython references in the __builtin__ namespace.""" |
|
80 | """Store ipython references in the __builtin__ namespace.""" | |
78 | self.add_builtin('exit', Quitter(self.shell, 'exit')) |
|
81 | self.add_builtin('exit', Quitter(self.shell, 'exit')) | |
79 | self.add_builtin('quit', Quitter(self.shell, 'quit')) |
|
82 | self.add_builtin('quit', Quitter(self.shell, 'quit')) | |
80 |
|
83 | |||
81 | # Recursive reload function |
|
84 | # Recursive reload function | |
82 | try: |
|
85 | try: | |
83 | from IPython.lib import deepreload |
|
86 | from IPython.lib import deepreload | |
84 | if self.shell.deep_reload: |
|
87 | if self.shell.deep_reload: | |
85 | self.add_builtin('reload', deepreload.reload) |
|
88 | self.add_builtin('reload', deepreload.reload) | |
86 | else: |
|
89 | else: | |
87 | self.add_builtin('dreload', deepreload.reload) |
|
90 | self.add_builtin('dreload', deepreload.reload) | |
88 | del deepreload |
|
91 | del deepreload | |
89 | except ImportError: |
|
92 | except ImportError: | |
90 | pass |
|
93 | pass | |
91 |
|
94 | |||
92 | # Keep in the builtins a flag for when IPython is active. We set it |
|
95 | # Keep in the builtins a flag for when IPython is active. We set it | |
93 | # with setdefault so that multiple nested IPythons don't clobber one |
|
96 | # with setdefault so that multiple nested IPythons don't clobber one | |
94 | # another. Each will increase its value by one upon being activated, |
|
97 | # another. Each will increase its value by one upon being activated, | |
95 | # which also gives us a way to determine the nesting level. |
|
98 | # which also gives us a way to determine the nesting level. | |
96 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
99 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) | |
97 |
|
100 | |||
98 | def unset(self): |
|
101 | def unset(self): | |
99 | """Remove any builtins which might have been added by add_builtins, or |
|
102 | """Remove any builtins which might have been added by add_builtins, or | |
100 | restore overwritten ones to their previous values.""" |
|
103 | restore overwritten ones to their previous values.""" | |
101 | for key in self._orig_builtins.keys(): |
|
104 | for key in self._orig_builtins.keys(): | |
102 | self.remove_builtin(key) |
|
105 | self.remove_builtin(key) | |
103 | self._orig_builtins.clear() |
|
106 | self._orig_builtins.clear() | |
104 | self._builtins_added = False |
|
107 | self._builtins_added = False | |
105 | try: |
|
108 | try: | |
106 | del __builtin__.__dict__['__IPYTHON__active'] |
|
109 | del __builtin__.__dict__['__IPYTHON__active'] | |
107 | except KeyError: |
|
110 | except KeyError: | |
108 | pass No newline at end of file |
|
111 | pass |
@@ -1,640 +1,642 b'' | |||||
1 | """Word completion for IPython. |
|
1 | """Word completion for IPython. | |
2 |
|
2 | |||
3 | This module is a fork of the rlcompleter module in the Python standard |
|
3 | This module is a fork of the rlcompleter module in the Python standard | |
4 | library. The original enhancements made to rlcompleter have been sent |
|
4 | library. The original enhancements made to rlcompleter have been sent | |
5 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
5 | upstream and were accepted as of Python 2.3, but we need a lot more | |
6 | functionality specific to IPython, so this module will continue to live as an |
|
6 | functionality specific to IPython, so this module will continue to live as an | |
7 | IPython-specific utility. |
|
7 | IPython-specific utility. | |
8 |
|
8 | |||
9 | Original rlcompleter documentation: |
|
9 | Original rlcompleter documentation: | |
10 |
|
10 | |||
11 | This requires the latest extension to the readline module (the |
|
11 | This requires the latest extension to the readline module (the | |
12 | completes keywords, built-ins and globals in __main__; when completing |
|
12 | completes keywords, built-ins and globals in __main__; when completing | |
13 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
13 | NAME.NAME..., it evaluates (!) the expression up to the last dot and | |
14 | completes its attributes. |
|
14 | completes its attributes. | |
15 |
|
15 | |||
16 | It's very cool to do "import string" type "string.", hit the |
|
16 | It's very cool to do "import string" type "string.", hit the | |
17 | completion key (twice), and see the list of names defined by the |
|
17 | completion key (twice), and see the list of names defined by the | |
18 | string module! |
|
18 | string module! | |
19 |
|
19 | |||
20 | Tip: to use the tab key as the completion key, call |
|
20 | Tip: to use the tab key as the completion key, call | |
21 |
|
21 | |||
22 | readline.parse_and_bind("tab: complete") |
|
22 | readline.parse_and_bind("tab: complete") | |
23 |
|
23 | |||
24 | Notes: |
|
24 | Notes: | |
25 |
|
25 | |||
26 | - Exceptions raised by the completer function are *ignored* (and |
|
26 | - Exceptions raised by the completer function are *ignored* (and | |
27 | generally cause the completion to fail). This is a feature -- since |
|
27 | generally cause the completion to fail). This is a feature -- since | |
28 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
28 | readline sets the tty device in raw (or cbreak) mode, printing a | |
29 | traceback wouldn't work well without some complicated hoopla to save, |
|
29 | traceback wouldn't work well without some complicated hoopla to save, | |
30 | reset and restore the tty state. |
|
30 | reset and restore the tty state. | |
31 |
|
31 | |||
32 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
32 | - The evaluation of the NAME.NAME... form may cause arbitrary | |
33 | application defined code to be executed if an object with a |
|
33 | application defined code to be executed if an object with a | |
34 | __getattr__ hook is found. Since it is the responsibility of the |
|
34 | __getattr__ hook is found. Since it is the responsibility of the | |
35 | application (or the user) to enable this feature, I consider this an |
|
35 | application (or the user) to enable this feature, I consider this an | |
36 | acceptable risk. More complicated expressions (e.g. function calls or |
|
36 | acceptable risk. More complicated expressions (e.g. function calls or | |
37 | indexing operations) are *not* evaluated. |
|
37 | indexing operations) are *not* evaluated. | |
38 |
|
38 | |||
39 | - GNU readline is also used by the built-in functions input() and |
|
39 | - GNU readline is also used by the built-in functions input() and | |
40 | raw_input(), and thus these also benefit/suffer from the completer |
|
40 | raw_input(), and thus these also benefit/suffer from the completer | |
41 | features. Clearly an interactive application can benefit by |
|
41 | features. Clearly an interactive application can benefit by | |
42 | specifying its own completer function and using raw_input() for all |
|
42 | specifying its own completer function and using raw_input() for all | |
43 | its input. |
|
43 | its input. | |
44 |
|
44 | |||
45 | - When the original stdin is not a tty device, GNU readline is never |
|
45 | - When the original stdin is not a tty device, GNU readline is never | |
46 | used, and this module (and the readline module) are silently inactive. |
|
46 | used, and this module (and the readline module) are silently inactive. | |
47 |
|
47 | |||
48 | """ |
|
48 | """ | |
49 |
|
49 | |||
50 | #***************************************************************************** |
|
50 | #***************************************************************************** | |
51 | # |
|
51 | # | |
52 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
52 | # Since this file is essentially a minimally modified copy of the rlcompleter | |
53 | # module which is part of the standard Python distribution, I assume that the |
|
53 | # module which is part of the standard Python distribution, I assume that the | |
54 | # proper procedure is to maintain its copyright as belonging to the Python |
|
54 | # proper procedure is to maintain its copyright as belonging to the Python | |
55 | # Software Foundation (in addition to my own, for all new code). |
|
55 | # Software Foundation (in addition to my own, for all new code). | |
56 | # |
|
56 | # | |
57 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
57 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
58 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
58 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
59 | # |
|
59 | # | |
60 | # Distributed under the terms of the BSD License. The full license is in |
|
60 | # Distributed under the terms of the BSD License. The full license is in | |
61 | # the file COPYING, distributed as part of this software. |
|
61 | # the file COPYING, distributed as part of this software. | |
62 | # |
|
62 | # | |
63 | #***************************************************************************** |
|
63 | #***************************************************************************** | |
64 |
|
64 | |||
65 | import __builtin__ |
|
65 | import __builtin__ | |
66 | import __main__ |
|
66 | import __main__ | |
67 | import glob |
|
67 | import glob | |
68 | import keyword |
|
68 | import keyword | |
69 | import os |
|
69 | import os | |
70 | import re |
|
70 | import re | |
71 | import shlex |
|
71 | import shlex | |
72 | import sys |
|
72 | import sys | |
73 | import itertools |
|
73 | import itertools | |
74 | import types |
|
74 | import types | |
75 |
|
75 | |||
76 | from IPython.core.error import TryNext |
|
76 | from IPython.core.error import TryNext | |
77 | import IPython.utils.rlineimpl as readline |
|
77 | from IPython.core.prefilter import ESC_MAGIC | |
|
78 | ||||
|
79 | import IPython.utils.rlineimpl as readline | |||
78 | from IPython.utils.ipstruct import Struct |
|
80 | from IPython.utils.ipstruct import Struct | |
79 | from IPython.utils import generics |
|
81 | from IPython.utils import generics | |
80 |
|
82 | |||
81 | # Python 2.4 offers sets as a builtin |
|
83 | # Python 2.4 offers sets as a builtin | |
82 | try: |
|
84 | try: | |
83 | set() |
|
85 | set() | |
84 | except NameError: |
|
86 | except NameError: | |
85 | from sets import Set as set |
|
87 | from sets import Set as set | |
86 |
|
88 | |||
87 | from IPython.utils.genutils import debugx, dir2 |
|
89 | from IPython.utils.genutils import debugx, dir2 | |
88 |
|
90 | |||
89 | __all__ = ['Completer','IPCompleter'] |
|
91 | __all__ = ['Completer','IPCompleter'] | |
90 |
|
92 | |||
91 | class Completer: |
|
93 | class Completer: | |
92 | def __init__(self,namespace=None,global_namespace=None): |
|
94 | def __init__(self,namespace=None,global_namespace=None): | |
93 | """Create a new completer for the command line. |
|
95 | """Create a new completer for the command line. | |
94 |
|
96 | |||
95 | Completer([namespace,global_namespace]) -> completer instance. |
|
97 | Completer([namespace,global_namespace]) -> completer instance. | |
96 |
|
98 | |||
97 | If unspecified, the default namespace where completions are performed |
|
99 | If unspecified, the default namespace where completions are performed | |
98 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
100 | is __main__ (technically, __main__.__dict__). Namespaces should be | |
99 | given as dictionaries. |
|
101 | given as dictionaries. | |
100 |
|
102 | |||
101 | An optional second namespace can be given. This allows the completer |
|
103 | An optional second namespace can be given. This allows the completer | |
102 | to handle cases where both the local and global scopes need to be |
|
104 | to handle cases where both the local and global scopes need to be | |
103 | distinguished. |
|
105 | distinguished. | |
104 |
|
106 | |||
105 | Completer instances should be used as the completion mechanism of |
|
107 | Completer instances should be used as the completion mechanism of | |
106 | readline via the set_completer() call: |
|
108 | readline via the set_completer() call: | |
107 |
|
109 | |||
108 | readline.set_completer(Completer(my_namespace).complete) |
|
110 | readline.set_completer(Completer(my_namespace).complete) | |
109 | """ |
|
111 | """ | |
110 |
|
112 | |||
111 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
113 | # Don't bind to namespace quite yet, but flag whether the user wants a | |
112 | # specific namespace or to use __main__.__dict__. This will allow us |
|
114 | # specific namespace or to use __main__.__dict__. This will allow us | |
113 | # to bind to __main__.__dict__ at completion time, not now. |
|
115 | # to bind to __main__.__dict__ at completion time, not now. | |
114 | if namespace is None: |
|
116 | if namespace is None: | |
115 | self.use_main_ns = 1 |
|
117 | self.use_main_ns = 1 | |
116 | else: |
|
118 | else: | |
117 | self.use_main_ns = 0 |
|
119 | self.use_main_ns = 0 | |
118 | self.namespace = namespace |
|
120 | self.namespace = namespace | |
119 |
|
121 | |||
120 | # The global namespace, if given, can be bound directly |
|
122 | # The global namespace, if given, can be bound directly | |
121 | if global_namespace is None: |
|
123 | if global_namespace is None: | |
122 | self.global_namespace = {} |
|
124 | self.global_namespace = {} | |
123 | else: |
|
125 | else: | |
124 | self.global_namespace = global_namespace |
|
126 | self.global_namespace = global_namespace | |
125 |
|
127 | |||
126 | def complete(self, text, state): |
|
128 | def complete(self, text, state): | |
127 | """Return the next possible completion for 'text'. |
|
129 | """Return the next possible completion for 'text'. | |
128 |
|
130 | |||
129 | This is called successively with state == 0, 1, 2, ... until it |
|
131 | This is called successively with state == 0, 1, 2, ... until it | |
130 | returns None. The completion should begin with 'text'. |
|
132 | returns None. The completion should begin with 'text'. | |
131 |
|
133 | |||
132 | """ |
|
134 | """ | |
133 | if self.use_main_ns: |
|
135 | if self.use_main_ns: | |
134 | self.namespace = __main__.__dict__ |
|
136 | self.namespace = __main__.__dict__ | |
135 |
|
137 | |||
136 | if state == 0: |
|
138 | if state == 0: | |
137 | if "." in text: |
|
139 | if "." in text: | |
138 | self.matches = self.attr_matches(text) |
|
140 | self.matches = self.attr_matches(text) | |
139 | else: |
|
141 | else: | |
140 | self.matches = self.global_matches(text) |
|
142 | self.matches = self.global_matches(text) | |
141 | try: |
|
143 | try: | |
142 | return self.matches[state] |
|
144 | return self.matches[state] | |
143 | except IndexError: |
|
145 | except IndexError: | |
144 | return None |
|
146 | return None | |
145 |
|
147 | |||
146 | def global_matches(self, text): |
|
148 | def global_matches(self, text): | |
147 | """Compute matches when text is a simple name. |
|
149 | """Compute matches when text is a simple name. | |
148 |
|
150 | |||
149 | Return a list of all keywords, built-in functions and names currently |
|
151 | Return a list of all keywords, built-in functions and names currently | |
150 | defined in self.namespace or self.global_namespace that match. |
|
152 | defined in self.namespace or self.global_namespace that match. | |
151 |
|
153 | |||
152 | """ |
|
154 | """ | |
153 | matches = [] |
|
155 | matches = [] | |
154 | match_append = matches.append |
|
156 | match_append = matches.append | |
155 | n = len(text) |
|
157 | n = len(text) | |
156 | for lst in [keyword.kwlist, |
|
158 | for lst in [keyword.kwlist, | |
157 | __builtin__.__dict__.keys(), |
|
159 | __builtin__.__dict__.keys(), | |
158 | self.namespace.keys(), |
|
160 | self.namespace.keys(), | |
159 | self.global_namespace.keys()]: |
|
161 | self.global_namespace.keys()]: | |
160 | for word in lst: |
|
162 | for word in lst: | |
161 | if word[:n] == text and word != "__builtins__": |
|
163 | if word[:n] == text and word != "__builtins__": | |
162 | match_append(word) |
|
164 | match_append(word) | |
163 | return matches |
|
165 | return matches | |
164 |
|
166 | |||
165 | def attr_matches(self, text): |
|
167 | def attr_matches(self, text): | |
166 | """Compute matches when text contains a dot. |
|
168 | """Compute matches when text contains a dot. | |
167 |
|
169 | |||
168 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
170 | Assuming the text is of the form NAME.NAME....[NAME], and is | |
169 | evaluatable in self.namespace or self.global_namespace, it will be |
|
171 | evaluatable in self.namespace or self.global_namespace, it will be | |
170 | evaluated and its attributes (as revealed by dir()) are used as |
|
172 | evaluated and its attributes (as revealed by dir()) are used as | |
171 | possible completions. (For class instances, class members are are |
|
173 | possible completions. (For class instances, class members are are | |
172 | also considered.) |
|
174 | also considered.) | |
173 |
|
175 | |||
174 | WARNING: this can still invoke arbitrary C code, if an object |
|
176 | WARNING: this can still invoke arbitrary C code, if an object | |
175 | with a __getattr__ hook is evaluated. |
|
177 | with a __getattr__ hook is evaluated. | |
176 |
|
178 | |||
177 | """ |
|
179 | """ | |
178 | import re |
|
180 | import re | |
179 |
|
181 | |||
180 | # Another option, seems to work great. Catches things like ''.<tab> |
|
182 | # Another option, seems to work great. Catches things like ''.<tab> | |
181 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
183 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |
182 |
|
184 | |||
183 | if not m: |
|
185 | if not m: | |
184 | return [] |
|
186 | return [] | |
185 |
|
187 | |||
186 | expr, attr = m.group(1, 3) |
|
188 | expr, attr = m.group(1, 3) | |
187 | try: |
|
189 | try: | |
188 | obj = eval(expr, self.namespace) |
|
190 | obj = eval(expr, self.namespace) | |
189 | except: |
|
191 | except: | |
190 | try: |
|
192 | try: | |
191 | obj = eval(expr, self.global_namespace) |
|
193 | obj = eval(expr, self.global_namespace) | |
192 | except: |
|
194 | except: | |
193 | return [] |
|
195 | return [] | |
194 |
|
196 | |||
195 | words = dir2(obj) |
|
197 | words = dir2(obj) | |
196 |
|
198 | |||
197 | try: |
|
199 | try: | |
198 | words = generics.complete_object(obj, words) |
|
200 | words = generics.complete_object(obj, words) | |
199 | except TryNext: |
|
201 | except TryNext: | |
200 | pass |
|
202 | pass | |
201 | # Build match list to return |
|
203 | # Build match list to return | |
202 | n = len(attr) |
|
204 | n = len(attr) | |
203 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
205 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] | |
204 | return res |
|
206 | return res | |
205 |
|
207 | |||
206 | class IPCompleter(Completer): |
|
208 | class IPCompleter(Completer): | |
207 | """Extension of the completer class with IPython-specific features""" |
|
209 | """Extension of the completer class with IPython-specific features""" | |
208 |
|
210 | |||
209 | def __init__(self,shell,namespace=None,global_namespace=None, |
|
211 | def __init__(self,shell,namespace=None,global_namespace=None, | |
210 | omit__names=0,alias_table=None): |
|
212 | omit__names=0,alias_table=None): | |
211 | """IPCompleter() -> completer |
|
213 | """IPCompleter() -> completer | |
212 |
|
214 | |||
213 | Return a completer object suitable for use by the readline library |
|
215 | Return a completer object suitable for use by the readline library | |
214 | via readline.set_completer(). |
|
216 | via readline.set_completer(). | |
215 |
|
217 | |||
216 | Inputs: |
|
218 | Inputs: | |
217 |
|
219 | |||
218 | - shell: a pointer to the ipython shell itself. This is needed |
|
220 | - shell: a pointer to the ipython shell itself. This is needed | |
219 | because this completer knows about magic functions, and those can |
|
221 | because this completer knows about magic functions, and those can | |
220 | only be accessed via the ipython instance. |
|
222 | only be accessed via the ipython instance. | |
221 |
|
223 | |||
222 | - namespace: an optional dict where completions are performed. |
|
224 | - namespace: an optional dict where completions are performed. | |
223 |
|
225 | |||
224 | - global_namespace: secondary optional dict for completions, to |
|
226 | - global_namespace: secondary optional dict for completions, to | |
225 | handle cases (such as IPython embedded inside functions) where |
|
227 | handle cases (such as IPython embedded inside functions) where | |
226 | both Python scopes are visible. |
|
228 | both Python scopes are visible. | |
227 |
|
229 | |||
228 | - The optional omit__names parameter sets the completer to omit the |
|
230 | - The optional omit__names parameter sets the completer to omit the | |
229 | 'magic' names (__magicname__) for python objects unless the text |
|
231 | 'magic' names (__magicname__) for python objects unless the text | |
230 | to be completed explicitly starts with one or more underscores. |
|
232 | to be completed explicitly starts with one or more underscores. | |
231 |
|
233 | |||
232 | - If alias_table is supplied, it should be a dictionary of aliases |
|
234 | - If alias_table is supplied, it should be a dictionary of aliases | |
233 | to complete. """ |
|
235 | to complete. """ | |
234 |
|
236 | |||
235 | Completer.__init__(self,namespace,global_namespace) |
|
237 | Completer.__init__(self,namespace,global_namespace) | |
236 | self.magic_prefix = shell.name+'.magic_' |
|
238 | self.magic_prefix = shell.name+'.magic_' | |
237 |
self.magic_escape = |
|
239 | self.magic_escape = ESC_MAGIC | |
238 | self.readline = readline |
|
240 | self.readline = readline | |
239 | delims = self.readline.get_completer_delims() |
|
241 | delims = self.readline.get_completer_delims() | |
240 | delims = delims.replace(self.magic_escape,'') |
|
242 | delims = delims.replace(self.magic_escape,'') | |
241 | self.readline.set_completer_delims(delims) |
|
243 | self.readline.set_completer_delims(delims) | |
242 | self.get_line_buffer = self.readline.get_line_buffer |
|
244 | self.get_line_buffer = self.readline.get_line_buffer | |
243 | self.get_endidx = self.readline.get_endidx |
|
245 | self.get_endidx = self.readline.get_endidx | |
244 | self.omit__names = omit__names |
|
246 | self.omit__names = omit__names | |
245 | self.merge_completions = shell.readline_merge_completions |
|
247 | self.merge_completions = shell.readline_merge_completions | |
246 | if alias_table is None: |
|
248 | if alias_table is None: | |
247 | alias_table = {} |
|
249 | alias_table = {} | |
248 | self.alias_table = alias_table |
|
250 | self.alias_table = alias_table | |
249 | # Regexp to split filenames with spaces in them |
|
251 | # Regexp to split filenames with spaces in them | |
250 | self.space_name_re = re.compile(r'([^\\] )') |
|
252 | self.space_name_re = re.compile(r'([^\\] )') | |
251 | # Hold a local ref. to glob.glob for speed |
|
253 | # Hold a local ref. to glob.glob for speed | |
252 | self.glob = glob.glob |
|
254 | self.glob = glob.glob | |
253 |
|
255 | |||
254 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
256 | # Determine if we are running on 'dumb' terminals, like (X)Emacs | |
255 | # buffers, to avoid completion problems. |
|
257 | # buffers, to avoid completion problems. | |
256 | term = os.environ.get('TERM','xterm') |
|
258 | term = os.environ.get('TERM','xterm') | |
257 | self.dumb_terminal = term in ['dumb','emacs'] |
|
259 | self.dumb_terminal = term in ['dumb','emacs'] | |
258 |
|
260 | |||
259 | # Special handling of backslashes needed in win32 platforms |
|
261 | # Special handling of backslashes needed in win32 platforms | |
260 | if sys.platform == "win32": |
|
262 | if sys.platform == "win32": | |
261 | self.clean_glob = self._clean_glob_win32 |
|
263 | self.clean_glob = self._clean_glob_win32 | |
262 | else: |
|
264 | else: | |
263 | self.clean_glob = self._clean_glob |
|
265 | self.clean_glob = self._clean_glob | |
264 | self.matchers = [self.python_matches, |
|
266 | self.matchers = [self.python_matches, | |
265 | self.file_matches, |
|
267 | self.file_matches, | |
266 | self.alias_matches, |
|
268 | self.alias_matches, | |
267 | self.python_func_kw_matches] |
|
269 | self.python_func_kw_matches] | |
268 |
|
270 | |||
269 |
|
271 | |||
270 | # Code contributed by Alex Schmolck, for ipython/emacs integration |
|
272 | # Code contributed by Alex Schmolck, for ipython/emacs integration | |
271 | def all_completions(self, text): |
|
273 | def all_completions(self, text): | |
272 | """Return all possible completions for the benefit of emacs.""" |
|
274 | """Return all possible completions for the benefit of emacs.""" | |
273 |
|
275 | |||
274 | completions = [] |
|
276 | completions = [] | |
275 | comp_append = completions.append |
|
277 | comp_append = completions.append | |
276 | try: |
|
278 | try: | |
277 | for i in xrange(sys.maxint): |
|
279 | for i in xrange(sys.maxint): | |
278 | res = self.complete(text, i) |
|
280 | res = self.complete(text, i) | |
279 |
|
281 | |||
280 | if not res: break |
|
282 | if not res: break | |
281 |
|
283 | |||
282 | comp_append(res) |
|
284 | comp_append(res) | |
283 | #XXX workaround for ``notDefined.<tab>`` |
|
285 | #XXX workaround for ``notDefined.<tab>`` | |
284 | except NameError: |
|
286 | except NameError: | |
285 | pass |
|
287 | pass | |
286 | return completions |
|
288 | return completions | |
287 | # /end Alex Schmolck code. |
|
289 | # /end Alex Schmolck code. | |
288 |
|
290 | |||
289 | def _clean_glob(self,text): |
|
291 | def _clean_glob(self,text): | |
290 | return self.glob("%s*" % text) |
|
292 | return self.glob("%s*" % text) | |
291 |
|
293 | |||
292 | def _clean_glob_win32(self,text): |
|
294 | def _clean_glob_win32(self,text): | |
293 | return [f.replace("\\","/") |
|
295 | return [f.replace("\\","/") | |
294 | for f in self.glob("%s*" % text)] |
|
296 | for f in self.glob("%s*" % text)] | |
295 |
|
297 | |||
296 | def file_matches(self, text): |
|
298 | def file_matches(self, text): | |
297 | """Match filenames, expanding ~USER type strings. |
|
299 | """Match filenames, expanding ~USER type strings. | |
298 |
|
300 | |||
299 | Most of the seemingly convoluted logic in this completer is an |
|
301 | Most of the seemingly convoluted logic in this completer is an | |
300 | attempt to handle filenames with spaces in them. And yet it's not |
|
302 | attempt to handle filenames with spaces in them. And yet it's not | |
301 | quite perfect, because Python's readline doesn't expose all of the |
|
303 | quite perfect, because Python's readline doesn't expose all of the | |
302 | GNU readline details needed for this to be done correctly. |
|
304 | GNU readline details needed for this to be done correctly. | |
303 |
|
305 | |||
304 | For a filename with a space in it, the printed completions will be |
|
306 | For a filename with a space in it, the printed completions will be | |
305 | only the parts after what's already been typed (instead of the |
|
307 | only the parts after what's already been typed (instead of the | |
306 | full completions, as is normally done). I don't think with the |
|
308 | full completions, as is normally done). I don't think with the | |
307 | current (as of Python 2.3) Python readline it's possible to do |
|
309 | current (as of Python 2.3) Python readline it's possible to do | |
308 | better.""" |
|
310 | better.""" | |
309 |
|
311 | |||
310 | #print 'Completer->file_matches: <%s>' % text # dbg |
|
312 | #print 'Completer->file_matches: <%s>' % text # dbg | |
311 |
|
313 | |||
312 | # chars that require escaping with backslash - i.e. chars |
|
314 | # chars that require escaping with backslash - i.e. chars | |
313 | # that readline treats incorrectly as delimiters, but we |
|
315 | # that readline treats incorrectly as delimiters, but we | |
314 | # don't want to treat as delimiters in filename matching |
|
316 | # don't want to treat as delimiters in filename matching | |
315 | # when escaped with backslash |
|
317 | # when escaped with backslash | |
316 |
|
318 | |||
317 | if sys.platform == 'win32': |
|
319 | if sys.platform == 'win32': | |
318 | protectables = ' ' |
|
320 | protectables = ' ' | |
319 | else: |
|
321 | else: | |
320 | protectables = ' ()' |
|
322 | protectables = ' ()' | |
321 |
|
323 | |||
322 | if text.startswith('!'): |
|
324 | if text.startswith('!'): | |
323 | text = text[1:] |
|
325 | text = text[1:] | |
324 | text_prefix = '!' |
|
326 | text_prefix = '!' | |
325 | else: |
|
327 | else: | |
326 | text_prefix = '' |
|
328 | text_prefix = '' | |
327 |
|
329 | |||
328 | def protect_filename(s): |
|
330 | def protect_filename(s): | |
329 | return "".join([(ch in protectables and '\\' + ch or ch) |
|
331 | return "".join([(ch in protectables and '\\' + ch or ch) | |
330 | for ch in s]) |
|
332 | for ch in s]) | |
331 |
|
333 | |||
332 | def single_dir_expand(matches): |
|
334 | def single_dir_expand(matches): | |
333 | "Recursively expand match lists containing a single dir." |
|
335 | "Recursively expand match lists containing a single dir." | |
334 |
|
336 | |||
335 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
337 | if len(matches) == 1 and os.path.isdir(matches[0]): | |
336 | # Takes care of links to directories also. Use '/' |
|
338 | # Takes care of links to directories also. Use '/' | |
337 | # explicitly, even under Windows, so that name completions |
|
339 | # explicitly, even under Windows, so that name completions | |
338 | # don't end up escaped. |
|
340 | # don't end up escaped. | |
339 | d = matches[0] |
|
341 | d = matches[0] | |
340 | if d[-1] in ['/','\\']: |
|
342 | if d[-1] in ['/','\\']: | |
341 | d = d[:-1] |
|
343 | d = d[:-1] | |
342 |
|
344 | |||
343 | subdirs = os.listdir(d) |
|
345 | subdirs = os.listdir(d) | |
344 | if subdirs: |
|
346 | if subdirs: | |
345 | matches = [ (d + '/' + p) for p in subdirs] |
|
347 | matches = [ (d + '/' + p) for p in subdirs] | |
346 | return single_dir_expand(matches) |
|
348 | return single_dir_expand(matches) | |
347 | else: |
|
349 | else: | |
348 | return matches |
|
350 | return matches | |
349 | else: |
|
351 | else: | |
350 | return matches |
|
352 | return matches | |
351 |
|
353 | |||
352 | lbuf = self.lbuf |
|
354 | lbuf = self.lbuf | |
353 | open_quotes = 0 # track strings with open quotes |
|
355 | open_quotes = 0 # track strings with open quotes | |
354 | try: |
|
356 | try: | |
355 | lsplit = shlex.split(lbuf)[-1] |
|
357 | lsplit = shlex.split(lbuf)[-1] | |
356 | except ValueError: |
|
358 | except ValueError: | |
357 | # typically an unmatched ", or backslash without escaped char. |
|
359 | # typically an unmatched ", or backslash without escaped char. | |
358 | if lbuf.count('"')==1: |
|
360 | if lbuf.count('"')==1: | |
359 | open_quotes = 1 |
|
361 | open_quotes = 1 | |
360 | lsplit = lbuf.split('"')[-1] |
|
362 | lsplit = lbuf.split('"')[-1] | |
361 | elif lbuf.count("'")==1: |
|
363 | elif lbuf.count("'")==1: | |
362 | open_quotes = 1 |
|
364 | open_quotes = 1 | |
363 | lsplit = lbuf.split("'")[-1] |
|
365 | lsplit = lbuf.split("'")[-1] | |
364 | else: |
|
366 | else: | |
365 | return [] |
|
367 | return [] | |
366 | except IndexError: |
|
368 | except IndexError: | |
367 | # tab pressed on empty line |
|
369 | # tab pressed on empty line | |
368 | lsplit = "" |
|
370 | lsplit = "" | |
369 |
|
371 | |||
370 | if lsplit != protect_filename(lsplit): |
|
372 | if lsplit != protect_filename(lsplit): | |
371 | # if protectables are found, do matching on the whole escaped |
|
373 | # if protectables are found, do matching on the whole escaped | |
372 | # name |
|
374 | # name | |
373 | has_protectables = 1 |
|
375 | has_protectables = 1 | |
374 | text0,text = text,lsplit |
|
376 | text0,text = text,lsplit | |
375 | else: |
|
377 | else: | |
376 | has_protectables = 0 |
|
378 | has_protectables = 0 | |
377 | text = os.path.expanduser(text) |
|
379 | text = os.path.expanduser(text) | |
378 |
|
380 | |||
379 | if text == "": |
|
381 | if text == "": | |
380 | return [text_prefix + protect_filename(f) for f in self.glob("*")] |
|
382 | return [text_prefix + protect_filename(f) for f in self.glob("*")] | |
381 |
|
383 | |||
382 | m0 = self.clean_glob(text.replace('\\','')) |
|
384 | m0 = self.clean_glob(text.replace('\\','')) | |
383 | if has_protectables: |
|
385 | if has_protectables: | |
384 | # If we had protectables, we need to revert our changes to the |
|
386 | # If we had protectables, we need to revert our changes to the | |
385 | # beginning of filename so that we don't double-write the part |
|
387 | # beginning of filename so that we don't double-write the part | |
386 | # of the filename we have so far |
|
388 | # of the filename we have so far | |
387 | len_lsplit = len(lsplit) |
|
389 | len_lsplit = len(lsplit) | |
388 | matches = [text_prefix + text0 + |
|
390 | matches = [text_prefix + text0 + | |
389 | protect_filename(f[len_lsplit:]) for f in m0] |
|
391 | protect_filename(f[len_lsplit:]) for f in m0] | |
390 | else: |
|
392 | else: | |
391 | if open_quotes: |
|
393 | if open_quotes: | |
392 | # if we have a string with an open quote, we don't need to |
|
394 | # if we have a string with an open quote, we don't need to | |
393 | # protect the names at all (and we _shouldn't_, as it |
|
395 | # protect the names at all (and we _shouldn't_, as it | |
394 | # would cause bugs when the filesystem call is made). |
|
396 | # would cause bugs when the filesystem call is made). | |
395 | matches = m0 |
|
397 | matches = m0 | |
396 | else: |
|
398 | else: | |
397 | matches = [text_prefix + |
|
399 | matches = [text_prefix + | |
398 | protect_filename(f) for f in m0] |
|
400 | protect_filename(f) for f in m0] | |
399 |
|
401 | |||
400 | #print 'mm',matches # dbg |
|
402 | #print 'mm',matches # dbg | |
401 | return single_dir_expand(matches) |
|
403 | return single_dir_expand(matches) | |
402 |
|
404 | |||
403 | def alias_matches(self, text): |
|
405 | def alias_matches(self, text): | |
404 | """Match internal system aliases""" |
|
406 | """Match internal system aliases""" | |
405 | #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg |
|
407 | #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg | |
406 |
|
408 | |||
407 | # if we are not in the first 'item', alias matching |
|
409 | # if we are not in the first 'item', alias matching | |
408 | # doesn't make sense - unless we are starting with 'sudo' command. |
|
410 | # doesn't make sense - unless we are starting with 'sudo' command. | |
409 | if ' ' in self.lbuf.lstrip() and not self.lbuf.lstrip().startswith('sudo'): |
|
411 | if ' ' in self.lbuf.lstrip() and not self.lbuf.lstrip().startswith('sudo'): | |
410 | return [] |
|
412 | return [] | |
411 | text = os.path.expanduser(text) |
|
413 | text = os.path.expanduser(text) | |
412 | aliases = self.alias_table.keys() |
|
414 | aliases = self.alias_table.keys() | |
413 | if text == "": |
|
415 | if text == "": | |
414 | return aliases |
|
416 | return aliases | |
415 | else: |
|
417 | else: | |
416 | return [alias for alias in aliases if alias.startswith(text)] |
|
418 | return [alias for alias in aliases if alias.startswith(text)] | |
417 |
|
419 | |||
418 | def python_matches(self,text): |
|
420 | def python_matches(self,text): | |
419 | """Match attributes or global python names""" |
|
421 | """Match attributes or global python names""" | |
420 |
|
422 | |||
421 | #print 'Completer->python_matches, txt=<%s>' % text # dbg |
|
423 | #print 'Completer->python_matches, txt=<%s>' % text # dbg | |
422 | if "." in text: |
|
424 | if "." in text: | |
423 | try: |
|
425 | try: | |
424 | matches = self.attr_matches(text) |
|
426 | matches = self.attr_matches(text) | |
425 | if text.endswith('.') and self.omit__names: |
|
427 | if text.endswith('.') and self.omit__names: | |
426 | if self.omit__names == 1: |
|
428 | if self.omit__names == 1: | |
427 | # true if txt is _not_ a __ name, false otherwise: |
|
429 | # true if txt is _not_ a __ name, false otherwise: | |
428 | no__name = (lambda txt: |
|
430 | no__name = (lambda txt: | |
429 | re.match(r'.*\.__.*?__',txt) is None) |
|
431 | re.match(r'.*\.__.*?__',txt) is None) | |
430 | else: |
|
432 | else: | |
431 | # true if txt is _not_ a _ name, false otherwise: |
|
433 | # true if txt is _not_ a _ name, false otherwise: | |
432 | no__name = (lambda txt: |
|
434 | no__name = (lambda txt: | |
433 | re.match(r'.*\._.*?',txt) is None) |
|
435 | re.match(r'.*\._.*?',txt) is None) | |
434 | matches = filter(no__name, matches) |
|
436 | matches = filter(no__name, matches) | |
435 | except NameError: |
|
437 | except NameError: | |
436 | # catches <undefined attributes>.<tab> |
|
438 | # catches <undefined attributes>.<tab> | |
437 | matches = [] |
|
439 | matches = [] | |
438 | else: |
|
440 | else: | |
439 | matches = self.global_matches(text) |
|
441 | matches = self.global_matches(text) | |
440 | # this is so completion finds magics when automagic is on: |
|
442 | # this is so completion finds magics when automagic is on: | |
441 | if (matches == [] and |
|
443 | if (matches == [] and | |
442 | not text.startswith(os.sep) and |
|
444 | not text.startswith(os.sep) and | |
443 | not ' ' in self.lbuf): |
|
445 | not ' ' in self.lbuf): | |
444 | matches = self.attr_matches(self.magic_prefix+text) |
|
446 | matches = self.attr_matches(self.magic_prefix+text) | |
445 | return matches |
|
447 | return matches | |
446 |
|
448 | |||
447 | def _default_arguments(self, obj): |
|
449 | def _default_arguments(self, obj): | |
448 | """Return the list of default arguments of obj if it is callable, |
|
450 | """Return the list of default arguments of obj if it is callable, | |
449 | or empty list otherwise.""" |
|
451 | or empty list otherwise.""" | |
450 |
|
452 | |||
451 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
453 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): | |
452 | # for classes, check for __init__,__new__ |
|
454 | # for classes, check for __init__,__new__ | |
453 | if inspect.isclass(obj): |
|
455 | if inspect.isclass(obj): | |
454 | obj = (getattr(obj,'__init__',None) or |
|
456 | obj = (getattr(obj,'__init__',None) or | |
455 | getattr(obj,'__new__',None)) |
|
457 | getattr(obj,'__new__',None)) | |
456 | # for all others, check if they are __call__able |
|
458 | # for all others, check if they are __call__able | |
457 | elif hasattr(obj, '__call__'): |
|
459 | elif hasattr(obj, '__call__'): | |
458 | obj = obj.__call__ |
|
460 | obj = obj.__call__ | |
459 | # XXX: is there a way to handle the builtins ? |
|
461 | # XXX: is there a way to handle the builtins ? | |
460 | try: |
|
462 | try: | |
461 | args,_,_1,defaults = inspect.getargspec(obj) |
|
463 | args,_,_1,defaults = inspect.getargspec(obj) | |
462 | if defaults: |
|
464 | if defaults: | |
463 | return args[-len(defaults):] |
|
465 | return args[-len(defaults):] | |
464 | except TypeError: pass |
|
466 | except TypeError: pass | |
465 | return [] |
|
467 | return [] | |
466 |
|
468 | |||
467 | def python_func_kw_matches(self,text): |
|
469 | def python_func_kw_matches(self,text): | |
468 | """Match named parameters (kwargs) of the last open function""" |
|
470 | """Match named parameters (kwargs) of the last open function""" | |
469 |
|
471 | |||
470 | if "." in text: # a parameter cannot be dotted |
|
472 | if "." in text: # a parameter cannot be dotted | |
471 | return [] |
|
473 | return [] | |
472 | try: regexp = self.__funcParamsRegex |
|
474 | try: regexp = self.__funcParamsRegex | |
473 | except AttributeError: |
|
475 | except AttributeError: | |
474 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
476 | regexp = self.__funcParamsRegex = re.compile(r''' | |
475 | '.*?' | # single quoted strings or |
|
477 | '.*?' | # single quoted strings or | |
476 | ".*?" | # double quoted strings or |
|
478 | ".*?" | # double quoted strings or | |
477 | \w+ | # identifier |
|
479 | \w+ | # identifier | |
478 | \S # other characters |
|
480 | \S # other characters | |
479 | ''', re.VERBOSE | re.DOTALL) |
|
481 | ''', re.VERBOSE | re.DOTALL) | |
480 | # 1. find the nearest identifier that comes before an unclosed |
|
482 | # 1. find the nearest identifier that comes before an unclosed | |
481 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
483 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" | |
482 | tokens = regexp.findall(self.get_line_buffer()) |
|
484 | tokens = regexp.findall(self.get_line_buffer()) | |
483 | tokens.reverse() |
|
485 | tokens.reverse() | |
484 | iterTokens = iter(tokens); openPar = 0 |
|
486 | iterTokens = iter(tokens); openPar = 0 | |
485 | for token in iterTokens: |
|
487 | for token in iterTokens: | |
486 | if token == ')': |
|
488 | if token == ')': | |
487 | openPar -= 1 |
|
489 | openPar -= 1 | |
488 | elif token == '(': |
|
490 | elif token == '(': | |
489 | openPar += 1 |
|
491 | openPar += 1 | |
490 | if openPar > 0: |
|
492 | if openPar > 0: | |
491 | # found the last unclosed parenthesis |
|
493 | # found the last unclosed parenthesis | |
492 | break |
|
494 | break | |
493 | else: |
|
495 | else: | |
494 | return [] |
|
496 | return [] | |
495 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
497 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) | |
496 | ids = [] |
|
498 | ids = [] | |
497 | isId = re.compile(r'\w+$').match |
|
499 | isId = re.compile(r'\w+$').match | |
498 | while True: |
|
500 | while True: | |
499 | try: |
|
501 | try: | |
500 | ids.append(iterTokens.next()) |
|
502 | ids.append(iterTokens.next()) | |
501 | if not isId(ids[-1]): |
|
503 | if not isId(ids[-1]): | |
502 | ids.pop(); break |
|
504 | ids.pop(); break | |
503 | if not iterTokens.next() == '.': |
|
505 | if not iterTokens.next() == '.': | |
504 | break |
|
506 | break | |
505 | except StopIteration: |
|
507 | except StopIteration: | |
506 | break |
|
508 | break | |
507 | # lookup the candidate callable matches either using global_matches |
|
509 | # lookup the candidate callable matches either using global_matches | |
508 | # or attr_matches for dotted names |
|
510 | # or attr_matches for dotted names | |
509 | if len(ids) == 1: |
|
511 | if len(ids) == 1: | |
510 | callableMatches = self.global_matches(ids[0]) |
|
512 | callableMatches = self.global_matches(ids[0]) | |
511 | else: |
|
513 | else: | |
512 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
514 | callableMatches = self.attr_matches('.'.join(ids[::-1])) | |
513 | argMatches = [] |
|
515 | argMatches = [] | |
514 | for callableMatch in callableMatches: |
|
516 | for callableMatch in callableMatches: | |
515 | try: namedArgs = self._default_arguments(eval(callableMatch, |
|
517 | try: namedArgs = self._default_arguments(eval(callableMatch, | |
516 | self.namespace)) |
|
518 | self.namespace)) | |
517 | except: continue |
|
519 | except: continue | |
518 | for namedArg in namedArgs: |
|
520 | for namedArg in namedArgs: | |
519 | if namedArg.startswith(text): |
|
521 | if namedArg.startswith(text): | |
520 | argMatches.append("%s=" %namedArg) |
|
522 | argMatches.append("%s=" %namedArg) | |
521 | return argMatches |
|
523 | return argMatches | |
522 |
|
524 | |||
523 | def dispatch_custom_completer(self,text): |
|
525 | def dispatch_custom_completer(self,text): | |
524 | #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg |
|
526 | #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg | |
525 | line = self.full_lbuf |
|
527 | line = self.full_lbuf | |
526 | if not line.strip(): |
|
528 | if not line.strip(): | |
527 | return None |
|
529 | return None | |
528 |
|
530 | |||
529 | event = Struct() |
|
531 | event = Struct() | |
530 | event.line = line |
|
532 | event.line = line | |
531 | event.symbol = text |
|
533 | event.symbol = text | |
532 | cmd = line.split(None,1)[0] |
|
534 | cmd = line.split(None,1)[0] | |
533 | event.command = cmd |
|
535 | event.command = cmd | |
534 | #print "\ncustom:{%s]\n" % event # dbg |
|
536 | #print "\ncustom:{%s]\n" % event # dbg | |
535 |
|
537 | |||
536 | # for foo etc, try also to find completer for %foo |
|
538 | # for foo etc, try also to find completer for %foo | |
537 | if not cmd.startswith(self.magic_escape): |
|
539 | if not cmd.startswith(self.magic_escape): | |
538 | try_magic = self.custom_completers.s_matches( |
|
540 | try_magic = self.custom_completers.s_matches( | |
539 | self.magic_escape + cmd) |
|
541 | self.magic_escape + cmd) | |
540 | else: |
|
542 | else: | |
541 | try_magic = [] |
|
543 | try_magic = [] | |
542 |
|
544 | |||
543 |
|
545 | |||
544 | for c in itertools.chain( |
|
546 | for c in itertools.chain( | |
545 | self.custom_completers.s_matches(cmd), |
|
547 | self.custom_completers.s_matches(cmd), | |
546 | try_magic, |
|
548 | try_magic, | |
547 | self.custom_completers.flat_matches(self.lbuf)): |
|
549 | self.custom_completers.flat_matches(self.lbuf)): | |
548 | #print "try",c # dbg |
|
550 | #print "try",c # dbg | |
549 | try: |
|
551 | try: | |
550 | res = c(event) |
|
552 | res = c(event) | |
551 | # first, try case sensitive match |
|
553 | # first, try case sensitive match | |
552 | withcase = [r for r in res if r.startswith(text)] |
|
554 | withcase = [r for r in res if r.startswith(text)] | |
553 | if withcase: |
|
555 | if withcase: | |
554 | return withcase |
|
556 | return withcase | |
555 | # if none, then case insensitive ones are ok too |
|
557 | # if none, then case insensitive ones are ok too | |
556 | return [r for r in res if r.lower().startswith(text.lower())] |
|
558 | return [r for r in res if r.lower().startswith(text.lower())] | |
557 | except TryNext: |
|
559 | except TryNext: | |
558 | pass |
|
560 | pass | |
559 |
|
561 | |||
560 | return None |
|
562 | return None | |
561 |
|
563 | |||
562 | def complete(self, text, state,line_buffer=None): |
|
564 | def complete(self, text, state,line_buffer=None): | |
563 | """Return the next possible completion for 'text'. |
|
565 | """Return the next possible completion for 'text'. | |
564 |
|
566 | |||
565 | This is called successively with state == 0, 1, 2, ... until it |
|
567 | This is called successively with state == 0, 1, 2, ... until it | |
566 | returns None. The completion should begin with 'text'. |
|
568 | returns None. The completion should begin with 'text'. | |
567 |
|
569 | |||
568 | :Keywords: |
|
570 | :Keywords: | |
569 | - line_buffer: string |
|
571 | - line_buffer: string | |
570 | If not given, the completer attempts to obtain the current line buffer |
|
572 | If not given, the completer attempts to obtain the current line buffer | |
571 | via readline. This keyword allows clients which are requesting for |
|
573 | via readline. This keyword allows clients which are requesting for | |
572 | text completions in non-readline contexts to inform the completer of |
|
574 | text completions in non-readline contexts to inform the completer of | |
573 | the entire text. |
|
575 | the entire text. | |
574 | """ |
|
576 | """ | |
575 |
|
577 | |||
576 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg |
|
578 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg | |
577 |
|
579 | |||
578 | # if there is only a tab on a line with only whitespace, instead |
|
580 | # if there is only a tab on a line with only whitespace, instead | |
579 | # of the mostly useless 'do you want to see all million |
|
581 | # of the mostly useless 'do you want to see all million | |
580 | # completions' message, just do the right thing and give the user |
|
582 | # completions' message, just do the right thing and give the user | |
581 | # his tab! Incidentally, this enables pasting of tabbed text from |
|
583 | # his tab! Incidentally, this enables pasting of tabbed text from | |
582 | # an editor (as long as autoindent is off). |
|
584 | # an editor (as long as autoindent is off). | |
583 |
|
585 | |||
584 | # It should be noted that at least pyreadline still shows |
|
586 | # It should be noted that at least pyreadline still shows | |
585 | # file completions - is there a way around it? |
|
587 | # file completions - is there a way around it? | |
586 |
|
588 | |||
587 | # don't apply this on 'dumb' terminals, such as emacs buffers, so we |
|
589 | # don't apply this on 'dumb' terminals, such as emacs buffers, so we | |
588 | # don't interfere with their own tab-completion mechanism. |
|
590 | # don't interfere with their own tab-completion mechanism. | |
589 | if line_buffer is None: |
|
591 | if line_buffer is None: | |
590 | self.full_lbuf = self.get_line_buffer() |
|
592 | self.full_lbuf = self.get_line_buffer() | |
591 | else: |
|
593 | else: | |
592 | self.full_lbuf = line_buffer |
|
594 | self.full_lbuf = line_buffer | |
593 |
|
595 | |||
594 | if not (self.dumb_terminal or self.full_lbuf.strip()): |
|
596 | if not (self.dumb_terminal or self.full_lbuf.strip()): | |
595 | self.readline.insert_text('\t') |
|
597 | self.readline.insert_text('\t') | |
596 | return None |
|
598 | return None | |
597 |
|
599 | |||
598 | magic_escape = self.magic_escape |
|
600 | magic_escape = self.magic_escape | |
599 | magic_prefix = self.magic_prefix |
|
601 | magic_prefix = self.magic_prefix | |
600 |
|
602 | |||
601 | self.lbuf = self.full_lbuf[:self.get_endidx()] |
|
603 | self.lbuf = self.full_lbuf[:self.get_endidx()] | |
602 |
|
604 | |||
603 | try: |
|
605 | try: | |
604 | if text.startswith(magic_escape): |
|
606 | if text.startswith(magic_escape): | |
605 | text = text.replace(magic_escape,magic_prefix) |
|
607 | text = text.replace(magic_escape,magic_prefix) | |
606 | elif text.startswith('~'): |
|
608 | elif text.startswith('~'): | |
607 | text = os.path.expanduser(text) |
|
609 | text = os.path.expanduser(text) | |
608 | if state == 0: |
|
610 | if state == 0: | |
609 | custom_res = self.dispatch_custom_completer(text) |
|
611 | custom_res = self.dispatch_custom_completer(text) | |
610 | if custom_res is not None: |
|
612 | if custom_res is not None: | |
611 | # did custom completers produce something? |
|
613 | # did custom completers produce something? | |
612 | self.matches = custom_res |
|
614 | self.matches = custom_res | |
613 | else: |
|
615 | else: | |
614 | # Extend the list of completions with the results of each |
|
616 | # Extend the list of completions with the results of each | |
615 | # matcher, so we return results to the user from all |
|
617 | # matcher, so we return results to the user from all | |
616 | # namespaces. |
|
618 | # namespaces. | |
617 | if self.merge_completions: |
|
619 | if self.merge_completions: | |
618 | self.matches = [] |
|
620 | self.matches = [] | |
619 | for matcher in self.matchers: |
|
621 | for matcher in self.matchers: | |
620 | self.matches.extend(matcher(text)) |
|
622 | self.matches.extend(matcher(text)) | |
621 | else: |
|
623 | else: | |
622 | for matcher in self.matchers: |
|
624 | for matcher in self.matchers: | |
623 | self.matches = matcher(text) |
|
625 | self.matches = matcher(text) | |
624 | if self.matches: |
|
626 | if self.matches: | |
625 | break |
|
627 | break | |
626 | def uniq(alist): |
|
628 | def uniq(alist): | |
627 | set = {} |
|
629 | set = {} | |
628 | return [set.setdefault(e,e) for e in alist if e not in set] |
|
630 | return [set.setdefault(e,e) for e in alist if e not in set] | |
629 | self.matches = uniq(self.matches) |
|
631 | self.matches = uniq(self.matches) | |
630 | try: |
|
632 | try: | |
631 | ret = self.matches[state].replace(magic_prefix,magic_escape) |
|
633 | ret = self.matches[state].replace(magic_prefix,magic_escape) | |
632 | return ret |
|
634 | return ret | |
633 | except IndexError: |
|
635 | except IndexError: | |
634 | return None |
|
636 | return None | |
635 | except: |
|
637 | except: | |
636 | #from IPython.core.ultratb import AutoFormattedTB; # dbg |
|
638 | #from IPython.core.ultratb import AutoFormattedTB; # dbg | |
637 | #tb=AutoFormattedTB('Verbose');tb() #dbg |
|
639 | #tb=AutoFormattedTB('Verbose');tb() #dbg | |
638 |
|
640 | |||
639 | # If completion fails, don't annoy the user. |
|
641 | # If completion fails, don't annoy the user. | |
640 | return None |
|
642 | return None |
@@ -1,305 +1,304 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | A lightweight component system for IPython. |
|
4 | A lightweight component system for IPython. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Brian Granger |
|
8 | * Brian Granger | |
9 | * Fernando Perez |
|
9 | * Fernando Perez | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Copyright (C) 2008-2009 The IPython Development Team |
|
13 | # Copyright (C) 2008-2009 The IPython Development Team | |
14 | # |
|
14 | # | |
15 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | # Distributed under the terms of the BSD License. The full license is in | |
16 | # the file COPYING, distributed as part of this software. |
|
16 | # the file COPYING, distributed as part of this software. | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Imports |
|
20 | # Imports | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | from copy import deepcopy |
|
23 | from copy import deepcopy | |
24 | import datetime |
|
24 | import datetime | |
25 | from weakref import WeakValueDictionary |
|
25 | from weakref import WeakValueDictionary | |
26 |
|
26 | |||
27 | from IPython.utils.importstring import import_item |
|
27 | from IPython.utils.importstring import import_item | |
28 | from IPython.utils.ipstruct import Struct |
|
28 | from IPython.utils.ipstruct import Struct | |
29 | from IPython.utils.traitlets import ( |
|
29 | from IPython.utils.traitlets import ( | |
30 | HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This |
|
30 | HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This | |
31 | ) |
|
31 | ) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | #----------------------------------------------------------------------------- |
|
34 | #----------------------------------------------------------------------------- | |
35 | # Helper classes for Components |
|
35 | # Helper classes for Components | |
36 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class ComponentError(Exception): |
|
39 | class ComponentError(Exception): | |
40 | pass |
|
40 | pass | |
41 |
|
41 | |||
42 | class MetaComponentTracker(type): |
|
42 | class MetaComponentTracker(type): | |
43 | """A metaclass that tracks instances of Components and its subclasses.""" |
|
43 | """A metaclass that tracks instances of Components and its subclasses.""" | |
44 |
|
44 | |||
45 | def __init__(cls, name, bases, d): |
|
45 | def __init__(cls, name, bases, d): | |
46 | super(MetaComponentTracker, cls).__init__(name, bases, d) |
|
46 | super(MetaComponentTracker, cls).__init__(name, bases, d) | |
47 | cls.__instance_refs = WeakValueDictionary() |
|
47 | cls.__instance_refs = WeakValueDictionary() | |
48 | cls.__numcreated = 0 |
|
48 | cls.__numcreated = 0 | |
49 |
|
49 | |||
50 | def __call__(cls, *args, **kw): |
|
50 | def __call__(cls, *args, **kw): | |
51 | """Called when a class is called (instantiated)!!! |
|
51 | """Called when a class is called (instantiated)!!! | |
52 |
|
52 | |||
53 | When a Component or subclass is instantiated, this is called and |
|
53 | When a Component or subclass is instantiated, this is called and | |
54 | the instance is saved in a WeakValueDictionary for tracking. |
|
54 | the instance is saved in a WeakValueDictionary for tracking. | |
55 | """ |
|
55 | """ | |
56 | instance = cls.__new__(cls, *args, **kw) |
|
56 | instance = cls.__new__(cls, *args, **kw) | |
57 |
|
57 | |||
58 | # Register the instance before __init__ is called so get_instances |
|
58 | # Register the instance before __init__ is called so get_instances | |
59 | # works inside __init__ methods! |
|
59 | # works inside __init__ methods! | |
60 | indices = cls.register_instance(instance) |
|
60 | indices = cls.register_instance(instance) | |
61 |
|
61 | |||
62 | # This is in a try/except because of the __init__ method fails, the |
|
62 | # This is in a try/except because of the __init__ method fails, the | |
63 | # instance is discarded and shouldn't be tracked. |
|
63 | # instance is discarded and shouldn't be tracked. | |
64 | try: |
|
64 | try: | |
65 | if isinstance(instance, cls): |
|
65 | if isinstance(instance, cls): | |
66 | cls.__init__(instance, *args, **kw) |
|
66 | cls.__init__(instance, *args, **kw) | |
67 | except: |
|
67 | except: | |
68 | # Unregister the instance because __init__ failed! |
|
68 | # Unregister the instance because __init__ failed! | |
69 | cls.unregister_instances(indices) |
|
69 | cls.unregister_instances(indices) | |
70 | raise |
|
70 | raise | |
71 | else: |
|
71 | else: | |
72 | return instance |
|
72 | return instance | |
73 |
|
73 | |||
74 | def register_instance(cls, instance): |
|
74 | def register_instance(cls, instance): | |
75 | """Register instance with cls and its subclasses.""" |
|
75 | """Register instance with cls and its subclasses.""" | |
76 | # indices is a list of the keys used to register the instance |
|
76 | # indices is a list of the keys used to register the instance | |
77 | # with. This list is needed if the instance needs to be unregistered. |
|
77 | # with. This list is needed if the instance needs to be unregistered. | |
78 | indices = [] |
|
78 | indices = [] | |
79 | for c in cls.__mro__: |
|
79 | for c in cls.__mro__: | |
80 | if issubclass(cls, c) and issubclass(c, Component): |
|
80 | if issubclass(cls, c) and issubclass(c, Component): | |
81 | c.__numcreated += 1 |
|
81 | c.__numcreated += 1 | |
82 | indices.append(c.__numcreated) |
|
82 | indices.append(c.__numcreated) | |
83 | c.__instance_refs[c.__numcreated] = instance |
|
83 | c.__instance_refs[c.__numcreated] = instance | |
84 | else: |
|
84 | else: | |
85 | break |
|
85 | break | |
86 | return indices |
|
86 | return indices | |
87 |
|
87 | |||
88 | def unregister_instances(cls, indices): |
|
88 | def unregister_instances(cls, indices): | |
89 | """Unregister instance with cls and its subclasses.""" |
|
89 | """Unregister instance with cls and its subclasses.""" | |
90 | for c, index in zip(cls.__mro__, indices): |
|
90 | for c, index in zip(cls.__mro__, indices): | |
91 | try: |
|
91 | try: | |
92 | del c.__instance_refs[index] |
|
92 | del c.__instance_refs[index] | |
93 | except KeyError: |
|
93 | except KeyError: | |
94 | pass |
|
94 | pass | |
95 |
|
95 | |||
96 | def clear_instances(cls): |
|
96 | def clear_instances(cls): | |
97 | """Clear all instances tracked by cls.""" |
|
97 | """Clear all instances tracked by cls.""" | |
98 | cls.__instance_refs.clear() |
|
98 | cls.__instance_refs.clear() | |
99 | cls.__numcreated = 0 |
|
99 | cls.__numcreated = 0 | |
100 |
|
100 | |||
101 | def get_instances(cls, name=None, root=None, klass=None): |
|
101 | def get_instances(cls, name=None, root=None, klass=None): | |
102 | """Get all instances of cls and its subclasses. |
|
102 | """Get all instances of cls and its subclasses. | |
103 |
|
103 | |||
104 | Parameters |
|
104 | Parameters | |
105 | ---------- |
|
105 | ---------- | |
106 | name : str |
|
106 | name : str | |
107 | Limit to components with this name. |
|
107 | Limit to components with this name. | |
108 | root : Component or subclass |
|
108 | root : Component or subclass | |
109 | Limit to components having this root. |
|
109 | Limit to components having this root. | |
110 | klass : class or str |
|
110 | klass : class or str | |
111 | Limits to instances of the class or its subclasses. If a str |
|
111 | Limits to instances of the class or its subclasses. If a str | |
112 | is given ut must be in the form 'foo.bar.MyClass'. The str |
|
112 | is given ut must be in the form 'foo.bar.MyClass'. The str | |
113 | form of this argument is useful for forward declarations. |
|
113 | form of this argument is useful for forward declarations. | |
114 | """ |
|
114 | """ | |
115 | if klass is not None: |
|
115 | if klass is not None: | |
116 | if isinstance(klass, basestring): |
|
116 | if isinstance(klass, basestring): | |
117 | klass = import_item(klass) |
|
117 | klass = import_item(klass) | |
118 | # Limit search to instances of klass for performance |
|
118 | # Limit search to instances of klass for performance | |
119 | if issubclass(klass, Component): |
|
119 | if issubclass(klass, Component): | |
120 | return klass.get_instances(name=name, root=root) |
|
120 | return klass.get_instances(name=name, root=root) | |
121 | instances = cls.__instance_refs.values() |
|
121 | instances = cls.__instance_refs.values() | |
122 | if name is not None: |
|
122 | if name is not None: | |
123 | instances = [i for i in instances if i.name == name] |
|
123 | instances = [i for i in instances if i.name == name] | |
124 | if klass is not None: |
|
124 | if klass is not None: | |
125 | instances = [i for i in instances if isinstance(i, klass)] |
|
125 | instances = [i for i in instances if isinstance(i, klass)] | |
126 | if root is not None: |
|
126 | if root is not None: | |
127 | instances = [i for i in instances if i.root == root] |
|
127 | instances = [i for i in instances if i.root == root] | |
128 | return instances |
|
128 | return instances | |
129 |
|
129 | |||
130 | def get_instances_by_condition(cls, call, name=None, root=None, |
|
130 | def get_instances_by_condition(cls, call, name=None, root=None, | |
131 | klass=None): |
|
131 | klass=None): | |
132 | """Get all instances of cls, i such that call(i)==True. |
|
132 | """Get all instances of cls, i such that call(i)==True. | |
133 |
|
133 | |||
134 | This also takes the ``name`` and ``root`` and ``classname`` |
|
134 | This also takes the ``name`` and ``root`` and ``classname`` | |
135 | arguments of :meth:`get_instance` |
|
135 | arguments of :meth:`get_instance` | |
136 | """ |
|
136 | """ | |
137 | return [i for i in cls.get_instances(name, root, klass) if call(i)] |
|
137 | return [i for i in cls.get_instances(name, root, klass) if call(i)] | |
138 |
|
138 | |||
139 |
|
139 | |||
140 | def masquerade_as(instance, cls): |
|
140 | def masquerade_as(instance, cls): | |
141 | """Let instance masquerade as an instance of cls. |
|
141 | """Let instance masquerade as an instance of cls. | |
142 |
|
142 | |||
143 | Sometimes, such as in testing code, it is useful to let a class |
|
143 | Sometimes, such as in testing code, it is useful to let a class | |
144 | masquerade as another. Python, being duck typed, allows this by |
|
144 | masquerade as another. Python, being duck typed, allows this by | |
145 | default. But, instances of components are tracked by their class type. |
|
145 | default. But, instances of components are tracked by their class type. | |
146 |
|
146 | |||
147 | After calling this, cls.get_instances() will return ``instance``. This |
|
147 | After calling this, cls.get_instances() will return ``instance``. This | |
148 | does not, however, cause isinstance(instance, cls) to return ``True``. |
|
148 | does not, however, cause isinstance(instance, cls) to return ``True``. | |
149 |
|
149 | |||
150 | Parameters |
|
150 | Parameters | |
151 | ---------- |
|
151 | ---------- | |
152 | instance : an instance of a Component or Component subclass |
|
152 | instance : an instance of a Component or Component subclass | |
153 | The instance that will pretend to be a cls. |
|
153 | The instance that will pretend to be a cls. | |
154 | cls : subclass of Component |
|
154 | cls : subclass of Component | |
155 | The Component subclass that instance will pretend to be. |
|
155 | The Component subclass that instance will pretend to be. | |
156 | """ |
|
156 | """ | |
157 | cls.register_instance(instance) |
|
157 | cls.register_instance(instance) | |
158 |
|
158 | |||
159 |
|
159 | |||
160 | class ComponentNameGenerator(object): |
|
160 | class ComponentNameGenerator(object): | |
161 | """A Singleton to generate unique component names.""" |
|
161 | """A Singleton to generate unique component names.""" | |
162 |
|
162 | |||
163 | def __init__(self, prefix): |
|
163 | def __init__(self, prefix): | |
164 | self.prefix = prefix |
|
164 | self.prefix = prefix | |
165 | self.i = 0 |
|
165 | self.i = 0 | |
166 |
|
166 | |||
167 | def __call__(self): |
|
167 | def __call__(self): | |
168 | count = self.i |
|
168 | count = self.i | |
169 | self.i += 1 |
|
169 | self.i += 1 | |
170 | return "%s%s" % (self.prefix, count) |
|
170 | return "%s%s" % (self.prefix, count) | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | ComponentNameGenerator = ComponentNameGenerator('ipython.component') |
|
173 | ComponentNameGenerator = ComponentNameGenerator('ipython.component') | |
174 |
|
174 | |||
175 |
|
175 | |||
176 | class MetaComponent(MetaHasTraitlets, MetaComponentTracker): |
|
176 | class MetaComponent(MetaHasTraitlets, MetaComponentTracker): | |
177 | pass |
|
177 | pass | |
178 |
|
178 | |||
179 |
|
179 | |||
180 | #----------------------------------------------------------------------------- |
|
180 | #----------------------------------------------------------------------------- | |
181 | # Component implementation |
|
181 | # Component implementation | |
182 | #----------------------------------------------------------------------------- |
|
182 | #----------------------------------------------------------------------------- | |
183 |
|
183 | |||
184 |
|
184 | |||
185 | class Component(HasTraitlets): |
|
185 | class Component(HasTraitlets): | |
186 |
|
186 | |||
187 | __metaclass__ = MetaComponent |
|
187 | __metaclass__ = MetaComponent | |
188 |
|
188 | |||
189 | # Traitlets are fun! |
|
189 | # Traitlets are fun! | |
190 | config = Instance(Struct,(),{}) |
|
190 | config = Instance(Struct,(),{}) | |
191 | parent = This() |
|
191 | parent = This() | |
192 | root = This() |
|
192 | root = This() | |
193 | created = None |
|
193 | created = None | |
194 |
|
194 | |||
195 | def __init__(self, parent, name=None, config=None): |
|
195 | def __init__(self, parent, name=None, config=None): | |
196 | """Create a component given a parent and possibly and name and config. |
|
196 | """Create a component given a parent and possibly and name and config. | |
197 |
|
197 | |||
198 | Parameters |
|
198 | Parameters | |
199 | ---------- |
|
199 | ---------- | |
200 | parent : Component subclass |
|
200 | parent : Component subclass | |
201 | The parent in the component graph. The parent is used |
|
201 | The parent in the component graph. The parent is used | |
202 | to get the root of the component graph. |
|
202 | to get the root of the component graph. | |
203 | name : str |
|
203 | name : str | |
204 | The unique name of the component. If empty, then a unique |
|
204 | The unique name of the component. If empty, then a unique | |
205 | one will be autogenerated. |
|
205 | one will be autogenerated. | |
206 | config : Struct |
|
206 | config : Struct | |
207 | If this is empty, self.config = parent.config, otherwise |
|
207 | If this is empty, self.config = parent.config, otherwise | |
208 | self.config = config and root.config is ignored. This argument |
|
208 | self.config = config and root.config is ignored. This argument | |
209 | should only be used to *override* the automatic inheritance of |
|
209 | should only be used to *override* the automatic inheritance of | |
210 | parent.config. If a caller wants to modify parent.config |
|
210 | parent.config. If a caller wants to modify parent.config | |
211 | (not override), the caller should make a copy and change |
|
211 | (not override), the caller should make a copy and change | |
212 | attributes and then pass the copy to this argument. |
|
212 | attributes and then pass the copy to this argument. | |
213 |
|
213 | |||
214 | Notes |
|
214 | Notes | |
215 | ----- |
|
215 | ----- | |
216 | Subclasses of Component must call the :meth:`__init__` method of |
|
216 | Subclasses of Component must call the :meth:`__init__` method of | |
217 | :class:`Component` *before* doing anything else and using |
|
217 | :class:`Component` *before* doing anything else and using | |
218 | :func:`super`:: |
|
218 | :func:`super`:: | |
219 |
|
219 | |||
220 | class MyComponent(Component): |
|
220 | class MyComponent(Component): | |
221 | def __init__(self, parent, name=None, config=None): |
|
221 | def __init__(self, parent, name=None, config=None): | |
222 | super(MyComponent, self).__init__(parent, name, config) |
|
222 | super(MyComponent, self).__init__(parent, name, config) | |
223 | # Then any other code you need to finish initialization. |
|
223 | # Then any other code you need to finish initialization. | |
224 |
|
224 | |||
225 | This ensures that the :attr:`parent`, :attr:`name` and :attr:`config` |
|
225 | This ensures that the :attr:`parent`, :attr:`name` and :attr:`config` | |
226 | attributes are handled properly. |
|
226 | attributes are handled properly. | |
227 | """ |
|
227 | """ | |
228 | super(Component, self).__init__() |
|
228 | super(Component, self).__init__() | |
229 | self._children = [] |
|
229 | self._children = [] | |
230 | if name is None: |
|
230 | if name is None: | |
231 | self.name = ComponentNameGenerator() |
|
231 | self.name = ComponentNameGenerator() | |
232 | else: |
|
232 | else: | |
233 | self.name = name |
|
233 | self.name = name | |
234 | self.root = self # This is the default, it is set when parent is set |
|
234 | self.root = self # This is the default, it is set when parent is set | |
235 | self.parent = parent |
|
235 | self.parent = parent | |
236 | if config is not None: |
|
236 | if config is not None: | |
237 | self.config = deepcopy(config) |
|
237 | self.config = deepcopy(config) | |
238 | else: |
|
238 | else: | |
239 | if self.parent is not None: |
|
239 | if self.parent is not None: | |
240 | self.config = deepcopy(self.parent.config) |
|
240 | self.config = deepcopy(self.parent.config) | |
241 |
|
241 | |||
242 | self.created = datetime.datetime.now() |
|
242 | self.created = datetime.datetime.now() | |
243 |
|
243 | |||
244 | #------------------------------------------------------------------------- |
|
244 | #------------------------------------------------------------------------- | |
245 | # Static traitlet notifiations |
|
245 | # Static traitlet notifiations | |
246 | #------------------------------------------------------------------------- |
|
246 | #------------------------------------------------------------------------- | |
247 |
|
247 | |||
248 | def _parent_changed(self, name, old, new): |
|
248 | def _parent_changed(self, name, old, new): | |
249 | if old is not None: |
|
249 | if old is not None: | |
250 | old._remove_child(self) |
|
250 | old._remove_child(self) | |
251 | if new is not None: |
|
251 | if new is not None: | |
252 | new._add_child(self) |
|
252 | new._add_child(self) | |
253 |
|
253 | |||
254 | if new is None: |
|
254 | if new is None: | |
255 | self.root = self |
|
255 | self.root = self | |
256 | else: |
|
256 | else: | |
257 | self.root = new.root |
|
257 | self.root = new.root | |
258 |
|
258 | |||
259 | def _root_changed(self, name, old, new): |
|
259 | def _root_changed(self, name, old, new): | |
260 | if self.parent is None: |
|
260 | if self.parent is None: | |
261 | if not (new is self): |
|
261 | if not (new is self): | |
262 | raise ComponentError("Root not self, but parent is None.") |
|
262 | raise ComponentError("Root not self, but parent is None.") | |
263 | else: |
|
263 | else: | |
264 | if not self.parent.root is new: |
|
264 | if not self.parent.root is new: | |
265 | raise ComponentError("Error in setting the root attribute: " |
|
265 | raise ComponentError("Error in setting the root attribute: " | |
266 | "root != parent.root") |
|
266 | "root != parent.root") | |
267 |
|
267 | |||
268 | def _config_changed(self, name, old, new): |
|
268 | def _config_changed(self, name, old, new): | |
269 | """Update all the class traits having a config_key with the config. |
|
269 | """Update all the class traits having a config_key with the config. | |
270 |
|
270 | |||
271 | For any class traitlet with a ``config_key`` metadata attribute, we |
|
271 | For any class traitlet with a ``config_key`` metadata attribute, we | |
272 | update the traitlet with the value of the corresponding config entry. |
|
272 | update the traitlet with the value of the corresponding config entry. | |
273 |
|
273 | |||
274 | In the future, we might want to do a pop here so stale config info |
|
274 | In the future, we might want to do a pop here so stale config info | |
275 | is not passed onto children. |
|
275 | is not passed onto children. | |
276 | """ |
|
276 | """ | |
277 | # Get all traitlets with a config_key metadata entry |
|
277 | # Get all traitlets with a config_key metadata entry | |
278 | traitlets = self.traitlets('config_key') |
|
278 | traitlets = self.traitlets('config_key') | |
279 | for k, v in traitlets.items(): |
|
279 | for k, v in traitlets.items(): | |
280 | try: |
|
280 | try: | |
281 | config_value = new[v.get_metadata('config_key')] |
|
281 | config_value = new[v.get_metadata('config_key')] | |
282 | except KeyError: |
|
282 | except KeyError: | |
283 | pass |
|
283 | pass | |
284 | else: |
|
284 | else: | |
285 | setattr(self, k, config_value) |
|
285 | setattr(self, k, config_value) | |
286 |
|
286 | |||
287 | @property |
|
287 | @property | |
288 | def children(self): |
|
288 | def children(self): | |
289 | """A list of all my child components.""" |
|
289 | """A list of all my child components.""" | |
290 | return self._children |
|
290 | return self._children | |
291 |
|
291 | |||
292 | def _remove_child(self, child): |
|
292 | def _remove_child(self, child): | |
293 | """A private method for removing children components.""" |
|
293 | """A private method for removing children components.""" | |
294 | if child in self._children: |
|
294 | if child in self._children: | |
295 | index = self._children.index(child) |
|
295 | index = self._children.index(child) | |
296 | del self._children[index] |
|
296 | del self._children[index] | |
297 |
|
297 | |||
298 | def _add_child(self, child): |
|
298 | def _add_child(self, child): | |
299 | """A private method for adding children components.""" |
|
299 | """A private method for adding children components.""" | |
300 | if child not in self._children: |
|
300 | if child not in self._children: | |
301 | self._children.append(child) |
|
301 | self._children.append(child) | |
302 |
|
302 | |||
303 | def __repr__(self): |
|
303 | def __repr__(self): | |
304 |
return "<%s('%s')>" % (self.__class__.__name__, |
|
304 | return "<%s('%s')>" % (self.__class__.__name__, self.name) | |
305 | # return "<Component('%s')>" % self.name |
|
@@ -1,62 +1,72 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | A context manager for handling sys.displayhook. |
|
4 | A context manager for handling sys.displayhook. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Robert Kern |
|
8 | * Robert Kern | |
9 | * Brian Granger |
|
9 | * Brian Granger | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Copyright (C) 2008-2009 The IPython Development Team |
|
13 | # Copyright (C) 2008-2009 The IPython Development Team | |
14 | # |
|
14 | # | |
15 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | # Distributed under the terms of the BSD License. The full license is in | |
16 | # the file COPYING, distributed as part of this software. |
|
16 | # the file COPYING, distributed as part of this software. | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Imports |
|
20 | # Imports | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | import sys |
|
23 | import sys | |
24 |
|
24 | |||
25 | from IPython.core.component import Component |
|
25 | from IPython.core.component import Component | |
26 |
|
26 | |||
|
27 | from IPython.utils.autoattr import auto_attr | |||
|
28 | ||||
27 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
28 | # Classes and functions |
|
30 | # Classes and functions | |
29 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
30 |
|
32 | |||
31 |
|
33 | |||
32 | class DisplayTrap(Component): |
|
34 | class DisplayTrap(Component): | |
33 | """Object to manage sys.displayhook. |
|
35 | """Object to manage sys.displayhook. | |
34 |
|
36 | |||
35 | This came from IPython.core.kernel.display_hook, but is simplified |
|
37 | This came from IPython.core.kernel.display_hook, but is simplified | |
36 | (no callbacks or formatters) until more of the core is refactored. |
|
38 | (no callbacks or formatters) until more of the core is refactored. | |
37 | """ |
|
39 | """ | |
38 |
|
40 | |||
39 | def __init__(self, parent, hook): |
|
41 | def __init__(self, parent, hook): | |
40 | super(DisplayTrap, self).__init__(parent, None, None) |
|
42 | super(DisplayTrap, self).__init__(parent, None, None) | |
41 |
|
43 | |||
42 | self.hook = hook |
|
44 | self.hook = hook | |
43 | self.old_hook = None |
|
45 | self.old_hook = None | |
44 |
|
46 | |||
|
47 | @auto_attr | |||
|
48 | def shell(self): | |||
|
49 | shell = Component.get_instances( | |||
|
50 | root=self.root, | |||
|
51 | klass='IPython.core.iplib.InteractiveShell' | |||
|
52 | )[0] | |||
|
53 | return shell | |||
|
54 | ||||
45 | def __enter__(self): |
|
55 | def __enter__(self): | |
46 | self.set() |
|
56 | self.set() | |
47 | return self |
|
57 | return self | |
48 |
|
58 | |||
49 | def __exit__(self, type, value, traceback): |
|
59 | def __exit__(self, type, value, traceback): | |
50 | self.unset() |
|
60 | self.unset() | |
51 | return True |
|
61 | return True | |
52 |
|
62 | |||
53 | def set(self): |
|
63 | def set(self): | |
54 | """Set the hook.""" |
|
64 | """Set the hook.""" | |
55 | if sys.displayhook is not self.hook: |
|
65 | if sys.displayhook is not self.hook: | |
56 | self.old_hook = sys.displayhook |
|
66 | self.old_hook = sys.displayhook | |
57 | sys.displayhook = self.hook |
|
67 | sys.displayhook = self.hook | |
58 |
|
68 | |||
59 | def unset(self): |
|
69 | def unset(self): | |
60 | """Unset the hook.""" |
|
70 | """Unset the hook.""" | |
61 | sys.displayhook = self.old_hook |
|
71 | sys.displayhook = self.old_hook | |
62 |
|
72 |
@@ -1,2849 +1,2506 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Main IPython Component |
|
3 | Main IPython Component | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
8 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
8 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
9 | # Copyright (C) 2008-2009 The IPython Development Team |
|
9 | # Copyright (C) 2008-2009 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from __future__ import with_statement |
|
19 | from __future__ import with_statement | |
20 |
|
20 | |||
21 | import __main__ |
|
21 | import __main__ | |
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | import StringIO |
|
23 | import StringIO | |
24 | import bdb |
|
24 | import bdb | |
25 | import codeop |
|
25 | import codeop | |
26 | import exceptions |
|
26 | import exceptions | |
27 | import glob |
|
27 | import glob | |
28 | import keyword |
|
28 | import keyword | |
29 | import new |
|
29 | import new | |
30 | import os |
|
30 | import os | |
31 | import re |
|
31 | import re | |
32 | import shutil |
|
32 | import shutil | |
33 | import string |
|
33 | import string | |
34 | import sys |
|
34 | import sys | |
35 | import tempfile |
|
35 | import tempfile | |
36 | from contextlib import nested |
|
36 | from contextlib import nested | |
37 |
|
37 | |||
38 | from IPython.core import ultratb |
|
38 | from IPython.core import ultratb | |
39 | from IPython.core import debugger, oinspect |
|
39 | from IPython.core import debugger, oinspect | |
40 | from IPython.core import shadowns |
|
40 | from IPython.core import shadowns | |
41 | from IPython.core import history as ipcorehist |
|
41 | from IPython.core import history as ipcorehist | |
42 | from IPython.core import prefilter |
|
42 | from IPython.core import prefilter | |
43 | from IPython.core.alias import AliasManager |
|
43 | from IPython.core.alias import AliasManager | |
44 | from IPython.core.autocall import IPyAutocall |
|
44 | from IPython.core.autocall import IPyAutocall | |
45 | from IPython.core.builtin_trap import BuiltinTrap |
|
45 | from IPython.core.builtin_trap import BuiltinTrap | |
46 | from IPython.core.display_trap import DisplayTrap |
|
46 | from IPython.core.display_trap import DisplayTrap | |
47 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
47 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict | |
48 | from IPython.core.logger import Logger |
|
48 | from IPython.core.logger import Logger | |
49 | from IPython.core.magic import Magic |
|
49 | from IPython.core.magic import Magic | |
50 | from IPython.core.prompts import CachedOutput |
|
50 | from IPython.core.prompts import CachedOutput | |
51 | from IPython.core.page import page |
|
51 | from IPython.core.page import page | |
|
52 | from IPython.core.prefilter import PrefilterManager | |||
52 | from IPython.core.component import Component |
|
53 | from IPython.core.component import Component | |
53 | from IPython.core.oldusersetup import user_setup |
|
54 | from IPython.core.oldusersetup import user_setup | |
54 | from IPython.core.usage import interactive_usage, default_banner |
|
55 | from IPython.core.usage import interactive_usage, default_banner | |
55 | from IPython.core.error import TryNext, UsageError |
|
56 | from IPython.core.error import TryNext, UsageError | |
|
57 | from IPython.core.splitinput import split_user_input | |||
56 |
|
58 | |||
57 | from IPython.extensions import pickleshare |
|
59 | from IPython.extensions import pickleshare | |
58 | from IPython.external.Itpl import ItplNS |
|
60 | from IPython.external.Itpl import ItplNS | |
59 | from IPython.lib.backgroundjobs import BackgroundJobManager |
|
61 | from IPython.lib.backgroundjobs import BackgroundJobManager | |
60 | from IPython.utils.ipstruct import Struct |
|
62 | from IPython.utils.ipstruct import Struct | |
61 | from IPython.utils import PyColorize |
|
63 | from IPython.utils import PyColorize | |
62 | from IPython.utils.genutils import * |
|
64 | from IPython.utils.genutils import * | |
63 | from IPython.utils.strdispatch import StrDispatch |
|
65 | from IPython.utils.strdispatch import StrDispatch | |
64 | from IPython.utils.platutils import toggle_set_term_title, set_term_title |
|
66 | from IPython.utils.platutils import toggle_set_term_title, set_term_title | |
65 |
|
67 | |||
66 | from IPython.utils import growl |
|
68 | # from IPython.utils import growl | |
67 | growl.start("IPython") |
|
69 | # growl.start("IPython") | |
68 |
|
70 | |||
69 | from IPython.utils.traitlets import ( |
|
71 | from IPython.utils.traitlets import ( | |
70 | Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode |
|
72 | Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode | |
71 | ) |
|
73 | ) | |
72 |
|
74 | |||
73 | #----------------------------------------------------------------------------- |
|
75 | #----------------------------------------------------------------------------- | |
74 | # Globals |
|
76 | # Globals | |
75 | #----------------------------------------------------------------------------- |
|
77 | #----------------------------------------------------------------------------- | |
76 |
|
78 | |||
77 |
|
79 | |||
78 | # store the builtin raw_input globally, and use this always, in case user code |
|
80 | # store the builtin raw_input globally, and use this always, in case user code | |
79 | # overwrites it (like wx.py.PyShell does) |
|
81 | # overwrites it (like wx.py.PyShell does) | |
80 | raw_input_original = raw_input |
|
82 | raw_input_original = raw_input | |
81 |
|
83 | |||
82 | # compiled regexps for autoindent management |
|
84 | # compiled regexps for autoindent management | |
83 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
85 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
84 |
|
86 | |||
85 |
|
87 | |||
86 | #----------------------------------------------------------------------------- |
|
88 | #----------------------------------------------------------------------------- | |
87 | # Utilities |
|
89 | # Utilities | |
88 | #----------------------------------------------------------------------------- |
|
90 | #----------------------------------------------------------------------------- | |
89 |
|
91 | |||
90 |
|
92 | |||
91 | ini_spaces_re = re.compile(r'^(\s+)') |
|
93 | ini_spaces_re = re.compile(r'^(\s+)') | |
92 |
|
94 | |||
93 |
|
95 | |||
94 | def num_ini_spaces(strng): |
|
96 | def num_ini_spaces(strng): | |
95 | """Return the number of initial spaces in a string""" |
|
97 | """Return the number of initial spaces in a string""" | |
96 |
|
98 | |||
97 | ini_spaces = ini_spaces_re.match(strng) |
|
99 | ini_spaces = ini_spaces_re.match(strng) | |
98 | if ini_spaces: |
|
100 | if ini_spaces: | |
99 | return ini_spaces.end() |
|
101 | return ini_spaces.end() | |
100 | else: |
|
102 | else: | |
101 | return 0 |
|
103 | return 0 | |
102 |
|
104 | |||
103 |
|
105 | |||
104 | def softspace(file, newvalue): |
|
106 | def softspace(file, newvalue): | |
105 | """Copied from code.py, to remove the dependency""" |
|
107 | """Copied from code.py, to remove the dependency""" | |
106 |
|
108 | |||
107 | oldvalue = 0 |
|
109 | oldvalue = 0 | |
108 | try: |
|
110 | try: | |
109 | oldvalue = file.softspace |
|
111 | oldvalue = file.softspace | |
110 | except AttributeError: |
|
112 | except AttributeError: | |
111 | pass |
|
113 | pass | |
112 | try: |
|
114 | try: | |
113 | file.softspace = newvalue |
|
115 | file.softspace = newvalue | |
114 | except (AttributeError, TypeError): |
|
116 | except (AttributeError, TypeError): | |
115 | # "attribute-less object" or "read-only attributes" |
|
117 | # "attribute-less object" or "read-only attributes" | |
116 | pass |
|
118 | pass | |
117 | return oldvalue |
|
119 | return oldvalue | |
118 |
|
120 | |||
119 |
|
121 | |||
120 | class SpaceInInput(exceptions.Exception): pass |
|
122 | class SpaceInInput(exceptions.Exception): pass | |
121 |
|
123 | |||
122 | class Bunch: pass |
|
124 | class Bunch: pass | |
123 |
|
125 | |||
124 | class InputList(list): |
|
126 | class InputList(list): | |
125 | """Class to store user input. |
|
127 | """Class to store user input. | |
126 |
|
128 | |||
127 | It's basically a list, but slices return a string instead of a list, thus |
|
129 | It's basically a list, but slices return a string instead of a list, thus | |
128 | allowing things like (assuming 'In' is an instance): |
|
130 | allowing things like (assuming 'In' is an instance): | |
129 |
|
131 | |||
130 | exec In[4:7] |
|
132 | exec In[4:7] | |
131 |
|
133 | |||
132 | or |
|
134 | or | |
133 |
|
135 | |||
134 | exec In[5:9] + In[14] + In[21:25]""" |
|
136 | exec In[5:9] + In[14] + In[21:25]""" | |
135 |
|
137 | |||
136 | def __getslice__(self,i,j): |
|
138 | def __getslice__(self,i,j): | |
137 | return ''.join(list.__getslice__(self,i,j)) |
|
139 | return ''.join(list.__getslice__(self,i,j)) | |
138 |
|
140 | |||
139 |
|
141 | |||
140 | class SyntaxTB(ultratb.ListTB): |
|
142 | class SyntaxTB(ultratb.ListTB): | |
141 | """Extension which holds some state: the last exception value""" |
|
143 | """Extension which holds some state: the last exception value""" | |
142 |
|
144 | |||
143 | def __init__(self,color_scheme = 'NoColor'): |
|
145 | def __init__(self,color_scheme = 'NoColor'): | |
144 | ultratb.ListTB.__init__(self,color_scheme) |
|
146 | ultratb.ListTB.__init__(self,color_scheme) | |
145 | self.last_syntax_error = None |
|
147 | self.last_syntax_error = None | |
146 |
|
148 | |||
147 | def __call__(self, etype, value, elist): |
|
149 | def __call__(self, etype, value, elist): | |
148 | self.last_syntax_error = value |
|
150 | self.last_syntax_error = value | |
149 | ultratb.ListTB.__call__(self,etype,value,elist) |
|
151 | ultratb.ListTB.__call__(self,etype,value,elist) | |
150 |
|
152 | |||
151 | def clear_err_state(self): |
|
153 | def clear_err_state(self): | |
152 | """Return the current error state and clear it""" |
|
154 | """Return the current error state and clear it""" | |
153 | e = self.last_syntax_error |
|
155 | e = self.last_syntax_error | |
154 | self.last_syntax_error = None |
|
156 | self.last_syntax_error = None | |
155 | return e |
|
157 | return e | |
156 |
|
158 | |||
157 |
|
159 | |||
158 | def get_default_editor(): |
|
160 | def get_default_editor(): | |
159 | try: |
|
161 | try: | |
160 | ed = os.environ['EDITOR'] |
|
162 | ed = os.environ['EDITOR'] | |
161 | except KeyError: |
|
163 | except KeyError: | |
162 | if os.name == 'posix': |
|
164 | if os.name == 'posix': | |
163 | ed = 'vi' # the only one guaranteed to be there! |
|
165 | ed = 'vi' # the only one guaranteed to be there! | |
164 | else: |
|
166 | else: | |
165 | ed = 'notepad' # same in Windows! |
|
167 | ed = 'notepad' # same in Windows! | |
166 | return ed |
|
168 | return ed | |
167 |
|
169 | |||
168 |
|
170 | |||
169 | class SeparateStr(Str): |
|
171 | class SeparateStr(Str): | |
170 | """A Str subclass to validate separate_in, separate_out, etc. |
|
172 | """A Str subclass to validate separate_in, separate_out, etc. | |
171 |
|
173 | |||
172 | This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'. |
|
174 | This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'. | |
173 | """ |
|
175 | """ | |
174 |
|
176 | |||
175 | def validate(self, obj, value): |
|
177 | def validate(self, obj, value): | |
176 | if value == '0': value = '' |
|
178 | if value == '0': value = '' | |
177 | value = value.replace('\\n','\n') |
|
179 | value = value.replace('\\n','\n') | |
178 | return super(SeparateStr, self).validate(obj, value) |
|
180 | return super(SeparateStr, self).validate(obj, value) | |
179 |
|
181 | |||
180 |
|
182 | |||
181 | #----------------------------------------------------------------------------- |
|
183 | #----------------------------------------------------------------------------- | |
182 | # Main IPython class |
|
184 | # Main IPython class | |
183 | #----------------------------------------------------------------------------- |
|
185 | #----------------------------------------------------------------------------- | |
184 |
|
186 | |||
185 |
|
187 | |||
186 | class InteractiveShell(Component, Magic): |
|
188 | class InteractiveShell(Component, Magic): | |
187 | """An enhanced, interactive shell for Python.""" |
|
189 | """An enhanced, interactive shell for Python.""" | |
188 |
|
190 | |||
189 | autocall = Enum((0,1,2), config_key='AUTOCALL') |
|
191 | autocall = Enum((0,1,2), config_key='AUTOCALL') | |
190 | autoedit_syntax = CBool(False, config_key='AUTOEDIT_SYNTAX') |
|
192 | autoedit_syntax = CBool(False, config_key='AUTOEDIT_SYNTAX') | |
191 | autoindent = CBool(True, config_key='AUTOINDENT') |
|
193 | autoindent = CBool(True, config_key='AUTOINDENT') | |
192 | automagic = CBool(True, config_key='AUTOMAGIC') |
|
194 | automagic = CBool(True, config_key='AUTOMAGIC') | |
193 | display_banner = CBool(True, config_key='DISPLAY_BANNER') |
|
195 | display_banner = CBool(True, config_key='DISPLAY_BANNER') | |
194 | banner = Str('') |
|
196 | banner = Str('') | |
195 | banner1 = Str(default_banner, config_key='BANNER1') |
|
197 | banner1 = Str(default_banner, config_key='BANNER1') | |
196 | banner2 = Str('', config_key='BANNER2') |
|
198 | banner2 = Str('', config_key='BANNER2') | |
197 | c = Str('', config_key='C') |
|
199 | c = Str('', config_key='C') | |
198 | cache_size = Int(1000, config_key='CACHE_SIZE') |
|
200 | cache_size = Int(1000, config_key='CACHE_SIZE') | |
199 | classic = CBool(False, config_key='CLASSIC') |
|
201 | classic = CBool(False, config_key='CLASSIC') | |
200 | color_info = CBool(True, config_key='COLOR_INFO') |
|
202 | color_info = CBool(True, config_key='COLOR_INFO') | |
201 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
203 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
202 | default_value='LightBG', config_key='COLORS') |
|
204 | default_value='LightBG', config_key='COLORS') | |
203 | confirm_exit = CBool(True, config_key='CONFIRM_EXIT') |
|
205 | confirm_exit = CBool(True, config_key='CONFIRM_EXIT') | |
204 | debug = CBool(False, config_key='DEBUG') |
|
206 | debug = CBool(False, config_key='DEBUG') | |
205 | deep_reload = CBool(False, config_key='DEEP_RELOAD') |
|
207 | deep_reload = CBool(False, config_key='DEEP_RELOAD') | |
206 | embedded = CBool(False) |
|
208 | embedded = CBool(False) | |
207 | embedded_active = CBool(False) |
|
209 | embedded_active = CBool(False) | |
208 | editor = Str(get_default_editor(), config_key='EDITOR') |
|
210 | editor = Str(get_default_editor(), config_key='EDITOR') | |
209 | filename = Str("<ipython console>") |
|
211 | filename = Str("<ipython console>") | |
210 | interactive = CBool(False, config_key='INTERACTIVE') |
|
212 | interactive = CBool(False, config_key='INTERACTIVE') | |
211 | ipythondir= Unicode('', config_key='IPYTHONDIR') # Set to os.getcwd() in __init__ |
|
213 | ipythondir= Unicode('', config_key='IPYTHONDIR') # Set to os.getcwd() in __init__ | |
212 | logstart = CBool(False, config_key='LOGSTART') |
|
214 | logstart = CBool(False, config_key='LOGSTART') | |
213 | logfile = Str('', config_key='LOGFILE') |
|
215 | logfile = Str('', config_key='LOGFILE') | |
214 | logplay = Str('', config_key='LOGPLAY') |
|
216 | logplay = Str('', config_key='LOGPLAY') | |
215 | multi_line_specials = CBool(True, config_key='MULTI_LINE_SPECIALS') |
|
|||
216 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
217 | object_info_string_level = Enum((0,1,2), default_value=0, | |
217 | config_keys='OBJECT_INFO_STRING_LEVEL') |
|
218 | config_keys='OBJECT_INFO_STRING_LEVEL') | |
218 | pager = Str('less', config_key='PAGER') |
|
219 | pager = Str('less', config_key='PAGER') | |
219 | pdb = CBool(False, config_key='PDB') |
|
220 | pdb = CBool(False, config_key='PDB') | |
220 | pprint = CBool(True, config_key='PPRINT') |
|
221 | pprint = CBool(True, config_key='PPRINT') | |
221 | profile = Str('', config_key='PROFILE') |
|
222 | profile = Str('', config_key='PROFILE') | |
222 | prompt_in1 = Str('In [\\#]: ', config_key='PROMPT_IN1') |
|
223 | prompt_in1 = Str('In [\\#]: ', config_key='PROMPT_IN1') | |
223 | prompt_in2 = Str(' .\\D.: ', config_key='PROMPT_IN2') |
|
224 | prompt_in2 = Str(' .\\D.: ', config_key='PROMPT_IN2') | |
224 | prompt_out = Str('Out[\\#]: ', config_key='PROMPT_OUT1') |
|
225 | prompt_out = Str('Out[\\#]: ', config_key='PROMPT_OUT1') | |
225 | prompts_pad_left = CBool(True, config_key='PROMPTS_PAD_LEFT') |
|
226 | prompts_pad_left = CBool(True, config_key='PROMPTS_PAD_LEFT') | |
226 | quiet = CBool(False, config_key='QUIET') |
|
227 | quiet = CBool(False, config_key='QUIET') | |
227 |
|
228 | |||
228 | readline_use = CBool(True, config_key='READLINE_USE') |
|
229 | readline_use = CBool(True, config_key='READLINE_USE') | |
229 | readline_merge_completions = CBool(True, |
|
230 | readline_merge_completions = CBool(True, | |
230 | config_key='READLINE_MERGE_COMPLETIONS') |
|
231 | config_key='READLINE_MERGE_COMPLETIONS') | |
231 | readline_omit__names = Enum((0,1,2), default_value=0, |
|
232 | readline_omit__names = Enum((0,1,2), default_value=0, | |
232 | config_key='READLINE_OMIT_NAMES') |
|
233 | config_key='READLINE_OMIT_NAMES') | |
233 | readline_remove_delims = Str('-/~', config_key='READLINE_REMOVE_DELIMS') |
|
234 | readline_remove_delims = Str('-/~', config_key='READLINE_REMOVE_DELIMS') | |
234 | readline_parse_and_bind = List([ |
|
235 | readline_parse_and_bind = List([ | |
235 | 'tab: complete', |
|
236 | 'tab: complete', | |
236 | '"\C-l": possible-completions', |
|
237 | '"\C-l": possible-completions', | |
237 | 'set show-all-if-ambiguous on', |
|
238 | 'set show-all-if-ambiguous on', | |
238 | '"\C-o": tab-insert', |
|
239 | '"\C-o": tab-insert', | |
239 | '"\M-i": " "', |
|
240 | '"\M-i": " "', | |
240 | '"\M-o": "\d\d\d\d"', |
|
241 | '"\M-o": "\d\d\d\d"', | |
241 | '"\M-I": "\d\d\d\d"', |
|
242 | '"\M-I": "\d\d\d\d"', | |
242 | '"\C-r": reverse-search-history', |
|
243 | '"\C-r": reverse-search-history', | |
243 | '"\C-s": forward-search-history', |
|
244 | '"\C-s": forward-search-history', | |
244 | '"\C-p": history-search-backward', |
|
245 | '"\C-p": history-search-backward', | |
245 | '"\C-n": history-search-forward', |
|
246 | '"\C-n": history-search-forward', | |
246 | '"\e[A": history-search-backward', |
|
247 | '"\e[A": history-search-backward', | |
247 | '"\e[B": history-search-forward', |
|
248 | '"\e[B": history-search-forward', | |
248 | '"\C-k": kill-line', |
|
249 | '"\C-k": kill-line', | |
249 | '"\C-u": unix-line-discard', |
|
250 | '"\C-u": unix-line-discard', | |
250 | ], allow_none=False, config_key='READLINE_PARSE_AND_BIND' |
|
251 | ], allow_none=False, config_key='READLINE_PARSE_AND_BIND' | |
251 | ) |
|
252 | ) | |
252 |
|
253 | |||
253 | screen_length = Int(0, config_key='SCREEN_LENGTH') |
|
254 | screen_length = Int(0, config_key='SCREEN_LENGTH') | |
254 |
|
255 | |||
255 | # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n' |
|
256 | # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n' | |
256 | separate_in = SeparateStr('\n', config_key='SEPARATE_IN') |
|
257 | separate_in = SeparateStr('\n', config_key='SEPARATE_IN') | |
257 | separate_out = SeparateStr('', config_key='SEPARATE_OUT') |
|
258 | separate_out = SeparateStr('', config_key='SEPARATE_OUT') | |
258 | separate_out2 = SeparateStr('', config_key='SEPARATE_OUT2') |
|
259 | separate_out2 = SeparateStr('', config_key='SEPARATE_OUT2') | |
259 |
|
260 | |||
260 | system_header = Str('IPython system call: ', config_key='SYSTEM_HEADER') |
|
261 | system_header = Str('IPython system call: ', config_key='SYSTEM_HEADER') | |
261 | system_verbose = CBool(False, config_key='SYSTEM_VERBOSE') |
|
262 | system_verbose = CBool(False, config_key='SYSTEM_VERBOSE') | |
262 | term_title = CBool(False, config_key='TERM_TITLE') |
|
263 | term_title = CBool(False, config_key='TERM_TITLE') | |
263 | wildcards_case_sensitive = CBool(True, config_key='WILDCARDS_CASE_SENSITIVE') |
|
264 | wildcards_case_sensitive = CBool(True, config_key='WILDCARDS_CASE_SENSITIVE') | |
264 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
265 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
265 | default_value='Context', config_key='XMODE') |
|
266 | default_value='Context', config_key='XMODE') | |
266 |
|
267 | |||
267 | alias = List(allow_none=False, config_key='ALIAS') |
|
268 | alias = List(allow_none=False, config_key='ALIAS') | |
268 | autoexec = List(allow_none=False) |
|
269 | autoexec = List(allow_none=False) | |
269 |
|
270 | |||
270 | # class attribute to indicate whether the class supports threads or not. |
|
271 | # class attribute to indicate whether the class supports threads or not. | |
271 | # Subclasses with thread support should override this as needed. |
|
272 | # Subclasses with thread support should override this as needed. | |
272 | isthreaded = False |
|
273 | isthreaded = False | |
273 |
|
274 | |||
274 | def __init__(self, parent=None, config=None, ipythondir=None, usage=None, |
|
275 | def __init__(self, parent=None, config=None, ipythondir=None, usage=None, | |
275 | user_ns=None, user_global_ns=None, |
|
276 | user_ns=None, user_global_ns=None, | |
276 | banner1=None, banner2=None, |
|
277 | banner1=None, banner2=None, | |
277 | custom_exceptions=((),None)): |
|
278 | custom_exceptions=((),None)): | |
278 |
|
279 | |||
279 | # This is where traitlets with a config_key argument are updated |
|
280 | # This is where traitlets with a config_key argument are updated | |
280 | # from the values on config. |
|
281 | # from the values on config. | |
281 | super(InteractiveShell, self).__init__(parent, config=config, name='__IP') |
|
282 | super(InteractiveShell, self).__init__(parent, config=config, name='__IP') | |
282 |
|
283 | |||
283 | # These are relatively independent and stateless |
|
284 | # These are relatively independent and stateless | |
284 | self.init_ipythondir(ipythondir) |
|
285 | self.init_ipythondir(ipythondir) | |
285 | self.init_instance_attrs() |
|
286 | self.init_instance_attrs() | |
286 | self.init_term_title() |
|
287 | self.init_term_title() | |
287 | self.init_usage(usage) |
|
288 | self.init_usage(usage) | |
288 | self.init_banner(banner1, banner2) |
|
289 | self.init_banner(banner1, banner2) | |
289 |
|
290 | |||
290 | # Create namespaces (user_ns, user_global_ns, alias_table, etc.) |
|
291 | # Create namespaces (user_ns, user_global_ns, alias_table, etc.) | |
291 | self.init_create_namespaces(user_ns, user_global_ns) |
|
292 | self.init_create_namespaces(user_ns, user_global_ns) | |
292 | # This has to be done after init_create_namespaces because it uses |
|
293 | # This has to be done after init_create_namespaces because it uses | |
293 | # something in self.user_ns, but before init_sys_modules, which |
|
294 | # something in self.user_ns, but before init_sys_modules, which | |
294 | # is the first thing to modify sys. |
|
295 | # is the first thing to modify sys. | |
295 | self.save_sys_module_state() |
|
296 | self.save_sys_module_state() | |
296 | self.init_sys_modules() |
|
297 | self.init_sys_modules() | |
297 |
|
298 | |||
298 | self.init_history() |
|
299 | self.init_history() | |
299 | self.init_encoding() |
|
300 | self.init_encoding() | |
300 |
self.init_ |
|
301 | self.init_prefilter() | |
301 |
|
302 | |||
302 | Magic.__init__(self, self) |
|
303 | Magic.__init__(self, self) | |
303 |
|
304 | |||
304 | self.init_syntax_highlighting() |
|
305 | self.init_syntax_highlighting() | |
305 | self.init_hooks() |
|
306 | self.init_hooks() | |
306 | self.init_pushd_popd_magic() |
|
307 | self.init_pushd_popd_magic() | |
307 | self.init_traceback_handlers(custom_exceptions) |
|
308 | self.init_traceback_handlers(custom_exceptions) | |
308 | self.init_user_ns() |
|
309 | self.init_user_ns() | |
309 | self.init_logger() |
|
310 | self.init_logger() | |
310 | self.init_alias() |
|
311 | self.init_alias() | |
311 | self.init_builtins() |
|
312 | self.init_builtins() | |
312 |
|
313 | |||
313 | # pre_config_initialization |
|
314 | # pre_config_initialization | |
314 | self.init_shadow_hist() |
|
315 | self.init_shadow_hist() | |
315 |
|
316 | |||
316 | # The next section should contain averything that was in ipmaker. |
|
317 | # The next section should contain averything that was in ipmaker. | |
317 | self.init_logstart() |
|
318 | self.init_logstart() | |
318 |
|
319 | |||
319 | # The following was in post_config_initialization |
|
320 | # The following was in post_config_initialization | |
320 | self.init_inspector() |
|
321 | self.init_inspector() | |
321 | self.init_readline() |
|
322 | self.init_readline() | |
322 | self.init_prompts() |
|
323 | self.init_prompts() | |
323 | self.init_displayhook() |
|
324 | self.init_displayhook() | |
324 | self.init_reload_doctest() |
|
325 | self.init_reload_doctest() | |
325 | self.init_magics() |
|
326 | self.init_magics() | |
326 | self.init_pdb() |
|
327 | self.init_pdb() | |
327 | self.hooks.late_startup_hook() |
|
328 | self.hooks.late_startup_hook() | |
328 |
|
329 | |||
329 | #------------------------------------------------------------------------- |
|
330 | #------------------------------------------------------------------------- | |
330 | # Traitlet changed handlers |
|
331 | # Traitlet changed handlers | |
331 | #------------------------------------------------------------------------- |
|
332 | #------------------------------------------------------------------------- | |
332 |
|
333 | |||
333 | def _banner1_changed(self): |
|
334 | def _banner1_changed(self): | |
334 | self.compute_banner() |
|
335 | self.compute_banner() | |
335 |
|
336 | |||
336 | def _banner2_changed(self): |
|
337 | def _banner2_changed(self): | |
337 | self.compute_banner() |
|
338 | self.compute_banner() | |
338 |
|
339 | |||
339 | @property |
|
340 | @property | |
340 | def usable_screen_length(self): |
|
341 | def usable_screen_length(self): | |
341 | if self.screen_length == 0: |
|
342 | if self.screen_length == 0: | |
342 | return 0 |
|
343 | return 0 | |
343 | else: |
|
344 | else: | |
344 | num_lines_bot = self.separate_in.count('\n')+1 |
|
345 | num_lines_bot = self.separate_in.count('\n')+1 | |
345 | return self.screen_length - num_lines_bot |
|
346 | return self.screen_length - num_lines_bot | |
346 |
|
347 | |||
347 | def _term_title_changed(self, name, new_value): |
|
348 | def _term_title_changed(self, name, new_value): | |
348 | self.init_term_title() |
|
349 | self.init_term_title() | |
349 |
|
350 | |||
350 | def set_autoindent(self,value=None): |
|
351 | def set_autoindent(self,value=None): | |
351 | """Set the autoindent flag, checking for readline support. |
|
352 | """Set the autoindent flag, checking for readline support. | |
352 |
|
353 | |||
353 | If called with no arguments, it acts as a toggle.""" |
|
354 | If called with no arguments, it acts as a toggle.""" | |
354 |
|
355 | |||
355 | if not self.has_readline: |
|
356 | if not self.has_readline: | |
356 | if os.name == 'posix': |
|
357 | if os.name == 'posix': | |
357 | warn("The auto-indent feature requires the readline library") |
|
358 | warn("The auto-indent feature requires the readline library") | |
358 | self.autoindent = 0 |
|
359 | self.autoindent = 0 | |
359 | return |
|
360 | return | |
360 | if value is None: |
|
361 | if value is None: | |
361 | self.autoindent = not self.autoindent |
|
362 | self.autoindent = not self.autoindent | |
362 | else: |
|
363 | else: | |
363 | self.autoindent = value |
|
364 | self.autoindent = value | |
364 |
|
365 | |||
365 | #------------------------------------------------------------------------- |
|
366 | #------------------------------------------------------------------------- | |
366 | # init_* methods called by __init__ |
|
367 | # init_* methods called by __init__ | |
367 | #------------------------------------------------------------------------- |
|
368 | #------------------------------------------------------------------------- | |
368 |
|
369 | |||
369 | def init_ipythondir(self, ipythondir): |
|
370 | def init_ipythondir(self, ipythondir): | |
370 | if ipythondir is not None: |
|
371 | if ipythondir is not None: | |
371 | self.ipythondir = ipythondir |
|
372 | self.ipythondir = ipythondir | |
372 | self.config.IPYTHONDIR = self.ipythondir |
|
373 | self.config.IPYTHONDIR = self.ipythondir | |
373 | return |
|
374 | return | |
374 |
|
375 | |||
375 | if hasattr(self.config, 'IPYTHONDIR'): |
|
376 | if hasattr(self.config, 'IPYTHONDIR'): | |
376 | self.ipythondir = self.config.IPYTHONDIR |
|
377 | self.ipythondir = self.config.IPYTHONDIR | |
377 | if not hasattr(self.config, 'IPYTHONDIR'): |
|
378 | if not hasattr(self.config, 'IPYTHONDIR'): | |
378 | # cdw is always defined |
|
379 | # cdw is always defined | |
379 | self.ipythondir = os.getcwd() |
|
380 | self.ipythondir = os.getcwd() | |
380 |
|
381 | |||
381 | # The caller must make sure that ipythondir exists. We should |
|
382 | # The caller must make sure that ipythondir exists. We should | |
382 | # probably handle this using a Dir traitlet. |
|
383 | # probably handle this using a Dir traitlet. | |
383 | if not os.path.isdir(self.ipythondir): |
|
384 | if not os.path.isdir(self.ipythondir): | |
384 | raise IOError('IPython dir does not exist: %s' % self.ipythondir) |
|
385 | raise IOError('IPython dir does not exist: %s' % self.ipythondir) | |
385 |
|
386 | |||
386 | # All children can just read this |
|
387 | # All children can just read this | |
387 | self.config.IPYTHONDIR = self.ipythondir |
|
388 | self.config.IPYTHONDIR = self.ipythondir | |
388 |
|
389 | |||
389 | def init_instance_attrs(self): |
|
390 | def init_instance_attrs(self): | |
390 | self.jobs = BackgroundJobManager() |
|
391 | self.jobs = BackgroundJobManager() | |
391 | self.more = False |
|
392 | self.more = False | |
392 |
|
393 | |||
393 | # command compiler |
|
394 | # command compiler | |
394 | self.compile = codeop.CommandCompiler() |
|
395 | self.compile = codeop.CommandCompiler() | |
395 |
|
396 | |||
396 | # User input buffer |
|
397 | # User input buffer | |
397 | self.buffer = [] |
|
398 | self.buffer = [] | |
398 |
|
399 | |||
399 | # Make an empty namespace, which extension writers can rely on both |
|
400 | # Make an empty namespace, which extension writers can rely on both | |
400 | # existing and NEVER being used by ipython itself. This gives them a |
|
401 | # existing and NEVER being used by ipython itself. This gives them a | |
401 | # convenient location for storing additional information and state |
|
402 | # convenient location for storing additional information and state | |
402 | # their extensions may require, without fear of collisions with other |
|
403 | # their extensions may require, without fear of collisions with other | |
403 | # ipython names that may develop later. |
|
404 | # ipython names that may develop later. | |
404 | self.meta = Struct() |
|
405 | self.meta = Struct() | |
405 |
|
406 | |||
406 | # Object variable to store code object waiting execution. This is |
|
407 | # Object variable to store code object waiting execution. This is | |
407 | # used mainly by the multithreaded shells, but it can come in handy in |
|
408 | # used mainly by the multithreaded shells, but it can come in handy in | |
408 | # other situations. No need to use a Queue here, since it's a single |
|
409 | # other situations. No need to use a Queue here, since it's a single | |
409 | # item which gets cleared once run. |
|
410 | # item which gets cleared once run. | |
410 | self.code_to_run = None |
|
411 | self.code_to_run = None | |
411 |
|
412 | |||
412 | # Flag to mark unconditional exit |
|
413 | # Flag to mark unconditional exit | |
413 | self.exit_now = False |
|
414 | self.exit_now = False | |
414 |
|
415 | |||
415 | # Temporary files used for various purposes. Deleted at exit. |
|
416 | # Temporary files used for various purposes. Deleted at exit. | |
416 | self.tempfiles = [] |
|
417 | self.tempfiles = [] | |
417 |
|
418 | |||
418 | # Keep track of readline usage (later set by init_readline) |
|
419 | # Keep track of readline usage (later set by init_readline) | |
419 | self.has_readline = False |
|
420 | self.has_readline = False | |
420 |
|
421 | |||
421 | # keep track of where we started running (mainly for crash post-mortem) |
|
422 | # keep track of where we started running (mainly for crash post-mortem) | |
422 | # This is not being used anywhere currently. |
|
423 | # This is not being used anywhere currently. | |
423 | self.starting_dir = os.getcwd() |
|
424 | self.starting_dir = os.getcwd() | |
424 |
|
425 | |||
425 | # Indentation management |
|
426 | # Indentation management | |
426 | self.indent_current_nsp = 0 |
|
427 | self.indent_current_nsp = 0 | |
427 |
|
428 | |||
428 | def init_term_title(self): |
|
429 | def init_term_title(self): | |
429 | # Enable or disable the terminal title. |
|
430 | # Enable or disable the terminal title. | |
430 | if self.term_title: |
|
431 | if self.term_title: | |
431 | toggle_set_term_title(True) |
|
432 | toggle_set_term_title(True) | |
432 | set_term_title('IPython: ' + abbrev_cwd()) |
|
433 | set_term_title('IPython: ' + abbrev_cwd()) | |
433 | else: |
|
434 | else: | |
434 | toggle_set_term_title(False) |
|
435 | toggle_set_term_title(False) | |
435 |
|
436 | |||
436 | def init_usage(self, usage=None): |
|
437 | def init_usage(self, usage=None): | |
437 | if usage is None: |
|
438 | if usage is None: | |
438 | self.usage = interactive_usage |
|
439 | self.usage = interactive_usage | |
439 | else: |
|
440 | else: | |
440 | self.usage = usage |
|
441 | self.usage = usage | |
441 |
|
442 | |||
442 | def init_banner(self, banner1, banner2): |
|
443 | def init_banner(self, banner1, banner2): | |
443 | if self.c: # regular python doesn't print the banner with -c |
|
444 | if self.c: # regular python doesn't print the banner with -c | |
444 | self.display_banner = False |
|
445 | self.display_banner = False | |
445 | if banner1 is not None: |
|
446 | if banner1 is not None: | |
446 | self.banner1 = banner1 |
|
447 | self.banner1 = banner1 | |
447 | if banner2 is not None: |
|
448 | if banner2 is not None: | |
448 | self.banner2 = banner2 |
|
449 | self.banner2 = banner2 | |
449 | self.compute_banner() |
|
450 | self.compute_banner() | |
450 |
|
451 | |||
451 | def compute_banner(self): |
|
452 | def compute_banner(self): | |
452 | self.banner = self.banner1 + '\n' |
|
453 | self.banner = self.banner1 + '\n' | |
453 | if self.profile: |
|
454 | if self.profile: | |
454 | self.banner += '\nIPython profile: %s\n' % self.profile |
|
455 | self.banner += '\nIPython profile: %s\n' % self.profile | |
455 | if self.banner2: |
|
456 | if self.banner2: | |
456 | self.banner += '\n' + self.banner2 + '\n' |
|
457 | self.banner += '\n' + self.banner2 + '\n' | |
457 |
|
458 | |||
458 | def init_encoding(self): |
|
459 | def init_encoding(self): | |
459 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
460 | # Get system encoding at startup time. Certain terminals (like Emacs | |
460 | # under Win32 have it set to None, and we need to have a known valid |
|
461 | # under Win32 have it set to None, and we need to have a known valid | |
461 | # encoding to use in the raw_input() method |
|
462 | # encoding to use in the raw_input() method | |
462 | try: |
|
463 | try: | |
463 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
464 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
464 | except AttributeError: |
|
465 | except AttributeError: | |
465 | self.stdin_encoding = 'ascii' |
|
466 | self.stdin_encoding = 'ascii' | |
466 |
|
467 | |||
467 | def init_syntax_highlighting(self): |
|
468 | def init_syntax_highlighting(self): | |
468 | # Python source parser/formatter for syntax highlighting |
|
469 | # Python source parser/formatter for syntax highlighting | |
469 | pyformat = PyColorize.Parser().format |
|
470 | pyformat = PyColorize.Parser().format | |
470 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
471 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
471 |
|
472 | |||
472 | def init_pushd_popd_magic(self): |
|
473 | def init_pushd_popd_magic(self): | |
473 | # for pushd/popd management |
|
474 | # for pushd/popd management | |
474 | try: |
|
475 | try: | |
475 | self.home_dir = get_home_dir() |
|
476 | self.home_dir = get_home_dir() | |
476 | except HomeDirError, msg: |
|
477 | except HomeDirError, msg: | |
477 | fatal(msg) |
|
478 | fatal(msg) | |
478 |
|
479 | |||
479 | self.dir_stack = [] |
|
480 | self.dir_stack = [] | |
480 |
|
481 | |||
481 | def init_logger(self): |
|
482 | def init_logger(self): | |
482 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') |
|
483 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') | |
483 | # local shortcut, this is used a LOT |
|
484 | # local shortcut, this is used a LOT | |
484 | self.log = self.logger.log |
|
485 | self.log = self.logger.log | |
485 | # template for logfile headers. It gets resolved at runtime by the |
|
486 | # template for logfile headers. It gets resolved at runtime by the | |
486 | # logstart method. |
|
487 | # logstart method. | |
487 | self.loghead_tpl = \ |
|
488 | self.loghead_tpl = \ | |
488 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
489 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** | |
489 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
490 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW | |
490 | #log# opts = %s |
|
491 | #log# opts = %s | |
491 | #log# args = %s |
|
492 | #log# args = %s | |
492 | #log# It is safe to make manual edits below here. |
|
493 | #log# It is safe to make manual edits below here. | |
493 | #log#----------------------------------------------------------------------- |
|
494 | #log#----------------------------------------------------------------------- | |
494 | """ |
|
495 | """ | |
495 |
|
496 | |||
496 | def init_logstart(self): |
|
497 | def init_logstart(self): | |
497 | if self.logplay: |
|
498 | if self.logplay: | |
498 | self.magic_logstart(self.logplay + ' append') |
|
499 | self.magic_logstart(self.logplay + ' append') | |
499 | elif self.logfile: |
|
500 | elif self.logfile: | |
500 | self.magic_logstart(self.logfile) |
|
501 | self.magic_logstart(self.logfile) | |
501 | elif self.logstart: |
|
502 | elif self.logstart: | |
502 | self.magic_logstart() |
|
503 | self.magic_logstart() | |
503 |
|
504 | |||
504 | def init_builtins(self): |
|
505 | def init_builtins(self): | |
505 | self.builtin_trap = BuiltinTrap(self) |
|
506 | self.builtin_trap = BuiltinTrap(self) | |
506 |
|
507 | |||
507 | def init_inspector(self): |
|
508 | def init_inspector(self): | |
508 | # Object inspector |
|
509 | # Object inspector | |
509 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
510 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
510 | PyColorize.ANSICodeColors, |
|
511 | PyColorize.ANSICodeColors, | |
511 | 'NoColor', |
|
512 | 'NoColor', | |
512 | self.object_info_string_level) |
|
513 | self.object_info_string_level) | |
513 |
|
514 | |||
514 | def init_prompts(self): |
|
515 | def init_prompts(self): | |
515 | # Initialize cache, set in/out prompts and printing system |
|
516 | # Initialize cache, set in/out prompts and printing system | |
516 | self.outputcache = CachedOutput(self, |
|
517 | self.outputcache = CachedOutput(self, | |
517 | self.cache_size, |
|
518 | self.cache_size, | |
518 | self.pprint, |
|
519 | self.pprint, | |
519 | input_sep = self.separate_in, |
|
520 | input_sep = self.separate_in, | |
520 | output_sep = self.separate_out, |
|
521 | output_sep = self.separate_out, | |
521 | output_sep2 = self.separate_out2, |
|
522 | output_sep2 = self.separate_out2, | |
522 | ps1 = self.prompt_in1, |
|
523 | ps1 = self.prompt_in1, | |
523 | ps2 = self.prompt_in2, |
|
524 | ps2 = self.prompt_in2, | |
524 | ps_out = self.prompt_out, |
|
525 | ps_out = self.prompt_out, | |
525 | pad_left = self.prompts_pad_left) |
|
526 | pad_left = self.prompts_pad_left) | |
526 |
|
527 | |||
527 | # user may have over-ridden the default print hook: |
|
528 | # user may have over-ridden the default print hook: | |
528 | try: |
|
529 | try: | |
529 | self.outputcache.__class__.display = self.hooks.display |
|
530 | self.outputcache.__class__.display = self.hooks.display | |
530 | except AttributeError: |
|
531 | except AttributeError: | |
531 | pass |
|
532 | pass | |
532 |
|
533 | |||
533 | def init_displayhook(self): |
|
534 | def init_displayhook(self): | |
534 | self.display_trap = DisplayTrap(self, self.outputcache) |
|
535 | self.display_trap = DisplayTrap(self, self.outputcache) | |
535 |
|
536 | |||
536 | def init_reload_doctest(self): |
|
537 | def init_reload_doctest(self): | |
537 | # Do a proper resetting of doctest, including the necessary displayhook |
|
538 | # Do a proper resetting of doctest, including the necessary displayhook | |
538 | # monkeypatching |
|
539 | # monkeypatching | |
539 | try: |
|
540 | try: | |
540 | doctest_reload() |
|
541 | doctest_reload() | |
541 | except ImportError: |
|
542 | except ImportError: | |
542 | warn("doctest module does not exist.") |
|
543 | warn("doctest module does not exist.") | |
543 |
|
544 | |||
544 | #------------------------------------------------------------------------- |
|
545 | #------------------------------------------------------------------------- | |
545 | # Things related to injections into the sys module |
|
546 | # Things related to injections into the sys module | |
546 | #------------------------------------------------------------------------- |
|
547 | #------------------------------------------------------------------------- | |
547 |
|
548 | |||
548 | def save_sys_module_state(self): |
|
549 | def save_sys_module_state(self): | |
549 | """Save the state of hooks in the sys module. |
|
550 | """Save the state of hooks in the sys module. | |
550 |
|
551 | |||
551 | This has to be called after self.user_ns is created. |
|
552 | This has to be called after self.user_ns is created. | |
552 | """ |
|
553 | """ | |
553 | self._orig_sys_module_state = {} |
|
554 | self._orig_sys_module_state = {} | |
554 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
555 | self._orig_sys_module_state['stdin'] = sys.stdin | |
555 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
556 | self._orig_sys_module_state['stdout'] = sys.stdout | |
556 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
557 | self._orig_sys_module_state['stderr'] = sys.stderr | |
557 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
558 | self._orig_sys_module_state['excepthook'] = sys.excepthook | |
558 | try: |
|
559 | try: | |
559 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
560 | self._orig_sys_modules_main_name = self.user_ns['__name__'] | |
560 | except KeyError: |
|
561 | except KeyError: | |
561 | pass |
|
562 | pass | |
562 |
|
563 | |||
563 | def restore_sys_module_state(self): |
|
564 | def restore_sys_module_state(self): | |
564 | """Restore the state of the sys module.""" |
|
565 | """Restore the state of the sys module.""" | |
565 | try: |
|
566 | try: | |
566 | for k, v in self._orig_sys_module_state.items(): |
|
567 | for k, v in self._orig_sys_module_state.items(): | |
567 | setattr(sys, k, v) |
|
568 | setattr(sys, k, v) | |
568 | except AttributeError: |
|
569 | except AttributeError: | |
569 | pass |
|
570 | pass | |
570 | try: |
|
571 | try: | |
571 | delattr(sys, 'ipcompleter') |
|
572 | delattr(sys, 'ipcompleter') | |
572 | except AttributeError: |
|
573 | except AttributeError: | |
573 | pass |
|
574 | pass | |
574 | # Reset what what done in self.init_sys_modules |
|
575 | # Reset what what done in self.init_sys_modules | |
575 | try: |
|
576 | try: | |
576 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
577 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name | |
577 | except (AttributeError, KeyError): |
|
578 | except (AttributeError, KeyError): | |
578 | pass |
|
579 | pass | |
579 |
|
580 | |||
580 | #------------------------------------------------------------------------- |
|
581 | #------------------------------------------------------------------------- | |
581 | # Things related to hooks |
|
582 | # Things related to hooks | |
582 | #------------------------------------------------------------------------- |
|
583 | #------------------------------------------------------------------------- | |
583 |
|
584 | |||
584 | def init_hooks(self): |
|
585 | def init_hooks(self): | |
585 | # hooks holds pointers used for user-side customizations |
|
586 | # hooks holds pointers used for user-side customizations | |
586 | self.hooks = Struct() |
|
587 | self.hooks = Struct() | |
587 |
|
588 | |||
588 | self.strdispatchers = {} |
|
589 | self.strdispatchers = {} | |
589 |
|
590 | |||
590 | # Set all default hooks, defined in the IPython.hooks module. |
|
591 | # Set all default hooks, defined in the IPython.hooks module. | |
591 | import IPython.core.hooks |
|
592 | import IPython.core.hooks | |
592 | hooks = IPython.core.hooks |
|
593 | hooks = IPython.core.hooks | |
593 | for hook_name in hooks.__all__: |
|
594 | for hook_name in hooks.__all__: | |
594 | # default hooks have priority 100, i.e. low; user hooks should have |
|
595 | # default hooks have priority 100, i.e. low; user hooks should have | |
595 | # 0-100 priority |
|
596 | # 0-100 priority | |
596 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
597 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
597 |
|
598 | |||
598 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
599 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
599 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
600 | """set_hook(name,hook) -> sets an internal IPython hook. | |
600 |
|
601 | |||
601 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
602 | IPython exposes some of its internal API as user-modifiable hooks. By | |
602 | adding your function to one of these hooks, you can modify IPython's |
|
603 | adding your function to one of these hooks, you can modify IPython's | |
603 | behavior to call at runtime your own routines.""" |
|
604 | behavior to call at runtime your own routines.""" | |
604 |
|
605 | |||
605 | # At some point in the future, this should validate the hook before it |
|
606 | # At some point in the future, this should validate the hook before it | |
606 | # accepts it. Probably at least check that the hook takes the number |
|
607 | # accepts it. Probably at least check that the hook takes the number | |
607 | # of args it's supposed to. |
|
608 | # of args it's supposed to. | |
608 |
|
609 | |||
609 | f = new.instancemethod(hook,self,self.__class__) |
|
610 | f = new.instancemethod(hook,self,self.__class__) | |
610 |
|
611 | |||
611 | # check if the hook is for strdispatcher first |
|
612 | # check if the hook is for strdispatcher first | |
612 | if str_key is not None: |
|
613 | if str_key is not None: | |
613 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
614 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
614 | sdp.add_s(str_key, f, priority ) |
|
615 | sdp.add_s(str_key, f, priority ) | |
615 | self.strdispatchers[name] = sdp |
|
616 | self.strdispatchers[name] = sdp | |
616 | return |
|
617 | return | |
617 | if re_key is not None: |
|
618 | if re_key is not None: | |
618 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
619 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
619 | sdp.add_re(re.compile(re_key), f, priority ) |
|
620 | sdp.add_re(re.compile(re_key), f, priority ) | |
620 | self.strdispatchers[name] = sdp |
|
621 | self.strdispatchers[name] = sdp | |
621 | return |
|
622 | return | |
622 |
|
623 | |||
623 | dp = getattr(self.hooks, name, None) |
|
624 | dp = getattr(self.hooks, name, None) | |
624 | if name not in IPython.core.hooks.__all__: |
|
625 | if name not in IPython.core.hooks.__all__: | |
625 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) |
|
626 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) | |
626 | if not dp: |
|
627 | if not dp: | |
627 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
628 | dp = IPython.core.hooks.CommandChainDispatcher() | |
628 |
|
629 | |||
629 | try: |
|
630 | try: | |
630 | dp.add(f,priority) |
|
631 | dp.add(f,priority) | |
631 | except AttributeError: |
|
632 | except AttributeError: | |
632 | # it was not commandchain, plain old func - replace |
|
633 | # it was not commandchain, plain old func - replace | |
633 | dp = f |
|
634 | dp = f | |
634 |
|
635 | |||
635 | setattr(self.hooks,name, dp) |
|
636 | setattr(self.hooks,name, dp) | |
636 |
|
637 | |||
637 | #------------------------------------------------------------------------- |
|
638 | #------------------------------------------------------------------------- | |
638 | # Things related to the "main" module |
|
639 | # Things related to the "main" module | |
639 | #------------------------------------------------------------------------- |
|
640 | #------------------------------------------------------------------------- | |
640 |
|
641 | |||
641 | def new_main_mod(self,ns=None): |
|
642 | def new_main_mod(self,ns=None): | |
642 | """Return a new 'main' module object for user code execution. |
|
643 | """Return a new 'main' module object for user code execution. | |
643 | """ |
|
644 | """ | |
644 | main_mod = self._user_main_module |
|
645 | main_mod = self._user_main_module | |
645 | init_fakemod_dict(main_mod,ns) |
|
646 | init_fakemod_dict(main_mod,ns) | |
646 | return main_mod |
|
647 | return main_mod | |
647 |
|
648 | |||
648 | def cache_main_mod(self,ns,fname): |
|
649 | def cache_main_mod(self,ns,fname): | |
649 | """Cache a main module's namespace. |
|
650 | """Cache a main module's namespace. | |
650 |
|
651 | |||
651 | When scripts are executed via %run, we must keep a reference to the |
|
652 | When scripts are executed via %run, we must keep a reference to the | |
652 | namespace of their __main__ module (a FakeModule instance) around so |
|
653 | namespace of their __main__ module (a FakeModule instance) around so | |
653 | that Python doesn't clear it, rendering objects defined therein |
|
654 | that Python doesn't clear it, rendering objects defined therein | |
654 | useless. |
|
655 | useless. | |
655 |
|
656 | |||
656 | This method keeps said reference in a private dict, keyed by the |
|
657 | This method keeps said reference in a private dict, keyed by the | |
657 | absolute path of the module object (which corresponds to the script |
|
658 | absolute path of the module object (which corresponds to the script | |
658 | path). This way, for multiple executions of the same script we only |
|
659 | path). This way, for multiple executions of the same script we only | |
659 | keep one copy of the namespace (the last one), thus preventing memory |
|
660 | keep one copy of the namespace (the last one), thus preventing memory | |
660 | leaks from old references while allowing the objects from the last |
|
661 | leaks from old references while allowing the objects from the last | |
661 | execution to be accessible. |
|
662 | execution to be accessible. | |
662 |
|
663 | |||
663 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
664 | Note: we can not allow the actual FakeModule instances to be deleted, | |
664 | because of how Python tears down modules (it hard-sets all their |
|
665 | because of how Python tears down modules (it hard-sets all their | |
665 | references to None without regard for reference counts). This method |
|
666 | references to None without regard for reference counts). This method | |
666 | must therefore make a *copy* of the given namespace, to allow the |
|
667 | must therefore make a *copy* of the given namespace, to allow the | |
667 | original module's __dict__ to be cleared and reused. |
|
668 | original module's __dict__ to be cleared and reused. | |
668 |
|
669 | |||
669 |
|
670 | |||
670 | Parameters |
|
671 | Parameters | |
671 | ---------- |
|
672 | ---------- | |
672 | ns : a namespace (a dict, typically) |
|
673 | ns : a namespace (a dict, typically) | |
673 |
|
674 | |||
674 | fname : str |
|
675 | fname : str | |
675 | Filename associated with the namespace. |
|
676 | Filename associated with the namespace. | |
676 |
|
677 | |||
677 | Examples |
|
678 | Examples | |
678 | -------- |
|
679 | -------- | |
679 |
|
680 | |||
680 | In [10]: import IPython |
|
681 | In [10]: import IPython | |
681 |
|
682 | |||
682 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
683 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
683 |
|
684 | |||
684 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
685 | In [12]: IPython.__file__ in _ip._main_ns_cache | |
685 | Out[12]: True |
|
686 | Out[12]: True | |
686 | """ |
|
687 | """ | |
687 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
688 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() | |
688 |
|
689 | |||
689 | def clear_main_mod_cache(self): |
|
690 | def clear_main_mod_cache(self): | |
690 | """Clear the cache of main modules. |
|
691 | """Clear the cache of main modules. | |
691 |
|
692 | |||
692 | Mainly for use by utilities like %reset. |
|
693 | Mainly for use by utilities like %reset. | |
693 |
|
694 | |||
694 | Examples |
|
695 | Examples | |
695 | -------- |
|
696 | -------- | |
696 |
|
697 | |||
697 | In [15]: import IPython |
|
698 | In [15]: import IPython | |
698 |
|
699 | |||
699 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
700 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
700 |
|
701 | |||
701 | In [17]: len(_ip._main_ns_cache) > 0 |
|
702 | In [17]: len(_ip._main_ns_cache) > 0 | |
702 | Out[17]: True |
|
703 | Out[17]: True | |
703 |
|
704 | |||
704 | In [18]: _ip.clear_main_mod_cache() |
|
705 | In [18]: _ip.clear_main_mod_cache() | |
705 |
|
706 | |||
706 | In [19]: len(_ip._main_ns_cache) == 0 |
|
707 | In [19]: len(_ip._main_ns_cache) == 0 | |
707 | Out[19]: True |
|
708 | Out[19]: True | |
708 | """ |
|
709 | """ | |
709 | self._main_ns_cache.clear() |
|
710 | self._main_ns_cache.clear() | |
710 |
|
711 | |||
711 | #------------------------------------------------------------------------- |
|
712 | #------------------------------------------------------------------------- | |
712 | # Things related to debugging |
|
713 | # Things related to debugging | |
713 | #------------------------------------------------------------------------- |
|
714 | #------------------------------------------------------------------------- | |
714 |
|
715 | |||
715 | def init_pdb(self): |
|
716 | def init_pdb(self): | |
716 | # Set calling of pdb on exceptions |
|
717 | # Set calling of pdb on exceptions | |
717 | # self.call_pdb is a property |
|
718 | # self.call_pdb is a property | |
718 | self.call_pdb = self.pdb |
|
719 | self.call_pdb = self.pdb | |
719 |
|
720 | |||
720 | def _get_call_pdb(self): |
|
721 | def _get_call_pdb(self): | |
721 | return self._call_pdb |
|
722 | return self._call_pdb | |
722 |
|
723 | |||
723 | def _set_call_pdb(self,val): |
|
724 | def _set_call_pdb(self,val): | |
724 |
|
725 | |||
725 | if val not in (0,1,False,True): |
|
726 | if val not in (0,1,False,True): | |
726 | raise ValueError,'new call_pdb value must be boolean' |
|
727 | raise ValueError,'new call_pdb value must be boolean' | |
727 |
|
728 | |||
728 | # store value in instance |
|
729 | # store value in instance | |
729 | self._call_pdb = val |
|
730 | self._call_pdb = val | |
730 |
|
731 | |||
731 | # notify the actual exception handlers |
|
732 | # notify the actual exception handlers | |
732 | self.InteractiveTB.call_pdb = val |
|
733 | self.InteractiveTB.call_pdb = val | |
733 | if self.isthreaded: |
|
734 | if self.isthreaded: | |
734 | try: |
|
735 | try: | |
735 | self.sys_excepthook.call_pdb = val |
|
736 | self.sys_excepthook.call_pdb = val | |
736 | except: |
|
737 | except: | |
737 | warn('Failed to activate pdb for threaded exception handler') |
|
738 | warn('Failed to activate pdb for threaded exception handler') | |
738 |
|
739 | |||
739 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
740 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
740 | 'Control auto-activation of pdb at exceptions') |
|
741 | 'Control auto-activation of pdb at exceptions') | |
741 |
|
742 | |||
742 | def debugger(self,force=False): |
|
743 | def debugger(self,force=False): | |
743 | """Call the pydb/pdb debugger. |
|
744 | """Call the pydb/pdb debugger. | |
744 |
|
745 | |||
745 | Keywords: |
|
746 | Keywords: | |
746 |
|
747 | |||
747 | - force(False): by default, this routine checks the instance call_pdb |
|
748 | - force(False): by default, this routine checks the instance call_pdb | |
748 | flag and does not actually invoke the debugger if the flag is false. |
|
749 | flag and does not actually invoke the debugger if the flag is false. | |
749 | The 'force' option forces the debugger to activate even if the flag |
|
750 | The 'force' option forces the debugger to activate even if the flag | |
750 | is false. |
|
751 | is false. | |
751 | """ |
|
752 | """ | |
752 |
|
753 | |||
753 | if not (force or self.call_pdb): |
|
754 | if not (force or self.call_pdb): | |
754 | return |
|
755 | return | |
755 |
|
756 | |||
756 | if not hasattr(sys,'last_traceback'): |
|
757 | if not hasattr(sys,'last_traceback'): | |
757 | error('No traceback has been produced, nothing to debug.') |
|
758 | error('No traceback has been produced, nothing to debug.') | |
758 | return |
|
759 | return | |
759 |
|
760 | |||
760 | # use pydb if available |
|
761 | # use pydb if available | |
761 | if debugger.has_pydb: |
|
762 | if debugger.has_pydb: | |
762 | from pydb import pm |
|
763 | from pydb import pm | |
763 | else: |
|
764 | else: | |
764 | # fallback to our internal debugger |
|
765 | # fallback to our internal debugger | |
765 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
766 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
766 | self.history_saving_wrapper(pm)() |
|
767 | self.history_saving_wrapper(pm)() | |
767 |
|
768 | |||
768 | #------------------------------------------------------------------------- |
|
769 | #------------------------------------------------------------------------- | |
769 | # Things related to IPython's various namespaces |
|
770 | # Things related to IPython's various namespaces | |
770 | #------------------------------------------------------------------------- |
|
771 | #------------------------------------------------------------------------- | |
771 |
|
772 | |||
772 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
773 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): | |
773 | # Create the namespace where the user will operate. user_ns is |
|
774 | # Create the namespace where the user will operate. user_ns is | |
774 | # normally the only one used, and it is passed to the exec calls as |
|
775 | # normally the only one used, and it is passed to the exec calls as | |
775 | # the locals argument. But we do carry a user_global_ns namespace |
|
776 | # the locals argument. But we do carry a user_global_ns namespace | |
776 | # given as the exec 'globals' argument, This is useful in embedding |
|
777 | # given as the exec 'globals' argument, This is useful in embedding | |
777 | # situations where the ipython shell opens in a context where the |
|
778 | # situations where the ipython shell opens in a context where the | |
778 | # distinction between locals and globals is meaningful. For |
|
779 | # distinction between locals and globals is meaningful. For | |
779 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
780 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
780 |
|
781 | |||
781 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
782 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
782 | # level as a dict instead of a module. This is a manual fix, but I |
|
783 | # level as a dict instead of a module. This is a manual fix, but I | |
783 | # should really track down where the problem is coming from. Alex |
|
784 | # should really track down where the problem is coming from. Alex | |
784 | # Schmolck reported this problem first. |
|
785 | # Schmolck reported this problem first. | |
785 |
|
786 | |||
786 | # A useful post by Alex Martelli on this topic: |
|
787 | # A useful post by Alex Martelli on this topic: | |
787 | # Re: inconsistent value from __builtins__ |
|
788 | # Re: inconsistent value from __builtins__ | |
788 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
789 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
789 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
790 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
790 | # Gruppen: comp.lang.python |
|
791 | # Gruppen: comp.lang.python | |
791 |
|
792 | |||
792 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
793 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
793 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
794 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
794 | # > <type 'dict'> |
|
795 | # > <type 'dict'> | |
795 | # > >>> print type(__builtins__) |
|
796 | # > >>> print type(__builtins__) | |
796 | # > <type 'module'> |
|
797 | # > <type 'module'> | |
797 | # > Is this difference in return value intentional? |
|
798 | # > Is this difference in return value intentional? | |
798 |
|
799 | |||
799 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
800 | # Well, it's documented that '__builtins__' can be either a dictionary | |
800 | # or a module, and it's been that way for a long time. Whether it's |
|
801 | # or a module, and it's been that way for a long time. Whether it's | |
801 | # intentional (or sensible), I don't know. In any case, the idea is |
|
802 | # intentional (or sensible), I don't know. In any case, the idea is | |
802 | # that if you need to access the built-in namespace directly, you |
|
803 | # that if you need to access the built-in namespace directly, you | |
803 | # should start with "import __builtin__" (note, no 's') which will |
|
804 | # should start with "import __builtin__" (note, no 's') which will | |
804 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
805 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
805 |
|
806 | |||
806 | # These routines return properly built dicts as needed by the rest of |
|
807 | # These routines return properly built dicts as needed by the rest of | |
807 | # the code, and can also be used by extension writers to generate |
|
808 | # the code, and can also be used by extension writers to generate | |
808 | # properly initialized namespaces. |
|
809 | # properly initialized namespaces. | |
809 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, |
|
810 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, | |
810 | user_global_ns) |
|
811 | user_global_ns) | |
811 |
|
812 | |||
812 | # Assign namespaces |
|
813 | # Assign namespaces | |
813 | # This is the namespace where all normal user variables live |
|
814 | # This is the namespace where all normal user variables live | |
814 | self.user_ns = user_ns |
|
815 | self.user_ns = user_ns | |
815 | self.user_global_ns = user_global_ns |
|
816 | self.user_global_ns = user_global_ns | |
816 |
|
817 | |||
817 | # An auxiliary namespace that checks what parts of the user_ns were |
|
818 | # An auxiliary namespace that checks what parts of the user_ns were | |
818 | # loaded at startup, so we can list later only variables defined in |
|
819 | # loaded at startup, so we can list later only variables defined in | |
819 | # actual interactive use. Since it is always a subset of user_ns, it |
|
820 | # actual interactive use. Since it is always a subset of user_ns, it | |
820 | # doesn't need to be seaparately tracked in the ns_table |
|
821 | # doesn't need to be seaparately tracked in the ns_table | |
821 | self.user_config_ns = {} |
|
822 | self.user_config_ns = {} | |
822 |
|
823 | |||
823 | # A namespace to keep track of internal data structures to prevent |
|
824 | # A namespace to keep track of internal data structures to prevent | |
824 | # them from cluttering user-visible stuff. Will be updated later |
|
825 | # them from cluttering user-visible stuff. Will be updated later | |
825 | self.internal_ns = {} |
|
826 | self.internal_ns = {} | |
826 |
|
827 | |||
827 | # Namespace of system aliases. Each entry in the alias |
|
828 | # Namespace of system aliases. Each entry in the alias | |
828 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
829 | # table must be a 2-tuple of the form (N,name), where N is the number | |
829 | # of positional arguments of the alias. |
|
830 | # of positional arguments of the alias. | |
830 | self.alias_table = {} |
|
831 | self.alias_table = {} | |
831 |
|
832 | |||
832 | # Now that FakeModule produces a real module, we've run into a nasty |
|
833 | # Now that FakeModule produces a real module, we've run into a nasty | |
833 | # problem: after script execution (via %run), the module where the user |
|
834 | # problem: after script execution (via %run), the module where the user | |
834 | # code ran is deleted. Now that this object is a true module (needed |
|
835 | # code ran is deleted. Now that this object is a true module (needed | |
835 | # so docetst and other tools work correctly), the Python module |
|
836 | # so docetst and other tools work correctly), the Python module | |
836 | # teardown mechanism runs over it, and sets to None every variable |
|
837 | # teardown mechanism runs over it, and sets to None every variable | |
837 | # present in that module. Top-level references to objects from the |
|
838 | # present in that module. Top-level references to objects from the | |
838 | # script survive, because the user_ns is updated with them. However, |
|
839 | # script survive, because the user_ns is updated with them. However, | |
839 | # calling functions defined in the script that use other things from |
|
840 | # calling functions defined in the script that use other things from | |
840 | # the script will fail, because the function's closure had references |
|
841 | # the script will fail, because the function's closure had references | |
841 | # to the original objects, which are now all None. So we must protect |
|
842 | # to the original objects, which are now all None. So we must protect | |
842 | # these modules from deletion by keeping a cache. |
|
843 | # these modules from deletion by keeping a cache. | |
843 | # |
|
844 | # | |
844 | # To avoid keeping stale modules around (we only need the one from the |
|
845 | # To avoid keeping stale modules around (we only need the one from the | |
845 | # last run), we use a dict keyed with the full path to the script, so |
|
846 | # last run), we use a dict keyed with the full path to the script, so | |
846 | # only the last version of the module is held in the cache. Note, |
|
847 | # only the last version of the module is held in the cache. Note, | |
847 | # however, that we must cache the module *namespace contents* (their |
|
848 | # however, that we must cache the module *namespace contents* (their | |
848 | # __dict__). Because if we try to cache the actual modules, old ones |
|
849 | # __dict__). Because if we try to cache the actual modules, old ones | |
849 | # (uncached) could be destroyed while still holding references (such as |
|
850 | # (uncached) could be destroyed while still holding references (such as | |
850 | # those held by GUI objects that tend to be long-lived)> |
|
851 | # those held by GUI objects that tend to be long-lived)> | |
851 | # |
|
852 | # | |
852 | # The %reset command will flush this cache. See the cache_main_mod() |
|
853 | # The %reset command will flush this cache. See the cache_main_mod() | |
853 | # and clear_main_mod_cache() methods for details on use. |
|
854 | # and clear_main_mod_cache() methods for details on use. | |
854 |
|
855 | |||
855 | # This is the cache used for 'main' namespaces |
|
856 | # This is the cache used for 'main' namespaces | |
856 | self._main_ns_cache = {} |
|
857 | self._main_ns_cache = {} | |
857 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
858 | # And this is the single instance of FakeModule whose __dict__ we keep | |
858 | # copying and clearing for reuse on each %run |
|
859 | # copying and clearing for reuse on each %run | |
859 | self._user_main_module = FakeModule() |
|
860 | self._user_main_module = FakeModule() | |
860 |
|
861 | |||
861 | # A table holding all the namespaces IPython deals with, so that |
|
862 | # A table holding all the namespaces IPython deals with, so that | |
862 | # introspection facilities can search easily. |
|
863 | # introspection facilities can search easily. | |
863 | self.ns_table = {'user':user_ns, |
|
864 | self.ns_table = {'user':user_ns, | |
864 | 'user_global':user_global_ns, |
|
865 | 'user_global':user_global_ns, | |
865 | 'alias':self.alias_table, |
|
866 | 'alias':self.alias_table, | |
866 | 'internal':self.internal_ns, |
|
867 | 'internal':self.internal_ns, | |
867 | 'builtin':__builtin__.__dict__ |
|
868 | 'builtin':__builtin__.__dict__ | |
868 | } |
|
869 | } | |
869 |
|
870 | |||
870 | # Similarly, track all namespaces where references can be held and that |
|
871 | # Similarly, track all namespaces where references can be held and that | |
871 | # we can safely clear (so it can NOT include builtin). This one can be |
|
872 | # we can safely clear (so it can NOT include builtin). This one can be | |
872 | # a simple list. |
|
873 | # a simple list. | |
873 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns, |
|
874 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns, | |
874 | self.alias_table, self.internal_ns, |
|
875 | self.alias_table, self.internal_ns, | |
875 | self._main_ns_cache ] |
|
876 | self._main_ns_cache ] | |
876 |
|
877 | |||
877 | def init_sys_modules(self): |
|
878 | def init_sys_modules(self): | |
878 | # We need to insert into sys.modules something that looks like a |
|
879 | # We need to insert into sys.modules something that looks like a | |
879 | # module but which accesses the IPython namespace, for shelve and |
|
880 | # module but which accesses the IPython namespace, for shelve and | |
880 | # pickle to work interactively. Normally they rely on getting |
|
881 | # pickle to work interactively. Normally they rely on getting | |
881 | # everything out of __main__, but for embedding purposes each IPython |
|
882 | # everything out of __main__, but for embedding purposes each IPython | |
882 | # instance has its own private namespace, so we can't go shoving |
|
883 | # instance has its own private namespace, so we can't go shoving | |
883 | # everything into __main__. |
|
884 | # everything into __main__. | |
884 |
|
885 | |||
885 | # note, however, that we should only do this for non-embedded |
|
886 | # note, however, that we should only do this for non-embedded | |
886 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
887 | # ipythons, which really mimic the __main__.__dict__ with their own | |
887 | # namespace. Embedded instances, on the other hand, should not do |
|
888 | # namespace. Embedded instances, on the other hand, should not do | |
888 | # this because they need to manage the user local/global namespaces |
|
889 | # this because they need to manage the user local/global namespaces | |
889 | # only, but they live within a 'normal' __main__ (meaning, they |
|
890 | # only, but they live within a 'normal' __main__ (meaning, they | |
890 | # shouldn't overtake the execution environment of the script they're |
|
891 | # shouldn't overtake the execution environment of the script they're | |
891 | # embedded in). |
|
892 | # embedded in). | |
892 |
|
893 | |||
893 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
894 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
894 |
|
895 | |||
895 | try: |
|
896 | try: | |
896 | main_name = self.user_ns['__name__'] |
|
897 | main_name = self.user_ns['__name__'] | |
897 | except KeyError: |
|
898 | except KeyError: | |
898 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
899 | raise KeyError('user_ns dictionary MUST have a "__name__" key') | |
899 | else: |
|
900 | else: | |
900 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
901 | sys.modules[main_name] = FakeModule(self.user_ns) | |
901 |
|
902 | |||
902 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
903 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): | |
903 | """Return a valid local and global user interactive namespaces. |
|
904 | """Return a valid local and global user interactive namespaces. | |
904 |
|
905 | |||
905 | This builds a dict with the minimal information needed to operate as a |
|
906 | This builds a dict with the minimal information needed to operate as a | |
906 | valid IPython user namespace, which you can pass to the various |
|
907 | valid IPython user namespace, which you can pass to the various | |
907 | embedding classes in ipython. The default implementation returns the |
|
908 | embedding classes in ipython. The default implementation returns the | |
908 | same dict for both the locals and the globals to allow functions to |
|
909 | same dict for both the locals and the globals to allow functions to | |
909 | refer to variables in the namespace. Customized implementations can |
|
910 | refer to variables in the namespace. Customized implementations can | |
910 | return different dicts. The locals dictionary can actually be anything |
|
911 | return different dicts. The locals dictionary can actually be anything | |
911 | following the basic mapping protocol of a dict, but the globals dict |
|
912 | following the basic mapping protocol of a dict, but the globals dict | |
912 | must be a true dict, not even a subclass. It is recommended that any |
|
913 | must be a true dict, not even a subclass. It is recommended that any | |
913 | custom object for the locals namespace synchronize with the globals |
|
914 | custom object for the locals namespace synchronize with the globals | |
914 | dict somehow. |
|
915 | dict somehow. | |
915 |
|
916 | |||
916 | Raises TypeError if the provided globals namespace is not a true dict. |
|
917 | Raises TypeError if the provided globals namespace is not a true dict. | |
917 |
|
918 | |||
918 | :Parameters: |
|
919 | :Parameters: | |
919 | user_ns : dict-like, optional |
|
920 | user_ns : dict-like, optional | |
920 | The current user namespace. The items in this namespace should |
|
921 | The current user namespace. The items in this namespace should | |
921 | be included in the output. If None, an appropriate blank |
|
922 | be included in the output. If None, an appropriate blank | |
922 | namespace should be created. |
|
923 | namespace should be created. | |
923 | user_global_ns : dict, optional |
|
924 | user_global_ns : dict, optional | |
924 | The current user global namespace. The items in this namespace |
|
925 | The current user global namespace. The items in this namespace | |
925 | should be included in the output. If None, an appropriate |
|
926 | should be included in the output. If None, an appropriate | |
926 | blank namespace should be created. |
|
927 | blank namespace should be created. | |
927 |
|
928 | |||
928 | :Returns: |
|
929 | :Returns: | |
929 | A tuple pair of dictionary-like object to be used as the local namespace |
|
930 | A tuple pair of dictionary-like object to be used as the local namespace | |
930 | of the interpreter and a dict to be used as the global namespace. |
|
931 | of the interpreter and a dict to be used as the global namespace. | |
931 | """ |
|
932 | """ | |
932 |
|
933 | |||
933 | if user_ns is None: |
|
934 | if user_ns is None: | |
934 | # Set __name__ to __main__ to better match the behavior of the |
|
935 | # Set __name__ to __main__ to better match the behavior of the | |
935 | # normal interpreter. |
|
936 | # normal interpreter. | |
936 | user_ns = {'__name__' :'__main__', |
|
937 | user_ns = {'__name__' :'__main__', | |
937 | '__builtins__' : __builtin__, |
|
938 | '__builtins__' : __builtin__, | |
938 | } |
|
939 | } | |
939 | else: |
|
940 | else: | |
940 | user_ns.setdefault('__name__','__main__') |
|
941 | user_ns.setdefault('__name__','__main__') | |
941 | user_ns.setdefault('__builtins__',__builtin__) |
|
942 | user_ns.setdefault('__builtins__',__builtin__) | |
942 |
|
943 | |||
943 | if user_global_ns is None: |
|
944 | if user_global_ns is None: | |
944 | user_global_ns = user_ns |
|
945 | user_global_ns = user_ns | |
945 | if type(user_global_ns) is not dict: |
|
946 | if type(user_global_ns) is not dict: | |
946 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
947 | raise TypeError("user_global_ns must be a true dict; got %r" | |
947 | % type(user_global_ns)) |
|
948 | % type(user_global_ns)) | |
948 |
|
949 | |||
949 | return user_ns, user_global_ns |
|
950 | return user_ns, user_global_ns | |
950 |
|
951 | |||
951 | def init_user_ns(self): |
|
952 | def init_user_ns(self): | |
952 | """Initialize all user-visible namespaces to their minimum defaults. |
|
953 | """Initialize all user-visible namespaces to their minimum defaults. | |
953 |
|
954 | |||
954 | Certain history lists are also initialized here, as they effectively |
|
955 | Certain history lists are also initialized here, as they effectively | |
955 | act as user namespaces. |
|
956 | act as user namespaces. | |
956 |
|
957 | |||
957 | Notes |
|
958 | Notes | |
958 | ----- |
|
959 | ----- | |
959 | All data structures here are only filled in, they are NOT reset by this |
|
960 | All data structures here are only filled in, they are NOT reset by this | |
960 | method. If they were not empty before, data will simply be added to |
|
961 | method. If they were not empty before, data will simply be added to | |
961 | therm. |
|
962 | therm. | |
962 | """ |
|
963 | """ | |
963 | # The user namespace MUST have a pointer to the shell itself. |
|
964 | # The user namespace MUST have a pointer to the shell itself. | |
964 | self.user_ns[self.name] = self |
|
965 | self.user_ns[self.name] = self | |
965 |
|
966 | |||
966 | # Store myself as the public api!!! |
|
967 | # Store myself as the public api!!! | |
967 | self.user_ns['_ip'] = self |
|
968 | self.user_ns['_ip'] = self | |
968 |
|
969 | |||
969 | # make global variables for user access to the histories |
|
970 | # make global variables for user access to the histories | |
970 | self.user_ns['_ih'] = self.input_hist |
|
971 | self.user_ns['_ih'] = self.input_hist | |
971 | self.user_ns['_oh'] = self.output_hist |
|
972 | self.user_ns['_oh'] = self.output_hist | |
972 | self.user_ns['_dh'] = self.dir_hist |
|
973 | self.user_ns['_dh'] = self.dir_hist | |
973 |
|
974 | |||
974 | # user aliases to input and output histories |
|
975 | # user aliases to input and output histories | |
975 | self.user_ns['In'] = self.input_hist |
|
976 | self.user_ns['In'] = self.input_hist | |
976 | self.user_ns['Out'] = self.output_hist |
|
977 | self.user_ns['Out'] = self.output_hist | |
977 |
|
978 | |||
978 | self.user_ns['_sh'] = shadowns |
|
979 | self.user_ns['_sh'] = shadowns | |
979 |
|
980 | |||
980 | # Put 'help' in the user namespace |
|
981 | # Put 'help' in the user namespace | |
981 | try: |
|
982 | try: | |
982 | from site import _Helper |
|
983 | from site import _Helper | |
983 | self.user_ns['help'] = _Helper() |
|
984 | self.user_ns['help'] = _Helper() | |
984 | except ImportError: |
|
985 | except ImportError: | |
985 | warn('help() not available - check site.py') |
|
986 | warn('help() not available - check site.py') | |
986 |
|
987 | |||
987 | def reset(self): |
|
988 | def reset(self): | |
988 | """Clear all internal namespaces. |
|
989 | """Clear all internal namespaces. | |
989 |
|
990 | |||
990 | Note that this is much more aggressive than %reset, since it clears |
|
991 | Note that this is much more aggressive than %reset, since it clears | |
991 | fully all namespaces, as well as all input/output lists. |
|
992 | fully all namespaces, as well as all input/output lists. | |
992 | """ |
|
993 | """ | |
993 | for ns in self.ns_refs_table: |
|
994 | for ns in self.ns_refs_table: | |
994 | ns.clear() |
|
995 | ns.clear() | |
995 |
|
996 | |||
996 | # Clear input and output histories |
|
997 | # Clear input and output histories | |
997 | self.input_hist[:] = [] |
|
998 | self.input_hist[:] = [] | |
998 | self.input_hist_raw[:] = [] |
|
999 | self.input_hist_raw[:] = [] | |
999 | self.output_hist.clear() |
|
1000 | self.output_hist.clear() | |
1000 | # Restore the user namespaces to minimal usability |
|
1001 | # Restore the user namespaces to minimal usability | |
1001 | self.init_user_ns() |
|
1002 | self.init_user_ns() | |
1002 |
|
1003 | |||
1003 | def push(self, variables, interactive=True): |
|
1004 | def push(self, variables, interactive=True): | |
1004 | """Inject a group of variables into the IPython user namespace. |
|
1005 | """Inject a group of variables into the IPython user namespace. | |
1005 |
|
1006 | |||
1006 | Parameters |
|
1007 | Parameters | |
1007 | ---------- |
|
1008 | ---------- | |
1008 | variables : dict, str or list/tuple of str |
|
1009 | variables : dict, str or list/tuple of str | |
1009 | The variables to inject into the user's namespace. If a dict, |
|
1010 | The variables to inject into the user's namespace. If a dict, | |
1010 | a simple update is done. If a str, the string is assumed to |
|
1011 | a simple update is done. If a str, the string is assumed to | |
1011 | have variable names separated by spaces. A list/tuple of str |
|
1012 | have variable names separated by spaces. A list/tuple of str | |
1012 | can also be used to give the variable names. If just the variable |
|
1013 | can also be used to give the variable names. If just the variable | |
1013 | names are give (list/tuple/str) then the variable values looked |
|
1014 | names are give (list/tuple/str) then the variable values looked | |
1014 | up in the callers frame. |
|
1015 | up in the callers frame. | |
1015 | interactive : bool |
|
1016 | interactive : bool | |
1016 | If True (default), the variables will be listed with the ``who`` |
|
1017 | If True (default), the variables will be listed with the ``who`` | |
1017 | magic. |
|
1018 | magic. | |
1018 | """ |
|
1019 | """ | |
1019 | vdict = None |
|
1020 | vdict = None | |
1020 |
|
1021 | |||
1021 | # We need a dict of name/value pairs to do namespace updates. |
|
1022 | # We need a dict of name/value pairs to do namespace updates. | |
1022 | if isinstance(variables, dict): |
|
1023 | if isinstance(variables, dict): | |
1023 | vdict = variables |
|
1024 | vdict = variables | |
1024 | elif isinstance(variables, (basestring, list, tuple)): |
|
1025 | elif isinstance(variables, (basestring, list, tuple)): | |
1025 | if isinstance(variables, basestring): |
|
1026 | if isinstance(variables, basestring): | |
1026 | vlist = variables.split() |
|
1027 | vlist = variables.split() | |
1027 | else: |
|
1028 | else: | |
1028 | vlist = variables |
|
1029 | vlist = variables | |
1029 | vdict = {} |
|
1030 | vdict = {} | |
1030 | cf = sys._getframe(1) |
|
1031 | cf = sys._getframe(1) | |
1031 | for name in vlist: |
|
1032 | for name in vlist: | |
1032 | try: |
|
1033 | try: | |
1033 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1034 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1034 | except: |
|
1035 | except: | |
1035 | print ('Could not get variable %s from %s' % |
|
1036 | print ('Could not get variable %s from %s' % | |
1036 | (name,cf.f_code.co_name)) |
|
1037 | (name,cf.f_code.co_name)) | |
1037 | else: |
|
1038 | else: | |
1038 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1039 | raise ValueError('variables must be a dict/str/list/tuple') | |
1039 |
|
1040 | |||
1040 | # Propagate variables to user namespace |
|
1041 | # Propagate variables to user namespace | |
1041 | self.user_ns.update(vdict) |
|
1042 | self.user_ns.update(vdict) | |
1042 |
|
1043 | |||
1043 | # And configure interactive visibility |
|
1044 | # And configure interactive visibility | |
1044 | config_ns = self.user_config_ns |
|
1045 | config_ns = self.user_config_ns | |
1045 | if interactive: |
|
1046 | if interactive: | |
1046 | for name, val in vdict.iteritems(): |
|
1047 | for name, val in vdict.iteritems(): | |
1047 | config_ns.pop(name, None) |
|
1048 | config_ns.pop(name, None) | |
1048 | else: |
|
1049 | else: | |
1049 | for name,val in vdict.iteritems(): |
|
1050 | for name,val in vdict.iteritems(): | |
1050 | config_ns[name] = val |
|
1051 | config_ns[name] = val | |
1051 |
|
1052 | |||
1052 | #------------------------------------------------------------------------- |
|
1053 | #------------------------------------------------------------------------- | |
1053 | # Things related to history management |
|
1054 | # Things related to history management | |
1054 | #------------------------------------------------------------------------- |
|
1055 | #------------------------------------------------------------------------- | |
1055 |
|
1056 | |||
1056 | def init_history(self): |
|
1057 | def init_history(self): | |
1057 | # List of input with multi-line handling. |
|
1058 | # List of input with multi-line handling. | |
1058 | self.input_hist = InputList() |
|
1059 | self.input_hist = InputList() | |
1059 | # This one will hold the 'raw' input history, without any |
|
1060 | # This one will hold the 'raw' input history, without any | |
1060 | # pre-processing. This will allow users to retrieve the input just as |
|
1061 | # pre-processing. This will allow users to retrieve the input just as | |
1061 | # it was exactly typed in by the user, with %hist -r. |
|
1062 | # it was exactly typed in by the user, with %hist -r. | |
1062 | self.input_hist_raw = InputList() |
|
1063 | self.input_hist_raw = InputList() | |
1063 |
|
1064 | |||
1064 | # list of visited directories |
|
1065 | # list of visited directories | |
1065 | try: |
|
1066 | try: | |
1066 | self.dir_hist = [os.getcwd()] |
|
1067 | self.dir_hist = [os.getcwd()] | |
1067 | except OSError: |
|
1068 | except OSError: | |
1068 | self.dir_hist = [] |
|
1069 | self.dir_hist = [] | |
1069 |
|
1070 | |||
1070 | # dict of output history |
|
1071 | # dict of output history | |
1071 | self.output_hist = {} |
|
1072 | self.output_hist = {} | |
1072 |
|
1073 | |||
1073 | # Now the history file |
|
1074 | # Now the history file | |
1074 | try: |
|
1075 | try: | |
1075 | histfname = 'history-%s' % self.profile |
|
1076 | histfname = 'history-%s' % self.profile | |
1076 | except AttributeError: |
|
1077 | except AttributeError: | |
1077 | histfname = 'history' |
|
1078 | histfname = 'history' | |
1078 | self.histfile = os.path.join(self.config.IPYTHONDIR, histfname) |
|
1079 | self.histfile = os.path.join(self.config.IPYTHONDIR, histfname) | |
1079 |
|
1080 | |||
1080 | # Fill the history zero entry, user counter starts at 1 |
|
1081 | # Fill the history zero entry, user counter starts at 1 | |
1081 | self.input_hist.append('\n') |
|
1082 | self.input_hist.append('\n') | |
1082 | self.input_hist_raw.append('\n') |
|
1083 | self.input_hist_raw.append('\n') | |
1083 |
|
1084 | |||
1084 | def init_shadow_hist(self): |
|
1085 | def init_shadow_hist(self): | |
1085 | try: |
|
1086 | try: | |
1086 | self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db") |
|
1087 | self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db") | |
1087 | except exceptions.UnicodeDecodeError: |
|
1088 | except exceptions.UnicodeDecodeError: | |
1088 | print "Your ipythondir can't be decoded to unicode!" |
|
1089 | print "Your ipythondir can't be decoded to unicode!" | |
1089 | print "Please set HOME environment variable to something that" |
|
1090 | print "Please set HOME environment variable to something that" | |
1090 | print r"only has ASCII characters, e.g. c:\home" |
|
1091 | print r"only has ASCII characters, e.g. c:\home" | |
1091 | print "Now it is", self.config.IPYTHONDIR |
|
1092 | print "Now it is", self.config.IPYTHONDIR | |
1092 | sys.exit() |
|
1093 | sys.exit() | |
1093 | self.shadowhist = ipcorehist.ShadowHist(self.db) |
|
1094 | self.shadowhist = ipcorehist.ShadowHist(self.db) | |
1094 |
|
1095 | |||
1095 | def savehist(self): |
|
1096 | def savehist(self): | |
1096 | """Save input history to a file (via readline library).""" |
|
1097 | """Save input history to a file (via readline library).""" | |
1097 |
|
1098 | |||
1098 | if not self.has_readline: |
|
1099 | if not self.has_readline: | |
1099 | return |
|
1100 | return | |
1100 |
|
1101 | |||
1101 | try: |
|
1102 | try: | |
1102 | self.readline.write_history_file(self.histfile) |
|
1103 | self.readline.write_history_file(self.histfile) | |
1103 | except: |
|
1104 | except: | |
1104 | print 'Unable to save IPython command history to file: ' + \ |
|
1105 | print 'Unable to save IPython command history to file: ' + \ | |
1105 | `self.histfile` |
|
1106 | `self.histfile` | |
1106 |
|
1107 | |||
1107 | def reloadhist(self): |
|
1108 | def reloadhist(self): | |
1108 | """Reload the input history from disk file.""" |
|
1109 | """Reload the input history from disk file.""" | |
1109 |
|
1110 | |||
1110 | if self.has_readline: |
|
1111 | if self.has_readline: | |
1111 | try: |
|
1112 | try: | |
1112 | self.readline.clear_history() |
|
1113 | self.readline.clear_history() | |
1113 | self.readline.read_history_file(self.shell.histfile) |
|
1114 | self.readline.read_history_file(self.shell.histfile) | |
1114 | except AttributeError: |
|
1115 | except AttributeError: | |
1115 | pass |
|
1116 | pass | |
1116 |
|
1117 | |||
1117 | def history_saving_wrapper(self, func): |
|
1118 | def history_saving_wrapper(self, func): | |
1118 | """ Wrap func for readline history saving |
|
1119 | """ Wrap func for readline history saving | |
1119 |
|
1120 | |||
1120 | Convert func into callable that saves & restores |
|
1121 | Convert func into callable that saves & restores | |
1121 | history around the call """ |
|
1122 | history around the call """ | |
1122 |
|
1123 | |||
1123 | if not self.has_readline: |
|
1124 | if not self.has_readline: | |
1124 | return func |
|
1125 | return func | |
1125 |
|
1126 | |||
1126 | def wrapper(): |
|
1127 | def wrapper(): | |
1127 | self.savehist() |
|
1128 | self.savehist() | |
1128 | try: |
|
1129 | try: | |
1129 | func() |
|
1130 | func() | |
1130 | finally: |
|
1131 | finally: | |
1131 | readline.read_history_file(self.histfile) |
|
1132 | readline.read_history_file(self.histfile) | |
1132 | return wrapper |
|
1133 | return wrapper | |
1133 |
|
1134 | |||
1134 | #------------------------------------------------------------------------- |
|
1135 | #------------------------------------------------------------------------- | |
1135 | # Things related to exception handling and tracebacks (not debugging) |
|
1136 | # Things related to exception handling and tracebacks (not debugging) | |
1136 | #------------------------------------------------------------------------- |
|
1137 | #------------------------------------------------------------------------- | |
1137 |
|
1138 | |||
1138 | def init_traceback_handlers(self, custom_exceptions): |
|
1139 | def init_traceback_handlers(self, custom_exceptions): | |
1139 | # Syntax error handler. |
|
1140 | # Syntax error handler. | |
1140 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
1141 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') | |
1141 |
|
1142 | |||
1142 | # The interactive one is initialized with an offset, meaning we always |
|
1143 | # The interactive one is initialized with an offset, meaning we always | |
1143 | # want to remove the topmost item in the traceback, which is our own |
|
1144 | # want to remove the topmost item in the traceback, which is our own | |
1144 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1145 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1145 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1146 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1146 | color_scheme='NoColor', |
|
1147 | color_scheme='NoColor', | |
1147 | tb_offset = 1) |
|
1148 | tb_offset = 1) | |
1148 |
|
1149 | |||
1149 | # IPython itself shouldn't crash. This will produce a detailed |
|
1150 | # IPython itself shouldn't crash. This will produce a detailed | |
1150 | # post-mortem if it does. But we only install the crash handler for |
|
1151 | # post-mortem if it does. But we only install the crash handler for | |
1151 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
1152 | # non-threaded shells, the threaded ones use a normal verbose reporter | |
1152 | # and lose the crash handler. This is because exceptions in the main |
|
1153 | # and lose the crash handler. This is because exceptions in the main | |
1153 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
1154 | # thread (such as in GUI code) propagate directly to sys.excepthook, | |
1154 | # and there's no point in printing crash dumps for every user exception. |
|
1155 | # and there's no point in printing crash dumps for every user exception. | |
1155 | if self.isthreaded: |
|
1156 | if self.isthreaded: | |
1156 | ipCrashHandler = ultratb.FormattedTB() |
|
1157 | ipCrashHandler = ultratb.FormattedTB() | |
1157 | else: |
|
1158 | else: | |
1158 | from IPython.core import crashhandler |
|
1159 | from IPython.core import crashhandler | |
1159 | ipCrashHandler = crashhandler.IPythonCrashHandler(self) |
|
1160 | ipCrashHandler = crashhandler.IPythonCrashHandler(self) | |
1160 | self.set_crash_handler(ipCrashHandler) |
|
1161 | self.set_crash_handler(ipCrashHandler) | |
1161 |
|
1162 | |||
1162 | # and add any custom exception handlers the user may have specified |
|
1163 | # and add any custom exception handlers the user may have specified | |
1163 | self.set_custom_exc(*custom_exceptions) |
|
1164 | self.set_custom_exc(*custom_exceptions) | |
1164 |
|
1165 | |||
1165 | def set_crash_handler(self, crashHandler): |
|
1166 | def set_crash_handler(self, crashHandler): | |
1166 | """Set the IPython crash handler. |
|
1167 | """Set the IPython crash handler. | |
1167 |
|
1168 | |||
1168 | This must be a callable with a signature suitable for use as |
|
1169 | This must be a callable with a signature suitable for use as | |
1169 | sys.excepthook.""" |
|
1170 | sys.excepthook.""" | |
1170 |
|
1171 | |||
1171 | # Install the given crash handler as the Python exception hook |
|
1172 | # Install the given crash handler as the Python exception hook | |
1172 | sys.excepthook = crashHandler |
|
1173 | sys.excepthook = crashHandler | |
1173 |
|
1174 | |||
1174 | # The instance will store a pointer to this, so that runtime code |
|
1175 | # The instance will store a pointer to this, so that runtime code | |
1175 | # (such as magics) can access it. This is because during the |
|
1176 | # (such as magics) can access it. This is because during the | |
1176 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
1177 | # read-eval loop, it gets temporarily overwritten (to deal with GUI | |
1177 | # frameworks). |
|
1178 | # frameworks). | |
1178 | self.sys_excepthook = sys.excepthook |
|
1179 | self.sys_excepthook = sys.excepthook | |
1179 |
|
1180 | |||
1180 | def set_custom_exc(self,exc_tuple,handler): |
|
1181 | def set_custom_exc(self,exc_tuple,handler): | |
1181 | """set_custom_exc(exc_tuple,handler) |
|
1182 | """set_custom_exc(exc_tuple,handler) | |
1182 |
|
1183 | |||
1183 | Set a custom exception handler, which will be called if any of the |
|
1184 | Set a custom exception handler, which will be called if any of the | |
1184 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1185 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1185 | runcode() method. |
|
1186 | runcode() method. | |
1186 |
|
1187 | |||
1187 | Inputs: |
|
1188 | Inputs: | |
1188 |
|
1189 | |||
1189 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
1190 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
1190 | handler for. It is very important that you use a tuple, and NOT A |
|
1191 | handler for. It is very important that you use a tuple, and NOT A | |
1191 | LIST here, because of the way Python's except statement works. If |
|
1192 | LIST here, because of the way Python's except statement works. If | |
1192 | you only want to trap a single exception, use a singleton tuple: |
|
1193 | you only want to trap a single exception, use a singleton tuple: | |
1193 |
|
1194 | |||
1194 | exc_tuple == (MyCustomException,) |
|
1195 | exc_tuple == (MyCustomException,) | |
1195 |
|
1196 | |||
1196 | - handler: this must be defined as a function with the following |
|
1197 | - handler: this must be defined as a function with the following | |
1197 | basic interface: def my_handler(self,etype,value,tb). |
|
1198 | basic interface: def my_handler(self,etype,value,tb). | |
1198 |
|
1199 | |||
1199 | This will be made into an instance method (via new.instancemethod) |
|
1200 | This will be made into an instance method (via new.instancemethod) | |
1200 | of IPython itself, and it will be called if any of the exceptions |
|
1201 | of IPython itself, and it will be called if any of the exceptions | |
1201 | listed in the exc_tuple are caught. If the handler is None, an |
|
1202 | listed in the exc_tuple are caught. If the handler is None, an | |
1202 | internal basic one is used, which just prints basic info. |
|
1203 | internal basic one is used, which just prints basic info. | |
1203 |
|
1204 | |||
1204 | WARNING: by putting in your own exception handler into IPython's main |
|
1205 | WARNING: by putting in your own exception handler into IPython's main | |
1205 | execution loop, you run a very good chance of nasty crashes. This |
|
1206 | execution loop, you run a very good chance of nasty crashes. This | |
1206 | facility should only be used if you really know what you are doing.""" |
|
1207 | facility should only be used if you really know what you are doing.""" | |
1207 |
|
1208 | |||
1208 | assert type(exc_tuple)==type(()) , \ |
|
1209 | assert type(exc_tuple)==type(()) , \ | |
1209 | "The custom exceptions must be given AS A TUPLE." |
|
1210 | "The custom exceptions must be given AS A TUPLE." | |
1210 |
|
1211 | |||
1211 | def dummy_handler(self,etype,value,tb): |
|
1212 | def dummy_handler(self,etype,value,tb): | |
1212 | print '*** Simple custom exception handler ***' |
|
1213 | print '*** Simple custom exception handler ***' | |
1213 | print 'Exception type :',etype |
|
1214 | print 'Exception type :',etype | |
1214 | print 'Exception value:',value |
|
1215 | print 'Exception value:',value | |
1215 | print 'Traceback :',tb |
|
1216 | print 'Traceback :',tb | |
1216 | print 'Source code :','\n'.join(self.buffer) |
|
1217 | print 'Source code :','\n'.join(self.buffer) | |
1217 |
|
1218 | |||
1218 | if handler is None: handler = dummy_handler |
|
1219 | if handler is None: handler = dummy_handler | |
1219 |
|
1220 | |||
1220 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
1221 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
1221 | self.custom_exceptions = exc_tuple |
|
1222 | self.custom_exceptions = exc_tuple | |
1222 |
|
1223 | |||
1223 | def excepthook(self, etype, value, tb): |
|
1224 | def excepthook(self, etype, value, tb): | |
1224 | """One more defense for GUI apps that call sys.excepthook. |
|
1225 | """One more defense for GUI apps that call sys.excepthook. | |
1225 |
|
1226 | |||
1226 | GUI frameworks like wxPython trap exceptions and call |
|
1227 | GUI frameworks like wxPython trap exceptions and call | |
1227 | sys.excepthook themselves. I guess this is a feature that |
|
1228 | sys.excepthook themselves. I guess this is a feature that | |
1228 | enables them to keep running after exceptions that would |
|
1229 | enables them to keep running after exceptions that would | |
1229 | otherwise kill their mainloop. This is a bother for IPython |
|
1230 | otherwise kill their mainloop. This is a bother for IPython | |
1230 | which excepts to catch all of the program exceptions with a try: |
|
1231 | which excepts to catch all of the program exceptions with a try: | |
1231 | except: statement. |
|
1232 | except: statement. | |
1232 |
|
1233 | |||
1233 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1234 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1234 | any app directly invokes sys.excepthook, it will look to the user like |
|
1235 | any app directly invokes sys.excepthook, it will look to the user like | |
1235 | IPython crashed. In order to work around this, we can disable the |
|
1236 | IPython crashed. In order to work around this, we can disable the | |
1236 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1237 | CrashHandler and replace it with this excepthook instead, which prints a | |
1237 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1238 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1238 | call sys.excepthook will generate a regular-looking exception from |
|
1239 | call sys.excepthook will generate a regular-looking exception from | |
1239 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1240 | IPython, and the CrashHandler will only be triggered by real IPython | |
1240 | crashes. |
|
1241 | crashes. | |
1241 |
|
1242 | |||
1242 | This hook should be used sparingly, only in places which are not likely |
|
1243 | This hook should be used sparingly, only in places which are not likely | |
1243 | to be true IPython errors. |
|
1244 | to be true IPython errors. | |
1244 | """ |
|
1245 | """ | |
1245 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1246 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1246 |
|
1247 | |||
1247 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1248 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): | |
1248 | """Display the exception that just occurred. |
|
1249 | """Display the exception that just occurred. | |
1249 |
|
1250 | |||
1250 | If nothing is known about the exception, this is the method which |
|
1251 | If nothing is known about the exception, this is the method which | |
1251 | should be used throughout the code for presenting user tracebacks, |
|
1252 | should be used throughout the code for presenting user tracebacks, | |
1252 | rather than directly invoking the InteractiveTB object. |
|
1253 | rather than directly invoking the InteractiveTB object. | |
1253 |
|
1254 | |||
1254 | A specific showsyntaxerror() also exists, but this method can take |
|
1255 | A specific showsyntaxerror() also exists, but this method can take | |
1255 | care of calling it if needed, so unless you are explicitly catching a |
|
1256 | care of calling it if needed, so unless you are explicitly catching a | |
1256 | SyntaxError exception, don't try to analyze the stack manually and |
|
1257 | SyntaxError exception, don't try to analyze the stack manually and | |
1257 | simply call this method.""" |
|
1258 | simply call this method.""" | |
1258 |
|
1259 | |||
1259 |
|
1260 | |||
1260 | # Though this won't be called by syntax errors in the input line, |
|
1261 | # Though this won't be called by syntax errors in the input line, | |
1261 | # there may be SyntaxError cases whith imported code. |
|
1262 | # there may be SyntaxError cases whith imported code. | |
1262 |
|
1263 | |||
1263 | try: |
|
1264 | try: | |
1264 | if exc_tuple is None: |
|
1265 | if exc_tuple is None: | |
1265 | etype, value, tb = sys.exc_info() |
|
1266 | etype, value, tb = sys.exc_info() | |
1266 | else: |
|
1267 | else: | |
1267 | etype, value, tb = exc_tuple |
|
1268 | etype, value, tb = exc_tuple | |
1268 |
|
1269 | |||
1269 | if etype is SyntaxError: |
|
1270 | if etype is SyntaxError: | |
1270 | self.showsyntaxerror(filename) |
|
1271 | self.showsyntaxerror(filename) | |
1271 | elif etype is UsageError: |
|
1272 | elif etype is UsageError: | |
1272 | print "UsageError:", value |
|
1273 | print "UsageError:", value | |
1273 | else: |
|
1274 | else: | |
1274 | # WARNING: these variables are somewhat deprecated and not |
|
1275 | # WARNING: these variables are somewhat deprecated and not | |
1275 | # necessarily safe to use in a threaded environment, but tools |
|
1276 | # necessarily safe to use in a threaded environment, but tools | |
1276 | # like pdb depend on their existence, so let's set them. If we |
|
1277 | # like pdb depend on their existence, so let's set them. If we | |
1277 | # find problems in the field, we'll need to revisit their use. |
|
1278 | # find problems in the field, we'll need to revisit their use. | |
1278 | sys.last_type = etype |
|
1279 | sys.last_type = etype | |
1279 | sys.last_value = value |
|
1280 | sys.last_value = value | |
1280 | sys.last_traceback = tb |
|
1281 | sys.last_traceback = tb | |
1281 |
|
1282 | |||
1282 | if etype in self.custom_exceptions: |
|
1283 | if etype in self.custom_exceptions: | |
1283 | self.CustomTB(etype,value,tb) |
|
1284 | self.CustomTB(etype,value,tb) | |
1284 | else: |
|
1285 | else: | |
1285 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1286 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) | |
1286 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1287 | if self.InteractiveTB.call_pdb and self.has_readline: | |
1287 | # pdb mucks up readline, fix it back |
|
1288 | # pdb mucks up readline, fix it back | |
1288 | self.set_completer() |
|
1289 | self.set_completer() | |
1289 | except KeyboardInterrupt: |
|
1290 | except KeyboardInterrupt: | |
1290 | self.write("\nKeyboardInterrupt\n") |
|
1291 | self.write("\nKeyboardInterrupt\n") | |
1291 |
|
1292 | |||
1292 | def showsyntaxerror(self, filename=None): |
|
1293 | def showsyntaxerror(self, filename=None): | |
1293 | """Display the syntax error that just occurred. |
|
1294 | """Display the syntax error that just occurred. | |
1294 |
|
1295 | |||
1295 | This doesn't display a stack trace because there isn't one. |
|
1296 | This doesn't display a stack trace because there isn't one. | |
1296 |
|
1297 | |||
1297 | If a filename is given, it is stuffed in the exception instead |
|
1298 | If a filename is given, it is stuffed in the exception instead | |
1298 | of what was there before (because Python's parser always uses |
|
1299 | of what was there before (because Python's parser always uses | |
1299 | "<string>" when reading from a string). |
|
1300 | "<string>" when reading from a string). | |
1300 | """ |
|
1301 | """ | |
1301 | etype, value, last_traceback = sys.exc_info() |
|
1302 | etype, value, last_traceback = sys.exc_info() | |
1302 |
|
1303 | |||
1303 | # See note about these variables in showtraceback() below |
|
1304 | # See note about these variables in showtraceback() below | |
1304 | sys.last_type = etype |
|
1305 | sys.last_type = etype | |
1305 | sys.last_value = value |
|
1306 | sys.last_value = value | |
1306 | sys.last_traceback = last_traceback |
|
1307 | sys.last_traceback = last_traceback | |
1307 |
|
1308 | |||
1308 | if filename and etype is SyntaxError: |
|
1309 | if filename and etype is SyntaxError: | |
1309 | # Work hard to stuff the correct filename in the exception |
|
1310 | # Work hard to stuff the correct filename in the exception | |
1310 | try: |
|
1311 | try: | |
1311 | msg, (dummy_filename, lineno, offset, line) = value |
|
1312 | msg, (dummy_filename, lineno, offset, line) = value | |
1312 | except: |
|
1313 | except: | |
1313 | # Not the format we expect; leave it alone |
|
1314 | # Not the format we expect; leave it alone | |
1314 | pass |
|
1315 | pass | |
1315 | else: |
|
1316 | else: | |
1316 | # Stuff in the right filename |
|
1317 | # Stuff in the right filename | |
1317 | try: |
|
1318 | try: | |
1318 | # Assume SyntaxError is a class exception |
|
1319 | # Assume SyntaxError is a class exception | |
1319 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1320 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1320 | except: |
|
1321 | except: | |
1321 | # If that failed, assume SyntaxError is a string |
|
1322 | # If that failed, assume SyntaxError is a string | |
1322 | value = msg, (filename, lineno, offset, line) |
|
1323 | value = msg, (filename, lineno, offset, line) | |
1323 | self.SyntaxTB(etype,value,[]) |
|
1324 | self.SyntaxTB(etype,value,[]) | |
1324 |
|
1325 | |||
1325 | def edit_syntax_error(self): |
|
1326 | def edit_syntax_error(self): | |
1326 | """The bottom half of the syntax error handler called in the main loop. |
|
1327 | """The bottom half of the syntax error handler called in the main loop. | |
1327 |
|
1328 | |||
1328 | Loop until syntax error is fixed or user cancels. |
|
1329 | Loop until syntax error is fixed or user cancels. | |
1329 | """ |
|
1330 | """ | |
1330 |
|
1331 | |||
1331 | while self.SyntaxTB.last_syntax_error: |
|
1332 | while self.SyntaxTB.last_syntax_error: | |
1332 | # copy and clear last_syntax_error |
|
1333 | # copy and clear last_syntax_error | |
1333 | err = self.SyntaxTB.clear_err_state() |
|
1334 | err = self.SyntaxTB.clear_err_state() | |
1334 | if not self._should_recompile(err): |
|
1335 | if not self._should_recompile(err): | |
1335 | return |
|
1336 | return | |
1336 | try: |
|
1337 | try: | |
1337 | # may set last_syntax_error again if a SyntaxError is raised |
|
1338 | # may set last_syntax_error again if a SyntaxError is raised | |
1338 | self.safe_execfile(err.filename,self.user_ns) |
|
1339 | self.safe_execfile(err.filename,self.user_ns) | |
1339 | except: |
|
1340 | except: | |
1340 | self.showtraceback() |
|
1341 | self.showtraceback() | |
1341 | else: |
|
1342 | else: | |
1342 | try: |
|
1343 | try: | |
1343 | f = file(err.filename) |
|
1344 | f = file(err.filename) | |
1344 | try: |
|
1345 | try: | |
1345 | # This should be inside a display_trap block and I |
|
1346 | # This should be inside a display_trap block and I | |
1346 | # think it is. |
|
1347 | # think it is. | |
1347 | sys.displayhook(f.read()) |
|
1348 | sys.displayhook(f.read()) | |
1348 | finally: |
|
1349 | finally: | |
1349 | f.close() |
|
1350 | f.close() | |
1350 | except: |
|
1351 | except: | |
1351 | self.showtraceback() |
|
1352 | self.showtraceback() | |
1352 |
|
1353 | |||
1353 | def _should_recompile(self,e): |
|
1354 | def _should_recompile(self,e): | |
1354 | """Utility routine for edit_syntax_error""" |
|
1355 | """Utility routine for edit_syntax_error""" | |
1355 |
|
1356 | |||
1356 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1357 | if e.filename in ('<ipython console>','<input>','<string>', | |
1357 | '<console>','<BackgroundJob compilation>', |
|
1358 | '<console>','<BackgroundJob compilation>', | |
1358 | None): |
|
1359 | None): | |
1359 |
|
1360 | |||
1360 | return False |
|
1361 | return False | |
1361 | try: |
|
1362 | try: | |
1362 | if (self.autoedit_syntax and |
|
1363 | if (self.autoedit_syntax and | |
1363 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1364 | not self.ask_yes_no('Return to editor to correct syntax error? ' | |
1364 | '[Y/n] ','y')): |
|
1365 | '[Y/n] ','y')): | |
1365 | return False |
|
1366 | return False | |
1366 | except EOFError: |
|
1367 | except EOFError: | |
1367 | return False |
|
1368 | return False | |
1368 |
|
1369 | |||
1369 | def int0(x): |
|
1370 | def int0(x): | |
1370 | try: |
|
1371 | try: | |
1371 | return int(x) |
|
1372 | return int(x) | |
1372 | except TypeError: |
|
1373 | except TypeError: | |
1373 | return 0 |
|
1374 | return 0 | |
1374 | # always pass integer line and offset values to editor hook |
|
1375 | # always pass integer line and offset values to editor hook | |
1375 | try: |
|
1376 | try: | |
1376 | self.hooks.fix_error_editor(e.filename, |
|
1377 | self.hooks.fix_error_editor(e.filename, | |
1377 | int0(e.lineno),int0(e.offset),e.msg) |
|
1378 | int0(e.lineno),int0(e.offset),e.msg) | |
1378 | except TryNext: |
|
1379 | except TryNext: | |
1379 | warn('Could not open editor') |
|
1380 | warn('Could not open editor') | |
1380 | return False |
|
1381 | return False | |
1381 | return True |
|
1382 | return True | |
1382 |
|
1383 | |||
1383 | #------------------------------------------------------------------------- |
|
1384 | #------------------------------------------------------------------------- | |
1384 | # Things related to tab completion |
|
1385 | # Things related to tab completion | |
1385 | #------------------------------------------------------------------------- |
|
1386 | #------------------------------------------------------------------------- | |
1386 |
|
1387 | |||
1387 | def complete(self, text): |
|
1388 | def complete(self, text): | |
1388 | """Return a sorted list of all possible completions on text. |
|
1389 | """Return a sorted list of all possible completions on text. | |
1389 |
|
1390 | |||
1390 | Inputs: |
|
1391 | Inputs: | |
1391 |
|
1392 | |||
1392 | - text: a string of text to be completed on. |
|
1393 | - text: a string of text to be completed on. | |
1393 |
|
1394 | |||
1394 | This is a wrapper around the completion mechanism, similar to what |
|
1395 | This is a wrapper around the completion mechanism, similar to what | |
1395 | readline does at the command line when the TAB key is hit. By |
|
1396 | readline does at the command line when the TAB key is hit. By | |
1396 | exposing it as a method, it can be used by other non-readline |
|
1397 | exposing it as a method, it can be used by other non-readline | |
1397 | environments (such as GUIs) for text completion. |
|
1398 | environments (such as GUIs) for text completion. | |
1398 |
|
1399 | |||
1399 | Simple usage example: |
|
1400 | Simple usage example: | |
1400 |
|
1401 | |||
1401 | In [7]: x = 'hello' |
|
1402 | In [7]: x = 'hello' | |
1402 |
|
1403 | |||
1403 | In [8]: x |
|
1404 | In [8]: x | |
1404 | Out[8]: 'hello' |
|
1405 | Out[8]: 'hello' | |
1405 |
|
1406 | |||
1406 | In [9]: print x |
|
1407 | In [9]: print x | |
1407 | hello |
|
1408 | hello | |
1408 |
|
1409 | |||
1409 | In [10]: _ip.complete('x.l') |
|
1410 | In [10]: _ip.complete('x.l') | |
1410 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] |
|
1411 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] | |
1411 | """ |
|
1412 | """ | |
1412 |
|
1413 | |||
1413 | # Inject names into __builtin__ so we can complete on the added names. |
|
1414 | # Inject names into __builtin__ so we can complete on the added names. | |
1414 | with self.builtin_trap: |
|
1415 | with self.builtin_trap: | |
1415 | complete = self.Completer.complete |
|
1416 | complete = self.Completer.complete | |
1416 | state = 0 |
|
1417 | state = 0 | |
1417 | # use a dict so we get unique keys, since ipyhton's multiple |
|
1418 | # use a dict so we get unique keys, since ipyhton's multiple | |
1418 | # completers can return duplicates. When we make 2.4 a requirement, |
|
1419 | # completers can return duplicates. When we make 2.4 a requirement, | |
1419 | # start using sets instead, which are faster. |
|
1420 | # start using sets instead, which are faster. | |
1420 | comps = {} |
|
1421 | comps = {} | |
1421 | while True: |
|
1422 | while True: | |
1422 | newcomp = complete(text,state,line_buffer=text) |
|
1423 | newcomp = complete(text,state,line_buffer=text) | |
1423 | if newcomp is None: |
|
1424 | if newcomp is None: | |
1424 | break |
|
1425 | break | |
1425 | comps[newcomp] = 1 |
|
1426 | comps[newcomp] = 1 | |
1426 | state += 1 |
|
1427 | state += 1 | |
1427 | outcomps = comps.keys() |
|
1428 | outcomps = comps.keys() | |
1428 | outcomps.sort() |
|
1429 | outcomps.sort() | |
1429 | #print "T:",text,"OC:",outcomps # dbg |
|
1430 | #print "T:",text,"OC:",outcomps # dbg | |
1430 | #print "vars:",self.user_ns.keys() |
|
1431 | #print "vars:",self.user_ns.keys() | |
1431 | return outcomps |
|
1432 | return outcomps | |
1432 |
|
1433 | |||
1433 | def set_custom_completer(self,completer,pos=0): |
|
1434 | def set_custom_completer(self,completer,pos=0): | |
1434 | """set_custom_completer(completer,pos=0) |
|
1435 | """set_custom_completer(completer,pos=0) | |
1435 |
|
1436 | |||
1436 | Adds a new custom completer function. |
|
1437 | Adds a new custom completer function. | |
1437 |
|
1438 | |||
1438 | The position argument (defaults to 0) is the index in the completers |
|
1439 | The position argument (defaults to 0) is the index in the completers | |
1439 | list where you want the completer to be inserted.""" |
|
1440 | list where you want the completer to be inserted.""" | |
1440 |
|
1441 | |||
1441 | newcomp = new.instancemethod(completer,self.Completer, |
|
1442 | newcomp = new.instancemethod(completer,self.Completer, | |
1442 | self.Completer.__class__) |
|
1443 | self.Completer.__class__) | |
1443 | self.Completer.matchers.insert(pos,newcomp) |
|
1444 | self.Completer.matchers.insert(pos,newcomp) | |
1444 |
|
1445 | |||
1445 | def set_completer(self): |
|
1446 | def set_completer(self): | |
1446 | """reset readline's completer to be our own.""" |
|
1447 | """reset readline's completer to be our own.""" | |
1447 | self.readline.set_completer(self.Completer.complete) |
|
1448 | self.readline.set_completer(self.Completer.complete) | |
1448 |
|
1449 | |||
1449 | #------------------------------------------------------------------------- |
|
1450 | #------------------------------------------------------------------------- | |
1450 | # Things related to readline |
|
1451 | # Things related to readline | |
1451 | #------------------------------------------------------------------------- |
|
1452 | #------------------------------------------------------------------------- | |
1452 |
|
1453 | |||
1453 | def init_readline(self): |
|
1454 | def init_readline(self): | |
1454 | """Command history completion/saving/reloading.""" |
|
1455 | """Command history completion/saving/reloading.""" | |
1455 |
|
1456 | |||
1456 | self.rl_next_input = None |
|
1457 | self.rl_next_input = None | |
1457 | self.rl_do_indent = False |
|
1458 | self.rl_do_indent = False | |
1458 |
|
1459 | |||
1459 | if not self.readline_use: |
|
1460 | if not self.readline_use: | |
1460 | return |
|
1461 | return | |
1461 |
|
1462 | |||
1462 | import IPython.utils.rlineimpl as readline |
|
1463 | import IPython.utils.rlineimpl as readline | |
1463 |
|
1464 | |||
1464 | if not readline.have_readline: |
|
1465 | if not readline.have_readline: | |
1465 | self.has_readline = 0 |
|
1466 | self.has_readline = 0 | |
1466 | self.readline = None |
|
1467 | self.readline = None | |
1467 | # no point in bugging windows users with this every time: |
|
1468 | # no point in bugging windows users with this every time: | |
1468 | warn('Readline services not available on this platform.') |
|
1469 | warn('Readline services not available on this platform.') | |
1469 | else: |
|
1470 | else: | |
1470 | sys.modules['readline'] = readline |
|
1471 | sys.modules['readline'] = readline | |
1471 | import atexit |
|
1472 | import atexit | |
1472 | from IPython.core.completer import IPCompleter |
|
1473 | from IPython.core.completer import IPCompleter | |
1473 | self.Completer = IPCompleter(self, |
|
1474 | self.Completer = IPCompleter(self, | |
1474 | self.user_ns, |
|
1475 | self.user_ns, | |
1475 | self.user_global_ns, |
|
1476 | self.user_global_ns, | |
1476 | self.readline_omit__names, |
|
1477 | self.readline_omit__names, | |
1477 | self.alias_table) |
|
1478 | self.alias_table) | |
1478 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1479 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1479 | self.strdispatchers['complete_command'] = sdisp |
|
1480 | self.strdispatchers['complete_command'] = sdisp | |
1480 | self.Completer.custom_completers = sdisp |
|
1481 | self.Completer.custom_completers = sdisp | |
1481 | # Platform-specific configuration |
|
1482 | # Platform-specific configuration | |
1482 | if os.name == 'nt': |
|
1483 | if os.name == 'nt': | |
1483 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1484 | self.readline_startup_hook = readline.set_pre_input_hook | |
1484 | else: |
|
1485 | else: | |
1485 | self.readline_startup_hook = readline.set_startup_hook |
|
1486 | self.readline_startup_hook = readline.set_startup_hook | |
1486 |
|
1487 | |||
1487 | # Load user's initrc file (readline config) |
|
1488 | # Load user's initrc file (readline config) | |
1488 | # Or if libedit is used, load editrc. |
|
1489 | # Or if libedit is used, load editrc. | |
1489 | inputrc_name = os.environ.get('INPUTRC') |
|
1490 | inputrc_name = os.environ.get('INPUTRC') | |
1490 | if inputrc_name is None: |
|
1491 | if inputrc_name is None: | |
1491 | home_dir = get_home_dir() |
|
1492 | home_dir = get_home_dir() | |
1492 | if home_dir is not None: |
|
1493 | if home_dir is not None: | |
1493 | inputrc_name = '.inputrc' |
|
1494 | inputrc_name = '.inputrc' | |
1494 | if readline.uses_libedit: |
|
1495 | if readline.uses_libedit: | |
1495 | inputrc_name = '.editrc' |
|
1496 | inputrc_name = '.editrc' | |
1496 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1497 | inputrc_name = os.path.join(home_dir, inputrc_name) | |
1497 | if os.path.isfile(inputrc_name): |
|
1498 | if os.path.isfile(inputrc_name): | |
1498 | try: |
|
1499 | try: | |
1499 | readline.read_init_file(inputrc_name) |
|
1500 | readline.read_init_file(inputrc_name) | |
1500 | except: |
|
1501 | except: | |
1501 | warn('Problems reading readline initialization file <%s>' |
|
1502 | warn('Problems reading readline initialization file <%s>' | |
1502 | % inputrc_name) |
|
1503 | % inputrc_name) | |
1503 |
|
1504 | |||
1504 | self.has_readline = 1 |
|
1505 | self.has_readline = 1 | |
1505 | self.readline = readline |
|
1506 | self.readline = readline | |
1506 | # save this in sys so embedded copies can restore it properly |
|
1507 | # save this in sys so embedded copies can restore it properly | |
1507 | sys.ipcompleter = self.Completer.complete |
|
1508 | sys.ipcompleter = self.Completer.complete | |
1508 | self.set_completer() |
|
1509 | self.set_completer() | |
1509 |
|
1510 | |||
1510 | # Configure readline according to user's prefs |
|
1511 | # Configure readline according to user's prefs | |
1511 | # This is only done if GNU readline is being used. If libedit |
|
1512 | # This is only done if GNU readline is being used. If libedit | |
1512 | # is being used (as on Leopard) the readline config is |
|
1513 | # is being used (as on Leopard) the readline config is | |
1513 | # not run as the syntax for libedit is different. |
|
1514 | # not run as the syntax for libedit is different. | |
1514 | if not readline.uses_libedit: |
|
1515 | if not readline.uses_libedit: | |
1515 | for rlcommand in self.readline_parse_and_bind: |
|
1516 | for rlcommand in self.readline_parse_and_bind: | |
1516 | #print "loading rl:",rlcommand # dbg |
|
1517 | #print "loading rl:",rlcommand # dbg | |
1517 | readline.parse_and_bind(rlcommand) |
|
1518 | readline.parse_and_bind(rlcommand) | |
1518 |
|
1519 | |||
1519 | # Remove some chars from the delimiters list. If we encounter |
|
1520 | # Remove some chars from the delimiters list. If we encounter | |
1520 | # unicode chars, discard them. |
|
1521 | # unicode chars, discard them. | |
1521 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
1522 | delims = readline.get_completer_delims().encode("ascii", "ignore") | |
1522 | delims = delims.translate(string._idmap, |
|
1523 | delims = delims.translate(string._idmap, | |
1523 | self.readline_remove_delims) |
|
1524 | self.readline_remove_delims) | |
1524 | readline.set_completer_delims(delims) |
|
1525 | readline.set_completer_delims(delims) | |
1525 | # otherwise we end up with a monster history after a while: |
|
1526 | # otherwise we end up with a monster history after a while: | |
1526 | readline.set_history_length(1000) |
|
1527 | readline.set_history_length(1000) | |
1527 | try: |
|
1528 | try: | |
1528 | #print '*** Reading readline history' # dbg |
|
1529 | #print '*** Reading readline history' # dbg | |
1529 | readline.read_history_file(self.histfile) |
|
1530 | readline.read_history_file(self.histfile) | |
1530 | except IOError: |
|
1531 | except IOError: | |
1531 | pass # It doesn't exist yet. |
|
1532 | pass # It doesn't exist yet. | |
1532 |
|
1533 | |||
1533 | atexit.register(self.atexit_operations) |
|
1534 | atexit.register(self.atexit_operations) | |
1534 | del atexit |
|
1535 | del atexit | |
1535 |
|
1536 | |||
1536 | # Configure auto-indent for all platforms |
|
1537 | # Configure auto-indent for all platforms | |
1537 | self.set_autoindent(self.autoindent) |
|
1538 | self.set_autoindent(self.autoindent) | |
1538 |
|
1539 | |||
1539 | def set_next_input(self, s): |
|
1540 | def set_next_input(self, s): | |
1540 | """ Sets the 'default' input string for the next command line. |
|
1541 | """ Sets the 'default' input string for the next command line. | |
1541 |
|
1542 | |||
1542 | Requires readline. |
|
1543 | Requires readline. | |
1543 |
|
1544 | |||
1544 | Example: |
|
1545 | Example: | |
1545 |
|
1546 | |||
1546 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1547 | [D:\ipython]|1> _ip.set_next_input("Hello Word") | |
1547 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1548 | [D:\ipython]|2> Hello Word_ # cursor is here | |
1548 | """ |
|
1549 | """ | |
1549 |
|
1550 | |||
1550 | self.rl_next_input = s |
|
1551 | self.rl_next_input = s | |
1551 |
|
1552 | |||
1552 | def pre_readline(self): |
|
1553 | def pre_readline(self): | |
1553 | """readline hook to be used at the start of each line. |
|
1554 | """readline hook to be used at the start of each line. | |
1554 |
|
1555 | |||
1555 | Currently it handles auto-indent only.""" |
|
1556 | Currently it handles auto-indent only.""" | |
1556 |
|
1557 | |||
1557 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1558 | #debugx('self.indent_current_nsp','pre_readline:') | |
1558 |
|
1559 | |||
1559 | if self.rl_do_indent: |
|
1560 | if self.rl_do_indent: | |
1560 | self.readline.insert_text(self._indent_current_str()) |
|
1561 | self.readline.insert_text(self._indent_current_str()) | |
1561 | if self.rl_next_input is not None: |
|
1562 | if self.rl_next_input is not None: | |
1562 | self.readline.insert_text(self.rl_next_input) |
|
1563 | self.readline.insert_text(self.rl_next_input) | |
1563 | self.rl_next_input = None |
|
1564 | self.rl_next_input = None | |
1564 |
|
1565 | |||
1565 | def _indent_current_str(self): |
|
1566 | def _indent_current_str(self): | |
1566 | """return the current level of indentation as a string""" |
|
1567 | """return the current level of indentation as a string""" | |
1567 | return self.indent_current_nsp * ' ' |
|
1568 | return self.indent_current_nsp * ' ' | |
1568 |
|
1569 | |||
1569 | #------------------------------------------------------------------------- |
|
1570 | #------------------------------------------------------------------------- | |
1570 | # Things related to magics |
|
1571 | # Things related to magics | |
1571 | #------------------------------------------------------------------------- |
|
1572 | #------------------------------------------------------------------------- | |
1572 |
|
1573 | |||
1573 | def init_magics(self): |
|
1574 | def init_magics(self): | |
1574 | # Set user colors (don't do it in the constructor above so that it |
|
1575 | # Set user colors (don't do it in the constructor above so that it | |
1575 | # doesn't crash if colors option is invalid) |
|
1576 | # doesn't crash if colors option is invalid) | |
1576 | self.magic_colors(self.colors) |
|
1577 | self.magic_colors(self.colors) | |
1577 |
|
1578 | |||
1578 | def magic(self,arg_s): |
|
1579 | def magic(self,arg_s): | |
1579 | """Call a magic function by name. |
|
1580 | """Call a magic function by name. | |
1580 |
|
1581 | |||
1581 | Input: a string containing the name of the magic function to call and any |
|
1582 | Input: a string containing the name of the magic function to call and any | |
1582 | additional arguments to be passed to the magic. |
|
1583 | additional arguments to be passed to the magic. | |
1583 |
|
1584 | |||
1584 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1585 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
1585 | prompt: |
|
1586 | prompt: | |
1586 |
|
1587 | |||
1587 | In[1]: %name -opt foo bar |
|
1588 | In[1]: %name -opt foo bar | |
1588 |
|
1589 | |||
1589 | To call a magic without arguments, simply use magic('name'). |
|
1590 | To call a magic without arguments, simply use magic('name'). | |
1590 |
|
1591 | |||
1591 | This provides a proper Python function to call IPython's magics in any |
|
1592 | This provides a proper Python function to call IPython's magics in any | |
1592 | valid Python code you can type at the interpreter, including loops and |
|
1593 | valid Python code you can type at the interpreter, including loops and | |
1593 | compound statements. |
|
1594 | compound statements. | |
1594 | """ |
|
1595 | """ | |
1595 |
|
1596 | |||
1596 | args = arg_s.split(' ',1) |
|
1597 | args = arg_s.split(' ',1) | |
1597 | magic_name = args[0] |
|
1598 | magic_name = args[0] | |
1598 |
magic_name = magic_name.lstrip( |
|
1599 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
1599 |
|
1600 | |||
1600 | try: |
|
1601 | try: | |
1601 | magic_args = args[1] |
|
1602 | magic_args = args[1] | |
1602 | except IndexError: |
|
1603 | except IndexError: | |
1603 | magic_args = '' |
|
1604 | magic_args = '' | |
1604 | fn = getattr(self,'magic_'+magic_name,None) |
|
1605 | fn = getattr(self,'magic_'+magic_name,None) | |
1605 | if fn is None: |
|
1606 | if fn is None: | |
1606 | error("Magic function `%s` not found." % magic_name) |
|
1607 | error("Magic function `%s` not found." % magic_name) | |
1607 | else: |
|
1608 | else: | |
1608 | magic_args = self.var_expand(magic_args,1) |
|
1609 | magic_args = self.var_expand(magic_args,1) | |
1609 | with nested(self.builtin_trap, self.display_trap): |
|
1610 | with nested(self.builtin_trap, self.display_trap): | |
1610 | return fn(magic_args) |
|
1611 | return fn(magic_args) | |
1611 | # return result |
|
|||
1612 |
|
1612 | |||
1613 | def define_magic(self, magicname, func): |
|
1613 | def define_magic(self, magicname, func): | |
1614 | """Expose own function as magic function for ipython |
|
1614 | """Expose own function as magic function for ipython | |
1615 |
|
1615 | |||
1616 | def foo_impl(self,parameter_s=''): |
|
1616 | def foo_impl(self,parameter_s=''): | |
1617 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1617 | 'My very own magic!. (Use docstrings, IPython reads them).' | |
1618 | print 'Magic function. Passed parameter is between < >:' |
|
1618 | print 'Magic function. Passed parameter is between < >:' | |
1619 | print '<%s>' % parameter_s |
|
1619 | print '<%s>' % parameter_s | |
1620 | print 'The self object is:',self |
|
1620 | print 'The self object is:',self | |
1621 |
|
1621 | |||
1622 | self.define_magic('foo',foo_impl) |
|
1622 | self.define_magic('foo',foo_impl) | |
1623 | """ |
|
1623 | """ | |
1624 |
|
1624 | |||
1625 | import new |
|
1625 | import new | |
1626 | im = new.instancemethod(func,self, self.__class__) |
|
1626 | im = new.instancemethod(func,self, self.__class__) | |
1627 | old = getattr(self, "magic_" + magicname, None) |
|
1627 | old = getattr(self, "magic_" + magicname, None) | |
1628 | setattr(self, "magic_" + magicname, im) |
|
1628 | setattr(self, "magic_" + magicname, im) | |
1629 | return old |
|
1629 | return old | |
1630 |
|
1630 | |||
1631 | #------------------------------------------------------------------------- |
|
1631 | #------------------------------------------------------------------------- | |
1632 | # Things related to macros |
|
1632 | # Things related to macros | |
1633 | #------------------------------------------------------------------------- |
|
1633 | #------------------------------------------------------------------------- | |
1634 |
|
1634 | |||
1635 | def define_macro(self, name, themacro): |
|
1635 | def define_macro(self, name, themacro): | |
1636 | """Define a new macro |
|
1636 | """Define a new macro | |
1637 |
|
1637 | |||
1638 | Parameters |
|
1638 | Parameters | |
1639 | ---------- |
|
1639 | ---------- | |
1640 | name : str |
|
1640 | name : str | |
1641 | The name of the macro. |
|
1641 | The name of the macro. | |
1642 | themacro : str or Macro |
|
1642 | themacro : str or Macro | |
1643 | The action to do upon invoking the macro. If a string, a new |
|
1643 | The action to do upon invoking the macro. If a string, a new | |
1644 | Macro object is created by passing the string to it. |
|
1644 | Macro object is created by passing the string to it. | |
1645 | """ |
|
1645 | """ | |
1646 |
|
1646 | |||
1647 | from IPython.core import macro |
|
1647 | from IPython.core import macro | |
1648 |
|
1648 | |||
1649 | if isinstance(themacro, basestring): |
|
1649 | if isinstance(themacro, basestring): | |
1650 | themacro = macro.Macro(themacro) |
|
1650 | themacro = macro.Macro(themacro) | |
1651 | if not isinstance(themacro, macro.Macro): |
|
1651 | if not isinstance(themacro, macro.Macro): | |
1652 | raise ValueError('A macro must be a string or a Macro instance.') |
|
1652 | raise ValueError('A macro must be a string or a Macro instance.') | |
1653 | self.user_ns[name] = themacro |
|
1653 | self.user_ns[name] = themacro | |
1654 |
|
1654 | |||
1655 | #------------------------------------------------------------------------- |
|
1655 | #------------------------------------------------------------------------- | |
1656 | # Things related to the running of system commands |
|
1656 | # Things related to the running of system commands | |
1657 | #------------------------------------------------------------------------- |
|
1657 | #------------------------------------------------------------------------- | |
1658 |
|
1658 | |||
1659 | def system(self, cmd): |
|
1659 | def system(self, cmd): | |
1660 | """Make a system call, using IPython.""" |
|
1660 | """Make a system call, using IPython.""" | |
1661 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) |
|
1661 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) | |
1662 |
|
1662 | |||
1663 | #------------------------------------------------------------------------- |
|
1663 | #------------------------------------------------------------------------- | |
1664 | # Things related to aliases |
|
1664 | # Things related to aliases | |
1665 | #------------------------------------------------------------------------- |
|
1665 | #------------------------------------------------------------------------- | |
1666 |
|
1666 | |||
1667 | def init_alias(self): |
|
1667 | def init_alias(self): | |
1668 | self.alias_manager = AliasManager(self, config=self.config) |
|
1668 | self.alias_manager = AliasManager(self, config=self.config) | |
1669 |
|
1669 | |||
1670 | def expand_alias(self, line): |
|
|||
1671 | """ Expand an alias in the command line |
|
|||
1672 |
|
||||
1673 | Returns the provided command line, possibly with the first word |
|
|||
1674 | (command) translated according to alias expansion rules. |
|
|||
1675 |
|
||||
1676 | [ipython]|16> _ip.expand_aliases("np myfile.txt") |
|
|||
1677 | <16> 'q:/opt/np/notepad++.exe myfile.txt' |
|
|||
1678 | """ |
|
|||
1679 |
|
||||
1680 | pre,fn,rest = self.split_user_input(line) |
|
|||
1681 | res = pre + self.expand_aliases(fn, rest) |
|
|||
1682 | return res |
|
|||
1683 |
|
||||
1684 | def expand_aliases(self, fn, rest): |
|
|||
1685 | """Expand multiple levels of aliases: |
|
|||
1686 |
|
||||
1687 | if: |
|
|||
1688 |
|
||||
1689 | alias foo bar /tmp |
|
|||
1690 | alias baz foo |
|
|||
1691 |
|
||||
1692 | then: |
|
|||
1693 |
|
||||
1694 | baz huhhahhei -> bar /tmp huhhahhei |
|
|||
1695 |
|
||||
1696 | """ |
|
|||
1697 | line = fn + " " + rest |
|
|||
1698 |
|
||||
1699 | done = set() |
|
|||
1700 | while 1: |
|
|||
1701 | pre,fn,rest = prefilter.splitUserInput(line, |
|
|||
1702 | prefilter.shell_line_split) |
|
|||
1703 | if fn in self.alias_manager.alias_table: |
|
|||
1704 | if fn in done: |
|
|||
1705 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
|||
1706 | return "" |
|
|||
1707 | done.add(fn) |
|
|||
1708 |
|
||||
1709 | l2 = self.alias_manager.transform_alias(fn, rest) |
|
|||
1710 | if l2 == line: |
|
|||
1711 | break |
|
|||
1712 | # ls -> ls -F should not recurse forever |
|
|||
1713 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
|||
1714 | line = l2 |
|
|||
1715 | break |
|
|||
1716 | line=l2 |
|
|||
1717 | else: |
|
|||
1718 | break |
|
|||
1719 |
|
||||
1720 | return line |
|
|||
1721 |
|
||||
1722 | #------------------------------------------------------------------------- |
|
1670 | #------------------------------------------------------------------------- | |
1723 | # Things related to the running of code |
|
1671 | # Things related to the running of code | |
1724 | #------------------------------------------------------------------------- |
|
1672 | #------------------------------------------------------------------------- | |
1725 |
|
1673 | |||
1726 | def ex(self, cmd): |
|
1674 | def ex(self, cmd): | |
1727 | """Execute a normal python statement in user namespace.""" |
|
1675 | """Execute a normal python statement in user namespace.""" | |
1728 | with nested(self.builtin_trap, self.display_trap): |
|
1676 | with nested(self.builtin_trap, self.display_trap): | |
1729 | exec cmd in self.user_global_ns, self.user_ns |
|
1677 | exec cmd in self.user_global_ns, self.user_ns | |
1730 |
|
1678 | |||
1731 | def ev(self, expr): |
|
1679 | def ev(self, expr): | |
1732 | """Evaluate python expression expr in user namespace. |
|
1680 | """Evaluate python expression expr in user namespace. | |
1733 |
|
1681 | |||
1734 | Returns the result of evaluation |
|
1682 | Returns the result of evaluation | |
1735 | """ |
|
1683 | """ | |
1736 | with nested(self.builtin_trap, self.display_trap): |
|
1684 | with nested(self.builtin_trap, self.display_trap): | |
1737 | return eval(expr, self.user_global_ns, self.user_ns) |
|
1685 | return eval(expr, self.user_global_ns, self.user_ns) | |
1738 |
|
1686 | |||
1739 | def mainloop(self, banner=None): |
|
1687 | def mainloop(self, banner=None): | |
1740 | """Start the mainloop. |
|
1688 | """Start the mainloop. | |
1741 |
|
1689 | |||
1742 | If an optional banner argument is given, it will override the |
|
1690 | If an optional banner argument is given, it will override the | |
1743 | internally created default banner. |
|
1691 | internally created default banner. | |
1744 | """ |
|
1692 | """ | |
1745 |
|
1693 | |||
1746 | with nested(self.builtin_trap, self.display_trap): |
|
1694 | with nested(self.builtin_trap, self.display_trap): | |
1747 | if self.c: # Emulate Python's -c option |
|
1695 | if self.c: # Emulate Python's -c option | |
1748 | self.exec_init_cmd() |
|
1696 | self.exec_init_cmd() | |
1749 |
|
1697 | |||
1750 | if self.display_banner: |
|
1698 | if self.display_banner: | |
1751 | if banner is None: |
|
1699 | if banner is None: | |
1752 | banner = self.banner |
|
1700 | banner = self.banner | |
1753 |
|
1701 | |||
1754 | # if you run stuff with -c <cmd>, raw hist is not updated |
|
1702 | # if you run stuff with -c <cmd>, raw hist is not updated | |
1755 | # ensure that it's in sync |
|
1703 | # ensure that it's in sync | |
1756 | if len(self.input_hist) != len (self.input_hist_raw): |
|
1704 | if len(self.input_hist) != len (self.input_hist_raw): | |
1757 | self.input_hist_raw = InputList(self.input_hist) |
|
1705 | self.input_hist_raw = InputList(self.input_hist) | |
1758 |
|
1706 | |||
1759 | while 1: |
|
1707 | while 1: | |
1760 | try: |
|
1708 | try: | |
1761 | self.interact() |
|
1709 | self.interact() | |
1762 | #self.interact_with_readline() |
|
1710 | #self.interact_with_readline() | |
1763 | # XXX for testing of a readline-decoupled repl loop, call |
|
1711 | # XXX for testing of a readline-decoupled repl loop, call | |
1764 | # interact_with_readline above |
|
1712 | # interact_with_readline above | |
1765 | break |
|
1713 | break | |
1766 | except KeyboardInterrupt: |
|
1714 | except KeyboardInterrupt: | |
1767 | # this should not be necessary, but KeyboardInterrupt |
|
1715 | # this should not be necessary, but KeyboardInterrupt | |
1768 | # handling seems rather unpredictable... |
|
1716 | # handling seems rather unpredictable... | |
1769 | self.write("\nKeyboardInterrupt in interact()\n") |
|
1717 | self.write("\nKeyboardInterrupt in interact()\n") | |
1770 |
|
1718 | |||
1771 | def exec_init_cmd(self): |
|
1719 | def exec_init_cmd(self): | |
1772 | """Execute a command given at the command line. |
|
1720 | """Execute a command given at the command line. | |
1773 |
|
1721 | |||
1774 | This emulates Python's -c option.""" |
|
1722 | This emulates Python's -c option.""" | |
1775 |
|
1723 | |||
1776 | #sys.argv = ['-c'] |
|
1724 | #sys.argv = ['-c'] | |
1777 | self.push_line(self.prefilter(self.c, False)) |
|
1725 | self.push_line(self.prefilter_manager.prefilter_lines(self.c, False)) | |
1778 | if not self.interactive: |
|
1726 | if not self.interactive: | |
1779 | self.ask_exit() |
|
1727 | self.ask_exit() | |
1780 |
|
1728 | |||
1781 | def interact_prompt(self): |
|
1729 | def interact_prompt(self): | |
1782 | """ Print the prompt (in read-eval-print loop) |
|
1730 | """ Print the prompt (in read-eval-print loop) | |
1783 |
|
1731 | |||
1784 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1732 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not | |
1785 | used in standard IPython flow. |
|
1733 | used in standard IPython flow. | |
1786 | """ |
|
1734 | """ | |
1787 | if self.more: |
|
1735 | if self.more: | |
1788 | try: |
|
1736 | try: | |
1789 | prompt = self.hooks.generate_prompt(True) |
|
1737 | prompt = self.hooks.generate_prompt(True) | |
1790 | except: |
|
1738 | except: | |
1791 | self.showtraceback() |
|
1739 | self.showtraceback() | |
1792 | if self.autoindent: |
|
1740 | if self.autoindent: | |
1793 | self.rl_do_indent = True |
|
1741 | self.rl_do_indent = True | |
1794 |
|
1742 | |||
1795 | else: |
|
1743 | else: | |
1796 | try: |
|
1744 | try: | |
1797 | prompt = self.hooks.generate_prompt(False) |
|
1745 | prompt = self.hooks.generate_prompt(False) | |
1798 | except: |
|
1746 | except: | |
1799 | self.showtraceback() |
|
1747 | self.showtraceback() | |
1800 | self.write(prompt) |
|
1748 | self.write(prompt) | |
1801 |
|
1749 | |||
1802 | def interact_handle_input(self,line): |
|
1750 | def interact_handle_input(self,line): | |
1803 | """ Handle the input line (in read-eval-print loop) |
|
1751 | """ Handle the input line (in read-eval-print loop) | |
1804 |
|
1752 | |||
1805 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1753 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not | |
1806 | used in standard IPython flow. |
|
1754 | used in standard IPython flow. | |
1807 | """ |
|
1755 | """ | |
1808 | if line.lstrip() == line: |
|
1756 | if line.lstrip() == line: | |
1809 | self.shadowhist.add(line.strip()) |
|
1757 | self.shadowhist.add(line.strip()) | |
1810 | lineout = self.prefilter(line,self.more) |
|
1758 | lineout = self.prefilter_manager.prefilter_lines(line,self.more) | |
1811 |
|
1759 | |||
1812 | if line.strip(): |
|
1760 | if line.strip(): | |
1813 | if self.more: |
|
1761 | if self.more: | |
1814 | self.input_hist_raw[-1] += '%s\n' % line |
|
1762 | self.input_hist_raw[-1] += '%s\n' % line | |
1815 | else: |
|
1763 | else: | |
1816 | self.input_hist_raw.append('%s\n' % line) |
|
1764 | self.input_hist_raw.append('%s\n' % line) | |
1817 |
|
1765 | |||
1818 |
|
1766 | |||
1819 | self.more = self.push_line(lineout) |
|
1767 | self.more = self.push_line(lineout) | |
1820 | if (self.SyntaxTB.last_syntax_error and |
|
1768 | if (self.SyntaxTB.last_syntax_error and | |
1821 | self.autoedit_syntax): |
|
1769 | self.autoedit_syntax): | |
1822 | self.edit_syntax_error() |
|
1770 | self.edit_syntax_error() | |
1823 |
|
1771 | |||
1824 | def interact_with_readline(self): |
|
1772 | def interact_with_readline(self): | |
1825 | """ Demo of using interact_handle_input, interact_prompt |
|
1773 | """ Demo of using interact_handle_input, interact_prompt | |
1826 |
|
1774 | |||
1827 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), |
|
1775 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), | |
1828 | it should work like this. |
|
1776 | it should work like this. | |
1829 | """ |
|
1777 | """ | |
1830 | self.readline_startup_hook(self.pre_readline) |
|
1778 | self.readline_startup_hook(self.pre_readline) | |
1831 | while not self.exit_now: |
|
1779 | while not self.exit_now: | |
1832 | self.interact_prompt() |
|
1780 | self.interact_prompt() | |
1833 | if self.more: |
|
1781 | if self.more: | |
1834 | self.rl_do_indent = True |
|
1782 | self.rl_do_indent = True | |
1835 | else: |
|
1783 | else: | |
1836 | self.rl_do_indent = False |
|
1784 | self.rl_do_indent = False | |
1837 | line = raw_input_original().decode(self.stdin_encoding) |
|
1785 | line = raw_input_original().decode(self.stdin_encoding) | |
1838 | self.interact_handle_input(line) |
|
1786 | self.interact_handle_input(line) | |
1839 |
|
1787 | |||
1840 | def interact(self, banner=None): |
|
1788 | def interact(self, banner=None): | |
1841 | """Closely emulate the interactive Python console.""" |
|
1789 | """Closely emulate the interactive Python console.""" | |
1842 |
|
1790 | |||
1843 | # batch run -> do not interact |
|
1791 | # batch run -> do not interact | |
1844 | if self.exit_now: |
|
1792 | if self.exit_now: | |
1845 | return |
|
1793 | return | |
1846 |
|
1794 | |||
1847 | if self.display_banner: |
|
1795 | if self.display_banner: | |
1848 | if banner is None: |
|
1796 | if banner is None: | |
1849 | banner = self.banner |
|
1797 | banner = self.banner | |
1850 | self.write(banner) |
|
1798 | self.write(banner) | |
1851 |
|
1799 | |||
1852 | more = 0 |
|
1800 | more = 0 | |
1853 |
|
1801 | |||
1854 | # Mark activity in the builtins |
|
1802 | # Mark activity in the builtins | |
1855 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1803 | __builtin__.__dict__['__IPYTHON__active'] += 1 | |
1856 |
|
1804 | |||
1857 | if self.has_readline: |
|
1805 | if self.has_readline: | |
1858 | self.readline_startup_hook(self.pre_readline) |
|
1806 | self.readline_startup_hook(self.pre_readline) | |
1859 | # exit_now is set by a call to %Exit or %Quit, through the |
|
1807 | # exit_now is set by a call to %Exit or %Quit, through the | |
1860 | # ask_exit callback. |
|
1808 | # ask_exit callback. | |
1861 |
|
1809 | |||
1862 | while not self.exit_now: |
|
1810 | while not self.exit_now: | |
1863 | self.hooks.pre_prompt_hook() |
|
1811 | self.hooks.pre_prompt_hook() | |
1864 | if more: |
|
1812 | if more: | |
1865 | try: |
|
1813 | try: | |
1866 | prompt = self.hooks.generate_prompt(True) |
|
1814 | prompt = self.hooks.generate_prompt(True) | |
1867 | except: |
|
1815 | except: | |
1868 | self.showtraceback() |
|
1816 | self.showtraceback() | |
1869 | if self.autoindent: |
|
1817 | if self.autoindent: | |
1870 | self.rl_do_indent = True |
|
1818 | self.rl_do_indent = True | |
1871 |
|
1819 | |||
1872 | else: |
|
1820 | else: | |
1873 | try: |
|
1821 | try: | |
1874 | prompt = self.hooks.generate_prompt(False) |
|
1822 | prompt = self.hooks.generate_prompt(False) | |
1875 | except: |
|
1823 | except: | |
1876 | self.showtraceback() |
|
1824 | self.showtraceback() | |
1877 | try: |
|
1825 | try: | |
1878 | line = self.raw_input(prompt, more) |
|
1826 | line = self.raw_input(prompt, more) | |
1879 | if self.exit_now: |
|
1827 | if self.exit_now: | |
1880 | # quick exit on sys.std[in|out] close |
|
1828 | # quick exit on sys.std[in|out] close | |
1881 | break |
|
1829 | break | |
1882 | if self.autoindent: |
|
1830 | if self.autoindent: | |
1883 | self.rl_do_indent = False |
|
1831 | self.rl_do_indent = False | |
1884 |
|
1832 | |||
1885 | except KeyboardInterrupt: |
|
1833 | except KeyboardInterrupt: | |
1886 | #double-guard against keyboardinterrupts during kbdint handling |
|
1834 | #double-guard against keyboardinterrupts during kbdint handling | |
1887 | try: |
|
1835 | try: | |
1888 | self.write('\nKeyboardInterrupt\n') |
|
1836 | self.write('\nKeyboardInterrupt\n') | |
1889 | self.resetbuffer() |
|
1837 | self.resetbuffer() | |
1890 | # keep cache in sync with the prompt counter: |
|
1838 | # keep cache in sync with the prompt counter: | |
1891 | self.outputcache.prompt_count -= 1 |
|
1839 | self.outputcache.prompt_count -= 1 | |
1892 |
|
1840 | |||
1893 | if self.autoindent: |
|
1841 | if self.autoindent: | |
1894 | self.indent_current_nsp = 0 |
|
1842 | self.indent_current_nsp = 0 | |
1895 | more = 0 |
|
1843 | more = 0 | |
1896 | except KeyboardInterrupt: |
|
1844 | except KeyboardInterrupt: | |
1897 | pass |
|
1845 | pass | |
1898 | except EOFError: |
|
1846 | except EOFError: | |
1899 | if self.autoindent: |
|
1847 | if self.autoindent: | |
1900 | self.rl_do_indent = False |
|
1848 | self.rl_do_indent = False | |
1901 | self.readline_startup_hook(None) |
|
1849 | self.readline_startup_hook(None) | |
1902 | self.write('\n') |
|
1850 | self.write('\n') | |
1903 | self.exit() |
|
1851 | self.exit() | |
1904 | except bdb.BdbQuit: |
|
1852 | except bdb.BdbQuit: | |
1905 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1853 | warn('The Python debugger has exited with a BdbQuit exception.\n' | |
1906 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1854 | 'Because of how pdb handles the stack, it is impossible\n' | |
1907 | 'for IPython to properly format this particular exception.\n' |
|
1855 | 'for IPython to properly format this particular exception.\n' | |
1908 | 'IPython will resume normal operation.') |
|
1856 | 'IPython will resume normal operation.') | |
1909 | except: |
|
1857 | except: | |
1910 | # exceptions here are VERY RARE, but they can be triggered |
|
1858 | # exceptions here are VERY RARE, but they can be triggered | |
1911 | # asynchronously by signal handlers, for example. |
|
1859 | # asynchronously by signal handlers, for example. | |
1912 | self.showtraceback() |
|
1860 | self.showtraceback() | |
1913 | else: |
|
1861 | else: | |
1914 | more = self.push_line(line) |
|
1862 | more = self.push_line(line) | |
1915 | if (self.SyntaxTB.last_syntax_error and |
|
1863 | if (self.SyntaxTB.last_syntax_error and | |
1916 | self.autoedit_syntax): |
|
1864 | self.autoedit_syntax): | |
1917 | self.edit_syntax_error() |
|
1865 | self.edit_syntax_error() | |
1918 |
|
1866 | |||
1919 | # We are off again... |
|
1867 | # We are off again... | |
1920 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1868 | __builtin__.__dict__['__IPYTHON__active'] -= 1 | |
1921 |
|
1869 | |||
1922 | def safe_execfile(self,fname,*where,**kw): |
|
1870 | def safe_execfile(self,fname,*where,**kw): | |
1923 | """A safe version of the builtin execfile(). |
|
1871 | """A safe version of the builtin execfile(). | |
1924 |
|
1872 | |||
1925 | This version will never throw an exception, and knows how to handle |
|
1873 | This version will never throw an exception, and knows how to handle | |
1926 | ipython logs as well. |
|
1874 | ipython logs as well. | |
1927 |
|
1875 | |||
1928 | :Parameters: |
|
1876 | :Parameters: | |
1929 | fname : string |
|
1877 | fname : string | |
1930 | Name of the file to be executed. |
|
1878 | Name of the file to be executed. | |
1931 |
|
1879 | |||
1932 | where : tuple |
|
1880 | where : tuple | |
1933 | One or two namespaces, passed to execfile() as (globals,locals). |
|
1881 | One or two namespaces, passed to execfile() as (globals,locals). | |
1934 | If only one is given, it is passed as both. |
|
1882 | If only one is given, it is passed as both. | |
1935 |
|
1883 | |||
1936 | :Keywords: |
|
1884 | :Keywords: | |
1937 | islog : boolean (False) |
|
1885 | islog : boolean (False) | |
1938 |
|
1886 | |||
1939 | quiet : boolean (True) |
|
1887 | quiet : boolean (True) | |
1940 |
|
1888 | |||
1941 | exit_ignore : boolean (False) |
|
1889 | exit_ignore : boolean (False) | |
1942 | """ |
|
1890 | """ | |
1943 |
|
1891 | |||
1944 | def syspath_cleanup(): |
|
1892 | def syspath_cleanup(): | |
1945 | """Internal cleanup routine for sys.path.""" |
|
1893 | """Internal cleanup routine for sys.path.""" | |
1946 | if add_dname: |
|
1894 | if add_dname: | |
1947 | try: |
|
1895 | try: | |
1948 | sys.path.remove(dname) |
|
1896 | sys.path.remove(dname) | |
1949 | except ValueError: |
|
1897 | except ValueError: | |
1950 | # For some reason the user has already removed it, ignore. |
|
1898 | # For some reason the user has already removed it, ignore. | |
1951 | pass |
|
1899 | pass | |
1952 |
|
1900 | |||
1953 | fname = os.path.expanduser(fname) |
|
1901 | fname = os.path.expanduser(fname) | |
1954 |
|
1902 | |||
1955 | # Find things also in current directory. This is needed to mimic the |
|
1903 | # Find things also in current directory. This is needed to mimic the | |
1956 | # behavior of running a script from the system command line, where |
|
1904 | # behavior of running a script from the system command line, where | |
1957 | # Python inserts the script's directory into sys.path |
|
1905 | # Python inserts the script's directory into sys.path | |
1958 | dname = os.path.dirname(os.path.abspath(fname)) |
|
1906 | dname = os.path.dirname(os.path.abspath(fname)) | |
1959 | add_dname = False |
|
1907 | add_dname = False | |
1960 | if dname not in sys.path: |
|
1908 | if dname not in sys.path: | |
1961 | sys.path.insert(0,dname) |
|
1909 | sys.path.insert(0,dname) | |
1962 | add_dname = True |
|
1910 | add_dname = True | |
1963 |
|
1911 | |||
1964 | try: |
|
1912 | try: | |
1965 | xfile = open(fname) |
|
1913 | xfile = open(fname) | |
1966 | except: |
|
1914 | except: | |
1967 | print >> Term.cerr, \ |
|
1915 | print >> Term.cerr, \ | |
1968 | 'Could not open file <%s> for safe execution.' % fname |
|
1916 | 'Could not open file <%s> for safe execution.' % fname | |
1969 | syspath_cleanup() |
|
1917 | syspath_cleanup() | |
1970 | return None |
|
1918 | return None | |
1971 |
|
1919 | |||
1972 | kw.setdefault('islog',0) |
|
1920 | kw.setdefault('islog',0) | |
1973 | kw.setdefault('quiet',1) |
|
1921 | kw.setdefault('quiet',1) | |
1974 | kw.setdefault('exit_ignore',0) |
|
1922 | kw.setdefault('exit_ignore',0) | |
1975 |
|
1923 | |||
1976 | first = xfile.readline() |
|
1924 | first = xfile.readline() | |
1977 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
1925 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() | |
1978 | xfile.close() |
|
1926 | xfile.close() | |
1979 | # line by line execution |
|
1927 | # line by line execution | |
1980 | if first.startswith(loghead) or kw['islog']: |
|
1928 | if first.startswith(loghead) or kw['islog']: | |
1981 | print 'Loading log file <%s> one line at a time...' % fname |
|
1929 | print 'Loading log file <%s> one line at a time...' % fname | |
1982 | if kw['quiet']: |
|
1930 | if kw['quiet']: | |
1983 | stdout_save = sys.stdout |
|
1931 | stdout_save = sys.stdout | |
1984 | sys.stdout = StringIO.StringIO() |
|
1932 | sys.stdout = StringIO.StringIO() | |
1985 | try: |
|
1933 | try: | |
1986 | globs,locs = where[0:2] |
|
1934 | globs,locs = where[0:2] | |
1987 | except: |
|
1935 | except: | |
1988 | try: |
|
1936 | try: | |
1989 | globs = locs = where[0] |
|
1937 | globs = locs = where[0] | |
1990 | except: |
|
1938 | except: | |
1991 | globs = locs = globals() |
|
1939 | globs = locs = globals() | |
1992 | badblocks = [] |
|
1940 | badblocks = [] | |
1993 |
|
1941 | |||
1994 | # we also need to identify indented blocks of code when replaying |
|
1942 | # we also need to identify indented blocks of code when replaying | |
1995 | # logs and put them together before passing them to an exec |
|
1943 | # logs and put them together before passing them to an exec | |
1996 | # statement. This takes a bit of regexp and look-ahead work in the |
|
1944 | # statement. This takes a bit of regexp and look-ahead work in the | |
1997 | # file. It's easiest if we swallow the whole thing in memory |
|
1945 | # file. It's easiest if we swallow the whole thing in memory | |
1998 | # first, and manually walk through the lines list moving the |
|
1946 | # first, and manually walk through the lines list moving the | |
1999 | # counter ourselves. |
|
1947 | # counter ourselves. | |
2000 | indent_re = re.compile('\s+\S') |
|
1948 | indent_re = re.compile('\s+\S') | |
2001 | xfile = open(fname) |
|
1949 | xfile = open(fname) | |
2002 | filelines = xfile.readlines() |
|
1950 | filelines = xfile.readlines() | |
2003 | xfile.close() |
|
1951 | xfile.close() | |
2004 | nlines = len(filelines) |
|
1952 | nlines = len(filelines) | |
2005 | lnum = 0 |
|
1953 | lnum = 0 | |
2006 | while lnum < nlines: |
|
1954 | while lnum < nlines: | |
2007 | line = filelines[lnum] |
|
1955 | line = filelines[lnum] | |
2008 | lnum += 1 |
|
1956 | lnum += 1 | |
2009 | # don't re-insert logger status info into cache |
|
1957 | # don't re-insert logger status info into cache | |
2010 | if line.startswith('#log#'): |
|
1958 | if line.startswith('#log#'): | |
2011 | continue |
|
1959 | continue | |
2012 | else: |
|
1960 | else: | |
2013 | # build a block of code (maybe a single line) for execution |
|
1961 | # build a block of code (maybe a single line) for execution | |
2014 | block = line |
|
1962 | block = line | |
2015 | try: |
|
1963 | try: | |
2016 | next = filelines[lnum] # lnum has already incremented |
|
1964 | next = filelines[lnum] # lnum has already incremented | |
2017 | except: |
|
1965 | except: | |
2018 | next = None |
|
1966 | next = None | |
2019 | while next and indent_re.match(next): |
|
1967 | while next and indent_re.match(next): | |
2020 | block += next |
|
1968 | block += next | |
2021 | lnum += 1 |
|
1969 | lnum += 1 | |
2022 | try: |
|
1970 | try: | |
2023 | next = filelines[lnum] |
|
1971 | next = filelines[lnum] | |
2024 | except: |
|
1972 | except: | |
2025 | next = None |
|
1973 | next = None | |
2026 | # now execute the block of one or more lines |
|
1974 | # now execute the block of one or more lines | |
2027 | try: |
|
1975 | try: | |
2028 | exec block in globs,locs |
|
1976 | exec block in globs,locs | |
2029 | except SystemExit: |
|
1977 | except SystemExit: | |
2030 | pass |
|
1978 | pass | |
2031 | except: |
|
1979 | except: | |
2032 | badblocks.append(block.rstrip()) |
|
1980 | badblocks.append(block.rstrip()) | |
2033 | if kw['quiet']: # restore stdout |
|
1981 | if kw['quiet']: # restore stdout | |
2034 | sys.stdout.close() |
|
1982 | sys.stdout.close() | |
2035 | sys.stdout = stdout_save |
|
1983 | sys.stdout = stdout_save | |
2036 | print 'Finished replaying log file <%s>' % fname |
|
1984 | print 'Finished replaying log file <%s>' % fname | |
2037 | if badblocks: |
|
1985 | if badblocks: | |
2038 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
1986 | print >> sys.stderr, ('\nThe following lines/blocks in file ' | |
2039 | '<%s> reported errors:' % fname) |
|
1987 | '<%s> reported errors:' % fname) | |
2040 |
|
1988 | |||
2041 | for badline in badblocks: |
|
1989 | for badline in badblocks: | |
2042 | print >> sys.stderr, badline |
|
1990 | print >> sys.stderr, badline | |
2043 | else: # regular file execution |
|
1991 | else: # regular file execution | |
2044 | try: |
|
1992 | try: | |
2045 | if sys.platform == 'win32' and sys.version_info < (2,5,1): |
|
1993 | if sys.platform == 'win32' and sys.version_info < (2,5,1): | |
2046 | # Work around a bug in Python for Windows. The bug was |
|
1994 | # Work around a bug in Python for Windows. The bug was | |
2047 | # fixed in in Python 2.5 r54159 and 54158, but that's still |
|
1995 | # fixed in in Python 2.5 r54159 and 54158, but that's still | |
2048 | # SVN Python as of March/07. For details, see: |
|
1996 | # SVN Python as of March/07. For details, see: | |
2049 | # http://projects.scipy.org/ipython/ipython/ticket/123 |
|
1997 | # http://projects.scipy.org/ipython/ipython/ticket/123 | |
2050 | try: |
|
1998 | try: | |
2051 | globs,locs = where[0:2] |
|
1999 | globs,locs = where[0:2] | |
2052 | except: |
|
2000 | except: | |
2053 | try: |
|
2001 | try: | |
2054 | globs = locs = where[0] |
|
2002 | globs = locs = where[0] | |
2055 | except: |
|
2003 | except: | |
2056 | globs = locs = globals() |
|
2004 | globs = locs = globals() | |
2057 | exec file(fname) in globs,locs |
|
2005 | exec file(fname) in globs,locs | |
2058 | else: |
|
2006 | else: | |
2059 | execfile(fname,*where) |
|
2007 | execfile(fname,*where) | |
2060 | except SyntaxError: |
|
2008 | except SyntaxError: | |
2061 | self.showsyntaxerror() |
|
2009 | self.showsyntaxerror() | |
2062 | warn('Failure executing file: <%s>' % fname) |
|
2010 | warn('Failure executing file: <%s>' % fname) | |
2063 | except SystemExit,status: |
|
2011 | except SystemExit,status: | |
2064 | # Code that correctly sets the exit status flag to success (0) |
|
2012 | # Code that correctly sets the exit status flag to success (0) | |
2065 | # shouldn't be bothered with a traceback. Note that a plain |
|
2013 | # shouldn't be bothered with a traceback. Note that a plain | |
2066 | # sys.exit() does NOT set the message to 0 (it's empty) so that |
|
2014 | # sys.exit() does NOT set the message to 0 (it's empty) so that | |
2067 | # will still get a traceback. Note that the structure of the |
|
2015 | # will still get a traceback. Note that the structure of the | |
2068 | # SystemExit exception changed between Python 2.4 and 2.5, so |
|
2016 | # SystemExit exception changed between Python 2.4 and 2.5, so | |
2069 | # the checks must be done in a version-dependent way. |
|
2017 | # the checks must be done in a version-dependent way. | |
2070 | show = False |
|
2018 | show = False | |
2071 |
|
2019 | |||
2072 | if sys.version_info[:2] > (2,5): |
|
2020 | if sys.version_info[:2] > (2,5): | |
2073 | if status.message!=0 and not kw['exit_ignore']: |
|
2021 | if status.message!=0 and not kw['exit_ignore']: | |
2074 | show = True |
|
2022 | show = True | |
2075 | else: |
|
2023 | else: | |
2076 | if status.code and not kw['exit_ignore']: |
|
2024 | if status.code and not kw['exit_ignore']: | |
2077 | show = True |
|
2025 | show = True | |
2078 | if show: |
|
2026 | if show: | |
2079 | self.showtraceback() |
|
2027 | self.showtraceback() | |
2080 | warn('Failure executing file: <%s>' % fname) |
|
2028 | warn('Failure executing file: <%s>' % fname) | |
2081 | except: |
|
2029 | except: | |
2082 | self.showtraceback() |
|
2030 | self.showtraceback() | |
2083 | warn('Failure executing file: <%s>' % fname) |
|
2031 | warn('Failure executing file: <%s>' % fname) | |
2084 |
|
2032 | |||
2085 | syspath_cleanup() |
|
2033 | syspath_cleanup() | |
2086 |
|
2034 | |||
2087 | def cleanup_ipy_script(self, script): |
|
2035 | def cleanup_ipy_script(self, script): | |
2088 | """Make a script safe for self.runlines() |
|
2036 | """Make a script safe for self.runlines() | |
2089 |
|
2037 | |||
2090 | Notes |
|
2038 | Notes | |
2091 | ----- |
|
2039 | ----- | |
2092 | This was copied over from the old ipapi and probably can be done |
|
2040 | This was copied over from the old ipapi and probably can be done | |
2093 | away with once we move to block based interpreter. |
|
2041 | away with once we move to block based interpreter. | |
2094 |
|
2042 | |||
2095 | - Removes empty lines Suffixes all indented blocks that end with |
|
2043 | - Removes empty lines Suffixes all indented blocks that end with | |
2096 | - unindented lines with empty lines |
|
2044 | - unindented lines with empty lines | |
2097 | """ |
|
2045 | """ | |
2098 |
|
2046 | |||
2099 | res = [] |
|
2047 | res = [] | |
2100 | lines = script.splitlines() |
|
2048 | lines = script.splitlines() | |
2101 |
|
2049 | |||
2102 | level = 0 |
|
2050 | level = 0 | |
2103 | for l in lines: |
|
2051 | for l in lines: | |
2104 | lstripped = l.lstrip() |
|
2052 | lstripped = l.lstrip() | |
2105 | stripped = l.strip() |
|
2053 | stripped = l.strip() | |
2106 | if not stripped: |
|
2054 | if not stripped: | |
2107 | continue |
|
2055 | continue | |
2108 | newlevel = len(l) - len(lstripped) |
|
2056 | newlevel = len(l) - len(lstripped) | |
2109 | def is_secondary_block_start(s): |
|
2057 | def is_secondary_block_start(s): | |
2110 | if not s.endswith(':'): |
|
2058 | if not s.endswith(':'): | |
2111 | return False |
|
2059 | return False | |
2112 | if (s.startswith('elif') or |
|
2060 | if (s.startswith('elif') or | |
2113 | s.startswith('else') or |
|
2061 | s.startswith('else') or | |
2114 | s.startswith('except') or |
|
2062 | s.startswith('except') or | |
2115 | s.startswith('finally')): |
|
2063 | s.startswith('finally')): | |
2116 | return True |
|
2064 | return True | |
2117 |
|
2065 | |||
2118 | if level > 0 and newlevel == 0 and \ |
|
2066 | if level > 0 and newlevel == 0 and \ | |
2119 | not is_secondary_block_start(stripped): |
|
2067 | not is_secondary_block_start(stripped): | |
2120 | # add empty line |
|
2068 | # add empty line | |
2121 | res.append('') |
|
2069 | res.append('') | |
2122 |
|
2070 | |||
2123 | res.append(l) |
|
2071 | res.append(l) | |
2124 | level = newlevel |
|
2072 | level = newlevel | |
2125 | return '\n'.join(res) + '\n' |
|
2073 | return '\n'.join(res) + '\n' | |
2126 |
|
2074 | |||
2127 | def runlines(self, lines, clean=False): |
|
2075 | def runlines(self, lines, clean=False): | |
2128 | """Run a string of one or more lines of source. |
|
2076 | """Run a string of one or more lines of source. | |
2129 |
|
2077 | |||
2130 | This method is capable of running a string containing multiple source |
|
2078 | This method is capable of running a string containing multiple source | |
2131 | lines, as if they had been entered at the IPython prompt. Since it |
|
2079 | lines, as if they had been entered at the IPython prompt. Since it | |
2132 | exposes IPython's processing machinery, the given strings can contain |
|
2080 | exposes IPython's processing machinery, the given strings can contain | |
2133 | magic calls (%magic), special shell access (!cmd), etc. |
|
2081 | magic calls (%magic), special shell access (!cmd), etc. | |
2134 | """ |
|
2082 | """ | |
2135 |
|
2083 | |||
2136 | if isinstance(lines, (list, tuple)): |
|
2084 | if isinstance(lines, (list, tuple)): | |
2137 | lines = '\n'.join(lines) |
|
2085 | lines = '\n'.join(lines) | |
2138 |
|
2086 | |||
2139 | if clean: |
|
2087 | if clean: | |
2140 | lines = self.cleanup_ipy_script(lines) |
|
2088 | lines = self.cleanup_ipy_script(lines) | |
2141 |
|
2089 | |||
2142 | # We must start with a clean buffer, in case this is run from an |
|
2090 | # We must start with a clean buffer, in case this is run from an | |
2143 | # interactive IPython session (via a magic, for example). |
|
2091 | # interactive IPython session (via a magic, for example). | |
2144 | self.resetbuffer() |
|
2092 | self.resetbuffer() | |
2145 | lines = lines.splitlines() |
|
2093 | lines = lines.splitlines() | |
2146 | more = 0 |
|
2094 | more = 0 | |
2147 |
|
2095 | |||
2148 | with nested(self.builtin_trap, self.display_trap): |
|
2096 | with nested(self.builtin_trap, self.display_trap): | |
2149 | for line in lines: |
|
2097 | for line in lines: | |
2150 | # skip blank lines so we don't mess up the prompt counter, but do |
|
2098 | # skip blank lines so we don't mess up the prompt counter, but do | |
2151 | # NOT skip even a blank line if we are in a code block (more is |
|
2099 | # NOT skip even a blank line if we are in a code block (more is | |
2152 | # true) |
|
2100 | # true) | |
2153 |
|
2101 | |||
2154 | if line or more: |
|
2102 | if line or more: | |
2155 | # push to raw history, so hist line numbers stay in sync |
|
2103 | # push to raw history, so hist line numbers stay in sync | |
2156 | self.input_hist_raw.append("# " + line + "\n") |
|
2104 | self.input_hist_raw.append("# " + line + "\n") | |
2157 | more = self.push_line(self.prefilter(line,more)) |
|
2105 | more = self.push_line(self.prefilter_manager.prefilter_lines(line,more)) | |
2158 | # IPython's runsource returns None if there was an error |
|
2106 | # IPython's runsource returns None if there was an error | |
2159 | # compiling the code. This allows us to stop processing right |
|
2107 | # compiling the code. This allows us to stop processing right | |
2160 | # away, so the user gets the error message at the right place. |
|
2108 | # away, so the user gets the error message at the right place. | |
2161 | if more is None: |
|
2109 | if more is None: | |
2162 | break |
|
2110 | break | |
2163 | else: |
|
2111 | else: | |
2164 | self.input_hist_raw.append("\n") |
|
2112 | self.input_hist_raw.append("\n") | |
2165 | # final newline in case the input didn't have it, so that the code |
|
2113 | # final newline in case the input didn't have it, so that the code | |
2166 | # actually does get executed |
|
2114 | # actually does get executed | |
2167 | if more: |
|
2115 | if more: | |
2168 | self.push_line('\n') |
|
2116 | self.push_line('\n') | |
2169 |
|
2117 | |||
2170 | def runsource(self, source, filename='<input>', symbol='single'): |
|
2118 | def runsource(self, source, filename='<input>', symbol='single'): | |
2171 | """Compile and run some source in the interpreter. |
|
2119 | """Compile and run some source in the interpreter. | |
2172 |
|
2120 | |||
2173 | Arguments are as for compile_command(). |
|
2121 | Arguments are as for compile_command(). | |
2174 |
|
2122 | |||
2175 | One several things can happen: |
|
2123 | One several things can happen: | |
2176 |
|
2124 | |||
2177 | 1) The input is incorrect; compile_command() raised an |
|
2125 | 1) The input is incorrect; compile_command() raised an | |
2178 | exception (SyntaxError or OverflowError). A syntax traceback |
|
2126 | exception (SyntaxError or OverflowError). A syntax traceback | |
2179 | will be printed by calling the showsyntaxerror() method. |
|
2127 | will be printed by calling the showsyntaxerror() method. | |
2180 |
|
2128 | |||
2181 | 2) The input is incomplete, and more input is required; |
|
2129 | 2) The input is incomplete, and more input is required; | |
2182 | compile_command() returned None. Nothing happens. |
|
2130 | compile_command() returned None. Nothing happens. | |
2183 |
|
2131 | |||
2184 | 3) The input is complete; compile_command() returned a code |
|
2132 | 3) The input is complete; compile_command() returned a code | |
2185 | object. The code is executed by calling self.runcode() (which |
|
2133 | object. The code is executed by calling self.runcode() (which | |
2186 | also handles run-time exceptions, except for SystemExit). |
|
2134 | also handles run-time exceptions, except for SystemExit). | |
2187 |
|
2135 | |||
2188 | The return value is: |
|
2136 | The return value is: | |
2189 |
|
2137 | |||
2190 | - True in case 2 |
|
2138 | - True in case 2 | |
2191 |
|
2139 | |||
2192 | - False in the other cases, unless an exception is raised, where |
|
2140 | - False in the other cases, unless an exception is raised, where | |
2193 | None is returned instead. This can be used by external callers to |
|
2141 | None is returned instead. This can be used by external callers to | |
2194 | know whether to continue feeding input or not. |
|
2142 | know whether to continue feeding input or not. | |
2195 |
|
2143 | |||
2196 | The return value can be used to decide whether to use sys.ps1 or |
|
2144 | The return value can be used to decide whether to use sys.ps1 or | |
2197 | sys.ps2 to prompt the next line.""" |
|
2145 | sys.ps2 to prompt the next line.""" | |
2198 |
|
2146 | |||
2199 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
2147 | # if the source code has leading blanks, add 'if 1:\n' to it | |
2200 | # this allows execution of indented pasted code. It is tempting |
|
2148 | # this allows execution of indented pasted code. It is tempting | |
2201 | # to add '\n' at the end of source to run commands like ' a=1' |
|
2149 | # to add '\n' at the end of source to run commands like ' a=1' | |
2202 | # directly, but this fails for more complicated scenarios |
|
2150 | # directly, but this fails for more complicated scenarios | |
2203 | source=source.encode(self.stdin_encoding) |
|
2151 | source=source.encode(self.stdin_encoding) | |
2204 | if source[:1] in [' ', '\t']: |
|
2152 | if source[:1] in [' ', '\t']: | |
2205 | source = 'if 1:\n%s' % source |
|
2153 | source = 'if 1:\n%s' % source | |
2206 |
|
2154 | |||
2207 | try: |
|
2155 | try: | |
2208 | code = self.compile(source,filename,symbol) |
|
2156 | code = self.compile(source,filename,symbol) | |
2209 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): |
|
2157 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): | |
2210 | # Case 1 |
|
2158 | # Case 1 | |
2211 | self.showsyntaxerror(filename) |
|
2159 | self.showsyntaxerror(filename) | |
2212 | return None |
|
2160 | return None | |
2213 |
|
2161 | |||
2214 | if code is None: |
|
2162 | if code is None: | |
2215 | # Case 2 |
|
2163 | # Case 2 | |
2216 | return True |
|
2164 | return True | |
2217 |
|
2165 | |||
2218 | # Case 3 |
|
2166 | # Case 3 | |
2219 | # We store the code object so that threaded shells and |
|
2167 | # We store the code object so that threaded shells and | |
2220 | # custom exception handlers can access all this info if needed. |
|
2168 | # custom exception handlers can access all this info if needed. | |
2221 | # The source corresponding to this can be obtained from the |
|
2169 | # The source corresponding to this can be obtained from the | |
2222 | # buffer attribute as '\n'.join(self.buffer). |
|
2170 | # buffer attribute as '\n'.join(self.buffer). | |
2223 | self.code_to_run = code |
|
2171 | self.code_to_run = code | |
2224 | # now actually execute the code object |
|
2172 | # now actually execute the code object | |
2225 | if self.runcode(code) == 0: |
|
2173 | if self.runcode(code) == 0: | |
2226 | return False |
|
2174 | return False | |
2227 | else: |
|
2175 | else: | |
2228 | return None |
|
2176 | return None | |
2229 |
|
2177 | |||
2230 | def runcode(self,code_obj): |
|
2178 | def runcode(self,code_obj): | |
2231 | """Execute a code object. |
|
2179 | """Execute a code object. | |
2232 |
|
2180 | |||
2233 | When an exception occurs, self.showtraceback() is called to display a |
|
2181 | When an exception occurs, self.showtraceback() is called to display a | |
2234 | traceback. |
|
2182 | traceback. | |
2235 |
|
2183 | |||
2236 | Return value: a flag indicating whether the code to be run completed |
|
2184 | Return value: a flag indicating whether the code to be run completed | |
2237 | successfully: |
|
2185 | successfully: | |
2238 |
|
2186 | |||
2239 | - 0: successful execution. |
|
2187 | - 0: successful execution. | |
2240 | - 1: an error occurred. |
|
2188 | - 1: an error occurred. | |
2241 | """ |
|
2189 | """ | |
2242 |
|
2190 | |||
2243 | # Set our own excepthook in case the user code tries to call it |
|
2191 | # Set our own excepthook in case the user code tries to call it | |
2244 | # directly, so that the IPython crash handler doesn't get triggered |
|
2192 | # directly, so that the IPython crash handler doesn't get triggered | |
2245 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2193 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
2246 |
|
2194 | |||
2247 | # we save the original sys.excepthook in the instance, in case config |
|
2195 | # we save the original sys.excepthook in the instance, in case config | |
2248 | # code (such as magics) needs access to it. |
|
2196 | # code (such as magics) needs access to it. | |
2249 | self.sys_excepthook = old_excepthook |
|
2197 | self.sys_excepthook = old_excepthook | |
2250 | outflag = 1 # happens in more places, so it's easier as default |
|
2198 | outflag = 1 # happens in more places, so it's easier as default | |
2251 | try: |
|
2199 | try: | |
2252 | try: |
|
2200 | try: | |
2253 | self.hooks.pre_runcode_hook() |
|
2201 | self.hooks.pre_runcode_hook() | |
2254 | exec code_obj in self.user_global_ns, self.user_ns |
|
2202 | exec code_obj in self.user_global_ns, self.user_ns | |
2255 | finally: |
|
2203 | finally: | |
2256 | # Reset our crash handler in place |
|
2204 | # Reset our crash handler in place | |
2257 | sys.excepthook = old_excepthook |
|
2205 | sys.excepthook = old_excepthook | |
2258 | except SystemExit: |
|
2206 | except SystemExit: | |
2259 | self.resetbuffer() |
|
2207 | self.resetbuffer() | |
2260 | self.showtraceback() |
|
2208 | self.showtraceback() | |
2261 | warn("Type %exit or %quit to exit IPython " |
|
2209 | warn("Type %exit or %quit to exit IPython " | |
2262 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
2210 | "(%Exit or %Quit do so unconditionally).",level=1) | |
2263 | except self.custom_exceptions: |
|
2211 | except self.custom_exceptions: | |
2264 | etype,value,tb = sys.exc_info() |
|
2212 | etype,value,tb = sys.exc_info() | |
2265 | self.CustomTB(etype,value,tb) |
|
2213 | self.CustomTB(etype,value,tb) | |
2266 | except: |
|
2214 | except: | |
2267 | self.showtraceback() |
|
2215 | self.showtraceback() | |
2268 | else: |
|
2216 | else: | |
2269 | outflag = 0 |
|
2217 | outflag = 0 | |
2270 | if softspace(sys.stdout, 0): |
|
2218 | if softspace(sys.stdout, 0): | |
2271 |
|
2219 | |||
2272 | # Flush out code object which has been run (and source) |
|
2220 | # Flush out code object which has been run (and source) | |
2273 | self.code_to_run = None |
|
2221 | self.code_to_run = None | |
2274 | return outflag |
|
2222 | return outflag | |
2275 |
|
2223 | |||
2276 | def push_line(self, line): |
|
2224 | def push_line(self, line): | |
2277 | """Push a line to the interpreter. |
|
2225 | """Push a line to the interpreter. | |
2278 |
|
2226 | |||
2279 | The line should not have a trailing newline; it may have |
|
2227 | The line should not have a trailing newline; it may have | |
2280 | internal newlines. The line is appended to a buffer and the |
|
2228 | internal newlines. The line is appended to a buffer and the | |
2281 | interpreter's runsource() method is called with the |
|
2229 | interpreter's runsource() method is called with the | |
2282 | concatenated contents of the buffer as source. If this |
|
2230 | concatenated contents of the buffer as source. If this | |
2283 | indicates that the command was executed or invalid, the buffer |
|
2231 | indicates that the command was executed or invalid, the buffer | |
2284 | is reset; otherwise, the command is incomplete, and the buffer |
|
2232 | is reset; otherwise, the command is incomplete, and the buffer | |
2285 | is left as it was after the line was appended. The return |
|
2233 | is left as it was after the line was appended. The return | |
2286 | value is 1 if more input is required, 0 if the line was dealt |
|
2234 | value is 1 if more input is required, 0 if the line was dealt | |
2287 | with in some way (this is the same as runsource()). |
|
2235 | with in some way (this is the same as runsource()). | |
2288 | """ |
|
2236 | """ | |
2289 |
|
2237 | |||
2290 | # autoindent management should be done here, and not in the |
|
2238 | # autoindent management should be done here, and not in the | |
2291 | # interactive loop, since that one is only seen by keyboard input. We |
|
2239 | # interactive loop, since that one is only seen by keyboard input. We | |
2292 | # need this done correctly even for code run via runlines (which uses |
|
2240 | # need this done correctly even for code run via runlines (which uses | |
2293 | # push). |
|
2241 | # push). | |
2294 |
|
2242 | |||
2295 | #print 'push line: <%s>' % line # dbg |
|
2243 | #print 'push line: <%s>' % line # dbg | |
2296 | for subline in line.splitlines(): |
|
2244 | for subline in line.splitlines(): | |
2297 | self._autoindent_update(subline) |
|
2245 | self._autoindent_update(subline) | |
2298 | self.buffer.append(line) |
|
2246 | self.buffer.append(line) | |
2299 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
2247 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
2300 | if not more: |
|
2248 | if not more: | |
2301 | self.resetbuffer() |
|
2249 | self.resetbuffer() | |
2302 | return more |
|
2250 | return more | |
2303 |
|
2251 | |||
2304 | def _autoindent_update(self,line): |
|
2252 | def _autoindent_update(self,line): | |
2305 | """Keep track of the indent level.""" |
|
2253 | """Keep track of the indent level.""" | |
2306 |
|
2254 | |||
2307 | #debugx('line') |
|
2255 | #debugx('line') | |
2308 | #debugx('self.indent_current_nsp') |
|
2256 | #debugx('self.indent_current_nsp') | |
2309 | if self.autoindent: |
|
2257 | if self.autoindent: | |
2310 | if line: |
|
2258 | if line: | |
2311 | inisp = num_ini_spaces(line) |
|
2259 | inisp = num_ini_spaces(line) | |
2312 | if inisp < self.indent_current_nsp: |
|
2260 | if inisp < self.indent_current_nsp: | |
2313 | self.indent_current_nsp = inisp |
|
2261 | self.indent_current_nsp = inisp | |
2314 |
|
2262 | |||
2315 | if line[-1] == ':': |
|
2263 | if line[-1] == ':': | |
2316 | self.indent_current_nsp += 4 |
|
2264 | self.indent_current_nsp += 4 | |
2317 | elif dedent_re.match(line): |
|
2265 | elif dedent_re.match(line): | |
2318 | self.indent_current_nsp -= 4 |
|
2266 | self.indent_current_nsp -= 4 | |
2319 | else: |
|
2267 | else: | |
2320 | self.indent_current_nsp = 0 |
|
2268 | self.indent_current_nsp = 0 | |
2321 |
|
2269 | |||
2322 | def split_user_input(self, line): |
|
|||
2323 | # This is really a hold-over to support ipapi and some extensions |
|
|||
2324 | return prefilter.splitUserInput(line) |
|
|||
2325 |
|
||||
2326 | def resetbuffer(self): |
|
2270 | def resetbuffer(self): | |
2327 | """Reset the input buffer.""" |
|
2271 | """Reset the input buffer.""" | |
2328 | self.buffer[:] = [] |
|
2272 | self.buffer[:] = [] | |
2329 |
|
2273 | |||
2330 | def raw_input(self,prompt='',continue_prompt=False): |
|
2274 | def raw_input(self,prompt='',continue_prompt=False): | |
2331 | """Write a prompt and read a line. |
|
2275 | """Write a prompt and read a line. | |
2332 |
|
2276 | |||
2333 | The returned line does not include the trailing newline. |
|
2277 | The returned line does not include the trailing newline. | |
2334 | When the user enters the EOF key sequence, EOFError is raised. |
|
2278 | When the user enters the EOF key sequence, EOFError is raised. | |
2335 |
|
2279 | |||
2336 | Optional inputs: |
|
2280 | Optional inputs: | |
2337 |
|
2281 | |||
2338 | - prompt(''): a string to be printed to prompt the user. |
|
2282 | - prompt(''): a string to be printed to prompt the user. | |
2339 |
|
2283 | |||
2340 | - continue_prompt(False): whether this line is the first one or a |
|
2284 | - continue_prompt(False): whether this line is the first one or a | |
2341 | continuation in a sequence of inputs. |
|
2285 | continuation in a sequence of inputs. | |
2342 | """ |
|
2286 | """ | |
2343 | growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt)) |
|
2287 | # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt)) | |
|
2288 | ||||
2344 | # Code run by the user may have modified the readline completer state. |
|
2289 | # Code run by the user may have modified the readline completer state. | |
2345 | # We must ensure that our completer is back in place. |
|
2290 | # We must ensure that our completer is back in place. | |
2346 |
|
2291 | |||
2347 | if self.has_readline: |
|
2292 | if self.has_readline: | |
2348 | self.set_completer() |
|
2293 | self.set_completer() | |
2349 |
|
2294 | |||
2350 | try: |
|
2295 | try: | |
2351 | line = raw_input_original(prompt).decode(self.stdin_encoding) |
|
2296 | line = raw_input_original(prompt).decode(self.stdin_encoding) | |
2352 | except ValueError: |
|
2297 | except ValueError: | |
2353 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
2298 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | |
2354 | " or sys.stdout.close()!\nExiting IPython!") |
|
2299 | " or sys.stdout.close()!\nExiting IPython!") | |
2355 | self.ask_exit() |
|
2300 | self.ask_exit() | |
2356 | return "" |
|
2301 | return "" | |
2357 |
|
2302 | |||
2358 | # Try to be reasonably smart about not re-indenting pasted input more |
|
2303 | # Try to be reasonably smart about not re-indenting pasted input more | |
2359 | # than necessary. We do this by trimming out the auto-indent initial |
|
2304 | # than necessary. We do this by trimming out the auto-indent initial | |
2360 | # spaces, if the user's actual input started itself with whitespace. |
|
2305 | # spaces, if the user's actual input started itself with whitespace. | |
2361 | #debugx('self.buffer[-1]') |
|
2306 | #debugx('self.buffer[-1]') | |
2362 |
|
2307 | |||
2363 | if self.autoindent: |
|
2308 | if self.autoindent: | |
2364 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
2309 | if num_ini_spaces(line) > self.indent_current_nsp: | |
2365 | line = line[self.indent_current_nsp:] |
|
2310 | line = line[self.indent_current_nsp:] | |
2366 | self.indent_current_nsp = 0 |
|
2311 | self.indent_current_nsp = 0 | |
2367 |
|
2312 | |||
2368 | # store the unfiltered input before the user has any chance to modify |
|
2313 | # store the unfiltered input before the user has any chance to modify | |
2369 | # it. |
|
2314 | # it. | |
2370 | if line.strip(): |
|
2315 | if line.strip(): | |
2371 | if continue_prompt: |
|
2316 | if continue_prompt: | |
2372 | self.input_hist_raw[-1] += '%s\n' % line |
|
2317 | self.input_hist_raw[-1] += '%s\n' % line | |
2373 | if self.has_readline: # and some config option is set? |
|
2318 | if self.has_readline: # and some config option is set? | |
2374 | try: |
|
2319 | try: | |
2375 | histlen = self.readline.get_current_history_length() |
|
2320 | histlen = self.readline.get_current_history_length() | |
2376 | if histlen > 1: |
|
2321 | if histlen > 1: | |
2377 | newhist = self.input_hist_raw[-1].rstrip() |
|
2322 | newhist = self.input_hist_raw[-1].rstrip() | |
2378 | self.readline.remove_history_item(histlen-1) |
|
2323 | self.readline.remove_history_item(histlen-1) | |
2379 | self.readline.replace_history_item(histlen-2, |
|
2324 | self.readline.replace_history_item(histlen-2, | |
2380 | newhist.encode(self.stdin_encoding)) |
|
2325 | newhist.encode(self.stdin_encoding)) | |
2381 | except AttributeError: |
|
2326 | except AttributeError: | |
2382 | pass # re{move,place}_history_item are new in 2.4. |
|
2327 | pass # re{move,place}_history_item are new in 2.4. | |
2383 | else: |
|
2328 | else: | |
2384 | self.input_hist_raw.append('%s\n' % line) |
|
2329 | self.input_hist_raw.append('%s\n' % line) | |
2385 | # only entries starting at first column go to shadow history |
|
2330 | # only entries starting at first column go to shadow history | |
2386 | if line.lstrip() == line: |
|
2331 | if line.lstrip() == line: | |
2387 | self.shadowhist.add(line.strip()) |
|
2332 | self.shadowhist.add(line.strip()) | |
2388 | elif not continue_prompt: |
|
2333 | elif not continue_prompt: | |
2389 | self.input_hist_raw.append('\n') |
|
2334 | self.input_hist_raw.append('\n') | |
2390 | try: |
|
2335 | try: | |
2391 | lineout = self.prefilter(line,continue_prompt) |
|
2336 | lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt) | |
2392 | except: |
|
2337 | except: | |
2393 | # blanket except, in case a user-defined prefilter crashes, so it |
|
2338 | # blanket except, in case a user-defined prefilter crashes, so it | |
2394 | # can't take all of ipython with it. |
|
2339 | # can't take all of ipython with it. | |
2395 | self.showtraceback() |
|
2340 | self.showtraceback() | |
2396 | return '' |
|
2341 | return '' | |
2397 | else: |
|
2342 | else: | |
2398 | return lineout |
|
2343 | return lineout | |
2399 |
|
2344 | |||
2400 | # def init_exec_commands(self): |
|
2345 | # def init_exec_commands(self): | |
2401 | # for cmd in self.config.EXECUTE: |
|
2346 | # for cmd in self.config.EXECUTE: | |
2402 | # print "execute:", cmd |
|
2347 | # print "execute:", cmd | |
2403 | # self.api.runlines(cmd) |
|
2348 | # self.api.runlines(cmd) | |
2404 | # |
|
2349 | # | |
2405 | # batchrun = False |
|
2350 | # batchrun = False | |
2406 | # if self.config.has_key('EXECFILE'): |
|
2351 | # if self.config.has_key('EXECFILE'): | |
2407 | # for batchfile in [path(arg) for arg in self.config.EXECFILE |
|
2352 | # for batchfile in [path(arg) for arg in self.config.EXECFILE | |
2408 | # if arg.lower().endswith('.ipy')]: |
|
2353 | # if arg.lower().endswith('.ipy')]: | |
2409 | # if not batchfile.isfile(): |
|
2354 | # if not batchfile.isfile(): | |
2410 | # print "No such batch file:", batchfile |
|
2355 | # print "No such batch file:", batchfile | |
2411 | # continue |
|
2356 | # continue | |
2412 | # self.api.runlines(batchfile.text()) |
|
2357 | # self.api.runlines(batchfile.text()) | |
2413 | # batchrun = True |
|
2358 | # batchrun = True | |
2414 | # # without -i option, exit after running the batch file |
|
2359 | # # without -i option, exit after running the batch file | |
2415 | # if batchrun and not self.interactive: |
|
2360 | # if batchrun and not self.interactive: | |
2416 | # self.ask_exit() |
|
2361 | # self.ask_exit() | |
2417 |
|
2362 | |||
2418 | # def load(self, mod): |
|
2363 | # def load(self, mod): | |
2419 | # """ Load an extension. |
|
2364 | # """ Load an extension. | |
2420 | # |
|
2365 | # | |
2421 | # Some modules should (or must) be 'load()':ed, rather than just imported. |
|
2366 | # Some modules should (or must) be 'load()':ed, rather than just imported. | |
2422 | # |
|
2367 | # | |
2423 | # Loading will do: |
|
2368 | # Loading will do: | |
2424 | # |
|
2369 | # | |
2425 | # - run init_ipython(ip) |
|
2370 | # - run init_ipython(ip) | |
2426 | # - run ipython_firstrun(ip) |
|
2371 | # - run ipython_firstrun(ip) | |
2427 | # """ |
|
2372 | # """ | |
2428 | # |
|
2373 | # | |
2429 | # if mod in self.extensions: |
|
2374 | # if mod in self.extensions: | |
2430 | # # just to make sure we don't init it twice |
|
2375 | # # just to make sure we don't init it twice | |
2431 | # # note that if you 'load' a module that has already been |
|
2376 | # # note that if you 'load' a module that has already been | |
2432 | # # imported, init_ipython gets run anyway |
|
2377 | # # imported, init_ipython gets run anyway | |
2433 | # |
|
2378 | # | |
2434 | # return self.extensions[mod] |
|
2379 | # return self.extensions[mod] | |
2435 | # __import__(mod) |
|
2380 | # __import__(mod) | |
2436 | # m = sys.modules[mod] |
|
2381 | # m = sys.modules[mod] | |
2437 | # if hasattr(m,'init_ipython'): |
|
2382 | # if hasattr(m,'init_ipython'): | |
2438 | # m.init_ipython(self) |
|
2383 | # m.init_ipython(self) | |
2439 | # |
|
2384 | # | |
2440 | # if hasattr(m,'ipython_firstrun'): |
|
2385 | # if hasattr(m,'ipython_firstrun'): | |
2441 | # already_loaded = self.db.get('firstrun_done', set()) |
|
2386 | # already_loaded = self.db.get('firstrun_done', set()) | |
2442 | # if mod not in already_loaded: |
|
2387 | # if mod not in already_loaded: | |
2443 | # m.ipython_firstrun(self) |
|
2388 | # m.ipython_firstrun(self) | |
2444 | # already_loaded.add(mod) |
|
2389 | # already_loaded.add(mod) | |
2445 | # self.db['firstrun_done'] = already_loaded |
|
2390 | # self.db['firstrun_done'] = already_loaded | |
2446 | # |
|
2391 | # | |
2447 | # self.extensions[mod] = m |
|
2392 | # self.extensions[mod] = m | |
2448 | # return m |
|
2393 | # return m | |
2449 |
|
2394 | |||
2450 | #------------------------------------------------------------------------- |
|
2395 | #------------------------------------------------------------------------- | |
2451 | # Things related to the prefilter |
|
2396 | # Things related to the prefilter | |
2452 | #------------------------------------------------------------------------- |
|
2397 | #------------------------------------------------------------------------- | |
2453 |
|
2398 | |||
2454 |
def init_ |
|
2399 | def init_prefilter(self): | |
2455 | # escapes for automatic behavior on the command line |
|
2400 | self.prefilter_manager = PrefilterManager(self, config=self.config) | |
2456 | self.ESC_SHELL = '!' |
|
|||
2457 | self.ESC_SH_CAP = '!!' |
|
|||
2458 | self.ESC_HELP = '?' |
|
|||
2459 | self.ESC_MAGIC = '%' |
|
|||
2460 | self.ESC_QUOTE = ',' |
|
|||
2461 | self.ESC_QUOTE2 = ';' |
|
|||
2462 | self.ESC_PAREN = '/' |
|
|||
2463 |
|
||||
2464 | # And their associated handlers |
|
|||
2465 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
|||
2466 | self.ESC_QUOTE : self.handle_auto, |
|
|||
2467 | self.ESC_QUOTE2 : self.handle_auto, |
|
|||
2468 | self.ESC_MAGIC : self.handle_magic, |
|
|||
2469 | self.ESC_HELP : self.handle_help, |
|
|||
2470 | self.ESC_SHELL : self.handle_shell_escape, |
|
|||
2471 | self.ESC_SH_CAP : self.handle_shell_escape, |
|
|||
2472 | } |
|
|||
2473 |
|
||||
2474 | def _prefilter(self, line, continue_prompt): |
|
|||
2475 | """Calls different preprocessors, depending on the form of line.""" |
|
|||
2476 |
|
||||
2477 | # All handlers *must* return a value, even if it's blank (''). |
|
|||
2478 |
|
||||
2479 | # Lines are NOT logged here. Handlers should process the line as |
|
|||
2480 | # needed, update the cache AND log it (so that the input cache array |
|
|||
2481 | # stays synced). |
|
|||
2482 |
|
||||
2483 | #..................................................................... |
|
|||
2484 | # Code begins |
|
|||
2485 |
|
||||
2486 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
|||
2487 |
|
||||
2488 | # save the line away in case we crash, so the post-mortem handler can |
|
|||
2489 | # record it |
|
|||
2490 | growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt)) |
|
|||
2491 |
|
||||
2492 | self._last_input_line = line |
|
|||
2493 |
|
||||
2494 | #print '***line: <%s>' % line # dbg |
|
|||
2495 |
|
||||
2496 | if not line: |
|
|||
2497 | # Return immediately on purely empty lines, so that if the user |
|
|||
2498 | # previously typed some whitespace that started a continuation |
|
|||
2499 | # prompt, he can break out of that loop with just an empty line. |
|
|||
2500 | # This is how the default python prompt works. |
|
|||
2501 |
|
||||
2502 | # Only return if the accumulated input buffer was just whitespace! |
|
|||
2503 | if ''.join(self.buffer).isspace(): |
|
|||
2504 | self.buffer[:] = [] |
|
|||
2505 | return '' |
|
|||
2506 |
|
||||
2507 | line_info = prefilter.LineInfo(line, continue_prompt) |
|
|||
2508 |
|
||||
2509 | # the input history needs to track even empty lines |
|
|||
2510 | stripped = line.strip() |
|
|||
2511 |
|
||||
2512 | if not stripped: |
|
|||
2513 | if not continue_prompt: |
|
|||
2514 | self.outputcache.prompt_count -= 1 |
|
|||
2515 | return self.handle_normal(line_info) |
|
|||
2516 |
|
||||
2517 | # print '***cont',continue_prompt # dbg |
|
|||
2518 | # special handlers are only allowed for single line statements |
|
|||
2519 | if continue_prompt and not self.multi_line_specials: |
|
|||
2520 | return self.handle_normal(line_info) |
|
|||
2521 |
|
||||
2522 |
|
||||
2523 | # See whether any pre-existing handler can take care of it |
|
|||
2524 | rewritten = self.hooks.input_prefilter(stripped) |
|
|||
2525 | if rewritten != stripped: # ok, some prefilter did something |
|
|||
2526 | rewritten = line_info.pre + rewritten # add indentation |
|
|||
2527 | return self.handle_normal(prefilter.LineInfo(rewritten, |
|
|||
2528 | continue_prompt)) |
|
|||
2529 |
|
||||
2530 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
|||
2531 |
|
||||
2532 | return prefilter.prefilter(line_info, self) |
|
|||
2533 |
|
||||
2534 |
|
||||
2535 | def _prefilter_dumb(self, line, continue_prompt): |
|
|||
2536 | """simple prefilter function, for debugging""" |
|
|||
2537 | return self.handle_normal(line,continue_prompt) |
|
|||
2538 |
|
||||
2539 |
|
||||
2540 | def multiline_prefilter(self, line, continue_prompt): |
|
|||
2541 | """ Run _prefilter for each line of input |
|
|||
2542 |
|
||||
2543 | Covers cases where there are multiple lines in the user entry, |
|
|||
2544 | which is the case when the user goes back to a multiline history |
|
|||
2545 | entry and presses enter. |
|
|||
2546 |
|
||||
2547 | """ |
|
|||
2548 | growl.notify("multiline_prefilter: ", "%s\n%s" % (line, continue_prompt)) |
|
|||
2549 | out = [] |
|
|||
2550 | for l in line.rstrip('\n').split('\n'): |
|
|||
2551 | out.append(self._prefilter(l, continue_prompt)) |
|
|||
2552 | growl.notify("multiline_prefilter return: ", '\n'.join(out)) |
|
|||
2553 | return '\n'.join(out) |
|
|||
2554 |
|
||||
2555 | # Set the default prefilter() function (this can be user-overridden) |
|
|||
2556 | prefilter = multiline_prefilter |
|
|||
2557 |
|
||||
2558 | def handle_normal(self, line_info): |
|
|||
2559 | """Handle normal input lines. Use as a template for handlers.""" |
|
|||
2560 |
|
||||
2561 | # With autoindent on, we need some way to exit the input loop, and I |
|
|||
2562 | # don't want to force the user to have to backspace all the way to |
|
|||
2563 | # clear the line. The rule will be in this case, that either two |
|
|||
2564 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
|||
2565 | # of a size different to the indent level, will exit the input loop. |
|
|||
2566 | line = line_info.line |
|
|||
2567 | continue_prompt = line_info.continue_prompt |
|
|||
2568 |
|
||||
2569 | if (continue_prompt and self.autoindent and line.isspace() and |
|
|||
2570 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
|||
2571 | (self.buffer[-1]).isspace() )): |
|
|||
2572 | line = '' |
|
|||
2573 |
|
||||
2574 | self.log(line,line,continue_prompt) |
|
|||
2575 | return line |
|
|||
2576 |
|
||||
2577 | def handle_alias(self, line_info): |
|
|||
2578 | """Handle alias input lines. """ |
|
|||
2579 | tgt = self.alias_manager.alias_table[line_info.iFun] |
|
|||
2580 | if callable(tgt): |
|
|||
2581 | if '$' in line_info.line: |
|
|||
2582 | call_meth = '(_ip, _ip.var_expand(%s))' |
|
|||
2583 | else: |
|
|||
2584 | call_meth = '(_ip,%s)' |
|
|||
2585 | line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, |
|
|||
2586 | line_info.iFun, |
|
|||
2587 | make_quoted_expr(line_info.line)) |
|
|||
2588 | else: |
|
|||
2589 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) |
|
|||
2590 |
|
||||
2591 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
|||
2592 | # aliases won't work in indented sections. |
|
|||
2593 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
|||
2594 | make_quoted_expr( transformed )) |
|
|||
2595 |
|
||||
2596 | self.log(line_info.line,line_out,line_info.continue_prompt) |
|
|||
2597 | #print 'line out:',line_out # dbg |
|
|||
2598 | return line_out |
|
|||
2599 |
|
||||
2600 | def handle_shell_escape(self, line_info): |
|
|||
2601 | """Execute the line in a shell, empty return value""" |
|
|||
2602 | #print 'line in :', `line` # dbg |
|
|||
2603 | line = line_info.line |
|
|||
2604 | if line.lstrip().startswith('!!'): |
|
|||
2605 | # rewrite LineInfo's line, iFun and theRest to properly hold the |
|
|||
2606 | # call to %sx and the actual command to be executed, so |
|
|||
2607 | # handle_magic can work correctly. Note that this works even if |
|
|||
2608 | # the line is indented, so it handles multi_line_specials |
|
|||
2609 | # properly. |
|
|||
2610 | new_rest = line.lstrip()[2:] |
|
|||
2611 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) |
|
|||
2612 | line_info.iFun = 'sx' |
|
|||
2613 | line_info.theRest = new_rest |
|
|||
2614 | return self.handle_magic(line_info) |
|
|||
2615 | else: |
|
|||
2616 | cmd = line.lstrip().lstrip('!') |
|
|||
2617 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
|||
2618 | make_quoted_expr(cmd)) |
|
|||
2619 | # update cache/log and return |
|
|||
2620 | self.log(line,line_out,line_info.continue_prompt) |
|
|||
2621 | return line_out |
|
|||
2622 |
|
||||
2623 | def handle_magic(self, line_info): |
|
|||
2624 | """Execute magic functions.""" |
|
|||
2625 | iFun = line_info.iFun |
|
|||
2626 | theRest = line_info.theRest |
|
|||
2627 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, |
|
|||
2628 | make_quoted_expr(iFun + " " + theRest)) |
|
|||
2629 | self.log(line_info.line,cmd,line_info.continue_prompt) |
|
|||
2630 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
|||
2631 | return cmd |
|
|||
2632 |
|
||||
2633 | def handle_auto(self, line_info): |
|
|||
2634 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
|||
2635 |
|
||||
2636 | line = line_info.line |
|
|||
2637 | iFun = line_info.iFun |
|
|||
2638 | theRest = line_info.theRest |
|
|||
2639 | pre = line_info.pre |
|
|||
2640 | continue_prompt = line_info.continue_prompt |
|
|||
2641 | obj = line_info.ofind(self)['obj'] |
|
|||
2642 |
|
||||
2643 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
|||
2644 |
|
||||
2645 | # This should only be active for single-line input! |
|
|||
2646 | if continue_prompt: |
|
|||
2647 | self.log(line,line,continue_prompt) |
|
|||
2648 | return line |
|
|||
2649 |
|
||||
2650 | force_auto = isinstance(obj, IPyAutocall) |
|
|||
2651 | auto_rewrite = True |
|
|||
2652 |
|
||||
2653 | if pre == self.ESC_QUOTE: |
|
|||
2654 | # Auto-quote splitting on whitespace |
|
|||
2655 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
|||
2656 | elif pre == self.ESC_QUOTE2: |
|
|||
2657 | # Auto-quote whole string |
|
|||
2658 | newcmd = '%s("%s")' % (iFun,theRest) |
|
|||
2659 | elif pre == self.ESC_PAREN: |
|
|||
2660 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
|||
2661 | else: |
|
|||
2662 | # Auto-paren. |
|
|||
2663 | # We only apply it to argument-less calls if the autocall |
|
|||
2664 | # parameter is set to 2. We only need to check that autocall is < |
|
|||
2665 | # 2, since this function isn't called unless it's at least 1. |
|
|||
2666 | if not theRest and (self.autocall < 2) and not force_auto: |
|
|||
2667 | newcmd = '%s %s' % (iFun,theRest) |
|
|||
2668 | auto_rewrite = False |
|
|||
2669 | else: |
|
|||
2670 | if not force_auto and theRest.startswith('['): |
|
|||
2671 | if hasattr(obj,'__getitem__'): |
|
|||
2672 | # Don't autocall in this case: item access for an object |
|
|||
2673 | # which is BOTH callable and implements __getitem__. |
|
|||
2674 | newcmd = '%s %s' % (iFun,theRest) |
|
|||
2675 | auto_rewrite = False |
|
|||
2676 | else: |
|
|||
2677 | # if the object doesn't support [] access, go ahead and |
|
|||
2678 | # autocall |
|
|||
2679 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
|||
2680 | elif theRest.endswith(';'): |
|
|||
2681 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
|||
2682 | else: |
|
|||
2683 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
|||
2684 |
|
||||
2685 | if auto_rewrite: |
|
|||
2686 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd |
|
|||
2687 |
|
||||
2688 | try: |
|
|||
2689 | # plain ascii works better w/ pyreadline, on some machines, so |
|
|||
2690 | # we use it and only print uncolored rewrite if we have unicode |
|
|||
2691 | rw = str(rw) |
|
|||
2692 | print >>Term.cout, rw |
|
|||
2693 | except UnicodeEncodeError: |
|
|||
2694 | print "-------------->" + newcmd |
|
|||
2695 |
|
||||
2696 | # log what is now valid Python, not the actual user input (without the |
|
|||
2697 | # final newline) |
|
|||
2698 | self.log(line,newcmd,continue_prompt) |
|
|||
2699 | return newcmd |
|
|||
2700 |
|
||||
2701 | def handle_help(self, line_info): |
|
|||
2702 | """Try to get some help for the object. |
|
|||
2703 |
|
||||
2704 | obj? or ?obj -> basic information. |
|
|||
2705 | obj?? or ??obj -> more details. |
|
|||
2706 | """ |
|
|||
2707 |
|
||||
2708 | line = line_info.line |
|
|||
2709 | # We need to make sure that we don't process lines which would be |
|
|||
2710 | # otherwise valid python, such as "x=1 # what?" |
|
|||
2711 | try: |
|
|||
2712 | codeop.compile_command(line) |
|
|||
2713 | except SyntaxError: |
|
|||
2714 | # We should only handle as help stuff which is NOT valid syntax |
|
|||
2715 | if line[0]==self.ESC_HELP: |
|
|||
2716 | line = line[1:] |
|
|||
2717 | elif line[-1]==self.ESC_HELP: |
|
|||
2718 | line = line[:-1] |
|
|||
2719 | self.log(line,'#?'+line,line_info.continue_prompt) |
|
|||
2720 | if line: |
|
|||
2721 | #print 'line:<%r>' % line # dbg |
|
|||
2722 | self.magic_pinfo(line) |
|
|||
2723 | else: |
|
|||
2724 | page(self.usage,screen_lines=self.usable_screen_length) |
|
|||
2725 | return '' # Empty string is needed here! |
|
|||
2726 | except: |
|
|||
2727 | # Pass any other exceptions through to the normal handler |
|
|||
2728 | return self.handle_normal(line_info) |
|
|||
2729 | else: |
|
|||
2730 | # If the code compiles ok, we should handle it normally |
|
|||
2731 | return self.handle_normal(line_info) |
|
|||
2732 |
|
||||
2733 | def handle_emacs(self, line_info): |
|
|||
2734 | """Handle input lines marked by python-mode.""" |
|
|||
2735 |
|
||||
2736 | # Currently, nothing is done. Later more functionality can be added |
|
|||
2737 | # here if needed. |
|
|||
2738 |
|
||||
2739 | # The input cache shouldn't be updated |
|
|||
2740 | return line_info.line |
|
|||
2741 |
|
2401 | |||
2742 | #------------------------------------------------------------------------- |
|
2402 | #------------------------------------------------------------------------- | |
2743 | # Utilities |
|
2403 | # Utilities | |
2744 | #------------------------------------------------------------------------- |
|
2404 | #------------------------------------------------------------------------- | |
2745 |
|
2405 | |||
2746 | def getoutput(self, cmd): |
|
2406 | def getoutput(self, cmd): | |
2747 | return getoutput(self.var_expand(cmd,depth=2), |
|
2407 | return getoutput(self.var_expand(cmd,depth=2), | |
2748 | header=self.system_header, |
|
2408 | header=self.system_header, | |
2749 | verbose=self.system_verbose) |
|
2409 | verbose=self.system_verbose) | |
2750 |
|
2410 | |||
2751 | def getoutputerror(self, cmd): |
|
2411 | def getoutputerror(self, cmd): | |
2752 | return getoutputerror(self.var_expand(cmd,depth=2), |
|
2412 | return getoutputerror(self.var_expand(cmd,depth=2), | |
2753 | header=self.system_header, |
|
2413 | header=self.system_header, | |
2754 | verbose=self.system_verbose) |
|
2414 | verbose=self.system_verbose) | |
2755 |
|
2415 | |||
2756 | def var_expand(self,cmd,depth=0): |
|
2416 | def var_expand(self,cmd,depth=0): | |
2757 | """Expand python variables in a string. |
|
2417 | """Expand python variables in a string. | |
2758 |
|
2418 | |||
2759 | The depth argument indicates how many frames above the caller should |
|
2419 | The depth argument indicates how many frames above the caller should | |
2760 | be walked to look for the local namespace where to expand variables. |
|
2420 | be walked to look for the local namespace where to expand variables. | |
2761 |
|
2421 | |||
2762 | The global namespace for expansion is always the user's interactive |
|
2422 | The global namespace for expansion is always the user's interactive | |
2763 | namespace. |
|
2423 | namespace. | |
2764 | """ |
|
2424 | """ | |
2765 |
|
2425 | |||
2766 | return str(ItplNS(cmd, |
|
2426 | return str(ItplNS(cmd, | |
2767 | self.user_ns, # globals |
|
2427 | self.user_ns, # globals | |
2768 | # Skip our own frame in searching for locals: |
|
2428 | # Skip our own frame in searching for locals: | |
2769 | sys._getframe(depth+1).f_locals # locals |
|
2429 | sys._getframe(depth+1).f_locals # locals | |
2770 | )) |
|
2430 | )) | |
2771 |
|
2431 | |||
2772 | def mktempfile(self,data=None): |
|
2432 | def mktempfile(self,data=None): | |
2773 | """Make a new tempfile and return its filename. |
|
2433 | """Make a new tempfile and return its filename. | |
2774 |
|
2434 | |||
2775 | This makes a call to tempfile.mktemp, but it registers the created |
|
2435 | This makes a call to tempfile.mktemp, but it registers the created | |
2776 | filename internally so ipython cleans it up at exit time. |
|
2436 | filename internally so ipython cleans it up at exit time. | |
2777 |
|
2437 | |||
2778 | Optional inputs: |
|
2438 | Optional inputs: | |
2779 |
|
2439 | |||
2780 | - data(None): if data is given, it gets written out to the temp file |
|
2440 | - data(None): if data is given, it gets written out to the temp file | |
2781 | immediately, and the file is closed again.""" |
|
2441 | immediately, and the file is closed again.""" | |
2782 |
|
2442 | |||
2783 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2443 | filename = tempfile.mktemp('.py','ipython_edit_') | |
2784 | self.tempfiles.append(filename) |
|
2444 | self.tempfiles.append(filename) | |
2785 |
|
2445 | |||
2786 | if data: |
|
2446 | if data: | |
2787 | tmp_file = open(filename,'w') |
|
2447 | tmp_file = open(filename,'w') | |
2788 | tmp_file.write(data) |
|
2448 | tmp_file.write(data) | |
2789 | tmp_file.close() |
|
2449 | tmp_file.close() | |
2790 | return filename |
|
2450 | return filename | |
2791 |
|
2451 | |||
2792 | def write(self,data): |
|
2452 | def write(self,data): | |
2793 | """Write a string to the default output""" |
|
2453 | """Write a string to the default output""" | |
2794 | Term.cout.write(data) |
|
2454 | Term.cout.write(data) | |
2795 |
|
2455 | |||
2796 | def write_err(self,data): |
|
2456 | def write_err(self,data): | |
2797 | """Write a string to the default error output""" |
|
2457 | """Write a string to the default error output""" | |
2798 | Term.cerr.write(data) |
|
2458 | Term.cerr.write(data) | |
2799 |
|
2459 | |||
2800 | def ask_yes_no(self,prompt,default=True): |
|
2460 | def ask_yes_no(self,prompt,default=True): | |
2801 | if self.quiet: |
|
2461 | if self.quiet: | |
2802 | return True |
|
2462 | return True | |
2803 | return ask_yes_no(prompt,default) |
|
2463 | return ask_yes_no(prompt,default) | |
2804 |
|
2464 | |||
2805 | #------------------------------------------------------------------------- |
|
2465 | #------------------------------------------------------------------------- | |
2806 | # Things related to IPython exiting |
|
2466 | # Things related to IPython exiting | |
2807 | #------------------------------------------------------------------------- |
|
2467 | #------------------------------------------------------------------------- | |
2808 |
|
2468 | |||
2809 | def ask_exit(self): |
|
2469 | def ask_exit(self): | |
2810 | """ Call for exiting. Can be overiden and used as a callback. """ |
|
2470 | """ Call for exiting. Can be overiden and used as a callback. """ | |
2811 | self.exit_now = True |
|
2471 | self.exit_now = True | |
2812 |
|
2472 | |||
2813 | def exit(self): |
|
2473 | def exit(self): | |
2814 | """Handle interactive exit. |
|
2474 | """Handle interactive exit. | |
2815 |
|
2475 | |||
2816 | This method calls the ask_exit callback.""" |
|
2476 | This method calls the ask_exit callback.""" | |
2817 | if self.confirm_exit: |
|
2477 | if self.confirm_exit: | |
2818 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2478 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): | |
2819 | self.ask_exit() |
|
2479 | self.ask_exit() | |
2820 | else: |
|
2480 | else: | |
2821 | self.ask_exit() |
|
2481 | self.ask_exit() | |
2822 |
|
2482 | |||
2823 | def atexit_operations(self): |
|
2483 | def atexit_operations(self): | |
2824 | """This will be executed at the time of exit. |
|
2484 | """This will be executed at the time of exit. | |
2825 |
|
2485 | |||
2826 | Saving of persistent data should be performed here. |
|
2486 | Saving of persistent data should be performed here. | |
2827 | """ |
|
2487 | """ | |
2828 | self.savehist() |
|
2488 | self.savehist() | |
2829 |
|
2489 | |||
2830 | # Cleanup all tempfiles left around |
|
2490 | # Cleanup all tempfiles left around | |
2831 | for tfile in self.tempfiles: |
|
2491 | for tfile in self.tempfiles: | |
2832 | try: |
|
2492 | try: | |
2833 | os.unlink(tfile) |
|
2493 | os.unlink(tfile) | |
2834 | except OSError: |
|
2494 | except OSError: | |
2835 | pass |
|
2495 | pass | |
2836 |
|
2496 | |||
2837 | # Clear all user namespaces to release all references cleanly. |
|
2497 | # Clear all user namespaces to release all references cleanly. | |
2838 | self.reset() |
|
2498 | self.reset() | |
2839 |
|
2499 | |||
2840 | # Run user hooks |
|
2500 | # Run user hooks | |
2841 | self.hooks.shutdown_hook() |
|
2501 | self.hooks.shutdown_hook() | |
2842 |
|
2502 | |||
2843 | def cleanup(self): |
|
2503 | def cleanup(self): | |
2844 | self.restore_sys_module_state() |
|
2504 | self.restore_sys_module_state() | |
2845 |
|
2505 | |||
2846 |
|
2506 | |||
2847 |
|
||||
2848 |
|
||||
2849 |
|
This diff has been collapsed as it changes many lines, (932 lines changed) Show them Hide them | |||||
@@ -1,321 +1,777 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | #!/usr/bin/env python | |
|
2 | # encoding: utf-8 | |||
2 | """ |
|
3 | """ | |
3 | Classes and functions for prefiltering (transforming) a line of user input. |
|
4 | Prefiltering components. | |
4 | This module is responsible, primarily, for breaking the line up into useful |
|
5 | ||
5 | pieces and triggering the appropriate handlers in iplib to do the actual |
|
6 | Authors: | |
6 | transforming work. |
|
7 | ||
|
8 | * Brian Granger | |||
|
9 | * Fernando Perez | |||
|
10 | * Dan Milstein | |||
7 | """ |
|
11 | """ | |
8 | __docformat__ = "restructuredtext en" |
|
|||
9 |
|
12 | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Copyright (C) 2008-2009 The IPython Development Team | |||
|
15 | # | |||
|
16 | # Distributed under the terms of the BSD License. The full license is in | |||
|
17 | # the file COPYING, distributed as part of this software. | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Imports | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | import __builtin__ | |||
|
25 | import codeop | |||
|
26 | import keyword | |||
|
27 | import os | |||
10 | import re |
|
28 | import re | |
|
29 | import sys | |||
|
30 | ||||
|
31 | from IPython.core.alias import AliasManager | |||
11 | from IPython.core.autocall import IPyAutocall |
|
32 | from IPython.core.autocall import IPyAutocall | |
|
33 | from IPython.core.component import Component | |||
|
34 | from IPython.core.splitinput import split_user_input | |||
|
35 | ||||
|
36 | from IPython.utils.traitlets import List, Int, Any, Str, CBool | |||
|
37 | from IPython.utils.genutils import make_quoted_expr | |||
|
38 | from IPython.utils.autoattr import auto_attr | |||
|
39 | ||||
|
40 | #----------------------------------------------------------------------------- | |||
|
41 | # Global utilities, errors and constants | |||
|
42 | #----------------------------------------------------------------------------- | |||
|
43 | ||||
|
44 | ||||
|
45 | ESC_SHELL = '!' | |||
|
46 | ESC_SH_CAP = '!!' | |||
|
47 | ESC_HELP = '?' | |||
|
48 | ESC_MAGIC = '%' | |||
|
49 | ESC_QUOTE = ',' | |||
|
50 | ESC_QUOTE2 = ';' | |||
|
51 | ESC_PAREN = '/' | |||
|
52 | ||||
|
53 | ||||
|
54 | class PrefilterError(Exception): | |||
|
55 | pass | |||
|
56 | ||||
|
57 | ||||
|
58 | # RegExp to identify potential function names | |||
|
59 | re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') | |||
|
60 | ||||
|
61 | # RegExp to exclude strings with this start from autocalling. In | |||
|
62 | # particular, all binary operators should be excluded, so that if foo is | |||
|
63 | # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The | |||
|
64 | # characters '!=()' don't need to be checked for, as the checkPythonChars | |||
|
65 | # routine explicitely does so, to catch direct calls and rebindings of | |||
|
66 | # existing names. | |||
|
67 | ||||
|
68 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise | |||
|
69 | # it affects the rest of the group in square brackets. | |||
|
70 | re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]' | |||
|
71 | r'|^is |^not |^in |^and |^or ') | |||
|
72 | ||||
|
73 | # try to catch also methods for stuff in lists/tuples/dicts: off | |||
|
74 | # (experimental). For this to work, the line_split regexp would need | |||
|
75 | # to be modified so it wouldn't break things at '['. That line is | |||
|
76 | # nasty enough that I shouldn't change it until I can test it _well_. | |||
|
77 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') | |||
|
78 | ||||
|
79 | ||||
|
80 | # Handler Check Utilities | |||
|
81 | def is_shadowed(identifier, ip): | |||
|
82 | """Is the given identifier defined in one of the namespaces which shadow | |||
|
83 | the alias and magic namespaces? Note that an identifier is different | |||
|
84 | than ifun, because it can not contain a '.' character.""" | |||
|
85 | # This is much safer than calling ofind, which can change state | |||
|
86 | return (identifier in ip.user_ns \ | |||
|
87 | or identifier in ip.internal_ns \ | |||
|
88 | or identifier in ip.ns_table['builtin']) | |||
|
89 | ||||
|
90 | ||||
|
91 | #----------------------------------------------------------------------------- | |||
|
92 | # The LineInfo class used throughout | |||
|
93 | #----------------------------------------------------------------------------- | |||
12 |
|
94 | |||
13 |
|
95 | |||
14 | class LineInfo(object): |
|
96 | class LineInfo(object): | |
15 | """A single line of input and associated info. |
|
97 | """A single line of input and associated info. | |
16 |
|
98 | |||
17 | Includes the following as properties: |
|
99 | Includes the following as properties: | |
18 |
|
100 | |||
19 | line |
|
101 | line | |
20 | The original, raw line |
|
102 | The original, raw line | |
21 |
|
103 | |||
22 | continue_prompt |
|
104 | continue_prompt | |
23 | Is this line a continuation in a sequence of multiline input? |
|
105 | Is this line a continuation in a sequence of multiline input? | |
24 |
|
106 | |||
25 | pre |
|
107 | pre | |
26 | The initial esc character or whitespace. |
|
108 | The initial esc character or whitespace. | |
27 |
|
109 | |||
28 |
pre |
|
110 | pre_char | |
29 | The escape character(s) in pre or the empty string if there isn't one. |
|
111 | The escape character(s) in pre or the empty string if there isn't one. | |
30 |
Note that '!!' is a possible value for pre |
|
112 | Note that '!!' is a possible value for pre_char. Otherwise it will | |
31 | always be a single character. |
|
113 | always be a single character. | |
32 |
|
114 | |||
33 |
pre |
|
115 | pre_whitespace | |
34 |
The leading whitespace from pre if it exists. If there is a pre |
|
116 | The leading whitespace from pre if it exists. If there is a pre_char, | |
35 | this is just ''. |
|
117 | this is just ''. | |
36 |
|
118 | |||
37 |
i |
|
119 | ifun | |
38 | The 'function part', which is basically the maximal initial sequence |
|
120 | The 'function part', which is basically the maximal initial sequence | |
39 | of valid python identifiers and the '.' character. This is what is |
|
121 | of valid python identifiers and the '.' character. This is what is | |
40 | checked for alias and magic transformations, used for auto-calling, |
|
122 | checked for alias and magic transformations, used for auto-calling, | |
41 | etc. |
|
123 | etc. | |
42 |
|
124 | |||
43 |
the |
|
125 | the_rest | |
44 | Everything else on the line. |
|
126 | Everything else on the line. | |
45 | """ |
|
127 | """ | |
46 | def __init__(self, line, continue_prompt): |
|
128 | def __init__(self, line, continue_prompt): | |
47 | self.line = line |
|
129 | self.line = line | |
48 | self.continue_prompt = continue_prompt |
|
130 | self.continue_prompt = continue_prompt | |
49 |
self.pre, self.i |
|
131 | self.pre, self.ifun, self.the_rest = split_user_input(line) | |
50 |
|
132 | |||
51 |
self.pre |
|
133 | self.pre_char = self.pre.strip() | |
52 |
if self.pre |
|
134 | if self.pre_char: | |
53 |
self.pre |
|
135 | self.pre_whitespace = '' # No whitespace allowd before esc chars | |
54 | else: |
|
136 | else: | |
55 |
self.pre |
|
137 | self.pre_whitespace = self.pre | |
56 |
|
138 | |||
57 | self._oinfo = None |
|
139 | self._oinfo = None | |
58 |
|
140 | |||
59 | def ofind(self, ip): |
|
141 | def ofind(self, ip): | |
60 |
"""Do a full, attribute-walking lookup of the i |
|
142 | """Do a full, attribute-walking lookup of the ifun in the various | |
61 | namespaces for the given IPython InteractiveShell instance. |
|
143 | namespaces for the given IPython InteractiveShell instance. | |
62 |
|
144 | |||
63 | Return a dict with keys: found,obj,ospace,ismagic |
|
145 | Return a dict with keys: found,obj,ospace,ismagic | |
64 |
|
146 | |||
65 | Note: can cause state changes because of calling getattr, but should |
|
147 | Note: can cause state changes because of calling getattr, but should | |
66 | only be run if autocall is on and if the line hasn't matched any |
|
148 | only be run if autocall is on and if the line hasn't matched any | |
67 | other, less dangerous handlers. |
|
149 | other, less dangerous handlers. | |
68 |
|
150 | |||
69 | Does cache the results of the call, so can be called multiple times |
|
151 | Does cache the results of the call, so can be called multiple times | |
70 | without worrying about *further* damaging state. |
|
152 | without worrying about *further* damaging state. | |
71 | """ |
|
153 | """ | |
72 | if not self._oinfo: |
|
154 | if not self._oinfo: | |
73 |
self._oinfo = ip._ofind(self.i |
|
155 | self._oinfo = ip._ofind(self.ifun) | |
74 | return self._oinfo |
|
156 | return self._oinfo | |
|
157 | ||||
75 | def __str__(self): |
|
158 | def __str__(self): | |
76 |
return "Lineinfo [%s|%s|%s]" %(self.pre,self.i |
|
159 | return "Lineinfo [%s|%s|%s]" %(self.pre,self.ifun,self.the_rest) | |
|
160 | ||||
77 |
|
161 | |||
78 | def splitUserInput(line, pattern=None): |
|
162 | #----------------------------------------------------------------------------- | |
79 | """Split user input into pre-char/whitespace, function part and rest. |
|
163 | # Main Prefilter manager | |
|
164 | #----------------------------------------------------------------------------- | |||
80 |
|
165 | |||
81 | Mostly internal to this module, but also used by iplib.expand_aliases, |
|
166 | ||
82 | which passes in a shell pattern. |
|
167 | class PrefilterManager(Component): | |
|
168 | """Main prefilter component. | |||
|
169 | ||||
|
170 | The IPython prefilter is run on all user input before it is run. The | |||
|
171 | prefilter consumes lines of input and produces transformed lines of | |||
|
172 | input. The implementation consists of checkers and handlers. The | |||
|
173 | checkers inspect the input line and select which handler will be used | |||
|
174 | to transform the input line. | |||
83 | """ |
|
175 | """ | |
84 | # It seems to me that the shell splitting should be a separate method. |
|
176 | ||
85 |
|
177 | multi_line_specials = CBool(True, config_key='MULTI_LINE_SPECIALS') | ||
86 | if not pattern: |
|
178 | ||
87 | pattern = line_split |
|
179 | def __init__(self, parent, config=None): | |
88 | match = pattern.match(line) |
|
180 | super(PrefilterManager, self).__init__(parent, config=config) | |
89 | if not match: |
|
181 | self.init_handlers() | |
90 | #print "match failed for line '%s'" % line |
|
182 | self.init_checkers() | |
|
183 | ||||
|
184 | @auto_attr | |||
|
185 | def shell(self): | |||
|
186 | shell = Component.get_instances( | |||
|
187 | root=self.root, | |||
|
188 | klass='IPython.core.iplib.InteractiveShell' | |||
|
189 | )[0] | |||
|
190 | return shell | |||
|
191 | ||||
|
192 | def init_checkers(self): | |||
|
193 | self._checkers = [] | |||
|
194 | for checker in _default_checkers: | |||
|
195 | self._checkers.append(checker(self, config=self.config)) | |||
|
196 | ||||
|
197 | def init_handlers(self): | |||
|
198 | self._handlers = {} | |||
|
199 | self._esc_handlers = {} | |||
|
200 | for handler in _default_handlers: | |||
|
201 | handler(self, config=self.config) | |||
|
202 | ||||
|
203 | @property | |||
|
204 | def sorted_checkers(self): | |||
|
205 | """Return a list of checkers, sorted by priority.""" | |||
|
206 | return sorted(self._checkers, cmp=lambda x,y: x.priority-y.priority) | |||
|
207 | ||||
|
208 | def register_handler(self, name, handler, esc_strings): | |||
|
209 | """Register a handler instance by name with esc_strings.""" | |||
|
210 | self._handlers[name] = handler | |||
|
211 | for esc_str in esc_strings: | |||
|
212 | self._esc_handlers[esc_str] = handler | |||
|
213 | ||||
|
214 | def unregister_handler(self, name, handler, esc_strings): | |||
|
215 | """Unregister a handler instance by name with esc_strings.""" | |||
91 | try: |
|
216 | try: | |
92 | iFun,theRest = line.split(None,1) |
|
217 | del self._handlers[name] | |
93 |
except |
|
218 | except KeyError: | |
94 | #print "split failed for line '%s'" % line |
|
219 | pass | |
95 | iFun,theRest = line,'' |
|
220 | for esc_str in esc_strings: | |
96 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
221 | h = self._esc_handlers.get(esc_str) | |
97 | else: |
|
222 | if h is handler: | |
98 | pre,iFun,theRest = match.groups() |
|
223 | del self._esc_handlers[esc_str] | |
99 |
|
224 | |||
100 | # iFun has to be a valid python identifier, so it better be only pure |
|
225 | def get_handler_by_name(self, name): | |
101 | # ascii, no unicode: |
|
226 | """Get a handler by its name.""" | |
102 | try: |
|
227 | return self._handlers.get(name) | |
103 | iFun = iFun.encode('ascii') |
|
228 | ||
104 | except UnicodeEncodeError: |
|
229 | def get_handler_by_esc(self, esc_str): | |
105 | theRest = iFun + u' ' + theRest |
|
230 | """Get a handler by its escape string.""" | |
106 | iFun = u'' |
|
231 | return self._esc_handlers.get(esc_str) | |
107 |
|
232 | |||
108 | #print 'line:<%s>' % line # dbg |
|
233 | def prefilter_line_info(self, line_info): | |
109 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
234 | """Prefilter a line that has been converted to a LineInfo object.""" | |
110 | return pre,iFun.strip(),theRest.lstrip() |
|
235 | handler = self.find_handler(line_info) | |
111 |
|
236 | return handler.handle(line_info) | ||
112 |
|
237 | |||
113 | # RegExp for splitting line contents into pre-char//first word-method//rest. |
|
238 | def find_handler(self, line_info): | |
114 | # For clarity, each group in on one line. |
|
239 | """Find a handler for the line_info by trying checkers.""" | |
115 |
|
240 | for checker in self.sorted_checkers: | ||
116 | # WARNING: update the regexp if the escapes in iplib are changed, as they |
|
241 | handler = checker.check(line_info) | |
117 | # are hardwired in. |
|
242 | if handler: | |
118 |
|
243 | return handler | ||
119 | # Although it's not solely driven by the regex, note that: |
|
244 | return self.get_handler_by_name('normal') | |
120 | # ,;/% only trigger if they are the first character on the line |
|
245 | ||
121 | # ! and !! trigger if they are first char(s) *or* follow an indent |
|
246 | def prefilter_line(self, line, continue_prompt): | |
122 | # ? triggers as first or last char. |
|
247 | """Prefilter a single input line as text.""" | |
123 |
|
248 | |||
124 | # The three parts of the regex are: |
|
249 | # All handlers *must* return a value, even if it's blank (''). | |
125 | # 1) pre: pre_char *or* initial whitespace |
|
250 | ||
126 | # 2) iFun: first word/method (mix of \w and '.') |
|
251 | # Lines are NOT logged here. Handlers should process the line as | |
127 | # 3) theRest: rest of line (separated from iFun by space if non-empty) |
|
252 | # needed, update the cache AND log it (so that the input cache array | |
128 | line_split = re.compile(r'^([,;/%?]|!!?|\s*)' |
|
253 | # stays synced). | |
129 | r'\s*([\w\.]+)' |
|
254 | ||
130 | r'(\s+.*$|$)') |
|
255 | # growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt)) | |
131 |
|
256 | |||
132 | shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)') |
|
257 | # save the line away in case we crash, so the post-mortem handler can | |
133 |
|
258 | # record it | ||
134 | def prefilter(line_info, ip): |
|
259 | self.shell._last_input_line = line | |
135 | """Call one of the passed-in InteractiveShell's handler preprocessors, |
|
260 | ||
136 | depending on the form of the line. Return the results, which must be a |
|
261 | if not line: | |
137 | value, even if it's a blank ('').""" |
|
262 | # Return immediately on purely empty lines, so that if the user | |
138 | # Note: the order of these checks does matter. |
|
263 | # previously typed some whitespace that started a continuation | |
139 | for check in [ checkEmacs, |
|
264 | # prompt, he can break out of that loop with just an empty line. | |
140 | checkShellEscape, |
|
265 | # This is how the default python prompt works. | |
141 | checkIPyAutocall, |
|
266 | ||
142 | checkMultiLineMagic, |
|
267 | # Only return if the accumulated input buffer was just whitespace! | |
143 | checkEscChars, |
|
268 | if ''.join(self.shell.buffer).isspace(): | |
144 | checkAssignment, |
|
269 | self.shell.buffer[:] = [] | |
145 | checkAutomagic, |
|
270 | return '' | |
146 | checkAlias, |
|
271 | ||
147 | checkPythonOps, |
|
272 | line_info = LineInfo(line, continue_prompt) | |
148 | checkAutocall, |
|
273 | ||
149 | ]: |
|
274 | # the input history needs to track even empty lines | |
150 | handler = check(line_info, ip) |
|
275 | stripped = line.strip() | |
151 | if handler: |
|
|||
152 | return handler(line_info) |
|
|||
153 |
|
||||
154 | return ip.handle_normal(line_info) |
|
|||
155 |
|
||||
156 | # Handler checks |
|
|||
157 | # |
|
|||
158 | # All have the same interface: they take a LineInfo object and a ref to the |
|
|||
159 | # iplib.InteractiveShell object. They check the line to see if a particular |
|
|||
160 | # handler should be called, and return either a handler or None. The |
|
|||
161 | # handlers which they return are *bound* methods of the InteractiveShell |
|
|||
162 | # object. |
|
|||
163 | # |
|
|||
164 | # In general, these checks should only take responsibility for their 'own' |
|
|||
165 | # handler. If it doesn't get triggered, they should just return None and |
|
|||
166 | # let the rest of the check sequence run. |
|
|||
167 |
|
||||
168 | def checkShellEscape(l_info,ip): |
|
|||
169 | if l_info.line.lstrip().startswith(ip.ESC_SHELL): |
|
|||
170 | return ip.handle_shell_escape |
|
|||
171 |
|
||||
172 | def checkEmacs(l_info,ip): |
|
|||
173 | "Emacs ipython-mode tags certain input lines." |
|
|||
174 | if l_info.line.endswith('# PYTHON-MODE'): |
|
|||
175 | return ip.handle_emacs |
|
|||
176 | else: |
|
|||
177 | return None |
|
|||
178 |
|
276 | |||
179 | def checkIPyAutocall(l_info,ip): |
|
277 | handle_normal = self.get_handler_by_name('normal') | |
180 | "Instances of IPyAutocall in user_ns get autocalled immediately" |
|
278 | if not stripped: | |
181 | obj = ip.user_ns.get(l_info.iFun, None) |
|
279 | if not continue_prompt: | |
182 | if isinstance(obj, IPyAutocall): |
|
280 | self.shell.outputcache.prompt_count -= 1 | |
183 | obj.set_ip(ip) |
|
|||
184 | return ip.handle_auto |
|
|||
185 | else: |
|
|||
186 | return None |
|
|||
187 |
|
||||
188 |
|
||||
189 | def checkMultiLineMagic(l_info,ip): |
|
|||
190 | "Allow ! and !! in multi-line statements if multi_line_specials is on" |
|
|||
191 | # Note that this one of the only places we check the first character of |
|
|||
192 | # iFun and *not* the preChar. Also note that the below test matches |
|
|||
193 | # both ! and !!. |
|
|||
194 | if l_info.continue_prompt \ |
|
|||
195 | and ip.multi_line_specials: |
|
|||
196 | if l_info.iFun.startswith(ip.ESC_MAGIC): |
|
|||
197 | return ip.handle_magic |
|
|||
198 | else: |
|
|||
199 | return None |
|
|||
200 |
|
281 | |||
201 | def checkEscChars(l_info,ip): |
|
282 | return handle_normal(line_info) | |
202 | """Check for escape character and return either a handler to handle it, |
|
|||
203 | or None if there is no escape char.""" |
|
|||
204 | if l_info.line[-1] == ip.ESC_HELP \ |
|
|||
205 | and l_info.preChar != ip.ESC_SHELL \ |
|
|||
206 | and l_info.preChar != ip.ESC_SH_CAP: |
|
|||
207 | # the ? can be at the end, but *not* for either kind of shell escape, |
|
|||
208 | # because a ? can be a vaild final char in a shell cmd |
|
|||
209 | return ip.handle_help |
|
|||
210 | elif l_info.preChar in ip.esc_handlers: |
|
|||
211 | return ip.esc_handlers[l_info.preChar] |
|
|||
212 | else: |
|
|||
213 | return None |
|
|||
214 |
|
283 | |||
|
284 | # special handlers are only allowed for single line statements | |||
|
285 | if continue_prompt and not self.multi_line_specials: | |||
|
286 | return handle_normal(line_info) | |||
215 |
|
287 | |||
216 | def checkAssignment(l_info,ip): |
|
288 | return self.prefilter_line_info(line_info) | |
217 | """Check to see if user is assigning to a var for the first time, in |
|
|||
218 | which case we want to avoid any sort of automagic / autocall games. |
|
|||
219 |
|
||||
220 | This allows users to assign to either alias or magic names true python |
|
|||
221 | variables (the magic/alias systems always take second seat to true |
|
|||
222 | python code). E.g. ls='hi', or ls,that=1,2""" |
|
|||
223 | if l_info.theRest and l_info.theRest[0] in '=,': |
|
|||
224 | return ip.handle_normal |
|
|||
225 | else: |
|
|||
226 | return None |
|
|||
227 |
|
289 | |||
|
290 | def prefilter_lines(self, lines, continue_prompt): | |||
|
291 | """Prefilter multiple input lines of text. | |||
228 |
|
|
292 | ||
229 | def checkAutomagic(l_info,ip): |
|
293 | Covers cases where there are multiple lines in the user entry, | |
230 | """If the iFun is magic, and automagic is on, run it. Note: normal, |
|
294 | which is the case when the user goes back to a multiline history | |
231 | non-auto magic would already have been triggered via '%' in |
|
295 | entry and presses enter. | |
232 | check_esc_chars. This just checks for automagic. Also, before |
|
296 | """ | |
233 | triggering the magic handler, make sure that there is nothing in the |
|
297 | # growl.notify("multiline_prefilter: ", "%s\n%s" % (line, continue_prompt)) | |
234 | user namespace which could shadow it.""" |
|
298 | out = [] | |
235 | if not ip.automagic or not hasattr(ip,'magic_'+l_info.iFun): |
|
299 | for line in lines.rstrip('\n').split('\n'): | |
236 | return None |
|
300 | out.append(self.prefilter_line(line, continue_prompt)) | |
|
301 | # growl.notify("multiline_prefilter return: ", '\n'.join(out)) | |||
|
302 | return '\n'.join(out) | |||
237 |
|
303 | |||
238 | # We have a likely magic method. Make sure we should actually call it. |
|
|||
239 | if l_info.continue_prompt and not ip.multi_line_specials: |
|
|||
240 | return None |
|
|||
241 |
|
304 | |||
242 | head = l_info.iFun.split('.',1)[0] |
|
305 | #----------------------------------------------------------------------------- | |
243 | if isShadowed(head,ip): |
|
306 | # Prefilter checkers | |
244 | return None |
|
307 | #----------------------------------------------------------------------------- | |
245 |
|
308 | |||
246 | return ip.handle_magic |
|
|||
247 |
|
309 | |||
248 |
|
310 | class PrefilterChecker(Component): | ||
249 | def checkAlias(l_info,ip): |
|
311 | """Inspect an input line and return a handler for that line.""" | |
250 | "Check if the initital identifier on the line is an alias." |
|
|||
251 | # Note: aliases can not contain '.' |
|
|||
252 | head = l_info.iFun.split('.',1)[0] |
|
|||
253 |
|
||||
254 | if l_info.iFun not in ip.alias_manager \ |
|
|||
255 | or head not in ip.alias_manager \ |
|
|||
256 | or isShadowed(head,ip): |
|
|||
257 | return None |
|
|||
258 |
|
312 | |||
259 | return ip.handle_alias |
|
313 | priority = Int(100) | |
|
314 | shell = Any | |||
|
315 | prefilter_manager = Any | |||
260 |
|
316 | |||
|
317 | def __init__(self, parent, config=None): | |||
|
318 | super(PrefilterChecker, self).__init__(parent, config=config) | |||
261 |
|
319 | |||
262 | def checkPythonOps(l_info,ip): |
|
320 | @auto_attr | |
263 | """If the 'rest' of the line begins with a function call or pretty much |
|
321 | def shell(self): | |
264 | any python operator, we should simply execute the line (regardless of |
|
322 | shell = Component.get_instances( | |
265 | whether or not there's a possible autocall expansion). This avoids |
|
323 | root=self.root, | |
266 | spurious (and very confusing) geattr() accesses.""" |
|
324 | klass='IPython.core.iplib.InteractiveShell' | |
267 | if l_info.theRest and l_info.theRest[0] in '!=()<>,+*/%^&|': |
|
325 | )[0] | |
268 |
return |
|
326 | return shell | |
269 | else: |
|
|||
270 | return None |
|
|||
271 |
|
327 | |||
|
328 | @auto_attr | |||
|
329 | def prefilter_manager(self): | |||
|
330 | return PrefilterManager.get_instances(root=self.root)[0] | |||
272 |
|
331 | |||
273 |
def check |
|
332 | def check(self, line_info): | |
274 | "Check if the initial word/function is callable and autocall is on." |
|
333 | """Inspect line_info and return a handler or None.""" | |
275 | if not ip.autocall: |
|
|||
276 | return None |
|
334 | return None | |
277 |
|
335 | |||
278 | oinfo = l_info.ofind(ip) # This can mutate state via getattr |
|
336 | ||
279 | if not oinfo['found']: |
|
337 | class EmacsChecker(PrefilterChecker): | |
280 | return None |
|
338 | ||
281 |
|
339 | priority = Int(100) | ||
282 | if callable(oinfo['obj']) \ |
|
340 | ||
283 | and (not re_exclude_auto.match(l_info.theRest)) \ |
|
341 | def check(self, line_info): | |
284 | and re_fun_name.match(l_info.iFun): |
|
342 | "Emacs ipython-mode tags certain input lines." | |
285 | #print 'going auto' # dbg |
|
343 | if line_info.line.endswith('# PYTHON-MODE'): | |
286 | return ip.handle_auto |
|
344 | return self.prefilter_manager.get_handler_by_name('emacs') | |
287 | else: |
|
345 | else: | |
288 | #print 'was callable?', callable(l_info.oinfo['obj']) # dbg |
|
346 | return None | |
289 | return None |
|
347 | ||
|
348 | ||||
|
349 | class ShellEscapeChecker(PrefilterChecker): | |||
|
350 | ||||
|
351 | priority = Int(200) | |||
|
352 | ||||
|
353 | def check(self, line_info): | |||
|
354 | if line_info.line.lstrip().startswith(ESC_SHELL): | |||
|
355 | return self.prefilter_manager.get_handler_by_name('shell') | |||
|
356 | ||||
|
357 | ||||
|
358 | class IPyAutocallChecker(PrefilterChecker): | |||
|
359 | ||||
|
360 | priority = Int(300) | |||
|
361 | ||||
|
362 | def check(self, line_info): | |||
|
363 | "Instances of IPyAutocall in user_ns get autocalled immediately" | |||
|
364 | obj = self.shell.user_ns.get(line_info.ifun, None) | |||
|
365 | if isinstance(obj, IPyAutocall): | |||
|
366 | obj.set_ip(self.shell) | |||
|
367 | return self.prefilter_manager.get_handler_by_name('auto') | |||
|
368 | else: | |||
|
369 | return None | |||
|
370 | ||||
|
371 | ||||
|
372 | class MultiLineMagicChecker(PrefilterChecker): | |||
|
373 | ||||
|
374 | priority = Int(400) | |||
|
375 | ||||
|
376 | def check(self, line_info): | |||
|
377 | "Allow ! and !! in multi-line statements if multi_line_specials is on" | |||
|
378 | # Note that this one of the only places we check the first character of | |||
|
379 | # ifun and *not* the pre_char. Also note that the below test matches | |||
|
380 | # both ! and !!. | |||
|
381 | if line_info.continue_prompt \ | |||
|
382 | and self.prefilter_manager.multi_line_specials: | |||
|
383 | if line_info.ifun.startswith(ESC_MAGIC): | |||
|
384 | return self.prefilter_manager.get_handler_by_name('magic') | |||
|
385 | else: | |||
|
386 | return None | |||
|
387 | ||||
|
388 | ||||
|
389 | class EscCharsChecker(PrefilterChecker): | |||
|
390 | ||||
|
391 | priority = Int(500) | |||
|
392 | ||||
|
393 | def check(self, line_info): | |||
|
394 | """Check for escape character and return either a handler to handle it, | |||
|
395 | or None if there is no escape char.""" | |||
|
396 | if line_info.line[-1] == ESC_HELP \ | |||
|
397 | and line_info.pre_char != ESC_SHELL \ | |||
|
398 | and line_info.pre_char != ESC_SH_CAP: | |||
|
399 | # the ? can be at the end, but *not* for either kind of shell escape, | |||
|
400 | # because a ? can be a vaild final char in a shell cmd | |||
|
401 | return self.prefilter_manager.get_handler_by_name('help') | |||
|
402 | else: | |||
|
403 | # This returns None like it should if no handler exists | |||
|
404 | return self.prefilter_manager.get_handler_by_esc(line_info.pre_char) | |||
|
405 | ||||
|
406 | ||||
|
407 | class AssignmentChecker(PrefilterChecker): | |||
|
408 | ||||
|
409 | priority = Int(600) | |||
|
410 | ||||
|
411 | def check(self, line_info): | |||
|
412 | """Check to see if user is assigning to a var for the first time, in | |||
|
413 | which case we want to avoid any sort of automagic / autocall games. | |||
290 |
|
|
414 | ||
291 | # RegExp to identify potential function names |
|
415 | This allows users to assign to either alias or magic names true python | |
292 | re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
416 | variables (the magic/alias systems always take second seat to true | |
|
417 | python code). E.g. ls='hi', or ls,that=1,2""" | |||
|
418 | if line_info.the_rest and line_info.the_rest[0] in '=,': | |||
|
419 | return self.prefilter_manager.get_handler_by_name('normal') | |||
|
420 | else: | |||
|
421 | return None | |||
293 |
|
422 | |||
294 | # RegExp to exclude strings with this start from autocalling. In |
|
|||
295 | # particular, all binary operators should be excluded, so that if foo is |
|
|||
296 | # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The |
|
|||
297 | # characters '!=()' don't need to be checked for, as the checkPythonChars |
|
|||
298 | # routine explicitely does so, to catch direct calls and rebindings of |
|
|||
299 | # existing names. |
|
|||
300 |
|
423 | |||
301 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise |
|
424 | class AutoMagicChecker(PrefilterChecker): | |
302 | # it affects the rest of the group in square brackets. |
|
|||
303 | re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]' |
|
|||
304 | r'|^is |^not |^in |^and |^or ') |
|
|||
305 |
|
425 | |||
306 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
426 | priority = Int(700) | |
307 | # (experimental). For this to work, the line_split regexp would need |
|
|||
308 | # to be modified so it wouldn't break things at '['. That line is |
|
|||
309 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
|||
310 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
|||
311 |
|
427 | |||
312 | # Handler Check Utilities |
|
428 | def check(self, line_info): | |
313 | def isShadowed(identifier,ip): |
|
429 | """If the ifun is magic, and automagic is on, run it. Note: normal, | |
314 | """Is the given identifier defined in one of the namespaces which shadow |
|
430 | non-auto magic would already have been triggered via '%' in | |
315 | the alias and magic namespaces? Note that an identifier is different |
|
431 | check_esc_chars. This just checks for automagic. Also, before | |
316 | than iFun, because it can not contain a '.' character.""" |
|
432 | triggering the magic handler, make sure that there is nothing in the | |
317 | # This is much safer than calling ofind, which can change state |
|
433 | user namespace which could shadow it.""" | |
318 | return (identifier in ip.user_ns \ |
|
434 | if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun): | |
319 | or identifier in ip.internal_ns \ |
|
435 | return None | |
320 | or identifier in ip.ns_table['builtin']) |
|
436 | ||
|
437 | # We have a likely magic method. Make sure we should actually call it. | |||
|
438 | if line_info.continue_prompt and not self.shell.multi_line_specials: | |||
|
439 | return None | |||
|
440 | ||||
|
441 | head = line_info.ifun.split('.',1)[0] | |||
|
442 | if is_shadowed(head, self.shell): | |||
|
443 | return None | |||
|
444 | ||||
|
445 | return self.prefilter_manager.get_handler_by_name('magic') | |||
|
446 | ||||
|
447 | ||||
|
448 | class AliasChecker(PrefilterChecker): | |||
|
449 | ||||
|
450 | priority = Int(800) | |||
|
451 | ||||
|
452 | @auto_attr | |||
|
453 | def alias_manager(self): | |||
|
454 | return AliasManager.get_instances(root=self.root)[0] | |||
|
455 | ||||
|
456 | def check(self, line_info): | |||
|
457 | "Check if the initital identifier on the line is an alias." | |||
|
458 | # Note: aliases can not contain '.' | |||
|
459 | head = line_info.ifun.split('.',1)[0] | |||
|
460 | if line_info.ifun not in self.alias_manager \ | |||
|
461 | or head not in self.alias_manager \ | |||
|
462 | or is_shadowed(head, self.shell): | |||
|
463 | return None | |||
|
464 | ||||
|
465 | return self.prefilter_manager.get_handler_by_name('alias') | |||
|
466 | ||||
|
467 | ||||
|
468 | class PythonOpsChecker(PrefilterChecker): | |||
|
469 | ||||
|
470 | priority = Int(900) | |||
|
471 | ||||
|
472 | def check(self, line_info): | |||
|
473 | """If the 'rest' of the line begins with a function call or pretty much | |||
|
474 | any python operator, we should simply execute the line (regardless of | |||
|
475 | whether or not there's a possible autocall expansion). This avoids | |||
|
476 | spurious (and very confusing) geattr() accesses.""" | |||
|
477 | if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|': | |||
|
478 | return self.prefilter_manager.get_handler_by_name('normal') | |||
|
479 | else: | |||
|
480 | return None | |||
|
481 | ||||
|
482 | ||||
|
483 | class AutocallChecker(PrefilterChecker): | |||
|
484 | ||||
|
485 | priority = Int(1000) | |||
|
486 | ||||
|
487 | def check(self, line_info): | |||
|
488 | "Check if the initial word/function is callable and autocall is on." | |||
|
489 | if not self.shell.autocall: | |||
|
490 | return None | |||
|
491 | ||||
|
492 | oinfo = line_info.ofind(self.shell) # This can mutate state via getattr | |||
|
493 | if not oinfo['found']: | |||
|
494 | return None | |||
|
495 | ||||
|
496 | if callable(oinfo['obj']) \ | |||
|
497 | and (not re_exclude_auto.match(line_info.the_rest)) \ | |||
|
498 | and re_fun_name.match(line_info.ifun): | |||
|
499 | return self.prefilter_manager.get_handler_by_name('auto') | |||
|
500 | else: | |||
|
501 | return None | |||
|
502 | ||||
|
503 | ||||
|
504 | #----------------------------------------------------------------------------- | |||
|
505 | # Prefilter handlers | |||
|
506 | #----------------------------------------------------------------------------- | |||
|
507 | ||||
|
508 | ||||
|
509 | class PrefilterHandler(Component): | |||
|
510 | ||||
|
511 | handler_name = Str('normal') | |||
|
512 | esc_strings = List([]) | |||
|
513 | shell = Any | |||
|
514 | prefilter_manager = Any | |||
|
515 | ||||
|
516 | def __init__(self, parent, config=None): | |||
|
517 | super(PrefilterHandler, self).__init__(parent, config=config) | |||
|
518 | self.prefilter_manager.register_handler( | |||
|
519 | self.handler_name, | |||
|
520 | self, | |||
|
521 | self.esc_strings | |||
|
522 | ) | |||
|
523 | ||||
|
524 | @auto_attr | |||
|
525 | def shell(self): | |||
|
526 | shell = Component.get_instances( | |||
|
527 | root=self.root, | |||
|
528 | klass='IPython.core.iplib.InteractiveShell' | |||
|
529 | )[0] | |||
|
530 | return shell | |||
|
531 | ||||
|
532 | @auto_attr | |||
|
533 | def prefilter_manager(self): | |||
|
534 | return PrefilterManager.get_instances(root=self.root)[0] | |||
|
535 | ||||
|
536 | def handle(self, line_info): | |||
|
537 | """Handle normal input lines. Use as a template for handlers.""" | |||
|
538 | ||||
|
539 | # With autoindent on, we need some way to exit the input loop, and I | |||
|
540 | # don't want to force the user to have to backspace all the way to | |||
|
541 | # clear the line. The rule will be in this case, that either two | |||
|
542 | # lines of pure whitespace in a row, or a line of pure whitespace but | |||
|
543 | # of a size different to the indent level, will exit the input loop. | |||
|
544 | line = line_info.line | |||
|
545 | continue_prompt = line_info.continue_prompt | |||
|
546 | ||||
|
547 | if (continue_prompt and self.shell.autoindent and line.isspace() and | |||
|
548 | (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2 or | |||
|
549 | (self.shell.buffer[-1]).isspace() )): | |||
|
550 | line = '' | |||
|
551 | ||||
|
552 | self.shell.log(line, line, continue_prompt) | |||
|
553 | return line | |||
|
554 | ||||
|
555 | ||||
|
556 | class AliasHandler(PrefilterHandler): | |||
|
557 | ||||
|
558 | handler_name = Str('alias') | |||
|
559 | esc_strings = List([]) | |||
|
560 | ||||
|
561 | @auto_attr | |||
|
562 | def alias_manager(self): | |||
|
563 | return AliasManager.get_instances(root=self.root)[0] | |||
|
564 | ||||
|
565 | def handle(self, line_info): | |||
|
566 | """Handle alias input lines. """ | |||
|
567 | transformed = self.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest) | |||
|
568 | # pre is needed, because it carries the leading whitespace. Otherwise | |||
|
569 | # aliases won't work in indented sections. | |||
|
570 | line_out = '%s_ip.system(%s)' % (line_info.pre_whitespace, | |||
|
571 | make_quoted_expr(transformed)) | |||
|
572 | ||||
|
573 | self.shell.log(line_info.line, line_out, line_info.continue_prompt) | |||
|
574 | return line_out | |||
|
575 | ||||
|
576 | ||||
|
577 | class ShellEscapeHandler(PrefilterHandler): | |||
|
578 | ||||
|
579 | handler_name = Str('shell') | |||
|
580 | esc_strings = List([ESC_SHELL, ESC_SH_CAP]) | |||
|
581 | ||||
|
582 | def handle(self, line_info): | |||
|
583 | """Execute the line in a shell, empty return value""" | |||
|
584 | magic_handler = self.prefilter_manager.get_handler_by_name('magic') | |||
|
585 | ||||
|
586 | line = line_info.line | |||
|
587 | if line.lstrip().startswith(ESC_SH_CAP): | |||
|
588 | # rewrite LineInfo's line, ifun and the_rest to properly hold the | |||
|
589 | # call to %sx and the actual command to be executed, so | |||
|
590 | # handle_magic can work correctly. Note that this works even if | |||
|
591 | # the line is indented, so it handles multi_line_specials | |||
|
592 | # properly. | |||
|
593 | new_rest = line.lstrip()[2:] | |||
|
594 | line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest) | |||
|
595 | line_info.ifun = 'sx' | |||
|
596 | line_info.the_rest = new_rest | |||
|
597 | return magic_handler.handle(line_info) | |||
|
598 | else: | |||
|
599 | cmd = line.lstrip().lstrip(ESC_SHELL) | |||
|
600 | line_out = '%s_ip.system(%s)' % (line_info.pre_whitespace, | |||
|
601 | make_quoted_expr(cmd)) | |||
|
602 | # update cache/log and return | |||
|
603 | self.shell.log(line, line_out, line_info.continue_prompt) | |||
|
604 | return line_out | |||
|
605 | ||||
|
606 | ||||
|
607 | class MagicHandler(PrefilterHandler): | |||
|
608 | ||||
|
609 | handler_name = Str('magic') | |||
|
610 | esc_strings = List(['%']) | |||
|
611 | ||||
|
612 | def handle(self, line_info): | |||
|
613 | """Execute magic functions.""" | |||
|
614 | ifun = line_info.ifun | |||
|
615 | the_rest = line_info.the_rest | |||
|
616 | cmd = '%s_ip.magic(%s)' % (line_info.pre_whitespace, | |||
|
617 | make_quoted_expr(ifun + " " + the_rest)) | |||
|
618 | self.shell.log(line_info.line, cmd, line_info.continue_prompt) | |||
|
619 | return cmd | |||
|
620 | ||||
|
621 | ||||
|
622 | class AutoHandler(PrefilterHandler): | |||
|
623 | ||||
|
624 | handler_name = Str('auto') | |||
|
625 | esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2]) | |||
|
626 | ||||
|
627 | def handle(self, line_info): | |||
|
628 | """Hande lines which can be auto-executed, quoting if requested.""" | |||
|
629 | line = line_info.line | |||
|
630 | ifun = line_info.ifun | |||
|
631 | the_rest = line_info.the_rest | |||
|
632 | pre = line_info.pre | |||
|
633 | continue_prompt = line_info.continue_prompt | |||
|
634 | obj = line_info.ofind(self)['obj'] | |||
|
635 | ||||
|
636 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg | |||
|
637 | ||||
|
638 | # This should only be active for single-line input! | |||
|
639 | if continue_prompt: | |||
|
640 | self.log(line,line,continue_prompt) | |||
|
641 | return line | |||
|
642 | ||||
|
643 | force_auto = isinstance(obj, IPyAutocall) | |||
|
644 | auto_rewrite = True | |||
|
645 | ||||
|
646 | if pre == ESC_QUOTE: | |||
|
647 | # Auto-quote splitting on whitespace | |||
|
648 | newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) ) | |||
|
649 | elif pre == ESC_QUOTE2: | |||
|
650 | # Auto-quote whole string | |||
|
651 | newcmd = '%s("%s")' % (ifun,the_rest) | |||
|
652 | elif pre == ESC_PAREN: | |||
|
653 | newcmd = '%s(%s)' % (ifun,",".join(the_rest.split())) | |||
|
654 | else: | |||
|
655 | # Auto-paren. | |||
|
656 | # We only apply it to argument-less calls if the autocall | |||
|
657 | # parameter is set to 2. We only need to check that autocall is < | |||
|
658 | # 2, since this function isn't called unless it's at least 1. | |||
|
659 | if not the_rest and (self.autocall < 2) and not force_auto: | |||
|
660 | newcmd = '%s %s' % (ifun,the_rest) | |||
|
661 | auto_rewrite = False | |||
|
662 | else: | |||
|
663 | if not force_auto and the_rest.startswith('['): | |||
|
664 | if hasattr(obj,'__getitem__'): | |||
|
665 | # Don't autocall in this case: item access for an object | |||
|
666 | # which is BOTH callable and implements __getitem__. | |||
|
667 | newcmd = '%s %s' % (ifun,the_rest) | |||
|
668 | auto_rewrite = False | |||
|
669 | else: | |||
|
670 | # if the object doesn't support [] access, go ahead and | |||
|
671 | # autocall | |||
|
672 | newcmd = '%s(%s)' % (ifun.rstrip(),the_rest) | |||
|
673 | elif the_rest.endswith(';'): | |||
|
674 | newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1]) | |||
|
675 | else: | |||
|
676 | newcmd = '%s(%s)' % (ifun.rstrip(), the_rest) | |||
|
677 | ||||
|
678 | if auto_rewrite: | |||
|
679 | rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd | |||
|
680 | ||||
|
681 | try: | |||
|
682 | # plain ascii works better w/ pyreadline, on some machines, so | |||
|
683 | # we use it and only print uncolored rewrite if we have unicode | |||
|
684 | rw = str(rw) | |||
|
685 | print >>Term.cout, rw | |||
|
686 | except UnicodeEncodeError: | |||
|
687 | print "-------------->" + newcmd | |||
|
688 | ||||
|
689 | # log what is now valid Python, not the actual user input (without the | |||
|
690 | # final newline) | |||
|
691 | self.shell.log(line,newcmd,continue_prompt) | |||
|
692 | return newcmd | |||
|
693 | ||||
|
694 | ||||
|
695 | class HelpHandler(PrefilterHandler): | |||
|
696 | ||||
|
697 | handler_name = Str('help') | |||
|
698 | esc_strings = List([ESC_HELP]) | |||
|
699 | ||||
|
700 | def handle(self, line_info): | |||
|
701 | """Try to get some help for the object. | |||
|
702 | ||||
|
703 | obj? or ?obj -> basic information. | |||
|
704 | obj?? or ??obj -> more details. | |||
|
705 | """ | |||
|
706 | normal_handler = self.prefilter_manager.get_handler_by_name('normal') | |||
|
707 | line = line_info.line | |||
|
708 | # We need to make sure that we don't process lines which would be | |||
|
709 | # otherwise valid python, such as "x=1 # what?" | |||
|
710 | try: | |||
|
711 | codeop.compile_command(line) | |||
|
712 | except SyntaxError: | |||
|
713 | # We should only handle as help stuff which is NOT valid syntax | |||
|
714 | if line[0]==ESC_HELP: | |||
|
715 | line = line[1:] | |||
|
716 | elif line[-1]==ESC_HELP: | |||
|
717 | line = line[:-1] | |||
|
718 | self.shell.log(line, '#?'+line, line_info.continue_prompt) | |||
|
719 | if line: | |||
|
720 | #print 'line:<%r>' % line # dbg | |||
|
721 | self.shell.magic_pinfo(line) | |||
|
722 | else: | |||
|
723 | page(self.shell.usage, screen_lines=self.shell.usable_screen_length) | |||
|
724 | return '' # Empty string is needed here! | |||
|
725 | except: | |||
|
726 | raise | |||
|
727 | # Pass any other exceptions through to the normal handler | |||
|
728 | return normal_handler.handle(line_info) | |||
|
729 | else: | |||
|
730 | raise | |||
|
731 | # If the code compiles ok, we should handle it normally | |||
|
732 | return normal_handler.handle(line_info) | |||
|
733 | ||||
|
734 | ||||
|
735 | class EmacsHandler(PrefilterHandler): | |||
|
736 | ||||
|
737 | handler_name = Str('emacs') | |||
|
738 | esc_strings = List([]) | |||
|
739 | ||||
|
740 | def handle(self, line_info): | |||
|
741 | """Handle input lines marked by python-mode.""" | |||
|
742 | ||||
|
743 | # Currently, nothing is done. Later more functionality can be added | |||
|
744 | # here if needed. | |||
|
745 | ||||
|
746 | # The input cache shouldn't be updated | |||
|
747 | return line_info.line | |||
|
748 | ||||
|
749 | ||||
|
750 | #----------------------------------------------------------------------------- | |||
|
751 | # Defaults | |||
|
752 | #----------------------------------------------------------------------------- | |||
|
753 | ||||
|
754 | ||||
|
755 | _default_checkers = [ | |||
|
756 | EmacsChecker, | |||
|
757 | ShellEscapeChecker, | |||
|
758 | IPyAutocallChecker, | |||
|
759 | MultiLineMagicChecker, | |||
|
760 | EscCharsChecker, | |||
|
761 | AssignmentChecker, | |||
|
762 | AutoMagicChecker, | |||
|
763 | AliasChecker, | |||
|
764 | PythonOpsChecker, | |||
|
765 | AutocallChecker | |||
|
766 | ] | |||
|
767 | ||||
|
768 | _default_handlers = [ | |||
|
769 | PrefilterHandler, | |||
|
770 | AliasHandler, | |||
|
771 | ShellEscapeHandler, | |||
|
772 | MagicHandler, | |||
|
773 | AutoHandler, | |||
|
774 | HelpHandler, | |||
|
775 | EmacsHandler | |||
|
776 | ] | |||
321 |
|
777 |
General Comments 0
You need to be logged in to leave comments.
Login now