Show More
@@ -1,658 +1,722 b'' | |||||
1 | """Word completion for IPython. |
|
1 | """Word completion for IPython. | |
2 |
|
2 | |||
3 | This module is a fork of the rlcompleter module in the Python standard |
|
3 | This module is a fork of the rlcompleter module in the Python standard | |
4 | library. The original enhancements made to rlcompleter have been sent |
|
4 | library. The original enhancements made to rlcompleter have been sent | |
5 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
5 | upstream and were accepted as of Python 2.3, but we need a lot more | |
6 | functionality specific to IPython, so this module will continue to live as an |
|
6 | functionality specific to IPython, so this module will continue to live as an | |
7 | IPython-specific utility. |
|
7 | IPython-specific utility. | |
8 |
|
8 | |||
9 | Original rlcompleter documentation: |
|
9 | Original rlcompleter documentation: | |
10 |
|
10 | |||
11 | This requires the latest extension to the readline module (the |
|
11 | This requires the latest extension to the readline module (the | |
12 | completes keywords, built-ins and globals in __main__; when completing |
|
12 | completes keywords, built-ins and globals in __main__; when completing | |
13 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
13 | NAME.NAME..., it evaluates (!) the expression up to the last dot and | |
14 | completes its attributes. |
|
14 | completes its attributes. | |
15 |
|
15 | |||
16 | It's very cool to do "import string" type "string.", hit the |
|
16 | It's very cool to do "import string" type "string.", hit the | |
17 | completion key (twice), and see the list of names defined by the |
|
17 | completion key (twice), and see the list of names defined by the | |
18 | string module! |
|
18 | string module! | |
19 |
|
19 | |||
20 | Tip: to use the tab key as the completion key, call |
|
20 | Tip: to use the tab key as the completion key, call | |
21 |
|
21 | |||
22 | readline.parse_and_bind("tab: complete") |
|
22 | readline.parse_and_bind("tab: complete") | |
23 |
|
23 | |||
24 | Notes: |
|
24 | Notes: | |
25 |
|
25 | |||
26 | - Exceptions raised by the completer function are *ignored* (and |
|
26 | - Exceptions raised by the completer function are *ignored* (and | |
27 | generally cause the completion to fail). This is a feature -- since |
|
27 | generally cause the completion to fail). This is a feature -- since | |
28 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
28 | readline sets the tty device in raw (or cbreak) mode, printing a | |
29 | traceback wouldn't work well without some complicated hoopla to save, |
|
29 | traceback wouldn't work well without some complicated hoopla to save, | |
30 | reset and restore the tty state. |
|
30 | reset and restore the tty state. | |
31 |
|
31 | |||
32 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
32 | - The evaluation of the NAME.NAME... form may cause arbitrary | |
33 | application defined code to be executed if an object with a |
|
33 | application defined code to be executed if an object with a | |
34 | __getattr__ hook is found. Since it is the responsibility of the |
|
34 | __getattr__ hook is found. Since it is the responsibility of the | |
35 | application (or the user) to enable this feature, I consider this an |
|
35 | application (or the user) to enable this feature, I consider this an | |
36 | acceptable risk. More complicated expressions (e.g. function calls or |
|
36 | acceptable risk. More complicated expressions (e.g. function calls or | |
37 | indexing operations) are *not* evaluated. |
|
37 | indexing operations) are *not* evaluated. | |
38 |
|
38 | |||
39 | - GNU readline is also used by the built-in functions input() and |
|
39 | - GNU readline is also used by the built-in functions input() and | |
40 | raw_input(), and thus these also benefit/suffer from the completer |
|
40 | raw_input(), and thus these also benefit/suffer from the completer | |
41 | features. Clearly an interactive application can benefit by |
|
41 | features. Clearly an interactive application can benefit by | |
42 | specifying its own completer function and using raw_input() for all |
|
42 | specifying its own completer function and using raw_input() for all | |
43 | its input. |
|
43 | its input. | |
44 |
|
44 | |||
45 | - When the original stdin is not a tty device, GNU readline is never |
|
45 | - When the original stdin is not a tty device, GNU readline is never | |
46 | used, and this module (and the readline module) are silently inactive. |
|
46 | used, and this module (and the readline module) are silently inactive. | |
47 | """ |
|
47 | """ | |
48 |
|
48 | |||
49 | #***************************************************************************** |
|
49 | #***************************************************************************** | |
50 | # |
|
50 | # | |
51 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
51 | # Since this file is essentially a minimally modified copy of the rlcompleter | |
52 | # module which is part of the standard Python distribution, I assume that the |
|
52 | # module which is part of the standard Python distribution, I assume that the | |
53 | # proper procedure is to maintain its copyright as belonging to the Python |
|
53 | # proper procedure is to maintain its copyright as belonging to the Python | |
54 | # Software Foundation (in addition to my own, for all new code). |
|
54 | # Software Foundation (in addition to my own, for all new code). | |
55 | # |
|
55 | # | |
56 | # Copyright (C) 2008-2010 IPython Development Team |
|
56 | # Copyright (C) 2008-2010 IPython Development Team | |
57 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
57 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
58 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
58 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
59 | # |
|
59 | # | |
60 | # Distributed under the terms of the BSD License. The full license is in |
|
60 | # Distributed under the terms of the BSD License. The full license is in | |
61 | # the file COPYING, distributed as part of this software. |
|
61 | # the file COPYING, distributed as part of this software. | |
62 | # |
|
62 | # | |
63 | #***************************************************************************** |
|
63 | #***************************************************************************** | |
64 |
|
64 | |||
65 | #----------------------------------------------------------------------------- |
|
65 | #----------------------------------------------------------------------------- | |
66 | # Imports |
|
66 | # Imports | |
67 | #----------------------------------------------------------------------------- |
|
67 | #----------------------------------------------------------------------------- | |
68 |
|
68 | |||
69 | import __builtin__ |
|
69 | import __builtin__ | |
70 | import __main__ |
|
70 | import __main__ | |
71 | import glob |
|
71 | import glob | |
72 | import inspect |
|
72 | import inspect | |
73 | import itertools |
|
73 | import itertools | |
74 | import keyword |
|
74 | import keyword | |
75 | import os |
|
75 | import os | |
76 | import re |
|
76 | import re | |
77 | import shlex |
|
77 | import shlex | |
78 | import sys |
|
78 | import sys | |
79 |
|
79 | |||
80 | from IPython.core.error import TryNext |
|
80 | from IPython.core.error import TryNext | |
81 | from IPython.core.prefilter import ESC_MAGIC |
|
81 | from IPython.core.prefilter import ESC_MAGIC | |
82 | from IPython.utils import generics |
|
82 | from IPython.utils import generics | |
83 | from IPython.utils.frame import debugx |
|
83 | from IPython.utils.frame import debugx | |
84 | from IPython.utils.dir2 import dir2 |
|
84 | from IPython.utils.dir2 import dir2 | |
85 | import IPython.utils.rlineimpl as readline |
|
|||
86 |
|
85 | |||
87 | #----------------------------------------------------------------------------- |
|
86 | #----------------------------------------------------------------------------- | |
88 | # Globals |
|
87 | # Globals | |
89 | #----------------------------------------------------------------------------- |
|
88 | #----------------------------------------------------------------------------- | |
90 |
|
89 | |||
91 | # Public API |
|
90 | # Public API | |
92 | __all__ = ['Completer','IPCompleter'] |
|
91 | __all__ = ['Completer','IPCompleter'] | |
93 |
|
92 | |||
94 | if sys.platform == 'win32': |
|
93 | if sys.platform == 'win32': | |
95 | PROTECTABLES = ' ' |
|
94 | PROTECTABLES = ' ' | |
96 | else: |
|
95 | else: | |
97 | PROTECTABLES = ' ()' |
|
96 | PROTECTABLES = ' ()' | |
98 |
|
97 | |||
99 | #----------------------------------------------------------------------------- |
|
98 | #----------------------------------------------------------------------------- | |
100 | # Main functions and classes |
|
99 | # Main functions and classes | |
101 | #----------------------------------------------------------------------------- |
|
100 | #----------------------------------------------------------------------------- | |
102 |
|
101 | |||
103 | def protect_filename(s): |
|
102 | def protect_filename(s): | |
104 | """Escape a string to protect certain characters.""" |
|
103 | """Escape a string to protect certain characters.""" | |
105 |
|
104 | |||
106 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) |
|
105 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) | |
107 | for ch in s]) |
|
106 | for ch in s]) | |
108 |
|
107 | |||
109 |
|
108 | |||
|
109 | def mark_dirs(matches): | |||
|
110 | """Mark directories in input list by appending '/' to their names.""" | |||
|
111 | out = [] | |||
|
112 | isdir = os.path.isdir | |||
|
113 | for x in matches: | |||
|
114 | if isdir(x): | |||
|
115 | out.append(x+'/') | |||
|
116 | else: | |||
|
117 | out.append(x) | |||
|
118 | return out | |||
|
119 | ||||
|
120 | ||||
110 | def single_dir_expand(matches): |
|
121 | def single_dir_expand(matches): | |
111 | "Recursively expand match lists containing a single dir." |
|
122 | "Recursively expand match lists containing a single dir." | |
112 |
|
123 | |||
113 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
124 | if len(matches) == 1 and os.path.isdir(matches[0]): | |
114 | # Takes care of links to directories also. Use '/' |
|
125 | # Takes care of links to directories also. Use '/' | |
115 | # explicitly, even under Windows, so that name completions |
|
126 | # explicitly, even under Windows, so that name completions | |
116 | # don't end up escaped. |
|
127 | # don't end up escaped. | |
117 | d = matches[0] |
|
128 | d = matches[0] | |
118 | if d[-1] in ['/','\\']: |
|
129 | if d[-1] in ['/','\\']: | |
119 | d = d[:-1] |
|
130 | d = d[:-1] | |
120 |
|
131 | |||
121 | subdirs = os.listdir(d) |
|
132 | subdirs = os.listdir(d) | |
122 | if subdirs: |
|
133 | if subdirs: | |
123 | matches = [ (d + '/' + p) for p in subdirs] |
|
134 | matches = [ (d + '/' + p) for p in subdirs] | |
124 | return single_dir_expand(matches) |
|
135 | return single_dir_expand(matches) | |
125 | else: |
|
136 | else: | |
126 | return matches |
|
137 | return matches | |
127 | else: |
|
138 | else: | |
128 | return matches |
|
139 | return matches | |
129 |
|
140 | |||
130 | class Bunch: pass |
|
141 | class Bunch: pass | |
131 |
|
142 | |||
132 | class Completer: |
|
143 | class Completer: | |
133 | def __init__(self,namespace=None,global_namespace=None): |
|
144 | def __init__(self,namespace=None,global_namespace=None): | |
134 | """Create a new completer for the command line. |
|
145 | """Create a new completer for the command line. | |
135 |
|
146 | |||
136 | Completer([namespace,global_namespace]) -> completer instance. |
|
147 | Completer([namespace,global_namespace]) -> completer instance. | |
137 |
|
148 | |||
138 | If unspecified, the default namespace where completions are performed |
|
149 | If unspecified, the default namespace where completions are performed | |
139 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
150 | is __main__ (technically, __main__.__dict__). Namespaces should be | |
140 | given as dictionaries. |
|
151 | given as dictionaries. | |
141 |
|
152 | |||
142 | An optional second namespace can be given. This allows the completer |
|
153 | An optional second namespace can be given. This allows the completer | |
143 | to handle cases where both the local and global scopes need to be |
|
154 | to handle cases where both the local and global scopes need to be | |
144 | distinguished. |
|
155 | distinguished. | |
145 |
|
156 | |||
146 | Completer instances should be used as the completion mechanism of |
|
157 | Completer instances should be used as the completion mechanism of | |
147 | readline via the set_completer() call: |
|
158 | readline via the set_completer() call: | |
148 |
|
159 | |||
149 | readline.set_completer(Completer(my_namespace).complete) |
|
160 | readline.set_completer(Completer(my_namespace).complete) | |
150 | """ |
|
161 | """ | |
151 |
|
162 | |||
152 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
163 | # Don't bind to namespace quite yet, but flag whether the user wants a | |
153 | # specific namespace or to use __main__.__dict__. This will allow us |
|
164 | # specific namespace or to use __main__.__dict__. This will allow us | |
154 | # to bind to __main__.__dict__ at completion time, not now. |
|
165 | # to bind to __main__.__dict__ at completion time, not now. | |
155 | if namespace is None: |
|
166 | if namespace is None: | |
156 | self.use_main_ns = 1 |
|
167 | self.use_main_ns = 1 | |
157 | else: |
|
168 | else: | |
158 | self.use_main_ns = 0 |
|
169 | self.use_main_ns = 0 | |
159 | self.namespace = namespace |
|
170 | self.namespace = namespace | |
160 |
|
171 | |||
161 | # The global namespace, if given, can be bound directly |
|
172 | # The global namespace, if given, can be bound directly | |
162 | if global_namespace is None: |
|
173 | if global_namespace is None: | |
163 | self.global_namespace = {} |
|
174 | self.global_namespace = {} | |
164 | else: |
|
175 | else: | |
165 | self.global_namespace = global_namespace |
|
176 | self.global_namespace = global_namespace | |
166 |
|
177 | |||
167 | def complete(self, text, state): |
|
178 | def complete(self, text, state): | |
168 | """Return the next possible completion for 'text'. |
|
179 | """Return the next possible completion for 'text'. | |
169 |
|
180 | |||
170 | This is called successively with state == 0, 1, 2, ... until it |
|
181 | This is called successively with state == 0, 1, 2, ... until it | |
171 | returns None. The completion should begin with 'text'. |
|
182 | returns None. The completion should begin with 'text'. | |
172 |
|
183 | |||
173 | """ |
|
184 | """ | |
174 | if self.use_main_ns: |
|
185 | if self.use_main_ns: | |
175 | self.namespace = __main__.__dict__ |
|
186 | self.namespace = __main__.__dict__ | |
176 |
|
187 | |||
177 | if state == 0: |
|
188 | if state == 0: | |
178 | if "." in text: |
|
189 | if "." in text: | |
179 | self.matches = self.attr_matches(text) |
|
190 | self.matches = self.attr_matches(text) | |
180 | else: |
|
191 | else: | |
181 | self.matches = self.global_matches(text) |
|
192 | self.matches = self.global_matches(text) | |
182 | try: |
|
193 | try: | |
183 | return self.matches[state] |
|
194 | return self.matches[state] | |
184 | except IndexError: |
|
195 | except IndexError: | |
185 | return None |
|
196 | return None | |
186 |
|
197 | |||
187 | def global_matches(self, text): |
|
198 | def global_matches(self, text): | |
188 | """Compute matches when text is a simple name. |
|
199 | """Compute matches when text is a simple name. | |
189 |
|
200 | |||
190 | Return a list of all keywords, built-in functions and names currently |
|
201 | Return a list of all keywords, built-in functions and names currently | |
191 | defined in self.namespace or self.global_namespace that match. |
|
202 | defined in self.namespace or self.global_namespace that match. | |
192 |
|
203 | |||
193 | """ |
|
204 | """ | |
194 | #print 'Completer->global_matches, txt=%r' % text # dbg |
|
205 | #print 'Completer->global_matches, txt=%r' % text # dbg | |
195 | matches = [] |
|
206 | matches = [] | |
196 | match_append = matches.append |
|
207 | match_append = matches.append | |
197 | n = len(text) |
|
208 | n = len(text) | |
198 | for lst in [keyword.kwlist, |
|
209 | for lst in [keyword.kwlist, | |
199 | __builtin__.__dict__.keys(), |
|
210 | __builtin__.__dict__.keys(), | |
200 | self.namespace.keys(), |
|
211 | self.namespace.keys(), | |
201 | self.global_namespace.keys()]: |
|
212 | self.global_namespace.keys()]: | |
202 | for word in lst: |
|
213 | for word in lst: | |
203 | if word[:n] == text and word != "__builtins__": |
|
214 | if word[:n] == text and word != "__builtins__": | |
204 | match_append(word) |
|
215 | match_append(word) | |
205 | return matches |
|
216 | return matches | |
206 |
|
217 | |||
207 | def attr_matches(self, text): |
|
218 | def attr_matches(self, text): | |
208 | """Compute matches when text contains a dot. |
|
219 | """Compute matches when text contains a dot. | |
209 |
|
220 | |||
210 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
221 | Assuming the text is of the form NAME.NAME....[NAME], and is | |
211 | evaluatable in self.namespace or self.global_namespace, it will be |
|
222 | evaluatable in self.namespace or self.global_namespace, it will be | |
212 | evaluated and its attributes (as revealed by dir()) are used as |
|
223 | evaluated and its attributes (as revealed by dir()) are used as | |
213 | possible completions. (For class instances, class members are are |
|
224 | possible completions. (For class instances, class members are are | |
214 | also considered.) |
|
225 | also considered.) | |
215 |
|
226 | |||
216 | WARNING: this can still invoke arbitrary C code, if an object |
|
227 | WARNING: this can still invoke arbitrary C code, if an object | |
217 | with a __getattr__ hook is evaluated. |
|
228 | with a __getattr__ hook is evaluated. | |
218 |
|
229 | |||
219 | """ |
|
230 | """ | |
220 |
|
231 | |||
221 | #print 'Completer->attr_matches, txt=%r' % text # dbg |
|
232 | #print 'Completer->attr_matches, txt=%r' % text # dbg | |
222 | # Another option, seems to work great. Catches things like ''.<tab> |
|
233 | # Another option, seems to work great. Catches things like ''.<tab> | |
223 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
234 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |
224 |
|
235 | |||
225 | if not m: |
|
236 | if not m: | |
226 | return [] |
|
237 | return [] | |
227 |
|
238 | |||
228 | expr, attr = m.group(1, 3) |
|
239 | expr, attr = m.group(1, 3) | |
229 | try: |
|
240 | try: | |
230 | obj = eval(expr, self.namespace) |
|
241 | obj = eval(expr, self.namespace) | |
231 | except: |
|
242 | except: | |
232 | try: |
|
243 | try: | |
233 | obj = eval(expr, self.global_namespace) |
|
244 | obj = eval(expr, self.global_namespace) | |
234 | except: |
|
245 | except: | |
235 | return [] |
|
246 | return [] | |
236 |
|
247 | |||
237 | words = dir2(obj) |
|
248 | words = dir2(obj) | |
238 |
|
249 | |||
239 | try: |
|
250 | try: | |
240 | words = generics.complete_object(obj, words) |
|
251 | words = generics.complete_object(obj, words) | |
241 | except TryNext: |
|
252 | except TryNext: | |
242 | pass |
|
253 | pass | |
243 | # Build match list to return |
|
254 | # Build match list to return | |
244 | n = len(attr) |
|
255 | n = len(attr) | |
245 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
256 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] | |
246 | return res |
|
257 | return res | |
247 |
|
258 | |||
248 |
|
259 | |||
249 | class IPCompleter(Completer): |
|
260 | class IPCompleter(Completer): | |
250 | """Extension of the completer class with IPython-specific features""" |
|
261 | """Extension of the completer class with IPython-specific features""" | |
251 |
|
262 | |||
252 | def __init__(self,shell,namespace=None,global_namespace=None, |
|
263 | def __init__(self, shell, namespace=None, global_namespace=None, | |
253 | omit__names=0,alias_table=None): |
|
264 | omit__names=0, alias_table=None, use_readline=True): | |
254 | """IPCompleter() -> completer |
|
265 | """IPCompleter() -> completer | |
255 |
|
266 | |||
256 | Return a completer object suitable for use by the readline library |
|
267 | Return a completer object suitable for use by the readline library | |
257 | via readline.set_completer(). |
|
268 | via readline.set_completer(). | |
258 |
|
269 | |||
259 | Inputs: |
|
270 | Inputs: | |
260 |
|
271 | |||
261 | - shell: a pointer to the ipython shell itself. This is needed |
|
272 | - shell: a pointer to the ipython shell itself. This is needed | |
262 | because this completer knows about magic functions, and those can |
|
273 | because this completer knows about magic functions, and those can | |
263 | only be accessed via the ipython instance. |
|
274 | only be accessed via the ipython instance. | |
264 |
|
275 | |||
265 | - namespace: an optional dict where completions are performed. |
|
276 | - namespace: an optional dict where completions are performed. | |
266 |
|
277 | |||
267 | - global_namespace: secondary optional dict for completions, to |
|
278 | - global_namespace: secondary optional dict for completions, to | |
268 | handle cases (such as IPython embedded inside functions) where |
|
279 | handle cases (such as IPython embedded inside functions) where | |
269 | both Python scopes are visible. |
|
280 | both Python scopes are visible. | |
270 |
|
281 | |||
271 | - The optional omit__names parameter sets the completer to omit the |
|
282 | - The optional omit__names parameter sets the completer to omit the | |
272 | 'magic' names (__magicname__) for python objects unless the text |
|
283 | 'magic' names (__magicname__) for python objects unless the text | |
273 | to be completed explicitly starts with one or more underscores. |
|
284 | to be completed explicitly starts with one or more underscores. | |
274 |
|
285 | |||
275 | - If alias_table is supplied, it should be a dictionary of aliases |
|
286 | - If alias_table is supplied, it should be a dictionary of aliases | |
276 |
to complete. |
|
287 | to complete. | |
|
288 | ||||
|
289 | use_readline : bool, optional | |||
|
290 | If true, use the readline library. This completer can still function | |||
|
291 | without readline, though in that case callers must provide some extra | |||
|
292 | information on each call about the current line.""" | |||
277 |
|
293 | |||
278 | Completer.__init__(self,namespace,global_namespace) |
|
294 | Completer.__init__(self,namespace,global_namespace) | |
279 |
|
295 | |||
280 | self.magic_escape = ESC_MAGIC |
|
296 | self.magic_escape = ESC_MAGIC | |
|
297 | ||||
|
298 | # Readline-dependent code | |||
|
299 | self.use_readline = use_readline | |||
|
300 | if use_readline: | |||
|
301 | import IPython.utils.rlineimpl as readline | |||
281 | self.readline = readline |
|
302 | self.readline = readline | |
282 | delims = self.readline.get_completer_delims() |
|
303 | delims = self.readline.get_completer_delims() | |
283 | delims = delims.replace(self.magic_escape,'') |
|
304 | delims = delims.replace(self.magic_escape,'') | |
284 | self.readline.set_completer_delims(delims) |
|
305 | self.readline.set_completer_delims(delims) | |
285 | self.get_line_buffer = self.readline.get_line_buffer |
|
306 | self.get_line_buffer = self.readline.get_line_buffer | |
286 | self.get_endidx = self.readline.get_endidx |
|
307 | self.get_endidx = self.readline.get_endidx | |
|
308 | # /end readline-dependent code | |||
|
309 | ||||
|
310 | # List where completion matches will be stored | |||
|
311 | self.matches = [] | |||
287 | self.omit__names = omit__names |
|
312 | self.omit__names = omit__names | |
288 | self.merge_completions = shell.readline_merge_completions |
|
313 | self.merge_completions = shell.readline_merge_completions | |
289 | self.shell = shell.shell |
|
314 | self.shell = shell.shell | |
290 | if alias_table is None: |
|
315 | if alias_table is None: | |
291 | alias_table = {} |
|
316 | alias_table = {} | |
292 | self.alias_table = alias_table |
|
317 | self.alias_table = alias_table | |
293 | # Regexp to split filenames with spaces in them |
|
318 | # Regexp to split filenames with spaces in them | |
294 | self.space_name_re = re.compile(r'([^\\] )') |
|
319 | self.space_name_re = re.compile(r'([^\\] )') | |
295 | # Hold a local ref. to glob.glob for speed |
|
320 | # Hold a local ref. to glob.glob for speed | |
296 | self.glob = glob.glob |
|
321 | self.glob = glob.glob | |
297 |
|
322 | |||
298 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
323 | # Determine if we are running on 'dumb' terminals, like (X)Emacs | |
299 | # buffers, to avoid completion problems. |
|
324 | # buffers, to avoid completion problems. | |
300 | term = os.environ.get('TERM','xterm') |
|
325 | term = os.environ.get('TERM','xterm') | |
301 | self.dumb_terminal = term in ['dumb','emacs'] |
|
326 | self.dumb_terminal = term in ['dumb','emacs'] | |
302 |
|
327 | |||
303 | # Special handling of backslashes needed in win32 platforms |
|
328 | # Special handling of backslashes needed in win32 platforms | |
304 | if sys.platform == "win32": |
|
329 | if sys.platform == "win32": | |
305 | self.clean_glob = self._clean_glob_win32 |
|
330 | self.clean_glob = self._clean_glob_win32 | |
306 | else: |
|
331 | else: | |
307 | self.clean_glob = self._clean_glob |
|
332 | self.clean_glob = self._clean_glob | |
308 |
|
333 | |||
309 | # All active matcher routines for completion |
|
334 | # All active matcher routines for completion | |
310 | self.matchers = [self.python_matches, |
|
335 | self.matchers = [self.python_matches, | |
311 | self.file_matches, |
|
336 | self.file_matches, | |
312 | self.magic_matches, |
|
337 | self.magic_matches, | |
313 | self.alias_matches, |
|
338 | self.alias_matches, | |
314 |
self.python_func_kw_matches |
|
339 | self.python_func_kw_matches, | |
|
340 | ] | |||
315 |
|
341 | |||
316 | # Code contributed by Alex Schmolck, for ipython/emacs integration |
|
342 | # Code contributed by Alex Schmolck, for ipython/emacs integration | |
317 | def all_completions(self, text): |
|
343 | def all_completions(self, text): | |
318 | """Return all possible completions for the benefit of emacs.""" |
|
344 | """Return all possible completions for the benefit of emacs.""" | |
319 |
|
345 | |||
320 | completions = [] |
|
346 | completions = [] | |
321 | comp_append = completions.append |
|
347 | comp_append = completions.append | |
322 | try: |
|
348 | try: | |
323 | for i in xrange(sys.maxint): |
|
349 | for i in xrange(sys.maxint): | |
324 | res = self.complete(text, i, text) |
|
350 | res = self.complete(text, i, text) | |
325 | if not res: |
|
351 | if not res: | |
326 | break |
|
352 | break | |
327 | comp_append(res) |
|
353 | comp_append(res) | |
328 | #XXX workaround for ``notDefined.<tab>`` |
|
354 | #XXX workaround for ``notDefined.<tab>`` | |
329 | except NameError: |
|
355 | except NameError: | |
330 | pass |
|
356 | pass | |
331 | return completions |
|
357 | return completions | |
332 | # /end Alex Schmolck code. |
|
358 | # /end Alex Schmolck code. | |
333 |
|
359 | |||
334 | def _clean_glob(self,text): |
|
360 | def _clean_glob(self,text): | |
335 | return self.glob("%s*" % text) |
|
361 | return self.glob("%s*" % text) | |
336 |
|
362 | |||
337 | def _clean_glob_win32(self,text): |
|
363 | def _clean_glob_win32(self,text): | |
338 | return [f.replace("\\","/") |
|
364 | return [f.replace("\\","/") | |
339 | for f in self.glob("%s*" % text)] |
|
365 | for f in self.glob("%s*" % text)] | |
340 |
|
366 | |||
341 | def file_matches(self, text): |
|
367 | def file_matches(self, text): | |
342 | """Match filenames, expanding ~USER type strings. |
|
368 | """Match filenames, expanding ~USER type strings. | |
343 |
|
369 | |||
344 | Most of the seemingly convoluted logic in this completer is an |
|
370 | Most of the seemingly convoluted logic in this completer is an | |
345 | attempt to handle filenames with spaces in them. And yet it's not |
|
371 | attempt to handle filenames with spaces in them. And yet it's not | |
346 | quite perfect, because Python's readline doesn't expose all of the |
|
372 | quite perfect, because Python's readline doesn't expose all of the | |
347 | GNU readline details needed for this to be done correctly. |
|
373 | GNU readline details needed for this to be done correctly. | |
348 |
|
374 | |||
349 | For a filename with a space in it, the printed completions will be |
|
375 | For a filename with a space in it, the printed completions will be | |
350 | only the parts after what's already been typed (instead of the |
|
376 | only the parts after what's already been typed (instead of the | |
351 | full completions, as is normally done). I don't think with the |
|
377 | full completions, as is normally done). I don't think with the | |
352 | current (as of Python 2.3) Python readline it's possible to do |
|
378 | current (as of Python 2.3) Python readline it's possible to do | |
353 | better.""" |
|
379 | better.""" | |
354 |
|
380 | |||
355 | #print 'Completer->file_matches: <%s>' % text # dbg |
|
381 | #print 'Completer->file_matches: <%s>' % text # dbg | |
356 |
|
382 | |||
357 | # chars that require escaping with backslash - i.e. chars |
|
383 | # chars that require escaping with backslash - i.e. chars | |
358 | # that readline treats incorrectly as delimiters, but we |
|
384 | # that readline treats incorrectly as delimiters, but we | |
359 | # don't want to treat as delimiters in filename matching |
|
385 | # don't want to treat as delimiters in filename matching | |
360 | # when escaped with backslash |
|
386 | # when escaped with backslash | |
361 |
|
387 | |||
362 | if text.startswith('!'): |
|
388 | if text.startswith('!'): | |
363 | text = text[1:] |
|
389 | text = text[1:] | |
364 | text_prefix = '!' |
|
390 | text_prefix = '!' | |
365 | else: |
|
391 | else: | |
366 | text_prefix = '' |
|
392 | text_prefix = '' | |
367 |
|
393 | |||
368 | lbuf = self.lbuf |
|
394 | lbuf = self.lbuf | |
369 | open_quotes = 0 # track strings with open quotes |
|
395 | open_quotes = 0 # track strings with open quotes | |
370 | try: |
|
396 | try: | |
371 | lsplit = shlex.split(lbuf)[-1] |
|
397 | lsplit = shlex.split(lbuf)[-1] | |
372 | except ValueError: |
|
398 | except ValueError: | |
373 | # typically an unmatched ", or backslash without escaped char. |
|
399 | # typically an unmatched ", or backslash without escaped char. | |
374 | if lbuf.count('"')==1: |
|
400 | if lbuf.count('"')==1: | |
375 | open_quotes = 1 |
|
401 | open_quotes = 1 | |
376 | lsplit = lbuf.split('"')[-1] |
|
402 | lsplit = lbuf.split('"')[-1] | |
377 | elif lbuf.count("'")==1: |
|
403 | elif lbuf.count("'")==1: | |
378 | open_quotes = 1 |
|
404 | open_quotes = 1 | |
379 | lsplit = lbuf.split("'")[-1] |
|
405 | lsplit = lbuf.split("'")[-1] | |
380 | else: |
|
406 | else: | |
381 | return [] |
|
407 | return [] | |
382 | except IndexError: |
|
408 | except IndexError: | |
383 | # tab pressed on empty line |
|
409 | # tab pressed on empty line | |
384 | lsplit = "" |
|
410 | lsplit = "" | |
385 |
|
411 | |||
386 | if lsplit != protect_filename(lsplit): |
|
412 | if lsplit != protect_filename(lsplit): | |
387 | # if protectables are found, do matching on the whole escaped |
|
413 | # if protectables are found, do matching on the whole escaped | |
388 | # name |
|
414 | # name | |
389 | has_protectables = 1 |
|
415 | has_protectables = 1 | |
390 | text0,text = text,lsplit |
|
416 | text0,text = text,lsplit | |
391 | else: |
|
417 | else: | |
392 | has_protectables = 0 |
|
418 | has_protectables = 0 | |
393 | text = os.path.expanduser(text) |
|
419 | text = os.path.expanduser(text) | |
394 |
|
420 | |||
395 | if text == "": |
|
421 | if text == "": | |
396 | return [text_prefix + protect_filename(f) for f in self.glob("*")] |
|
422 | return [text_prefix + protect_filename(f) for f in self.glob("*")] | |
397 |
|
423 | |||
398 | m0 = self.clean_glob(text.replace('\\','')) |
|
424 | m0 = self.clean_glob(text.replace('\\','')) | |
399 | if has_protectables: |
|
425 | if has_protectables: | |
400 | # If we had protectables, we need to revert our changes to the |
|
426 | # If we had protectables, we need to revert our changes to the | |
401 | # beginning of filename so that we don't double-write the part |
|
427 | # beginning of filename so that we don't double-write the part | |
402 | # of the filename we have so far |
|
428 | # of the filename we have so far | |
403 | len_lsplit = len(lsplit) |
|
429 | len_lsplit = len(lsplit) | |
404 | matches = [text_prefix + text0 + |
|
430 | matches = [text_prefix + text0 + | |
405 | protect_filename(f[len_lsplit:]) for f in m0] |
|
431 | protect_filename(f[len_lsplit:]) for f in m0] | |
406 | else: |
|
432 | else: | |
407 | if open_quotes: |
|
433 | if open_quotes: | |
408 | # if we have a string with an open quote, we don't need to |
|
434 | # if we have a string with an open quote, we don't need to | |
409 | # protect the names at all (and we _shouldn't_, as it |
|
435 | # protect the names at all (and we _shouldn't_, as it | |
410 | # would cause bugs when the filesystem call is made). |
|
436 | # would cause bugs when the filesystem call is made). | |
411 | matches = m0 |
|
437 | matches = m0 | |
412 | else: |
|
438 | else: | |
413 | matches = [text_prefix + |
|
439 | matches = [text_prefix + | |
414 | protect_filename(f) for f in m0] |
|
440 | protect_filename(f) for f in m0] | |
415 |
|
441 | |||
416 | #print 'mm',matches # dbg |
|
442 | #print 'mm',matches # dbg | |
417 | return single_dir_expand(matches) |
|
443 | #return single_dir_expand(matches) | |
|
444 | return mark_dirs(matches) | |||
418 |
|
445 | |||
419 | def magic_matches(self, text): |
|
446 | def magic_matches(self, text): | |
420 | """Match magics""" |
|
447 | """Match magics""" | |
421 | #print 'Completer->magic_matches:',text,'lb',self.lbuf # dbg |
|
448 | #print 'Completer->magic_matches:',text,'lb',self.lbuf # dbg | |
422 | # Get all shell magics now rather than statically, so magics loaded at |
|
449 | # Get all shell magics now rather than statically, so magics loaded at | |
423 | # runtime show up too |
|
450 | # runtime show up too | |
424 | magics = self.shell.lsmagic() |
|
451 | magics = self.shell.lsmagic() | |
425 | pre = self.magic_escape |
|
452 | pre = self.magic_escape | |
426 | baretext = text.lstrip(pre) |
|
453 | baretext = text.lstrip(pre) | |
427 | return [ pre+m for m in magics if m.startswith(baretext)] |
|
454 | return [ pre+m for m in magics if m.startswith(baretext)] | |
428 |
|
455 | |||
429 | def alias_matches(self, text): |
|
456 | def alias_matches(self, text): | |
430 | """Match internal system aliases""" |
|
457 | """Match internal system aliases""" | |
431 | #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg |
|
458 | #print 'Completer->alias_matches:',text,'lb',self.lbuf # dbg | |
432 |
|
459 | |||
433 | # if we are not in the first 'item', alias matching |
|
460 | # if we are not in the first 'item', alias matching | |
434 | # doesn't make sense - unless we are starting with 'sudo' command. |
|
461 | # doesn't make sense - unless we are starting with 'sudo' command. | |
435 | if ' ' in self.lbuf.lstrip() and \ |
|
462 | if ' ' in self.lbuf.lstrip() and \ | |
436 | not self.lbuf.lstrip().startswith('sudo'): |
|
463 | not self.lbuf.lstrip().startswith('sudo'): | |
437 | return [] |
|
464 | return [] | |
438 | text = os.path.expanduser(text) |
|
465 | text = os.path.expanduser(text) | |
439 | aliases = self.alias_table.keys() |
|
466 | aliases = self.alias_table.keys() | |
440 | if text == "": |
|
467 | if text == "": | |
441 | return aliases |
|
468 | return aliases | |
442 | else: |
|
469 | else: | |
443 | return [alias for alias in aliases if alias.startswith(text)] |
|
470 | return [alias for alias in aliases if alias.startswith(text)] | |
444 |
|
471 | |||
445 | def python_matches(self,text): |
|
472 | def python_matches(self,text): | |
446 | """Match attributes or global python names""" |
|
473 | """Match attributes or global python names""" | |
447 |
|
474 | |||
448 | #print 'Completer->python_matches, txt=%r' % text # dbg |
|
475 | #print 'Completer->python_matches, txt=%r' % text # dbg | |
449 | if "." in text: |
|
476 | if "." in text: | |
450 | try: |
|
477 | try: | |
451 | matches = self.attr_matches(text) |
|
478 | matches = self.attr_matches(text) | |
452 | if text.endswith('.') and self.omit__names: |
|
479 | if text.endswith('.') and self.omit__names: | |
453 | if self.omit__names == 1: |
|
480 | if self.omit__names == 1: | |
454 | # true if txt is _not_ a __ name, false otherwise: |
|
481 | # true if txt is _not_ a __ name, false otherwise: | |
455 | no__name = (lambda txt: |
|
482 | no__name = (lambda txt: | |
456 | re.match(r'.*\.__.*?__',txt) is None) |
|
483 | re.match(r'.*\.__.*?__',txt) is None) | |
457 | else: |
|
484 | else: | |
458 | # true if txt is _not_ a _ name, false otherwise: |
|
485 | # true if txt is _not_ a _ name, false otherwise: | |
459 | no__name = (lambda txt: |
|
486 | no__name = (lambda txt: | |
460 | re.match(r'.*\._.*?',txt) is None) |
|
487 | re.match(r'.*\._.*?',txt) is None) | |
461 | matches = filter(no__name, matches) |
|
488 | matches = filter(no__name, matches) | |
462 | except NameError: |
|
489 | except NameError: | |
463 | # catches <undefined attributes>.<tab> |
|
490 | # catches <undefined attributes>.<tab> | |
464 | matches = [] |
|
491 | matches = [] | |
465 | else: |
|
492 | else: | |
466 | matches = self.global_matches(text) |
|
493 | matches = self.global_matches(text) | |
467 |
|
494 | |||
468 | return matches |
|
495 | return matches | |
469 |
|
496 | |||
470 | def _default_arguments(self, obj): |
|
497 | def _default_arguments(self, obj): | |
471 | """Return the list of default arguments of obj if it is callable, |
|
498 | """Return the list of default arguments of obj if it is callable, | |
472 | or empty list otherwise.""" |
|
499 | or empty list otherwise.""" | |
473 |
|
500 | |||
474 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
501 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): | |
475 | # for classes, check for __init__,__new__ |
|
502 | # for classes, check for __init__,__new__ | |
476 | if inspect.isclass(obj): |
|
503 | if inspect.isclass(obj): | |
477 | obj = (getattr(obj,'__init__',None) or |
|
504 | obj = (getattr(obj,'__init__',None) or | |
478 | getattr(obj,'__new__',None)) |
|
505 | getattr(obj,'__new__',None)) | |
479 | # for all others, check if they are __call__able |
|
506 | # for all others, check if they are __call__able | |
480 | elif hasattr(obj, '__call__'): |
|
507 | elif hasattr(obj, '__call__'): | |
481 | obj = obj.__call__ |
|
508 | obj = obj.__call__ | |
482 | # XXX: is there a way to handle the builtins ? |
|
509 | # XXX: is there a way to handle the builtins ? | |
483 | try: |
|
510 | try: | |
484 | args,_,_1,defaults = inspect.getargspec(obj) |
|
511 | args,_,_1,defaults = inspect.getargspec(obj) | |
485 | if defaults: |
|
512 | if defaults: | |
486 | return args[-len(defaults):] |
|
513 | return args[-len(defaults):] | |
487 | except TypeError: pass |
|
514 | except TypeError: pass | |
488 | return [] |
|
515 | return [] | |
489 |
|
516 | |||
490 | def python_func_kw_matches(self,text): |
|
517 | def python_func_kw_matches(self,text): | |
491 | """Match named parameters (kwargs) of the last open function""" |
|
518 | """Match named parameters (kwargs) of the last open function""" | |
492 |
|
519 | |||
493 | if "." in text: # a parameter cannot be dotted |
|
520 | if "." in text: # a parameter cannot be dotted | |
494 | return [] |
|
521 | return [] | |
495 | try: regexp = self.__funcParamsRegex |
|
522 | try: regexp = self.__funcParamsRegex | |
496 | except AttributeError: |
|
523 | except AttributeError: | |
497 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
524 | regexp = self.__funcParamsRegex = re.compile(r''' | |
498 | '.*?' | # single quoted strings or |
|
525 | '.*?' | # single quoted strings or | |
499 | ".*?" | # double quoted strings or |
|
526 | ".*?" | # double quoted strings or | |
500 | \w+ | # identifier |
|
527 | \w+ | # identifier | |
501 | \S # other characters |
|
528 | \S # other characters | |
502 | ''', re.VERBOSE | re.DOTALL) |
|
529 | ''', re.VERBOSE | re.DOTALL) | |
503 | # 1. find the nearest identifier that comes before an unclosed |
|
530 | # 1. find the nearest identifier that comes before an unclosed | |
504 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
531 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" | |
505 | tokens = regexp.findall(self.get_line_buffer()) |
|
532 | tokens = regexp.findall(self.get_line_buffer()) | |
506 | tokens.reverse() |
|
533 | tokens.reverse() | |
507 | iterTokens = iter(tokens); openPar = 0 |
|
534 | iterTokens = iter(tokens); openPar = 0 | |
508 | for token in iterTokens: |
|
535 | for token in iterTokens: | |
509 | if token == ')': |
|
536 | if token == ')': | |
510 | openPar -= 1 |
|
537 | openPar -= 1 | |
511 | elif token == '(': |
|
538 | elif token == '(': | |
512 | openPar += 1 |
|
539 | openPar += 1 | |
513 | if openPar > 0: |
|
540 | if openPar > 0: | |
514 | # found the last unclosed parenthesis |
|
541 | # found the last unclosed parenthesis | |
515 | break |
|
542 | break | |
516 | else: |
|
543 | else: | |
517 | return [] |
|
544 | return [] | |
518 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
545 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) | |
519 | ids = [] |
|
546 | ids = [] | |
520 | isId = re.compile(r'\w+$').match |
|
547 | isId = re.compile(r'\w+$').match | |
521 | while True: |
|
548 | while True: | |
522 | try: |
|
549 | try: | |
523 | ids.append(iterTokens.next()) |
|
550 | ids.append(iterTokens.next()) | |
524 | if not isId(ids[-1]): |
|
551 | if not isId(ids[-1]): | |
525 | ids.pop(); break |
|
552 | ids.pop(); break | |
526 | if not iterTokens.next() == '.': |
|
553 | if not iterTokens.next() == '.': | |
527 | break |
|
554 | break | |
528 | except StopIteration: |
|
555 | except StopIteration: | |
529 | break |
|
556 | break | |
530 | # lookup the candidate callable matches either using global_matches |
|
557 | # lookup the candidate callable matches either using global_matches | |
531 | # or attr_matches for dotted names |
|
558 | # or attr_matches for dotted names | |
532 | if len(ids) == 1: |
|
559 | if len(ids) == 1: | |
533 | callableMatches = self.global_matches(ids[0]) |
|
560 | callableMatches = self.global_matches(ids[0]) | |
534 | else: |
|
561 | else: | |
535 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
562 | callableMatches = self.attr_matches('.'.join(ids[::-1])) | |
536 | argMatches = [] |
|
563 | argMatches = [] | |
537 | for callableMatch in callableMatches: |
|
564 | for callableMatch in callableMatches: | |
538 | try: |
|
565 | try: | |
539 | namedArgs = self._default_arguments(eval(callableMatch, |
|
566 | namedArgs = self._default_arguments(eval(callableMatch, | |
540 | self.namespace)) |
|
567 | self.namespace)) | |
541 | except: |
|
568 | except: | |
542 | continue |
|
569 | continue | |
543 | for namedArg in namedArgs: |
|
570 | for namedArg in namedArgs: | |
544 | if namedArg.startswith(text): |
|
571 | if namedArg.startswith(text): | |
545 | argMatches.append("%s=" %namedArg) |
|
572 | argMatches.append("%s=" %namedArg) | |
546 | return argMatches |
|
573 | return argMatches | |
547 |
|
574 | |||
548 | def dispatch_custom_completer(self,text): |
|
575 | def dispatch_custom_completer(self,text): | |
549 | #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg |
|
576 | #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg | |
550 | line = self.full_lbuf |
|
577 | line = self.full_lbuf | |
551 | if not line.strip(): |
|
578 | if not line.strip(): | |
552 | return None |
|
579 | return None | |
553 |
|
580 | |||
554 | event = Bunch() |
|
581 | event = Bunch() | |
555 | event.line = line |
|
582 | event.line = line | |
556 | event.symbol = text |
|
583 | event.symbol = text | |
557 | cmd = line.split(None,1)[0] |
|
584 | cmd = line.split(None,1)[0] | |
558 | event.command = cmd |
|
585 | event.command = cmd | |
559 | #print "\ncustom:{%s]\n" % event # dbg |
|
586 | #print "\ncustom:{%s]\n" % event # dbg | |
560 |
|
587 | |||
561 | # for foo etc, try also to find completer for %foo |
|
588 | # for foo etc, try also to find completer for %foo | |
562 | if not cmd.startswith(self.magic_escape): |
|
589 | if not cmd.startswith(self.magic_escape): | |
563 | try_magic = self.custom_completers.s_matches( |
|
590 | try_magic = self.custom_completers.s_matches( | |
564 | self.magic_escape + cmd) |
|
591 | self.magic_escape + cmd) | |
565 | else: |
|
592 | else: | |
566 | try_magic = [] |
|
593 | try_magic = [] | |
567 |
|
594 | |||
568 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
595 | for c in itertools.chain(self.custom_completers.s_matches(cmd), | |
569 | try_magic, |
|
596 | try_magic, | |
570 | self.custom_completers.flat_matches(self.lbuf)): |
|
597 | self.custom_completers.flat_matches(self.lbuf)): | |
571 | #print "try",c # dbg |
|
598 | #print "try",c # dbg | |
572 | try: |
|
599 | try: | |
573 | res = c(event) |
|
600 | res = c(event) | |
574 | # first, try case sensitive match |
|
601 | # first, try case sensitive match | |
575 | withcase = [r for r in res if r.startswith(text)] |
|
602 | withcase = [r for r in res if r.startswith(text)] | |
576 | if withcase: |
|
603 | if withcase: | |
577 | return withcase |
|
604 | return withcase | |
578 | # if none, then case insensitive ones are ok too |
|
605 | # if none, then case insensitive ones are ok too | |
579 | text_low = text.lower() |
|
606 | text_low = text.lower() | |
580 | return [r for r in res if r.lower().startswith(text_low)] |
|
607 | return [r for r in res if r.lower().startswith(text_low)] | |
581 | except TryNext: |
|
608 | except TryNext: | |
582 | pass |
|
609 | pass | |
583 |
|
610 | |||
584 | return None |
|
611 | return None | |
585 |
|
612 | |||
586 |
def complete(self, text, |
|
613 | def complete(self, text, line_buffer, cursor_pos=None): | |
587 |
"""Return the |
|
614 | """Return the state-th possible completion for 'text'. | |
588 |
|
615 | |||
589 | This is called successively with state == 0, 1, 2, ... until it |
|
616 | This is called successively with state == 0, 1, 2, ... until it | |
590 | returns None. The completion should begin with 'text'. |
|
617 | returns None. The completion should begin with 'text'. | |
591 |
|
618 | |||
592 |
|
|
619 | Parameters | |
593 | - line_buffer: string |
|
620 | ---------- | |
594 | If not given, the completer attempts to obtain the current line buffer |
|
621 | text : string | |
595 | via readline. This keyword allows clients which are requesting for |
|
622 | Text to perform the completion on. | |
596 | text completions in non-readline contexts to inform the completer of |
|
|||
597 | the entire text. |
|
|||
598 | """ |
|
|||
599 |
|
||||
600 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg |
|
|||
601 |
|
|
623 | ||
602 | # if there is only a tab on a line with only whitespace, instead |
|
624 | line_buffer : string, optional | |
603 | # of the mostly useless 'do you want to see all million |
|
625 | If not given, the completer attempts to obtain the current line | |
604 | # completions' message, just do the right thing and give the user |
|
626 | buffer via readline. This keyword allows clients which are | |
605 | # his tab! Incidentally, this enables pasting of tabbed text from |
|
627 | requesting for text completions in non-readline contexts to inform | |
606 | # an editor (as long as autoindent is off). |
|
628 | the completer of the entire text. | |
607 |
|
|
629 | ||
608 | # It should be noted that at least pyreadline still shows |
|
630 | cursor_pos : int, optional | |
609 | # file completions - is there a way around it? |
|
631 | Index of the cursor in the full line buffer. Should be provided by | |
610 |
|
632 | remote frontends where kernel has no access to frontend state. | ||
611 | # don't apply this on 'dumb' terminals, such as emacs buffers, so we |
|
633 | """ | |
612 | # don't interfere with their own tab-completion mechanism. |
|
|||
613 | if line_buffer is None: |
|
|||
614 | self.full_lbuf = self.get_line_buffer() |
|
|||
615 | else: |
|
|||
616 | self.full_lbuf = line_buffer |
|
|||
617 |
|
||||
618 | if not (self.dumb_terminal or self.full_lbuf.strip()): |
|
|||
619 | self.readline.insert_text('\t') |
|
|||
620 | return None |
|
|||
621 |
|
634 | |||
622 | magic_escape = self.magic_escape |
|
635 | magic_escape = self.magic_escape | |
|
636 | self.full_lbuf = line_buffer | |||
|
637 | self.lbuf = self.full_lbuf[:cursor_pos] | |||
623 |
|
638 | |||
624 | self.lbuf = self.full_lbuf[:self.get_endidx()] |
|
|||
625 |
|
||||
626 | try: |
|
|||
627 |
|
|
639 | if text.startswith('~'): | |
628 |
|
|
640 | text = os.path.expanduser(text) | |
629 | if state == 0: |
|
641 | ||
|
642 | # Start with a clean slate of completions | |||
|
643 | self.matches[:] = [] | |||
630 |
|
|
644 | custom_res = self.dispatch_custom_completer(text) | |
631 |
|
|
645 | if custom_res is not None: | |
632 |
|
|
646 | # did custom completers produce something? | |
633 |
|
|
647 | self.matches = custom_res | |
634 |
|
|
648 | else: | |
635 |
|
|
649 | # Extend the list of completions with the results of each | |
636 |
|
|
650 | # matcher, so we return results to the user from all | |
637 |
|
|
651 | # namespaces. | |
638 |
|
|
652 | if self.merge_completions: | |
639 |
|
|
653 | self.matches = [] | |
640 |
|
|
654 | for matcher in self.matchers: | |
641 |
|
|
655 | self.matches.extend(matcher(text)) | |
642 |
|
|
656 | else: | |
643 |
|
|
657 | for matcher in self.matchers: | |
644 |
|
|
658 | self.matches = matcher(text) | |
645 |
|
|
659 | if self.matches: | |
646 |
|
|
660 | break | |
647 | self.matches = list(set(self.matches)) |
|
661 | # FIXME: we should extend our api to return a dict with completions for | |
|
662 | # different types of objects. The rlcomplete() method could then | |||
|
663 | # simply collapse the dict into a list for readline, but we'd have | |||
|
664 | # richer completion semantics in other evironments. | |||
|
665 | self.matches = sorted(set(self.matches)) | |||
|
666 | #from IPython.utils.io import rprint; rprint(self.matches) # dbg | |||
|
667 | return self.matches | |||
|
668 | ||||
|
669 | def rlcomplete(self, text, state): | |||
|
670 | """Return the state-th possible completion for 'text'. | |||
|
671 | ||||
|
672 | This is called successively with state == 0, 1, 2, ... until it | |||
|
673 | returns None. The completion should begin with 'text'. | |||
|
674 | ||||
|
675 | Parameters | |||
|
676 | ---------- | |||
|
677 | text : string | |||
|
678 | Text to perform the completion on. | |||
|
679 | ||||
|
680 | state : int | |||
|
681 | Counter used by readline. | |||
|
682 | ||||
|
683 | """ | |||
|
684 | ||||
|
685 | #print "rlcomplete! '%s' %s" % (text, state) # dbg | |||
|
686 | ||||
|
687 | if state==0: | |||
|
688 | self.full_lbuf = line_buffer = self.get_line_buffer() | |||
|
689 | cursor_pos = self.get_endidx() | |||
|
690 | ||||
|
691 | # if there is only a tab on a line with only whitespace, instead of | |||
|
692 | # the mostly useless 'do you want to see all million completions' | |||
|
693 | # message, just do the right thing and give the user his tab! | |||
|
694 | # Incidentally, this enables pasting of tabbed text from an editor | |||
|
695 | # (as long as autoindent is off). | |||
|
696 | ||||
|
697 | # It should be noted that at least pyreadline still shows file | |||
|
698 | # completions - is there a way around it? | |||
|
699 | ||||
|
700 | # don't apply this on 'dumb' terminals, such as emacs buffers, so | |||
|
701 | # we don't interfere with their own tab-completion mechanism. | |||
|
702 | if not (self.dumb_terminal or self.full_lbuf.strip()): | |||
|
703 | self.readline.insert_text('\t') | |||
|
704 | sys.stdout.flush() | |||
|
705 | return None | |||
|
706 | ||||
|
707 | # This method computes the self.matches array | |||
|
708 | self.complete(text, line_buffer, cursor_pos) | |||
|
709 | ||||
|
710 | # Debug version, since readline silences all exceptions making it | |||
|
711 | # impossible to debug any problem in the above code | |||
|
712 | ||||
|
713 | ## try: | |||
|
714 | ## self.complete(text, line_buffer, cursor_pos) | |||
|
715 | ## except: | |||
|
716 | ## import traceback; traceback.print_exc() | |||
|
717 | ||||
648 |
|
|
718 | try: | |
649 | #print "MATCH: %r" % self.matches[state] # dbg |
|
|||
650 |
|
|
719 | return self.matches[state] | |
651 |
|
|
720 | except IndexError: | |
652 |
|
|
721 | return None | |
653 | except: |
|
|||
654 | #from IPython.core.ultratb import AutoFormattedTB; # dbg |
|
|||
655 | #tb=AutoFormattedTB('Verbose');tb() #dbg |
|
|||
656 |
|
722 | |||
657 | # If completion fails, don't annoy the user. |
|
|||
658 | return None |
|
@@ -1,2150 +1,2147 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Main IPython class.""" |
|
2 | """Main IPython class.""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 | # Copyright (C) 2008-2010 The IPython Development Team |
|
7 | # Copyright (C) 2008-2010 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from __future__ import with_statement |
|
17 | from __future__ import with_statement | |
18 | from __future__ import absolute_import |
|
18 | from __future__ import absolute_import | |
19 |
|
19 | |||
20 | import __builtin__ |
|
20 | import __builtin__ | |
21 | import abc |
|
21 | import abc | |
22 | import codeop |
|
22 | import codeop | |
23 | import exceptions |
|
23 | import exceptions | |
24 | import new |
|
24 | import new | |
25 | import os |
|
25 | import os | |
26 | import re |
|
26 | import re | |
27 | import string |
|
27 | import string | |
28 | import sys |
|
28 | import sys | |
29 | import tempfile |
|
29 | import tempfile | |
30 | from contextlib import nested |
|
30 | from contextlib import nested | |
31 |
|
31 | |||
32 | from IPython.config.configurable import Configurable |
|
32 | from IPython.config.configurable import Configurable | |
33 | from IPython.core import debugger, oinspect |
|
33 | from IPython.core import debugger, oinspect | |
34 | from IPython.core import history as ipcorehist |
|
34 | from IPython.core import history as ipcorehist | |
35 | from IPython.core import prefilter |
|
35 | from IPython.core import prefilter | |
36 | from IPython.core import shadowns |
|
36 | from IPython.core import shadowns | |
37 | from IPython.core import ultratb |
|
37 | from IPython.core import ultratb | |
38 | from IPython.core.alias import AliasManager |
|
38 | from IPython.core.alias import AliasManager | |
39 | from IPython.core.builtin_trap import BuiltinTrap |
|
39 | from IPython.core.builtin_trap import BuiltinTrap | |
40 | from IPython.core.display_trap import DisplayTrap |
|
40 | from IPython.core.display_trap import DisplayTrap | |
41 | from IPython.core.displayhook import DisplayHook |
|
41 | from IPython.core.displayhook import DisplayHook | |
42 | from IPython.core.error import UsageError |
|
42 | from IPython.core.error import UsageError | |
43 | from IPython.core.extensions import ExtensionManager |
|
43 | from IPython.core.extensions import ExtensionManager | |
44 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
44 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict | |
45 | from IPython.core.inputlist import InputList |
|
45 | from IPython.core.inputlist import InputList | |
46 | from IPython.core.logger import Logger |
|
46 | from IPython.core.logger import Logger | |
47 | from IPython.core.magic import Magic |
|
47 | from IPython.core.magic import Magic | |
48 | from IPython.core.payload import PayloadManager |
|
48 | from IPython.core.payload import PayloadManager | |
49 | from IPython.core.plugin import PluginManager |
|
49 | from IPython.core.plugin import PluginManager | |
50 | from IPython.core.prefilter import PrefilterManager |
|
50 | from IPython.core.prefilter import PrefilterManager | |
51 | from IPython.external.Itpl import ItplNS |
|
51 | from IPython.external.Itpl import ItplNS | |
52 | from IPython.utils import PyColorize |
|
52 | from IPython.utils import PyColorize | |
53 | from IPython.utils import io |
|
53 | from IPython.utils import io | |
54 | from IPython.utils import pickleshare |
|
54 | from IPython.utils import pickleshare | |
55 | from IPython.utils.doctestreload import doctest_reload |
|
55 | from IPython.utils.doctestreload import doctest_reload | |
56 | from IPython.utils.io import ask_yes_no, rprint |
|
56 | from IPython.utils.io import ask_yes_no, rprint | |
57 | from IPython.utils.ipstruct import Struct |
|
57 | from IPython.utils.ipstruct import Struct | |
58 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError |
|
58 | from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError | |
59 | from IPython.utils.process import getoutput, getoutputerror |
|
59 | from IPython.utils.process import getoutput, getoutputerror | |
60 | from IPython.utils.strdispatch import StrDispatch |
|
60 | from IPython.utils.strdispatch import StrDispatch | |
61 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
61 | from IPython.utils.syspathcontext import prepended_to_syspath | |
62 | from IPython.utils.text import num_ini_spaces |
|
62 | from IPython.utils.text import num_ini_spaces | |
63 | from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum, |
|
63 | from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum, | |
64 | List, Unicode, Instance, Type) |
|
64 | List, Unicode, Instance, Type) | |
65 | from IPython.utils.warn import warn, error, fatal |
|
65 | from IPython.utils.warn import warn, error, fatal | |
66 | import IPython.core.hooks |
|
66 | import IPython.core.hooks | |
67 |
|
67 | |||
68 | # from IPython.utils import growl |
|
68 | # from IPython.utils import growl | |
69 | # growl.start("IPython") |
|
69 | # growl.start("IPython") | |
70 |
|
70 | |||
71 | #----------------------------------------------------------------------------- |
|
71 | #----------------------------------------------------------------------------- | |
72 | # Globals |
|
72 | # Globals | |
73 | #----------------------------------------------------------------------------- |
|
73 | #----------------------------------------------------------------------------- | |
74 |
|
74 | |||
75 | # compiled regexps for autoindent management |
|
75 | # compiled regexps for autoindent management | |
76 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
76 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
77 |
|
77 | |||
78 | #----------------------------------------------------------------------------- |
|
78 | #----------------------------------------------------------------------------- | |
79 | # Utilities |
|
79 | # Utilities | |
80 | #----------------------------------------------------------------------------- |
|
80 | #----------------------------------------------------------------------------- | |
81 |
|
81 | |||
82 | # store the builtin raw_input globally, and use this always, in case user code |
|
82 | # store the builtin raw_input globally, and use this always, in case user code | |
83 | # overwrites it (like wx.py.PyShell does) |
|
83 | # overwrites it (like wx.py.PyShell does) | |
84 | raw_input_original = raw_input |
|
84 | raw_input_original = raw_input | |
85 |
|
85 | |||
86 | def softspace(file, newvalue): |
|
86 | def softspace(file, newvalue): | |
87 | """Copied from code.py, to remove the dependency""" |
|
87 | """Copied from code.py, to remove the dependency""" | |
88 |
|
88 | |||
89 | oldvalue = 0 |
|
89 | oldvalue = 0 | |
90 | try: |
|
90 | try: | |
91 | oldvalue = file.softspace |
|
91 | oldvalue = file.softspace | |
92 | except AttributeError: |
|
92 | except AttributeError: | |
93 | pass |
|
93 | pass | |
94 | try: |
|
94 | try: | |
95 | file.softspace = newvalue |
|
95 | file.softspace = newvalue | |
96 | except (AttributeError, TypeError): |
|
96 | except (AttributeError, TypeError): | |
97 | # "attribute-less object" or "read-only attributes" |
|
97 | # "attribute-less object" or "read-only attributes" | |
98 | pass |
|
98 | pass | |
99 | return oldvalue |
|
99 | return oldvalue | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | def no_op(*a, **kw): pass |
|
102 | def no_op(*a, **kw): pass | |
103 |
|
103 | |||
104 | class SpaceInInput(exceptions.Exception): pass |
|
104 | class SpaceInInput(exceptions.Exception): pass | |
105 |
|
105 | |||
106 | class Bunch: pass |
|
106 | class Bunch: pass | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | def get_default_colors(): |
|
109 | def get_default_colors(): | |
110 | if sys.platform=='darwin': |
|
110 | if sys.platform=='darwin': | |
111 | return "LightBG" |
|
111 | return "LightBG" | |
112 | elif os.name=='nt': |
|
112 | elif os.name=='nt': | |
113 | return 'Linux' |
|
113 | return 'Linux' | |
114 | else: |
|
114 | else: | |
115 | return 'Linux' |
|
115 | return 'Linux' | |
116 |
|
116 | |||
117 |
|
117 | |||
118 | class SeparateStr(Str): |
|
118 | class SeparateStr(Str): | |
119 | """A Str subclass to validate separate_in, separate_out, etc. |
|
119 | """A Str subclass to validate separate_in, separate_out, etc. | |
120 |
|
120 | |||
121 | This is a Str based trait that converts '0'->'' and '\\n'->'\n'. |
|
121 | This is a Str based trait that converts '0'->'' and '\\n'->'\n'. | |
122 | """ |
|
122 | """ | |
123 |
|
123 | |||
124 | def validate(self, obj, value): |
|
124 | def validate(self, obj, value): | |
125 | if value == '0': value = '' |
|
125 | if value == '0': value = '' | |
126 | value = value.replace('\\n','\n') |
|
126 | value = value.replace('\\n','\n') | |
127 | return super(SeparateStr, self).validate(obj, value) |
|
127 | return super(SeparateStr, self).validate(obj, value) | |
128 |
|
128 | |||
129 | class MultipleInstanceError(Exception): |
|
129 | class MultipleInstanceError(Exception): | |
130 | pass |
|
130 | pass | |
131 |
|
131 | |||
132 |
|
132 | |||
133 | #----------------------------------------------------------------------------- |
|
133 | #----------------------------------------------------------------------------- | |
134 | # Main IPython class |
|
134 | # Main IPython class | |
135 | #----------------------------------------------------------------------------- |
|
135 | #----------------------------------------------------------------------------- | |
136 |
|
136 | |||
137 |
|
137 | |||
138 | class InteractiveShell(Configurable, Magic): |
|
138 | class InteractiveShell(Configurable, Magic): | |
139 | """An enhanced, interactive shell for Python.""" |
|
139 | """An enhanced, interactive shell for Python.""" | |
140 |
|
140 | |||
141 | _instance = None |
|
141 | _instance = None | |
142 | autocall = Enum((0,1,2), default_value=1, config=True) |
|
142 | autocall = Enum((0,1,2), default_value=1, config=True) | |
143 | # TODO: remove all autoindent logic and put into frontends. |
|
143 | # TODO: remove all autoindent logic and put into frontends. | |
144 | # We can't do this yet because even runlines uses the autoindent. |
|
144 | # We can't do this yet because even runlines uses the autoindent. | |
145 | autoindent = CBool(True, config=True) |
|
145 | autoindent = CBool(True, config=True) | |
146 | automagic = CBool(True, config=True) |
|
146 | automagic = CBool(True, config=True) | |
147 | cache_size = Int(1000, config=True) |
|
147 | cache_size = Int(1000, config=True) | |
148 | color_info = CBool(True, config=True) |
|
148 | color_info = CBool(True, config=True) | |
149 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
149 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
150 | default_value=get_default_colors(), config=True) |
|
150 | default_value=get_default_colors(), config=True) | |
151 | debug = CBool(False, config=True) |
|
151 | debug = CBool(False, config=True) | |
152 | deep_reload = CBool(False, config=True) |
|
152 | deep_reload = CBool(False, config=True) | |
153 | displayhook_class = Type(DisplayHook) |
|
153 | displayhook_class = Type(DisplayHook) | |
154 | filename = Str("<ipython console>") |
|
154 | filename = Str("<ipython console>") | |
155 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ |
|
155 | ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__ | |
156 | logstart = CBool(False, config=True) |
|
156 | logstart = CBool(False, config=True) | |
157 | logfile = Str('', config=True) |
|
157 | logfile = Str('', config=True) | |
158 | logappend = Str('', config=True) |
|
158 | logappend = Str('', config=True) | |
159 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
159 | object_info_string_level = Enum((0,1,2), default_value=0, | |
160 | config=True) |
|
160 | config=True) | |
161 | pdb = CBool(False, config=True) |
|
161 | pdb = CBool(False, config=True) | |
162 | pprint = CBool(True, config=True) |
|
162 | pprint = CBool(True, config=True) | |
163 | profile = Str('', config=True) |
|
163 | profile = Str('', config=True) | |
164 | prompt_in1 = Str('In [\\#]: ', config=True) |
|
164 | prompt_in1 = Str('In [\\#]: ', config=True) | |
165 | prompt_in2 = Str(' .\\D.: ', config=True) |
|
165 | prompt_in2 = Str(' .\\D.: ', config=True) | |
166 | prompt_out = Str('Out[\\#]: ', config=True) |
|
166 | prompt_out = Str('Out[\\#]: ', config=True) | |
167 | prompts_pad_left = CBool(True, config=True) |
|
167 | prompts_pad_left = CBool(True, config=True) | |
168 | quiet = CBool(False, config=True) |
|
168 | quiet = CBool(False, config=True) | |
169 |
|
169 | |||
170 | # The readline stuff will eventually be moved to the terminal subclass |
|
170 | # The readline stuff will eventually be moved to the terminal subclass | |
171 | # but for now, we can't do that as readline is welded in everywhere. |
|
171 | # but for now, we can't do that as readline is welded in everywhere. | |
172 | readline_use = CBool(True, config=True) |
|
172 | readline_use = CBool(True, config=True) | |
173 | readline_merge_completions = CBool(True, config=True) |
|
173 | readline_merge_completions = CBool(True, config=True) | |
174 | readline_omit__names = Enum((0,1,2), default_value=0, config=True) |
|
174 | readline_omit__names = Enum((0,1,2), default_value=0, config=True) | |
175 | readline_remove_delims = Str('-/~', config=True) |
|
175 | readline_remove_delims = Str('-/~', config=True) | |
176 | readline_parse_and_bind = List([ |
|
176 | readline_parse_and_bind = List([ | |
177 | 'tab: complete', |
|
177 | 'tab: complete', | |
178 | '"\C-l": clear-screen', |
|
178 | '"\C-l": clear-screen', | |
179 | 'set show-all-if-ambiguous on', |
|
179 | 'set show-all-if-ambiguous on', | |
180 | '"\C-o": tab-insert', |
|
180 | '"\C-o": tab-insert', | |
181 | '"\M-i": " "', |
|
181 | '"\M-i": " "', | |
182 | '"\M-o": "\d\d\d\d"', |
|
182 | '"\M-o": "\d\d\d\d"', | |
183 | '"\M-I": "\d\d\d\d"', |
|
183 | '"\M-I": "\d\d\d\d"', | |
184 | '"\C-r": reverse-search-history', |
|
184 | '"\C-r": reverse-search-history', | |
185 | '"\C-s": forward-search-history', |
|
185 | '"\C-s": forward-search-history', | |
186 | '"\C-p": history-search-backward', |
|
186 | '"\C-p": history-search-backward', | |
187 | '"\C-n": history-search-forward', |
|
187 | '"\C-n": history-search-forward', | |
188 | '"\e[A": history-search-backward', |
|
188 | '"\e[A": history-search-backward', | |
189 | '"\e[B": history-search-forward', |
|
189 | '"\e[B": history-search-forward', | |
190 | '"\C-k": kill-line', |
|
190 | '"\C-k": kill-line', | |
191 | '"\C-u": unix-line-discard', |
|
191 | '"\C-u": unix-line-discard', | |
192 | ], allow_none=False, config=True) |
|
192 | ], allow_none=False, config=True) | |
193 |
|
193 | |||
194 | # TODO: this part of prompt management should be moved to the frontends. |
|
194 | # TODO: this part of prompt management should be moved to the frontends. | |
195 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
195 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
196 | separate_in = SeparateStr('\n', config=True) |
|
196 | separate_in = SeparateStr('\n', config=True) | |
197 | separate_out = SeparateStr('\n', config=True) |
|
197 | separate_out = SeparateStr('\n', config=True) | |
198 | separate_out2 = SeparateStr('\n', config=True) |
|
198 | separate_out2 = SeparateStr('\n', config=True) | |
199 | system_header = Str('IPython system call: ', config=True) |
|
199 | system_header = Str('IPython system call: ', config=True) | |
200 | system_verbose = CBool(False, config=True) |
|
200 | system_verbose = CBool(False, config=True) | |
201 | wildcards_case_sensitive = CBool(True, config=True) |
|
201 | wildcards_case_sensitive = CBool(True, config=True) | |
202 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
202 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
203 | default_value='Context', config=True) |
|
203 | default_value='Context', config=True) | |
204 |
|
204 | |||
205 | # Subcomponents of InteractiveShell |
|
205 | # Subcomponents of InteractiveShell | |
206 | alias_manager = Instance('IPython.core.alias.AliasManager') |
|
206 | alias_manager = Instance('IPython.core.alias.AliasManager') | |
207 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') |
|
207 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager') | |
208 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') |
|
208 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap') | |
209 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') |
|
209 | display_trap = Instance('IPython.core.display_trap.DisplayTrap') | |
210 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') |
|
210 | extension_manager = Instance('IPython.core.extensions.ExtensionManager') | |
211 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
211 | plugin_manager = Instance('IPython.core.plugin.PluginManager') | |
212 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
212 | payload_manager = Instance('IPython.core.payload.PayloadManager') | |
213 |
|
213 | |||
214 | def __init__(self, config=None, ipython_dir=None, |
|
214 | def __init__(self, config=None, ipython_dir=None, | |
215 | user_ns=None, user_global_ns=None, |
|
215 | user_ns=None, user_global_ns=None, | |
216 | custom_exceptions=((),None)): |
|
216 | custom_exceptions=((),None)): | |
217 |
|
217 | |||
218 | # This is where traits with a config_key argument are updated |
|
218 | # This is where traits with a config_key argument are updated | |
219 | # from the values on config. |
|
219 | # from the values on config. | |
220 | super(InteractiveShell, self).__init__(config=config) |
|
220 | super(InteractiveShell, self).__init__(config=config) | |
221 |
|
221 | |||
222 | # These are relatively independent and stateless |
|
222 | # These are relatively independent and stateless | |
223 | self.init_ipython_dir(ipython_dir) |
|
223 | self.init_ipython_dir(ipython_dir) | |
224 | self.init_instance_attrs() |
|
224 | self.init_instance_attrs() | |
225 |
|
225 | |||
226 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
226 | # Create namespaces (user_ns, user_global_ns, etc.) | |
227 | self.init_create_namespaces(user_ns, user_global_ns) |
|
227 | self.init_create_namespaces(user_ns, user_global_ns) | |
228 | # This has to be done after init_create_namespaces because it uses |
|
228 | # This has to be done after init_create_namespaces because it uses | |
229 | # something in self.user_ns, but before init_sys_modules, which |
|
229 | # something in self.user_ns, but before init_sys_modules, which | |
230 | # is the first thing to modify sys. |
|
230 | # is the first thing to modify sys. | |
231 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
231 | # TODO: When we override sys.stdout and sys.stderr before this class | |
232 | # is created, we are saving the overridden ones here. Not sure if this |
|
232 | # is created, we are saving the overridden ones here. Not sure if this | |
233 | # is what we want to do. |
|
233 | # is what we want to do. | |
234 | self.save_sys_module_state() |
|
234 | self.save_sys_module_state() | |
235 | self.init_sys_modules() |
|
235 | self.init_sys_modules() | |
236 |
|
236 | |||
237 | self.init_history() |
|
237 | self.init_history() | |
238 | self.init_encoding() |
|
238 | self.init_encoding() | |
239 | self.init_prefilter() |
|
239 | self.init_prefilter() | |
240 |
|
240 | |||
241 | Magic.__init__(self, self) |
|
241 | Magic.__init__(self, self) | |
242 |
|
242 | |||
243 | self.init_syntax_highlighting() |
|
243 | self.init_syntax_highlighting() | |
244 | self.init_hooks() |
|
244 | self.init_hooks() | |
245 | self.init_pushd_popd_magic() |
|
245 | self.init_pushd_popd_magic() | |
246 | # self.init_traceback_handlers use to be here, but we moved it below |
|
246 | # self.init_traceback_handlers use to be here, but we moved it below | |
247 | # because it and init_io have to come after init_readline. |
|
247 | # because it and init_io have to come after init_readline. | |
248 | self.init_user_ns() |
|
248 | self.init_user_ns() | |
249 | self.init_logger() |
|
249 | self.init_logger() | |
250 | self.init_alias() |
|
250 | self.init_alias() | |
251 | self.init_builtins() |
|
251 | self.init_builtins() | |
252 |
|
252 | |||
253 | # pre_config_initialization |
|
253 | # pre_config_initialization | |
254 | self.init_shadow_hist() |
|
254 | self.init_shadow_hist() | |
255 |
|
255 | |||
256 | # The next section should contain averything that was in ipmaker. |
|
256 | # The next section should contain averything that was in ipmaker. | |
257 | self.init_logstart() |
|
257 | self.init_logstart() | |
258 |
|
258 | |||
259 | # The following was in post_config_initialization |
|
259 | # The following was in post_config_initialization | |
260 | self.init_inspector() |
|
260 | self.init_inspector() | |
261 | # init_readline() must come before init_io(), because init_io uses |
|
261 | # init_readline() must come before init_io(), because init_io uses | |
262 | # readline related things. |
|
262 | # readline related things. | |
263 | self.init_readline() |
|
263 | self.init_readline() | |
264 | # TODO: init_io() needs to happen before init_traceback handlers |
|
264 | # TODO: init_io() needs to happen before init_traceback handlers | |
265 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
265 | # because the traceback handlers hardcode the stdout/stderr streams. | |
266 | # This logic in in debugger.Pdb and should eventually be changed. |
|
266 | # This logic in in debugger.Pdb and should eventually be changed. | |
267 | self.init_io() |
|
267 | self.init_io() | |
268 | self.init_traceback_handlers(custom_exceptions) |
|
268 | self.init_traceback_handlers(custom_exceptions) | |
269 | self.init_prompts() |
|
269 | self.init_prompts() | |
270 | self.init_displayhook() |
|
270 | self.init_displayhook() | |
271 | self.init_reload_doctest() |
|
271 | self.init_reload_doctest() | |
272 | self.init_magics() |
|
272 | self.init_magics() | |
273 | self.init_pdb() |
|
273 | self.init_pdb() | |
274 | self.init_extension_manager() |
|
274 | self.init_extension_manager() | |
275 | self.init_plugin_manager() |
|
275 | self.init_plugin_manager() | |
276 | self.init_payload() |
|
276 | self.init_payload() | |
277 | self.hooks.late_startup_hook() |
|
277 | self.hooks.late_startup_hook() | |
278 |
|
278 | |||
279 | @classmethod |
|
279 | @classmethod | |
280 | def instance(cls, *args, **kwargs): |
|
280 | def instance(cls, *args, **kwargs): | |
281 | """Returns a global InteractiveShell instance.""" |
|
281 | """Returns a global InteractiveShell instance.""" | |
282 | if cls._instance is None: |
|
282 | if cls._instance is None: | |
283 | inst = cls(*args, **kwargs) |
|
283 | inst = cls(*args, **kwargs) | |
284 | # Now make sure that the instance will also be returned by |
|
284 | # Now make sure that the instance will also be returned by | |
285 | # the subclasses instance attribute. |
|
285 | # the subclasses instance attribute. | |
286 | for subclass in cls.mro(): |
|
286 | for subclass in cls.mro(): | |
287 | if issubclass(cls, subclass) and issubclass(subclass, InteractiveShell): |
|
287 | if issubclass(cls, subclass) and issubclass(subclass, InteractiveShell): | |
288 | subclass._instance = inst |
|
288 | subclass._instance = inst | |
289 | else: |
|
289 | else: | |
290 | break |
|
290 | break | |
291 | if isinstance(cls._instance, cls): |
|
291 | if isinstance(cls._instance, cls): | |
292 | return cls._instance |
|
292 | return cls._instance | |
293 | else: |
|
293 | else: | |
294 | raise MultipleInstanceError( |
|
294 | raise MultipleInstanceError( | |
295 | 'Multiple incompatible subclass instances of ' |
|
295 | 'Multiple incompatible subclass instances of ' | |
296 | 'InteractiveShell are being created.' |
|
296 | 'InteractiveShell are being created.' | |
297 | ) |
|
297 | ) | |
298 |
|
298 | |||
299 | @classmethod |
|
299 | @classmethod | |
300 | def initialized(cls): |
|
300 | def initialized(cls): | |
301 | return hasattr(cls, "_instance") |
|
301 | return hasattr(cls, "_instance") | |
302 |
|
302 | |||
303 | def get_ipython(self): |
|
303 | def get_ipython(self): | |
304 | """Return the currently running IPython instance.""" |
|
304 | """Return the currently running IPython instance.""" | |
305 | return self |
|
305 | return self | |
306 |
|
306 | |||
307 | #------------------------------------------------------------------------- |
|
307 | #------------------------------------------------------------------------- | |
308 | # Trait changed handlers |
|
308 | # Trait changed handlers | |
309 | #------------------------------------------------------------------------- |
|
309 | #------------------------------------------------------------------------- | |
310 |
|
310 | |||
311 | def _ipython_dir_changed(self, name, new): |
|
311 | def _ipython_dir_changed(self, name, new): | |
312 | if not os.path.isdir(new): |
|
312 | if not os.path.isdir(new): | |
313 | os.makedirs(new, mode = 0777) |
|
313 | os.makedirs(new, mode = 0777) | |
314 |
|
314 | |||
315 | def set_autoindent(self,value=None): |
|
315 | def set_autoindent(self,value=None): | |
316 | """Set the autoindent flag, checking for readline support. |
|
316 | """Set the autoindent flag, checking for readline support. | |
317 |
|
317 | |||
318 | If called with no arguments, it acts as a toggle.""" |
|
318 | If called with no arguments, it acts as a toggle.""" | |
319 |
|
319 | |||
320 | if not self.has_readline: |
|
320 | if not self.has_readline: | |
321 | if os.name == 'posix': |
|
321 | if os.name == 'posix': | |
322 | warn("The auto-indent feature requires the readline library") |
|
322 | warn("The auto-indent feature requires the readline library") | |
323 | self.autoindent = 0 |
|
323 | self.autoindent = 0 | |
324 | return |
|
324 | return | |
325 | if value is None: |
|
325 | if value is None: | |
326 | self.autoindent = not self.autoindent |
|
326 | self.autoindent = not self.autoindent | |
327 | else: |
|
327 | else: | |
328 | self.autoindent = value |
|
328 | self.autoindent = value | |
329 |
|
329 | |||
330 | #------------------------------------------------------------------------- |
|
330 | #------------------------------------------------------------------------- | |
331 | # init_* methods called by __init__ |
|
331 | # init_* methods called by __init__ | |
332 | #------------------------------------------------------------------------- |
|
332 | #------------------------------------------------------------------------- | |
333 |
|
333 | |||
334 | def init_ipython_dir(self, ipython_dir): |
|
334 | def init_ipython_dir(self, ipython_dir): | |
335 | if ipython_dir is not None: |
|
335 | if ipython_dir is not None: | |
336 | self.ipython_dir = ipython_dir |
|
336 | self.ipython_dir = ipython_dir | |
337 | self.config.Global.ipython_dir = self.ipython_dir |
|
337 | self.config.Global.ipython_dir = self.ipython_dir | |
338 | return |
|
338 | return | |
339 |
|
339 | |||
340 | if hasattr(self.config.Global, 'ipython_dir'): |
|
340 | if hasattr(self.config.Global, 'ipython_dir'): | |
341 | self.ipython_dir = self.config.Global.ipython_dir |
|
341 | self.ipython_dir = self.config.Global.ipython_dir | |
342 | else: |
|
342 | else: | |
343 | self.ipython_dir = get_ipython_dir() |
|
343 | self.ipython_dir = get_ipython_dir() | |
344 |
|
344 | |||
345 | # All children can just read this |
|
345 | # All children can just read this | |
346 | self.config.Global.ipython_dir = self.ipython_dir |
|
346 | self.config.Global.ipython_dir = self.ipython_dir | |
347 |
|
347 | |||
348 | def init_instance_attrs(self): |
|
348 | def init_instance_attrs(self): | |
349 | self.more = False |
|
349 | self.more = False | |
350 |
|
350 | |||
351 | # command compiler |
|
351 | # command compiler | |
352 | self.compile = codeop.CommandCompiler() |
|
352 | self.compile = codeop.CommandCompiler() | |
353 |
|
353 | |||
354 | # User input buffer |
|
354 | # User input buffer | |
355 | self.buffer = [] |
|
355 | self.buffer = [] | |
356 |
|
356 | |||
357 | # Make an empty namespace, which extension writers can rely on both |
|
357 | # Make an empty namespace, which extension writers can rely on both | |
358 | # existing and NEVER being used by ipython itself. This gives them a |
|
358 | # existing and NEVER being used by ipython itself. This gives them a | |
359 | # convenient location for storing additional information and state |
|
359 | # convenient location for storing additional information and state | |
360 | # their extensions may require, without fear of collisions with other |
|
360 | # their extensions may require, without fear of collisions with other | |
361 | # ipython names that may develop later. |
|
361 | # ipython names that may develop later. | |
362 | self.meta = Struct() |
|
362 | self.meta = Struct() | |
363 |
|
363 | |||
364 | # Object variable to store code object waiting execution. This is |
|
364 | # Object variable to store code object waiting execution. This is | |
365 | # used mainly by the multithreaded shells, but it can come in handy in |
|
365 | # used mainly by the multithreaded shells, but it can come in handy in | |
366 | # other situations. No need to use a Queue here, since it's a single |
|
366 | # other situations. No need to use a Queue here, since it's a single | |
367 | # item which gets cleared once run. |
|
367 | # item which gets cleared once run. | |
368 | self.code_to_run = None |
|
368 | self.code_to_run = None | |
369 |
|
369 | |||
370 | # Temporary files used for various purposes. Deleted at exit. |
|
370 | # Temporary files used for various purposes. Deleted at exit. | |
371 | self.tempfiles = [] |
|
371 | self.tempfiles = [] | |
372 |
|
372 | |||
373 | # Keep track of readline usage (later set by init_readline) |
|
373 | # Keep track of readline usage (later set by init_readline) | |
374 | self.has_readline = False |
|
374 | self.has_readline = False | |
375 |
|
375 | |||
376 | # keep track of where we started running (mainly for crash post-mortem) |
|
376 | # keep track of where we started running (mainly for crash post-mortem) | |
377 | # This is not being used anywhere currently. |
|
377 | # This is not being used anywhere currently. | |
378 | self.starting_dir = os.getcwd() |
|
378 | self.starting_dir = os.getcwd() | |
379 |
|
379 | |||
380 | # Indentation management |
|
380 | # Indentation management | |
381 | self.indent_current_nsp = 0 |
|
381 | self.indent_current_nsp = 0 | |
382 |
|
382 | |||
383 | def init_encoding(self): |
|
383 | def init_encoding(self): | |
384 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
384 | # Get system encoding at startup time. Certain terminals (like Emacs | |
385 | # under Win32 have it set to None, and we need to have a known valid |
|
385 | # under Win32 have it set to None, and we need to have a known valid | |
386 | # encoding to use in the raw_input() method |
|
386 | # encoding to use in the raw_input() method | |
387 | try: |
|
387 | try: | |
388 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
388 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
389 | except AttributeError: |
|
389 | except AttributeError: | |
390 | self.stdin_encoding = 'ascii' |
|
390 | self.stdin_encoding = 'ascii' | |
391 |
|
391 | |||
392 | def init_syntax_highlighting(self): |
|
392 | def init_syntax_highlighting(self): | |
393 | # Python source parser/formatter for syntax highlighting |
|
393 | # Python source parser/formatter for syntax highlighting | |
394 | pyformat = PyColorize.Parser().format |
|
394 | pyformat = PyColorize.Parser().format | |
395 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
395 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
396 |
|
396 | |||
397 | def init_pushd_popd_magic(self): |
|
397 | def init_pushd_popd_magic(self): | |
398 | # for pushd/popd management |
|
398 | # for pushd/popd management | |
399 | try: |
|
399 | try: | |
400 | self.home_dir = get_home_dir() |
|
400 | self.home_dir = get_home_dir() | |
401 | except HomeDirError, msg: |
|
401 | except HomeDirError, msg: | |
402 | fatal(msg) |
|
402 | fatal(msg) | |
403 |
|
403 | |||
404 | self.dir_stack = [] |
|
404 | self.dir_stack = [] | |
405 |
|
405 | |||
406 | def init_logger(self): |
|
406 | def init_logger(self): | |
407 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') |
|
407 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') | |
408 | # local shortcut, this is used a LOT |
|
408 | # local shortcut, this is used a LOT | |
409 | self.log = self.logger.log |
|
409 | self.log = self.logger.log | |
410 |
|
410 | |||
411 | def init_logstart(self): |
|
411 | def init_logstart(self): | |
412 | if self.logappend: |
|
412 | if self.logappend: | |
413 | self.magic_logstart(self.logappend + ' append') |
|
413 | self.magic_logstart(self.logappend + ' append') | |
414 | elif self.logfile: |
|
414 | elif self.logfile: | |
415 | self.magic_logstart(self.logfile) |
|
415 | self.magic_logstart(self.logfile) | |
416 | elif self.logstart: |
|
416 | elif self.logstart: | |
417 | self.magic_logstart() |
|
417 | self.magic_logstart() | |
418 |
|
418 | |||
419 | def init_builtins(self): |
|
419 | def init_builtins(self): | |
420 | self.builtin_trap = BuiltinTrap(shell=self) |
|
420 | self.builtin_trap = BuiltinTrap(shell=self) | |
421 |
|
421 | |||
422 | def init_inspector(self): |
|
422 | def init_inspector(self): | |
423 | # Object inspector |
|
423 | # Object inspector | |
424 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
424 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
425 | PyColorize.ANSICodeColors, |
|
425 | PyColorize.ANSICodeColors, | |
426 | 'NoColor', |
|
426 | 'NoColor', | |
427 | self.object_info_string_level) |
|
427 | self.object_info_string_level) | |
428 |
|
428 | |||
429 | def init_io(self): |
|
429 | def init_io(self): | |
430 | import IPython.utils.io |
|
430 | import IPython.utils.io | |
431 | if sys.platform == 'win32' and self.has_readline: |
|
431 | if sys.platform == 'win32' and self.has_readline: | |
432 | Term = io.IOTerm( |
|
432 | Term = io.IOTerm( | |
433 | cout=self.readline._outputfile,cerr=self.readline._outputfile |
|
433 | cout=self.readline._outputfile,cerr=self.readline._outputfile | |
434 | ) |
|
434 | ) | |
435 | else: |
|
435 | else: | |
436 | Term = io.IOTerm() |
|
436 | Term = io.IOTerm() | |
437 | io.Term = Term |
|
437 | io.Term = Term | |
438 |
|
438 | |||
439 | def init_prompts(self): |
|
439 | def init_prompts(self): | |
440 | # TODO: This is a pass for now because the prompts are managed inside |
|
440 | # TODO: This is a pass for now because the prompts are managed inside | |
441 | # the DisplayHook. Once there is a separate prompt manager, this |
|
441 | # the DisplayHook. Once there is a separate prompt manager, this | |
442 | # will initialize that object and all prompt related information. |
|
442 | # will initialize that object and all prompt related information. | |
443 | pass |
|
443 | pass | |
444 |
|
444 | |||
445 | def init_displayhook(self): |
|
445 | def init_displayhook(self): | |
446 | # Initialize displayhook, set in/out prompts and printing system |
|
446 | # Initialize displayhook, set in/out prompts and printing system | |
447 | self.displayhook = self.displayhook_class( |
|
447 | self.displayhook = self.displayhook_class( | |
448 | shell=self, |
|
448 | shell=self, | |
449 | cache_size=self.cache_size, |
|
449 | cache_size=self.cache_size, | |
450 | input_sep = self.separate_in, |
|
450 | input_sep = self.separate_in, | |
451 | output_sep = self.separate_out, |
|
451 | output_sep = self.separate_out, | |
452 | output_sep2 = self.separate_out2, |
|
452 | output_sep2 = self.separate_out2, | |
453 | ps1 = self.prompt_in1, |
|
453 | ps1 = self.prompt_in1, | |
454 | ps2 = self.prompt_in2, |
|
454 | ps2 = self.prompt_in2, | |
455 | ps_out = self.prompt_out, |
|
455 | ps_out = self.prompt_out, | |
456 | pad_left = self.prompts_pad_left |
|
456 | pad_left = self.prompts_pad_left | |
457 | ) |
|
457 | ) | |
458 | # This is a context manager that installs/revmoes the displayhook at |
|
458 | # This is a context manager that installs/revmoes the displayhook at | |
459 | # the appropriate time. |
|
459 | # the appropriate time. | |
460 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
460 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
461 |
|
461 | |||
462 | def init_reload_doctest(self): |
|
462 | def init_reload_doctest(self): | |
463 | # Do a proper resetting of doctest, including the necessary displayhook |
|
463 | # Do a proper resetting of doctest, including the necessary displayhook | |
464 | # monkeypatching |
|
464 | # monkeypatching | |
465 | try: |
|
465 | try: | |
466 | doctest_reload() |
|
466 | doctest_reload() | |
467 | except ImportError: |
|
467 | except ImportError: | |
468 | warn("doctest module does not exist.") |
|
468 | warn("doctest module does not exist.") | |
469 |
|
469 | |||
470 | #------------------------------------------------------------------------- |
|
470 | #------------------------------------------------------------------------- | |
471 | # Things related to injections into the sys module |
|
471 | # Things related to injections into the sys module | |
472 | #------------------------------------------------------------------------- |
|
472 | #------------------------------------------------------------------------- | |
473 |
|
473 | |||
474 | def save_sys_module_state(self): |
|
474 | def save_sys_module_state(self): | |
475 | """Save the state of hooks in the sys module. |
|
475 | """Save the state of hooks in the sys module. | |
476 |
|
476 | |||
477 | This has to be called after self.user_ns is created. |
|
477 | This has to be called after self.user_ns is created. | |
478 | """ |
|
478 | """ | |
479 | self._orig_sys_module_state = {} |
|
479 | self._orig_sys_module_state = {} | |
480 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
480 | self._orig_sys_module_state['stdin'] = sys.stdin | |
481 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
481 | self._orig_sys_module_state['stdout'] = sys.stdout | |
482 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
482 | self._orig_sys_module_state['stderr'] = sys.stderr | |
483 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
483 | self._orig_sys_module_state['excepthook'] = sys.excepthook | |
484 | try: |
|
484 | try: | |
485 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
485 | self._orig_sys_modules_main_name = self.user_ns['__name__'] | |
486 | except KeyError: |
|
486 | except KeyError: | |
487 | pass |
|
487 | pass | |
488 |
|
488 | |||
489 | def restore_sys_module_state(self): |
|
489 | def restore_sys_module_state(self): | |
490 | """Restore the state of the sys module.""" |
|
490 | """Restore the state of the sys module.""" | |
491 | try: |
|
491 | try: | |
492 | for k, v in self._orig_sys_module_state.items(): |
|
492 | for k, v in self._orig_sys_module_state.items(): | |
493 | setattr(sys, k, v) |
|
493 | setattr(sys, k, v) | |
494 | except AttributeError: |
|
494 | except AttributeError: | |
495 | pass |
|
495 | pass | |
496 | try: |
|
496 | try: | |
497 | delattr(sys, 'ipcompleter') |
|
497 | delattr(sys, 'ipcompleter') | |
498 | except AttributeError: |
|
498 | except AttributeError: | |
499 | pass |
|
499 | pass | |
500 | # Reset what what done in self.init_sys_modules |
|
500 | # Reset what what done in self.init_sys_modules | |
501 | try: |
|
501 | try: | |
502 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
502 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name | |
503 | except (AttributeError, KeyError): |
|
503 | except (AttributeError, KeyError): | |
504 | pass |
|
504 | pass | |
505 |
|
505 | |||
506 | #------------------------------------------------------------------------- |
|
506 | #------------------------------------------------------------------------- | |
507 | # Things related to hooks |
|
507 | # Things related to hooks | |
508 | #------------------------------------------------------------------------- |
|
508 | #------------------------------------------------------------------------- | |
509 |
|
509 | |||
510 | def init_hooks(self): |
|
510 | def init_hooks(self): | |
511 | # hooks holds pointers used for user-side customizations |
|
511 | # hooks holds pointers used for user-side customizations | |
512 | self.hooks = Struct() |
|
512 | self.hooks = Struct() | |
513 |
|
513 | |||
514 | self.strdispatchers = {} |
|
514 | self.strdispatchers = {} | |
515 |
|
515 | |||
516 | # Set all default hooks, defined in the IPython.hooks module. |
|
516 | # Set all default hooks, defined in the IPython.hooks module. | |
517 | hooks = IPython.core.hooks |
|
517 | hooks = IPython.core.hooks | |
518 | for hook_name in hooks.__all__: |
|
518 | for hook_name in hooks.__all__: | |
519 | # default hooks have priority 100, i.e. low; user hooks should have |
|
519 | # default hooks have priority 100, i.e. low; user hooks should have | |
520 | # 0-100 priority |
|
520 | # 0-100 priority | |
521 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
521 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
522 |
|
522 | |||
523 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
523 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
524 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
524 | """set_hook(name,hook) -> sets an internal IPython hook. | |
525 |
|
525 | |||
526 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
526 | IPython exposes some of its internal API as user-modifiable hooks. By | |
527 | adding your function to one of these hooks, you can modify IPython's |
|
527 | adding your function to one of these hooks, you can modify IPython's | |
528 | behavior to call at runtime your own routines.""" |
|
528 | behavior to call at runtime your own routines.""" | |
529 |
|
529 | |||
530 | # At some point in the future, this should validate the hook before it |
|
530 | # At some point in the future, this should validate the hook before it | |
531 | # accepts it. Probably at least check that the hook takes the number |
|
531 | # accepts it. Probably at least check that the hook takes the number | |
532 | # of args it's supposed to. |
|
532 | # of args it's supposed to. | |
533 |
|
533 | |||
534 | f = new.instancemethod(hook,self,self.__class__) |
|
534 | f = new.instancemethod(hook,self,self.__class__) | |
535 |
|
535 | |||
536 | # check if the hook is for strdispatcher first |
|
536 | # check if the hook is for strdispatcher first | |
537 | if str_key is not None: |
|
537 | if str_key is not None: | |
538 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
538 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
539 | sdp.add_s(str_key, f, priority ) |
|
539 | sdp.add_s(str_key, f, priority ) | |
540 | self.strdispatchers[name] = sdp |
|
540 | self.strdispatchers[name] = sdp | |
541 | return |
|
541 | return | |
542 | if re_key is not None: |
|
542 | if re_key is not None: | |
543 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
543 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
544 | sdp.add_re(re.compile(re_key), f, priority ) |
|
544 | sdp.add_re(re.compile(re_key), f, priority ) | |
545 | self.strdispatchers[name] = sdp |
|
545 | self.strdispatchers[name] = sdp | |
546 | return |
|
546 | return | |
547 |
|
547 | |||
548 | dp = getattr(self.hooks, name, None) |
|
548 | dp = getattr(self.hooks, name, None) | |
549 | if name not in IPython.core.hooks.__all__: |
|
549 | if name not in IPython.core.hooks.__all__: | |
550 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) |
|
550 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) | |
551 | if not dp: |
|
551 | if not dp: | |
552 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
552 | dp = IPython.core.hooks.CommandChainDispatcher() | |
553 |
|
553 | |||
554 | try: |
|
554 | try: | |
555 | dp.add(f,priority) |
|
555 | dp.add(f,priority) | |
556 | except AttributeError: |
|
556 | except AttributeError: | |
557 | # it was not commandchain, plain old func - replace |
|
557 | # it was not commandchain, plain old func - replace | |
558 | dp = f |
|
558 | dp = f | |
559 |
|
559 | |||
560 | setattr(self.hooks,name, dp) |
|
560 | setattr(self.hooks,name, dp) | |
561 |
|
561 | |||
562 | #------------------------------------------------------------------------- |
|
562 | #------------------------------------------------------------------------- | |
563 | # Things related to the "main" module |
|
563 | # Things related to the "main" module | |
564 | #------------------------------------------------------------------------- |
|
564 | #------------------------------------------------------------------------- | |
565 |
|
565 | |||
566 | def new_main_mod(self,ns=None): |
|
566 | def new_main_mod(self,ns=None): | |
567 | """Return a new 'main' module object for user code execution. |
|
567 | """Return a new 'main' module object for user code execution. | |
568 | """ |
|
568 | """ | |
569 | main_mod = self._user_main_module |
|
569 | main_mod = self._user_main_module | |
570 | init_fakemod_dict(main_mod,ns) |
|
570 | init_fakemod_dict(main_mod,ns) | |
571 | return main_mod |
|
571 | return main_mod | |
572 |
|
572 | |||
573 | def cache_main_mod(self,ns,fname): |
|
573 | def cache_main_mod(self,ns,fname): | |
574 | """Cache a main module's namespace. |
|
574 | """Cache a main module's namespace. | |
575 |
|
575 | |||
576 | When scripts are executed via %run, we must keep a reference to the |
|
576 | When scripts are executed via %run, we must keep a reference to the | |
577 | namespace of their __main__ module (a FakeModule instance) around so |
|
577 | namespace of their __main__ module (a FakeModule instance) around so | |
578 | that Python doesn't clear it, rendering objects defined therein |
|
578 | that Python doesn't clear it, rendering objects defined therein | |
579 | useless. |
|
579 | useless. | |
580 |
|
580 | |||
581 | This method keeps said reference in a private dict, keyed by the |
|
581 | This method keeps said reference in a private dict, keyed by the | |
582 | absolute path of the module object (which corresponds to the script |
|
582 | absolute path of the module object (which corresponds to the script | |
583 | path). This way, for multiple executions of the same script we only |
|
583 | path). This way, for multiple executions of the same script we only | |
584 | keep one copy of the namespace (the last one), thus preventing memory |
|
584 | keep one copy of the namespace (the last one), thus preventing memory | |
585 | leaks from old references while allowing the objects from the last |
|
585 | leaks from old references while allowing the objects from the last | |
586 | execution to be accessible. |
|
586 | execution to be accessible. | |
587 |
|
587 | |||
588 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
588 | Note: we can not allow the actual FakeModule instances to be deleted, | |
589 | because of how Python tears down modules (it hard-sets all their |
|
589 | because of how Python tears down modules (it hard-sets all their | |
590 | references to None without regard for reference counts). This method |
|
590 | references to None without regard for reference counts). This method | |
591 | must therefore make a *copy* of the given namespace, to allow the |
|
591 | must therefore make a *copy* of the given namespace, to allow the | |
592 | original module's __dict__ to be cleared and reused. |
|
592 | original module's __dict__ to be cleared and reused. | |
593 |
|
593 | |||
594 |
|
594 | |||
595 | Parameters |
|
595 | Parameters | |
596 | ---------- |
|
596 | ---------- | |
597 | ns : a namespace (a dict, typically) |
|
597 | ns : a namespace (a dict, typically) | |
598 |
|
598 | |||
599 | fname : str |
|
599 | fname : str | |
600 | Filename associated with the namespace. |
|
600 | Filename associated with the namespace. | |
601 |
|
601 | |||
602 | Examples |
|
602 | Examples | |
603 | -------- |
|
603 | -------- | |
604 |
|
604 | |||
605 | In [10]: import IPython |
|
605 | In [10]: import IPython | |
606 |
|
606 | |||
607 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
607 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
608 |
|
608 | |||
609 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
609 | In [12]: IPython.__file__ in _ip._main_ns_cache | |
610 | Out[12]: True |
|
610 | Out[12]: True | |
611 | """ |
|
611 | """ | |
612 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
612 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() | |
613 |
|
613 | |||
614 | def clear_main_mod_cache(self): |
|
614 | def clear_main_mod_cache(self): | |
615 | """Clear the cache of main modules. |
|
615 | """Clear the cache of main modules. | |
616 |
|
616 | |||
617 | Mainly for use by utilities like %reset. |
|
617 | Mainly for use by utilities like %reset. | |
618 |
|
618 | |||
619 | Examples |
|
619 | Examples | |
620 | -------- |
|
620 | -------- | |
621 |
|
621 | |||
622 | In [15]: import IPython |
|
622 | In [15]: import IPython | |
623 |
|
623 | |||
624 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
624 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
625 |
|
625 | |||
626 | In [17]: len(_ip._main_ns_cache) > 0 |
|
626 | In [17]: len(_ip._main_ns_cache) > 0 | |
627 | Out[17]: True |
|
627 | Out[17]: True | |
628 |
|
628 | |||
629 | In [18]: _ip.clear_main_mod_cache() |
|
629 | In [18]: _ip.clear_main_mod_cache() | |
630 |
|
630 | |||
631 | In [19]: len(_ip._main_ns_cache) == 0 |
|
631 | In [19]: len(_ip._main_ns_cache) == 0 | |
632 | Out[19]: True |
|
632 | Out[19]: True | |
633 | """ |
|
633 | """ | |
634 | self._main_ns_cache.clear() |
|
634 | self._main_ns_cache.clear() | |
635 |
|
635 | |||
636 | #------------------------------------------------------------------------- |
|
636 | #------------------------------------------------------------------------- | |
637 | # Things related to debugging |
|
637 | # Things related to debugging | |
638 | #------------------------------------------------------------------------- |
|
638 | #------------------------------------------------------------------------- | |
639 |
|
639 | |||
640 | def init_pdb(self): |
|
640 | def init_pdb(self): | |
641 | # Set calling of pdb on exceptions |
|
641 | # Set calling of pdb on exceptions | |
642 | # self.call_pdb is a property |
|
642 | # self.call_pdb is a property | |
643 | self.call_pdb = self.pdb |
|
643 | self.call_pdb = self.pdb | |
644 |
|
644 | |||
645 | def _get_call_pdb(self): |
|
645 | def _get_call_pdb(self): | |
646 | return self._call_pdb |
|
646 | return self._call_pdb | |
647 |
|
647 | |||
648 | def _set_call_pdb(self,val): |
|
648 | def _set_call_pdb(self,val): | |
649 |
|
649 | |||
650 | if val not in (0,1,False,True): |
|
650 | if val not in (0,1,False,True): | |
651 | raise ValueError,'new call_pdb value must be boolean' |
|
651 | raise ValueError,'new call_pdb value must be boolean' | |
652 |
|
652 | |||
653 | # store value in instance |
|
653 | # store value in instance | |
654 | self._call_pdb = val |
|
654 | self._call_pdb = val | |
655 |
|
655 | |||
656 | # notify the actual exception handlers |
|
656 | # notify the actual exception handlers | |
657 | self.InteractiveTB.call_pdb = val |
|
657 | self.InteractiveTB.call_pdb = val | |
658 |
|
658 | |||
659 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
659 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
660 | 'Control auto-activation of pdb at exceptions') |
|
660 | 'Control auto-activation of pdb at exceptions') | |
661 |
|
661 | |||
662 | def debugger(self,force=False): |
|
662 | def debugger(self,force=False): | |
663 | """Call the pydb/pdb debugger. |
|
663 | """Call the pydb/pdb debugger. | |
664 |
|
664 | |||
665 | Keywords: |
|
665 | Keywords: | |
666 |
|
666 | |||
667 | - force(False): by default, this routine checks the instance call_pdb |
|
667 | - force(False): by default, this routine checks the instance call_pdb | |
668 | flag and does not actually invoke the debugger if the flag is false. |
|
668 | flag and does not actually invoke the debugger if the flag is false. | |
669 | The 'force' option forces the debugger to activate even if the flag |
|
669 | The 'force' option forces the debugger to activate even if the flag | |
670 | is false. |
|
670 | is false. | |
671 | """ |
|
671 | """ | |
672 |
|
672 | |||
673 | if not (force or self.call_pdb): |
|
673 | if not (force or self.call_pdb): | |
674 | return |
|
674 | return | |
675 |
|
675 | |||
676 | if not hasattr(sys,'last_traceback'): |
|
676 | if not hasattr(sys,'last_traceback'): | |
677 | error('No traceback has been produced, nothing to debug.') |
|
677 | error('No traceback has been produced, nothing to debug.') | |
678 | return |
|
678 | return | |
679 |
|
679 | |||
680 | # use pydb if available |
|
680 | # use pydb if available | |
681 | if debugger.has_pydb: |
|
681 | if debugger.has_pydb: | |
682 | from pydb import pm |
|
682 | from pydb import pm | |
683 | else: |
|
683 | else: | |
684 | # fallback to our internal debugger |
|
684 | # fallback to our internal debugger | |
685 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
685 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
686 | self.history_saving_wrapper(pm)() |
|
686 | self.history_saving_wrapper(pm)() | |
687 |
|
687 | |||
688 | #------------------------------------------------------------------------- |
|
688 | #------------------------------------------------------------------------- | |
689 | # Things related to IPython's various namespaces |
|
689 | # Things related to IPython's various namespaces | |
690 | #------------------------------------------------------------------------- |
|
690 | #------------------------------------------------------------------------- | |
691 |
|
691 | |||
692 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
692 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): | |
693 | # Create the namespace where the user will operate. user_ns is |
|
693 | # Create the namespace where the user will operate. user_ns is | |
694 | # normally the only one used, and it is passed to the exec calls as |
|
694 | # normally the only one used, and it is passed to the exec calls as | |
695 | # the locals argument. But we do carry a user_global_ns namespace |
|
695 | # the locals argument. But we do carry a user_global_ns namespace | |
696 | # given as the exec 'globals' argument, This is useful in embedding |
|
696 | # given as the exec 'globals' argument, This is useful in embedding | |
697 | # situations where the ipython shell opens in a context where the |
|
697 | # situations where the ipython shell opens in a context where the | |
698 | # distinction between locals and globals is meaningful. For |
|
698 | # distinction between locals and globals is meaningful. For | |
699 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
699 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
700 |
|
700 | |||
701 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
701 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
702 | # level as a dict instead of a module. This is a manual fix, but I |
|
702 | # level as a dict instead of a module. This is a manual fix, but I | |
703 | # should really track down where the problem is coming from. Alex |
|
703 | # should really track down where the problem is coming from. Alex | |
704 | # Schmolck reported this problem first. |
|
704 | # Schmolck reported this problem first. | |
705 |
|
705 | |||
706 | # A useful post by Alex Martelli on this topic: |
|
706 | # A useful post by Alex Martelli on this topic: | |
707 | # Re: inconsistent value from __builtins__ |
|
707 | # Re: inconsistent value from __builtins__ | |
708 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
708 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
709 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
709 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
710 | # Gruppen: comp.lang.python |
|
710 | # Gruppen: comp.lang.python | |
711 |
|
711 | |||
712 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
712 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
713 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
713 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
714 | # > <type 'dict'> |
|
714 | # > <type 'dict'> | |
715 | # > >>> print type(__builtins__) |
|
715 | # > >>> print type(__builtins__) | |
716 | # > <type 'module'> |
|
716 | # > <type 'module'> | |
717 | # > Is this difference in return value intentional? |
|
717 | # > Is this difference in return value intentional? | |
718 |
|
718 | |||
719 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
719 | # Well, it's documented that '__builtins__' can be either a dictionary | |
720 | # or a module, and it's been that way for a long time. Whether it's |
|
720 | # or a module, and it's been that way for a long time. Whether it's | |
721 | # intentional (or sensible), I don't know. In any case, the idea is |
|
721 | # intentional (or sensible), I don't know. In any case, the idea is | |
722 | # that if you need to access the built-in namespace directly, you |
|
722 | # that if you need to access the built-in namespace directly, you | |
723 | # should start with "import __builtin__" (note, no 's') which will |
|
723 | # should start with "import __builtin__" (note, no 's') which will | |
724 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
724 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
725 |
|
725 | |||
726 | # These routines return properly built dicts as needed by the rest of |
|
726 | # These routines return properly built dicts as needed by the rest of | |
727 | # the code, and can also be used by extension writers to generate |
|
727 | # the code, and can also be used by extension writers to generate | |
728 | # properly initialized namespaces. |
|
728 | # properly initialized namespaces. | |
729 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns) |
|
729 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns) | |
730 |
|
730 | |||
731 | # Assign namespaces |
|
731 | # Assign namespaces | |
732 | # This is the namespace where all normal user variables live |
|
732 | # This is the namespace where all normal user variables live | |
733 | self.user_ns = user_ns |
|
733 | self.user_ns = user_ns | |
734 | self.user_global_ns = user_global_ns |
|
734 | self.user_global_ns = user_global_ns | |
735 |
|
735 | |||
736 | # An auxiliary namespace that checks what parts of the user_ns were |
|
736 | # An auxiliary namespace that checks what parts of the user_ns were | |
737 | # loaded at startup, so we can list later only variables defined in |
|
737 | # loaded at startup, so we can list later only variables defined in | |
738 | # actual interactive use. Since it is always a subset of user_ns, it |
|
738 | # actual interactive use. Since it is always a subset of user_ns, it | |
739 | # doesn't need to be separately tracked in the ns_table. |
|
739 | # doesn't need to be separately tracked in the ns_table. | |
740 | self.user_ns_hidden = {} |
|
740 | self.user_ns_hidden = {} | |
741 |
|
741 | |||
742 | # A namespace to keep track of internal data structures to prevent |
|
742 | # A namespace to keep track of internal data structures to prevent | |
743 | # them from cluttering user-visible stuff. Will be updated later |
|
743 | # them from cluttering user-visible stuff. Will be updated later | |
744 | self.internal_ns = {} |
|
744 | self.internal_ns = {} | |
745 |
|
745 | |||
746 | # Now that FakeModule produces a real module, we've run into a nasty |
|
746 | # Now that FakeModule produces a real module, we've run into a nasty | |
747 | # problem: after script execution (via %run), the module where the user |
|
747 | # problem: after script execution (via %run), the module where the user | |
748 | # code ran is deleted. Now that this object is a true module (needed |
|
748 | # code ran is deleted. Now that this object is a true module (needed | |
749 | # so docetst and other tools work correctly), the Python module |
|
749 | # so docetst and other tools work correctly), the Python module | |
750 | # teardown mechanism runs over it, and sets to None every variable |
|
750 | # teardown mechanism runs over it, and sets to None every variable | |
751 | # present in that module. Top-level references to objects from the |
|
751 | # present in that module. Top-level references to objects from the | |
752 | # script survive, because the user_ns is updated with them. However, |
|
752 | # script survive, because the user_ns is updated with them. However, | |
753 | # calling functions defined in the script that use other things from |
|
753 | # calling functions defined in the script that use other things from | |
754 | # the script will fail, because the function's closure had references |
|
754 | # the script will fail, because the function's closure had references | |
755 | # to the original objects, which are now all None. So we must protect |
|
755 | # to the original objects, which are now all None. So we must protect | |
756 | # these modules from deletion by keeping a cache. |
|
756 | # these modules from deletion by keeping a cache. | |
757 | # |
|
757 | # | |
758 | # To avoid keeping stale modules around (we only need the one from the |
|
758 | # To avoid keeping stale modules around (we only need the one from the | |
759 | # last run), we use a dict keyed with the full path to the script, so |
|
759 | # last run), we use a dict keyed with the full path to the script, so | |
760 | # only the last version of the module is held in the cache. Note, |
|
760 | # only the last version of the module is held in the cache. Note, | |
761 | # however, that we must cache the module *namespace contents* (their |
|
761 | # however, that we must cache the module *namespace contents* (their | |
762 | # __dict__). Because if we try to cache the actual modules, old ones |
|
762 | # __dict__). Because if we try to cache the actual modules, old ones | |
763 | # (uncached) could be destroyed while still holding references (such as |
|
763 | # (uncached) could be destroyed while still holding references (such as | |
764 | # those held by GUI objects that tend to be long-lived)> |
|
764 | # those held by GUI objects that tend to be long-lived)> | |
765 | # |
|
765 | # | |
766 | # The %reset command will flush this cache. See the cache_main_mod() |
|
766 | # The %reset command will flush this cache. See the cache_main_mod() | |
767 | # and clear_main_mod_cache() methods for details on use. |
|
767 | # and clear_main_mod_cache() methods for details on use. | |
768 |
|
768 | |||
769 | # This is the cache used for 'main' namespaces |
|
769 | # This is the cache used for 'main' namespaces | |
770 | self._main_ns_cache = {} |
|
770 | self._main_ns_cache = {} | |
771 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
771 | # And this is the single instance of FakeModule whose __dict__ we keep | |
772 | # copying and clearing for reuse on each %run |
|
772 | # copying and clearing for reuse on each %run | |
773 | self._user_main_module = FakeModule() |
|
773 | self._user_main_module = FakeModule() | |
774 |
|
774 | |||
775 | # A table holding all the namespaces IPython deals with, so that |
|
775 | # A table holding all the namespaces IPython deals with, so that | |
776 | # introspection facilities can search easily. |
|
776 | # introspection facilities can search easily. | |
777 | self.ns_table = {'user':user_ns, |
|
777 | self.ns_table = {'user':user_ns, | |
778 | 'user_global':user_global_ns, |
|
778 | 'user_global':user_global_ns, | |
779 | 'internal':self.internal_ns, |
|
779 | 'internal':self.internal_ns, | |
780 | 'builtin':__builtin__.__dict__ |
|
780 | 'builtin':__builtin__.__dict__ | |
781 | } |
|
781 | } | |
782 |
|
782 | |||
783 | # Similarly, track all namespaces where references can be held and that |
|
783 | # Similarly, track all namespaces where references can be held and that | |
784 | # we can safely clear (so it can NOT include builtin). This one can be |
|
784 | # we can safely clear (so it can NOT include builtin). This one can be | |
785 | # a simple list. |
|
785 | # a simple list. | |
786 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden, |
|
786 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden, | |
787 | self.internal_ns, self._main_ns_cache ] |
|
787 | self.internal_ns, self._main_ns_cache ] | |
788 |
|
788 | |||
789 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
789 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): | |
790 | """Return a valid local and global user interactive namespaces. |
|
790 | """Return a valid local and global user interactive namespaces. | |
791 |
|
791 | |||
792 | This builds a dict with the minimal information needed to operate as a |
|
792 | This builds a dict with the minimal information needed to operate as a | |
793 | valid IPython user namespace, which you can pass to the various |
|
793 | valid IPython user namespace, which you can pass to the various | |
794 | embedding classes in ipython. The default implementation returns the |
|
794 | embedding classes in ipython. The default implementation returns the | |
795 | same dict for both the locals and the globals to allow functions to |
|
795 | same dict for both the locals and the globals to allow functions to | |
796 | refer to variables in the namespace. Customized implementations can |
|
796 | refer to variables in the namespace. Customized implementations can | |
797 | return different dicts. The locals dictionary can actually be anything |
|
797 | return different dicts. The locals dictionary can actually be anything | |
798 | following the basic mapping protocol of a dict, but the globals dict |
|
798 | following the basic mapping protocol of a dict, but the globals dict | |
799 | must be a true dict, not even a subclass. It is recommended that any |
|
799 | must be a true dict, not even a subclass. It is recommended that any | |
800 | custom object for the locals namespace synchronize with the globals |
|
800 | custom object for the locals namespace synchronize with the globals | |
801 | dict somehow. |
|
801 | dict somehow. | |
802 |
|
802 | |||
803 | Raises TypeError if the provided globals namespace is not a true dict. |
|
803 | Raises TypeError if the provided globals namespace is not a true dict. | |
804 |
|
804 | |||
805 | Parameters |
|
805 | Parameters | |
806 | ---------- |
|
806 | ---------- | |
807 | user_ns : dict-like, optional |
|
807 | user_ns : dict-like, optional | |
808 | The current user namespace. The items in this namespace should |
|
808 | The current user namespace. The items in this namespace should | |
809 | be included in the output. If None, an appropriate blank |
|
809 | be included in the output. If None, an appropriate blank | |
810 | namespace should be created. |
|
810 | namespace should be created. | |
811 | user_global_ns : dict, optional |
|
811 | user_global_ns : dict, optional | |
812 | The current user global namespace. The items in this namespace |
|
812 | The current user global namespace. The items in this namespace | |
813 | should be included in the output. If None, an appropriate |
|
813 | should be included in the output. If None, an appropriate | |
814 | blank namespace should be created. |
|
814 | blank namespace should be created. | |
815 |
|
815 | |||
816 | Returns |
|
816 | Returns | |
817 | ------- |
|
817 | ------- | |
818 | A pair of dictionary-like object to be used as the local namespace |
|
818 | A pair of dictionary-like object to be used as the local namespace | |
819 | of the interpreter and a dict to be used as the global namespace. |
|
819 | of the interpreter and a dict to be used as the global namespace. | |
820 | """ |
|
820 | """ | |
821 |
|
821 | |||
822 |
|
822 | |||
823 | # We must ensure that __builtin__ (without the final 's') is always |
|
823 | # We must ensure that __builtin__ (without the final 's') is always | |
824 | # available and pointing to the __builtin__ *module*. For more details: |
|
824 | # available and pointing to the __builtin__ *module*. For more details: | |
825 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
825 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
826 |
|
826 | |||
827 | if user_ns is None: |
|
827 | if user_ns is None: | |
828 | # Set __name__ to __main__ to better match the behavior of the |
|
828 | # Set __name__ to __main__ to better match the behavior of the | |
829 | # normal interpreter. |
|
829 | # normal interpreter. | |
830 | user_ns = {'__name__' :'__main__', |
|
830 | user_ns = {'__name__' :'__main__', | |
831 | '__builtin__' : __builtin__, |
|
831 | '__builtin__' : __builtin__, | |
832 | '__builtins__' : __builtin__, |
|
832 | '__builtins__' : __builtin__, | |
833 | } |
|
833 | } | |
834 | else: |
|
834 | else: | |
835 | user_ns.setdefault('__name__','__main__') |
|
835 | user_ns.setdefault('__name__','__main__') | |
836 | user_ns.setdefault('__builtin__',__builtin__) |
|
836 | user_ns.setdefault('__builtin__',__builtin__) | |
837 | user_ns.setdefault('__builtins__',__builtin__) |
|
837 | user_ns.setdefault('__builtins__',__builtin__) | |
838 |
|
838 | |||
839 | if user_global_ns is None: |
|
839 | if user_global_ns is None: | |
840 | user_global_ns = user_ns |
|
840 | user_global_ns = user_ns | |
841 | if type(user_global_ns) is not dict: |
|
841 | if type(user_global_ns) is not dict: | |
842 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
842 | raise TypeError("user_global_ns must be a true dict; got %r" | |
843 | % type(user_global_ns)) |
|
843 | % type(user_global_ns)) | |
844 |
|
844 | |||
845 | return user_ns, user_global_ns |
|
845 | return user_ns, user_global_ns | |
846 |
|
846 | |||
847 | def init_sys_modules(self): |
|
847 | def init_sys_modules(self): | |
848 | # We need to insert into sys.modules something that looks like a |
|
848 | # We need to insert into sys.modules something that looks like a | |
849 | # module but which accesses the IPython namespace, for shelve and |
|
849 | # module but which accesses the IPython namespace, for shelve and | |
850 | # pickle to work interactively. Normally they rely on getting |
|
850 | # pickle to work interactively. Normally they rely on getting | |
851 | # everything out of __main__, but for embedding purposes each IPython |
|
851 | # everything out of __main__, but for embedding purposes each IPython | |
852 | # instance has its own private namespace, so we can't go shoving |
|
852 | # instance has its own private namespace, so we can't go shoving | |
853 | # everything into __main__. |
|
853 | # everything into __main__. | |
854 |
|
854 | |||
855 | # note, however, that we should only do this for non-embedded |
|
855 | # note, however, that we should only do this for non-embedded | |
856 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
856 | # ipythons, which really mimic the __main__.__dict__ with their own | |
857 | # namespace. Embedded instances, on the other hand, should not do |
|
857 | # namespace. Embedded instances, on the other hand, should not do | |
858 | # this because they need to manage the user local/global namespaces |
|
858 | # this because they need to manage the user local/global namespaces | |
859 | # only, but they live within a 'normal' __main__ (meaning, they |
|
859 | # only, but they live within a 'normal' __main__ (meaning, they | |
860 | # shouldn't overtake the execution environment of the script they're |
|
860 | # shouldn't overtake the execution environment of the script they're | |
861 | # embedded in). |
|
861 | # embedded in). | |
862 |
|
862 | |||
863 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
863 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
864 |
|
864 | |||
865 | try: |
|
865 | try: | |
866 | main_name = self.user_ns['__name__'] |
|
866 | main_name = self.user_ns['__name__'] | |
867 | except KeyError: |
|
867 | except KeyError: | |
868 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
868 | raise KeyError('user_ns dictionary MUST have a "__name__" key') | |
869 | else: |
|
869 | else: | |
870 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
870 | sys.modules[main_name] = FakeModule(self.user_ns) | |
871 |
|
871 | |||
872 | def init_user_ns(self): |
|
872 | def init_user_ns(self): | |
873 | """Initialize all user-visible namespaces to their minimum defaults. |
|
873 | """Initialize all user-visible namespaces to their minimum defaults. | |
874 |
|
874 | |||
875 | Certain history lists are also initialized here, as they effectively |
|
875 | Certain history lists are also initialized here, as they effectively | |
876 | act as user namespaces. |
|
876 | act as user namespaces. | |
877 |
|
877 | |||
878 | Notes |
|
878 | Notes | |
879 | ----- |
|
879 | ----- | |
880 | All data structures here are only filled in, they are NOT reset by this |
|
880 | All data structures here are only filled in, they are NOT reset by this | |
881 | method. If they were not empty before, data will simply be added to |
|
881 | method. If they were not empty before, data will simply be added to | |
882 | therm. |
|
882 | therm. | |
883 | """ |
|
883 | """ | |
884 | # This function works in two parts: first we put a few things in |
|
884 | # This function works in two parts: first we put a few things in | |
885 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
885 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
886 | # initial variables aren't shown by %who. After the sync, we add the |
|
886 | # initial variables aren't shown by %who. After the sync, we add the | |
887 | # rest of what we *do* want the user to see with %who even on a new |
|
887 | # rest of what we *do* want the user to see with %who even on a new | |
888 | # session (probably nothing, so theye really only see their own stuff) |
|
888 | # session (probably nothing, so theye really only see their own stuff) | |
889 |
|
889 | |||
890 | # The user dict must *always* have a __builtin__ reference to the |
|
890 | # The user dict must *always* have a __builtin__ reference to the | |
891 | # Python standard __builtin__ namespace, which must be imported. |
|
891 | # Python standard __builtin__ namespace, which must be imported. | |
892 | # This is so that certain operations in prompt evaluation can be |
|
892 | # This is so that certain operations in prompt evaluation can be | |
893 | # reliably executed with builtins. Note that we can NOT use |
|
893 | # reliably executed with builtins. Note that we can NOT use | |
894 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
894 | # __builtins__ (note the 's'), because that can either be a dict or a | |
895 | # module, and can even mutate at runtime, depending on the context |
|
895 | # module, and can even mutate at runtime, depending on the context | |
896 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
896 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
897 | # always a module object, though it must be explicitly imported. |
|
897 | # always a module object, though it must be explicitly imported. | |
898 |
|
898 | |||
899 | # For more details: |
|
899 | # For more details: | |
900 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
900 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
901 | ns = dict(__builtin__ = __builtin__) |
|
901 | ns = dict(__builtin__ = __builtin__) | |
902 |
|
902 | |||
903 | # Put 'help' in the user namespace |
|
903 | # Put 'help' in the user namespace | |
904 | try: |
|
904 | try: | |
905 | from site import _Helper |
|
905 | from site import _Helper | |
906 | ns['help'] = _Helper() |
|
906 | ns['help'] = _Helper() | |
907 | except ImportError: |
|
907 | except ImportError: | |
908 | warn('help() not available - check site.py') |
|
908 | warn('help() not available - check site.py') | |
909 |
|
909 | |||
910 | # make global variables for user access to the histories |
|
910 | # make global variables for user access to the histories | |
911 | ns['_ih'] = self.input_hist |
|
911 | ns['_ih'] = self.input_hist | |
912 | ns['_oh'] = self.output_hist |
|
912 | ns['_oh'] = self.output_hist | |
913 | ns['_dh'] = self.dir_hist |
|
913 | ns['_dh'] = self.dir_hist | |
914 |
|
914 | |||
915 | ns['_sh'] = shadowns |
|
915 | ns['_sh'] = shadowns | |
916 |
|
916 | |||
917 | # user aliases to input and output histories. These shouldn't show up |
|
917 | # user aliases to input and output histories. These shouldn't show up | |
918 | # in %who, as they can have very large reprs. |
|
918 | # in %who, as they can have very large reprs. | |
919 | ns['In'] = self.input_hist |
|
919 | ns['In'] = self.input_hist | |
920 | ns['Out'] = self.output_hist |
|
920 | ns['Out'] = self.output_hist | |
921 |
|
921 | |||
922 | # Store myself as the public api!!! |
|
922 | # Store myself as the public api!!! | |
923 | ns['get_ipython'] = self.get_ipython |
|
923 | ns['get_ipython'] = self.get_ipython | |
924 |
|
924 | |||
925 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
925 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
926 | # by %who |
|
926 | # by %who | |
927 | self.user_ns_hidden.update(ns) |
|
927 | self.user_ns_hidden.update(ns) | |
928 |
|
928 | |||
929 | # Anything put into ns now would show up in %who. Think twice before |
|
929 | # Anything put into ns now would show up in %who. Think twice before | |
930 | # putting anything here, as we really want %who to show the user their |
|
930 | # putting anything here, as we really want %who to show the user their | |
931 | # stuff, not our variables. |
|
931 | # stuff, not our variables. | |
932 |
|
932 | |||
933 | # Finally, update the real user's namespace |
|
933 | # Finally, update the real user's namespace | |
934 | self.user_ns.update(ns) |
|
934 | self.user_ns.update(ns) | |
935 |
|
935 | |||
936 |
|
936 | |||
937 | def reset(self): |
|
937 | def reset(self): | |
938 | """Clear all internal namespaces. |
|
938 | """Clear all internal namespaces. | |
939 |
|
939 | |||
940 | Note that this is much more aggressive than %reset, since it clears |
|
940 | Note that this is much more aggressive than %reset, since it clears | |
941 | fully all namespaces, as well as all input/output lists. |
|
941 | fully all namespaces, as well as all input/output lists. | |
942 | """ |
|
942 | """ | |
943 | for ns in self.ns_refs_table: |
|
943 | for ns in self.ns_refs_table: | |
944 | ns.clear() |
|
944 | ns.clear() | |
945 |
|
945 | |||
946 | self.alias_manager.clear_aliases() |
|
946 | self.alias_manager.clear_aliases() | |
947 |
|
947 | |||
948 | # Clear input and output histories |
|
948 | # Clear input and output histories | |
949 | self.input_hist[:] = [] |
|
949 | self.input_hist[:] = [] | |
950 | self.input_hist_raw[:] = [] |
|
950 | self.input_hist_raw[:] = [] | |
951 | self.output_hist.clear() |
|
951 | self.output_hist.clear() | |
952 |
|
952 | |||
953 | # Restore the user namespaces to minimal usability |
|
953 | # Restore the user namespaces to minimal usability | |
954 | self.init_user_ns() |
|
954 | self.init_user_ns() | |
955 |
|
955 | |||
956 | # Restore the default and user aliases |
|
956 | # Restore the default and user aliases | |
957 | self.alias_manager.init_aliases() |
|
957 | self.alias_manager.init_aliases() | |
958 |
|
958 | |||
959 | def reset_selective(self, regex=None): |
|
959 | def reset_selective(self, regex=None): | |
960 | """Clear selective variables from internal namespaces based on a specified regular expression. |
|
960 | """Clear selective variables from internal namespaces based on a specified regular expression. | |
961 |
|
961 | |||
962 | Parameters |
|
962 | Parameters | |
963 | ---------- |
|
963 | ---------- | |
964 | regex : string or compiled pattern, optional |
|
964 | regex : string or compiled pattern, optional | |
965 | A regular expression pattern that will be used in searching variable names in the users |
|
965 | A regular expression pattern that will be used in searching variable names in the users | |
966 | namespaces. |
|
966 | namespaces. | |
967 | """ |
|
967 | """ | |
968 | if regex is not None: |
|
968 | if regex is not None: | |
969 | try: |
|
969 | try: | |
970 | m = re.compile(regex) |
|
970 | m = re.compile(regex) | |
971 | except TypeError: |
|
971 | except TypeError: | |
972 | raise TypeError('regex must be a string or compiled pattern') |
|
972 | raise TypeError('regex must be a string or compiled pattern') | |
973 | # Search for keys in each namespace that match the given regex |
|
973 | # Search for keys in each namespace that match the given regex | |
974 | # If a match is found, delete the key/value pair. |
|
974 | # If a match is found, delete the key/value pair. | |
975 | for ns in self.ns_refs_table: |
|
975 | for ns in self.ns_refs_table: | |
976 | for var in ns: |
|
976 | for var in ns: | |
977 | if m.search(var): |
|
977 | if m.search(var): | |
978 | del ns[var] |
|
978 | del ns[var] | |
979 |
|
979 | |||
980 | def push(self, variables, interactive=True): |
|
980 | def push(self, variables, interactive=True): | |
981 | """Inject a group of variables into the IPython user namespace. |
|
981 | """Inject a group of variables into the IPython user namespace. | |
982 |
|
982 | |||
983 | Parameters |
|
983 | Parameters | |
984 | ---------- |
|
984 | ---------- | |
985 | variables : dict, str or list/tuple of str |
|
985 | variables : dict, str or list/tuple of str | |
986 | The variables to inject into the user's namespace. If a dict, |
|
986 | The variables to inject into the user's namespace. If a dict, | |
987 | a simple update is done. If a str, the string is assumed to |
|
987 | a simple update is done. If a str, the string is assumed to | |
988 | have variable names separated by spaces. A list/tuple of str |
|
988 | have variable names separated by spaces. A list/tuple of str | |
989 | can also be used to give the variable names. If just the variable |
|
989 | can also be used to give the variable names. If just the variable | |
990 | names are give (list/tuple/str) then the variable values looked |
|
990 | names are give (list/tuple/str) then the variable values looked | |
991 | up in the callers frame. |
|
991 | up in the callers frame. | |
992 | interactive : bool |
|
992 | interactive : bool | |
993 | If True (default), the variables will be listed with the ``who`` |
|
993 | If True (default), the variables will be listed with the ``who`` | |
994 | magic. |
|
994 | magic. | |
995 | """ |
|
995 | """ | |
996 | vdict = None |
|
996 | vdict = None | |
997 |
|
997 | |||
998 | # We need a dict of name/value pairs to do namespace updates. |
|
998 | # We need a dict of name/value pairs to do namespace updates. | |
999 | if isinstance(variables, dict): |
|
999 | if isinstance(variables, dict): | |
1000 | vdict = variables |
|
1000 | vdict = variables | |
1001 | elif isinstance(variables, (basestring, list, tuple)): |
|
1001 | elif isinstance(variables, (basestring, list, tuple)): | |
1002 | if isinstance(variables, basestring): |
|
1002 | if isinstance(variables, basestring): | |
1003 | vlist = variables.split() |
|
1003 | vlist = variables.split() | |
1004 | else: |
|
1004 | else: | |
1005 | vlist = variables |
|
1005 | vlist = variables | |
1006 | vdict = {} |
|
1006 | vdict = {} | |
1007 | cf = sys._getframe(1) |
|
1007 | cf = sys._getframe(1) | |
1008 | for name in vlist: |
|
1008 | for name in vlist: | |
1009 | try: |
|
1009 | try: | |
1010 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1010 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1011 | except: |
|
1011 | except: | |
1012 | print ('Could not get variable %s from %s' % |
|
1012 | print ('Could not get variable %s from %s' % | |
1013 | (name,cf.f_code.co_name)) |
|
1013 | (name,cf.f_code.co_name)) | |
1014 | else: |
|
1014 | else: | |
1015 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1015 | raise ValueError('variables must be a dict/str/list/tuple') | |
1016 |
|
1016 | |||
1017 | # Propagate variables to user namespace |
|
1017 | # Propagate variables to user namespace | |
1018 | self.user_ns.update(vdict) |
|
1018 | self.user_ns.update(vdict) | |
1019 |
|
1019 | |||
1020 | # And configure interactive visibility |
|
1020 | # And configure interactive visibility | |
1021 | config_ns = self.user_ns_hidden |
|
1021 | config_ns = self.user_ns_hidden | |
1022 | if interactive: |
|
1022 | if interactive: | |
1023 | for name, val in vdict.iteritems(): |
|
1023 | for name, val in vdict.iteritems(): | |
1024 | config_ns.pop(name, None) |
|
1024 | config_ns.pop(name, None) | |
1025 | else: |
|
1025 | else: | |
1026 | for name,val in vdict.iteritems(): |
|
1026 | for name,val in vdict.iteritems(): | |
1027 | config_ns[name] = val |
|
1027 | config_ns[name] = val | |
1028 |
|
1028 | |||
1029 | #------------------------------------------------------------------------- |
|
1029 | #------------------------------------------------------------------------- | |
1030 | # Things related to history management |
|
1030 | # Things related to history management | |
1031 | #------------------------------------------------------------------------- |
|
1031 | #------------------------------------------------------------------------- | |
1032 |
|
1032 | |||
1033 | def init_history(self): |
|
1033 | def init_history(self): | |
1034 | # List of input with multi-line handling. |
|
1034 | # List of input with multi-line handling. | |
1035 | self.input_hist = InputList() |
|
1035 | self.input_hist = InputList() | |
1036 | # This one will hold the 'raw' input history, without any |
|
1036 | # This one will hold the 'raw' input history, without any | |
1037 | # pre-processing. This will allow users to retrieve the input just as |
|
1037 | # pre-processing. This will allow users to retrieve the input just as | |
1038 | # it was exactly typed in by the user, with %hist -r. |
|
1038 | # it was exactly typed in by the user, with %hist -r. | |
1039 | self.input_hist_raw = InputList() |
|
1039 | self.input_hist_raw = InputList() | |
1040 |
|
1040 | |||
1041 | # list of visited directories |
|
1041 | # list of visited directories | |
1042 | try: |
|
1042 | try: | |
1043 | self.dir_hist = [os.getcwd()] |
|
1043 | self.dir_hist = [os.getcwd()] | |
1044 | except OSError: |
|
1044 | except OSError: | |
1045 | self.dir_hist = [] |
|
1045 | self.dir_hist = [] | |
1046 |
|
1046 | |||
1047 | # dict of output history |
|
1047 | # dict of output history | |
1048 | self.output_hist = {} |
|
1048 | self.output_hist = {} | |
1049 |
|
1049 | |||
1050 | # Now the history file |
|
1050 | # Now the history file | |
1051 | if self.profile: |
|
1051 | if self.profile: | |
1052 | histfname = 'history-%s' % self.profile |
|
1052 | histfname = 'history-%s' % self.profile | |
1053 | else: |
|
1053 | else: | |
1054 | histfname = 'history' |
|
1054 | histfname = 'history' | |
1055 | self.histfile = os.path.join(self.ipython_dir, histfname) |
|
1055 | self.histfile = os.path.join(self.ipython_dir, histfname) | |
1056 |
|
1056 | |||
1057 | # Fill the history zero entry, user counter starts at 1 |
|
1057 | # Fill the history zero entry, user counter starts at 1 | |
1058 | self.input_hist.append('\n') |
|
1058 | self.input_hist.append('\n') | |
1059 | self.input_hist_raw.append('\n') |
|
1059 | self.input_hist_raw.append('\n') | |
1060 |
|
1060 | |||
1061 | def init_shadow_hist(self): |
|
1061 | def init_shadow_hist(self): | |
1062 | try: |
|
1062 | try: | |
1063 | self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db") |
|
1063 | self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db") | |
1064 | except exceptions.UnicodeDecodeError: |
|
1064 | except exceptions.UnicodeDecodeError: | |
1065 | print "Your ipython_dir can't be decoded to unicode!" |
|
1065 | print "Your ipython_dir can't be decoded to unicode!" | |
1066 | print "Please set HOME environment variable to something that" |
|
1066 | print "Please set HOME environment variable to something that" | |
1067 | print r"only has ASCII characters, e.g. c:\home" |
|
1067 | print r"only has ASCII characters, e.g. c:\home" | |
1068 | print "Now it is", self.ipython_dir |
|
1068 | print "Now it is", self.ipython_dir | |
1069 | sys.exit() |
|
1069 | sys.exit() | |
1070 | self.shadowhist = ipcorehist.ShadowHist(self.db) |
|
1070 | self.shadowhist = ipcorehist.ShadowHist(self.db) | |
1071 |
|
1071 | |||
1072 | def savehist(self): |
|
1072 | def savehist(self): | |
1073 | """Save input history to a file (via readline library).""" |
|
1073 | """Save input history to a file (via readline library).""" | |
1074 |
|
1074 | |||
1075 | try: |
|
1075 | try: | |
1076 | self.readline.write_history_file(self.histfile) |
|
1076 | self.readline.write_history_file(self.histfile) | |
1077 | except: |
|
1077 | except: | |
1078 | print 'Unable to save IPython command history to file: ' + \ |
|
1078 | print 'Unable to save IPython command history to file: ' + \ | |
1079 | `self.histfile` |
|
1079 | `self.histfile` | |
1080 |
|
1080 | |||
1081 | def reloadhist(self): |
|
1081 | def reloadhist(self): | |
1082 | """Reload the input history from disk file.""" |
|
1082 | """Reload the input history from disk file.""" | |
1083 |
|
1083 | |||
1084 | try: |
|
1084 | try: | |
1085 | self.readline.clear_history() |
|
1085 | self.readline.clear_history() | |
1086 | self.readline.read_history_file(self.shell.histfile) |
|
1086 | self.readline.read_history_file(self.shell.histfile) | |
1087 | except AttributeError: |
|
1087 | except AttributeError: | |
1088 | pass |
|
1088 | pass | |
1089 |
|
1089 | |||
1090 | def history_saving_wrapper(self, func): |
|
1090 | def history_saving_wrapper(self, func): | |
1091 | """ Wrap func for readline history saving |
|
1091 | """ Wrap func for readline history saving | |
1092 |
|
1092 | |||
1093 | Convert func into callable that saves & restores |
|
1093 | Convert func into callable that saves & restores | |
1094 | history around the call """ |
|
1094 | history around the call """ | |
1095 |
|
1095 | |||
1096 | if self.has_readline: |
|
1096 | if self.has_readline: | |
1097 | from IPython.utils import rlineimpl as readline |
|
1097 | from IPython.utils import rlineimpl as readline | |
1098 | else: |
|
1098 | else: | |
1099 | return func |
|
1099 | return func | |
1100 |
|
1100 | |||
1101 | def wrapper(): |
|
1101 | def wrapper(): | |
1102 | self.savehist() |
|
1102 | self.savehist() | |
1103 | try: |
|
1103 | try: | |
1104 | func() |
|
1104 | func() | |
1105 | finally: |
|
1105 | finally: | |
1106 | readline.read_history_file(self.histfile) |
|
1106 | readline.read_history_file(self.histfile) | |
1107 | return wrapper |
|
1107 | return wrapper | |
1108 |
|
1108 | |||
1109 | def get_history(self, index=None, raw=False, output=True): |
|
1109 | def get_history(self, index=None, raw=False, output=True): | |
1110 | """Get the history list. |
|
1110 | """Get the history list. | |
1111 |
|
1111 | |||
1112 | Get the input and output history. |
|
1112 | Get the input and output history. | |
1113 |
|
1113 | |||
1114 | Parameters |
|
1114 | Parameters | |
1115 | ---------- |
|
1115 | ---------- | |
1116 | index : n or (n1, n2) or None |
|
1116 | index : n or (n1, n2) or None | |
1117 | If n, then the last entries. If a tuple, then all in |
|
1117 | If n, then the last entries. If a tuple, then all in | |
1118 | range(n1, n2). If None, then all entries. Raises IndexError if |
|
1118 | range(n1, n2). If None, then all entries. Raises IndexError if | |
1119 | the format of index is incorrect. |
|
1119 | the format of index is incorrect. | |
1120 | raw : bool |
|
1120 | raw : bool | |
1121 | If True, return the raw input. |
|
1121 | If True, return the raw input. | |
1122 | output : bool |
|
1122 | output : bool | |
1123 | If True, then return the output as well. |
|
1123 | If True, then return the output as well. | |
1124 |
|
1124 | |||
1125 | Returns |
|
1125 | Returns | |
1126 | ------- |
|
1126 | ------- | |
1127 | If output is True, then return a dict of tuples, keyed by the prompt |
|
1127 | If output is True, then return a dict of tuples, keyed by the prompt | |
1128 | numbers and with values of (input, output). If output is False, then |
|
1128 | numbers and with values of (input, output). If output is False, then | |
1129 | a dict, keyed by the prompt number with the values of input. Raises |
|
1129 | a dict, keyed by the prompt number with the values of input. Raises | |
1130 | IndexError if no history is found. |
|
1130 | IndexError if no history is found. | |
1131 | """ |
|
1131 | """ | |
1132 | if raw: |
|
1132 | if raw: | |
1133 | input_hist = self.input_hist_raw |
|
1133 | input_hist = self.input_hist_raw | |
1134 | else: |
|
1134 | else: | |
1135 | input_hist = self.input_hist |
|
1135 | input_hist = self.input_hist | |
1136 | if output: |
|
1136 | if output: | |
1137 | output_hist = self.user_ns['Out'] |
|
1137 | output_hist = self.user_ns['Out'] | |
1138 | n = len(input_hist) |
|
1138 | n = len(input_hist) | |
1139 | if index is None: |
|
1139 | if index is None: | |
1140 | start=0; stop=n |
|
1140 | start=0; stop=n | |
1141 | elif isinstance(index, int): |
|
1141 | elif isinstance(index, int): | |
1142 | start=n-index; stop=n |
|
1142 | start=n-index; stop=n | |
1143 | elif isinstance(index, tuple) and len(index) == 2: |
|
1143 | elif isinstance(index, tuple) and len(index) == 2: | |
1144 | start=index[0]; stop=index[1] |
|
1144 | start=index[0]; stop=index[1] | |
1145 | else: |
|
1145 | else: | |
1146 | raise IndexError('Not a valid index for the input history: %r' % index) |
|
1146 | raise IndexError('Not a valid index for the input history: %r' % index) | |
1147 | hist = {} |
|
1147 | hist = {} | |
1148 | for i in range(start, stop): |
|
1148 | for i in range(start, stop): | |
1149 | if output: |
|
1149 | if output: | |
1150 | hist[i] = (input_hist[i], output_hist.get(i)) |
|
1150 | hist[i] = (input_hist[i], output_hist.get(i)) | |
1151 | else: |
|
1151 | else: | |
1152 | hist[i] = input_hist[i] |
|
1152 | hist[i] = input_hist[i] | |
1153 | if len(hist)==0: |
|
1153 | if len(hist)==0: | |
1154 | raise IndexError('No history for range of indices: %r' % index) |
|
1154 | raise IndexError('No history for range of indices: %r' % index) | |
1155 | return hist |
|
1155 | return hist | |
1156 |
|
1156 | |||
1157 | #------------------------------------------------------------------------- |
|
1157 | #------------------------------------------------------------------------- | |
1158 | # Things related to exception handling and tracebacks (not debugging) |
|
1158 | # Things related to exception handling and tracebacks (not debugging) | |
1159 | #------------------------------------------------------------------------- |
|
1159 | #------------------------------------------------------------------------- | |
1160 |
|
1160 | |||
1161 | def init_traceback_handlers(self, custom_exceptions): |
|
1161 | def init_traceback_handlers(self, custom_exceptions): | |
1162 | # Syntax error handler. |
|
1162 | # Syntax error handler. | |
1163 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1163 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | |
1164 |
|
1164 | |||
1165 | # The interactive one is initialized with an offset, meaning we always |
|
1165 | # The interactive one is initialized with an offset, meaning we always | |
1166 | # want to remove the topmost item in the traceback, which is our own |
|
1166 | # want to remove the topmost item in the traceback, which is our own | |
1167 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1167 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1168 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1168 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1169 | color_scheme='NoColor', |
|
1169 | color_scheme='NoColor', | |
1170 | tb_offset = 1) |
|
1170 | tb_offset = 1) | |
1171 |
|
1171 | |||
1172 | # The instance will store a pointer to the system-wide exception hook, |
|
1172 | # The instance will store a pointer to the system-wide exception hook, | |
1173 | # so that runtime code (such as magics) can access it. This is because |
|
1173 | # so that runtime code (such as magics) can access it. This is because | |
1174 | # during the read-eval loop, it may get temporarily overwritten. |
|
1174 | # during the read-eval loop, it may get temporarily overwritten. | |
1175 | self.sys_excepthook = sys.excepthook |
|
1175 | self.sys_excepthook = sys.excepthook | |
1176 |
|
1176 | |||
1177 | # and add any custom exception handlers the user may have specified |
|
1177 | # and add any custom exception handlers the user may have specified | |
1178 | self.set_custom_exc(*custom_exceptions) |
|
1178 | self.set_custom_exc(*custom_exceptions) | |
1179 |
|
1179 | |||
1180 | # Set the exception mode |
|
1180 | # Set the exception mode | |
1181 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1181 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1182 |
|
1182 | |||
1183 | def set_custom_exc(self, exc_tuple, handler): |
|
1183 | def set_custom_exc(self, exc_tuple, handler): | |
1184 | """set_custom_exc(exc_tuple,handler) |
|
1184 | """set_custom_exc(exc_tuple,handler) | |
1185 |
|
1185 | |||
1186 | Set a custom exception handler, which will be called if any of the |
|
1186 | Set a custom exception handler, which will be called if any of the | |
1187 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1187 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1188 | runcode() method. |
|
1188 | runcode() method. | |
1189 |
|
1189 | |||
1190 | Inputs: |
|
1190 | Inputs: | |
1191 |
|
1191 | |||
1192 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
1192 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
1193 | handler for. It is very important that you use a tuple, and NOT A |
|
1193 | handler for. It is very important that you use a tuple, and NOT A | |
1194 | LIST here, because of the way Python's except statement works. If |
|
1194 | LIST here, because of the way Python's except statement works. If | |
1195 | you only want to trap a single exception, use a singleton tuple: |
|
1195 | you only want to trap a single exception, use a singleton tuple: | |
1196 |
|
1196 | |||
1197 | exc_tuple == (MyCustomException,) |
|
1197 | exc_tuple == (MyCustomException,) | |
1198 |
|
1198 | |||
1199 | - handler: this must be defined as a function with the following |
|
1199 | - handler: this must be defined as a function with the following | |
1200 | basic interface:: |
|
1200 | basic interface:: | |
1201 |
|
1201 | |||
1202 | def my_handler(self, etype, value, tb, tb_offset=None) |
|
1202 | def my_handler(self, etype, value, tb, tb_offset=None) | |
1203 | ... |
|
1203 | ... | |
1204 | # The return value must be |
|
1204 | # The return value must be | |
1205 | return structured_traceback |
|
1205 | return structured_traceback | |
1206 |
|
1206 | |||
1207 | This will be made into an instance method (via new.instancemethod) |
|
1207 | This will be made into an instance method (via new.instancemethod) | |
1208 | of IPython itself, and it will be called if any of the exceptions |
|
1208 | of IPython itself, and it will be called if any of the exceptions | |
1209 | listed in the exc_tuple are caught. If the handler is None, an |
|
1209 | listed in the exc_tuple are caught. If the handler is None, an | |
1210 | internal basic one is used, which just prints basic info. |
|
1210 | internal basic one is used, which just prints basic info. | |
1211 |
|
1211 | |||
1212 | WARNING: by putting in your own exception handler into IPython's main |
|
1212 | WARNING: by putting in your own exception handler into IPython's main | |
1213 | execution loop, you run a very good chance of nasty crashes. This |
|
1213 | execution loop, you run a very good chance of nasty crashes. This | |
1214 | facility should only be used if you really know what you are doing.""" |
|
1214 | facility should only be used if you really know what you are doing.""" | |
1215 |
|
1215 | |||
1216 | assert type(exc_tuple)==type(()) , \ |
|
1216 | assert type(exc_tuple)==type(()) , \ | |
1217 | "The custom exceptions must be given AS A TUPLE." |
|
1217 | "The custom exceptions must be given AS A TUPLE." | |
1218 |
|
1218 | |||
1219 | def dummy_handler(self,etype,value,tb): |
|
1219 | def dummy_handler(self,etype,value,tb): | |
1220 | print '*** Simple custom exception handler ***' |
|
1220 | print '*** Simple custom exception handler ***' | |
1221 | print 'Exception type :',etype |
|
1221 | print 'Exception type :',etype | |
1222 | print 'Exception value:',value |
|
1222 | print 'Exception value:',value | |
1223 | print 'Traceback :',tb |
|
1223 | print 'Traceback :',tb | |
1224 | print 'Source code :','\n'.join(self.buffer) |
|
1224 | print 'Source code :','\n'.join(self.buffer) | |
1225 |
|
1225 | |||
1226 | if handler is None: handler = dummy_handler |
|
1226 | if handler is None: handler = dummy_handler | |
1227 |
|
1227 | |||
1228 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
1228 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
1229 | self.custom_exceptions = exc_tuple |
|
1229 | self.custom_exceptions = exc_tuple | |
1230 |
|
1230 | |||
1231 | def excepthook(self, etype, value, tb): |
|
1231 | def excepthook(self, etype, value, tb): | |
1232 | """One more defense for GUI apps that call sys.excepthook. |
|
1232 | """One more defense for GUI apps that call sys.excepthook. | |
1233 |
|
1233 | |||
1234 | GUI frameworks like wxPython trap exceptions and call |
|
1234 | GUI frameworks like wxPython trap exceptions and call | |
1235 | sys.excepthook themselves. I guess this is a feature that |
|
1235 | sys.excepthook themselves. I guess this is a feature that | |
1236 | enables them to keep running after exceptions that would |
|
1236 | enables them to keep running after exceptions that would | |
1237 | otherwise kill their mainloop. This is a bother for IPython |
|
1237 | otherwise kill their mainloop. This is a bother for IPython | |
1238 | which excepts to catch all of the program exceptions with a try: |
|
1238 | which excepts to catch all of the program exceptions with a try: | |
1239 | except: statement. |
|
1239 | except: statement. | |
1240 |
|
1240 | |||
1241 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1241 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1242 | any app directly invokes sys.excepthook, it will look to the user like |
|
1242 | any app directly invokes sys.excepthook, it will look to the user like | |
1243 | IPython crashed. In order to work around this, we can disable the |
|
1243 | IPython crashed. In order to work around this, we can disable the | |
1244 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1244 | CrashHandler and replace it with this excepthook instead, which prints a | |
1245 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1245 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1246 | call sys.excepthook will generate a regular-looking exception from |
|
1246 | call sys.excepthook will generate a regular-looking exception from | |
1247 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1247 | IPython, and the CrashHandler will only be triggered by real IPython | |
1248 | crashes. |
|
1248 | crashes. | |
1249 |
|
1249 | |||
1250 | This hook should be used sparingly, only in places which are not likely |
|
1250 | This hook should be used sparingly, only in places which are not likely | |
1251 | to be true IPython errors. |
|
1251 | to be true IPython errors. | |
1252 | """ |
|
1252 | """ | |
1253 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
1253 | self.showtraceback((etype,value,tb),tb_offset=0) | |
1254 |
|
1254 | |||
1255 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, |
|
1255 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None, | |
1256 | exception_only=False): |
|
1256 | exception_only=False): | |
1257 | """Display the exception that just occurred. |
|
1257 | """Display the exception that just occurred. | |
1258 |
|
1258 | |||
1259 | If nothing is known about the exception, this is the method which |
|
1259 | If nothing is known about the exception, this is the method which | |
1260 | should be used throughout the code for presenting user tracebacks, |
|
1260 | should be used throughout the code for presenting user tracebacks, | |
1261 | rather than directly invoking the InteractiveTB object. |
|
1261 | rather than directly invoking the InteractiveTB object. | |
1262 |
|
1262 | |||
1263 | A specific showsyntaxerror() also exists, but this method can take |
|
1263 | A specific showsyntaxerror() also exists, but this method can take | |
1264 | care of calling it if needed, so unless you are explicitly catching a |
|
1264 | care of calling it if needed, so unless you are explicitly catching a | |
1265 | SyntaxError exception, don't try to analyze the stack manually and |
|
1265 | SyntaxError exception, don't try to analyze the stack manually and | |
1266 | simply call this method.""" |
|
1266 | simply call this method.""" | |
1267 |
|
1267 | |||
1268 | try: |
|
1268 | try: | |
1269 | if exc_tuple is None: |
|
1269 | if exc_tuple is None: | |
1270 | etype, value, tb = sys.exc_info() |
|
1270 | etype, value, tb = sys.exc_info() | |
1271 | else: |
|
1271 | else: | |
1272 | etype, value, tb = exc_tuple |
|
1272 | etype, value, tb = exc_tuple | |
1273 |
|
1273 | |||
1274 | if etype is None: |
|
1274 | if etype is None: | |
1275 | if hasattr(sys, 'last_type'): |
|
1275 | if hasattr(sys, 'last_type'): | |
1276 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1276 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1277 | sys.last_traceback |
|
1277 | sys.last_traceback | |
1278 | else: |
|
1278 | else: | |
1279 | self.write_err('No traceback available to show.\n') |
|
1279 | self.write_err('No traceback available to show.\n') | |
1280 | return |
|
1280 | return | |
1281 |
|
1281 | |||
1282 | if etype is SyntaxError: |
|
1282 | if etype is SyntaxError: | |
1283 | # Though this won't be called by syntax errors in the input |
|
1283 | # Though this won't be called by syntax errors in the input | |
1284 | # line, there may be SyntaxError cases whith imported code. |
|
1284 | # line, there may be SyntaxError cases whith imported code. | |
1285 | self.showsyntaxerror(filename) |
|
1285 | self.showsyntaxerror(filename) | |
1286 | elif etype is UsageError: |
|
1286 | elif etype is UsageError: | |
1287 | print "UsageError:", value |
|
1287 | print "UsageError:", value | |
1288 | else: |
|
1288 | else: | |
1289 | # WARNING: these variables are somewhat deprecated and not |
|
1289 | # WARNING: these variables are somewhat deprecated and not | |
1290 | # necessarily safe to use in a threaded environment, but tools |
|
1290 | # necessarily safe to use in a threaded environment, but tools | |
1291 | # like pdb depend on their existence, so let's set them. If we |
|
1291 | # like pdb depend on their existence, so let's set them. If we | |
1292 | # find problems in the field, we'll need to revisit their use. |
|
1292 | # find problems in the field, we'll need to revisit their use. | |
1293 | sys.last_type = etype |
|
1293 | sys.last_type = etype | |
1294 | sys.last_value = value |
|
1294 | sys.last_value = value | |
1295 | sys.last_traceback = tb |
|
1295 | sys.last_traceback = tb | |
1296 |
|
1296 | |||
1297 | if etype in self.custom_exceptions: |
|
1297 | if etype in self.custom_exceptions: | |
1298 | # FIXME: Old custom traceback objects may just return a |
|
1298 | # FIXME: Old custom traceback objects may just return a | |
1299 | # string, in that case we just put it into a list |
|
1299 | # string, in that case we just put it into a list | |
1300 | stb = self.CustomTB(etype, value, tb, tb_offset) |
|
1300 | stb = self.CustomTB(etype, value, tb, tb_offset) | |
1301 | if isinstance(ctb, basestring): |
|
1301 | if isinstance(ctb, basestring): | |
1302 | stb = [stb] |
|
1302 | stb = [stb] | |
1303 | else: |
|
1303 | else: | |
1304 | if exception_only: |
|
1304 | if exception_only: | |
1305 | stb = ['An exception has occurred, use %tb to see ' |
|
1305 | stb = ['An exception has occurred, use %tb to see ' | |
1306 | 'the full traceback.'] |
|
1306 | 'the full traceback.\n'] | |
1307 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1307 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1308 | value)) |
|
1308 | value)) | |
1309 | else: |
|
1309 | else: | |
1310 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1310 | stb = self.InteractiveTB.structured_traceback(etype, | |
1311 | value, tb, tb_offset=tb_offset) |
|
1311 | value, tb, tb_offset=tb_offset) | |
1312 | # FIXME: the pdb calling should be done by us, not by |
|
1312 | # FIXME: the pdb calling should be done by us, not by | |
1313 | # the code computing the traceback. |
|
1313 | # the code computing the traceback. | |
1314 | if self.InteractiveTB.call_pdb: |
|
1314 | if self.InteractiveTB.call_pdb: | |
1315 | # pdb mucks up readline, fix it back |
|
1315 | # pdb mucks up readline, fix it back | |
1316 | self.set_completer() |
|
1316 | self.set_completer() | |
1317 |
|
1317 | |||
1318 | # Actually show the traceback |
|
1318 | # Actually show the traceback | |
1319 | self._showtraceback(etype, value, stb) |
|
1319 | self._showtraceback(etype, value, stb) | |
1320 |
|
1320 | |||
1321 | except KeyboardInterrupt: |
|
1321 | except KeyboardInterrupt: | |
1322 | self.write_err("\nKeyboardInterrupt\n") |
|
1322 | self.write_err("\nKeyboardInterrupt\n") | |
1323 |
|
1323 | |||
1324 | def _showtraceback(self, etype, evalue, stb): |
|
1324 | def _showtraceback(self, etype, evalue, stb): | |
1325 | """Actually show a traceback. |
|
1325 | """Actually show a traceback. | |
1326 |
|
1326 | |||
1327 | Subclasses may override this method to put the traceback on a different |
|
1327 | Subclasses may override this method to put the traceback on a different | |
1328 | place, like a side channel. |
|
1328 | place, like a side channel. | |
1329 | """ |
|
1329 | """ | |
1330 | self.write_err('\n'.join(stb)) |
|
1330 | # FIXME: this should use the proper write channels, but our test suite | |
|
1331 | # relies on it coming out of stdout... | |||
|
1332 | print >> sys.stdout, self.InteractiveTB.stb2text(stb) | |||
1331 |
|
1333 | |||
1332 | def showsyntaxerror(self, filename=None): |
|
1334 | def showsyntaxerror(self, filename=None): | |
1333 | """Display the syntax error that just occurred. |
|
1335 | """Display the syntax error that just occurred. | |
1334 |
|
1336 | |||
1335 | This doesn't display a stack trace because there isn't one. |
|
1337 | This doesn't display a stack trace because there isn't one. | |
1336 |
|
1338 | |||
1337 | If a filename is given, it is stuffed in the exception instead |
|
1339 | If a filename is given, it is stuffed in the exception instead | |
1338 | of what was there before (because Python's parser always uses |
|
1340 | of what was there before (because Python's parser always uses | |
1339 | "<string>" when reading from a string). |
|
1341 | "<string>" when reading from a string). | |
1340 | """ |
|
1342 | """ | |
1341 | etype, value, last_traceback = sys.exc_info() |
|
1343 | etype, value, last_traceback = sys.exc_info() | |
1342 |
|
1344 | |||
1343 | # See note about these variables in showtraceback() above |
|
1345 | # See note about these variables in showtraceback() above | |
1344 | sys.last_type = etype |
|
1346 | sys.last_type = etype | |
1345 | sys.last_value = value |
|
1347 | sys.last_value = value | |
1346 | sys.last_traceback = last_traceback |
|
1348 | sys.last_traceback = last_traceback | |
1347 |
|
1349 | |||
1348 | if filename and etype is SyntaxError: |
|
1350 | if filename and etype is SyntaxError: | |
1349 | # Work hard to stuff the correct filename in the exception |
|
1351 | # Work hard to stuff the correct filename in the exception | |
1350 | try: |
|
1352 | try: | |
1351 | msg, (dummy_filename, lineno, offset, line) = value |
|
1353 | msg, (dummy_filename, lineno, offset, line) = value | |
1352 | except: |
|
1354 | except: | |
1353 | # Not the format we expect; leave it alone |
|
1355 | # Not the format we expect; leave it alone | |
1354 | pass |
|
1356 | pass | |
1355 | else: |
|
1357 | else: | |
1356 | # Stuff in the right filename |
|
1358 | # Stuff in the right filename | |
1357 | try: |
|
1359 | try: | |
1358 | # Assume SyntaxError is a class exception |
|
1360 | # Assume SyntaxError is a class exception | |
1359 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1361 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1360 | except: |
|
1362 | except: | |
1361 | # If that failed, assume SyntaxError is a string |
|
1363 | # If that failed, assume SyntaxError is a string | |
1362 | value = msg, (filename, lineno, offset, line) |
|
1364 | value = msg, (filename, lineno, offset, line) | |
1363 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1365 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | |
1364 | self._showtraceback(etype, value, stb) |
|
1366 | self._showtraceback(etype, value, stb) | |
1365 |
|
1367 | |||
1366 | #------------------------------------------------------------------------- |
|
1368 | #------------------------------------------------------------------------- | |
1367 | # Things related to tab completion |
|
1369 | # Things related to tab completion | |
1368 | #------------------------------------------------------------------------- |
|
1370 | #------------------------------------------------------------------------- | |
1369 |
|
1371 | |||
1370 | def complete(self, text): |
|
1372 | def complete(self, text, line=None, cursor_pos=None): | |
1371 | """Return a sorted list of all possible completions on text. |
|
1373 | """Return a sorted list of all possible completions on text. | |
1372 |
|
1374 | |||
1373 |
|
|
1375 | Parameters | |
|
1376 | ---------- | |||
|
1377 | ||||
|
1378 | text : string | |||
|
1379 | A string of text to be completed on. | |||
1374 |
|
1380 | |||
1375 | - text: a string of text to be completed on. |
|
1381 | line : string, optional | |
|
1382 | The complete line that text is part of. | |||
|
1383 | ||||
|
1384 | cursor_pos : int, optional | |||
|
1385 | The position of the cursor on the input line. | |||
|
1386 | ||||
|
1387 | The optional arguments allow the completion to take more context into | |||
|
1388 | account, and are part of the low-level completion API. | |||
1376 |
|
1389 | |||
1377 | This is a wrapper around the completion mechanism, similar to what |
|
1390 | This is a wrapper around the completion mechanism, similar to what | |
1378 | readline does at the command line when the TAB key is hit. By |
|
1391 | readline does at the command line when the TAB key is hit. By | |
1379 | exposing it as a method, it can be used by other non-readline |
|
1392 | exposing it as a method, it can be used by other non-readline | |
1380 | environments (such as GUIs) for text completion. |
|
1393 | environments (such as GUIs) for text completion. | |
1381 |
|
1394 | |||
1382 | Simple usage example: |
|
1395 | Simple usage example: | |
1383 |
|
1396 | |||
1384 | In [7]: x = 'hello' |
|
1397 | In [7]: x = 'hello' | |
1385 |
|
1398 | |||
1386 | In [8]: x |
|
1399 | In [8]: x | |
1387 | Out[8]: 'hello' |
|
1400 | Out[8]: 'hello' | |
1388 |
|
1401 | |||
1389 | In [9]: print x |
|
1402 | In [9]: print x | |
1390 | hello |
|
1403 | hello | |
1391 |
|
1404 | |||
1392 | In [10]: _ip.complete('x.l') |
|
1405 | In [10]: _ip.complete('x.l') | |
1393 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] |
|
1406 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] | |
1394 | """ |
|
1407 | """ | |
1395 |
|
1408 | |||
1396 | # Inject names into __builtin__ so we can complete on the added names. |
|
1409 | # Inject names into __builtin__ so we can complete on the added names. | |
1397 | with self.builtin_trap: |
|
1410 | with self.builtin_trap: | |
1398 |
|
|
1411 | return self.Completer.complete(text,line_buffer=text) | |
1399 | state = 0 |
|
|||
1400 | # use a dict so we get unique keys, since ipyhton's multiple |
|
|||
1401 | # completers can return duplicates. When we make 2.4 a requirement, |
|
|||
1402 | # start using sets instead, which are faster. |
|
|||
1403 | comps = {} |
|
|||
1404 | while True: |
|
|||
1405 | newcomp = complete(text,state,line_buffer=text) |
|
|||
1406 | if newcomp is None: |
|
|||
1407 | break |
|
|||
1408 | comps[newcomp] = 1 |
|
|||
1409 | state += 1 |
|
|||
1410 | outcomps = comps.keys() |
|
|||
1411 | outcomps.sort() |
|
|||
1412 | #print "T:",text,"OC:",outcomps # dbg |
|
|||
1413 | #print "vars:",self.user_ns.keys() |
|
|||
1414 | return outcomps |
|
|||
1415 |
|
1412 | |||
1416 | def set_custom_completer(self,completer,pos=0): |
|
1413 | def set_custom_completer(self,completer,pos=0): | |
1417 | """Adds a new custom completer function. |
|
1414 | """Adds a new custom completer function. | |
1418 |
|
1415 | |||
1419 | The position argument (defaults to 0) is the index in the completers |
|
1416 | The position argument (defaults to 0) is the index in the completers | |
1420 | list where you want the completer to be inserted.""" |
|
1417 | list where you want the completer to be inserted.""" | |
1421 |
|
1418 | |||
1422 | newcomp = new.instancemethod(completer,self.Completer, |
|
1419 | newcomp = new.instancemethod(completer,self.Completer, | |
1423 | self.Completer.__class__) |
|
1420 | self.Completer.__class__) | |
1424 | self.Completer.matchers.insert(pos,newcomp) |
|
1421 | self.Completer.matchers.insert(pos,newcomp) | |
1425 |
|
1422 | |||
1426 | def set_completer(self): |
|
1423 | def set_completer(self): | |
1427 | """Reset readline's completer to be our own.""" |
|
1424 | """Reset readline's completer to be our own.""" | |
1428 | self.readline.set_completer(self.Completer.complete) |
|
1425 | self.readline.set_completer(self.Completer.rlcomplete) | |
1429 |
|
1426 | |||
1430 | def set_completer_frame(self, frame=None): |
|
1427 | def set_completer_frame(self, frame=None): | |
1431 | """Set the frame of the completer.""" |
|
1428 | """Set the frame of the completer.""" | |
1432 | if frame: |
|
1429 | if frame: | |
1433 | self.Completer.namespace = frame.f_locals |
|
1430 | self.Completer.namespace = frame.f_locals | |
1434 | self.Completer.global_namespace = frame.f_globals |
|
1431 | self.Completer.global_namespace = frame.f_globals | |
1435 | else: |
|
1432 | else: | |
1436 | self.Completer.namespace = self.user_ns |
|
1433 | self.Completer.namespace = self.user_ns | |
1437 | self.Completer.global_namespace = self.user_global_ns |
|
1434 | self.Completer.global_namespace = self.user_global_ns | |
1438 |
|
1435 | |||
1439 | #------------------------------------------------------------------------- |
|
1436 | #------------------------------------------------------------------------- | |
1440 | # Things related to readline |
|
1437 | # Things related to readline | |
1441 | #------------------------------------------------------------------------- |
|
1438 | #------------------------------------------------------------------------- | |
1442 |
|
1439 | |||
1443 | def init_readline(self): |
|
1440 | def init_readline(self): | |
1444 | """Command history completion/saving/reloading.""" |
|
1441 | """Command history completion/saving/reloading.""" | |
1445 |
|
1442 | |||
1446 | if self.readline_use: |
|
1443 | if self.readline_use: | |
1447 | import IPython.utils.rlineimpl as readline |
|
1444 | import IPython.utils.rlineimpl as readline | |
1448 |
|
1445 | |||
1449 | self.rl_next_input = None |
|
1446 | self.rl_next_input = None | |
1450 | self.rl_do_indent = False |
|
1447 | self.rl_do_indent = False | |
1451 |
|
1448 | |||
1452 | if not self.readline_use or not readline.have_readline: |
|
1449 | if not self.readline_use or not readline.have_readline: | |
1453 | self.has_readline = False |
|
1450 | self.has_readline = False | |
1454 | self.readline = None |
|
1451 | self.readline = None | |
1455 | # Set a number of methods that depend on readline to be no-op |
|
1452 | # Set a number of methods that depend on readline to be no-op | |
1456 | self.savehist = no_op |
|
1453 | self.savehist = no_op | |
1457 | self.reloadhist = no_op |
|
1454 | self.reloadhist = no_op | |
1458 | self.set_completer = no_op |
|
1455 | self.set_completer = no_op | |
1459 | self.set_custom_completer = no_op |
|
1456 | self.set_custom_completer = no_op | |
1460 | self.set_completer_frame = no_op |
|
1457 | self.set_completer_frame = no_op | |
1461 | warn('Readline services not available or not loaded.') |
|
1458 | warn('Readline services not available or not loaded.') | |
1462 | else: |
|
1459 | else: | |
1463 | self.has_readline = True |
|
1460 | self.has_readline = True | |
1464 | self.readline = readline |
|
1461 | self.readline = readline | |
1465 | sys.modules['readline'] = readline |
|
1462 | sys.modules['readline'] = readline | |
1466 | import atexit |
|
1463 | import atexit | |
1467 | from IPython.core.completer import IPCompleter |
|
1464 | from IPython.core.completer import IPCompleter | |
1468 | self.Completer = IPCompleter(self, |
|
1465 | self.Completer = IPCompleter(self, | |
1469 | self.user_ns, |
|
1466 | self.user_ns, | |
1470 | self.user_global_ns, |
|
1467 | self.user_global_ns, | |
1471 | self.readline_omit__names, |
|
1468 | self.readline_omit__names, | |
1472 | self.alias_manager.alias_table) |
|
1469 | self.alias_manager.alias_table) | |
1473 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1470 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1474 | self.strdispatchers['complete_command'] = sdisp |
|
1471 | self.strdispatchers['complete_command'] = sdisp | |
1475 | self.Completer.custom_completers = sdisp |
|
1472 | self.Completer.custom_completers = sdisp | |
1476 | # Platform-specific configuration |
|
1473 | # Platform-specific configuration | |
1477 | if os.name == 'nt': |
|
1474 | if os.name == 'nt': | |
1478 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1475 | self.readline_startup_hook = readline.set_pre_input_hook | |
1479 | else: |
|
1476 | else: | |
1480 | self.readline_startup_hook = readline.set_startup_hook |
|
1477 | self.readline_startup_hook = readline.set_startup_hook | |
1481 |
|
1478 | |||
1482 | # Load user's initrc file (readline config) |
|
1479 | # Load user's initrc file (readline config) | |
1483 | # Or if libedit is used, load editrc. |
|
1480 | # Or if libedit is used, load editrc. | |
1484 | inputrc_name = os.environ.get('INPUTRC') |
|
1481 | inputrc_name = os.environ.get('INPUTRC') | |
1485 | if inputrc_name is None: |
|
1482 | if inputrc_name is None: | |
1486 | home_dir = get_home_dir() |
|
1483 | home_dir = get_home_dir() | |
1487 | if home_dir is not None: |
|
1484 | if home_dir is not None: | |
1488 | inputrc_name = '.inputrc' |
|
1485 | inputrc_name = '.inputrc' | |
1489 | if readline.uses_libedit: |
|
1486 | if readline.uses_libedit: | |
1490 | inputrc_name = '.editrc' |
|
1487 | inputrc_name = '.editrc' | |
1491 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
1488 | inputrc_name = os.path.join(home_dir, inputrc_name) | |
1492 | if os.path.isfile(inputrc_name): |
|
1489 | if os.path.isfile(inputrc_name): | |
1493 | try: |
|
1490 | try: | |
1494 | readline.read_init_file(inputrc_name) |
|
1491 | readline.read_init_file(inputrc_name) | |
1495 | except: |
|
1492 | except: | |
1496 | warn('Problems reading readline initialization file <%s>' |
|
1493 | warn('Problems reading readline initialization file <%s>' | |
1497 | % inputrc_name) |
|
1494 | % inputrc_name) | |
1498 |
|
1495 | |||
1499 | # save this in sys so embedded copies can restore it properly |
|
1496 | # save this in sys so embedded copies can restore it properly | |
1500 | sys.ipcompleter = self.Completer.complete |
|
1497 | sys.ipcompleter = self.Completer.rlcomplete | |
1501 | self.set_completer() |
|
1498 | self.set_completer() | |
1502 |
|
1499 | |||
1503 | # Configure readline according to user's prefs |
|
1500 | # Configure readline according to user's prefs | |
1504 | # This is only done if GNU readline is being used. If libedit |
|
1501 | # This is only done if GNU readline is being used. If libedit | |
1505 | # is being used (as on Leopard) the readline config is |
|
1502 | # is being used (as on Leopard) the readline config is | |
1506 | # not run as the syntax for libedit is different. |
|
1503 | # not run as the syntax for libedit is different. | |
1507 | if not readline.uses_libedit: |
|
1504 | if not readline.uses_libedit: | |
1508 | for rlcommand in self.readline_parse_and_bind: |
|
1505 | for rlcommand in self.readline_parse_and_bind: | |
1509 | #print "loading rl:",rlcommand # dbg |
|
1506 | #print "loading rl:",rlcommand # dbg | |
1510 | readline.parse_and_bind(rlcommand) |
|
1507 | readline.parse_and_bind(rlcommand) | |
1511 |
|
1508 | |||
1512 | # Remove some chars from the delimiters list. If we encounter |
|
1509 | # Remove some chars from the delimiters list. If we encounter | |
1513 | # unicode chars, discard them. |
|
1510 | # unicode chars, discard them. | |
1514 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
1511 | delims = readline.get_completer_delims().encode("ascii", "ignore") | |
1515 | delims = delims.translate(string._idmap, |
|
1512 | delims = delims.translate(string._idmap, | |
1516 | self.readline_remove_delims) |
|
1513 | self.readline_remove_delims) | |
1517 | readline.set_completer_delims(delims) |
|
1514 | readline.set_completer_delims(delims) | |
1518 | # otherwise we end up with a monster history after a while: |
|
1515 | # otherwise we end up with a monster history after a while: | |
1519 | readline.set_history_length(1000) |
|
1516 | readline.set_history_length(1000) | |
1520 | try: |
|
1517 | try: | |
1521 | #print '*** Reading readline history' # dbg |
|
1518 | #print '*** Reading readline history' # dbg | |
1522 | readline.read_history_file(self.histfile) |
|
1519 | readline.read_history_file(self.histfile) | |
1523 | except IOError: |
|
1520 | except IOError: | |
1524 | pass # It doesn't exist yet. |
|
1521 | pass # It doesn't exist yet. | |
1525 |
|
1522 | |||
1526 | atexit.register(self.atexit_operations) |
|
1523 | atexit.register(self.atexit_operations) | |
1527 | del atexit |
|
1524 | del atexit | |
1528 |
|
1525 | |||
1529 | # Configure auto-indent for all platforms |
|
1526 | # Configure auto-indent for all platforms | |
1530 | self.set_autoindent(self.autoindent) |
|
1527 | self.set_autoindent(self.autoindent) | |
1531 |
|
1528 | |||
1532 | def set_next_input(self, s): |
|
1529 | def set_next_input(self, s): | |
1533 | """ Sets the 'default' input string for the next command line. |
|
1530 | """ Sets the 'default' input string for the next command line. | |
1534 |
|
1531 | |||
1535 | Requires readline. |
|
1532 | Requires readline. | |
1536 |
|
1533 | |||
1537 | Example: |
|
1534 | Example: | |
1538 |
|
1535 | |||
1539 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1536 | [D:\ipython]|1> _ip.set_next_input("Hello Word") | |
1540 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1537 | [D:\ipython]|2> Hello Word_ # cursor is here | |
1541 | """ |
|
1538 | """ | |
1542 |
|
1539 | |||
1543 | self.rl_next_input = s |
|
1540 | self.rl_next_input = s | |
1544 |
|
1541 | |||
1545 | # Maybe move this to the terminal subclass? |
|
1542 | # Maybe move this to the terminal subclass? | |
1546 | def pre_readline(self): |
|
1543 | def pre_readline(self): | |
1547 | """readline hook to be used at the start of each line. |
|
1544 | """readline hook to be used at the start of each line. | |
1548 |
|
1545 | |||
1549 | Currently it handles auto-indent only.""" |
|
1546 | Currently it handles auto-indent only.""" | |
1550 |
|
1547 | |||
1551 | if self.rl_do_indent: |
|
1548 | if self.rl_do_indent: | |
1552 | self.readline.insert_text(self._indent_current_str()) |
|
1549 | self.readline.insert_text(self._indent_current_str()) | |
1553 | if self.rl_next_input is not None: |
|
1550 | if self.rl_next_input is not None: | |
1554 | self.readline.insert_text(self.rl_next_input) |
|
1551 | self.readline.insert_text(self.rl_next_input) | |
1555 | self.rl_next_input = None |
|
1552 | self.rl_next_input = None | |
1556 |
|
1553 | |||
1557 | def _indent_current_str(self): |
|
1554 | def _indent_current_str(self): | |
1558 | """return the current level of indentation as a string""" |
|
1555 | """return the current level of indentation as a string""" | |
1559 | return self.indent_current_nsp * ' ' |
|
1556 | return self.indent_current_nsp * ' ' | |
1560 |
|
1557 | |||
1561 | #------------------------------------------------------------------------- |
|
1558 | #------------------------------------------------------------------------- | |
1562 | # Things related to magics |
|
1559 | # Things related to magics | |
1563 | #------------------------------------------------------------------------- |
|
1560 | #------------------------------------------------------------------------- | |
1564 |
|
1561 | |||
1565 | def init_magics(self): |
|
1562 | def init_magics(self): | |
1566 | # FIXME: Move the color initialization to the DisplayHook, which |
|
1563 | # FIXME: Move the color initialization to the DisplayHook, which | |
1567 | # should be split into a prompt manager and displayhook. We probably |
|
1564 | # should be split into a prompt manager and displayhook. We probably | |
1568 | # even need a centralize colors management object. |
|
1565 | # even need a centralize colors management object. | |
1569 | self.magic_colors(self.colors) |
|
1566 | self.magic_colors(self.colors) | |
1570 | # History was moved to a separate module |
|
1567 | # History was moved to a separate module | |
1571 | from . import history |
|
1568 | from . import history | |
1572 | history.init_ipython(self) |
|
1569 | history.init_ipython(self) | |
1573 |
|
1570 | |||
1574 | def magic(self,arg_s): |
|
1571 | def magic(self,arg_s): | |
1575 | """Call a magic function by name. |
|
1572 | """Call a magic function by name. | |
1576 |
|
1573 | |||
1577 | Input: a string containing the name of the magic function to call and any |
|
1574 | Input: a string containing the name of the magic function to call and any | |
1578 | additional arguments to be passed to the magic. |
|
1575 | additional arguments to be passed to the magic. | |
1579 |
|
1576 | |||
1580 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1577 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
1581 | prompt: |
|
1578 | prompt: | |
1582 |
|
1579 | |||
1583 | In[1]: %name -opt foo bar |
|
1580 | In[1]: %name -opt foo bar | |
1584 |
|
1581 | |||
1585 | To call a magic without arguments, simply use magic('name'). |
|
1582 | To call a magic without arguments, simply use magic('name'). | |
1586 |
|
1583 | |||
1587 | This provides a proper Python function to call IPython's magics in any |
|
1584 | This provides a proper Python function to call IPython's magics in any | |
1588 | valid Python code you can type at the interpreter, including loops and |
|
1585 | valid Python code you can type at the interpreter, including loops and | |
1589 | compound statements. |
|
1586 | compound statements. | |
1590 | """ |
|
1587 | """ | |
1591 | args = arg_s.split(' ',1) |
|
1588 | args = arg_s.split(' ',1) | |
1592 | magic_name = args[0] |
|
1589 | magic_name = args[0] | |
1593 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
1590 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
1594 |
|
1591 | |||
1595 | try: |
|
1592 | try: | |
1596 | magic_args = args[1] |
|
1593 | magic_args = args[1] | |
1597 | except IndexError: |
|
1594 | except IndexError: | |
1598 | magic_args = '' |
|
1595 | magic_args = '' | |
1599 | fn = getattr(self,'magic_'+magic_name,None) |
|
1596 | fn = getattr(self,'magic_'+magic_name,None) | |
1600 | if fn is None: |
|
1597 | if fn is None: | |
1601 | error("Magic function `%s` not found." % magic_name) |
|
1598 | error("Magic function `%s` not found." % magic_name) | |
1602 | else: |
|
1599 | else: | |
1603 | magic_args = self.var_expand(magic_args,1) |
|
1600 | magic_args = self.var_expand(magic_args,1) | |
1604 | with nested(self.builtin_trap,): |
|
1601 | with nested(self.builtin_trap,): | |
1605 | result = fn(magic_args) |
|
1602 | result = fn(magic_args) | |
1606 | return result |
|
1603 | return result | |
1607 |
|
1604 | |||
1608 | def define_magic(self, magicname, func): |
|
1605 | def define_magic(self, magicname, func): | |
1609 | """Expose own function as magic function for ipython |
|
1606 | """Expose own function as magic function for ipython | |
1610 |
|
1607 | |||
1611 | def foo_impl(self,parameter_s=''): |
|
1608 | def foo_impl(self,parameter_s=''): | |
1612 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1609 | 'My very own magic!. (Use docstrings, IPython reads them).' | |
1613 | print 'Magic function. Passed parameter is between < >:' |
|
1610 | print 'Magic function. Passed parameter is between < >:' | |
1614 | print '<%s>' % parameter_s |
|
1611 | print '<%s>' % parameter_s | |
1615 | print 'The self object is:',self |
|
1612 | print 'The self object is:',self | |
1616 |
|
1613 | |||
1617 | self.define_magic('foo',foo_impl) |
|
1614 | self.define_magic('foo',foo_impl) | |
1618 | """ |
|
1615 | """ | |
1619 |
|
1616 | |||
1620 | import new |
|
1617 | import new | |
1621 | im = new.instancemethod(func,self, self.__class__) |
|
1618 | im = new.instancemethod(func,self, self.__class__) | |
1622 | old = getattr(self, "magic_" + magicname, None) |
|
1619 | old = getattr(self, "magic_" + magicname, None) | |
1623 | setattr(self, "magic_" + magicname, im) |
|
1620 | setattr(self, "magic_" + magicname, im) | |
1624 | return old |
|
1621 | return old | |
1625 |
|
1622 | |||
1626 | #------------------------------------------------------------------------- |
|
1623 | #------------------------------------------------------------------------- | |
1627 | # Things related to macros |
|
1624 | # Things related to macros | |
1628 | #------------------------------------------------------------------------- |
|
1625 | #------------------------------------------------------------------------- | |
1629 |
|
1626 | |||
1630 | def define_macro(self, name, themacro): |
|
1627 | def define_macro(self, name, themacro): | |
1631 | """Define a new macro |
|
1628 | """Define a new macro | |
1632 |
|
1629 | |||
1633 | Parameters |
|
1630 | Parameters | |
1634 | ---------- |
|
1631 | ---------- | |
1635 | name : str |
|
1632 | name : str | |
1636 | The name of the macro. |
|
1633 | The name of the macro. | |
1637 | themacro : str or Macro |
|
1634 | themacro : str or Macro | |
1638 | The action to do upon invoking the macro. If a string, a new |
|
1635 | The action to do upon invoking the macro. If a string, a new | |
1639 | Macro object is created by passing the string to it. |
|
1636 | Macro object is created by passing the string to it. | |
1640 | """ |
|
1637 | """ | |
1641 |
|
1638 | |||
1642 | from IPython.core import macro |
|
1639 | from IPython.core import macro | |
1643 |
|
1640 | |||
1644 | if isinstance(themacro, basestring): |
|
1641 | if isinstance(themacro, basestring): | |
1645 | themacro = macro.Macro(themacro) |
|
1642 | themacro = macro.Macro(themacro) | |
1646 | if not isinstance(themacro, macro.Macro): |
|
1643 | if not isinstance(themacro, macro.Macro): | |
1647 | raise ValueError('A macro must be a string or a Macro instance.') |
|
1644 | raise ValueError('A macro must be a string or a Macro instance.') | |
1648 | self.user_ns[name] = themacro |
|
1645 | self.user_ns[name] = themacro | |
1649 |
|
1646 | |||
1650 | #------------------------------------------------------------------------- |
|
1647 | #------------------------------------------------------------------------- | |
1651 | # Things related to the running of system commands |
|
1648 | # Things related to the running of system commands | |
1652 | #------------------------------------------------------------------------- |
|
1649 | #------------------------------------------------------------------------- | |
1653 |
|
1650 | |||
1654 | def system(self, cmd): |
|
1651 | def system(self, cmd): | |
1655 | """Make a system call, using IPython.""" |
|
1652 | """Make a system call, using IPython.""" | |
1656 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) |
|
1653 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) | |
1657 |
|
1654 | |||
1658 | #------------------------------------------------------------------------- |
|
1655 | #------------------------------------------------------------------------- | |
1659 | # Things related to aliases |
|
1656 | # Things related to aliases | |
1660 | #------------------------------------------------------------------------- |
|
1657 | #------------------------------------------------------------------------- | |
1661 |
|
1658 | |||
1662 | def init_alias(self): |
|
1659 | def init_alias(self): | |
1663 | self.alias_manager = AliasManager(shell=self, config=self.config) |
|
1660 | self.alias_manager = AliasManager(shell=self, config=self.config) | |
1664 | self.ns_table['alias'] = self.alias_manager.alias_table, |
|
1661 | self.ns_table['alias'] = self.alias_manager.alias_table, | |
1665 |
|
1662 | |||
1666 | #------------------------------------------------------------------------- |
|
1663 | #------------------------------------------------------------------------- | |
1667 | # Things related to extensions and plugins |
|
1664 | # Things related to extensions and plugins | |
1668 | #------------------------------------------------------------------------- |
|
1665 | #------------------------------------------------------------------------- | |
1669 |
|
1666 | |||
1670 | def init_extension_manager(self): |
|
1667 | def init_extension_manager(self): | |
1671 | self.extension_manager = ExtensionManager(shell=self, config=self.config) |
|
1668 | self.extension_manager = ExtensionManager(shell=self, config=self.config) | |
1672 |
|
1669 | |||
1673 | def init_plugin_manager(self): |
|
1670 | def init_plugin_manager(self): | |
1674 | self.plugin_manager = PluginManager(config=self.config) |
|
1671 | self.plugin_manager = PluginManager(config=self.config) | |
1675 |
|
1672 | |||
1676 | #------------------------------------------------------------------------- |
|
1673 | #------------------------------------------------------------------------- | |
1677 | # Things related to payloads |
|
1674 | # Things related to payloads | |
1678 | #------------------------------------------------------------------------- |
|
1675 | #------------------------------------------------------------------------- | |
1679 |
|
1676 | |||
1680 | def init_payload(self): |
|
1677 | def init_payload(self): | |
1681 | self.payload_manager = PayloadManager(config=self.config) |
|
1678 | self.payload_manager = PayloadManager(config=self.config) | |
1682 |
|
1679 | |||
1683 | #------------------------------------------------------------------------- |
|
1680 | #------------------------------------------------------------------------- | |
1684 | # Things related to the prefilter |
|
1681 | # Things related to the prefilter | |
1685 | #------------------------------------------------------------------------- |
|
1682 | #------------------------------------------------------------------------- | |
1686 |
|
1683 | |||
1687 | def init_prefilter(self): |
|
1684 | def init_prefilter(self): | |
1688 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) |
|
1685 | self.prefilter_manager = PrefilterManager(shell=self, config=self.config) | |
1689 | # Ultimately this will be refactored in the new interpreter code, but |
|
1686 | # Ultimately this will be refactored in the new interpreter code, but | |
1690 | # for now, we should expose the main prefilter method (there's legacy |
|
1687 | # for now, we should expose the main prefilter method (there's legacy | |
1691 | # code out there that may rely on this). |
|
1688 | # code out there that may rely on this). | |
1692 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
1689 | self.prefilter = self.prefilter_manager.prefilter_lines | |
1693 |
|
1690 | |||
1694 | #------------------------------------------------------------------------- |
|
1691 | #------------------------------------------------------------------------- | |
1695 | # Things related to the running of code |
|
1692 | # Things related to the running of code | |
1696 | #------------------------------------------------------------------------- |
|
1693 | #------------------------------------------------------------------------- | |
1697 |
|
1694 | |||
1698 | def ex(self, cmd): |
|
1695 | def ex(self, cmd): | |
1699 | """Execute a normal python statement in user namespace.""" |
|
1696 | """Execute a normal python statement in user namespace.""" | |
1700 | with nested(self.builtin_trap,): |
|
1697 | with nested(self.builtin_trap,): | |
1701 | exec cmd in self.user_global_ns, self.user_ns |
|
1698 | exec cmd in self.user_global_ns, self.user_ns | |
1702 |
|
1699 | |||
1703 | def ev(self, expr): |
|
1700 | def ev(self, expr): | |
1704 | """Evaluate python expression expr in user namespace. |
|
1701 | """Evaluate python expression expr in user namespace. | |
1705 |
|
1702 | |||
1706 | Returns the result of evaluation |
|
1703 | Returns the result of evaluation | |
1707 | """ |
|
1704 | """ | |
1708 | with nested(self.builtin_trap,): |
|
1705 | with nested(self.builtin_trap,): | |
1709 | return eval(expr, self.user_global_ns, self.user_ns) |
|
1706 | return eval(expr, self.user_global_ns, self.user_ns) | |
1710 |
|
1707 | |||
1711 | def safe_execfile(self, fname, *where, **kw): |
|
1708 | def safe_execfile(self, fname, *where, **kw): | |
1712 | """A safe version of the builtin execfile(). |
|
1709 | """A safe version of the builtin execfile(). | |
1713 |
|
1710 | |||
1714 | This version will never throw an exception, but instead print |
|
1711 | This version will never throw an exception, but instead print | |
1715 | helpful error messages to the screen. This only works on pure |
|
1712 | helpful error messages to the screen. This only works on pure | |
1716 | Python files with the .py extension. |
|
1713 | Python files with the .py extension. | |
1717 |
|
1714 | |||
1718 | Parameters |
|
1715 | Parameters | |
1719 | ---------- |
|
1716 | ---------- | |
1720 | fname : string |
|
1717 | fname : string | |
1721 | The name of the file to be executed. |
|
1718 | The name of the file to be executed. | |
1722 | where : tuple |
|
1719 | where : tuple | |
1723 | One or two namespaces, passed to execfile() as (globals,locals). |
|
1720 | One or two namespaces, passed to execfile() as (globals,locals). | |
1724 | If only one is given, it is passed as both. |
|
1721 | If only one is given, it is passed as both. | |
1725 | exit_ignore : bool (False) |
|
1722 | exit_ignore : bool (False) | |
1726 | If True, then silence SystemExit for non-zero status (it is always |
|
1723 | If True, then silence SystemExit for non-zero status (it is always | |
1727 | silenced for zero status, as it is so common). |
|
1724 | silenced for zero status, as it is so common). | |
1728 | """ |
|
1725 | """ | |
1729 | kw.setdefault('exit_ignore', False) |
|
1726 | kw.setdefault('exit_ignore', False) | |
1730 |
|
1727 | |||
1731 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
1728 | fname = os.path.abspath(os.path.expanduser(fname)) | |
1732 |
|
1729 | |||
1733 | # Make sure we have a .py file |
|
1730 | # Make sure we have a .py file | |
1734 | if not fname.endswith('.py'): |
|
1731 | if not fname.endswith('.py'): | |
1735 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
1732 | warn('File must end with .py to be run using execfile: <%s>' % fname) | |
1736 |
|
1733 | |||
1737 | # Make sure we can open the file |
|
1734 | # Make sure we can open the file | |
1738 | try: |
|
1735 | try: | |
1739 | with open(fname) as thefile: |
|
1736 | with open(fname) as thefile: | |
1740 | pass |
|
1737 | pass | |
1741 | except: |
|
1738 | except: | |
1742 | warn('Could not open file <%s> for safe execution.' % fname) |
|
1739 | warn('Could not open file <%s> for safe execution.' % fname) | |
1743 | return |
|
1740 | return | |
1744 |
|
1741 | |||
1745 | # Find things also in current directory. This is needed to mimic the |
|
1742 | # Find things also in current directory. This is needed to mimic the | |
1746 | # behavior of running a script from the system command line, where |
|
1743 | # behavior of running a script from the system command line, where | |
1747 | # Python inserts the script's directory into sys.path |
|
1744 | # Python inserts the script's directory into sys.path | |
1748 | dname = os.path.dirname(fname) |
|
1745 | dname = os.path.dirname(fname) | |
1749 |
|
1746 | |||
1750 | with prepended_to_syspath(dname): |
|
1747 | with prepended_to_syspath(dname): | |
1751 | try: |
|
1748 | try: | |
1752 | execfile(fname,*where) |
|
1749 | execfile(fname,*where) | |
1753 | except SystemExit, status: |
|
1750 | except SystemExit, status: | |
1754 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
1751 | # If the call was made with 0 or None exit status (sys.exit(0) | |
1755 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
1752 | # or sys.exit() ), don't bother showing a traceback, as both of | |
1756 | # these are considered normal by the OS: |
|
1753 | # these are considered normal by the OS: | |
1757 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
1754 | # > python -c'import sys;sys.exit(0)'; echo $? | |
1758 | # 0 |
|
1755 | # 0 | |
1759 | # > python -c'import sys;sys.exit()'; echo $? |
|
1756 | # > python -c'import sys;sys.exit()'; echo $? | |
1760 | # 0 |
|
1757 | # 0 | |
1761 | # For other exit status, we show the exception unless |
|
1758 | # For other exit status, we show the exception unless | |
1762 | # explicitly silenced, but only in short form. |
|
1759 | # explicitly silenced, but only in short form. | |
1763 | if status.code not in (0, None) and not kw['exit_ignore']: |
|
1760 | if status.code not in (0, None) and not kw['exit_ignore']: | |
1764 | self.showtraceback(exception_only=True) |
|
1761 | self.showtraceback(exception_only=True) | |
1765 | except: |
|
1762 | except: | |
1766 | self.showtraceback() |
|
1763 | self.showtraceback() | |
1767 |
|
1764 | |||
1768 | def safe_execfile_ipy(self, fname): |
|
1765 | def safe_execfile_ipy(self, fname): | |
1769 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
1766 | """Like safe_execfile, but for .ipy files with IPython syntax. | |
1770 |
|
1767 | |||
1771 | Parameters |
|
1768 | Parameters | |
1772 | ---------- |
|
1769 | ---------- | |
1773 | fname : str |
|
1770 | fname : str | |
1774 | The name of the file to execute. The filename must have a |
|
1771 | The name of the file to execute. The filename must have a | |
1775 | .ipy extension. |
|
1772 | .ipy extension. | |
1776 | """ |
|
1773 | """ | |
1777 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
1774 | fname = os.path.abspath(os.path.expanduser(fname)) | |
1778 |
|
1775 | |||
1779 | # Make sure we have a .py file |
|
1776 | # Make sure we have a .py file | |
1780 | if not fname.endswith('.ipy'): |
|
1777 | if not fname.endswith('.ipy'): | |
1781 | warn('File must end with .py to be run using execfile: <%s>' % fname) |
|
1778 | warn('File must end with .py to be run using execfile: <%s>' % fname) | |
1782 |
|
1779 | |||
1783 | # Make sure we can open the file |
|
1780 | # Make sure we can open the file | |
1784 | try: |
|
1781 | try: | |
1785 | with open(fname) as thefile: |
|
1782 | with open(fname) as thefile: | |
1786 | pass |
|
1783 | pass | |
1787 | except: |
|
1784 | except: | |
1788 | warn('Could not open file <%s> for safe execution.' % fname) |
|
1785 | warn('Could not open file <%s> for safe execution.' % fname) | |
1789 | return |
|
1786 | return | |
1790 |
|
1787 | |||
1791 | # Find things also in current directory. This is needed to mimic the |
|
1788 | # Find things also in current directory. This is needed to mimic the | |
1792 | # behavior of running a script from the system command line, where |
|
1789 | # behavior of running a script from the system command line, where | |
1793 | # Python inserts the script's directory into sys.path |
|
1790 | # Python inserts the script's directory into sys.path | |
1794 | dname = os.path.dirname(fname) |
|
1791 | dname = os.path.dirname(fname) | |
1795 |
|
1792 | |||
1796 | with prepended_to_syspath(dname): |
|
1793 | with prepended_to_syspath(dname): | |
1797 | try: |
|
1794 | try: | |
1798 | with open(fname) as thefile: |
|
1795 | with open(fname) as thefile: | |
1799 | script = thefile.read() |
|
1796 | script = thefile.read() | |
1800 | # self.runlines currently captures all exceptions |
|
1797 | # self.runlines currently captures all exceptions | |
1801 | # raise in user code. It would be nice if there were |
|
1798 | # raise in user code. It would be nice if there were | |
1802 | # versions of runlines, execfile that did raise, so |
|
1799 | # versions of runlines, execfile that did raise, so | |
1803 | # we could catch the errors. |
|
1800 | # we could catch the errors. | |
1804 | self.runlines(script, clean=True) |
|
1801 | self.runlines(script, clean=True) | |
1805 | except: |
|
1802 | except: | |
1806 | self.showtraceback() |
|
1803 | self.showtraceback() | |
1807 | warn('Unknown failure executing file: <%s>' % fname) |
|
1804 | warn('Unknown failure executing file: <%s>' % fname) | |
1808 |
|
1805 | |||
1809 | def runlines(self, lines, clean=False): |
|
1806 | def runlines(self, lines, clean=False): | |
1810 | """Run a string of one or more lines of source. |
|
1807 | """Run a string of one or more lines of source. | |
1811 |
|
1808 | |||
1812 | This method is capable of running a string containing multiple source |
|
1809 | This method is capable of running a string containing multiple source | |
1813 | lines, as if they had been entered at the IPython prompt. Since it |
|
1810 | lines, as if they had been entered at the IPython prompt. Since it | |
1814 | exposes IPython's processing machinery, the given strings can contain |
|
1811 | exposes IPython's processing machinery, the given strings can contain | |
1815 | magic calls (%magic), special shell access (!cmd), etc. |
|
1812 | magic calls (%magic), special shell access (!cmd), etc. | |
1816 | """ |
|
1813 | """ | |
1817 |
|
1814 | |||
1818 | if isinstance(lines, (list, tuple)): |
|
1815 | if isinstance(lines, (list, tuple)): | |
1819 | lines = '\n'.join(lines) |
|
1816 | lines = '\n'.join(lines) | |
1820 |
|
1817 | |||
1821 | if clean: |
|
1818 | if clean: | |
1822 | lines = self._cleanup_ipy_script(lines) |
|
1819 | lines = self._cleanup_ipy_script(lines) | |
1823 |
|
1820 | |||
1824 | # We must start with a clean buffer, in case this is run from an |
|
1821 | # We must start with a clean buffer, in case this is run from an | |
1825 | # interactive IPython session (via a magic, for example). |
|
1822 | # interactive IPython session (via a magic, for example). | |
1826 | self.resetbuffer() |
|
1823 | self.resetbuffer() | |
1827 | lines = lines.splitlines() |
|
1824 | lines = lines.splitlines() | |
1828 | more = 0 |
|
1825 | more = 0 | |
1829 |
|
1826 | |||
1830 | with nested(self.builtin_trap, self.display_trap): |
|
1827 | with nested(self.builtin_trap, self.display_trap): | |
1831 | for line in lines: |
|
1828 | for line in lines: | |
1832 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1829 | # skip blank lines so we don't mess up the prompt counter, but do | |
1833 | # NOT skip even a blank line if we are in a code block (more is |
|
1830 | # NOT skip even a blank line if we are in a code block (more is | |
1834 | # true) |
|
1831 | # true) | |
1835 |
|
1832 | |||
1836 | if line or more: |
|
1833 | if line or more: | |
1837 | # push to raw history, so hist line numbers stay in sync |
|
1834 | # push to raw history, so hist line numbers stay in sync | |
1838 | self.input_hist_raw.append("# " + line + "\n") |
|
1835 | self.input_hist_raw.append("# " + line + "\n") | |
1839 | prefiltered = self.prefilter_manager.prefilter_lines(line,more) |
|
1836 | prefiltered = self.prefilter_manager.prefilter_lines(line,more) | |
1840 | more = self.push_line(prefiltered) |
|
1837 | more = self.push_line(prefiltered) | |
1841 | # IPython's runsource returns None if there was an error |
|
1838 | # IPython's runsource returns None if there was an error | |
1842 | # compiling the code. This allows us to stop processing right |
|
1839 | # compiling the code. This allows us to stop processing right | |
1843 | # away, so the user gets the error message at the right place. |
|
1840 | # away, so the user gets the error message at the right place. | |
1844 | if more is None: |
|
1841 | if more is None: | |
1845 | break |
|
1842 | break | |
1846 | else: |
|
1843 | else: | |
1847 | self.input_hist_raw.append("\n") |
|
1844 | self.input_hist_raw.append("\n") | |
1848 | # final newline in case the input didn't have it, so that the code |
|
1845 | # final newline in case the input didn't have it, so that the code | |
1849 | # actually does get executed |
|
1846 | # actually does get executed | |
1850 | if more: |
|
1847 | if more: | |
1851 | self.push_line('\n') |
|
1848 | self.push_line('\n') | |
1852 |
|
1849 | |||
1853 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1850 | def runsource(self, source, filename='<input>', symbol='single'): | |
1854 | """Compile and run some source in the interpreter. |
|
1851 | """Compile and run some source in the interpreter. | |
1855 |
|
1852 | |||
1856 | Arguments are as for compile_command(). |
|
1853 | Arguments are as for compile_command(). | |
1857 |
|
1854 | |||
1858 | One several things can happen: |
|
1855 | One several things can happen: | |
1859 |
|
1856 | |||
1860 | 1) The input is incorrect; compile_command() raised an |
|
1857 | 1) The input is incorrect; compile_command() raised an | |
1861 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1858 | exception (SyntaxError or OverflowError). A syntax traceback | |
1862 | will be printed by calling the showsyntaxerror() method. |
|
1859 | will be printed by calling the showsyntaxerror() method. | |
1863 |
|
1860 | |||
1864 | 2) The input is incomplete, and more input is required; |
|
1861 | 2) The input is incomplete, and more input is required; | |
1865 | compile_command() returned None. Nothing happens. |
|
1862 | compile_command() returned None. Nothing happens. | |
1866 |
|
1863 | |||
1867 | 3) The input is complete; compile_command() returned a code |
|
1864 | 3) The input is complete; compile_command() returned a code | |
1868 | object. The code is executed by calling self.runcode() (which |
|
1865 | object. The code is executed by calling self.runcode() (which | |
1869 | also handles run-time exceptions, except for SystemExit). |
|
1866 | also handles run-time exceptions, except for SystemExit). | |
1870 |
|
1867 | |||
1871 | The return value is: |
|
1868 | The return value is: | |
1872 |
|
1869 | |||
1873 | - True in case 2 |
|
1870 | - True in case 2 | |
1874 |
|
1871 | |||
1875 | - False in the other cases, unless an exception is raised, where |
|
1872 | - False in the other cases, unless an exception is raised, where | |
1876 | None is returned instead. This can be used by external callers to |
|
1873 | None is returned instead. This can be used by external callers to | |
1877 | know whether to continue feeding input or not. |
|
1874 | know whether to continue feeding input or not. | |
1878 |
|
1875 | |||
1879 | The return value can be used to decide whether to use sys.ps1 or |
|
1876 | The return value can be used to decide whether to use sys.ps1 or | |
1880 | sys.ps2 to prompt the next line.""" |
|
1877 | sys.ps2 to prompt the next line.""" | |
1881 |
|
1878 | |||
1882 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
1879 | # if the source code has leading blanks, add 'if 1:\n' to it | |
1883 | # this allows execution of indented pasted code. It is tempting |
|
1880 | # this allows execution of indented pasted code. It is tempting | |
1884 | # to add '\n' at the end of source to run commands like ' a=1' |
|
1881 | # to add '\n' at the end of source to run commands like ' a=1' | |
1885 | # directly, but this fails for more complicated scenarios |
|
1882 | # directly, but this fails for more complicated scenarios | |
1886 | source=source.encode(self.stdin_encoding) |
|
1883 | source=source.encode(self.stdin_encoding) | |
1887 | if source[:1] in [' ', '\t']: |
|
1884 | if source[:1] in [' ', '\t']: | |
1888 | source = 'if 1:\n%s' % source |
|
1885 | source = 'if 1:\n%s' % source | |
1889 |
|
1886 | |||
1890 | try: |
|
1887 | try: | |
1891 | code = self.compile(source,filename,symbol) |
|
1888 | code = self.compile(source,filename,symbol) | |
1892 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): |
|
1889 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): | |
1893 | # Case 1 |
|
1890 | # Case 1 | |
1894 | self.showsyntaxerror(filename) |
|
1891 | self.showsyntaxerror(filename) | |
1895 | return None |
|
1892 | return None | |
1896 |
|
1893 | |||
1897 | if code is None: |
|
1894 | if code is None: | |
1898 | # Case 2 |
|
1895 | # Case 2 | |
1899 | return True |
|
1896 | return True | |
1900 |
|
1897 | |||
1901 | # Case 3 |
|
1898 | # Case 3 | |
1902 | # We store the code object so that threaded shells and |
|
1899 | # We store the code object so that threaded shells and | |
1903 | # custom exception handlers can access all this info if needed. |
|
1900 | # custom exception handlers can access all this info if needed. | |
1904 | # The source corresponding to this can be obtained from the |
|
1901 | # The source corresponding to this can be obtained from the | |
1905 | # buffer attribute as '\n'.join(self.buffer). |
|
1902 | # buffer attribute as '\n'.join(self.buffer). | |
1906 | self.code_to_run = code |
|
1903 | self.code_to_run = code | |
1907 | # now actually execute the code object |
|
1904 | # now actually execute the code object | |
1908 | if self.runcode(code) == 0: |
|
1905 | if self.runcode(code) == 0: | |
1909 | return False |
|
1906 | return False | |
1910 | else: |
|
1907 | else: | |
1911 | return None |
|
1908 | return None | |
1912 |
|
1909 | |||
1913 | def runcode(self,code_obj): |
|
1910 | def runcode(self,code_obj): | |
1914 | """Execute a code object. |
|
1911 | """Execute a code object. | |
1915 |
|
1912 | |||
1916 | When an exception occurs, self.showtraceback() is called to display a |
|
1913 | When an exception occurs, self.showtraceback() is called to display a | |
1917 | traceback. |
|
1914 | traceback. | |
1918 |
|
1915 | |||
1919 | Return value: a flag indicating whether the code to be run completed |
|
1916 | Return value: a flag indicating whether the code to be run completed | |
1920 | successfully: |
|
1917 | successfully: | |
1921 |
|
1918 | |||
1922 | - 0: successful execution. |
|
1919 | - 0: successful execution. | |
1923 | - 1: an error occurred. |
|
1920 | - 1: an error occurred. | |
1924 | """ |
|
1921 | """ | |
1925 |
|
1922 | |||
1926 | # Set our own excepthook in case the user code tries to call it |
|
1923 | # Set our own excepthook in case the user code tries to call it | |
1927 | # directly, so that the IPython crash handler doesn't get triggered |
|
1924 | # directly, so that the IPython crash handler doesn't get triggered | |
1928 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1925 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
1929 |
|
1926 | |||
1930 | # we save the original sys.excepthook in the instance, in case config |
|
1927 | # we save the original sys.excepthook in the instance, in case config | |
1931 | # code (such as magics) needs access to it. |
|
1928 | # code (such as magics) needs access to it. | |
1932 | self.sys_excepthook = old_excepthook |
|
1929 | self.sys_excepthook = old_excepthook | |
1933 | outflag = 1 # happens in more places, so it's easier as default |
|
1930 | outflag = 1 # happens in more places, so it's easier as default | |
1934 | try: |
|
1931 | try: | |
1935 | try: |
|
1932 | try: | |
1936 | self.hooks.pre_runcode_hook() |
|
1933 | self.hooks.pre_runcode_hook() | |
1937 | #rprint('Running code') # dbg |
|
1934 | #rprint('Running code') # dbg | |
1938 | exec code_obj in self.user_global_ns, self.user_ns |
|
1935 | exec code_obj in self.user_global_ns, self.user_ns | |
1939 | finally: |
|
1936 | finally: | |
1940 | # Reset our crash handler in place |
|
1937 | # Reset our crash handler in place | |
1941 | sys.excepthook = old_excepthook |
|
1938 | sys.excepthook = old_excepthook | |
1942 | except SystemExit: |
|
1939 | except SystemExit: | |
1943 | self.resetbuffer() |
|
1940 | self.resetbuffer() | |
1944 | self.showtraceback(exception_only=True) |
|
1941 | self.showtraceback(exception_only=True) | |
1945 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) |
|
1942 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) | |
1946 | except self.custom_exceptions: |
|
1943 | except self.custom_exceptions: | |
1947 | etype,value,tb = sys.exc_info() |
|
1944 | etype,value,tb = sys.exc_info() | |
1948 | self.CustomTB(etype,value,tb) |
|
1945 | self.CustomTB(etype,value,tb) | |
1949 | except: |
|
1946 | except: | |
1950 | self.showtraceback() |
|
1947 | self.showtraceback() | |
1951 | else: |
|
1948 | else: | |
1952 | outflag = 0 |
|
1949 | outflag = 0 | |
1953 | if softspace(sys.stdout, 0): |
|
1950 | if softspace(sys.stdout, 0): | |
1954 |
|
1951 | |||
1955 | # Flush out code object which has been run (and source) |
|
1952 | # Flush out code object which has been run (and source) | |
1956 | self.code_to_run = None |
|
1953 | self.code_to_run = None | |
1957 | return outflag |
|
1954 | return outflag | |
1958 |
|
1955 | |||
1959 | def push_line(self, line): |
|
1956 | def push_line(self, line): | |
1960 | """Push a line to the interpreter. |
|
1957 | """Push a line to the interpreter. | |
1961 |
|
1958 | |||
1962 | The line should not have a trailing newline; it may have |
|
1959 | The line should not have a trailing newline; it may have | |
1963 | internal newlines. The line is appended to a buffer and the |
|
1960 | internal newlines. The line is appended to a buffer and the | |
1964 | interpreter's runsource() method is called with the |
|
1961 | interpreter's runsource() method is called with the | |
1965 | concatenated contents of the buffer as source. If this |
|
1962 | concatenated contents of the buffer as source. If this | |
1966 | indicates that the command was executed or invalid, the buffer |
|
1963 | indicates that the command was executed or invalid, the buffer | |
1967 | is reset; otherwise, the command is incomplete, and the buffer |
|
1964 | is reset; otherwise, the command is incomplete, and the buffer | |
1968 | is left as it was after the line was appended. The return |
|
1965 | is left as it was after the line was appended. The return | |
1969 | value is 1 if more input is required, 0 if the line was dealt |
|
1966 | value is 1 if more input is required, 0 if the line was dealt | |
1970 | with in some way (this is the same as runsource()). |
|
1967 | with in some way (this is the same as runsource()). | |
1971 | """ |
|
1968 | """ | |
1972 |
|
1969 | |||
1973 | # autoindent management should be done here, and not in the |
|
1970 | # autoindent management should be done here, and not in the | |
1974 | # interactive loop, since that one is only seen by keyboard input. We |
|
1971 | # interactive loop, since that one is only seen by keyboard input. We | |
1975 | # need this done correctly even for code run via runlines (which uses |
|
1972 | # need this done correctly even for code run via runlines (which uses | |
1976 | # push). |
|
1973 | # push). | |
1977 |
|
1974 | |||
1978 | #print 'push line: <%s>' % line # dbg |
|
1975 | #print 'push line: <%s>' % line # dbg | |
1979 | for subline in line.splitlines(): |
|
1976 | for subline in line.splitlines(): | |
1980 | self._autoindent_update(subline) |
|
1977 | self._autoindent_update(subline) | |
1981 | self.buffer.append(line) |
|
1978 | self.buffer.append(line) | |
1982 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1979 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
1983 | if not more: |
|
1980 | if not more: | |
1984 | self.resetbuffer() |
|
1981 | self.resetbuffer() | |
1985 | return more |
|
1982 | return more | |
1986 |
|
1983 | |||
1987 | def resetbuffer(self): |
|
1984 | def resetbuffer(self): | |
1988 | """Reset the input buffer.""" |
|
1985 | """Reset the input buffer.""" | |
1989 | self.buffer[:] = [] |
|
1986 | self.buffer[:] = [] | |
1990 |
|
1987 | |||
1991 | def _is_secondary_block_start(self, s): |
|
1988 | def _is_secondary_block_start(self, s): | |
1992 | if not s.endswith(':'): |
|
1989 | if not s.endswith(':'): | |
1993 | return False |
|
1990 | return False | |
1994 | if (s.startswith('elif') or |
|
1991 | if (s.startswith('elif') or | |
1995 | s.startswith('else') or |
|
1992 | s.startswith('else') or | |
1996 | s.startswith('except') or |
|
1993 | s.startswith('except') or | |
1997 | s.startswith('finally')): |
|
1994 | s.startswith('finally')): | |
1998 | return True |
|
1995 | return True | |
1999 |
|
1996 | |||
2000 | def _cleanup_ipy_script(self, script): |
|
1997 | def _cleanup_ipy_script(self, script): | |
2001 | """Make a script safe for self.runlines() |
|
1998 | """Make a script safe for self.runlines() | |
2002 |
|
1999 | |||
2003 | Currently, IPython is lines based, with blocks being detected by |
|
2000 | Currently, IPython is lines based, with blocks being detected by | |
2004 | empty lines. This is a problem for block based scripts that may |
|
2001 | empty lines. This is a problem for block based scripts that may | |
2005 | not have empty lines after blocks. This script adds those empty |
|
2002 | not have empty lines after blocks. This script adds those empty | |
2006 | lines to make scripts safe for running in the current line based |
|
2003 | lines to make scripts safe for running in the current line based | |
2007 | IPython. |
|
2004 | IPython. | |
2008 | """ |
|
2005 | """ | |
2009 | res = [] |
|
2006 | res = [] | |
2010 | lines = script.splitlines() |
|
2007 | lines = script.splitlines() | |
2011 | level = 0 |
|
2008 | level = 0 | |
2012 |
|
2009 | |||
2013 | for l in lines: |
|
2010 | for l in lines: | |
2014 | lstripped = l.lstrip() |
|
2011 | lstripped = l.lstrip() | |
2015 | stripped = l.strip() |
|
2012 | stripped = l.strip() | |
2016 | if not stripped: |
|
2013 | if not stripped: | |
2017 | continue |
|
2014 | continue | |
2018 | newlevel = len(l) - len(lstripped) |
|
2015 | newlevel = len(l) - len(lstripped) | |
2019 | if level > 0 and newlevel == 0 and \ |
|
2016 | if level > 0 and newlevel == 0 and \ | |
2020 | not self._is_secondary_block_start(stripped): |
|
2017 | not self._is_secondary_block_start(stripped): | |
2021 | # add empty line |
|
2018 | # add empty line | |
2022 | res.append('') |
|
2019 | res.append('') | |
2023 | res.append(l) |
|
2020 | res.append(l) | |
2024 | level = newlevel |
|
2021 | level = newlevel | |
2025 |
|
2022 | |||
2026 | return '\n'.join(res) + '\n' |
|
2023 | return '\n'.join(res) + '\n' | |
2027 |
|
2024 | |||
2028 | def _autoindent_update(self,line): |
|
2025 | def _autoindent_update(self,line): | |
2029 | """Keep track of the indent level.""" |
|
2026 | """Keep track of the indent level.""" | |
2030 |
|
2027 | |||
2031 | #debugx('line') |
|
2028 | #debugx('line') | |
2032 | #debugx('self.indent_current_nsp') |
|
2029 | #debugx('self.indent_current_nsp') | |
2033 | if self.autoindent: |
|
2030 | if self.autoindent: | |
2034 | if line: |
|
2031 | if line: | |
2035 | inisp = num_ini_spaces(line) |
|
2032 | inisp = num_ini_spaces(line) | |
2036 | if inisp < self.indent_current_nsp: |
|
2033 | if inisp < self.indent_current_nsp: | |
2037 | self.indent_current_nsp = inisp |
|
2034 | self.indent_current_nsp = inisp | |
2038 |
|
2035 | |||
2039 | if line[-1] == ':': |
|
2036 | if line[-1] == ':': | |
2040 | self.indent_current_nsp += 4 |
|
2037 | self.indent_current_nsp += 4 | |
2041 | elif dedent_re.match(line): |
|
2038 | elif dedent_re.match(line): | |
2042 | self.indent_current_nsp -= 4 |
|
2039 | self.indent_current_nsp -= 4 | |
2043 | else: |
|
2040 | else: | |
2044 | self.indent_current_nsp = 0 |
|
2041 | self.indent_current_nsp = 0 | |
2045 |
|
2042 | |||
2046 | #------------------------------------------------------------------------- |
|
2043 | #------------------------------------------------------------------------- | |
2047 | # Things related to GUI support and pylab |
|
2044 | # Things related to GUI support and pylab | |
2048 | #------------------------------------------------------------------------- |
|
2045 | #------------------------------------------------------------------------- | |
2049 |
|
2046 | |||
2050 | def enable_pylab(self, gui=None): |
|
2047 | def enable_pylab(self, gui=None): | |
2051 | raise NotImplementedError('Implement enable_pylab in a subclass') |
|
2048 | raise NotImplementedError('Implement enable_pylab in a subclass') | |
2052 |
|
2049 | |||
2053 | #------------------------------------------------------------------------- |
|
2050 | #------------------------------------------------------------------------- | |
2054 | # Utilities |
|
2051 | # Utilities | |
2055 | #------------------------------------------------------------------------- |
|
2052 | #------------------------------------------------------------------------- | |
2056 |
|
2053 | |||
2057 | def getoutput(self, cmd): |
|
2054 | def getoutput(self, cmd): | |
2058 | return getoutput(self.var_expand(cmd,depth=2), |
|
2055 | return getoutput(self.var_expand(cmd,depth=2), | |
2059 | header=self.system_header, |
|
2056 | header=self.system_header, | |
2060 | verbose=self.system_verbose) |
|
2057 | verbose=self.system_verbose) | |
2061 |
|
2058 | |||
2062 | def getoutputerror(self, cmd): |
|
2059 | def getoutputerror(self, cmd): | |
2063 | return getoutputerror(self.var_expand(cmd,depth=2), |
|
2060 | return getoutputerror(self.var_expand(cmd,depth=2), | |
2064 | header=self.system_header, |
|
2061 | header=self.system_header, | |
2065 | verbose=self.system_verbose) |
|
2062 | verbose=self.system_verbose) | |
2066 |
|
2063 | |||
2067 | def var_expand(self,cmd,depth=0): |
|
2064 | def var_expand(self,cmd,depth=0): | |
2068 | """Expand python variables in a string. |
|
2065 | """Expand python variables in a string. | |
2069 |
|
2066 | |||
2070 | The depth argument indicates how many frames above the caller should |
|
2067 | The depth argument indicates how many frames above the caller should | |
2071 | be walked to look for the local namespace where to expand variables. |
|
2068 | be walked to look for the local namespace where to expand variables. | |
2072 |
|
2069 | |||
2073 | The global namespace for expansion is always the user's interactive |
|
2070 | The global namespace for expansion is always the user's interactive | |
2074 | namespace. |
|
2071 | namespace. | |
2075 | """ |
|
2072 | """ | |
2076 |
|
2073 | |||
2077 | return str(ItplNS(cmd, |
|
2074 | return str(ItplNS(cmd, | |
2078 | self.user_ns, # globals |
|
2075 | self.user_ns, # globals | |
2079 | # Skip our own frame in searching for locals: |
|
2076 | # Skip our own frame in searching for locals: | |
2080 | sys._getframe(depth+1).f_locals # locals |
|
2077 | sys._getframe(depth+1).f_locals # locals | |
2081 | )) |
|
2078 | )) | |
2082 |
|
2079 | |||
2083 | def mktempfile(self,data=None): |
|
2080 | def mktempfile(self,data=None): | |
2084 | """Make a new tempfile and return its filename. |
|
2081 | """Make a new tempfile and return its filename. | |
2085 |
|
2082 | |||
2086 | This makes a call to tempfile.mktemp, but it registers the created |
|
2083 | This makes a call to tempfile.mktemp, but it registers the created | |
2087 | filename internally so ipython cleans it up at exit time. |
|
2084 | filename internally so ipython cleans it up at exit time. | |
2088 |
|
2085 | |||
2089 | Optional inputs: |
|
2086 | Optional inputs: | |
2090 |
|
2087 | |||
2091 | - data(None): if data is given, it gets written out to the temp file |
|
2088 | - data(None): if data is given, it gets written out to the temp file | |
2092 | immediately, and the file is closed again.""" |
|
2089 | immediately, and the file is closed again.""" | |
2093 |
|
2090 | |||
2094 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2091 | filename = tempfile.mktemp('.py','ipython_edit_') | |
2095 | self.tempfiles.append(filename) |
|
2092 | self.tempfiles.append(filename) | |
2096 |
|
2093 | |||
2097 | if data: |
|
2094 | if data: | |
2098 | tmp_file = open(filename,'w') |
|
2095 | tmp_file = open(filename,'w') | |
2099 | tmp_file.write(data) |
|
2096 | tmp_file.write(data) | |
2100 | tmp_file.close() |
|
2097 | tmp_file.close() | |
2101 | return filename |
|
2098 | return filename | |
2102 |
|
2099 | |||
2103 | # TODO: This should be removed when Term is refactored. |
|
2100 | # TODO: This should be removed when Term is refactored. | |
2104 | def write(self,data): |
|
2101 | def write(self,data): | |
2105 | """Write a string to the default output""" |
|
2102 | """Write a string to the default output""" | |
2106 | io.Term.cout.write(data) |
|
2103 | io.Term.cout.write(data) | |
2107 |
|
2104 | |||
2108 | # TODO: This should be removed when Term is refactored. |
|
2105 | # TODO: This should be removed when Term is refactored. | |
2109 | def write_err(self,data): |
|
2106 | def write_err(self,data): | |
2110 | """Write a string to the default error output""" |
|
2107 | """Write a string to the default error output""" | |
2111 | io.Term.cerr.write(data) |
|
2108 | io.Term.cerr.write(data) | |
2112 |
|
2109 | |||
2113 | def ask_yes_no(self,prompt,default=True): |
|
2110 | def ask_yes_no(self,prompt,default=True): | |
2114 | if self.quiet: |
|
2111 | if self.quiet: | |
2115 | return True |
|
2112 | return True | |
2116 | return ask_yes_no(prompt,default) |
|
2113 | return ask_yes_no(prompt,default) | |
2117 |
|
2114 | |||
2118 | #------------------------------------------------------------------------- |
|
2115 | #------------------------------------------------------------------------- | |
2119 | # Things related to IPython exiting |
|
2116 | # Things related to IPython exiting | |
2120 | #------------------------------------------------------------------------- |
|
2117 | #------------------------------------------------------------------------- | |
2121 |
|
2118 | |||
2122 | def atexit_operations(self): |
|
2119 | def atexit_operations(self): | |
2123 | """This will be executed at the time of exit. |
|
2120 | """This will be executed at the time of exit. | |
2124 |
|
2121 | |||
2125 | Saving of persistent data should be performed here. |
|
2122 | Saving of persistent data should be performed here. | |
2126 | """ |
|
2123 | """ | |
2127 | self.savehist() |
|
2124 | self.savehist() | |
2128 |
|
2125 | |||
2129 | # Cleanup all tempfiles left around |
|
2126 | # Cleanup all tempfiles left around | |
2130 | for tfile in self.tempfiles: |
|
2127 | for tfile in self.tempfiles: | |
2131 | try: |
|
2128 | try: | |
2132 | os.unlink(tfile) |
|
2129 | os.unlink(tfile) | |
2133 | except OSError: |
|
2130 | except OSError: | |
2134 | pass |
|
2131 | pass | |
2135 |
|
2132 | |||
2136 | # Clear all user namespaces to release all references cleanly. |
|
2133 | # Clear all user namespaces to release all references cleanly. | |
2137 | self.reset() |
|
2134 | self.reset() | |
2138 |
|
2135 | |||
2139 | # Run user hooks |
|
2136 | # Run user hooks | |
2140 | self.hooks.shutdown_hook() |
|
2137 | self.hooks.shutdown_hook() | |
2141 |
|
2138 | |||
2142 | def cleanup(self): |
|
2139 | def cleanup(self): | |
2143 | self.restore_sys_module_state() |
|
2140 | self.restore_sys_module_state() | |
2144 |
|
2141 | |||
2145 |
|
2142 | |||
2146 | class InteractiveShellABC(object): |
|
2143 | class InteractiveShellABC(object): | |
2147 | """An abstract base class for InteractiveShell.""" |
|
2144 | """An abstract base class for InteractiveShell.""" | |
2148 | __metaclass__ = abc.ABCMeta |
|
2145 | __metaclass__ = abc.ABCMeta | |
2149 |
|
2146 | |||
2150 | InteractiveShellABC.register(InteractiveShell) |
|
2147 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,1202 +1,1201 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | ultratb.py -- Spice up your tracebacks! |
|
3 | ultratb.py -- Spice up your tracebacks! | |
4 |
|
4 | |||
5 | * ColorTB |
|
5 | * ColorTB | |
6 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
6 | I've always found it a bit hard to visually parse tracebacks in Python. The | |
7 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
7 | ColorTB class is a solution to that problem. It colors the different parts of a | |
8 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
8 | traceback in a manner similar to what you would expect from a syntax-highlighting | |
9 | text editor. |
|
9 | text editor. | |
10 |
|
10 | |||
11 | Installation instructions for ColorTB: |
|
11 | Installation instructions for ColorTB: | |
12 | import sys,ultratb |
|
12 | import sys,ultratb | |
13 | sys.excepthook = ultratb.ColorTB() |
|
13 | sys.excepthook = ultratb.ColorTB() | |
14 |
|
14 | |||
15 | * VerboseTB |
|
15 | * VerboseTB | |
16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds | |
17 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
17 | of useful info when a traceback occurs. Ping originally had it spit out HTML | |
18 | and intended it for CGI programmers, but why should they have all the fun? I |
|
18 | and intended it for CGI programmers, but why should they have all the fun? I | |
19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, | |
20 | but kind of neat, and maybe useful for long-running programs that you believe |
|
20 | but kind of neat, and maybe useful for long-running programs that you believe | |
21 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
21 | are bug-free. If a crash *does* occur in that type of program you want details. | |
22 | Give it a shot--you'll love it or you'll hate it. |
|
22 | Give it a shot--you'll love it or you'll hate it. | |
23 |
|
23 | |||
24 | Note: |
|
24 | Note: | |
25 |
|
25 | |||
26 | The Verbose mode prints the variables currently visible where the exception |
|
26 | The Verbose mode prints the variables currently visible where the exception | |
27 | happened (shortening their strings if too long). This can potentially be |
|
27 | happened (shortening their strings if too long). This can potentially be | |
28 | very slow, if you happen to have a huge data structure whose string |
|
28 | very slow, if you happen to have a huge data structure whose string | |
29 | representation is complex to compute. Your computer may appear to freeze for |
|
29 | representation is complex to compute. Your computer may appear to freeze for | |
30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback | |
31 | with Ctrl-C (maybe hitting it more than once). |
|
31 | with Ctrl-C (maybe hitting it more than once). | |
32 |
|
32 | |||
33 | If you encounter this kind of situation often, you may want to use the |
|
33 | If you encounter this kind of situation often, you may want to use the | |
34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting | |
35 | variables (but otherwise includes the information and context given by |
|
35 | variables (but otherwise includes the information and context given by | |
36 | Verbose). |
|
36 | Verbose). | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | Installation instructions for ColorTB: |
|
39 | Installation instructions for ColorTB: | |
40 | import sys,ultratb |
|
40 | import sys,ultratb | |
41 | sys.excepthook = ultratb.VerboseTB() |
|
41 | sys.excepthook = ultratb.VerboseTB() | |
42 |
|
42 | |||
43 | Note: Much of the code in this module was lifted verbatim from the standard |
|
43 | Note: Much of the code in this module was lifted verbatim from the standard | |
44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. | |
45 |
|
45 | |||
46 | * Color schemes |
|
46 | * Color schemes | |
47 | The colors are defined in the class TBTools through the use of the |
|
47 | The colors are defined in the class TBTools through the use of the | |
48 | ColorSchemeTable class. Currently the following exist: |
|
48 | ColorSchemeTable class. Currently the following exist: | |
49 |
|
49 | |||
50 | - NoColor: allows all of this module to be used in any terminal (the color |
|
50 | - NoColor: allows all of this module to be used in any terminal (the color | |
51 | escapes are just dummy blank strings). |
|
51 | escapes are just dummy blank strings). | |
52 |
|
52 | |||
53 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
53 | - Linux: is meant to look good in a terminal like the Linux console (black | |
54 | or very dark background). |
|
54 | or very dark background). | |
55 |
|
55 | |||
56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable | |
57 | in light background terminals. |
|
57 | in light background terminals. | |
58 |
|
58 | |||
59 | You can implement other color schemes easily, the syntax is fairly |
|
59 | You can implement other color schemes easily, the syntax is fairly | |
60 | self-explanatory. Please send back new schemes you develop to the author for |
|
60 | self-explanatory. Please send back new schemes you develop to the author for | |
61 | possible inclusion in future releases. |
|
61 | possible inclusion in future releases. | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | #***************************************************************************** |
|
64 | #***************************************************************************** | |
65 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
65 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
66 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
66 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
67 | # |
|
67 | # | |
68 | # Distributed under the terms of the BSD License. The full license is in |
|
68 | # Distributed under the terms of the BSD License. The full license is in | |
69 | # the file COPYING, distributed as part of this software. |
|
69 | # the file COPYING, distributed as part of this software. | |
70 | #***************************************************************************** |
|
70 | #***************************************************************************** | |
71 |
|
71 | |||
72 | from __future__ import with_statement |
|
72 | from __future__ import with_statement | |
73 |
|
73 | |||
74 | import inspect |
|
74 | import inspect | |
75 | import keyword |
|
75 | import keyword | |
76 | import linecache |
|
76 | import linecache | |
77 | import os |
|
77 | import os | |
78 | import pydoc |
|
78 | import pydoc | |
79 | import re |
|
79 | import re | |
80 | import string |
|
80 | import string | |
81 | import sys |
|
81 | import sys | |
82 | import time |
|
82 | import time | |
83 | import tokenize |
|
83 | import tokenize | |
84 | import traceback |
|
84 | import traceback | |
85 | import types |
|
85 | import types | |
86 |
|
86 | |||
87 | # For purposes of monkeypatching inspect to fix a bug in it. |
|
87 | # For purposes of monkeypatching inspect to fix a bug in it. | |
88 | from inspect import getsourcefile, getfile, getmodule,\ |
|
88 | from inspect import getsourcefile, getfile, getmodule,\ | |
89 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode |
|
89 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode | |
90 |
|
90 | |||
91 | # IPython's own modules |
|
91 | # IPython's own modules | |
92 | # Modified pdb which doesn't damage IPython's readline handling |
|
92 | # Modified pdb which doesn't damage IPython's readline handling | |
93 | from IPython.core import debugger, ipapi |
|
93 | from IPython.core import debugger, ipapi | |
94 | from IPython.core.display_trap import DisplayTrap |
|
94 | from IPython.core.display_trap import DisplayTrap | |
95 | from IPython.core.excolors import exception_colors |
|
95 | from IPython.core.excolors import exception_colors | |
96 | from IPython.utils import PyColorize |
|
96 | from IPython.utils import PyColorize | |
97 | from IPython.utils import io |
|
97 | from IPython.utils import io | |
98 | from IPython.utils.data import uniq_stable |
|
98 | from IPython.utils.data import uniq_stable | |
99 | from IPython.utils.warn import info, error |
|
99 | from IPython.utils.warn import info, error | |
100 |
|
100 | |||
101 | # Globals |
|
101 | # Globals | |
102 | # amount of space to put line numbers before verbose tracebacks |
|
102 | # amount of space to put line numbers before verbose tracebacks | |
103 | INDENT_SIZE = 8 |
|
103 | INDENT_SIZE = 8 | |
104 |
|
104 | |||
105 | # Default color scheme. This is used, for example, by the traceback |
|
105 | # Default color scheme. This is used, for example, by the traceback | |
106 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
106 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
107 | # value is used, but havinga module global makes this functionality available |
|
107 | # value is used, but havinga module global makes this functionality available | |
108 | # to users of ultratb who are NOT running inside ipython. |
|
108 | # to users of ultratb who are NOT running inside ipython. | |
109 | DEFAULT_SCHEME = 'NoColor' |
|
109 | DEFAULT_SCHEME = 'NoColor' | |
110 |
|
110 | |||
111 | #--------------------------------------------------------------------------- |
|
111 | #--------------------------------------------------------------------------- | |
112 | # Code begins |
|
112 | # Code begins | |
113 |
|
113 | |||
114 | # Utility functions |
|
114 | # Utility functions | |
115 | def inspect_error(): |
|
115 | def inspect_error(): | |
116 | """Print a message about internal inspect errors. |
|
116 | """Print a message about internal inspect errors. | |
117 |
|
117 | |||
118 | These are unfortunately quite common.""" |
|
118 | These are unfortunately quite common.""" | |
119 |
|
119 | |||
120 | error('Internal Python error in the inspect module.\n' |
|
120 | error('Internal Python error in the inspect module.\n' | |
121 | 'Below is the traceback from this internal error.\n') |
|
121 | 'Below is the traceback from this internal error.\n') | |
122 |
|
122 | |||
123 |
|
123 | |||
124 | def findsource(object): |
|
124 | def findsource(object): | |
125 | """Return the entire source file and starting line number for an object. |
|
125 | """Return the entire source file and starting line number for an object. | |
126 |
|
126 | |||
127 | The argument may be a module, class, method, function, traceback, frame, |
|
127 | The argument may be a module, class, method, function, traceback, frame, | |
128 | or code object. The source code is returned as a list of all the lines |
|
128 | or code object. The source code is returned as a list of all the lines | |
129 | in the file and the line number indexes a line in that list. An IOError |
|
129 | in the file and the line number indexes a line in that list. An IOError | |
130 | is raised if the source code cannot be retrieved. |
|
130 | is raised if the source code cannot be retrieved. | |
131 |
|
131 | |||
132 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" |
|
132 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" | |
133 |
|
133 | |||
134 | file = getsourcefile(object) or getfile(object) |
|
134 | file = getsourcefile(object) or getfile(object) | |
135 | # If the object is a frame, then trying to get the globals dict from its |
|
135 | # If the object is a frame, then trying to get the globals dict from its | |
136 | # module won't work. Instead, the frame object itself has the globals |
|
136 | # module won't work. Instead, the frame object itself has the globals | |
137 | # dictionary. |
|
137 | # dictionary. | |
138 | globals_dict = None |
|
138 | globals_dict = None | |
139 | if inspect.isframe(object): |
|
139 | if inspect.isframe(object): | |
140 | # XXX: can this ever be false? |
|
140 | # XXX: can this ever be false? | |
141 | globals_dict = object.f_globals |
|
141 | globals_dict = object.f_globals | |
142 | else: |
|
142 | else: | |
143 | module = getmodule(object, file) |
|
143 | module = getmodule(object, file) | |
144 | if module: |
|
144 | if module: | |
145 | globals_dict = module.__dict__ |
|
145 | globals_dict = module.__dict__ | |
146 | lines = linecache.getlines(file, globals_dict) |
|
146 | lines = linecache.getlines(file, globals_dict) | |
147 | if not lines: |
|
147 | if not lines: | |
148 | raise IOError('could not get source code') |
|
148 | raise IOError('could not get source code') | |
149 |
|
149 | |||
150 | if ismodule(object): |
|
150 | if ismodule(object): | |
151 | return lines, 0 |
|
151 | return lines, 0 | |
152 |
|
152 | |||
153 | if isclass(object): |
|
153 | if isclass(object): | |
154 | name = object.__name__ |
|
154 | name = object.__name__ | |
155 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
155 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') | |
156 | # make some effort to find the best matching class definition: |
|
156 | # make some effort to find the best matching class definition: | |
157 | # use the one with the least indentation, which is the one |
|
157 | # use the one with the least indentation, which is the one | |
158 | # that's most probably not inside a function definition. |
|
158 | # that's most probably not inside a function definition. | |
159 | candidates = [] |
|
159 | candidates = [] | |
160 | for i in range(len(lines)): |
|
160 | for i in range(len(lines)): | |
161 | match = pat.match(lines[i]) |
|
161 | match = pat.match(lines[i]) | |
162 | if match: |
|
162 | if match: | |
163 | # if it's at toplevel, it's already the best one |
|
163 | # if it's at toplevel, it's already the best one | |
164 | if lines[i][0] == 'c': |
|
164 | if lines[i][0] == 'c': | |
165 | return lines, i |
|
165 | return lines, i | |
166 | # else add whitespace to candidate list |
|
166 | # else add whitespace to candidate list | |
167 | candidates.append((match.group(1), i)) |
|
167 | candidates.append((match.group(1), i)) | |
168 | if candidates: |
|
168 | if candidates: | |
169 | # this will sort by whitespace, and by line number, |
|
169 | # this will sort by whitespace, and by line number, | |
170 | # less whitespace first |
|
170 | # less whitespace first | |
171 | candidates.sort() |
|
171 | candidates.sort() | |
172 | return lines, candidates[0][1] |
|
172 | return lines, candidates[0][1] | |
173 | else: |
|
173 | else: | |
174 | raise IOError('could not find class definition') |
|
174 | raise IOError('could not find class definition') | |
175 |
|
175 | |||
176 | if ismethod(object): |
|
176 | if ismethod(object): | |
177 | object = object.im_func |
|
177 | object = object.im_func | |
178 | if isfunction(object): |
|
178 | if isfunction(object): | |
179 | object = object.func_code |
|
179 | object = object.func_code | |
180 | if istraceback(object): |
|
180 | if istraceback(object): | |
181 | object = object.tb_frame |
|
181 | object = object.tb_frame | |
182 | if isframe(object): |
|
182 | if isframe(object): | |
183 | object = object.f_code |
|
183 | object = object.f_code | |
184 | if iscode(object): |
|
184 | if iscode(object): | |
185 | if not hasattr(object, 'co_firstlineno'): |
|
185 | if not hasattr(object, 'co_firstlineno'): | |
186 | raise IOError('could not find function definition') |
|
186 | raise IOError('could not find function definition') | |
187 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
187 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') | |
188 | pmatch = pat.match |
|
188 | pmatch = pat.match | |
189 | # fperez - fix: sometimes, co_firstlineno can give a number larger than |
|
189 | # fperez - fix: sometimes, co_firstlineno can give a number larger than | |
190 | # the length of lines, which causes an error. Safeguard against that. |
|
190 | # the length of lines, which causes an error. Safeguard against that. | |
191 | lnum = min(object.co_firstlineno,len(lines))-1 |
|
191 | lnum = min(object.co_firstlineno,len(lines))-1 | |
192 | while lnum > 0: |
|
192 | while lnum > 0: | |
193 | if pmatch(lines[lnum]): break |
|
193 | if pmatch(lines[lnum]): break | |
194 | lnum -= 1 |
|
194 | lnum -= 1 | |
195 |
|
195 | |||
196 | return lines, lnum |
|
196 | return lines, lnum | |
197 | raise IOError('could not find code object') |
|
197 | raise IOError('could not find code object') | |
198 |
|
198 | |||
199 | # Monkeypatch inspect to apply our bugfix. This code only works with py25 |
|
199 | # Monkeypatch inspect to apply our bugfix. This code only works with py25 | |
200 | if sys.version_info[:2] >= (2,5): |
|
200 | if sys.version_info[:2] >= (2,5): | |
201 | inspect.findsource = findsource |
|
201 | inspect.findsource = findsource | |
202 |
|
202 | |||
203 | def fix_frame_records_filenames(records): |
|
203 | def fix_frame_records_filenames(records): | |
204 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
204 | """Try to fix the filenames in each record from inspect.getinnerframes(). | |
205 |
|
205 | |||
206 | Particularly, modules loaded from within zip files have useless filenames |
|
206 | Particularly, modules loaded from within zip files have useless filenames | |
207 | attached to their code object, and inspect.getinnerframes() just uses it. |
|
207 | attached to their code object, and inspect.getinnerframes() just uses it. | |
208 | """ |
|
208 | """ | |
209 | fixed_records = [] |
|
209 | fixed_records = [] | |
210 | for frame, filename, line_no, func_name, lines, index in records: |
|
210 | for frame, filename, line_no, func_name, lines, index in records: | |
211 | # Look inside the frame's globals dictionary for __file__, which should |
|
211 | # Look inside the frame's globals dictionary for __file__, which should | |
212 | # be better. |
|
212 | # be better. | |
213 | better_fn = frame.f_globals.get('__file__', None) |
|
213 | better_fn = frame.f_globals.get('__file__', None) | |
214 | if isinstance(better_fn, str): |
|
214 | if isinstance(better_fn, str): | |
215 | # Check the type just in case someone did something weird with |
|
215 | # Check the type just in case someone did something weird with | |
216 | # __file__. It might also be None if the error occurred during |
|
216 | # __file__. It might also be None if the error occurred during | |
217 | # import. |
|
217 | # import. | |
218 | filename = better_fn |
|
218 | filename = better_fn | |
219 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) |
|
219 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) | |
220 | return fixed_records |
|
220 | return fixed_records | |
221 |
|
221 | |||
222 |
|
222 | |||
223 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): |
|
223 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): | |
224 | import linecache |
|
224 | import linecache | |
225 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
225 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 | |
226 |
|
226 | |||
227 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) |
|
227 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) | |
228 |
|
228 | |||
229 | # If the error is at the console, don't build any context, since it would |
|
229 | # If the error is at the console, don't build any context, since it would | |
230 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
230 | # otherwise produce 5 blank lines printed out (there is no file at the | |
231 | # console) |
|
231 | # console) | |
232 | rec_check = records[tb_offset:] |
|
232 | rec_check = records[tb_offset:] | |
233 | try: |
|
233 | try: | |
234 | rname = rec_check[0][1] |
|
234 | rname = rec_check[0][1] | |
235 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
235 | if rname == '<ipython console>' or rname.endswith('<string>'): | |
236 | return rec_check |
|
236 | return rec_check | |
237 | except IndexError: |
|
237 | except IndexError: | |
238 | pass |
|
238 | pass | |
239 |
|
239 | |||
240 | aux = traceback.extract_tb(etb) |
|
240 | aux = traceback.extract_tb(etb) | |
241 | assert len(records) == len(aux) |
|
241 | assert len(records) == len(aux) | |
242 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): |
|
242 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): | |
243 | maybeStart = lnum-1 - context//2 |
|
243 | maybeStart = lnum-1 - context//2 | |
244 | start = max(maybeStart, 0) |
|
244 | start = max(maybeStart, 0) | |
245 | end = start + context |
|
245 | end = start + context | |
246 | lines = linecache.getlines(file)[start:end] |
|
246 | lines = linecache.getlines(file)[start:end] | |
247 | # pad with empty lines if necessary |
|
247 | # pad with empty lines if necessary | |
248 | if maybeStart < 0: |
|
248 | if maybeStart < 0: | |
249 | lines = (['\n'] * -maybeStart) + lines |
|
249 | lines = (['\n'] * -maybeStart) + lines | |
250 | if len(lines) < context: |
|
250 | if len(lines) < context: | |
251 | lines += ['\n'] * (context - len(lines)) |
|
251 | lines += ['\n'] * (context - len(lines)) | |
252 | buf = list(records[i]) |
|
252 | buf = list(records[i]) | |
253 | buf[LNUM_POS] = lnum |
|
253 | buf[LNUM_POS] = lnum | |
254 | buf[INDEX_POS] = lnum - 1 - start |
|
254 | buf[INDEX_POS] = lnum - 1 - start | |
255 | buf[LINES_POS] = lines |
|
255 | buf[LINES_POS] = lines | |
256 | records[i] = tuple(buf) |
|
256 | records[i] = tuple(buf) | |
257 | return records[tb_offset:] |
|
257 | return records[tb_offset:] | |
258 |
|
258 | |||
259 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
259 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
260 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
260 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
261 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
261 | # can be recognized properly by ipython.el's py-traceback-line-re | |
262 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
262 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
263 |
|
263 | |||
264 | _parser = PyColorize.Parser() |
|
264 | _parser = PyColorize.Parser() | |
265 |
|
265 | |||
266 | def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None): |
|
266 | def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None): | |
267 | numbers_width = INDENT_SIZE - 1 |
|
267 | numbers_width = INDENT_SIZE - 1 | |
268 | res = [] |
|
268 | res = [] | |
269 | i = lnum - index |
|
269 | i = lnum - index | |
270 |
|
270 | |||
271 | # This lets us get fully syntax-highlighted tracebacks. |
|
271 | # This lets us get fully syntax-highlighted tracebacks. | |
272 | if scheme is None: |
|
272 | if scheme is None: | |
273 | ipinst = ipapi.get() |
|
273 | ipinst = ipapi.get() | |
274 | if ipinst is not None: |
|
274 | if ipinst is not None: | |
275 | scheme = ipinst.colors |
|
275 | scheme = ipinst.colors | |
276 | else: |
|
276 | else: | |
277 | scheme = DEFAULT_SCHEME |
|
277 | scheme = DEFAULT_SCHEME | |
278 |
|
278 | |||
279 | _line_format = _parser.format2 |
|
279 | _line_format = _parser.format2 | |
280 |
|
280 | |||
281 | for line in lines: |
|
281 | for line in lines: | |
282 | new_line, err = _line_format(line,'str',scheme) |
|
282 | new_line, err = _line_format(line,'str',scheme) | |
283 | if not err: line = new_line |
|
283 | if not err: line = new_line | |
284 |
|
284 | |||
285 | if i == lnum: |
|
285 | if i == lnum: | |
286 | # This is the line with the error |
|
286 | # This is the line with the error | |
287 | pad = numbers_width - len(str(i)) |
|
287 | pad = numbers_width - len(str(i)) | |
288 | if pad >= 3: |
|
288 | if pad >= 3: | |
289 | marker = '-'*(pad-3) + '-> ' |
|
289 | marker = '-'*(pad-3) + '-> ' | |
290 | elif pad == 2: |
|
290 | elif pad == 2: | |
291 | marker = '> ' |
|
291 | marker = '> ' | |
292 | elif pad == 1: |
|
292 | elif pad == 1: | |
293 | marker = '>' |
|
293 | marker = '>' | |
294 | else: |
|
294 | else: | |
295 | marker = '' |
|
295 | marker = '' | |
296 | num = marker + str(i) |
|
296 | num = marker + str(i) | |
297 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, |
|
297 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, | |
298 | Colors.line, line, Colors.Normal) |
|
298 | Colors.line, line, Colors.Normal) | |
299 | else: |
|
299 | else: | |
300 | num = '%*s' % (numbers_width,i) |
|
300 | num = '%*s' % (numbers_width,i) | |
301 | line = '%s%s%s %s' %(Colors.lineno, num, |
|
301 | line = '%s%s%s %s' %(Colors.lineno, num, | |
302 | Colors.Normal, line) |
|
302 | Colors.Normal, line) | |
303 |
|
303 | |||
304 | res.append(line) |
|
304 | res.append(line) | |
305 | if lvals and i == lnum: |
|
305 | if lvals and i == lnum: | |
306 | res.append(lvals + '\n') |
|
306 | res.append(lvals + '\n') | |
307 | i = i + 1 |
|
307 | i = i + 1 | |
308 | return res |
|
308 | return res | |
309 |
|
309 | |||
310 |
|
310 | |||
311 | #--------------------------------------------------------------------------- |
|
311 | #--------------------------------------------------------------------------- | |
312 | # Module classes |
|
312 | # Module classes | |
313 | class TBTools(object): |
|
313 | class TBTools(object): | |
314 | """Basic tools used by all traceback printer classes.""" |
|
314 | """Basic tools used by all traceback printer classes.""" | |
315 |
|
315 | |||
316 | # This attribute us used in globalipapp.py to have stdout used for |
|
316 | # This attribute us used in globalipapp.py to have stdout used for | |
317 | # writting exceptions. This is needed so nose can trap them. This attribute |
|
317 | # writting exceptions. This is needed so nose can trap them. This attribute | |
318 | # should be None (the default, which will use IPython.utils.io.Term) or |
|
318 | # should be None (the default, which will use IPython.utils.io.Term) or | |
319 | # the string 'stdout' which will cause the override to sys.stdout. |
|
319 | # the string 'stdout' which will cause the override to sys.stdout. | |
320 | out_stream = None |
|
320 | out_stream = None | |
321 |
|
321 | |||
322 | # Number of frames to skip when reporting tracebacks |
|
322 | # Number of frames to skip when reporting tracebacks | |
323 | tb_offset = 0 |
|
323 | tb_offset = 0 | |
324 |
|
324 | |||
325 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): |
|
325 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): | |
326 | # Whether to call the interactive pdb debugger after printing |
|
326 | # Whether to call the interactive pdb debugger after printing | |
327 | # tracebacks or not |
|
327 | # tracebacks or not | |
328 | self.call_pdb = call_pdb |
|
328 | self.call_pdb = call_pdb | |
329 |
|
329 | |||
330 | # Create color table |
|
330 | # Create color table | |
331 | self.color_scheme_table = exception_colors() |
|
331 | self.color_scheme_table = exception_colors() | |
332 |
|
332 | |||
333 | self.set_colors(color_scheme) |
|
333 | self.set_colors(color_scheme) | |
334 | self.old_scheme = color_scheme # save initial value for toggles |
|
334 | self.old_scheme = color_scheme # save initial value for toggles | |
335 |
|
335 | |||
336 | if call_pdb: |
|
336 | if call_pdb: | |
337 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) |
|
337 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) | |
338 | else: |
|
338 | else: | |
339 | self.pdb = None |
|
339 | self.pdb = None | |
340 |
|
340 | |||
341 | def set_colors(self,*args,**kw): |
|
341 | def set_colors(self,*args,**kw): | |
342 | """Shorthand access to the color table scheme selector method.""" |
|
342 | """Shorthand access to the color table scheme selector method.""" | |
343 |
|
343 | |||
344 | # Set own color table |
|
344 | # Set own color table | |
345 | self.color_scheme_table.set_active_scheme(*args,**kw) |
|
345 | self.color_scheme_table.set_active_scheme(*args,**kw) | |
346 | # for convenience, set Colors to the active scheme |
|
346 | # for convenience, set Colors to the active scheme | |
347 | self.Colors = self.color_scheme_table.active_colors |
|
347 | self.Colors = self.color_scheme_table.active_colors | |
348 | # Also set colors of debugger |
|
348 | # Also set colors of debugger | |
349 | if hasattr(self,'pdb') and self.pdb is not None: |
|
349 | if hasattr(self,'pdb') and self.pdb is not None: | |
350 | self.pdb.set_colors(*args,**kw) |
|
350 | self.pdb.set_colors(*args,**kw) | |
351 |
|
351 | |||
352 | def color_toggle(self): |
|
352 | def color_toggle(self): | |
353 | """Toggle between the currently active color scheme and NoColor.""" |
|
353 | """Toggle between the currently active color scheme and NoColor.""" | |
354 |
|
354 | |||
355 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
355 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
356 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
356 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
357 | self.Colors = self.color_scheme_table.active_colors |
|
357 | self.Colors = self.color_scheme_table.active_colors | |
358 | else: |
|
358 | else: | |
359 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
359 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
360 | self.color_scheme_table.set_active_scheme('NoColor') |
|
360 | self.color_scheme_table.set_active_scheme('NoColor') | |
361 | self.Colors = self.color_scheme_table.active_colors |
|
361 | self.Colors = self.color_scheme_table.active_colors | |
362 |
|
362 | |||
|
363 | def stb2text(self, stb): | |||
|
364 | """Convert a structured traceback (a list) to a string.""" | |||
|
365 | return '\n'.join(stb) | |||
|
366 | ||||
363 | def text(self, etype, value, tb, tb_offset=None, context=5): |
|
367 | def text(self, etype, value, tb, tb_offset=None, context=5): | |
364 | """Return formatted traceback. |
|
368 | """Return formatted traceback. | |
365 |
|
369 | |||
366 | Subclasses may override this if they add extra arguments. |
|
370 | Subclasses may override this if they add extra arguments. | |
367 | """ |
|
371 | """ | |
368 | tb_list = self.structured_traceback(etype, value, tb, |
|
372 | tb_list = self.structured_traceback(etype, value, tb, | |
369 | tb_offset, context) |
|
373 | tb_offset, context) | |
370 |
return |
|
374 | return self.stb2text(tb_list) | |
371 |
|
375 | |||
372 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, |
|
376 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, | |
373 | context=5, mode=None): |
|
377 | context=5, mode=None): | |
374 | """Return a list of traceback frames. |
|
378 | """Return a list of traceback frames. | |
375 |
|
379 | |||
376 | Must be implemented by each class. |
|
380 | Must be implemented by each class. | |
377 | """ |
|
381 | """ | |
378 | raise NotImplementedError() |
|
382 | raise NotImplementedError() | |
379 |
|
383 | |||
380 |
|
384 | |||
381 | #--------------------------------------------------------------------------- |
|
385 | #--------------------------------------------------------------------------- | |
382 | class ListTB(TBTools): |
|
386 | class ListTB(TBTools): | |
383 | """Print traceback information from a traceback list, with optional color. |
|
387 | """Print traceback information from a traceback list, with optional color. | |
384 |
|
388 | |||
385 | Calling: requires 3 arguments: |
|
389 | Calling: requires 3 arguments: | |
386 | (etype, evalue, elist) |
|
390 | (etype, evalue, elist) | |
387 | as would be obtained by: |
|
391 | as would be obtained by: | |
388 | etype, evalue, tb = sys.exc_info() |
|
392 | etype, evalue, tb = sys.exc_info() | |
389 | if tb: |
|
393 | if tb: | |
390 | elist = traceback.extract_tb(tb) |
|
394 | elist = traceback.extract_tb(tb) | |
391 | else: |
|
395 | else: | |
392 | elist = None |
|
396 | elist = None | |
393 |
|
397 | |||
394 | It can thus be used by programs which need to process the traceback before |
|
398 | It can thus be used by programs which need to process the traceback before | |
395 | printing (such as console replacements based on the code module from the |
|
399 | printing (such as console replacements based on the code module from the | |
396 | standard library). |
|
400 | standard library). | |
397 |
|
401 | |||
398 | Because they are meant to be called without a full traceback (only a |
|
402 | Because they are meant to be called without a full traceback (only a | |
399 | list), instances of this class can't call the interactive pdb debugger.""" |
|
403 | list), instances of this class can't call the interactive pdb debugger.""" | |
400 |
|
404 | |||
401 | def __init__(self,color_scheme = 'NoColor'): |
|
405 | def __init__(self,color_scheme = 'NoColor'): | |
402 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) |
|
406 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) | |
403 |
|
407 | |||
404 | def __call__(self, etype, value, elist): |
|
408 | def __call__(self, etype, value, elist): | |
405 | io.Term.cout.flush() |
|
409 | io.Term.cout.flush() | |
406 | io.Term.cerr.write(self.text(etype, value, elist)) |
|
410 | io.Term.cerr.write(self.text(etype, value, elist)) | |
407 | io.Term.cerr.write('\n') |
|
411 | io.Term.cerr.write('\n') | |
408 |
|
412 | |||
409 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
413 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
410 | context=5): |
|
414 | context=5): | |
411 | """Return a color formatted string with the traceback info. |
|
415 | """Return a color formatted string with the traceback info. | |
412 |
|
416 | |||
413 | Parameters |
|
417 | Parameters | |
414 | ---------- |
|
418 | ---------- | |
415 | etype : exception type |
|
419 | etype : exception type | |
416 | Type of the exception raised. |
|
420 | Type of the exception raised. | |
417 |
|
421 | |||
418 | value : object |
|
422 | value : object | |
419 | Data stored in the exception |
|
423 | Data stored in the exception | |
420 |
|
424 | |||
421 | elist : list |
|
425 | elist : list | |
422 | List of frames, see class docstring for details. |
|
426 | List of frames, see class docstring for details. | |
423 |
|
427 | |||
424 | tb_offset : int, optional |
|
428 | tb_offset : int, optional | |
425 | Number of frames in the traceback to skip. If not given, the |
|
429 | Number of frames in the traceback to skip. If not given, the | |
426 | instance value is used (set in constructor). |
|
430 | instance value is used (set in constructor). | |
427 |
|
431 | |||
428 | context : int, optional |
|
432 | context : int, optional | |
429 | Number of lines of context information to print. |
|
433 | Number of lines of context information to print. | |
430 |
|
434 | |||
431 | Returns |
|
435 | Returns | |
432 | ------- |
|
436 | ------- | |
433 | String with formatted exception. |
|
437 | String with formatted exception. | |
434 | """ |
|
438 | """ | |
435 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
439 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
436 | Colors = self.Colors |
|
440 | Colors = self.Colors | |
437 | out_list = [] |
|
441 | out_list = [] | |
438 | if elist: |
|
442 | if elist: | |
439 |
|
443 | |||
440 | if tb_offset and len(elist) > tb_offset: |
|
444 | if tb_offset and len(elist) > tb_offset: | |
441 | elist = elist[tb_offset:] |
|
445 | elist = elist[tb_offset:] | |
442 |
|
446 | |||
443 | out_list.append('Traceback %s(most recent call last)%s:' % |
|
447 | out_list.append('Traceback %s(most recent call last)%s:' % | |
444 | (Colors.normalEm, Colors.Normal) + '\n') |
|
448 | (Colors.normalEm, Colors.Normal) + '\n') | |
445 | out_list.extend(self._format_list(elist)) |
|
449 | out_list.extend(self._format_list(elist)) | |
446 | # The exception info should be a single entry in the list. |
|
450 | # The exception info should be a single entry in the list. | |
447 | lines = ''.join(self._format_exception_only(etype, value)) |
|
451 | lines = ''.join(self._format_exception_only(etype, value)) | |
448 | out_list.append(lines) |
|
452 | out_list.append(lines) | |
449 |
|
453 | |||
450 | # Note: this code originally read: |
|
454 | # Note: this code originally read: | |
451 |
|
455 | |||
452 | ## for line in lines[:-1]: |
|
456 | ## for line in lines[:-1]: | |
453 | ## out_list.append(" "+line) |
|
457 | ## out_list.append(" "+line) | |
454 | ## out_list.append(lines[-1]) |
|
458 | ## out_list.append(lines[-1]) | |
455 |
|
459 | |||
456 | # This means it was indenting everything but the last line by a little |
|
460 | # This means it was indenting everything but the last line by a little | |
457 | # bit. I've disabled this for now, but if we see ugliness somewhre we |
|
461 | # bit. I've disabled this for now, but if we see ugliness somewhre we | |
458 | # can restore it. |
|
462 | # can restore it. | |
459 |
|
463 | |||
460 | return out_list |
|
464 | return out_list | |
461 |
|
465 | |||
462 | def _format_list(self, extracted_list): |
|
466 | def _format_list(self, extracted_list): | |
463 | """Format a list of traceback entry tuples for printing. |
|
467 | """Format a list of traceback entry tuples for printing. | |
464 |
|
468 | |||
465 | Given a list of tuples as returned by extract_tb() or |
|
469 | Given a list of tuples as returned by extract_tb() or | |
466 | extract_stack(), return a list of strings ready for printing. |
|
470 | extract_stack(), return a list of strings ready for printing. | |
467 | Each string in the resulting list corresponds to the item with the |
|
471 | Each string in the resulting list corresponds to the item with the | |
468 | same index in the argument list. Each string ends in a newline; |
|
472 | same index in the argument list. Each string ends in a newline; | |
469 | the strings may contain internal newlines as well, for those items |
|
473 | the strings may contain internal newlines as well, for those items | |
470 | whose source text line is not None. |
|
474 | whose source text line is not None. | |
471 |
|
475 | |||
472 | Lifted almost verbatim from traceback.py |
|
476 | Lifted almost verbatim from traceback.py | |
473 | """ |
|
477 | """ | |
474 |
|
478 | |||
475 | Colors = self.Colors |
|
479 | Colors = self.Colors | |
476 | list = [] |
|
480 | list = [] | |
477 | for filename, lineno, name, line in extracted_list[:-1]: |
|
481 | for filename, lineno, name, line in extracted_list[:-1]: | |
478 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
482 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ | |
479 | (Colors.filename, filename, Colors.Normal, |
|
483 | (Colors.filename, filename, Colors.Normal, | |
480 | Colors.lineno, lineno, Colors.Normal, |
|
484 | Colors.lineno, lineno, Colors.Normal, | |
481 | Colors.name, name, Colors.Normal) |
|
485 | Colors.name, name, Colors.Normal) | |
482 | if line: |
|
486 | if line: | |
483 | item = item + ' %s\n' % line.strip() |
|
487 | item = item + ' %s\n' % line.strip() | |
484 | list.append(item) |
|
488 | list.append(item) | |
485 | # Emphasize the last entry |
|
489 | # Emphasize the last entry | |
486 | filename, lineno, name, line = extracted_list[-1] |
|
490 | filename, lineno, name, line = extracted_list[-1] | |
487 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
491 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ | |
488 | (Colors.normalEm, |
|
492 | (Colors.normalEm, | |
489 | Colors.filenameEm, filename, Colors.normalEm, |
|
493 | Colors.filenameEm, filename, Colors.normalEm, | |
490 | Colors.linenoEm, lineno, Colors.normalEm, |
|
494 | Colors.linenoEm, lineno, Colors.normalEm, | |
491 | Colors.nameEm, name, Colors.normalEm, |
|
495 | Colors.nameEm, name, Colors.normalEm, | |
492 | Colors.Normal) |
|
496 | Colors.Normal) | |
493 | if line: |
|
497 | if line: | |
494 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), |
|
498 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), | |
495 | Colors.Normal) |
|
499 | Colors.Normal) | |
496 | list.append(item) |
|
500 | list.append(item) | |
497 | #from pprint import pformat; print 'LISTTB', pformat(list) # dbg |
|
501 | #from pprint import pformat; print 'LISTTB', pformat(list) # dbg | |
498 | return list |
|
502 | return list | |
499 |
|
503 | |||
500 | def _format_exception_only(self, etype, value): |
|
504 | def _format_exception_only(self, etype, value): | |
501 | """Format the exception part of a traceback. |
|
505 | """Format the exception part of a traceback. | |
502 |
|
506 | |||
503 | The arguments are the exception type and value such as given by |
|
507 | The arguments are the exception type and value such as given by | |
504 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
508 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
505 | in a newline. Normally, the list contains a single string; however, |
|
509 | in a newline. Normally, the list contains a single string; however, | |
506 | for SyntaxError exceptions, it contains several lines that (when |
|
510 | for SyntaxError exceptions, it contains several lines that (when | |
507 | printed) display detailed information about where the syntax error |
|
511 | printed) display detailed information about where the syntax error | |
508 | occurred. The message indicating which exception occurred is the |
|
512 | occurred. The message indicating which exception occurred is the | |
509 | always last string in the list. |
|
513 | always last string in the list. | |
510 |
|
514 | |||
511 | Also lifted nearly verbatim from traceback.py |
|
515 | Also lifted nearly verbatim from traceback.py | |
512 | """ |
|
516 | """ | |
513 |
|
517 | |||
514 | have_filedata = False |
|
518 | have_filedata = False | |
515 | Colors = self.Colors |
|
519 | Colors = self.Colors | |
516 | list = [] |
|
520 | list = [] | |
517 | try: |
|
521 | try: | |
518 | stype = Colors.excName + etype.__name__ + Colors.Normal |
|
522 | stype = Colors.excName + etype.__name__ + Colors.Normal | |
519 | except AttributeError: |
|
523 | except AttributeError: | |
520 | stype = etype # String exceptions don't get special coloring |
|
524 | stype = etype # String exceptions don't get special coloring | |
521 | if value is None: |
|
525 | if value is None: | |
522 | list.append( str(stype) + '\n') |
|
526 | list.append( str(stype) + '\n') | |
523 | else: |
|
527 | else: | |
524 | if etype is SyntaxError: |
|
528 | if etype is SyntaxError: | |
525 | try: |
|
529 | try: | |
526 | msg, (filename, lineno, offset, line) = value |
|
530 | msg, (filename, lineno, offset, line) = value | |
527 | except: |
|
531 | except: | |
528 | have_filedata = False |
|
532 | have_filedata = False | |
529 | else: |
|
533 | else: | |
530 | have_filedata = True |
|
534 | have_filedata = True | |
531 | #print 'filename is',filename # dbg |
|
535 | #print 'filename is',filename # dbg | |
532 | if not filename: filename = "<string>" |
|
536 | if not filename: filename = "<string>" | |
533 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ |
|
537 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ | |
534 | (Colors.normalEm, |
|
538 | (Colors.normalEm, | |
535 | Colors.filenameEm, filename, Colors.normalEm, |
|
539 | Colors.filenameEm, filename, Colors.normalEm, | |
536 | Colors.linenoEm, lineno, Colors.Normal )) |
|
540 | Colors.linenoEm, lineno, Colors.Normal )) | |
537 | if line is not None: |
|
541 | if line is not None: | |
538 | i = 0 |
|
542 | i = 0 | |
539 | while i < len(line) and line[i].isspace(): |
|
543 | while i < len(line) and line[i].isspace(): | |
540 | i = i+1 |
|
544 | i = i+1 | |
541 | list.append('%s %s%s\n' % (Colors.line, |
|
545 | list.append('%s %s%s\n' % (Colors.line, | |
542 | line.strip(), |
|
546 | line.strip(), | |
543 | Colors.Normal)) |
|
547 | Colors.Normal)) | |
544 | if offset is not None: |
|
548 | if offset is not None: | |
545 | s = ' ' |
|
549 | s = ' ' | |
546 | for c in line[i:offset-1]: |
|
550 | for c in line[i:offset-1]: | |
547 | if c.isspace(): |
|
551 | if c.isspace(): | |
548 | s = s + c |
|
552 | s = s + c | |
549 | else: |
|
553 | else: | |
550 | s = s + ' ' |
|
554 | s = s + ' ' | |
551 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
555 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
552 | Colors.Normal) ) |
|
556 | Colors.Normal) ) | |
553 | value = msg |
|
557 | value = msg | |
554 | s = self._some_str(value) |
|
558 | s = self._some_str(value) | |
555 | if s: |
|
559 | if s: | |
556 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, |
|
560 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, | |
557 | Colors.Normal, s)) |
|
561 | Colors.Normal, s)) | |
558 | else: |
|
562 | else: | |
559 | list.append('%s\n' % str(stype)) |
|
563 | list.append('%s\n' % str(stype)) | |
560 |
|
564 | |||
561 | # sync with user hooks |
|
565 | # sync with user hooks | |
562 | if have_filedata: |
|
566 | if have_filedata: | |
563 | ipinst = ipapi.get() |
|
567 | ipinst = ipapi.get() | |
564 | if ipinst is not None: |
|
568 | if ipinst is not None: | |
565 | ipinst.hooks.synchronize_with_editor(filename, lineno, 0) |
|
569 | ipinst.hooks.synchronize_with_editor(filename, lineno, 0) | |
566 |
|
570 | |||
567 | return list |
|
571 | return list | |
568 |
|
572 | |||
569 | def get_exception_only(self, etype, value): |
|
573 | def get_exception_only(self, etype, value): | |
570 | """Only print the exception type and message, without a traceback. |
|
574 | """Only print the exception type and message, without a traceback. | |
571 |
|
575 | |||
572 | Parameters |
|
576 | Parameters | |
573 | ---------- |
|
577 | ---------- | |
574 | etype : exception type |
|
578 | etype : exception type | |
575 | value : exception value |
|
579 | value : exception value | |
576 | """ |
|
580 | """ | |
577 | return ListTB.structured_traceback(self, etype, value, []) |
|
581 | return ListTB.structured_traceback(self, etype, value, []) | |
578 |
|
582 | |||
579 |
|
583 | |||
580 | def show_exception_only(self, etype, value): |
|
584 | def show_exception_only(self, etype, value): | |
581 | """Only print the exception type and message, without a traceback. |
|
585 | """Only print the exception type and message, without a traceback. | |
582 |
|
586 | |||
583 | Parameters |
|
587 | Parameters | |
584 | ---------- |
|
588 | ---------- | |
585 | etype : exception type |
|
589 | etype : exception type | |
586 | value : exception value |
|
590 | value : exception value | |
587 | """ |
|
591 | """ | |
588 | # This method needs to use __call__ from *this* class, not the one from |
|
592 | # This method needs to use __call__ from *this* class, not the one from | |
589 | # a subclass whose signature or behavior may be different |
|
593 | # a subclass whose signature or behavior may be different | |
590 | if self.out_stream == 'stdout': |
|
594 | if self.out_stream == 'stdout': | |
591 | ostream = sys.stdout |
|
595 | ostream = sys.stdout | |
592 | else: |
|
596 | else: | |
593 | ostream = io.Term.cerr |
|
597 | ostream = io.Term.cerr | |
594 | ostream.flush() |
|
598 | ostream.flush() | |
595 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) |
|
599 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) | |
596 | ostream.flush() |
|
600 | ostream.flush() | |
597 |
|
601 | |||
598 | def _some_str(self, value): |
|
602 | def _some_str(self, value): | |
599 | # Lifted from traceback.py |
|
603 | # Lifted from traceback.py | |
600 | try: |
|
604 | try: | |
601 | return str(value) |
|
605 | return str(value) | |
602 | except: |
|
606 | except: | |
603 | return '<unprintable %s object>' % type(value).__name__ |
|
607 | return '<unprintable %s object>' % type(value).__name__ | |
604 |
|
608 | |||
605 | #---------------------------------------------------------------------------- |
|
609 | #---------------------------------------------------------------------------- | |
606 | class VerboseTB(TBTools): |
|
610 | class VerboseTB(TBTools): | |
607 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
611 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
608 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
612 | of HTML. Requires inspect and pydoc. Crazy, man. | |
609 |
|
613 | |||
610 | Modified version which optionally strips the topmost entries from the |
|
614 | Modified version which optionally strips the topmost entries from the | |
611 | traceback, to be used with alternate interpreters (because their own code |
|
615 | traceback, to be used with alternate interpreters (because their own code | |
612 | would appear in the traceback).""" |
|
616 | would appear in the traceback).""" | |
613 |
|
617 | |||
614 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, |
|
618 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, | |
615 | call_pdb = 0, include_vars=1): |
|
619 | call_pdb = 0, include_vars=1): | |
616 | """Specify traceback offset, headers and color scheme. |
|
620 | """Specify traceback offset, headers and color scheme. | |
617 |
|
621 | |||
618 | Define how many frames to drop from the tracebacks. Calling it with |
|
622 | Define how many frames to drop from the tracebacks. Calling it with | |
619 | tb_offset=1 allows use of this handler in interpreters which will have |
|
623 | tb_offset=1 allows use of this handler in interpreters which will have | |
620 | their own code at the top of the traceback (VerboseTB will first |
|
624 | their own code at the top of the traceback (VerboseTB will first | |
621 | remove that frame before printing the traceback info).""" |
|
625 | remove that frame before printing the traceback info).""" | |
622 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) |
|
626 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) | |
623 | self.tb_offset = tb_offset |
|
627 | self.tb_offset = tb_offset | |
624 | self.long_header = long_header |
|
628 | self.long_header = long_header | |
625 | self.include_vars = include_vars |
|
629 | self.include_vars = include_vars | |
626 |
|
630 | |||
627 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, |
|
631 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, | |
628 | context=5): |
|
632 | context=5): | |
629 | """Return a nice text document describing the traceback.""" |
|
633 | """Return a nice text document describing the traceback.""" | |
630 |
|
634 | |||
631 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
635 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
632 |
|
636 | |||
633 | # some locals |
|
637 | # some locals | |
634 | try: |
|
638 | try: | |
635 | etype = etype.__name__ |
|
639 | etype = etype.__name__ | |
636 | except AttributeError: |
|
640 | except AttributeError: | |
637 | pass |
|
641 | pass | |
638 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
642 | Colors = self.Colors # just a shorthand + quicker name lookup | |
639 | ColorsNormal = Colors.Normal # used a lot |
|
643 | ColorsNormal = Colors.Normal # used a lot | |
640 | col_scheme = self.color_scheme_table.active_scheme_name |
|
644 | col_scheme = self.color_scheme_table.active_scheme_name | |
641 | indent = ' '*INDENT_SIZE |
|
645 | indent = ' '*INDENT_SIZE | |
642 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) |
|
646 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) | |
643 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
647 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) | |
644 | exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) |
|
648 | exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) | |
645 |
|
649 | |||
646 | # some internal-use functions |
|
650 | # some internal-use functions | |
647 | def text_repr(value): |
|
651 | def text_repr(value): | |
648 | """Hopefully pretty robust repr equivalent.""" |
|
652 | """Hopefully pretty robust repr equivalent.""" | |
649 | # this is pretty horrible but should always return *something* |
|
653 | # this is pretty horrible but should always return *something* | |
650 | try: |
|
654 | try: | |
651 | return pydoc.text.repr(value) |
|
655 | return pydoc.text.repr(value) | |
652 | except KeyboardInterrupt: |
|
656 | except KeyboardInterrupt: | |
653 | raise |
|
657 | raise | |
654 | except: |
|
658 | except: | |
655 | try: |
|
659 | try: | |
656 | return repr(value) |
|
660 | return repr(value) | |
657 | except KeyboardInterrupt: |
|
661 | except KeyboardInterrupt: | |
658 | raise |
|
662 | raise | |
659 | except: |
|
663 | except: | |
660 | try: |
|
664 | try: | |
661 | # all still in an except block so we catch |
|
665 | # all still in an except block so we catch | |
662 | # getattr raising |
|
666 | # getattr raising | |
663 | name = getattr(value, '__name__', None) |
|
667 | name = getattr(value, '__name__', None) | |
664 | if name: |
|
668 | if name: | |
665 | # ick, recursion |
|
669 | # ick, recursion | |
666 | return text_repr(name) |
|
670 | return text_repr(name) | |
667 | klass = getattr(value, '__class__', None) |
|
671 | klass = getattr(value, '__class__', None) | |
668 | if klass: |
|
672 | if klass: | |
669 | return '%s instance' % text_repr(klass) |
|
673 | return '%s instance' % text_repr(klass) | |
670 | except KeyboardInterrupt: |
|
674 | except KeyboardInterrupt: | |
671 | raise |
|
675 | raise | |
672 | except: |
|
676 | except: | |
673 | return 'UNRECOVERABLE REPR FAILURE' |
|
677 | return 'UNRECOVERABLE REPR FAILURE' | |
674 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) |
|
678 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) | |
675 | def nullrepr(value, repr=text_repr): return '' |
|
679 | def nullrepr(value, repr=text_repr): return '' | |
676 |
|
680 | |||
677 | # meat of the code begins |
|
681 | # meat of the code begins | |
678 | try: |
|
682 | try: | |
679 | etype = etype.__name__ |
|
683 | etype = etype.__name__ | |
680 | except AttributeError: |
|
684 | except AttributeError: | |
681 | pass |
|
685 | pass | |
682 |
|
686 | |||
683 | if self.long_header: |
|
687 | if self.long_header: | |
684 | # Header with the exception type, python version, and date |
|
688 | # Header with the exception type, python version, and date | |
685 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable |
|
689 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable | |
686 | date = time.ctime(time.time()) |
|
690 | date = time.ctime(time.time()) | |
687 |
|
691 | |||
688 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, |
|
692 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, | |
689 | exc, ' '*(75-len(str(etype))-len(pyver)), |
|
693 | exc, ' '*(75-len(str(etype))-len(pyver)), | |
690 | pyver, string.rjust(date, 75) ) |
|
694 | pyver, string.rjust(date, 75) ) | |
691 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ |
|
695 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ | |
692 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
696 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
693 | else: |
|
697 | else: | |
694 | # Simplified header |
|
698 | # Simplified header | |
695 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, |
|
699 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, | |
696 | string.rjust('Traceback (most recent call last)', |
|
700 | string.rjust('Traceback (most recent call last)', | |
697 | 75 - len(str(etype)) ) ) |
|
701 | 75 - len(str(etype)) ) ) | |
698 | frames = [] |
|
702 | frames = [] | |
699 | # Flush cache before calling inspect. This helps alleviate some of the |
|
703 | # Flush cache before calling inspect. This helps alleviate some of the | |
700 | # problems with python 2.3's inspect.py. |
|
704 | # problems with python 2.3's inspect.py. | |
701 | linecache.checkcache() |
|
705 | linecache.checkcache() | |
702 | # Drop topmost frames if requested |
|
706 | # Drop topmost frames if requested | |
703 | try: |
|
707 | try: | |
704 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
708 | # Try the default getinnerframes and Alex's: Alex's fixes some | |
705 | # problems, but it generates empty tracebacks for console errors |
|
709 | # problems, but it generates empty tracebacks for console errors | |
706 | # (5 blanks lines) where none should be returned. |
|
710 | # (5 blanks lines) where none should be returned. | |
707 | #records = inspect.getinnerframes(etb, context)[tb_offset:] |
|
711 | #records = inspect.getinnerframes(etb, context)[tb_offset:] | |
708 | #print 'python records:', records # dbg |
|
712 | #print 'python records:', records # dbg | |
709 | records = _fixed_getinnerframes(etb, context, tb_offset) |
|
713 | records = _fixed_getinnerframes(etb, context, tb_offset) | |
710 | #print 'alex records:', records # dbg |
|
714 | #print 'alex records:', records # dbg | |
711 | except: |
|
715 | except: | |
712 |
|
716 | |||
713 | # FIXME: I've been getting many crash reports from python 2.3 |
|
717 | # FIXME: I've been getting many crash reports from python 2.3 | |
714 | # users, traceable to inspect.py. If I can find a small test-case |
|
718 | # users, traceable to inspect.py. If I can find a small test-case | |
715 | # to reproduce this, I should either write a better workaround or |
|
719 | # to reproduce this, I should either write a better workaround or | |
716 | # file a bug report against inspect (if that's the real problem). |
|
720 | # file a bug report against inspect (if that's the real problem). | |
717 | # So far, I haven't been able to find an isolated example to |
|
721 | # So far, I haven't been able to find an isolated example to | |
718 | # reproduce the problem. |
|
722 | # reproduce the problem. | |
719 | inspect_error() |
|
723 | inspect_error() | |
720 | traceback.print_exc(file=io.Term.cerr) |
|
724 | traceback.print_exc(file=io.Term.cerr) | |
721 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
725 | info('\nUnfortunately, your original traceback can not be constructed.\n') | |
722 | return '' |
|
726 | return '' | |
723 |
|
727 | |||
724 | # build some color string templates outside these nested loops |
|
728 | # build some color string templates outside these nested loops | |
725 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) |
|
729 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) | |
726 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
730 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, | |
727 | ColorsNormal) |
|
731 | ColorsNormal) | |
728 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
732 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ | |
729 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
733 | (Colors.vName, Colors.valEm, ColorsNormal) | |
730 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
734 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) | |
731 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
735 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, | |
732 | Colors.vName, ColorsNormal) |
|
736 | Colors.vName, ColorsNormal) | |
733 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
737 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) | |
734 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) |
|
738 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) | |
735 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, |
|
739 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, | |
736 | ColorsNormal) |
|
740 | ColorsNormal) | |
737 |
|
741 | |||
738 | # now, loop over all records printing context and info |
|
742 | # now, loop over all records printing context and info | |
739 | abspath = os.path.abspath |
|
743 | abspath = os.path.abspath | |
740 | for frame, file, lnum, func, lines, index in records: |
|
744 | for frame, file, lnum, func, lines, index in records: | |
741 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
745 | #print '*** record:',file,lnum,func,lines,index # dbg | |
742 | try: |
|
746 | try: | |
743 | file = file and abspath(file) or '?' |
|
747 | file = file and abspath(file) or '?' | |
744 | except OSError: |
|
748 | except OSError: | |
745 | # if file is '<console>' or something not in the filesystem, |
|
749 | # if file is '<console>' or something not in the filesystem, | |
746 | # the abspath call will throw an OSError. Just ignore it and |
|
750 | # the abspath call will throw an OSError. Just ignore it and | |
747 | # keep the original file string. |
|
751 | # keep the original file string. | |
748 | pass |
|
752 | pass | |
749 | link = tpl_link % file |
|
753 | link = tpl_link % file | |
750 | try: |
|
754 | try: | |
751 | args, varargs, varkw, locals = inspect.getargvalues(frame) |
|
755 | args, varargs, varkw, locals = inspect.getargvalues(frame) | |
752 | except: |
|
756 | except: | |
753 | # This can happen due to a bug in python2.3. We should be |
|
757 | # This can happen due to a bug in python2.3. We should be | |
754 | # able to remove this try/except when 2.4 becomes a |
|
758 | # able to remove this try/except when 2.4 becomes a | |
755 | # requirement. Bug details at http://python.org/sf/1005466 |
|
759 | # requirement. Bug details at http://python.org/sf/1005466 | |
756 | inspect_error() |
|
760 | inspect_error() | |
757 | traceback.print_exc(file=io.Term.cerr) |
|
761 | traceback.print_exc(file=io.Term.cerr) | |
758 | info("\nIPython's exception reporting continues...\n") |
|
762 | info("\nIPython's exception reporting continues...\n") | |
759 |
|
763 | |||
760 | if func == '?': |
|
764 | if func == '?': | |
761 | call = '' |
|
765 | call = '' | |
762 | else: |
|
766 | else: | |
763 | # Decide whether to include variable details or not |
|
767 | # Decide whether to include variable details or not | |
764 | var_repr = self.include_vars and eqrepr or nullrepr |
|
768 | var_repr = self.include_vars and eqrepr or nullrepr | |
765 | try: |
|
769 | try: | |
766 | call = tpl_call % (func,inspect.formatargvalues(args, |
|
770 | call = tpl_call % (func,inspect.formatargvalues(args, | |
767 | varargs, varkw, |
|
771 | varargs, varkw, | |
768 | locals,formatvalue=var_repr)) |
|
772 | locals,formatvalue=var_repr)) | |
769 | except KeyError: |
|
773 | except KeyError: | |
770 | # Very odd crash from inspect.formatargvalues(). The |
|
774 | # Very odd crash from inspect.formatargvalues(). The | |
771 | # scenario under which it appeared was a call to |
|
775 | # scenario under which it appeared was a call to | |
772 | # view(array,scale) in NumTut.view.view(), where scale had |
|
776 | # view(array,scale) in NumTut.view.view(), where scale had | |
773 | # been defined as a scalar (it should be a tuple). Somehow |
|
777 | # been defined as a scalar (it should be a tuple). Somehow | |
774 | # inspect messes up resolving the argument list of view() |
|
778 | # inspect messes up resolving the argument list of view() | |
775 | # and barfs out. At some point I should dig into this one |
|
779 | # and barfs out. At some point I should dig into this one | |
776 | # and file a bug report about it. |
|
780 | # and file a bug report about it. | |
777 | inspect_error() |
|
781 | inspect_error() | |
778 | traceback.print_exc(file=io.Term.cerr) |
|
782 | traceback.print_exc(file=io.Term.cerr) | |
779 | info("\nIPython's exception reporting continues...\n") |
|
783 | info("\nIPython's exception reporting continues...\n") | |
780 | call = tpl_call_fail % func |
|
784 | call = tpl_call_fail % func | |
781 |
|
785 | |||
782 | # Initialize a list of names on the current line, which the |
|
786 | # Initialize a list of names on the current line, which the | |
783 | # tokenizer below will populate. |
|
787 | # tokenizer below will populate. | |
784 | names = [] |
|
788 | names = [] | |
785 |
|
789 | |||
786 | def tokeneater(token_type, token, start, end, line): |
|
790 | def tokeneater(token_type, token, start, end, line): | |
787 | """Stateful tokeneater which builds dotted names. |
|
791 | """Stateful tokeneater which builds dotted names. | |
788 |
|
792 | |||
789 | The list of names it appends to (from the enclosing scope) can |
|
793 | The list of names it appends to (from the enclosing scope) can | |
790 | contain repeated composite names. This is unavoidable, since |
|
794 | contain repeated composite names. This is unavoidable, since | |
791 | there is no way to disambguate partial dotted structures until |
|
795 | there is no way to disambguate partial dotted structures until | |
792 | the full list is known. The caller is responsible for pruning |
|
796 | the full list is known. The caller is responsible for pruning | |
793 | the final list of duplicates before using it.""" |
|
797 | the final list of duplicates before using it.""" | |
794 |
|
798 | |||
795 | # build composite names |
|
799 | # build composite names | |
796 | if token == '.': |
|
800 | if token == '.': | |
797 | try: |
|
801 | try: | |
798 | names[-1] += '.' |
|
802 | names[-1] += '.' | |
799 | # store state so the next token is added for x.y.z names |
|
803 | # store state so the next token is added for x.y.z names | |
800 | tokeneater.name_cont = True |
|
804 | tokeneater.name_cont = True | |
801 | return |
|
805 | return | |
802 | except IndexError: |
|
806 | except IndexError: | |
803 | pass |
|
807 | pass | |
804 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
808 | if token_type == tokenize.NAME and token not in keyword.kwlist: | |
805 | if tokeneater.name_cont: |
|
809 | if tokeneater.name_cont: | |
806 | # Dotted names |
|
810 | # Dotted names | |
807 | names[-1] += token |
|
811 | names[-1] += token | |
808 | tokeneater.name_cont = False |
|
812 | tokeneater.name_cont = False | |
809 | else: |
|
813 | else: | |
810 | # Regular new names. We append everything, the caller |
|
814 | # Regular new names. We append everything, the caller | |
811 | # will be responsible for pruning the list later. It's |
|
815 | # will be responsible for pruning the list later. It's | |
812 | # very tricky to try to prune as we go, b/c composite |
|
816 | # very tricky to try to prune as we go, b/c composite | |
813 | # names can fool us. The pruning at the end is easy |
|
817 | # names can fool us. The pruning at the end is easy | |
814 | # to do (or the caller can print a list with repeated |
|
818 | # to do (or the caller can print a list with repeated | |
815 | # names if so desired. |
|
819 | # names if so desired. | |
816 | names.append(token) |
|
820 | names.append(token) | |
817 | elif token_type == tokenize.NEWLINE: |
|
821 | elif token_type == tokenize.NEWLINE: | |
818 | raise IndexError |
|
822 | raise IndexError | |
819 | # we need to store a bit of state in the tokenizer to build |
|
823 | # we need to store a bit of state in the tokenizer to build | |
820 | # dotted names |
|
824 | # dotted names | |
821 | tokeneater.name_cont = False |
|
825 | tokeneater.name_cont = False | |
822 |
|
826 | |||
823 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
827 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): | |
824 | line = getline(file, lnum[0]) |
|
828 | line = getline(file, lnum[0]) | |
825 | lnum[0] += 1 |
|
829 | lnum[0] += 1 | |
826 | return line |
|
830 | return line | |
827 |
|
831 | |||
828 | # Build the list of names on this line of code where the exception |
|
832 | # Build the list of names on this line of code where the exception | |
829 | # occurred. |
|
833 | # occurred. | |
830 | try: |
|
834 | try: | |
831 | # This builds the names list in-place by capturing it from the |
|
835 | # This builds the names list in-place by capturing it from the | |
832 | # enclosing scope. |
|
836 | # enclosing scope. | |
833 | tokenize.tokenize(linereader, tokeneater) |
|
837 | tokenize.tokenize(linereader, tokeneater) | |
834 | except IndexError: |
|
838 | except IndexError: | |
835 | # signals exit of tokenizer |
|
839 | # signals exit of tokenizer | |
836 | pass |
|
840 | pass | |
837 | except tokenize.TokenError,msg: |
|
841 | except tokenize.TokenError,msg: | |
838 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
842 | _m = ("An unexpected error occurred while tokenizing input\n" | |
839 | "The following traceback may be corrupted or invalid\n" |
|
843 | "The following traceback may be corrupted or invalid\n" | |
840 | "The error message is: %s\n" % msg) |
|
844 | "The error message is: %s\n" % msg) | |
841 | error(_m) |
|
845 | error(_m) | |
842 |
|
846 | |||
843 | # prune names list of duplicates, but keep the right order |
|
847 | # prune names list of duplicates, but keep the right order | |
844 | unique_names = uniq_stable(names) |
|
848 | unique_names = uniq_stable(names) | |
845 |
|
849 | |||
846 | # Start loop over vars |
|
850 | # Start loop over vars | |
847 | lvals = [] |
|
851 | lvals = [] | |
848 | if self.include_vars: |
|
852 | if self.include_vars: | |
849 | for name_full in unique_names: |
|
853 | for name_full in unique_names: | |
850 | name_base = name_full.split('.',1)[0] |
|
854 | name_base = name_full.split('.',1)[0] | |
851 | if name_base in frame.f_code.co_varnames: |
|
855 | if name_base in frame.f_code.co_varnames: | |
852 | if locals.has_key(name_base): |
|
856 | if locals.has_key(name_base): | |
853 | try: |
|
857 | try: | |
854 | value = repr(eval(name_full,locals)) |
|
858 | value = repr(eval(name_full,locals)) | |
855 | except: |
|
859 | except: | |
856 | value = undefined |
|
860 | value = undefined | |
857 | else: |
|
861 | else: | |
858 | value = undefined |
|
862 | value = undefined | |
859 | name = tpl_local_var % name_full |
|
863 | name = tpl_local_var % name_full | |
860 | else: |
|
864 | else: | |
861 | if frame.f_globals.has_key(name_base): |
|
865 | if frame.f_globals.has_key(name_base): | |
862 | try: |
|
866 | try: | |
863 | value = repr(eval(name_full,frame.f_globals)) |
|
867 | value = repr(eval(name_full,frame.f_globals)) | |
864 | except: |
|
868 | except: | |
865 | value = undefined |
|
869 | value = undefined | |
866 | else: |
|
870 | else: | |
867 | value = undefined |
|
871 | value = undefined | |
868 | name = tpl_global_var % name_full |
|
872 | name = tpl_global_var % name_full | |
869 | lvals.append(tpl_name_val % (name,value)) |
|
873 | lvals.append(tpl_name_val % (name,value)) | |
870 | if lvals: |
|
874 | if lvals: | |
871 | lvals = '%s%s' % (indent,em_normal.join(lvals)) |
|
875 | lvals = '%s%s' % (indent,em_normal.join(lvals)) | |
872 | else: |
|
876 | else: | |
873 | lvals = '' |
|
877 | lvals = '' | |
874 |
|
878 | |||
875 | level = '%s %s\n' % (link,call) |
|
879 | level = '%s %s\n' % (link,call) | |
876 |
|
880 | |||
877 | if index is None: |
|
881 | if index is None: | |
878 | frames.append(level) |
|
882 | frames.append(level) | |
879 | else: |
|
883 | else: | |
880 | frames.append('%s%s' % (level,''.join( |
|
884 | frames.append('%s%s' % (level,''.join( | |
881 | _format_traceback_lines(lnum,index,lines,Colors,lvals, |
|
885 | _format_traceback_lines(lnum,index,lines,Colors,lvals, | |
882 | col_scheme)))) |
|
886 | col_scheme)))) | |
883 |
|
887 | |||
884 | # Get (safely) a string form of the exception info |
|
888 | # Get (safely) a string form of the exception info | |
885 | try: |
|
889 | try: | |
886 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
890 | etype_str,evalue_str = map(str,(etype,evalue)) | |
887 | except: |
|
891 | except: | |
888 | # User exception is improperly defined. |
|
892 | # User exception is improperly defined. | |
889 | etype,evalue = str,sys.exc_info()[:2] |
|
893 | etype,evalue = str,sys.exc_info()[:2] | |
890 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
894 | etype_str,evalue_str = map(str,(etype,evalue)) | |
891 | # ... and format it |
|
895 | # ... and format it | |
892 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, |
|
896 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, | |
893 | ColorsNormal, evalue_str)] |
|
897 | ColorsNormal, evalue_str)] | |
894 | if type(evalue) is types.InstanceType: |
|
898 | if type(evalue) is types.InstanceType: | |
895 | try: |
|
899 | try: | |
896 | names = [w for w in dir(evalue) if isinstance(w, basestring)] |
|
900 | names = [w for w in dir(evalue) if isinstance(w, basestring)] | |
897 | except: |
|
901 | except: | |
898 | # Every now and then, an object with funny inernals blows up |
|
902 | # Every now and then, an object with funny inernals blows up | |
899 | # when dir() is called on it. We do the best we can to report |
|
903 | # when dir() is called on it. We do the best we can to report | |
900 | # the problem and continue |
|
904 | # the problem and continue | |
901 | _m = '%sException reporting error (object with broken dir())%s:' |
|
905 | _m = '%sException reporting error (object with broken dir())%s:' | |
902 | exception.append(_m % (Colors.excName,ColorsNormal)) |
|
906 | exception.append(_m % (Colors.excName,ColorsNormal)) | |
903 | etype_str,evalue_str = map(str,sys.exc_info()[:2]) |
|
907 | etype_str,evalue_str = map(str,sys.exc_info()[:2]) | |
904 | exception.append('%s%s%s: %s' % (Colors.excName,etype_str, |
|
908 | exception.append('%s%s%s: %s' % (Colors.excName,etype_str, | |
905 | ColorsNormal, evalue_str)) |
|
909 | ColorsNormal, evalue_str)) | |
906 | names = [] |
|
910 | names = [] | |
907 | for name in names: |
|
911 | for name in names: | |
908 | value = text_repr(getattr(evalue, name)) |
|
912 | value = text_repr(getattr(evalue, name)) | |
909 | exception.append('\n%s%s = %s' % (indent, name, value)) |
|
913 | exception.append('\n%s%s = %s' % (indent, name, value)) | |
910 |
|
914 | |||
911 | # vds: >> |
|
915 | # vds: >> | |
912 | if records: |
|
916 | if records: | |
913 | filepath, lnum = records[-1][1:3] |
|
917 | filepath, lnum = records[-1][1:3] | |
914 | #print "file:", str(file), "linenb", str(lnum) # dbg |
|
918 | #print "file:", str(file), "linenb", str(lnum) # dbg | |
915 | filepath = os.path.abspath(filepath) |
|
919 | filepath = os.path.abspath(filepath) | |
916 | ipinst = ipapi.get() |
|
920 | ipinst = ipapi.get() | |
917 | if ipinst is not None: |
|
921 | if ipinst is not None: | |
918 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) |
|
922 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) | |
919 | # vds: << |
|
923 | # vds: << | |
920 |
|
924 | |||
921 | # return all our info assembled as a single string |
|
925 | # return all our info assembled as a single string | |
922 | # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) |
|
926 | # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) | |
923 | return [head] + frames + [''.join(exception[0])] |
|
927 | return [head] + frames + [''.join(exception[0])] | |
924 |
|
928 | |||
925 | def debugger(self,force=False): |
|
929 | def debugger(self,force=False): | |
926 | """Call up the pdb debugger if desired, always clean up the tb |
|
930 | """Call up the pdb debugger if desired, always clean up the tb | |
927 | reference. |
|
931 | reference. | |
928 |
|
932 | |||
929 | Keywords: |
|
933 | Keywords: | |
930 |
|
934 | |||
931 | - force(False): by default, this routine checks the instance call_pdb |
|
935 | - force(False): by default, this routine checks the instance call_pdb | |
932 | flag and does not actually invoke the debugger if the flag is false. |
|
936 | flag and does not actually invoke the debugger if the flag is false. | |
933 | The 'force' option forces the debugger to activate even if the flag |
|
937 | The 'force' option forces the debugger to activate even if the flag | |
934 | is false. |
|
938 | is false. | |
935 |
|
939 | |||
936 | If the call_pdb flag is set, the pdb interactive debugger is |
|
940 | If the call_pdb flag is set, the pdb interactive debugger is | |
937 | invoked. In all cases, the self.tb reference to the current traceback |
|
941 | invoked. In all cases, the self.tb reference to the current traceback | |
938 | is deleted to prevent lingering references which hamper memory |
|
942 | is deleted to prevent lingering references which hamper memory | |
939 | management. |
|
943 | management. | |
940 |
|
944 | |||
941 | Note that each call to pdb() does an 'import readline', so if your app |
|
945 | Note that each call to pdb() does an 'import readline', so if your app | |
942 | requires a special setup for the readline completers, you'll have to |
|
946 | requires a special setup for the readline completers, you'll have to | |
943 | fix that by hand after invoking the exception handler.""" |
|
947 | fix that by hand after invoking the exception handler.""" | |
944 |
|
948 | |||
945 | if force or self.call_pdb: |
|
949 | if force or self.call_pdb: | |
946 | if self.pdb is None: |
|
950 | if self.pdb is None: | |
947 | self.pdb = debugger.Pdb( |
|
951 | self.pdb = debugger.Pdb( | |
948 | self.color_scheme_table.active_scheme_name) |
|
952 | self.color_scheme_table.active_scheme_name) | |
949 | # the system displayhook may have changed, restore the original |
|
953 | # the system displayhook may have changed, restore the original | |
950 | # for pdb |
|
954 | # for pdb | |
951 | display_trap = DisplayTrap(hook=sys.__displayhook__) |
|
955 | display_trap = DisplayTrap(hook=sys.__displayhook__) | |
952 | with display_trap: |
|
956 | with display_trap: | |
953 | self.pdb.reset() |
|
957 | self.pdb.reset() | |
954 | # Find the right frame so we don't pop up inside ipython itself |
|
958 | # Find the right frame so we don't pop up inside ipython itself | |
955 | if hasattr(self,'tb') and self.tb is not None: |
|
959 | if hasattr(self,'tb') and self.tb is not None: | |
956 | etb = self.tb |
|
960 | etb = self.tb | |
957 | else: |
|
961 | else: | |
958 | etb = self.tb = sys.last_traceback |
|
962 | etb = self.tb = sys.last_traceback | |
959 | while self.tb is not None and self.tb.tb_next is not None: |
|
963 | while self.tb is not None and self.tb.tb_next is not None: | |
960 | self.tb = self.tb.tb_next |
|
964 | self.tb = self.tb.tb_next | |
961 | if etb and etb.tb_next: |
|
965 | if etb and etb.tb_next: | |
962 | etb = etb.tb_next |
|
966 | etb = etb.tb_next | |
963 | self.pdb.botframe = etb.tb_frame |
|
967 | self.pdb.botframe = etb.tb_frame | |
964 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
968 | self.pdb.interaction(self.tb.tb_frame, self.tb) | |
965 |
|
969 | |||
966 | if hasattr(self,'tb'): |
|
970 | if hasattr(self,'tb'): | |
967 | del self.tb |
|
971 | del self.tb | |
968 |
|
972 | |||
969 | def handler(self, info=None): |
|
973 | def handler(self, info=None): | |
970 | (etype, evalue, etb) = info or sys.exc_info() |
|
974 | (etype, evalue, etb) = info or sys.exc_info() | |
971 | self.tb = etb |
|
975 | self.tb = etb | |
972 | io.Term.cout.flush() |
|
976 | io.Term.cout.flush() | |
973 | io.Term.cerr.write(self.text(etype, evalue, etb)) |
|
977 | io.Term.cerr.write(self.text(etype, evalue, etb)) | |
974 | io.Term.cerr.write('\n') |
|
978 | io.Term.cerr.write('\n') | |
975 |
|
979 | |||
976 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
980 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
977 | # out the right info on its own. |
|
981 | # out the right info on its own. | |
978 | def __call__(self, etype=None, evalue=None, etb=None): |
|
982 | def __call__(self, etype=None, evalue=None, etb=None): | |
979 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
983 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
980 | if etb is None: |
|
984 | if etb is None: | |
981 | self.handler() |
|
985 | self.handler() | |
982 | else: |
|
986 | else: | |
983 | self.handler((etype, evalue, etb)) |
|
987 | self.handler((etype, evalue, etb)) | |
984 | try: |
|
988 | try: | |
985 | self.debugger() |
|
989 | self.debugger() | |
986 | except KeyboardInterrupt: |
|
990 | except KeyboardInterrupt: | |
987 | print "\nKeyboardInterrupt" |
|
991 | print "\nKeyboardInterrupt" | |
988 |
|
992 | |||
989 | #---------------------------------------------------------------------------- |
|
993 | #---------------------------------------------------------------------------- | |
990 | class FormattedTB(VerboseTB, ListTB): |
|
994 | class FormattedTB(VerboseTB, ListTB): | |
991 | """Subclass ListTB but allow calling with a traceback. |
|
995 | """Subclass ListTB but allow calling with a traceback. | |
992 |
|
996 | |||
993 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
997 | It can thus be used as a sys.excepthook for Python > 2.1. | |
994 |
|
998 | |||
995 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
999 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
996 |
|
1000 | |||
997 | Allows a tb_offset to be specified. This is useful for situations where |
|
1001 | Allows a tb_offset to be specified. This is useful for situations where | |
998 | one needs to remove a number of topmost frames from the traceback (such as |
|
1002 | one needs to remove a number of topmost frames from the traceback (such as | |
999 | occurs with python programs that themselves execute other python code, |
|
1003 | occurs with python programs that themselves execute other python code, | |
1000 | like Python shells). """ |
|
1004 | like Python shells). """ | |
1001 |
|
1005 | |||
1002 | def __init__(self, mode='Plain', color_scheme='Linux', |
|
1006 | def __init__(self, mode='Plain', color_scheme='Linux', | |
1003 | tb_offset=0, long_header=0, call_pdb=0, include_vars=0): |
|
1007 | tb_offset=0, long_header=0, call_pdb=0, include_vars=0): | |
1004 |
|
1008 | |||
1005 | # NEVER change the order of this list. Put new modes at the end: |
|
1009 | # NEVER change the order of this list. Put new modes at the end: | |
1006 | self.valid_modes = ['Plain','Context','Verbose'] |
|
1010 | self.valid_modes = ['Plain','Context','Verbose'] | |
1007 | self.verbose_modes = self.valid_modes[1:3] |
|
1011 | self.verbose_modes = self.valid_modes[1:3] | |
1008 |
|
1012 | |||
1009 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, |
|
1013 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, | |
1010 | call_pdb=call_pdb,include_vars=include_vars) |
|
1014 | call_pdb=call_pdb,include_vars=include_vars) | |
|
1015 | ||||
|
1016 | # Different types of tracebacks are joined with different separators to | |||
|
1017 | # form a single string. They are taken from this dict | |||
|
1018 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n') | |||
|
1019 | # set_mode also sets the tb_join_char attribute | |||
1011 | self.set_mode(mode) |
|
1020 | self.set_mode(mode) | |
1012 |
|
1021 | |||
1013 | def _extract_tb(self,tb): |
|
1022 | def _extract_tb(self,tb): | |
1014 | if tb: |
|
1023 | if tb: | |
1015 | return traceback.extract_tb(tb) |
|
1024 | return traceback.extract_tb(tb) | |
1016 | else: |
|
1025 | else: | |
1017 | return None |
|
1026 | return None | |
1018 |
|
1027 | |||
1019 | def structured_traceback(self, etype, value, tb, tb_offset=None, |
|
1028 | def structured_traceback(self, etype, value, tb, tb_offset=None, context=5): | |
1020 | context=5, mode=None): |
|
|||
1021 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1029 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1022 |
mode = self.mode |
|
1030 | mode = self.mode | |
1023 | if mode in self.verbose_modes: |
|
1031 | if mode in self.verbose_modes: | |
1024 | # Verbose modes need a full traceback |
|
1032 | # Verbose modes need a full traceback | |
1025 | return VerboseTB.structured_traceback( |
|
1033 | return VerboseTB.structured_traceback( | |
1026 | self, etype, value, tb, tb_offset, context |
|
1034 | self, etype, value, tb, tb_offset, context | |
1027 | ) |
|
1035 | ) | |
1028 | else: |
|
1036 | else: | |
1029 | # We must check the source cache because otherwise we can print |
|
1037 | # We must check the source cache because otherwise we can print | |
1030 | # out-of-date source code. |
|
1038 | # out-of-date source code. | |
1031 | linecache.checkcache() |
|
1039 | linecache.checkcache() | |
1032 | # Now we can extract and format the exception |
|
1040 | # Now we can extract and format the exception | |
1033 | elist = self._extract_tb(tb) |
|
1041 | elist = self._extract_tb(tb) | |
1034 | return ListTB.structured_traceback( |
|
1042 | return ListTB.structured_traceback( | |
1035 | self, etype, value, elist, tb_offset, context |
|
1043 | self, etype, value, elist, tb_offset, context | |
1036 | ) |
|
1044 | ) | |
1037 |
|
1045 | |||
1038 | def text(self, etype, value, tb, tb_offset=None, context=5, mode=None): |
|
1046 | def stb2text(self, stb): | |
1039 | """Return formatted traceback. |
|
1047 | """Convert a structured traceback (a list) to a string.""" | |
1040 |
|
1048 | return self.tb_join_char.join(stb) | ||
1041 | If the optional mode parameter is given, it overrides the current |
|
|||
1042 | mode.""" |
|
|||
1043 |
|
||||
1044 | mode = self.mode if mode is None else mode |
|
|||
1045 | tb_list = self.structured_traceback(etype, value, tb, tb_offset, |
|
|||
1046 | context, mode) |
|
|||
1047 | return '\n'.join(tb_list) |
|
|||
1048 |
|
1049 | |||
1049 |
|
1050 | |||
1050 | def set_mode(self,mode=None): |
|
1051 | def set_mode(self,mode=None): | |
1051 | """Switch to the desired mode. |
|
1052 | """Switch to the desired mode. | |
1052 |
|
1053 | |||
1053 | If mode is not specified, cycles through the available modes.""" |
|
1054 | If mode is not specified, cycles through the available modes.""" | |
1054 |
|
1055 | |||
1055 | if not mode: |
|
1056 | if not mode: | |
1056 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ |
|
1057 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ | |
1057 | len(self.valid_modes) |
|
1058 | len(self.valid_modes) | |
1058 | self.mode = self.valid_modes[new_idx] |
|
1059 | self.mode = self.valid_modes[new_idx] | |
1059 | elif mode not in self.valid_modes: |
|
1060 | elif mode not in self.valid_modes: | |
1060 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ |
|
1061 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ | |
1061 | 'Valid modes: '+str(self.valid_modes) |
|
1062 | 'Valid modes: '+str(self.valid_modes) | |
1062 | else: |
|
1063 | else: | |
1063 | self.mode = mode |
|
1064 | self.mode = mode | |
1064 | # include variable details only in 'Verbose' mode |
|
1065 | # include variable details only in 'Verbose' mode | |
1065 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
1066 | self.include_vars = (self.mode == self.valid_modes[2]) | |
|
1067 | # Set the join character for generating text tracebacks | |||
|
1068 | self.tb_join_char = self._join_chars[mode] | |||
1066 |
|
1069 | |||
1067 | # some convenient shorcuts |
|
1070 | # some convenient shorcuts | |
1068 | def plain(self): |
|
1071 | def plain(self): | |
1069 | self.set_mode(self.valid_modes[0]) |
|
1072 | self.set_mode(self.valid_modes[0]) | |
1070 |
|
1073 | |||
1071 | def context(self): |
|
1074 | def context(self): | |
1072 | self.set_mode(self.valid_modes[1]) |
|
1075 | self.set_mode(self.valid_modes[1]) | |
1073 |
|
1076 | |||
1074 | def verbose(self): |
|
1077 | def verbose(self): | |
1075 | self.set_mode(self.valid_modes[2]) |
|
1078 | self.set_mode(self.valid_modes[2]) | |
1076 |
|
1079 | |||
1077 | #---------------------------------------------------------------------------- |
|
1080 | #---------------------------------------------------------------------------- | |
1078 | class AutoFormattedTB(FormattedTB): |
|
1081 | class AutoFormattedTB(FormattedTB): | |
1079 | """A traceback printer which can be called on the fly. |
|
1082 | """A traceback printer which can be called on the fly. | |
1080 |
|
1083 | |||
1081 | It will find out about exceptions by itself. |
|
1084 | It will find out about exceptions by itself. | |
1082 |
|
1085 | |||
1083 | A brief example: |
|
1086 | A brief example: | |
1084 |
|
1087 | |||
1085 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
1088 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
1086 | try: |
|
1089 | try: | |
1087 | ... |
|
1090 | ... | |
1088 | except: |
|
1091 | except: | |
1089 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
1092 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
1090 | """ |
|
1093 | """ | |
1091 |
|
1094 | |||
1092 | def __call__(self,etype=None,evalue=None,etb=None, |
|
1095 | def __call__(self,etype=None,evalue=None,etb=None, | |
1093 | out=None,tb_offset=None): |
|
1096 | out=None,tb_offset=None): | |
1094 | """Print out a formatted exception traceback. |
|
1097 | """Print out a formatted exception traceback. | |
1095 |
|
1098 | |||
1096 | Optional arguments: |
|
1099 | Optional arguments: | |
1097 | - out: an open file-like object to direct output to. |
|
1100 | - out: an open file-like object to direct output to. | |
1098 |
|
1101 | |||
1099 | - tb_offset: the number of frames to skip over in the stack, on a |
|
1102 | - tb_offset: the number of frames to skip over in the stack, on a | |
1100 | per-call basis (this overrides temporarily the instance's tb_offset |
|
1103 | per-call basis (this overrides temporarily the instance's tb_offset | |
1101 | given at initialization time. """ |
|
1104 | given at initialization time. """ | |
1102 |
|
1105 | |||
1103 | if out is None: |
|
1106 | if out is None: | |
1104 | if self.out_stream == 'stdout': |
|
1107 | if self.out_stream == 'stdout': | |
1105 | out = sys.stdout |
|
1108 | out = sys.stdout | |
1106 | else: |
|
1109 | else: | |
1107 | out = io.Term.cerr |
|
1110 | out = io.Term.cerr | |
1108 | out.flush() |
|
1111 | out.flush() | |
1109 | out.write(self.text(etype, evalue, etb, tb_offset)) |
|
1112 | out.write(self.text(etype, evalue, etb, tb_offset)) | |
1110 | out.write('\n') |
|
1113 | out.write('\n') | |
1111 | out.flush() |
|
1114 | out.flush() | |
1112 | # FIXME: we should remove the auto pdb behavior from here and leave |
|
1115 | # FIXME: we should remove the auto pdb behavior from here and leave | |
1113 | # that to the clients. |
|
1116 | # that to the clients. | |
1114 | try: |
|
1117 | try: | |
1115 | self.debugger() |
|
1118 | self.debugger() | |
1116 | except KeyboardInterrupt: |
|
1119 | except KeyboardInterrupt: | |
1117 | print "\nKeyboardInterrupt" |
|
1120 | print "\nKeyboardInterrupt" | |
1118 |
|
1121 | |||
1119 | def structured_traceback(self, etype=None, value=None, tb=None, |
|
1122 | def structured_traceback(self, etype=None, value=None, tb=None, | |
1120 |
tb_offset=None, context=5 |
|
1123 | tb_offset=None, context=5): | |
1121 | if etype is None: |
|
1124 | if etype is None: | |
1122 | etype,value,tb = sys.exc_info() |
|
1125 | etype,value,tb = sys.exc_info() | |
1123 | self.tb = tb |
|
1126 | self.tb = tb | |
1124 | return FormattedTB.structured_traceback( |
|
1127 | return FormattedTB.structured_traceback( | |
1125 |
self, etype, value, tb, tb_offset, context |
|
1128 | self, etype, value, tb, tb_offset, context) | |
1126 |
|
1129 | |||
1127 | #--------------------------------------------------------------------------- |
|
1130 | #--------------------------------------------------------------------------- | |
1128 |
|
1131 | |||
1129 | # A simple class to preserve Nathan's original functionality. |
|
1132 | # A simple class to preserve Nathan's original functionality. | |
1130 | class ColorTB(FormattedTB): |
|
1133 | class ColorTB(FormattedTB): | |
1131 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
1134 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1132 | def __init__(self,color_scheme='Linux',call_pdb=0): |
|
1135 | def __init__(self,color_scheme='Linux',call_pdb=0): | |
1133 | FormattedTB.__init__(self,color_scheme=color_scheme, |
|
1136 | FormattedTB.__init__(self,color_scheme=color_scheme, | |
1134 | call_pdb=call_pdb) |
|
1137 | call_pdb=call_pdb) | |
1135 |
|
1138 | |||
1136 |
|
1139 | |||
1137 | class SyntaxTB(ListTB): |
|
1140 | class SyntaxTB(ListTB): | |
1138 | """Extension which holds some state: the last exception value""" |
|
1141 | """Extension which holds some state: the last exception value""" | |
1139 |
|
1142 | |||
1140 | def __init__(self,color_scheme = 'NoColor'): |
|
1143 | def __init__(self,color_scheme = 'NoColor'): | |
1141 | ListTB.__init__(self,color_scheme) |
|
1144 | ListTB.__init__(self,color_scheme) | |
1142 | self.last_syntax_error = None |
|
1145 | self.last_syntax_error = None | |
1143 |
|
1146 | |||
1144 | def __call__(self, etype, value, elist): |
|
1147 | def __call__(self, etype, value, elist): | |
1145 | self.last_syntax_error = value |
|
1148 | self.last_syntax_error = value | |
1146 | ListTB.__call__(self,etype,value,elist) |
|
1149 | ListTB.__call__(self,etype,value,elist) | |
1147 |
|
1150 | |||
1148 | def clear_err_state(self): |
|
1151 | def clear_err_state(self): | |
1149 | """Return the current error state and clear it""" |
|
1152 | """Return the current error state and clear it""" | |
1150 | e = self.last_syntax_error |
|
1153 | e = self.last_syntax_error | |
1151 | self.last_syntax_error = None |
|
1154 | self.last_syntax_error = None | |
1152 | return e |
|
1155 | return e | |
1153 |
|
1156 | |||
1154 | def text(self, etype, value, tb, tb_offset=None, context=5): |
|
1157 | def stb2text(self, stb): | |
1155 | """Return formatted traceback. |
|
1158 | """Convert a structured traceback (a list) to a string.""" | |
|
1159 | return ''.join(stb) | |||
1156 |
|
1160 | |||
1157 | Subclasses may override this if they add extra arguments. |
|
|||
1158 | """ |
|
|||
1159 | tb_list = self.structured_traceback(etype, value, tb, |
|
|||
1160 | tb_offset, context) |
|
|||
1161 | return ''.join(tb_list) |
|
|||
1162 |
|
1161 | |||
1163 | #---------------------------------------------------------------------------- |
|
1162 | #---------------------------------------------------------------------------- | |
1164 | # module testing (minimal) |
|
1163 | # module testing (minimal) | |
1165 | if __name__ == "__main__": |
|
1164 | if __name__ == "__main__": | |
1166 | def spam(c, (d, e)): |
|
1165 | def spam(c, (d, e)): | |
1167 | x = c + d |
|
1166 | x = c + d | |
1168 | y = c * d |
|
1167 | y = c * d | |
1169 | foo(x, y) |
|
1168 | foo(x, y) | |
1170 |
|
1169 | |||
1171 | def foo(a, b, bar=1): |
|
1170 | def foo(a, b, bar=1): | |
1172 | eggs(a, b + bar) |
|
1171 | eggs(a, b + bar) | |
1173 |
|
1172 | |||
1174 | def eggs(f, g, z=globals()): |
|
1173 | def eggs(f, g, z=globals()): | |
1175 | h = f + g |
|
1174 | h = f + g | |
1176 | i = f - g |
|
1175 | i = f - g | |
1177 | return h / i |
|
1176 | return h / i | |
1178 |
|
1177 | |||
1179 | print '' |
|
1178 | print '' | |
1180 | print '*** Before ***' |
|
1179 | print '*** Before ***' | |
1181 | try: |
|
1180 | try: | |
1182 | print spam(1, (2, 3)) |
|
1181 | print spam(1, (2, 3)) | |
1183 | except: |
|
1182 | except: | |
1184 | traceback.print_exc() |
|
1183 | traceback.print_exc() | |
1185 | print '' |
|
1184 | print '' | |
1186 |
|
1185 | |||
1187 | handler = ColorTB() |
|
1186 | handler = ColorTB() | |
1188 | print '*** ColorTB ***' |
|
1187 | print '*** ColorTB ***' | |
1189 | try: |
|
1188 | try: | |
1190 | print spam(1, (2, 3)) |
|
1189 | print spam(1, (2, 3)) | |
1191 | except: |
|
1190 | except: | |
1192 | apply(handler, sys.exc_info() ) |
|
1191 | apply(handler, sys.exc_info() ) | |
1193 | print '' |
|
1192 | print '' | |
1194 |
|
1193 | |||
1195 | handler = VerboseTB() |
|
1194 | handler = VerboseTB() | |
1196 | print '*** VerboseTB ***' |
|
1195 | print '*** VerboseTB ***' | |
1197 | try: |
|
1196 | try: | |
1198 | print spam(1, (2, 3)) |
|
1197 | print spam(1, (2, 3)) | |
1199 | except: |
|
1198 | except: | |
1200 | apply(handler, sys.exc_info() ) |
|
1199 | apply(handler, sys.exc_info() ) | |
1201 | print '' |
|
1200 | print '' | |
1202 |
|
1201 |
@@ -1,377 +1,384 b'' | |||||
1 | # Standard library imports |
|
1 | # Standard library imports | |
2 | import signal |
|
2 | import signal | |
3 | import sys |
|
3 | import sys | |
4 |
|
4 | |||
5 | # System library imports |
|
5 | # System library imports | |
6 | from pygments.lexers import PythonLexer |
|
6 | from pygments.lexers import PythonLexer | |
7 | from PyQt4 import QtCore, QtGui |
|
7 | from PyQt4 import QtCore, QtGui | |
8 | import zmq |
|
8 | import zmq | |
9 |
|
9 | |||
10 | # Local imports |
|
10 | # Local imports | |
11 | from IPython.core.inputsplitter import InputSplitter |
|
11 | from IPython.core.inputsplitter import InputSplitter | |
12 | from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin |
|
12 | from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin | |
13 | from call_tip_widget import CallTipWidget |
|
13 | from call_tip_widget import CallTipWidget | |
14 | from completion_lexer import CompletionLexer |
|
14 | from completion_lexer import CompletionLexer | |
15 | from console_widget import HistoryConsoleWidget |
|
15 | from console_widget import HistoryConsoleWidget | |
16 | from pygments_highlighter import PygmentsHighlighter |
|
16 | from pygments_highlighter import PygmentsHighlighter | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | class FrontendHighlighter(PygmentsHighlighter): |
|
19 | class FrontendHighlighter(PygmentsHighlighter): | |
20 | """ A PygmentsHighlighter that can be turned on and off and that ignores |
|
20 | """ A PygmentsHighlighter that can be turned on and off and that ignores | |
21 | prompts. |
|
21 | prompts. | |
22 | """ |
|
22 | """ | |
23 |
|
23 | |||
24 | def __init__(self, frontend): |
|
24 | def __init__(self, frontend): | |
25 | super(FrontendHighlighter, self).__init__(frontend._control.document()) |
|
25 | super(FrontendHighlighter, self).__init__(frontend._control.document()) | |
26 | self._current_offset = 0 |
|
26 | self._current_offset = 0 | |
27 | self._frontend = frontend |
|
27 | self._frontend = frontend | |
28 | self.highlighting_on = False |
|
28 | self.highlighting_on = False | |
29 |
|
29 | |||
30 | def highlightBlock(self, qstring): |
|
30 | def highlightBlock(self, qstring): | |
31 | """ Highlight a block of text. Reimplemented to highlight selectively. |
|
31 | """ Highlight a block of text. Reimplemented to highlight selectively. | |
32 | """ |
|
32 | """ | |
33 | if not self.highlighting_on: |
|
33 | if not self.highlighting_on: | |
34 | return |
|
34 | return | |
35 |
|
35 | |||
36 | # The input to this function is unicode string that may contain |
|
36 | # The input to this function is unicode string that may contain | |
37 | # paragraph break characters, non-breaking spaces, etc. Here we acquire |
|
37 | # paragraph break characters, non-breaking spaces, etc. Here we acquire | |
38 | # the string as plain text so we can compare it. |
|
38 | # the string as plain text so we can compare it. | |
39 | current_block = self.currentBlock() |
|
39 | current_block = self.currentBlock() | |
40 | string = self._frontend._get_block_plain_text(current_block) |
|
40 | string = self._frontend._get_block_plain_text(current_block) | |
41 |
|
41 | |||
42 | # Decide whether to check for the regular or continuation prompt. |
|
42 | # Decide whether to check for the regular or continuation prompt. | |
43 | if current_block.contains(self._frontend._prompt_pos): |
|
43 | if current_block.contains(self._frontend._prompt_pos): | |
44 | prompt = self._frontend._prompt |
|
44 | prompt = self._frontend._prompt | |
45 | else: |
|
45 | else: | |
46 | prompt = self._frontend._continuation_prompt |
|
46 | prompt = self._frontend._continuation_prompt | |
47 |
|
47 | |||
48 | # Don't highlight the part of the string that contains the prompt. |
|
48 | # Don't highlight the part of the string that contains the prompt. | |
49 | if string.startswith(prompt): |
|
49 | if string.startswith(prompt): | |
50 | self._current_offset = len(prompt) |
|
50 | self._current_offset = len(prompt) | |
51 | qstring.remove(0, len(prompt)) |
|
51 | qstring.remove(0, len(prompt)) | |
52 | else: |
|
52 | else: | |
53 | self._current_offset = 0 |
|
53 | self._current_offset = 0 | |
54 |
|
54 | |||
55 | PygmentsHighlighter.highlightBlock(self, qstring) |
|
55 | PygmentsHighlighter.highlightBlock(self, qstring) | |
56 |
|
56 | |||
57 | def rehighlightBlock(self, block): |
|
57 | def rehighlightBlock(self, block): | |
58 | """ Reimplemented to temporarily enable highlighting if disabled. |
|
58 | """ Reimplemented to temporarily enable highlighting if disabled. | |
59 | """ |
|
59 | """ | |
60 | old = self.highlighting_on |
|
60 | old = self.highlighting_on | |
61 | self.highlighting_on = True |
|
61 | self.highlighting_on = True | |
62 | super(FrontendHighlighter, self).rehighlightBlock(block) |
|
62 | super(FrontendHighlighter, self).rehighlightBlock(block) | |
63 | self.highlighting_on = old |
|
63 | self.highlighting_on = old | |
64 |
|
64 | |||
65 | def setFormat(self, start, count, format): |
|
65 | def setFormat(self, start, count, format): | |
66 | """ Reimplemented to highlight selectively. |
|
66 | """ Reimplemented to highlight selectively. | |
67 | """ |
|
67 | """ | |
68 | start += self._current_offset |
|
68 | start += self._current_offset | |
69 | PygmentsHighlighter.setFormat(self, start, count, format) |
|
69 | PygmentsHighlighter.setFormat(self, start, count, format) | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): |
|
72 | class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): | |
73 | """ A Qt frontend for a generic Python kernel. |
|
73 | """ A Qt frontend for a generic Python kernel. | |
74 | """ |
|
74 | """ | |
75 |
|
75 | |||
76 | # Emitted when an 'execute_reply' has been received from the kernel and |
|
76 | # Emitted when an 'execute_reply' has been received from the kernel and | |
77 | # processed by the FrontendWidget. |
|
77 | # processed by the FrontendWidget. | |
78 | executed = QtCore.pyqtSignal(object) |
|
78 | executed = QtCore.pyqtSignal(object) | |
79 |
|
79 | |||
80 | # Protected class attributes. |
|
80 | # Protected class attributes. | |
81 | _highlighter_class = FrontendHighlighter |
|
81 | _highlighter_class = FrontendHighlighter | |
82 | _input_splitter_class = InputSplitter |
|
82 | _input_splitter_class = InputSplitter | |
83 |
|
83 | |||
84 | #--------------------------------------------------------------------------- |
|
84 | #--------------------------------------------------------------------------- | |
85 | # 'object' interface |
|
85 | # 'object' interface | |
86 | #--------------------------------------------------------------------------- |
|
86 | #--------------------------------------------------------------------------- | |
87 |
|
87 | |||
88 | def __init__(self, *args, **kw): |
|
88 | def __init__(self, *args, **kw): | |
89 | super(FrontendWidget, self).__init__(*args, **kw) |
|
89 | super(FrontendWidget, self).__init__(*args, **kw) | |
90 |
|
90 | |||
91 | # FrontendWidget protected variables. |
|
91 | # FrontendWidget protected variables. | |
92 | self._call_tip_widget = CallTipWidget(self._control) |
|
92 | self._call_tip_widget = CallTipWidget(self._control) | |
93 | self._completion_lexer = CompletionLexer(PythonLexer()) |
|
93 | self._completion_lexer = CompletionLexer(PythonLexer()) | |
94 | self._hidden = False |
|
94 | self._hidden = False | |
95 | self._highlighter = self._highlighter_class(self) |
|
95 | self._highlighter = self._highlighter_class(self) | |
96 | self._input_splitter = self._input_splitter_class(input_mode='replace') |
|
96 | self._input_splitter = self._input_splitter_class(input_mode='replace') | |
97 | self._kernel_manager = None |
|
97 | self._kernel_manager = None | |
98 |
|
98 | |||
99 | # Configure the ConsoleWidget. |
|
99 | # Configure the ConsoleWidget. | |
100 | self.tab_width = 4 |
|
100 | self.tab_width = 4 | |
101 | self._set_continuation_prompt('... ') |
|
101 | self._set_continuation_prompt('... ') | |
102 |
|
102 | |||
103 | # Connect signal handlers. |
|
103 | # Connect signal handlers. | |
104 | document = self._control.document() |
|
104 | document = self._control.document() | |
105 | document.contentsChange.connect(self._document_contents_change) |
|
105 | document.contentsChange.connect(self._document_contents_change) | |
106 |
|
106 | |||
107 | #--------------------------------------------------------------------------- |
|
107 | #--------------------------------------------------------------------------- | |
108 | # 'ConsoleWidget' abstract interface |
|
108 | # 'ConsoleWidget' abstract interface | |
109 | #--------------------------------------------------------------------------- |
|
109 | #--------------------------------------------------------------------------- | |
110 |
|
110 | |||
111 | def _is_complete(self, source, interactive): |
|
111 | def _is_complete(self, source, interactive): | |
112 | """ Returns whether 'source' can be completely processed and a new |
|
112 | """ Returns whether 'source' can be completely processed and a new | |
113 | prompt created. When triggered by an Enter/Return key press, |
|
113 | prompt created. When triggered by an Enter/Return key press, | |
114 | 'interactive' is True; otherwise, it is False. |
|
114 | 'interactive' is True; otherwise, it is False. | |
115 | """ |
|
115 | """ | |
116 | complete = self._input_splitter.push(source.expandtabs(4)) |
|
116 | complete = self._input_splitter.push(source.expandtabs(4)) | |
117 | if interactive: |
|
117 | if interactive: | |
118 | complete = not self._input_splitter.push_accepts_more() |
|
118 | complete = not self._input_splitter.push_accepts_more() | |
119 | return complete |
|
119 | return complete | |
120 |
|
120 | |||
121 | def _execute(self, source, hidden): |
|
121 | def _execute(self, source, hidden): | |
122 | """ Execute 'source'. If 'hidden', do not show any output. |
|
122 | """ Execute 'source'. If 'hidden', do not show any output. | |
123 | """ |
|
123 | """ | |
124 | self.kernel_manager.xreq_channel.execute(source) |
|
124 | self.kernel_manager.xreq_channel.execute(source) | |
125 | self._hidden = hidden |
|
125 | self._hidden = hidden | |
126 |
|
126 | |||
127 | def _execute_interrupt(self): |
|
127 | def _execute_interrupt(self): | |
128 | """ Attempts to stop execution. Returns whether this method has an |
|
128 | """ Attempts to stop execution. Returns whether this method has an | |
129 | implementation. |
|
129 | implementation. | |
130 | """ |
|
130 | """ | |
131 | self._interrupt_kernel() |
|
131 | self._interrupt_kernel() | |
132 | return True |
|
132 | return True | |
133 |
|
133 | |||
134 | def _prompt_started_hook(self): |
|
134 | def _prompt_started_hook(self): | |
135 | """ Called immediately after a new prompt is displayed. |
|
135 | """ Called immediately after a new prompt is displayed. | |
136 | """ |
|
136 | """ | |
137 | if not self._reading: |
|
137 | if not self._reading: | |
138 | self._highlighter.highlighting_on = True |
|
138 | self._highlighter.highlighting_on = True | |
139 |
|
139 | |||
140 | def _prompt_finished_hook(self): |
|
140 | def _prompt_finished_hook(self): | |
141 | """ Called immediately after a prompt is finished, i.e. when some input |
|
141 | """ Called immediately after a prompt is finished, i.e. when some input | |
142 | will be processed and a new prompt displayed. |
|
142 | will be processed and a new prompt displayed. | |
143 | """ |
|
143 | """ | |
144 | if not self._reading: |
|
144 | if not self._reading: | |
145 | self._highlighter.highlighting_on = False |
|
145 | self._highlighter.highlighting_on = False | |
146 |
|
146 | |||
147 | def _tab_pressed(self): |
|
147 | def _tab_pressed(self): | |
148 | """ Called when the tab key is pressed. Returns whether to continue |
|
148 | """ Called when the tab key is pressed. Returns whether to continue | |
149 | processing the event. |
|
149 | processing the event. | |
150 | """ |
|
150 | """ | |
151 | self._keep_cursor_in_buffer() |
|
151 | self._keep_cursor_in_buffer() | |
152 | cursor = self._get_cursor() |
|
152 | cursor = self._get_cursor() | |
153 | return not self._complete() |
|
153 | return not self._complete() | |
154 |
|
154 | |||
155 | #--------------------------------------------------------------------------- |
|
155 | #--------------------------------------------------------------------------- | |
156 | # 'ConsoleWidget' protected interface |
|
156 | # 'ConsoleWidget' protected interface | |
157 | #--------------------------------------------------------------------------- |
|
157 | #--------------------------------------------------------------------------- | |
158 |
|
158 | |||
159 | def _show_continuation_prompt(self): |
|
159 | def _show_continuation_prompt(self): | |
160 | """ Reimplemented for auto-indentation. |
|
160 | """ Reimplemented for auto-indentation. | |
161 | """ |
|
161 | """ | |
162 | super(FrontendWidget, self)._show_continuation_prompt() |
|
162 | super(FrontendWidget, self)._show_continuation_prompt() | |
163 | spaces = self._input_splitter.indent_spaces |
|
163 | spaces = self._input_splitter.indent_spaces | |
164 | self._append_plain_text('\t' * (spaces / self.tab_width)) |
|
164 | self._append_plain_text('\t' * (spaces / self.tab_width)) | |
165 | self._append_plain_text(' ' * (spaces % self.tab_width)) |
|
165 | self._append_plain_text(' ' * (spaces % self.tab_width)) | |
166 |
|
166 | |||
167 | #--------------------------------------------------------------------------- |
|
167 | #--------------------------------------------------------------------------- | |
168 | # 'BaseFrontendMixin' abstract interface |
|
168 | # 'BaseFrontendMixin' abstract interface | |
169 | #--------------------------------------------------------------------------- |
|
169 | #--------------------------------------------------------------------------- | |
170 |
|
170 | |||
171 | def _handle_complete_reply(self, rep): |
|
171 | def _handle_complete_reply(self, rep): | |
172 | """ Handle replies for tab completion. |
|
172 | """ Handle replies for tab completion. | |
173 | """ |
|
173 | """ | |
174 | cursor = self._get_cursor() |
|
174 | cursor = self._get_cursor() | |
175 | if rep['parent_header']['msg_id'] == self._complete_id and \ |
|
175 | if rep['parent_header']['msg_id'] == self._complete_id and \ | |
176 | cursor.position() == self._complete_pos: |
|
176 | cursor.position() == self._complete_pos: | |
177 | text = '.'.join(self._get_context()) |
|
177 | text = '.'.join(self._get_context()) | |
178 | cursor.movePosition(QtGui.QTextCursor.Left, n=len(text)) |
|
178 | cursor.movePosition(QtGui.QTextCursor.Left, n=len(text)) | |
179 | self._complete_with_items(cursor, rep['content']['matches']) |
|
179 | self._complete_with_items(cursor, rep['content']['matches']) | |
180 |
|
180 | |||
181 | def _handle_execute_reply(self, msg): |
|
181 | def _handle_execute_reply(self, msg): | |
182 | """ Handles replies for code execution. |
|
182 | """ Handles replies for code execution. | |
183 | """ |
|
183 | """ | |
184 | if not self._hidden: |
|
184 | if not self._hidden: | |
185 | # Make sure that all output from the SUB channel has been processed |
|
185 | # Make sure that all output from the SUB channel has been processed | |
186 | # before writing a new prompt. |
|
186 | # before writing a new prompt. | |
187 | self.kernel_manager.sub_channel.flush() |
|
187 | self.kernel_manager.sub_channel.flush() | |
188 |
|
188 | |||
189 | content = msg['content'] |
|
189 | content = msg['content'] | |
190 | status = content['status'] |
|
190 | status = content['status'] | |
191 | if status == 'ok': |
|
191 | if status == 'ok': | |
192 | self._process_execute_ok(msg) |
|
192 | self._process_execute_ok(msg) | |
193 | elif status == 'error': |
|
193 | elif status == 'error': | |
194 | self._process_execute_error(msg) |
|
194 | self._process_execute_error(msg) | |
195 | elif status == 'abort': |
|
195 | elif status == 'abort': | |
196 | self._process_execute_abort(msg) |
|
196 | self._process_execute_abort(msg) | |
197 |
|
197 | |||
198 | self._show_interpreter_prompt_for_reply(msg) |
|
198 | self._show_interpreter_prompt_for_reply(msg) | |
199 | self.executed.emit(msg) |
|
199 | self.executed.emit(msg) | |
200 |
|
200 | |||
201 | def _handle_input_request(self, msg): |
|
201 | def _handle_input_request(self, msg): | |
202 | """ Handle requests for raw_input. |
|
202 | """ Handle requests for raw_input. | |
203 | """ |
|
203 | """ | |
204 | if self._hidden: |
|
204 | if self._hidden: | |
205 | raise RuntimeError('Request for raw input during hidden execution.') |
|
205 | raise RuntimeError('Request for raw input during hidden execution.') | |
206 |
|
206 | |||
207 | # Make sure that all output from the SUB channel has been processed |
|
207 | # Make sure that all output from the SUB channel has been processed | |
208 | # before entering readline mode. |
|
208 | # before entering readline mode. | |
209 | self.kernel_manager.sub_channel.flush() |
|
209 | self.kernel_manager.sub_channel.flush() | |
210 |
|
210 | |||
211 | def callback(line): |
|
211 | def callback(line): | |
212 | self.kernel_manager.rep_channel.input(line) |
|
212 | self.kernel_manager.rep_channel.input(line) | |
213 | self._readline(msg['content']['prompt'], callback=callback) |
|
213 | self._readline(msg['content']['prompt'], callback=callback) | |
214 |
|
214 | |||
215 | def _handle_object_info_reply(self, rep): |
|
215 | def _handle_object_info_reply(self, rep): | |
216 | """ Handle replies for call tips. |
|
216 | """ Handle replies for call tips. | |
217 | """ |
|
217 | """ | |
218 | cursor = self._get_cursor() |
|
218 | cursor = self._get_cursor() | |
219 | if rep['parent_header']['msg_id'] == self._call_tip_id and \ |
|
219 | if rep['parent_header']['msg_id'] == self._call_tip_id and \ | |
220 | cursor.position() == self._call_tip_pos: |
|
220 | cursor.position() == self._call_tip_pos: | |
221 | doc = rep['content']['docstring'] |
|
221 | doc = rep['content']['docstring'] | |
222 | if doc: |
|
222 | if doc: | |
223 | self._call_tip_widget.show_docstring(doc) |
|
223 | self._call_tip_widget.show_docstring(doc) | |
224 |
|
224 | |||
225 | def _handle_pyout(self, msg): |
|
225 | def _handle_pyout(self, msg): | |
226 | """ Handle display hook output. |
|
226 | """ Handle display hook output. | |
227 | """ |
|
227 | """ | |
228 | if not self._hidden and self._is_from_this_session(msg): |
|
228 | if not self._hidden and self._is_from_this_session(msg): | |
229 | self._append_plain_text(msg['content']['data'] + '\n') |
|
229 | self._append_plain_text(msg['content']['data'] + '\n') | |
230 |
|
230 | |||
231 | def _handle_stream(self, msg): |
|
231 | def _handle_stream(self, msg): | |
232 | """ Handle stdout, stderr, and stdin. |
|
232 | """ Handle stdout, stderr, and stdin. | |
233 | """ |
|
233 | """ | |
234 | if not self._hidden and self._is_from_this_session(msg): |
|
234 | if not self._hidden and self._is_from_this_session(msg): | |
235 | self._append_plain_text(msg['content']['data']) |
|
235 | self._append_plain_text(msg['content']['data']) | |
236 | self._control.moveCursor(QtGui.QTextCursor.End) |
|
236 | self._control.moveCursor(QtGui.QTextCursor.End) | |
237 |
|
237 | |||
238 | def _started_channels(self): |
|
238 | def _started_channels(self): | |
239 | """ Called when the KernelManager channels have started listening or |
|
239 | """ Called when the KernelManager channels have started listening or | |
240 | when the frontend is assigned an already listening KernelManager. |
|
240 | when the frontend is assigned an already listening KernelManager. | |
241 | """ |
|
241 | """ | |
242 | self._reset() |
|
242 | self._reset() | |
243 | self._append_plain_text(self._get_banner()) |
|
243 | self._append_plain_text(self._get_banner()) | |
244 | self._show_interpreter_prompt() |
|
244 | self._show_interpreter_prompt() | |
245 |
|
245 | |||
246 | def _stopped_channels(self): |
|
246 | def _stopped_channels(self): | |
247 | """ Called when the KernelManager channels have stopped listening or |
|
247 | """ Called when the KernelManager channels have stopped listening or | |
248 | when a listening KernelManager is removed from the frontend. |
|
248 | when a listening KernelManager is removed from the frontend. | |
249 | """ |
|
249 | """ | |
250 | # FIXME: Print a message here? |
|
250 | # FIXME: Print a message here? | |
251 | pass |
|
251 | pass | |
252 |
|
252 | |||
253 | #--------------------------------------------------------------------------- |
|
253 | #--------------------------------------------------------------------------- | |
254 | # 'FrontendWidget' interface |
|
254 | # 'FrontendWidget' interface | |
255 | #--------------------------------------------------------------------------- |
|
255 | #--------------------------------------------------------------------------- | |
256 |
|
256 | |||
257 | def execute_file(self, path, hidden=False): |
|
257 | def execute_file(self, path, hidden=False): | |
258 | """ Attempts to execute file with 'path'. If 'hidden', no output is |
|
258 | """ Attempts to execute file with 'path'. If 'hidden', no output is | |
259 | shown. |
|
259 | shown. | |
260 | """ |
|
260 | """ | |
261 | self.execute('execfile("%s")' % path, hidden=hidden) |
|
261 | self.execute('execfile("%s")' % path, hidden=hidden) | |
262 |
|
262 | |||
263 | #--------------------------------------------------------------------------- |
|
263 | #--------------------------------------------------------------------------- | |
264 | # 'FrontendWidget' protected interface |
|
264 | # 'FrontendWidget' protected interface | |
265 | #--------------------------------------------------------------------------- |
|
265 | #--------------------------------------------------------------------------- | |
266 |
|
266 | |||
267 | def _call_tip(self): |
|
267 | def _call_tip(self): | |
268 | """ Shows a call tip, if appropriate, at the current cursor location. |
|
268 | """ Shows a call tip, if appropriate, at the current cursor location. | |
269 | """ |
|
269 | """ | |
270 | # Decide if it makes sense to show a call tip |
|
270 | # Decide if it makes sense to show a call tip | |
271 | cursor = self._get_cursor() |
|
271 | cursor = self._get_cursor() | |
272 | cursor.movePosition(QtGui.QTextCursor.Left) |
|
272 | cursor.movePosition(QtGui.QTextCursor.Left) | |
273 | document = self._control.document() |
|
273 | document = self._control.document() | |
274 | if document.characterAt(cursor.position()).toAscii() != '(': |
|
274 | if document.characterAt(cursor.position()).toAscii() != '(': | |
275 | return False |
|
275 | return False | |
276 | context = self._get_context(cursor) |
|
276 | context = self._get_context(cursor) | |
277 | if not context: |
|
277 | if not context: | |
278 | return False |
|
278 | return False | |
279 |
|
279 | |||
280 | # Send the metadata request to the kernel |
|
280 | # Send the metadata request to the kernel | |
281 | name = '.'.join(context) |
|
281 | name = '.'.join(context) | |
282 | self._call_tip_id = self.kernel_manager.xreq_channel.object_info(name) |
|
282 | self._call_tip_id = self.kernel_manager.xreq_channel.object_info(name) | |
283 | self._call_tip_pos = self._get_cursor().position() |
|
283 | self._call_tip_pos = self._get_cursor().position() | |
284 | return True |
|
284 | return True | |
285 |
|
285 | |||
286 | def _complete(self): |
|
286 | def _complete(self): | |
287 | """ Performs completion at the current cursor location. |
|
287 | """ Performs completion at the current cursor location. | |
288 | """ |
|
288 | """ | |
289 | # Decide if it makes sense to do completion |
|
289 | # Decide if it makes sense to do completion | |
290 | context = self._get_context() |
|
290 | context = self._get_context() | |
291 | if not context: |
|
291 | if not context: | |
292 | return False |
|
292 | return False | |
293 |
|
293 | |||
294 | # Send the completion request to the kernel |
|
294 | # Send the completion request to the kernel | |
295 | text = '.'.join(context) |
|
295 | text = '.'.join(context) | |
|
296 | ||||
|
297 | # FIXME - Evan: we need the position of the cursor in the current input | |||
|
298 | # buffer. I tried this line below but the numbers I get are bogus. - | |||
|
299 | # Not sure what to do. fperez. | |||
|
300 | cursor_pos = self._get_cursor().position() | |||
|
301 | ||||
296 | self._complete_id = self.kernel_manager.xreq_channel.complete( |
|
302 | self._complete_id = self.kernel_manager.xreq_channel.complete( | |
297 |
text, self._get_input_buffer_cursor_line(), |
|
303 | text, self._get_input_buffer_cursor_line(), cursor_pos, | |
|
304 | self.input_buffer) | |||
298 | self._complete_pos = self._get_cursor().position() |
|
305 | self._complete_pos = self._get_cursor().position() | |
299 | return True |
|
306 | return True | |
300 |
|
307 | |||
301 | def _get_banner(self): |
|
308 | def _get_banner(self): | |
302 | """ Gets a banner to display at the beginning of a session. |
|
309 | """ Gets a banner to display at the beginning of a session. | |
303 | """ |
|
310 | """ | |
304 | banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \ |
|
311 | banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \ | |
305 | '"license" for more information.' |
|
312 | '"license" for more information.' | |
306 | return banner % (sys.version, sys.platform) |
|
313 | return banner % (sys.version, sys.platform) | |
307 |
|
314 | |||
308 | def _get_context(self, cursor=None): |
|
315 | def _get_context(self, cursor=None): | |
309 | """ Gets the context at the current cursor location. |
|
316 | """ Gets the context at the current cursor location. | |
310 | """ |
|
317 | """ | |
311 | if cursor is None: |
|
318 | if cursor is None: | |
312 | cursor = self._get_cursor() |
|
319 | cursor = self._get_cursor() | |
313 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock, |
|
320 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock, | |
314 | QtGui.QTextCursor.KeepAnchor) |
|
321 | QtGui.QTextCursor.KeepAnchor) | |
315 | text = str(cursor.selection().toPlainText()) |
|
322 | text = str(cursor.selection().toPlainText()) | |
316 | return self._completion_lexer.get_context(text) |
|
323 | return self._completion_lexer.get_context(text) | |
317 |
|
324 | |||
318 | def _interrupt_kernel(self): |
|
325 | def _interrupt_kernel(self): | |
319 | """ Attempts to the interrupt the kernel. |
|
326 | """ Attempts to the interrupt the kernel. | |
320 | """ |
|
327 | """ | |
321 | if self.kernel_manager.has_kernel: |
|
328 | if self.kernel_manager.has_kernel: | |
322 | self.kernel_manager.signal_kernel(signal.SIGINT) |
|
329 | self.kernel_manager.signal_kernel(signal.SIGINT) | |
323 | else: |
|
330 | else: | |
324 | self._append_plain_text('Kernel process is either remote or ' |
|
331 | self._append_plain_text('Kernel process is either remote or ' | |
325 | 'unspecified. Cannot interrupt.\n') |
|
332 | 'unspecified. Cannot interrupt.\n') | |
326 |
|
333 | |||
327 | def _process_execute_abort(self, msg): |
|
334 | def _process_execute_abort(self, msg): | |
328 | """ Process a reply for an aborted execution request. |
|
335 | """ Process a reply for an aborted execution request. | |
329 | """ |
|
336 | """ | |
330 | self._append_plain_text("ERROR: execution aborted\n") |
|
337 | self._append_plain_text("ERROR: execution aborted\n") | |
331 |
|
338 | |||
332 | def _process_execute_error(self, msg): |
|
339 | def _process_execute_error(self, msg): | |
333 | """ Process a reply for an execution request that resulted in an error. |
|
340 | """ Process a reply for an execution request that resulted in an error. | |
334 | """ |
|
341 | """ | |
335 | content = msg['content'] |
|
342 | content = msg['content'] | |
336 | traceback = ''.join(content['traceback']) |
|
343 | traceback = ''.join(content['traceback']) | |
337 | self._append_plain_text(traceback) |
|
344 | self._append_plain_text(traceback) | |
338 |
|
345 | |||
339 | def _process_execute_ok(self, msg): |
|
346 | def _process_execute_ok(self, msg): | |
340 | """ Process a reply for a successful execution equest. |
|
347 | """ Process a reply for a successful execution equest. | |
341 | """ |
|
348 | """ | |
342 | payload = msg['content']['payload'] |
|
349 | payload = msg['content']['payload'] | |
343 | for item in payload: |
|
350 | for item in payload: | |
344 | if not self._process_execute_payload(item): |
|
351 | if not self._process_execute_payload(item): | |
345 | warning = 'Received unknown payload of type %s\n' |
|
352 | warning = 'Received unknown payload of type %s\n' | |
346 | self._append_plain_text(warning % repr(item['source'])) |
|
353 | self._append_plain_text(warning % repr(item['source'])) | |
347 |
|
354 | |||
348 | def _process_execute_payload(self, item): |
|
355 | def _process_execute_payload(self, item): | |
349 | """ Process a single payload item from the list of payload items in an |
|
356 | """ Process a single payload item from the list of payload items in an | |
350 | execution reply. Returns whether the payload was handled. |
|
357 | execution reply. Returns whether the payload was handled. | |
351 | """ |
|
358 | """ | |
352 | # The basic FrontendWidget doesn't handle payloads, as they are a |
|
359 | # The basic FrontendWidget doesn't handle payloads, as they are a | |
353 | # mechanism for going beyond the standard Python interpreter model. |
|
360 | # mechanism for going beyond the standard Python interpreter model. | |
354 | return False |
|
361 | return False | |
355 |
|
362 | |||
356 | def _show_interpreter_prompt(self): |
|
363 | def _show_interpreter_prompt(self): | |
357 | """ Shows a prompt for the interpreter. |
|
364 | """ Shows a prompt for the interpreter. | |
358 | """ |
|
365 | """ | |
359 | self._show_prompt('>>> ') |
|
366 | self._show_prompt('>>> ') | |
360 |
|
367 | |||
361 | def _show_interpreter_prompt_for_reply(self, msg): |
|
368 | def _show_interpreter_prompt_for_reply(self, msg): | |
362 | """ Shows a prompt for the interpreter given an 'execute_reply' message. |
|
369 | """ Shows a prompt for the interpreter given an 'execute_reply' message. | |
363 | """ |
|
370 | """ | |
364 | self._show_interpreter_prompt() |
|
371 | self._show_interpreter_prompt() | |
365 |
|
372 | |||
366 | #------ Signal handlers ---------------------------------------------------- |
|
373 | #------ Signal handlers ---------------------------------------------------- | |
367 |
|
374 | |||
368 | def _document_contents_change(self, position, removed, added): |
|
375 | def _document_contents_change(self, position, removed, added): | |
369 | """ Called whenever the document's content changes. Display a call tip |
|
376 | """ Called whenever the document's content changes. Display a call tip | |
370 | if appropriate. |
|
377 | if appropriate. | |
371 | """ |
|
378 | """ | |
372 | # Calculate where the cursor should be *after* the change: |
|
379 | # Calculate where the cursor should be *after* the change: | |
373 | position += added |
|
380 | position += added | |
374 |
|
381 | |||
375 | document = self._control.document() |
|
382 | document = self._control.document() | |
376 | if position == self._get_cursor().position(): |
|
383 | if position == self._get_cursor().position(): | |
377 | self._call_tip() |
|
384 | self._call_tip() |
@@ -1,387 +1,398 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | """A simple interactive kernel that talks to a frontend over 0MQ. |
|
2 | """A simple interactive kernel that talks to a frontend over 0MQ. | |
3 |
|
3 | |||
4 | Things to do: |
|
4 | Things to do: | |
5 |
|
5 | |||
6 | * Implement `set_parent` logic. Right before doing exec, the Kernel should |
|
6 | * Implement `set_parent` logic. Right before doing exec, the Kernel should | |
7 | call set_parent on all the PUB objects with the message about to be executed. |
|
7 | call set_parent on all the PUB objects with the message about to be executed. | |
8 | * Implement random port and security key logic. |
|
8 | * Implement random port and security key logic. | |
9 | * Implement control messages. |
|
9 | * Implement control messages. | |
10 | * Implement event loop and poll version. |
|
10 | * Implement event loop and poll version. | |
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | # Standard library imports. |
|
17 | # Standard library imports. | |
18 | import __builtin__ |
|
18 | import __builtin__ | |
19 | import sys |
|
19 | import sys | |
20 | import time |
|
20 | import time | |
21 | import traceback |
|
21 | import traceback | |
22 |
|
22 | |||
23 | # System library imports. |
|
23 | # System library imports. | |
24 | import zmq |
|
24 | import zmq | |
25 |
|
25 | |||
26 | # Local imports. |
|
26 | # Local imports. | |
27 | from IPython.config.configurable import Configurable |
|
27 | from IPython.config.configurable import Configurable | |
28 | from IPython.utils.traitlets import Instance |
|
28 | from IPython.utils.traitlets import Instance | |
29 | from completer import KernelCompleter |
|
29 | from completer import KernelCompleter | |
30 | from entry_point import base_launch_kernel, make_argument_parser, make_kernel, \ |
|
30 | from entry_point import base_launch_kernel, make_argument_parser, make_kernel, \ | |
31 | start_kernel |
|
31 | start_kernel | |
32 | from iostream import OutStream |
|
32 | from iostream import OutStream | |
33 | from session import Session, Message |
|
33 | from session import Session, Message | |
34 | from zmqshell import ZMQInteractiveShell |
|
34 | from zmqshell import ZMQInteractiveShell | |
35 |
|
35 | |||
36 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
37 | # Main kernel class |
|
37 | # Main kernel class | |
38 | #----------------------------------------------------------------------------- |
|
38 | #----------------------------------------------------------------------------- | |
39 |
|
39 | |||
40 | class Kernel(Configurable): |
|
40 | class Kernel(Configurable): | |
41 |
|
41 | |||
42 | #--------------------------------------------------------------------------- |
|
42 | #--------------------------------------------------------------------------- | |
43 | # Kernel interface |
|
43 | # Kernel interface | |
44 | #--------------------------------------------------------------------------- |
|
44 | #--------------------------------------------------------------------------- | |
45 |
|
45 | |||
46 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
46 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
47 | session = Instance(Session) |
|
47 | session = Instance(Session) | |
48 | reply_socket = Instance('zmq.Socket') |
|
48 | reply_socket = Instance('zmq.Socket') | |
49 | pub_socket = Instance('zmq.Socket') |
|
49 | pub_socket = Instance('zmq.Socket') | |
50 | req_socket = Instance('zmq.Socket') |
|
50 | req_socket = Instance('zmq.Socket') | |
51 |
|
51 | |||
52 | # Maps user-friendly backend names to matplotlib backend identifiers. |
|
52 | # Maps user-friendly backend names to matplotlib backend identifiers. | |
53 | _pylab_map = { 'tk': 'TkAgg', |
|
53 | _pylab_map = { 'tk': 'TkAgg', | |
54 | 'gtk': 'GTKAgg', |
|
54 | 'gtk': 'GTKAgg', | |
55 | 'wx': 'WXAgg', |
|
55 | 'wx': 'WXAgg', | |
56 | 'qt': 'Qt4Agg', # qt3 not supported |
|
56 | 'qt': 'Qt4Agg', # qt3 not supported | |
57 | 'qt4': 'Qt4Agg', |
|
57 | 'qt4': 'Qt4Agg', | |
58 | 'payload-svg' : \ |
|
58 | 'payload-svg' : \ | |
59 | 'module://IPython.zmq.pylab.backend_payload_svg' } |
|
59 | 'module://IPython.zmq.pylab.backend_payload_svg' } | |
60 |
|
60 | |||
61 | def __init__(self, **kwargs): |
|
61 | def __init__(self, **kwargs): | |
62 | super(Kernel, self).__init__(**kwargs) |
|
62 | super(Kernel, self).__init__(**kwargs) | |
63 |
|
63 | |||
64 | # Initialize the InteractiveShell subclass |
|
64 | # Initialize the InteractiveShell subclass | |
65 | self.shell = ZMQInteractiveShell.instance() |
|
65 | self.shell = ZMQInteractiveShell.instance() | |
66 | self.shell.displayhook.session = self.session |
|
66 | self.shell.displayhook.session = self.session | |
67 | self.shell.displayhook.pub_socket = self.pub_socket |
|
67 | self.shell.displayhook.pub_socket = self.pub_socket | |
68 |
|
68 | |||
69 | # TMP - hack while developing |
|
69 | # TMP - hack while developing | |
70 | self.shell._reply_content = None |
|
70 | self.shell._reply_content = None | |
71 |
|
71 | |||
72 | # Build dict of handlers for message types |
|
72 | # Build dict of handlers for message types | |
73 | msg_types = [ 'execute_request', 'complete_request', |
|
73 | msg_types = [ 'execute_request', 'complete_request', | |
74 | 'object_info_request', 'prompt_request', |
|
74 | 'object_info_request', 'prompt_request', | |
75 | 'history_request' ] |
|
75 | 'history_request' ] | |
76 | self.handlers = {} |
|
76 | self.handlers = {} | |
77 | for msg_type in msg_types: |
|
77 | for msg_type in msg_types: | |
78 | self.handlers[msg_type] = getattr(self, msg_type) |
|
78 | self.handlers[msg_type] = getattr(self, msg_type) | |
79 |
|
79 | |||
80 | def activate_pylab(self, backend=None, import_all=True): |
|
80 | def activate_pylab(self, backend=None, import_all=True): | |
81 | """ Activates pylab in this kernel's namespace. |
|
81 | """ Activates pylab in this kernel's namespace. | |
82 |
|
82 | |||
83 | Parameters: |
|
83 | Parameters: | |
84 | ----------- |
|
84 | ----------- | |
85 | backend : str, optional |
|
85 | backend : str, optional | |
86 | A valid backend name. |
|
86 | A valid backend name. | |
87 |
|
87 | |||
88 | import_all : bool, optional |
|
88 | import_all : bool, optional | |
89 | If true, an 'import *' is done from numpy and pylab. |
|
89 | If true, an 'import *' is done from numpy and pylab. | |
90 | """ |
|
90 | """ | |
91 | # FIXME: This is adapted from IPython.lib.pylabtools.pylab_activate. |
|
91 | # FIXME: This is adapted from IPython.lib.pylabtools.pylab_activate. | |
92 | # Common functionality should be refactored. |
|
92 | # Common functionality should be refactored. | |
93 |
|
93 | |||
94 | # We must set the desired backend before importing pylab. |
|
94 | # We must set the desired backend before importing pylab. | |
95 | import matplotlib |
|
95 | import matplotlib | |
96 | if backend: |
|
96 | if backend: | |
97 | backend_id = self._pylab_map[backend] |
|
97 | backend_id = self._pylab_map[backend] | |
98 | if backend_id.startswith('module://'): |
|
98 | if backend_id.startswith('module://'): | |
99 | # Work around bug in matplotlib: matplotlib.use converts the |
|
99 | # Work around bug in matplotlib: matplotlib.use converts the | |
100 | # backend_id to lowercase even if a module name is specified! |
|
100 | # backend_id to lowercase even if a module name is specified! | |
101 | matplotlib.rcParams['backend'] = backend_id |
|
101 | matplotlib.rcParams['backend'] = backend_id | |
102 | else: |
|
102 | else: | |
103 | matplotlib.use(backend_id) |
|
103 | matplotlib.use(backend_id) | |
104 |
|
104 | |||
105 | # Import numpy as np/pyplot as plt are conventions we're trying to |
|
105 | # Import numpy as np/pyplot as plt are conventions we're trying to | |
106 | # somewhat standardize on. Making them available to users by default |
|
106 | # somewhat standardize on. Making them available to users by default | |
107 | # will greatly help this. |
|
107 | # will greatly help this. | |
108 | exec ("import numpy\n" |
|
108 | exec ("import numpy\n" | |
109 | "import matplotlib\n" |
|
109 | "import matplotlib\n" | |
110 | "from matplotlib import pylab, mlab, pyplot\n" |
|
110 | "from matplotlib import pylab, mlab, pyplot\n" | |
111 | "np = numpy\n" |
|
111 | "np = numpy\n" | |
112 | "plt = pyplot\n" |
|
112 | "plt = pyplot\n" | |
113 | ) in self.shell.user_ns |
|
113 | ) in self.shell.user_ns | |
114 |
|
114 | |||
115 | if import_all: |
|
115 | if import_all: | |
116 | exec("from matplotlib.pylab import *\n" |
|
116 | exec("from matplotlib.pylab import *\n" | |
117 | "from numpy import *\n") in self.shell.user_ns |
|
117 | "from numpy import *\n") in self.shell.user_ns | |
118 |
|
118 | |||
119 | matplotlib.interactive(True) |
|
119 | matplotlib.interactive(True) | |
120 |
|
120 | |||
121 | def start(self): |
|
121 | def start(self): | |
122 | """ Start the kernel main loop. |
|
122 | """ Start the kernel main loop. | |
123 | """ |
|
123 | """ | |
124 | while True: |
|
124 | while True: | |
125 | ident = self.reply_socket.recv() |
|
125 | ident = self.reply_socket.recv() | |
126 | assert self.reply_socket.rcvmore(), "Missing message part." |
|
126 | assert self.reply_socket.rcvmore(), "Missing message part." | |
127 | msg = self.reply_socket.recv_json() |
|
127 | msg = self.reply_socket.recv_json() | |
128 | omsg = Message(msg) |
|
128 | omsg = Message(msg) | |
129 | print>>sys.__stdout__ |
|
129 | print>>sys.__stdout__ | |
130 | print>>sys.__stdout__, omsg |
|
130 | print>>sys.__stdout__, omsg | |
131 | handler = self.handlers.get(omsg.msg_type, None) |
|
131 | handler = self.handlers.get(omsg.msg_type, None) | |
132 | if handler is None: |
|
132 | if handler is None: | |
133 | print >> sys.__stderr__, "UNKNOWN MESSAGE TYPE:", omsg |
|
133 | print >> sys.__stderr__, "UNKNOWN MESSAGE TYPE:", omsg | |
134 | else: |
|
134 | else: | |
135 | handler(ident, omsg) |
|
135 | handler(ident, omsg) | |
136 |
|
136 | |||
137 | #--------------------------------------------------------------------------- |
|
137 | #--------------------------------------------------------------------------- | |
138 | # Kernel request handlers |
|
138 | # Kernel request handlers | |
139 | #--------------------------------------------------------------------------- |
|
139 | #--------------------------------------------------------------------------- | |
140 |
|
140 | |||
141 | def execute_request(self, ident, parent): |
|
141 | def execute_request(self, ident, parent): | |
142 | try: |
|
142 | try: | |
143 | code = parent[u'content'][u'code'] |
|
143 | code = parent[u'content'][u'code'] | |
144 | except: |
|
144 | except: | |
145 | print>>sys.__stderr__, "Got bad msg: " |
|
145 | print>>sys.__stderr__, "Got bad msg: " | |
146 | print>>sys.__stderr__, Message(parent) |
|
146 | print>>sys.__stderr__, Message(parent) | |
147 | return |
|
147 | return | |
148 | pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) |
|
148 | pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) | |
149 | self.pub_socket.send_json(pyin_msg) |
|
149 | self.pub_socket.send_json(pyin_msg) | |
150 |
|
150 | |||
151 | try: |
|
151 | try: | |
152 | # Replace raw_input. Note that is not sufficient to replace |
|
152 | # Replace raw_input. Note that is not sufficient to replace | |
153 | # raw_input in the user namespace. |
|
153 | # raw_input in the user namespace. | |
154 | raw_input = lambda prompt='': self._raw_input(prompt, ident, parent) |
|
154 | raw_input = lambda prompt='': self._raw_input(prompt, ident, parent) | |
155 | __builtin__.raw_input = raw_input |
|
155 | __builtin__.raw_input = raw_input | |
156 |
|
156 | |||
157 | # Set the parent message of the display hook and out streams. |
|
157 | # Set the parent message of the display hook and out streams. | |
158 | self.shell.displayhook.set_parent(parent) |
|
158 | self.shell.displayhook.set_parent(parent) | |
159 | sys.stdout.set_parent(parent) |
|
159 | sys.stdout.set_parent(parent) | |
160 | sys.stderr.set_parent(parent) |
|
160 | sys.stderr.set_parent(parent) | |
161 |
|
161 | |||
162 | # FIXME: runlines calls the exception handler itself. We should |
|
162 | # FIXME: runlines calls the exception handler itself. We should | |
163 | # clean this up. |
|
163 | # clean this up. | |
164 | self.shell._reply_content = None |
|
164 | self.shell._reply_content = None | |
165 | self.shell.runlines(code) |
|
165 | self.shell.runlines(code) | |
166 | except: |
|
166 | except: | |
167 | # FIXME: this code right now isn't being used yet by default, |
|
167 | # FIXME: this code right now isn't being used yet by default, | |
168 | # because the runlines() call above directly fires off exception |
|
168 | # because the runlines() call above directly fires off exception | |
169 | # reporting. This code, therefore, is only active in the scenario |
|
169 | # reporting. This code, therefore, is only active in the scenario | |
170 | # where runlines itself has an unhandled exception. We need to |
|
170 | # where runlines itself has an unhandled exception. We need to | |
171 | # uniformize this, for all exception construction to come from a |
|
171 | # uniformize this, for all exception construction to come from a | |
172 | # single location in the codbase. |
|
172 | # single location in the codbase. | |
173 | etype, evalue, tb = sys.exc_info() |
|
173 | etype, evalue, tb = sys.exc_info() | |
174 | tb_list = traceback.format_exception(etype, evalue, tb) |
|
174 | tb_list = traceback.format_exception(etype, evalue, tb) | |
175 | reply_content = self.shell._showtraceback(etype, evalue, tb_list) |
|
175 | reply_content = self.shell._showtraceback(etype, evalue, tb_list) | |
176 | else: |
|
176 | else: | |
177 | payload = self.shell.payload_manager.read_payload() |
|
177 | payload = self.shell.payload_manager.read_payload() | |
178 | # Be agressive about clearing the payload because we don't want |
|
178 | # Be agressive about clearing the payload because we don't want | |
179 | # it to sit in memory until the next execute_request comes in. |
|
179 | # it to sit in memory until the next execute_request comes in. | |
180 | self.shell.payload_manager.clear_payload() |
|
180 | self.shell.payload_manager.clear_payload() | |
181 | reply_content = { 'status' : 'ok', 'payload' : payload } |
|
181 | reply_content = { 'status' : 'ok', 'payload' : payload } | |
182 |
|
182 | |||
183 | # Compute the prompt information |
|
183 | # Compute the prompt information | |
184 | prompt_number = self.shell.displayhook.prompt_count |
|
184 | prompt_number = self.shell.displayhook.prompt_count | |
185 | reply_content['prompt_number'] = prompt_number |
|
185 | reply_content['prompt_number'] = prompt_number | |
186 | prompt_string = self.shell.displayhook.prompt1.peek_next_prompt() |
|
186 | prompt_string = self.shell.displayhook.prompt1.peek_next_prompt() | |
187 | next_prompt = {'prompt_string' : prompt_string, |
|
187 | next_prompt = {'prompt_string' : prompt_string, | |
188 | 'prompt_number' : prompt_number+1, |
|
188 | 'prompt_number' : prompt_number+1, | |
189 | 'input_sep' : self.shell.displayhook.input_sep} |
|
189 | 'input_sep' : self.shell.displayhook.input_sep} | |
190 | reply_content['next_prompt'] = next_prompt |
|
190 | reply_content['next_prompt'] = next_prompt | |
191 |
|
191 | |||
192 | # TMP - fish exception info out of shell, possibly left there by |
|
192 | # TMP - fish exception info out of shell, possibly left there by | |
193 | # runlines |
|
193 | # runlines | |
194 | if self.shell._reply_content is not None: |
|
194 | if self.shell._reply_content is not None: | |
195 | reply_content.update(self.shell._reply_content) |
|
195 | reply_content.update(self.shell._reply_content) | |
196 |
|
196 | |||
197 | # Flush output before sending the reply. |
|
197 | # Flush output before sending the reply. | |
198 | sys.stderr.flush() |
|
198 | sys.stderr.flush() | |
199 | sys.stdout.flush() |
|
199 | sys.stdout.flush() | |
200 |
|
200 | |||
201 | # Send the reply. |
|
201 | # Send the reply. | |
202 | reply_msg = self.session.msg(u'execute_reply', reply_content, parent) |
|
202 | reply_msg = self.session.msg(u'execute_reply', reply_content, parent) | |
203 | print>>sys.__stdout__, Message(reply_msg) |
|
203 | print>>sys.__stdout__, Message(reply_msg) | |
204 | self.reply_socket.send(ident, zmq.SNDMORE) |
|
204 | self.reply_socket.send(ident, zmq.SNDMORE) | |
205 | self.reply_socket.send_json(reply_msg) |
|
205 | self.reply_socket.send_json(reply_msg) | |
206 | if reply_msg['content']['status'] == u'error': |
|
206 | if reply_msg['content']['status'] == u'error': | |
207 | self._abort_queue() |
|
207 | self._abort_queue() | |
208 |
|
208 | |||
209 | def complete_request(self, ident, parent): |
|
209 | def complete_request(self, ident, parent): | |
210 | matches = {'matches' : self._complete(parent), |
|
210 | matches = {'matches' : self._complete(parent), | |
211 | 'status' : 'ok'} |
|
211 | 'status' : 'ok'} | |
212 | completion_msg = self.session.send(self.reply_socket, 'complete_reply', |
|
212 | completion_msg = self.session.send(self.reply_socket, 'complete_reply', | |
213 | matches, parent, ident) |
|
213 | matches, parent, ident) | |
214 | print >> sys.__stdout__, completion_msg |
|
214 | print >> sys.__stdout__, completion_msg | |
215 |
|
215 | |||
216 | def object_info_request(self, ident, parent): |
|
216 | def object_info_request(self, ident, parent): | |
217 | context = parent['content']['oname'].split('.') |
|
217 | context = parent['content']['oname'].split('.') | |
218 | object_info = self._object_info(context) |
|
218 | object_info = self._object_info(context) | |
219 | msg = self.session.send(self.reply_socket, 'object_info_reply', |
|
219 | msg = self.session.send(self.reply_socket, 'object_info_reply', | |
220 | object_info, parent, ident) |
|
220 | object_info, parent, ident) | |
221 | print >> sys.__stdout__, msg |
|
221 | print >> sys.__stdout__, msg | |
222 |
|
222 | |||
223 | def prompt_request(self, ident, parent): |
|
223 | def prompt_request(self, ident, parent): | |
224 | prompt_number = self.shell.displayhook.prompt_count |
|
224 | prompt_number = self.shell.displayhook.prompt_count | |
225 | prompt_string = self.shell.displayhook.prompt1.peek_next_prompt() |
|
225 | prompt_string = self.shell.displayhook.prompt1.peek_next_prompt() | |
226 | content = {'prompt_string' : prompt_string, |
|
226 | content = {'prompt_string' : prompt_string, | |
227 | 'prompt_number' : prompt_number+1} |
|
227 | 'prompt_number' : prompt_number+1} | |
228 | msg = self.session.send(self.reply_socket, 'prompt_reply', |
|
228 | msg = self.session.send(self.reply_socket, 'prompt_reply', | |
229 | content, parent, ident) |
|
229 | content, parent, ident) | |
230 | print >> sys.__stdout__, msg |
|
230 | print >> sys.__stdout__, msg | |
231 |
|
231 | |||
232 | def history_request(self, ident, parent): |
|
232 | def history_request(self, ident, parent): | |
233 | output = parent['content'].get('output', True) |
|
233 | output = parent['content'].get('output', True) | |
234 | index = parent['content'].get('index') |
|
234 | index = parent['content'].get('index') | |
235 | raw = parent['content'].get('raw', False) |
|
235 | raw = parent['content'].get('raw', False) | |
236 | hist = self.shell.get_history(index=index, raw=raw, output=output) |
|
236 | hist = self.shell.get_history(index=index, raw=raw, output=output) | |
237 | content = {'history' : hist} |
|
237 | content = {'history' : hist} | |
238 | msg = self.session.send(self.reply_socket, 'history_reply', |
|
238 | msg = self.session.send(self.reply_socket, 'history_reply', | |
239 | content, parent, ident) |
|
239 | content, parent, ident) | |
240 | print >> sys.__stdout__, msg |
|
240 | print >> sys.__stdout__, msg | |
241 |
|
241 | |||
242 | #--------------------------------------------------------------------------- |
|
242 | #--------------------------------------------------------------------------- | |
243 | # Protected interface |
|
243 | # Protected interface | |
244 | #--------------------------------------------------------------------------- |
|
244 | #--------------------------------------------------------------------------- | |
245 |
|
245 | |||
246 | def _abort_queue(self): |
|
246 | def _abort_queue(self): | |
247 | while True: |
|
247 | while True: | |
248 | try: |
|
248 | try: | |
249 | ident = self.reply_socket.recv(zmq.NOBLOCK) |
|
249 | ident = self.reply_socket.recv(zmq.NOBLOCK) | |
250 | except zmq.ZMQError, e: |
|
250 | except zmq.ZMQError, e: | |
251 | if e.errno == zmq.EAGAIN: |
|
251 | if e.errno == zmq.EAGAIN: | |
252 | break |
|
252 | break | |
253 | else: |
|
253 | else: | |
254 | assert self.reply_socket.rcvmore(), "Unexpected missing message part." |
|
254 | assert self.reply_socket.rcvmore(), "Unexpected missing message part." | |
255 | msg = self.reply_socket.recv_json() |
|
255 | msg = self.reply_socket.recv_json() | |
256 | print>>sys.__stdout__, "Aborting:" |
|
256 | print>>sys.__stdout__, "Aborting:" | |
257 | print>>sys.__stdout__, Message(msg) |
|
257 | print>>sys.__stdout__, Message(msg) | |
258 | msg_type = msg['msg_type'] |
|
258 | msg_type = msg['msg_type'] | |
259 | reply_type = msg_type.split('_')[0] + '_reply' |
|
259 | reply_type = msg_type.split('_')[0] + '_reply' | |
260 | reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg) |
|
260 | reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg) | |
261 | print>>sys.__stdout__, Message(reply_msg) |
|
261 | print>>sys.__stdout__, Message(reply_msg) | |
262 | self.reply_socket.send(ident,zmq.SNDMORE) |
|
262 | self.reply_socket.send(ident,zmq.SNDMORE) | |
263 | self.reply_socket.send_json(reply_msg) |
|
263 | self.reply_socket.send_json(reply_msg) | |
264 | # We need to wait a bit for requests to come in. This can probably |
|
264 | # We need to wait a bit for requests to come in. This can probably | |
265 | # be set shorter for true asynchronous clients. |
|
265 | # be set shorter for true asynchronous clients. | |
266 | time.sleep(0.1) |
|
266 | time.sleep(0.1) | |
267 |
|
267 | |||
268 | def _raw_input(self, prompt, ident, parent): |
|
268 | def _raw_input(self, prompt, ident, parent): | |
269 | # Flush output before making the request. |
|
269 | # Flush output before making the request. | |
270 | sys.stderr.flush() |
|
270 | sys.stderr.flush() | |
271 | sys.stdout.flush() |
|
271 | sys.stdout.flush() | |
272 |
|
272 | |||
273 | # Send the input request. |
|
273 | # Send the input request. | |
274 | content = dict(prompt=prompt) |
|
274 | content = dict(prompt=prompt) | |
275 | msg = self.session.msg(u'input_request', content, parent) |
|
275 | msg = self.session.msg(u'input_request', content, parent) | |
276 | self.req_socket.send_json(msg) |
|
276 | self.req_socket.send_json(msg) | |
277 |
|
277 | |||
278 | # Await a response. |
|
278 | # Await a response. | |
279 | reply = self.req_socket.recv_json() |
|
279 | reply = self.req_socket.recv_json() | |
280 | try: |
|
280 | try: | |
281 | value = reply['content']['value'] |
|
281 | value = reply['content']['value'] | |
282 | except: |
|
282 | except: | |
283 | print>>sys.__stderr__, "Got bad raw_input reply: " |
|
283 | print>>sys.__stderr__, "Got bad raw_input reply: " | |
284 | print>>sys.__stderr__, Message(parent) |
|
284 | print>>sys.__stderr__, Message(parent) | |
285 | value = '' |
|
285 | value = '' | |
286 | return value |
|
286 | return value | |
287 |
|
287 | |||
288 | def _complete(self, msg): |
|
288 | def _complete(self, msg): | |
289 | return self.shell.complete(msg.content.line) |
|
289 | #from IPython.utils.io import rprint # dbg | |
|
290 | #rprint('\n\n**MSG**\n\n', msg) # dbg | |||
|
291 | #import traceback; rprint(''.join(traceback.format_stack())) # dbg | |||
|
292 | c = msg['content'] | |||
|
293 | try: | |||
|
294 | cpos = int(c['cursor_pos']) | |||
|
295 | except: | |||
|
296 | # If we don't get something that we can convert to an integer, at | |||
|
297 | # leasat attempt the completion guessing the cursor is at the end | |||
|
298 | # of the text | |||
|
299 | cpos = len(c['text']) | |||
|
300 | return self.shell.complete(c['text'], c['line'], cpos) | |||
290 |
|
301 | |||
291 | def _object_info(self, context): |
|
302 | def _object_info(self, context): | |
292 | symbol, leftover = self._symbol_from_context(context) |
|
303 | symbol, leftover = self._symbol_from_context(context) | |
293 | if symbol is not None and not leftover: |
|
304 | if symbol is not None and not leftover: | |
294 | doc = getattr(symbol, '__doc__', '') |
|
305 | doc = getattr(symbol, '__doc__', '') | |
295 | else: |
|
306 | else: | |
296 | doc = '' |
|
307 | doc = '' | |
297 | object_info = dict(docstring = doc) |
|
308 | object_info = dict(docstring = doc) | |
298 | return object_info |
|
309 | return object_info | |
299 |
|
310 | |||
300 | def _symbol_from_context(self, context): |
|
311 | def _symbol_from_context(self, context): | |
301 | if not context: |
|
312 | if not context: | |
302 | return None, context |
|
313 | return None, context | |
303 |
|
314 | |||
304 | base_symbol_string = context[0] |
|
315 | base_symbol_string = context[0] | |
305 | symbol = self.shell.user_ns.get(base_symbol_string, None) |
|
316 | symbol = self.shell.user_ns.get(base_symbol_string, None) | |
306 | if symbol is None: |
|
317 | if symbol is None: | |
307 | symbol = __builtin__.__dict__.get(base_symbol_string, None) |
|
318 | symbol = __builtin__.__dict__.get(base_symbol_string, None) | |
308 | if symbol is None: |
|
319 | if symbol is None: | |
309 | return None, context |
|
320 | return None, context | |
310 |
|
321 | |||
311 | context = context[1:] |
|
322 | context = context[1:] | |
312 | for i, name in enumerate(context): |
|
323 | for i, name in enumerate(context): | |
313 | new_symbol = getattr(symbol, name, None) |
|
324 | new_symbol = getattr(symbol, name, None) | |
314 | if new_symbol is None: |
|
325 | if new_symbol is None: | |
315 | return symbol, context[i:] |
|
326 | return symbol, context[i:] | |
316 | else: |
|
327 | else: | |
317 | symbol = new_symbol |
|
328 | symbol = new_symbol | |
318 |
|
329 | |||
319 | return symbol, [] |
|
330 | return symbol, [] | |
320 |
|
331 | |||
321 | #----------------------------------------------------------------------------- |
|
332 | #----------------------------------------------------------------------------- | |
322 | # Kernel main and launch functions |
|
333 | # Kernel main and launch functions | |
323 | #----------------------------------------------------------------------------- |
|
334 | #----------------------------------------------------------------------------- | |
324 |
|
335 | |||
325 | def launch_kernel(xrep_port=0, pub_port=0, req_port=0, independent=False, |
|
336 | def launch_kernel(xrep_port=0, pub_port=0, req_port=0, independent=False, | |
326 | pylab=False): |
|
337 | pylab=False): | |
327 | """ Launches a localhost kernel, binding to the specified ports. |
|
338 | """ Launches a localhost kernel, binding to the specified ports. | |
328 |
|
339 | |||
329 | Parameters |
|
340 | Parameters | |
330 | ---------- |
|
341 | ---------- | |
331 | xrep_port : int, optional |
|
342 | xrep_port : int, optional | |
332 | The port to use for XREP channel. |
|
343 | The port to use for XREP channel. | |
333 |
|
344 | |||
334 | pub_port : int, optional |
|
345 | pub_port : int, optional | |
335 | The port to use for the SUB channel. |
|
346 | The port to use for the SUB channel. | |
336 |
|
347 | |||
337 | req_port : int, optional |
|
348 | req_port : int, optional | |
338 | The port to use for the REQ (raw input) channel. |
|
349 | The port to use for the REQ (raw input) channel. | |
339 |
|
350 | |||
340 | independent : bool, optional (default False) |
|
351 | independent : bool, optional (default False) | |
341 | If set, the kernel process is guaranteed to survive if this process |
|
352 | If set, the kernel process is guaranteed to survive if this process | |
342 | dies. If not set, an effort is made to ensure that the kernel is killed |
|
353 | dies. If not set, an effort is made to ensure that the kernel is killed | |
343 | when this process dies. Note that in this case it is still good practice |
|
354 | when this process dies. Note that in this case it is still good practice | |
344 | to kill kernels manually before exiting. |
|
355 | to kill kernels manually before exiting. | |
345 |
|
356 | |||
346 | pylab : bool or string, optional (default False) |
|
357 | pylab : bool or string, optional (default False) | |
347 | If not False, the kernel will be launched with pylab enabled. If a |
|
358 | If not False, the kernel will be launched with pylab enabled. If a | |
348 | string is passed, matplotlib will use the specified backend. Otherwise, |
|
359 | string is passed, matplotlib will use the specified backend. Otherwise, | |
349 | matplotlib's default backend will be used. |
|
360 | matplotlib's default backend will be used. | |
350 |
|
361 | |||
351 | Returns |
|
362 | Returns | |
352 | ------- |
|
363 | ------- | |
353 | A tuple of form: |
|
364 | A tuple of form: | |
354 | (kernel_process, xrep_port, pub_port, req_port) |
|
365 | (kernel_process, xrep_port, pub_port, req_port) | |
355 | where kernel_process is a Popen object and the ports are integers. |
|
366 | where kernel_process is a Popen object and the ports are integers. | |
356 | """ |
|
367 | """ | |
357 | extra_arguments = [] |
|
368 | extra_arguments = [] | |
358 | if pylab: |
|
369 | if pylab: | |
359 | extra_arguments.append('--pylab') |
|
370 | extra_arguments.append('--pylab') | |
360 | if isinstance(pylab, basestring): |
|
371 | if isinstance(pylab, basestring): | |
361 | extra_arguments.append(pylab) |
|
372 | extra_arguments.append(pylab) | |
362 | return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', |
|
373 | return base_launch_kernel('from IPython.zmq.ipkernel import main; main()', | |
363 | xrep_port, pub_port, req_port, independent, |
|
374 | xrep_port, pub_port, req_port, independent, | |
364 | extra_arguments) |
|
375 | extra_arguments) | |
365 |
|
376 | |||
366 | def main(): |
|
377 | def main(): | |
367 | """ The IPython kernel main entry point. |
|
378 | """ The IPython kernel main entry point. | |
368 | """ |
|
379 | """ | |
369 | parser = make_argument_parser() |
|
380 | parser = make_argument_parser() | |
370 | parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?', |
|
381 | parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?', | |
371 | const='auto', help = \ |
|
382 | const='auto', help = \ | |
372 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ |
|
383 | "Pre-load matplotlib and numpy for interactive use. If GUI is not \ | |
373 | given, the GUI backend is matplotlib's, otherwise use one of: \ |
|
384 | given, the GUI backend is matplotlib's, otherwise use one of: \ | |
374 | ['tk', 'gtk', 'qt', 'wx', 'payload-svg'].") |
|
385 | ['tk', 'gtk', 'qt', 'wx', 'payload-svg'].") | |
375 | namespace = parser.parse_args() |
|
386 | namespace = parser.parse_args() | |
376 |
|
387 | |||
377 | kernel = make_kernel(namespace, Kernel, OutStream) |
|
388 | kernel = make_kernel(namespace, Kernel, OutStream) | |
378 | if namespace.pylab: |
|
389 | if namespace.pylab: | |
379 | if namespace.pylab == 'auto': |
|
390 | if namespace.pylab == 'auto': | |
380 | kernel.activate_pylab() |
|
391 | kernel.activate_pylab() | |
381 | else: |
|
392 | else: | |
382 | kernel.activate_pylab(namespace.pylab) |
|
393 | kernel.activate_pylab(namespace.pylab) | |
383 |
|
394 | |||
384 | start_kernel(namespace, kernel) |
|
395 | start_kernel(namespace, kernel) | |
385 |
|
396 | |||
386 | if __name__ == '__main__': |
|
397 | if __name__ == '__main__': | |
387 | main() |
|
398 | main() |
@@ -1,578 +1,578 b'' | |||||
1 | """Base classes to manage the interaction with a running kernel. |
|
1 | """Base classes to manage the interaction with a running kernel. | |
2 |
|
2 | |||
3 | Todo |
|
3 | Todo | |
4 | ==== |
|
4 | ==== | |
5 |
|
5 | |||
6 | * Create logger to handle debugging and console messages. |
|
6 | * Create logger to handle debugging and console messages. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2008-2010 The IPython Development Team |
|
10 | # Copyright (C) 2008-2010 The IPython Development Team | |
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 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Imports |
|
17 | # Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | # Standard library imports. |
|
20 | # Standard library imports. | |
21 | from Queue import Queue, Empty |
|
21 | from Queue import Queue, Empty | |
22 | from subprocess import Popen |
|
22 | from subprocess import Popen | |
23 | from threading import Thread |
|
23 | from threading import Thread | |
24 | import time |
|
24 | import time | |
25 |
|
25 | |||
26 | # System library imports. |
|
26 | # System library imports. | |
27 | import zmq |
|
27 | import zmq | |
28 | from zmq import POLLIN, POLLOUT, POLLERR |
|
28 | from zmq import POLLIN, POLLOUT, POLLERR | |
29 | from zmq.eventloop import ioloop |
|
29 | from zmq.eventloop import ioloop | |
30 |
|
30 | |||
31 | # Local imports. |
|
31 | # Local imports. | |
32 | from IPython.utils.traitlets import HasTraits, Any, Instance, Type, TCPAddress |
|
32 | from IPython.utils.traitlets import HasTraits, Any, Instance, Type, TCPAddress | |
33 | from session import Session |
|
33 | from session import Session | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 | # Constants and exceptions |
|
36 | # Constants and exceptions | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
39 | LOCALHOST = '127.0.0.1' |
|
39 | LOCALHOST = '127.0.0.1' | |
40 |
|
40 | |||
41 | class InvalidPortNumber(Exception): |
|
41 | class InvalidPortNumber(Exception): | |
42 | pass |
|
42 | pass | |
43 |
|
43 | |||
44 | #----------------------------------------------------------------------------- |
|
44 | #----------------------------------------------------------------------------- | |
45 | # ZMQ Socket Channel classes |
|
45 | # ZMQ Socket Channel classes | |
46 | #----------------------------------------------------------------------------- |
|
46 | #----------------------------------------------------------------------------- | |
47 |
|
47 | |||
48 | class ZmqSocketChannel(Thread): |
|
48 | class ZmqSocketChannel(Thread): | |
49 | """The base class for the channels that use ZMQ sockets. |
|
49 | """The base class for the channels that use ZMQ sockets. | |
50 | """ |
|
50 | """ | |
51 | context = None |
|
51 | context = None | |
52 | session = None |
|
52 | session = None | |
53 | socket = None |
|
53 | socket = None | |
54 | ioloop = None |
|
54 | ioloop = None | |
55 | iostate = None |
|
55 | iostate = None | |
56 | _address = None |
|
56 | _address = None | |
57 |
|
57 | |||
58 | def __init__(self, context, session, address): |
|
58 | def __init__(self, context, session, address): | |
59 | """Create a channel |
|
59 | """Create a channel | |
60 |
|
60 | |||
61 | Parameters |
|
61 | Parameters | |
62 | ---------- |
|
62 | ---------- | |
63 | context : :class:`zmq.Context` |
|
63 | context : :class:`zmq.Context` | |
64 | The ZMQ context to use. |
|
64 | The ZMQ context to use. | |
65 | session : :class:`session.Session` |
|
65 | session : :class:`session.Session` | |
66 | The session to use. |
|
66 | The session to use. | |
67 | address : tuple |
|
67 | address : tuple | |
68 | Standard (ip, port) tuple that the kernel is listening on. |
|
68 | Standard (ip, port) tuple that the kernel is listening on. | |
69 | """ |
|
69 | """ | |
70 | super(ZmqSocketChannel, self).__init__() |
|
70 | super(ZmqSocketChannel, self).__init__() | |
71 | self.daemon = True |
|
71 | self.daemon = True | |
72 |
|
72 | |||
73 | self.context = context |
|
73 | self.context = context | |
74 | self.session = session |
|
74 | self.session = session | |
75 | if address[1] == 0: |
|
75 | if address[1] == 0: | |
76 | message = 'The port number for a channel cannot be 0.' |
|
76 | message = 'The port number for a channel cannot be 0.' | |
77 | raise InvalidPortNumber(message) |
|
77 | raise InvalidPortNumber(message) | |
78 | self._address = address |
|
78 | self._address = address | |
79 |
|
79 | |||
80 | def stop(self): |
|
80 | def stop(self): | |
81 | """Stop the channel's activity. |
|
81 | """Stop the channel's activity. | |
82 |
|
82 | |||
83 | This calls :method:`Thread.join` and returns when the thread |
|
83 | This calls :method:`Thread.join` and returns when the thread | |
84 | terminates. :class:`RuntimeError` will be raised if |
|
84 | terminates. :class:`RuntimeError` will be raised if | |
85 | :method:`self.start` is called again. |
|
85 | :method:`self.start` is called again. | |
86 | """ |
|
86 | """ | |
87 | self.join() |
|
87 | self.join() | |
88 |
|
88 | |||
89 | @property |
|
89 | @property | |
90 | def address(self): |
|
90 | def address(self): | |
91 | """Get the channel's address as an (ip, port) tuple. |
|
91 | """Get the channel's address as an (ip, port) tuple. | |
92 |
|
92 | |||
93 | By the default, the address is (localhost, 0), where 0 means a random |
|
93 | By the default, the address is (localhost, 0), where 0 means a random | |
94 | port. |
|
94 | port. | |
95 | """ |
|
95 | """ | |
96 | return self._address |
|
96 | return self._address | |
97 |
|
97 | |||
98 | def add_io_state(self, state): |
|
98 | def add_io_state(self, state): | |
99 | """Add IO state to the eventloop. |
|
99 | """Add IO state to the eventloop. | |
100 |
|
100 | |||
101 | Parameters |
|
101 | Parameters | |
102 | ---------- |
|
102 | ---------- | |
103 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR |
|
103 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR | |
104 | The IO state flag to set. |
|
104 | The IO state flag to set. | |
105 |
|
105 | |||
106 | This is thread safe as it uses the thread safe IOLoop.add_callback. |
|
106 | This is thread safe as it uses the thread safe IOLoop.add_callback. | |
107 | """ |
|
107 | """ | |
108 | def add_io_state_callback(): |
|
108 | def add_io_state_callback(): | |
109 | if not self.iostate & state: |
|
109 | if not self.iostate & state: | |
110 | self.iostate = self.iostate | state |
|
110 | self.iostate = self.iostate | state | |
111 | self.ioloop.update_handler(self.socket, self.iostate) |
|
111 | self.ioloop.update_handler(self.socket, self.iostate) | |
112 | self.ioloop.add_callback(add_io_state_callback) |
|
112 | self.ioloop.add_callback(add_io_state_callback) | |
113 |
|
113 | |||
114 | def drop_io_state(self, state): |
|
114 | def drop_io_state(self, state): | |
115 | """Drop IO state from the eventloop. |
|
115 | """Drop IO state from the eventloop. | |
116 |
|
116 | |||
117 | Parameters |
|
117 | Parameters | |
118 | ---------- |
|
118 | ---------- | |
119 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR |
|
119 | state : zmq.POLLIN|zmq.POLLOUT|zmq.POLLERR | |
120 | The IO state flag to set. |
|
120 | The IO state flag to set. | |
121 |
|
121 | |||
122 | This is thread safe as it uses the thread safe IOLoop.add_callback. |
|
122 | This is thread safe as it uses the thread safe IOLoop.add_callback. | |
123 | """ |
|
123 | """ | |
124 | def drop_io_state_callback(): |
|
124 | def drop_io_state_callback(): | |
125 | if self.iostate & state: |
|
125 | if self.iostate & state: | |
126 | self.iostate = self.iostate & (~state) |
|
126 | self.iostate = self.iostate & (~state) | |
127 | self.ioloop.update_handler(self.socket, self.iostate) |
|
127 | self.ioloop.update_handler(self.socket, self.iostate) | |
128 | self.ioloop.add_callback(drop_io_state_callback) |
|
128 | self.ioloop.add_callback(drop_io_state_callback) | |
129 |
|
129 | |||
130 |
|
130 | |||
131 | class XReqSocketChannel(ZmqSocketChannel): |
|
131 | class XReqSocketChannel(ZmqSocketChannel): | |
132 | """The XREQ channel for issues request/replies to the kernel. |
|
132 | """The XREQ channel for issues request/replies to the kernel. | |
133 | """ |
|
133 | """ | |
134 |
|
134 | |||
135 | command_queue = None |
|
135 | command_queue = None | |
136 |
|
136 | |||
137 | def __init__(self, context, session, address): |
|
137 | def __init__(self, context, session, address): | |
138 | self.command_queue = Queue() |
|
138 | self.command_queue = Queue() | |
139 | super(XReqSocketChannel, self).__init__(context, session, address) |
|
139 | super(XReqSocketChannel, self).__init__(context, session, address) | |
140 |
|
140 | |||
141 | def run(self): |
|
141 | def run(self): | |
142 | """The thread's main activity. Call start() instead.""" |
|
142 | """The thread's main activity. Call start() instead.""" | |
143 | self.socket = self.context.socket(zmq.XREQ) |
|
143 | self.socket = self.context.socket(zmq.XREQ) | |
144 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) |
|
144 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) | |
145 | self.socket.connect('tcp://%s:%i' % self.address) |
|
145 | self.socket.connect('tcp://%s:%i' % self.address) | |
146 | self.ioloop = ioloop.IOLoop() |
|
146 | self.ioloop = ioloop.IOLoop() | |
147 | self.iostate = POLLERR|POLLIN |
|
147 | self.iostate = POLLERR|POLLIN | |
148 | self.ioloop.add_handler(self.socket, self._handle_events, |
|
148 | self.ioloop.add_handler(self.socket, self._handle_events, | |
149 | self.iostate) |
|
149 | self.iostate) | |
150 | self.ioloop.start() |
|
150 | self.ioloop.start() | |
151 |
|
151 | |||
152 | def stop(self): |
|
152 | def stop(self): | |
153 | self.ioloop.stop() |
|
153 | self.ioloop.stop() | |
154 | super(XReqSocketChannel, self).stop() |
|
154 | super(XReqSocketChannel, self).stop() | |
155 |
|
155 | |||
156 | def call_handlers(self, msg): |
|
156 | def call_handlers(self, msg): | |
157 | """This method is called in the ioloop thread when a message arrives. |
|
157 | """This method is called in the ioloop thread when a message arrives. | |
158 |
|
158 | |||
159 | Subclasses should override this method to handle incoming messages. |
|
159 | Subclasses should override this method to handle incoming messages. | |
160 | It is important to remember that this method is called in the thread |
|
160 | It is important to remember that this method is called in the thread | |
161 | so that some logic must be done to ensure that the application leve |
|
161 | so that some logic must be done to ensure that the application leve | |
162 | handlers are called in the application thread. |
|
162 | handlers are called in the application thread. | |
163 | """ |
|
163 | """ | |
164 | raise NotImplementedError('call_handlers must be defined in a subclass.') |
|
164 | raise NotImplementedError('call_handlers must be defined in a subclass.') | |
165 |
|
165 | |||
166 | def execute(self, code): |
|
166 | def execute(self, code): | |
167 | """Execute code in the kernel. |
|
167 | """Execute code in the kernel. | |
168 |
|
168 | |||
169 | Parameters |
|
169 | Parameters | |
170 | ---------- |
|
170 | ---------- | |
171 | code : str |
|
171 | code : str | |
172 | A string of Python code. |
|
172 | A string of Python code. | |
173 |
|
173 | |||
174 | Returns |
|
174 | Returns | |
175 | ------- |
|
175 | ------- | |
176 | The msg_id of the message sent. |
|
176 | The msg_id of the message sent. | |
177 | """ |
|
177 | """ | |
178 | # Create class for content/msg creation. Related to, but possibly |
|
178 | # Create class for content/msg creation. Related to, but possibly | |
179 | # not in Session. |
|
179 | # not in Session. | |
180 | content = dict(code=code) |
|
180 | content = dict(code=code) | |
181 | msg = self.session.msg('execute_request', content) |
|
181 | msg = self.session.msg('execute_request', content) | |
182 | self._queue_request(msg) |
|
182 | self._queue_request(msg) | |
183 | return msg['header']['msg_id'] |
|
183 | return msg['header']['msg_id'] | |
184 |
|
184 | |||
185 | def complete(self, text, line, block=None): |
|
185 | def complete(self, text, line, cursor_pos, block=None): | |
186 | """Tab complete text, line, block in the kernel's namespace. |
|
186 | """Tab complete text, line, block in the kernel's namespace. | |
187 |
|
187 | |||
188 | Parameters |
|
188 | Parameters | |
189 | ---------- |
|
189 | ---------- | |
190 | text : str |
|
190 | text : str | |
191 | The text to complete. |
|
191 | The text to complete. | |
192 | line : str |
|
192 | line : str | |
193 | The full line of text that is the surrounding context for the |
|
193 | The full line of text that is the surrounding context for the | |
194 | text to complete. |
|
194 | text to complete. | |
195 | block : str |
|
195 | block : str | |
196 | The full block of code in which the completion is being requested. |
|
196 | The full block of code in which the completion is being requested. | |
197 |
|
197 | |||
198 | Returns |
|
198 | Returns | |
199 | ------- |
|
199 | ------- | |
200 | The msg_id of the message sent. |
|
200 | The msg_id of the message sent. | |
201 | """ |
|
201 | """ | |
202 | content = dict(text=text, line=line) |
|
202 | content = dict(text=text, line=line, block=block, cursor_pos=cursor_pos) | |
203 | msg = self.session.msg('complete_request', content) |
|
203 | msg = self.session.msg('complete_request', content) | |
204 | self._queue_request(msg) |
|
204 | self._queue_request(msg) | |
205 | return msg['header']['msg_id'] |
|
205 | return msg['header']['msg_id'] | |
206 |
|
206 | |||
207 | def object_info(self, oname): |
|
207 | def object_info(self, oname): | |
208 | """Get metadata information about an object. |
|
208 | """Get metadata information about an object. | |
209 |
|
209 | |||
210 | Parameters |
|
210 | Parameters | |
211 | ---------- |
|
211 | ---------- | |
212 | oname : str |
|
212 | oname : str | |
213 | A string specifying the object name. |
|
213 | A string specifying the object name. | |
214 |
|
214 | |||
215 | Returns |
|
215 | Returns | |
216 | ------- |
|
216 | ------- | |
217 | The msg_id of the message sent. |
|
217 | The msg_id of the message sent. | |
218 | """ |
|
218 | """ | |
219 | content = dict(oname=oname) |
|
219 | content = dict(oname=oname) | |
220 | msg = self.session.msg('object_info_request', content) |
|
220 | msg = self.session.msg('object_info_request', content) | |
221 | self._queue_request(msg) |
|
221 | self._queue_request(msg) | |
222 | return msg['header']['msg_id'] |
|
222 | return msg['header']['msg_id'] | |
223 |
|
223 | |||
224 | def _handle_events(self, socket, events): |
|
224 | def _handle_events(self, socket, events): | |
225 | if events & POLLERR: |
|
225 | if events & POLLERR: | |
226 | self._handle_err() |
|
226 | self._handle_err() | |
227 | if events & POLLOUT: |
|
227 | if events & POLLOUT: | |
228 | self._handle_send() |
|
228 | self._handle_send() | |
229 | if events & POLLIN: |
|
229 | if events & POLLIN: | |
230 | self._handle_recv() |
|
230 | self._handle_recv() | |
231 |
|
231 | |||
232 | def _handle_recv(self): |
|
232 | def _handle_recv(self): | |
233 | msg = self.socket.recv_json() |
|
233 | msg = self.socket.recv_json() | |
234 | self.call_handlers(msg) |
|
234 | self.call_handlers(msg) | |
235 |
|
235 | |||
236 | def _handle_send(self): |
|
236 | def _handle_send(self): | |
237 | try: |
|
237 | try: | |
238 | msg = self.command_queue.get(False) |
|
238 | msg = self.command_queue.get(False) | |
239 | except Empty: |
|
239 | except Empty: | |
240 | pass |
|
240 | pass | |
241 | else: |
|
241 | else: | |
242 | self.socket.send_json(msg) |
|
242 | self.socket.send_json(msg) | |
243 | if self.command_queue.empty(): |
|
243 | if self.command_queue.empty(): | |
244 | self.drop_io_state(POLLOUT) |
|
244 | self.drop_io_state(POLLOUT) | |
245 |
|
245 | |||
246 | def _handle_err(self): |
|
246 | def _handle_err(self): | |
247 | # We don't want to let this go silently, so eventually we should log. |
|
247 | # We don't want to let this go silently, so eventually we should log. | |
248 | raise zmq.ZMQError() |
|
248 | raise zmq.ZMQError() | |
249 |
|
249 | |||
250 | def _queue_request(self, msg): |
|
250 | def _queue_request(self, msg): | |
251 | self.command_queue.put(msg) |
|
251 | self.command_queue.put(msg) | |
252 | self.add_io_state(POLLOUT) |
|
252 | self.add_io_state(POLLOUT) | |
253 |
|
253 | |||
254 |
|
254 | |||
255 | class SubSocketChannel(ZmqSocketChannel): |
|
255 | class SubSocketChannel(ZmqSocketChannel): | |
256 | """The SUB channel which listens for messages that the kernel publishes. |
|
256 | """The SUB channel which listens for messages that the kernel publishes. | |
257 | """ |
|
257 | """ | |
258 |
|
258 | |||
259 | def __init__(self, context, session, address): |
|
259 | def __init__(self, context, session, address): | |
260 | super(SubSocketChannel, self).__init__(context, session, address) |
|
260 | super(SubSocketChannel, self).__init__(context, session, address) | |
261 |
|
261 | |||
262 | def run(self): |
|
262 | def run(self): | |
263 | """The thread's main activity. Call start() instead.""" |
|
263 | """The thread's main activity. Call start() instead.""" | |
264 | self.socket = self.context.socket(zmq.SUB) |
|
264 | self.socket = self.context.socket(zmq.SUB) | |
265 | self.socket.setsockopt(zmq.SUBSCRIBE,'') |
|
265 | self.socket.setsockopt(zmq.SUBSCRIBE,'') | |
266 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) |
|
266 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) | |
267 | self.socket.connect('tcp://%s:%i' % self.address) |
|
267 | self.socket.connect('tcp://%s:%i' % self.address) | |
268 | self.ioloop = ioloop.IOLoop() |
|
268 | self.ioloop = ioloop.IOLoop() | |
269 | self.iostate = POLLIN|POLLERR |
|
269 | self.iostate = POLLIN|POLLERR | |
270 | self.ioloop.add_handler(self.socket, self._handle_events, |
|
270 | self.ioloop.add_handler(self.socket, self._handle_events, | |
271 | self.iostate) |
|
271 | self.iostate) | |
272 | self.ioloop.start() |
|
272 | self.ioloop.start() | |
273 |
|
273 | |||
274 | def stop(self): |
|
274 | def stop(self): | |
275 | self.ioloop.stop() |
|
275 | self.ioloop.stop() | |
276 | super(SubSocketChannel, self).stop() |
|
276 | super(SubSocketChannel, self).stop() | |
277 |
|
277 | |||
278 | def call_handlers(self, msg): |
|
278 | def call_handlers(self, msg): | |
279 | """This method is called in the ioloop thread when a message arrives. |
|
279 | """This method is called in the ioloop thread when a message arrives. | |
280 |
|
280 | |||
281 | Subclasses should override this method to handle incoming messages. |
|
281 | Subclasses should override this method to handle incoming messages. | |
282 | It is important to remember that this method is called in the thread |
|
282 | It is important to remember that this method is called in the thread | |
283 | so that some logic must be done to ensure that the application leve |
|
283 | so that some logic must be done to ensure that the application leve | |
284 | handlers are called in the application thread. |
|
284 | handlers are called in the application thread. | |
285 | """ |
|
285 | """ | |
286 | raise NotImplementedError('call_handlers must be defined in a subclass.') |
|
286 | raise NotImplementedError('call_handlers must be defined in a subclass.') | |
287 |
|
287 | |||
288 | def flush(self, timeout=1.0): |
|
288 | def flush(self, timeout=1.0): | |
289 | """Immediately processes all pending messages on the SUB channel. |
|
289 | """Immediately processes all pending messages on the SUB channel. | |
290 |
|
290 | |||
291 | Callers should use this method to ensure that :method:`call_handlers` |
|
291 | Callers should use this method to ensure that :method:`call_handlers` | |
292 | has been called for all messages that have been received on the |
|
292 | has been called for all messages that have been received on the | |
293 | 0MQ SUB socket of this channel. |
|
293 | 0MQ SUB socket of this channel. | |
294 |
|
294 | |||
295 | This method is thread safe. |
|
295 | This method is thread safe. | |
296 |
|
296 | |||
297 | Parameters |
|
297 | Parameters | |
298 | ---------- |
|
298 | ---------- | |
299 | timeout : float, optional |
|
299 | timeout : float, optional | |
300 | The maximum amount of time to spend flushing, in seconds. The |
|
300 | The maximum amount of time to spend flushing, in seconds. The | |
301 | default is one second. |
|
301 | default is one second. | |
302 | """ |
|
302 | """ | |
303 | # We do the IOLoop callback process twice to ensure that the IOLoop |
|
303 | # We do the IOLoop callback process twice to ensure that the IOLoop | |
304 | # gets to perform at least one full poll. |
|
304 | # gets to perform at least one full poll. | |
305 | stop_time = time.time() + timeout |
|
305 | stop_time = time.time() + timeout | |
306 | for i in xrange(2): |
|
306 | for i in xrange(2): | |
307 | self._flushed = False |
|
307 | self._flushed = False | |
308 | self.ioloop.add_callback(self._flush) |
|
308 | self.ioloop.add_callback(self._flush) | |
309 | while not self._flushed and time.time() < stop_time: |
|
309 | while not self._flushed and time.time() < stop_time: | |
310 | time.sleep(0.01) |
|
310 | time.sleep(0.01) | |
311 |
|
311 | |||
312 | def _handle_events(self, socket, events): |
|
312 | def _handle_events(self, socket, events): | |
313 | # Turn on and off POLLOUT depending on if we have made a request |
|
313 | # Turn on and off POLLOUT depending on if we have made a request | |
314 | if events & POLLERR: |
|
314 | if events & POLLERR: | |
315 | self._handle_err() |
|
315 | self._handle_err() | |
316 | if events & POLLIN: |
|
316 | if events & POLLIN: | |
317 | self._handle_recv() |
|
317 | self._handle_recv() | |
318 |
|
318 | |||
319 | def _handle_err(self): |
|
319 | def _handle_err(self): | |
320 | # We don't want to let this go silently, so eventually we should log. |
|
320 | # We don't want to let this go silently, so eventually we should log. | |
321 | raise zmq.ZMQError() |
|
321 | raise zmq.ZMQError() | |
322 |
|
322 | |||
323 | def _handle_recv(self): |
|
323 | def _handle_recv(self): | |
324 | # Get all of the messages we can |
|
324 | # Get all of the messages we can | |
325 | while True: |
|
325 | while True: | |
326 | try: |
|
326 | try: | |
327 | msg = self.socket.recv_json(zmq.NOBLOCK) |
|
327 | msg = self.socket.recv_json(zmq.NOBLOCK) | |
328 | except zmq.ZMQError: |
|
328 | except zmq.ZMQError: | |
329 | # Check the errno? |
|
329 | # Check the errno? | |
330 | # Will this trigger POLLERR? |
|
330 | # Will this trigger POLLERR? | |
331 | break |
|
331 | break | |
332 | else: |
|
332 | else: | |
333 | self.call_handlers(msg) |
|
333 | self.call_handlers(msg) | |
334 |
|
334 | |||
335 | def _flush(self): |
|
335 | def _flush(self): | |
336 | """Callback for :method:`self.flush`.""" |
|
336 | """Callback for :method:`self.flush`.""" | |
337 | self._flushed = True |
|
337 | self._flushed = True | |
338 |
|
338 | |||
339 |
|
339 | |||
340 | class RepSocketChannel(ZmqSocketChannel): |
|
340 | class RepSocketChannel(ZmqSocketChannel): | |
341 | """A reply channel to handle raw_input requests that the kernel makes.""" |
|
341 | """A reply channel to handle raw_input requests that the kernel makes.""" | |
342 |
|
342 | |||
343 | msg_queue = None |
|
343 | msg_queue = None | |
344 |
|
344 | |||
345 | def __init__(self, context, session, address): |
|
345 | def __init__(self, context, session, address): | |
346 | self.msg_queue = Queue() |
|
346 | self.msg_queue = Queue() | |
347 | super(RepSocketChannel, self).__init__(context, session, address) |
|
347 | super(RepSocketChannel, self).__init__(context, session, address) | |
348 |
|
348 | |||
349 | def run(self): |
|
349 | def run(self): | |
350 | """The thread's main activity. Call start() instead.""" |
|
350 | """The thread's main activity. Call start() instead.""" | |
351 | self.socket = self.context.socket(zmq.XREQ) |
|
351 | self.socket = self.context.socket(zmq.XREQ) | |
352 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) |
|
352 | self.socket.setsockopt(zmq.IDENTITY, self.session.session) | |
353 | self.socket.connect('tcp://%s:%i' % self.address) |
|
353 | self.socket.connect('tcp://%s:%i' % self.address) | |
354 | self.ioloop = ioloop.IOLoop() |
|
354 | self.ioloop = ioloop.IOLoop() | |
355 | self.iostate = POLLERR|POLLIN |
|
355 | self.iostate = POLLERR|POLLIN | |
356 | self.ioloop.add_handler(self.socket, self._handle_events, |
|
356 | self.ioloop.add_handler(self.socket, self._handle_events, | |
357 | self.iostate) |
|
357 | self.iostate) | |
358 | self.ioloop.start() |
|
358 | self.ioloop.start() | |
359 |
|
359 | |||
360 | def stop(self): |
|
360 | def stop(self): | |
361 | self.ioloop.stop() |
|
361 | self.ioloop.stop() | |
362 | super(RepSocketChannel, self).stop() |
|
362 | super(RepSocketChannel, self).stop() | |
363 |
|
363 | |||
364 | def call_handlers(self, msg): |
|
364 | def call_handlers(self, msg): | |
365 | """This method is called in the ioloop thread when a message arrives. |
|
365 | """This method is called in the ioloop thread when a message arrives. | |
366 |
|
366 | |||
367 | Subclasses should override this method to handle incoming messages. |
|
367 | Subclasses should override this method to handle incoming messages. | |
368 | It is important to remember that this method is called in the thread |
|
368 | It is important to remember that this method is called in the thread | |
369 | so that some logic must be done to ensure that the application leve |
|
369 | so that some logic must be done to ensure that the application leve | |
370 | handlers are called in the application thread. |
|
370 | handlers are called in the application thread. | |
371 | """ |
|
371 | """ | |
372 | raise NotImplementedError('call_handlers must be defined in a subclass.') |
|
372 | raise NotImplementedError('call_handlers must be defined in a subclass.') | |
373 |
|
373 | |||
374 | def input(self, string): |
|
374 | def input(self, string): | |
375 | """Send a string of raw input to the kernel.""" |
|
375 | """Send a string of raw input to the kernel.""" | |
376 | content = dict(value=string) |
|
376 | content = dict(value=string) | |
377 | msg = self.session.msg('input_reply', content) |
|
377 | msg = self.session.msg('input_reply', content) | |
378 | self._queue_reply(msg) |
|
378 | self._queue_reply(msg) | |
379 |
|
379 | |||
380 | def _handle_events(self, socket, events): |
|
380 | def _handle_events(self, socket, events): | |
381 | if events & POLLERR: |
|
381 | if events & POLLERR: | |
382 | self._handle_err() |
|
382 | self._handle_err() | |
383 | if events & POLLOUT: |
|
383 | if events & POLLOUT: | |
384 | self._handle_send() |
|
384 | self._handle_send() | |
385 | if events & POLLIN: |
|
385 | if events & POLLIN: | |
386 | self._handle_recv() |
|
386 | self._handle_recv() | |
387 |
|
387 | |||
388 | def _handle_recv(self): |
|
388 | def _handle_recv(self): | |
389 | msg = self.socket.recv_json() |
|
389 | msg = self.socket.recv_json() | |
390 | self.call_handlers(msg) |
|
390 | self.call_handlers(msg) | |
391 |
|
391 | |||
392 | def _handle_send(self): |
|
392 | def _handle_send(self): | |
393 | try: |
|
393 | try: | |
394 | msg = self.msg_queue.get(False) |
|
394 | msg = self.msg_queue.get(False) | |
395 | except Empty: |
|
395 | except Empty: | |
396 | pass |
|
396 | pass | |
397 | else: |
|
397 | else: | |
398 | self.socket.send_json(msg) |
|
398 | self.socket.send_json(msg) | |
399 | if self.msg_queue.empty(): |
|
399 | if self.msg_queue.empty(): | |
400 | self.drop_io_state(POLLOUT) |
|
400 | self.drop_io_state(POLLOUT) | |
401 |
|
401 | |||
402 | def _handle_err(self): |
|
402 | def _handle_err(self): | |
403 | # We don't want to let this go silently, so eventually we should log. |
|
403 | # We don't want to let this go silently, so eventually we should log. | |
404 | raise zmq.ZMQError() |
|
404 | raise zmq.ZMQError() | |
405 |
|
405 | |||
406 | def _queue_reply(self, msg): |
|
406 | def _queue_reply(self, msg): | |
407 | self.msg_queue.put(msg) |
|
407 | self.msg_queue.put(msg) | |
408 | self.add_io_state(POLLOUT) |
|
408 | self.add_io_state(POLLOUT) | |
409 |
|
409 | |||
410 |
|
410 | |||
411 | #----------------------------------------------------------------------------- |
|
411 | #----------------------------------------------------------------------------- | |
412 | # Main kernel manager class |
|
412 | # Main kernel manager class | |
413 | #----------------------------------------------------------------------------- |
|
413 | #----------------------------------------------------------------------------- | |
414 |
|
414 | |||
415 | class KernelManager(HasTraits): |
|
415 | class KernelManager(HasTraits): | |
416 | """ Manages a kernel for a frontend. |
|
416 | """ Manages a kernel for a frontend. | |
417 |
|
417 | |||
418 | The SUB channel is for the frontend to receive messages published by the |
|
418 | The SUB channel is for the frontend to receive messages published by the | |
419 | kernel. |
|
419 | kernel. | |
420 |
|
420 | |||
421 | The REQ channel is for the frontend to make requests of the kernel. |
|
421 | The REQ channel is for the frontend to make requests of the kernel. | |
422 |
|
422 | |||
423 | The REP channel is for the kernel to request stdin (raw_input) from the |
|
423 | The REP channel is for the kernel to request stdin (raw_input) from the | |
424 | frontend. |
|
424 | frontend. | |
425 | """ |
|
425 | """ | |
426 | # The PyZMQ Context to use for communication with the kernel. |
|
426 | # The PyZMQ Context to use for communication with the kernel. | |
427 | context = Instance(zmq.Context,(),{}) |
|
427 | context = Instance(zmq.Context,(),{}) | |
428 |
|
428 | |||
429 | # The Session to use for communication with the kernel. |
|
429 | # The Session to use for communication with the kernel. | |
430 | session = Instance(Session,(),{}) |
|
430 | session = Instance(Session,(),{}) | |
431 |
|
431 | |||
432 | # The kernel process with which the KernelManager is communicating. |
|
432 | # The kernel process with which the KernelManager is communicating. | |
433 | kernel = Instance(Popen) |
|
433 | kernel = Instance(Popen) | |
434 |
|
434 | |||
435 | # The addresses for the communication channels. |
|
435 | # The addresses for the communication channels. | |
436 | xreq_address = TCPAddress((LOCALHOST, 0)) |
|
436 | xreq_address = TCPAddress((LOCALHOST, 0)) | |
437 | sub_address = TCPAddress((LOCALHOST, 0)) |
|
437 | sub_address = TCPAddress((LOCALHOST, 0)) | |
438 | rep_address = TCPAddress((LOCALHOST, 0)) |
|
438 | rep_address = TCPAddress((LOCALHOST, 0)) | |
439 |
|
439 | |||
440 | # The classes to use for the various channels. |
|
440 | # The classes to use for the various channels. | |
441 | xreq_channel_class = Type(XReqSocketChannel) |
|
441 | xreq_channel_class = Type(XReqSocketChannel) | |
442 | sub_channel_class = Type(SubSocketChannel) |
|
442 | sub_channel_class = Type(SubSocketChannel) | |
443 | rep_channel_class = Type(RepSocketChannel) |
|
443 | rep_channel_class = Type(RepSocketChannel) | |
444 |
|
444 | |||
445 | # Protected traits. |
|
445 | # Protected traits. | |
446 | _xreq_channel = Any |
|
446 | _xreq_channel = Any | |
447 | _sub_channel = Any |
|
447 | _sub_channel = Any | |
448 | _rep_channel = Any |
|
448 | _rep_channel = Any | |
449 |
|
449 | |||
450 | #-------------------------------------------------------------------------- |
|
450 | #-------------------------------------------------------------------------- | |
451 | # Channel management methods: |
|
451 | # Channel management methods: | |
452 | #-------------------------------------------------------------------------- |
|
452 | #-------------------------------------------------------------------------- | |
453 |
|
453 | |||
454 | def start_channels(self): |
|
454 | def start_channels(self): | |
455 | """Starts the channels for this kernel. |
|
455 | """Starts the channels for this kernel. | |
456 |
|
456 | |||
457 | This will create the channels if they do not exist and then start |
|
457 | This will create the channels if they do not exist and then start | |
458 | them. If port numbers of 0 are being used (random ports) then you |
|
458 | them. If port numbers of 0 are being used (random ports) then you | |
459 | must first call :method:`start_kernel`. If the channels have been |
|
459 | must first call :method:`start_kernel`. If the channels have been | |
460 | stopped and you call this, :class:`RuntimeError` will be raised. |
|
460 | stopped and you call this, :class:`RuntimeError` will be raised. | |
461 | """ |
|
461 | """ | |
462 | self.xreq_channel.start() |
|
462 | self.xreq_channel.start() | |
463 | self.sub_channel.start() |
|
463 | self.sub_channel.start() | |
464 | self.rep_channel.start() |
|
464 | self.rep_channel.start() | |
465 |
|
465 | |||
466 | def stop_channels(self): |
|
466 | def stop_channels(self): | |
467 | """Stops the channels for this kernel. |
|
467 | """Stops the channels for this kernel. | |
468 |
|
468 | |||
469 | This stops the channels by joining their threads. If the channels |
|
469 | This stops the channels by joining their threads. If the channels | |
470 | were not started, :class:`RuntimeError` will be raised. |
|
470 | were not started, :class:`RuntimeError` will be raised. | |
471 | """ |
|
471 | """ | |
472 | self.xreq_channel.stop() |
|
472 | self.xreq_channel.stop() | |
473 | self.sub_channel.stop() |
|
473 | self.sub_channel.stop() | |
474 | self.rep_channel.stop() |
|
474 | self.rep_channel.stop() | |
475 |
|
475 | |||
476 | @property |
|
476 | @property | |
477 | def channels_running(self): |
|
477 | def channels_running(self): | |
478 | """Are all of the channels created and running?""" |
|
478 | """Are all of the channels created and running?""" | |
479 | return self.xreq_channel.is_alive() \ |
|
479 | return self.xreq_channel.is_alive() \ | |
480 | and self.sub_channel.is_alive() \ |
|
480 | and self.sub_channel.is_alive() \ | |
481 | and self.rep_channel.is_alive() |
|
481 | and self.rep_channel.is_alive() | |
482 |
|
482 | |||
483 | #-------------------------------------------------------------------------- |
|
483 | #-------------------------------------------------------------------------- | |
484 | # Kernel process management methods: |
|
484 | # Kernel process management methods: | |
485 | #-------------------------------------------------------------------------- |
|
485 | #-------------------------------------------------------------------------- | |
486 |
|
486 | |||
487 | def start_kernel(self, ipython=True, **kw): |
|
487 | def start_kernel(self, ipython=True, **kw): | |
488 | """Starts a kernel process and configures the manager to use it. |
|
488 | """Starts a kernel process and configures the manager to use it. | |
489 |
|
489 | |||
490 | If random ports (port=0) are being used, this method must be called |
|
490 | If random ports (port=0) are being used, this method must be called | |
491 | before the channels are created. |
|
491 | before the channels are created. | |
492 |
|
492 | |||
493 | Parameters: |
|
493 | Parameters: | |
494 | ----------- |
|
494 | ----------- | |
495 | ipython : bool, optional (default True) |
|
495 | ipython : bool, optional (default True) | |
496 | Whether to use an IPython kernel instead of a plain Python kernel. |
|
496 | Whether to use an IPython kernel instead of a plain Python kernel. | |
497 | """ |
|
497 | """ | |
498 | xreq, sub, rep = self.xreq_address, self.sub_address, self.rep_address |
|
498 | xreq, sub, rep = self.xreq_address, self.sub_address, self.rep_address | |
499 | if xreq[0] != LOCALHOST or sub[0] != LOCALHOST or rep[0] != LOCALHOST: |
|
499 | if xreq[0] != LOCALHOST or sub[0] != LOCALHOST or rep[0] != LOCALHOST: | |
500 | raise RuntimeError("Can only launch a kernel on localhost." |
|
500 | raise RuntimeError("Can only launch a kernel on localhost." | |
501 | "Make sure that the '*_address' attributes are " |
|
501 | "Make sure that the '*_address' attributes are " | |
502 | "configured properly.") |
|
502 | "configured properly.") | |
503 |
|
503 | |||
504 | if ipython: |
|
504 | if ipython: | |
505 | from ipkernel import launch_kernel as launch |
|
505 | from ipkernel import launch_kernel as launch | |
506 | else: |
|
506 | else: | |
507 | from pykernel import launch_kernel as launch |
|
507 | from pykernel import launch_kernel as launch | |
508 | self.kernel, xrep, pub, req = launch(xrep_port=xreq[1], pub_port=sub[1], |
|
508 | self.kernel, xrep, pub, req = launch(xrep_port=xreq[1], pub_port=sub[1], | |
509 | req_port=rep[1], **kw) |
|
509 | req_port=rep[1], **kw) | |
510 | self.xreq_address = (LOCALHOST, xrep) |
|
510 | self.xreq_address = (LOCALHOST, xrep) | |
511 | self.sub_address = (LOCALHOST, pub) |
|
511 | self.sub_address = (LOCALHOST, pub) | |
512 | self.rep_address = (LOCALHOST, req) |
|
512 | self.rep_address = (LOCALHOST, req) | |
513 |
|
513 | |||
514 | @property |
|
514 | @property | |
515 | def has_kernel(self): |
|
515 | def has_kernel(self): | |
516 | """Returns whether a kernel process has been specified for the kernel |
|
516 | """Returns whether a kernel process has been specified for the kernel | |
517 | manager. |
|
517 | manager. | |
518 | """ |
|
518 | """ | |
519 | return self.kernel is not None |
|
519 | return self.kernel is not None | |
520 |
|
520 | |||
521 | def kill_kernel(self): |
|
521 | def kill_kernel(self): | |
522 | """ Kill the running kernel. """ |
|
522 | """ Kill the running kernel. """ | |
523 | if self.kernel is not None: |
|
523 | if self.kernel is not None: | |
524 | self.kernel.kill() |
|
524 | self.kernel.kill() | |
525 | self.kernel = None |
|
525 | self.kernel = None | |
526 | else: |
|
526 | else: | |
527 | raise RuntimeError("Cannot kill kernel. No kernel is running!") |
|
527 | raise RuntimeError("Cannot kill kernel. No kernel is running!") | |
528 |
|
528 | |||
529 | def signal_kernel(self, signum): |
|
529 | def signal_kernel(self, signum): | |
530 | """ Sends a signal to the kernel. """ |
|
530 | """ Sends a signal to the kernel. """ | |
531 | if self.kernel is not None: |
|
531 | if self.kernel is not None: | |
532 | self.kernel.send_signal(signum) |
|
532 | self.kernel.send_signal(signum) | |
533 | else: |
|
533 | else: | |
534 | raise RuntimeError("Cannot signal kernel. No kernel is running!") |
|
534 | raise RuntimeError("Cannot signal kernel. No kernel is running!") | |
535 |
|
535 | |||
536 | @property |
|
536 | @property | |
537 | def is_alive(self): |
|
537 | def is_alive(self): | |
538 | """Is the kernel process still running?""" |
|
538 | """Is the kernel process still running?""" | |
539 | if self.kernel is not None: |
|
539 | if self.kernel is not None: | |
540 | if self.kernel.poll() is None: |
|
540 | if self.kernel.poll() is None: | |
541 | return True |
|
541 | return True | |
542 | else: |
|
542 | else: | |
543 | return False |
|
543 | return False | |
544 | else: |
|
544 | else: | |
545 | # We didn't start the kernel with this KernelManager so we don't |
|
545 | # We didn't start the kernel with this KernelManager so we don't | |
546 | # know if it is running. We should use a heartbeat for this case. |
|
546 | # know if it is running. We should use a heartbeat for this case. | |
547 | return True |
|
547 | return True | |
548 |
|
548 | |||
549 | #-------------------------------------------------------------------------- |
|
549 | #-------------------------------------------------------------------------- | |
550 | # Channels used for communication with the kernel: |
|
550 | # Channels used for communication with the kernel: | |
551 | #-------------------------------------------------------------------------- |
|
551 | #-------------------------------------------------------------------------- | |
552 |
|
552 | |||
553 | @property |
|
553 | @property | |
554 | def xreq_channel(self): |
|
554 | def xreq_channel(self): | |
555 | """Get the REQ socket channel object to make requests of the kernel.""" |
|
555 | """Get the REQ socket channel object to make requests of the kernel.""" | |
556 | if self._xreq_channel is None: |
|
556 | if self._xreq_channel is None: | |
557 | self._xreq_channel = self.xreq_channel_class(self.context, |
|
557 | self._xreq_channel = self.xreq_channel_class(self.context, | |
558 | self.session, |
|
558 | self.session, | |
559 | self.xreq_address) |
|
559 | self.xreq_address) | |
560 | return self._xreq_channel |
|
560 | return self._xreq_channel | |
561 |
|
561 | |||
562 | @property |
|
562 | @property | |
563 | def sub_channel(self): |
|
563 | def sub_channel(self): | |
564 | """Get the SUB socket channel object.""" |
|
564 | """Get the SUB socket channel object.""" | |
565 | if self._sub_channel is None: |
|
565 | if self._sub_channel is None: | |
566 | self._sub_channel = self.sub_channel_class(self.context, |
|
566 | self._sub_channel = self.sub_channel_class(self.context, | |
567 | self.session, |
|
567 | self.session, | |
568 | self.sub_address) |
|
568 | self.sub_address) | |
569 | return self._sub_channel |
|
569 | return self._sub_channel | |
570 |
|
570 | |||
571 | @property |
|
571 | @property | |
572 | def rep_channel(self): |
|
572 | def rep_channel(self): | |
573 | """Get the REP socket channel object to handle stdin (raw_input).""" |
|
573 | """Get the REP socket channel object to handle stdin (raw_input).""" | |
574 | if self._rep_channel is None: |
|
574 | if self._rep_channel is None: | |
575 | self._rep_channel = self.rep_channel_class(self.context, |
|
575 | self._rep_channel = self.rep_channel_class(self.context, | |
576 | self.session, |
|
576 | self.session, | |
577 | self.rep_address) |
|
577 | self.rep_address) | |
578 | return self._rep_channel |
|
578 | return self._rep_channel |
General Comments 0
You need to be logged in to leave comments.
Login now