Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,192 +1,214 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Word completion for GNU readline 2.0. |
|
2 | """Word completion for GNU readline 2.0. | |
3 |
|
3 | |||
4 | --------------------------------------------------------------------------- |
|
4 | --------------------------------------------------------------------------- | |
5 | NOTE: This version is a re-implementation of rlcompleter with selectable |
|
5 | NOTE: This version is a re-implementation of rlcompleter with selectable | |
6 | namespace. |
|
6 | namespace. | |
7 |
|
7 | |||
8 | The problem with rlcompleter is that it's hardwired to work with |
|
8 | The problem with rlcompleter is that it's hardwired to work with | |
9 | __main__.__dict__, and in some cases one may have 'sandboxed' namespaces. So |
|
9 | __main__.__dict__, and in some cases one may have 'sandboxed' namespaces. So | |
10 | this class is a ripoff of rlcompleter, with the namespace to work in as an |
|
10 | this class is a ripoff of rlcompleter, with the namespace to work in as an | |
11 | optional parameter. |
|
11 | optional parameter. | |
12 |
|
12 | |||
13 | This class can be used just like rlcompleter, but the Completer class now has |
|
13 | This class can be used just like rlcompleter, but the Completer class now has | |
14 | a constructor with the optional 'namespace' parameter. |
|
14 | a constructor with the optional 'namespace' parameter. | |
15 |
|
15 | |||
16 | A patch has been submitted to Python@sourceforge for these changes to go in |
|
16 | A patch has been submitted to Python@sourceforge for these changes to go in | |
17 | the standard Python distribution. |
|
17 | the standard Python distribution. | |
18 |
|
18 | |||
19 | The patch went in for Python 2.3. Once IPython drops support for Python 2.2, |
|
19 | The patch went in for Python 2.3. Once IPython drops support for Python 2.2, | |
20 | this file can be significantly reduced. |
|
20 | this file can be significantly reduced. | |
21 | --------------------------------------------------------------------------- |
|
21 | --------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | Original rlcompleter documentation: |
|
23 | Original rlcompleter documentation: | |
24 |
|
24 | |||
25 | This requires the latest extension to the readline module (the |
|
25 | This requires the latest extension to the readline module (the | |
26 | completes keywords, built-ins and globals in __main__; when completing |
|
26 | completes keywords, built-ins and globals in __main__; when completing | |
27 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
27 | NAME.NAME..., it evaluates (!) the expression up to the last dot and | |
28 | completes its attributes. |
|
28 | completes its attributes. | |
29 |
|
29 | |||
30 | It's very cool to do "import string" type "string.", hit the |
|
30 | It's very cool to do "import string" type "string.", hit the | |
31 | completion key (twice), and see the list of names defined by the |
|
31 | completion key (twice), and see the list of names defined by the | |
32 | string module! |
|
32 | string module! | |
33 |
|
33 | |||
34 | Tip: to use the tab key as the completion key, call |
|
34 | Tip: to use the tab key as the completion key, call | |
35 |
|
35 | |||
36 | readline.parse_and_bind("tab: complete") |
|
36 | readline.parse_and_bind("tab: complete") | |
37 |
|
37 | |||
38 | Notes: |
|
38 | Notes: | |
39 |
|
39 | |||
40 | - Exceptions raised by the completer function are *ignored* (and |
|
40 | - Exceptions raised by the completer function are *ignored* (and | |
41 | generally cause the completion to fail). This is a feature -- since |
|
41 | generally cause the completion to fail). This is a feature -- since | |
42 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
42 | readline sets the tty device in raw (or cbreak) mode, printing a | |
43 | traceback wouldn't work well without some complicated hoopla to save, |
|
43 | traceback wouldn't work well without some complicated hoopla to save, | |
44 | reset and restore the tty state. |
|
44 | reset and restore the tty state. | |
45 |
|
45 | |||
46 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
46 | - The evaluation of the NAME.NAME... form may cause arbitrary | |
47 | application defined code to be executed if an object with a |
|
47 | application defined code to be executed if an object with a | |
48 | __getattr__ hook is found. Since it is the responsibility of the |
|
48 | __getattr__ hook is found. Since it is the responsibility of the | |
49 | application (or the user) to enable this feature, I consider this an |
|
49 | application (or the user) to enable this feature, I consider this an | |
50 | acceptable risk. More complicated expressions (e.g. function calls or |
|
50 | acceptable risk. More complicated expressions (e.g. function calls or | |
51 | indexing operations) are *not* evaluated. |
|
51 | indexing operations) are *not* evaluated. | |
52 |
|
52 | |||
53 | - GNU readline is also used by the built-in functions input() and |
|
53 | - GNU readline is also used by the built-in functions input() and | |
54 | raw_input(), and thus these also benefit/suffer from the completer |
|
54 | raw_input(), and thus these also benefit/suffer from the completer | |
55 | features. Clearly an interactive application can benefit by |
|
55 | features. Clearly an interactive application can benefit by | |
56 | specifying its own completer function and using raw_input() for all |
|
56 | specifying its own completer function and using raw_input() for all | |
57 | its input. |
|
57 | its input. | |
58 |
|
58 | |||
59 | - When the original stdin is not a tty device, GNU readline is never |
|
59 | - When the original stdin is not a tty device, GNU readline is never | |
60 | used, and this module (and the readline module) are silently inactive. |
|
60 | used, and this module (and the readline module) are silently inactive. | |
61 |
|
61 | |||
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | #***************************************************************************** |
|
64 | #***************************************************************************** | |
65 | # |
|
65 | # | |
66 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
66 | # Since this file is essentially a minimally modified copy of the rlcompleter | |
67 | # module which is part of the standard Python distribution, I assume that the |
|
67 | # module which is part of the standard Python distribution, I assume that the | |
68 | # proper procedure is to maintain its copyright as belonging to the Python |
|
68 | # proper procedure is to maintain its copyright as belonging to the Python | |
69 | # Software Foundation: |
|
69 | # Software Foundation: | |
70 | # |
|
70 | # | |
71 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
71 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
72 | # |
|
72 | # | |
73 | # Distributed under the terms of the Python Software Foundation license. |
|
73 | # Distributed under the terms of the Python Software Foundation license. | |
74 | # |
|
74 | # | |
75 | # Full text available at: |
|
75 | # Full text available at: | |
76 | # |
|
76 | # | |
77 | # http://www.python.org/2.1/license.html |
|
77 | # http://www.python.org/2.1/license.html | |
78 | # |
|
78 | # | |
79 | #***************************************************************************** |
|
79 | #***************************************************************************** | |
80 |
|
80 | |||
81 | import readline |
|
|||
82 | import __builtin__ |
|
81 | import __builtin__ | |
83 | import __main__ |
|
82 | import __main__ | |
|
83 | import readline | |||
|
84 | import keyword | |||
|
85 | import types | |||
84 |
|
86 | |||
85 | __all__ = ["Completer"] |
|
87 | __all__ = ["Completer"] | |
86 |
|
88 | |||
87 | class Completer: |
|
89 | class Completer: | |
88 |
def __init__(self, |
|
90 | def __init__(self,namespace=None,global_namespace=None): | |
89 | """Create a new completer for the command line. |
|
91 | """Create a new completer for the command line. | |
90 |
|
92 | |||
91 | Completer([namespace]) -> completer instance. |
|
93 | Completer([namespace,global_namespace]) -> completer instance. | |
92 |
|
94 | |||
93 | If unspecified, the default namespace where completions are performed |
|
95 | If unspecified, the default namespace where completions are performed | |
94 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
96 | is __main__ (technically, __main__.__dict__). Namespaces should be | |
95 | given as dictionaries. |
|
97 | given as dictionaries. | |
96 |
|
98 | |||
|
99 | An optional second namespace can be given. This allows the completer | |||
|
100 | to handle cases where both the local and global scopes need to be | |||
|
101 | distinguished. | |||
|
102 | ||||
97 | Completer instances should be used as the completion mechanism of |
|
103 | Completer instances should be used as the completion mechanism of | |
98 | readline via the set_completer() call: |
|
104 | readline via the set_completer() call: | |
99 |
|
105 | |||
100 | readline.set_completer(Completer(my_namespace).complete) |
|
106 | readline.set_completer(Completer(my_namespace).complete) | |
101 | """ |
|
107 | """ | |
102 |
|
108 | |||
103 |
if namespace and type(namespace) != type |
|
109 | if namespace and type(namespace) != types.DictType: | |
104 | raise TypeError,'namespace must be a dictionary' |
|
110 | raise TypeError,'namespace must be a dictionary' | |
105 |
|
111 | |||
|
112 | if global_namespace and type(global_namespace) != types.DictType: | |||
|
113 | raise TypeError,'global_namespace must be a dictionary' | |||
|
114 | ||||
106 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
115 | # Don't bind to namespace quite yet, but flag whether the user wants a | |
107 | # specific namespace or to use __main__.__dict__. This will allow us |
|
116 | # specific namespace or to use __main__.__dict__. This will allow us | |
108 | # to bind to __main__.__dict__ at completion time, not now. |
|
117 | # to bind to __main__.__dict__ at completion time, not now. | |
109 | if namespace is None: |
|
118 | if namespace is None: | |
110 | self.use_main_ns = 1 |
|
119 | self.use_main_ns = 1 | |
111 | else: |
|
120 | else: | |
112 | self.use_main_ns = 0 |
|
121 | self.use_main_ns = 0 | |
113 | self.namespace = namespace |
|
122 | self.namespace = namespace | |
114 |
|
123 | |||
|
124 | # The global namespace, if given, can be bound directly | |||
|
125 | if global_namespace is None: | |||
|
126 | self.global_namespace = {} | |||
|
127 | else: | |||
|
128 | self.global_namespace = global_namespace | |||
|
129 | ||||
115 | def complete(self, text, state): |
|
130 | def complete(self, text, state): | |
116 | """Return the next possible completion for 'text'. |
|
131 | """Return the next possible completion for 'text'. | |
117 |
|
132 | |||
118 | This is called successively with state == 0, 1, 2, ... until it |
|
133 | This is called successively with state == 0, 1, 2, ... until it | |
119 | returns None. The completion should begin with 'text'. |
|
134 | returns None. The completion should begin with 'text'. | |
120 |
|
135 | |||
121 | """ |
|
136 | """ | |
122 | if self.use_main_ns: |
|
137 | if self.use_main_ns: | |
123 | self.namespace = __main__.__dict__ |
|
138 | self.namespace = __main__.__dict__ | |
124 |
|
139 | |||
125 | if state == 0: |
|
140 | if state == 0: | |
126 | if "." in text: |
|
141 | if "." in text: | |
127 | self.matches = self.attr_matches(text) |
|
142 | self.matches = self.attr_matches(text) | |
128 | else: |
|
143 | else: | |
129 | self.matches = self.global_matches(text) |
|
144 | self.matches = self.global_matches(text) | |
130 | try: |
|
145 | try: | |
131 | return self.matches[state] |
|
146 | return self.matches[state] | |
132 | except IndexError: |
|
147 | except IndexError: | |
133 | return None |
|
148 | return None | |
134 |
|
149 | |||
135 | def global_matches(self, text): |
|
150 | def global_matches(self, text): | |
136 | """Compute matches when text is a simple name. |
|
151 | """Compute matches when text is a simple name. | |
137 |
|
152 | |||
138 | Return a list of all keywords, built-in functions and names currently |
|
153 | Return a list of all keywords, built-in functions and names currently | |
139 | defined in self.namespace that match. |
|
154 | defined in self.namespace or self.global_namespace that match. | |
140 |
|
155 | |||
141 | """ |
|
156 | """ | |
142 | import keyword |
|
|||
143 | matches = [] |
|
157 | matches = [] | |
|
158 | match_append = matches.append | |||
144 | n = len(text) |
|
159 | n = len(text) | |
145 |
for l |
|
160 | for lst in [keyword.kwlist, | |
146 |
|
|
161 | __builtin__.__dict__.keys(), | |
147 |
|
|
162 | self.namespace.keys(), | |
148 | for word in list: |
|
163 | self.global_namespace.keys()]: | |
|
164 | for word in lst: | |||
149 | if word[:n] == text and word != "__builtins__": |
|
165 | if word[:n] == text and word != "__builtins__": | |
150 |
match |
|
166 | match_append(word) | |
151 | return matches |
|
167 | return matches | |
152 |
|
168 | |||
153 | def attr_matches(self, text): |
|
169 | def attr_matches(self, text): | |
154 | """Compute matches when text contains a dot. |
|
170 | """Compute matches when text contains a dot. | |
155 |
|
171 | |||
156 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
172 | Assuming the text is of the form NAME.NAME....[NAME], and is | |
157 |
evaluatable in self.namespace |
|
173 | evaluatable in self.namespace or self.global_namespace, it will be | |
158 | (as revealed by dir()) are used as possible completions. (For class |
|
174 | evaluated and its attributes (as revealed by dir()) are used as | |
159 |
instances, class members are are |
|
175 | possible completions. (For class instances, class members are are | |
|
176 | also considered.) | |||
160 |
|
177 | |||
161 | WARNING: this can still invoke arbitrary C code, if an object |
|
178 | WARNING: this can still invoke arbitrary C code, if an object | |
162 | with a __getattr__ hook is evaluated. |
|
179 | with a __getattr__ hook is evaluated. | |
163 |
|
180 | |||
164 | """ |
|
181 | """ | |
165 | import re |
|
182 | import re | |
166 |
|
183 | |||
167 | # Another option, seems to work great. Catches things like ''.<tab> |
|
184 | # Another option, seems to work great. Catches things like ''.<tab> | |
168 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
185 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |
169 |
|
186 | |||
170 | if not m: |
|
187 | if not m: | |
171 | return [] |
|
188 | return [] | |
172 | expr, attr = m.group(1, 3) |
|
189 | expr, attr = m.group(1, 3) | |
173 | object = eval(expr, self.namespace) |
|
190 | print 'expr:',expr # dbg | |
|
191 | try: | |||
|
192 | object = eval(expr, self.namespace) | |||
|
193 | except: | |||
|
194 | object = eval(expr, self.global_namespace) | |||
|
195 | print 'obj:',object # dbg | |||
174 | words = [w for w in dir(object) if isinstance(w, basestring)] |
|
196 | words = [w for w in dir(object) if isinstance(w, basestring)] | |
175 | if hasattr(object,'__class__'): |
|
197 | if hasattr(object,'__class__'): | |
176 | words.append('__class__') |
|
198 | words.append('__class__') | |
177 | words.extend(get_class_members(object.__class__)) |
|
199 | words.extend(get_class_members(object.__class__)) | |
178 | n = len(attr) |
|
200 | n = len(attr) | |
179 | matches = [] |
|
201 | matches = [] | |
180 | for word in words: |
|
202 | for word in words: | |
181 | if word[:n] == attr and word != "__builtins__": |
|
203 | if word[:n] == attr and word != "__builtins__": | |
182 | matches.append("%s.%s" % (expr, word)) |
|
204 | matches.append("%s.%s" % (expr, word)) | |
183 | return matches |
|
205 | return matches | |
184 |
|
206 | |||
185 | def get_class_members(klass): |
|
207 | def get_class_members(klass): | |
186 | ret = dir(klass) |
|
208 | ret = dir(klass) | |
187 | if hasattr(klass,'__bases__'): |
|
209 | if hasattr(klass,'__bases__'): | |
188 | for base in klass.__bases__: |
|
210 | for base in klass.__bases__: | |
189 | ret.extend(get_class_members(base)) |
|
211 | ret.extend(get_class_members(base)) | |
190 | return ret |
|
212 | return ret | |
191 |
|
213 | |||
192 | readline.set_completer(Completer().complete) |
|
214 | readline.set_completer(Completer().complete) |
@@ -1,900 +1,901 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """IPython Shell classes. |
|
2 | """IPython Shell classes. | |
3 |
|
3 | |||
4 | All the matplotlib support code was co-developed with John Hunter, |
|
4 | All the matplotlib support code was co-developed with John Hunter, | |
5 | matplotlib's author. |
|
5 | matplotlib's author. | |
6 |
|
6 | |||
7 |
$Id: Shell.py 92 |
|
7 | $Id: Shell.py 952 2005-12-26 17:51:33Z fperez $""" | |
8 |
|
8 | |||
9 | #***************************************************************************** |
|
9 | #***************************************************************************** | |
10 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
10 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #***************************************************************************** |
|
14 | #***************************************************************************** | |
15 |
|
15 | |||
16 | from IPython import Release |
|
16 | from IPython import Release | |
17 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
17 | __author__ = '%s <%s>' % Release.authors['Fernando'] | |
18 | __license__ = Release.license |
|
18 | __license__ = Release.license | |
19 |
|
19 | |||
20 | # Code begins |
|
20 | # Code begins | |
21 | import __main__ |
|
21 | import __main__ | |
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | import sys |
|
23 | import sys | |
24 | import os |
|
24 | import os | |
25 | import code |
|
25 | import code | |
26 | import threading |
|
26 | import threading | |
27 | import signal |
|
27 | import signal | |
28 |
|
28 | |||
29 | import IPython |
|
29 | import IPython | |
30 | from IPython.iplib import InteractiveShell |
|
30 | from IPython.iplib import InteractiveShell | |
31 | from IPython.ipmaker import make_IPython |
|
31 | from IPython.ipmaker import make_IPython | |
32 | from IPython.genutils import Term,warn,error,flag_calls |
|
32 | from IPython.genutils import Term,warn,error,flag_calls | |
33 | from IPython.Struct import Struct |
|
33 | from IPython.Struct import Struct | |
34 | from IPython.Magic import Magic |
|
34 | from IPython.Magic import Magic | |
35 | from IPython import ultraTB |
|
35 | from IPython import ultraTB | |
36 |
|
36 | |||
37 | # global flag to pass around information about Ctrl-C without exceptions |
|
37 | # global flag to pass around information about Ctrl-C without exceptions | |
38 | KBINT = False |
|
38 | KBINT = False | |
39 |
|
39 | |||
40 | # global flag to turn on/off Tk support. |
|
40 | # global flag to turn on/off Tk support. | |
41 | USE_TK = False |
|
41 | USE_TK = False | |
42 |
|
42 | |||
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 | # This class is trivial now, but I want to have it in to publish a clean |
|
44 | # This class is trivial now, but I want to have it in to publish a clean | |
45 | # interface. Later when the internals are reorganized, code that uses this |
|
45 | # interface. Later when the internals are reorganized, code that uses this | |
46 | # shouldn't have to change. |
|
46 | # shouldn't have to change. | |
47 |
|
47 | |||
48 | class IPShell: |
|
48 | class IPShell: | |
49 | """Create an IPython instance.""" |
|
49 | """Create an IPython instance.""" | |
50 |
|
50 | |||
51 | def __init__(self,argv=None,user_ns=None,debug=1, |
|
51 | def __init__(self,argv=None,user_ns=None,debug=1, | |
52 | shell_class=InteractiveShell): |
|
52 | shell_class=InteractiveShell): | |
53 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, |
|
53 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, | |
54 | shell_class=shell_class) |
|
54 | shell_class=shell_class) | |
55 |
|
55 | |||
56 | def mainloop(self,sys_exit=0,banner=None): |
|
56 | def mainloop(self,sys_exit=0,banner=None): | |
57 | self.IP.mainloop(banner) |
|
57 | self.IP.mainloop(banner) | |
58 | if sys_exit: |
|
58 | if sys_exit: | |
59 | sys.exit() |
|
59 | sys.exit() | |
60 |
|
60 | |||
61 | #----------------------------------------------------------------------------- |
|
61 | #----------------------------------------------------------------------------- | |
62 | class IPShellEmbed: |
|
62 | class IPShellEmbed: | |
63 | """Allow embedding an IPython shell into a running program. |
|
63 | """Allow embedding an IPython shell into a running program. | |
64 |
|
64 | |||
65 | Instances of this class are callable, with the __call__ method being an |
|
65 | Instances of this class are callable, with the __call__ method being an | |
66 | alias to the embed() method of an InteractiveShell instance. |
|
66 | alias to the embed() method of an InteractiveShell instance. | |
67 |
|
67 | |||
68 | Usage (see also the example-embed.py file for a running example): |
|
68 | Usage (see also the example-embed.py file for a running example): | |
69 |
|
69 | |||
70 | ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override]) |
|
70 | ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override]) | |
71 |
|
71 | |||
72 | - argv: list containing valid command-line options for IPython, as they |
|
72 | - argv: list containing valid command-line options for IPython, as they | |
73 | would appear in sys.argv[1:]. |
|
73 | would appear in sys.argv[1:]. | |
74 |
|
74 | |||
75 | For example, the following command-line options: |
|
75 | For example, the following command-line options: | |
76 |
|
76 | |||
77 | $ ipython -prompt_in1 'Input <\\#>' -colors LightBG |
|
77 | $ ipython -prompt_in1 'Input <\\#>' -colors LightBG | |
78 |
|
78 | |||
79 | would be passed in the argv list as: |
|
79 | would be passed in the argv list as: | |
80 |
|
80 | |||
81 | ['-prompt_in1','Input <\\#>','-colors','LightBG'] |
|
81 | ['-prompt_in1','Input <\\#>','-colors','LightBG'] | |
82 |
|
82 | |||
83 | - banner: string which gets printed every time the interpreter starts. |
|
83 | - banner: string which gets printed every time the interpreter starts. | |
84 |
|
84 | |||
85 | - exit_msg: string which gets printed every time the interpreter exits. |
|
85 | - exit_msg: string which gets printed every time the interpreter exits. | |
86 |
|
86 | |||
87 | - rc_override: a dict or Struct of configuration options such as those |
|
87 | - rc_override: a dict or Struct of configuration options such as those | |
88 | used by IPython. These options are read from your ~/.ipython/ipythonrc |
|
88 | used by IPython. These options are read from your ~/.ipython/ipythonrc | |
89 | file when the Shell object is created. Passing an explicit rc_override |
|
89 | file when the Shell object is created. Passing an explicit rc_override | |
90 | dict with any options you want allows you to override those values at |
|
90 | dict with any options you want allows you to override those values at | |
91 | creation time without having to modify the file. This way you can create |
|
91 | creation time without having to modify the file. This way you can create | |
92 | embeddable instances configured in any way you want without editing any |
|
92 | embeddable instances configured in any way you want without editing any | |
93 | global files (thus keeping your interactive IPython configuration |
|
93 | global files (thus keeping your interactive IPython configuration | |
94 | unchanged). |
|
94 | unchanged). | |
95 |
|
95 | |||
96 | Then the ipshell instance can be called anywhere inside your code: |
|
96 | Then the ipshell instance can be called anywhere inside your code: | |
97 |
|
97 | |||
98 | ipshell(header='') -> Opens up an IPython shell. |
|
98 | ipshell(header='') -> Opens up an IPython shell. | |
99 |
|
99 | |||
100 | - header: string printed by the IPython shell upon startup. This can let |
|
100 | - header: string printed by the IPython shell upon startup. This can let | |
101 | you know where in your code you are when dropping into the shell. Note |
|
101 | you know where in your code you are when dropping into the shell. Note | |
102 | that 'banner' gets prepended to all calls, so header is used for |
|
102 | that 'banner' gets prepended to all calls, so header is used for | |
103 | location-specific information. |
|
103 | location-specific information. | |
104 |
|
104 | |||
105 | For more details, see the __call__ method below. |
|
105 | For more details, see the __call__ method below. | |
106 |
|
106 | |||
107 | When the IPython shell is exited with Ctrl-D, normal program execution |
|
107 | When the IPython shell is exited with Ctrl-D, normal program execution | |
108 | resumes. |
|
108 | resumes. | |
109 |
|
109 | |||
110 | This functionality was inspired by a posting on comp.lang.python by cmkl |
|
110 | This functionality was inspired by a posting on comp.lang.python by cmkl | |
111 | <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and |
|
111 | <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and | |
112 | by the IDL stop/continue commands.""" |
|
112 | by the IDL stop/continue commands.""" | |
113 |
|
113 | |||
114 | def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None): |
|
114 | def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None): | |
115 | """Note that argv here is a string, NOT a list.""" |
|
115 | """Note that argv here is a string, NOT a list.""" | |
116 | self.set_banner(banner) |
|
116 | self.set_banner(banner) | |
117 | self.set_exit_msg(exit_msg) |
|
117 | self.set_exit_msg(exit_msg) | |
118 | self.set_dummy_mode(0) |
|
118 | self.set_dummy_mode(0) | |
119 |
|
119 | |||
120 | # sys.displayhook is a global, we need to save the user's original |
|
120 | # sys.displayhook is a global, we need to save the user's original | |
121 | # Don't rely on __displayhook__, as the user may have changed that. |
|
121 | # Don't rely on __displayhook__, as the user may have changed that. | |
122 | self.sys_displayhook_ori = sys.displayhook |
|
122 | self.sys_displayhook_ori = sys.displayhook | |
123 |
|
123 | |||
124 | # save readline completer status |
|
124 | # save readline completer status | |
125 | try: |
|
125 | try: | |
126 | #print 'Save completer',sys.ipcompleter # dbg |
|
126 | #print 'Save completer',sys.ipcompleter # dbg | |
127 | self.sys_ipcompleter_ori = sys.ipcompleter |
|
127 | self.sys_ipcompleter_ori = sys.ipcompleter | |
128 | except: |
|
128 | except: | |
129 | pass # not nested with IPython |
|
129 | pass # not nested with IPython | |
130 |
|
130 | |||
131 | # FIXME. Passing user_ns breaks namespace handling. |
|
131 | # FIXME. Passing user_ns breaks namespace handling. | |
132 | #self.IP = make_IPython(argv,user_ns=__main__.__dict__) |
|
132 | #self.IP = make_IPython(argv,user_ns=__main__.__dict__) | |
133 | self.IP = make_IPython(argv,rc_override=rc_override,embedded=True) |
|
133 | self.IP = make_IPython(argv,rc_override=rc_override,embedded=True) | |
134 |
|
134 | |||
135 | # copy our own displayhook also |
|
135 | # copy our own displayhook also | |
136 | self.sys_displayhook_embed = sys.displayhook |
|
136 | self.sys_displayhook_embed = sys.displayhook | |
137 | # and leave the system's display hook clean |
|
137 | # and leave the system's display hook clean | |
138 | sys.displayhook = self.sys_displayhook_ori |
|
138 | sys.displayhook = self.sys_displayhook_ori | |
139 | # don't use the ipython crash handler so that user exceptions aren't |
|
139 | # don't use the ipython crash handler so that user exceptions aren't | |
140 | # trapped |
|
140 | # trapped | |
141 | sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors, |
|
141 | sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors, | |
142 | mode = self.IP.rc.xmode, |
|
142 | mode = self.IP.rc.xmode, | |
143 | call_pdb = self.IP.rc.pdb) |
|
143 | call_pdb = self.IP.rc.pdb) | |
144 | self.restore_system_completer() |
|
144 | self.restore_system_completer() | |
145 |
|
145 | |||
146 | def restore_system_completer(self): |
|
146 | def restore_system_completer(self): | |
147 | """Restores the readline completer which was in place. |
|
147 | """Restores the readline completer which was in place. | |
148 |
|
148 | |||
149 | This allows embedded IPython within IPython not to disrupt the |
|
149 | This allows embedded IPython within IPython not to disrupt the | |
150 | parent's completion. |
|
150 | parent's completion. | |
151 | """ |
|
151 | """ | |
152 |
|
152 | |||
153 | try: |
|
153 | try: | |
154 | self.IP.readline.set_completer(self.sys_ipcompleter_ori) |
|
154 | self.IP.readline.set_completer(self.sys_ipcompleter_ori) | |
155 | sys.ipcompleter = self.sys_ipcompleter_ori |
|
155 | sys.ipcompleter = self.sys_ipcompleter_ori | |
156 | except: |
|
156 | except: | |
157 | pass |
|
157 | pass | |
158 |
|
158 | |||
159 | def __call__(self,header='',local_ns=None,global_ns=None,dummy=None): |
|
159 | def __call__(self,header='',local_ns=None,global_ns=None,dummy=None): | |
160 | """Activate the interactive interpreter. |
|
160 | """Activate the interactive interpreter. | |
161 |
|
161 | |||
162 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start |
|
162 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start | |
163 | the interpreter shell with the given local and global namespaces, and |
|
163 | the interpreter shell with the given local and global namespaces, and | |
164 | optionally print a header string at startup. |
|
164 | optionally print a header string at startup. | |
165 |
|
165 | |||
166 | The shell can be globally activated/deactivated using the |
|
166 | The shell can be globally activated/deactivated using the | |
167 | set/get_dummy_mode methods. This allows you to turn off a shell used |
|
167 | set/get_dummy_mode methods. This allows you to turn off a shell used | |
168 | for debugging globally. |
|
168 | for debugging globally. | |
169 |
|
169 | |||
170 | However, *each* time you call the shell you can override the current |
|
170 | However, *each* time you call the shell you can override the current | |
171 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
171 | state of dummy_mode with the optional keyword parameter 'dummy'. For | |
172 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you |
|
172 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you | |
173 | can still have a specific call work by making it as IPShell(dummy=0). |
|
173 | can still have a specific call work by making it as IPShell(dummy=0). | |
174 |
|
174 | |||
175 | The optional keyword parameter dummy controls whether the call |
|
175 | The optional keyword parameter dummy controls whether the call | |
176 | actually does anything. """ |
|
176 | actually does anything. """ | |
177 |
|
177 | |||
178 | # Allow the dummy parameter to override the global __dummy_mode |
|
178 | # Allow the dummy parameter to override the global __dummy_mode | |
179 | if dummy or (dummy != 0 and self.__dummy_mode): |
|
179 | if dummy or (dummy != 0 and self.__dummy_mode): | |
180 | return |
|
180 | return | |
181 |
|
181 | |||
182 | # Set global subsystems (display,completions) to our values |
|
182 | # Set global subsystems (display,completions) to our values | |
183 | sys.displayhook = self.sys_displayhook_embed |
|
183 | sys.displayhook = self.sys_displayhook_embed | |
184 | if self.IP.has_readline: |
|
184 | if self.IP.has_readline: | |
185 | self.IP.readline.set_completer(self.IP.Completer.complete) |
|
185 | self.IP.readline.set_completer(self.IP.Completer.complete) | |
186 |
|
186 | |||
187 | if self.banner and header: |
|
187 | if self.banner and header: | |
188 | format = '%s\n%s\n' |
|
188 | format = '%s\n%s\n' | |
189 | else: |
|
189 | else: | |
190 | format = '%s%s\n' |
|
190 | format = '%s%s\n' | |
191 | banner = format % (self.banner,header) |
|
191 | banner = format % (self.banner,header) | |
192 |
|
192 | |||
193 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
193 | # Call the embedding code with a stack depth of 1 so it can skip over | |
194 | # our call and get the original caller's namespaces. |
|
194 | # our call and get the original caller's namespaces. | |
195 | self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1) |
|
195 | self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1) | |
196 |
|
196 | |||
197 | if self.exit_msg: |
|
197 | if self.exit_msg: | |
198 | print self.exit_msg |
|
198 | print self.exit_msg | |
199 |
|
199 | |||
200 | # Restore global systems (display, completion) |
|
200 | # Restore global systems (display, completion) | |
201 | sys.displayhook = self.sys_displayhook_ori |
|
201 | sys.displayhook = self.sys_displayhook_ori | |
202 | self.restore_system_completer() |
|
202 | self.restore_system_completer() | |
203 |
|
203 | |||
204 | def set_dummy_mode(self,dummy): |
|
204 | def set_dummy_mode(self,dummy): | |
205 | """Sets the embeddable shell's dummy mode parameter. |
|
205 | """Sets the embeddable shell's dummy mode parameter. | |
206 |
|
206 | |||
207 | set_dummy_mode(dummy): dummy = 0 or 1. |
|
207 | set_dummy_mode(dummy): dummy = 0 or 1. | |
208 |
|
208 | |||
209 | This parameter is persistent and makes calls to the embeddable shell |
|
209 | This parameter is persistent and makes calls to the embeddable shell | |
210 | silently return without performing any action. This allows you to |
|
210 | silently return without performing any action. This allows you to | |
211 | globally activate or deactivate a shell you're using with a single call. |
|
211 | globally activate or deactivate a shell you're using with a single call. | |
212 |
|
212 | |||
213 | If you need to manually""" |
|
213 | If you need to manually""" | |
214 |
|
214 | |||
215 | if dummy not in [0,1]: |
|
215 | if dummy not in [0,1,False,True]: | |
216 |
raise ValueError,'dummy parameter must be |
|
216 | raise ValueError,'dummy parameter must be boolean' | |
217 | self.__dummy_mode = dummy |
|
217 | self.__dummy_mode = dummy | |
218 |
|
218 | |||
219 | def get_dummy_mode(self): |
|
219 | def get_dummy_mode(self): | |
220 | """Return the current value of the dummy mode parameter. |
|
220 | """Return the current value of the dummy mode parameter. | |
221 | """ |
|
221 | """ | |
222 | return self.__dummy_mode |
|
222 | return self.__dummy_mode | |
223 |
|
223 | |||
224 | def set_banner(self,banner): |
|
224 | def set_banner(self,banner): | |
225 | """Sets the global banner. |
|
225 | """Sets the global banner. | |
226 |
|
226 | |||
227 | This banner gets prepended to every header printed when the shell |
|
227 | This banner gets prepended to every header printed when the shell | |
228 | instance is called.""" |
|
228 | instance is called.""" | |
229 |
|
229 | |||
230 | self.banner = banner |
|
230 | self.banner = banner | |
231 |
|
231 | |||
232 | def set_exit_msg(self,exit_msg): |
|
232 | def set_exit_msg(self,exit_msg): | |
233 | """Sets the global exit_msg. |
|
233 | """Sets the global exit_msg. | |
234 |
|
234 | |||
235 | This exit message gets printed upon exiting every time the embedded |
|
235 | This exit message gets printed upon exiting every time the embedded | |
236 | shell is called. It is None by default. """ |
|
236 | shell is called. It is None by default. """ | |
237 |
|
237 | |||
238 | self.exit_msg = exit_msg |
|
238 | self.exit_msg = exit_msg | |
239 |
|
239 | |||
240 | #----------------------------------------------------------------------------- |
|
240 | #----------------------------------------------------------------------------- | |
241 | def sigint_handler (signum,stack_frame): |
|
241 | def sigint_handler (signum,stack_frame): | |
242 | """Sigint handler for threaded apps. |
|
242 | """Sigint handler for threaded apps. | |
243 |
|
243 | |||
244 | This is a horrible hack to pass information about SIGINT _without_ using |
|
244 | This is a horrible hack to pass information about SIGINT _without_ using | |
245 | exceptions, since I haven't been able to properly manage cross-thread |
|
245 | exceptions, since I haven't been able to properly manage cross-thread | |
246 | exceptions in GTK/WX. In fact, I don't think it can be done (or at least |
|
246 | exceptions in GTK/WX. In fact, I don't think it can be done (or at least | |
247 | that's my understanding from a c.l.py thread where this was discussed).""" |
|
247 | that's my understanding from a c.l.py thread where this was discussed).""" | |
248 |
|
248 | |||
249 | global KBINT |
|
249 | global KBINT | |
250 |
|
250 | |||
251 | print '\nKeyboardInterrupt - Press <Enter> to continue.', |
|
251 | print '\nKeyboardInterrupt - Press <Enter> to continue.', | |
252 | Term.cout.flush() |
|
252 | Term.cout.flush() | |
253 | # Set global flag so that runsource can know that Ctrl-C was hit |
|
253 | # Set global flag so that runsource can know that Ctrl-C was hit | |
254 | KBINT = True |
|
254 | KBINT = True | |
255 |
|
255 | |||
256 | class MTInteractiveShell(InteractiveShell): |
|
256 | class MTInteractiveShell(InteractiveShell): | |
257 | """Simple multi-threaded shell.""" |
|
257 | """Simple multi-threaded shell.""" | |
258 |
|
258 | |||
259 | # Threading strategy taken from: |
|
259 | # Threading strategy taken from: | |
260 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian |
|
260 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian | |
261 | # McErlean and John Finlay. Modified with corrections by Antoon Pardon, |
|
261 | # McErlean and John Finlay. Modified with corrections by Antoon Pardon, | |
262 | # from the pygtk mailing list, to avoid lockups with system calls. |
|
262 | # from the pygtk mailing list, to avoid lockups with system calls. | |
263 |
|
263 | |||
264 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
264 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
265 | user_ns = None, banner2='',**kw): |
|
265 | user_ns = None, banner2='',**kw): | |
266 | """Similar to the normal InteractiveShell, but with threading control""" |
|
266 | """Similar to the normal InteractiveShell, but with threading control""" | |
267 |
|
267 | |||
268 | IPython.iplib.InteractiveShell.__init__(self,name,usage,rc,user_ns,banner2) |
|
268 | IPython.iplib.InteractiveShell.__init__(self,name,usage,rc,user_ns,banner2) | |
269 |
|
269 | |||
270 | # Locking control variable |
|
270 | # Locking control variable | |
271 | self.thread_ready = threading.Condition() |
|
271 | self.thread_ready = threading.Condition() | |
272 |
|
272 | |||
273 | # Stuff to do at closing time |
|
273 | # Stuff to do at closing time | |
274 | self._kill = False |
|
274 | self._kill = False | |
275 | on_kill = kw.get('on_kill') |
|
275 | on_kill = kw.get('on_kill') | |
276 | if on_kill is None: |
|
276 | if on_kill is None: | |
277 | on_kill = [] |
|
277 | on_kill = [] | |
278 | # Check that all things to kill are callable: |
|
278 | # Check that all things to kill are callable: | |
279 | for t in on_kill: |
|
279 | for t in on_kill: | |
280 | if not callable(t): |
|
280 | if not callable(t): | |
281 | raise TypeError,'on_kill must be a list of callables' |
|
281 | raise TypeError,'on_kill must be a list of callables' | |
282 | self.on_kill = on_kill |
|
282 | self.on_kill = on_kill | |
283 |
|
283 | |||
284 | def runsource(self, source, filename="<input>", symbol="single"): |
|
284 | def runsource(self, source, filename="<input>", symbol="single"): | |
285 | """Compile and run some source in the interpreter. |
|
285 | """Compile and run some source in the interpreter. | |
286 |
|
286 | |||
287 | Modified version of code.py's runsource(), to handle threading issues. |
|
287 | Modified version of code.py's runsource(), to handle threading issues. | |
288 | See the original for full docstring details.""" |
|
288 | See the original for full docstring details.""" | |
289 |
|
289 | |||
290 | global KBINT |
|
290 | global KBINT | |
291 |
|
291 | |||
292 | # If Ctrl-C was typed, we reset the flag and return right away |
|
292 | # If Ctrl-C was typed, we reset the flag and return right away | |
293 | if KBINT: |
|
293 | if KBINT: | |
294 | KBINT = False |
|
294 | KBINT = False | |
295 | return False |
|
295 | return False | |
296 |
|
296 | |||
297 | try: |
|
297 | try: | |
298 | code = self.compile(source, filename, symbol) |
|
298 | code = self.compile(source, filename, symbol) | |
299 | except (OverflowError, SyntaxError, ValueError): |
|
299 | except (OverflowError, SyntaxError, ValueError): | |
300 | # Case 1 |
|
300 | # Case 1 | |
301 | self.showsyntaxerror(filename) |
|
301 | self.showsyntaxerror(filename) | |
302 | return False |
|
302 | return False | |
303 |
|
303 | |||
304 | if code is None: |
|
304 | if code is None: | |
305 | # Case 2 |
|
305 | # Case 2 | |
306 | return True |
|
306 | return True | |
307 |
|
307 | |||
308 | # Case 3 |
|
308 | # Case 3 | |
309 | # Store code in self, so the execution thread can handle it |
|
309 | # Store code in self, so the execution thread can handle it | |
310 | self.thread_ready.acquire() |
|
310 | self.thread_ready.acquire() | |
311 | self.code_to_run = code |
|
311 | self.code_to_run = code | |
312 | self.thread_ready.wait() # Wait until processed in timeout interval |
|
312 | self.thread_ready.wait() # Wait until processed in timeout interval | |
313 | self.thread_ready.release() |
|
313 | self.thread_ready.release() | |
314 |
|
314 | |||
315 | return False |
|
315 | return False | |
316 |
|
316 | |||
317 | def runcode(self): |
|
317 | def runcode(self): | |
318 | """Execute a code object. |
|
318 | """Execute a code object. | |
319 |
|
319 | |||
320 | Multithreaded wrapper around IPython's runcode().""" |
|
320 | Multithreaded wrapper around IPython's runcode().""" | |
321 |
|
321 | |||
322 | # lock thread-protected stuff |
|
322 | # lock thread-protected stuff | |
323 | self.thread_ready.acquire() |
|
323 | self.thread_ready.acquire() | |
324 |
|
324 | |||
325 | # Install sigint handler |
|
325 | # Install sigint handler | |
326 | try: |
|
326 | try: | |
327 | signal.signal(signal.SIGINT, sigint_handler) |
|
327 | signal.signal(signal.SIGINT, sigint_handler) | |
328 | except SystemError: |
|
328 | except SystemError: | |
329 | # This happens under Windows, which seems to have all sorts |
|
329 | # This happens under Windows, which seems to have all sorts | |
330 | # of problems with signal handling. Oh well... |
|
330 | # of problems with signal handling. Oh well... | |
331 | pass |
|
331 | pass | |
332 |
|
332 | |||
333 | if self._kill: |
|
333 | if self._kill: | |
334 | print >>Term.cout, 'Closing threads...', |
|
334 | print >>Term.cout, 'Closing threads...', | |
335 | Term.cout.flush() |
|
335 | Term.cout.flush() | |
336 | for tokill in self.on_kill: |
|
336 | for tokill in self.on_kill: | |
337 | tokill() |
|
337 | tokill() | |
338 | print >>Term.cout, 'Done.' |
|
338 | print >>Term.cout, 'Done.' | |
339 |
|
339 | |||
340 | # Run pending code by calling parent class |
|
340 | # Run pending code by calling parent class | |
341 | if self.code_to_run is not None: |
|
341 | if self.code_to_run is not None: | |
342 | self.thread_ready.notify() |
|
342 | self.thread_ready.notify() | |
343 | InteractiveShell.runcode(self,self.code_to_run) |
|
343 | InteractiveShell.runcode(self,self.code_to_run) | |
344 |
|
344 | |||
345 | # We're done with thread-protected variables |
|
345 | # We're done with thread-protected variables | |
346 | self.thread_ready.release() |
|
346 | self.thread_ready.release() | |
347 | # This MUST return true for gtk threading to work |
|
347 | # This MUST return true for gtk threading to work | |
348 | return True |
|
348 | return True | |
349 |
|
349 | |||
350 | def kill (self): |
|
350 | def kill (self): | |
351 | """Kill the thread, returning when it has been shut down.""" |
|
351 | """Kill the thread, returning when it has been shut down.""" | |
352 | self.thread_ready.acquire() |
|
352 | self.thread_ready.acquire() | |
353 | self._kill = True |
|
353 | self._kill = True | |
354 | self.thread_ready.release() |
|
354 | self.thread_ready.release() | |
355 |
|
355 | |||
356 | class MatplotlibShellBase: |
|
356 | class MatplotlibShellBase: | |
357 | """Mixin class to provide the necessary modifications to regular IPython |
|
357 | """Mixin class to provide the necessary modifications to regular IPython | |
358 | shell classes for matplotlib support. |
|
358 | shell classes for matplotlib support. | |
359 |
|
359 | |||
360 | Given Python's MRO, this should be used as the FIRST class in the |
|
360 | Given Python's MRO, this should be used as the FIRST class in the | |
361 | inheritance hierarchy, so that it overrides the relevant methods.""" |
|
361 | inheritance hierarchy, so that it overrides the relevant methods.""" | |
362 |
|
362 | |||
363 | def _matplotlib_config(self,name): |
|
363 | def _matplotlib_config(self,name): | |
364 | """Return various items needed to setup the user's shell with matplotlib""" |
|
364 | """Return various items needed to setup the user's shell with matplotlib""" | |
365 |
|
365 | |||
366 | # Initialize matplotlib to interactive mode always |
|
366 | # Initialize matplotlib to interactive mode always | |
367 | import matplotlib |
|
367 | import matplotlib | |
368 | from matplotlib import backends |
|
368 | from matplotlib import backends | |
369 | matplotlib.interactive(True) |
|
369 | matplotlib.interactive(True) | |
370 |
|
370 | |||
371 | def use(arg): |
|
371 | def use(arg): | |
372 | """IPython wrapper for matplotlib's backend switcher. |
|
372 | """IPython wrapper for matplotlib's backend switcher. | |
373 |
|
373 | |||
374 | In interactive use, we can not allow switching to a different |
|
374 | In interactive use, we can not allow switching to a different | |
375 | interactive backend, since thread conflicts will most likely crash |
|
375 | interactive backend, since thread conflicts will most likely crash | |
376 | the python interpreter. This routine does a safety check first, |
|
376 | the python interpreter. This routine does a safety check first, | |
377 | and refuses to perform a dangerous switch. It still allows |
|
377 | and refuses to perform a dangerous switch. It still allows | |
378 | switching to non-interactive backends.""" |
|
378 | switching to non-interactive backends.""" | |
379 |
|
379 | |||
380 | if arg in backends.interactive_bk and arg != self.mpl_backend: |
|
380 | if arg in backends.interactive_bk and arg != self.mpl_backend: | |
381 | m=('invalid matplotlib backend switch.\n' |
|
381 | m=('invalid matplotlib backend switch.\n' | |
382 | 'This script attempted to switch to the interactive ' |
|
382 | 'This script attempted to switch to the interactive ' | |
383 | 'backend: `%s`\n' |
|
383 | 'backend: `%s`\n' | |
384 | 'Your current choice of interactive backend is: `%s`\n\n' |
|
384 | 'Your current choice of interactive backend is: `%s`\n\n' | |
385 | 'Switching interactive matplotlib backends at runtime\n' |
|
385 | 'Switching interactive matplotlib backends at runtime\n' | |
386 | 'would crash the python interpreter, ' |
|
386 | 'would crash the python interpreter, ' | |
387 | 'and IPython has blocked it.\n\n' |
|
387 | 'and IPython has blocked it.\n\n' | |
388 | 'You need to either change your choice of matplotlib backend\n' |
|
388 | 'You need to either change your choice of matplotlib backend\n' | |
389 | 'by editing your .matplotlibrc file, or run this script as a \n' |
|
389 | 'by editing your .matplotlibrc file, or run this script as a \n' | |
390 | 'standalone file from the command line, not using IPython.\n' % |
|
390 | 'standalone file from the command line, not using IPython.\n' % | |
391 | (arg,self.mpl_backend) ) |
|
391 | (arg,self.mpl_backend) ) | |
392 | raise RuntimeError, m |
|
392 | raise RuntimeError, m | |
393 | else: |
|
393 | else: | |
394 | self.mpl_use(arg) |
|
394 | self.mpl_use(arg) | |
395 | self.mpl_use._called = True |
|
395 | self.mpl_use._called = True | |
396 |
|
396 | |||
397 | self.matplotlib = matplotlib |
|
397 | self.matplotlib = matplotlib | |
398 | self.mpl_backend = matplotlib.rcParams['backend'] |
|
398 | self.mpl_backend = matplotlib.rcParams['backend'] | |
399 |
|
399 | |||
400 | # we also need to block switching of interactive backends by use() |
|
400 | # we also need to block switching of interactive backends by use() | |
401 | self.mpl_use = matplotlib.use |
|
401 | self.mpl_use = matplotlib.use | |
402 | self.mpl_use._called = False |
|
402 | self.mpl_use._called = False | |
403 | # overwrite the original matplotlib.use with our wrapper |
|
403 | # overwrite the original matplotlib.use with our wrapper | |
404 | matplotlib.use = use |
|
404 | matplotlib.use = use | |
405 |
|
405 | |||
406 |
|
406 | |||
407 | # This must be imported last in the matplotlib series, after |
|
407 | # This must be imported last in the matplotlib series, after | |
408 | # backend/interactivity choices have been made |
|
408 | # backend/interactivity choices have been made | |
409 | try: |
|
409 | try: | |
410 | import matplotlib.pylab as pylab |
|
410 | import matplotlib.pylab as pylab | |
411 | self.pylab = pylab |
|
411 | self.pylab = pylab | |
412 | self.pylab_name = 'pylab' |
|
412 | self.pylab_name = 'pylab' | |
413 | except ImportError: |
|
413 | except ImportError: | |
414 | import matplotlib.matlab as matlab |
|
414 | import matplotlib.matlab as matlab | |
415 | self.pylab = matlab |
|
415 | self.pylab = matlab | |
416 | self.pylab_name = 'matlab' |
|
416 | self.pylab_name = 'matlab' | |
417 |
|
417 | |||
418 | self.pylab.show._needmain = False |
|
418 | self.pylab.show._needmain = False | |
419 | # We need to detect at runtime whether show() is called by the user. |
|
419 | # We need to detect at runtime whether show() is called by the user. | |
420 | # For this, we wrap it into a decorator which adds a 'called' flag. |
|
420 | # For this, we wrap it into a decorator which adds a 'called' flag. | |
421 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) |
|
421 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) | |
422 |
|
422 | |||
423 | # Build a user namespace initialized with matplotlib/matlab features. |
|
423 | # Build a user namespace initialized with matplotlib/matlab features. | |
424 | user_ns = {'__name__':'__main__', |
|
424 | user_ns = {'__name__':'__main__', | |
425 | '__builtins__' : __builtin__ } |
|
425 | '__builtins__' : __builtin__ } | |
426 |
|
426 | |||
427 | # Be careful not to remove the final \n in the code string below, or |
|
427 | # Be careful not to remove the final \n in the code string below, or | |
428 | # things will break badly with py22 (I think it's a python bug, 2.3 is |
|
428 | # things will break badly with py22 (I think it's a python bug, 2.3 is | |
429 | # OK). |
|
429 | # OK). | |
430 | pname = self.pylab_name # Python can't interpolate dotted var names |
|
430 | pname = self.pylab_name # Python can't interpolate dotted var names | |
431 | exec ("import matplotlib\n" |
|
431 | exec ("import matplotlib\n" | |
432 | "import matplotlib.%(pname)s as %(pname)s\n" |
|
432 | "import matplotlib.%(pname)s as %(pname)s\n" | |
433 | "from matplotlib.%(pname)s import *\n" % locals()) in user_ns |
|
433 | "from matplotlib.%(pname)s import *\n" % locals()) in user_ns | |
434 |
|
434 | |||
435 | # Build matplotlib info banner |
|
435 | # Build matplotlib info banner | |
436 | b=""" |
|
436 | b=""" | |
437 | Welcome to pylab, a matplotlib-based Python environment. |
|
437 | Welcome to pylab, a matplotlib-based Python environment. | |
438 | For more information, type 'help(pylab)'. |
|
438 | For more information, type 'help(pylab)'. | |
439 | """ |
|
439 | """ | |
440 | return user_ns,b |
|
440 | return user_ns,b | |
441 |
|
441 | |||
442 | def mplot_exec(self,fname,*where,**kw): |
|
442 | def mplot_exec(self,fname,*where,**kw): | |
443 | """Execute a matplotlib script. |
|
443 | """Execute a matplotlib script. | |
444 |
|
444 | |||
445 | This is a call to execfile(), but wrapped in safeties to properly |
|
445 | This is a call to execfile(), but wrapped in safeties to properly | |
446 | handle interactive rendering and backend switching.""" |
|
446 | handle interactive rendering and backend switching.""" | |
447 |
|
447 | |||
448 | #print '*** Matplotlib runner ***' # dbg |
|
448 | #print '*** Matplotlib runner ***' # dbg | |
449 | # turn off rendering until end of script |
|
449 | # turn off rendering until end of script | |
450 | isInteractive = self.matplotlib.rcParams['interactive'] |
|
450 | isInteractive = self.matplotlib.rcParams['interactive'] | |
451 | self.matplotlib.interactive(False) |
|
451 | self.matplotlib.interactive(False) | |
452 | self.safe_execfile(fname,*where,**kw) |
|
452 | self.safe_execfile(fname,*where,**kw) | |
453 | self.matplotlib.interactive(isInteractive) |
|
453 | self.matplotlib.interactive(isInteractive) | |
454 | # make rendering call now, if the user tried to do it |
|
454 | # make rendering call now, if the user tried to do it | |
455 | if self.pylab.draw_if_interactive.called: |
|
455 | if self.pylab.draw_if_interactive.called: | |
456 | self.pylab.draw() |
|
456 | self.pylab.draw() | |
457 | self.pylab.draw_if_interactive.called = False |
|
457 | self.pylab.draw_if_interactive.called = False | |
458 |
|
458 | |||
459 | # if a backend switch was performed, reverse it now |
|
459 | # if a backend switch was performed, reverse it now | |
460 | if self.mpl_use._called: |
|
460 | if self.mpl_use._called: | |
461 | self.matplotlib.rcParams['backend'] = self.mpl_backend |
|
461 | self.matplotlib.rcParams['backend'] = self.mpl_backend | |
462 |
|
462 | |||
463 | def magic_run(self,parameter_s=''): |
|
463 | def magic_run(self,parameter_s=''): | |
464 | Magic.magic_run(self,parameter_s,runner=self.mplot_exec) |
|
464 | Magic.magic_run(self,parameter_s,runner=self.mplot_exec) | |
465 |
|
465 | |||
466 | # Fix the docstring so users see the original as well |
|
466 | # Fix the docstring so users see the original as well | |
467 | magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__, |
|
467 | magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__, | |
468 | "\n *** Modified %run for Matplotlib," |
|
468 | "\n *** Modified %run for Matplotlib," | |
469 | " with proper interactive handling ***") |
|
469 | " with proper interactive handling ***") | |
470 |
|
470 | |||
471 | # Now we provide 2 versions of a matplotlib-aware IPython base shells, single |
|
471 | # Now we provide 2 versions of a matplotlib-aware IPython base shells, single | |
472 | # and multithreaded. Note that these are meant for internal use, the IPShell* |
|
472 | # and multithreaded. Note that these are meant for internal use, the IPShell* | |
473 | # classes below are the ones meant for public consumption. |
|
473 | # classes below are the ones meant for public consumption. | |
474 |
|
474 | |||
475 | class MatplotlibShell(MatplotlibShellBase,InteractiveShell): |
|
475 | class MatplotlibShell(MatplotlibShellBase,InteractiveShell): | |
476 | """Single-threaded shell with matplotlib support.""" |
|
476 | """Single-threaded shell with matplotlib support.""" | |
477 |
|
477 | |||
478 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
478 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
479 | user_ns = None, **kw): |
|
479 | user_ns = None, **kw): | |
480 | user_ns,b2 = self._matplotlib_config(name) |
|
480 | user_ns,b2 = self._matplotlib_config(name) | |
481 | InteractiveShell.__init__(self,name,usage,rc,user_ns,banner2=b2,**kw) |
|
481 | InteractiveShell.__init__(self,name,usage,rc,user_ns,banner2=b2,**kw) | |
482 |
|
482 | |||
483 | class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell): |
|
483 | class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell): | |
484 | """Multi-threaded shell with matplotlib support.""" |
|
484 | """Multi-threaded shell with matplotlib support.""" | |
485 |
|
485 | |||
486 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
486 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
487 | user_ns = None, **kw): |
|
487 | user_ns = None, **kw): | |
488 | user_ns,b2 = self._matplotlib_config(name) |
|
488 | user_ns,b2 = self._matplotlib_config(name) | |
489 | MTInteractiveShell.__init__(self,name,usage,rc,user_ns,banner2=b2,**kw) |
|
489 | MTInteractiveShell.__init__(self,name,usage,rc,user_ns,banner2=b2,**kw) | |
490 |
|
490 | |||
491 | #----------------------------------------------------------------------------- |
|
491 | #----------------------------------------------------------------------------- | |
492 | # Utility functions for the different GUI enabled IPShell* classes. |
|
492 | # Utility functions for the different GUI enabled IPShell* classes. | |
493 |
|
493 | |||
494 | def get_tk(): |
|
494 | def get_tk(): | |
495 | """Tries to import Tkinter and returns a withdrawn Tkinter root |
|
495 | """Tries to import Tkinter and returns a withdrawn Tkinter root | |
496 | window. If Tkinter is already imported or not available, this |
|
496 | window. If Tkinter is already imported or not available, this | |
497 | returns None. This function calls `hijack_tk` underneath. |
|
497 | returns None. This function calls `hijack_tk` underneath. | |
498 | """ |
|
498 | """ | |
499 | if not USE_TK or sys.modules.has_key('Tkinter'): |
|
499 | if not USE_TK or sys.modules.has_key('Tkinter'): | |
500 | return None |
|
500 | return None | |
501 | else: |
|
501 | else: | |
502 | try: |
|
502 | try: | |
503 | import Tkinter |
|
503 | import Tkinter | |
504 | except ImportError: |
|
504 | except ImportError: | |
505 | return None |
|
505 | return None | |
506 | else: |
|
506 | else: | |
507 | hijack_tk() |
|
507 | hijack_tk() | |
508 | r = Tkinter.Tk() |
|
508 | r = Tkinter.Tk() | |
509 | r.withdraw() |
|
509 | r.withdraw() | |
510 | return r |
|
510 | return r | |
511 |
|
511 | |||
512 | def hijack_tk(): |
|
512 | def hijack_tk(): | |
513 | """Modifies Tkinter's mainloop with a dummy so when a module calls |
|
513 | """Modifies Tkinter's mainloop with a dummy so when a module calls | |
514 | mainloop, it does not block. |
|
514 | mainloop, it does not block. | |
515 |
|
515 | |||
516 | """ |
|
516 | """ | |
517 | def misc_mainloop(self, n=0): |
|
517 | def misc_mainloop(self, n=0): | |
518 | pass |
|
518 | pass | |
519 | def tkinter_mainloop(n=0): |
|
519 | def tkinter_mainloop(n=0): | |
520 | pass |
|
520 | pass | |
521 |
|
521 | |||
522 | import Tkinter |
|
522 | import Tkinter | |
523 | Tkinter.Misc.mainloop = misc_mainloop |
|
523 | Tkinter.Misc.mainloop = misc_mainloop | |
524 | Tkinter.mainloop = tkinter_mainloop |
|
524 | Tkinter.mainloop = tkinter_mainloop | |
525 |
|
525 | |||
526 | def update_tk(tk): |
|
526 | def update_tk(tk): | |
527 | """Updates the Tkinter event loop. This is typically called from |
|
527 | """Updates the Tkinter event loop. This is typically called from | |
528 | the respective WX or GTK mainloops. |
|
528 | the respective WX or GTK mainloops. | |
529 | """ |
|
529 | """ | |
530 | if tk: |
|
530 | if tk: | |
531 | tk.update() |
|
531 | tk.update() | |
532 |
|
532 | |||
533 | def hijack_wx(): |
|
533 | def hijack_wx(): | |
534 | """Modifies wxPython's MainLoop with a dummy so user code does not |
|
534 | """Modifies wxPython's MainLoop with a dummy so user code does not | |
535 | block IPython. The hijacked mainloop function is returned. |
|
535 | block IPython. The hijacked mainloop function is returned. | |
536 | """ |
|
536 | """ | |
537 | def dummy_mainloop(*args, **kw): |
|
537 | def dummy_mainloop(*args, **kw): | |
538 | pass |
|
538 | pass | |
539 | import wxPython |
|
539 | import wxPython | |
540 | ver = wxPython.__version__ |
|
540 | ver = wxPython.__version__ | |
541 | orig_mainloop = None |
|
541 | orig_mainloop = None | |
542 | if ver[:3] >= '2.5': |
|
542 | if ver[:3] >= '2.5': | |
543 | import wx |
|
543 | import wx | |
544 | if hasattr(wx, '_core_'): core = getattr(wx, '_core_') |
|
544 | if hasattr(wx, '_core_'): core = getattr(wx, '_core_') | |
545 | elif hasattr(wx, '_core'): core = getattr(wx, '_core') |
|
545 | elif hasattr(wx, '_core'): core = getattr(wx, '_core') | |
546 | else: raise AttributeError('Could not find wx core module') |
|
546 | else: raise AttributeError('Could not find wx core module') | |
547 | orig_mainloop = core.PyApp_MainLoop |
|
547 | orig_mainloop = core.PyApp_MainLoop | |
548 | core.PyApp_MainLoop = dummy_mainloop |
|
548 | core.PyApp_MainLoop = dummy_mainloop | |
549 | elif ver[:3] == '2.4': |
|
549 | elif ver[:3] == '2.4': | |
550 | orig_mainloop = wxPython.wxc.wxPyApp_MainLoop |
|
550 | orig_mainloop = wxPython.wxc.wxPyApp_MainLoop | |
551 | wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop |
|
551 | wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop | |
552 | else: |
|
552 | else: | |
553 | warn("Unable to find either wxPython version 2.4 or >= 2.5.") |
|
553 | warn("Unable to find either wxPython version 2.4 or >= 2.5.") | |
554 | return orig_mainloop |
|
554 | return orig_mainloop | |
555 |
|
555 | |||
556 | def hijack_gtk(): |
|
556 | def hijack_gtk(): | |
557 | """Modifies pyGTK's mainloop with a dummy so user code does not |
|
557 | """Modifies pyGTK's mainloop with a dummy so user code does not | |
558 | block IPython. This function returns the original `gtk.mainloop` |
|
558 | block IPython. This function returns the original `gtk.mainloop` | |
559 | function that has been hijacked. |
|
559 | function that has been hijacked. | |
560 | """ |
|
560 | """ | |
561 | def dummy_mainloop(*args, **kw): |
|
561 | def dummy_mainloop(*args, **kw): | |
562 | pass |
|
562 | pass | |
563 | import gtk |
|
563 | import gtk | |
564 | if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main |
|
564 | if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main | |
565 | else: orig_mainloop = gtk.mainloop |
|
565 | else: orig_mainloop = gtk.mainloop | |
566 | gtk.mainloop = dummy_mainloop |
|
566 | gtk.mainloop = dummy_mainloop | |
567 | gtk.main = dummy_mainloop |
|
567 | gtk.main = dummy_mainloop | |
568 | return orig_mainloop |
|
568 | return orig_mainloop | |
569 |
|
569 | |||
570 | #----------------------------------------------------------------------------- |
|
570 | #----------------------------------------------------------------------------- | |
571 | # The IPShell* classes below are the ones meant to be run by external code as |
|
571 | # The IPShell* classes below are the ones meant to be run by external code as | |
572 | # IPython instances. Note that unless a specific threading strategy is |
|
572 | # IPython instances. Note that unless a specific threading strategy is | |
573 | # desired, the factory function start() below should be used instead (it |
|
573 | # desired, the factory function start() below should be used instead (it | |
574 | # selects the proper threaded class). |
|
574 | # selects the proper threaded class). | |
575 |
|
575 | |||
576 | class IPShellGTK(threading.Thread): |
|
576 | class IPShellGTK(threading.Thread): | |
577 | """Run a gtk mainloop() in a separate thread. |
|
577 | """Run a gtk mainloop() in a separate thread. | |
578 |
|
578 | |||
579 | Python commands can be passed to the thread where they will be executed. |
|
579 | Python commands can be passed to the thread where they will be executed. | |
580 | This is implemented by periodically checking for passed code using a |
|
580 | This is implemented by periodically checking for passed code using a | |
581 | GTK timeout callback.""" |
|
581 | GTK timeout callback.""" | |
582 |
|
582 | |||
583 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
583 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
584 |
|
584 | |||
585 | def __init__(self,argv=None,user_ns=None,debug=1, |
|
585 | def __init__(self,argv=None,user_ns=None,debug=1, | |
586 | shell_class=MTInteractiveShell): |
|
586 | shell_class=MTInteractiveShell): | |
587 |
|
587 | |||
588 | import gtk |
|
588 | import gtk | |
589 |
|
589 | |||
590 | self.gtk = gtk |
|
590 | self.gtk = gtk | |
591 | self.gtk_mainloop = hijack_gtk() |
|
591 | self.gtk_mainloop = hijack_gtk() | |
592 |
|
592 | |||
593 | # Allows us to use both Tk and GTK. |
|
593 | # Allows us to use both Tk and GTK. | |
594 | self.tk = get_tk() |
|
594 | self.tk = get_tk() | |
595 |
|
595 | |||
596 | if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit |
|
596 | if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit | |
597 | else: mainquit = self.gtk.mainquit |
|
597 | else: mainquit = self.gtk.mainquit | |
598 |
|
598 | |||
599 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, |
|
599 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, | |
600 | shell_class=shell_class, |
|
600 | shell_class=shell_class, | |
601 | on_kill=[mainquit]) |
|
601 | on_kill=[mainquit]) | |
602 |
|
602 | |||
603 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
603 | # HACK: slot for banner in self; it will be passed to the mainloop | |
604 | # method only and .run() needs it. The actual value will be set by |
|
604 | # method only and .run() needs it. The actual value will be set by | |
605 | # .mainloop(). |
|
605 | # .mainloop(). | |
606 | self._banner = None |
|
606 | self._banner = None | |
607 |
|
607 | |||
608 | threading.Thread.__init__(self) |
|
608 | threading.Thread.__init__(self) | |
609 |
|
609 | |||
610 | def run(self): |
|
610 | def run(self): | |
611 | self.IP.mainloop(self._banner) |
|
611 | self.IP.mainloop(self._banner) | |
612 | self.IP.kill() |
|
612 | self.IP.kill() | |
613 |
|
613 | |||
614 | def mainloop(self,sys_exit=0,banner=None): |
|
614 | def mainloop(self,sys_exit=0,banner=None): | |
615 |
|
615 | |||
616 | self._banner = banner |
|
616 | self._banner = banner | |
617 |
|
617 | |||
618 | if self.gtk.pygtk_version >= (2,4,0): |
|
618 | if self.gtk.pygtk_version >= (2,4,0): | |
619 | import gobject |
|
619 | import gobject | |
620 | gobject.idle_add(self.on_timer) |
|
620 | gobject.idle_add(self.on_timer) | |
621 | else: |
|
621 | else: | |
622 | self.gtk.idle_add(self.on_timer) |
|
622 | self.gtk.idle_add(self.on_timer) | |
623 |
|
623 | |||
624 | if sys.platform != 'win32': |
|
624 | if sys.platform != 'win32': | |
625 | try: |
|
625 | try: | |
626 | if self.gtk.gtk_version[0] >= 2: |
|
626 | if self.gtk.gtk_version[0] >= 2: | |
627 | self.gtk.threads_init() |
|
627 | self.gtk.threads_init() | |
628 | except AttributeError: |
|
628 | except AttributeError: | |
629 | pass |
|
629 | pass | |
630 | except RuntimeError: |
|
630 | except RuntimeError: | |
631 | error('Your pyGTK likely has not been compiled with ' |
|
631 | error('Your pyGTK likely has not been compiled with ' | |
632 | 'threading support.\n' |
|
632 | 'threading support.\n' | |
633 | 'The exception printout is below.\n' |
|
633 | 'The exception printout is below.\n' | |
634 | 'You can either rebuild pyGTK with threads, or ' |
|
634 | 'You can either rebuild pyGTK with threads, or ' | |
635 | 'try using \n' |
|
635 | 'try using \n' | |
636 | 'matplotlib with a different backend (like Tk or WX).\n' |
|
636 | 'matplotlib with a different backend (like Tk or WX).\n' | |
637 | 'Note that matplotlib will most likely not work in its ' |
|
637 | 'Note that matplotlib will most likely not work in its ' | |
638 | 'current state!') |
|
638 | 'current state!') | |
639 | self.IP.InteractiveTB() |
|
639 | self.IP.InteractiveTB() | |
640 | self.start() |
|
640 | self.start() | |
641 | self.gtk.threads_enter() |
|
641 | self.gtk.threads_enter() | |
642 | self.gtk_mainloop() |
|
642 | self.gtk_mainloop() | |
643 | self.gtk.threads_leave() |
|
643 | self.gtk.threads_leave() | |
644 | self.join() |
|
644 | self.join() | |
645 |
|
645 | |||
646 | def on_timer(self): |
|
646 | def on_timer(self): | |
647 | update_tk(self.tk) |
|
647 | update_tk(self.tk) | |
648 | return self.IP.runcode() |
|
648 | return self.IP.runcode() | |
649 |
|
649 | |||
650 |
|
650 | |||
651 | class IPShellWX(threading.Thread): |
|
651 | class IPShellWX(threading.Thread): | |
652 | """Run a wx mainloop() in a separate thread. |
|
652 | """Run a wx mainloop() in a separate thread. | |
653 |
|
653 | |||
654 | Python commands can be passed to the thread where they will be executed. |
|
654 | Python commands can be passed to the thread where they will be executed. | |
655 | This is implemented by periodically checking for passed code using a |
|
655 | This is implemented by periodically checking for passed code using a | |
656 | GTK timeout callback.""" |
|
656 | GTK timeout callback.""" | |
657 |
|
657 | |||
658 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
658 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
659 |
|
659 | |||
660 | def __init__(self,argv=None,user_ns=None,debug=1, |
|
660 | def __init__(self,argv=None,user_ns=None,debug=1, | |
661 | shell_class=MTInteractiveShell): |
|
661 | shell_class=MTInteractiveShell): | |
662 |
|
662 | |||
663 | import wxPython.wx as wx |
|
663 | import wxPython.wx as wx | |
664 |
|
664 | |||
665 | threading.Thread.__init__(self) |
|
665 | threading.Thread.__init__(self) | |
666 | self.wx = wx |
|
666 | self.wx = wx | |
667 | self.wx_mainloop = hijack_wx() |
|
667 | self.wx_mainloop = hijack_wx() | |
668 |
|
668 | |||
669 | # Allows us to use both Tk and GTK. |
|
669 | # Allows us to use both Tk and GTK. | |
670 | self.tk = get_tk() |
|
670 | self.tk = get_tk() | |
671 |
|
671 | |||
672 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, |
|
672 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, | |
673 | shell_class=shell_class, |
|
673 | shell_class=shell_class, | |
674 | on_kill=[self.wxexit]) |
|
674 | on_kill=[self.wxexit]) | |
675 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
675 | # HACK: slot for banner in self; it will be passed to the mainloop | |
676 | # method only and .run() needs it. The actual value will be set by |
|
676 | # method only and .run() needs it. The actual value will be set by | |
677 | # .mainloop(). |
|
677 | # .mainloop(). | |
678 | self._banner = None |
|
678 | self._banner = None | |
679 |
|
679 | |||
680 | self.app = None |
|
680 | self.app = None | |
681 |
|
681 | |||
682 | def wxexit(self, *args): |
|
682 | def wxexit(self, *args): | |
683 | if self.app is not None: |
|
683 | if self.app is not None: | |
684 | self.app.agent.timer.Stop() |
|
684 | self.app.agent.timer.Stop() | |
685 | self.app.ExitMainLoop() |
|
685 | self.app.ExitMainLoop() | |
686 |
|
686 | |||
687 | def run(self): |
|
687 | def run(self): | |
688 | self.IP.mainloop(self._banner) |
|
688 | self.IP.mainloop(self._banner) | |
689 | self.IP.kill() |
|
689 | self.IP.kill() | |
690 |
|
690 | |||
691 | def mainloop(self,sys_exit=0,banner=None): |
|
691 | def mainloop(self,sys_exit=0,banner=None): | |
692 |
|
692 | |||
693 | self._banner = banner |
|
693 | self._banner = banner | |
694 |
|
694 | |||
695 | self.start() |
|
695 | self.start() | |
696 |
|
696 | |||
697 | class TimerAgent(self.wx.wxMiniFrame): |
|
697 | class TimerAgent(self.wx.wxMiniFrame): | |
698 | wx = self.wx |
|
698 | wx = self.wx | |
699 | IP = self.IP |
|
699 | IP = self.IP | |
700 | tk = self.tk |
|
700 | tk = self.tk | |
701 | def __init__(self, parent, interval): |
|
701 | def __init__(self, parent, interval): | |
702 | style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ |
|
702 | style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ | |
703 | self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200), |
|
703 | self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200), | |
704 | size=(100, 100),style=style) |
|
704 | size=(100, 100),style=style) | |
705 | self.Show(False) |
|
705 | self.Show(False) | |
706 | self.interval = interval |
|
706 | self.interval = interval | |
707 | self.timerId = self.wx.wxNewId() |
|
707 | self.timerId = self.wx.wxNewId() | |
708 |
|
708 | |||
709 | def StartWork(self): |
|
709 | def StartWork(self): | |
710 | self.timer = self.wx.wxTimer(self, self.timerId) |
|
710 | self.timer = self.wx.wxTimer(self, self.timerId) | |
711 | self.wx.EVT_TIMER(self, self.timerId, self.OnTimer) |
|
711 | self.wx.EVT_TIMER(self, self.timerId, self.OnTimer) | |
712 | self.timer.Start(self.interval) |
|
712 | self.timer.Start(self.interval) | |
713 |
|
713 | |||
714 | def OnTimer(self, event): |
|
714 | def OnTimer(self, event): | |
715 | update_tk(self.tk) |
|
715 | update_tk(self.tk) | |
716 | self.IP.runcode() |
|
716 | self.IP.runcode() | |
717 |
|
717 | |||
718 | class App(self.wx.wxApp): |
|
718 | class App(self.wx.wxApp): | |
719 | wx = self.wx |
|
719 | wx = self.wx | |
720 | TIMEOUT = self.TIMEOUT |
|
720 | TIMEOUT = self.TIMEOUT | |
721 | def OnInit(self): |
|
721 | def OnInit(self): | |
722 | 'Create the main window and insert the custom frame' |
|
722 | 'Create the main window and insert the custom frame' | |
723 | self.agent = TimerAgent(None, self.TIMEOUT) |
|
723 | self.agent = TimerAgent(None, self.TIMEOUT) | |
724 | self.agent.Show(self.wx.false) |
|
724 | self.agent.Show(self.wx.false) | |
725 | self.agent.StartWork() |
|
725 | self.agent.StartWork() | |
726 | return self.wx.true |
|
726 | return self.wx.true | |
727 |
|
727 | |||
728 | self.app = App(redirect=False) |
|
728 | self.app = App(redirect=False) | |
729 | self.wx_mainloop(self.app) |
|
729 | self.wx_mainloop(self.app) | |
730 | self.join() |
|
730 | self.join() | |
731 |
|
731 | |||
732 |
|
732 | |||
733 | class IPShellQt(threading.Thread): |
|
733 | class IPShellQt(threading.Thread): | |
734 | """Run a Qt event loop in a separate thread. |
|
734 | """Run a Qt event loop in a separate thread. | |
735 |
|
735 | |||
736 | Python commands can be passed to the thread where they will be executed. |
|
736 | Python commands can be passed to the thread where they will be executed. | |
737 | This is implemented by periodically checking for passed code using a |
|
737 | This is implemented by periodically checking for passed code using a | |
738 | Qt timer / slot.""" |
|
738 | Qt timer / slot.""" | |
739 |
|
739 | |||
740 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
740 | TIMEOUT = 100 # Millisecond interval between timeouts. | |
741 |
|
741 | |||
742 | def __init__(self,argv=None,user_ns=None,debug=0, |
|
742 | def __init__(self,argv=None,user_ns=None,debug=0, | |
743 | shell_class=MTInteractiveShell): |
|
743 | shell_class=MTInteractiveShell): | |
744 |
|
744 | |||
745 | import qt |
|
745 | import qt | |
746 |
|
746 | |||
747 | class newQApplication: |
|
747 | class newQApplication: | |
748 | def __init__( self ): |
|
748 | def __init__( self ): | |
749 | self.QApplication = qt.QApplication |
|
749 | self.QApplication = qt.QApplication | |
750 |
|
750 | |||
751 | def __call__( *args, **kwargs ): |
|
751 | def __call__( *args, **kwargs ): | |
752 | return qt.qApp |
|
752 | return qt.qApp | |
753 |
|
753 | |||
754 | def exec_loop( *args, **kwargs ): |
|
754 | def exec_loop( *args, **kwargs ): | |
755 | pass |
|
755 | pass | |
756 |
|
756 | |||
757 | def __getattr__( self, name ): |
|
757 | def __getattr__( self, name ): | |
758 | return getattr( self.QApplication, name ) |
|
758 | return getattr( self.QApplication, name ) | |
759 |
|
759 | |||
760 | qt.QApplication = newQApplication() |
|
760 | qt.QApplication = newQApplication() | |
761 |
|
761 | |||
762 | # Allows us to use both Tk and QT. |
|
762 | # Allows us to use both Tk and QT. | |
763 | self.tk = get_tk() |
|
763 | self.tk = get_tk() | |
764 |
|
764 | |||
765 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, |
|
765 | self.IP = make_IPython(argv,user_ns=user_ns,debug=debug, | |
766 | shell_class=shell_class, |
|
766 | shell_class=shell_class, | |
767 | on_kill=[qt.qApp.exit]) |
|
767 | on_kill=[qt.qApp.exit]) | |
768 |
|
768 | |||
769 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
769 | # HACK: slot for banner in self; it will be passed to the mainloop | |
770 | # method only and .run() needs it. The actual value will be set by |
|
770 | # method only and .run() needs it. The actual value will be set by | |
771 | # .mainloop(). |
|
771 | # .mainloop(). | |
772 | self._banner = None |
|
772 | self._banner = None | |
773 |
|
773 | |||
774 | threading.Thread.__init__(self) |
|
774 | threading.Thread.__init__(self) | |
775 |
|
775 | |||
776 | def run(self): |
|
776 | def run(self): | |
777 | self.IP.mainloop(self._banner) |
|
777 | self.IP.mainloop(self._banner) | |
778 | self.IP.kill() |
|
778 | self.IP.kill() | |
779 |
|
779 | |||
780 | def mainloop(self,sys_exit=0,banner=None): |
|
780 | def mainloop(self,sys_exit=0,banner=None): | |
781 |
|
781 | |||
782 | import qt |
|
782 | import qt | |
783 |
|
783 | |||
784 | self._banner = banner |
|
784 | self._banner = banner | |
785 |
|
785 | |||
786 | if qt.QApplication.startingUp(): |
|
786 | if qt.QApplication.startingUp(): | |
787 | a = qt.QApplication.QApplication(sys.argv) |
|
787 | a = qt.QApplication.QApplication(sys.argv) | |
788 | self.timer = qt.QTimer() |
|
788 | self.timer = qt.QTimer() | |
789 | qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer ) |
|
789 | qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer ) | |
790 |
|
790 | |||
791 | self.start() |
|
791 | self.start() | |
792 | self.timer.start( self.TIMEOUT, True ) |
|
792 | self.timer.start( self.TIMEOUT, True ) | |
793 | while True: |
|
793 | while True: | |
794 | if self.IP._kill: break |
|
794 | if self.IP._kill: break | |
795 | qt.qApp.exec_loop() |
|
795 | qt.qApp.exec_loop() | |
796 | self.join() |
|
796 | self.join() | |
797 |
|
797 | |||
798 | def on_timer(self): |
|
798 | def on_timer(self): | |
799 | update_tk(self.tk) |
|
799 | update_tk(self.tk) | |
800 | result = self.IP.runcode() |
|
800 | result = self.IP.runcode() | |
801 | self.timer.start( self.TIMEOUT, True ) |
|
801 | self.timer.start( self.TIMEOUT, True ) | |
802 | return result |
|
802 | return result | |
803 |
|
803 | |||
804 | # A set of matplotlib public IPython shell classes, for single-threaded |
|
804 | # A set of matplotlib public IPython shell classes, for single-threaded | |
805 | # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use. |
|
805 | # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use. | |
806 | class IPShellMatplotlib(IPShell): |
|
806 | class IPShellMatplotlib(IPShell): | |
807 | """Subclass IPShell with MatplotlibShell as the internal shell. |
|
807 | """Subclass IPShell with MatplotlibShell as the internal shell. | |
808 |
|
808 | |||
809 | Single-threaded class, meant for the Tk* and FLTK* backends. |
|
809 | Single-threaded class, meant for the Tk* and FLTK* backends. | |
810 |
|
810 | |||
811 | Having this on a separate class simplifies the external driver code.""" |
|
811 | Having this on a separate class simplifies the external driver code.""" | |
812 |
|
812 | |||
813 | def __init__(self,argv=None,user_ns=None,debug=1): |
|
813 | def __init__(self,argv=None,user_ns=None,debug=1): | |
814 | IPShell.__init__(self,argv,user_ns,debug,shell_class=MatplotlibShell) |
|
814 | IPShell.__init__(self,argv,user_ns,debug,shell_class=MatplotlibShell) | |
815 |
|
815 | |||
816 | class IPShellMatplotlibGTK(IPShellGTK): |
|
816 | class IPShellMatplotlibGTK(IPShellGTK): | |
817 | """Subclass IPShellGTK with MatplotlibMTShell as the internal shell. |
|
817 | """Subclass IPShellGTK with MatplotlibMTShell as the internal shell. | |
818 |
|
818 | |||
819 | Multi-threaded class, meant for the GTK* backends.""" |
|
819 | Multi-threaded class, meant for the GTK* backends.""" | |
820 |
|
820 | |||
821 | def __init__(self,argv=None,user_ns=None,debug=1): |
|
821 | def __init__(self,argv=None,user_ns=None,debug=1): | |
822 | IPShellGTK.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell) |
|
822 | IPShellGTK.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell) | |
823 |
|
823 | |||
824 | class IPShellMatplotlibWX(IPShellWX): |
|
824 | class IPShellMatplotlibWX(IPShellWX): | |
825 | """Subclass IPShellWX with MatplotlibMTShell as the internal shell. |
|
825 | """Subclass IPShellWX with MatplotlibMTShell as the internal shell. | |
826 |
|
826 | |||
827 | Multi-threaded class, meant for the WX* backends.""" |
|
827 | Multi-threaded class, meant for the WX* backends.""" | |
828 |
|
828 | |||
829 | def __init__(self,argv=None,user_ns=None,debug=1): |
|
829 | def __init__(self,argv=None,user_ns=None,debug=1): | |
830 | IPShellWX.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell) |
|
830 | IPShellWX.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell) | |
831 |
|
831 | |||
832 | class IPShellMatplotlibQt(IPShellQt): |
|
832 | class IPShellMatplotlibQt(IPShellQt): | |
833 | """Subclass IPShellQt with MatplotlibMTShell as the internal shell. |
|
833 | """Subclass IPShellQt with MatplotlibMTShell as the internal shell. | |
834 |
|
834 | |||
835 | Multi-threaded class, meant for the Qt* backends.""" |
|
835 | Multi-threaded class, meant for the Qt* backends.""" | |
836 |
|
836 | |||
837 | def __init__(self,argv=None,user_ns=None,debug=1): |
|
837 | def __init__(self,argv=None,user_ns=None,debug=1): | |
838 | IPShellQt.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell) |
|
838 | IPShellQt.__init__(self,argv,user_ns,debug,shell_class=MatplotlibMTShell) | |
839 |
|
839 | |||
840 | #----------------------------------------------------------------------------- |
|
840 | #----------------------------------------------------------------------------- | |
841 | # Factory functions to actually start the proper thread-aware shell |
|
841 | # Factory functions to actually start the proper thread-aware shell | |
842 |
|
842 | |||
843 | def _matplotlib_shell_class(): |
|
843 | def _matplotlib_shell_class(): | |
844 | """Factory function to handle shell class selection for matplotlib. |
|
844 | """Factory function to handle shell class selection for matplotlib. | |
845 |
|
845 | |||
846 | The proper shell class to use depends on the matplotlib backend, since |
|
846 | The proper shell class to use depends on the matplotlib backend, since | |
847 | each backend requires a different threading strategy.""" |
|
847 | each backend requires a different threading strategy.""" | |
848 |
|
848 | |||
849 | try: |
|
849 | try: | |
850 | import matplotlib |
|
850 | import matplotlib | |
851 | except ImportError: |
|
851 | except ImportError: | |
852 | error('matplotlib could NOT be imported! Starting normal IPython.') |
|
852 | error('matplotlib could NOT be imported! Starting normal IPython.') | |
853 | sh_class = IPShell |
|
853 | sh_class = IPShell | |
854 | else: |
|
854 | else: | |
855 | backend = matplotlib.rcParams['backend'] |
|
855 | backend = matplotlib.rcParams['backend'] | |
856 | if backend.startswith('GTK'): |
|
856 | if backend.startswith('GTK'): | |
857 | sh_class = IPShellMatplotlibGTK |
|
857 | sh_class = IPShellMatplotlibGTK | |
858 | elif backend.startswith('WX'): |
|
858 | elif backend.startswith('WX'): | |
859 | sh_class = IPShellMatplotlibWX |
|
859 | sh_class = IPShellMatplotlibWX | |
860 | elif backend.startswith('Qt'): |
|
860 | elif backend.startswith('Qt'): | |
861 | sh_class = IPShellMatplotlibQt |
|
861 | sh_class = IPShellMatplotlibQt | |
862 | else: |
|
862 | else: | |
863 | sh_class = IPShellMatplotlib |
|
863 | sh_class = IPShellMatplotlib | |
864 | #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg |
|
864 | #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg | |
865 | return sh_class |
|
865 | return sh_class | |
866 |
|
866 | |||
867 | # This is the one which should be called by external code. |
|
867 | # This is the one which should be called by external code. | |
868 | def start(): |
|
868 | def start(): | |
869 | """Return a running shell instance, dealing with threading options. |
|
869 | """Return a running shell instance, dealing with threading options. | |
870 |
|
870 | |||
871 | This is a factory function which will instantiate the proper IPython shell |
|
871 | This is a factory function which will instantiate the proper IPython shell | |
872 | based on the user's threading choice. Such a selector is needed because |
|
872 | based on the user's threading choice. Such a selector is needed because | |
873 | different GUI toolkits require different thread handling details.""" |
|
873 | different GUI toolkits require different thread handling details.""" | |
874 |
|
874 | |||
875 | global USE_TK |
|
875 | global USE_TK | |
876 | # Crude sys.argv hack to extract the threading options. |
|
876 | # Crude sys.argv hack to extract the threading options. | |
877 | if len(sys.argv) > 1: |
|
877 | argv = sys.argv | |
878 |
|
|
878 | if len(argv) > 1: | |
879 | arg2 = sys.argv[2] |
|
879 | if len(argv) > 2: | |
|
880 | arg2 = argv[2] | |||
880 | if arg2.endswith('-tk'): |
|
881 | if arg2.endswith('-tk'): | |
881 | USE_TK = True |
|
882 | USE_TK = True | |
882 |
arg1 = |
|
883 | arg1 = argv[1] | |
883 | if arg1.endswith('-gthread'): |
|
884 | if arg1.endswith('-gthread'): | |
884 | shell = IPShellGTK |
|
885 | shell = IPShellGTK | |
885 | elif arg1.endswith( '-qthread' ): |
|
886 | elif arg1.endswith( '-qthread' ): | |
886 | shell = IPShellQt |
|
887 | shell = IPShellQt | |
887 | elif arg1.endswith('-wthread'): |
|
888 | elif arg1.endswith('-wthread'): | |
888 | shell = IPShellWX |
|
889 | shell = IPShellWX | |
889 | elif arg1.endswith('-pylab'): |
|
890 | elif arg1.endswith('-pylab'): | |
890 | shell = _matplotlib_shell_class() |
|
891 | shell = _matplotlib_shell_class() | |
891 | else: |
|
892 | else: | |
892 | shell = IPShell |
|
893 | shell = IPShell | |
893 | else: |
|
894 | else: | |
894 | shell = IPShell |
|
895 | shell = IPShell | |
895 | return shell() |
|
896 | return shell() | |
896 |
|
897 | |||
897 | # Some aliases for backwards compatibility |
|
898 | # Some aliases for backwards compatibility | |
898 | IPythonShell = IPShell |
|
899 | IPythonShell = IPShell | |
899 | IPythonShellEmbed = IPShellEmbed |
|
900 | IPythonShellEmbed = IPShellEmbed | |
900 | #************************ End of file <Shell.py> *************************** |
|
901 | #************************ End of file <Shell.py> *************************** |
@@ -1,2112 +1,2132 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | IPython -- An enhanced Interactive Python |
|
3 | IPython -- An enhanced Interactive Python | |
4 |
|
4 | |||
5 | Requires Python 2.1 or newer. |
|
5 | Requires Python 2.1 or newer. | |
6 |
|
6 | |||
7 | This file contains all the classes and helper functions specific to IPython. |
|
7 | This file contains all the classes and helper functions specific to IPython. | |
8 |
|
8 | |||
9 |
$Id: iplib.py 95 |
|
9 | $Id: iplib.py 952 2005-12-26 17:51:33Z fperez $ | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #***************************************************************************** |
|
12 | #***************************************************************************** | |
13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
14 | # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu> |
|
14 | # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu> | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | # |
|
18 | # | |
19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
19 | # Note: this code originally subclassed code.InteractiveConsole from the | |
20 | # Python standard library. Over time, much of that class has been copied |
|
20 | # Python standard library. Over time, much of that class has been copied | |
21 | # verbatim here for modifications which could not be accomplished by |
|
21 | # verbatim here for modifications which could not be accomplished by | |
22 | # subclassing. The Python License (sec. 2) allows for this, but it's always |
|
22 | # subclassing. The Python License (sec. 2) allows for this, but it's always | |
23 | # nice to acknowledge credit where credit is due. |
|
23 | # nice to acknowledge credit where credit is due. | |
24 | #***************************************************************************** |
|
24 | #***************************************************************************** | |
25 |
|
25 | |||
26 | #**************************************************************************** |
|
26 | #**************************************************************************** | |
27 | # Modules and globals |
|
27 | # Modules and globals | |
28 |
|
28 | |||
29 | from __future__ import generators # for 2.2 backwards-compatibility |
|
29 | from __future__ import generators # for 2.2 backwards-compatibility | |
30 |
|
30 | |||
31 | from IPython import Release |
|
31 | from IPython import Release | |
32 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
32 | __author__ = '%s <%s>\n%s <%s>' % \ | |
33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) | |
34 | __license__ = Release.license |
|
34 | __license__ = Release.license | |
35 | __version__ = Release.version |
|
35 | __version__ = Release.version | |
36 |
|
36 | |||
37 | # Python standard modules |
|
37 | # Python standard modules | |
38 | import __main__ |
|
38 | import __main__ | |
39 | import __builtin__ |
|
39 | import __builtin__ | |
40 | import exceptions |
|
40 | import exceptions | |
41 | import keyword |
|
41 | import keyword | |
42 | import new |
|
42 | import new | |
43 | import os, sys, shutil |
|
43 | import os, sys, shutil | |
44 | import code, glob, types, re |
|
44 | import code, glob, types, re | |
45 | import string, StringIO |
|
45 | import string, StringIO | |
46 | import inspect, pydoc |
|
46 | import inspect, pydoc | |
47 | import bdb, pdb |
|
47 | import bdb, pdb | |
48 | import UserList # don't subclass list so this works with Python2.1 |
|
48 | import UserList # don't subclass list so this works with Python2.1 | |
49 | from pprint import pprint, pformat |
|
49 | from pprint import pprint, pformat | |
50 | import cPickle as pickle |
|
50 | import cPickle as pickle | |
51 | import traceback |
|
51 | import traceback | |
52 | from codeop import CommandCompiler |
|
52 | from codeop import CommandCompiler | |
53 |
|
53 | |||
54 | # IPython's own modules |
|
54 | # IPython's own modules | |
55 | import IPython |
|
55 | import IPython | |
56 | from IPython import OInspect,PyColorize,ultraTB |
|
56 | from IPython import OInspect,PyColorize,ultraTB | |
57 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
57 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names | |
58 | from IPython.Logger import Logger |
|
58 | from IPython.Logger import Logger | |
59 | from IPython.Magic import Magic,magic2python,shlex_split |
|
59 | from IPython.Magic import Magic,magic2python,shlex_split | |
60 | from IPython.usage import cmd_line_usage,interactive_usage |
|
60 | from IPython.usage import cmd_line_usage,interactive_usage | |
61 | from IPython.Struct import Struct |
|
61 | from IPython.Struct import Struct | |
62 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
62 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns | |
63 | from IPython.FakeModule import FakeModule |
|
63 | from IPython.FakeModule import FakeModule | |
64 | from IPython.background_jobs import BackgroundJobManager |
|
64 | from IPython.background_jobs import BackgroundJobManager | |
65 | from IPython.PyColorize import Parser |
|
65 | from IPython.PyColorize import Parser | |
66 | from IPython.genutils import * |
|
66 | from IPython.genutils import * | |
67 |
|
67 | |||
68 | # Global pointer to the running |
|
68 | # Global pointer to the running | |
69 |
|
69 | |||
70 | # store the builtin raw_input globally, and use this always, in case user code |
|
70 | # store the builtin raw_input globally, and use this always, in case user code | |
71 | # overwrites it (like wx.py.PyShell does) |
|
71 | # overwrites it (like wx.py.PyShell does) | |
72 | raw_input_original = raw_input |
|
72 | raw_input_original = raw_input | |
73 |
|
73 | |||
74 | #**************************************************************************** |
|
74 | #**************************************************************************** | |
75 | # Some utility function definitions |
|
75 | # Some utility function definitions | |
76 |
|
76 | |||
77 | class Bunch: pass |
|
77 | class Bunch: pass | |
78 |
|
78 | |||
79 | def esc_quotes(strng): |
|
79 | def esc_quotes(strng): | |
80 | """Return the input string with single and double quotes escaped out""" |
|
80 | """Return the input string with single and double quotes escaped out""" | |
81 |
|
81 | |||
82 | return strng.replace('"','\\"').replace("'","\\'") |
|
82 | return strng.replace('"','\\"').replace("'","\\'") | |
83 |
|
83 | |||
84 | def import_fail_info(mod_name,fns=None): |
|
84 | def import_fail_info(mod_name,fns=None): | |
85 | """Inform load failure for a module.""" |
|
85 | """Inform load failure for a module.""" | |
86 |
|
86 | |||
87 | if fns == None: |
|
87 | if fns == None: | |
88 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
88 | warn("Loading of %s failed.\n" % (mod_name,)) | |
89 | else: |
|
89 | else: | |
90 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
90 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) | |
91 |
|
91 | |||
92 | def qw_lol(indata): |
|
92 | def qw_lol(indata): | |
93 | """qw_lol('a b') -> [['a','b']], |
|
93 | """qw_lol('a b') -> [['a','b']], | |
94 | otherwise it's just a call to qw(). |
|
94 | otherwise it's just a call to qw(). | |
95 |
|
95 | |||
96 | We need this to make sure the modules_some keys *always* end up as a |
|
96 | We need this to make sure the modules_some keys *always* end up as a | |
97 | list of lists.""" |
|
97 | list of lists.""" | |
98 |
|
98 | |||
99 | if type(indata) in StringTypes: |
|
99 | if type(indata) in StringTypes: | |
100 | return [qw(indata)] |
|
100 | return [qw(indata)] | |
101 | else: |
|
101 | else: | |
102 | return qw(indata) |
|
102 | return qw(indata) | |
103 |
|
103 | |||
104 | def ipmagic(arg_s): |
|
104 | def ipmagic(arg_s): | |
105 | """Call a magic function by name. |
|
105 | """Call a magic function by name. | |
106 |
|
106 | |||
107 | Input: a string containing the name of the magic function to call and any |
|
107 | Input: a string containing the name of the magic function to call and any | |
108 | additional arguments to be passed to the magic. |
|
108 | additional arguments to be passed to the magic. | |
109 |
|
109 | |||
110 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
110 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython | |
111 | prompt: |
|
111 | prompt: | |
112 |
|
112 | |||
113 | In[1]: %name -opt foo bar |
|
113 | In[1]: %name -opt foo bar | |
114 |
|
114 | |||
115 | To call a magic without arguments, simply use ipmagic('name'). |
|
115 | To call a magic without arguments, simply use ipmagic('name'). | |
116 |
|
116 | |||
117 | This provides a proper Python function to call IPython's magics in any |
|
117 | This provides a proper Python function to call IPython's magics in any | |
118 | valid Python code you can type at the interpreter, including loops and |
|
118 | valid Python code you can type at the interpreter, including loops and | |
119 | compound statements. It is added by IPython to the Python builtin |
|
119 | compound statements. It is added by IPython to the Python builtin | |
120 | namespace upon initialization.""" |
|
120 | namespace upon initialization.""" | |
121 |
|
121 | |||
122 | args = arg_s.split(' ',1) |
|
122 | args = arg_s.split(' ',1) | |
123 | magic_name = args[0] |
|
123 | magic_name = args[0] | |
124 | if magic_name.startswith(__IPYTHON__.ESC_MAGIC): |
|
124 | if magic_name.startswith(__IPYTHON__.ESC_MAGIC): | |
125 | magic_name = magic_name[1:] |
|
125 | magic_name = magic_name[1:] | |
126 | try: |
|
126 | try: | |
127 | magic_args = args[1] |
|
127 | magic_args = args[1] | |
128 | except IndexError: |
|
128 | except IndexError: | |
129 | magic_args = '' |
|
129 | magic_args = '' | |
130 | fn = getattr(__IPYTHON__,'magic_'+magic_name,None) |
|
130 | fn = getattr(__IPYTHON__,'magic_'+magic_name,None) | |
131 | if fn is None: |
|
131 | if fn is None: | |
132 | error("Magic function `%s` not found." % magic_name) |
|
132 | error("Magic function `%s` not found." % magic_name) | |
133 | else: |
|
133 | else: | |
134 | magic_args = __IPYTHON__.var_expand(magic_args) |
|
134 | magic_args = __IPYTHON__.var_expand(magic_args) | |
135 | return fn(magic_args) |
|
135 | return fn(magic_args) | |
136 |
|
136 | |||
137 | def ipalias(arg_s): |
|
137 | def ipalias(arg_s): | |
138 | """Call an alias by name. |
|
138 | """Call an alias by name. | |
139 |
|
139 | |||
140 | Input: a string containing the name of the alias to call and any |
|
140 | Input: a string containing the name of the alias to call and any | |
141 | additional arguments to be passed to the magic. |
|
141 | additional arguments to be passed to the magic. | |
142 |
|
142 | |||
143 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
143 | ipalias('name -opt foo bar') is equivalent to typing at the ipython | |
144 | prompt: |
|
144 | prompt: | |
145 |
|
145 | |||
146 | In[1]: name -opt foo bar |
|
146 | In[1]: name -opt foo bar | |
147 |
|
147 | |||
148 | To call an alias without arguments, simply use ipalias('name'). |
|
148 | To call an alias without arguments, simply use ipalias('name'). | |
149 |
|
149 | |||
150 | This provides a proper Python function to call IPython's aliases in any |
|
150 | This provides a proper Python function to call IPython's aliases in any | |
151 | valid Python code you can type at the interpreter, including loops and |
|
151 | valid Python code you can type at the interpreter, including loops and | |
152 | compound statements. It is added by IPython to the Python builtin |
|
152 | compound statements. It is added by IPython to the Python builtin | |
153 | namespace upon initialization.""" |
|
153 | namespace upon initialization.""" | |
154 |
|
154 | |||
155 | args = arg_s.split(' ',1) |
|
155 | args = arg_s.split(' ',1) | |
156 | alias_name = args[0] |
|
156 | alias_name = args[0] | |
157 | try: |
|
157 | try: | |
158 | alias_args = args[1] |
|
158 | alias_args = args[1] | |
159 | except IndexError: |
|
159 | except IndexError: | |
160 | alias_args = '' |
|
160 | alias_args = '' | |
161 | if alias_name in __IPYTHON__.alias_table: |
|
161 | if alias_name in __IPYTHON__.alias_table: | |
162 | __IPYTHON__.call_alias(alias_name,alias_args) |
|
162 | __IPYTHON__.call_alias(alias_name,alias_args) | |
163 | else: |
|
163 | else: | |
164 | error("Alias `%s` not found." % alias_name) |
|
164 | error("Alias `%s` not found." % alias_name) | |
165 |
|
165 | |||
166 | #----------------------------------------------------------------------------- |
|
166 | #----------------------------------------------------------------------------- | |
167 | # Local use classes |
|
167 | # Local use classes | |
168 | try: |
|
168 | try: | |
169 | from IPython import FlexCompleter |
|
169 | from IPython import FlexCompleter | |
170 |
|
170 | |||
171 | class MagicCompleter(FlexCompleter.Completer): |
|
171 | class MagicCompleter(FlexCompleter.Completer): | |
172 | """Extension of the completer class to work on %-prefixed lines.""" |
|
172 | """Extension of the completer class to work on %-prefixed lines.""" | |
173 |
|
173 | |||
174 |
def __init__(self,shell,namespace=None, |
|
174 | def __init__(self,shell,namespace=None,global_namespace=None, | |
|
175 | omit__names=0,alias_table=None): | |||
175 | """MagicCompleter() -> completer |
|
176 | """MagicCompleter() -> completer | |
176 |
|
177 | |||
177 | Return a completer object suitable for use by the readline library |
|
178 | Return a completer object suitable for use by the readline library | |
178 | via readline.set_completer(). |
|
179 | via readline.set_completer(). | |
179 |
|
180 | |||
180 | Inputs: |
|
181 | Inputs: | |
181 |
|
182 | |||
182 | - shell: a pointer to the ipython shell itself. This is needed |
|
183 | - shell: a pointer to the ipython shell itself. This is needed | |
183 | because this completer knows about magic functions, and those can |
|
184 | because this completer knows about magic functions, and those can | |
184 | only be accessed via the ipython instance. |
|
185 | only be accessed via the ipython instance. | |
185 |
|
186 | |||
186 | - namespace: an optional dict where completions are performed. |
|
187 | - namespace: an optional dict where completions are performed. | |
|
188 | ||||
|
189 | - global_namespace: secondary optional dict for completions, to | |||
|
190 | handle cases (such as IPython embedded inside functions) where | |||
|
191 | both Python scopes are visible. | |||
187 |
|
192 | |||
188 | - The optional omit__names parameter sets the completer to omit the |
|
193 | - The optional omit__names parameter sets the completer to omit the | |
189 | 'magic' names (__magicname__) for python objects unless the text |
|
194 | 'magic' names (__magicname__) for python objects unless the text | |
190 | to be completed explicitly starts with one or more underscores. |
|
195 | to be completed explicitly starts with one or more underscores. | |
191 |
|
196 | |||
192 | - If alias_table is supplied, it should be a dictionary of aliases |
|
197 | - If alias_table is supplied, it should be a dictionary of aliases | |
193 | to complete. """ |
|
198 | to complete. """ | |
194 |
|
199 | |||
195 | FlexCompleter.Completer.__init__(self,namespace) |
|
200 | FlexCompleter.Completer.__init__(self,namespace) | |
196 | self.magic_prefix = shell.name+'.magic_' |
|
201 | self.magic_prefix = shell.name+'.magic_' | |
197 | self.magic_escape = shell.ESC_MAGIC |
|
202 | self.magic_escape = shell.ESC_MAGIC | |
198 | self.readline = FlexCompleter.readline |
|
203 | self.readline = FlexCompleter.readline | |
199 | delims = self.readline.get_completer_delims() |
|
204 | delims = self.readline.get_completer_delims() | |
200 | delims = delims.replace(self.magic_escape,'') |
|
205 | delims = delims.replace(self.magic_escape,'') | |
201 | self.readline.set_completer_delims(delims) |
|
206 | self.readline.set_completer_delims(delims) | |
202 | self.get_line_buffer = self.readline.get_line_buffer |
|
207 | self.get_line_buffer = self.readline.get_line_buffer | |
203 | self.omit__names = omit__names |
|
208 | self.omit__names = omit__names | |
204 | self.merge_completions = shell.rc.readline_merge_completions |
|
209 | self.merge_completions = shell.rc.readline_merge_completions | |
205 |
|
210 | |||
206 | if alias_table is None: |
|
211 | if alias_table is None: | |
207 | alias_table = {} |
|
212 | alias_table = {} | |
208 | self.alias_table = alias_table |
|
213 | self.alias_table = alias_table | |
209 | # Regexp to split filenames with spaces in them |
|
214 | # Regexp to split filenames with spaces in them | |
210 | self.space_name_re = re.compile(r'([^\\] )') |
|
215 | self.space_name_re = re.compile(r'([^\\] )') | |
211 | # Hold a local ref. to glob.glob for speed |
|
216 | # Hold a local ref. to glob.glob for speed | |
212 | self.glob = glob.glob |
|
217 | self.glob = glob.glob | |
213 | # Special handling of backslashes needed in win32 platforms |
|
218 | # Special handling of backslashes needed in win32 platforms | |
214 | if sys.platform == "win32": |
|
219 | if sys.platform == "win32": | |
215 | self.clean_glob = self._clean_glob_win32 |
|
220 | self.clean_glob = self._clean_glob_win32 | |
216 | else: |
|
221 | else: | |
217 | self.clean_glob = self._clean_glob |
|
222 | self.clean_glob = self._clean_glob | |
218 | self.matchers = [self.python_matches, |
|
223 | self.matchers = [self.python_matches, | |
219 | self.file_matches, |
|
224 | self.file_matches, | |
220 | self.alias_matches, |
|
225 | self.alias_matches, | |
221 | self.python_func_kw_matches] |
|
226 | self.python_func_kw_matches] | |
222 |
|
227 | |||
223 | # Code contributed by Alex Schmolck, for ipython/emacs integration |
|
228 | # Code contributed by Alex Schmolck, for ipython/emacs integration | |
224 | def all_completions(self, text): |
|
229 | def all_completions(self, text): | |
225 | """Return all possible completions for the benefit of emacs.""" |
|
230 | """Return all possible completions for the benefit of emacs.""" | |
226 |
|
231 | |||
227 | completions = [] |
|
232 | completions = [] | |
|
233 | comp_append = completions.append | |||
228 | try: |
|
234 | try: | |
229 | for i in xrange(sys.maxint): |
|
235 | for i in xrange(sys.maxint): | |
230 | res = self.complete(text, i) |
|
236 | res = self.complete(text, i) | |
231 |
|
237 | |||
232 | if not res: break |
|
238 | if not res: break | |
233 |
|
239 | |||
234 |
comp |
|
240 | comp_append(res) | |
235 | #XXX workaround for ``notDefined.<tab>`` |
|
241 | #XXX workaround for ``notDefined.<tab>`` | |
236 | except NameError: |
|
242 | except NameError: | |
237 | pass |
|
243 | pass | |
238 | return completions |
|
244 | return completions | |
239 | # /end Alex Schmolck code. |
|
245 | # /end Alex Schmolck code. | |
240 |
|
246 | |||
241 | def _clean_glob(self,text): |
|
247 | def _clean_glob(self,text): | |
242 | return self.glob("%s*" % text) |
|
248 | return self.glob("%s*" % text) | |
243 |
|
249 | |||
244 | def _clean_glob_win32(self,text): |
|
250 | def _clean_glob_win32(self,text): | |
245 | return [f.replace("\\","/") |
|
251 | return [f.replace("\\","/") | |
246 | for f in self.glob("%s*" % text)] |
|
252 | for f in self.glob("%s*" % text)] | |
247 |
|
253 | |||
248 | def file_matches(self, text): |
|
254 | def file_matches(self, text): | |
249 | """Match filneames, expanding ~USER type strings. |
|
255 | """Match filneames, expanding ~USER type strings. | |
250 |
|
256 | |||
251 | Most of the seemingly convoluted logic in this completer is an |
|
257 | Most of the seemingly convoluted logic in this completer is an | |
252 | attempt to handle filenames with spaces in them. And yet it's not |
|
258 | attempt to handle filenames with spaces in them. And yet it's not | |
253 | quite perfect, because Python's readline doesn't expose all of the |
|
259 | quite perfect, because Python's readline doesn't expose all of the | |
254 | GNU readline details needed for this to be done correctly. |
|
260 | GNU readline details needed for this to be done correctly. | |
255 |
|
261 | |||
256 | For a filename with a space in it, the printed completions will be |
|
262 | For a filename with a space in it, the printed completions will be | |
257 | only the parts after what's already been typed (instead of the |
|
263 | only the parts after what's already been typed (instead of the | |
258 | full completions, as is normally done). I don't think with the |
|
264 | full completions, as is normally done). I don't think with the | |
259 | current (as of Python 2.3) Python readline it's possible to do |
|
265 | current (as of Python 2.3) Python readline it's possible to do | |
260 | better.""" |
|
266 | better.""" | |
261 |
|
267 | |||
262 | #print 'Completer->file_matches: <%s>' % text # dbg |
|
268 | #print 'Completer->file_matches: <%s>' % text # dbg | |
263 |
|
269 | |||
264 | # chars that require escaping with backslash - i.e. chars |
|
270 | # chars that require escaping with backslash - i.e. chars | |
265 | # that readline treats incorrectly as delimiters, but we |
|
271 | # that readline treats incorrectly as delimiters, but we | |
266 | # don't want to treat as delimiters in filename matching |
|
272 | # don't want to treat as delimiters in filename matching | |
267 | # when escaped with backslash |
|
273 | # when escaped with backslash | |
268 |
|
274 | |||
269 | protectables = ' ()[]{}' |
|
275 | protectables = ' ()[]{}' | |
270 |
|
276 | |||
271 | def protect_filename(s): |
|
277 | def protect_filename(s): | |
272 | return "".join([(ch in protectables and '\\' + ch or ch) |
|
278 | return "".join([(ch in protectables and '\\' + ch or ch) | |
273 | for ch in s]) |
|
279 | for ch in s]) | |
274 |
|
280 | |||
275 | lbuf = self.get_line_buffer()[:self.readline.get_endidx()] |
|
281 | lbuf = self.get_line_buffer()[:self.readline.get_endidx()] | |
276 | open_quotes = 0 # track strings with open quotes |
|
282 | open_quotes = 0 # track strings with open quotes | |
277 | try: |
|
283 | try: | |
278 | lsplit = shlex_split(lbuf)[-1] |
|
284 | lsplit = shlex_split(lbuf)[-1] | |
279 | except ValueError: |
|
285 | except ValueError: | |
280 | # typically an unmatched ", or backslash without escaped char. |
|
286 | # typically an unmatched ", or backslash without escaped char. | |
281 | if lbuf.count('"')==1: |
|
287 | if lbuf.count('"')==1: | |
282 | open_quotes = 1 |
|
288 | open_quotes = 1 | |
283 | lsplit = lbuf.split('"')[-1] |
|
289 | lsplit = lbuf.split('"')[-1] | |
284 | elif lbuf.count("'")==1: |
|
290 | elif lbuf.count("'")==1: | |
285 | open_quotes = 1 |
|
291 | open_quotes = 1 | |
286 | lsplit = lbuf.split("'")[-1] |
|
292 | lsplit = lbuf.split("'")[-1] | |
287 | else: |
|
293 | else: | |
288 | return None |
|
294 | return None | |
289 | except IndexError: |
|
295 | except IndexError: | |
290 | # tab pressed on empty line |
|
296 | # tab pressed on empty line | |
291 | lsplit = "" |
|
297 | lsplit = "" | |
292 |
|
298 | |||
293 | if lsplit != protect_filename(lsplit): |
|
299 | if lsplit != protect_filename(lsplit): | |
294 | # if protectables are found, do matching on the whole escaped |
|
300 | # if protectables are found, do matching on the whole escaped | |
295 | # name |
|
301 | # name | |
296 | has_protectables = 1 |
|
302 | has_protectables = 1 | |
297 | text0,text = text,lsplit |
|
303 | text0,text = text,lsplit | |
298 | else: |
|
304 | else: | |
299 | has_protectables = 0 |
|
305 | has_protectables = 0 | |
300 | text = os.path.expanduser(text) |
|
306 | text = os.path.expanduser(text) | |
301 |
|
307 | |||
302 | if text == "": |
|
308 | if text == "": | |
303 | return [protect_filename(f) for f in self.glob("*")] |
|
309 | return [protect_filename(f) for f in self.glob("*")] | |
304 |
|
310 | |||
305 | m0 = self.clean_glob(text.replace('\\','')) |
|
311 | m0 = self.clean_glob(text.replace('\\','')) | |
306 | if has_protectables: |
|
312 | if has_protectables: | |
307 | # If we had protectables, we need to revert our changes to the |
|
313 | # If we had protectables, we need to revert our changes to the | |
308 | # beginning of filename so that we don't double-write the part |
|
314 | # beginning of filename so that we don't double-write the part | |
309 | # of the filename we have so far |
|
315 | # of the filename we have so far | |
310 | len_lsplit = len(lsplit) |
|
316 | len_lsplit = len(lsplit) | |
311 | matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0] |
|
317 | matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0] | |
312 | else: |
|
318 | else: | |
313 | if open_quotes: |
|
319 | if open_quotes: | |
314 | # if we have a string with an open quote, we don't need to |
|
320 | # if we have a string with an open quote, we don't need to | |
315 | # protect the names at all (and we _shouldn't_, as it |
|
321 | # protect the names at all (and we _shouldn't_, as it | |
316 | # would cause bugs when the filesystem call is made). |
|
322 | # would cause bugs when the filesystem call is made). | |
317 | matches = m0 |
|
323 | matches = m0 | |
318 | else: |
|
324 | else: | |
319 | matches = [protect_filename(f) for f in m0] |
|
325 | matches = [protect_filename(f) for f in m0] | |
320 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
326 | if len(matches) == 1 and os.path.isdir(matches[0]): | |
321 | # Takes care of links to directories also. Use '/' |
|
327 | # Takes care of links to directories also. Use '/' | |
322 | # explicitly, even under Windows, so that name completions |
|
328 | # explicitly, even under Windows, so that name completions | |
323 | # don't end up escaped. |
|
329 | # don't end up escaped. | |
324 | matches[0] += '/' |
|
330 | matches[0] += '/' | |
325 | return matches |
|
331 | return matches | |
326 |
|
332 | |||
327 | def alias_matches(self, text): |
|
333 | def alias_matches(self, text): | |
328 | """Match internal system aliases""" |
|
334 | """Match internal system aliases""" | |
329 | #print 'Completer->alias_matches:',text # dbg |
|
335 | #print 'Completer->alias_matches:',text # dbg | |
330 | text = os.path.expanduser(text) |
|
336 | text = os.path.expanduser(text) | |
331 | aliases = self.alias_table.keys() |
|
337 | aliases = self.alias_table.keys() | |
332 | if text == "": |
|
338 | if text == "": | |
333 | return aliases |
|
339 | return aliases | |
334 | else: |
|
340 | else: | |
335 | return [alias for alias in aliases if alias.startswith(text)] |
|
341 | return [alias for alias in aliases if alias.startswith(text)] | |
336 |
|
342 | |||
337 | def python_matches(self,text): |
|
343 | def python_matches(self,text): | |
338 | """Match attributes or global python names""" |
|
344 | """Match attributes or global python names""" | |
339 | #print 'Completer->python_matches' # dbg |
|
345 | #print 'Completer->python_matches' # dbg | |
340 | if "." in text: |
|
346 | if "." in text: | |
341 | try: |
|
347 | try: | |
342 | matches = self.attr_matches(text) |
|
348 | matches = self.attr_matches(text) | |
343 | if text.endswith('.') and self.omit__names: |
|
349 | if text.endswith('.') and self.omit__names: | |
344 | if self.omit__names == 1: |
|
350 | if self.omit__names == 1: | |
345 | # true if txt is _not_ a __ name, false otherwise: |
|
351 | # true if txt is _not_ a __ name, false otherwise: | |
346 | no__name = (lambda txt: |
|
352 | no__name = (lambda txt: | |
347 | re.match(r'.*\.__.*?__',txt) is None) |
|
353 | re.match(r'.*\.__.*?__',txt) is None) | |
348 | else: |
|
354 | else: | |
349 | # true if txt is _not_ a _ name, false otherwise: |
|
355 | # true if txt is _not_ a _ name, false otherwise: | |
350 | no__name = (lambda txt: |
|
356 | no__name = (lambda txt: | |
351 | re.match(r'.*\._.*?',txt) is None) |
|
357 | re.match(r'.*\._.*?',txt) is None) | |
352 | matches = filter(no__name, matches) |
|
358 | matches = filter(no__name, matches) | |
353 | except NameError: |
|
359 | except NameError: | |
354 | # catches <undefined attributes>.<tab> |
|
360 | # catches <undefined attributes>.<tab> | |
355 | matches = [] |
|
361 | matches = [] | |
356 | else: |
|
362 | else: | |
357 | matches = self.global_matches(text) |
|
363 | matches = self.global_matches(text) | |
358 | # this is so completion finds magics when automagic is on: |
|
364 | # this is so completion finds magics when automagic is on: | |
359 | if matches == [] and not text.startswith(os.sep): |
|
365 | if matches == [] and not text.startswith(os.sep): | |
360 | matches = self.attr_matches(self.magic_prefix+text) |
|
366 | matches = self.attr_matches(self.magic_prefix+text) | |
361 | return matches |
|
367 | return matches | |
362 |
|
368 | |||
363 | def _default_arguments(self, obj): |
|
369 | def _default_arguments(self, obj): | |
364 | """Return the list of default arguments of obj if it is callable, |
|
370 | """Return the list of default arguments of obj if it is callable, | |
365 | or empty list otherwise.""" |
|
371 | or empty list otherwise.""" | |
366 |
|
372 | |||
367 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
373 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): | |
368 | # for classes, check for __init__,__new__ |
|
374 | # for classes, check for __init__,__new__ | |
369 | if inspect.isclass(obj): |
|
375 | if inspect.isclass(obj): | |
370 | obj = (getattr(obj,'__init__',None) or |
|
376 | obj = (getattr(obj,'__init__',None) or | |
371 | getattr(obj,'__new__',None)) |
|
377 | getattr(obj,'__new__',None)) | |
372 | # for all others, check if they are __call__able |
|
378 | # for all others, check if they are __call__able | |
373 | elif hasattr(obj, '__call__'): |
|
379 | elif hasattr(obj, '__call__'): | |
374 | obj = obj.__call__ |
|
380 | obj = obj.__call__ | |
375 | # XXX: is there a way to handle the builtins ? |
|
381 | # XXX: is there a way to handle the builtins ? | |
376 | try: |
|
382 | try: | |
377 | args,_,_1,defaults = inspect.getargspec(obj) |
|
383 | args,_,_1,defaults = inspect.getargspec(obj) | |
378 | if defaults: |
|
384 | if defaults: | |
379 | return args[-len(defaults):] |
|
385 | return args[-len(defaults):] | |
380 | except TypeError: pass |
|
386 | except TypeError: pass | |
381 | return [] |
|
387 | return [] | |
382 |
|
388 | |||
383 | def python_func_kw_matches(self,text): |
|
389 | def python_func_kw_matches(self,text): | |
384 | """Match named parameters (kwargs) of the last open function""" |
|
390 | """Match named parameters (kwargs) of the last open function""" | |
385 |
|
391 | |||
386 | if "." in text: # a parameter cannot be dotted |
|
392 | if "." in text: # a parameter cannot be dotted | |
387 | return [] |
|
393 | return [] | |
388 | try: regexp = self.__funcParamsRegex |
|
394 | try: regexp = self.__funcParamsRegex | |
389 | except AttributeError: |
|
395 | except AttributeError: | |
390 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
396 | regexp = self.__funcParamsRegex = re.compile(r''' | |
391 | '.*?' | # single quoted strings or |
|
397 | '.*?' | # single quoted strings or | |
392 | ".*?" | # double quoted strings or |
|
398 | ".*?" | # double quoted strings or | |
393 | \w+ | # identifier |
|
399 | \w+ | # identifier | |
394 | \S # other characters |
|
400 | \S # other characters | |
395 | ''', re.VERBOSE | re.DOTALL) |
|
401 | ''', re.VERBOSE | re.DOTALL) | |
396 | # 1. find the nearest identifier that comes before an unclosed |
|
402 | # 1. find the nearest identifier that comes before an unclosed | |
397 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
403 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" | |
398 | tokens = regexp.findall(self.get_line_buffer()) |
|
404 | tokens = regexp.findall(self.get_line_buffer()) | |
399 | tokens.reverse() |
|
405 | tokens.reverse() | |
400 | iterTokens = iter(tokens); openPar = 0 |
|
406 | iterTokens = iter(tokens); openPar = 0 | |
401 | for token in iterTokens: |
|
407 | for token in iterTokens: | |
402 | if token == ')': |
|
408 | if token == ')': | |
403 | openPar -= 1 |
|
409 | openPar -= 1 | |
404 | elif token == '(': |
|
410 | elif token == '(': | |
405 | openPar += 1 |
|
411 | openPar += 1 | |
406 | if openPar > 0: |
|
412 | if openPar > 0: | |
407 | # found the last unclosed parenthesis |
|
413 | # found the last unclosed parenthesis | |
408 | break |
|
414 | break | |
409 | else: |
|
415 | else: | |
410 | return [] |
|
416 | return [] | |
411 | # 2. Concatenate any dotted names (e.g. "foo.bar" for "foo.bar(x, pa" ) |
|
417 | # 2. Concatenate any dotted names (e.g. "foo.bar" for "foo.bar(x, pa" ) | |
412 | ids = [] |
|
418 | ids = [] | |
413 | isId = re.compile(r'\w+$').match |
|
419 | isId = re.compile(r'\w+$').match | |
414 | while True: |
|
420 | while True: | |
415 | try: |
|
421 | try: | |
416 | ids.append(iterTokens.next()) |
|
422 | ids.append(iterTokens.next()) | |
417 | if not isId(ids[-1]): |
|
423 | if not isId(ids[-1]): | |
418 | ids.pop(); break |
|
424 | ids.pop(); break | |
419 | if not iterTokens.next() == '.': |
|
425 | if not iterTokens.next() == '.': | |
420 | break |
|
426 | break | |
421 | except StopIteration: |
|
427 | except StopIteration: | |
422 | break |
|
428 | break | |
423 | # lookup the candidate callable matches either using global_matches |
|
429 | # lookup the candidate callable matches either using global_matches | |
424 | # or attr_matches for dotted names |
|
430 | # or attr_matches for dotted names | |
425 | if len(ids) == 1: |
|
431 | if len(ids) == 1: | |
426 | callableMatches = self.global_matches(ids[0]) |
|
432 | callableMatches = self.global_matches(ids[0]) | |
427 | else: |
|
433 | else: | |
428 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
434 | callableMatches = self.attr_matches('.'.join(ids[::-1])) | |
429 | argMatches = [] |
|
435 | argMatches = [] | |
430 | for callableMatch in callableMatches: |
|
436 | for callableMatch in callableMatches: | |
431 | try: namedArgs = self._default_arguments(eval(callableMatch, |
|
437 | try: namedArgs = self._default_arguments(eval(callableMatch, | |
432 | self.namespace)) |
|
438 | self.namespace)) | |
433 | except: continue |
|
439 | except: continue | |
434 | for namedArg in namedArgs: |
|
440 | for namedArg in namedArgs: | |
435 | if namedArg.startswith(text): |
|
441 | if namedArg.startswith(text): | |
436 | argMatches.append("%s=" %namedArg) |
|
442 | argMatches.append("%s=" %namedArg) | |
437 | return argMatches |
|
443 | return argMatches | |
438 |
|
444 | |||
439 | def complete(self, text, state): |
|
445 | def complete(self, text, state): | |
440 | """Return the next possible completion for 'text'. |
|
446 | """Return the next possible completion for 'text'. | |
441 |
|
447 | |||
442 | This is called successively with state == 0, 1, 2, ... until it |
|
448 | This is called successively with state == 0, 1, 2, ... until it | |
443 | returns None. The completion should begin with 'text'. """ |
|
449 | returns None. The completion should begin with 'text'. """ | |
444 |
|
450 | |||
445 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg |
|
451 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg | |
446 | magic_escape = self.magic_escape |
|
452 | magic_escape = self.magic_escape | |
447 | magic_prefix = self.magic_prefix |
|
453 | magic_prefix = self.magic_prefix | |
448 |
|
454 | |||
449 | try: |
|
455 | try: | |
450 | if text.startswith(magic_escape): |
|
456 | if text.startswith(magic_escape): | |
451 | text = text.replace(magic_escape,magic_prefix) |
|
457 | text = text.replace(magic_escape,magic_prefix) | |
452 | elif text.startswith('~'): |
|
458 | elif text.startswith('~'): | |
453 | text = os.path.expanduser(text) |
|
459 | text = os.path.expanduser(text) | |
454 | if state == 0: |
|
460 | if state == 0: | |
455 | # Extend the list of completions with the results of each |
|
461 | # Extend the list of completions with the results of each | |
456 | # matcher, so we return results to the user from all |
|
462 | # matcher, so we return results to the user from all | |
457 | # namespaces. |
|
463 | # namespaces. | |
458 | if self.merge_completions: |
|
464 | if self.merge_completions: | |
459 | self.matches = [] |
|
465 | self.matches = [] | |
460 | for matcher in self.matchers: |
|
466 | for matcher in self.matchers: | |
461 | self.matches.extend(matcher(text)) |
|
467 | self.matches.extend(matcher(text)) | |
462 | else: |
|
468 | else: | |
463 | for matcher in self.matchers: |
|
469 | for matcher in self.matchers: | |
464 | self.matches = matcher(text) |
|
470 | self.matches = matcher(text) | |
465 | if self.matches: |
|
471 | if self.matches: | |
466 | break |
|
472 | break | |
467 |
|
473 | |||
468 | try: |
|
474 | try: | |
469 | return self.matches[state].replace(magic_prefix,magic_escape) |
|
475 | return self.matches[state].replace(magic_prefix,magic_escape) | |
470 | except IndexError: |
|
476 | except IndexError: | |
471 | return None |
|
477 | return None | |
472 | except: |
|
478 | except: | |
473 | # If completion fails, don't annoy the user. |
|
479 | # If completion fails, don't annoy the user. | |
474 | pass |
|
480 | pass | |
475 |
|
481 | |||
476 | except ImportError: |
|
482 | except ImportError: | |
477 | pass # no readline support |
|
483 | pass # no readline support | |
478 |
|
484 | |||
479 | except KeyError: |
|
485 | except KeyError: | |
480 | pass # Windows doesn't set TERM, it doesn't matter |
|
486 | pass # Windows doesn't set TERM, it doesn't matter | |
481 |
|
487 | |||
482 |
|
488 | |||
483 | class InputList(UserList.UserList): |
|
489 | class InputList(UserList.UserList): | |
484 | """Class to store user input. |
|
490 | """Class to store user input. | |
485 |
|
491 | |||
486 | It's basically a list, but slices return a string instead of a list, thus |
|
492 | It's basically a list, but slices return a string instead of a list, thus | |
487 | allowing things like (assuming 'In' is an instance): |
|
493 | allowing things like (assuming 'In' is an instance): | |
488 |
|
494 | |||
489 | exec In[4:7] |
|
495 | exec In[4:7] | |
490 |
|
496 | |||
491 | or |
|
497 | or | |
492 |
|
498 | |||
493 | exec In[5:9] + In[14] + In[21:25]""" |
|
499 | exec In[5:9] + In[14] + In[21:25]""" | |
494 |
|
500 | |||
495 | def __getslice__(self,i,j): |
|
501 | def __getslice__(self,i,j): | |
496 | return ''.join(UserList.UserList.__getslice__(self,i,j)) |
|
502 | return ''.join(UserList.UserList.__getslice__(self,i,j)) | |
497 |
|
503 | |||
498 | #**************************************************************************** |
|
504 | #**************************************************************************** | |
499 | # Local use exceptions |
|
505 | # Local use exceptions | |
500 | class SpaceInInput(exceptions.Exception): |
|
506 | class SpaceInInput(exceptions.Exception): | |
501 | pass |
|
507 | pass | |
502 |
|
508 | |||
503 | #**************************************************************************** |
|
509 | #**************************************************************************** | |
504 | # Main IPython class |
|
510 | # Main IPython class | |
505 |
|
511 | |||
506 | class InteractiveShell(code.InteractiveConsole, Logger, Magic): |
|
512 | class InteractiveShell(code.InteractiveConsole, Logger, Magic): | |
507 | """An enhanced console for Python.""" |
|
513 | """An enhanced console for Python.""" | |
508 |
|
514 | |||
509 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
515 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), | |
510 | user_ns = None,user_global_ns=None,banner2='', |
|
516 | user_ns = None,user_global_ns=None,banner2='', | |
511 | custom_exceptions=((),None),embedded=False): |
|
517 | custom_exceptions=((),None),embedded=False): | |
512 |
|
518 | |||
513 | # Put a reference to self in builtins so that any form of embedded or |
|
519 | # Put a reference to self in builtins so that any form of embedded or | |
514 | # imported code can test for being inside IPython. |
|
520 | # imported code can test for being inside IPython. | |
515 | __builtin__.__IPYTHON__ = self |
|
521 | __builtin__.__IPYTHON__ = self | |
516 |
|
522 | |||
517 | # And load into builtins ipmagic/ipalias as well |
|
523 | # And load into builtins ipmagic/ipalias as well | |
518 | __builtin__.ipmagic = ipmagic |
|
524 | __builtin__.ipmagic = ipmagic | |
519 | __builtin__.ipalias = ipalias |
|
525 | __builtin__.ipalias = ipalias | |
520 |
|
526 | |||
521 | # Add to __builtin__ other parts of IPython's public API |
|
527 | # Add to __builtin__ other parts of IPython's public API | |
522 | __builtin__.ip_set_hook = self.set_hook |
|
528 | __builtin__.ip_set_hook = self.set_hook | |
523 |
|
529 | |||
524 | # Keep in the builtins a flag for when IPython is active. We set it |
|
530 | # Keep in the builtins a flag for when IPython is active. We set it | |
525 | # with setdefault so that multiple nested IPythons don't clobber one |
|
531 | # with setdefault so that multiple nested IPythons don't clobber one | |
526 | # another. Each will increase its value by one upon being activated, |
|
532 | # another. Each will increase its value by one upon being activated, | |
527 | # which also gives us a way to determine the nesting level. |
|
533 | # which also gives us a way to determine the nesting level. | |
528 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
534 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) | |
529 |
|
535 | |||
530 | # Inform the user of ipython's fast exit magics. |
|
536 | # Inform the user of ipython's fast exit magics. | |
531 | _exit = ' Use %Exit or %Quit to exit without confirmation.' |
|
537 | _exit = ' Use %Exit or %Quit to exit without confirmation.' | |
532 | __builtin__.exit += _exit |
|
538 | __builtin__.exit += _exit | |
533 | __builtin__.quit += _exit |
|
539 | __builtin__.quit += _exit | |
534 |
|
540 | |||
535 | # We need to know whether the instance is meant for embedding, since |
|
541 | # We need to know whether the instance is meant for embedding, since | |
536 | # global/local namespaces need to be handled differently in that case |
|
542 | # global/local namespaces need to be handled differently in that case | |
537 | self.embedded = embedded |
|
543 | self.embedded = embedded | |
538 |
|
544 | |||
539 | # compiler command |
|
545 | # compiler command | |
540 | self.compile = CommandCompiler() |
|
546 | self.compile = CommandCompiler() | |
541 |
|
547 | |||
542 | # User input buffer |
|
548 | # User input buffer | |
543 | self.buffer = [] |
|
549 | self.buffer = [] | |
544 |
|
550 | |||
545 | # Default name given in compilation of code |
|
551 | # Default name given in compilation of code | |
546 | self.filename = '<ipython console>' |
|
552 | self.filename = '<ipython console>' | |
547 |
|
553 | |||
548 | # Create the namespace where the user will operate. user_ns is |
|
554 | # Create the namespace where the user will operate. user_ns is | |
549 | # normally the only one used, and it is passed to the exec calls as |
|
555 | # normally the only one used, and it is passed to the exec calls as | |
550 | # the locals argument. But we do carry a user_global_ns namespace |
|
556 | # the locals argument. But we do carry a user_global_ns namespace | |
551 | # given as the exec 'globals' argument, This is useful in embedding |
|
557 | # given as the exec 'globals' argument, This is useful in embedding | |
552 | # situations where the ipython shell opens in a context where the |
|
558 | # situations where the ipython shell opens in a context where the | |
553 | # distinction between locals and globals is meaningful. |
|
559 | # distinction between locals and globals is meaningful. | |
554 |
|
560 | |||
555 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
561 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
556 | # level as a dict instead of a module. This is a manual fix, but I |
|
562 | # level as a dict instead of a module. This is a manual fix, but I | |
557 | # should really track down where the problem is coming from. Alex |
|
563 | # should really track down where the problem is coming from. Alex | |
558 | # Schmolck reported this problem first. |
|
564 | # Schmolck reported this problem first. | |
559 |
|
565 | |||
560 | # A useful post by Alex Martelli on this topic: |
|
566 | # A useful post by Alex Martelli on this topic: | |
561 | # Re: inconsistent value from __builtins__ |
|
567 | # Re: inconsistent value from __builtins__ | |
562 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
568 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
563 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
569 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
564 | # Gruppen: comp.lang.python |
|
570 | # Gruppen: comp.lang.python | |
565 | # Referenzen: 1 |
|
571 | # Referenzen: 1 | |
566 |
|
572 | |||
567 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
573 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
568 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
574 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
569 | # > <type 'dict'> |
|
575 | # > <type 'dict'> | |
570 | # > >>> print type(__builtins__) |
|
576 | # > >>> print type(__builtins__) | |
571 | # > <type 'module'> |
|
577 | # > <type 'module'> | |
572 | # > Is this difference in return value intentional? |
|
578 | # > Is this difference in return value intentional? | |
573 |
|
579 | |||
574 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
580 | # Well, it's documented that '__builtins__' can be either a dictionary | |
575 | # or a module, and it's been that way for a long time. Whether it's |
|
581 | # or a module, and it's been that way for a long time. Whether it's | |
576 | # intentional (or sensible), I don't know. In any case, the idea is that |
|
582 | # intentional (or sensible), I don't know. In any case, the idea is that | |
577 | # if you need to access the built-in namespace directly, you should start |
|
583 | # if you need to access the built-in namespace directly, you should start | |
578 | # with "import __builtin__" (note, no 's') which will definitely give you |
|
584 | # with "import __builtin__" (note, no 's') which will definitely give you | |
579 | # a module. Yeah, it's somewhat confusing:-(. |
|
585 | # a module. Yeah, it's somewhat confusing:-(. | |
580 |
|
586 | |||
581 | if user_ns is None: |
|
587 | if user_ns is None: | |
582 | # Set __name__ to __main__ to better match the behavior of the |
|
588 | # Set __name__ to __main__ to better match the behavior of the | |
583 | # normal interpreter. |
|
589 | # normal interpreter. | |
584 | user_ns = {'__name__' :'__main__', |
|
590 | user_ns = {'__name__' :'__main__', | |
585 | '__builtins__' : __builtin__, |
|
591 | '__builtins__' : __builtin__, | |
586 | } |
|
592 | } | |
587 |
|
593 | |||
588 | if user_global_ns is None: |
|
594 | if user_global_ns is None: | |
589 | user_global_ns = {} |
|
595 | user_global_ns = {} | |
590 |
|
596 | |||
591 | # Assign namespaces |
|
597 | # Assign namespaces | |
592 | # This is the namespace where all normal user variables live |
|
598 | # This is the namespace where all normal user variables live | |
593 | self.user_ns = user_ns |
|
599 | self.user_ns = user_ns | |
594 | # Embedded instances require a separate namespace for globals. |
|
600 | # Embedded instances require a separate namespace for globals. | |
595 | # Normally this one is unused by non-embedded instances. |
|
601 | # Normally this one is unused by non-embedded instances. | |
596 | self.user_global_ns = user_global_ns |
|
602 | self.user_global_ns = user_global_ns | |
597 | # A namespace to keep track of internal data structures to prevent |
|
603 | # A namespace to keep track of internal data structures to prevent | |
598 | # them from cluttering user-visible stuff. Will be updated later |
|
604 | # them from cluttering user-visible stuff. Will be updated later | |
599 | self.internal_ns = {} |
|
605 | self.internal_ns = {} | |
600 |
|
606 | |||
601 | # Namespace of system aliases. Each entry in the alias |
|
607 | # Namespace of system aliases. Each entry in the alias | |
602 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
608 | # table must be a 2-tuple of the form (N,name), where N is the number | |
603 | # of positional arguments of the alias. |
|
609 | # of positional arguments of the alias. | |
604 | self.alias_table = {} |
|
610 | self.alias_table = {} | |
605 |
|
611 | |||
606 | # A table holding all the namespaces IPython deals with, so that |
|
612 | # A table holding all the namespaces IPython deals with, so that | |
607 | # introspection facilities can search easily. |
|
613 | # introspection facilities can search easily. | |
608 | self.ns_table = {'user':user_ns, |
|
614 | self.ns_table = {'user':user_ns, | |
609 | 'user_global':user_global_ns, |
|
615 | 'user_global':user_global_ns, | |
610 | 'alias':self.alias_table, |
|
616 | 'alias':self.alias_table, | |
611 | 'internal':self.internal_ns, |
|
617 | 'internal':self.internal_ns, | |
612 | 'builtin':__builtin__.__dict__ |
|
618 | 'builtin':__builtin__.__dict__ | |
613 | } |
|
619 | } | |
614 |
|
620 | |||
615 | # The user namespace MUST have a pointer to the shell itself. |
|
621 | # The user namespace MUST have a pointer to the shell itself. | |
616 | self.user_ns[name] = self |
|
622 | self.user_ns[name] = self | |
617 |
|
623 | |||
618 | # We need to insert into sys.modules something that looks like a |
|
624 | # We need to insert into sys.modules something that looks like a | |
619 | # module but which accesses the IPython namespace, for shelve and |
|
625 | # module but which accesses the IPython namespace, for shelve and | |
620 | # pickle to work interactively. Normally they rely on getting |
|
626 | # pickle to work interactively. Normally they rely on getting | |
621 | # everything out of __main__, but for embedding purposes each IPython |
|
627 | # everything out of __main__, but for embedding purposes each IPython | |
622 | # instance has its own private namespace, so we can't go shoving |
|
628 | # instance has its own private namespace, so we can't go shoving | |
623 | # everything into __main__. |
|
629 | # everything into __main__. | |
624 |
|
630 | |||
625 | try: |
|
631 | # note, however, that we should only do this for non-embedded | |
626 | main_name = self.user_ns['__name__'] |
|
632 | # ipythons, which really mimic the __main__.__dict__ with their own | |
627 | except KeyError: |
|
633 | # namespace. Embedded instances, on the other hand, should not do | |
628 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
634 | # this because they need to manage the user local/global namespaces | |
629 | else: |
|
635 | # only, but they live within a 'normal' __main__ (meaning, they | |
630 | #print "pickle hack in place" # dbg |
|
636 | # shouldn't overtake the execution environment of the script they're | |
631 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
637 | # embedded in). | |
|
638 | ||||
|
639 | if not embedded: | |||
|
640 | try: | |||
|
641 | main_name = self.user_ns['__name__'] | |||
|
642 | except KeyError: | |||
|
643 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' | |||
|
644 | else: | |||
|
645 | #print "pickle hack in place" # dbg | |||
|
646 | sys.modules[main_name] = FakeModule(self.user_ns) | |||
632 |
|
647 | |||
633 | # List of input with multi-line handling. |
|
648 | # List of input with multi-line handling. | |
634 | # Fill its zero entry, user counter starts at 1 |
|
649 | # Fill its zero entry, user counter starts at 1 | |
635 | self.input_hist = InputList(['\n']) |
|
650 | self.input_hist = InputList(['\n']) | |
636 |
|
651 | |||
637 | # list of visited directories |
|
652 | # list of visited directories | |
638 | try: |
|
653 | try: | |
639 | self.dir_hist = [os.getcwd()] |
|
654 | self.dir_hist = [os.getcwd()] | |
640 | except IOError, e: |
|
655 | except IOError, e: | |
641 | self.dir_hist = [] |
|
656 | self.dir_hist = [] | |
642 |
|
657 | |||
643 | # dict of output history |
|
658 | # dict of output history | |
644 | self.output_hist = {} |
|
659 | self.output_hist = {} | |
645 |
|
660 | |||
646 | # dict of things NOT to alias (keywords, builtins and some special magics) |
|
661 | # dict of things NOT to alias (keywords, builtins and some special magics) | |
647 | no_alias = {} |
|
662 | no_alias = {} | |
648 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
663 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] | |
649 | for key in keyword.kwlist + no_alias_magics: |
|
664 | for key in keyword.kwlist + no_alias_magics: | |
650 | no_alias[key] = 1 |
|
665 | no_alias[key] = 1 | |
651 | no_alias.update(__builtin__.__dict__) |
|
666 | no_alias.update(__builtin__.__dict__) | |
652 | self.no_alias = no_alias |
|
667 | self.no_alias = no_alias | |
653 |
|
668 | |||
654 | # make global variables for user access to these |
|
669 | # make global variables for user access to these | |
655 | self.user_ns['_ih'] = self.input_hist |
|
670 | self.user_ns['_ih'] = self.input_hist | |
656 | self.user_ns['_oh'] = self.output_hist |
|
671 | self.user_ns['_oh'] = self.output_hist | |
657 | self.user_ns['_dh'] = self.dir_hist |
|
672 | self.user_ns['_dh'] = self.dir_hist | |
658 |
|
673 | |||
659 | # user aliases to input and output histories |
|
674 | # user aliases to input and output histories | |
660 | self.user_ns['In'] = self.input_hist |
|
675 | self.user_ns['In'] = self.input_hist | |
661 | self.user_ns['Out'] = self.output_hist |
|
676 | self.user_ns['Out'] = self.output_hist | |
662 |
|
677 | |||
663 | # Store the actual shell's name |
|
678 | # Store the actual shell's name | |
664 | self.name = name |
|
679 | self.name = name | |
665 |
|
680 | |||
666 | # Object variable to store code object waiting execution. This is |
|
681 | # Object variable to store code object waiting execution. This is | |
667 | # used mainly by the multithreaded shells, but it can come in handy in |
|
682 | # used mainly by the multithreaded shells, but it can come in handy in | |
668 | # other situations. No need to use a Queue here, since it's a single |
|
683 | # other situations. No need to use a Queue here, since it's a single | |
669 | # item which gets cleared once run. |
|
684 | # item which gets cleared once run. | |
670 | self.code_to_run = None |
|
685 | self.code_to_run = None | |
671 |
|
686 | |||
672 | # Job manager (for jobs run as background threads) |
|
687 | # Job manager (for jobs run as background threads) | |
673 | self.jobs = BackgroundJobManager() |
|
688 | self.jobs = BackgroundJobManager() | |
674 | # Put the job manager into builtins so it's always there. |
|
689 | # Put the job manager into builtins so it's always there. | |
675 | __builtin__.jobs = self.jobs |
|
690 | __builtin__.jobs = self.jobs | |
676 |
|
691 | |||
677 | # escapes for automatic behavior on the command line |
|
692 | # escapes for automatic behavior on the command line | |
678 | self.ESC_SHELL = '!' |
|
693 | self.ESC_SHELL = '!' | |
679 | self.ESC_HELP = '?' |
|
694 | self.ESC_HELP = '?' | |
680 | self.ESC_MAGIC = '%' |
|
695 | self.ESC_MAGIC = '%' | |
681 | self.ESC_QUOTE = ',' |
|
696 | self.ESC_QUOTE = ',' | |
682 | self.ESC_QUOTE2 = ';' |
|
697 | self.ESC_QUOTE2 = ';' | |
683 | self.ESC_PAREN = '/' |
|
698 | self.ESC_PAREN = '/' | |
684 |
|
699 | |||
685 | # And their associated handlers |
|
700 | # And their associated handlers | |
686 | self.esc_handlers = {self.ESC_PAREN:self.handle_auto, |
|
701 | self.esc_handlers = {self.ESC_PAREN:self.handle_auto, | |
687 | self.ESC_QUOTE:self.handle_auto, |
|
702 | self.ESC_QUOTE:self.handle_auto, | |
688 | self.ESC_QUOTE2:self.handle_auto, |
|
703 | self.ESC_QUOTE2:self.handle_auto, | |
689 | self.ESC_MAGIC:self.handle_magic, |
|
704 | self.ESC_MAGIC:self.handle_magic, | |
690 | self.ESC_HELP:self.handle_help, |
|
705 | self.ESC_HELP:self.handle_help, | |
691 | self.ESC_SHELL:self.handle_shell_escape, |
|
706 | self.ESC_SHELL:self.handle_shell_escape, | |
692 | } |
|
707 | } | |
693 |
|
708 | |||
694 | # class initializations |
|
709 | # class initializations | |
695 | Logger.__init__(self,log_ns = self.user_ns) |
|
710 | Logger.__init__(self,log_ns = self.user_ns) | |
696 | Magic.__init__(self,self) |
|
711 | Magic.__init__(self,self) | |
697 |
|
712 | |||
698 | # an ugly hack to get a pointer to the shell, so I can start writing |
|
713 | # an ugly hack to get a pointer to the shell, so I can start writing | |
699 | # magic code via this pointer instead of the current mixin salad. |
|
714 | # magic code via this pointer instead of the current mixin salad. | |
700 | Magic.set_shell(self,self) |
|
715 | Magic.set_shell(self,self) | |
701 |
|
716 | |||
702 | # Python source parser/formatter for syntax highlighting |
|
717 | # Python source parser/formatter for syntax highlighting | |
703 | pyformat = Parser().format |
|
718 | pyformat = Parser().format | |
704 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
719 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) | |
705 |
|
720 | |||
706 | # hooks holds pointers used for user-side customizations |
|
721 | # hooks holds pointers used for user-side customizations | |
707 | self.hooks = Struct() |
|
722 | self.hooks = Struct() | |
708 |
|
723 | |||
709 | # Set all default hooks, defined in the IPython.hooks module. |
|
724 | # Set all default hooks, defined in the IPython.hooks module. | |
710 | hooks = IPython.hooks |
|
725 | hooks = IPython.hooks | |
711 | for hook_name in hooks.__all__: |
|
726 | for hook_name in hooks.__all__: | |
712 | self.set_hook(hook_name,getattr(hooks,hook_name)) |
|
727 | self.set_hook(hook_name,getattr(hooks,hook_name)) | |
713 |
|
728 | |||
714 | # Flag to mark unconditional exit |
|
729 | # Flag to mark unconditional exit | |
715 | self.exit_now = False |
|
730 | self.exit_now = False | |
716 |
|
731 | |||
717 | self.usage_min = """\ |
|
732 | self.usage_min = """\ | |
718 | An enhanced console for Python. |
|
733 | An enhanced console for Python. | |
719 | Some of its features are: |
|
734 | Some of its features are: | |
720 | - Readline support if the readline library is present. |
|
735 | - Readline support if the readline library is present. | |
721 | - Tab completion in the local namespace. |
|
736 | - Tab completion in the local namespace. | |
722 | - Logging of input, see command-line options. |
|
737 | - Logging of input, see command-line options. | |
723 | - System shell escape via ! , eg !ls. |
|
738 | - System shell escape via ! , eg !ls. | |
724 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
739 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) | |
725 | - Keeps track of locally defined variables via %who, %whos. |
|
740 | - Keeps track of locally defined variables via %who, %whos. | |
726 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
741 | - Show object information with a ? eg ?x or x? (use ?? for more info). | |
727 | """ |
|
742 | """ | |
728 | if usage: self.usage = usage |
|
743 | if usage: self.usage = usage | |
729 | else: self.usage = self.usage_min |
|
744 | else: self.usage = self.usage_min | |
730 |
|
745 | |||
731 | # Storage |
|
746 | # Storage | |
732 | self.rc = rc # This will hold all configuration information |
|
747 | self.rc = rc # This will hold all configuration information | |
733 | self.inputcache = [] |
|
748 | self.inputcache = [] | |
734 | self._boundcache = [] |
|
749 | self._boundcache = [] | |
735 | self.pager = 'less' |
|
750 | self.pager = 'less' | |
736 | # temporary files used for various purposes. Deleted at exit. |
|
751 | # temporary files used for various purposes. Deleted at exit. | |
737 | self.tempfiles = [] |
|
752 | self.tempfiles = [] | |
738 |
|
753 | |||
739 | # Keep track of readline usage (later set by init_readline) |
|
754 | # Keep track of readline usage (later set by init_readline) | |
740 | self.has_readline = 0 |
|
755 | self.has_readline = 0 | |
741 |
|
756 | |||
742 | # for pushd/popd management |
|
757 | # for pushd/popd management | |
743 | try: |
|
758 | try: | |
744 | self.home_dir = get_home_dir() |
|
759 | self.home_dir = get_home_dir() | |
745 | except HomeDirError,msg: |
|
760 | except HomeDirError,msg: | |
746 | fatal(msg) |
|
761 | fatal(msg) | |
747 |
|
762 | |||
748 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
763 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] | |
749 |
|
764 | |||
750 | # Functions to call the underlying shell. |
|
765 | # Functions to call the underlying shell. | |
751 |
|
766 | |||
752 | # utility to expand user variables via Itpl |
|
767 | # utility to expand user variables via Itpl | |
753 | self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'), |
|
768 | self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'), | |
754 | self.user_ns)) |
|
769 | self.user_ns)) | |
755 | # The first is similar to os.system, but it doesn't return a value, |
|
770 | # The first is similar to os.system, but it doesn't return a value, | |
756 | # and it allows interpolation of variables in the user's namespace. |
|
771 | # and it allows interpolation of variables in the user's namespace. | |
757 | self.system = lambda cmd: shell(self.var_expand(cmd), |
|
772 | self.system = lambda cmd: shell(self.var_expand(cmd), | |
758 | header='IPython system call: ', |
|
773 | header='IPython system call: ', | |
759 | verbose=self.rc.system_verbose) |
|
774 | verbose=self.rc.system_verbose) | |
760 | # These are for getoutput and getoutputerror: |
|
775 | # These are for getoutput and getoutputerror: | |
761 | self.getoutput = lambda cmd: \ |
|
776 | self.getoutput = lambda cmd: \ | |
762 | getoutput(self.var_expand(cmd), |
|
777 | getoutput(self.var_expand(cmd), | |
763 | header='IPython system call: ', |
|
778 | header='IPython system call: ', | |
764 | verbose=self.rc.system_verbose) |
|
779 | verbose=self.rc.system_verbose) | |
765 | self.getoutputerror = lambda cmd: \ |
|
780 | self.getoutputerror = lambda cmd: \ | |
766 | getoutputerror(str(ItplNS(cmd.replace('#','\#'), |
|
781 | getoutputerror(str(ItplNS(cmd.replace('#','\#'), | |
767 | self.user_ns)), |
|
782 | self.user_ns)), | |
768 | header='IPython system call: ', |
|
783 | header='IPython system call: ', | |
769 | verbose=self.rc.system_verbose) |
|
784 | verbose=self.rc.system_verbose) | |
770 |
|
785 | |||
771 | # RegExp for splitting line contents into pre-char//first |
|
786 | # RegExp for splitting line contents into pre-char//first | |
772 | # word-method//rest. For clarity, each group in on one line. |
|
787 | # word-method//rest. For clarity, each group in on one line. | |
773 |
|
788 | |||
774 | # WARNING: update the regexp if the above escapes are changed, as they |
|
789 | # WARNING: update the regexp if the above escapes are changed, as they | |
775 | # are hardwired in. |
|
790 | # are hardwired in. | |
776 |
|
791 | |||
777 | # Don't get carried away with trying to make the autocalling catch too |
|
792 | # Don't get carried away with trying to make the autocalling catch too | |
778 | # much: it's better to be conservative rather than to trigger hidden |
|
793 | # much: it's better to be conservative rather than to trigger hidden | |
779 | # evals() somewhere and end up causing side effects. |
|
794 | # evals() somewhere and end up causing side effects. | |
780 |
|
795 | |||
781 | self.line_split = re.compile(r'^([\s*,;/])' |
|
796 | self.line_split = re.compile(r'^([\s*,;/])' | |
782 | r'([\?\w\.]+\w*\s*)' |
|
797 | r'([\?\w\.]+\w*\s*)' | |
783 | r'(\(?.*$)') |
|
798 | r'(\(?.*$)') | |
784 |
|
799 | |||
785 | # Original re, keep around for a while in case changes break something |
|
800 | # Original re, keep around for a while in case changes break something | |
786 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' |
|
801 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' | |
787 | # r'(\s*[\?\w\.]+\w*\s*)' |
|
802 | # r'(\s*[\?\w\.]+\w*\s*)' | |
788 | # r'(\(?.*$)') |
|
803 | # r'(\(?.*$)') | |
789 |
|
804 | |||
790 | # RegExp to identify potential function names |
|
805 | # RegExp to identify potential function names | |
791 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
806 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') | |
792 | # RegExp to exclude strings with this start from autocalling |
|
807 | # RegExp to exclude strings with this start from autocalling | |
793 | self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ') |
|
808 | self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ') | |
794 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
809 | # try to catch also methods for stuff in lists/tuples/dicts: off | |
795 | # (experimental). For this to work, the line_split regexp would need |
|
810 | # (experimental). For this to work, the line_split regexp would need | |
796 | # to be modified so it wouldn't break things at '['. That line is |
|
811 | # to be modified so it wouldn't break things at '['. That line is | |
797 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
812 | # nasty enough that I shouldn't change it until I can test it _well_. | |
798 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
813 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') | |
799 |
|
814 | |||
800 | # keep track of where we started running (mainly for crash post-mortem) |
|
815 | # keep track of where we started running (mainly for crash post-mortem) | |
801 | self.starting_dir = os.getcwd() |
|
816 | self.starting_dir = os.getcwd() | |
802 |
|
817 | |||
803 | # Attributes for Logger mixin class, make defaults here |
|
818 | # Attributes for Logger mixin class, make defaults here | |
804 | self._dolog = 0 |
|
819 | self._dolog = 0 | |
805 | self.LOG = '' |
|
820 | self.LOG = '' | |
806 | self.LOGDEF = '.InteractiveShell.log' |
|
821 | self.LOGDEF = '.InteractiveShell.log' | |
807 | self.LOGMODE = 'over' |
|
822 | self.LOGMODE = 'over' | |
808 | self.LOGHEAD = Itpl( |
|
823 | self.LOGHEAD = Itpl( | |
809 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
824 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** | |
810 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
825 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW | |
811 | #log# opts = $self.rc.opts |
|
826 | #log# opts = $self.rc.opts | |
812 | #log# args = $self.rc.args |
|
827 | #log# args = $self.rc.args | |
813 | #log# It is safe to make manual edits below here. |
|
828 | #log# It is safe to make manual edits below here. | |
814 | #log#----------------------------------------------------------------------- |
|
829 | #log#----------------------------------------------------------------------- | |
815 | """) |
|
830 | """) | |
816 | # Various switches which can be set |
|
831 | # Various switches which can be set | |
817 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
832 | self.CACHELENGTH = 5000 # this is cheap, it's just text | |
818 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
833 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ | |
819 | self.banner2 = banner2 |
|
834 | self.banner2 = banner2 | |
820 |
|
835 | |||
821 | # TraceBack handlers: |
|
836 | # TraceBack handlers: | |
822 | # Need two, one for syntax errors and one for other exceptions. |
|
837 | # Need two, one for syntax errors and one for other exceptions. | |
823 | self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor') |
|
838 | self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor') | |
824 | # This one is initialized with an offset, meaning we always want to |
|
839 | # This one is initialized with an offset, meaning we always want to | |
825 | # remove the topmost item in the traceback, which is our own internal |
|
840 | # remove the topmost item in the traceback, which is our own internal | |
826 | # code. Valid modes: ['Plain','Context','Verbose'] |
|
841 | # code. Valid modes: ['Plain','Context','Verbose'] | |
827 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
842 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', | |
828 | color_scheme='NoColor', |
|
843 | color_scheme='NoColor', | |
829 | tb_offset = 1) |
|
844 | tb_offset = 1) | |
830 | # and add any custom exception handlers the user may have specified |
|
845 | # and add any custom exception handlers the user may have specified | |
831 | self.set_custom_exc(*custom_exceptions) |
|
846 | self.set_custom_exc(*custom_exceptions) | |
832 |
|
847 | |||
833 | # Object inspector |
|
848 | # Object inspector | |
834 | ins_colors = OInspect.InspectColors |
|
849 | ins_colors = OInspect.InspectColors | |
835 | code_colors = PyColorize.ANSICodeColors |
|
850 | code_colors = PyColorize.ANSICodeColors | |
836 | self.inspector = OInspect.Inspector(ins_colors,code_colors,'NoColor') |
|
851 | self.inspector = OInspect.Inspector(ins_colors,code_colors,'NoColor') | |
837 | self.autoindent = 0 |
|
852 | self.autoindent = 0 | |
838 |
|
853 | |||
839 | # Make some aliases automatically |
|
854 | # Make some aliases automatically | |
840 | # Prepare list of shell aliases to auto-define |
|
855 | # Prepare list of shell aliases to auto-define | |
841 | if os.name == 'posix': |
|
856 | if os.name == 'posix': | |
842 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
857 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', | |
843 | 'mv mv -i','rm rm -i','cp cp -i', |
|
858 | 'mv mv -i','rm rm -i','cp cp -i', | |
844 | 'cat cat','less less','clear clear', |
|
859 | 'cat cat','less less','clear clear', | |
845 | # a better ls |
|
860 | # a better ls | |
846 | 'ls ls -F', |
|
861 | 'ls ls -F', | |
847 | # long ls |
|
862 | # long ls | |
848 | 'll ls -lF', |
|
863 | 'll ls -lF', | |
849 | # color ls |
|
864 | # color ls | |
850 | 'lc ls -F -o --color', |
|
865 | 'lc ls -F -o --color', | |
851 | # ls normal files only |
|
866 | # ls normal files only | |
852 | 'lf ls -F -o --color %l | grep ^-', |
|
867 | 'lf ls -F -o --color %l | grep ^-', | |
853 | # ls symbolic links |
|
868 | # ls symbolic links | |
854 | 'lk ls -F -o --color %l | grep ^l', |
|
869 | 'lk ls -F -o --color %l | grep ^l', | |
855 | # directories or links to directories, |
|
870 | # directories or links to directories, | |
856 | 'ldir ls -F -o --color %l | grep /$', |
|
871 | 'ldir ls -F -o --color %l | grep /$', | |
857 | # things which are executable |
|
872 | # things which are executable | |
858 | 'lx ls -F -o --color %l | grep ^-..x', |
|
873 | 'lx ls -F -o --color %l | grep ^-..x', | |
859 | ) |
|
874 | ) | |
860 | elif os.name in ['nt','dos']: |
|
875 | elif os.name in ['nt','dos']: | |
861 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
876 | auto_alias = ('dir dir /on', 'ls dir /on', | |
862 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
877 | 'ddir dir /ad /on', 'ldir dir /ad /on', | |
863 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
878 | 'mkdir mkdir','rmdir rmdir','echo echo', | |
864 | 'ren ren','cls cls','copy copy') |
|
879 | 'ren ren','cls cls','copy copy') | |
865 | else: |
|
880 | else: | |
866 | auto_alias = () |
|
881 | auto_alias = () | |
867 | self.auto_alias = map(lambda s:s.split(None,1),auto_alias) |
|
882 | self.auto_alias = map(lambda s:s.split(None,1),auto_alias) | |
868 | # Call the actual (public) initializer |
|
883 | # Call the actual (public) initializer | |
869 | self.init_auto_alias() |
|
884 | self.init_auto_alias() | |
870 | # end __init__ |
|
885 | # end __init__ | |
871 |
|
886 | |||
872 | def set_hook(self,name,hook): |
|
887 | def set_hook(self,name,hook): | |
873 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
888 | """set_hook(name,hook) -> sets an internal IPython hook. | |
874 |
|
889 | |||
875 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
890 | IPython exposes some of its internal API as user-modifiable hooks. By | |
876 | resetting one of these hooks, you can modify IPython's behavior to |
|
891 | resetting one of these hooks, you can modify IPython's behavior to | |
877 | call at runtime your own routines.""" |
|
892 | call at runtime your own routines.""" | |
878 |
|
893 | |||
879 | # At some point in the future, this should validate the hook before it |
|
894 | # At some point in the future, this should validate the hook before it | |
880 | # accepts it. Probably at least check that the hook takes the number |
|
895 | # accepts it. Probably at least check that the hook takes the number | |
881 | # of args it's supposed to. |
|
896 | # of args it's supposed to. | |
882 | setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
897 | setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) | |
883 |
|
898 | |||
884 | def set_custom_exc(self,exc_tuple,handler): |
|
899 | def set_custom_exc(self,exc_tuple,handler): | |
885 | """set_custom_exc(exc_tuple,handler) |
|
900 | """set_custom_exc(exc_tuple,handler) | |
886 |
|
901 | |||
887 | Set a custom exception handler, which will be called if any of the |
|
902 | Set a custom exception handler, which will be called if any of the | |
888 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
903 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
889 | runcode() method. |
|
904 | runcode() method. | |
890 |
|
905 | |||
891 | Inputs: |
|
906 | Inputs: | |
892 |
|
907 | |||
893 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
908 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
894 | handler for. It is very important that you use a tuple, and NOT A |
|
909 | handler for. It is very important that you use a tuple, and NOT A | |
895 | LIST here, because of the way Python's except statement works. If |
|
910 | LIST here, because of the way Python's except statement works. If | |
896 | you only want to trap a single exception, use a singleton tuple: |
|
911 | you only want to trap a single exception, use a singleton tuple: | |
897 |
|
912 | |||
898 | exc_tuple == (MyCustomException,) |
|
913 | exc_tuple == (MyCustomException,) | |
899 |
|
914 | |||
900 | - handler: this must be defined as a function with the following |
|
915 | - handler: this must be defined as a function with the following | |
901 | basic interface: def my_handler(self,etype,value,tb). |
|
916 | basic interface: def my_handler(self,etype,value,tb). | |
902 |
|
917 | |||
903 | This will be made into an instance method (via new.instancemethod) |
|
918 | This will be made into an instance method (via new.instancemethod) | |
904 | of IPython itself, and it will be called if any of the exceptions |
|
919 | of IPython itself, and it will be called if any of the exceptions | |
905 | listed in the exc_tuple are caught. If the handler is None, an |
|
920 | listed in the exc_tuple are caught. If the handler is None, an | |
906 | internal basic one is used, which just prints basic info. |
|
921 | internal basic one is used, which just prints basic info. | |
907 |
|
922 | |||
908 | WARNING: by putting in your own exception handler into IPython's main |
|
923 | WARNING: by putting in your own exception handler into IPython's main | |
909 | execution loop, you run a very good chance of nasty crashes. This |
|
924 | execution loop, you run a very good chance of nasty crashes. This | |
910 | facility should only be used if you really know what you are doing.""" |
|
925 | facility should only be used if you really know what you are doing.""" | |
911 |
|
926 | |||
912 | assert type(exc_tuple)==type(()) , \ |
|
927 | assert type(exc_tuple)==type(()) , \ | |
913 | "The custom exceptions must be given AS A TUPLE." |
|
928 | "The custom exceptions must be given AS A TUPLE." | |
914 |
|
929 | |||
915 | def dummy_handler(self,etype,value,tb): |
|
930 | def dummy_handler(self,etype,value,tb): | |
916 | print '*** Simple custom exception handler ***' |
|
931 | print '*** Simple custom exception handler ***' | |
917 | print 'Exception type :',etype |
|
932 | print 'Exception type :',etype | |
918 | print 'Exception value:',value |
|
933 | print 'Exception value:',value | |
919 | print 'Traceback :',tb |
|
934 | print 'Traceback :',tb | |
920 | print 'Source code :','\n'.join(self.buffer) |
|
935 | print 'Source code :','\n'.join(self.buffer) | |
921 |
|
936 | |||
922 | if handler is None: handler = dummy_handler |
|
937 | if handler is None: handler = dummy_handler | |
923 |
|
938 | |||
924 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
939 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
925 | self.custom_exceptions = exc_tuple |
|
940 | self.custom_exceptions = exc_tuple | |
926 |
|
941 | |||
927 | def set_custom_completer(self,completer,pos=0): |
|
942 | def set_custom_completer(self,completer,pos=0): | |
928 | """set_custom_completer(completer,pos=0) |
|
943 | """set_custom_completer(completer,pos=0) | |
929 |
|
944 | |||
930 | Adds a new custom completer function. |
|
945 | Adds a new custom completer function. | |
931 |
|
946 | |||
932 | The position argument (defaults to 0) is the index in the completers |
|
947 | The position argument (defaults to 0) is the index in the completers | |
933 | list where you want the completer to be inserted.""" |
|
948 | list where you want the completer to be inserted.""" | |
934 |
|
949 | |||
935 | newcomp = new.instancemethod(completer,self.Completer, |
|
950 | newcomp = new.instancemethod(completer,self.Completer, | |
936 | self.Completer.__class__) |
|
951 | self.Completer.__class__) | |
937 | self.Completer.matchers.insert(pos,newcomp) |
|
952 | self.Completer.matchers.insert(pos,newcomp) | |
938 |
|
953 | |||
939 | def complete(self,text): |
|
954 | def complete(self,text): | |
940 | """Return a sorted list of all possible completions on text. |
|
955 | """Return a sorted list of all possible completions on text. | |
941 |
|
956 | |||
942 | Inputs: |
|
957 | Inputs: | |
943 |
|
958 | |||
944 | - text: a string of text to be completed on. |
|
959 | - text: a string of text to be completed on. | |
945 |
|
960 | |||
946 | This is a wrapper around the completion mechanism, similar to what |
|
961 | This is a wrapper around the completion mechanism, similar to what | |
947 | readline does at the command line when the TAB key is hit. By |
|
962 | readline does at the command line when the TAB key is hit. By | |
948 | exposing it as a method, it can be used by other non-readline |
|
963 | exposing it as a method, it can be used by other non-readline | |
949 | environments (such as GUIs) for text completion. |
|
964 | environments (such as GUIs) for text completion. | |
950 |
|
965 | |||
951 | Simple usage example: |
|
966 | Simple usage example: | |
952 |
|
967 | |||
953 | In [1]: x = 'hello' |
|
968 | In [1]: x = 'hello' | |
954 |
|
969 | |||
955 | In [2]: __IP.complete('x.l') |
|
970 | In [2]: __IP.complete('x.l') | |
956 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
971 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" | |
957 |
|
972 | |||
958 | complete = self.Completer.complete |
|
973 | complete = self.Completer.complete | |
959 | state = 0 |
|
974 | state = 0 | |
960 | # use a dict so we get unique keys, since ipyhton's multiple |
|
975 | # use a dict so we get unique keys, since ipyhton's multiple | |
961 | # completers can return duplicates. |
|
976 | # completers can return duplicates. | |
962 | comps = {} |
|
977 | comps = {} | |
963 | while True: |
|
978 | while True: | |
964 | newcomp = complete(text,state) |
|
979 | newcomp = complete(text,state) | |
965 | if newcomp is None: |
|
980 | if newcomp is None: | |
966 | break |
|
981 | break | |
967 | comps[newcomp] = 1 |
|
982 | comps[newcomp] = 1 | |
968 | state += 1 |
|
983 | state += 1 | |
969 | outcomps = comps.keys() |
|
984 | outcomps = comps.keys() | |
970 | outcomps.sort() |
|
985 | outcomps.sort() | |
971 | return outcomps |
|
986 | return outcomps | |
972 |
|
987 | |||
973 | def set_completer_frame(self, frame): |
|
988 | def set_completer_frame(self, frame): | |
974 | if frame: |
|
989 | if frame: | |
975 |
ns = frame.f_ |
|
990 | self.Completer.namespace = frame.f_locals | |
976 |
|
|
991 | self.Completer.global_namespace = frame.f_globals | |
977 | self.Completer.namespace = ns |
|
|||
978 | else: |
|
992 | else: | |
979 | self.Completer.namespace = self.user_ns |
|
993 | self.Completer.namespace = self.user_ns | |
|
994 | self.Completer.global_namespace = self.user_global_ns | |||
980 |
|
995 | |||
981 | def post_config_initialization(self): |
|
996 | def post_config_initialization(self): | |
982 | """Post configuration init method |
|
997 | """Post configuration init method | |
983 |
|
998 | |||
984 | This is called after the configuration files have been processed to |
|
999 | This is called after the configuration files have been processed to | |
985 | 'finalize' the initialization.""" |
|
1000 | 'finalize' the initialization.""" | |
986 |
|
1001 | |||
987 | rc = self.rc |
|
1002 | rc = self.rc | |
988 |
|
1003 | |||
989 | # Load readline proper |
|
1004 | # Load readline proper | |
990 | if rc.readline: |
|
1005 | if rc.readline: | |
991 | self.init_readline() |
|
1006 | self.init_readline() | |
992 |
|
1007 | |||
993 | # Set user colors (don't do it in the constructor above so that it doesn't |
|
1008 | # Set user colors (don't do it in the constructor above so that it doesn't | |
994 | # crash if colors option is invalid) |
|
1009 | # crash if colors option is invalid) | |
995 | self.magic_colors(rc.colors) |
|
1010 | self.magic_colors(rc.colors) | |
996 |
|
1011 | |||
997 | # Load user aliases |
|
1012 | # Load user aliases | |
998 | for alias in rc.alias: |
|
1013 | for alias in rc.alias: | |
999 | self.magic_alias(alias) |
|
1014 | self.magic_alias(alias) | |
1000 |
|
1015 | |||
1001 | # dynamic data that survives through sessions |
|
1016 | # dynamic data that survives through sessions | |
1002 | # XXX make the filename a config option? |
|
1017 | # XXX make the filename a config option? | |
1003 | persist_base = 'persist' |
|
1018 | persist_base = 'persist' | |
1004 | if rc.profile: |
|
1019 | if rc.profile: | |
1005 | persist_base += '_%s' % rc.profile |
|
1020 | persist_base += '_%s' % rc.profile | |
1006 | self.persist_fname = os.path.join(rc.ipythondir,persist_base) |
|
1021 | self.persist_fname = os.path.join(rc.ipythondir,persist_base) | |
1007 |
|
1022 | |||
1008 | try: |
|
1023 | try: | |
1009 | self.persist = pickle.load(file(self.persist_fname)) |
|
1024 | self.persist = pickle.load(file(self.persist_fname)) | |
1010 | except: |
|
1025 | except: | |
1011 | self.persist = {} |
|
1026 | self.persist = {} | |
1012 |
|
1027 | |||
1013 | def init_auto_alias(self): |
|
1028 | def init_auto_alias(self): | |
1014 | """Define some aliases automatically. |
|
1029 | """Define some aliases automatically. | |
1015 |
|
1030 | |||
1016 | These are ALL parameter-less aliases""" |
|
1031 | These are ALL parameter-less aliases""" | |
1017 | for alias,cmd in self.auto_alias: |
|
1032 | for alias,cmd in self.auto_alias: | |
1018 | self.alias_table[alias] = (0,cmd) |
|
1033 | self.alias_table[alias] = (0,cmd) | |
1019 |
|
1034 | |||
1020 | def alias_table_validate(self,verbose=0): |
|
1035 | def alias_table_validate(self,verbose=0): | |
1021 | """Update information about the alias table. |
|
1036 | """Update information about the alias table. | |
1022 |
|
1037 | |||
1023 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1038 | In particular, make sure no Python keywords/builtins are in it.""" | |
1024 |
|
1039 | |||
1025 | no_alias = self.no_alias |
|
1040 | no_alias = self.no_alias | |
1026 | for k in self.alias_table.keys(): |
|
1041 | for k in self.alias_table.keys(): | |
1027 | if k in no_alias: |
|
1042 | if k in no_alias: | |
1028 | del self.alias_table[k] |
|
1043 | del self.alias_table[k] | |
1029 | if verbose: |
|
1044 | if verbose: | |
1030 | print ("Deleting alias <%s>, it's a Python " |
|
1045 | print ("Deleting alias <%s>, it's a Python " | |
1031 | "keyword or builtin." % k) |
|
1046 | "keyword or builtin." % k) | |
1032 |
|
1047 | |||
1033 | def set_autoindent(self,value=None): |
|
1048 | def set_autoindent(self,value=None): | |
1034 | """Set the autoindent flag, checking for readline support. |
|
1049 | """Set the autoindent flag, checking for readline support. | |
1035 |
|
1050 | |||
1036 | If called with no arguments, it acts as a toggle.""" |
|
1051 | If called with no arguments, it acts as a toggle.""" | |
1037 |
|
1052 | |||
1038 | if not self.has_readline: |
|
1053 | if not self.has_readline: | |
1039 | if os.name == 'posix': |
|
1054 | if os.name == 'posix': | |
1040 | warn("The auto-indent feature requires the readline library") |
|
1055 | warn("The auto-indent feature requires the readline library") | |
1041 | self.autoindent = 0 |
|
1056 | self.autoindent = 0 | |
1042 | return |
|
1057 | return | |
1043 | if value is None: |
|
1058 | if value is None: | |
1044 | self.autoindent = not self.autoindent |
|
1059 | self.autoindent = not self.autoindent | |
1045 | else: |
|
1060 | else: | |
1046 | self.autoindent = value |
|
1061 | self.autoindent = value | |
1047 |
|
1062 | |||
1048 | def rc_set_toggle(self,rc_field,value=None): |
|
1063 | def rc_set_toggle(self,rc_field,value=None): | |
1049 | """Set or toggle a field in IPython's rc config. structure. |
|
1064 | """Set or toggle a field in IPython's rc config. structure. | |
1050 |
|
1065 | |||
1051 | If called with no arguments, it acts as a toggle. |
|
1066 | If called with no arguments, it acts as a toggle. | |
1052 |
|
1067 | |||
1053 | If called with a non-existent field, the resulting AttributeError |
|
1068 | If called with a non-existent field, the resulting AttributeError | |
1054 | exception will propagate out.""" |
|
1069 | exception will propagate out.""" | |
1055 |
|
1070 | |||
1056 | rc_val = getattr(self.rc,rc_field) |
|
1071 | rc_val = getattr(self.rc,rc_field) | |
1057 | if value is None: |
|
1072 | if value is None: | |
1058 | value = not rc_val |
|
1073 | value = not rc_val | |
1059 | setattr(self.rc,rc_field,value) |
|
1074 | setattr(self.rc,rc_field,value) | |
1060 |
|
1075 | |||
1061 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1076 | def user_setup(self,ipythondir,rc_suffix,mode='install'): | |
1062 | """Install the user configuration directory. |
|
1077 | """Install the user configuration directory. | |
1063 |
|
1078 | |||
1064 | Can be called when running for the first time or to upgrade the user's |
|
1079 | Can be called when running for the first time or to upgrade the user's | |
1065 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1080 | .ipython/ directory with the mode parameter. Valid modes are 'install' | |
1066 | and 'upgrade'.""" |
|
1081 | and 'upgrade'.""" | |
1067 |
|
1082 | |||
1068 | def wait(): |
|
1083 | def wait(): | |
1069 | try: |
|
1084 | try: | |
1070 | raw_input("Please press <RETURN> to start IPython.") |
|
1085 | raw_input("Please press <RETURN> to start IPython.") | |
1071 | except EOFError: |
|
1086 | except EOFError: | |
1072 | print >> Term.cout |
|
1087 | print >> Term.cout | |
1073 | print '*'*70 |
|
1088 | print '*'*70 | |
1074 |
|
1089 | |||
1075 | cwd = os.getcwd() # remember where we started |
|
1090 | cwd = os.getcwd() # remember where we started | |
1076 | glb = glob.glob |
|
1091 | glb = glob.glob | |
1077 | print '*'*70 |
|
1092 | print '*'*70 | |
1078 | if mode == 'install': |
|
1093 | if mode == 'install': | |
1079 | print \ |
|
1094 | print \ | |
1080 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1095 | """Welcome to IPython. I will try to create a personal configuration directory | |
1081 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1096 | where you can customize many aspects of IPython's functionality in:\n""" | |
1082 | else: |
|
1097 | else: | |
1083 | print 'I am going to upgrade your configuration in:' |
|
1098 | print 'I am going to upgrade your configuration in:' | |
1084 |
|
1099 | |||
1085 | print ipythondir |
|
1100 | print ipythondir | |
1086 |
|
1101 | |||
1087 | rcdirend = os.path.join('IPython','UserConfig') |
|
1102 | rcdirend = os.path.join('IPython','UserConfig') | |
1088 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1103 | cfg = lambda d: os.path.join(d,rcdirend) | |
1089 | try: |
|
1104 | try: | |
1090 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1105 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] | |
1091 | except IOError: |
|
1106 | except IOError: | |
1092 | warning = """ |
|
1107 | warning = """ | |
1093 | Installation error. IPython's directory was not found. |
|
1108 | Installation error. IPython's directory was not found. | |
1094 |
|
1109 | |||
1095 | Check the following: |
|
1110 | Check the following: | |
1096 |
|
1111 | |||
1097 | The ipython/IPython directory should be in a directory belonging to your |
|
1112 | The ipython/IPython directory should be in a directory belonging to your | |
1098 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1113 | PYTHONPATH environment variable (that is, it should be in a directory | |
1099 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1114 | belonging to sys.path). You can copy it explicitly there or just link to it. | |
1100 |
|
1115 | |||
1101 | IPython will proceed with builtin defaults. |
|
1116 | IPython will proceed with builtin defaults. | |
1102 | """ |
|
1117 | """ | |
1103 | warn(warning) |
|
1118 | warn(warning) | |
1104 | wait() |
|
1119 | wait() | |
1105 | return |
|
1120 | return | |
1106 |
|
1121 | |||
1107 | if mode == 'install': |
|
1122 | if mode == 'install': | |
1108 | try: |
|
1123 | try: | |
1109 | shutil.copytree(rcdir,ipythondir) |
|
1124 | shutil.copytree(rcdir,ipythondir) | |
1110 | os.chdir(ipythondir) |
|
1125 | os.chdir(ipythondir) | |
1111 | rc_files = glb("ipythonrc*") |
|
1126 | rc_files = glb("ipythonrc*") | |
1112 | for rc_file in rc_files: |
|
1127 | for rc_file in rc_files: | |
1113 | os.rename(rc_file,rc_file+rc_suffix) |
|
1128 | os.rename(rc_file,rc_file+rc_suffix) | |
1114 | except: |
|
1129 | except: | |
1115 | warning = """ |
|
1130 | warning = """ | |
1116 |
|
1131 | |||
1117 | There was a problem with the installation: |
|
1132 | There was a problem with the installation: | |
1118 | %s |
|
1133 | %s | |
1119 | Try to correct it or contact the developers if you think it's a bug. |
|
1134 | Try to correct it or contact the developers if you think it's a bug. | |
1120 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1135 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] | |
1121 | warn(warning) |
|
1136 | warn(warning) | |
1122 | wait() |
|
1137 | wait() | |
1123 | return |
|
1138 | return | |
1124 |
|
1139 | |||
1125 | elif mode == 'upgrade': |
|
1140 | elif mode == 'upgrade': | |
1126 | try: |
|
1141 | try: | |
1127 | os.chdir(ipythondir) |
|
1142 | os.chdir(ipythondir) | |
1128 | except: |
|
1143 | except: | |
1129 | print """ |
|
1144 | print """ | |
1130 | Can not upgrade: changing to directory %s failed. Details: |
|
1145 | Can not upgrade: changing to directory %s failed. Details: | |
1131 | %s |
|
1146 | %s | |
1132 | """ % (ipythondir,sys.exc_info()[1]) |
|
1147 | """ % (ipythondir,sys.exc_info()[1]) | |
1133 | wait() |
|
1148 | wait() | |
1134 | return |
|
1149 | return | |
1135 | else: |
|
1150 | else: | |
1136 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1151 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) | |
1137 | for new_full_path in sources: |
|
1152 | for new_full_path in sources: | |
1138 | new_filename = os.path.basename(new_full_path) |
|
1153 | new_filename = os.path.basename(new_full_path) | |
1139 | if new_filename.startswith('ipythonrc'): |
|
1154 | if new_filename.startswith('ipythonrc'): | |
1140 | new_filename = new_filename + rc_suffix |
|
1155 | new_filename = new_filename + rc_suffix | |
1141 | # The config directory should only contain files, skip any |
|
1156 | # The config directory should only contain files, skip any | |
1142 | # directories which may be there (like CVS) |
|
1157 | # directories which may be there (like CVS) | |
1143 | if os.path.isdir(new_full_path): |
|
1158 | if os.path.isdir(new_full_path): | |
1144 | continue |
|
1159 | continue | |
1145 | if os.path.exists(new_filename): |
|
1160 | if os.path.exists(new_filename): | |
1146 | old_file = new_filename+'.old' |
|
1161 | old_file = new_filename+'.old' | |
1147 | if os.path.exists(old_file): |
|
1162 | if os.path.exists(old_file): | |
1148 | os.remove(old_file) |
|
1163 | os.remove(old_file) | |
1149 | os.rename(new_filename,old_file) |
|
1164 | os.rename(new_filename,old_file) | |
1150 | shutil.copy(new_full_path,new_filename) |
|
1165 | shutil.copy(new_full_path,new_filename) | |
1151 | else: |
|
1166 | else: | |
1152 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1167 | raise ValueError,'unrecognized mode for install:',`mode` | |
1153 |
|
1168 | |||
1154 | # Fix line-endings to those native to each platform in the config |
|
1169 | # Fix line-endings to those native to each platform in the config | |
1155 | # directory. |
|
1170 | # directory. | |
1156 | try: |
|
1171 | try: | |
1157 | os.chdir(ipythondir) |
|
1172 | os.chdir(ipythondir) | |
1158 | except: |
|
1173 | except: | |
1159 | print """ |
|
1174 | print """ | |
1160 | Problem: changing to directory %s failed. |
|
1175 | Problem: changing to directory %s failed. | |
1161 | Details: |
|
1176 | Details: | |
1162 | %s |
|
1177 | %s | |
1163 |
|
1178 | |||
1164 | Some configuration files may have incorrect line endings. This should not |
|
1179 | Some configuration files may have incorrect line endings. This should not | |
1165 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1180 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) | |
1166 | wait() |
|
1181 | wait() | |
1167 | else: |
|
1182 | else: | |
1168 | for fname in glb('ipythonrc*'): |
|
1183 | for fname in glb('ipythonrc*'): | |
1169 | try: |
|
1184 | try: | |
1170 | native_line_ends(fname,backup=0) |
|
1185 | native_line_ends(fname,backup=0) | |
1171 | except IOError: |
|
1186 | except IOError: | |
1172 | pass |
|
1187 | pass | |
1173 |
|
1188 | |||
1174 | if mode == 'install': |
|
1189 | if mode == 'install': | |
1175 | print """ |
|
1190 | print """ | |
1176 | Successful installation! |
|
1191 | Successful installation! | |
1177 |
|
1192 | |||
1178 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1193 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the | |
1179 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1194 | IPython manual (there are both HTML and PDF versions supplied with the | |
1180 | distribution) to make sure that your system environment is properly configured |
|
1195 | distribution) to make sure that your system environment is properly configured | |
1181 | to take advantage of IPython's features.""" |
|
1196 | to take advantage of IPython's features.""" | |
1182 | else: |
|
1197 | else: | |
1183 | print """ |
|
1198 | print """ | |
1184 | Successful upgrade! |
|
1199 | Successful upgrade! | |
1185 |
|
1200 | |||
1186 | All files in your directory: |
|
1201 | All files in your directory: | |
1187 | %(ipythondir)s |
|
1202 | %(ipythondir)s | |
1188 | which would have been overwritten by the upgrade were backed up with a .old |
|
1203 | which would have been overwritten by the upgrade were backed up with a .old | |
1189 | extension. If you had made particular customizations in those files you may |
|
1204 | extension. If you had made particular customizations in those files you may | |
1190 | want to merge them back into the new files.""" % locals() |
|
1205 | want to merge them back into the new files.""" % locals() | |
1191 | wait() |
|
1206 | wait() | |
1192 | os.chdir(cwd) |
|
1207 | os.chdir(cwd) | |
1193 | # end user_setup() |
|
1208 | # end user_setup() | |
1194 |
|
1209 | |||
1195 | def atexit_operations(self): |
|
1210 | def atexit_operations(self): | |
1196 | """This will be executed at the time of exit. |
|
1211 | """This will be executed at the time of exit. | |
1197 |
|
1212 | |||
1198 | Saving of persistent data should be performed here. """ |
|
1213 | Saving of persistent data should be performed here. """ | |
1199 |
|
1214 | |||
1200 | # input history |
|
1215 | # input history | |
1201 | self.savehist() |
|
1216 | self.savehist() | |
1202 |
|
1217 | |||
1203 | # Cleanup all tempfiles left around |
|
1218 | # Cleanup all tempfiles left around | |
1204 | for tfile in self.tempfiles: |
|
1219 | for tfile in self.tempfiles: | |
1205 | try: |
|
1220 | try: | |
1206 | os.unlink(tfile) |
|
1221 | os.unlink(tfile) | |
1207 | except OSError: |
|
1222 | except OSError: | |
1208 | pass |
|
1223 | pass | |
1209 |
|
1224 | |||
1210 | # save the "persistent data" catch-all dictionary |
|
1225 | # save the "persistent data" catch-all dictionary | |
1211 | try: |
|
1226 | try: | |
1212 | pickle.dump(self.persist, open(self.persist_fname,"w")) |
|
1227 | pickle.dump(self.persist, open(self.persist_fname,"w")) | |
1213 | except: |
|
1228 | except: | |
1214 | print "*** ERROR *** persistent data saving failed." |
|
1229 | print "*** ERROR *** persistent data saving failed." | |
1215 |
|
1230 | |||
1216 | def savehist(self): |
|
1231 | def savehist(self): | |
1217 | """Save input history to a file (via readline library).""" |
|
1232 | """Save input history to a file (via readline library).""" | |
1218 | try: |
|
1233 | try: | |
1219 | self.readline.write_history_file(self.histfile) |
|
1234 | self.readline.write_history_file(self.histfile) | |
1220 | except: |
|
1235 | except: | |
1221 | print 'Unable to save IPython command history to file: ' + \ |
|
1236 | print 'Unable to save IPython command history to file: ' + \ | |
1222 | `self.histfile` |
|
1237 | `self.histfile` | |
1223 |
|
1238 | |||
1224 | def pre_readline(self): |
|
1239 | def pre_readline(self): | |
1225 | """readline hook to be used at the start of each line. |
|
1240 | """readline hook to be used at the start of each line. | |
1226 |
|
1241 | |||
1227 | Currently it handles auto-indent only.""" |
|
1242 | Currently it handles auto-indent only.""" | |
1228 |
|
1243 | |||
1229 | self.readline.insert_text(' '* self.readline_indent) |
|
1244 | self.readline.insert_text(' '* self.readline_indent) | |
1230 |
|
1245 | |||
1231 | def init_readline(self): |
|
1246 | def init_readline(self): | |
1232 | """Command history completion/saving/reloading.""" |
|
1247 | """Command history completion/saving/reloading.""" | |
1233 | try: |
|
1248 | try: | |
1234 | import readline |
|
1249 | import readline | |
1235 | self.Completer = MagicCompleter(self, |
|
1250 | self.Completer = MagicCompleter(self, | |
1236 | self.user_ns, |
|
1251 | self.user_ns, | |
|
1252 | self.user_global_ns, | |||
1237 | self.rc.readline_omit__names, |
|
1253 | self.rc.readline_omit__names, | |
1238 | self.alias_table) |
|
1254 | self.alias_table) | |
1239 | except ImportError,NameError: |
|
1255 | except ImportError,NameError: | |
1240 | # If FlexCompleter failed to import, MagicCompleter won't be |
|
1256 | # If FlexCompleter failed to import, MagicCompleter won't be | |
1241 | # defined. This can happen because of a problem with readline |
|
1257 | # defined. This can happen because of a problem with readline | |
1242 | self.has_readline = 0 |
|
1258 | self.has_readline = 0 | |
1243 | # no point in bugging windows users with this every time: |
|
1259 | # no point in bugging windows users with this every time: | |
1244 | if os.name == 'posix': |
|
1260 | if os.name == 'posix': | |
1245 | warn('Readline services not available on this platform.') |
|
1261 | warn('Readline services not available on this platform.') | |
1246 | else: |
|
1262 | else: | |
1247 | import atexit |
|
1263 | import atexit | |
1248 |
|
1264 | |||
1249 | # Platform-specific configuration |
|
1265 | # Platform-specific configuration | |
1250 | if os.name == 'nt': |
|
1266 | if os.name == 'nt': | |
1251 | # readline under Windows modifies the default exit behavior |
|
1267 | # readline under Windows modifies the default exit behavior | |
1252 | # from being Ctrl-Z/Return to the Unix Ctrl-D one. |
|
1268 | # from being Ctrl-Z/Return to the Unix Ctrl-D one. | |
1253 | __builtin__.exit = __builtin__.quit = \ |
|
1269 | __builtin__.exit = __builtin__.quit = \ | |
1254 | ('Use Ctrl-D (i.e. EOF) to exit. ' |
|
1270 | ('Use Ctrl-D (i.e. EOF) to exit. ' | |
1255 | 'Use %Exit or %Quit to exit without confirmation.') |
|
1271 | 'Use %Exit or %Quit to exit without confirmation.') | |
1256 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1272 | self.readline_startup_hook = readline.set_pre_input_hook | |
1257 | else: |
|
1273 | else: | |
1258 | self.readline_startup_hook = readline.set_startup_hook |
|
1274 | self.readline_startup_hook = readline.set_startup_hook | |
1259 |
|
1275 | |||
1260 | # Load user's initrc file (readline config) |
|
1276 | # Load user's initrc file (readline config) | |
1261 | inputrc_name = os.environ.get('INPUTRC') |
|
1277 | inputrc_name = os.environ.get('INPUTRC') | |
1262 | if inputrc_name is None: |
|
1278 | if inputrc_name is None: | |
1263 | home_dir = get_home_dir() |
|
1279 | home_dir = get_home_dir() | |
1264 | if home_dir is not None: |
|
1280 | if home_dir is not None: | |
1265 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1281 | inputrc_name = os.path.join(home_dir,'.inputrc') | |
1266 | if os.path.isfile(inputrc_name): |
|
1282 | if os.path.isfile(inputrc_name): | |
1267 | try: |
|
1283 | try: | |
1268 | readline.read_init_file(inputrc_name) |
|
1284 | readline.read_init_file(inputrc_name) | |
1269 | except: |
|
1285 | except: | |
1270 | warn('Problems reading readline initialization file <%s>' |
|
1286 | warn('Problems reading readline initialization file <%s>' | |
1271 | % inputrc_name) |
|
1287 | % inputrc_name) | |
1272 |
|
1288 | |||
1273 | self.has_readline = 1 |
|
1289 | self.has_readline = 1 | |
1274 | self.readline = readline |
|
1290 | self.readline = readline | |
1275 | self.readline_indent = 0 # for auto-indenting via readline |
|
1291 | self.readline_indent = 0 # for auto-indenting via readline | |
1276 | # save this in sys so embedded copies can restore it properly |
|
1292 | # save this in sys so embedded copies can restore it properly | |
1277 | sys.ipcompleter = self.Completer.complete |
|
1293 | sys.ipcompleter = self.Completer.complete | |
1278 | readline.set_completer(self.Completer.complete) |
|
1294 | readline.set_completer(self.Completer.complete) | |
1279 |
|
1295 | |||
1280 | # Configure readline according to user's prefs |
|
1296 | # Configure readline according to user's prefs | |
1281 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1297 | for rlcommand in self.rc.readline_parse_and_bind: | |
1282 | readline.parse_and_bind(rlcommand) |
|
1298 | readline.parse_and_bind(rlcommand) | |
1283 |
|
1299 | |||
1284 | # remove some chars from the delimiters list |
|
1300 | # remove some chars from the delimiters list | |
1285 | delims = readline.get_completer_delims() |
|
1301 | delims = readline.get_completer_delims() | |
1286 | delims = delims.translate(string._idmap, |
|
1302 | delims = delims.translate(string._idmap, | |
1287 | self.rc.readline_remove_delims) |
|
1303 | self.rc.readline_remove_delims) | |
1288 | readline.set_completer_delims(delims) |
|
1304 | readline.set_completer_delims(delims) | |
1289 | # otherwise we end up with a monster history after a while: |
|
1305 | # otherwise we end up with a monster history after a while: | |
1290 | readline.set_history_length(1000) |
|
1306 | readline.set_history_length(1000) | |
1291 | try: |
|
1307 | try: | |
1292 | #print '*** Reading readline history' # dbg |
|
1308 | #print '*** Reading readline history' # dbg | |
1293 | readline.read_history_file(self.histfile) |
|
1309 | readline.read_history_file(self.histfile) | |
1294 | except IOError: |
|
1310 | except IOError: | |
1295 | pass # It doesn't exist yet. |
|
1311 | pass # It doesn't exist yet. | |
1296 |
|
1312 | |||
1297 | atexit.register(self.atexit_operations) |
|
1313 | atexit.register(self.atexit_operations) | |
1298 | del atexit |
|
1314 | del atexit | |
1299 |
|
1315 | |||
1300 | # Configure auto-indent for all platforms |
|
1316 | # Configure auto-indent for all platforms | |
1301 | self.set_autoindent(self.rc.autoindent) |
|
1317 | self.set_autoindent(self.rc.autoindent) | |
1302 |
|
1318 | |||
1303 | def showsyntaxerror(self, filename=None): |
|
1319 | def showsyntaxerror(self, filename=None): | |
1304 | """Display the syntax error that just occurred. |
|
1320 | """Display the syntax error that just occurred. | |
1305 |
|
1321 | |||
1306 | This doesn't display a stack trace because there isn't one. |
|
1322 | This doesn't display a stack trace because there isn't one. | |
1307 |
|
1323 | |||
1308 | If a filename is given, it is stuffed in the exception instead |
|
1324 | If a filename is given, it is stuffed in the exception instead | |
1309 | of what was there before (because Python's parser always uses |
|
1325 | of what was there before (because Python's parser always uses | |
1310 | "<string>" when reading from a string). |
|
1326 | "<string>" when reading from a string). | |
1311 | """ |
|
1327 | """ | |
1312 | type, value, sys.last_traceback = sys.exc_info() |
|
1328 | type, value, sys.last_traceback = sys.exc_info() | |
1313 | sys.last_type = type |
|
1329 | sys.last_type = type | |
1314 | sys.last_value = value |
|
1330 | sys.last_value = value | |
1315 | if filename and type is SyntaxError: |
|
1331 | if filename and type is SyntaxError: | |
1316 | # Work hard to stuff the correct filename in the exception |
|
1332 | # Work hard to stuff the correct filename in the exception | |
1317 | try: |
|
1333 | try: | |
1318 | msg, (dummy_filename, lineno, offset, line) = value |
|
1334 | msg, (dummy_filename, lineno, offset, line) = value | |
1319 | except: |
|
1335 | except: | |
1320 | # Not the format we expect; leave it alone |
|
1336 | # Not the format we expect; leave it alone | |
1321 | pass |
|
1337 | pass | |
1322 | else: |
|
1338 | else: | |
1323 | # Stuff in the right filename |
|
1339 | # Stuff in the right filename | |
1324 | try: |
|
1340 | try: | |
1325 | # Assume SyntaxError is a class exception |
|
1341 | # Assume SyntaxError is a class exception | |
1326 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1342 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1327 | except: |
|
1343 | except: | |
1328 | # If that failed, assume SyntaxError is a string |
|
1344 | # If that failed, assume SyntaxError is a string | |
1329 | value = msg, (filename, lineno, offset, line) |
|
1345 | value = msg, (filename, lineno, offset, line) | |
1330 | self.SyntaxTB(type,value,[]) |
|
1346 | self.SyntaxTB(type,value,[]) | |
1331 |
|
1347 | |||
1332 | def debugger(self): |
|
1348 | def debugger(self): | |
1333 | """Call the pdb debugger.""" |
|
1349 | """Call the pdb debugger.""" | |
1334 |
|
1350 | |||
1335 | if not self.rc.pdb: |
|
1351 | if not self.rc.pdb: | |
1336 | return |
|
1352 | return | |
1337 | pdb.pm() |
|
1353 | pdb.pm() | |
1338 |
|
1354 | |||
1339 | def showtraceback(self,exc_tuple = None,filename=None): |
|
1355 | def showtraceback(self,exc_tuple = None,filename=None): | |
1340 | """Display the exception that just occurred.""" |
|
1356 | """Display the exception that just occurred.""" | |
1341 |
|
1357 | |||
1342 | # Though this won't be called by syntax errors in the input line, |
|
1358 | # Though this won't be called by syntax errors in the input line, | |
1343 | # there may be SyntaxError cases whith imported code. |
|
1359 | # there may be SyntaxError cases whith imported code. | |
1344 | if exc_tuple is None: |
|
1360 | if exc_tuple is None: | |
1345 | type, value, tb = sys.exc_info() |
|
1361 | type, value, tb = sys.exc_info() | |
1346 | else: |
|
1362 | else: | |
1347 | type, value, tb = exc_tuple |
|
1363 | type, value, tb = exc_tuple | |
1348 | if type is SyntaxError: |
|
1364 | if type is SyntaxError: | |
1349 | self.showsyntaxerror(filename) |
|
1365 | self.showsyntaxerror(filename) | |
1350 | else: |
|
1366 | else: | |
1351 | sys.last_type = type |
|
1367 | sys.last_type = type | |
1352 | sys.last_value = value |
|
1368 | sys.last_value = value | |
1353 | sys.last_traceback = tb |
|
1369 | sys.last_traceback = tb | |
1354 | self.InteractiveTB() |
|
1370 | self.InteractiveTB() | |
1355 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1371 | if self.InteractiveTB.call_pdb and self.has_readline: | |
1356 | # pdb mucks up readline, fix it back |
|
1372 | # pdb mucks up readline, fix it back | |
1357 | self.readline.set_completer(self.Completer.complete) |
|
1373 | self.readline.set_completer(self.Completer.complete) | |
1358 |
|
1374 | |||
1359 | def update_cache(self, line): |
|
1375 | def update_cache(self, line): | |
1360 | """puts line into cache""" |
|
1376 | """puts line into cache""" | |
1361 | self.inputcache.insert(0, line) # This copies the cache every time ... :-( |
|
1377 | self.inputcache.insert(0, line) # This copies the cache every time ... :-( | |
1362 | if len(self.inputcache) >= self.CACHELENGTH: |
|
1378 | if len(self.inputcache) >= self.CACHELENGTH: | |
1363 | self.inputcache.pop() # This not :-) |
|
1379 | self.inputcache.pop() # This not :-) | |
1364 |
|
1380 | |||
1365 | def mainloop(self,banner=None): |
|
1381 | def mainloop(self,banner=None): | |
1366 | """Creates the local namespace and starts the mainloop. |
|
1382 | """Creates the local namespace and starts the mainloop. | |
1367 |
|
1383 | |||
1368 | If an optional banner argument is given, it will override the |
|
1384 | If an optional banner argument is given, it will override the | |
1369 | internally created default banner.""" |
|
1385 | internally created default banner.""" | |
1370 |
|
1386 | |||
1371 | if self.rc.c: # Emulate Python's -c option |
|
1387 | if self.rc.c: # Emulate Python's -c option | |
1372 | self.exec_init_cmd() |
|
1388 | self.exec_init_cmd() | |
1373 | if banner is None: |
|
1389 | if banner is None: | |
1374 | if self.rc.banner: |
|
1390 | if self.rc.banner: | |
1375 | banner = self.BANNER+self.banner2 |
|
1391 | banner = self.BANNER+self.banner2 | |
1376 | else: |
|
1392 | else: | |
1377 | banner = '' |
|
1393 | banner = '' | |
1378 | self.interact(banner) |
|
1394 | self.interact(banner) | |
1379 |
|
1395 | |||
1380 | def exec_init_cmd(self): |
|
1396 | def exec_init_cmd(self): | |
1381 | """Execute a command given at the command line. |
|
1397 | """Execute a command given at the command line. | |
1382 |
|
1398 | |||
1383 | This emulates Python's -c option.""" |
|
1399 | This emulates Python's -c option.""" | |
1384 |
|
1400 | |||
1385 | sys.argv = ['-c'] |
|
1401 | sys.argv = ['-c'] | |
1386 | self.push(self.rc.c) |
|
1402 | self.push(self.rc.c) | |
1387 |
|
1403 | |||
1388 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1404 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): | |
1389 | """Embeds IPython into a running python program. |
|
1405 | """Embeds IPython into a running python program. | |
1390 |
|
1406 | |||
1391 | Input: |
|
1407 | Input: | |
1392 |
|
1408 | |||
1393 | - header: An optional header message can be specified. |
|
1409 | - header: An optional header message can be specified. | |
1394 |
|
1410 | |||
1395 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1411 | - local_ns, global_ns: working namespaces. If given as None, the | |
1396 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1412 | IPython-initialized one is updated with __main__.__dict__, so that | |
1397 | program variables become visible but user-specific configuration |
|
1413 | program variables become visible but user-specific configuration | |
1398 | remains possible. |
|
1414 | remains possible. | |
1399 |
|
1415 | |||
1400 | - stack_depth: specifies how many levels in the stack to go to |
|
1416 | - stack_depth: specifies how many levels in the stack to go to | |
1401 | looking for namespaces (when local_ns and global_ns are None). This |
|
1417 | looking for namespaces (when local_ns and global_ns are None). This | |
1402 | allows an intermediate caller to make sure that this function gets |
|
1418 | allows an intermediate caller to make sure that this function gets | |
1403 | the namespace from the intended level in the stack. By default (0) |
|
1419 | the namespace from the intended level in the stack. By default (0) | |
1404 | it will get its locals and globals from the immediate caller. |
|
1420 | it will get its locals and globals from the immediate caller. | |
1405 |
|
1421 | |||
1406 | Warning: it's possible to use this in a program which is being run by |
|
1422 | Warning: it's possible to use this in a program which is being run by | |
1407 | IPython itself (via %run), but some funny things will happen (a few |
|
1423 | IPython itself (via %run), but some funny things will happen (a few | |
1408 | globals get overwritten). In the future this will be cleaned up, as |
|
1424 | globals get overwritten). In the future this will be cleaned up, as | |
1409 | there is no fundamental reason why it can't work perfectly.""" |
|
1425 | there is no fundamental reason why it can't work perfectly.""" | |
1410 |
|
1426 | |||
1411 | # Get locals and globals from caller |
|
1427 | # Get locals and globals from caller | |
1412 | if local_ns is None or global_ns is None: |
|
1428 | if local_ns is None or global_ns is None: | |
1413 | call_frame = sys._getframe(stack_depth).f_back |
|
1429 | call_frame = sys._getframe(stack_depth).f_back | |
1414 |
|
1430 | |||
1415 | if local_ns is None: |
|
1431 | if local_ns is None: | |
1416 | local_ns = call_frame.f_locals |
|
1432 | local_ns = call_frame.f_locals | |
1417 | if global_ns is None: |
|
1433 | if global_ns is None: | |
1418 | global_ns = call_frame.f_globals |
|
1434 | global_ns = call_frame.f_globals | |
1419 |
|
1435 | |||
1420 | # Update namespaces and fire up interpreter |
|
1436 | # Update namespaces and fire up interpreter | |
1421 | self.user_ns = local_ns |
|
1437 | self.user_ns = local_ns | |
1422 | self.user_global_ns = global_ns |
|
1438 | self.user_global_ns = global_ns | |
1423 |
|
1439 | |||
1424 | # Patch for global embedding to make sure that things don't overwrite |
|
1440 | # Patch for global embedding to make sure that things don't overwrite | |
1425 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1441 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> | |
1426 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1442 | # FIXME. Test this a bit more carefully (the if.. is new) | |
1427 | if local_ns is None and global_ns is None: |
|
1443 | if local_ns is None and global_ns is None: | |
1428 | self.user_global_ns.update(__main__.__dict__) |
|
1444 | self.user_global_ns.update(__main__.__dict__) | |
1429 |
|
1445 | |||
|
1446 | # make sure the tab-completer has the correct frame information, so it | |||
|
1447 | # actually completes using the frame's locals/globals | |||
|
1448 | self.set_completer_frame(call_frame) | |||
|
1449 | ||||
1430 | self.interact(header) |
|
1450 | self.interact(header) | |
1431 |
|
1451 | |||
1432 | def interact(self, banner=None): |
|
1452 | def interact(self, banner=None): | |
1433 | """Closely emulate the interactive Python console. |
|
1453 | """Closely emulate the interactive Python console. | |
1434 |
|
1454 | |||
1435 | The optional banner argument specify the banner to print |
|
1455 | The optional banner argument specify the banner to print | |
1436 | before the first interaction; by default it prints a banner |
|
1456 | before the first interaction; by default it prints a banner | |
1437 | similar to the one printed by the real Python interpreter, |
|
1457 | similar to the one printed by the real Python interpreter, | |
1438 | followed by the current class name in parentheses (so as not |
|
1458 | followed by the current class name in parentheses (so as not | |
1439 | to confuse this with the real interpreter -- since it's so |
|
1459 | to confuse this with the real interpreter -- since it's so | |
1440 | close!). |
|
1460 | close!). | |
1441 |
|
1461 | |||
1442 | """ |
|
1462 | """ | |
1443 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1463 | cprt = 'Type "copyright", "credits" or "license" for more information.' | |
1444 | if banner is None: |
|
1464 | if banner is None: | |
1445 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1465 | self.write("Python %s on %s\n%s\n(%s)\n" % | |
1446 | (sys.version, sys.platform, cprt, |
|
1466 | (sys.version, sys.platform, cprt, | |
1447 | self.__class__.__name__)) |
|
1467 | self.__class__.__name__)) | |
1448 | else: |
|
1468 | else: | |
1449 | self.write(banner) |
|
1469 | self.write(banner) | |
1450 |
|
1470 | |||
1451 | more = 0 |
|
1471 | more = 0 | |
1452 |
|
1472 | |||
1453 | # Mark activity in the builtins |
|
1473 | # Mark activity in the builtins | |
1454 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1474 | __builtin__.__dict__['__IPYTHON__active'] += 1 | |
1455 |
|
1475 | |||
1456 | # exit_now is set by a call to %Exit or %Quit |
|
1476 | # exit_now is set by a call to %Exit or %Quit | |
1457 | while not self.exit_now: |
|
1477 | while not self.exit_now: | |
1458 | try: |
|
1478 | try: | |
1459 | if more: |
|
1479 | if more: | |
1460 | prompt = self.outputcache.prompt2 |
|
1480 | prompt = self.outputcache.prompt2 | |
1461 | if self.autoindent: |
|
1481 | if self.autoindent: | |
1462 | self.readline_startup_hook(self.pre_readline) |
|
1482 | self.readline_startup_hook(self.pre_readline) | |
1463 | else: |
|
1483 | else: | |
1464 | prompt = self.outputcache.prompt1 |
|
1484 | prompt = self.outputcache.prompt1 | |
1465 | try: |
|
1485 | try: | |
1466 | line = self.raw_input(prompt) |
|
1486 | line = self.raw_input(prompt) | |
1467 | if self.autoindent: |
|
1487 | if self.autoindent: | |
1468 | self.readline_startup_hook(None) |
|
1488 | self.readline_startup_hook(None) | |
1469 | except EOFError: |
|
1489 | except EOFError: | |
1470 | if self.autoindent: |
|
1490 | if self.autoindent: | |
1471 | self.readline_startup_hook(None) |
|
1491 | self.readline_startup_hook(None) | |
1472 | self.write("\n") |
|
1492 | self.write("\n") | |
1473 | if self.rc.confirm_exit: |
|
1493 | if self.rc.confirm_exit: | |
1474 | if ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
1494 | if ask_yes_no('Do you really want to exit ([y]/n)?','y'): | |
1475 | break |
|
1495 | break | |
1476 | else: |
|
1496 | else: | |
1477 | break |
|
1497 | break | |
1478 | else: |
|
1498 | else: | |
1479 | more = self.push(line) |
|
1499 | more = self.push(line) | |
1480 | # Auto-indent management |
|
1500 | # Auto-indent management | |
1481 | if self.autoindent: |
|
1501 | if self.autoindent: | |
1482 | if line: |
|
1502 | if line: | |
1483 | ini_spaces = re.match('^(\s+)',line) |
|
1503 | ini_spaces = re.match('^(\s+)',line) | |
1484 | if ini_spaces: |
|
1504 | if ini_spaces: | |
1485 | nspaces = ini_spaces.end() |
|
1505 | nspaces = ini_spaces.end() | |
1486 | else: |
|
1506 | else: | |
1487 | nspaces = 0 |
|
1507 | nspaces = 0 | |
1488 | self.readline_indent = nspaces |
|
1508 | self.readline_indent = nspaces | |
1489 |
|
1509 | |||
1490 | if line[-1] == ':': |
|
1510 | if line[-1] == ':': | |
1491 | self.readline_indent += 4 |
|
1511 | self.readline_indent += 4 | |
1492 | elif re.match(r'^\s+raise|^\s+return',line): |
|
1512 | elif re.match(r'^\s+raise|^\s+return',line): | |
1493 | self.readline_indent -= 4 |
|
1513 | self.readline_indent -= 4 | |
1494 | else: |
|
1514 | else: | |
1495 | self.readline_indent = 0 |
|
1515 | self.readline_indent = 0 | |
1496 |
|
1516 | |||
1497 | except KeyboardInterrupt: |
|
1517 | except KeyboardInterrupt: | |
1498 | self.write("\nKeyboardInterrupt\n") |
|
1518 | self.write("\nKeyboardInterrupt\n") | |
1499 | self.resetbuffer() |
|
1519 | self.resetbuffer() | |
1500 | more = 0 |
|
1520 | more = 0 | |
1501 | # keep cache in sync with the prompt counter: |
|
1521 | # keep cache in sync with the prompt counter: | |
1502 | self.outputcache.prompt_count -= 1 |
|
1522 | self.outputcache.prompt_count -= 1 | |
1503 |
|
1523 | |||
1504 | if self.autoindent: |
|
1524 | if self.autoindent: | |
1505 | self.readline_indent = 0 |
|
1525 | self.readline_indent = 0 | |
1506 |
|
1526 | |||
1507 | except bdb.BdbQuit: |
|
1527 | except bdb.BdbQuit: | |
1508 | warn("The Python debugger has exited with a BdbQuit exception.\n" |
|
1528 | warn("The Python debugger has exited with a BdbQuit exception.\n" | |
1509 | "Because of how pdb handles the stack, it is impossible\n" |
|
1529 | "Because of how pdb handles the stack, it is impossible\n" | |
1510 | "for IPython to properly format this particular exception.\n" |
|
1530 | "for IPython to properly format this particular exception.\n" | |
1511 | "IPython will resume normal operation.") |
|
1531 | "IPython will resume normal operation.") | |
1512 |
|
1532 | |||
1513 | # We are off again... |
|
1533 | # We are off again... | |
1514 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1534 | __builtin__.__dict__['__IPYTHON__active'] -= 1 | |
1515 |
|
1535 | |||
1516 | def excepthook(self, type, value, tb): |
|
1536 | def excepthook(self, type, value, tb): | |
1517 | """One more defense for GUI apps that call sys.excepthook. |
|
1537 | """One more defense for GUI apps that call sys.excepthook. | |
1518 |
|
1538 | |||
1519 | GUI frameworks like wxPython trap exceptions and call |
|
1539 | GUI frameworks like wxPython trap exceptions and call | |
1520 | sys.excepthook themselves. I guess this is a feature that |
|
1540 | sys.excepthook themselves. I guess this is a feature that | |
1521 | enables them to keep running after exceptions that would |
|
1541 | enables them to keep running after exceptions that would | |
1522 | otherwise kill their mainloop. This is a bother for IPython |
|
1542 | otherwise kill their mainloop. This is a bother for IPython | |
1523 | which excepts to catch all of the program exceptions with a try: |
|
1543 | which excepts to catch all of the program exceptions with a try: | |
1524 | except: statement. |
|
1544 | except: statement. | |
1525 |
|
1545 | |||
1526 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1546 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1527 | any app directly invokes sys.excepthook, it will look to the user like |
|
1547 | any app directly invokes sys.excepthook, it will look to the user like | |
1528 | IPython crashed. In order to work around this, we can disable the |
|
1548 | IPython crashed. In order to work around this, we can disable the | |
1529 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1549 | CrashHandler and replace it with this excepthook instead, which prints a | |
1530 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1550 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1531 | call sys.excepthook will generate a regular-looking exception from |
|
1551 | call sys.excepthook will generate a regular-looking exception from | |
1532 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1552 | IPython, and the CrashHandler will only be triggered by real IPython | |
1533 | crashes. |
|
1553 | crashes. | |
1534 |
|
1554 | |||
1535 | This hook should be used sparingly, only in places which are not likely |
|
1555 | This hook should be used sparingly, only in places which are not likely | |
1536 | to be true IPython errors. |
|
1556 | to be true IPython errors. | |
1537 | """ |
|
1557 | """ | |
1538 |
|
1558 | |||
1539 | self.InteractiveTB(type, value, tb, tb_offset=0) |
|
1559 | self.InteractiveTB(type, value, tb, tb_offset=0) | |
1540 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1560 | if self.InteractiveTB.call_pdb and self.has_readline: | |
1541 | self.readline.set_completer(self.Completer.complete) |
|
1561 | self.readline.set_completer(self.Completer.complete) | |
1542 |
|
1562 | |||
1543 | def call_alias(self,alias,rest=''): |
|
1563 | def call_alias(self,alias,rest=''): | |
1544 | """Call an alias given its name and the rest of the line. |
|
1564 | """Call an alias given its name and the rest of the line. | |
1545 |
|
1565 | |||
1546 | This function MUST be given a proper alias, because it doesn't make |
|
1566 | This function MUST be given a proper alias, because it doesn't make | |
1547 | any checks when looking up into the alias table. The caller is |
|
1567 | any checks when looking up into the alias table. The caller is | |
1548 | responsible for invoking it only with a valid alias.""" |
|
1568 | responsible for invoking it only with a valid alias.""" | |
1549 |
|
1569 | |||
1550 | #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg |
|
1570 | #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg | |
1551 | nargs,cmd = self.alias_table[alias] |
|
1571 | nargs,cmd = self.alias_table[alias] | |
1552 | # Expand the %l special to be the user's input line |
|
1572 | # Expand the %l special to be the user's input line | |
1553 | if cmd.find('%l') >= 0: |
|
1573 | if cmd.find('%l') >= 0: | |
1554 | cmd = cmd.replace('%l',rest) |
|
1574 | cmd = cmd.replace('%l',rest) | |
1555 | rest = '' |
|
1575 | rest = '' | |
1556 | if nargs==0: |
|
1576 | if nargs==0: | |
1557 | # Simple, argument-less aliases |
|
1577 | # Simple, argument-less aliases | |
1558 | cmd = '%s %s' % (cmd,rest) |
|
1578 | cmd = '%s %s' % (cmd,rest) | |
1559 | else: |
|
1579 | else: | |
1560 | # Handle aliases with positional arguments |
|
1580 | # Handle aliases with positional arguments | |
1561 | args = rest.split(None,nargs) |
|
1581 | args = rest.split(None,nargs) | |
1562 | if len(args)< nargs: |
|
1582 | if len(args)< nargs: | |
1563 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1583 | error('Alias <%s> requires %s arguments, %s given.' % | |
1564 | (alias,nargs,len(args))) |
|
1584 | (alias,nargs,len(args))) | |
1565 | return |
|
1585 | return | |
1566 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1586 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) | |
1567 | # Now call the macro, evaluating in the user's namespace |
|
1587 | # Now call the macro, evaluating in the user's namespace | |
1568 | try: |
|
1588 | try: | |
1569 | self.system(cmd) |
|
1589 | self.system(cmd) | |
1570 | except: |
|
1590 | except: | |
1571 | self.showtraceback() |
|
1591 | self.showtraceback() | |
1572 |
|
1592 | |||
1573 | def runlines(self,lines): |
|
1593 | def runlines(self,lines): | |
1574 | """Run a string of one or more lines of source. |
|
1594 | """Run a string of one or more lines of source. | |
1575 |
|
1595 | |||
1576 | This method is capable of running a string containing multiple source |
|
1596 | This method is capable of running a string containing multiple source | |
1577 | lines, as if they had been entered at the IPython prompt. Since it |
|
1597 | lines, as if they had been entered at the IPython prompt. Since it | |
1578 | exposes IPython's processing machinery, the given strings can contain |
|
1598 | exposes IPython's processing machinery, the given strings can contain | |
1579 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1599 | magic calls (%magic), special shell access (!cmd), etc.""" | |
1580 |
|
1600 | |||
1581 | # We must start with a clean buffer, in case this is run from an |
|
1601 | # We must start with a clean buffer, in case this is run from an | |
1582 | # interactive IPython session (via a magic, for example). |
|
1602 | # interactive IPython session (via a magic, for example). | |
1583 | self.resetbuffer() |
|
1603 | self.resetbuffer() | |
1584 | lines = lines.split('\n') |
|
1604 | lines = lines.split('\n') | |
1585 | more = 0 |
|
1605 | more = 0 | |
1586 | for line in lines: |
|
1606 | for line in lines: | |
1587 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1607 | # skip blank lines so we don't mess up the prompt counter, but do | |
1588 | # NOT skip even a blank line if we are in a code block (more is |
|
1608 | # NOT skip even a blank line if we are in a code block (more is | |
1589 | # true) |
|
1609 | # true) | |
1590 | if line or more: |
|
1610 | if line or more: | |
1591 | more = self.push((self.prefilter(line,more))) |
|
1611 | more = self.push((self.prefilter(line,more))) | |
1592 | # IPython's runsource returns None if there was an error |
|
1612 | # IPython's runsource returns None if there was an error | |
1593 | # compiling the code. This allows us to stop processing right |
|
1613 | # compiling the code. This allows us to stop processing right | |
1594 | # away, so the user gets the error message at the right place. |
|
1614 | # away, so the user gets the error message at the right place. | |
1595 | if more is None: |
|
1615 | if more is None: | |
1596 | break |
|
1616 | break | |
1597 | # final newline in case the input didn't have it, so that the code |
|
1617 | # final newline in case the input didn't have it, so that the code | |
1598 | # actually does get executed |
|
1618 | # actually does get executed | |
1599 | if more: |
|
1619 | if more: | |
1600 | self.push('\n') |
|
1620 | self.push('\n') | |
1601 |
|
1621 | |||
1602 | def runsource(self, source, filename="<input>", symbol="single"): |
|
1622 | def runsource(self, source, filename="<input>", symbol="single"): | |
1603 | """Compile and run some source in the interpreter. |
|
1623 | """Compile and run some source in the interpreter. | |
1604 |
|
1624 | |||
1605 | Arguments are as for compile_command(). |
|
1625 | Arguments are as for compile_command(). | |
1606 |
|
1626 | |||
1607 | One several things can happen: |
|
1627 | One several things can happen: | |
1608 |
|
1628 | |||
1609 | 1) The input is incorrect; compile_command() raised an |
|
1629 | 1) The input is incorrect; compile_command() raised an | |
1610 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1630 | exception (SyntaxError or OverflowError). A syntax traceback | |
1611 | will be printed by calling the showsyntaxerror() method. |
|
1631 | will be printed by calling the showsyntaxerror() method. | |
1612 |
|
1632 | |||
1613 | 2) The input is incomplete, and more input is required; |
|
1633 | 2) The input is incomplete, and more input is required; | |
1614 | compile_command() returned None. Nothing happens. |
|
1634 | compile_command() returned None. Nothing happens. | |
1615 |
|
1635 | |||
1616 | 3) The input is complete; compile_command() returned a code |
|
1636 | 3) The input is complete; compile_command() returned a code | |
1617 | object. The code is executed by calling self.runcode() (which |
|
1637 | object. The code is executed by calling self.runcode() (which | |
1618 | also handles run-time exceptions, except for SystemExit). |
|
1638 | also handles run-time exceptions, except for SystemExit). | |
1619 |
|
1639 | |||
1620 | The return value is: |
|
1640 | The return value is: | |
1621 |
|
1641 | |||
1622 | - True in case 2 |
|
1642 | - True in case 2 | |
1623 |
|
1643 | |||
1624 | - False in the other cases, unless an exception is raised, where |
|
1644 | - False in the other cases, unless an exception is raised, where | |
1625 | None is returned instead. This can be used by external callers to |
|
1645 | None is returned instead. This can be used by external callers to | |
1626 | know whether to continue feeding input or not. |
|
1646 | know whether to continue feeding input or not. | |
1627 |
|
1647 | |||
1628 | The return value can be used to decide whether to use sys.ps1 or |
|
1648 | The return value can be used to decide whether to use sys.ps1 or | |
1629 | sys.ps2 to prompt the next line.""" |
|
1649 | sys.ps2 to prompt the next line.""" | |
1630 |
|
1650 | |||
1631 | try: |
|
1651 | try: | |
1632 | code = self.compile(source, filename, symbol) |
|
1652 | code = self.compile(source, filename, symbol) | |
1633 | except (OverflowError, SyntaxError, ValueError): |
|
1653 | except (OverflowError, SyntaxError, ValueError): | |
1634 | # Case 1 |
|
1654 | # Case 1 | |
1635 | self.showsyntaxerror(filename) |
|
1655 | self.showsyntaxerror(filename) | |
1636 | return None |
|
1656 | return None | |
1637 |
|
1657 | |||
1638 | if code is None: |
|
1658 | if code is None: | |
1639 | # Case 2 |
|
1659 | # Case 2 | |
1640 | return True |
|
1660 | return True | |
1641 |
|
1661 | |||
1642 | # Case 3 |
|
1662 | # Case 3 | |
1643 | # We store the code object so that threaded shells and |
|
1663 | # We store the code object so that threaded shells and | |
1644 | # custom exception handlers can access all this info if needed. |
|
1664 | # custom exception handlers can access all this info if needed. | |
1645 | # The source corresponding to this can be obtained from the |
|
1665 | # The source corresponding to this can be obtained from the | |
1646 | # buffer attribute as '\n'.join(self.buffer). |
|
1666 | # buffer attribute as '\n'.join(self.buffer). | |
1647 | self.code_to_run = code |
|
1667 | self.code_to_run = code | |
1648 | # now actually execute the code object |
|
1668 | # now actually execute the code object | |
1649 | if self.runcode(code) == 0: |
|
1669 | if self.runcode(code) == 0: | |
1650 | return False |
|
1670 | return False | |
1651 | else: |
|
1671 | else: | |
1652 | return None |
|
1672 | return None | |
1653 |
|
1673 | |||
1654 | def runcode(self,code_obj): |
|
1674 | def runcode(self,code_obj): | |
1655 | """Execute a code object. |
|
1675 | """Execute a code object. | |
1656 |
|
1676 | |||
1657 | When an exception occurs, self.showtraceback() is called to display a |
|
1677 | When an exception occurs, self.showtraceback() is called to display a | |
1658 | traceback. |
|
1678 | traceback. | |
1659 |
|
1679 | |||
1660 | Return value: a flag indicating whether the code to be run completed |
|
1680 | Return value: a flag indicating whether the code to be run completed | |
1661 | successfully: |
|
1681 | successfully: | |
1662 |
|
1682 | |||
1663 | - 0: successful execution. |
|
1683 | - 0: successful execution. | |
1664 | - 1: an error occurred. |
|
1684 | - 1: an error occurred. | |
1665 | """ |
|
1685 | """ | |
1666 |
|
1686 | |||
1667 | # Set our own excepthook in case the user code tries to call it |
|
1687 | # Set our own excepthook in case the user code tries to call it | |
1668 | # directly, so that the IPython crash handler doesn't get triggered |
|
1688 | # directly, so that the IPython crash handler doesn't get triggered | |
1669 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1689 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
1670 | outflag = 1 # happens in more places, so it's easier as default |
|
1690 | outflag = 1 # happens in more places, so it's easier as default | |
1671 | try: |
|
1691 | try: | |
1672 | try: |
|
1692 | try: | |
1673 | # Embedded instances require separate global/local namespaces |
|
1693 | # Embedded instances require separate global/local namespaces | |
1674 | # so they can see both the surrounding (local) namespace and |
|
1694 | # so they can see both the surrounding (local) namespace and | |
1675 | # the module-level globals when called inside another function. |
|
1695 | # the module-level globals when called inside another function. | |
1676 | if self.embedded: |
|
1696 | if self.embedded: | |
1677 | exec code_obj in self.user_global_ns, self.user_ns |
|
1697 | exec code_obj in self.user_global_ns, self.user_ns | |
1678 | # Normal (non-embedded) instances should only have a single |
|
1698 | # Normal (non-embedded) instances should only have a single | |
1679 | # namespace for user code execution, otherwise functions won't |
|
1699 | # namespace for user code execution, otherwise functions won't | |
1680 | # see interactive top-level globals. |
|
1700 | # see interactive top-level globals. | |
1681 | else: |
|
1701 | else: | |
1682 | exec code_obj in self.user_ns |
|
1702 | exec code_obj in self.user_ns | |
1683 | finally: |
|
1703 | finally: | |
1684 | # Reset our crash handler in place |
|
1704 | # Reset our crash handler in place | |
1685 | sys.excepthook = old_excepthook |
|
1705 | sys.excepthook = old_excepthook | |
1686 | except SystemExit: |
|
1706 | except SystemExit: | |
1687 | self.resetbuffer() |
|
1707 | self.resetbuffer() | |
1688 | self.showtraceback() |
|
1708 | self.showtraceback() | |
1689 | warn( __builtin__.exit,level=1) |
|
1709 | warn( __builtin__.exit,level=1) | |
1690 | except self.custom_exceptions: |
|
1710 | except self.custom_exceptions: | |
1691 | etype,value,tb = sys.exc_info() |
|
1711 | etype,value,tb = sys.exc_info() | |
1692 | self.CustomTB(etype,value,tb) |
|
1712 | self.CustomTB(etype,value,tb) | |
1693 | except: |
|
1713 | except: | |
1694 | self.showtraceback() |
|
1714 | self.showtraceback() | |
1695 | else: |
|
1715 | else: | |
1696 | outflag = 0 |
|
1716 | outflag = 0 | |
1697 | if code.softspace(sys.stdout, 0): |
|
1717 | if code.softspace(sys.stdout, 0): | |
1698 |
|
1718 | |||
1699 | # Flush out code object which has been run (and source) |
|
1719 | # Flush out code object which has been run (and source) | |
1700 | self.code_to_run = None |
|
1720 | self.code_to_run = None | |
1701 | return outflag |
|
1721 | return outflag | |
1702 |
|
1722 | |||
1703 | def raw_input(self, prompt=""): |
|
1723 | def raw_input(self, prompt=""): | |
1704 | """Write a prompt and read a line. |
|
1724 | """Write a prompt and read a line. | |
1705 |
|
1725 | |||
1706 | The returned line does not include the trailing newline. |
|
1726 | The returned line does not include the trailing newline. | |
1707 | When the user enters the EOF key sequence, EOFError is raised. |
|
1727 | When the user enters the EOF key sequence, EOFError is raised. | |
1708 |
|
1728 | |||
1709 | The base implementation uses the built-in function |
|
1729 | The base implementation uses the built-in function | |
1710 | raw_input(); a subclass may replace this with a different |
|
1730 | raw_input(); a subclass may replace this with a different | |
1711 | implementation. |
|
1731 | implementation. | |
1712 | """ |
|
1732 | """ | |
1713 | return self.prefilter(raw_input_original(prompt), |
|
1733 | return self.prefilter(raw_input_original(prompt), | |
1714 | prompt==self.outputcache.prompt2) |
|
1734 | prompt==self.outputcache.prompt2) | |
1715 |
|
1735 | |||
1716 | def split_user_input(self,line): |
|
1736 | def split_user_input(self,line): | |
1717 | """Split user input into pre-char, function part and rest.""" |
|
1737 | """Split user input into pre-char, function part and rest.""" | |
1718 |
|
1738 | |||
1719 | lsplit = self.line_split.match(line) |
|
1739 | lsplit = self.line_split.match(line) | |
1720 | if lsplit is None: # no regexp match returns None |
|
1740 | if lsplit is None: # no regexp match returns None | |
1721 | try: |
|
1741 | try: | |
1722 | iFun,theRest = line.split(None,1) |
|
1742 | iFun,theRest = line.split(None,1) | |
1723 | except ValueError: |
|
1743 | except ValueError: | |
1724 | iFun,theRest = line,'' |
|
1744 | iFun,theRest = line,'' | |
1725 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
1745 | pre = re.match('^(\s*)(.*)',line).groups()[0] | |
1726 | else: |
|
1746 | else: | |
1727 | pre,iFun,theRest = lsplit.groups() |
|
1747 | pre,iFun,theRest = lsplit.groups() | |
1728 |
|
1748 | |||
1729 | #print 'line:<%s>' % line # dbg |
|
1749 | #print 'line:<%s>' % line # dbg | |
1730 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
1750 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg | |
1731 | return pre,iFun.strip(),theRest |
|
1751 | return pre,iFun.strip(),theRest | |
1732 |
|
1752 | |||
1733 | def _prefilter(self, line, continue_prompt): |
|
1753 | def _prefilter(self, line, continue_prompt): | |
1734 | """Calls different preprocessors, depending on the form of line.""" |
|
1754 | """Calls different preprocessors, depending on the form of line.""" | |
1735 |
|
1755 | |||
1736 | # All handlers *must* return a value, even if it's blank (''). |
|
1756 | # All handlers *must* return a value, even if it's blank (''). | |
1737 |
|
1757 | |||
1738 | # Lines are NOT logged here. Handlers should process the line as |
|
1758 | # Lines are NOT logged here. Handlers should process the line as | |
1739 | # needed, update the cache AND log it (so that the input cache array |
|
1759 | # needed, update the cache AND log it (so that the input cache array | |
1740 | # stays synced). |
|
1760 | # stays synced). | |
1741 |
|
1761 | |||
1742 | # This function is _very_ delicate, and since it's also the one which |
|
1762 | # This function is _very_ delicate, and since it's also the one which | |
1743 | # determines IPython's response to user input, it must be as efficient |
|
1763 | # determines IPython's response to user input, it must be as efficient | |
1744 | # as possible. For this reason it has _many_ returns in it, trying |
|
1764 | # as possible. For this reason it has _many_ returns in it, trying | |
1745 | # always to exit as quickly as it can figure out what it needs to do. |
|
1765 | # always to exit as quickly as it can figure out what it needs to do. | |
1746 |
|
1766 | |||
1747 | # This function is the main responsible for maintaining IPython's |
|
1767 | # This function is the main responsible for maintaining IPython's | |
1748 | # behavior respectful of Python's semantics. So be _very_ careful if |
|
1768 | # behavior respectful of Python's semantics. So be _very_ careful if | |
1749 | # making changes to anything here. |
|
1769 | # making changes to anything here. | |
1750 |
|
1770 | |||
1751 | #..................................................................... |
|
1771 | #..................................................................... | |
1752 | # Code begins |
|
1772 | # Code begins | |
1753 |
|
1773 | |||
1754 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
1774 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg | |
1755 |
|
1775 | |||
1756 | # save the line away in case we crash, so the post-mortem handler can |
|
1776 | # save the line away in case we crash, so the post-mortem handler can | |
1757 | # record it |
|
1777 | # record it | |
1758 | self._last_input_line = line |
|
1778 | self._last_input_line = line | |
1759 |
|
1779 | |||
1760 | #print '***line: <%s>' % line # dbg |
|
1780 | #print '***line: <%s>' % line # dbg | |
1761 |
|
1781 | |||
1762 | # the input history needs to track even empty lines |
|
1782 | # the input history needs to track even empty lines | |
1763 | if not line.strip(): |
|
1783 | if not line.strip(): | |
1764 | if not continue_prompt: |
|
1784 | if not continue_prompt: | |
1765 | self.outputcache.prompt_count -= 1 |
|
1785 | self.outputcache.prompt_count -= 1 | |
1766 | return self.handle_normal('',continue_prompt) |
|
1786 | return self.handle_normal('',continue_prompt) | |
1767 |
|
1787 | |||
1768 | # print '***cont',continue_prompt # dbg |
|
1788 | # print '***cont',continue_prompt # dbg | |
1769 | # special handlers are only allowed for single line statements |
|
1789 | # special handlers are only allowed for single line statements | |
1770 | if continue_prompt and not self.rc.multi_line_specials: |
|
1790 | if continue_prompt and not self.rc.multi_line_specials: | |
1771 | return self.handle_normal(line,continue_prompt) |
|
1791 | return self.handle_normal(line,continue_prompt) | |
1772 |
|
1792 | |||
1773 | # For the rest, we need the structure of the input |
|
1793 | # For the rest, we need the structure of the input | |
1774 | pre,iFun,theRest = self.split_user_input(line) |
|
1794 | pre,iFun,theRest = self.split_user_input(line) | |
1775 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1795 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
1776 |
|
1796 | |||
1777 | # First check for explicit escapes in the last/first character |
|
1797 | # First check for explicit escapes in the last/first character | |
1778 | handler = None |
|
1798 | handler = None | |
1779 | if line[-1] == self.ESC_HELP: |
|
1799 | if line[-1] == self.ESC_HELP: | |
1780 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end |
|
1800 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end | |
1781 | if handler is None: |
|
1801 | if handler is None: | |
1782 | # look at the first character of iFun, NOT of line, so we skip |
|
1802 | # look at the first character of iFun, NOT of line, so we skip | |
1783 | # leading whitespace in multiline input |
|
1803 | # leading whitespace in multiline input | |
1784 | handler = self.esc_handlers.get(iFun[0:1]) |
|
1804 | handler = self.esc_handlers.get(iFun[0:1]) | |
1785 | if handler is not None: |
|
1805 | if handler is not None: | |
1786 | return handler(line,continue_prompt,pre,iFun,theRest) |
|
1806 | return handler(line,continue_prompt,pre,iFun,theRest) | |
1787 | # Emacs ipython-mode tags certain input lines |
|
1807 | # Emacs ipython-mode tags certain input lines | |
1788 | if line.endswith('# PYTHON-MODE'): |
|
1808 | if line.endswith('# PYTHON-MODE'): | |
1789 | return self.handle_emacs(line,continue_prompt) |
|
1809 | return self.handle_emacs(line,continue_prompt) | |
1790 |
|
1810 | |||
1791 | # Next, check if we can automatically execute this thing |
|
1811 | # Next, check if we can automatically execute this thing | |
1792 |
|
1812 | |||
1793 | # Allow ! in multi-line statements if multi_line_specials is on: |
|
1813 | # Allow ! in multi-line statements if multi_line_specials is on: | |
1794 | if continue_prompt and self.rc.multi_line_specials and \ |
|
1814 | if continue_prompt and self.rc.multi_line_specials and \ | |
1795 | iFun.startswith(self.ESC_SHELL): |
|
1815 | iFun.startswith(self.ESC_SHELL): | |
1796 | return self.handle_shell_escape(line,continue_prompt, |
|
1816 | return self.handle_shell_escape(line,continue_prompt, | |
1797 | pre=pre,iFun=iFun, |
|
1817 | pre=pre,iFun=iFun, | |
1798 | theRest=theRest) |
|
1818 | theRest=theRest) | |
1799 |
|
1819 | |||
1800 | # Let's try to find if the input line is a magic fn |
|
1820 | # Let's try to find if the input line is a magic fn | |
1801 | oinfo = None |
|
1821 | oinfo = None | |
1802 | if hasattr(self,'magic_'+iFun): |
|
1822 | if hasattr(self,'magic_'+iFun): | |
1803 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1823 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic | |
1804 | if oinfo['ismagic']: |
|
1824 | if oinfo['ismagic']: | |
1805 | # Be careful not to call magics when a variable assignment is |
|
1825 | # Be careful not to call magics when a variable assignment is | |
1806 | # being made (ls='hi', for example) |
|
1826 | # being made (ls='hi', for example) | |
1807 | if self.rc.automagic and \ |
|
1827 | if self.rc.automagic and \ | |
1808 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ |
|
1828 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ | |
1809 | (self.rc.multi_line_specials or not continue_prompt): |
|
1829 | (self.rc.multi_line_specials or not continue_prompt): | |
1810 | return self.handle_magic(line,continue_prompt, |
|
1830 | return self.handle_magic(line,continue_prompt, | |
1811 | pre,iFun,theRest) |
|
1831 | pre,iFun,theRest) | |
1812 | else: |
|
1832 | else: | |
1813 | return self.handle_normal(line,continue_prompt) |
|
1833 | return self.handle_normal(line,continue_prompt) | |
1814 |
|
1834 | |||
1815 | # If the rest of the line begins with an (in)equality, assginment or |
|
1835 | # If the rest of the line begins with an (in)equality, assginment or | |
1816 | # function call, we should not call _ofind but simply execute it. |
|
1836 | # function call, we should not call _ofind but simply execute it. | |
1817 | # This avoids spurious geattr() accesses on objects upon assignment. |
|
1837 | # This avoids spurious geattr() accesses on objects upon assignment. | |
1818 | # |
|
1838 | # | |
1819 | # It also allows users to assign to either alias or magic names true |
|
1839 | # It also allows users to assign to either alias or magic names true | |
1820 | # python variables (the magic/alias systems always take second seat to |
|
1840 | # python variables (the magic/alias systems always take second seat to | |
1821 | # true python code). |
|
1841 | # true python code). | |
1822 | if theRest and theRest[0] in '!=()': |
|
1842 | if theRest and theRest[0] in '!=()': | |
1823 | return self.handle_normal(line,continue_prompt) |
|
1843 | return self.handle_normal(line,continue_prompt) | |
1824 |
|
1844 | |||
1825 | if oinfo is None: |
|
1845 | if oinfo is None: | |
1826 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1846 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic | |
1827 |
|
1847 | |||
1828 | if not oinfo['found']: |
|
1848 | if not oinfo['found']: | |
1829 | return self.handle_normal(line,continue_prompt) |
|
1849 | return self.handle_normal(line,continue_prompt) | |
1830 | else: |
|
1850 | else: | |
1831 | #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg |
|
1851 | #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg | |
1832 | if oinfo['isalias']: |
|
1852 | if oinfo['isalias']: | |
1833 | return self.handle_alias(line,continue_prompt, |
|
1853 | return self.handle_alias(line,continue_prompt, | |
1834 | pre,iFun,theRest) |
|
1854 | pre,iFun,theRest) | |
1835 |
|
1855 | |||
1836 | if self.rc.autocall and \ |
|
1856 | if self.rc.autocall and \ | |
1837 | not self.re_exclude_auto.match(theRest) and \ |
|
1857 | not self.re_exclude_auto.match(theRest) and \ | |
1838 | self.re_fun_name.match(iFun) and \ |
|
1858 | self.re_fun_name.match(iFun) and \ | |
1839 | callable(oinfo['obj']) : |
|
1859 | callable(oinfo['obj']) : | |
1840 | #print 'going auto' # dbg |
|
1860 | #print 'going auto' # dbg | |
1841 | return self.handle_auto(line,continue_prompt,pre,iFun,theRest) |
|
1861 | return self.handle_auto(line,continue_prompt,pre,iFun,theRest) | |
1842 | else: |
|
1862 | else: | |
1843 | #print 'was callable?', callable(oinfo['obj']) # dbg |
|
1863 | #print 'was callable?', callable(oinfo['obj']) # dbg | |
1844 | return self.handle_normal(line,continue_prompt) |
|
1864 | return self.handle_normal(line,continue_prompt) | |
1845 |
|
1865 | |||
1846 | # If we get here, we have a normal Python line. Log and return. |
|
1866 | # If we get here, we have a normal Python line. Log and return. | |
1847 | return self.handle_normal(line,continue_prompt) |
|
1867 | return self.handle_normal(line,continue_prompt) | |
1848 |
|
1868 | |||
1849 | def _prefilter_dumb(self, line, continue_prompt): |
|
1869 | def _prefilter_dumb(self, line, continue_prompt): | |
1850 | """simple prefilter function, for debugging""" |
|
1870 | """simple prefilter function, for debugging""" | |
1851 | return self.handle_normal(line,continue_prompt) |
|
1871 | return self.handle_normal(line,continue_prompt) | |
1852 |
|
1872 | |||
1853 | # Set the default prefilter() function (this can be user-overridden) |
|
1873 | # Set the default prefilter() function (this can be user-overridden) | |
1854 | prefilter = _prefilter |
|
1874 | prefilter = _prefilter | |
1855 |
|
1875 | |||
1856 | def handle_normal(self,line,continue_prompt=None, |
|
1876 | def handle_normal(self,line,continue_prompt=None, | |
1857 | pre=None,iFun=None,theRest=None): |
|
1877 | pre=None,iFun=None,theRest=None): | |
1858 | """Handle normal input lines. Use as a template for handlers.""" |
|
1878 | """Handle normal input lines. Use as a template for handlers.""" | |
1859 |
|
1879 | |||
1860 | self.log(line,continue_prompt) |
|
1880 | self.log(line,continue_prompt) | |
1861 | self.update_cache(line) |
|
1881 | self.update_cache(line) | |
1862 | return line |
|
1882 | return line | |
1863 |
|
1883 | |||
1864 | def handle_alias(self,line,continue_prompt=None, |
|
1884 | def handle_alias(self,line,continue_prompt=None, | |
1865 | pre=None,iFun=None,theRest=None): |
|
1885 | pre=None,iFun=None,theRest=None): | |
1866 | """Handle alias input lines. """ |
|
1886 | """Handle alias input lines. """ | |
1867 |
|
1887 | |||
1868 | theRest = esc_quotes(theRest) |
|
1888 | theRest = esc_quotes(theRest) | |
1869 | line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest) |
|
1889 | line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest) | |
1870 | self.log(line_out,continue_prompt) |
|
1890 | self.log(line_out,continue_prompt) | |
1871 | self.update_cache(line_out) |
|
1891 | self.update_cache(line_out) | |
1872 | return line_out |
|
1892 | return line_out | |
1873 |
|
1893 | |||
1874 | def handle_shell_escape(self, line, continue_prompt=None, |
|
1894 | def handle_shell_escape(self, line, continue_prompt=None, | |
1875 | pre=None,iFun=None,theRest=None): |
|
1895 | pre=None,iFun=None,theRest=None): | |
1876 | """Execute the line in a shell, empty return value""" |
|
1896 | """Execute the line in a shell, empty return value""" | |
1877 |
|
1897 | |||
1878 | #print 'line in :', `line` # dbg |
|
1898 | #print 'line in :', `line` # dbg | |
1879 | # Example of a special handler. Others follow a similar pattern. |
|
1899 | # Example of a special handler. Others follow a similar pattern. | |
1880 | if continue_prompt: # multi-line statements |
|
1900 | if continue_prompt: # multi-line statements | |
1881 | if iFun.startswith('!!'): |
|
1901 | if iFun.startswith('!!'): | |
1882 | print 'SyntaxError: !! is not allowed in multiline statements' |
|
1902 | print 'SyntaxError: !! is not allowed in multiline statements' | |
1883 | return pre |
|
1903 | return pre | |
1884 | else: |
|
1904 | else: | |
1885 | cmd = ("%s %s" % (iFun[1:],theRest)).replace('"','\\"') |
|
1905 | cmd = ("%s %s" % (iFun[1:],theRest)).replace('"','\\"') | |
1886 | line_out = '%s%s.system("%s")' % (pre,self.name,cmd) |
|
1906 | line_out = '%s%s.system("%s")' % (pre,self.name,cmd) | |
1887 | #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')' |
|
1907 | #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')' | |
1888 | else: # single-line input |
|
1908 | else: # single-line input | |
1889 | if line.startswith('!!'): |
|
1909 | if line.startswith('!!'): | |
1890 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
1910 | # rewrite iFun/theRest to properly hold the call to %sx and | |
1891 | # the actual command to be executed, so handle_magic can work |
|
1911 | # the actual command to be executed, so handle_magic can work | |
1892 | # correctly |
|
1912 | # correctly | |
1893 | theRest = '%s %s' % (iFun[2:],theRest) |
|
1913 | theRest = '%s %s' % (iFun[2:],theRest) | |
1894 | iFun = 'sx' |
|
1914 | iFun = 'sx' | |
1895 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]), |
|
1915 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]), | |
1896 | continue_prompt,pre,iFun,theRest) |
|
1916 | continue_prompt,pre,iFun,theRest) | |
1897 | else: |
|
1917 | else: | |
1898 | cmd = esc_quotes(line[1:]) |
|
1918 | cmd = esc_quotes(line[1:]) | |
1899 | line_out = '%s.system("%s")' % (self.name,cmd) |
|
1919 | line_out = '%s.system("%s")' % (self.name,cmd) | |
1900 | #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')' |
|
1920 | #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')' | |
1901 | # update cache/log and return |
|
1921 | # update cache/log and return | |
1902 | self.log(line_out,continue_prompt) |
|
1922 | self.log(line_out,continue_prompt) | |
1903 | self.update_cache(line_out) # readline cache gets normal line |
|
1923 | self.update_cache(line_out) # readline cache gets normal line | |
1904 | #print 'line out r:', `line_out` # dbg |
|
1924 | #print 'line out r:', `line_out` # dbg | |
1905 | #print 'line out s:', line_out # dbg |
|
1925 | #print 'line out s:', line_out # dbg | |
1906 | return line_out |
|
1926 | return line_out | |
1907 |
|
1927 | |||
1908 | def handle_magic(self, line, continue_prompt=None, |
|
1928 | def handle_magic(self, line, continue_prompt=None, | |
1909 | pre=None,iFun=None,theRest=None): |
|
1929 | pre=None,iFun=None,theRest=None): | |
1910 | """Execute magic functions. |
|
1930 | """Execute magic functions. | |
1911 |
|
1931 | |||
1912 | Also log them with a prepended # so the log is clean Python.""" |
|
1932 | Also log them with a prepended # so the log is clean Python.""" | |
1913 |
|
1933 | |||
1914 | cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest))) |
|
1934 | cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest))) | |
1915 | self.log(cmd,continue_prompt) |
|
1935 | self.log(cmd,continue_prompt) | |
1916 | self.update_cache(line) |
|
1936 | self.update_cache(line) | |
1917 |
|
|
1937 | print 'in handle_magic, cmd=<%s>' % cmd # dbg | |
1918 | return cmd |
|
1938 | return cmd | |
1919 |
|
1939 | |||
1920 | def handle_auto(self, line, continue_prompt=None, |
|
1940 | def handle_auto(self, line, continue_prompt=None, | |
1921 | pre=None,iFun=None,theRest=None): |
|
1941 | pre=None,iFun=None,theRest=None): | |
1922 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
1942 | """Hande lines which can be auto-executed, quoting if requested.""" | |
1923 |
|
1943 | |||
1924 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1944 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
1925 |
|
1945 | |||
1926 | # This should only be active for single-line input! |
|
1946 | # This should only be active for single-line input! | |
1927 | if continue_prompt: |
|
1947 | if continue_prompt: | |
1928 | return line |
|
1948 | return line | |
1929 |
|
1949 | |||
1930 | if pre == self.ESC_QUOTE: |
|
1950 | if pre == self.ESC_QUOTE: | |
1931 | # Auto-quote splitting on whitespace |
|
1951 | # Auto-quote splitting on whitespace | |
1932 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
1952 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) | |
1933 | elif pre == self.ESC_QUOTE2: |
|
1953 | elif pre == self.ESC_QUOTE2: | |
1934 | # Auto-quote whole string |
|
1954 | # Auto-quote whole string | |
1935 | newcmd = '%s("%s")' % (iFun,theRest) |
|
1955 | newcmd = '%s("%s")' % (iFun,theRest) | |
1936 | else: |
|
1956 | else: | |
1937 | # Auto-paren |
|
1957 | # Auto-paren | |
1938 | if theRest[0:1] in ('=','['): |
|
1958 | if theRest[0:1] in ('=','['): | |
1939 | # Don't autocall in these cases. They can be either |
|
1959 | # Don't autocall in these cases. They can be either | |
1940 | # rebindings of an existing callable's name, or item access |
|
1960 | # rebindings of an existing callable's name, or item access | |
1941 | # for an object which is BOTH callable and implements |
|
1961 | # for an object which is BOTH callable and implements | |
1942 | # __getitem__. |
|
1962 | # __getitem__. | |
1943 | return '%s %s' % (iFun,theRest) |
|
1963 | return '%s %s' % (iFun,theRest) | |
1944 | if theRest.endswith(';'): |
|
1964 | if theRest.endswith(';'): | |
1945 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
1965 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) | |
1946 | else: |
|
1966 | else: | |
1947 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
1967 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) | |
1948 |
|
1968 | |||
1949 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd |
|
1969 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd | |
1950 | # log what is now valid Python, not the actual user input (without the |
|
1970 | # log what is now valid Python, not the actual user input (without the | |
1951 | # final newline) |
|
1971 | # final newline) | |
1952 | self.log(newcmd,continue_prompt) |
|
1972 | self.log(newcmd,continue_prompt) | |
1953 | return newcmd |
|
1973 | return newcmd | |
1954 |
|
1974 | |||
1955 | def handle_help(self, line, continue_prompt=None, |
|
1975 | def handle_help(self, line, continue_prompt=None, | |
1956 | pre=None,iFun=None,theRest=None): |
|
1976 | pre=None,iFun=None,theRest=None): | |
1957 | """Try to get some help for the object. |
|
1977 | """Try to get some help for the object. | |
1958 |
|
1978 | |||
1959 | obj? or ?obj -> basic information. |
|
1979 | obj? or ?obj -> basic information. | |
1960 | obj?? or ??obj -> more details. |
|
1980 | obj?? or ??obj -> more details. | |
1961 | """ |
|
1981 | """ | |
1962 |
|
1982 | |||
1963 | # We need to make sure that we don't process lines which would be |
|
1983 | # We need to make sure that we don't process lines which would be | |
1964 | # otherwise valid python, such as "x=1 # what?" |
|
1984 | # otherwise valid python, such as "x=1 # what?" | |
1965 | try: |
|
1985 | try: | |
1966 | code.compile_command(line) |
|
1986 | code.compile_command(line) | |
1967 | except SyntaxError: |
|
1987 | except SyntaxError: | |
1968 | # We should only handle as help stuff which is NOT valid syntax |
|
1988 | # We should only handle as help stuff which is NOT valid syntax | |
1969 | if line[0]==self.ESC_HELP: |
|
1989 | if line[0]==self.ESC_HELP: | |
1970 | line = line[1:] |
|
1990 | line = line[1:] | |
1971 | elif line[-1]==self.ESC_HELP: |
|
1991 | elif line[-1]==self.ESC_HELP: | |
1972 | line = line[:-1] |
|
1992 | line = line[:-1] | |
1973 | self.log('#?'+line) |
|
1993 | self.log('#?'+line) | |
1974 | self.update_cache(line) |
|
1994 | self.update_cache(line) | |
1975 | if line: |
|
1995 | if line: | |
1976 | self.magic_pinfo(line) |
|
1996 | self.magic_pinfo(line) | |
1977 | else: |
|
1997 | else: | |
1978 | page(self.usage,screen_lines=self.rc.screen_length) |
|
1998 | page(self.usage,screen_lines=self.rc.screen_length) | |
1979 | return '' # Empty string is needed here! |
|
1999 | return '' # Empty string is needed here! | |
1980 | except: |
|
2000 | except: | |
1981 | # Pass any other exceptions through to the normal handler |
|
2001 | # Pass any other exceptions through to the normal handler | |
1982 | return self.handle_normal(line,continue_prompt) |
|
2002 | return self.handle_normal(line,continue_prompt) | |
1983 | else: |
|
2003 | else: | |
1984 | # If the code compiles ok, we should handle it normally |
|
2004 | # If the code compiles ok, we should handle it normally | |
1985 | return self.handle_normal(line,continue_prompt) |
|
2005 | return self.handle_normal(line,continue_prompt) | |
1986 |
|
2006 | |||
1987 | def handle_emacs(self,line,continue_prompt=None, |
|
2007 | def handle_emacs(self,line,continue_prompt=None, | |
1988 | pre=None,iFun=None,theRest=None): |
|
2008 | pre=None,iFun=None,theRest=None): | |
1989 | """Handle input lines marked by python-mode.""" |
|
2009 | """Handle input lines marked by python-mode.""" | |
1990 |
|
2010 | |||
1991 | # Currently, nothing is done. Later more functionality can be added |
|
2011 | # Currently, nothing is done. Later more functionality can be added | |
1992 | # here if needed. |
|
2012 | # here if needed. | |
1993 |
|
2013 | |||
1994 | # The input cache shouldn't be updated |
|
2014 | # The input cache shouldn't be updated | |
1995 |
|
2015 | |||
1996 | return line |
|
2016 | return line | |
1997 |
|
2017 | |||
1998 | def write(self,data): |
|
2018 | def write(self,data): | |
1999 | """Write a string to the default output""" |
|
2019 | """Write a string to the default output""" | |
2000 | Term.cout.write(data) |
|
2020 | Term.cout.write(data) | |
2001 |
|
2021 | |||
2002 | def write_err(self,data): |
|
2022 | def write_err(self,data): | |
2003 | """Write a string to the default error output""" |
|
2023 | """Write a string to the default error output""" | |
2004 | Term.cerr.write(data) |
|
2024 | Term.cerr.write(data) | |
2005 |
|
2025 | |||
2006 | def safe_execfile(self,fname,*where,**kw): |
|
2026 | def safe_execfile(self,fname,*where,**kw): | |
2007 | fname = os.path.expanduser(fname) |
|
2027 | fname = os.path.expanduser(fname) | |
2008 |
|
2028 | |||
2009 | # find things also in current directory |
|
2029 | # find things also in current directory | |
2010 | dname = os.path.dirname(fname) |
|
2030 | dname = os.path.dirname(fname) | |
2011 | if not sys.path.count(dname): |
|
2031 | if not sys.path.count(dname): | |
2012 | sys.path.append(dname) |
|
2032 | sys.path.append(dname) | |
2013 |
|
2033 | |||
2014 | try: |
|
2034 | try: | |
2015 | xfile = open(fname) |
|
2035 | xfile = open(fname) | |
2016 | except: |
|
2036 | except: | |
2017 | print >> Term.cerr, \ |
|
2037 | print >> Term.cerr, \ | |
2018 | 'Could not open file <%s> for safe execution.' % fname |
|
2038 | 'Could not open file <%s> for safe execution.' % fname | |
2019 | return None |
|
2039 | return None | |
2020 |
|
2040 | |||
2021 | kw.setdefault('islog',0) |
|
2041 | kw.setdefault('islog',0) | |
2022 | kw.setdefault('quiet',1) |
|
2042 | kw.setdefault('quiet',1) | |
2023 | kw.setdefault('exit_ignore',0) |
|
2043 | kw.setdefault('exit_ignore',0) | |
2024 | first = xfile.readline() |
|
2044 | first = xfile.readline() | |
2025 | _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip() |
|
2045 | _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip() | |
2026 | xfile.close() |
|
2046 | xfile.close() | |
2027 | # line by line execution |
|
2047 | # line by line execution | |
2028 | if first.startswith(_LOGHEAD) or kw['islog']: |
|
2048 | if first.startswith(_LOGHEAD) or kw['islog']: | |
2029 | print 'Loading log file <%s> one line at a time...' % fname |
|
2049 | print 'Loading log file <%s> one line at a time...' % fname | |
2030 | if kw['quiet']: |
|
2050 | if kw['quiet']: | |
2031 | stdout_save = sys.stdout |
|
2051 | stdout_save = sys.stdout | |
2032 | sys.stdout = StringIO.StringIO() |
|
2052 | sys.stdout = StringIO.StringIO() | |
2033 | try: |
|
2053 | try: | |
2034 | globs,locs = where[0:2] |
|
2054 | globs,locs = where[0:2] | |
2035 | except: |
|
2055 | except: | |
2036 | try: |
|
2056 | try: | |
2037 | globs = locs = where[0] |
|
2057 | globs = locs = where[0] | |
2038 | except: |
|
2058 | except: | |
2039 | globs = locs = globals() |
|
2059 | globs = locs = globals() | |
2040 | badblocks = [] |
|
2060 | badblocks = [] | |
2041 |
|
2061 | |||
2042 | # we also need to identify indented blocks of code when replaying |
|
2062 | # we also need to identify indented blocks of code when replaying | |
2043 | # logs and put them together before passing them to an exec |
|
2063 | # logs and put them together before passing them to an exec | |
2044 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2064 | # statement. This takes a bit of regexp and look-ahead work in the | |
2045 | # file. It's easiest if we swallow the whole thing in memory |
|
2065 | # file. It's easiest if we swallow the whole thing in memory | |
2046 | # first, and manually walk through the lines list moving the |
|
2066 | # first, and manually walk through the lines list moving the | |
2047 | # counter ourselves. |
|
2067 | # counter ourselves. | |
2048 | indent_re = re.compile('\s+\S') |
|
2068 | indent_re = re.compile('\s+\S') | |
2049 | xfile = open(fname) |
|
2069 | xfile = open(fname) | |
2050 | filelines = xfile.readlines() |
|
2070 | filelines = xfile.readlines() | |
2051 | xfile.close() |
|
2071 | xfile.close() | |
2052 | nlines = len(filelines) |
|
2072 | nlines = len(filelines) | |
2053 | lnum = 0 |
|
2073 | lnum = 0 | |
2054 | while lnum < nlines: |
|
2074 | while lnum < nlines: | |
2055 | line = filelines[lnum] |
|
2075 | line = filelines[lnum] | |
2056 | lnum += 1 |
|
2076 | lnum += 1 | |
2057 | # don't re-insert logger status info into cache |
|
2077 | # don't re-insert logger status info into cache | |
2058 | if line.startswith('#log#'): |
|
2078 | if line.startswith('#log#'): | |
2059 | continue |
|
2079 | continue | |
2060 | elif line.startswith('#%s'% self.ESC_MAGIC): |
|
2080 | elif line.startswith('#%s'% self.ESC_MAGIC): | |
2061 | self.update_cache(line[1:]) |
|
2081 | self.update_cache(line[1:]) | |
2062 | line = magic2python(line) |
|
2082 | line = magic2python(line) | |
2063 | elif line.startswith('#!'): |
|
2083 | elif line.startswith('#!'): | |
2064 | self.update_cache(line[1:]) |
|
2084 | self.update_cache(line[1:]) | |
2065 | else: |
|
2085 | else: | |
2066 | # build a block of code (maybe a single line) for execution |
|
2086 | # build a block of code (maybe a single line) for execution | |
2067 | block = line |
|
2087 | block = line | |
2068 | try: |
|
2088 | try: | |
2069 | next = filelines[lnum] # lnum has already incremented |
|
2089 | next = filelines[lnum] # lnum has already incremented | |
2070 | except: |
|
2090 | except: | |
2071 | next = None |
|
2091 | next = None | |
2072 | while next and indent_re.match(next): |
|
2092 | while next and indent_re.match(next): | |
2073 | block += next |
|
2093 | block += next | |
2074 | lnum += 1 |
|
2094 | lnum += 1 | |
2075 | try: |
|
2095 | try: | |
2076 | next = filelines[lnum] |
|
2096 | next = filelines[lnum] | |
2077 | except: |
|
2097 | except: | |
2078 | next = None |
|
2098 | next = None | |
2079 | # now execute the block of one or more lines |
|
2099 | # now execute the block of one or more lines | |
2080 | try: |
|
2100 | try: | |
2081 | exec block in globs,locs |
|
2101 | exec block in globs,locs | |
2082 | self.update_cache(block.rstrip()) |
|
2102 | self.update_cache(block.rstrip()) | |
2083 | except SystemExit: |
|
2103 | except SystemExit: | |
2084 | pass |
|
2104 | pass | |
2085 | except: |
|
2105 | except: | |
2086 | badblocks.append(block.rstrip()) |
|
2106 | badblocks.append(block.rstrip()) | |
2087 | if kw['quiet']: # restore stdout |
|
2107 | if kw['quiet']: # restore stdout | |
2088 | sys.stdout.close() |
|
2108 | sys.stdout.close() | |
2089 | sys.stdout = stdout_save |
|
2109 | sys.stdout = stdout_save | |
2090 | print 'Finished replaying log file <%s>' % fname |
|
2110 | print 'Finished replaying log file <%s>' % fname | |
2091 | if badblocks: |
|
2111 | if badblocks: | |
2092 | print >> sys.stderr, \ |
|
2112 | print >> sys.stderr, \ | |
2093 | '\nThe following lines/blocks in file <%s> reported errors:' \ |
|
2113 | '\nThe following lines/blocks in file <%s> reported errors:' \ | |
2094 | % fname |
|
2114 | % fname | |
2095 | for badline in badblocks: |
|
2115 | for badline in badblocks: | |
2096 | print >> sys.stderr, badline |
|
2116 | print >> sys.stderr, badline | |
2097 | else: # regular file execution |
|
2117 | else: # regular file execution | |
2098 | try: |
|
2118 | try: | |
2099 | execfile(fname,*where) |
|
2119 | execfile(fname,*where) | |
2100 | except SyntaxError: |
|
2120 | except SyntaxError: | |
2101 | etype, evalue = sys.exc_info()[0:2] |
|
2121 | etype, evalue = sys.exc_info()[0:2] | |
2102 | self.SyntaxTB(etype,evalue,[]) |
|
2122 | self.SyntaxTB(etype,evalue,[]) | |
2103 | warn('Failure executing file: <%s>' % fname) |
|
2123 | warn('Failure executing file: <%s>' % fname) | |
2104 | except SystemExit,status: |
|
2124 | except SystemExit,status: | |
2105 | if not kw['exit_ignore']: |
|
2125 | if not kw['exit_ignore']: | |
2106 | self.InteractiveTB() |
|
2126 | self.InteractiveTB() | |
2107 | warn('Failure executing file: <%s>' % fname) |
|
2127 | warn('Failure executing file: <%s>' % fname) | |
2108 | except: |
|
2128 | except: | |
2109 | self.InteractiveTB() |
|
2129 | self.InteractiveTB() | |
2110 | warn('Failure executing file: <%s>' % fname) |
|
2130 | warn('Failure executing file: <%s>' % fname) | |
2111 |
|
2131 | |||
2112 | #************************* end of file <iplib.py> ***************************** |
|
2132 | #************************* end of file <iplib.py> ***************************** |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now