##// END OF EJS Templates
Improvements to docs formatting.
Thomas Kluyver -
Show More
@@ -1,979 +1,979 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 IPython Development Team
56 # Copyright (C) 2008 IPython Development Team
57 # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu>
57 # Copyright (C) 2001 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 sys
77 import sys
78
78
79 from IPython.config.configurable import Configurable
79 from IPython.config.configurable import Configurable
80 from IPython.core.error import TryNext
80 from IPython.core.error import TryNext
81 from IPython.core.inputsplitter import ESC_MAGIC
81 from IPython.core.inputsplitter import ESC_MAGIC
82 from IPython.utils import generics
82 from IPython.utils import generics
83 from IPython.utils import io
83 from IPython.utils import io
84 from IPython.utils.dir2 import dir2
84 from IPython.utils.dir2 import dir2
85 from IPython.utils.process import arg_split
85 from IPython.utils.process import arg_split
86 from IPython.utils.traitlets import CBool, Enum
86 from IPython.utils.traitlets import CBool, Enum
87
87
88 #-----------------------------------------------------------------------------
88 #-----------------------------------------------------------------------------
89 # Globals
89 # Globals
90 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
91
91
92 # Public API
92 # Public API
93 __all__ = ['Completer','IPCompleter']
93 __all__ = ['Completer','IPCompleter']
94
94
95 if sys.platform == 'win32':
95 if sys.platform == 'win32':
96 PROTECTABLES = ' '
96 PROTECTABLES = ' '
97 else:
97 else:
98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
99
99
100 #-----------------------------------------------------------------------------
100 #-----------------------------------------------------------------------------
101 # Main functions and classes
101 # Main functions and classes
102 #-----------------------------------------------------------------------------
102 #-----------------------------------------------------------------------------
103
103
104 def has_open_quotes(s):
104 def has_open_quotes(s):
105 """Return whether a string has open quotes.
105 """Return whether a string has open quotes.
106
106
107 This simply counts whether the number of quote characters of either type in
107 This simply counts whether the number of quote characters of either type in
108 the string is odd.
108 the string is odd.
109
109
110 Returns
110 Returns
111 -------
111 -------
112 If there is an open quote, the quote character is returned. Else, return
112 If there is an open quote, the quote character is returned. Else, return
113 False.
113 False.
114 """
114 """
115 # We check " first, then ', so complex cases with nested quotes will get
115 # We check " first, then ', so complex cases with nested quotes will get
116 # the " to take precedence.
116 # the " to take precedence.
117 if s.count('"') % 2:
117 if s.count('"') % 2:
118 return '"'
118 return '"'
119 elif s.count("'") % 2:
119 elif s.count("'") % 2:
120 return "'"
120 return "'"
121 else:
121 else:
122 return False
122 return False
123
123
124
124
125 def protect_filename(s):
125 def protect_filename(s):
126 """Escape a string to protect certain characters."""
126 """Escape a string to protect certain characters."""
127
127
128 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
128 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
129 for ch in s])
129 for ch in s])
130
130
131 def expand_user(path):
131 def expand_user(path):
132 """Expand '~'-style usernames in strings.
132 """Expand '~'-style usernames in strings.
133
133
134 This is similar to :func:`os.path.expanduser`, but it computes and returns
134 This is similar to :func:`os.path.expanduser`, but it computes and returns
135 extra information that will be useful if the input was being used in
135 extra information that will be useful if the input was being used in
136 computing completions, and you wish to return the completions with the
136 computing completions, and you wish to return the completions with the
137 original '~' instead of its expanded value.
137 original '~' instead of its expanded value.
138
138
139 Parameters
139 Parameters
140 ----------
140 ----------
141 path : str
141 path : str
142 String to be expanded. If no ~ is present, the output is the same as the
142 String to be expanded. If no ~ is present, the output is the same as the
143 input.
143 input.
144
144
145 Returns
145 Returns
146 -------
146 -------
147 newpath : str
147 newpath : str
148 Result of ~ expansion in the input path.
148 Result of ~ expansion in the input path.
149 tilde_expand : bool
149 tilde_expand : bool
150 Whether any expansion was performed or not.
150 Whether any expansion was performed or not.
151 tilde_val : str
151 tilde_val : str
152 The value that ~ was replaced with.
152 The value that ~ was replaced with.
153 """
153 """
154 # Default values
154 # Default values
155 tilde_expand = False
155 tilde_expand = False
156 tilde_val = ''
156 tilde_val = ''
157 newpath = path
157 newpath = path
158
158
159 if path.startswith('~'):
159 if path.startswith('~'):
160 tilde_expand = True
160 tilde_expand = True
161 rest = len(path)-1
161 rest = len(path)-1
162 newpath = os.path.expanduser(path)
162 newpath = os.path.expanduser(path)
163 if rest:
163 if rest:
164 tilde_val = newpath[:-rest]
164 tilde_val = newpath[:-rest]
165 else:
165 else:
166 tilde_val = newpath
166 tilde_val = newpath
167
167
168 return newpath, tilde_expand, tilde_val
168 return newpath, tilde_expand, tilde_val
169
169
170
170
171 def compress_user(path, tilde_expand, tilde_val):
171 def compress_user(path, tilde_expand, tilde_val):
172 """Does the opposite of expand_user, with its outputs.
172 """Does the opposite of expand_user, with its outputs.
173 """
173 """
174 if tilde_expand:
174 if tilde_expand:
175 return path.replace(tilde_val, '~')
175 return path.replace(tilde_val, '~')
176 else:
176 else:
177 return path
177 return path
178
178
179
179
180 class Bunch(object): pass
180 class Bunch(object): pass
181
181
182
182
183 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
183 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
184 GREEDY_DELIMS = ' =\r\n'
184 GREEDY_DELIMS = ' =\r\n'
185
185
186
186
187 class CompletionSplitter(object):
187 class CompletionSplitter(object):
188 """An object to split an input line in a manner similar to readline.
188 """An object to split an input line in a manner similar to readline.
189
189
190 By having our own implementation, we can expose readline-like completion in
190 By having our own implementation, we can expose readline-like completion in
191 a uniform manner to all frontends. This object only needs to be given the
191 a uniform manner to all frontends. This object only needs to be given the
192 line of text to be split and the cursor position on said line, and it
192 line of text to be split and the cursor position on said line, and it
193 returns the 'word' to be completed on at the cursor after splitting the
193 returns the 'word' to be completed on at the cursor after splitting the
194 entire line.
194 entire line.
195
195
196 What characters are used as splitting delimiters can be controlled by
196 What characters are used as splitting delimiters can be controlled by
197 setting the `delims` attribute (this is a property that internally
197 setting the `delims` attribute (this is a property that internally
198 automatically builds the necessary regular expression)"""
198 automatically builds the necessary regular expression)"""
199
199
200 # Private interface
200 # Private interface
201
201
202 # A string of delimiter characters. The default value makes sense for
202 # A string of delimiter characters. The default value makes sense for
203 # IPython's most typical usage patterns.
203 # IPython's most typical usage patterns.
204 _delims = DELIMS
204 _delims = DELIMS
205
205
206 # The expression (a normal string) to be compiled into a regular expression
206 # The expression (a normal string) to be compiled into a regular expression
207 # for actual splitting. We store it as an attribute mostly for ease of
207 # for actual splitting. We store it as an attribute mostly for ease of
208 # debugging, since this type of code can be so tricky to debug.
208 # debugging, since this type of code can be so tricky to debug.
209 _delim_expr = None
209 _delim_expr = None
210
210
211 # The regular expression that does the actual splitting
211 # The regular expression that does the actual splitting
212 _delim_re = None
212 _delim_re = None
213
213
214 def __init__(self, delims=None):
214 def __init__(self, delims=None):
215 delims = CompletionSplitter._delims if delims is None else delims
215 delims = CompletionSplitter._delims if delims is None else delims
216 self.delims = delims
216 self.delims = delims
217
217
218 @property
218 @property
219 def delims(self):
219 def delims(self):
220 """Return the string of delimiter characters."""
220 """Return the string of delimiter characters."""
221 return self._delims
221 return self._delims
222
222
223 @delims.setter
223 @delims.setter
224 def delims(self, delims):
224 def delims(self, delims):
225 """Set the delimiters for line splitting."""
225 """Set the delimiters for line splitting."""
226 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
226 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
227 self._delim_re = re.compile(expr)
227 self._delim_re = re.compile(expr)
228 self._delims = delims
228 self._delims = delims
229 self._delim_expr = expr
229 self._delim_expr = expr
230
230
231 def split_line(self, line, cursor_pos=None):
231 def split_line(self, line, cursor_pos=None):
232 """Split a line of text with a cursor at the given position.
232 """Split a line of text with a cursor at the given position.
233 """
233 """
234 l = line if cursor_pos is None else line[:cursor_pos]
234 l = line if cursor_pos is None else line[:cursor_pos]
235 return self._delim_re.split(l)[-1]
235 return self._delim_re.split(l)[-1]
236
236
237
237
238 class Completer(Configurable):
238 class Completer(Configurable):
239
239
240 greedy = CBool(False, config=True,
240 greedy = CBool(False, config=True,
241 help="""Activate greedy completion
241 help="""Activate greedy completion
242
242
243 This will enable completion on elements of lists, results of function calls, etc.,
243 This will enable completion on elements of lists, results of function calls, etc.,
244 but can be unsafe because the code is actually evaluated on TAB.
244 but can be unsafe because the code is actually evaluated on TAB.
245 """
245 """
246 )
246 )
247
247
248
248
249 def __init__(self, namespace=None, global_namespace=None, **kwargs):
249 def __init__(self, namespace=None, global_namespace=None, **kwargs):
250 """Create a new completer for the command line.
250 """Create a new completer for the command line.
251
251
252 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
252 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
253
253
254 If unspecified, the default namespace where completions are performed
254 If unspecified, the default namespace where completions are performed
255 is __main__ (technically, __main__.__dict__). Namespaces should be
255 is __main__ (technically, __main__.__dict__). Namespaces should be
256 given as dictionaries.
256 given as dictionaries.
257
257
258 An optional second namespace can be given. This allows the completer
258 An optional second namespace can be given. This allows the completer
259 to handle cases where both the local and global scopes need to be
259 to handle cases where both the local and global scopes need to be
260 distinguished.
260 distinguished.
261
261
262 Completer instances should be used as the completion mechanism of
262 Completer instances should be used as the completion mechanism of
263 readline via the set_completer() call:
263 readline via the set_completer() call:
264
264
265 readline.set_completer(Completer(my_namespace).complete)
265 readline.set_completer(Completer(my_namespace).complete)
266 """
266 """
267
267
268 # Don't bind to namespace quite yet, but flag whether the user wants a
268 # Don't bind to namespace quite yet, but flag whether the user wants a
269 # specific namespace or to use __main__.__dict__. This will allow us
269 # specific namespace or to use __main__.__dict__. This will allow us
270 # to bind to __main__.__dict__ at completion time, not now.
270 # to bind to __main__.__dict__ at completion time, not now.
271 if namespace is None:
271 if namespace is None:
272 self.use_main_ns = 1
272 self.use_main_ns = 1
273 else:
273 else:
274 self.use_main_ns = 0
274 self.use_main_ns = 0
275 self.namespace = namespace
275 self.namespace = namespace
276
276
277 # The global namespace, if given, can be bound directly
277 # The global namespace, if given, can be bound directly
278 if global_namespace is None:
278 if global_namespace is None:
279 self.global_namespace = {}
279 self.global_namespace = {}
280 else:
280 else:
281 self.global_namespace = global_namespace
281 self.global_namespace = global_namespace
282
282
283 super(Completer, self).__init__(**kwargs)
283 super(Completer, self).__init__(**kwargs)
284
284
285 def complete(self, text, state):
285 def complete(self, text, state):
286 """Return the next possible completion for 'text'.
286 """Return the next possible completion for 'text'.
287
287
288 This is called successively with state == 0, 1, 2, ... until it
288 This is called successively with state == 0, 1, 2, ... until it
289 returns None. The completion should begin with 'text'.
289 returns None. The completion should begin with 'text'.
290
290
291 """
291 """
292 if self.use_main_ns:
292 if self.use_main_ns:
293 self.namespace = __main__.__dict__
293 self.namespace = __main__.__dict__
294
294
295 if state == 0:
295 if state == 0:
296 if "." in text:
296 if "." in text:
297 self.matches = self.attr_matches(text)
297 self.matches = self.attr_matches(text)
298 else:
298 else:
299 self.matches = self.global_matches(text)
299 self.matches = self.global_matches(text)
300 try:
300 try:
301 return self.matches[state]
301 return self.matches[state]
302 except IndexError:
302 except IndexError:
303 return None
303 return None
304
304
305 def global_matches(self, text):
305 def global_matches(self, text):
306 """Compute matches when text is a simple name.
306 """Compute matches when text is a simple name.
307
307
308 Return a list of all keywords, built-in functions and names currently
308 Return a list of all keywords, built-in functions and names currently
309 defined in self.namespace or self.global_namespace that match.
309 defined in self.namespace or self.global_namespace that match.
310
310
311 """
311 """
312 #print 'Completer->global_matches, txt=%r' % text # dbg
312 #print 'Completer->global_matches, txt=%r' % text # dbg
313 matches = []
313 matches = []
314 match_append = matches.append
314 match_append = matches.append
315 n = len(text)
315 n = len(text)
316 for lst in [keyword.kwlist,
316 for lst in [keyword.kwlist,
317 __builtin__.__dict__.keys(),
317 __builtin__.__dict__.keys(),
318 self.namespace.keys(),
318 self.namespace.keys(),
319 self.global_namespace.keys()]:
319 self.global_namespace.keys()]:
320 for word in lst:
320 for word in lst:
321 if word[:n] == text and word != "__builtins__":
321 if word[:n] == text and word != "__builtins__":
322 match_append(word)
322 match_append(word)
323 return matches
323 return matches
324
324
325 def attr_matches(self, text):
325 def attr_matches(self, text):
326 """Compute matches when text contains a dot.
326 """Compute matches when text contains a dot.
327
327
328 Assuming the text is of the form NAME.NAME....[NAME], and is
328 Assuming the text is of the form NAME.NAME....[NAME], and is
329 evaluatable in self.namespace or self.global_namespace, it will be
329 evaluatable in self.namespace or self.global_namespace, it will be
330 evaluated and its attributes (as revealed by dir()) are used as
330 evaluated and its attributes (as revealed by dir()) are used as
331 possible completions. (For class instances, class members are are
331 possible completions. (For class instances, class members are are
332 also considered.)
332 also considered.)
333
333
334 WARNING: this can still invoke arbitrary C code, if an object
334 WARNING: this can still invoke arbitrary C code, if an object
335 with a __getattr__ hook is evaluated.
335 with a __getattr__ hook is evaluated.
336
336
337 """
337 """
338
338
339 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
339 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
340 # Another option, seems to work great. Catches things like ''.<tab>
340 # Another option, seems to work great. Catches things like ''.<tab>
341 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
341 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
342
342
343 if m:
343 if m:
344 expr, attr = m.group(1, 3)
344 expr, attr = m.group(1, 3)
345 elif self.greedy:
345 elif self.greedy:
346 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
346 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
347 if not m2:
347 if not m2:
348 return []
348 return []
349 expr, attr = m2.group(1,2)
349 expr, attr = m2.group(1,2)
350 else:
350 else:
351 return []
351 return []
352
352
353 try:
353 try:
354 obj = eval(expr, self.namespace)
354 obj = eval(expr, self.namespace)
355 except:
355 except:
356 try:
356 try:
357 obj = eval(expr, self.global_namespace)
357 obj = eval(expr, self.global_namespace)
358 except:
358 except:
359 return []
359 return []
360
360
361 if self.limit_to__all__ and hasattr(obj, '__all__'):
361 if self.limit_to__all__ and hasattr(obj, '__all__'):
362 words = get__all__entries(obj)
362 words = get__all__entries(obj)
363 else:
363 else:
364 words = dir2(obj)
364 words = dir2(obj)
365
365
366 try:
366 try:
367 words = generics.complete_object(obj, words)
367 words = generics.complete_object(obj, words)
368 except TryNext:
368 except TryNext:
369 pass
369 pass
370 except Exception:
370 except Exception:
371 # Silence errors from completion function
371 # Silence errors from completion function
372 #raise # dbg
372 #raise # dbg
373 pass
373 pass
374 # Build match list to return
374 # Build match list to return
375 n = len(attr)
375 n = len(attr)
376 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
376 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
377 return res
377 return res
378
378
379
379
380 def get__all__entries(obj):
380 def get__all__entries(obj):
381 """returns the strings in the __all__ attribute"""
381 """returns the strings in the __all__ attribute"""
382 try:
382 try:
383 words = getattr(obj, '__all__')
383 words = getattr(obj, '__all__')
384 except:
384 except:
385 return []
385 return []
386
386
387 return [w for w in words if isinstance(w, basestring)]
387 return [w for w in words if isinstance(w, basestring)]
388
388
389
389
390 class IPCompleter(Completer):
390 class IPCompleter(Completer):
391 """Extension of the completer class with IPython-specific features"""
391 """Extension of the completer class with IPython-specific features"""
392
392
393 def _greedy_changed(self, name, old, new):
393 def _greedy_changed(self, name, old, new):
394 """update the splitter and readline delims when greedy is changed"""
394 """update the splitter and readline delims when greedy is changed"""
395 if new:
395 if new:
396 self.splitter.delims = GREEDY_DELIMS
396 self.splitter.delims = GREEDY_DELIMS
397 else:
397 else:
398 self.splitter.delims = DELIMS
398 self.splitter.delims = DELIMS
399
399
400 if self.readline:
400 if self.readline:
401 self.readline.set_completer_delims(self.splitter.delims)
401 self.readline.set_completer_delims(self.splitter.delims)
402
402
403 merge_completions = CBool(True, config=True,
403 merge_completions = CBool(True, config=True,
404 help="""Whether to merge completion results into a single list
404 help="""Whether to merge completion results into a single list
405
405
406 If False, only the completion results from the first non-empty
406 If False, only the completion results from the first non-empty
407 completer will be returned.
407 completer will be returned.
408 """
408 """
409 )
409 )
410 omit__names = Enum((0,1,2), default_value=2, config=True,
410 omit__names = Enum((0,1,2), default_value=2, config=True,
411 help="""Instruct the completer to omit private method names
411 help="""Instruct the completer to omit private method names
412
412
413 Specifically, when completing on ``object.<tab>``.
413 Specifically, when completing on ``object.<tab>``.
414
414
415 When 2 [default]: all names that start with '_' will be excluded.
415 When 2 [default]: all names that start with '_' will be excluded.
416
416
417 When 1: all 'magic' names (``__foo__``) will be excluded.
417 When 1: all 'magic' names (``__foo__``) will be excluded.
418
418
419 When 0: nothing will be excluded.
419 When 0: nothing will be excluded.
420 """
420 """
421 )
421 )
422 limit_to__all__ = CBool(default_value=False, config=True,
422 limit_to__all__ = CBool(default_value=False, config=True,
423 help="""Instruct the completer to use __all__ for the completion
423 help="""Instruct the completer to use __all__ for the completion
424
424
425 Specifically, when completing on ``object.<tab>``.
425 Specifically, when completing on ``object.<tab>``.
426
426
427 When True: only those names in obj.__all__ will be included.
427 When True: only those names in obj.__all__ will be included.
428
428
429 When False [default]: the __all__ attribute is ignored
429 When False [default]: the __all__ attribute is ignored
430 """
430 """
431 )
431 )
432
432
433 def __init__(self, shell=None, namespace=None, global_namespace=None,
433 def __init__(self, shell=None, namespace=None, global_namespace=None,
434 alias_table=None, use_readline=True,
434 alias_table=None, use_readline=True,
435 config=None, **kwargs):
435 config=None, **kwargs):
436 """IPCompleter() -> completer
436 """IPCompleter() -> completer
437
437
438 Return a completer object suitable for use by the readline library
438 Return a completer object suitable for use by the readline library
439 via readline.set_completer().
439 via readline.set_completer().
440
440
441 Inputs:
441 Inputs:
442
442
443 - shell: a pointer to the ipython shell itself. This is needed
443 - shell: a pointer to the ipython shell itself. This is needed
444 because this completer knows about magic functions, and those can
444 because this completer knows about magic functions, and those can
445 only be accessed via the ipython instance.
445 only be accessed via the ipython instance.
446
446
447 - namespace: an optional dict where completions are performed.
447 - namespace: an optional dict where completions are performed.
448
448
449 - global_namespace: secondary optional dict for completions, to
449 - global_namespace: secondary optional dict for completions, to
450 handle cases (such as IPython embedded inside functions) where
450 handle cases (such as IPython embedded inside functions) where
451 both Python scopes are visible.
451 both Python scopes are visible.
452
452
453 - If alias_table is supplied, it should be a dictionary of aliases
453 - If alias_table is supplied, it should be a dictionary of aliases
454 to complete.
454 to complete.
455
455
456 use_readline : bool, optional
456 use_readline : bool, optional
457 If true, use the readline library. This completer can still function
457 If true, use the readline library. This completer can still function
458 without readline, though in that case callers must provide some extra
458 without readline, though in that case callers must provide some extra
459 information on each call about the current line."""
459 information on each call about the current line."""
460
460
461 self.magic_escape = ESC_MAGIC
461 self.magic_escape = ESC_MAGIC
462 self.splitter = CompletionSplitter()
462 self.splitter = CompletionSplitter()
463
463
464 # Readline configuration, only used by the rlcompleter method.
464 # Readline configuration, only used by the rlcompleter method.
465 if use_readline:
465 if use_readline:
466 # We store the right version of readline so that later code
466 # We store the right version of readline so that later code
467 import IPython.utils.rlineimpl as readline
467 import IPython.utils.rlineimpl as readline
468 self.readline = readline
468 self.readline = readline
469 else:
469 else:
470 self.readline = None
470 self.readline = None
471
471
472 # _greedy_changed() depends on splitter and readline being defined:
472 # _greedy_changed() depends on splitter and readline being defined:
473 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
473 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
474 config=config, **kwargs)
474 config=config, **kwargs)
475
475
476 # List where completion matches will be stored
476 # List where completion matches will be stored
477 self.matches = []
477 self.matches = []
478 self.shell = shell
478 self.shell = shell
479 if alias_table is None:
479 if alias_table is None:
480 alias_table = {}
480 alias_table = {}
481 self.alias_table = alias_table
481 self.alias_table = alias_table
482 # Regexp to split filenames with spaces in them
482 # Regexp to split filenames with spaces in them
483 self.space_name_re = re.compile(r'([^\\] )')
483 self.space_name_re = re.compile(r'([^\\] )')
484 # Hold a local ref. to glob.glob for speed
484 # Hold a local ref. to glob.glob for speed
485 self.glob = glob.glob
485 self.glob = glob.glob
486
486
487 # Determine if we are running on 'dumb' terminals, like (X)Emacs
487 # Determine if we are running on 'dumb' terminals, like (X)Emacs
488 # buffers, to avoid completion problems.
488 # buffers, to avoid completion problems.
489 term = os.environ.get('TERM','xterm')
489 term = os.environ.get('TERM','xterm')
490 self.dumb_terminal = term in ['dumb','emacs']
490 self.dumb_terminal = term in ['dumb','emacs']
491
491
492 # Special handling of backslashes needed in win32 platforms
492 # Special handling of backslashes needed in win32 platforms
493 if sys.platform == "win32":
493 if sys.platform == "win32":
494 self.clean_glob = self._clean_glob_win32
494 self.clean_glob = self._clean_glob_win32
495 else:
495 else:
496 self.clean_glob = self._clean_glob
496 self.clean_glob = self._clean_glob
497
497
498 #regexp to parse docstring for function signature
498 #regexp to parse docstring for function signature
499 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
499 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
500 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
500 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
501 #use this if positional argument name is also needed
501 #use this if positional argument name is also needed
502 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
502 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
503
503
504 # All active matcher routines for completion
504 # All active matcher routines for completion
505 self.matchers = [self.python_matches,
505 self.matchers = [self.python_matches,
506 self.file_matches,
506 self.file_matches,
507 self.magic_matches,
507 self.magic_matches,
508 self.alias_matches,
508 self.alias_matches,
509 self.python_func_kw_matches,
509 self.python_func_kw_matches,
510 ]
510 ]
511
511
512 def all_completions(self, text):
512 def all_completions(self, text):
513 """
513 """
514 Wrapper around the complete method for the benefit of emacs
514 Wrapper around the complete method for the benefit of emacs
515 and pydb.
515 and pydb.
516 """
516 """
517 return self.complete(text)[1]
517 return self.complete(text)[1]
518
518
519 def _clean_glob(self,text):
519 def _clean_glob(self,text):
520 return self.glob("%s*" % text)
520 return self.glob("%s*" % text)
521
521
522 def _clean_glob_win32(self,text):
522 def _clean_glob_win32(self,text):
523 return [f.replace("\\","/")
523 return [f.replace("\\","/")
524 for f in self.glob("%s*" % text)]
524 for f in self.glob("%s*" % text)]
525
525
526 def file_matches(self, text):
526 def file_matches(self, text):
527 """Match filenames, expanding ~USER type strings.
527 """Match filenames, expanding ~USER type strings.
528
528
529 Most of the seemingly convoluted logic in this completer is an
529 Most of the seemingly convoluted logic in this completer is an
530 attempt to handle filenames with spaces in them. And yet it's not
530 attempt to handle filenames with spaces in them. And yet it's not
531 quite perfect, because Python's readline doesn't expose all of the
531 quite perfect, because Python's readline doesn't expose all of the
532 GNU readline details needed for this to be done correctly.
532 GNU readline details needed for this to be done correctly.
533
533
534 For a filename with a space in it, the printed completions will be
534 For a filename with a space in it, the printed completions will be
535 only the parts after what's already been typed (instead of the
535 only the parts after what's already been typed (instead of the
536 full completions, as is normally done). I don't think with the
536 full completions, as is normally done). I don't think with the
537 current (as of Python 2.3) Python readline it's possible to do
537 current (as of Python 2.3) Python readline it's possible to do
538 better."""
538 better."""
539
539
540 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
540 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
541
541
542 # chars that require escaping with backslash - i.e. chars
542 # chars that require escaping with backslash - i.e. chars
543 # that readline treats incorrectly as delimiters, but we
543 # that readline treats incorrectly as delimiters, but we
544 # don't want to treat as delimiters in filename matching
544 # don't want to treat as delimiters in filename matching
545 # when escaped with backslash
545 # when escaped with backslash
546 if text.startswith('!'):
546 if text.startswith('!'):
547 text = text[1:]
547 text = text[1:]
548 text_prefix = '!'
548 text_prefix = '!'
549 else:
549 else:
550 text_prefix = ''
550 text_prefix = ''
551
551
552 text_until_cursor = self.text_until_cursor
552 text_until_cursor = self.text_until_cursor
553 # track strings with open quotes
553 # track strings with open quotes
554 open_quotes = has_open_quotes(text_until_cursor)
554 open_quotes = has_open_quotes(text_until_cursor)
555
555
556 if '(' in text_until_cursor or '[' in text_until_cursor:
556 if '(' in text_until_cursor or '[' in text_until_cursor:
557 lsplit = text
557 lsplit = text
558 else:
558 else:
559 try:
559 try:
560 # arg_split ~ shlex.split, but with unicode bugs fixed by us
560 # arg_split ~ shlex.split, but with unicode bugs fixed by us
561 lsplit = arg_split(text_until_cursor)[-1]
561 lsplit = arg_split(text_until_cursor)[-1]
562 except ValueError:
562 except ValueError:
563 # typically an unmatched ", or backslash without escaped char.
563 # typically an unmatched ", or backslash without escaped char.
564 if open_quotes:
564 if open_quotes:
565 lsplit = text_until_cursor.split(open_quotes)[-1]
565 lsplit = text_until_cursor.split(open_quotes)[-1]
566 else:
566 else:
567 return []
567 return []
568 except IndexError:
568 except IndexError:
569 # tab pressed on empty line
569 # tab pressed on empty line
570 lsplit = ""
570 lsplit = ""
571
571
572 if not open_quotes and lsplit != protect_filename(lsplit):
572 if not open_quotes and lsplit != protect_filename(lsplit):
573 # if protectables are found, do matching on the whole escaped name
573 # if protectables are found, do matching on the whole escaped name
574 has_protectables = True
574 has_protectables = True
575 text0,text = text,lsplit
575 text0,text = text,lsplit
576 else:
576 else:
577 has_protectables = False
577 has_protectables = False
578 text = os.path.expanduser(text)
578 text = os.path.expanduser(text)
579
579
580 if text == "":
580 if text == "":
581 return [text_prefix + protect_filename(f) for f in self.glob("*")]
581 return [text_prefix + protect_filename(f) for f in self.glob("*")]
582
582
583 # Compute the matches from the filesystem
583 # Compute the matches from the filesystem
584 m0 = self.clean_glob(text.replace('\\',''))
584 m0 = self.clean_glob(text.replace('\\',''))
585
585
586 if has_protectables:
586 if has_protectables:
587 # If we had protectables, we need to revert our changes to the
587 # If we had protectables, we need to revert our changes to the
588 # beginning of filename so that we don't double-write the part
588 # beginning of filename so that we don't double-write the part
589 # of the filename we have so far
589 # of the filename we have so far
590 len_lsplit = len(lsplit)
590 len_lsplit = len(lsplit)
591 matches = [text_prefix + text0 +
591 matches = [text_prefix + text0 +
592 protect_filename(f[len_lsplit:]) for f in m0]
592 protect_filename(f[len_lsplit:]) for f in m0]
593 else:
593 else:
594 if open_quotes:
594 if open_quotes:
595 # if we have a string with an open quote, we don't need to
595 # if we have a string with an open quote, we don't need to
596 # protect the names at all (and we _shouldn't_, as it
596 # protect the names at all (and we _shouldn't_, as it
597 # would cause bugs when the filesystem call is made).
597 # would cause bugs when the filesystem call is made).
598 matches = m0
598 matches = m0
599 else:
599 else:
600 matches = [text_prefix +
600 matches = [text_prefix +
601 protect_filename(f) for f in m0]
601 protect_filename(f) for f in m0]
602
602
603 #io.rprint('mm', matches) # dbg
603 #io.rprint('mm', matches) # dbg
604
604
605 # Mark directories in input list by appending '/' to their names.
605 # Mark directories in input list by appending '/' to their names.
606 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
606 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
607 return matches
607 return matches
608
608
609 def magic_matches(self, text):
609 def magic_matches(self, text):
610 """Match magics"""
610 """Match magics"""
611 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
611 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
612 # Get all shell magics now rather than statically, so magics loaded at
612 # Get all shell magics now rather than statically, so magics loaded at
613 # runtime show up too.
613 # runtime show up too.
614 lsm = self.shell.magics_manager.lsmagic()
614 lsm = self.shell.magics_manager.lsmagic()
615 line_magics = lsm['line']
615 line_magics = lsm['line']
616 cell_magics = lsm['cell']
616 cell_magics = lsm['cell']
617 pre = self.magic_escape
617 pre = self.magic_escape
618 pre2 = pre+pre
618 pre2 = pre+pre
619
619
620 # Completion logic:
620 # Completion logic:
621 # - user gives %%: only do cell magics
621 # - user gives %%: only do cell magics
622 # - user gives %: do both line and cell magics
622 # - user gives %: do both line and cell magics
623 # - no prefix: do both
623 # - no prefix: do both
624 # In other words, line magics are skipped if the user gives %% explicitly
624 # In other words, line magics are skipped if the user gives %% explicitly
625 bare_text = text.lstrip(pre)
625 bare_text = text.lstrip(pre)
626 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
626 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
627 if not text.startswith(pre2):
627 if not text.startswith(pre2):
628 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
628 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
629 return comp
629 return comp
630
630
631 def alias_matches(self, text):
631 def alias_matches(self, text):
632 """Match internal system aliases"""
632 """Match internal system aliases"""
633 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
633 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
634
634
635 # if we are not in the first 'item', alias matching
635 # if we are not in the first 'item', alias matching
636 # doesn't make sense - unless we are starting with 'sudo' command.
636 # doesn't make sense - unless we are starting with 'sudo' command.
637 main_text = self.text_until_cursor.lstrip()
637 main_text = self.text_until_cursor.lstrip()
638 if ' ' in main_text and not main_text.startswith('sudo'):
638 if ' ' in main_text and not main_text.startswith('sudo'):
639 return []
639 return []
640 text = os.path.expanduser(text)
640 text = os.path.expanduser(text)
641 aliases = self.alias_table.keys()
641 aliases = self.alias_table.keys()
642 if text == '':
642 if text == '':
643 return aliases
643 return aliases
644 else:
644 else:
645 return [a for a in aliases if a.startswith(text)]
645 return [a for a in aliases if a.startswith(text)]
646
646
647 def python_matches(self,text):
647 def python_matches(self,text):
648 """Match attributes or global python names"""
648 """Match attributes or global python names"""
649
649
650 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
650 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
651 if "." in text:
651 if "." in text:
652 try:
652 try:
653 matches = self.attr_matches(text)
653 matches = self.attr_matches(text)
654 if text.endswith('.') and self.omit__names:
654 if text.endswith('.') and self.omit__names:
655 if self.omit__names == 1:
655 if self.omit__names == 1:
656 # true if txt is _not_ a __ name, false otherwise:
656 # true if txt is _not_ a __ name, false otherwise:
657 no__name = (lambda txt:
657 no__name = (lambda txt:
658 re.match(r'.*\.__.*?__',txt) is None)
658 re.match(r'.*\.__.*?__',txt) is None)
659 else:
659 else:
660 # true if txt is _not_ a _ name, false otherwise:
660 # true if txt is _not_ a _ name, false otherwise:
661 no__name = (lambda txt:
661 no__name = (lambda txt:
662 re.match(r'.*\._.*?',txt) is None)
662 re.match(r'.*\._.*?',txt) is None)
663 matches = filter(no__name, matches)
663 matches = filter(no__name, matches)
664 except NameError:
664 except NameError:
665 # catches <undefined attributes>.<tab>
665 # catches <undefined attributes>.<tab>
666 matches = []
666 matches = []
667 else:
667 else:
668 matches = self.global_matches(text)
668 matches = self.global_matches(text)
669
669
670 return matches
670 return matches
671
671
672 def _default_arguments_from_docstring(self, doc):
672 def _default_arguments_from_docstring(self, doc):
673 """Parse the first line of docstring for call signature.
673 """Parse the first line of docstring for call signature.
674
674
675 Docstring should be of the form 'min(iterable[, key=func])\n'.
675 Docstring should be of the form 'min(iterable[, key=func])\n'.
676 It can also parse cython docstring of the form
676 It can also parse cython docstring of the form
677 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
677 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
678 """
678 """
679 if doc is None:
679 if doc is None:
680 return []
680 return []
681
681
682 #care only the firstline
682 #care only the firstline
683 line = doc.lstrip().splitlines()[0]
683 line = doc.lstrip().splitlines()[0]
684
684
685 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
685 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
686 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
686 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
687 sig = self.docstring_sig_re.search(line)
687 sig = self.docstring_sig_re.search(line)
688 if sig is None:
688 if sig is None:
689 return []
689 return []
690 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
690 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
691 sig = sig.groups()[0].split(',')
691 sig = sig.groups()[0].split(',')
692 ret = []
692 ret = []
693 for s in sig:
693 for s in sig:
694 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
694 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
695 ret += self.docstring_kwd_re.findall(s)
695 ret += self.docstring_kwd_re.findall(s)
696 return ret
696 return ret
697
697
698 def _default_arguments(self, obj):
698 def _default_arguments(self, obj):
699 """Return the list of default arguments of obj if it is callable,
699 """Return the list of default arguments of obj if it is callable,
700 or empty list otherwise."""
700 or empty list otherwise."""
701 call_obj = obj
701 call_obj = obj
702 ret = []
702 ret = []
703 if inspect.isbuiltin(obj):
703 if inspect.isbuiltin(obj):
704 pass
704 pass
705 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
705 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
706 if inspect.isclass(obj):
706 if inspect.isclass(obj):
707 #for cython embededsignature=True the constructor docstring
707 #for cython embededsignature=True the constructor docstring
708 #belongs to the object itself not __init__
708 #belongs to the object itself not __init__
709 ret += self._default_arguments_from_docstring(
709 ret += self._default_arguments_from_docstring(
710 getattr(obj, '__doc__', ''))
710 getattr(obj, '__doc__', ''))
711 # for classes, check for __init__,__new__
711 # for classes, check for __init__,__new__
712 call_obj = (getattr(obj, '__init__', None) or
712 call_obj = (getattr(obj, '__init__', None) or
713 getattr(obj, '__new__', None))
713 getattr(obj, '__new__', None))
714 # for all others, check if they are __call__able
714 # for all others, check if they are __call__able
715 elif hasattr(obj, '__call__'):
715 elif hasattr(obj, '__call__'):
716 call_obj = obj.__call__
716 call_obj = obj.__call__
717
717
718 ret += self._default_arguments_from_docstring(
718 ret += self._default_arguments_from_docstring(
719 getattr(call_obj, '__doc__', ''))
719 getattr(call_obj, '__doc__', ''))
720
720
721 try:
721 try:
722 args,_,_1,defaults = inspect.getargspec(call_obj)
722 args,_,_1,defaults = inspect.getargspec(call_obj)
723 if defaults:
723 if defaults:
724 ret+=args[-len(defaults):]
724 ret+=args[-len(defaults):]
725 except TypeError:
725 except TypeError:
726 pass
726 pass
727
727
728 return list(set(ret))
728 return list(set(ret))
729
729
730 def python_func_kw_matches(self,text):
730 def python_func_kw_matches(self,text):
731 """Match named parameters (kwargs) of the last open function"""
731 """Match named parameters (kwargs) of the last open function"""
732
732
733 if "." in text: # a parameter cannot be dotted
733 if "." in text: # a parameter cannot be dotted
734 return []
734 return []
735 try: regexp = self.__funcParamsRegex
735 try: regexp = self.__funcParamsRegex
736 except AttributeError:
736 except AttributeError:
737 regexp = self.__funcParamsRegex = re.compile(r'''
737 regexp = self.__funcParamsRegex = re.compile(r'''
738 '.*?(?<!\\)' | # single quoted strings or
738 '.*?(?<!\\)' | # single quoted strings or
739 ".*?(?<!\\)" | # double quoted strings or
739 ".*?(?<!\\)" | # double quoted strings or
740 \w+ | # identifier
740 \w+ | # identifier
741 \S # other characters
741 \S # other characters
742 ''', re.VERBOSE | re.DOTALL)
742 ''', re.VERBOSE | re.DOTALL)
743 # 1. find the nearest identifier that comes before an unclosed
743 # 1. find the nearest identifier that comes before an unclosed
744 # parenthesis before the cursor
744 # parenthesis before the cursor
745 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
745 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
746 tokens = regexp.findall(self.text_until_cursor)
746 tokens = regexp.findall(self.text_until_cursor)
747 tokens.reverse()
747 tokens.reverse()
748 iterTokens = iter(tokens); openPar = 0
748 iterTokens = iter(tokens); openPar = 0
749
749
750 for token in iterTokens:
750 for token in iterTokens:
751 if token == ')':
751 if token == ')':
752 openPar -= 1
752 openPar -= 1
753 elif token == '(':
753 elif token == '(':
754 openPar += 1
754 openPar += 1
755 if openPar > 0:
755 if openPar > 0:
756 # found the last unclosed parenthesis
756 # found the last unclosed parenthesis
757 break
757 break
758 else:
758 else:
759 return []
759 return []
760 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
760 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
761 ids = []
761 ids = []
762 isId = re.compile(r'\w+$').match
762 isId = re.compile(r'\w+$').match
763
763
764 while True:
764 while True:
765 try:
765 try:
766 ids.append(next(iterTokens))
766 ids.append(next(iterTokens))
767 if not isId(ids[-1]):
767 if not isId(ids[-1]):
768 ids.pop(); break
768 ids.pop(); break
769 if not next(iterTokens) == '.':
769 if not next(iterTokens) == '.':
770 break
770 break
771 except StopIteration:
771 except StopIteration:
772 break
772 break
773 # lookup the candidate callable matches either using global_matches
773 # lookup the candidate callable matches either using global_matches
774 # or attr_matches for dotted names
774 # or attr_matches for dotted names
775 if len(ids) == 1:
775 if len(ids) == 1:
776 callableMatches = self.global_matches(ids[0])
776 callableMatches = self.global_matches(ids[0])
777 else:
777 else:
778 callableMatches = self.attr_matches('.'.join(ids[::-1]))
778 callableMatches = self.attr_matches('.'.join(ids[::-1]))
779 argMatches = []
779 argMatches = []
780 for callableMatch in callableMatches:
780 for callableMatch in callableMatches:
781 try:
781 try:
782 namedArgs = self._default_arguments(eval(callableMatch,
782 namedArgs = self._default_arguments(eval(callableMatch,
783 self.namespace))
783 self.namespace))
784 except:
784 except:
785 continue
785 continue
786
786
787 for namedArg in namedArgs:
787 for namedArg in namedArgs:
788 if namedArg.startswith(text):
788 if namedArg.startswith(text):
789 argMatches.append("%s=" %namedArg)
789 argMatches.append("%s=" %namedArg)
790 return argMatches
790 return argMatches
791
791
792 def dispatch_custom_completer(self, text):
792 def dispatch_custom_completer(self, text):
793 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
793 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
794 line = self.line_buffer
794 line = self.line_buffer
795 if not line.strip():
795 if not line.strip():
796 return None
796 return None
797
797
798 # Create a little structure to pass all the relevant information about
798 # Create a little structure to pass all the relevant information about
799 # the current completion to any custom completer.
799 # the current completion to any custom completer.
800 event = Bunch()
800 event = Bunch()
801 event.line = line
801 event.line = line
802 event.symbol = text
802 event.symbol = text
803 cmd = line.split(None,1)[0]
803 cmd = line.split(None,1)[0]
804 event.command = cmd
804 event.command = cmd
805 event.text_until_cursor = self.text_until_cursor
805 event.text_until_cursor = self.text_until_cursor
806
806
807 #print "\ncustom:{%s]\n" % event # dbg
807 #print "\ncustom:{%s]\n" % event # dbg
808
808
809 # for foo etc, try also to find completer for %foo
809 # for foo etc, try also to find completer for %foo
810 if not cmd.startswith(self.magic_escape):
810 if not cmd.startswith(self.magic_escape):
811 try_magic = self.custom_completers.s_matches(
811 try_magic = self.custom_completers.s_matches(
812 self.magic_escape + cmd)
812 self.magic_escape + cmd)
813 else:
813 else:
814 try_magic = []
814 try_magic = []
815
815
816 for c in itertools.chain(self.custom_completers.s_matches(cmd),
816 for c in itertools.chain(self.custom_completers.s_matches(cmd),
817 try_magic,
817 try_magic,
818 self.custom_completers.flat_matches(self.text_until_cursor)):
818 self.custom_completers.flat_matches(self.text_until_cursor)):
819 #print "try",c # dbg
819 #print "try",c # dbg
820 try:
820 try:
821 res = c(event)
821 res = c(event)
822 if res:
822 if res:
823 # first, try case sensitive match
823 # first, try case sensitive match
824 withcase = [r for r in res if r.startswith(text)]
824 withcase = [r for r in res if r.startswith(text)]
825 if withcase:
825 if withcase:
826 return withcase
826 return withcase
827 # if none, then case insensitive ones are ok too
827 # if none, then case insensitive ones are ok too
828 text_low = text.lower()
828 text_low = text.lower()
829 return [r for r in res if r.lower().startswith(text_low)]
829 return [r for r in res if r.lower().startswith(text_low)]
830 except TryNext:
830 except TryNext:
831 pass
831 pass
832
832
833 return None
833 return None
834
834
835 def complete(self, text=None, line_buffer=None, cursor_pos=None):
835 def complete(self, text=None, line_buffer=None, cursor_pos=None):
836 """Find completions for the given text and line context.
836 """Find completions for the given text and line context.
837
837
838 This is called successively with state == 0, 1, 2, ... until it
838 This is called successively with state == 0, 1, 2, ... until it
839 returns None. The completion should begin with 'text'.
839 returns None. The completion should begin with 'text'.
840
840
841 Note that both the text and the line_buffer are optional, but at least
841 Note that both the text and the line_buffer are optional, but at least
842 one of them must be given.
842 one of them must be given.
843
843
844 Parameters
844 Parameters
845 ----------
845 ----------
846 text : string, optional
846 text : string, optional
847 Text to perform the completion on. If not given, the line buffer
847 Text to perform the completion on. If not given, the line buffer
848 is split using the instance's CompletionSplitter object.
848 is split using the instance's CompletionSplitter object.
849
849
850 line_buffer : string, optional
850 line_buffer : string, optional
851 If not given, the completer attempts to obtain the current line
851 If not given, the completer attempts to obtain the current line
852 buffer via readline. This keyword allows clients which are
852 buffer via readline. This keyword allows clients which are
853 requesting for text completions in non-readline contexts to inform
853 requesting for text completions in non-readline contexts to inform
854 the completer of the entire text.
854 the completer of the entire text.
855
855
856 cursor_pos : int, optional
856 cursor_pos : int, optional
857 Index of the cursor in the full line buffer. Should be provided by
857 Index of the cursor in the full line buffer. Should be provided by
858 remote frontends where kernel has no access to frontend state.
858 remote frontends where kernel has no access to frontend state.
859
859
860 Returns
860 Returns
861 -------
861 -------
862 text : str
862 text : str
863 Text that was actually used in the completion.
863 Text that was actually used in the completion.
864
864
865 matches : list
865 matches : list
866 A list of completion matches.
866 A list of completion matches.
867 """
867 """
868 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
868 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
869
869
870 # if the cursor position isn't given, the only sane assumption we can
870 # if the cursor position isn't given, the only sane assumption we can
871 # make is that it's at the end of the line (the common case)
871 # make is that it's at the end of the line (the common case)
872 if cursor_pos is None:
872 if cursor_pos is None:
873 cursor_pos = len(line_buffer) if text is None else len(text)
873 cursor_pos = len(line_buffer) if text is None else len(text)
874
874
875 # if text is either None or an empty string, rely on the line buffer
875 # if text is either None or an empty string, rely on the line buffer
876 if not text:
876 if not text:
877 text = self.splitter.split_line(line_buffer, cursor_pos)
877 text = self.splitter.split_line(line_buffer, cursor_pos)
878
878
879 # If no line buffer is given, assume the input text is all there was
879 # If no line buffer is given, assume the input text is all there was
880 if line_buffer is None:
880 if line_buffer is None:
881 line_buffer = text
881 line_buffer = text
882
882
883 self.line_buffer = line_buffer
883 self.line_buffer = line_buffer
884 self.text_until_cursor = self.line_buffer[:cursor_pos]
884 self.text_until_cursor = self.line_buffer[:cursor_pos]
885 #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
885 #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
886
886
887 # Start with a clean slate of completions
887 # Start with a clean slate of completions
888 self.matches[:] = []
888 self.matches[:] = []
889 custom_res = self.dispatch_custom_completer(text)
889 custom_res = self.dispatch_custom_completer(text)
890 if custom_res is not None:
890 if custom_res is not None:
891 # did custom completers produce something?
891 # did custom completers produce something?
892 self.matches = custom_res
892 self.matches = custom_res
893 else:
893 else:
894 # Extend the list of completions with the results of each
894 # Extend the list of completions with the results of each
895 # matcher, so we return results to the user from all
895 # matcher, so we return results to the user from all
896 # namespaces.
896 # namespaces.
897 if self.merge_completions:
897 if self.merge_completions:
898 self.matches = []
898 self.matches = []
899 for matcher in self.matchers:
899 for matcher in self.matchers:
900 try:
900 try:
901 self.matches.extend(matcher(text))
901 self.matches.extend(matcher(text))
902 except:
902 except:
903 # Show the ugly traceback if the matcher causes an
903 # Show the ugly traceback if the matcher causes an
904 # exception, but do NOT crash the kernel!
904 # exception, but do NOT crash the kernel!
905 sys.excepthook(*sys.exc_info())
905 sys.excepthook(*sys.exc_info())
906 else:
906 else:
907 for matcher in self.matchers:
907 for matcher in self.matchers:
908 self.matches = matcher(text)
908 self.matches = matcher(text)
909 if self.matches:
909 if self.matches:
910 break
910 break
911 # FIXME: we should extend our api to return a dict with completions for
911 # FIXME: we should extend our api to return a dict with completions for
912 # different types of objects. The rlcomplete() method could then
912 # different types of objects. The rlcomplete() method could then
913 # simply collapse the dict into a list for readline, but we'd have
913 # simply collapse the dict into a list for readline, but we'd have
914 # richer completion semantics in other evironments.
914 # richer completion semantics in other evironments.
915 self.matches = sorted(set(self.matches))
915 self.matches = sorted(set(self.matches))
916 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
916 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
917 return text, self.matches
917 return text, self.matches
918
918
919 def rlcomplete(self, text, state):
919 def rlcomplete(self, text, state):
920 """Return the state-th possible completion for 'text'.
920 """Return the state-th possible completion for 'text'.
921
921
922 This is called successively with state == 0, 1, 2, ... until it
922 This is called successively with state == 0, 1, 2, ... until it
923 returns None. The completion should begin with 'text'.
923 returns None. The completion should begin with 'text'.
924
924
925 Parameters
925 Parameters
926 ----------
926 ----------
927 text : string
927 text : string
928 Text to perform the completion on.
928 Text to perform the completion on.
929
929
930 state : int
930 state : int
931 Counter used by readline.
931 Counter used by readline.
932 """
932 """
933 if state==0:
933 if state==0:
934
934
935 self.line_buffer = line_buffer = self.readline.get_line_buffer()
935 self.line_buffer = line_buffer = self.readline.get_line_buffer()
936 cursor_pos = self.readline.get_endidx()
936 cursor_pos = self.readline.get_endidx()
937
937
938 #io.rprint("\nRLCOMPLETE: %r %r %r" %
938 #io.rprint("\nRLCOMPLETE: %r %r %r" %
939 # (text, line_buffer, cursor_pos) ) # dbg
939 # (text, line_buffer, cursor_pos) ) # dbg
940
940
941 # if there is only a tab on a line with only whitespace, instead of
941 # if there is only a tab on a line with only whitespace, instead of
942 # the mostly useless 'do you want to see all million completions'
942 # the mostly useless 'do you want to see all million completions'
943 # message, just do the right thing and give the user his tab!
943 # message, just do the right thing and give the user his tab!
944 # Incidentally, this enables pasting of tabbed text from an editor
944 # Incidentally, this enables pasting of tabbed text from an editor
945 # (as long as autoindent is off).
945 # (as long as autoindent is off).
946
946
947 # It should be noted that at least pyreadline still shows file
947 # It should be noted that at least pyreadline still shows file
948 # completions - is there a way around it?
948 # completions - is there a way around it?
949
949
950 # don't apply this on 'dumb' terminals, such as emacs buffers, so
950 # don't apply this on 'dumb' terminals, such as emacs buffers, so
951 # we don't interfere with their own tab-completion mechanism.
951 # we don't interfere with their own tab-completion mechanism.
952 if not (self.dumb_terminal or line_buffer.strip()):
952 if not (self.dumb_terminal or line_buffer.strip()):
953 self.readline.insert_text('\t')
953 self.readline.insert_text('\t')
954 sys.stdout.flush()
954 sys.stdout.flush()
955 return None
955 return None
956
956
957 # Note: debugging exceptions that may occur in completion is very
957 # Note: debugging exceptions that may occur in completion is very
958 # tricky, because readline unconditionally silences them. So if
958 # tricky, because readline unconditionally silences them. So if
959 # during development you suspect a bug in the completion code, turn
959 # during development you suspect a bug in the completion code, turn
960 # this flag on temporarily by uncommenting the second form (don't
960 # this flag on temporarily by uncommenting the second form (don't
961 # flip the value in the first line, as the '# dbg' marker can be
961 # flip the value in the first line, as the '# dbg' marker can be
962 # automatically detected and is used elsewhere).
962 # automatically detected and is used elsewhere).
963 DEBUG = False
963 DEBUG = False
964 #DEBUG = True # dbg
964 #DEBUG = True # dbg
965 if DEBUG:
965 if DEBUG:
966 try:
966 try:
967 self.complete(text, line_buffer, cursor_pos)
967 self.complete(text, line_buffer, cursor_pos)
968 except:
968 except:
969 import traceback; traceback.print_exc()
969 import traceback; traceback.print_exc()
970 else:
970 else:
971 # The normal production version is here
971 # The normal production version is here
972
972
973 # This method computes the self.matches array
973 # This method computes the self.matches array
974 self.complete(text, line_buffer, cursor_pos)
974 self.complete(text, line_buffer, cursor_pos)
975
975
976 try:
976 try:
977 return self.matches[state]
977 return self.matches[state]
978 except IndexError:
978 except IndexError:
979 return None
979 return None
@@ -1,579 +1,584 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Pdb debugger class.
3 Pdb debugger class.
4
4
5 Modified from the standard pdb.Pdb class to avoid including readline, so that
5 Modified from the standard pdb.Pdb class to avoid including readline, so that
6 the command line completion of other programs which include this isn't
6 the command line completion of other programs which include this isn't
7 damaged.
7 damaged.
8
8
9 In the future, this class will be expanded with improvements over the standard
9 In the future, this class will be expanded with improvements over the standard
10 pdb.
10 pdb.
11
11
12 The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
12 The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
13 changes. Licensing should therefore be under the standard Python terms. For
13 changes. Licensing should therefore be under the standard Python terms. For
14 details on the PSF (Python Software Foundation) standard license, see:
14 details on the PSF (Python Software Foundation) standard license, see:
15
15
16 http://www.python.org/2.2.3/license.html"""
16 http://www.python.org/2.2.3/license.html"""
17
17
18 #*****************************************************************************
18 #*****************************************************************************
19 #
19 #
20 # This file is licensed under the PSF license.
20 # This file is licensed under the PSF license.
21 #
21 #
22 # Copyright (C) 2001 Python Software Foundation, www.python.org
22 # Copyright (C) 2001 Python Software Foundation, www.python.org
23 # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu>
23 # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu>
24 #
24 #
25 #
25 #
26 #*****************************************************************************
26 #*****************************************************************************
27 from __future__ import print_function
27 from __future__ import print_function
28
28
29 import bdb
29 import bdb
30 import functools
30 import functools
31 import linecache
31 import linecache
32 import sys
32 import sys
33
33
34 from IPython import get_ipython
34 from IPython import get_ipython
35 from IPython.utils import PyColorize, ulinecache
35 from IPython.utils import PyColorize, ulinecache
36 from IPython.utils import coloransi, io, py3compat
36 from IPython.utils import coloransi, io, py3compat
37 from IPython.core.excolors import exception_colors
37 from IPython.core.excolors import exception_colors
38 from IPython.testing.skipdoctest import skip_doctest
38 from IPython.testing.skipdoctest import skip_doctest
39
39
40 # See if we can use pydb.
40 # See if we can use pydb.
41 has_pydb = False
41 has_pydb = False
42 prompt = 'ipdb> '
42 prompt = 'ipdb> '
43 #We have to check this directly from sys.argv, config struct not yet available
43 #We have to check this directly from sys.argv, config struct not yet available
44 if '--pydb' in sys.argv:
44 if '--pydb' in sys.argv:
45 try:
45 try:
46 import pydb
46 import pydb
47 if hasattr(pydb.pydb, "runl") and pydb.version>'1.17':
47 if hasattr(pydb.pydb, "runl") and pydb.version>'1.17':
48 # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we
48 # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we
49 # better protect against it.
49 # better protect against it.
50 has_pydb = True
50 has_pydb = True
51 except ImportError:
51 except ImportError:
52 print("Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available")
52 print("Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available")
53
53
54 if has_pydb:
54 if has_pydb:
55 from pydb import Pdb as OldPdb
55 from pydb import Pdb as OldPdb
56 #print "Using pydb for %run -d and post-mortem" #dbg
56 #print "Using pydb for %run -d and post-mortem" #dbg
57 prompt = 'ipydb> '
57 prompt = 'ipydb> '
58 else:
58 else:
59 from pdb import Pdb as OldPdb
59 from pdb import Pdb as OldPdb
60
60
61 # Allow the set_trace code to operate outside of an ipython instance, even if
61 # Allow the set_trace code to operate outside of an ipython instance, even if
62 # it does so with some limitations. The rest of this support is implemented in
62 # it does so with some limitations. The rest of this support is implemented in
63 # the Tracer constructor.
63 # the Tracer constructor.
64 def BdbQuit_excepthook(et, ev, tb, excepthook=None):
64 def BdbQuit_excepthook(et, ev, tb, excepthook=None):
65 """Exception hook which handles `BdbQuit` exceptions.
65 """Exception hook which handles `BdbQuit` exceptions.
66
66
67 All other exceptions are processed using the `excepthook`
67 All other exceptions are processed using the `excepthook`
68 parameter.
68 parameter.
69 """
69 """
70 if et==bdb.BdbQuit:
70 if et==bdb.BdbQuit:
71 print('Exiting Debugger.')
71 print('Exiting Debugger.')
72 elif excepthook is not None:
72 elif excepthook is not None:
73 excepthook(et, ev, tb)
73 excepthook(et, ev, tb)
74 else:
74 else:
75 # Backwards compatibility. Raise deprecation warning?
75 # Backwards compatibility. Raise deprecation warning?
76 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
76 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
77
77
78 def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None):
78 def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None):
79 print('Exiting Debugger.')
79 print('Exiting Debugger.')
80
80
81
81
82 class Tracer(object):
82 class Tracer(object):
83 """Class for local debugging, similar to pdb.set_trace.
83 """Class for local debugging, similar to pdb.set_trace.
84
84
85 Instances of this class, when called, behave like pdb.set_trace, but
85 Instances of this class, when called, behave like pdb.set_trace, but
86 providing IPython's enhanced capabilities.
86 providing IPython's enhanced capabilities.
87
87
88 This is implemented as a class which must be initialized in your own code
88 This is implemented as a class which must be initialized in your own code
89 and not as a standalone function because we need to detect at runtime
89 and not as a standalone function because we need to detect at runtime
90 whether IPython is already active or not. That detection is done in the
90 whether IPython is already active or not. That detection is done in the
91 constructor, ensuring that this code plays nicely with a running IPython,
91 constructor, ensuring that this code plays nicely with a running IPython,
92 while functioning acceptably (though with limitations) if outside of it.
92 while functioning acceptably (though with limitations) if outside of it.
93 """
93 """
94
94
95 @skip_doctest
95 @skip_doctest
96 def __init__(self,colors=None):
96 def __init__(self,colors=None):
97 """Create a local debugger instance.
97 """Create a local debugger instance.
98
98
99 :Parameters:
99 Parameters
100 ----------
100
101
101 - `colors` (None): a string containing the name of the color scheme to
102 colors : str, optional
102 use, it must be one of IPython's valid color schemes. If not given, the
103 The name of the color scheme to use, it must be one of IPython's
103 function will default to the current IPython scheme when running inside
104 valid color schemes. If not given, the function will default to
104 IPython, and to 'NoColor' otherwise.
105 the current IPython scheme when running inside IPython, and to
106 'NoColor' otherwise.
105
107
106 Usage example:
108 Examples
109 --------
110 ::
107
111
108 from IPython.core.debugger import Tracer; debug_here = Tracer()
112 from IPython.core.debugger import Tracer; debug_here = Tracer()
109
113
110 ... later in your code
114 Later in your code::
115
111 debug_here() # -> will open up the debugger at that point.
116 debug_here() # -> will open up the debugger at that point.
112
117
113 Once the debugger activates, you can use all of its regular commands to
118 Once the debugger activates, you can use all of its regular commands to
114 step through code, set breakpoints, etc. See the pdb documentation
119 step through code, set breakpoints, etc. See the pdb documentation
115 from the Python standard library for usage details.
120 from the Python standard library for usage details.
116 """
121 """
117
122
118 ip = get_ipython()
123 ip = get_ipython()
119 if ip is None:
124 if ip is None:
120 # Outside of ipython, we set our own exception hook manually
125 # Outside of ipython, we set our own exception hook manually
121 sys.excepthook = functools.partial(BdbQuit_excepthook,
126 sys.excepthook = functools.partial(BdbQuit_excepthook,
122 excepthook=sys.excepthook)
127 excepthook=sys.excepthook)
123 def_colors = 'NoColor'
128 def_colors = 'NoColor'
124 try:
129 try:
125 # Limited tab completion support
130 # Limited tab completion support
126 import readline
131 import readline
127 readline.parse_and_bind('tab: complete')
132 readline.parse_and_bind('tab: complete')
128 except ImportError:
133 except ImportError:
129 pass
134 pass
130 else:
135 else:
131 # In ipython, we use its custom exception handler mechanism
136 # In ipython, we use its custom exception handler mechanism
132 def_colors = ip.colors
137 def_colors = ip.colors
133 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
138 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
134
139
135 if colors is None:
140 if colors is None:
136 colors = def_colors
141 colors = def_colors
137
142
138 # The stdlib debugger internally uses a modified repr from the `repr`
143 # The stdlib debugger internally uses a modified repr from the `repr`
139 # module, that limits the length of printed strings to a hardcoded
144 # module, that limits the length of printed strings to a hardcoded
140 # limit of 30 characters. That much trimming is too aggressive, let's
145 # limit of 30 characters. That much trimming is too aggressive, let's
141 # at least raise that limit to 80 chars, which should be enough for
146 # at least raise that limit to 80 chars, which should be enough for
142 # most interactive uses.
147 # most interactive uses.
143 try:
148 try:
144 from repr import aRepr
149 from repr import aRepr
145 aRepr.maxstring = 80
150 aRepr.maxstring = 80
146 except:
151 except:
147 # This is only a user-facing convenience, so any error we encounter
152 # This is only a user-facing convenience, so any error we encounter
148 # here can be warned about but can be otherwise ignored. These
153 # here can be warned about but can be otherwise ignored. These
149 # printouts will tell us about problems if this API changes
154 # printouts will tell us about problems if this API changes
150 import traceback
155 import traceback
151 traceback.print_exc()
156 traceback.print_exc()
152
157
153 self.debugger = Pdb(colors)
158 self.debugger = Pdb(colors)
154
159
155 def __call__(self):
160 def __call__(self):
156 """Starts an interactive debugger at the point where called.
161 """Starts an interactive debugger at the point where called.
157
162
158 This is similar to the pdb.set_trace() function from the std lib, but
163 This is similar to the pdb.set_trace() function from the std lib, but
159 using IPython's enhanced debugger."""
164 using IPython's enhanced debugger."""
160
165
161 self.debugger.set_trace(sys._getframe().f_back)
166 self.debugger.set_trace(sys._getframe().f_back)
162
167
163
168
164 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
169 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
165 """Make new_fn have old_fn's doc string. This is particularly useful
170 """Make new_fn have old_fn's doc string. This is particularly useful
166 for the ``do_...`` commands that hook into the help system.
171 for the ``do_...`` commands that hook into the help system.
167 Adapted from from a comp.lang.python posting
172 Adapted from from a comp.lang.python posting
168 by Duncan Booth."""
173 by Duncan Booth."""
169 def wrapper(*args, **kw):
174 def wrapper(*args, **kw):
170 return new_fn(*args, **kw)
175 return new_fn(*args, **kw)
171 if old_fn.__doc__:
176 if old_fn.__doc__:
172 wrapper.__doc__ = old_fn.__doc__ + additional_text
177 wrapper.__doc__ = old_fn.__doc__ + additional_text
173 return wrapper
178 return wrapper
174
179
175
180
176 def _file_lines(fname):
181 def _file_lines(fname):
177 """Return the contents of a named file as a list of lines.
182 """Return the contents of a named file as a list of lines.
178
183
179 This function never raises an IOError exception: if the file can't be
184 This function never raises an IOError exception: if the file can't be
180 read, it simply returns an empty list."""
185 read, it simply returns an empty list."""
181
186
182 try:
187 try:
183 outfile = open(fname)
188 outfile = open(fname)
184 except IOError:
189 except IOError:
185 return []
190 return []
186 else:
191 else:
187 out = outfile.readlines()
192 out = outfile.readlines()
188 outfile.close()
193 outfile.close()
189 return out
194 return out
190
195
191
196
192 class Pdb(OldPdb):
197 class Pdb(OldPdb):
193 """Modified Pdb class, does not load readline."""
198 """Modified Pdb class, does not load readline."""
194
199
195 def __init__(self,color_scheme='NoColor',completekey=None,
200 def __init__(self,color_scheme='NoColor',completekey=None,
196 stdin=None, stdout=None):
201 stdin=None, stdout=None):
197
202
198 # Parent constructor:
203 # Parent constructor:
199 if has_pydb and completekey is None:
204 if has_pydb and completekey is None:
200 OldPdb.__init__(self,stdin=stdin,stdout=io.stdout)
205 OldPdb.__init__(self,stdin=stdin,stdout=io.stdout)
201 else:
206 else:
202 OldPdb.__init__(self,completekey,stdin,stdout)
207 OldPdb.__init__(self,completekey,stdin,stdout)
203
208
204 self.prompt = prompt # The default prompt is '(Pdb)'
209 self.prompt = prompt # The default prompt is '(Pdb)'
205
210
206 # IPython changes...
211 # IPython changes...
207 self.is_pydb = has_pydb
212 self.is_pydb = has_pydb
208
213
209 self.shell = get_ipython()
214 self.shell = get_ipython()
210
215
211 if self.shell is None:
216 if self.shell is None:
212 # No IPython instance running, we must create one
217 # No IPython instance running, we must create one
213 from IPython.terminal.interactiveshell import \
218 from IPython.terminal.interactiveshell import \
214 TerminalInteractiveShell
219 TerminalInteractiveShell
215 self.shell = TerminalInteractiveShell.instance()
220 self.shell = TerminalInteractiveShell.instance()
216
221
217 if self.is_pydb:
222 if self.is_pydb:
218
223
219 # interactiveshell.py's ipalias seems to want pdb's checkline
224 # interactiveshell.py's ipalias seems to want pdb's checkline
220 # which located in pydb.fn
225 # which located in pydb.fn
221 import pydb.fns
226 import pydb.fns
222 self.checkline = lambda filename, lineno: \
227 self.checkline = lambda filename, lineno: \
223 pydb.fns.checkline(self, filename, lineno)
228 pydb.fns.checkline(self, filename, lineno)
224
229
225 self.curframe = None
230 self.curframe = None
226 self.do_restart = self.new_do_restart
231 self.do_restart = self.new_do_restart
227
232
228 self.old_all_completions = self.shell.Completer.all_completions
233 self.old_all_completions = self.shell.Completer.all_completions
229 self.shell.Completer.all_completions=self.all_completions
234 self.shell.Completer.all_completions=self.all_completions
230
235
231 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
236 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
232 OldPdb.do_list)
237 OldPdb.do_list)
233 self.do_l = self.do_list
238 self.do_l = self.do_list
234 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
239 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
235 OldPdb.do_frame)
240 OldPdb.do_frame)
236
241
237 self.aliases = {}
242 self.aliases = {}
238
243
239 # Create color table: we copy the default one from the traceback
244 # Create color table: we copy the default one from the traceback
240 # module and add a few attributes needed for debugging
245 # module and add a few attributes needed for debugging
241 self.color_scheme_table = exception_colors()
246 self.color_scheme_table = exception_colors()
242
247
243 # shorthands
248 # shorthands
244 C = coloransi.TermColors
249 C = coloransi.TermColors
245 cst = self.color_scheme_table
250 cst = self.color_scheme_table
246
251
247 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
252 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
248 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
253 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
249
254
250 cst['Linux'].colors.breakpoint_enabled = C.LightRed
255 cst['Linux'].colors.breakpoint_enabled = C.LightRed
251 cst['Linux'].colors.breakpoint_disabled = C.Red
256 cst['Linux'].colors.breakpoint_disabled = C.Red
252
257
253 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
258 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
254 cst['LightBG'].colors.breakpoint_disabled = C.Red
259 cst['LightBG'].colors.breakpoint_disabled = C.Red
255
260
256 self.set_colors(color_scheme)
261 self.set_colors(color_scheme)
257
262
258 # Add a python parser so we can syntax highlight source while
263 # Add a python parser so we can syntax highlight source while
259 # debugging.
264 # debugging.
260 self.parser = PyColorize.Parser()
265 self.parser = PyColorize.Parser()
261
266
262 def set_colors(self, scheme):
267 def set_colors(self, scheme):
263 """Shorthand access to the color table scheme selector method."""
268 """Shorthand access to the color table scheme selector method."""
264 self.color_scheme_table.set_active_scheme(scheme)
269 self.color_scheme_table.set_active_scheme(scheme)
265
270
266 def interaction(self, frame, traceback):
271 def interaction(self, frame, traceback):
267 self.shell.set_completer_frame(frame)
272 self.shell.set_completer_frame(frame)
268 while True:
273 while True:
269 try:
274 try:
270 OldPdb.interaction(self, frame, traceback)
275 OldPdb.interaction(self, frame, traceback)
271 except KeyboardInterrupt:
276 except KeyboardInterrupt:
272 self.shell.write("\nKeyboardInterrupt\n")
277 self.shell.write("\nKeyboardInterrupt\n")
273 else:
278 else:
274 break
279 break
275
280
276 def new_do_up(self, arg):
281 def new_do_up(self, arg):
277 OldPdb.do_up(self, arg)
282 OldPdb.do_up(self, arg)
278 self.shell.set_completer_frame(self.curframe)
283 self.shell.set_completer_frame(self.curframe)
279 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
284 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
280
285
281 def new_do_down(self, arg):
286 def new_do_down(self, arg):
282 OldPdb.do_down(self, arg)
287 OldPdb.do_down(self, arg)
283 self.shell.set_completer_frame(self.curframe)
288 self.shell.set_completer_frame(self.curframe)
284
289
285 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
290 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
286
291
287 def new_do_frame(self, arg):
292 def new_do_frame(self, arg):
288 OldPdb.do_frame(self, arg)
293 OldPdb.do_frame(self, arg)
289 self.shell.set_completer_frame(self.curframe)
294 self.shell.set_completer_frame(self.curframe)
290
295
291 def new_do_quit(self, arg):
296 def new_do_quit(self, arg):
292
297
293 if hasattr(self, 'old_all_completions'):
298 if hasattr(self, 'old_all_completions'):
294 self.shell.Completer.all_completions=self.old_all_completions
299 self.shell.Completer.all_completions=self.old_all_completions
295
300
296
301
297 return OldPdb.do_quit(self, arg)
302 return OldPdb.do_quit(self, arg)
298
303
299 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
304 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
300
305
301 def new_do_restart(self, arg):
306 def new_do_restart(self, arg):
302 """Restart command. In the context of ipython this is exactly the same
307 """Restart command. In the context of ipython this is exactly the same
303 thing as 'quit'."""
308 thing as 'quit'."""
304 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
309 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
305 return self.do_quit(arg)
310 return self.do_quit(arg)
306
311
307 def postloop(self):
312 def postloop(self):
308 self.shell.set_completer_frame(None)
313 self.shell.set_completer_frame(None)
309
314
310 def print_stack_trace(self):
315 def print_stack_trace(self):
311 try:
316 try:
312 for frame_lineno in self.stack:
317 for frame_lineno in self.stack:
313 self.print_stack_entry(frame_lineno, context = 5)
318 self.print_stack_entry(frame_lineno, context = 5)
314 except KeyboardInterrupt:
319 except KeyboardInterrupt:
315 pass
320 pass
316
321
317 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
322 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
318 context = 3):
323 context = 3):
319 #frame, lineno = frame_lineno
324 #frame, lineno = frame_lineno
320 print(self.format_stack_entry(frame_lineno, '', context), file=io.stdout)
325 print(self.format_stack_entry(frame_lineno, '', context), file=io.stdout)
321
326
322 # vds: >>
327 # vds: >>
323 frame, lineno = frame_lineno
328 frame, lineno = frame_lineno
324 filename = frame.f_code.co_filename
329 filename = frame.f_code.co_filename
325 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
330 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
326 # vds: <<
331 # vds: <<
327
332
328 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
333 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
329 import repr
334 import repr
330
335
331 ret = []
336 ret = []
332
337
333 Colors = self.color_scheme_table.active_colors
338 Colors = self.color_scheme_table.active_colors
334 ColorsNormal = Colors.Normal
339 ColorsNormal = Colors.Normal
335 tpl_link = u'%s%%s%s' % (Colors.filenameEm, ColorsNormal)
340 tpl_link = u'%s%%s%s' % (Colors.filenameEm, ColorsNormal)
336 tpl_call = u'%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
341 tpl_call = u'%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
337 tpl_line = u'%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
342 tpl_line = u'%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
338 tpl_line_em = u'%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
343 tpl_line_em = u'%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
339 ColorsNormal)
344 ColorsNormal)
340
345
341 frame, lineno = frame_lineno
346 frame, lineno = frame_lineno
342
347
343 return_value = ''
348 return_value = ''
344 if '__return__' in frame.f_locals:
349 if '__return__' in frame.f_locals:
345 rv = frame.f_locals['__return__']
350 rv = frame.f_locals['__return__']
346 #return_value += '->'
351 #return_value += '->'
347 return_value += repr.repr(rv) + '\n'
352 return_value += repr.repr(rv) + '\n'
348 ret.append(return_value)
353 ret.append(return_value)
349
354
350 #s = filename + '(' + `lineno` + ')'
355 #s = filename + '(' + `lineno` + ')'
351 filename = self.canonic(frame.f_code.co_filename)
356 filename = self.canonic(frame.f_code.co_filename)
352 link = tpl_link % py3compat.cast_unicode(filename)
357 link = tpl_link % py3compat.cast_unicode(filename)
353
358
354 if frame.f_code.co_name:
359 if frame.f_code.co_name:
355 func = frame.f_code.co_name
360 func = frame.f_code.co_name
356 else:
361 else:
357 func = "<lambda>"
362 func = "<lambda>"
358
363
359 call = ''
364 call = ''
360 if func != '?':
365 if func != '?':
361 if '__args__' in frame.f_locals:
366 if '__args__' in frame.f_locals:
362 args = repr.repr(frame.f_locals['__args__'])
367 args = repr.repr(frame.f_locals['__args__'])
363 else:
368 else:
364 args = '()'
369 args = '()'
365 call = tpl_call % (func, args)
370 call = tpl_call % (func, args)
366
371
367 # The level info should be generated in the same format pdb uses, to
372 # The level info should be generated in the same format pdb uses, to
368 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
373 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
369 if frame is self.curframe:
374 if frame is self.curframe:
370 ret.append('> ')
375 ret.append('> ')
371 else:
376 else:
372 ret.append(' ')
377 ret.append(' ')
373 ret.append(u'%s(%s)%s\n' % (link,lineno,call))
378 ret.append(u'%s(%s)%s\n' % (link,lineno,call))
374
379
375 start = lineno - 1 - context//2
380 start = lineno - 1 - context//2
376 lines = ulinecache.getlines(filename)
381 lines = ulinecache.getlines(filename)
377 start = min(start, len(lines) - context)
382 start = min(start, len(lines) - context)
378 start = max(start, 0)
383 start = max(start, 0)
379 lines = lines[start : start + context]
384 lines = lines[start : start + context]
380
385
381 for i,line in enumerate(lines):
386 for i,line in enumerate(lines):
382 show_arrow = (start + 1 + i == lineno)
387 show_arrow = (start + 1 + i == lineno)
383 linetpl = (frame is self.curframe or show_arrow) \
388 linetpl = (frame is self.curframe or show_arrow) \
384 and tpl_line_em \
389 and tpl_line_em \
385 or tpl_line
390 or tpl_line
386 ret.append(self.__format_line(linetpl, filename,
391 ret.append(self.__format_line(linetpl, filename,
387 start + 1 + i, line,
392 start + 1 + i, line,
388 arrow = show_arrow) )
393 arrow = show_arrow) )
389 return ''.join(ret)
394 return ''.join(ret)
390
395
391 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
396 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
392 bp_mark = ""
397 bp_mark = ""
393 bp_mark_color = ""
398 bp_mark_color = ""
394
399
395 scheme = self.color_scheme_table.active_scheme_name
400 scheme = self.color_scheme_table.active_scheme_name
396 new_line, err = self.parser.format2(line, 'str', scheme)
401 new_line, err = self.parser.format2(line, 'str', scheme)
397 if not err: line = new_line
402 if not err: line = new_line
398
403
399 bp = None
404 bp = None
400 if lineno in self.get_file_breaks(filename):
405 if lineno in self.get_file_breaks(filename):
401 bps = self.get_breaks(filename, lineno)
406 bps = self.get_breaks(filename, lineno)
402 bp = bps[-1]
407 bp = bps[-1]
403
408
404 if bp:
409 if bp:
405 Colors = self.color_scheme_table.active_colors
410 Colors = self.color_scheme_table.active_colors
406 bp_mark = str(bp.number)
411 bp_mark = str(bp.number)
407 bp_mark_color = Colors.breakpoint_enabled
412 bp_mark_color = Colors.breakpoint_enabled
408 if not bp.enabled:
413 if not bp.enabled:
409 bp_mark_color = Colors.breakpoint_disabled
414 bp_mark_color = Colors.breakpoint_disabled
410
415
411 numbers_width = 7
416 numbers_width = 7
412 if arrow:
417 if arrow:
413 # This is the line with the error
418 # This is the line with the error
414 pad = numbers_width - len(str(lineno)) - len(bp_mark)
419 pad = numbers_width - len(str(lineno)) - len(bp_mark)
415 if pad >= 3:
420 if pad >= 3:
416 marker = '-'*(pad-3) + '-> '
421 marker = '-'*(pad-3) + '-> '
417 elif pad == 2:
422 elif pad == 2:
418 marker = '> '
423 marker = '> '
419 elif pad == 1:
424 elif pad == 1:
420 marker = '>'
425 marker = '>'
421 else:
426 else:
422 marker = ''
427 marker = ''
423 num = '%s%s' % (marker, str(lineno))
428 num = '%s%s' % (marker, str(lineno))
424 line = tpl_line % (bp_mark_color + bp_mark, num, line)
429 line = tpl_line % (bp_mark_color + bp_mark, num, line)
425 else:
430 else:
426 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
431 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
427 line = tpl_line % (bp_mark_color + bp_mark, num, line)
432 line = tpl_line % (bp_mark_color + bp_mark, num, line)
428
433
429 return line
434 return line
430
435
431 def list_command_pydb(self, arg):
436 def list_command_pydb(self, arg):
432 """List command to use if we have a newer pydb installed"""
437 """List command to use if we have a newer pydb installed"""
433 filename, first, last = OldPdb.parse_list_cmd(self, arg)
438 filename, first, last = OldPdb.parse_list_cmd(self, arg)
434 if filename is not None:
439 if filename is not None:
435 self.print_list_lines(filename, first, last)
440 self.print_list_lines(filename, first, last)
436
441
437 def print_list_lines(self, filename, first, last):
442 def print_list_lines(self, filename, first, last):
438 """The printing (as opposed to the parsing part of a 'list'
443 """The printing (as opposed to the parsing part of a 'list'
439 command."""
444 command."""
440 try:
445 try:
441 Colors = self.color_scheme_table.active_colors
446 Colors = self.color_scheme_table.active_colors
442 ColorsNormal = Colors.Normal
447 ColorsNormal = Colors.Normal
443 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
448 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
444 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
449 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
445 src = []
450 src = []
446 if filename == "<string>" and hasattr(self, "_exec_filename"):
451 if filename == "<string>" and hasattr(self, "_exec_filename"):
447 filename = self._exec_filename
452 filename = self._exec_filename
448
453
449 for lineno in range(first, last+1):
454 for lineno in range(first, last+1):
450 line = ulinecache.getline(filename, lineno)
455 line = ulinecache.getline(filename, lineno)
451 if not line:
456 if not line:
452 break
457 break
453
458
454 if lineno == self.curframe.f_lineno:
459 if lineno == self.curframe.f_lineno:
455 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
460 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
456 else:
461 else:
457 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
462 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
458
463
459 src.append(line)
464 src.append(line)
460 self.lineno = lineno
465 self.lineno = lineno
461
466
462 print(''.join(src), file=io.stdout)
467 print(''.join(src), file=io.stdout)
463
468
464 except KeyboardInterrupt:
469 except KeyboardInterrupt:
465 pass
470 pass
466
471
467 def do_list(self, arg):
472 def do_list(self, arg):
468 self.lastcmd = 'list'
473 self.lastcmd = 'list'
469 last = None
474 last = None
470 if arg:
475 if arg:
471 try:
476 try:
472 x = eval(arg, {}, {})
477 x = eval(arg, {}, {})
473 if type(x) == type(()):
478 if type(x) == type(()):
474 first, last = x
479 first, last = x
475 first = int(first)
480 first = int(first)
476 last = int(last)
481 last = int(last)
477 if last < first:
482 if last < first:
478 # Assume it's a count
483 # Assume it's a count
479 last = first + last
484 last = first + last
480 else:
485 else:
481 first = max(1, int(x) - 5)
486 first = max(1, int(x) - 5)
482 except:
487 except:
483 print('*** Error in argument:', repr(arg))
488 print('*** Error in argument:', repr(arg))
484 return
489 return
485 elif self.lineno is None:
490 elif self.lineno is None:
486 first = max(1, self.curframe.f_lineno - 5)
491 first = max(1, self.curframe.f_lineno - 5)
487 else:
492 else:
488 first = self.lineno + 1
493 first = self.lineno + 1
489 if last is None:
494 if last is None:
490 last = first + 10
495 last = first + 10
491 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
496 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
492
497
493 # vds: >>
498 # vds: >>
494 lineno = first
499 lineno = first
495 filename = self.curframe.f_code.co_filename
500 filename = self.curframe.f_code.co_filename
496 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
501 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
497 # vds: <<
502 # vds: <<
498
503
499 do_l = do_list
504 do_l = do_list
500
505
501 def do_pdef(self, arg):
506 def do_pdef(self, arg):
502 """Print the call signature for any callable object.
507 """Print the call signature for any callable object.
503
508
504 The debugger interface to %pdef"""
509 The debugger interface to %pdef"""
505 namespaces = [('Locals', self.curframe.f_locals),
510 namespaces = [('Locals', self.curframe.f_locals),
506 ('Globals', self.curframe.f_globals)]
511 ('Globals', self.curframe.f_globals)]
507 self.shell.find_line_magic('pdef')(arg, namespaces=namespaces)
512 self.shell.find_line_magic('pdef')(arg, namespaces=namespaces)
508
513
509 def do_pdoc(self, arg):
514 def do_pdoc(self, arg):
510 """Print the docstring for an object.
515 """Print the docstring for an object.
511
516
512 The debugger interface to %pdoc."""
517 The debugger interface to %pdoc."""
513 namespaces = [('Locals', self.curframe.f_locals),
518 namespaces = [('Locals', self.curframe.f_locals),
514 ('Globals', self.curframe.f_globals)]
519 ('Globals', self.curframe.f_globals)]
515 self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces)
520 self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces)
516
521
517 def do_pfile(self, arg):
522 def do_pfile(self, arg):
518 """Print (or run through pager) the file where an object is defined.
523 """Print (or run through pager) the file where an object is defined.
519
524
520 The debugger interface to %pfile.
525 The debugger interface to %pfile.
521 """
526 """
522 namespaces = [('Locals', self.curframe.f_locals),
527 namespaces = [('Locals', self.curframe.f_locals),
523 ('Globals', self.curframe.f_globals)]
528 ('Globals', self.curframe.f_globals)]
524 self.shell.find_line_magic('pfile')(arg, namespaces=namespaces)
529 self.shell.find_line_magic('pfile')(arg, namespaces=namespaces)
525
530
526 def do_pinfo(self, arg):
531 def do_pinfo(self, arg):
527 """Provide detailed information about an object.
532 """Provide detailed information about an object.
528
533
529 The debugger interface to %pinfo, i.e., obj?."""
534 The debugger interface to %pinfo, i.e., obj?."""
530 namespaces = [('Locals', self.curframe.f_locals),
535 namespaces = [('Locals', self.curframe.f_locals),
531 ('Globals', self.curframe.f_globals)]
536 ('Globals', self.curframe.f_globals)]
532 self.shell.find_line_magic('pinfo')(arg, namespaces=namespaces)
537 self.shell.find_line_magic('pinfo')(arg, namespaces=namespaces)
533
538
534 def do_pinfo2(self, arg):
539 def do_pinfo2(self, arg):
535 """Provide extra detailed information about an object.
540 """Provide extra detailed information about an object.
536
541
537 The debugger interface to %pinfo2, i.e., obj??."""
542 The debugger interface to %pinfo2, i.e., obj??."""
538 namespaces = [('Locals', self.curframe.f_locals),
543 namespaces = [('Locals', self.curframe.f_locals),
539 ('Globals', self.curframe.f_globals)]
544 ('Globals', self.curframe.f_globals)]
540 self.shell.find_line_magic('pinfo2')(arg, namespaces=namespaces)
545 self.shell.find_line_magic('pinfo2')(arg, namespaces=namespaces)
541
546
542 def do_psource(self, arg):
547 def do_psource(self, arg):
543 """Print (or run through pager) the source code for an object."""
548 """Print (or run through pager) the source code for an object."""
544 namespaces = [('Locals', self.curframe.f_locals),
549 namespaces = [('Locals', self.curframe.f_locals),
545 ('Globals', self.curframe.f_globals)]
550 ('Globals', self.curframe.f_globals)]
546 self.shell.find_line_magic('psource')(arg, namespaces=namespaces)
551 self.shell.find_line_magic('psource')(arg, namespaces=namespaces)
547
552
548 def checkline(self, filename, lineno):
553 def checkline(self, filename, lineno):
549 """Check whether specified line seems to be executable.
554 """Check whether specified line seems to be executable.
550
555
551 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
556 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
552 line or EOF). Warning: testing is not comprehensive.
557 line or EOF). Warning: testing is not comprehensive.
553 """
558 """
554 #######################################################################
559 #######################################################################
555 # XXX Hack! Use python-2.5 compatible code for this call, because with
560 # XXX Hack! Use python-2.5 compatible code for this call, because with
556 # all of our changes, we've drifted from the pdb api in 2.6. For now,
561 # all of our changes, we've drifted from the pdb api in 2.6. For now,
557 # changing:
562 # changing:
558 #
563 #
559 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
564 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
560 # to:
565 # to:
561 #
566 #
562 line = linecache.getline(filename, lineno)
567 line = linecache.getline(filename, lineno)
563 #
568 #
564 # does the trick. But in reality, we need to fix this by reconciling
569 # does the trick. But in reality, we need to fix this by reconciling
565 # our updates with the new Pdb APIs in Python 2.6.
570 # our updates with the new Pdb APIs in Python 2.6.
566 #
571 #
567 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
572 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
568 #######################################################################
573 #######################################################################
569
574
570 if not line:
575 if not line:
571 print('End of file', file=self.stdout)
576 print('End of file', file=self.stdout)
572 return 0
577 return 0
573 line = line.strip()
578 line = line.strip()
574 # Don't allow setting breakpoint at a blank line
579 # Don't allow setting breakpoint at a blank line
575 if (not line or (line[0] == '#') or
580 if (not line or (line[0] == '#') or
576 (line[:3] == '"""') or line[:3] == "'''"):
581 (line[:3] == '"""') or line[:3] == "'''"):
577 print('*** Blank or comment', file=self.stdout)
582 print('*** Blank or comment', file=self.stdout)
578 return 0
583 return 0
579 return lineno
584 return lineno
@@ -1,3150 +1,3151 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-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 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 absolute_import
17 from __future__ import absolute_import
18 from __future__ import print_function
18 from __future__ import print_function
19
19
20 import __builtin__ as builtin_mod
20 import __builtin__ as builtin_mod
21 import __future__
21 import __future__
22 import abc
22 import abc
23 import ast
23 import ast
24 import atexit
24 import atexit
25 import functools
25 import functools
26 import os
26 import os
27 import re
27 import re
28 import runpy
28 import runpy
29 import sys
29 import sys
30 import tempfile
30 import tempfile
31 import types
31 import types
32 from io import open as io_open
32 from io import open as io_open
33
33
34 from IPython.config.configurable import SingletonConfigurable
34 from IPython.config.configurable import SingletonConfigurable
35 from IPython.core import debugger, oinspect
35 from IPython.core import debugger, oinspect
36 from IPython.core import magic
36 from IPython.core import magic
37 from IPython.core import page
37 from IPython.core import page
38 from IPython.core import prefilter
38 from IPython.core import prefilter
39 from IPython.core import shadowns
39 from IPython.core import shadowns
40 from IPython.core import ultratb
40 from IPython.core import ultratb
41 from IPython.core.alias import AliasManager, AliasError
41 from IPython.core.alias import AliasManager, AliasError
42 from IPython.core.autocall import ExitAutocall
42 from IPython.core.autocall import ExitAutocall
43 from IPython.core.builtin_trap import BuiltinTrap
43 from IPython.core.builtin_trap import BuiltinTrap
44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
45 from IPython.core.display_trap import DisplayTrap
45 from IPython.core.display_trap import DisplayTrap
46 from IPython.core.displayhook import DisplayHook
46 from IPython.core.displayhook import DisplayHook
47 from IPython.core.displaypub import DisplayPublisher
47 from IPython.core.displaypub import DisplayPublisher
48 from IPython.core.error import UsageError
48 from IPython.core.error import UsageError
49 from IPython.core.extensions import ExtensionManager
49 from IPython.core.extensions import ExtensionManager
50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
50 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
51 from IPython.core.formatters import DisplayFormatter
51 from IPython.core.formatters import DisplayFormatter
52 from IPython.core.history import HistoryManager
52 from IPython.core.history import HistoryManager
53 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
53 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
54 from IPython.core.logger import Logger
54 from IPython.core.logger import Logger
55 from IPython.core.macro import Macro
55 from IPython.core.macro import Macro
56 from IPython.core.payload import PayloadManager
56 from IPython.core.payload import PayloadManager
57 from IPython.core.prefilter import PrefilterManager
57 from IPython.core.prefilter import PrefilterManager
58 from IPython.core.profiledir import ProfileDir
58 from IPython.core.profiledir import ProfileDir
59 from IPython.core.prompts import PromptManager
59 from IPython.core.prompts import PromptManager
60 from IPython.lib.latextools import LaTeXTool
60 from IPython.lib.latextools import LaTeXTool
61 from IPython.testing.skipdoctest import skip_doctest
61 from IPython.testing.skipdoctest import skip_doctest
62 from IPython.utils import PyColorize
62 from IPython.utils import PyColorize
63 from IPython.utils import io
63 from IPython.utils import io
64 from IPython.utils import py3compat
64 from IPython.utils import py3compat
65 from IPython.utils import openpy
65 from IPython.utils import openpy
66 from IPython.utils.decorators import undoc
66 from IPython.utils.decorators import undoc
67 from IPython.utils.io import ask_yes_no
67 from IPython.utils.io import ask_yes_no
68 from IPython.utils.ipstruct import Struct
68 from IPython.utils.ipstruct import Struct
69 from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename
69 from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename
70 from IPython.utils.pickleshare import PickleShareDB
70 from IPython.utils.pickleshare import PickleShareDB
71 from IPython.utils.process import system, getoutput
71 from IPython.utils.process import system, getoutput
72 from IPython.utils.strdispatch import StrDispatch
72 from IPython.utils.strdispatch import StrDispatch
73 from IPython.utils.syspathcontext import prepended_to_syspath
73 from IPython.utils.syspathcontext import prepended_to_syspath
74 from IPython.utils.text import (format_screen, LSString, SList,
74 from IPython.utils.text import (format_screen, LSString, SList,
75 DollarFormatter)
75 DollarFormatter)
76 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
76 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
77 List, Unicode, Instance, Type)
77 List, Unicode, Instance, Type)
78 from IPython.utils.warn import warn, error
78 from IPython.utils.warn import warn, error
79 import IPython.core.hooks
79 import IPython.core.hooks
80
80
81 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
82 # Globals
82 # Globals
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84
84
85 # compiled regexps for autoindent management
85 # compiled regexps for autoindent management
86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
87
87
88 #-----------------------------------------------------------------------------
88 #-----------------------------------------------------------------------------
89 # Utilities
89 # Utilities
90 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
91
91
92 @undoc
92 @undoc
93 def softspace(file, newvalue):
93 def softspace(file, newvalue):
94 """Copied from code.py, to remove the dependency"""
94 """Copied from code.py, to remove the dependency"""
95
95
96 oldvalue = 0
96 oldvalue = 0
97 try:
97 try:
98 oldvalue = file.softspace
98 oldvalue = file.softspace
99 except AttributeError:
99 except AttributeError:
100 pass
100 pass
101 try:
101 try:
102 file.softspace = newvalue
102 file.softspace = newvalue
103 except (AttributeError, TypeError):
103 except (AttributeError, TypeError):
104 # "attribute-less object" or "read-only attributes"
104 # "attribute-less object" or "read-only attributes"
105 pass
105 pass
106 return oldvalue
106 return oldvalue
107
107
108 @undoc
108 @undoc
109 def no_op(*a, **kw): pass
109 def no_op(*a, **kw): pass
110
110
111 @undoc
111 @undoc
112 class NoOpContext(object):
112 class NoOpContext(object):
113 def __enter__(self): pass
113 def __enter__(self): pass
114 def __exit__(self, type, value, traceback): pass
114 def __exit__(self, type, value, traceback): pass
115 no_op_context = NoOpContext()
115 no_op_context = NoOpContext()
116
116
117 class SpaceInInput(Exception): pass
117 class SpaceInInput(Exception): pass
118
118
119 @undoc
119 @undoc
120 class Bunch: pass
120 class Bunch: pass
121
121
122
122
123 def get_default_colors():
123 def get_default_colors():
124 if sys.platform=='darwin':
124 if sys.platform=='darwin':
125 return "LightBG"
125 return "LightBG"
126 elif os.name=='nt':
126 elif os.name=='nt':
127 return 'Linux'
127 return 'Linux'
128 else:
128 else:
129 return 'Linux'
129 return 'Linux'
130
130
131
131
132 class SeparateUnicode(Unicode):
132 class SeparateUnicode(Unicode):
133 """A Unicode subclass to validate separate_in, separate_out, etc.
133 """A Unicode subclass to validate separate_in, separate_out, etc.
134
134
135 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
135 This is a Unicode based trait that converts '0'->'' and '\\n'->'\n'.
136 """
136 """
137
137
138 def validate(self, obj, value):
138 def validate(self, obj, value):
139 if value == '0': value = ''
139 if value == '0': value = ''
140 value = value.replace('\\n','\n')
140 value = value.replace('\\n','\n')
141 return super(SeparateUnicode, self).validate(obj, value)
141 return super(SeparateUnicode, self).validate(obj, value)
142
142
143
143
144 class ReadlineNoRecord(object):
144 class ReadlineNoRecord(object):
145 """Context manager to execute some code, then reload readline history
145 """Context manager to execute some code, then reload readline history
146 so that interactive input to the code doesn't appear when pressing up."""
146 so that interactive input to the code doesn't appear when pressing up."""
147 def __init__(self, shell):
147 def __init__(self, shell):
148 self.shell = shell
148 self.shell = shell
149 self._nested_level = 0
149 self._nested_level = 0
150
150
151 def __enter__(self):
151 def __enter__(self):
152 if self._nested_level == 0:
152 if self._nested_level == 0:
153 try:
153 try:
154 self.orig_length = self.current_length()
154 self.orig_length = self.current_length()
155 self.readline_tail = self.get_readline_tail()
155 self.readline_tail = self.get_readline_tail()
156 except (AttributeError, IndexError): # Can fail with pyreadline
156 except (AttributeError, IndexError): # Can fail with pyreadline
157 self.orig_length, self.readline_tail = 999999, []
157 self.orig_length, self.readline_tail = 999999, []
158 self._nested_level += 1
158 self._nested_level += 1
159
159
160 def __exit__(self, type, value, traceback):
160 def __exit__(self, type, value, traceback):
161 self._nested_level -= 1
161 self._nested_level -= 1
162 if self._nested_level == 0:
162 if self._nested_level == 0:
163 # Try clipping the end if it's got longer
163 # Try clipping the end if it's got longer
164 try:
164 try:
165 e = self.current_length() - self.orig_length
165 e = self.current_length() - self.orig_length
166 if e > 0:
166 if e > 0:
167 for _ in range(e):
167 for _ in range(e):
168 self.shell.readline.remove_history_item(self.orig_length)
168 self.shell.readline.remove_history_item(self.orig_length)
169
169
170 # If it still doesn't match, just reload readline history.
170 # If it still doesn't match, just reload readline history.
171 if self.current_length() != self.orig_length \
171 if self.current_length() != self.orig_length \
172 or self.get_readline_tail() != self.readline_tail:
172 or self.get_readline_tail() != self.readline_tail:
173 self.shell.refill_readline_hist()
173 self.shell.refill_readline_hist()
174 except (AttributeError, IndexError):
174 except (AttributeError, IndexError):
175 pass
175 pass
176 # Returning False will cause exceptions to propagate
176 # Returning False will cause exceptions to propagate
177 return False
177 return False
178
178
179 def current_length(self):
179 def current_length(self):
180 return self.shell.readline.get_current_history_length()
180 return self.shell.readline.get_current_history_length()
181
181
182 def get_readline_tail(self, n=10):
182 def get_readline_tail(self, n=10):
183 """Get the last n items in readline history."""
183 """Get the last n items in readline history."""
184 end = self.shell.readline.get_current_history_length() + 1
184 end = self.shell.readline.get_current_history_length() + 1
185 start = max(end-n, 1)
185 start = max(end-n, 1)
186 ghi = self.shell.readline.get_history_item
186 ghi = self.shell.readline.get_history_item
187 return [ghi(x) for x in range(start, end)]
187 return [ghi(x) for x in range(start, end)]
188
188
189
189
190 @undoc
190 @undoc
191 class DummyMod(object):
191 class DummyMod(object):
192 """A dummy module used for IPython's interactive module when
192 """A dummy module used for IPython's interactive module when
193 a namespace must be assigned to the module's __dict__."""
193 a namespace must be assigned to the module's __dict__."""
194 pass
194 pass
195
195
196 #-----------------------------------------------------------------------------
196 #-----------------------------------------------------------------------------
197 # Main IPython class
197 # Main IPython class
198 #-----------------------------------------------------------------------------
198 #-----------------------------------------------------------------------------
199
199
200 class InteractiveShell(SingletonConfigurable):
200 class InteractiveShell(SingletonConfigurable):
201 """An enhanced, interactive shell for Python."""
201 """An enhanced, interactive shell for Python."""
202
202
203 _instance = None
203 _instance = None
204
204
205 ast_transformers = List([], config=True, help=
205 ast_transformers = List([], config=True, help=
206 """
206 """
207 A list of ast.NodeTransformer subclass instances, which will be applied
207 A list of ast.NodeTransformer subclass instances, which will be applied
208 to user input before code is run.
208 to user input before code is run.
209 """
209 """
210 )
210 )
211
211
212 autocall = Enum((0,1,2), default_value=0, config=True, help=
212 autocall = Enum((0,1,2), default_value=0, config=True, help=
213 """
213 """
214 Make IPython automatically call any callable object even if you didn't
214 Make IPython automatically call any callable object even if you didn't
215 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
215 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
216 automatically. The value can be '0' to disable the feature, '1' for
216 automatically. The value can be '0' to disable the feature, '1' for
217 'smart' autocall, where it is not applied if there are no more
217 'smart' autocall, where it is not applied if there are no more
218 arguments on the line, and '2' for 'full' autocall, where all callable
218 arguments on the line, and '2' for 'full' autocall, where all callable
219 objects are automatically called (even if no arguments are present).
219 objects are automatically called (even if no arguments are present).
220 """
220 """
221 )
221 )
222 # TODO: remove all autoindent logic and put into frontends.
222 # TODO: remove all autoindent logic and put into frontends.
223 # We can't do this yet because even runlines uses the autoindent.
223 # We can't do this yet because even runlines uses the autoindent.
224 autoindent = CBool(True, config=True, help=
224 autoindent = CBool(True, config=True, help=
225 """
225 """
226 Autoindent IPython code entered interactively.
226 Autoindent IPython code entered interactively.
227 """
227 """
228 )
228 )
229 automagic = CBool(True, config=True, help=
229 automagic = CBool(True, config=True, help=
230 """
230 """
231 Enable magic commands to be called without the leading %.
231 Enable magic commands to be called without the leading %.
232 """
232 """
233 )
233 )
234 cache_size = Integer(1000, config=True, help=
234 cache_size = Integer(1000, config=True, help=
235 """
235 """
236 Set the size of the output cache. The default is 1000, you can
236 Set the size of the output cache. The default is 1000, you can
237 change it permanently in your config file. Setting it to 0 completely
237 change it permanently in your config file. Setting it to 0 completely
238 disables the caching system, and the minimum value accepted is 20 (if
238 disables the caching system, and the minimum value accepted is 20 (if
239 you provide a value less than 20, it is reset to 0 and a warning is
239 you provide a value less than 20, it is reset to 0 and a warning is
240 issued). This limit is defined because otherwise you'll spend more
240 issued). This limit is defined because otherwise you'll spend more
241 time re-flushing a too small cache than working
241 time re-flushing a too small cache than working
242 """
242 """
243 )
243 )
244 color_info = CBool(True, config=True, help=
244 color_info = CBool(True, config=True, help=
245 """
245 """
246 Use colors for displaying information about objects. Because this
246 Use colors for displaying information about objects. Because this
247 information is passed through a pager (like 'less'), and some pagers
247 information is passed through a pager (like 'less'), and some pagers
248 get confused with color codes, this capability can be turned off.
248 get confused with color codes, this capability can be turned off.
249 """
249 """
250 )
250 )
251 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
251 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
252 default_value=get_default_colors(), config=True,
252 default_value=get_default_colors(), config=True,
253 help="Set the color scheme (NoColor, Linux, or LightBG)."
253 help="Set the color scheme (NoColor, Linux, or LightBG)."
254 )
254 )
255 colors_force = CBool(False, help=
255 colors_force = CBool(False, help=
256 """
256 """
257 Force use of ANSI color codes, regardless of OS and readline
257 Force use of ANSI color codes, regardless of OS and readline
258 availability.
258 availability.
259 """
259 """
260 # FIXME: This is essentially a hack to allow ZMQShell to show colors
260 # FIXME: This is essentially a hack to allow ZMQShell to show colors
261 # without readline on Win32. When the ZMQ formatting system is
261 # without readline on Win32. When the ZMQ formatting system is
262 # refactored, this should be removed.
262 # refactored, this should be removed.
263 )
263 )
264 debug = CBool(False, config=True)
264 debug = CBool(False, config=True)
265 deep_reload = CBool(False, config=True, help=
265 deep_reload = CBool(False, config=True, help=
266 """
266 """
267 Enable deep (recursive) reloading by default. IPython can use the
267 Enable deep (recursive) reloading by default. IPython can use the
268 deep_reload module which reloads changes in modules recursively (it
268 deep_reload module which reloads changes in modules recursively (it
269 replaces the reload() function, so you don't need to change anything to
269 replaces the reload() function, so you don't need to change anything to
270 use it). deep_reload() forces a full reload of modules whose code may
270 use it). deep_reload() forces a full reload of modules whose code may
271 have changed, which the default reload() function does not. When
271 have changed, which the default reload() function does not. When
272 deep_reload is off, IPython will use the normal reload(), but
272 deep_reload is off, IPython will use the normal reload(), but
273 deep_reload will still be available as dreload().
273 deep_reload will still be available as dreload().
274 """
274 """
275 )
275 )
276 disable_failing_post_execute = CBool(False, config=True,
276 disable_failing_post_execute = CBool(False, config=True,
277 help="Don't call post-execute functions that have failed in the past."
277 help="Don't call post-execute functions that have failed in the past."
278 )
278 )
279 display_formatter = Instance(DisplayFormatter)
279 display_formatter = Instance(DisplayFormatter)
280 displayhook_class = Type(DisplayHook)
280 displayhook_class = Type(DisplayHook)
281 display_pub_class = Type(DisplayPublisher)
281 display_pub_class = Type(DisplayPublisher)
282 data_pub_class = None
282 data_pub_class = None
283
283
284 exit_now = CBool(False)
284 exit_now = CBool(False)
285 exiter = Instance(ExitAutocall)
285 exiter = Instance(ExitAutocall)
286 def _exiter_default(self):
286 def _exiter_default(self):
287 return ExitAutocall(self)
287 return ExitAutocall(self)
288 # Monotonically increasing execution counter
288 # Monotonically increasing execution counter
289 execution_count = Integer(1)
289 execution_count = Integer(1)
290 filename = Unicode("<ipython console>")
290 filename = Unicode("<ipython console>")
291 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
291 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
292
292
293 # Input splitter, to transform input line by line and detect when a block
293 # Input splitter, to transform input line by line and detect when a block
294 # is ready to be executed.
294 # is ready to be executed.
295 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
295 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
296 (), {'line_input_checker': True})
296 (), {'line_input_checker': True})
297
297
298 # This InputSplitter instance is used to transform completed cells before
298 # This InputSplitter instance is used to transform completed cells before
299 # running them. It allows cell magics to contain blank lines.
299 # running them. It allows cell magics to contain blank lines.
300 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
300 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
301 (), {'line_input_checker': False})
301 (), {'line_input_checker': False})
302
302
303 logstart = CBool(False, config=True, help=
303 logstart = CBool(False, config=True, help=
304 """
304 """
305 Start logging to the default log file.
305 Start logging to the default log file.
306 """
306 """
307 )
307 )
308 logfile = Unicode('', config=True, help=
308 logfile = Unicode('', config=True, help=
309 """
309 """
310 The name of the logfile to use.
310 The name of the logfile to use.
311 """
311 """
312 )
312 )
313 logappend = Unicode('', config=True, help=
313 logappend = Unicode('', config=True, help=
314 """
314 """
315 Start logging to the given file in append mode.
315 Start logging to the given file in append mode.
316 """
316 """
317 )
317 )
318 object_info_string_level = Enum((0,1,2), default_value=0,
318 object_info_string_level = Enum((0,1,2), default_value=0,
319 config=True)
319 config=True)
320 pdb = CBool(False, config=True, help=
320 pdb = CBool(False, config=True, help=
321 """
321 """
322 Automatically call the pdb debugger after every exception.
322 Automatically call the pdb debugger after every exception.
323 """
323 """
324 )
324 )
325 multiline_history = CBool(sys.platform != 'win32', config=True,
325 multiline_history = CBool(sys.platform != 'win32', config=True,
326 help="Save multi-line entries as one entry in readline history"
326 help="Save multi-line entries as one entry in readline history"
327 )
327 )
328
328
329 # deprecated prompt traits:
329 # deprecated prompt traits:
330
330
331 prompt_in1 = Unicode('In [\\#]: ', config=True,
331 prompt_in1 = Unicode('In [\\#]: ', config=True,
332 help="Deprecated, use PromptManager.in_template")
332 help="Deprecated, use PromptManager.in_template")
333 prompt_in2 = Unicode(' .\\D.: ', config=True,
333 prompt_in2 = Unicode(' .\\D.: ', config=True,
334 help="Deprecated, use PromptManager.in2_template")
334 help="Deprecated, use PromptManager.in2_template")
335 prompt_out = Unicode('Out[\\#]: ', config=True,
335 prompt_out = Unicode('Out[\\#]: ', config=True,
336 help="Deprecated, use PromptManager.out_template")
336 help="Deprecated, use PromptManager.out_template")
337 prompts_pad_left = CBool(True, config=True,
337 prompts_pad_left = CBool(True, config=True,
338 help="Deprecated, use PromptManager.justify")
338 help="Deprecated, use PromptManager.justify")
339
339
340 def _prompt_trait_changed(self, name, old, new):
340 def _prompt_trait_changed(self, name, old, new):
341 table = {
341 table = {
342 'prompt_in1' : 'in_template',
342 'prompt_in1' : 'in_template',
343 'prompt_in2' : 'in2_template',
343 'prompt_in2' : 'in2_template',
344 'prompt_out' : 'out_template',
344 'prompt_out' : 'out_template',
345 'prompts_pad_left' : 'justify',
345 'prompts_pad_left' : 'justify',
346 }
346 }
347 warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format(
347 warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format(
348 name=name, newname=table[name])
348 name=name, newname=table[name])
349 )
349 )
350 # protect against weird cases where self.config may not exist:
350 # protect against weird cases where self.config may not exist:
351 if self.config is not None:
351 if self.config is not None:
352 # propagate to corresponding PromptManager trait
352 # propagate to corresponding PromptManager trait
353 setattr(self.config.PromptManager, table[name], new)
353 setattr(self.config.PromptManager, table[name], new)
354
354
355 _prompt_in1_changed = _prompt_trait_changed
355 _prompt_in1_changed = _prompt_trait_changed
356 _prompt_in2_changed = _prompt_trait_changed
356 _prompt_in2_changed = _prompt_trait_changed
357 _prompt_out_changed = _prompt_trait_changed
357 _prompt_out_changed = _prompt_trait_changed
358 _prompt_pad_left_changed = _prompt_trait_changed
358 _prompt_pad_left_changed = _prompt_trait_changed
359
359
360 show_rewritten_input = CBool(True, config=True,
360 show_rewritten_input = CBool(True, config=True,
361 help="Show rewritten input, e.g. for autocall."
361 help="Show rewritten input, e.g. for autocall."
362 )
362 )
363
363
364 quiet = CBool(False, config=True)
364 quiet = CBool(False, config=True)
365
365
366 history_length = Integer(10000, config=True)
366 history_length = Integer(10000, config=True)
367
367
368 # The readline stuff will eventually be moved to the terminal subclass
368 # The readline stuff will eventually be moved to the terminal subclass
369 # but for now, we can't do that as readline is welded in everywhere.
369 # but for now, we can't do that as readline is welded in everywhere.
370 readline_use = CBool(True, config=True)
370 readline_use = CBool(True, config=True)
371 readline_remove_delims = Unicode('-/~', config=True)
371 readline_remove_delims = Unicode('-/~', config=True)
372 readline_delims = Unicode() # set by init_readline()
372 readline_delims = Unicode() # set by init_readline()
373 # don't use \M- bindings by default, because they
373 # don't use \M- bindings by default, because they
374 # conflict with 8-bit encodings. See gh-58,gh-88
374 # conflict with 8-bit encodings. See gh-58,gh-88
375 readline_parse_and_bind = List([
375 readline_parse_and_bind = List([
376 'tab: complete',
376 'tab: complete',
377 '"\C-l": clear-screen',
377 '"\C-l": clear-screen',
378 'set show-all-if-ambiguous on',
378 'set show-all-if-ambiguous on',
379 '"\C-o": tab-insert',
379 '"\C-o": tab-insert',
380 '"\C-r": reverse-search-history',
380 '"\C-r": reverse-search-history',
381 '"\C-s": forward-search-history',
381 '"\C-s": forward-search-history',
382 '"\C-p": history-search-backward',
382 '"\C-p": history-search-backward',
383 '"\C-n": history-search-forward',
383 '"\C-n": history-search-forward',
384 '"\e[A": history-search-backward',
384 '"\e[A": history-search-backward',
385 '"\e[B": history-search-forward',
385 '"\e[B": history-search-forward',
386 '"\C-k": kill-line',
386 '"\C-k": kill-line',
387 '"\C-u": unix-line-discard',
387 '"\C-u": unix-line-discard',
388 ], allow_none=False, config=True)
388 ], allow_none=False, config=True)
389
389
390 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
390 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
391 default_value='last_expr', config=True,
391 default_value='last_expr', config=True,
392 help="""
392 help="""
393 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
393 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
394 run interactively (displaying output from expressions).""")
394 run interactively (displaying output from expressions).""")
395
395
396 # TODO: this part of prompt management should be moved to the frontends.
396 # TODO: this part of prompt management should be moved to the frontends.
397 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
397 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
398 separate_in = SeparateUnicode('\n', config=True)
398 separate_in = SeparateUnicode('\n', config=True)
399 separate_out = SeparateUnicode('', config=True)
399 separate_out = SeparateUnicode('', config=True)
400 separate_out2 = SeparateUnicode('', config=True)
400 separate_out2 = SeparateUnicode('', config=True)
401 wildcards_case_sensitive = CBool(True, config=True)
401 wildcards_case_sensitive = CBool(True, config=True)
402 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
402 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
403 default_value='Context', config=True)
403 default_value='Context', config=True)
404
404
405 # Subcomponents of InteractiveShell
405 # Subcomponents of InteractiveShell
406 alias_manager = Instance('IPython.core.alias.AliasManager')
406 alias_manager = Instance('IPython.core.alias.AliasManager')
407 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
407 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
408 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
408 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
409 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
409 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
410 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
410 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
411 payload_manager = Instance('IPython.core.payload.PayloadManager')
411 payload_manager = Instance('IPython.core.payload.PayloadManager')
412 history_manager = Instance('IPython.core.history.HistoryManager')
412 history_manager = Instance('IPython.core.history.HistoryManager')
413 magics_manager = Instance('IPython.core.magic.MagicsManager')
413 magics_manager = Instance('IPython.core.magic.MagicsManager')
414
414
415 profile_dir = Instance('IPython.core.application.ProfileDir')
415 profile_dir = Instance('IPython.core.application.ProfileDir')
416 @property
416 @property
417 def profile(self):
417 def profile(self):
418 if self.profile_dir is not None:
418 if self.profile_dir is not None:
419 name = os.path.basename(self.profile_dir.location)
419 name = os.path.basename(self.profile_dir.location)
420 return name.replace('profile_','')
420 return name.replace('profile_','')
421
421
422
422
423 # Private interface
423 # Private interface
424 _post_execute = Instance(dict)
424 _post_execute = Instance(dict)
425
425
426 # Tracks any GUI loop loaded for pylab
426 # Tracks any GUI loop loaded for pylab
427 pylab_gui_select = None
427 pylab_gui_select = None
428
428
429 def __init__(self, ipython_dir=None, profile_dir=None,
429 def __init__(self, ipython_dir=None, profile_dir=None,
430 user_module=None, user_ns=None,
430 user_module=None, user_ns=None,
431 custom_exceptions=((), None), **kwargs):
431 custom_exceptions=((), None), **kwargs):
432
432
433 # This is where traits with a config_key argument are updated
433 # This is where traits with a config_key argument are updated
434 # from the values on config.
434 # from the values on config.
435 super(InteractiveShell, self).__init__(**kwargs)
435 super(InteractiveShell, self).__init__(**kwargs)
436 self.configurables = [self]
436 self.configurables = [self]
437
437
438 # These are relatively independent and stateless
438 # These are relatively independent and stateless
439 self.init_ipython_dir(ipython_dir)
439 self.init_ipython_dir(ipython_dir)
440 self.init_profile_dir(profile_dir)
440 self.init_profile_dir(profile_dir)
441 self.init_instance_attrs()
441 self.init_instance_attrs()
442 self.init_environment()
442 self.init_environment()
443
443
444 # Check if we're in a virtualenv, and set up sys.path.
444 # Check if we're in a virtualenv, and set up sys.path.
445 self.init_virtualenv()
445 self.init_virtualenv()
446
446
447 # Create namespaces (user_ns, user_global_ns, etc.)
447 # Create namespaces (user_ns, user_global_ns, etc.)
448 self.init_create_namespaces(user_module, user_ns)
448 self.init_create_namespaces(user_module, user_ns)
449 # This has to be done after init_create_namespaces because it uses
449 # This has to be done after init_create_namespaces because it uses
450 # something in self.user_ns, but before init_sys_modules, which
450 # something in self.user_ns, but before init_sys_modules, which
451 # is the first thing to modify sys.
451 # is the first thing to modify sys.
452 # TODO: When we override sys.stdout and sys.stderr before this class
452 # TODO: When we override sys.stdout and sys.stderr before this class
453 # is created, we are saving the overridden ones here. Not sure if this
453 # is created, we are saving the overridden ones here. Not sure if this
454 # is what we want to do.
454 # is what we want to do.
455 self.save_sys_module_state()
455 self.save_sys_module_state()
456 self.init_sys_modules()
456 self.init_sys_modules()
457
457
458 # While we're trying to have each part of the code directly access what
458 # While we're trying to have each part of the code directly access what
459 # it needs without keeping redundant references to objects, we have too
459 # it needs without keeping redundant references to objects, we have too
460 # much legacy code that expects ip.db to exist.
460 # much legacy code that expects ip.db to exist.
461 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
461 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
462
462
463 self.init_history()
463 self.init_history()
464 self.init_encoding()
464 self.init_encoding()
465 self.init_prefilter()
465 self.init_prefilter()
466
466
467 self.init_syntax_highlighting()
467 self.init_syntax_highlighting()
468 self.init_hooks()
468 self.init_hooks()
469 self.init_pushd_popd_magic()
469 self.init_pushd_popd_magic()
470 # self.init_traceback_handlers use to be here, but we moved it below
470 # self.init_traceback_handlers use to be here, but we moved it below
471 # because it and init_io have to come after init_readline.
471 # because it and init_io have to come after init_readline.
472 self.init_user_ns()
472 self.init_user_ns()
473 self.init_logger()
473 self.init_logger()
474 self.init_alias()
474 self.init_alias()
475 self.init_builtins()
475 self.init_builtins()
476
476
477 # The following was in post_config_initialization
477 # The following was in post_config_initialization
478 self.init_inspector()
478 self.init_inspector()
479 # init_readline() must come before init_io(), because init_io uses
479 # init_readline() must come before init_io(), because init_io uses
480 # readline related things.
480 # readline related things.
481 self.init_readline()
481 self.init_readline()
482 # We save this here in case user code replaces raw_input, but it needs
482 # We save this here in case user code replaces raw_input, but it needs
483 # to be after init_readline(), because PyPy's readline works by replacing
483 # to be after init_readline(), because PyPy's readline works by replacing
484 # raw_input.
484 # raw_input.
485 if py3compat.PY3:
485 if py3compat.PY3:
486 self.raw_input_original = input
486 self.raw_input_original = input
487 else:
487 else:
488 self.raw_input_original = raw_input
488 self.raw_input_original = raw_input
489 # init_completer must come after init_readline, because it needs to
489 # init_completer must come after init_readline, because it needs to
490 # know whether readline is present or not system-wide to configure the
490 # know whether readline is present or not system-wide to configure the
491 # completers, since the completion machinery can now operate
491 # completers, since the completion machinery can now operate
492 # independently of readline (e.g. over the network)
492 # independently of readline (e.g. over the network)
493 self.init_completer()
493 self.init_completer()
494 # TODO: init_io() needs to happen before init_traceback handlers
494 # TODO: init_io() needs to happen before init_traceback handlers
495 # because the traceback handlers hardcode the stdout/stderr streams.
495 # because the traceback handlers hardcode the stdout/stderr streams.
496 # This logic in in debugger.Pdb and should eventually be changed.
496 # This logic in in debugger.Pdb and should eventually be changed.
497 self.init_io()
497 self.init_io()
498 self.init_traceback_handlers(custom_exceptions)
498 self.init_traceback_handlers(custom_exceptions)
499 self.init_prompts()
499 self.init_prompts()
500 self.init_display_formatter()
500 self.init_display_formatter()
501 self.init_display_pub()
501 self.init_display_pub()
502 self.init_data_pub()
502 self.init_data_pub()
503 self.init_displayhook()
503 self.init_displayhook()
504 self.init_latextool()
504 self.init_latextool()
505 self.init_magics()
505 self.init_magics()
506 self.init_logstart()
506 self.init_logstart()
507 self.init_pdb()
507 self.init_pdb()
508 self.init_extension_manager()
508 self.init_extension_manager()
509 self.init_payload()
509 self.init_payload()
510 self.hooks.late_startup_hook()
510 self.hooks.late_startup_hook()
511 atexit.register(self.atexit_operations)
511 atexit.register(self.atexit_operations)
512
512
513 def get_ipython(self):
513 def get_ipython(self):
514 """Return the currently running IPython instance."""
514 """Return the currently running IPython instance."""
515 return self
515 return self
516
516
517 #-------------------------------------------------------------------------
517 #-------------------------------------------------------------------------
518 # Trait changed handlers
518 # Trait changed handlers
519 #-------------------------------------------------------------------------
519 #-------------------------------------------------------------------------
520
520
521 def _ipython_dir_changed(self, name, new):
521 def _ipython_dir_changed(self, name, new):
522 if not os.path.isdir(new):
522 if not os.path.isdir(new):
523 os.makedirs(new, mode = 0o777)
523 os.makedirs(new, mode = 0o777)
524
524
525 def set_autoindent(self,value=None):
525 def set_autoindent(self,value=None):
526 """Set the autoindent flag, checking for readline support.
526 """Set the autoindent flag, checking for readline support.
527
527
528 If called with no arguments, it acts as a toggle."""
528 If called with no arguments, it acts as a toggle."""
529
529
530 if value != 0 and not self.has_readline:
530 if value != 0 and not self.has_readline:
531 if os.name == 'posix':
531 if os.name == 'posix':
532 warn("The auto-indent feature requires the readline library")
532 warn("The auto-indent feature requires the readline library")
533 self.autoindent = 0
533 self.autoindent = 0
534 return
534 return
535 if value is None:
535 if value is None:
536 self.autoindent = not self.autoindent
536 self.autoindent = not self.autoindent
537 else:
537 else:
538 self.autoindent = value
538 self.autoindent = value
539
539
540 #-------------------------------------------------------------------------
540 #-------------------------------------------------------------------------
541 # init_* methods called by __init__
541 # init_* methods called by __init__
542 #-------------------------------------------------------------------------
542 #-------------------------------------------------------------------------
543
543
544 def init_ipython_dir(self, ipython_dir):
544 def init_ipython_dir(self, ipython_dir):
545 if ipython_dir is not None:
545 if ipython_dir is not None:
546 self.ipython_dir = ipython_dir
546 self.ipython_dir = ipython_dir
547 return
547 return
548
548
549 self.ipython_dir = get_ipython_dir()
549 self.ipython_dir = get_ipython_dir()
550
550
551 def init_profile_dir(self, profile_dir):
551 def init_profile_dir(self, profile_dir):
552 if profile_dir is not None:
552 if profile_dir is not None:
553 self.profile_dir = profile_dir
553 self.profile_dir = profile_dir
554 return
554 return
555 self.profile_dir =\
555 self.profile_dir =\
556 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
556 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
557
557
558 def init_instance_attrs(self):
558 def init_instance_attrs(self):
559 self.more = False
559 self.more = False
560
560
561 # command compiler
561 # command compiler
562 self.compile = CachingCompiler()
562 self.compile = CachingCompiler()
563
563
564 # Make an empty namespace, which extension writers can rely on both
564 # Make an empty namespace, which extension writers can rely on both
565 # existing and NEVER being used by ipython itself. This gives them a
565 # existing and NEVER being used by ipython itself. This gives them a
566 # convenient location for storing additional information and state
566 # convenient location for storing additional information and state
567 # their extensions may require, without fear of collisions with other
567 # their extensions may require, without fear of collisions with other
568 # ipython names that may develop later.
568 # ipython names that may develop later.
569 self.meta = Struct()
569 self.meta = Struct()
570
570
571 # Temporary files used for various purposes. Deleted at exit.
571 # Temporary files used for various purposes. Deleted at exit.
572 self.tempfiles = []
572 self.tempfiles = []
573
573
574 # Keep track of readline usage (later set by init_readline)
574 # Keep track of readline usage (later set by init_readline)
575 self.has_readline = False
575 self.has_readline = False
576
576
577 # keep track of where we started running (mainly for crash post-mortem)
577 # keep track of where we started running (mainly for crash post-mortem)
578 # This is not being used anywhere currently.
578 # This is not being used anywhere currently.
579 self.starting_dir = os.getcwdu()
579 self.starting_dir = os.getcwdu()
580
580
581 # Indentation management
581 # Indentation management
582 self.indent_current_nsp = 0
582 self.indent_current_nsp = 0
583
583
584 # Dict to track post-execution functions that have been registered
584 # Dict to track post-execution functions that have been registered
585 self._post_execute = {}
585 self._post_execute = {}
586
586
587 def init_environment(self):
587 def init_environment(self):
588 """Any changes we need to make to the user's environment."""
588 """Any changes we need to make to the user's environment."""
589 pass
589 pass
590
590
591 def init_encoding(self):
591 def init_encoding(self):
592 # Get system encoding at startup time. Certain terminals (like Emacs
592 # Get system encoding at startup time. Certain terminals (like Emacs
593 # under Win32 have it set to None, and we need to have a known valid
593 # under Win32 have it set to None, and we need to have a known valid
594 # encoding to use in the raw_input() method
594 # encoding to use in the raw_input() method
595 try:
595 try:
596 self.stdin_encoding = sys.stdin.encoding or 'ascii'
596 self.stdin_encoding = sys.stdin.encoding or 'ascii'
597 except AttributeError:
597 except AttributeError:
598 self.stdin_encoding = 'ascii'
598 self.stdin_encoding = 'ascii'
599
599
600 def init_syntax_highlighting(self):
600 def init_syntax_highlighting(self):
601 # Python source parser/formatter for syntax highlighting
601 # Python source parser/formatter for syntax highlighting
602 pyformat = PyColorize.Parser().format
602 pyformat = PyColorize.Parser().format
603 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
603 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
604
604
605 def init_pushd_popd_magic(self):
605 def init_pushd_popd_magic(self):
606 # for pushd/popd management
606 # for pushd/popd management
607 self.home_dir = get_home_dir()
607 self.home_dir = get_home_dir()
608
608
609 self.dir_stack = []
609 self.dir_stack = []
610
610
611 def init_logger(self):
611 def init_logger(self):
612 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
612 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
613 logmode='rotate')
613 logmode='rotate')
614
614
615 def init_logstart(self):
615 def init_logstart(self):
616 """Initialize logging in case it was requested at the command line.
616 """Initialize logging in case it was requested at the command line.
617 """
617 """
618 if self.logappend:
618 if self.logappend:
619 self.magic('logstart %s append' % self.logappend)
619 self.magic('logstart %s append' % self.logappend)
620 elif self.logfile:
620 elif self.logfile:
621 self.magic('logstart %s' % self.logfile)
621 self.magic('logstart %s' % self.logfile)
622 elif self.logstart:
622 elif self.logstart:
623 self.magic('logstart')
623 self.magic('logstart')
624
624
625 def init_builtins(self):
625 def init_builtins(self):
626 # A single, static flag that we set to True. Its presence indicates
626 # A single, static flag that we set to True. Its presence indicates
627 # that an IPython shell has been created, and we make no attempts at
627 # that an IPython shell has been created, and we make no attempts at
628 # removing on exit or representing the existence of more than one
628 # removing on exit or representing the existence of more than one
629 # IPython at a time.
629 # IPython at a time.
630 builtin_mod.__dict__['__IPYTHON__'] = True
630 builtin_mod.__dict__['__IPYTHON__'] = True
631
631
632 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
632 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
633 # manage on enter/exit, but with all our shells it's virtually
633 # manage on enter/exit, but with all our shells it's virtually
634 # impossible to get all the cases right. We're leaving the name in for
634 # impossible to get all the cases right. We're leaving the name in for
635 # those who adapted their codes to check for this flag, but will
635 # those who adapted their codes to check for this flag, but will
636 # eventually remove it after a few more releases.
636 # eventually remove it after a few more releases.
637 builtin_mod.__dict__['__IPYTHON__active'] = \
637 builtin_mod.__dict__['__IPYTHON__active'] = \
638 'Deprecated, check for __IPYTHON__'
638 'Deprecated, check for __IPYTHON__'
639
639
640 self.builtin_trap = BuiltinTrap(shell=self)
640 self.builtin_trap = BuiltinTrap(shell=self)
641
641
642 def init_inspector(self):
642 def init_inspector(self):
643 # Object inspector
643 # Object inspector
644 self.inspector = oinspect.Inspector(oinspect.InspectColors,
644 self.inspector = oinspect.Inspector(oinspect.InspectColors,
645 PyColorize.ANSICodeColors,
645 PyColorize.ANSICodeColors,
646 'NoColor',
646 'NoColor',
647 self.object_info_string_level)
647 self.object_info_string_level)
648
648
649 def init_io(self):
649 def init_io(self):
650 # This will just use sys.stdout and sys.stderr. If you want to
650 # This will just use sys.stdout and sys.stderr. If you want to
651 # override sys.stdout and sys.stderr themselves, you need to do that
651 # override sys.stdout and sys.stderr themselves, you need to do that
652 # *before* instantiating this class, because io holds onto
652 # *before* instantiating this class, because io holds onto
653 # references to the underlying streams.
653 # references to the underlying streams.
654 if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline:
654 if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline:
655 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
655 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
656 else:
656 else:
657 io.stdout = io.IOStream(sys.stdout)
657 io.stdout = io.IOStream(sys.stdout)
658 io.stderr = io.IOStream(sys.stderr)
658 io.stderr = io.IOStream(sys.stderr)
659
659
660 def init_prompts(self):
660 def init_prompts(self):
661 self.prompt_manager = PromptManager(shell=self, parent=self)
661 self.prompt_manager = PromptManager(shell=self, parent=self)
662 self.configurables.append(self.prompt_manager)
662 self.configurables.append(self.prompt_manager)
663 # Set system prompts, so that scripts can decide if they are running
663 # Set system prompts, so that scripts can decide if they are running
664 # interactively.
664 # interactively.
665 sys.ps1 = 'In : '
665 sys.ps1 = 'In : '
666 sys.ps2 = '...: '
666 sys.ps2 = '...: '
667 sys.ps3 = 'Out: '
667 sys.ps3 = 'Out: '
668
668
669 def init_display_formatter(self):
669 def init_display_formatter(self):
670 self.display_formatter = DisplayFormatter(parent=self)
670 self.display_formatter = DisplayFormatter(parent=self)
671 self.configurables.append(self.display_formatter)
671 self.configurables.append(self.display_formatter)
672
672
673 def init_display_pub(self):
673 def init_display_pub(self):
674 self.display_pub = self.display_pub_class(parent=self)
674 self.display_pub = self.display_pub_class(parent=self)
675 self.configurables.append(self.display_pub)
675 self.configurables.append(self.display_pub)
676
676
677 def init_data_pub(self):
677 def init_data_pub(self):
678 if not self.data_pub_class:
678 if not self.data_pub_class:
679 self.data_pub = None
679 self.data_pub = None
680 return
680 return
681 self.data_pub = self.data_pub_class(parent=self)
681 self.data_pub = self.data_pub_class(parent=self)
682 self.configurables.append(self.data_pub)
682 self.configurables.append(self.data_pub)
683
683
684 def init_displayhook(self):
684 def init_displayhook(self):
685 # Initialize displayhook, set in/out prompts and printing system
685 # Initialize displayhook, set in/out prompts and printing system
686 self.displayhook = self.displayhook_class(
686 self.displayhook = self.displayhook_class(
687 parent=self,
687 parent=self,
688 shell=self,
688 shell=self,
689 cache_size=self.cache_size,
689 cache_size=self.cache_size,
690 )
690 )
691 self.configurables.append(self.displayhook)
691 self.configurables.append(self.displayhook)
692 # This is a context manager that installs/revmoes the displayhook at
692 # This is a context manager that installs/revmoes the displayhook at
693 # the appropriate time.
693 # the appropriate time.
694 self.display_trap = DisplayTrap(hook=self.displayhook)
694 self.display_trap = DisplayTrap(hook=self.displayhook)
695
695
696 def init_latextool(self):
696 def init_latextool(self):
697 """Configure LaTeXTool."""
697 """Configure LaTeXTool."""
698 cfg = LaTeXTool.instance(parent=self)
698 cfg = LaTeXTool.instance(parent=self)
699 if cfg not in self.configurables:
699 if cfg not in self.configurables:
700 self.configurables.append(cfg)
700 self.configurables.append(cfg)
701
701
702 def init_virtualenv(self):
702 def init_virtualenv(self):
703 """Add a virtualenv to sys.path so the user can import modules from it.
703 """Add a virtualenv to sys.path so the user can import modules from it.
704 This isn't perfect: it doesn't use the Python interpreter with which the
704 This isn't perfect: it doesn't use the Python interpreter with which the
705 virtualenv was built, and it ignores the --no-site-packages option. A
705 virtualenv was built, and it ignores the --no-site-packages option. A
706 warning will appear suggesting the user installs IPython in the
706 warning will appear suggesting the user installs IPython in the
707 virtualenv, but for many cases, it probably works well enough.
707 virtualenv, but for many cases, it probably works well enough.
708
708
709 Adapted from code snippets online.
709 Adapted from code snippets online.
710
710
711 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
711 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
712 """
712 """
713 if 'VIRTUAL_ENV' not in os.environ:
713 if 'VIRTUAL_ENV' not in os.environ:
714 # Not in a virtualenv
714 # Not in a virtualenv
715 return
715 return
716
716
717 if sys.executable.startswith(os.environ['VIRTUAL_ENV']):
717 if sys.executable.startswith(os.environ['VIRTUAL_ENV']):
718 # Running properly in the virtualenv, don't need to do anything
718 # Running properly in the virtualenv, don't need to do anything
719 return
719 return
720
720
721 warn("Attempting to work in a virtualenv. If you encounter problems, please "
721 warn("Attempting to work in a virtualenv. If you encounter problems, please "
722 "install IPython inside the virtualenv.")
722 "install IPython inside the virtualenv.")
723 if sys.platform == "win32":
723 if sys.platform == "win32":
724 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
724 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
725 else:
725 else:
726 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
726 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
727 'python%d.%d' % sys.version_info[:2], 'site-packages')
727 'python%d.%d' % sys.version_info[:2], 'site-packages')
728
728
729 import site
729 import site
730 sys.path.insert(0, virtual_env)
730 sys.path.insert(0, virtual_env)
731 site.addsitedir(virtual_env)
731 site.addsitedir(virtual_env)
732
732
733 #-------------------------------------------------------------------------
733 #-------------------------------------------------------------------------
734 # Things related to injections into the sys module
734 # Things related to injections into the sys module
735 #-------------------------------------------------------------------------
735 #-------------------------------------------------------------------------
736
736
737 def save_sys_module_state(self):
737 def save_sys_module_state(self):
738 """Save the state of hooks in the sys module.
738 """Save the state of hooks in the sys module.
739
739
740 This has to be called after self.user_module is created.
740 This has to be called after self.user_module is created.
741 """
741 """
742 self._orig_sys_module_state = {}
742 self._orig_sys_module_state = {}
743 self._orig_sys_module_state['stdin'] = sys.stdin
743 self._orig_sys_module_state['stdin'] = sys.stdin
744 self._orig_sys_module_state['stdout'] = sys.stdout
744 self._orig_sys_module_state['stdout'] = sys.stdout
745 self._orig_sys_module_state['stderr'] = sys.stderr
745 self._orig_sys_module_state['stderr'] = sys.stderr
746 self._orig_sys_module_state['excepthook'] = sys.excepthook
746 self._orig_sys_module_state['excepthook'] = sys.excepthook
747 self._orig_sys_modules_main_name = self.user_module.__name__
747 self._orig_sys_modules_main_name = self.user_module.__name__
748 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
748 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
749
749
750 def restore_sys_module_state(self):
750 def restore_sys_module_state(self):
751 """Restore the state of the sys module."""
751 """Restore the state of the sys module."""
752 try:
752 try:
753 for k, v in self._orig_sys_module_state.iteritems():
753 for k, v in self._orig_sys_module_state.iteritems():
754 setattr(sys, k, v)
754 setattr(sys, k, v)
755 except AttributeError:
755 except AttributeError:
756 pass
756 pass
757 # Reset what what done in self.init_sys_modules
757 # Reset what what done in self.init_sys_modules
758 if self._orig_sys_modules_main_mod is not None:
758 if self._orig_sys_modules_main_mod is not None:
759 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
759 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
760
760
761 #-------------------------------------------------------------------------
761 #-------------------------------------------------------------------------
762 # Things related to hooks
762 # Things related to hooks
763 #-------------------------------------------------------------------------
763 #-------------------------------------------------------------------------
764
764
765 def init_hooks(self):
765 def init_hooks(self):
766 # hooks holds pointers used for user-side customizations
766 # hooks holds pointers used for user-side customizations
767 self.hooks = Struct()
767 self.hooks = Struct()
768
768
769 self.strdispatchers = {}
769 self.strdispatchers = {}
770
770
771 # Set all default hooks, defined in the IPython.hooks module.
771 # Set all default hooks, defined in the IPython.hooks module.
772 hooks = IPython.core.hooks
772 hooks = IPython.core.hooks
773 for hook_name in hooks.__all__:
773 for hook_name in hooks.__all__:
774 # default hooks have priority 100, i.e. low; user hooks should have
774 # default hooks have priority 100, i.e. low; user hooks should have
775 # 0-100 priority
775 # 0-100 priority
776 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
776 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
777
777
778 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
778 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
779 """set_hook(name,hook) -> sets an internal IPython hook.
779 """set_hook(name,hook) -> sets an internal IPython hook.
780
780
781 IPython exposes some of its internal API as user-modifiable hooks. By
781 IPython exposes some of its internal API as user-modifiable hooks. By
782 adding your function to one of these hooks, you can modify IPython's
782 adding your function to one of these hooks, you can modify IPython's
783 behavior to call at runtime your own routines."""
783 behavior to call at runtime your own routines."""
784
784
785 # At some point in the future, this should validate the hook before it
785 # At some point in the future, this should validate the hook before it
786 # accepts it. Probably at least check that the hook takes the number
786 # accepts it. Probably at least check that the hook takes the number
787 # of args it's supposed to.
787 # of args it's supposed to.
788
788
789 f = types.MethodType(hook,self)
789 f = types.MethodType(hook,self)
790
790
791 # check if the hook is for strdispatcher first
791 # check if the hook is for strdispatcher first
792 if str_key is not None:
792 if str_key is not None:
793 sdp = self.strdispatchers.get(name, StrDispatch())
793 sdp = self.strdispatchers.get(name, StrDispatch())
794 sdp.add_s(str_key, f, priority )
794 sdp.add_s(str_key, f, priority )
795 self.strdispatchers[name] = sdp
795 self.strdispatchers[name] = sdp
796 return
796 return
797 if re_key is not None:
797 if re_key is not None:
798 sdp = self.strdispatchers.get(name, StrDispatch())
798 sdp = self.strdispatchers.get(name, StrDispatch())
799 sdp.add_re(re.compile(re_key), f, priority )
799 sdp.add_re(re.compile(re_key), f, priority )
800 self.strdispatchers[name] = sdp
800 self.strdispatchers[name] = sdp
801 return
801 return
802
802
803 dp = getattr(self.hooks, name, None)
803 dp = getattr(self.hooks, name, None)
804 if name not in IPython.core.hooks.__all__:
804 if name not in IPython.core.hooks.__all__:
805 print("Warning! Hook '%s' is not one of %s" % \
805 print("Warning! Hook '%s' is not one of %s" % \
806 (name, IPython.core.hooks.__all__ ))
806 (name, IPython.core.hooks.__all__ ))
807 if not dp:
807 if not dp:
808 dp = IPython.core.hooks.CommandChainDispatcher()
808 dp = IPython.core.hooks.CommandChainDispatcher()
809
809
810 try:
810 try:
811 dp.add(f,priority)
811 dp.add(f,priority)
812 except AttributeError:
812 except AttributeError:
813 # it was not commandchain, plain old func - replace
813 # it was not commandchain, plain old func - replace
814 dp = f
814 dp = f
815
815
816 setattr(self.hooks,name, dp)
816 setattr(self.hooks,name, dp)
817
817
818 def register_post_execute(self, func):
818 def register_post_execute(self, func):
819 """Register a function for calling after code execution.
819 """Register a function for calling after code execution.
820 """
820 """
821 if not callable(func):
821 if not callable(func):
822 raise ValueError('argument %s must be callable' % func)
822 raise ValueError('argument %s must be callable' % func)
823 self._post_execute[func] = True
823 self._post_execute[func] = True
824
824
825 #-------------------------------------------------------------------------
825 #-------------------------------------------------------------------------
826 # Things related to the "main" module
826 # Things related to the "main" module
827 #-------------------------------------------------------------------------
827 #-------------------------------------------------------------------------
828
828
829 def new_main_mod(self, filename):
829 def new_main_mod(self, filename):
830 """Return a new 'main' module object for user code execution.
830 """Return a new 'main' module object for user code execution.
831
831
832 ``filename`` should be the path of the script which will be run in the
832 ``filename`` should be the path of the script which will be run in the
833 module. Requests with the same filename will get the same module, with
833 module. Requests with the same filename will get the same module, with
834 its namespace cleared.
834 its namespace cleared.
835
835
836 When scripts are executed via %run, we must keep a reference to their
836 When scripts are executed via %run, we must keep a reference to their
837 __main__ module (a FakeModule instance) around so that Python doesn't
837 __main__ module (a FakeModule instance) around so that Python doesn't
838 clear it, rendering references to module globals useless.
838 clear it, rendering references to module globals useless.
839
839
840 This method keeps said reference in a private dict, keyed by the
840 This method keeps said reference in a private dict, keyed by the
841 absolute path of the script. This way, for multiple executions of the
841 absolute path of the script. This way, for multiple executions of the
842 same script we only keep one copy of the namespace (the last one),
842 same script we only keep one copy of the namespace (the last one),
843 thus preventing memory leaks from old references while allowing the
843 thus preventing memory leaks from old references while allowing the
844 objects from the last execution to be accessible.
844 objects from the last execution to be accessible.
845 """
845 """
846 filename = os.path.abspath(filename)
846 filename = os.path.abspath(filename)
847 try:
847 try:
848 main_mod = self._main_mod_cache[filename]
848 main_mod = self._main_mod_cache[filename]
849 except KeyError:
849 except KeyError:
850 main_mod = self._main_mod_cache[filename] = FakeModule()
850 main_mod = self._main_mod_cache[filename] = FakeModule()
851 else:
851 else:
852 init_fakemod_dict(main_mod)
852 init_fakemod_dict(main_mod)
853
853
854 return main_mod
854 return main_mod
855
855
856 def clear_main_mod_cache(self):
856 def clear_main_mod_cache(self):
857 """Clear the cache of main modules.
857 """Clear the cache of main modules.
858
858
859 Mainly for use by utilities like %reset.
859 Mainly for use by utilities like %reset.
860
860
861 Examples
861 Examples
862 --------
862 --------
863
863
864 In [15]: import IPython
864 In [15]: import IPython
865
865
866 In [16]: m = _ip.new_main_mod(IPython.__file__)
866 In [16]: m = _ip.new_main_mod(IPython.__file__)
867
867
868 In [17]: len(_ip._main_mod_cache) > 0
868 In [17]: len(_ip._main_mod_cache) > 0
869 Out[17]: True
869 Out[17]: True
870
870
871 In [18]: _ip.clear_main_mod_cache()
871 In [18]: _ip.clear_main_mod_cache()
872
872
873 In [19]: len(_ip._main_mod_cache) == 0
873 In [19]: len(_ip._main_mod_cache) == 0
874 Out[19]: True
874 Out[19]: True
875 """
875 """
876 self._main_mod_cache.clear()
876 self._main_mod_cache.clear()
877
877
878 #-------------------------------------------------------------------------
878 #-------------------------------------------------------------------------
879 # Things related to debugging
879 # Things related to debugging
880 #-------------------------------------------------------------------------
880 #-------------------------------------------------------------------------
881
881
882 def init_pdb(self):
882 def init_pdb(self):
883 # Set calling of pdb on exceptions
883 # Set calling of pdb on exceptions
884 # self.call_pdb is a property
884 # self.call_pdb is a property
885 self.call_pdb = self.pdb
885 self.call_pdb = self.pdb
886
886
887 def _get_call_pdb(self):
887 def _get_call_pdb(self):
888 return self._call_pdb
888 return self._call_pdb
889
889
890 def _set_call_pdb(self,val):
890 def _set_call_pdb(self,val):
891
891
892 if val not in (0,1,False,True):
892 if val not in (0,1,False,True):
893 raise ValueError('new call_pdb value must be boolean')
893 raise ValueError('new call_pdb value must be boolean')
894
894
895 # store value in instance
895 # store value in instance
896 self._call_pdb = val
896 self._call_pdb = val
897
897
898 # notify the actual exception handlers
898 # notify the actual exception handlers
899 self.InteractiveTB.call_pdb = val
899 self.InteractiveTB.call_pdb = val
900
900
901 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
901 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
902 'Control auto-activation of pdb at exceptions')
902 'Control auto-activation of pdb at exceptions')
903
903
904 def debugger(self,force=False):
904 def debugger(self,force=False):
905 """Call the pydb/pdb debugger.
905 """Call the pydb/pdb debugger.
906
906
907 Keywords:
907 Keywords:
908
908
909 - force(False): by default, this routine checks the instance call_pdb
909 - force(False): by default, this routine checks the instance call_pdb
910 flag and does not actually invoke the debugger if the flag is false.
910 flag and does not actually invoke the debugger if the flag is false.
911 The 'force' option forces the debugger to activate even if the flag
911 The 'force' option forces the debugger to activate even if the flag
912 is false.
912 is false.
913 """
913 """
914
914
915 if not (force or self.call_pdb):
915 if not (force or self.call_pdb):
916 return
916 return
917
917
918 if not hasattr(sys,'last_traceback'):
918 if not hasattr(sys,'last_traceback'):
919 error('No traceback has been produced, nothing to debug.')
919 error('No traceback has been produced, nothing to debug.')
920 return
920 return
921
921
922 # use pydb if available
922 # use pydb if available
923 if debugger.has_pydb:
923 if debugger.has_pydb:
924 from pydb import pm
924 from pydb import pm
925 else:
925 else:
926 # fallback to our internal debugger
926 # fallback to our internal debugger
927 pm = lambda : self.InteractiveTB.debugger(force=True)
927 pm = lambda : self.InteractiveTB.debugger(force=True)
928
928
929 with self.readline_no_record:
929 with self.readline_no_record:
930 pm()
930 pm()
931
931
932 #-------------------------------------------------------------------------
932 #-------------------------------------------------------------------------
933 # Things related to IPython's various namespaces
933 # Things related to IPython's various namespaces
934 #-------------------------------------------------------------------------
934 #-------------------------------------------------------------------------
935 default_user_namespaces = True
935 default_user_namespaces = True
936
936
937 def init_create_namespaces(self, user_module=None, user_ns=None):
937 def init_create_namespaces(self, user_module=None, user_ns=None):
938 # Create the namespace where the user will operate. user_ns is
938 # Create the namespace where the user will operate. user_ns is
939 # normally the only one used, and it is passed to the exec calls as
939 # normally the only one used, and it is passed to the exec calls as
940 # the locals argument. But we do carry a user_global_ns namespace
940 # the locals argument. But we do carry a user_global_ns namespace
941 # given as the exec 'globals' argument, This is useful in embedding
941 # given as the exec 'globals' argument, This is useful in embedding
942 # situations where the ipython shell opens in a context where the
942 # situations where the ipython shell opens in a context where the
943 # distinction between locals and globals is meaningful. For
943 # distinction between locals and globals is meaningful. For
944 # non-embedded contexts, it is just the same object as the user_ns dict.
944 # non-embedded contexts, it is just the same object as the user_ns dict.
945
945
946 # FIXME. For some strange reason, __builtins__ is showing up at user
946 # FIXME. For some strange reason, __builtins__ is showing up at user
947 # level as a dict instead of a module. This is a manual fix, but I
947 # level as a dict instead of a module. This is a manual fix, but I
948 # should really track down where the problem is coming from. Alex
948 # should really track down where the problem is coming from. Alex
949 # Schmolck reported this problem first.
949 # Schmolck reported this problem first.
950
950
951 # A useful post by Alex Martelli on this topic:
951 # A useful post by Alex Martelli on this topic:
952 # Re: inconsistent value from __builtins__
952 # Re: inconsistent value from __builtins__
953 # Von: Alex Martelli <aleaxit@yahoo.com>
953 # Von: Alex Martelli <aleaxit@yahoo.com>
954 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
954 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
955 # Gruppen: comp.lang.python
955 # Gruppen: comp.lang.python
956
956
957 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
957 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
958 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
958 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
959 # > <type 'dict'>
959 # > <type 'dict'>
960 # > >>> print type(__builtins__)
960 # > >>> print type(__builtins__)
961 # > <type 'module'>
961 # > <type 'module'>
962 # > Is this difference in return value intentional?
962 # > Is this difference in return value intentional?
963
963
964 # Well, it's documented that '__builtins__' can be either a dictionary
964 # Well, it's documented that '__builtins__' can be either a dictionary
965 # or a module, and it's been that way for a long time. Whether it's
965 # or a module, and it's been that way for a long time. Whether it's
966 # intentional (or sensible), I don't know. In any case, the idea is
966 # intentional (or sensible), I don't know. In any case, the idea is
967 # that if you need to access the built-in namespace directly, you
967 # that if you need to access the built-in namespace directly, you
968 # should start with "import __builtin__" (note, no 's') which will
968 # should start with "import __builtin__" (note, no 's') which will
969 # definitely give you a module. Yeah, it's somewhat confusing:-(.
969 # definitely give you a module. Yeah, it's somewhat confusing:-(.
970
970
971 # These routines return a properly built module and dict as needed by
971 # These routines return a properly built module and dict as needed by
972 # the rest of the code, and can also be used by extension writers to
972 # the rest of the code, and can also be used by extension writers to
973 # generate properly initialized namespaces.
973 # generate properly initialized namespaces.
974 if (user_ns is not None) or (user_module is not None):
974 if (user_ns is not None) or (user_module is not None):
975 self.default_user_namespaces = False
975 self.default_user_namespaces = False
976 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
976 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
977
977
978 # A record of hidden variables we have added to the user namespace, so
978 # A record of hidden variables we have added to the user namespace, so
979 # we can list later only variables defined in actual interactive use.
979 # we can list later only variables defined in actual interactive use.
980 self.user_ns_hidden = {}
980 self.user_ns_hidden = {}
981
981
982 # Now that FakeModule produces a real module, we've run into a nasty
982 # Now that FakeModule produces a real module, we've run into a nasty
983 # problem: after script execution (via %run), the module where the user
983 # problem: after script execution (via %run), the module where the user
984 # code ran is deleted. Now that this object is a true module (needed
984 # code ran is deleted. Now that this object is a true module (needed
985 # so docetst and other tools work correctly), the Python module
985 # so docetst and other tools work correctly), the Python module
986 # teardown mechanism runs over it, and sets to None every variable
986 # teardown mechanism runs over it, and sets to None every variable
987 # present in that module. Top-level references to objects from the
987 # present in that module. Top-level references to objects from the
988 # script survive, because the user_ns is updated with them. However,
988 # script survive, because the user_ns is updated with them. However,
989 # calling functions defined in the script that use other things from
989 # calling functions defined in the script that use other things from
990 # the script will fail, because the function's closure had references
990 # the script will fail, because the function's closure had references
991 # to the original objects, which are now all None. So we must protect
991 # to the original objects, which are now all None. So we must protect
992 # these modules from deletion by keeping a cache.
992 # these modules from deletion by keeping a cache.
993 #
993 #
994 # To avoid keeping stale modules around (we only need the one from the
994 # To avoid keeping stale modules around (we only need the one from the
995 # last run), we use a dict keyed with the full path to the script, so
995 # last run), we use a dict keyed with the full path to the script, so
996 # only the last version of the module is held in the cache. Note,
996 # only the last version of the module is held in the cache. Note,
997 # however, that we must cache the module *namespace contents* (their
997 # however, that we must cache the module *namespace contents* (their
998 # __dict__). Because if we try to cache the actual modules, old ones
998 # __dict__). Because if we try to cache the actual modules, old ones
999 # (uncached) could be destroyed while still holding references (such as
999 # (uncached) could be destroyed while still holding references (such as
1000 # those held by GUI objects that tend to be long-lived)>
1000 # those held by GUI objects that tend to be long-lived)>
1001 #
1001 #
1002 # The %reset command will flush this cache. See the cache_main_mod()
1002 # The %reset command will flush this cache. See the cache_main_mod()
1003 # and clear_main_mod_cache() methods for details on use.
1003 # and clear_main_mod_cache() methods for details on use.
1004
1004
1005 # This is the cache used for 'main' namespaces
1005 # This is the cache used for 'main' namespaces
1006 self._main_mod_cache = {}
1006 self._main_mod_cache = {}
1007
1007
1008 # A table holding all the namespaces IPython deals with, so that
1008 # A table holding all the namespaces IPython deals with, so that
1009 # introspection facilities can search easily.
1009 # introspection facilities can search easily.
1010 self.ns_table = {'user_global':self.user_module.__dict__,
1010 self.ns_table = {'user_global':self.user_module.__dict__,
1011 'user_local':self.user_ns,
1011 'user_local':self.user_ns,
1012 'builtin':builtin_mod.__dict__
1012 'builtin':builtin_mod.__dict__
1013 }
1013 }
1014
1014
1015 @property
1015 @property
1016 def user_global_ns(self):
1016 def user_global_ns(self):
1017 return self.user_module.__dict__
1017 return self.user_module.__dict__
1018
1018
1019 def prepare_user_module(self, user_module=None, user_ns=None):
1019 def prepare_user_module(self, user_module=None, user_ns=None):
1020 """Prepare the module and namespace in which user code will be run.
1020 """Prepare the module and namespace in which user code will be run.
1021
1021
1022 When IPython is started normally, both parameters are None: a new module
1022 When IPython is started normally, both parameters are None: a new module
1023 is created automatically, and its __dict__ used as the namespace.
1023 is created automatically, and its __dict__ used as the namespace.
1024
1024
1025 If only user_module is provided, its __dict__ is used as the namespace.
1025 If only user_module is provided, its __dict__ is used as the namespace.
1026 If only user_ns is provided, a dummy module is created, and user_ns
1026 If only user_ns is provided, a dummy module is created, and user_ns
1027 becomes the global namespace. If both are provided (as they may be
1027 becomes the global namespace. If both are provided (as they may be
1028 when embedding), user_ns is the local namespace, and user_module
1028 when embedding), user_ns is the local namespace, and user_module
1029 provides the global namespace.
1029 provides the global namespace.
1030
1030
1031 Parameters
1031 Parameters
1032 ----------
1032 ----------
1033 user_module : module, optional
1033 user_module : module, optional
1034 The current user module in which IPython is being run. If None,
1034 The current user module in which IPython is being run. If None,
1035 a clean module will be created.
1035 a clean module will be created.
1036 user_ns : dict, optional
1036 user_ns : dict, optional
1037 A namespace in which to run interactive commands.
1037 A namespace in which to run interactive commands.
1038
1038
1039 Returns
1039 Returns
1040 -------
1040 -------
1041 A tuple of user_module and user_ns, each properly initialised.
1041 A tuple of user_module and user_ns, each properly initialised.
1042 """
1042 """
1043 if user_module is None and user_ns is not None:
1043 if user_module is None and user_ns is not None:
1044 user_ns.setdefault("__name__", "__main__")
1044 user_ns.setdefault("__name__", "__main__")
1045 user_module = DummyMod()
1045 user_module = DummyMod()
1046 user_module.__dict__ = user_ns
1046 user_module.__dict__ = user_ns
1047
1047
1048 if user_module is None:
1048 if user_module is None:
1049 user_module = types.ModuleType("__main__",
1049 user_module = types.ModuleType("__main__",
1050 doc="Automatically created module for IPython interactive environment")
1050 doc="Automatically created module for IPython interactive environment")
1051
1051
1052 # We must ensure that __builtin__ (without the final 's') is always
1052 # We must ensure that __builtin__ (without the final 's') is always
1053 # available and pointing to the __builtin__ *module*. For more details:
1053 # available and pointing to the __builtin__ *module*. For more details:
1054 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1054 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1055 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1055 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1056 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1056 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1057
1057
1058 if user_ns is None:
1058 if user_ns is None:
1059 user_ns = user_module.__dict__
1059 user_ns = user_module.__dict__
1060
1060
1061 return user_module, user_ns
1061 return user_module, user_ns
1062
1062
1063 def init_sys_modules(self):
1063 def init_sys_modules(self):
1064 # We need to insert into sys.modules something that looks like a
1064 # We need to insert into sys.modules something that looks like a
1065 # module but which accesses the IPython namespace, for shelve and
1065 # module but which accesses the IPython namespace, for shelve and
1066 # pickle to work interactively. Normally they rely on getting
1066 # pickle to work interactively. Normally they rely on getting
1067 # everything out of __main__, but for embedding purposes each IPython
1067 # everything out of __main__, but for embedding purposes each IPython
1068 # instance has its own private namespace, so we can't go shoving
1068 # instance has its own private namespace, so we can't go shoving
1069 # everything into __main__.
1069 # everything into __main__.
1070
1070
1071 # note, however, that we should only do this for non-embedded
1071 # note, however, that we should only do this for non-embedded
1072 # ipythons, which really mimic the __main__.__dict__ with their own
1072 # ipythons, which really mimic the __main__.__dict__ with their own
1073 # namespace. Embedded instances, on the other hand, should not do
1073 # namespace. Embedded instances, on the other hand, should not do
1074 # this because they need to manage the user local/global namespaces
1074 # this because they need to manage the user local/global namespaces
1075 # only, but they live within a 'normal' __main__ (meaning, they
1075 # only, but they live within a 'normal' __main__ (meaning, they
1076 # shouldn't overtake the execution environment of the script they're
1076 # shouldn't overtake the execution environment of the script they're
1077 # embedded in).
1077 # embedded in).
1078
1078
1079 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1079 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1080 main_name = self.user_module.__name__
1080 main_name = self.user_module.__name__
1081 sys.modules[main_name] = self.user_module
1081 sys.modules[main_name] = self.user_module
1082
1082
1083 def init_user_ns(self):
1083 def init_user_ns(self):
1084 """Initialize all user-visible namespaces to their minimum defaults.
1084 """Initialize all user-visible namespaces to their minimum defaults.
1085
1085
1086 Certain history lists are also initialized here, as they effectively
1086 Certain history lists are also initialized here, as they effectively
1087 act as user namespaces.
1087 act as user namespaces.
1088
1088
1089 Notes
1089 Notes
1090 -----
1090 -----
1091 All data structures here are only filled in, they are NOT reset by this
1091 All data structures here are only filled in, they are NOT reset by this
1092 method. If they were not empty before, data will simply be added to
1092 method. If they were not empty before, data will simply be added to
1093 therm.
1093 therm.
1094 """
1094 """
1095 # This function works in two parts: first we put a few things in
1095 # This function works in two parts: first we put a few things in
1096 # user_ns, and we sync that contents into user_ns_hidden so that these
1096 # user_ns, and we sync that contents into user_ns_hidden so that these
1097 # initial variables aren't shown by %who. After the sync, we add the
1097 # initial variables aren't shown by %who. After the sync, we add the
1098 # rest of what we *do* want the user to see with %who even on a new
1098 # rest of what we *do* want the user to see with %who even on a new
1099 # session (probably nothing, so theye really only see their own stuff)
1099 # session (probably nothing, so theye really only see their own stuff)
1100
1100
1101 # The user dict must *always* have a __builtin__ reference to the
1101 # The user dict must *always* have a __builtin__ reference to the
1102 # Python standard __builtin__ namespace, which must be imported.
1102 # Python standard __builtin__ namespace, which must be imported.
1103 # This is so that certain operations in prompt evaluation can be
1103 # This is so that certain operations in prompt evaluation can be
1104 # reliably executed with builtins. Note that we can NOT use
1104 # reliably executed with builtins. Note that we can NOT use
1105 # __builtins__ (note the 's'), because that can either be a dict or a
1105 # __builtins__ (note the 's'), because that can either be a dict or a
1106 # module, and can even mutate at runtime, depending on the context
1106 # module, and can even mutate at runtime, depending on the context
1107 # (Python makes no guarantees on it). In contrast, __builtin__ is
1107 # (Python makes no guarantees on it). In contrast, __builtin__ is
1108 # always a module object, though it must be explicitly imported.
1108 # always a module object, though it must be explicitly imported.
1109
1109
1110 # For more details:
1110 # For more details:
1111 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1111 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1112 ns = dict()
1112 ns = dict()
1113
1113
1114 # Put 'help' in the user namespace
1114 # Put 'help' in the user namespace
1115 try:
1115 try:
1116 from site import _Helper
1116 from site import _Helper
1117 ns['help'] = _Helper()
1117 ns['help'] = _Helper()
1118 except ImportError:
1118 except ImportError:
1119 warn('help() not available - check site.py')
1119 warn('help() not available - check site.py')
1120
1120
1121 # make global variables for user access to the histories
1121 # make global variables for user access to the histories
1122 ns['_ih'] = self.history_manager.input_hist_parsed
1122 ns['_ih'] = self.history_manager.input_hist_parsed
1123 ns['_oh'] = self.history_manager.output_hist
1123 ns['_oh'] = self.history_manager.output_hist
1124 ns['_dh'] = self.history_manager.dir_hist
1124 ns['_dh'] = self.history_manager.dir_hist
1125
1125
1126 ns['_sh'] = shadowns
1126 ns['_sh'] = shadowns
1127
1127
1128 # user aliases to input and output histories. These shouldn't show up
1128 # user aliases to input and output histories. These shouldn't show up
1129 # in %who, as they can have very large reprs.
1129 # in %who, as they can have very large reprs.
1130 ns['In'] = self.history_manager.input_hist_parsed
1130 ns['In'] = self.history_manager.input_hist_parsed
1131 ns['Out'] = self.history_manager.output_hist
1131 ns['Out'] = self.history_manager.output_hist
1132
1132
1133 # Store myself as the public api!!!
1133 # Store myself as the public api!!!
1134 ns['get_ipython'] = self.get_ipython
1134 ns['get_ipython'] = self.get_ipython
1135
1135
1136 ns['exit'] = self.exiter
1136 ns['exit'] = self.exiter
1137 ns['quit'] = self.exiter
1137 ns['quit'] = self.exiter
1138
1138
1139 # Sync what we've added so far to user_ns_hidden so these aren't seen
1139 # Sync what we've added so far to user_ns_hidden so these aren't seen
1140 # by %who
1140 # by %who
1141 self.user_ns_hidden.update(ns)
1141 self.user_ns_hidden.update(ns)
1142
1142
1143 # Anything put into ns now would show up in %who. Think twice before
1143 # Anything put into ns now would show up in %who. Think twice before
1144 # putting anything here, as we really want %who to show the user their
1144 # putting anything here, as we really want %who to show the user their
1145 # stuff, not our variables.
1145 # stuff, not our variables.
1146
1146
1147 # Finally, update the real user's namespace
1147 # Finally, update the real user's namespace
1148 self.user_ns.update(ns)
1148 self.user_ns.update(ns)
1149
1149
1150 @property
1150 @property
1151 def all_ns_refs(self):
1151 def all_ns_refs(self):
1152 """Get a list of references to all the namespace dictionaries in which
1152 """Get a list of references to all the namespace dictionaries in which
1153 IPython might store a user-created object.
1153 IPython might store a user-created object.
1154
1154
1155 Note that this does not include the displayhook, which also caches
1155 Note that this does not include the displayhook, which also caches
1156 objects from the output."""
1156 objects from the output."""
1157 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1157 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1158 [m.__dict__ for m in self._main_mod_cache.values()]
1158 [m.__dict__ for m in self._main_mod_cache.values()]
1159
1159
1160 def reset(self, new_session=True):
1160 def reset(self, new_session=True):
1161 """Clear all internal namespaces, and attempt to release references to
1161 """Clear all internal namespaces, and attempt to release references to
1162 user objects.
1162 user objects.
1163
1163
1164 If new_session is True, a new history session will be opened.
1164 If new_session is True, a new history session will be opened.
1165 """
1165 """
1166 # Clear histories
1166 # Clear histories
1167 self.history_manager.reset(new_session)
1167 self.history_manager.reset(new_session)
1168 # Reset counter used to index all histories
1168 # Reset counter used to index all histories
1169 if new_session:
1169 if new_session:
1170 self.execution_count = 1
1170 self.execution_count = 1
1171
1171
1172 # Flush cached output items
1172 # Flush cached output items
1173 if self.displayhook.do_full_cache:
1173 if self.displayhook.do_full_cache:
1174 self.displayhook.flush()
1174 self.displayhook.flush()
1175
1175
1176 # The main execution namespaces must be cleared very carefully,
1176 # The main execution namespaces must be cleared very carefully,
1177 # skipping the deletion of the builtin-related keys, because doing so
1177 # skipping the deletion of the builtin-related keys, because doing so
1178 # would cause errors in many object's __del__ methods.
1178 # would cause errors in many object's __del__ methods.
1179 if self.user_ns is not self.user_global_ns:
1179 if self.user_ns is not self.user_global_ns:
1180 self.user_ns.clear()
1180 self.user_ns.clear()
1181 ns = self.user_global_ns
1181 ns = self.user_global_ns
1182 drop_keys = set(ns.keys())
1182 drop_keys = set(ns.keys())
1183 drop_keys.discard('__builtin__')
1183 drop_keys.discard('__builtin__')
1184 drop_keys.discard('__builtins__')
1184 drop_keys.discard('__builtins__')
1185 drop_keys.discard('__name__')
1185 drop_keys.discard('__name__')
1186 for k in drop_keys:
1186 for k in drop_keys:
1187 del ns[k]
1187 del ns[k]
1188
1188
1189 self.user_ns_hidden.clear()
1189 self.user_ns_hidden.clear()
1190
1190
1191 # Restore the user namespaces to minimal usability
1191 # Restore the user namespaces to minimal usability
1192 self.init_user_ns()
1192 self.init_user_ns()
1193
1193
1194 # Restore the default and user aliases
1194 # Restore the default and user aliases
1195 self.alias_manager.clear_aliases()
1195 self.alias_manager.clear_aliases()
1196 self.alias_manager.init_aliases()
1196 self.alias_manager.init_aliases()
1197
1197
1198 # Flush the private list of module references kept for script
1198 # Flush the private list of module references kept for script
1199 # execution protection
1199 # execution protection
1200 self.clear_main_mod_cache()
1200 self.clear_main_mod_cache()
1201
1201
1202 def del_var(self, varname, by_name=False):
1202 def del_var(self, varname, by_name=False):
1203 """Delete a variable from the various namespaces, so that, as
1203 """Delete a variable from the various namespaces, so that, as
1204 far as possible, we're not keeping any hidden references to it.
1204 far as possible, we're not keeping any hidden references to it.
1205
1205
1206 Parameters
1206 Parameters
1207 ----------
1207 ----------
1208 varname : str
1208 varname : str
1209 The name of the variable to delete.
1209 The name of the variable to delete.
1210 by_name : bool
1210 by_name : bool
1211 If True, delete variables with the given name in each
1211 If True, delete variables with the given name in each
1212 namespace. If False (default), find the variable in the user
1212 namespace. If False (default), find the variable in the user
1213 namespace, and delete references to it.
1213 namespace, and delete references to it.
1214 """
1214 """
1215 if varname in ('__builtin__', '__builtins__'):
1215 if varname in ('__builtin__', '__builtins__'):
1216 raise ValueError("Refusing to delete %s" % varname)
1216 raise ValueError("Refusing to delete %s" % varname)
1217
1217
1218 ns_refs = self.all_ns_refs
1218 ns_refs = self.all_ns_refs
1219
1219
1220 if by_name: # Delete by name
1220 if by_name: # Delete by name
1221 for ns in ns_refs:
1221 for ns in ns_refs:
1222 try:
1222 try:
1223 del ns[varname]
1223 del ns[varname]
1224 except KeyError:
1224 except KeyError:
1225 pass
1225 pass
1226 else: # Delete by object
1226 else: # Delete by object
1227 try:
1227 try:
1228 obj = self.user_ns[varname]
1228 obj = self.user_ns[varname]
1229 except KeyError:
1229 except KeyError:
1230 raise NameError("name '%s' is not defined" % varname)
1230 raise NameError("name '%s' is not defined" % varname)
1231 # Also check in output history
1231 # Also check in output history
1232 ns_refs.append(self.history_manager.output_hist)
1232 ns_refs.append(self.history_manager.output_hist)
1233 for ns in ns_refs:
1233 for ns in ns_refs:
1234 to_delete = [n for n, o in ns.iteritems() if o is obj]
1234 to_delete = [n for n, o in ns.iteritems() if o is obj]
1235 for name in to_delete:
1235 for name in to_delete:
1236 del ns[name]
1236 del ns[name]
1237
1237
1238 # displayhook keeps extra references, but not in a dictionary
1238 # displayhook keeps extra references, but not in a dictionary
1239 for name in ('_', '__', '___'):
1239 for name in ('_', '__', '___'):
1240 if getattr(self.displayhook, name) is obj:
1240 if getattr(self.displayhook, name) is obj:
1241 setattr(self.displayhook, name, None)
1241 setattr(self.displayhook, name, None)
1242
1242
1243 def reset_selective(self, regex=None):
1243 def reset_selective(self, regex=None):
1244 """Clear selective variables from internal namespaces based on a
1244 """Clear selective variables from internal namespaces based on a
1245 specified regular expression.
1245 specified regular expression.
1246
1246
1247 Parameters
1247 Parameters
1248 ----------
1248 ----------
1249 regex : string or compiled pattern, optional
1249 regex : string or compiled pattern, optional
1250 A regular expression pattern that will be used in searching
1250 A regular expression pattern that will be used in searching
1251 variable names in the users namespaces.
1251 variable names in the users namespaces.
1252 """
1252 """
1253 if regex is not None:
1253 if regex is not None:
1254 try:
1254 try:
1255 m = re.compile(regex)
1255 m = re.compile(regex)
1256 except TypeError:
1256 except TypeError:
1257 raise TypeError('regex must be a string or compiled pattern')
1257 raise TypeError('regex must be a string or compiled pattern')
1258 # Search for keys in each namespace that match the given regex
1258 # Search for keys in each namespace that match the given regex
1259 # If a match is found, delete the key/value pair.
1259 # If a match is found, delete the key/value pair.
1260 for ns in self.all_ns_refs:
1260 for ns in self.all_ns_refs:
1261 for var in ns:
1261 for var in ns:
1262 if m.search(var):
1262 if m.search(var):
1263 del ns[var]
1263 del ns[var]
1264
1264
1265 def push(self, variables, interactive=True):
1265 def push(self, variables, interactive=True):
1266 """Inject a group of variables into the IPython user namespace.
1266 """Inject a group of variables into the IPython user namespace.
1267
1267
1268 Parameters
1268 Parameters
1269 ----------
1269 ----------
1270 variables : dict, str or list/tuple of str
1270 variables : dict, str or list/tuple of str
1271 The variables to inject into the user's namespace. If a dict, a
1271 The variables to inject into the user's namespace. If a dict, a
1272 simple update is done. If a str, the string is assumed to have
1272 simple update is done. If a str, the string is assumed to have
1273 variable names separated by spaces. A list/tuple of str can also
1273 variable names separated by spaces. A list/tuple of str can also
1274 be used to give the variable names. If just the variable names are
1274 be used to give the variable names. If just the variable names are
1275 give (list/tuple/str) then the variable values looked up in the
1275 give (list/tuple/str) then the variable values looked up in the
1276 callers frame.
1276 callers frame.
1277 interactive : bool
1277 interactive : bool
1278 If True (default), the variables will be listed with the ``who``
1278 If True (default), the variables will be listed with the ``who``
1279 magic.
1279 magic.
1280 """
1280 """
1281 vdict = None
1281 vdict = None
1282
1282
1283 # We need a dict of name/value pairs to do namespace updates.
1283 # We need a dict of name/value pairs to do namespace updates.
1284 if isinstance(variables, dict):
1284 if isinstance(variables, dict):
1285 vdict = variables
1285 vdict = variables
1286 elif isinstance(variables, (basestring, list, tuple)):
1286 elif isinstance(variables, (basestring, list, tuple)):
1287 if isinstance(variables, basestring):
1287 if isinstance(variables, basestring):
1288 vlist = variables.split()
1288 vlist = variables.split()
1289 else:
1289 else:
1290 vlist = variables
1290 vlist = variables
1291 vdict = {}
1291 vdict = {}
1292 cf = sys._getframe(1)
1292 cf = sys._getframe(1)
1293 for name in vlist:
1293 for name in vlist:
1294 try:
1294 try:
1295 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1295 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1296 except:
1296 except:
1297 print('Could not get variable %s from %s' %
1297 print('Could not get variable %s from %s' %
1298 (name,cf.f_code.co_name))
1298 (name,cf.f_code.co_name))
1299 else:
1299 else:
1300 raise ValueError('variables must be a dict/str/list/tuple')
1300 raise ValueError('variables must be a dict/str/list/tuple')
1301
1301
1302 # Propagate variables to user namespace
1302 # Propagate variables to user namespace
1303 self.user_ns.update(vdict)
1303 self.user_ns.update(vdict)
1304
1304
1305 # And configure interactive visibility
1305 # And configure interactive visibility
1306 user_ns_hidden = self.user_ns_hidden
1306 user_ns_hidden = self.user_ns_hidden
1307 if interactive:
1307 if interactive:
1308 for name in vdict:
1308 for name in vdict:
1309 user_ns_hidden.pop(name, None)
1309 user_ns_hidden.pop(name, None)
1310 else:
1310 else:
1311 user_ns_hidden.update(vdict)
1311 user_ns_hidden.update(vdict)
1312
1312
1313 def drop_by_id(self, variables):
1313 def drop_by_id(self, variables):
1314 """Remove a dict of variables from the user namespace, if they are the
1314 """Remove a dict of variables from the user namespace, if they are the
1315 same as the values in the dictionary.
1315 same as the values in the dictionary.
1316
1316
1317 This is intended for use by extensions: variables that they've added can
1317 This is intended for use by extensions: variables that they've added can
1318 be taken back out if they are unloaded, without removing any that the
1318 be taken back out if they are unloaded, without removing any that the
1319 user has overwritten.
1319 user has overwritten.
1320
1320
1321 Parameters
1321 Parameters
1322 ----------
1322 ----------
1323 variables : dict
1323 variables : dict
1324 A dictionary mapping object names (as strings) to the objects.
1324 A dictionary mapping object names (as strings) to the objects.
1325 """
1325 """
1326 for name, obj in variables.iteritems():
1326 for name, obj in variables.iteritems():
1327 if name in self.user_ns and self.user_ns[name] is obj:
1327 if name in self.user_ns and self.user_ns[name] is obj:
1328 del self.user_ns[name]
1328 del self.user_ns[name]
1329 self.user_ns_hidden.pop(name, None)
1329 self.user_ns_hidden.pop(name, None)
1330
1330
1331 #-------------------------------------------------------------------------
1331 #-------------------------------------------------------------------------
1332 # Things related to object introspection
1332 # Things related to object introspection
1333 #-------------------------------------------------------------------------
1333 #-------------------------------------------------------------------------
1334
1334
1335 def _ofind(self, oname, namespaces=None):
1335 def _ofind(self, oname, namespaces=None):
1336 """Find an object in the available namespaces.
1336 """Find an object in the available namespaces.
1337
1337
1338 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1338 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1339
1339
1340 Has special code to detect magic functions.
1340 Has special code to detect magic functions.
1341 """
1341 """
1342 oname = oname.strip()
1342 oname = oname.strip()
1343 #print '1- oname: <%r>' % oname # dbg
1343 #print '1- oname: <%r>' % oname # dbg
1344 if not oname.startswith(ESC_MAGIC) and \
1344 if not oname.startswith(ESC_MAGIC) and \
1345 not oname.startswith(ESC_MAGIC2) and \
1345 not oname.startswith(ESC_MAGIC2) and \
1346 not py3compat.isidentifier(oname, dotted=True):
1346 not py3compat.isidentifier(oname, dotted=True):
1347 return dict(found=False)
1347 return dict(found=False)
1348
1348
1349 alias_ns = None
1349 alias_ns = None
1350 if namespaces is None:
1350 if namespaces is None:
1351 # Namespaces to search in:
1351 # Namespaces to search in:
1352 # Put them in a list. The order is important so that we
1352 # Put them in a list. The order is important so that we
1353 # find things in the same order that Python finds them.
1353 # find things in the same order that Python finds them.
1354 namespaces = [ ('Interactive', self.user_ns),
1354 namespaces = [ ('Interactive', self.user_ns),
1355 ('Interactive (global)', self.user_global_ns),
1355 ('Interactive (global)', self.user_global_ns),
1356 ('Python builtin', builtin_mod.__dict__),
1356 ('Python builtin', builtin_mod.__dict__),
1357 ('Alias', self.alias_manager.alias_table),
1357 ('Alias', self.alias_manager.alias_table),
1358 ]
1358 ]
1359 alias_ns = self.alias_manager.alias_table
1359 alias_ns = self.alias_manager.alias_table
1360
1360
1361 # initialize results to 'null'
1361 # initialize results to 'null'
1362 found = False; obj = None; ospace = None; ds = None;
1362 found = False; obj = None; ospace = None; ds = None;
1363 ismagic = False; isalias = False; parent = None
1363 ismagic = False; isalias = False; parent = None
1364
1364
1365 # We need to special-case 'print', which as of python2.6 registers as a
1365 # We need to special-case 'print', which as of python2.6 registers as a
1366 # function but should only be treated as one if print_function was
1366 # function but should only be treated as one if print_function was
1367 # loaded with a future import. In this case, just bail.
1367 # loaded with a future import. In this case, just bail.
1368 if (oname == 'print' and not py3compat.PY3 and not \
1368 if (oname == 'print' and not py3compat.PY3 and not \
1369 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1369 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1370 return {'found':found, 'obj':obj, 'namespace':ospace,
1370 return {'found':found, 'obj':obj, 'namespace':ospace,
1371 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1371 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1372
1372
1373 # Look for the given name by splitting it in parts. If the head is
1373 # Look for the given name by splitting it in parts. If the head is
1374 # found, then we look for all the remaining parts as members, and only
1374 # found, then we look for all the remaining parts as members, and only
1375 # declare success if we can find them all.
1375 # declare success if we can find them all.
1376 oname_parts = oname.split('.')
1376 oname_parts = oname.split('.')
1377 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1377 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1378 for nsname,ns in namespaces:
1378 for nsname,ns in namespaces:
1379 try:
1379 try:
1380 obj = ns[oname_head]
1380 obj = ns[oname_head]
1381 except KeyError:
1381 except KeyError:
1382 continue
1382 continue
1383 else:
1383 else:
1384 #print 'oname_rest:', oname_rest # dbg
1384 #print 'oname_rest:', oname_rest # dbg
1385 for part in oname_rest:
1385 for part in oname_rest:
1386 try:
1386 try:
1387 parent = obj
1387 parent = obj
1388 obj = getattr(obj,part)
1388 obj = getattr(obj,part)
1389 except:
1389 except:
1390 # Blanket except b/c some badly implemented objects
1390 # Blanket except b/c some badly implemented objects
1391 # allow __getattr__ to raise exceptions other than
1391 # allow __getattr__ to raise exceptions other than
1392 # AttributeError, which then crashes IPython.
1392 # AttributeError, which then crashes IPython.
1393 break
1393 break
1394 else:
1394 else:
1395 # If we finish the for loop (no break), we got all members
1395 # If we finish the for loop (no break), we got all members
1396 found = True
1396 found = True
1397 ospace = nsname
1397 ospace = nsname
1398 if ns == alias_ns:
1398 if ns == alias_ns:
1399 isalias = True
1399 isalias = True
1400 break # namespace loop
1400 break # namespace loop
1401
1401
1402 # Try to see if it's magic
1402 # Try to see if it's magic
1403 if not found:
1403 if not found:
1404 obj = None
1404 obj = None
1405 if oname.startswith(ESC_MAGIC2):
1405 if oname.startswith(ESC_MAGIC2):
1406 oname = oname.lstrip(ESC_MAGIC2)
1406 oname = oname.lstrip(ESC_MAGIC2)
1407 obj = self.find_cell_magic(oname)
1407 obj = self.find_cell_magic(oname)
1408 elif oname.startswith(ESC_MAGIC):
1408 elif oname.startswith(ESC_MAGIC):
1409 oname = oname.lstrip(ESC_MAGIC)
1409 oname = oname.lstrip(ESC_MAGIC)
1410 obj = self.find_line_magic(oname)
1410 obj = self.find_line_magic(oname)
1411 else:
1411 else:
1412 # search without prefix, so run? will find %run?
1412 # search without prefix, so run? will find %run?
1413 obj = self.find_line_magic(oname)
1413 obj = self.find_line_magic(oname)
1414 if obj is None:
1414 if obj is None:
1415 obj = self.find_cell_magic(oname)
1415 obj = self.find_cell_magic(oname)
1416 if obj is not None:
1416 if obj is not None:
1417 found = True
1417 found = True
1418 ospace = 'IPython internal'
1418 ospace = 'IPython internal'
1419 ismagic = True
1419 ismagic = True
1420
1420
1421 # Last try: special-case some literals like '', [], {}, etc:
1421 # Last try: special-case some literals like '', [], {}, etc:
1422 if not found and oname_head in ["''",'""','[]','{}','()']:
1422 if not found and oname_head in ["''",'""','[]','{}','()']:
1423 obj = eval(oname_head)
1423 obj = eval(oname_head)
1424 found = True
1424 found = True
1425 ospace = 'Interactive'
1425 ospace = 'Interactive'
1426
1426
1427 return {'found':found, 'obj':obj, 'namespace':ospace,
1427 return {'found':found, 'obj':obj, 'namespace':ospace,
1428 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1428 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1429
1429
1430 def _ofind_property(self, oname, info):
1430 def _ofind_property(self, oname, info):
1431 """Second part of object finding, to look for property details."""
1431 """Second part of object finding, to look for property details."""
1432 if info.found:
1432 if info.found:
1433 # Get the docstring of the class property if it exists.
1433 # Get the docstring of the class property if it exists.
1434 path = oname.split('.')
1434 path = oname.split('.')
1435 root = '.'.join(path[:-1])
1435 root = '.'.join(path[:-1])
1436 if info.parent is not None:
1436 if info.parent is not None:
1437 try:
1437 try:
1438 target = getattr(info.parent, '__class__')
1438 target = getattr(info.parent, '__class__')
1439 # The object belongs to a class instance.
1439 # The object belongs to a class instance.
1440 try:
1440 try:
1441 target = getattr(target, path[-1])
1441 target = getattr(target, path[-1])
1442 # The class defines the object.
1442 # The class defines the object.
1443 if isinstance(target, property):
1443 if isinstance(target, property):
1444 oname = root + '.__class__.' + path[-1]
1444 oname = root + '.__class__.' + path[-1]
1445 info = Struct(self._ofind(oname))
1445 info = Struct(self._ofind(oname))
1446 except AttributeError: pass
1446 except AttributeError: pass
1447 except AttributeError: pass
1447 except AttributeError: pass
1448
1448
1449 # We return either the new info or the unmodified input if the object
1449 # We return either the new info or the unmodified input if the object
1450 # hadn't been found
1450 # hadn't been found
1451 return info
1451 return info
1452
1452
1453 def _object_find(self, oname, namespaces=None):
1453 def _object_find(self, oname, namespaces=None):
1454 """Find an object and return a struct with info about it."""
1454 """Find an object and return a struct with info about it."""
1455 inf = Struct(self._ofind(oname, namespaces))
1455 inf = Struct(self._ofind(oname, namespaces))
1456 return Struct(self._ofind_property(oname, inf))
1456 return Struct(self._ofind_property(oname, inf))
1457
1457
1458 def _inspect(self, meth, oname, namespaces=None, **kw):
1458 def _inspect(self, meth, oname, namespaces=None, **kw):
1459 """Generic interface to the inspector system.
1459 """Generic interface to the inspector system.
1460
1460
1461 This function is meant to be called by pdef, pdoc & friends."""
1461 This function is meant to be called by pdef, pdoc & friends."""
1462 info = self._object_find(oname, namespaces)
1462 info = self._object_find(oname, namespaces)
1463 if info.found:
1463 if info.found:
1464 pmethod = getattr(self.inspector, meth)
1464 pmethod = getattr(self.inspector, meth)
1465 formatter = format_screen if info.ismagic else None
1465 formatter = format_screen if info.ismagic else None
1466 if meth == 'pdoc':
1466 if meth == 'pdoc':
1467 pmethod(info.obj, oname, formatter)
1467 pmethod(info.obj, oname, formatter)
1468 elif meth == 'pinfo':
1468 elif meth == 'pinfo':
1469 pmethod(info.obj, oname, formatter, info, **kw)
1469 pmethod(info.obj, oname, formatter, info, **kw)
1470 else:
1470 else:
1471 pmethod(info.obj, oname)
1471 pmethod(info.obj, oname)
1472 else:
1472 else:
1473 print('Object `%s` not found.' % oname)
1473 print('Object `%s` not found.' % oname)
1474 return 'not found' # so callers can take other action
1474 return 'not found' # so callers can take other action
1475
1475
1476 def object_inspect(self, oname, detail_level=0):
1476 def object_inspect(self, oname, detail_level=0):
1477 with self.builtin_trap:
1477 with self.builtin_trap:
1478 info = self._object_find(oname)
1478 info = self._object_find(oname)
1479 if info.found:
1479 if info.found:
1480 return self.inspector.info(info.obj, oname, info=info,
1480 return self.inspector.info(info.obj, oname, info=info,
1481 detail_level=detail_level
1481 detail_level=detail_level
1482 )
1482 )
1483 else:
1483 else:
1484 return oinspect.object_info(name=oname, found=False)
1484 return oinspect.object_info(name=oname, found=False)
1485
1485
1486 #-------------------------------------------------------------------------
1486 #-------------------------------------------------------------------------
1487 # Things related to history management
1487 # Things related to history management
1488 #-------------------------------------------------------------------------
1488 #-------------------------------------------------------------------------
1489
1489
1490 def init_history(self):
1490 def init_history(self):
1491 """Sets up the command history, and starts regular autosaves."""
1491 """Sets up the command history, and starts regular autosaves."""
1492 self.history_manager = HistoryManager(shell=self, parent=self)
1492 self.history_manager = HistoryManager(shell=self, parent=self)
1493 self.configurables.append(self.history_manager)
1493 self.configurables.append(self.history_manager)
1494
1494
1495 #-------------------------------------------------------------------------
1495 #-------------------------------------------------------------------------
1496 # Things related to exception handling and tracebacks (not debugging)
1496 # Things related to exception handling and tracebacks (not debugging)
1497 #-------------------------------------------------------------------------
1497 #-------------------------------------------------------------------------
1498
1498
1499 def init_traceback_handlers(self, custom_exceptions):
1499 def init_traceback_handlers(self, custom_exceptions):
1500 # Syntax error handler.
1500 # Syntax error handler.
1501 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1501 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1502
1502
1503 # The interactive one is initialized with an offset, meaning we always
1503 # The interactive one is initialized with an offset, meaning we always
1504 # want to remove the topmost item in the traceback, which is our own
1504 # want to remove the topmost item in the traceback, which is our own
1505 # internal code. Valid modes: ['Plain','Context','Verbose']
1505 # internal code. Valid modes: ['Plain','Context','Verbose']
1506 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1506 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1507 color_scheme='NoColor',
1507 color_scheme='NoColor',
1508 tb_offset = 1,
1508 tb_offset = 1,
1509 check_cache=check_linecache_ipython)
1509 check_cache=check_linecache_ipython)
1510
1510
1511 # The instance will store a pointer to the system-wide exception hook,
1511 # The instance will store a pointer to the system-wide exception hook,
1512 # so that runtime code (such as magics) can access it. This is because
1512 # so that runtime code (such as magics) can access it. This is because
1513 # during the read-eval loop, it may get temporarily overwritten.
1513 # during the read-eval loop, it may get temporarily overwritten.
1514 self.sys_excepthook = sys.excepthook
1514 self.sys_excepthook = sys.excepthook
1515
1515
1516 # and add any custom exception handlers the user may have specified
1516 # and add any custom exception handlers the user may have specified
1517 self.set_custom_exc(*custom_exceptions)
1517 self.set_custom_exc(*custom_exceptions)
1518
1518
1519 # Set the exception mode
1519 # Set the exception mode
1520 self.InteractiveTB.set_mode(mode=self.xmode)
1520 self.InteractiveTB.set_mode(mode=self.xmode)
1521
1521
1522 def set_custom_exc(self, exc_tuple, handler):
1522 def set_custom_exc(self, exc_tuple, handler):
1523 """set_custom_exc(exc_tuple,handler)
1523 """set_custom_exc(exc_tuple,handler)
1524
1524
1525 Set a custom exception handler, which will be called if any of the
1525 Set a custom exception handler, which will be called if any of the
1526 exceptions in exc_tuple occur in the mainloop (specifically, in the
1526 exceptions in exc_tuple occur in the mainloop (specifically, in the
1527 run_code() method).
1527 run_code() method).
1528
1528
1529 Parameters
1529 Parameters
1530 ----------
1530 ----------
1531
1531
1532 exc_tuple : tuple of exception classes
1532 exc_tuple : tuple of exception classes
1533 A *tuple* of exception classes, for which to call the defined
1533 A *tuple* of exception classes, for which to call the defined
1534 handler. It is very important that you use a tuple, and NOT A
1534 handler. It is very important that you use a tuple, and NOT A
1535 LIST here, because of the way Python's except statement works. If
1535 LIST here, because of the way Python's except statement works. If
1536 you only want to trap a single exception, use a singleton tuple::
1536 you only want to trap a single exception, use a singleton tuple::
1537
1537
1538 exc_tuple == (MyCustomException,)
1538 exc_tuple == (MyCustomException,)
1539
1539
1540 handler : callable
1540 handler : callable
1541 handler must have the following signature::
1541 handler must have the following signature::
1542
1542
1543 def my_handler(self, etype, value, tb, tb_offset=None):
1543 def my_handler(self, etype, value, tb, tb_offset=None):
1544 ...
1544 ...
1545 return structured_traceback
1545 return structured_traceback
1546
1546
1547 Your handler must return a structured traceback (a list of strings),
1547 Your handler must return a structured traceback (a list of strings),
1548 or None.
1548 or None.
1549
1549
1550 This will be made into an instance method (via types.MethodType)
1550 This will be made into an instance method (via types.MethodType)
1551 of IPython itself, and it will be called if any of the exceptions
1551 of IPython itself, and it will be called if any of the exceptions
1552 listed in the exc_tuple are caught. If the handler is None, an
1552 listed in the exc_tuple are caught. If the handler is None, an
1553 internal basic one is used, which just prints basic info.
1553 internal basic one is used, which just prints basic info.
1554
1554
1555 To protect IPython from crashes, if your handler ever raises an
1555 To protect IPython from crashes, if your handler ever raises an
1556 exception or returns an invalid result, it will be immediately
1556 exception or returns an invalid result, it will be immediately
1557 disabled.
1557 disabled.
1558
1558
1559 WARNING: by putting in your own exception handler into IPython's main
1559 WARNING: by putting in your own exception handler into IPython's main
1560 execution loop, you run a very good chance of nasty crashes. This
1560 execution loop, you run a very good chance of nasty crashes. This
1561 facility should only be used if you really know what you are doing."""
1561 facility should only be used if you really know what you are doing."""
1562
1562
1563 assert type(exc_tuple)==type(()) , \
1563 assert type(exc_tuple)==type(()) , \
1564 "The custom exceptions must be given AS A TUPLE."
1564 "The custom exceptions must be given AS A TUPLE."
1565
1565
1566 def dummy_handler(self,etype,value,tb,tb_offset=None):
1566 def dummy_handler(self,etype,value,tb,tb_offset=None):
1567 print('*** Simple custom exception handler ***')
1567 print('*** Simple custom exception handler ***')
1568 print('Exception type :',etype)
1568 print('Exception type :',etype)
1569 print('Exception value:',value)
1569 print('Exception value:',value)
1570 print('Traceback :',tb)
1570 print('Traceback :',tb)
1571 #print 'Source code :','\n'.join(self.buffer)
1571 #print 'Source code :','\n'.join(self.buffer)
1572
1572
1573 def validate_stb(stb):
1573 def validate_stb(stb):
1574 """validate structured traceback return type
1574 """validate structured traceback return type
1575
1575
1576 return type of CustomTB *should* be a list of strings, but allow
1576 return type of CustomTB *should* be a list of strings, but allow
1577 single strings or None, which are harmless.
1577 single strings or None, which are harmless.
1578
1578
1579 This function will *always* return a list of strings,
1579 This function will *always* return a list of strings,
1580 and will raise a TypeError if stb is inappropriate.
1580 and will raise a TypeError if stb is inappropriate.
1581 """
1581 """
1582 msg = "CustomTB must return list of strings, not %r" % stb
1582 msg = "CustomTB must return list of strings, not %r" % stb
1583 if stb is None:
1583 if stb is None:
1584 return []
1584 return []
1585 elif isinstance(stb, basestring):
1585 elif isinstance(stb, basestring):
1586 return [stb]
1586 return [stb]
1587 elif not isinstance(stb, list):
1587 elif not isinstance(stb, list):
1588 raise TypeError(msg)
1588 raise TypeError(msg)
1589 # it's a list
1589 # it's a list
1590 for line in stb:
1590 for line in stb:
1591 # check every element
1591 # check every element
1592 if not isinstance(line, basestring):
1592 if not isinstance(line, basestring):
1593 raise TypeError(msg)
1593 raise TypeError(msg)
1594 return stb
1594 return stb
1595
1595
1596 if handler is None:
1596 if handler is None:
1597 wrapped = dummy_handler
1597 wrapped = dummy_handler
1598 else:
1598 else:
1599 def wrapped(self,etype,value,tb,tb_offset=None):
1599 def wrapped(self,etype,value,tb,tb_offset=None):
1600 """wrap CustomTB handler, to protect IPython from user code
1600 """wrap CustomTB handler, to protect IPython from user code
1601
1601
1602 This makes it harder (but not impossible) for custom exception
1602 This makes it harder (but not impossible) for custom exception
1603 handlers to crash IPython.
1603 handlers to crash IPython.
1604 """
1604 """
1605 try:
1605 try:
1606 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1606 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1607 return validate_stb(stb)
1607 return validate_stb(stb)
1608 except:
1608 except:
1609 # clear custom handler immediately
1609 # clear custom handler immediately
1610 self.set_custom_exc((), None)
1610 self.set_custom_exc((), None)
1611 print("Custom TB Handler failed, unregistering", file=io.stderr)
1611 print("Custom TB Handler failed, unregistering", file=io.stderr)
1612 # show the exception in handler first
1612 # show the exception in handler first
1613 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1613 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1614 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1614 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1615 print("The original exception:", file=io.stdout)
1615 print("The original exception:", file=io.stdout)
1616 stb = self.InteractiveTB.structured_traceback(
1616 stb = self.InteractiveTB.structured_traceback(
1617 (etype,value,tb), tb_offset=tb_offset
1617 (etype,value,tb), tb_offset=tb_offset
1618 )
1618 )
1619 return stb
1619 return stb
1620
1620
1621 self.CustomTB = types.MethodType(wrapped,self)
1621 self.CustomTB = types.MethodType(wrapped,self)
1622 self.custom_exceptions = exc_tuple
1622 self.custom_exceptions = exc_tuple
1623
1623
1624 def excepthook(self, etype, value, tb):
1624 def excepthook(self, etype, value, tb):
1625 """One more defense for GUI apps that call sys.excepthook.
1625 """One more defense for GUI apps that call sys.excepthook.
1626
1626
1627 GUI frameworks like wxPython trap exceptions and call
1627 GUI frameworks like wxPython trap exceptions and call
1628 sys.excepthook themselves. I guess this is a feature that
1628 sys.excepthook themselves. I guess this is a feature that
1629 enables them to keep running after exceptions that would
1629 enables them to keep running after exceptions that would
1630 otherwise kill their mainloop. This is a bother for IPython
1630 otherwise kill their mainloop. This is a bother for IPython
1631 which excepts to catch all of the program exceptions with a try:
1631 which excepts to catch all of the program exceptions with a try:
1632 except: statement.
1632 except: statement.
1633
1633
1634 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1634 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1635 any app directly invokes sys.excepthook, it will look to the user like
1635 any app directly invokes sys.excepthook, it will look to the user like
1636 IPython crashed. In order to work around this, we can disable the
1636 IPython crashed. In order to work around this, we can disable the
1637 CrashHandler and replace it with this excepthook instead, which prints a
1637 CrashHandler and replace it with this excepthook instead, which prints a
1638 regular traceback using our InteractiveTB. In this fashion, apps which
1638 regular traceback using our InteractiveTB. In this fashion, apps which
1639 call sys.excepthook will generate a regular-looking exception from
1639 call sys.excepthook will generate a regular-looking exception from
1640 IPython, and the CrashHandler will only be triggered by real IPython
1640 IPython, and the CrashHandler will only be triggered by real IPython
1641 crashes.
1641 crashes.
1642
1642
1643 This hook should be used sparingly, only in places which are not likely
1643 This hook should be used sparingly, only in places which are not likely
1644 to be true IPython errors.
1644 to be true IPython errors.
1645 """
1645 """
1646 self.showtraceback((etype,value,tb),tb_offset=0)
1646 self.showtraceback((etype,value,tb),tb_offset=0)
1647
1647
1648 def _get_exc_info(self, exc_tuple=None):
1648 def _get_exc_info(self, exc_tuple=None):
1649 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1649 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1650
1650
1651 Ensures sys.last_type,value,traceback hold the exc_info we found,
1651 Ensures sys.last_type,value,traceback hold the exc_info we found,
1652 from whichever source.
1652 from whichever source.
1653
1653
1654 raises ValueError if none of these contain any information
1654 raises ValueError if none of these contain any information
1655 """
1655 """
1656 if exc_tuple is None:
1656 if exc_tuple is None:
1657 etype, value, tb = sys.exc_info()
1657 etype, value, tb = sys.exc_info()
1658 else:
1658 else:
1659 etype, value, tb = exc_tuple
1659 etype, value, tb = exc_tuple
1660
1660
1661 if etype is None:
1661 if etype is None:
1662 if hasattr(sys, 'last_type'):
1662 if hasattr(sys, 'last_type'):
1663 etype, value, tb = sys.last_type, sys.last_value, \
1663 etype, value, tb = sys.last_type, sys.last_value, \
1664 sys.last_traceback
1664 sys.last_traceback
1665
1665
1666 if etype is None:
1666 if etype is None:
1667 raise ValueError("No exception to find")
1667 raise ValueError("No exception to find")
1668
1668
1669 # Now store the exception info in sys.last_type etc.
1669 # Now store the exception info in sys.last_type etc.
1670 # WARNING: these variables are somewhat deprecated and not
1670 # WARNING: these variables are somewhat deprecated and not
1671 # necessarily safe to use in a threaded environment, but tools
1671 # necessarily safe to use in a threaded environment, but tools
1672 # like pdb depend on their existence, so let's set them. If we
1672 # like pdb depend on their existence, so let's set them. If we
1673 # find problems in the field, we'll need to revisit their use.
1673 # find problems in the field, we'll need to revisit their use.
1674 sys.last_type = etype
1674 sys.last_type = etype
1675 sys.last_value = value
1675 sys.last_value = value
1676 sys.last_traceback = tb
1676 sys.last_traceback = tb
1677
1677
1678 return etype, value, tb
1678 return etype, value, tb
1679
1679
1680 def show_usage_error(self, exc):
1680 def show_usage_error(self, exc):
1681 """Show a short message for UsageErrors
1681 """Show a short message for UsageErrors
1682
1682
1683 These are special exceptions that shouldn't show a traceback.
1683 These are special exceptions that shouldn't show a traceback.
1684 """
1684 """
1685 self.write_err("UsageError: %s" % exc)
1685 self.write_err("UsageError: %s" % exc)
1686
1686
1687 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1687 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1688 exception_only=False):
1688 exception_only=False):
1689 """Display the exception that just occurred.
1689 """Display the exception that just occurred.
1690
1690
1691 If nothing is known about the exception, this is the method which
1691 If nothing is known about the exception, this is the method which
1692 should be used throughout the code for presenting user tracebacks,
1692 should be used throughout the code for presenting user tracebacks,
1693 rather than directly invoking the InteractiveTB object.
1693 rather than directly invoking the InteractiveTB object.
1694
1694
1695 A specific showsyntaxerror() also exists, but this method can take
1695 A specific showsyntaxerror() also exists, but this method can take
1696 care of calling it if needed, so unless you are explicitly catching a
1696 care of calling it if needed, so unless you are explicitly catching a
1697 SyntaxError exception, don't try to analyze the stack manually and
1697 SyntaxError exception, don't try to analyze the stack manually and
1698 simply call this method."""
1698 simply call this method."""
1699
1699
1700 try:
1700 try:
1701 try:
1701 try:
1702 etype, value, tb = self._get_exc_info(exc_tuple)
1702 etype, value, tb = self._get_exc_info(exc_tuple)
1703 except ValueError:
1703 except ValueError:
1704 self.write_err('No traceback available to show.\n')
1704 self.write_err('No traceback available to show.\n')
1705 return
1705 return
1706
1706
1707 if issubclass(etype, SyntaxError):
1707 if issubclass(etype, SyntaxError):
1708 # Though this won't be called by syntax errors in the input
1708 # Though this won't be called by syntax errors in the input
1709 # line, there may be SyntaxError cases with imported code.
1709 # line, there may be SyntaxError cases with imported code.
1710 self.showsyntaxerror(filename)
1710 self.showsyntaxerror(filename)
1711 elif etype is UsageError:
1711 elif etype is UsageError:
1712 self.show_usage_error(value)
1712 self.show_usage_error(value)
1713 else:
1713 else:
1714 if exception_only:
1714 if exception_only:
1715 stb = ['An exception has occurred, use %tb to see '
1715 stb = ['An exception has occurred, use %tb to see '
1716 'the full traceback.\n']
1716 'the full traceback.\n']
1717 stb.extend(self.InteractiveTB.get_exception_only(etype,
1717 stb.extend(self.InteractiveTB.get_exception_only(etype,
1718 value))
1718 value))
1719 else:
1719 else:
1720 try:
1720 try:
1721 # Exception classes can customise their traceback - we
1721 # Exception classes can customise their traceback - we
1722 # use this in IPython.parallel for exceptions occurring
1722 # use this in IPython.parallel for exceptions occurring
1723 # in the engines. This should return a list of strings.
1723 # in the engines. This should return a list of strings.
1724 stb = value._render_traceback_()
1724 stb = value._render_traceback_()
1725 except Exception:
1725 except Exception:
1726 stb = self.InteractiveTB.structured_traceback(etype,
1726 stb = self.InteractiveTB.structured_traceback(etype,
1727 value, tb, tb_offset=tb_offset)
1727 value, tb, tb_offset=tb_offset)
1728
1728
1729 self._showtraceback(etype, value, stb)
1729 self._showtraceback(etype, value, stb)
1730 if self.call_pdb:
1730 if self.call_pdb:
1731 # drop into debugger
1731 # drop into debugger
1732 self.debugger(force=True)
1732 self.debugger(force=True)
1733 return
1733 return
1734
1734
1735 # Actually show the traceback
1735 # Actually show the traceback
1736 self._showtraceback(etype, value, stb)
1736 self._showtraceback(etype, value, stb)
1737
1737
1738 except KeyboardInterrupt:
1738 except KeyboardInterrupt:
1739 self.write_err("\nKeyboardInterrupt\n")
1739 self.write_err("\nKeyboardInterrupt\n")
1740
1740
1741 def _showtraceback(self, etype, evalue, stb):
1741 def _showtraceback(self, etype, evalue, stb):
1742 """Actually show a traceback.
1742 """Actually show a traceback.
1743
1743
1744 Subclasses may override this method to put the traceback on a different
1744 Subclasses may override this method to put the traceback on a different
1745 place, like a side channel.
1745 place, like a side channel.
1746 """
1746 """
1747 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1747 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1748
1748
1749 def showsyntaxerror(self, filename=None):
1749 def showsyntaxerror(self, filename=None):
1750 """Display the syntax error that just occurred.
1750 """Display the syntax error that just occurred.
1751
1751
1752 This doesn't display a stack trace because there isn't one.
1752 This doesn't display a stack trace because there isn't one.
1753
1753
1754 If a filename is given, it is stuffed in the exception instead
1754 If a filename is given, it is stuffed in the exception instead
1755 of what was there before (because Python's parser always uses
1755 of what was there before (because Python's parser always uses
1756 "<string>" when reading from a string).
1756 "<string>" when reading from a string).
1757 """
1757 """
1758 etype, value, last_traceback = self._get_exc_info()
1758 etype, value, last_traceback = self._get_exc_info()
1759
1759
1760 if filename and issubclass(etype, SyntaxError):
1760 if filename and issubclass(etype, SyntaxError):
1761 try:
1761 try:
1762 value.filename = filename
1762 value.filename = filename
1763 except:
1763 except:
1764 # Not the format we expect; leave it alone
1764 # Not the format we expect; leave it alone
1765 pass
1765 pass
1766
1766
1767 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1767 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1768 self._showtraceback(etype, value, stb)
1768 self._showtraceback(etype, value, stb)
1769
1769
1770 # This is overridden in TerminalInteractiveShell to show a message about
1770 # This is overridden in TerminalInteractiveShell to show a message about
1771 # the %paste magic.
1771 # the %paste magic.
1772 def showindentationerror(self):
1772 def showindentationerror(self):
1773 """Called by run_cell when there's an IndentationError in code entered
1773 """Called by run_cell when there's an IndentationError in code entered
1774 at the prompt.
1774 at the prompt.
1775
1775
1776 This is overridden in TerminalInteractiveShell to show a message about
1776 This is overridden in TerminalInteractiveShell to show a message about
1777 the %paste magic."""
1777 the %paste magic."""
1778 self.showsyntaxerror()
1778 self.showsyntaxerror()
1779
1779
1780 #-------------------------------------------------------------------------
1780 #-------------------------------------------------------------------------
1781 # Things related to readline
1781 # Things related to readline
1782 #-------------------------------------------------------------------------
1782 #-------------------------------------------------------------------------
1783
1783
1784 def init_readline(self):
1784 def init_readline(self):
1785 """Command history completion/saving/reloading."""
1785 """Command history completion/saving/reloading."""
1786
1786
1787 if self.readline_use:
1787 if self.readline_use:
1788 import IPython.utils.rlineimpl as readline
1788 import IPython.utils.rlineimpl as readline
1789
1789
1790 self.rl_next_input = None
1790 self.rl_next_input = None
1791 self.rl_do_indent = False
1791 self.rl_do_indent = False
1792
1792
1793 if not self.readline_use or not readline.have_readline:
1793 if not self.readline_use or not readline.have_readline:
1794 self.has_readline = False
1794 self.has_readline = False
1795 self.readline = None
1795 self.readline = None
1796 # Set a number of methods that depend on readline to be no-op
1796 # Set a number of methods that depend on readline to be no-op
1797 self.readline_no_record = no_op_context
1797 self.readline_no_record = no_op_context
1798 self.set_readline_completer = no_op
1798 self.set_readline_completer = no_op
1799 self.set_custom_completer = no_op
1799 self.set_custom_completer = no_op
1800 if self.readline_use:
1800 if self.readline_use:
1801 warn('Readline services not available or not loaded.')
1801 warn('Readline services not available or not loaded.')
1802 else:
1802 else:
1803 self.has_readline = True
1803 self.has_readline = True
1804 self.readline = readline
1804 self.readline = readline
1805 sys.modules['readline'] = readline
1805 sys.modules['readline'] = readline
1806
1806
1807 # Platform-specific configuration
1807 # Platform-specific configuration
1808 if os.name == 'nt':
1808 if os.name == 'nt':
1809 # FIXME - check with Frederick to see if we can harmonize
1809 # FIXME - check with Frederick to see if we can harmonize
1810 # naming conventions with pyreadline to avoid this
1810 # naming conventions with pyreadline to avoid this
1811 # platform-dependent check
1811 # platform-dependent check
1812 self.readline_startup_hook = readline.set_pre_input_hook
1812 self.readline_startup_hook = readline.set_pre_input_hook
1813 else:
1813 else:
1814 self.readline_startup_hook = readline.set_startup_hook
1814 self.readline_startup_hook = readline.set_startup_hook
1815
1815
1816 # Load user's initrc file (readline config)
1816 # Load user's initrc file (readline config)
1817 # Or if libedit is used, load editrc.
1817 # Or if libedit is used, load editrc.
1818 inputrc_name = os.environ.get('INPUTRC')
1818 inputrc_name = os.environ.get('INPUTRC')
1819 if inputrc_name is None:
1819 if inputrc_name is None:
1820 inputrc_name = '.inputrc'
1820 inputrc_name = '.inputrc'
1821 if readline.uses_libedit:
1821 if readline.uses_libedit:
1822 inputrc_name = '.editrc'
1822 inputrc_name = '.editrc'
1823 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1823 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1824 if os.path.isfile(inputrc_name):
1824 if os.path.isfile(inputrc_name):
1825 try:
1825 try:
1826 readline.read_init_file(inputrc_name)
1826 readline.read_init_file(inputrc_name)
1827 except:
1827 except:
1828 warn('Problems reading readline initialization file <%s>'
1828 warn('Problems reading readline initialization file <%s>'
1829 % inputrc_name)
1829 % inputrc_name)
1830
1830
1831 # Configure readline according to user's prefs
1831 # Configure readline according to user's prefs
1832 # This is only done if GNU readline is being used. If libedit
1832 # This is only done if GNU readline is being used. If libedit
1833 # is being used (as on Leopard) the readline config is
1833 # is being used (as on Leopard) the readline config is
1834 # not run as the syntax for libedit is different.
1834 # not run as the syntax for libedit is different.
1835 if not readline.uses_libedit:
1835 if not readline.uses_libedit:
1836 for rlcommand in self.readline_parse_and_bind:
1836 for rlcommand in self.readline_parse_and_bind:
1837 #print "loading rl:",rlcommand # dbg
1837 #print "loading rl:",rlcommand # dbg
1838 readline.parse_and_bind(rlcommand)
1838 readline.parse_and_bind(rlcommand)
1839
1839
1840 # Remove some chars from the delimiters list. If we encounter
1840 # Remove some chars from the delimiters list. If we encounter
1841 # unicode chars, discard them.
1841 # unicode chars, discard them.
1842 delims = readline.get_completer_delims()
1842 delims = readline.get_completer_delims()
1843 if not py3compat.PY3:
1843 if not py3compat.PY3:
1844 delims = delims.encode("ascii", "ignore")
1844 delims = delims.encode("ascii", "ignore")
1845 for d in self.readline_remove_delims:
1845 for d in self.readline_remove_delims:
1846 delims = delims.replace(d, "")
1846 delims = delims.replace(d, "")
1847 delims = delims.replace(ESC_MAGIC, '')
1847 delims = delims.replace(ESC_MAGIC, '')
1848 readline.set_completer_delims(delims)
1848 readline.set_completer_delims(delims)
1849 # Store these so we can restore them if something like rpy2 modifies
1849 # Store these so we can restore them if something like rpy2 modifies
1850 # them.
1850 # them.
1851 self.readline_delims = delims
1851 self.readline_delims = delims
1852 # otherwise we end up with a monster history after a while:
1852 # otherwise we end up with a monster history after a while:
1853 readline.set_history_length(self.history_length)
1853 readline.set_history_length(self.history_length)
1854
1854
1855 self.refill_readline_hist()
1855 self.refill_readline_hist()
1856 self.readline_no_record = ReadlineNoRecord(self)
1856 self.readline_no_record = ReadlineNoRecord(self)
1857
1857
1858 # Configure auto-indent for all platforms
1858 # Configure auto-indent for all platforms
1859 self.set_autoindent(self.autoindent)
1859 self.set_autoindent(self.autoindent)
1860
1860
1861 def refill_readline_hist(self):
1861 def refill_readline_hist(self):
1862 # Load the last 1000 lines from history
1862 # Load the last 1000 lines from history
1863 self.readline.clear_history()
1863 self.readline.clear_history()
1864 stdin_encoding = sys.stdin.encoding or "utf-8"
1864 stdin_encoding = sys.stdin.encoding or "utf-8"
1865 last_cell = u""
1865 last_cell = u""
1866 for _, _, cell in self.history_manager.get_tail(1000,
1866 for _, _, cell in self.history_manager.get_tail(1000,
1867 include_latest=True):
1867 include_latest=True):
1868 # Ignore blank lines and consecutive duplicates
1868 # Ignore blank lines and consecutive duplicates
1869 cell = cell.rstrip()
1869 cell = cell.rstrip()
1870 if cell and (cell != last_cell):
1870 if cell and (cell != last_cell):
1871 try:
1871 try:
1872 if self.multiline_history:
1872 if self.multiline_history:
1873 self.readline.add_history(py3compat.unicode_to_str(cell,
1873 self.readline.add_history(py3compat.unicode_to_str(cell,
1874 stdin_encoding))
1874 stdin_encoding))
1875 else:
1875 else:
1876 for line in cell.splitlines():
1876 for line in cell.splitlines():
1877 self.readline.add_history(py3compat.unicode_to_str(line,
1877 self.readline.add_history(py3compat.unicode_to_str(line,
1878 stdin_encoding))
1878 stdin_encoding))
1879 last_cell = cell
1879 last_cell = cell
1880
1880
1881 except TypeError:
1881 except TypeError:
1882 # The history DB can get corrupted so it returns strings
1882 # The history DB can get corrupted so it returns strings
1883 # containing null bytes, which readline objects to.
1883 # containing null bytes, which readline objects to.
1884 continue
1884 continue
1885
1885
1886 @skip_doctest
1886 @skip_doctest
1887 def set_next_input(self, s):
1887 def set_next_input(self, s):
1888 """ Sets the 'default' input string for the next command line.
1888 """ Sets the 'default' input string for the next command line.
1889
1889
1890 Requires readline.
1890 Requires readline.
1891
1891
1892 Example::
1892 Example::
1893
1893
1894 In [1]: _ip.set_next_input("Hello Word")
1894 In [1]: _ip.set_next_input("Hello Word")
1895 In [2]: Hello Word_ # cursor is here
1895 In [2]: Hello Word_ # cursor is here
1896 """
1896 """
1897 self.rl_next_input = py3compat.cast_bytes_py2(s)
1897 self.rl_next_input = py3compat.cast_bytes_py2(s)
1898
1898
1899 # Maybe move this to the terminal subclass?
1899 # Maybe move this to the terminal subclass?
1900 def pre_readline(self):
1900 def pre_readline(self):
1901 """readline hook to be used at the start of each line.
1901 """readline hook to be used at the start of each line.
1902
1902
1903 Currently it handles auto-indent only."""
1903 Currently it handles auto-indent only."""
1904
1904
1905 if self.rl_do_indent:
1905 if self.rl_do_indent:
1906 self.readline.insert_text(self._indent_current_str())
1906 self.readline.insert_text(self._indent_current_str())
1907 if self.rl_next_input is not None:
1907 if self.rl_next_input is not None:
1908 self.readline.insert_text(self.rl_next_input)
1908 self.readline.insert_text(self.rl_next_input)
1909 self.rl_next_input = None
1909 self.rl_next_input = None
1910
1910
1911 def _indent_current_str(self):
1911 def _indent_current_str(self):
1912 """return the current level of indentation as a string"""
1912 """return the current level of indentation as a string"""
1913 return self.input_splitter.indent_spaces * ' '
1913 return self.input_splitter.indent_spaces * ' '
1914
1914
1915 #-------------------------------------------------------------------------
1915 #-------------------------------------------------------------------------
1916 # Things related to text completion
1916 # Things related to text completion
1917 #-------------------------------------------------------------------------
1917 #-------------------------------------------------------------------------
1918
1918
1919 def init_completer(self):
1919 def init_completer(self):
1920 """Initialize the completion machinery.
1920 """Initialize the completion machinery.
1921
1921
1922 This creates completion machinery that can be used by client code,
1922 This creates completion machinery that can be used by client code,
1923 either interactively in-process (typically triggered by the readline
1923 either interactively in-process (typically triggered by the readline
1924 library), programatically (such as in test suites) or out-of-prcess
1924 library), programatically (such as in test suites) or out-of-prcess
1925 (typically over the network by remote frontends).
1925 (typically over the network by remote frontends).
1926 """
1926 """
1927 from IPython.core.completer import IPCompleter
1927 from IPython.core.completer import IPCompleter
1928 from IPython.core.completerlib import (module_completer,
1928 from IPython.core.completerlib import (module_completer,
1929 magic_run_completer, cd_completer, reset_completer)
1929 magic_run_completer, cd_completer, reset_completer)
1930
1930
1931 self.Completer = IPCompleter(shell=self,
1931 self.Completer = IPCompleter(shell=self,
1932 namespace=self.user_ns,
1932 namespace=self.user_ns,
1933 global_namespace=self.user_global_ns,
1933 global_namespace=self.user_global_ns,
1934 alias_table=self.alias_manager.alias_table,
1934 alias_table=self.alias_manager.alias_table,
1935 use_readline=self.has_readline,
1935 use_readline=self.has_readline,
1936 parent=self,
1936 parent=self,
1937 )
1937 )
1938 self.configurables.append(self.Completer)
1938 self.configurables.append(self.Completer)
1939
1939
1940 # Add custom completers to the basic ones built into IPCompleter
1940 # Add custom completers to the basic ones built into IPCompleter
1941 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1941 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1942 self.strdispatchers['complete_command'] = sdisp
1942 self.strdispatchers['complete_command'] = sdisp
1943 self.Completer.custom_completers = sdisp
1943 self.Completer.custom_completers = sdisp
1944
1944
1945 self.set_hook('complete_command', module_completer, str_key = 'import')
1945 self.set_hook('complete_command', module_completer, str_key = 'import')
1946 self.set_hook('complete_command', module_completer, str_key = 'from')
1946 self.set_hook('complete_command', module_completer, str_key = 'from')
1947 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1947 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1948 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1948 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1949 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1949 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1950
1950
1951 # Only configure readline if we truly are using readline. IPython can
1951 # Only configure readline if we truly are using readline. IPython can
1952 # do tab-completion over the network, in GUIs, etc, where readline
1952 # do tab-completion over the network, in GUIs, etc, where readline
1953 # itself may be absent
1953 # itself may be absent
1954 if self.has_readline:
1954 if self.has_readline:
1955 self.set_readline_completer()
1955 self.set_readline_completer()
1956
1956
1957 def complete(self, text, line=None, cursor_pos=None):
1957 def complete(self, text, line=None, cursor_pos=None):
1958 """Return the completed text and a list of completions.
1958 """Return the completed text and a list of completions.
1959
1959
1960 Parameters
1960 Parameters
1961 ----------
1961 ----------
1962
1962
1963 text : string
1963 text : string
1964 A string of text to be completed on. It can be given as empty and
1964 A string of text to be completed on. It can be given as empty and
1965 instead a line/position pair are given. In this case, the
1965 instead a line/position pair are given. In this case, the
1966 completer itself will split the line like readline does.
1966 completer itself will split the line like readline does.
1967
1967
1968 line : string, optional
1968 line : string, optional
1969 The complete line that text is part of.
1969 The complete line that text is part of.
1970
1970
1971 cursor_pos : int, optional
1971 cursor_pos : int, optional
1972 The position of the cursor on the input line.
1972 The position of the cursor on the input line.
1973
1973
1974 Returns
1974 Returns
1975 -------
1975 -------
1976 text : string
1976 text : string
1977 The actual text that was completed.
1977 The actual text that was completed.
1978
1978
1979 matches : list
1979 matches : list
1980 A sorted list with all possible completions.
1980 A sorted list with all possible completions.
1981
1981
1982 The optional arguments allow the completion to take more context into
1982 The optional arguments allow the completion to take more context into
1983 account, and are part of the low-level completion API.
1983 account, and are part of the low-level completion API.
1984
1984
1985 This is a wrapper around the completion mechanism, similar to what
1985 This is a wrapper around the completion mechanism, similar to what
1986 readline does at the command line when the TAB key is hit. By
1986 readline does at the command line when the TAB key is hit. By
1987 exposing it as a method, it can be used by other non-readline
1987 exposing it as a method, it can be used by other non-readline
1988 environments (such as GUIs) for text completion.
1988 environments (such as GUIs) for text completion.
1989
1989
1990 Simple usage example:
1990 Simple usage example:
1991
1991
1992 In [1]: x = 'hello'
1992 In [1]: x = 'hello'
1993
1993
1994 In [2]: _ip.complete('x.l')
1994 In [2]: _ip.complete('x.l')
1995 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1995 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1996 """
1996 """
1997
1997
1998 # Inject names into __builtin__ so we can complete on the added names.
1998 # Inject names into __builtin__ so we can complete on the added names.
1999 with self.builtin_trap:
1999 with self.builtin_trap:
2000 return self.Completer.complete(text, line, cursor_pos)
2000 return self.Completer.complete(text, line, cursor_pos)
2001
2001
2002 def set_custom_completer(self, completer, pos=0):
2002 def set_custom_completer(self, completer, pos=0):
2003 """Adds a new custom completer function.
2003 """Adds a new custom completer function.
2004
2004
2005 The position argument (defaults to 0) is the index in the completers
2005 The position argument (defaults to 0) is the index in the completers
2006 list where you want the completer to be inserted."""
2006 list where you want the completer to be inserted."""
2007
2007
2008 newcomp = types.MethodType(completer,self.Completer)
2008 newcomp = types.MethodType(completer,self.Completer)
2009 self.Completer.matchers.insert(pos,newcomp)
2009 self.Completer.matchers.insert(pos,newcomp)
2010
2010
2011 def set_readline_completer(self):
2011 def set_readline_completer(self):
2012 """Reset readline's completer to be our own."""
2012 """Reset readline's completer to be our own."""
2013 self.readline.set_completer(self.Completer.rlcomplete)
2013 self.readline.set_completer(self.Completer.rlcomplete)
2014
2014
2015 def set_completer_frame(self, frame=None):
2015 def set_completer_frame(self, frame=None):
2016 """Set the frame of the completer."""
2016 """Set the frame of the completer."""
2017 if frame:
2017 if frame:
2018 self.Completer.namespace = frame.f_locals
2018 self.Completer.namespace = frame.f_locals
2019 self.Completer.global_namespace = frame.f_globals
2019 self.Completer.global_namespace = frame.f_globals
2020 else:
2020 else:
2021 self.Completer.namespace = self.user_ns
2021 self.Completer.namespace = self.user_ns
2022 self.Completer.global_namespace = self.user_global_ns
2022 self.Completer.global_namespace = self.user_global_ns
2023
2023
2024 #-------------------------------------------------------------------------
2024 #-------------------------------------------------------------------------
2025 # Things related to magics
2025 # Things related to magics
2026 #-------------------------------------------------------------------------
2026 #-------------------------------------------------------------------------
2027
2027
2028 def init_magics(self):
2028 def init_magics(self):
2029 from IPython.core import magics as m
2029 from IPython.core import magics as m
2030 self.magics_manager = magic.MagicsManager(shell=self,
2030 self.magics_manager = magic.MagicsManager(shell=self,
2031 parent=self,
2031 parent=self,
2032 user_magics=m.UserMagics(self))
2032 user_magics=m.UserMagics(self))
2033 self.configurables.append(self.magics_manager)
2033 self.configurables.append(self.magics_manager)
2034
2034
2035 # Expose as public API from the magics manager
2035 # Expose as public API from the magics manager
2036 self.register_magics = self.magics_manager.register
2036 self.register_magics = self.magics_manager.register
2037 self.define_magic = self.magics_manager.define_magic
2037 self.define_magic = self.magics_manager.define_magic
2038
2038
2039 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2039 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2040 m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics,
2040 m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics,
2041 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2041 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2042 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2042 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2043 )
2043 )
2044
2044
2045 # Register Magic Aliases
2045 # Register Magic Aliases
2046 mman = self.magics_manager
2046 mman = self.magics_manager
2047 # FIXME: magic aliases should be defined by the Magics classes
2047 # FIXME: magic aliases should be defined by the Magics classes
2048 # or in MagicsManager, not here
2048 # or in MagicsManager, not here
2049 mman.register_alias('ed', 'edit')
2049 mman.register_alias('ed', 'edit')
2050 mman.register_alias('hist', 'history')
2050 mman.register_alias('hist', 'history')
2051 mman.register_alias('rep', 'recall')
2051 mman.register_alias('rep', 'recall')
2052 mman.register_alias('SVG', 'svg', 'cell')
2052 mman.register_alias('SVG', 'svg', 'cell')
2053 mman.register_alias('HTML', 'html', 'cell')
2053 mman.register_alias('HTML', 'html', 'cell')
2054 mman.register_alias('file', 'writefile', 'cell')
2054 mman.register_alias('file', 'writefile', 'cell')
2055
2055
2056 # FIXME: Move the color initialization to the DisplayHook, which
2056 # FIXME: Move the color initialization to the DisplayHook, which
2057 # should be split into a prompt manager and displayhook. We probably
2057 # should be split into a prompt manager and displayhook. We probably
2058 # even need a centralize colors management object.
2058 # even need a centralize colors management object.
2059 self.magic('colors %s' % self.colors)
2059 self.magic('colors %s' % self.colors)
2060
2060
2061 # Defined here so that it's included in the documentation
2061 # Defined here so that it's included in the documentation
2062 @functools.wraps(magic.MagicsManager.register_function)
2062 @functools.wraps(magic.MagicsManager.register_function)
2063 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2063 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2064 self.magics_manager.register_function(func,
2064 self.magics_manager.register_function(func,
2065 magic_kind=magic_kind, magic_name=magic_name)
2065 magic_kind=magic_kind, magic_name=magic_name)
2066
2066
2067 def run_line_magic(self, magic_name, line):
2067 def run_line_magic(self, magic_name, line):
2068 """Execute the given line magic.
2068 """Execute the given line magic.
2069
2069
2070 Parameters
2070 Parameters
2071 ----------
2071 ----------
2072 magic_name : str
2072 magic_name : str
2073 Name of the desired magic function, without '%' prefix.
2073 Name of the desired magic function, without '%' prefix.
2074
2074
2075 line : str
2075 line : str
2076 The rest of the input line as a single string.
2076 The rest of the input line as a single string.
2077 """
2077 """
2078 fn = self.find_line_magic(magic_name)
2078 fn = self.find_line_magic(magic_name)
2079 if fn is None:
2079 if fn is None:
2080 cm = self.find_cell_magic(magic_name)
2080 cm = self.find_cell_magic(magic_name)
2081 etpl = "Line magic function `%%%s` not found%s."
2081 etpl = "Line magic function `%%%s` not found%s."
2082 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2082 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2083 'did you mean that instead?)' % magic_name )
2083 'did you mean that instead?)' % magic_name )
2084 error(etpl % (magic_name, extra))
2084 error(etpl % (magic_name, extra))
2085 else:
2085 else:
2086 # Note: this is the distance in the stack to the user's frame.
2086 # Note: this is the distance in the stack to the user's frame.
2087 # This will need to be updated if the internal calling logic gets
2087 # This will need to be updated if the internal calling logic gets
2088 # refactored, or else we'll be expanding the wrong variables.
2088 # refactored, or else we'll be expanding the wrong variables.
2089 stack_depth = 2
2089 stack_depth = 2
2090 magic_arg_s = self.var_expand(line, stack_depth)
2090 magic_arg_s = self.var_expand(line, stack_depth)
2091 # Put magic args in a list so we can call with f(*a) syntax
2091 # Put magic args in a list so we can call with f(*a) syntax
2092 args = [magic_arg_s]
2092 args = [magic_arg_s]
2093 kwargs = {}
2093 kwargs = {}
2094 # Grab local namespace if we need it:
2094 # Grab local namespace if we need it:
2095 if getattr(fn, "needs_local_scope", False):
2095 if getattr(fn, "needs_local_scope", False):
2096 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2096 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2097 with self.builtin_trap:
2097 with self.builtin_trap:
2098 result = fn(*args,**kwargs)
2098 result = fn(*args,**kwargs)
2099 return result
2099 return result
2100
2100
2101 def run_cell_magic(self, magic_name, line, cell):
2101 def run_cell_magic(self, magic_name, line, cell):
2102 """Execute the given cell magic.
2102 """Execute the given cell magic.
2103
2103
2104 Parameters
2104 Parameters
2105 ----------
2105 ----------
2106 magic_name : str
2106 magic_name : str
2107 Name of the desired magic function, without '%' prefix.
2107 Name of the desired magic function, without '%' prefix.
2108
2108
2109 line : str
2109 line : str
2110 The rest of the first input line as a single string.
2110 The rest of the first input line as a single string.
2111
2111
2112 cell : str
2112 cell : str
2113 The body of the cell as a (possibly multiline) string.
2113 The body of the cell as a (possibly multiline) string.
2114 """
2114 """
2115 fn = self.find_cell_magic(magic_name)
2115 fn = self.find_cell_magic(magic_name)
2116 if fn is None:
2116 if fn is None:
2117 lm = self.find_line_magic(magic_name)
2117 lm = self.find_line_magic(magic_name)
2118 etpl = "Cell magic `%%{0}` not found{1}."
2118 etpl = "Cell magic `%%{0}` not found{1}."
2119 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2119 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2120 'did you mean that instead?)'.format(magic_name))
2120 'did you mean that instead?)'.format(magic_name))
2121 error(etpl.format(magic_name, extra))
2121 error(etpl.format(magic_name, extra))
2122 elif cell == '':
2122 elif cell == '':
2123 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2123 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2124 if self.find_line_magic(magic_name) is not None:
2124 if self.find_line_magic(magic_name) is not None:
2125 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2125 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2126 raise UsageError(message)
2126 raise UsageError(message)
2127 else:
2127 else:
2128 # Note: this is the distance in the stack to the user's frame.
2128 # Note: this is the distance in the stack to the user's frame.
2129 # This will need to be updated if the internal calling logic gets
2129 # This will need to be updated if the internal calling logic gets
2130 # refactored, or else we'll be expanding the wrong variables.
2130 # refactored, or else we'll be expanding the wrong variables.
2131 stack_depth = 2
2131 stack_depth = 2
2132 magic_arg_s = self.var_expand(line, stack_depth)
2132 magic_arg_s = self.var_expand(line, stack_depth)
2133 with self.builtin_trap:
2133 with self.builtin_trap:
2134 result = fn(magic_arg_s, cell)
2134 result = fn(magic_arg_s, cell)
2135 return result
2135 return result
2136
2136
2137 def find_line_magic(self, magic_name):
2137 def find_line_magic(self, magic_name):
2138 """Find and return a line magic by name.
2138 """Find and return a line magic by name.
2139
2139
2140 Returns None if the magic isn't found."""
2140 Returns None if the magic isn't found."""
2141 return self.magics_manager.magics['line'].get(magic_name)
2141 return self.magics_manager.magics['line'].get(magic_name)
2142
2142
2143 def find_cell_magic(self, magic_name):
2143 def find_cell_magic(self, magic_name):
2144 """Find and return a cell magic by name.
2144 """Find and return a cell magic by name.
2145
2145
2146 Returns None if the magic isn't found."""
2146 Returns None if the magic isn't found."""
2147 return self.magics_manager.magics['cell'].get(magic_name)
2147 return self.magics_manager.magics['cell'].get(magic_name)
2148
2148
2149 def find_magic(self, magic_name, magic_kind='line'):
2149 def find_magic(self, magic_name, magic_kind='line'):
2150 """Find and return a magic of the given type by name.
2150 """Find and return a magic of the given type by name.
2151
2151
2152 Returns None if the magic isn't found."""
2152 Returns None if the magic isn't found."""
2153 return self.magics_manager.magics[magic_kind].get(magic_name)
2153 return self.magics_manager.magics[magic_kind].get(magic_name)
2154
2154
2155 def magic(self, arg_s):
2155 def magic(self, arg_s):
2156 """DEPRECATED. Use run_line_magic() instead.
2156 """DEPRECATED. Use run_line_magic() instead.
2157
2157
2158 Call a magic function by name.
2158 Call a magic function by name.
2159
2159
2160 Input: a string containing the name of the magic function to call and
2160 Input: a string containing the name of the magic function to call and
2161 any additional arguments to be passed to the magic.
2161 any additional arguments to be passed to the magic.
2162
2162
2163 magic('name -opt foo bar') is equivalent to typing at the ipython
2163 magic('name -opt foo bar') is equivalent to typing at the ipython
2164 prompt:
2164 prompt:
2165
2165
2166 In[1]: %name -opt foo bar
2166 In[1]: %name -opt foo bar
2167
2167
2168 To call a magic without arguments, simply use magic('name').
2168 To call a magic without arguments, simply use magic('name').
2169
2169
2170 This provides a proper Python function to call IPython's magics in any
2170 This provides a proper Python function to call IPython's magics in any
2171 valid Python code you can type at the interpreter, including loops and
2171 valid Python code you can type at the interpreter, including loops and
2172 compound statements.
2172 compound statements.
2173 """
2173 """
2174 # TODO: should we issue a loud deprecation warning here?
2174 # TODO: should we issue a loud deprecation warning here?
2175 magic_name, _, magic_arg_s = arg_s.partition(' ')
2175 magic_name, _, magic_arg_s = arg_s.partition(' ')
2176 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2176 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2177 return self.run_line_magic(magic_name, magic_arg_s)
2177 return self.run_line_magic(magic_name, magic_arg_s)
2178
2178
2179 #-------------------------------------------------------------------------
2179 #-------------------------------------------------------------------------
2180 # Things related to macros
2180 # Things related to macros
2181 #-------------------------------------------------------------------------
2181 #-------------------------------------------------------------------------
2182
2182
2183 def define_macro(self, name, themacro):
2183 def define_macro(self, name, themacro):
2184 """Define a new macro
2184 """Define a new macro
2185
2185
2186 Parameters
2186 Parameters
2187 ----------
2187 ----------
2188 name : str
2188 name : str
2189 The name of the macro.
2189 The name of the macro.
2190 themacro : str or Macro
2190 themacro : str or Macro
2191 The action to do upon invoking the macro. If a string, a new
2191 The action to do upon invoking the macro. If a string, a new
2192 Macro object is created by passing the string to it.
2192 Macro object is created by passing the string to it.
2193 """
2193 """
2194
2194
2195 from IPython.core import macro
2195 from IPython.core import macro
2196
2196
2197 if isinstance(themacro, basestring):
2197 if isinstance(themacro, basestring):
2198 themacro = macro.Macro(themacro)
2198 themacro = macro.Macro(themacro)
2199 if not isinstance(themacro, macro.Macro):
2199 if not isinstance(themacro, macro.Macro):
2200 raise ValueError('A macro must be a string or a Macro instance.')
2200 raise ValueError('A macro must be a string or a Macro instance.')
2201 self.user_ns[name] = themacro
2201 self.user_ns[name] = themacro
2202
2202
2203 #-------------------------------------------------------------------------
2203 #-------------------------------------------------------------------------
2204 # Things related to the running of system commands
2204 # Things related to the running of system commands
2205 #-------------------------------------------------------------------------
2205 #-------------------------------------------------------------------------
2206
2206
2207 def system_piped(self, cmd):
2207 def system_piped(self, cmd):
2208 """Call the given cmd in a subprocess, piping stdout/err
2208 """Call the given cmd in a subprocess, piping stdout/err
2209
2209
2210 Parameters
2210 Parameters
2211 ----------
2211 ----------
2212 cmd : str
2212 cmd : str
2213 Command to execute (can not end in '&', as background processes are
2213 Command to execute (can not end in '&', as background processes are
2214 not supported. Should not be a command that expects input
2214 not supported. Should not be a command that expects input
2215 other than simple text.
2215 other than simple text.
2216 """
2216 """
2217 if cmd.rstrip().endswith('&'):
2217 if cmd.rstrip().endswith('&'):
2218 # this is *far* from a rigorous test
2218 # this is *far* from a rigorous test
2219 # We do not support backgrounding processes because we either use
2219 # We do not support backgrounding processes because we either use
2220 # pexpect or pipes to read from. Users can always just call
2220 # pexpect or pipes to read from. Users can always just call
2221 # os.system() or use ip.system=ip.system_raw
2221 # os.system() or use ip.system=ip.system_raw
2222 # if they really want a background process.
2222 # if they really want a background process.
2223 raise OSError("Background processes not supported.")
2223 raise OSError("Background processes not supported.")
2224
2224
2225 # we explicitly do NOT return the subprocess status code, because
2225 # we explicitly do NOT return the subprocess status code, because
2226 # a non-None value would trigger :func:`sys.displayhook` calls.
2226 # a non-None value would trigger :func:`sys.displayhook` calls.
2227 # Instead, we store the exit_code in user_ns.
2227 # Instead, we store the exit_code in user_ns.
2228 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2228 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2229
2229
2230 def system_raw(self, cmd):
2230 def system_raw(self, cmd):
2231 """Call the given cmd in a subprocess using os.system
2231 """Call the given cmd in a subprocess using os.system
2232
2232
2233 Parameters
2233 Parameters
2234 ----------
2234 ----------
2235 cmd : str
2235 cmd : str
2236 Command to execute.
2236 Command to execute.
2237 """
2237 """
2238 cmd = self.var_expand(cmd, depth=1)
2238 cmd = self.var_expand(cmd, depth=1)
2239 # protect os.system from UNC paths on Windows, which it can't handle:
2239 # protect os.system from UNC paths on Windows, which it can't handle:
2240 if sys.platform == 'win32':
2240 if sys.platform == 'win32':
2241 from IPython.utils._process_win32 import AvoidUNCPath
2241 from IPython.utils._process_win32 import AvoidUNCPath
2242 with AvoidUNCPath() as path:
2242 with AvoidUNCPath() as path:
2243 if path is not None:
2243 if path is not None:
2244 cmd = '"pushd %s &&"%s' % (path, cmd)
2244 cmd = '"pushd %s &&"%s' % (path, cmd)
2245 cmd = py3compat.unicode_to_str(cmd)
2245 cmd = py3compat.unicode_to_str(cmd)
2246 ec = os.system(cmd)
2246 ec = os.system(cmd)
2247 else:
2247 else:
2248 cmd = py3compat.unicode_to_str(cmd)
2248 cmd = py3compat.unicode_to_str(cmd)
2249 ec = os.system(cmd)
2249 ec = os.system(cmd)
2250 # The high byte is the exit code, the low byte is a signal number
2250 # The high byte is the exit code, the low byte is a signal number
2251 # that we discard for now. See the docs for os.wait()
2251 # that we discard for now. See the docs for os.wait()
2252 if ec > 255:
2252 if ec > 255:
2253 ec >>= 8
2253 ec >>= 8
2254
2254
2255 # We explicitly do NOT return the subprocess status code, because
2255 # We explicitly do NOT return the subprocess status code, because
2256 # a non-None value would trigger :func:`sys.displayhook` calls.
2256 # a non-None value would trigger :func:`sys.displayhook` calls.
2257 # Instead, we store the exit_code in user_ns.
2257 # Instead, we store the exit_code in user_ns.
2258 self.user_ns['_exit_code'] = ec
2258 self.user_ns['_exit_code'] = ec
2259
2259
2260 # use piped system by default, because it is better behaved
2260 # use piped system by default, because it is better behaved
2261 system = system_piped
2261 system = system_piped
2262
2262
2263 def getoutput(self, cmd, split=True, depth=0):
2263 def getoutput(self, cmd, split=True, depth=0):
2264 """Get output (possibly including stderr) from a subprocess.
2264 """Get output (possibly including stderr) from a subprocess.
2265
2265
2266 Parameters
2266 Parameters
2267 ----------
2267 ----------
2268 cmd : str
2268 cmd : str
2269 Command to execute (can not end in '&', as background processes are
2269 Command to execute (can not end in '&', as background processes are
2270 not supported.
2270 not supported.
2271 split : bool, optional
2271 split : bool, optional
2272 If True, split the output into an IPython SList. Otherwise, an
2272 If True, split the output into an IPython SList. Otherwise, an
2273 IPython LSString is returned. These are objects similar to normal
2273 IPython LSString is returned. These are objects similar to normal
2274 lists and strings, with a few convenience attributes for easier
2274 lists and strings, with a few convenience attributes for easier
2275 manipulation of line-based output. You can use '?' on them for
2275 manipulation of line-based output. You can use '?' on them for
2276 details.
2276 details.
2277 depth : int, optional
2277 depth : int, optional
2278 How many frames above the caller are the local variables which should
2278 How many frames above the caller are the local variables which should
2279 be expanded in the command string? The default (0) assumes that the
2279 be expanded in the command string? The default (0) assumes that the
2280 expansion variables are in the stack frame calling this function.
2280 expansion variables are in the stack frame calling this function.
2281 """
2281 """
2282 if cmd.rstrip().endswith('&'):
2282 if cmd.rstrip().endswith('&'):
2283 # this is *far* from a rigorous test
2283 # this is *far* from a rigorous test
2284 raise OSError("Background processes not supported.")
2284 raise OSError("Background processes not supported.")
2285 out = getoutput(self.var_expand(cmd, depth=depth+1))
2285 out = getoutput(self.var_expand(cmd, depth=depth+1))
2286 if split:
2286 if split:
2287 out = SList(out.splitlines())
2287 out = SList(out.splitlines())
2288 else:
2288 else:
2289 out = LSString(out)
2289 out = LSString(out)
2290 return out
2290 return out
2291
2291
2292 #-------------------------------------------------------------------------
2292 #-------------------------------------------------------------------------
2293 # Things related to aliases
2293 # Things related to aliases
2294 #-------------------------------------------------------------------------
2294 #-------------------------------------------------------------------------
2295
2295
2296 def init_alias(self):
2296 def init_alias(self):
2297 self.alias_manager = AliasManager(shell=self, parent=self)
2297 self.alias_manager = AliasManager(shell=self, parent=self)
2298 self.configurables.append(self.alias_manager)
2298 self.configurables.append(self.alias_manager)
2299 self.ns_table['alias'] = self.alias_manager.alias_table,
2299 self.ns_table['alias'] = self.alias_manager.alias_table,
2300
2300
2301 #-------------------------------------------------------------------------
2301 #-------------------------------------------------------------------------
2302 # Things related to extensions
2302 # Things related to extensions
2303 #-------------------------------------------------------------------------
2303 #-------------------------------------------------------------------------
2304
2304
2305 def init_extension_manager(self):
2305 def init_extension_manager(self):
2306 self.extension_manager = ExtensionManager(shell=self, parent=self)
2306 self.extension_manager = ExtensionManager(shell=self, parent=self)
2307 self.configurables.append(self.extension_manager)
2307 self.configurables.append(self.extension_manager)
2308
2308
2309 #-------------------------------------------------------------------------
2309 #-------------------------------------------------------------------------
2310 # Things related to payloads
2310 # Things related to payloads
2311 #-------------------------------------------------------------------------
2311 #-------------------------------------------------------------------------
2312
2312
2313 def init_payload(self):
2313 def init_payload(self):
2314 self.payload_manager = PayloadManager(parent=self)
2314 self.payload_manager = PayloadManager(parent=self)
2315 self.configurables.append(self.payload_manager)
2315 self.configurables.append(self.payload_manager)
2316
2316
2317 #-------------------------------------------------------------------------
2317 #-------------------------------------------------------------------------
2318 # Things related to the prefilter
2318 # Things related to the prefilter
2319 #-------------------------------------------------------------------------
2319 #-------------------------------------------------------------------------
2320
2320
2321 def init_prefilter(self):
2321 def init_prefilter(self):
2322 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2322 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2323 self.configurables.append(self.prefilter_manager)
2323 self.configurables.append(self.prefilter_manager)
2324 # Ultimately this will be refactored in the new interpreter code, but
2324 # Ultimately this will be refactored in the new interpreter code, but
2325 # for now, we should expose the main prefilter method (there's legacy
2325 # for now, we should expose the main prefilter method (there's legacy
2326 # code out there that may rely on this).
2326 # code out there that may rely on this).
2327 self.prefilter = self.prefilter_manager.prefilter_lines
2327 self.prefilter = self.prefilter_manager.prefilter_lines
2328
2328
2329 def auto_rewrite_input(self, cmd):
2329 def auto_rewrite_input(self, cmd):
2330 """Print to the screen the rewritten form of the user's command.
2330 """Print to the screen the rewritten form of the user's command.
2331
2331
2332 This shows visual feedback by rewriting input lines that cause
2332 This shows visual feedback by rewriting input lines that cause
2333 automatic calling to kick in, like::
2333 automatic calling to kick in, like::
2334
2334
2335 /f x
2335 /f x
2336
2336
2337 into::
2337 into::
2338
2338
2339 ------> f(x)
2339 ------> f(x)
2340
2340
2341 after the user's input prompt. This helps the user understand that the
2341 after the user's input prompt. This helps the user understand that the
2342 input line was transformed automatically by IPython.
2342 input line was transformed automatically by IPython.
2343 """
2343 """
2344 if not self.show_rewritten_input:
2344 if not self.show_rewritten_input:
2345 return
2345 return
2346
2346
2347 rw = self.prompt_manager.render('rewrite') + cmd
2347 rw = self.prompt_manager.render('rewrite') + cmd
2348
2348
2349 try:
2349 try:
2350 # plain ascii works better w/ pyreadline, on some machines, so
2350 # plain ascii works better w/ pyreadline, on some machines, so
2351 # we use it and only print uncolored rewrite if we have unicode
2351 # we use it and only print uncolored rewrite if we have unicode
2352 rw = str(rw)
2352 rw = str(rw)
2353 print(rw, file=io.stdout)
2353 print(rw, file=io.stdout)
2354 except UnicodeEncodeError:
2354 except UnicodeEncodeError:
2355 print("------> " + cmd)
2355 print("------> " + cmd)
2356
2356
2357 #-------------------------------------------------------------------------
2357 #-------------------------------------------------------------------------
2358 # Things related to extracting values/expressions from kernel and user_ns
2358 # Things related to extracting values/expressions from kernel and user_ns
2359 #-------------------------------------------------------------------------
2359 #-------------------------------------------------------------------------
2360
2360
2361 def _user_obj_error(self):
2361 def _user_obj_error(self):
2362 """return simple exception dict
2362 """return simple exception dict
2363
2363
2364 for use in user_variables / expressions
2364 for use in user_variables / expressions
2365 """
2365 """
2366
2366
2367 etype, evalue, tb = self._get_exc_info()
2367 etype, evalue, tb = self._get_exc_info()
2368 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2368 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2369
2369
2370 exc_info = {
2370 exc_info = {
2371 u'status' : 'error',
2371 u'status' : 'error',
2372 u'traceback' : stb,
2372 u'traceback' : stb,
2373 u'ename' : unicode(etype.__name__),
2373 u'ename' : unicode(etype.__name__),
2374 u'evalue' : py3compat.safe_unicode(evalue),
2374 u'evalue' : py3compat.safe_unicode(evalue),
2375 }
2375 }
2376
2376
2377 return exc_info
2377 return exc_info
2378
2378
2379 def _format_user_obj(self, obj):
2379 def _format_user_obj(self, obj):
2380 """format a user object to display dict
2380 """format a user object to display dict
2381
2381
2382 for use in user_expressions / variables
2382 for use in user_expressions / variables
2383 """
2383 """
2384
2384
2385 data, md = self.display_formatter.format(obj)
2385 data, md = self.display_formatter.format(obj)
2386 value = {
2386 value = {
2387 'status' : 'ok',
2387 'status' : 'ok',
2388 'data' : data,
2388 'data' : data,
2389 'metadata' : md,
2389 'metadata' : md,
2390 }
2390 }
2391 return value
2391 return value
2392
2392
2393 def user_variables(self, names):
2393 def user_variables(self, names):
2394 """Get a list of variable names from the user's namespace.
2394 """Get a list of variable names from the user's namespace.
2395
2395
2396 Parameters
2396 Parameters
2397 ----------
2397 ----------
2398 names : list of strings
2398 names : list of strings
2399 A list of names of variables to be read from the user namespace.
2399 A list of names of variables to be read from the user namespace.
2400
2400
2401 Returns
2401 Returns
2402 -------
2402 -------
2403 A dict, keyed by the input names and with the rich mime-type repr(s) of each value.
2403 A dict, keyed by the input names and with the rich mime-type repr(s) of each value.
2404 Each element will be a sub-dict of the same form as a display_data message.
2404 Each element will be a sub-dict of the same form as a display_data message.
2405 """
2405 """
2406 out = {}
2406 out = {}
2407 user_ns = self.user_ns
2407 user_ns = self.user_ns
2408
2408
2409 for varname in names:
2409 for varname in names:
2410 try:
2410 try:
2411 value = self._format_user_obj(user_ns[varname])
2411 value = self._format_user_obj(user_ns[varname])
2412 except:
2412 except:
2413 value = self._user_obj_error()
2413 value = self._user_obj_error()
2414 out[varname] = value
2414 out[varname] = value
2415 return out
2415 return out
2416
2416
2417 def user_expressions(self, expressions):
2417 def user_expressions(self, expressions):
2418 """Evaluate a dict of expressions in the user's namespace.
2418 """Evaluate a dict of expressions in the user's namespace.
2419
2419
2420 Parameters
2420 Parameters
2421 ----------
2421 ----------
2422 expressions : dict
2422 expressions : dict
2423 A dict with string keys and string values. The expression values
2423 A dict with string keys and string values. The expression values
2424 should be valid Python expressions, each of which will be evaluated
2424 should be valid Python expressions, each of which will be evaluated
2425 in the user namespace.
2425 in the user namespace.
2426
2426
2427 Returns
2427 Returns
2428 -------
2428 -------
2429 A dict, keyed like the input expressions dict, with the rich mime-typed
2429 A dict, keyed like the input expressions dict, with the rich mime-typed
2430 display_data of each value.
2430 display_data of each value.
2431 """
2431 """
2432 out = {}
2432 out = {}
2433 user_ns = self.user_ns
2433 user_ns = self.user_ns
2434 global_ns = self.user_global_ns
2434 global_ns = self.user_global_ns
2435
2435
2436 for key, expr in expressions.iteritems():
2436 for key, expr in expressions.iteritems():
2437 try:
2437 try:
2438 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2438 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2439 except:
2439 except:
2440 value = self._user_obj_error()
2440 value = self._user_obj_error()
2441 out[key] = value
2441 out[key] = value
2442 return out
2442 return out
2443
2443
2444 #-------------------------------------------------------------------------
2444 #-------------------------------------------------------------------------
2445 # Things related to the running of code
2445 # Things related to the running of code
2446 #-------------------------------------------------------------------------
2446 #-------------------------------------------------------------------------
2447
2447
2448 def ex(self, cmd):
2448 def ex(self, cmd):
2449 """Execute a normal python statement in user namespace."""
2449 """Execute a normal python statement in user namespace."""
2450 with self.builtin_trap:
2450 with self.builtin_trap:
2451 exec cmd in self.user_global_ns, self.user_ns
2451 exec cmd in self.user_global_ns, self.user_ns
2452
2452
2453 def ev(self, expr):
2453 def ev(self, expr):
2454 """Evaluate python expression expr in user namespace.
2454 """Evaluate python expression expr in user namespace.
2455
2455
2456 Returns the result of evaluation
2456 Returns the result of evaluation
2457 """
2457 """
2458 with self.builtin_trap:
2458 with self.builtin_trap:
2459 return eval(expr, self.user_global_ns, self.user_ns)
2459 return eval(expr, self.user_global_ns, self.user_ns)
2460
2460
2461 def safe_execfile(self, fname, *where, **kw):
2461 def safe_execfile(self, fname, *where, **kw):
2462 """A safe version of the builtin execfile().
2462 """A safe version of the builtin execfile().
2463
2463
2464 This version will never throw an exception, but instead print
2464 This version will never throw an exception, but instead print
2465 helpful error messages to the screen. This only works on pure
2465 helpful error messages to the screen. This only works on pure
2466 Python files with the .py extension.
2466 Python files with the .py extension.
2467
2467
2468 Parameters
2468 Parameters
2469 ----------
2469 ----------
2470 fname : string
2470 fname : string
2471 The name of the file to be executed.
2471 The name of the file to be executed.
2472 where : tuple
2472 where : tuple
2473 One or two namespaces, passed to execfile() as (globals,locals).
2473 One or two namespaces, passed to execfile() as (globals,locals).
2474 If only one is given, it is passed as both.
2474 If only one is given, it is passed as both.
2475 exit_ignore : bool (False)
2475 exit_ignore : bool (False)
2476 If True, then silence SystemExit for non-zero status (it is always
2476 If True, then silence SystemExit for non-zero status (it is always
2477 silenced for zero status, as it is so common).
2477 silenced for zero status, as it is so common).
2478 raise_exceptions : bool (False)
2478 raise_exceptions : bool (False)
2479 If True raise exceptions everywhere. Meant for testing.
2479 If True raise exceptions everywhere. Meant for testing.
2480
2480
2481 """
2481 """
2482 kw.setdefault('exit_ignore', False)
2482 kw.setdefault('exit_ignore', False)
2483 kw.setdefault('raise_exceptions', False)
2483 kw.setdefault('raise_exceptions', False)
2484
2484
2485 fname = os.path.abspath(os.path.expanduser(fname))
2485 fname = os.path.abspath(os.path.expanduser(fname))
2486
2486
2487 # Make sure we can open the file
2487 # Make sure we can open the file
2488 try:
2488 try:
2489 with open(fname) as thefile:
2489 with open(fname) as thefile:
2490 pass
2490 pass
2491 except:
2491 except:
2492 warn('Could not open file <%s> for safe execution.' % fname)
2492 warn('Could not open file <%s> for safe execution.' % fname)
2493 return
2493 return
2494
2494
2495 # Find things also in current directory. This is needed to mimic the
2495 # Find things also in current directory. This is needed to mimic the
2496 # behavior of running a script from the system command line, where
2496 # behavior of running a script from the system command line, where
2497 # Python inserts the script's directory into sys.path
2497 # Python inserts the script's directory into sys.path
2498 dname = os.path.dirname(fname)
2498 dname = os.path.dirname(fname)
2499
2499
2500 with prepended_to_syspath(dname):
2500 with prepended_to_syspath(dname):
2501 try:
2501 try:
2502 py3compat.execfile(fname,*where)
2502 py3compat.execfile(fname,*where)
2503 except SystemExit as status:
2503 except SystemExit as status:
2504 # If the call was made with 0 or None exit status (sys.exit(0)
2504 # If the call was made with 0 or None exit status (sys.exit(0)
2505 # or sys.exit() ), don't bother showing a traceback, as both of
2505 # or sys.exit() ), don't bother showing a traceback, as both of
2506 # these are considered normal by the OS:
2506 # these are considered normal by the OS:
2507 # > python -c'import sys;sys.exit(0)'; echo $?
2507 # > python -c'import sys;sys.exit(0)'; echo $?
2508 # 0
2508 # 0
2509 # > python -c'import sys;sys.exit()'; echo $?
2509 # > python -c'import sys;sys.exit()'; echo $?
2510 # 0
2510 # 0
2511 # For other exit status, we show the exception unless
2511 # For other exit status, we show the exception unless
2512 # explicitly silenced, but only in short form.
2512 # explicitly silenced, but only in short form.
2513 if kw['raise_exceptions']:
2513 if kw['raise_exceptions']:
2514 raise
2514 raise
2515 if status.code and not kw['exit_ignore']:
2515 if status.code and not kw['exit_ignore']:
2516 self.showtraceback(exception_only=True)
2516 self.showtraceback(exception_only=True)
2517 except:
2517 except:
2518 if kw['raise_exceptions']:
2518 if kw['raise_exceptions']:
2519 raise
2519 raise
2520 self.showtraceback()
2520 self.showtraceback()
2521
2521
2522 def safe_execfile_ipy(self, fname):
2522 def safe_execfile_ipy(self, fname):
2523 """Like safe_execfile, but for .ipy files with IPython syntax.
2523 """Like safe_execfile, but for .ipy files with IPython syntax.
2524
2524
2525 Parameters
2525 Parameters
2526 ----------
2526 ----------
2527 fname : str
2527 fname : str
2528 The name of the file to execute. The filename must have a
2528 The name of the file to execute. The filename must have a
2529 .ipy extension.
2529 .ipy extension.
2530 """
2530 """
2531 fname = os.path.abspath(os.path.expanduser(fname))
2531 fname = os.path.abspath(os.path.expanduser(fname))
2532
2532
2533 # Make sure we can open the file
2533 # Make sure we can open the file
2534 try:
2534 try:
2535 with open(fname) as thefile:
2535 with open(fname) as thefile:
2536 pass
2536 pass
2537 except:
2537 except:
2538 warn('Could not open file <%s> for safe execution.' % fname)
2538 warn('Could not open file <%s> for safe execution.' % fname)
2539 return
2539 return
2540
2540
2541 # Find things also in current directory. This is needed to mimic the
2541 # Find things also in current directory. This is needed to mimic the
2542 # behavior of running a script from the system command line, where
2542 # behavior of running a script from the system command line, where
2543 # Python inserts the script's directory into sys.path
2543 # Python inserts the script's directory into sys.path
2544 dname = os.path.dirname(fname)
2544 dname = os.path.dirname(fname)
2545
2545
2546 with prepended_to_syspath(dname):
2546 with prepended_to_syspath(dname):
2547 try:
2547 try:
2548 with open(fname) as thefile:
2548 with open(fname) as thefile:
2549 # self.run_cell currently captures all exceptions
2549 # self.run_cell currently captures all exceptions
2550 # raised in user code. It would be nice if there were
2550 # raised in user code. It would be nice if there were
2551 # versions of runlines, execfile that did raise, so
2551 # versions of runlines, execfile that did raise, so
2552 # we could catch the errors.
2552 # we could catch the errors.
2553 self.run_cell(thefile.read(), store_history=False, shell_futures=False)
2553 self.run_cell(thefile.read(), store_history=False, shell_futures=False)
2554 except:
2554 except:
2555 self.showtraceback()
2555 self.showtraceback()
2556 warn('Unknown failure executing file: <%s>' % fname)
2556 warn('Unknown failure executing file: <%s>' % fname)
2557
2557
2558 def safe_run_module(self, mod_name, where):
2558 def safe_run_module(self, mod_name, where):
2559 """A safe version of runpy.run_module().
2559 """A safe version of runpy.run_module().
2560
2560
2561 This version will never throw an exception, but instead print
2561 This version will never throw an exception, but instead print
2562 helpful error messages to the screen.
2562 helpful error messages to the screen.
2563
2563
2564 `SystemExit` exceptions with status code 0 or None are ignored.
2564 `SystemExit` exceptions with status code 0 or None are ignored.
2565
2565
2566 Parameters
2566 Parameters
2567 ----------
2567 ----------
2568 mod_name : string
2568 mod_name : string
2569 The name of the module to be executed.
2569 The name of the module to be executed.
2570 where : dict
2570 where : dict
2571 The globals namespace.
2571 The globals namespace.
2572 """
2572 """
2573 try:
2573 try:
2574 try:
2574 try:
2575 where.update(
2575 where.update(
2576 runpy.run_module(str(mod_name), run_name="__main__",
2576 runpy.run_module(str(mod_name), run_name="__main__",
2577 alter_sys=True)
2577 alter_sys=True)
2578 )
2578 )
2579 except SystemExit as status:
2579 except SystemExit as status:
2580 if status.code:
2580 if status.code:
2581 raise
2581 raise
2582 except:
2582 except:
2583 self.showtraceback()
2583 self.showtraceback()
2584 warn('Unknown failure executing module: <%s>' % mod_name)
2584 warn('Unknown failure executing module: <%s>' % mod_name)
2585
2585
2586 def _run_cached_cell_magic(self, magic_name, line):
2586 def _run_cached_cell_magic(self, magic_name, line):
2587 """Special method to call a cell magic with the data stored in self.
2587 """Special method to call a cell magic with the data stored in self.
2588 """
2588 """
2589 cell = self._current_cell_magic_body
2589 cell = self._current_cell_magic_body
2590 self._current_cell_magic_body = None
2590 self._current_cell_magic_body = None
2591 return self.run_cell_magic(magic_name, line, cell)
2591 return self.run_cell_magic(magic_name, line, cell)
2592
2592
2593 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2593 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2594 """Run a complete IPython cell.
2594 """Run a complete IPython cell.
2595
2595
2596 Parameters
2596 Parameters
2597 ----------
2597 ----------
2598 raw_cell : str
2598 raw_cell : str
2599 The code (including IPython code such as %magic functions) to run.
2599 The code (including IPython code such as %magic functions) to run.
2600 store_history : bool
2600 store_history : bool
2601 If True, the raw and translated cell will be stored in IPython's
2601 If True, the raw and translated cell will be stored in IPython's
2602 history. For user code calling back into IPython's machinery, this
2602 history. For user code calling back into IPython's machinery, this
2603 should be set to False.
2603 should be set to False.
2604 silent : bool
2604 silent : bool
2605 If True, avoid side-effects, such as implicit displayhooks and
2605 If True, avoid side-effects, such as implicit displayhooks and
2606 and logging. silent=True forces store_history=False.
2606 and logging. silent=True forces store_history=False.
2607 shell_futures : bool
2607 shell_futures : bool
2608 If True, the code will share future statements with the interactive
2608 If True, the code will share future statements with the interactive
2609 shell. It will both be affected by previous __future__ imports, and
2609 shell. It will both be affected by previous __future__ imports, and
2610 any __future__ imports in the code will affect the shell. If False,
2610 any __future__ imports in the code will affect the shell. If False,
2611 __future__ imports are not shared in either direction.
2611 __future__ imports are not shared in either direction.
2612 """
2612 """
2613 if (not raw_cell) or raw_cell.isspace():
2613 if (not raw_cell) or raw_cell.isspace():
2614 return
2614 return
2615
2615
2616 if silent:
2616 if silent:
2617 store_history = False
2617 store_history = False
2618
2618
2619 self.input_transformer_manager.push(raw_cell)
2619 self.input_transformer_manager.push(raw_cell)
2620 cell = self.input_transformer_manager.source_reset()
2620 cell = self.input_transformer_manager.source_reset()
2621
2621
2622 # Our own compiler remembers the __future__ environment. If we want to
2622 # Our own compiler remembers the __future__ environment. If we want to
2623 # run code with a separate __future__ environment, use the default
2623 # run code with a separate __future__ environment, use the default
2624 # compiler
2624 # compiler
2625 compiler = self.compile if shell_futures else CachingCompiler()
2625 compiler = self.compile if shell_futures else CachingCompiler()
2626
2626
2627 with self.builtin_trap:
2627 with self.builtin_trap:
2628 prefilter_failed = False
2628 prefilter_failed = False
2629 if len(cell.splitlines()) == 1:
2629 if len(cell.splitlines()) == 1:
2630 try:
2630 try:
2631 # use prefilter_lines to handle trailing newlines
2631 # use prefilter_lines to handle trailing newlines
2632 # restore trailing newline for ast.parse
2632 # restore trailing newline for ast.parse
2633 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2633 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2634 except AliasError as e:
2634 except AliasError as e:
2635 error(e)
2635 error(e)
2636 prefilter_failed = True
2636 prefilter_failed = True
2637 except Exception:
2637 except Exception:
2638 # don't allow prefilter errors to crash IPython
2638 # don't allow prefilter errors to crash IPython
2639 self.showtraceback()
2639 self.showtraceback()
2640 prefilter_failed = True
2640 prefilter_failed = True
2641
2641
2642 # Store raw and processed history
2642 # Store raw and processed history
2643 if store_history:
2643 if store_history:
2644 self.history_manager.store_inputs(self.execution_count,
2644 self.history_manager.store_inputs(self.execution_count,
2645 cell, raw_cell)
2645 cell, raw_cell)
2646 if not silent:
2646 if not silent:
2647 self.logger.log(cell, raw_cell)
2647 self.logger.log(cell, raw_cell)
2648
2648
2649 if not prefilter_failed:
2649 if not prefilter_failed:
2650 # don't run if prefilter failed
2650 # don't run if prefilter failed
2651 cell_name = self.compile.cache(cell, self.execution_count)
2651 cell_name = self.compile.cache(cell, self.execution_count)
2652
2652
2653 with self.display_trap:
2653 with self.display_trap:
2654 try:
2654 try:
2655 code_ast = compiler.ast_parse(cell, filename=cell_name)
2655 code_ast = compiler.ast_parse(cell, filename=cell_name)
2656 except IndentationError:
2656 except IndentationError:
2657 self.showindentationerror()
2657 self.showindentationerror()
2658 if store_history:
2658 if store_history:
2659 self.execution_count += 1
2659 self.execution_count += 1
2660 return None
2660 return None
2661 except (OverflowError, SyntaxError, ValueError, TypeError,
2661 except (OverflowError, SyntaxError, ValueError, TypeError,
2662 MemoryError):
2662 MemoryError):
2663 self.showsyntaxerror()
2663 self.showsyntaxerror()
2664 if store_history:
2664 if store_history:
2665 self.execution_count += 1
2665 self.execution_count += 1
2666 return None
2666 return None
2667
2667
2668 code_ast = self.transform_ast(code_ast)
2668 code_ast = self.transform_ast(code_ast)
2669
2669
2670 interactivity = "none" if silent else self.ast_node_interactivity
2670 interactivity = "none" if silent else self.ast_node_interactivity
2671 self.run_ast_nodes(code_ast.body, cell_name,
2671 self.run_ast_nodes(code_ast.body, cell_name,
2672 interactivity=interactivity, compiler=compiler)
2672 interactivity=interactivity, compiler=compiler)
2673
2673
2674 # Execute any registered post-execution functions.
2674 # Execute any registered post-execution functions.
2675 # unless we are silent
2675 # unless we are silent
2676 post_exec = [] if silent else self._post_execute.iteritems()
2676 post_exec = [] if silent else self._post_execute.iteritems()
2677
2677
2678 for func, status in post_exec:
2678 for func, status in post_exec:
2679 if self.disable_failing_post_execute and not status:
2679 if self.disable_failing_post_execute and not status:
2680 continue
2680 continue
2681 try:
2681 try:
2682 func()
2682 func()
2683 except KeyboardInterrupt:
2683 except KeyboardInterrupt:
2684 print("\nKeyboardInterrupt", file=io.stderr)
2684 print("\nKeyboardInterrupt", file=io.stderr)
2685 except Exception:
2685 except Exception:
2686 # register as failing:
2686 # register as failing:
2687 self._post_execute[func] = False
2687 self._post_execute[func] = False
2688 self.showtraceback()
2688 self.showtraceback()
2689 print('\n'.join([
2689 print('\n'.join([
2690 "post-execution function %r produced an error." % func,
2690 "post-execution function %r produced an error." % func,
2691 "If this problem persists, you can disable failing post-exec functions with:",
2691 "If this problem persists, you can disable failing post-exec functions with:",
2692 "",
2692 "",
2693 " get_ipython().disable_failing_post_execute = True"
2693 " get_ipython().disable_failing_post_execute = True"
2694 ]), file=io.stderr)
2694 ]), file=io.stderr)
2695
2695
2696 if store_history:
2696 if store_history:
2697 # Write output to the database. Does nothing unless
2697 # Write output to the database. Does nothing unless
2698 # history output logging is enabled.
2698 # history output logging is enabled.
2699 self.history_manager.store_output(self.execution_count)
2699 self.history_manager.store_output(self.execution_count)
2700 # Each cell is a *single* input, regardless of how many lines it has
2700 # Each cell is a *single* input, regardless of how many lines it has
2701 self.execution_count += 1
2701 self.execution_count += 1
2702
2702
2703 def transform_ast(self, node):
2703 def transform_ast(self, node):
2704 """Apply the AST transformations from self.ast_transformers
2704 """Apply the AST transformations from self.ast_transformers
2705
2705
2706 Parameters
2706 Parameters
2707 ----------
2707 ----------
2708 node : ast.Node
2708 node : ast.Node
2709 The root node to be transformed. Typically called with the ast.Module
2709 The root node to be transformed. Typically called with the ast.Module
2710 produced by parsing user input.
2710 produced by parsing user input.
2711
2711
2712 Returns
2712 Returns
2713 -------
2713 -------
2714 An ast.Node corresponding to the node it was called with. Note that it
2714 An ast.Node corresponding to the node it was called with. Note that it
2715 may also modify the passed object, so don't rely on references to the
2715 may also modify the passed object, so don't rely on references to the
2716 original AST.
2716 original AST.
2717 """
2717 """
2718 for transformer in self.ast_transformers:
2718 for transformer in self.ast_transformers:
2719 try:
2719 try:
2720 node = transformer.visit(node)
2720 node = transformer.visit(node)
2721 except Exception:
2721 except Exception:
2722 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2722 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2723 self.ast_transformers.remove(transformer)
2723 self.ast_transformers.remove(transformer)
2724
2724
2725 if self.ast_transformers:
2725 if self.ast_transformers:
2726 ast.fix_missing_locations(node)
2726 ast.fix_missing_locations(node)
2727 return node
2727 return node
2728
2728
2729
2729
2730 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2730 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2731 compiler=compile):
2731 compiler=compile):
2732 """Run a sequence of AST nodes. The execution mode depends on the
2732 """Run a sequence of AST nodes. The execution mode depends on the
2733 interactivity parameter.
2733 interactivity parameter.
2734
2734
2735 Parameters
2735 Parameters
2736 ----------
2736 ----------
2737 nodelist : list
2737 nodelist : list
2738 A sequence of AST nodes to run.
2738 A sequence of AST nodes to run.
2739 cell_name : str
2739 cell_name : str
2740 Will be passed to the compiler as the filename of the cell. Typically
2740 Will be passed to the compiler as the filename of the cell. Typically
2741 the value returned by ip.compile.cache(cell).
2741 the value returned by ip.compile.cache(cell).
2742 interactivity : str
2742 interactivity : str
2743 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2743 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2744 run interactively (displaying output from expressions). 'last_expr'
2744 run interactively (displaying output from expressions). 'last_expr'
2745 will run the last node interactively only if it is an expression (i.e.
2745 will run the last node interactively only if it is an expression (i.e.
2746 expressions in loops or other blocks are not displayed. Other values
2746 expressions in loops or other blocks are not displayed. Other values
2747 for this parameter will raise a ValueError.
2747 for this parameter will raise a ValueError.
2748 compiler : callable
2748 compiler : callable
2749 A function with the same interface as the built-in compile(), to turn
2749 A function with the same interface as the built-in compile(), to turn
2750 the AST nodes into code objects. Default is the built-in compile().
2750 the AST nodes into code objects. Default is the built-in compile().
2751 """
2751 """
2752 if not nodelist:
2752 if not nodelist:
2753 return
2753 return
2754
2754
2755 if interactivity == 'last_expr':
2755 if interactivity == 'last_expr':
2756 if isinstance(nodelist[-1], ast.Expr):
2756 if isinstance(nodelist[-1], ast.Expr):
2757 interactivity = "last"
2757 interactivity = "last"
2758 else:
2758 else:
2759 interactivity = "none"
2759 interactivity = "none"
2760
2760
2761 if interactivity == 'none':
2761 if interactivity == 'none':
2762 to_run_exec, to_run_interactive = nodelist, []
2762 to_run_exec, to_run_interactive = nodelist, []
2763 elif interactivity == 'last':
2763 elif interactivity == 'last':
2764 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2764 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2765 elif interactivity == 'all':
2765 elif interactivity == 'all':
2766 to_run_exec, to_run_interactive = [], nodelist
2766 to_run_exec, to_run_interactive = [], nodelist
2767 else:
2767 else:
2768 raise ValueError("Interactivity was %r" % interactivity)
2768 raise ValueError("Interactivity was %r" % interactivity)
2769
2769
2770 exec_count = self.execution_count
2770 exec_count = self.execution_count
2771
2771
2772 try:
2772 try:
2773 for i, node in enumerate(to_run_exec):
2773 for i, node in enumerate(to_run_exec):
2774 mod = ast.Module([node])
2774 mod = ast.Module([node])
2775 code = compiler(mod, cell_name, "exec")
2775 code = compiler(mod, cell_name, "exec")
2776 if self.run_code(code):
2776 if self.run_code(code):
2777 return True
2777 return True
2778
2778
2779 for i, node in enumerate(to_run_interactive):
2779 for i, node in enumerate(to_run_interactive):
2780 mod = ast.Interactive([node])
2780 mod = ast.Interactive([node])
2781 code = compiler(mod, cell_name, "single")
2781 code = compiler(mod, cell_name, "single")
2782 if self.run_code(code):
2782 if self.run_code(code):
2783 return True
2783 return True
2784
2784
2785 # Flush softspace
2785 # Flush softspace
2786 if softspace(sys.stdout, 0):
2786 if softspace(sys.stdout, 0):
2787 print()
2787 print()
2788
2788
2789 except:
2789 except:
2790 # It's possible to have exceptions raised here, typically by
2790 # It's possible to have exceptions raised here, typically by
2791 # compilation of odd code (such as a naked 'return' outside a
2791 # compilation of odd code (such as a naked 'return' outside a
2792 # function) that did parse but isn't valid. Typically the exception
2792 # function) that did parse but isn't valid. Typically the exception
2793 # is a SyntaxError, but it's safest just to catch anything and show
2793 # is a SyntaxError, but it's safest just to catch anything and show
2794 # the user a traceback.
2794 # the user a traceback.
2795
2795
2796 # We do only one try/except outside the loop to minimize the impact
2796 # We do only one try/except outside the loop to minimize the impact
2797 # on runtime, and also because if any node in the node list is
2797 # on runtime, and also because if any node in the node list is
2798 # broken, we should stop execution completely.
2798 # broken, we should stop execution completely.
2799 self.showtraceback()
2799 self.showtraceback()
2800
2800
2801 return False
2801 return False
2802
2802
2803 def run_code(self, code_obj):
2803 def run_code(self, code_obj):
2804 """Execute a code object.
2804 """Execute a code object.
2805
2805
2806 When an exception occurs, self.showtraceback() is called to display a
2806 When an exception occurs, self.showtraceback() is called to display a
2807 traceback.
2807 traceback.
2808
2808
2809 Parameters
2809 Parameters
2810 ----------
2810 ----------
2811 code_obj : code object
2811 code_obj : code object
2812 A compiled code object, to be executed
2812 A compiled code object, to be executed
2813
2813
2814 Returns
2814 Returns
2815 -------
2815 -------
2816 False : successful execution.
2816 False : successful execution.
2817 True : an error occurred.
2817 True : an error occurred.
2818 """
2818 """
2819
2819
2820 # Set our own excepthook in case the user code tries to call it
2820 # Set our own excepthook in case the user code tries to call it
2821 # directly, so that the IPython crash handler doesn't get triggered
2821 # directly, so that the IPython crash handler doesn't get triggered
2822 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2822 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2823
2823
2824 # we save the original sys.excepthook in the instance, in case config
2824 # we save the original sys.excepthook in the instance, in case config
2825 # code (such as magics) needs access to it.
2825 # code (such as magics) needs access to it.
2826 self.sys_excepthook = old_excepthook
2826 self.sys_excepthook = old_excepthook
2827 outflag = 1 # happens in more places, so it's easier as default
2827 outflag = 1 # happens in more places, so it's easier as default
2828 try:
2828 try:
2829 try:
2829 try:
2830 self.hooks.pre_run_code_hook()
2830 self.hooks.pre_run_code_hook()
2831 #rprint('Running code', repr(code_obj)) # dbg
2831 #rprint('Running code', repr(code_obj)) # dbg
2832 exec code_obj in self.user_global_ns, self.user_ns
2832 exec code_obj in self.user_global_ns, self.user_ns
2833 finally:
2833 finally:
2834 # Reset our crash handler in place
2834 # Reset our crash handler in place
2835 sys.excepthook = old_excepthook
2835 sys.excepthook = old_excepthook
2836 except SystemExit:
2836 except SystemExit:
2837 self.showtraceback(exception_only=True)
2837 self.showtraceback(exception_only=True)
2838 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2838 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2839 except self.custom_exceptions:
2839 except self.custom_exceptions:
2840 etype,value,tb = sys.exc_info()
2840 etype,value,tb = sys.exc_info()
2841 self.CustomTB(etype,value,tb)
2841 self.CustomTB(etype,value,tb)
2842 except:
2842 except:
2843 self.showtraceback()
2843 self.showtraceback()
2844 else:
2844 else:
2845 outflag = 0
2845 outflag = 0
2846 return outflag
2846 return outflag
2847
2847
2848 # For backwards compatibility
2848 # For backwards compatibility
2849 runcode = run_code
2849 runcode = run_code
2850
2850
2851 #-------------------------------------------------------------------------
2851 #-------------------------------------------------------------------------
2852 # Things related to GUI support and pylab
2852 # Things related to GUI support and pylab
2853 #-------------------------------------------------------------------------
2853 #-------------------------------------------------------------------------
2854
2854
2855 def enable_gui(self, gui=None):
2855 def enable_gui(self, gui=None):
2856 raise NotImplementedError('Implement enable_gui in a subclass')
2856 raise NotImplementedError('Implement enable_gui in a subclass')
2857
2857
2858 def enable_matplotlib(self, gui=None):
2858 def enable_matplotlib(self, gui=None):
2859 """Enable interactive matplotlib and inline figure support.
2859 """Enable interactive matplotlib and inline figure support.
2860
2860
2861 This takes the following steps:
2861 This takes the following steps:
2862
2862
2863 1. select the appropriate eventloop and matplotlib backend
2863 1. select the appropriate eventloop and matplotlib backend
2864 2. set up matplotlib for interactive use with that backend
2864 2. set up matplotlib for interactive use with that backend
2865 3. configure formatters for inline figure display
2865 3. configure formatters for inline figure display
2866 4. enable the selected gui eventloop
2866 4. enable the selected gui eventloop
2867
2867
2868 Parameters
2868 Parameters
2869 ----------
2869 ----------
2870 gui : optional, string
2870 gui : optional, string
2871 If given, dictates the choice of matplotlib GUI backend to use
2871 If given, dictates the choice of matplotlib GUI backend to use
2872 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2872 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2873 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2873 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2874 matplotlib (as dictated by the matplotlib build-time options plus the
2874 matplotlib (as dictated by the matplotlib build-time options plus the
2875 user's matplotlibrc configuration file). Note that not all backends
2875 user's matplotlibrc configuration file). Note that not all backends
2876 make sense in all contexts, for example a terminal ipython can't
2876 make sense in all contexts, for example a terminal ipython can't
2877 display figures inline.
2877 display figures inline.
2878 """
2878 """
2879 from IPython.core import pylabtools as pt
2879 from IPython.core import pylabtools as pt
2880 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2880 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2881
2881
2882 if gui != 'inline':
2882 if gui != 'inline':
2883 # If we have our first gui selection, store it
2883 # If we have our first gui selection, store it
2884 if self.pylab_gui_select is None:
2884 if self.pylab_gui_select is None:
2885 self.pylab_gui_select = gui
2885 self.pylab_gui_select = gui
2886 # Otherwise if they are different
2886 # Otherwise if they are different
2887 elif gui != self.pylab_gui_select:
2887 elif gui != self.pylab_gui_select:
2888 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2888 print ('Warning: Cannot change to a different GUI toolkit: %s.'
2889 ' Using %s instead.' % (gui, self.pylab_gui_select))
2889 ' Using %s instead.' % (gui, self.pylab_gui_select))
2890 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2890 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
2891
2891
2892 pt.activate_matplotlib(backend)
2892 pt.activate_matplotlib(backend)
2893 pt.configure_inline_support(self, backend)
2893 pt.configure_inline_support(self, backend)
2894
2894
2895 # Now we must activate the gui pylab wants to use, and fix %run to take
2895 # Now we must activate the gui pylab wants to use, and fix %run to take
2896 # plot updates into account
2896 # plot updates into account
2897 self.enable_gui(gui)
2897 self.enable_gui(gui)
2898 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2898 self.magics_manager.registry['ExecutionMagics'].default_runner = \
2899 pt.mpl_runner(self.safe_execfile)
2899 pt.mpl_runner(self.safe_execfile)
2900
2900
2901 return gui, backend
2901 return gui, backend
2902
2902
2903 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2903 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
2904 """Activate pylab support at runtime.
2904 """Activate pylab support at runtime.
2905
2905
2906 This turns on support for matplotlib, preloads into the interactive
2906 This turns on support for matplotlib, preloads into the interactive
2907 namespace all of numpy and pylab, and configures IPython to correctly
2907 namespace all of numpy and pylab, and configures IPython to correctly
2908 interact with the GUI event loop. The GUI backend to be used can be
2908 interact with the GUI event loop. The GUI backend to be used can be
2909 optionally selected with the optional ``gui`` argument.
2909 optionally selected with the optional ``gui`` argument.
2910
2910
2911 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2911 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
2912
2912
2913 Parameters
2913 Parameters
2914 ----------
2914 ----------
2915 gui : optional, string
2915 gui : optional, string
2916 If given, dictates the choice of matplotlib GUI backend to use
2916 If given, dictates the choice of matplotlib GUI backend to use
2917 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2917 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
2918 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2918 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
2919 matplotlib (as dictated by the matplotlib build-time options plus the
2919 matplotlib (as dictated by the matplotlib build-time options plus the
2920 user's matplotlibrc configuration file). Note that not all backends
2920 user's matplotlibrc configuration file). Note that not all backends
2921 make sense in all contexts, for example a terminal ipython can't
2921 make sense in all contexts, for example a terminal ipython can't
2922 display figures inline.
2922 display figures inline.
2923 import_all : optional, bool, default: True
2923 import_all : optional, bool, default: True
2924 Whether to do `from numpy import *` and `from pylab import *`
2924 Whether to do `from numpy import *` and `from pylab import *`
2925 in addition to module imports.
2925 in addition to module imports.
2926 welcome_message : deprecated
2926 welcome_message : deprecated
2927 This argument is ignored, no welcome message will be displayed.
2927 This argument is ignored, no welcome message will be displayed.
2928 """
2928 """
2929 from IPython.core.pylabtools import import_pylab
2929 from IPython.core.pylabtools import import_pylab
2930
2930
2931 gui, backend = self.enable_matplotlib(gui)
2931 gui, backend = self.enable_matplotlib(gui)
2932
2932
2933 # We want to prevent the loading of pylab to pollute the user's
2933 # We want to prevent the loading of pylab to pollute the user's
2934 # namespace as shown by the %who* magics, so we execute the activation
2934 # namespace as shown by the %who* magics, so we execute the activation
2935 # code in an empty namespace, and we update *both* user_ns and
2935 # code in an empty namespace, and we update *both* user_ns and
2936 # user_ns_hidden with this information.
2936 # user_ns_hidden with this information.
2937 ns = {}
2937 ns = {}
2938 import_pylab(ns, import_all)
2938 import_pylab(ns, import_all)
2939 # warn about clobbered names
2939 # warn about clobbered names
2940 ignored = set(["__builtins__"])
2940 ignored = set(["__builtins__"])
2941 both = set(ns).intersection(self.user_ns).difference(ignored)
2941 both = set(ns).intersection(self.user_ns).difference(ignored)
2942 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2942 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
2943 self.user_ns.update(ns)
2943 self.user_ns.update(ns)
2944 self.user_ns_hidden.update(ns)
2944 self.user_ns_hidden.update(ns)
2945 return gui, backend, clobbered
2945 return gui, backend, clobbered
2946
2946
2947 #-------------------------------------------------------------------------
2947 #-------------------------------------------------------------------------
2948 # Utilities
2948 # Utilities
2949 #-------------------------------------------------------------------------
2949 #-------------------------------------------------------------------------
2950
2950
2951 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2951 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
2952 """Expand python variables in a string.
2952 """Expand python variables in a string.
2953
2953
2954 The depth argument indicates how many frames above the caller should
2954 The depth argument indicates how many frames above the caller should
2955 be walked to look for the local namespace where to expand variables.
2955 be walked to look for the local namespace where to expand variables.
2956
2956
2957 The global namespace for expansion is always the user's interactive
2957 The global namespace for expansion is always the user's interactive
2958 namespace.
2958 namespace.
2959 """
2959 """
2960 ns = self.user_ns.copy()
2960 ns = self.user_ns.copy()
2961 ns.update(sys._getframe(depth+1).f_locals)
2961 ns.update(sys._getframe(depth+1).f_locals)
2962 try:
2962 try:
2963 # We have to use .vformat() here, because 'self' is a valid and common
2963 # We have to use .vformat() here, because 'self' is a valid and common
2964 # name, and expanding **ns for .format() would make it collide with
2964 # name, and expanding **ns for .format() would make it collide with
2965 # the 'self' argument of the method.
2965 # the 'self' argument of the method.
2966 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
2966 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
2967 except Exception:
2967 except Exception:
2968 # if formatter couldn't format, just let it go untransformed
2968 # if formatter couldn't format, just let it go untransformed
2969 pass
2969 pass
2970 return cmd
2970 return cmd
2971
2971
2972 def mktempfile(self, data=None, prefix='ipython_edit_'):
2972 def mktempfile(self, data=None, prefix='ipython_edit_'):
2973 """Make a new tempfile and return its filename.
2973 """Make a new tempfile and return its filename.
2974
2974
2975 This makes a call to tempfile.mktemp, but it registers the created
2975 This makes a call to tempfile.mktemp, but it registers the created
2976 filename internally so ipython cleans it up at exit time.
2976 filename internally so ipython cleans it up at exit time.
2977
2977
2978 Optional inputs:
2978 Optional inputs:
2979
2979
2980 - data(None): if data is given, it gets written out to the temp file
2980 - data(None): if data is given, it gets written out to the temp file
2981 immediately, and the file is closed again."""
2981 immediately, and the file is closed again."""
2982
2982
2983 filename = tempfile.mktemp('.py', prefix)
2983 filename = tempfile.mktemp('.py', prefix)
2984 self.tempfiles.append(filename)
2984 self.tempfiles.append(filename)
2985
2985
2986 if data:
2986 if data:
2987 tmp_file = open(filename,'w')
2987 tmp_file = open(filename,'w')
2988 tmp_file.write(data)
2988 tmp_file.write(data)
2989 tmp_file.close()
2989 tmp_file.close()
2990 return filename
2990 return filename
2991
2991
2992 # TODO: This should be removed when Term is refactored.
2992 # TODO: This should be removed when Term is refactored.
2993 def write(self,data):
2993 def write(self,data):
2994 """Write a string to the default output"""
2994 """Write a string to the default output"""
2995 io.stdout.write(data)
2995 io.stdout.write(data)
2996
2996
2997 # TODO: This should be removed when Term is refactored.
2997 # TODO: This should be removed when Term is refactored.
2998 def write_err(self,data):
2998 def write_err(self,data):
2999 """Write a string to the default error output"""
2999 """Write a string to the default error output"""
3000 io.stderr.write(data)
3000 io.stderr.write(data)
3001
3001
3002 def ask_yes_no(self, prompt, default=None):
3002 def ask_yes_no(self, prompt, default=None):
3003 if self.quiet:
3003 if self.quiet:
3004 return True
3004 return True
3005 return ask_yes_no(prompt,default)
3005 return ask_yes_no(prompt,default)
3006
3006
3007 def show_usage(self):
3007 def show_usage(self):
3008 """Show a usage message"""
3008 """Show a usage message"""
3009 page.page(IPython.core.usage.interactive_usage)
3009 page.page(IPython.core.usage.interactive_usage)
3010
3010
3011 def extract_input_lines(self, range_str, raw=False):
3011 def extract_input_lines(self, range_str, raw=False):
3012 """Return as a string a set of input history slices.
3012 """Return as a string a set of input history slices.
3013
3013
3014 Parameters
3014 Parameters
3015 ----------
3015 ----------
3016 range_str : string
3016 range_str : string
3017 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3017 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3018 since this function is for use by magic functions which get their
3018 since this function is for use by magic functions which get their
3019 arguments as strings. The number before the / is the session
3019 arguments as strings. The number before the / is the session
3020 number: ~n goes n back from the current session.
3020 number: ~n goes n back from the current session.
3021
3021
3022 Optional Parameters:
3022 Optional Parameters:
3023 - raw(False): by default, the processed input is used. If this is
3023 - raw(False): by default, the processed input is used. If this is
3024 true, the raw input history is used instead.
3024 true, the raw input history is used instead.
3025
3025
3026 Note that slices can be called with two notations:
3026 Note that slices can be called with two notations:
3027
3027
3028 N:M -> standard python form, means including items N...(M-1).
3028 N:M -> standard python form, means including items N...(M-1).
3029
3029
3030 N-M -> include items N..M (closed endpoint)."""
3030 N-M -> include items N..M (closed endpoint).
3031 """
3031 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3032 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3032 return "\n".join(x for _, _, x in lines)
3033 return "\n".join(x for _, _, x in lines)
3033
3034
3034 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True):
3035 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True):
3035 """Get a code string from history, file, url, or a string or macro.
3036 """Get a code string from history, file, url, or a string or macro.
3036
3037
3037 This is mainly used by magic functions.
3038 This is mainly used by magic functions.
3038
3039
3039 Parameters
3040 Parameters
3040 ----------
3041 ----------
3041
3042
3042 target : str
3043 target : str
3043
3044
3044 A string specifying code to retrieve. This will be tried respectively
3045 A string specifying code to retrieve. This will be tried respectively
3045 as: ranges of input history (see %history for syntax), url,
3046 as: ranges of input history (see %history for syntax), url,
3046 correspnding .py file, filename, or an expression evaluating to a
3047 correspnding .py file, filename, or an expression evaluating to a
3047 string or Macro in the user namespace.
3048 string or Macro in the user namespace.
3048
3049
3049 raw : bool
3050 raw : bool
3050 If true (default), retrieve raw history. Has no effect on the other
3051 If true (default), retrieve raw history. Has no effect on the other
3051 retrieval mechanisms.
3052 retrieval mechanisms.
3052
3053
3053 py_only : bool (default False)
3054 py_only : bool (default False)
3054 Only try to fetch python code, do not try alternative methods to decode file
3055 Only try to fetch python code, do not try alternative methods to decode file
3055 if unicode fails.
3056 if unicode fails.
3056
3057
3057 Returns
3058 Returns
3058 -------
3059 -------
3059 A string of code.
3060 A string of code.
3060
3061
3061 ValueError is raised if nothing is found, and TypeError if it evaluates
3062 ValueError is raised if nothing is found, and TypeError if it evaluates
3062 to an object of another type. In each case, .args[0] is a printable
3063 to an object of another type. In each case, .args[0] is a printable
3063 message.
3064 message.
3064 """
3065 """
3065 code = self.extract_input_lines(target, raw=raw) # Grab history
3066 code = self.extract_input_lines(target, raw=raw) # Grab history
3066 if code:
3067 if code:
3067 return code
3068 return code
3068 utarget = unquote_filename(target)
3069 utarget = unquote_filename(target)
3069 try:
3070 try:
3070 if utarget.startswith(('http://', 'https://')):
3071 if utarget.startswith(('http://', 'https://')):
3071 return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie)
3072 return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie)
3072 except UnicodeDecodeError:
3073 except UnicodeDecodeError:
3073 if not py_only :
3074 if not py_only :
3074 from urllib import urlopen # Deferred import
3075 from urllib import urlopen # Deferred import
3075 response = urlopen(target)
3076 response = urlopen(target)
3076 return response.read().decode('latin1')
3077 return response.read().decode('latin1')
3077 raise ValueError(("'%s' seem to be unreadable.") % utarget)
3078 raise ValueError(("'%s' seem to be unreadable.") % utarget)
3078
3079
3079 potential_target = [target]
3080 potential_target = [target]
3080 try :
3081 try :
3081 potential_target.insert(0,get_py_filename(target))
3082 potential_target.insert(0,get_py_filename(target))
3082 except IOError:
3083 except IOError:
3083 pass
3084 pass
3084
3085
3085 for tgt in potential_target :
3086 for tgt in potential_target :
3086 if os.path.isfile(tgt): # Read file
3087 if os.path.isfile(tgt): # Read file
3087 try :
3088 try :
3088 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3089 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3089 except UnicodeDecodeError :
3090 except UnicodeDecodeError :
3090 if not py_only :
3091 if not py_only :
3091 with io_open(tgt,'r', encoding='latin1') as f :
3092 with io_open(tgt,'r', encoding='latin1') as f :
3092 return f.read()
3093 return f.read()
3093 raise ValueError(("'%s' seem to be unreadable.") % target)
3094 raise ValueError(("'%s' seem to be unreadable.") % target)
3094 elif os.path.isdir(os.path.expanduser(tgt)):
3095 elif os.path.isdir(os.path.expanduser(tgt)):
3095 raise ValueError("'%s' is a directory, not a regular file." % target)
3096 raise ValueError("'%s' is a directory, not a regular file." % target)
3096
3097
3097 try: # User namespace
3098 try: # User namespace
3098 codeobj = eval(target, self.user_ns)
3099 codeobj = eval(target, self.user_ns)
3099 except Exception:
3100 except Exception:
3100 raise ValueError(("'%s' was not found in history, as a file, url, "
3101 raise ValueError(("'%s' was not found in history, as a file, url, "
3101 "nor in the user namespace.") % target)
3102 "nor in the user namespace.") % target)
3102 if isinstance(codeobj, basestring):
3103 if isinstance(codeobj, basestring):
3103 return codeobj
3104 return codeobj
3104 elif isinstance(codeobj, Macro):
3105 elif isinstance(codeobj, Macro):
3105 return codeobj.value
3106 return codeobj.value
3106
3107
3107 raise TypeError("%s is neither a string nor a macro." % target,
3108 raise TypeError("%s is neither a string nor a macro." % target,
3108 codeobj)
3109 codeobj)
3109
3110
3110 #-------------------------------------------------------------------------
3111 #-------------------------------------------------------------------------
3111 # Things related to IPython exiting
3112 # Things related to IPython exiting
3112 #-------------------------------------------------------------------------
3113 #-------------------------------------------------------------------------
3113 def atexit_operations(self):
3114 def atexit_operations(self):
3114 """This will be executed at the time of exit.
3115 """This will be executed at the time of exit.
3115
3116
3116 Cleanup operations and saving of persistent data that is done
3117 Cleanup operations and saving of persistent data that is done
3117 unconditionally by IPython should be performed here.
3118 unconditionally by IPython should be performed here.
3118
3119
3119 For things that may depend on startup flags or platform specifics (such
3120 For things that may depend on startup flags or platform specifics (such
3120 as having readline or not), register a separate atexit function in the
3121 as having readline or not), register a separate atexit function in the
3121 code that has the appropriate information, rather than trying to
3122 code that has the appropriate information, rather than trying to
3122 clutter
3123 clutter
3123 """
3124 """
3124 # Close the history session (this stores the end time and line count)
3125 # Close the history session (this stores the end time and line count)
3125 # this must be *before* the tempfile cleanup, in case of temporary
3126 # this must be *before* the tempfile cleanup, in case of temporary
3126 # history db
3127 # history db
3127 self.history_manager.end_session()
3128 self.history_manager.end_session()
3128
3129
3129 # Cleanup all tempfiles left around
3130 # Cleanup all tempfiles left around
3130 for tfile in self.tempfiles:
3131 for tfile in self.tempfiles:
3131 try:
3132 try:
3132 os.unlink(tfile)
3133 os.unlink(tfile)
3133 except OSError:
3134 except OSError:
3134 pass
3135 pass
3135
3136
3136 # Clear all user namespaces to release all references cleanly.
3137 # Clear all user namespaces to release all references cleanly.
3137 self.reset(new_session=False)
3138 self.reset(new_session=False)
3138
3139
3139 # Run user hooks
3140 # Run user hooks
3140 self.hooks.shutdown_hook()
3141 self.hooks.shutdown_hook()
3141
3142
3142 def cleanup(self):
3143 def cleanup(self):
3143 self.restore_sys_module_state()
3144 self.restore_sys_module_state()
3144
3145
3145
3146
3146 class InteractiveShellABC(object):
3147 class InteractiveShellABC(object):
3147 """An abstract base class for InteractiveShell."""
3148 """An abstract base class for InteractiveShell."""
3148 __metaclass__ = abc.ABCMeta
3149 __metaclass__ = abc.ABCMeta
3149
3150
3150 InteractiveShellABC.register(InteractiveShell)
3151 InteractiveShellABC.register(InteractiveShell)
1 NO CONTENT: modified file
NO CONTENT: modified file
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -1,1231 +1,1248 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Implementation of execution-related magic functions.
2 """Implementation of execution-related magic functions.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2012 The IPython Development Team.
5 # Copyright (c) 2012 The IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 # Stdlib
16 # Stdlib
17 import __builtin__ as builtin_mod
17 import __builtin__ as builtin_mod
18 import ast
18 import ast
19 import bdb
19 import bdb
20 import os
20 import os
21 import sys
21 import sys
22 import time
22 import time
23 from StringIO import StringIO
23 from StringIO import StringIO
24
24
25 # cProfile was added in Python2.5
25 # cProfile was added in Python2.5
26 try:
26 try:
27 import cProfile as profile
27 import cProfile as profile
28 import pstats
28 import pstats
29 except ImportError:
29 except ImportError:
30 # profile isn't bundled by default in Debian for license reasons
30 # profile isn't bundled by default in Debian for license reasons
31 try:
31 try:
32 import profile, pstats
32 import profile, pstats
33 except ImportError:
33 except ImportError:
34 profile = pstats = None
34 profile = pstats = None
35
35
36 # Our own packages
36 # Our own packages
37 from IPython.core import debugger, oinspect
37 from IPython.core import debugger, oinspect
38 from IPython.core import magic_arguments
38 from IPython.core import magic_arguments
39 from IPython.core import page
39 from IPython.core import page
40 from IPython.core.error import UsageError
40 from IPython.core.error import UsageError
41 from IPython.core.macro import Macro
41 from IPython.core.macro import Macro
42 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
42 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
43 line_cell_magic, on_off, needs_local_scope)
43 line_cell_magic, on_off, needs_local_scope)
44 from IPython.testing.skipdoctest import skip_doctest
44 from IPython.testing.skipdoctest import skip_doctest
45 from IPython.utils import py3compat
45 from IPython.utils import py3compat
46 from IPython.utils.contexts import preserve_keys
46 from IPython.utils.contexts import preserve_keys
47 from IPython.utils.io import capture_output
47 from IPython.utils.io import capture_output
48 from IPython.utils.ipstruct import Struct
48 from IPython.utils.ipstruct import Struct
49 from IPython.utils.module_paths import find_mod
49 from IPython.utils.module_paths import find_mod
50 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
50 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
51 from IPython.utils.timing import clock, clock2
51 from IPython.utils.timing import clock, clock2
52 from IPython.utils.warn import warn, error
52 from IPython.utils.warn import warn, error
53
53
54
54
55 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
56 # Magic implementation classes
56 # Magic implementation classes
57 #-----------------------------------------------------------------------------
57 #-----------------------------------------------------------------------------
58
58
59 @magics_class
59 @magics_class
60 class ExecutionMagics(Magics):
60 class ExecutionMagics(Magics):
61 """Magics related to code execution, debugging, profiling, etc.
61 """Magics related to code execution, debugging, profiling, etc.
62
62
63 """
63 """
64
64
65 def __init__(self, shell):
65 def __init__(self, shell):
66 super(ExecutionMagics, self).__init__(shell)
66 super(ExecutionMagics, self).__init__(shell)
67 if profile is None:
67 if profile is None:
68 self.prun = self.profile_missing_notice
68 self.prun = self.profile_missing_notice
69 # Default execution function used to actually run user code.
69 # Default execution function used to actually run user code.
70 self.default_runner = None
70 self.default_runner = None
71
71
72 def profile_missing_notice(self, *args, **kwargs):
72 def profile_missing_notice(self, *args, **kwargs):
73 error("""\
73 error("""\
74 The profile module could not be found. It has been removed from the standard
74 The profile module could not be found. It has been removed from the standard
75 python packages because of its non-free license. To use profiling, install the
75 python packages because of its non-free license. To use profiling, install the
76 python-profiler package from non-free.""")
76 python-profiler package from non-free.""")
77
77
78 @skip_doctest
78 @skip_doctest
79 @line_cell_magic
79 @line_cell_magic
80 def prun(self, parameter_s='', cell=None):
80 def prun(self, parameter_s='', cell=None):
81
81
82 """Run a statement through the python code profiler.
82 """Run a statement through the python code profiler.
83
83
84 Usage, in line mode:
84 Usage, in line mode:
85 %prun [options] statement
85 %prun [options] statement
86
86
87 Usage, in cell mode:
87 Usage, in cell mode:
88 %%prun [options] [statement]
88 %%prun [options] [statement]
89 code...
89 code...
90 code...
90 code...
91
91
92 In cell mode, the additional code lines are appended to the (possibly
92 In cell mode, the additional code lines are appended to the (possibly
93 empty) statement in the first line. Cell mode allows you to easily
93 empty) statement in the first line. Cell mode allows you to easily
94 profile multiline blocks without having to put them in a separate
94 profile multiline blocks without having to put them in a separate
95 function.
95 function.
96
96
97 The given statement (which doesn't require quote marks) is run via the
97 The given statement (which doesn't require quote marks) is run via the
98 python profiler in a manner similar to the profile.run() function.
98 python profiler in a manner similar to the profile.run() function.
99 Namespaces are internally managed to work correctly; profile.run
99 Namespaces are internally managed to work correctly; profile.run
100 cannot be used in IPython because it makes certain assumptions about
100 cannot be used in IPython because it makes certain assumptions about
101 namespaces which do not hold under IPython.
101 namespaces which do not hold under IPython.
102
102
103 Options:
103 Options:
104
104
105 -l <limit>: you can place restrictions on what or how much of the
105 -l <limit>
106 you can place restrictions on what or how much of the
106 profile gets printed. The limit value can be:
107 profile gets printed. The limit value can be:
107
108
108 * A string: only information for function names containing this string
109 * A string: only information for function names containing this string
109 is printed.
110 is printed.
110
111
111 * An integer: only these many lines are printed.
112 * An integer: only these many lines are printed.
112
113
113 * A float (between 0 and 1): this fraction of the report is printed
114 * A float (between 0 and 1): this fraction of the report is printed
114 (for example, use a limit of 0.4 to see the topmost 40% only).
115 (for example, use a limit of 0.4 to see the topmost 40% only).
115
116
116 You can combine several limits with repeated use of the option. For
117 You can combine several limits with repeated use of the option. For
117 example, '-l __init__ -l 5' will print only the topmost 5 lines of
118 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
118 information about class constructors.
119 information about class constructors.
119
120
120 -r: return the pstats.Stats object generated by the profiling. This
121 -r
122 return the pstats.Stats object generated by the profiling. This
121 object has all the information about the profile in it, and you can
123 object has all the information about the profile in it, and you can
122 later use it for further analysis or in other functions.
124 later use it for further analysis or in other functions.
123
125
124 -s <key>: sort profile by given key. You can provide more than one key
126 -s <key>
127 sort profile by given key. You can provide more than one key
125 by using the option several times: '-s key1 -s key2 -s key3...'. The
128 by using the option several times: '-s key1 -s key2 -s key3...'. The
126 default sorting key is 'time'.
129 default sorting key is 'time'.
127
130
128 The following is copied verbatim from the profile documentation
131 The following is copied verbatim from the profile documentation
129 referenced below:
132 referenced below:
130
133
131 When more than one key is provided, additional keys are used as
134 When more than one key is provided, additional keys are used as
132 secondary criteria when the there is equality in all keys selected
135 secondary criteria when the there is equality in all keys selected
133 before them.
136 before them.
134
137
135 Abbreviations can be used for any key names, as long as the
138 Abbreviations can be used for any key names, as long as the
136 abbreviation is unambiguous. The following are the keys currently
139 abbreviation is unambiguous. The following are the keys currently
137 defined:
140 defined:
138
141
142 ============ =====================
139 Valid Arg Meaning
143 Valid Arg Meaning
144 ============ =====================
140 "calls" call count
145 "calls" call count
141 "cumulative" cumulative time
146 "cumulative" cumulative time
142 "file" file name
147 "file" file name
143 "module" file name
148 "module" file name
144 "pcalls" primitive call count
149 "pcalls" primitive call count
145 "line" line number
150 "line" line number
146 "name" function name
151 "name" function name
147 "nfl" name/file/line
152 "nfl" name/file/line
148 "stdname" standard name
153 "stdname" standard name
149 "time" internal time
154 "time" internal time
155 ============ =====================
150
156
151 Note that all sorts on statistics are in descending order (placing
157 Note that all sorts on statistics are in descending order (placing
152 most time consuming items first), where as name, file, and line number
158 most time consuming items first), where as name, file, and line number
153 searches are in ascending order (i.e., alphabetical). The subtle
159 searches are in ascending order (i.e., alphabetical). The subtle
154 distinction between "nfl" and "stdname" is that the standard name is a
160 distinction between "nfl" and "stdname" is that the standard name is a
155 sort of the name as printed, which means that the embedded line
161 sort of the name as printed, which means that the embedded line
156 numbers get compared in an odd way. For example, lines 3, 20, and 40
162 numbers get compared in an odd way. For example, lines 3, 20, and 40
157 would (if the file names were the same) appear in the string order
163 would (if the file names were the same) appear in the string order
158 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
164 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
159 line numbers. In fact, sort_stats("nfl") is the same as
165 line numbers. In fact, sort_stats("nfl") is the same as
160 sort_stats("name", "file", "line").
166 sort_stats("name", "file", "line").
161
167
162 -T <filename>: save profile results as shown on screen to a text
168 -T <filename>
169 save profile results as shown on screen to a text
163 file. The profile is still shown on screen.
170 file. The profile is still shown on screen.
164
171
165 -D <filename>: save (via dump_stats) profile statistics to given
172 -D <filename>
173 save (via dump_stats) profile statistics to given
166 filename. This data is in a format understood by the pstats module, and
174 filename. This data is in a format understood by the pstats module, and
167 is generated by a call to the dump_stats() method of profile
175 is generated by a call to the dump_stats() method of profile
168 objects. The profile is still shown on screen.
176 objects. The profile is still shown on screen.
169
177
170 -q: suppress output to the pager. Best used with -T and/or -D above.
178 -q
179 suppress output to the pager. Best used with -T and/or -D above.
171
180
172 If you want to run complete programs under the profiler's control, use
181 If you want to run complete programs under the profiler's control, use
173 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
182 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
174 contains profiler specific options as described here.
183 contains profiler specific options as described here.
175
184
176 You can read the complete documentation for the profile module with::
185 You can read the complete documentation for the profile module with::
177
186
178 In [1]: import profile; profile.help()
187 In [1]: import profile; profile.help()
179 """
188 """
180 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
189 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
181 list_all=True, posix=False)
190 list_all=True, posix=False)
182 if cell is not None:
191 if cell is not None:
183 arg_str += '\n' + cell
192 arg_str += '\n' + cell
184 arg_str = self.shell.input_splitter.transform_cell(arg_str)
193 arg_str = self.shell.input_splitter.transform_cell(arg_str)
185 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
194 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
186
195
187 def _run_with_profiler(self, code, opts, namespace):
196 def _run_with_profiler(self, code, opts, namespace):
188 """
197 """
189 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
198 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
190
199
191 Parameters
200 Parameters
192 ----------
201 ----------
193 code : str
202 code : str
194 Code to be executed.
203 Code to be executed.
195 opts : Struct
204 opts : Struct
196 Options parsed by `self.parse_options`.
205 Options parsed by `self.parse_options`.
197 namespace : dict
206 namespace : dict
198 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
207 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
199
208
200 """
209 """
201
210
202 # Fill default values for unspecified options:
211 # Fill default values for unspecified options:
203 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
212 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
204
213
205 prof = profile.Profile()
214 prof = profile.Profile()
206 try:
215 try:
207 prof = prof.runctx(code, namespace, namespace)
216 prof = prof.runctx(code, namespace, namespace)
208 sys_exit = ''
217 sys_exit = ''
209 except SystemExit:
218 except SystemExit:
210 sys_exit = """*** SystemExit exception caught in code being profiled."""
219 sys_exit = """*** SystemExit exception caught in code being profiled."""
211
220
212 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
221 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
213
222
214 lims = opts.l
223 lims = opts.l
215 if lims:
224 if lims:
216 lims = [] # rebuild lims with ints/floats/strings
225 lims = [] # rebuild lims with ints/floats/strings
217 for lim in opts.l:
226 for lim in opts.l:
218 try:
227 try:
219 lims.append(int(lim))
228 lims.append(int(lim))
220 except ValueError:
229 except ValueError:
221 try:
230 try:
222 lims.append(float(lim))
231 lims.append(float(lim))
223 except ValueError:
232 except ValueError:
224 lims.append(lim)
233 lims.append(lim)
225
234
226 # Trap output.
235 # Trap output.
227 stdout_trap = StringIO()
236 stdout_trap = StringIO()
228 stats_stream = stats.stream
237 stats_stream = stats.stream
229 try:
238 try:
230 stats.stream = stdout_trap
239 stats.stream = stdout_trap
231 stats.print_stats(*lims)
240 stats.print_stats(*lims)
232 finally:
241 finally:
233 stats.stream = stats_stream
242 stats.stream = stats_stream
234
243
235 output = stdout_trap.getvalue()
244 output = stdout_trap.getvalue()
236 output = output.rstrip()
245 output = output.rstrip()
237
246
238 if 'q' not in opts:
247 if 'q' not in opts:
239 page.page(output)
248 page.page(output)
240 print sys_exit,
249 print sys_exit,
241
250
242 dump_file = opts.D[0]
251 dump_file = opts.D[0]
243 text_file = opts.T[0]
252 text_file = opts.T[0]
244 if dump_file:
253 if dump_file:
245 dump_file = unquote_filename(dump_file)
254 dump_file = unquote_filename(dump_file)
246 prof.dump_stats(dump_file)
255 prof.dump_stats(dump_file)
247 print '\n*** Profile stats marshalled to file',\
256 print '\n*** Profile stats marshalled to file',\
248 repr(dump_file)+'.',sys_exit
257 repr(dump_file)+'.',sys_exit
249 if text_file:
258 if text_file:
250 text_file = unquote_filename(text_file)
259 text_file = unquote_filename(text_file)
251 pfile = open(text_file,'w')
260 pfile = open(text_file,'w')
252 pfile.write(output)
261 pfile.write(output)
253 pfile.close()
262 pfile.close()
254 print '\n*** Profile printout saved to text file',\
263 print '\n*** Profile printout saved to text file',\
255 repr(text_file)+'.',sys_exit
264 repr(text_file)+'.',sys_exit
256
265
257 if 'r' in opts:
266 if 'r' in opts:
258 return stats
267 return stats
259 else:
268 else:
260 return None
269 return None
261
270
262 @line_magic
271 @line_magic
263 def pdb(self, parameter_s=''):
272 def pdb(self, parameter_s=''):
264 """Control the automatic calling of the pdb interactive debugger.
273 """Control the automatic calling of the pdb interactive debugger.
265
274
266 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
275 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
267 argument it works as a toggle.
276 argument it works as a toggle.
268
277
269 When an exception is triggered, IPython can optionally call the
278 When an exception is triggered, IPython can optionally call the
270 interactive pdb debugger after the traceback printout. %pdb toggles
279 interactive pdb debugger after the traceback printout. %pdb toggles
271 this feature on and off.
280 this feature on and off.
272
281
273 The initial state of this feature is set in your configuration
282 The initial state of this feature is set in your configuration
274 file (the option is ``InteractiveShell.pdb``).
283 file (the option is ``InteractiveShell.pdb``).
275
284
276 If you want to just activate the debugger AFTER an exception has fired,
285 If you want to just activate the debugger AFTER an exception has fired,
277 without having to type '%pdb on' and rerunning your code, you can use
286 without having to type '%pdb on' and rerunning your code, you can use
278 the %debug magic."""
287 the %debug magic."""
279
288
280 par = parameter_s.strip().lower()
289 par = parameter_s.strip().lower()
281
290
282 if par:
291 if par:
283 try:
292 try:
284 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
293 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
285 except KeyError:
294 except KeyError:
286 print ('Incorrect argument. Use on/1, off/0, '
295 print ('Incorrect argument. Use on/1, off/0, '
287 'or nothing for a toggle.')
296 'or nothing for a toggle.')
288 return
297 return
289 else:
298 else:
290 # toggle
299 # toggle
291 new_pdb = not self.shell.call_pdb
300 new_pdb = not self.shell.call_pdb
292
301
293 # set on the shell
302 # set on the shell
294 self.shell.call_pdb = new_pdb
303 self.shell.call_pdb = new_pdb
295 print 'Automatic pdb calling has been turned',on_off(new_pdb)
304 print 'Automatic pdb calling has been turned',on_off(new_pdb)
296
305
297 @skip_doctest
306 @skip_doctest
298 @magic_arguments.magic_arguments()
307 @magic_arguments.magic_arguments()
299 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
308 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
300 help="""
309 help="""
301 Set break point at LINE in FILE.
310 Set break point at LINE in FILE.
302 """
311 """
303 )
312 )
304 @magic_arguments.argument('statement', nargs='*',
313 @magic_arguments.argument('statement', nargs='*',
305 help="""
314 help="""
306 Code to run in debugger.
315 Code to run in debugger.
307 You can omit this in cell magic mode.
316 You can omit this in cell magic mode.
308 """
317 """
309 )
318 )
310 @line_cell_magic
319 @line_cell_magic
311 def debug(self, line='', cell=None):
320 def debug(self, line='', cell=None):
312 """Activate the interactive debugger.
321 """Activate the interactive debugger.
313
322
314 This magic command support two ways of activating debugger.
323 This magic command support two ways of activating debugger.
315 One is to activate debugger before executing code. This way, you
324 One is to activate debugger before executing code. This way, you
316 can set a break point, to step through the code from the point.
325 can set a break point, to step through the code from the point.
317 You can use this mode by giving statements to execute and optionally
326 You can use this mode by giving statements to execute and optionally
318 a breakpoint.
327 a breakpoint.
319
328
320 The other one is to activate debugger in post-mortem mode. You can
329 The other one is to activate debugger in post-mortem mode. You can
321 activate this mode simply running %debug without any argument.
330 activate this mode simply running %debug without any argument.
322 If an exception has just occurred, this lets you inspect its stack
331 If an exception has just occurred, this lets you inspect its stack
323 frames interactively. Note that this will always work only on the last
332 frames interactively. Note that this will always work only on the last
324 traceback that occurred, so you must call this quickly after an
333 traceback that occurred, so you must call this quickly after an
325 exception that you wish to inspect has fired, because if another one
334 exception that you wish to inspect has fired, because if another one
326 occurs, it clobbers the previous one.
335 occurs, it clobbers the previous one.
327
336
328 If you want IPython to automatically do this on every exception, see
337 If you want IPython to automatically do this on every exception, see
329 the %pdb magic for more details.
338 the %pdb magic for more details.
330 """
339 """
331 args = magic_arguments.parse_argstring(self.debug, line)
340 args = magic_arguments.parse_argstring(self.debug, line)
332
341
333 if not (args.breakpoint or args.statement or cell):
342 if not (args.breakpoint or args.statement or cell):
334 self._debug_post_mortem()
343 self._debug_post_mortem()
335 else:
344 else:
336 code = "\n".join(args.statement)
345 code = "\n".join(args.statement)
337 if cell:
346 if cell:
338 code += "\n" + cell
347 code += "\n" + cell
339 self._debug_exec(code, args.breakpoint)
348 self._debug_exec(code, args.breakpoint)
340
349
341 def _debug_post_mortem(self):
350 def _debug_post_mortem(self):
342 self.shell.debugger(force=True)
351 self.shell.debugger(force=True)
343
352
344 def _debug_exec(self, code, breakpoint):
353 def _debug_exec(self, code, breakpoint):
345 if breakpoint:
354 if breakpoint:
346 (filename, bp_line) = breakpoint.split(':', 1)
355 (filename, bp_line) = breakpoint.split(':', 1)
347 bp_line = int(bp_line)
356 bp_line = int(bp_line)
348 else:
357 else:
349 (filename, bp_line) = (None, None)
358 (filename, bp_line) = (None, None)
350 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
359 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
351
360
352 @line_magic
361 @line_magic
353 def tb(self, s):
362 def tb(self, s):
354 """Print the last traceback with the currently active exception mode.
363 """Print the last traceback with the currently active exception mode.
355
364
356 See %xmode for changing exception reporting modes."""
365 See %xmode for changing exception reporting modes."""
357 self.shell.showtraceback()
366 self.shell.showtraceback()
358
367
359 @skip_doctest
368 @skip_doctest
360 @line_magic
369 @line_magic
361 def run(self, parameter_s='', runner=None,
370 def run(self, parameter_s='', runner=None,
362 file_finder=get_py_filename):
371 file_finder=get_py_filename):
363 """Run the named file inside IPython as a program.
372 """Run the named file inside IPython as a program.
364
373
365 Usage:
374 Usage::
375
366 %run [-n -i -e -G]
376 %run [-n -i -e -G]
367 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
377 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
368 ( -m mod | file ) [args]
378 ( -m mod | file ) [args]
369
379
370 Parameters after the filename are passed as command-line arguments to
380 Parameters after the filename are passed as command-line arguments to
371 the program (put in sys.argv). Then, control returns to IPython's
381 the program (put in sys.argv). Then, control returns to IPython's
372 prompt.
382 prompt.
373
383
374 This is similar to running at a system prompt:\\
384 This is similar to running at a system prompt ``python file args``,
375 $ python file args\\
376 but with the advantage of giving you IPython's tracebacks, and of
385 but with the advantage of giving you IPython's tracebacks, and of
377 loading all variables into your interactive namespace for further use
386 loading all variables into your interactive namespace for further use
378 (unless -p is used, see below).
387 (unless -p is used, see below).
379
388
380 The file is executed in a namespace initially consisting only of
389 The file is executed in a namespace initially consisting only of
381 __name__=='__main__' and sys.argv constructed as indicated. It thus
390 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
382 sees its environment as if it were being run as a stand-alone program
391 sees its environment as if it were being run as a stand-alone program
383 (except for sharing global objects such as previously imported
392 (except for sharing global objects such as previously imported
384 modules). But after execution, the IPython interactive namespace gets
393 modules). But after execution, the IPython interactive namespace gets
385 updated with all variables defined in the program (except for __name__
394 updated with all variables defined in the program (except for __name__
386 and sys.argv). This allows for very convenient loading of code for
395 and sys.argv). This allows for very convenient loading of code for
387 interactive work, while giving each program a 'clean sheet' to run in.
396 interactive work, while giving each program a 'clean sheet' to run in.
388
397
389 Arguments are expanded using shell-like glob match. Patterns
398 Arguments are expanded using shell-like glob match. Patterns
390 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
399 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
391 tilde '~' will be expanded into user's home directory. Unlike
400 tilde '~' will be expanded into user's home directory. Unlike
392 real shells, quotation does not suppress expansions. Use
401 real shells, quotation does not suppress expansions. Use
393 *two* back slashes (e.g., '\\\\*') to suppress expansions.
402 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
394 To completely disable these expansions, you can use -G flag.
403 To completely disable these expansions, you can use -G flag.
395
404
396 Options:
405 Options:
397
406
398 -n: __name__ is NOT set to '__main__', but to the running file's name
407 -n
408 __name__ is NOT set to '__main__', but to the running file's name
399 without extension (as python does under import). This allows running
409 without extension (as python does under import). This allows running
400 scripts and reloading the definitions in them without calling code
410 scripts and reloading the definitions in them without calling code
401 protected by an ' if __name__ == "__main__" ' clause.
411 protected by an ``if __name__ == "__main__"`` clause.
402
412
403 -i: run the file in IPython's namespace instead of an empty one. This
413 -i
414 run the file in IPython's namespace instead of an empty one. This
404 is useful if you are experimenting with code written in a text editor
415 is useful if you are experimenting with code written in a text editor
405 which depends on variables defined interactively.
416 which depends on variables defined interactively.
406
417
407 -e: ignore sys.exit() calls or SystemExit exceptions in the script
418 -e
419 ignore sys.exit() calls or SystemExit exceptions in the script
408 being run. This is particularly useful if IPython is being used to
420 being run. This is particularly useful if IPython is being used to
409 run unittests, which always exit with a sys.exit() call. In such
421 run unittests, which always exit with a sys.exit() call. In such
410 cases you are interested in the output of the test results, not in
422 cases you are interested in the output of the test results, not in
411 seeing a traceback of the unittest module.
423 seeing a traceback of the unittest module.
412
424
413 -t: print timing information at the end of the run. IPython will give
425 -t
426 print timing information at the end of the run. IPython will give
414 you an estimated CPU time consumption for your script, which under
427 you an estimated CPU time consumption for your script, which under
415 Unix uses the resource module to avoid the wraparound problems of
428 Unix uses the resource module to avoid the wraparound problems of
416 time.clock(). Under Unix, an estimate of time spent on system tasks
429 time.clock(). Under Unix, an estimate of time spent on system tasks
417 is also given (for Windows platforms this is reported as 0.0).
430 is also given (for Windows platforms this is reported as 0.0).
418
431
419 If -t is given, an additional -N<N> option can be given, where <N>
432 If -t is given, an additional ``-N<N>`` option can be given, where <N>
420 must be an integer indicating how many times you want the script to
433 must be an integer indicating how many times you want the script to
421 run. The final timing report will include total and per run results.
434 run. The final timing report will include total and per run results.
422
435
423 For example (testing the script uniq_stable.py)::
436 For example (testing the script uniq_stable.py)::
424
437
425 In [1]: run -t uniq_stable
438 In [1]: run -t uniq_stable
426
439
427 IPython CPU timings (estimated):\\
440 IPython CPU timings (estimated):
428 User : 0.19597 s.\\
441 User : 0.19597 s.
429 System: 0.0 s.\\
442 System: 0.0 s.
430
443
431 In [2]: run -t -N5 uniq_stable
444 In [2]: run -t -N5 uniq_stable
432
445
433 IPython CPU timings (estimated):\\
446 IPython CPU timings (estimated):
434 Total runs performed: 5\\
447 Total runs performed: 5
435 Times : Total Per run\\
448 Times : Total Per run
436 User : 0.910862 s, 0.1821724 s.\\
449 User : 0.910862 s, 0.1821724 s.
437 System: 0.0 s, 0.0 s.
450 System: 0.0 s, 0.0 s.
438
451
439 -d: run your program under the control of pdb, the Python debugger.
452 -d
453 run your program under the control of pdb, the Python debugger.
440 This allows you to execute your program step by step, watch variables,
454 This allows you to execute your program step by step, watch variables,
441 etc. Internally, what IPython does is similar to calling:
455 etc. Internally, what IPython does is similar to calling::
442
456
443 pdb.run('execfile("YOURFILENAME")')
457 pdb.run('execfile("YOURFILENAME")')
444
458
445 with a breakpoint set on line 1 of your file. You can change the line
459 with a breakpoint set on line 1 of your file. You can change the line
446 number for this automatic breakpoint to be <N> by using the -bN option
460 number for this automatic breakpoint to be <N> by using the -bN option
447 (where N must be an integer). For example::
461 (where N must be an integer). For example::
448
462
449 %run -d -b40 myscript
463 %run -d -b40 myscript
450
464
451 will set the first breakpoint at line 40 in myscript.py. Note that
465 will set the first breakpoint at line 40 in myscript.py. Note that
452 the first breakpoint must be set on a line which actually does
466 the first breakpoint must be set on a line which actually does
453 something (not a comment or docstring) for it to stop execution.
467 something (not a comment or docstring) for it to stop execution.
454
468
455 Or you can specify a breakpoint in a different file::
469 Or you can specify a breakpoint in a different file::
456
470
457 %run -d -b myotherfile.py:20 myscript
471 %run -d -b myotherfile.py:20 myscript
458
472
459 When the pdb debugger starts, you will see a (Pdb) prompt. You must
473 When the pdb debugger starts, you will see a (Pdb) prompt. You must
460 first enter 'c' (without quotes) to start execution up to the first
474 first enter 'c' (without quotes) to start execution up to the first
461 breakpoint.
475 breakpoint.
462
476
463 Entering 'help' gives information about the use of the debugger. You
477 Entering 'help' gives information about the use of the debugger. You
464 can easily see pdb's full documentation with "import pdb;pdb.help()"
478 can easily see pdb's full documentation with "import pdb;pdb.help()"
465 at a prompt.
479 at a prompt.
466
480
467 -p: run program under the control of the Python profiler module (which
481 -p
482 run program under the control of the Python profiler module (which
468 prints a detailed report of execution times, function calls, etc).
483 prints a detailed report of execution times, function calls, etc).
469
484
470 You can pass other options after -p which affect the behavior of the
485 You can pass other options after -p which affect the behavior of the
471 profiler itself. See the docs for %prun for details.
486 profiler itself. See the docs for %prun for details.
472
487
473 In this mode, the program's variables do NOT propagate back to the
488 In this mode, the program's variables do NOT propagate back to the
474 IPython interactive namespace (because they remain in the namespace
489 IPython interactive namespace (because they remain in the namespace
475 where the profiler executes them).
490 where the profiler executes them).
476
491
477 Internally this triggers a call to %prun, see its documentation for
492 Internally this triggers a call to %prun, see its documentation for
478 details on the options available specifically for profiling.
493 details on the options available specifically for profiling.
479
494
480 There is one special usage for which the text above doesn't apply:
495 There is one special usage for which the text above doesn't apply:
481 if the filename ends with .ipy, the file is run as ipython script,
496 if the filename ends with .ipy, the file is run as ipython script,
482 just as if the commands were written on IPython prompt.
497 just as if the commands were written on IPython prompt.
483
498
484 -m: specify module name to load instead of script path. Similar to
499 -m
500 specify module name to load instead of script path. Similar to
485 the -m option for the python interpreter. Use this option last if you
501 the -m option for the python interpreter. Use this option last if you
486 want to combine with other %run options. Unlike the python interpreter
502 want to combine with other %run options. Unlike the python interpreter
487 only source modules are allowed no .pyc or .pyo files.
503 only source modules are allowed no .pyc or .pyo files.
488 For example::
504 For example::
489
505
490 %run -m example
506 %run -m example
491
507
492 will run the example module.
508 will run the example module.
493
509
494 -G: disable shell-like glob expansion of arguments.
510 -G
511 disable shell-like glob expansion of arguments.
495
512
496 """
513 """
497
514
498 # get arguments and set sys.argv for program to be run.
515 # get arguments and set sys.argv for program to be run.
499 opts, arg_lst = self.parse_options(parameter_s,
516 opts, arg_lst = self.parse_options(parameter_s,
500 'nidtN:b:pD:l:rs:T:em:G',
517 'nidtN:b:pD:l:rs:T:em:G',
501 mode='list', list_all=1)
518 mode='list', list_all=1)
502 if "m" in opts:
519 if "m" in opts:
503 modulename = opts["m"][0]
520 modulename = opts["m"][0]
504 modpath = find_mod(modulename)
521 modpath = find_mod(modulename)
505 if modpath is None:
522 if modpath is None:
506 warn('%r is not a valid modulename on sys.path'%modulename)
523 warn('%r is not a valid modulename on sys.path'%modulename)
507 return
524 return
508 arg_lst = [modpath] + arg_lst
525 arg_lst = [modpath] + arg_lst
509 try:
526 try:
510 filename = file_finder(arg_lst[0])
527 filename = file_finder(arg_lst[0])
511 except IndexError:
528 except IndexError:
512 warn('you must provide at least a filename.')
529 warn('you must provide at least a filename.')
513 print '\n%run:\n', oinspect.getdoc(self.run)
530 print '\n%run:\n', oinspect.getdoc(self.run)
514 return
531 return
515 except IOError as e:
532 except IOError as e:
516 try:
533 try:
517 msg = str(e)
534 msg = str(e)
518 except UnicodeError:
535 except UnicodeError:
519 msg = e.message
536 msg = e.message
520 error(msg)
537 error(msg)
521 return
538 return
522
539
523 if filename.lower().endswith('.ipy'):
540 if filename.lower().endswith('.ipy'):
524 with preserve_keys(self.shell.user_ns, '__file__'):
541 with preserve_keys(self.shell.user_ns, '__file__'):
525 self.shell.user_ns['__file__'] = filename
542 self.shell.user_ns['__file__'] = filename
526 self.shell.safe_execfile_ipy(filename)
543 self.shell.safe_execfile_ipy(filename)
527 return
544 return
528
545
529 # Control the response to exit() calls made by the script being run
546 # Control the response to exit() calls made by the script being run
530 exit_ignore = 'e' in opts
547 exit_ignore = 'e' in opts
531
548
532 # Make sure that the running script gets a proper sys.argv as if it
549 # Make sure that the running script gets a proper sys.argv as if it
533 # were run from a system shell.
550 # were run from a system shell.
534 save_argv = sys.argv # save it for later restoring
551 save_argv = sys.argv # save it for later restoring
535
552
536 if 'G' in opts:
553 if 'G' in opts:
537 args = arg_lst[1:]
554 args = arg_lst[1:]
538 else:
555 else:
539 # tilde and glob expansion
556 # tilde and glob expansion
540 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
557 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
541
558
542 sys.argv = [filename] + args # put in the proper filename
559 sys.argv = [filename] + args # put in the proper filename
543 # protect sys.argv from potential unicode strings on Python 2:
560 # protect sys.argv from potential unicode strings on Python 2:
544 if not py3compat.PY3:
561 if not py3compat.PY3:
545 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
562 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
546
563
547 if 'i' in opts:
564 if 'i' in opts:
548 # Run in user's interactive namespace
565 # Run in user's interactive namespace
549 prog_ns = self.shell.user_ns
566 prog_ns = self.shell.user_ns
550 __name__save = self.shell.user_ns['__name__']
567 __name__save = self.shell.user_ns['__name__']
551 prog_ns['__name__'] = '__main__'
568 prog_ns['__name__'] = '__main__'
552 main_mod = self.shell.user_module
569 main_mod = self.shell.user_module
553 else:
570 else:
554 # Run in a fresh, empty namespace
571 # Run in a fresh, empty namespace
555 if 'n' in opts:
572 if 'n' in opts:
556 name = os.path.splitext(os.path.basename(filename))[0]
573 name = os.path.splitext(os.path.basename(filename))[0]
557 else:
574 else:
558 name = '__main__'
575 name = '__main__'
559
576
560 # The shell MUST hold a reference to prog_ns so after %run
577 # The shell MUST hold a reference to prog_ns so after %run
561 # exits, the python deletion mechanism doesn't zero it out
578 # exits, the python deletion mechanism doesn't zero it out
562 # (leaving dangling references). See interactiveshell for details
579 # (leaving dangling references). See interactiveshell for details
563 main_mod = self.shell.new_main_mod(filename)
580 main_mod = self.shell.new_main_mod(filename)
564 prog_ns = main_mod.__dict__
581 prog_ns = main_mod.__dict__
565 prog_ns['__name__'] = name
582 prog_ns['__name__'] = name
566
583
567 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
584 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
568 # set the __file__ global in the script's namespace
585 # set the __file__ global in the script's namespace
569 prog_ns['__file__'] = filename
586 prog_ns['__file__'] = filename
570
587
571 # pickle fix. See interactiveshell for an explanation. But we need to
588 # pickle fix. See interactiveshell for an explanation. But we need to
572 # make sure that, if we overwrite __main__, we replace it at the end
589 # make sure that, if we overwrite __main__, we replace it at the end
573 main_mod_name = prog_ns['__name__']
590 main_mod_name = prog_ns['__name__']
574
591
575 if main_mod_name == '__main__':
592 if main_mod_name == '__main__':
576 restore_main = sys.modules['__main__']
593 restore_main = sys.modules['__main__']
577 else:
594 else:
578 restore_main = False
595 restore_main = False
579
596
580 # This needs to be undone at the end to prevent holding references to
597 # This needs to be undone at the end to prevent holding references to
581 # every single object ever created.
598 # every single object ever created.
582 sys.modules[main_mod_name] = main_mod
599 sys.modules[main_mod_name] = main_mod
583
600
584 if 'p' in opts or 'd' in opts:
601 if 'p' in opts or 'd' in opts:
585 if 'm' in opts:
602 if 'm' in opts:
586 code = 'run_module(modulename, prog_ns)'
603 code = 'run_module(modulename, prog_ns)'
587 code_ns = {
604 code_ns = {
588 'run_module': self.shell.safe_run_module,
605 'run_module': self.shell.safe_run_module,
589 'prog_ns': prog_ns,
606 'prog_ns': prog_ns,
590 'modulename': modulename,
607 'modulename': modulename,
591 }
608 }
592 else:
609 else:
593 code = 'execfile(filename, prog_ns)'
610 code = 'execfile(filename, prog_ns)'
594 code_ns = {
611 code_ns = {
595 'execfile': self.shell.safe_execfile,
612 'execfile': self.shell.safe_execfile,
596 'prog_ns': prog_ns,
613 'prog_ns': prog_ns,
597 'filename': get_py_filename(filename),
614 'filename': get_py_filename(filename),
598 }
615 }
599
616
600 try:
617 try:
601 stats = None
618 stats = None
602 with self.shell.readline_no_record:
619 with self.shell.readline_no_record:
603 if 'p' in opts:
620 if 'p' in opts:
604 stats = self._run_with_profiler(code, opts, code_ns)
621 stats = self._run_with_profiler(code, opts, code_ns)
605 else:
622 else:
606 if 'd' in opts:
623 if 'd' in opts:
607 bp_file, bp_line = parse_breakpoint(
624 bp_file, bp_line = parse_breakpoint(
608 opts.get('b', ['1'])[0], filename)
625 opts.get('b', ['1'])[0], filename)
609 self._run_with_debugger(
626 self._run_with_debugger(
610 code, code_ns, filename, bp_line, bp_file)
627 code, code_ns, filename, bp_line, bp_file)
611 else:
628 else:
612 if 'm' in opts:
629 if 'm' in opts:
613 def run():
630 def run():
614 self.shell.safe_run_module(modulename, prog_ns)
631 self.shell.safe_run_module(modulename, prog_ns)
615 else:
632 else:
616 if runner is None:
633 if runner is None:
617 runner = self.default_runner
634 runner = self.default_runner
618 if runner is None:
635 if runner is None:
619 runner = self.shell.safe_execfile
636 runner = self.shell.safe_execfile
620
637
621 def run():
638 def run():
622 runner(filename, prog_ns, prog_ns,
639 runner(filename, prog_ns, prog_ns,
623 exit_ignore=exit_ignore)
640 exit_ignore=exit_ignore)
624
641
625 if 't' in opts:
642 if 't' in opts:
626 # timed execution
643 # timed execution
627 try:
644 try:
628 nruns = int(opts['N'][0])
645 nruns = int(opts['N'][0])
629 if nruns < 1:
646 if nruns < 1:
630 error('Number of runs must be >=1')
647 error('Number of runs must be >=1')
631 return
648 return
632 except (KeyError):
649 except (KeyError):
633 nruns = 1
650 nruns = 1
634 self._run_with_timing(run, nruns)
651 self._run_with_timing(run, nruns)
635 else:
652 else:
636 # regular execution
653 # regular execution
637 run()
654 run()
638
655
639 if 'i' in opts:
656 if 'i' in opts:
640 self.shell.user_ns['__name__'] = __name__save
657 self.shell.user_ns['__name__'] = __name__save
641 else:
658 else:
642 # update IPython interactive namespace
659 # update IPython interactive namespace
643
660
644 # Some forms of read errors on the file may mean the
661 # Some forms of read errors on the file may mean the
645 # __name__ key was never set; using pop we don't have to
662 # __name__ key was never set; using pop we don't have to
646 # worry about a possible KeyError.
663 # worry about a possible KeyError.
647 prog_ns.pop('__name__', None)
664 prog_ns.pop('__name__', None)
648
665
649 with preserve_keys(self.shell.user_ns, '__file__'):
666 with preserve_keys(self.shell.user_ns, '__file__'):
650 self.shell.user_ns.update(prog_ns)
667 self.shell.user_ns.update(prog_ns)
651 finally:
668 finally:
652 # It's a bit of a mystery why, but __builtins__ can change from
669 # It's a bit of a mystery why, but __builtins__ can change from
653 # being a module to becoming a dict missing some key data after
670 # being a module to becoming a dict missing some key data after
654 # %run. As best I can see, this is NOT something IPython is doing
671 # %run. As best I can see, this is NOT something IPython is doing
655 # at all, and similar problems have been reported before:
672 # at all, and similar problems have been reported before:
656 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
673 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
657 # Since this seems to be done by the interpreter itself, the best
674 # Since this seems to be done by the interpreter itself, the best
658 # we can do is to at least restore __builtins__ for the user on
675 # we can do is to at least restore __builtins__ for the user on
659 # exit.
676 # exit.
660 self.shell.user_ns['__builtins__'] = builtin_mod
677 self.shell.user_ns['__builtins__'] = builtin_mod
661
678
662 # Ensure key global structures are restored
679 # Ensure key global structures are restored
663 sys.argv = save_argv
680 sys.argv = save_argv
664 if restore_main:
681 if restore_main:
665 sys.modules['__main__'] = restore_main
682 sys.modules['__main__'] = restore_main
666 else:
683 else:
667 # Remove from sys.modules the reference to main_mod we'd
684 # Remove from sys.modules the reference to main_mod we'd
668 # added. Otherwise it will trap references to objects
685 # added. Otherwise it will trap references to objects
669 # contained therein.
686 # contained therein.
670 del sys.modules[main_mod_name]
687 del sys.modules[main_mod_name]
671
688
672 return stats
689 return stats
673
690
674 def _run_with_debugger(self, code, code_ns, filename=None,
691 def _run_with_debugger(self, code, code_ns, filename=None,
675 bp_line=None, bp_file=None):
692 bp_line=None, bp_file=None):
676 """
693 """
677 Run `code` in debugger with a break point.
694 Run `code` in debugger with a break point.
678
695
679 Parameters
696 Parameters
680 ----------
697 ----------
681 code : str
698 code : str
682 Code to execute.
699 Code to execute.
683 code_ns : dict
700 code_ns : dict
684 A namespace in which `code` is executed.
701 A namespace in which `code` is executed.
685 filename : str
702 filename : str
686 `code` is ran as if it is in `filename`.
703 `code` is ran as if it is in `filename`.
687 bp_line : int, optional
704 bp_line : int, optional
688 Line number of the break point.
705 Line number of the break point.
689 bp_file : str, optional
706 bp_file : str, optional
690 Path to the file in which break point is specified.
707 Path to the file in which break point is specified.
691 `filename` is used if not given.
708 `filename` is used if not given.
692
709
693 Raises
710 Raises
694 ------
711 ------
695 UsageError
712 UsageError
696 If the break point given by `bp_line` is not valid.
713 If the break point given by `bp_line` is not valid.
697
714
698 """
715 """
699 deb = debugger.Pdb(self.shell.colors)
716 deb = debugger.Pdb(self.shell.colors)
700 # reset Breakpoint state, which is moronically kept
717 # reset Breakpoint state, which is moronically kept
701 # in a class
718 # in a class
702 bdb.Breakpoint.next = 1
719 bdb.Breakpoint.next = 1
703 bdb.Breakpoint.bplist = {}
720 bdb.Breakpoint.bplist = {}
704 bdb.Breakpoint.bpbynumber = [None]
721 bdb.Breakpoint.bpbynumber = [None]
705 if bp_line is not None:
722 if bp_line is not None:
706 # Set an initial breakpoint to stop execution
723 # Set an initial breakpoint to stop execution
707 maxtries = 10
724 maxtries = 10
708 bp_file = bp_file or filename
725 bp_file = bp_file or filename
709 checkline = deb.checkline(bp_file, bp_line)
726 checkline = deb.checkline(bp_file, bp_line)
710 if not checkline:
727 if not checkline:
711 for bp in range(bp_line + 1, bp_line + maxtries + 1):
728 for bp in range(bp_line + 1, bp_line + maxtries + 1):
712 if deb.checkline(bp_file, bp):
729 if deb.checkline(bp_file, bp):
713 break
730 break
714 else:
731 else:
715 msg = ("\nI failed to find a valid line to set "
732 msg = ("\nI failed to find a valid line to set "
716 "a breakpoint\n"
733 "a breakpoint\n"
717 "after trying up to line: %s.\n"
734 "after trying up to line: %s.\n"
718 "Please set a valid breakpoint manually "
735 "Please set a valid breakpoint manually "
719 "with the -b option." % bp)
736 "with the -b option." % bp)
720 raise UsageError(msg)
737 raise UsageError(msg)
721 # if we find a good linenumber, set the breakpoint
738 # if we find a good linenumber, set the breakpoint
722 deb.do_break('%s:%s' % (bp_file, bp_line))
739 deb.do_break('%s:%s' % (bp_file, bp_line))
723
740
724 if filename:
741 if filename:
725 # Mimic Pdb._runscript(...)
742 # Mimic Pdb._runscript(...)
726 deb._wait_for_mainpyfile = True
743 deb._wait_for_mainpyfile = True
727 deb.mainpyfile = deb.canonic(filename)
744 deb.mainpyfile = deb.canonic(filename)
728
745
729 # Start file run
746 # Start file run
730 print "NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt
747 print "NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt
731 try:
748 try:
732 if filename:
749 if filename:
733 # save filename so it can be used by methods on the deb object
750 # save filename so it can be used by methods on the deb object
734 deb._exec_filename = filename
751 deb._exec_filename = filename
735 deb.run(code, code_ns)
752 deb.run(code, code_ns)
736
753
737 except:
754 except:
738 etype, value, tb = sys.exc_info()
755 etype, value, tb = sys.exc_info()
739 # Skip three frames in the traceback: the %run one,
756 # Skip three frames in the traceback: the %run one,
740 # one inside bdb.py, and the command-line typed by the
757 # one inside bdb.py, and the command-line typed by the
741 # user (run by exec in pdb itself).
758 # user (run by exec in pdb itself).
742 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
759 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
743
760
744 @staticmethod
761 @staticmethod
745 def _run_with_timing(run, nruns):
762 def _run_with_timing(run, nruns):
746 """
763 """
747 Run function `run` and print timing information.
764 Run function `run` and print timing information.
748
765
749 Parameters
766 Parameters
750 ----------
767 ----------
751 run : callable
768 run : callable
752 Any callable object which takes no argument.
769 Any callable object which takes no argument.
753 nruns : int
770 nruns : int
754 Number of times to execute `run`.
771 Number of times to execute `run`.
755
772
756 """
773 """
757 twall0 = time.time()
774 twall0 = time.time()
758 if nruns == 1:
775 if nruns == 1:
759 t0 = clock2()
776 t0 = clock2()
760 run()
777 run()
761 t1 = clock2()
778 t1 = clock2()
762 t_usr = t1[0] - t0[0]
779 t_usr = t1[0] - t0[0]
763 t_sys = t1[1] - t0[1]
780 t_sys = t1[1] - t0[1]
764 print "\nIPython CPU timings (estimated):"
781 print "\nIPython CPU timings (estimated):"
765 print " User : %10.2f s." % t_usr
782 print " User : %10.2f s." % t_usr
766 print " System : %10.2f s." % t_sys
783 print " System : %10.2f s." % t_sys
767 else:
784 else:
768 runs = range(nruns)
785 runs = range(nruns)
769 t0 = clock2()
786 t0 = clock2()
770 for nr in runs:
787 for nr in runs:
771 run()
788 run()
772 t1 = clock2()
789 t1 = clock2()
773 t_usr = t1[0] - t0[0]
790 t_usr = t1[0] - t0[0]
774 t_sys = t1[1] - t0[1]
791 t_sys = t1[1] - t0[1]
775 print "\nIPython CPU timings (estimated):"
792 print "\nIPython CPU timings (estimated):"
776 print "Total runs performed:", nruns
793 print "Total runs performed:", nruns
777 print " Times : %10s %10s" % ('Total', 'Per run')
794 print " Times : %10s %10s" % ('Total', 'Per run')
778 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
795 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
779 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
796 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
780 twall1 = time.time()
797 twall1 = time.time()
781 print "Wall time: %10.2f s." % (twall1 - twall0)
798 print "Wall time: %10.2f s." % (twall1 - twall0)
782
799
783 @skip_doctest
800 @skip_doctest
784 @line_cell_magic
801 @line_cell_magic
785 def timeit(self, line='', cell=None):
802 def timeit(self, line='', cell=None):
786 """Time execution of a Python statement or expression
803 """Time execution of a Python statement or expression
787
804
788 Usage, in line mode:
805 Usage, in line mode:
789 %timeit [-n<N> -r<R> [-t|-c]] statement
806 %timeit [-n<N> -r<R> [-t|-c]] statement
790 or in cell mode:
807 or in cell mode:
791 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
808 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
792 code
809 code
793 code...
810 code...
794
811
795 Time execution of a Python statement or expression using the timeit
812 Time execution of a Python statement or expression using the timeit
796 module. This function can be used both as a line and cell magic:
813 module. This function can be used both as a line and cell magic:
797
814
798 - In line mode you can time a single-line statement (though multiple
815 - In line mode you can time a single-line statement (though multiple
799 ones can be chained with using semicolons).
816 ones can be chained with using semicolons).
800
817
801 - In cell mode, the statement in the first line is used as setup code
818 - In cell mode, the statement in the first line is used as setup code
802 (executed but not timed) and the body of the cell is timed. The cell
819 (executed but not timed) and the body of the cell is timed. The cell
803 body has access to any variables created in the setup code.
820 body has access to any variables created in the setup code.
804
821
805 Options:
822 Options:
806 -n<N>: execute the given statement <N> times in a loop. If this value
823 -n<N>: execute the given statement <N> times in a loop. If this value
807 is not given, a fitting value is chosen.
824 is not given, a fitting value is chosen.
808
825
809 -r<R>: repeat the loop iteration <R> times and take the best result.
826 -r<R>: repeat the loop iteration <R> times and take the best result.
810 Default: 3
827 Default: 3
811
828
812 -t: use time.time to measure the time, which is the default on Unix.
829 -t: use time.time to measure the time, which is the default on Unix.
813 This function measures wall time.
830 This function measures wall time.
814
831
815 -c: use time.clock to measure the time, which is the default on
832 -c: use time.clock to measure the time, which is the default on
816 Windows and measures wall time. On Unix, resource.getrusage is used
833 Windows and measures wall time. On Unix, resource.getrusage is used
817 instead and returns the CPU user time.
834 instead and returns the CPU user time.
818
835
819 -p<P>: use a precision of <P> digits to display the timing result.
836 -p<P>: use a precision of <P> digits to display the timing result.
820 Default: 3
837 Default: 3
821
838
822
839
823 Examples
840 Examples
824 --------
841 --------
825 ::
842 ::
826
843
827 In [1]: %timeit pass
844 In [1]: %timeit pass
828 10000000 loops, best of 3: 53.3 ns per loop
845 10000000 loops, best of 3: 53.3 ns per loop
829
846
830 In [2]: u = None
847 In [2]: u = None
831
848
832 In [3]: %timeit u is None
849 In [3]: %timeit u is None
833 10000000 loops, best of 3: 184 ns per loop
850 10000000 loops, best of 3: 184 ns per loop
834
851
835 In [4]: %timeit -r 4 u == None
852 In [4]: %timeit -r 4 u == None
836 1000000 loops, best of 4: 242 ns per loop
853 1000000 loops, best of 4: 242 ns per loop
837
854
838 In [5]: import time
855 In [5]: import time
839
856
840 In [6]: %timeit -n1 time.sleep(2)
857 In [6]: %timeit -n1 time.sleep(2)
841 1 loops, best of 3: 2 s per loop
858 1 loops, best of 3: 2 s per loop
842
859
843
860
844 The times reported by %timeit will be slightly higher than those
861 The times reported by %timeit will be slightly higher than those
845 reported by the timeit.py script when variables are accessed. This is
862 reported by the timeit.py script when variables are accessed. This is
846 due to the fact that %timeit executes the statement in the namespace
863 due to the fact that %timeit executes the statement in the namespace
847 of the shell, compared with timeit.py, which uses a single setup
864 of the shell, compared with timeit.py, which uses a single setup
848 statement to import function or create variables. Generally, the bias
865 statement to import function or create variables. Generally, the bias
849 does not matter as long as results from timeit.py are not mixed with
866 does not matter as long as results from timeit.py are not mixed with
850 those from %timeit."""
867 those from %timeit."""
851
868
852 import timeit
869 import timeit
853
870
854 opts, stmt = self.parse_options(line,'n:r:tcp:',
871 opts, stmt = self.parse_options(line,'n:r:tcp:',
855 posix=False, strict=False)
872 posix=False, strict=False)
856 if stmt == "" and cell is None:
873 if stmt == "" and cell is None:
857 return
874 return
858
875
859 timefunc = timeit.default_timer
876 timefunc = timeit.default_timer
860 number = int(getattr(opts, "n", 0))
877 number = int(getattr(opts, "n", 0))
861 repeat = int(getattr(opts, "r", timeit.default_repeat))
878 repeat = int(getattr(opts, "r", timeit.default_repeat))
862 precision = int(getattr(opts, "p", 3))
879 precision = int(getattr(opts, "p", 3))
863 if hasattr(opts, "t"):
880 if hasattr(opts, "t"):
864 timefunc = time.time
881 timefunc = time.time
865 if hasattr(opts, "c"):
882 if hasattr(opts, "c"):
866 timefunc = clock
883 timefunc = clock
867
884
868 timer = timeit.Timer(timer=timefunc)
885 timer = timeit.Timer(timer=timefunc)
869 # this code has tight coupling to the inner workings of timeit.Timer,
886 # this code has tight coupling to the inner workings of timeit.Timer,
870 # but is there a better way to achieve that the code stmt has access
887 # but is there a better way to achieve that the code stmt has access
871 # to the shell namespace?
888 # to the shell namespace?
872 transform = self.shell.input_splitter.transform_cell
889 transform = self.shell.input_splitter.transform_cell
873
890
874 if cell is None:
891 if cell is None:
875 # called as line magic
892 # called as line magic
876 ast_setup = ast.parse("pass")
893 ast_setup = ast.parse("pass")
877 ast_stmt = ast.parse(transform(stmt))
894 ast_stmt = ast.parse(transform(stmt))
878 else:
895 else:
879 ast_setup = ast.parse(transform(stmt))
896 ast_setup = ast.parse(transform(stmt))
880 ast_stmt = ast.parse(transform(cell))
897 ast_stmt = ast.parse(transform(cell))
881
898
882 ast_setup = self.shell.transform_ast(ast_setup)
899 ast_setup = self.shell.transform_ast(ast_setup)
883 ast_stmt = self.shell.transform_ast(ast_stmt)
900 ast_stmt = self.shell.transform_ast(ast_stmt)
884
901
885 # This codestring is taken from timeit.template - we fill it in as an
902 # This codestring is taken from timeit.template - we fill it in as an
886 # AST, so that we can apply our AST transformations to the user code
903 # AST, so that we can apply our AST transformations to the user code
887 # without affecting the timing code.
904 # without affecting the timing code.
888 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
905 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
889 ' setup\n'
906 ' setup\n'
890 ' _t0 = _timer()\n'
907 ' _t0 = _timer()\n'
891 ' for _i in _it:\n'
908 ' for _i in _it:\n'
892 ' stmt\n'
909 ' stmt\n'
893 ' _t1 = _timer()\n'
910 ' _t1 = _timer()\n'
894 ' return _t1 - _t0\n')
911 ' return _t1 - _t0\n')
895
912
896 class TimeitTemplateFiller(ast.NodeTransformer):
913 class TimeitTemplateFiller(ast.NodeTransformer):
897 "This is quite tightly tied to the template definition above."
914 "This is quite tightly tied to the template definition above."
898 def visit_FunctionDef(self, node):
915 def visit_FunctionDef(self, node):
899 "Fill in the setup statement"
916 "Fill in the setup statement"
900 self.generic_visit(node)
917 self.generic_visit(node)
901 if node.name == "inner":
918 if node.name == "inner":
902 node.body[:1] = ast_setup.body
919 node.body[:1] = ast_setup.body
903
920
904 return node
921 return node
905
922
906 def visit_For(self, node):
923 def visit_For(self, node):
907 "Fill in the statement to be timed"
924 "Fill in the statement to be timed"
908 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
925 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
909 node.body = ast_stmt.body
926 node.body = ast_stmt.body
910 return node
927 return node
911
928
912 timeit_ast = TimeitTemplateFiller().visit(timeit_ast_template)
929 timeit_ast = TimeitTemplateFiller().visit(timeit_ast_template)
913 timeit_ast = ast.fix_missing_locations(timeit_ast)
930 timeit_ast = ast.fix_missing_locations(timeit_ast)
914
931
915 # Track compilation time so it can be reported if too long
932 # Track compilation time so it can be reported if too long
916 # Minimum time above which compilation time will be reported
933 # Minimum time above which compilation time will be reported
917 tc_min = 0.1
934 tc_min = 0.1
918
935
919 t0 = clock()
936 t0 = clock()
920 code = compile(timeit_ast, "<magic-timeit>", "exec")
937 code = compile(timeit_ast, "<magic-timeit>", "exec")
921 tc = clock()-t0
938 tc = clock()-t0
922
939
923 ns = {}
940 ns = {}
924 exec code in self.shell.user_ns, ns
941 exec code in self.shell.user_ns, ns
925 timer.inner = ns["inner"]
942 timer.inner = ns["inner"]
926
943
927 if number == 0:
944 if number == 0:
928 # determine number so that 0.2 <= total time < 2.0
945 # determine number so that 0.2 <= total time < 2.0
929 number = 1
946 number = 1
930 for i in range(1, 10):
947 for i in range(1, 10):
931 if timer.timeit(number) >= 0.2:
948 if timer.timeit(number) >= 0.2:
932 break
949 break
933 number *= 10
950 number *= 10
934
951
935 best = min(timer.repeat(repeat, number)) / number
952 best = min(timer.repeat(repeat, number)) / number
936
953
937 print u"%d loops, best of %d: %s per loop" % (number, repeat,
954 print u"%d loops, best of %d: %s per loop" % (number, repeat,
938 _format_time(best, precision))
955 _format_time(best, precision))
939 if tc > tc_min:
956 if tc > tc_min:
940 print "Compiler time: %.2f s" % tc
957 print "Compiler time: %.2f s" % tc
941
958
942 @skip_doctest
959 @skip_doctest
943 @needs_local_scope
960 @needs_local_scope
944 @line_cell_magic
961 @line_cell_magic
945 def time(self,line='', cell=None, local_ns=None):
962 def time(self,line='', cell=None, local_ns=None):
946 """Time execution of a Python statement or expression.
963 """Time execution of a Python statement or expression.
947
964
948 The CPU and wall clock times are printed, and the value of the
965 The CPU and wall clock times are printed, and the value of the
949 expression (if any) is returned. Note that under Win32, system time
966 expression (if any) is returned. Note that under Win32, system time
950 is always reported as 0, since it can not be measured.
967 is always reported as 0, since it can not be measured.
951
968
952 This function can be used both as a line and cell magic:
969 This function can be used both as a line and cell magic:
953
970
954 - In line mode you can time a single-line statement (though multiple
971 - In line mode you can time a single-line statement (though multiple
955 ones can be chained with using semicolons).
972 ones can be chained with using semicolons).
956
973
957 - In cell mode, you can time the cell body (a directly
974 - In cell mode, you can time the cell body (a directly
958 following statement raises an error).
975 following statement raises an error).
959
976
960 This function provides very basic timing functionality. Use the timeit
977 This function provides very basic timing functionality. Use the timeit
961 magic for more controll over the measurement.
978 magic for more controll over the measurement.
962
979
963 Examples
980 Examples
964 --------
981 --------
965 ::
982 ::
966
983
967 In [1]: %time 2**128
984 In [1]: %time 2**128
968 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
985 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
969 Wall time: 0.00
986 Wall time: 0.00
970 Out[1]: 340282366920938463463374607431768211456L
987 Out[1]: 340282366920938463463374607431768211456L
971
988
972 In [2]: n = 1000000
989 In [2]: n = 1000000
973
990
974 In [3]: %time sum(range(n))
991 In [3]: %time sum(range(n))
975 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
992 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
976 Wall time: 1.37
993 Wall time: 1.37
977 Out[3]: 499999500000L
994 Out[3]: 499999500000L
978
995
979 In [4]: %time print 'hello world'
996 In [4]: %time print 'hello world'
980 hello world
997 hello world
981 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
998 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
982 Wall time: 0.00
999 Wall time: 0.00
983
1000
984 Note that the time needed by Python to compile the given expression
1001 Note that the time needed by Python to compile the given expression
985 will be reported if it is more than 0.1s. In this example, the
1002 will be reported if it is more than 0.1s. In this example, the
986 actual exponentiation is done by Python at compilation time, so while
1003 actual exponentiation is done by Python at compilation time, so while
987 the expression can take a noticeable amount of time to compute, that
1004 the expression can take a noticeable amount of time to compute, that
988 time is purely due to the compilation:
1005 time is purely due to the compilation:
989
1006
990 In [5]: %time 3**9999;
1007 In [5]: %time 3**9999;
991 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1008 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
992 Wall time: 0.00 s
1009 Wall time: 0.00 s
993
1010
994 In [6]: %time 3**999999;
1011 In [6]: %time 3**999999;
995 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1012 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
996 Wall time: 0.00 s
1013 Wall time: 0.00 s
997 Compiler : 0.78 s
1014 Compiler : 0.78 s
998 """
1015 """
999
1016
1000 # fail immediately if the given expression can't be compiled
1017 # fail immediately if the given expression can't be compiled
1001
1018
1002 if line and cell:
1019 if line and cell:
1003 raise UsageError("Can't use statement directly after '%%time'!")
1020 raise UsageError("Can't use statement directly after '%%time'!")
1004
1021
1005 if cell:
1022 if cell:
1006 expr = self.shell.input_transformer_manager.transform_cell(cell)
1023 expr = self.shell.input_transformer_manager.transform_cell(cell)
1007 else:
1024 else:
1008 expr = self.shell.input_transformer_manager.transform_cell(line)
1025 expr = self.shell.input_transformer_manager.transform_cell(line)
1009
1026
1010 # Minimum time above which parse time will be reported
1027 # Minimum time above which parse time will be reported
1011 tp_min = 0.1
1028 tp_min = 0.1
1012
1029
1013 t0 = clock()
1030 t0 = clock()
1014 expr_ast = ast.parse(expr)
1031 expr_ast = ast.parse(expr)
1015 tp = clock()-t0
1032 tp = clock()-t0
1016
1033
1017 # Apply AST transformations
1034 # Apply AST transformations
1018 expr_ast = self.shell.transform_ast(expr_ast)
1035 expr_ast = self.shell.transform_ast(expr_ast)
1019
1036
1020 # Minimum time above which compilation time will be reported
1037 # Minimum time above which compilation time will be reported
1021 tc_min = 0.1
1038 tc_min = 0.1
1022
1039
1023 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1040 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1024 mode = 'eval'
1041 mode = 'eval'
1025 source = '<timed eval>'
1042 source = '<timed eval>'
1026 expr_ast = ast.Expression(expr_ast.body[0].value)
1043 expr_ast = ast.Expression(expr_ast.body[0].value)
1027 else:
1044 else:
1028 mode = 'exec'
1045 mode = 'exec'
1029 source = '<timed exec>'
1046 source = '<timed exec>'
1030 t0 = clock()
1047 t0 = clock()
1031 code = compile(expr_ast, source, mode)
1048 code = compile(expr_ast, source, mode)
1032 tc = clock()-t0
1049 tc = clock()-t0
1033
1050
1034 # skew measurement as little as possible
1051 # skew measurement as little as possible
1035 glob = self.shell.user_ns
1052 glob = self.shell.user_ns
1036 wtime = time.time
1053 wtime = time.time
1037 # time execution
1054 # time execution
1038 wall_st = wtime()
1055 wall_st = wtime()
1039 if mode=='eval':
1056 if mode=='eval':
1040 st = clock2()
1057 st = clock2()
1041 out = eval(code, glob, local_ns)
1058 out = eval(code, glob, local_ns)
1042 end = clock2()
1059 end = clock2()
1043 else:
1060 else:
1044 st = clock2()
1061 st = clock2()
1045 exec code in glob, local_ns
1062 exec code in glob, local_ns
1046 end = clock2()
1063 end = clock2()
1047 out = None
1064 out = None
1048 wall_end = wtime()
1065 wall_end = wtime()
1049 # Compute actual times and report
1066 # Compute actual times and report
1050 wall_time = wall_end-wall_st
1067 wall_time = wall_end-wall_st
1051 cpu_user = end[0]-st[0]
1068 cpu_user = end[0]-st[0]
1052 cpu_sys = end[1]-st[1]
1069 cpu_sys = end[1]-st[1]
1053 cpu_tot = cpu_user+cpu_sys
1070 cpu_tot = cpu_user+cpu_sys
1054 # On windows cpu_sys is always zero, so no new information to the next print
1071 # On windows cpu_sys is always zero, so no new information to the next print
1055 if sys.platform != 'win32':
1072 if sys.platform != 'win32':
1056 print "CPU times: user %s, sys: %s, total: %s" % \
1073 print "CPU times: user %s, sys: %s, total: %s" % \
1057 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))
1074 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))
1058 print "Wall time: %s" % _format_time(wall_time)
1075 print "Wall time: %s" % _format_time(wall_time)
1059 if tc > tc_min:
1076 if tc > tc_min:
1060 print "Compiler : %s" % _format_time(tc)
1077 print "Compiler : %s" % _format_time(tc)
1061 if tp > tp_min:
1078 if tp > tp_min:
1062 print "Parser : %s" % _format_time(tp)
1079 print "Parser : %s" % _format_time(tp)
1063 return out
1080 return out
1064
1081
1065 @skip_doctest
1082 @skip_doctest
1066 @line_magic
1083 @line_magic
1067 def macro(self, parameter_s=''):
1084 def macro(self, parameter_s=''):
1068 """Define a macro for future re-execution. It accepts ranges of history,
1085 """Define a macro for future re-execution. It accepts ranges of history,
1069 filenames or string objects.
1086 filenames or string objects.
1070
1087
1071 Usage:\\
1088 Usage:\\
1072 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1089 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1073
1090
1074 Options:
1091 Options:
1075
1092
1076 -r: use 'raw' input. By default, the 'processed' history is used,
1093 -r: use 'raw' input. By default, the 'processed' history is used,
1077 so that magics are loaded in their transformed version to valid
1094 so that magics are loaded in their transformed version to valid
1078 Python. If this option is given, the raw input as typed at the
1095 Python. If this option is given, the raw input as typed at the
1079 command line is used instead.
1096 command line is used instead.
1080
1097
1081 -q: quiet macro definition. By default, a tag line is printed
1098 -q: quiet macro definition. By default, a tag line is printed
1082 to indicate the macro has been created, and then the contents of
1099 to indicate the macro has been created, and then the contents of
1083 the macro are printed. If this option is given, then no printout
1100 the macro are printed. If this option is given, then no printout
1084 is produced once the macro is created.
1101 is produced once the macro is created.
1085
1102
1086 This will define a global variable called `name` which is a string
1103 This will define a global variable called `name` which is a string
1087 made of joining the slices and lines you specify (n1,n2,... numbers
1104 made of joining the slices and lines you specify (n1,n2,... numbers
1088 above) from your input history into a single string. This variable
1105 above) from your input history into a single string. This variable
1089 acts like an automatic function which re-executes those lines as if
1106 acts like an automatic function which re-executes those lines as if
1090 you had typed them. You just type 'name' at the prompt and the code
1107 you had typed them. You just type 'name' at the prompt and the code
1091 executes.
1108 executes.
1092
1109
1093 The syntax for indicating input ranges is described in %history.
1110 The syntax for indicating input ranges is described in %history.
1094
1111
1095 Note: as a 'hidden' feature, you can also use traditional python slice
1112 Note: as a 'hidden' feature, you can also use traditional python slice
1096 notation, where N:M means numbers N through M-1.
1113 notation, where N:M means numbers N through M-1.
1097
1114
1098 For example, if your history contains (print using %hist -n )::
1115 For example, if your history contains (print using %hist -n )::
1099
1116
1100 44: x=1
1117 44: x=1
1101 45: y=3
1118 45: y=3
1102 46: z=x+y
1119 46: z=x+y
1103 47: print x
1120 47: print x
1104 48: a=5
1121 48: a=5
1105 49: print 'x',x,'y',y
1122 49: print 'x',x,'y',y
1106
1123
1107 you can create a macro with lines 44 through 47 (included) and line 49
1124 you can create a macro with lines 44 through 47 (included) and line 49
1108 called my_macro with::
1125 called my_macro with::
1109
1126
1110 In [55]: %macro my_macro 44-47 49
1127 In [55]: %macro my_macro 44-47 49
1111
1128
1112 Now, typing `my_macro` (without quotes) will re-execute all this code
1129 Now, typing `my_macro` (without quotes) will re-execute all this code
1113 in one pass.
1130 in one pass.
1114
1131
1115 You don't need to give the line-numbers in order, and any given line
1132 You don't need to give the line-numbers in order, and any given line
1116 number can appear multiple times. You can assemble macros with any
1133 number can appear multiple times. You can assemble macros with any
1117 lines from your input history in any order.
1134 lines from your input history in any order.
1118
1135
1119 The macro is a simple object which holds its value in an attribute,
1136 The macro is a simple object which holds its value in an attribute,
1120 but IPython's display system checks for macros and executes them as
1137 but IPython's display system checks for macros and executes them as
1121 code instead of printing them when you type their name.
1138 code instead of printing them when you type their name.
1122
1139
1123 You can view a macro's contents by explicitly printing it with::
1140 You can view a macro's contents by explicitly printing it with::
1124
1141
1125 print macro_name
1142 print macro_name
1126
1143
1127 """
1144 """
1128 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1145 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1129 if not args: # List existing macros
1146 if not args: # List existing macros
1130 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
1147 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
1131 isinstance(v, Macro))
1148 isinstance(v, Macro))
1132 if len(args) == 1:
1149 if len(args) == 1:
1133 raise UsageError(
1150 raise UsageError(
1134 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1151 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1135 name, codefrom = args[0], " ".join(args[1:])
1152 name, codefrom = args[0], " ".join(args[1:])
1136
1153
1137 #print 'rng',ranges # dbg
1154 #print 'rng',ranges # dbg
1138 try:
1155 try:
1139 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1156 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1140 except (ValueError, TypeError) as e:
1157 except (ValueError, TypeError) as e:
1141 print e.args[0]
1158 print e.args[0]
1142 return
1159 return
1143 macro = Macro(lines)
1160 macro = Macro(lines)
1144 self.shell.define_macro(name, macro)
1161 self.shell.define_macro(name, macro)
1145 if not ( 'q' in opts) :
1162 if not ( 'q' in opts) :
1146 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1163 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1147 print '=== Macro contents: ==='
1164 print '=== Macro contents: ==='
1148 print macro,
1165 print macro,
1149
1166
1150 @magic_arguments.magic_arguments()
1167 @magic_arguments.magic_arguments()
1151 @magic_arguments.argument('output', type=str, default='', nargs='?',
1168 @magic_arguments.argument('output', type=str, default='', nargs='?',
1152 help="""The name of the variable in which to store output.
1169 help="""The name of the variable in which to store output.
1153 This is a utils.io.CapturedIO object with stdout/err attributes
1170 This is a utils.io.CapturedIO object with stdout/err attributes
1154 for the text of the captured output.
1171 for the text of the captured output.
1155
1172
1156 CapturedOutput also has a show() method for displaying the output,
1173 CapturedOutput also has a show() method for displaying the output,
1157 and __call__ as well, so you can use that to quickly display the
1174 and __call__ as well, so you can use that to quickly display the
1158 output.
1175 output.
1159
1176
1160 If unspecified, captured output is discarded.
1177 If unspecified, captured output is discarded.
1161 """
1178 """
1162 )
1179 )
1163 @magic_arguments.argument('--no-stderr', action="store_true",
1180 @magic_arguments.argument('--no-stderr', action="store_true",
1164 help="""Don't capture stderr."""
1181 help="""Don't capture stderr."""
1165 )
1182 )
1166 @magic_arguments.argument('--no-stdout', action="store_true",
1183 @magic_arguments.argument('--no-stdout', action="store_true",
1167 help="""Don't capture stdout."""
1184 help="""Don't capture stdout."""
1168 )
1185 )
1169 @magic_arguments.argument('--no-display', action="store_true",
1186 @magic_arguments.argument('--no-display', action="store_true",
1170 help="""Don't capture IPython's rich display."""
1187 help="""Don't capture IPython's rich display."""
1171 )
1188 )
1172 @cell_magic
1189 @cell_magic
1173 def capture(self, line, cell):
1190 def capture(self, line, cell):
1174 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1191 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1175 args = magic_arguments.parse_argstring(self.capture, line)
1192 args = magic_arguments.parse_argstring(self.capture, line)
1176 out = not args.no_stdout
1193 out = not args.no_stdout
1177 err = not args.no_stderr
1194 err = not args.no_stderr
1178 disp = not args.no_display
1195 disp = not args.no_display
1179 with capture_output(out, err, disp) as io:
1196 with capture_output(out, err, disp) as io:
1180 self.shell.run_cell(cell)
1197 self.shell.run_cell(cell)
1181 if args.output:
1198 if args.output:
1182 self.shell.user_ns[args.output] = io
1199 self.shell.user_ns[args.output] = io
1183
1200
1184 def parse_breakpoint(text, current_file):
1201 def parse_breakpoint(text, current_file):
1185 '''Returns (file, line) for file:line and (current_file, line) for line'''
1202 '''Returns (file, line) for file:line and (current_file, line) for line'''
1186 colon = text.find(':')
1203 colon = text.find(':')
1187 if colon == -1:
1204 if colon == -1:
1188 return current_file, int(text)
1205 return current_file, int(text)
1189 else:
1206 else:
1190 return text[:colon], int(text[colon+1:])
1207 return text[:colon], int(text[colon+1:])
1191
1208
1192 def _format_time(timespan, precision=3):
1209 def _format_time(timespan, precision=3):
1193 """Formats the timespan in a human readable form"""
1210 """Formats the timespan in a human readable form"""
1194 import math
1211 import math
1195
1212
1196 if timespan >= 60.0:
1213 if timespan >= 60.0:
1197 # we have more than a minute, format that in a human readable form
1214 # we have more than a minute, format that in a human readable form
1198 # Idea from http://snipplr.com/view/5713/
1215 # Idea from http://snipplr.com/view/5713/
1199 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1216 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1200 time = []
1217 time = []
1201 leftover = timespan
1218 leftover = timespan
1202 for suffix, length in parts:
1219 for suffix, length in parts:
1203 value = int(leftover / length)
1220 value = int(leftover / length)
1204 if value > 0:
1221 if value > 0:
1205 leftover = leftover % length
1222 leftover = leftover % length
1206 time.append(u'%s%s' % (str(value), suffix))
1223 time.append(u'%s%s' % (str(value), suffix))
1207 if leftover < 1:
1224 if leftover < 1:
1208 break
1225 break
1209 return " ".join(time)
1226 return " ".join(time)
1210
1227
1211
1228
1212 # Unfortunately the unicode 'micro' symbol can cause problems in
1229 # Unfortunately the unicode 'micro' symbol can cause problems in
1213 # certain terminals.
1230 # certain terminals.
1214 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1231 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1215 # Try to prevent crashes by being more secure than it needs to
1232 # Try to prevent crashes by being more secure than it needs to
1216 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1233 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1217 units = [u"s", u"ms",u'us',"ns"] # the save value
1234 units = [u"s", u"ms",u'us',"ns"] # the save value
1218 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1235 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1219 try:
1236 try:
1220 u'\xb5'.encode(sys.stdout.encoding)
1237 u'\xb5'.encode(sys.stdout.encoding)
1221 units = [u"s", u"ms",u'\xb5s',"ns"]
1238 units = [u"s", u"ms",u'\xb5s',"ns"]
1222 except:
1239 except:
1223 pass
1240 pass
1224 scaling = [1, 1e3, 1e6, 1e9]
1241 scaling = [1, 1e3, 1e6, 1e9]
1225
1242
1226 if timespan > 0.0:
1243 if timespan > 0.0:
1227 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1244 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1228 else:
1245 else:
1229 order = 3
1246 order = 3
1230 ret = u"%.*g %s" % (precision, timespan * scaling[order], units[order])
1247 ret = u"%.*g %s" % (precision, timespan * scaling[order], units[order])
1231 return ret
1248 return ret
@@ -1,170 +1,184 b''
1 """Implementation of magic functions for IPython's own logging.
1 """Implementation of magic functions for IPython's own logging.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import os
16 import os
17 import sys
17 import sys
18
18
19 # Our own packages
19 # Our own packages
20 from IPython.core.magic import Magics, magics_class, line_magic
20 from IPython.core.magic import Magics, magics_class, line_magic
21 from IPython.utils.warn import warn
21 from IPython.utils.warn import warn
22 from IPython.utils.py3compat import str_to_unicode
22 from IPython.utils.py3compat import str_to_unicode
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Magic implementation classes
25 # Magic implementation classes
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 @magics_class
28 @magics_class
29 class LoggingMagics(Magics):
29 class LoggingMagics(Magics):
30 """Magics related to all logging machinery."""
30 """Magics related to all logging machinery."""
31
31
32 @line_magic
32 @line_magic
33 def logstart(self, parameter_s=''):
33 def logstart(self, parameter_s=''):
34 """Start logging anywhere in a session.
34 """Start logging anywhere in a session.
35
35
36 %logstart [-o|-r|-t] [log_name [log_mode]]
36 %logstart [-o|-r|-t] [log_name [log_mode]]
37
37
38 If no name is given, it defaults to a file named 'ipython_log.py' in your
38 If no name is given, it defaults to a file named 'ipython_log.py' in your
39 current directory, in 'rotate' mode (see below).
39 current directory, in 'rotate' mode (see below).
40
40
41 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
41 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
42 history up to that point and then continues logging.
42 history up to that point and then continues logging.
43
43
44 %logstart takes a second optional parameter: logging mode. This can be one
44 %logstart takes a second optional parameter: logging mode. This can be one
45 of (note that the modes are given unquoted):\\
45 of (note that the modes are given unquoted):
46 append: well, that says it.\\
46
47 backup: rename (if exists) to name~ and start name.\\
47 append
48 global: single logfile in your home dir, appended to.\\
48 Keep logging at the end of any existing file.
49 over : overwrite existing log.\\
49
50 rotate: create rotating logs name.1~, name.2~, etc.
50 backup
51 Rename any existing file to name~ and start name.
52
53 global
54 Append to a single logfile in your home directory.
55
56 over
57 Overwrite any existing log.
58
59 rotate
60 Create rotating logs: name.1~, name.2~, etc.
51
61
52 Options:
62 Options:
53
63
54 -o: log also IPython's output. In this mode, all commands which
64 -o
65 log also IPython's output. In this mode, all commands which
55 generate an Out[NN] prompt are recorded to the logfile, right after
66 generate an Out[NN] prompt are recorded to the logfile, right after
56 their corresponding input line. The output lines are always
67 their corresponding input line. The output lines are always
57 prepended with a '#[Out]# ' marker, so that the log remains valid
68 prepended with a '#[Out]# ' marker, so that the log remains valid
58 Python code.
69 Python code.
59
70
60 Since this marker is always the same, filtering only the output from
71 Since this marker is always the same, filtering only the output from
61 a log is very easy, using for example a simple awk call::
72 a log is very easy, using for example a simple awk call::
62
73
63 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
74 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
64
75
65 -r: log 'raw' input. Normally, IPython's logs contain the processed
76 -r
77 log 'raw' input. Normally, IPython's logs contain the processed
66 input, so that user lines are logged in their final form, converted
78 input, so that user lines are logged in their final form, converted
67 into valid Python. For example, %Exit is logged as
79 into valid Python. For example, %Exit is logged as
68 _ip.magic("Exit"). If the -r flag is given, all input is logged
80 _ip.magic("Exit"). If the -r flag is given, all input is logged
69 exactly as typed, with no transformations applied.
81 exactly as typed, with no transformations applied.
70
82
71 -t: put timestamps before each input line logged (these are put in
83 -t
72 comments)."""
84 put timestamps before each input line logged (these are put in
85 comments).
86 """
73
87
74 opts,par = self.parse_options(parameter_s,'ort')
88 opts,par = self.parse_options(parameter_s,'ort')
75 log_output = 'o' in opts
89 log_output = 'o' in opts
76 log_raw_input = 'r' in opts
90 log_raw_input = 'r' in opts
77 timestamp = 't' in opts
91 timestamp = 't' in opts
78
92
79 logger = self.shell.logger
93 logger = self.shell.logger
80
94
81 # if no args are given, the defaults set in the logger constructor by
95 # if no args are given, the defaults set in the logger constructor by
82 # ipython remain valid
96 # ipython remain valid
83 if par:
97 if par:
84 try:
98 try:
85 logfname,logmode = par.split()
99 logfname,logmode = par.split()
86 except:
100 except:
87 logfname = par
101 logfname = par
88 logmode = 'backup'
102 logmode = 'backup'
89 else:
103 else:
90 logfname = logger.logfname
104 logfname = logger.logfname
91 logmode = logger.logmode
105 logmode = logger.logmode
92 # put logfname into rc struct as if it had been called on the command
106 # put logfname into rc struct as if it had been called on the command
93 # line, so it ends up saved in the log header Save it in case we need
107 # line, so it ends up saved in the log header Save it in case we need
94 # to restore it...
108 # to restore it...
95 old_logfile = self.shell.logfile
109 old_logfile = self.shell.logfile
96 if logfname:
110 if logfname:
97 logfname = os.path.expanduser(logfname)
111 logfname = os.path.expanduser(logfname)
98 self.shell.logfile = logfname
112 self.shell.logfile = logfname
99
113
100 loghead = u'# IPython log file\n\n'
114 loghead = u'# IPython log file\n\n'
101 try:
115 try:
102 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
116 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
103 log_raw_input)
117 log_raw_input)
104 except:
118 except:
105 self.shell.logfile = old_logfile
119 self.shell.logfile = old_logfile
106 warn("Couldn't start log: %s" % sys.exc_info()[1])
120 warn("Couldn't start log: %s" % sys.exc_info()[1])
107 else:
121 else:
108 # log input history up to this point, optionally interleaving
122 # log input history up to this point, optionally interleaving
109 # output if requested
123 # output if requested
110
124
111 if timestamp:
125 if timestamp:
112 # disable timestamping for the previous history, since we've
126 # disable timestamping for the previous history, since we've
113 # lost those already (no time machine here).
127 # lost those already (no time machine here).
114 logger.timestamp = False
128 logger.timestamp = False
115
129
116 if log_raw_input:
130 if log_raw_input:
117 input_hist = self.shell.history_manager.input_hist_raw
131 input_hist = self.shell.history_manager.input_hist_raw
118 else:
132 else:
119 input_hist = self.shell.history_manager.input_hist_parsed
133 input_hist = self.shell.history_manager.input_hist_parsed
120
134
121 if log_output:
135 if log_output:
122 log_write = logger.log_write
136 log_write = logger.log_write
123 output_hist = self.shell.history_manager.output_hist
137 output_hist = self.shell.history_manager.output_hist
124 for n in range(1,len(input_hist)-1):
138 for n in range(1,len(input_hist)-1):
125 log_write(input_hist[n].rstrip() + u'\n')
139 log_write(input_hist[n].rstrip() + u'\n')
126 if n in output_hist:
140 if n in output_hist:
127 log_write(str_to_unicode(repr(output_hist[n])),'output')
141 log_write(str_to_unicode(repr(output_hist[n])),'output')
128 else:
142 else:
129 logger.log_write(u'\n'.join(input_hist[1:]))
143 logger.log_write(u'\n'.join(input_hist[1:]))
130 logger.log_write(u'\n')
144 logger.log_write(u'\n')
131 if timestamp:
145 if timestamp:
132 # re-enable timestamping
146 # re-enable timestamping
133 logger.timestamp = True
147 logger.timestamp = True
134
148
135 print ('Activating auto-logging. '
149 print ('Activating auto-logging. '
136 'Current session state plus future input saved.')
150 'Current session state plus future input saved.')
137 logger.logstate()
151 logger.logstate()
138
152
139 @line_magic
153 @line_magic
140 def logstop(self, parameter_s=''):
154 def logstop(self, parameter_s=''):
141 """Fully stop logging and close log file.
155 """Fully stop logging and close log file.
142
156
143 In order to start logging again, a new %logstart call needs to be made,
157 In order to start logging again, a new %logstart call needs to be made,
144 possibly (though not necessarily) with a new filename, mode and other
158 possibly (though not necessarily) with a new filename, mode and other
145 options."""
159 options."""
146 self.shell.logger.logstop()
160 self.shell.logger.logstop()
147
161
148 @line_magic
162 @line_magic
149 def logoff(self, parameter_s=''):
163 def logoff(self, parameter_s=''):
150 """Temporarily stop logging.
164 """Temporarily stop logging.
151
165
152 You must have previously started logging."""
166 You must have previously started logging."""
153 self.shell.logger.switch_log(0)
167 self.shell.logger.switch_log(0)
154
168
155 @line_magic
169 @line_magic
156 def logon(self, parameter_s=''):
170 def logon(self, parameter_s=''):
157 """Restart logging.
171 """Restart logging.
158
172
159 This function is for restarting logging which you've temporarily
173 This function is for restarting logging which you've temporarily
160 stopped with %logoff. For starting logging for the first time, you
174 stopped with %logoff. For starting logging for the first time, you
161 must use the %logstart function, which allows you to specify an
175 must use the %logstart function, which allows you to specify an
162 optional log filename."""
176 optional log filename."""
163
177
164 self.shell.logger.switch_log(1)
178 self.shell.logger.switch_log(1)
165
179
166 @line_magic
180 @line_magic
167 def logstate(self, parameter_s=''):
181 def logstate(self, parameter_s=''):
168 """Print the status of the logging system."""
182 """Print the status of the logging system."""
169
183
170 self.shell.logger.logstate()
184 self.shell.logger.logstate()
1 NO CONTENT: modified file
NO CONTENT: modified file
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -1,348 +1,350 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Paging capabilities for IPython.core
3 Paging capabilities for IPython.core
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Fernando Perez
8 * Fernando Perez
9
9
10 Notes
10 Notes
11 -----
11 -----
12
12
13 For now this uses ipapi, so it can't be in IPython.utils. If we can get
13 For now this uses ipapi, so it can't be in IPython.utils. If we can get
14 rid of that dependency, we could move it there.
14 rid of that dependency, we could move it there.
15 -----
15 -----
16 """
16 """
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Copyright (C) 2008-2011 The IPython Development Team
19 # Copyright (C) 2008-2011 The IPython Development Team
20 #
20 #
21 # Distributed under the terms of the BSD License. The full license is in
21 # Distributed under the terms of the BSD License. The full license is in
22 # the file COPYING, distributed as part of this software.
22 # the file COPYING, distributed as part of this software.
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Imports
26 # Imports
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 from __future__ import print_function
28 from __future__ import print_function
29
29
30 import os
30 import os
31 import re
31 import re
32 import sys
32 import sys
33 import tempfile
33 import tempfile
34
34
35 from io import UnsupportedOperation
35 from io import UnsupportedOperation
36
36
37 from IPython import get_ipython
37 from IPython import get_ipython
38 from IPython.core.error import TryNext
38 from IPython.core.error import TryNext
39 from IPython.utils.data import chop
39 from IPython.utils.data import chop
40 from IPython.utils import io
40 from IPython.utils import io
41 from IPython.utils.process import system
41 from IPython.utils.process import system
42 from IPython.utils.terminal import get_terminal_size
42 from IPython.utils.terminal import get_terminal_size
43 from IPython.utils import py3compat
43 from IPython.utils import py3compat
44
44
45
45
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47 # Classes and functions
47 # Classes and functions
48 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
49
49
50 esc_re = re.compile(r"(\x1b[^m]+m)")
50 esc_re = re.compile(r"(\x1b[^m]+m)")
51
51
52 def page_dumb(strng, start=0, screen_lines=25):
52 def page_dumb(strng, start=0, screen_lines=25):
53 """Very dumb 'pager' in Python, for when nothing else works.
53 """Very dumb 'pager' in Python, for when nothing else works.
54
54
55 Only moves forward, same interface as page(), except for pager_cmd and
55 Only moves forward, same interface as page(), except for pager_cmd and
56 mode."""
56 mode."""
57
57
58 out_ln = strng.splitlines()[start:]
58 out_ln = strng.splitlines()[start:]
59 screens = chop(out_ln,screen_lines-1)
59 screens = chop(out_ln,screen_lines-1)
60 if len(screens) == 1:
60 if len(screens) == 1:
61 print(os.linesep.join(screens[0]), file=io.stdout)
61 print(os.linesep.join(screens[0]), file=io.stdout)
62 else:
62 else:
63 last_escape = ""
63 last_escape = ""
64 for scr in screens[0:-1]:
64 for scr in screens[0:-1]:
65 hunk = os.linesep.join(scr)
65 hunk = os.linesep.join(scr)
66 print(last_escape + hunk, file=io.stdout)
66 print(last_escape + hunk, file=io.stdout)
67 if not page_more():
67 if not page_more():
68 return
68 return
69 esc_list = esc_re.findall(hunk)
69 esc_list = esc_re.findall(hunk)
70 if len(esc_list) > 0:
70 if len(esc_list) > 0:
71 last_escape = esc_list[-1]
71 last_escape = esc_list[-1]
72 print(last_escape + os.linesep.join(screens[-1]), file=io.stdout)
72 print(last_escape + os.linesep.join(screens[-1]), file=io.stdout)
73
73
74 def _detect_screen_size(screen_lines_def):
74 def _detect_screen_size(screen_lines_def):
75 """Attempt to work out the number of lines on the screen.
75 """Attempt to work out the number of lines on the screen.
76
76
77 This is called by page(). It can raise an error (e.g. when run in the
77 This is called by page(). It can raise an error (e.g. when run in the
78 test suite), so it's separated out so it can easily be called in a try block.
78 test suite), so it's separated out so it can easily be called in a try block.
79 """
79 """
80 TERM = os.environ.get('TERM',None)
80 TERM = os.environ.get('TERM',None)
81 if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'):
81 if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'):
82 # curses causes problems on many terminals other than xterm, and
82 # curses causes problems on many terminals other than xterm, and
83 # some termios calls lock up on Sun OS5.
83 # some termios calls lock up on Sun OS5.
84 return screen_lines_def
84 return screen_lines_def
85
85
86 try:
86 try:
87 import termios
87 import termios
88 import curses
88 import curses
89 except ImportError:
89 except ImportError:
90 return screen_lines_def
90 return screen_lines_def
91
91
92 # There is a bug in curses, where *sometimes* it fails to properly
92 # There is a bug in curses, where *sometimes* it fails to properly
93 # initialize, and then after the endwin() call is made, the
93 # initialize, and then after the endwin() call is made, the
94 # terminal is left in an unusable state. Rather than trying to
94 # terminal is left in an unusable state. Rather than trying to
95 # check everytime for this (by requesting and comparing termios
95 # check everytime for this (by requesting and comparing termios
96 # flags each time), we just save the initial terminal state and
96 # flags each time), we just save the initial terminal state and
97 # unconditionally reset it every time. It's cheaper than making
97 # unconditionally reset it every time. It's cheaper than making
98 # the checks.
98 # the checks.
99 term_flags = termios.tcgetattr(sys.stdout)
99 term_flags = termios.tcgetattr(sys.stdout)
100
100
101 # Curses modifies the stdout buffer size by default, which messes
101 # Curses modifies the stdout buffer size by default, which messes
102 # up Python's normal stdout buffering. This would manifest itself
102 # up Python's normal stdout buffering. This would manifest itself
103 # to IPython users as delayed printing on stdout after having used
103 # to IPython users as delayed printing on stdout after having used
104 # the pager.
104 # the pager.
105 #
105 #
106 # We can prevent this by manually setting the NCURSES_NO_SETBUF
106 # We can prevent this by manually setting the NCURSES_NO_SETBUF
107 # environment variable. For more details, see:
107 # environment variable. For more details, see:
108 # http://bugs.python.org/issue10144
108 # http://bugs.python.org/issue10144
109 NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None)
109 NCURSES_NO_SETBUF = os.environ.get('NCURSES_NO_SETBUF', None)
110 os.environ['NCURSES_NO_SETBUF'] = ''
110 os.environ['NCURSES_NO_SETBUF'] = ''
111
111
112 # Proceed with curses initialization
112 # Proceed with curses initialization
113 try:
113 try:
114 scr = curses.initscr()
114 scr = curses.initscr()
115 except AttributeError:
115 except AttributeError:
116 # Curses on Solaris may not be complete, so we can't use it there
116 # Curses on Solaris may not be complete, so we can't use it there
117 return screen_lines_def
117 return screen_lines_def
118
118
119 screen_lines_real,screen_cols = scr.getmaxyx()
119 screen_lines_real,screen_cols = scr.getmaxyx()
120 curses.endwin()
120 curses.endwin()
121
121
122 # Restore environment
122 # Restore environment
123 if NCURSES_NO_SETBUF is None:
123 if NCURSES_NO_SETBUF is None:
124 del os.environ['NCURSES_NO_SETBUF']
124 del os.environ['NCURSES_NO_SETBUF']
125 else:
125 else:
126 os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF
126 os.environ['NCURSES_NO_SETBUF'] = NCURSES_NO_SETBUF
127
127
128 # Restore terminal state in case endwin() didn't.
128 # Restore terminal state in case endwin() didn't.
129 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
129 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
130 # Now we have what we needed: the screen size in rows/columns
130 # Now we have what we needed: the screen size in rows/columns
131 return screen_lines_real
131 return screen_lines_real
132 #print '***Screen size:',screen_lines_real,'lines x',\
132 #print '***Screen size:',screen_lines_real,'lines x',\
133 #screen_cols,'columns.' # dbg
133 #screen_cols,'columns.' # dbg
134
134
135 def page(strng, start=0, screen_lines=0, pager_cmd=None):
135 def page(strng, start=0, screen_lines=0, pager_cmd=None):
136 """Print a string, piping through a pager after a certain length.
136 """Print a string, piping through a pager after a certain length.
137
137
138 The screen_lines parameter specifies the number of *usable* lines of your
138 The screen_lines parameter specifies the number of *usable* lines of your
139 terminal screen (total lines minus lines you need to reserve to show other
139 terminal screen (total lines minus lines you need to reserve to show other
140 information).
140 information).
141
141
142 If you set screen_lines to a number <=0, page() will try to auto-determine
142 If you set screen_lines to a number <=0, page() will try to auto-determine
143 your screen size and will only use up to (screen_size+screen_lines) for
143 your screen size and will only use up to (screen_size+screen_lines) for
144 printing, paging after that. That is, if you want auto-detection but need
144 printing, paging after that. That is, if you want auto-detection but need
145 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
145 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
146 auto-detection without any lines reserved simply use screen_lines = 0.
146 auto-detection without any lines reserved simply use screen_lines = 0.
147
147
148 If a string won't fit in the allowed lines, it is sent through the
148 If a string won't fit in the allowed lines, it is sent through the
149 specified pager command. If none given, look for PAGER in the environment,
149 specified pager command. If none given, look for PAGER in the environment,
150 and ultimately default to less.
150 and ultimately default to less.
151
151
152 If no system pager works, the string is sent through a 'dumb pager'
152 If no system pager works, the string is sent through a 'dumb pager'
153 written in python, very simplistic.
153 written in python, very simplistic.
154 """
154 """
155
155
156 # Some routines may auto-compute start offsets incorrectly and pass a
156 # Some routines may auto-compute start offsets incorrectly and pass a
157 # negative value. Offset to 0 for robustness.
157 # negative value. Offset to 0 for robustness.
158 start = max(0, start)
158 start = max(0, start)
159
159
160 # first, try the hook
160 # first, try the hook
161 ip = get_ipython()
161 ip = get_ipython()
162 if ip:
162 if ip:
163 try:
163 try:
164 ip.hooks.show_in_pager(strng)
164 ip.hooks.show_in_pager(strng)
165 return
165 return
166 except TryNext:
166 except TryNext:
167 pass
167 pass
168
168
169 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
169 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
170 TERM = os.environ.get('TERM','dumb')
170 TERM = os.environ.get('TERM','dumb')
171 if TERM in ['dumb','emacs'] and os.name != 'nt':
171 if TERM in ['dumb','emacs'] and os.name != 'nt':
172 print(strng)
172 print(strng)
173 return
173 return
174 # chop off the topmost part of the string we don't want to see
174 # chop off the topmost part of the string we don't want to see
175 str_lines = strng.splitlines()[start:]
175 str_lines = strng.splitlines()[start:]
176 str_toprint = os.linesep.join(str_lines)
176 str_toprint = os.linesep.join(str_lines)
177 num_newlines = len(str_lines)
177 num_newlines = len(str_lines)
178 len_str = len(str_toprint)
178 len_str = len(str_toprint)
179
179
180 # Dumb heuristics to guesstimate number of on-screen lines the string
180 # Dumb heuristics to guesstimate number of on-screen lines the string
181 # takes. Very basic, but good enough for docstrings in reasonable
181 # takes. Very basic, but good enough for docstrings in reasonable
182 # terminals. If someone later feels like refining it, it's not hard.
182 # terminals. If someone later feels like refining it, it's not hard.
183 numlines = max(num_newlines,int(len_str/80)+1)
183 numlines = max(num_newlines,int(len_str/80)+1)
184
184
185 screen_lines_def = get_terminal_size()[1]
185 screen_lines_def = get_terminal_size()[1]
186
186
187 # auto-determine screen size
187 # auto-determine screen size
188 if screen_lines <= 0:
188 if screen_lines <= 0:
189 try:
189 try:
190 screen_lines += _detect_screen_size(screen_lines_def)
190 screen_lines += _detect_screen_size(screen_lines_def)
191 except (TypeError, UnsupportedOperation):
191 except (TypeError, UnsupportedOperation):
192 print(str_toprint, file=io.stdout)
192 print(str_toprint, file=io.stdout)
193 return
193 return
194
194
195 #print 'numlines',numlines,'screenlines',screen_lines # dbg
195 #print 'numlines',numlines,'screenlines',screen_lines # dbg
196 if numlines <= screen_lines :
196 if numlines <= screen_lines :
197 #print '*** normal print' # dbg
197 #print '*** normal print' # dbg
198 print(str_toprint, file=io.stdout)
198 print(str_toprint, file=io.stdout)
199 else:
199 else:
200 # Try to open pager and default to internal one if that fails.
200 # Try to open pager and default to internal one if that fails.
201 # All failure modes are tagged as 'retval=1', to match the return
201 # All failure modes are tagged as 'retval=1', to match the return
202 # value of a failed system command. If any intermediate attempt
202 # value of a failed system command. If any intermediate attempt
203 # sets retval to 1, at the end we resort to our own page_dumb() pager.
203 # sets retval to 1, at the end we resort to our own page_dumb() pager.
204 pager_cmd = get_pager_cmd(pager_cmd)
204 pager_cmd = get_pager_cmd(pager_cmd)
205 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
205 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
206 if os.name == 'nt':
206 if os.name == 'nt':
207 if pager_cmd.startswith('type'):
207 if pager_cmd.startswith('type'):
208 # The default WinXP 'type' command is failing on complex strings.
208 # The default WinXP 'type' command is failing on complex strings.
209 retval = 1
209 retval = 1
210 else:
210 else:
211 tmpname = tempfile.mktemp('.txt')
211 tmpname = tempfile.mktemp('.txt')
212 tmpfile = open(tmpname,'wt')
212 tmpfile = open(tmpname,'wt')
213 tmpfile.write(strng)
213 tmpfile.write(strng)
214 tmpfile.close()
214 tmpfile.close()
215 cmd = "%s < %s" % (pager_cmd,tmpname)
215 cmd = "%s < %s" % (pager_cmd,tmpname)
216 if os.system(cmd):
216 if os.system(cmd):
217 retval = 1
217 retval = 1
218 else:
218 else:
219 retval = None
219 retval = None
220 os.remove(tmpname)
220 os.remove(tmpname)
221 else:
221 else:
222 try:
222 try:
223 retval = None
223 retval = None
224 # if I use popen4, things hang. No idea why.
224 # if I use popen4, things hang. No idea why.
225 #pager,shell_out = os.popen4(pager_cmd)
225 #pager,shell_out = os.popen4(pager_cmd)
226 pager = os.popen(pager_cmd, 'w')
226 pager = os.popen(pager_cmd, 'w')
227 try:
227 try:
228 pager_encoding = pager.encoding or sys.stdout.encoding
228 pager_encoding = pager.encoding or sys.stdout.encoding
229 pager.write(py3compat.cast_bytes_py2(
229 pager.write(py3compat.cast_bytes_py2(
230 strng, encoding=pager_encoding))
230 strng, encoding=pager_encoding))
231 finally:
231 finally:
232 retval = pager.close()
232 retval = pager.close()
233 except IOError as msg: # broken pipe when user quits
233 except IOError as msg: # broken pipe when user quits
234 if msg.args == (32, 'Broken pipe'):
234 if msg.args == (32, 'Broken pipe'):
235 retval = None
235 retval = None
236 else:
236 else:
237 retval = 1
237 retval = 1
238 except OSError:
238 except OSError:
239 # Other strange problems, sometimes seen in Win2k/cygwin
239 # Other strange problems, sometimes seen in Win2k/cygwin
240 retval = 1
240 retval = 1
241 if retval is not None:
241 if retval is not None:
242 page_dumb(strng,screen_lines=screen_lines)
242 page_dumb(strng,screen_lines=screen_lines)
243
243
244
244
245 def page_file(fname, start=0, pager_cmd=None):
245 def page_file(fname, start=0, pager_cmd=None):
246 """Page a file, using an optional pager command and starting line.
246 """Page a file, using an optional pager command and starting line.
247 """
247 """
248
248
249 pager_cmd = get_pager_cmd(pager_cmd)
249 pager_cmd = get_pager_cmd(pager_cmd)
250 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
250 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
251
251
252 try:
252 try:
253 if os.environ['TERM'] in ['emacs','dumb']:
253 if os.environ['TERM'] in ['emacs','dumb']:
254 raise EnvironmentError
254 raise EnvironmentError
255 system(pager_cmd + ' ' + fname)
255 system(pager_cmd + ' ' + fname)
256 except:
256 except:
257 try:
257 try:
258 if start > 0:
258 if start > 0:
259 start -= 1
259 start -= 1
260 page(open(fname).read(),start)
260 page(open(fname).read(),start)
261 except:
261 except:
262 print('Unable to show file',repr(fname))
262 print('Unable to show file',repr(fname))
263
263
264
264
265 def get_pager_cmd(pager_cmd=None):
265 def get_pager_cmd(pager_cmd=None):
266 """Return a pager command.
266 """Return a pager command.
267
267
268 Makes some attempts at finding an OS-correct one.
268 Makes some attempts at finding an OS-correct one.
269 """
269 """
270 if os.name == 'posix':
270 if os.name == 'posix':
271 default_pager_cmd = 'less -r' # -r for color control sequences
271 default_pager_cmd = 'less -r' # -r for color control sequences
272 elif os.name in ['nt','dos']:
272 elif os.name in ['nt','dos']:
273 default_pager_cmd = 'type'
273 default_pager_cmd = 'type'
274
274
275 if pager_cmd is None:
275 if pager_cmd is None:
276 try:
276 try:
277 pager_cmd = os.environ['PAGER']
277 pager_cmd = os.environ['PAGER']
278 except:
278 except:
279 pager_cmd = default_pager_cmd
279 pager_cmd = default_pager_cmd
280 return pager_cmd
280 return pager_cmd
281
281
282
282
283 def get_pager_start(pager, start):
283 def get_pager_start(pager, start):
284 """Return the string for paging files with an offset.
284 """Return the string for paging files with an offset.
285
285
286 This is the '+N' argument which less and more (under Unix) accept.
286 This is the '+N' argument which less and more (under Unix) accept.
287 """
287 """
288
288
289 if pager in ['less','more']:
289 if pager in ['less','more']:
290 if start:
290 if start:
291 start_string = '+' + str(start)
291 start_string = '+' + str(start)
292 else:
292 else:
293 start_string = ''
293 start_string = ''
294 else:
294 else:
295 start_string = ''
295 start_string = ''
296 return start_string
296 return start_string
297
297
298
298
299 # (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch()
299 # (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch()
300 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
300 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
301 import msvcrt
301 import msvcrt
302 def page_more():
302 def page_more():
303 """ Smart pausing between pages
303 """ Smart pausing between pages
304
304
305 @return: True if need print more lines, False if quit
305 @return: True if need print more lines, False if quit
306 """
306 """
307 io.stdout.write('---Return to continue, q to quit--- ')
307 io.stdout.write('---Return to continue, q to quit--- ')
308 ans = msvcrt.getwch()
308 ans = msvcrt.getwch()
309 if ans in ("q", "Q"):
309 if ans in ("q", "Q"):
310 result = False
310 result = False
311 else:
311 else:
312 result = True
312 result = True
313 io.stdout.write("\b"*37 + " "*37 + "\b"*37)
313 io.stdout.write("\b"*37 + " "*37 + "\b"*37)
314 return result
314 return result
315 else:
315 else:
316 def page_more():
316 def page_more():
317 ans = raw_input('---Return to continue, q to quit--- ')
317 ans = raw_input('---Return to continue, q to quit--- ')
318 if ans.lower().startswith('q'):
318 if ans.lower().startswith('q'):
319 return False
319 return False
320 else:
320 else:
321 return True
321 return True
322
322
323
323
324 def snip_print(str,width = 75,print_full = 0,header = ''):
324 def snip_print(str,width = 75,print_full = 0,header = ''):
325 """Print a string snipping the midsection to fit in width.
325 """Print a string snipping the midsection to fit in width.
326
326
327 print_full: mode control:
327 print_full: mode control:
328
328 - 0: only snip long strings
329 - 0: only snip long strings
329 - 1: send to page() directly.
330 - 1: send to page() directly.
330 - 2: snip long strings and ask for full length viewing with page()
331 - 2: snip long strings and ask for full length viewing with page()
332
331 Return 1 if snipping was necessary, 0 otherwise."""
333 Return 1 if snipping was necessary, 0 otherwise."""
332
334
333 if print_full == 1:
335 if print_full == 1:
334 page(header+str)
336 page(header+str)
335 return 0
337 return 0
336
338
337 print(header, end=' ')
339 print(header, end=' ')
338 if len(str) < width:
340 if len(str) < width:
339 print(str)
341 print(str)
340 snip = 0
342 snip = 0
341 else:
343 else:
342 whalf = int((width -5)/2)
344 whalf = int((width -5)/2)
343 print(str[:whalf] + ' <...> ' + str[-whalf:])
345 print(str[:whalf] + ' <...> ' + str[-whalf:])
344 snip = 1
346 snip = 1
345 if snip and print_full == 2:
347 if snip and print_full == 2:
346 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
348 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
347 page(str)
349 page(str)
348 return snip
350 return snip
@@ -1,1247 +1,1251 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
12 import sys,ultratb
13 import sys,ultratb
13 sys.excepthook = ultratb.ColorTB()
14 sys.excepthook = ultratb.ColorTB()
14
15
15 * VerboseTB
16 * VerboseTB
16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
17 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
18 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
19 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,
20 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
21 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.
22 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.
23 Give it a shot--you'll love it or you'll hate it.
23
24
24 Note:
25 .. note::
25
26
26 The Verbose mode prints the variables currently visible where the exception
27 The Verbose mode prints the variables currently visible where the exception
27 happened (shortening their strings if too long). This can potentially be
28 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
29 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
30 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
31 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).
32 with Ctrl-C (maybe hitting it more than once).
32
33
33 If you encounter this kind of situation often, you may want to use the
34 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
35 Verbose_novars mode instead of the regular Verbose, which avoids formatting
35 variables (but otherwise includes the information and context given by
36 variables (but otherwise includes the information and context given by
36 Verbose).
37 Verbose).
37
38
38
39
39 Installation instructions for ColorTB:
40 Installation instructions for ColorTB::
41
40 import sys,ultratb
42 import sys,ultratb
41 sys.excepthook = ultratb.VerboseTB()
43 sys.excepthook = ultratb.VerboseTB()
42
44
43 Note: Much of the code in this module was lifted verbatim from the standard
45 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'.
46 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
45
47
46 * Color schemes
48 Color schemes
49 -------------
50
47 The colors are defined in the class TBTools through the use of the
51 The colors are defined in the class TBTools through the use of the
48 ColorSchemeTable class. Currently the following exist:
52 ColorSchemeTable class. Currently the following exist:
49
53
50 - NoColor: allows all of this module to be used in any terminal (the color
54 - NoColor: allows all of this module to be used in any terminal (the color
51 escapes are just dummy blank strings).
55 escapes are just dummy blank strings).
52
56
53 - Linux: is meant to look good in a terminal like the Linux console (black
57 - Linux: is meant to look good in a terminal like the Linux console (black
54 or very dark background).
58 or very dark background).
55
59
56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
60 - LightBG: similar to Linux but swaps dark/light colors to be more readable
57 in light background terminals.
61 in light background terminals.
58
62
59 You can implement other color schemes easily, the syntax is fairly
63 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
64 self-explanatory. Please send back new schemes you develop to the author for
61 possible inclusion in future releases.
65 possible inclusion in future releases.
62
66
63 Inheritance diagram:
67 Inheritance diagram:
64
68
65 .. inheritance-diagram:: IPython.core.ultratb
69 .. inheritance-diagram:: IPython.core.ultratb
66 :parts: 3
70 :parts: 3
67 """
71 """
68
72
69 #*****************************************************************************
73 #*****************************************************************************
70 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
74 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
71 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
75 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
72 #
76 #
73 # Distributed under the terms of the BSD License. The full license is in
77 # Distributed under the terms of the BSD License. The full license is in
74 # the file COPYING, distributed as part of this software.
78 # the file COPYING, distributed as part of this software.
75 #*****************************************************************************
79 #*****************************************************************************
76
80
77 from __future__ import unicode_literals
81 from __future__ import unicode_literals
78
82
79 import inspect
83 import inspect
80 import keyword
84 import keyword
81 import linecache
85 import linecache
82 import os
86 import os
83 import pydoc
87 import pydoc
84 import re
88 import re
85 import sys
89 import sys
86 import time
90 import time
87 import tokenize
91 import tokenize
88 import traceback
92 import traceback
89 import types
93 import types
90
94
91 try: # Python 2
95 try: # Python 2
92 generate_tokens = tokenize.generate_tokens
96 generate_tokens = tokenize.generate_tokens
93 except AttributeError: # Python 3
97 except AttributeError: # Python 3
94 generate_tokens = tokenize.tokenize
98 generate_tokens = tokenize.tokenize
95
99
96 # For purposes of monkeypatching inspect to fix a bug in it.
100 # For purposes of monkeypatching inspect to fix a bug in it.
97 from inspect import getsourcefile, getfile, getmodule,\
101 from inspect import getsourcefile, getfile, getmodule,\
98 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
102 ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
99
103
100 # IPython's own modules
104 # IPython's own modules
101 # Modified pdb which doesn't damage IPython's readline handling
105 # Modified pdb which doesn't damage IPython's readline handling
102 from IPython import get_ipython
106 from IPython import get_ipython
103 from IPython.core import debugger
107 from IPython.core import debugger
104 from IPython.core.display_trap import DisplayTrap
108 from IPython.core.display_trap import DisplayTrap
105 from IPython.core.excolors import exception_colors
109 from IPython.core.excolors import exception_colors
106 from IPython.utils import PyColorize
110 from IPython.utils import PyColorize
107 from IPython.utils import io
111 from IPython.utils import io
108 from IPython.utils import openpy
112 from IPython.utils import openpy
109 from IPython.utils import path as util_path
113 from IPython.utils import path as util_path
110 from IPython.utils import py3compat
114 from IPython.utils import py3compat
111 from IPython.utils import ulinecache
115 from IPython.utils import ulinecache
112 from IPython.utils.data import uniq_stable
116 from IPython.utils.data import uniq_stable
113 from IPython.utils.warn import info, error
117 from IPython.utils.warn import info, error
114
118
115 # Globals
119 # Globals
116 # amount of space to put line numbers before verbose tracebacks
120 # amount of space to put line numbers before verbose tracebacks
117 INDENT_SIZE = 8
121 INDENT_SIZE = 8
118
122
119 # Default color scheme. This is used, for example, by the traceback
123 # Default color scheme. This is used, for example, by the traceback
120 # formatter. When running in an actual IPython instance, the user's rc.colors
124 # formatter. When running in an actual IPython instance, the user's rc.colors
121 # value is used, but havinga module global makes this functionality available
125 # value is used, but havinga module global makes this functionality available
122 # to users of ultratb who are NOT running inside ipython.
126 # to users of ultratb who are NOT running inside ipython.
123 DEFAULT_SCHEME = 'NoColor'
127 DEFAULT_SCHEME = 'NoColor'
124
128
125 #---------------------------------------------------------------------------
129 #---------------------------------------------------------------------------
126 # Code begins
130 # Code begins
127
131
128 # Utility functions
132 # Utility functions
129 def inspect_error():
133 def inspect_error():
130 """Print a message about internal inspect errors.
134 """Print a message about internal inspect errors.
131
135
132 These are unfortunately quite common."""
136 These are unfortunately quite common."""
133
137
134 error('Internal Python error in the inspect module.\n'
138 error('Internal Python error in the inspect module.\n'
135 'Below is the traceback from this internal error.\n')
139 'Below is the traceback from this internal error.\n')
136
140
137 # This function is a monkeypatch we apply to the Python inspect module. We have
141 # This function is a monkeypatch we apply to the Python inspect module. We have
138 # now found when it's needed (see discussion on issue gh-1456), and we have a
142 # now found when it's needed (see discussion on issue gh-1456), and we have a
139 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
143 # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
140 # the monkeypatch is not applied. TK, Aug 2012.
144 # the monkeypatch is not applied. TK, Aug 2012.
141 def findsource(object):
145 def findsource(object):
142 """Return the entire source file and starting line number for an object.
146 """Return the entire source file and starting line number for an object.
143
147
144 The argument may be a module, class, method, function, traceback, frame,
148 The argument may be a module, class, method, function, traceback, frame,
145 or code object. The source code is returned as a list of all the lines
149 or code object. The source code is returned as a list of all the lines
146 in the file and the line number indexes a line in that list. An IOError
150 in the file and the line number indexes a line in that list. An IOError
147 is raised if the source code cannot be retrieved.
151 is raised if the source code cannot be retrieved.
148
152
149 FIXED version with which we monkeypatch the stdlib to work around a bug."""
153 FIXED version with which we monkeypatch the stdlib to work around a bug."""
150
154
151 file = getsourcefile(object) or getfile(object)
155 file = getsourcefile(object) or getfile(object)
152 # If the object is a frame, then trying to get the globals dict from its
156 # If the object is a frame, then trying to get the globals dict from its
153 # module won't work. Instead, the frame object itself has the globals
157 # module won't work. Instead, the frame object itself has the globals
154 # dictionary.
158 # dictionary.
155 globals_dict = None
159 globals_dict = None
156 if inspect.isframe(object):
160 if inspect.isframe(object):
157 # XXX: can this ever be false?
161 # XXX: can this ever be false?
158 globals_dict = object.f_globals
162 globals_dict = object.f_globals
159 else:
163 else:
160 module = getmodule(object, file)
164 module = getmodule(object, file)
161 if module:
165 if module:
162 globals_dict = module.__dict__
166 globals_dict = module.__dict__
163 lines = linecache.getlines(file, globals_dict)
167 lines = linecache.getlines(file, globals_dict)
164 if not lines:
168 if not lines:
165 raise IOError('could not get source code')
169 raise IOError('could not get source code')
166
170
167 if ismodule(object):
171 if ismodule(object):
168 return lines, 0
172 return lines, 0
169
173
170 if isclass(object):
174 if isclass(object):
171 name = object.__name__
175 name = object.__name__
172 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
176 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
173 # make some effort to find the best matching class definition:
177 # make some effort to find the best matching class definition:
174 # use the one with the least indentation, which is the one
178 # use the one with the least indentation, which is the one
175 # that's most probably not inside a function definition.
179 # that's most probably not inside a function definition.
176 candidates = []
180 candidates = []
177 for i in range(len(lines)):
181 for i in range(len(lines)):
178 match = pat.match(lines[i])
182 match = pat.match(lines[i])
179 if match:
183 if match:
180 # if it's at toplevel, it's already the best one
184 # if it's at toplevel, it's already the best one
181 if lines[i][0] == 'c':
185 if lines[i][0] == 'c':
182 return lines, i
186 return lines, i
183 # else add whitespace to candidate list
187 # else add whitespace to candidate list
184 candidates.append((match.group(1), i))
188 candidates.append((match.group(1), i))
185 if candidates:
189 if candidates:
186 # this will sort by whitespace, and by line number,
190 # this will sort by whitespace, and by line number,
187 # less whitespace first
191 # less whitespace first
188 candidates.sort()
192 candidates.sort()
189 return lines, candidates[0][1]
193 return lines, candidates[0][1]
190 else:
194 else:
191 raise IOError('could not find class definition')
195 raise IOError('could not find class definition')
192
196
193 if ismethod(object):
197 if ismethod(object):
194 object = object.im_func
198 object = object.im_func
195 if isfunction(object):
199 if isfunction(object):
196 object = object.func_code
200 object = object.func_code
197 if istraceback(object):
201 if istraceback(object):
198 object = object.tb_frame
202 object = object.tb_frame
199 if isframe(object):
203 if isframe(object):
200 object = object.f_code
204 object = object.f_code
201 if iscode(object):
205 if iscode(object):
202 if not hasattr(object, 'co_firstlineno'):
206 if not hasattr(object, 'co_firstlineno'):
203 raise IOError('could not find function definition')
207 raise IOError('could not find function definition')
204 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
208 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
205 pmatch = pat.match
209 pmatch = pat.match
206 # fperez - fix: sometimes, co_firstlineno can give a number larger than
210 # fperez - fix: sometimes, co_firstlineno can give a number larger than
207 # the length of lines, which causes an error. Safeguard against that.
211 # the length of lines, which causes an error. Safeguard against that.
208 lnum = min(object.co_firstlineno,len(lines))-1
212 lnum = min(object.co_firstlineno,len(lines))-1
209 while lnum > 0:
213 while lnum > 0:
210 if pmatch(lines[lnum]): break
214 if pmatch(lines[lnum]): break
211 lnum -= 1
215 lnum -= 1
212
216
213 return lines, lnum
217 return lines, lnum
214 raise IOError('could not find code object')
218 raise IOError('could not find code object')
215
219
216 # Monkeypatch inspect to apply our bugfix. This code only works with Python >= 2.5
220 # Monkeypatch inspect to apply our bugfix. This code only works with Python >= 2.5
217 inspect.findsource = findsource
221 inspect.findsource = findsource
218
222
219 def fix_frame_records_filenames(records):
223 def fix_frame_records_filenames(records):
220 """Try to fix the filenames in each record from inspect.getinnerframes().
224 """Try to fix the filenames in each record from inspect.getinnerframes().
221
225
222 Particularly, modules loaded from within zip files have useless filenames
226 Particularly, modules loaded from within zip files have useless filenames
223 attached to their code object, and inspect.getinnerframes() just uses it.
227 attached to their code object, and inspect.getinnerframes() just uses it.
224 """
228 """
225 fixed_records = []
229 fixed_records = []
226 for frame, filename, line_no, func_name, lines, index in records:
230 for frame, filename, line_no, func_name, lines, index in records:
227 # Look inside the frame's globals dictionary for __file__, which should
231 # Look inside the frame's globals dictionary for __file__, which should
228 # be better.
232 # be better.
229 better_fn = frame.f_globals.get('__file__', None)
233 better_fn = frame.f_globals.get('__file__', None)
230 if isinstance(better_fn, str):
234 if isinstance(better_fn, str):
231 # Check the type just in case someone did something weird with
235 # Check the type just in case someone did something weird with
232 # __file__. It might also be None if the error occurred during
236 # __file__. It might also be None if the error occurred during
233 # import.
237 # import.
234 filename = better_fn
238 filename = better_fn
235 fixed_records.append((frame, filename, line_no, func_name, lines, index))
239 fixed_records.append((frame, filename, line_no, func_name, lines, index))
236 return fixed_records
240 return fixed_records
237
241
238
242
239 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
243 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
240 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
244 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
241
245
242 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
246 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
243
247
244 # If the error is at the console, don't build any context, since it would
248 # If the error is at the console, don't build any context, since it would
245 # otherwise produce 5 blank lines printed out (there is no file at the
249 # otherwise produce 5 blank lines printed out (there is no file at the
246 # console)
250 # console)
247 rec_check = records[tb_offset:]
251 rec_check = records[tb_offset:]
248 try:
252 try:
249 rname = rec_check[0][1]
253 rname = rec_check[0][1]
250 if rname == '<ipython console>' or rname.endswith('<string>'):
254 if rname == '<ipython console>' or rname.endswith('<string>'):
251 return rec_check
255 return rec_check
252 except IndexError:
256 except IndexError:
253 pass
257 pass
254
258
255 aux = traceback.extract_tb(etb)
259 aux = traceback.extract_tb(etb)
256 assert len(records) == len(aux)
260 assert len(records) == len(aux)
257 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
261 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
258 maybeStart = lnum-1 - context//2
262 maybeStart = lnum-1 - context//2
259 start = max(maybeStart, 0)
263 start = max(maybeStart, 0)
260 end = start + context
264 end = start + context
261 lines = ulinecache.getlines(file)[start:end]
265 lines = ulinecache.getlines(file)[start:end]
262 buf = list(records[i])
266 buf = list(records[i])
263 buf[LNUM_POS] = lnum
267 buf[LNUM_POS] = lnum
264 buf[INDEX_POS] = lnum - 1 - start
268 buf[INDEX_POS] = lnum - 1 - start
265 buf[LINES_POS] = lines
269 buf[LINES_POS] = lines
266 records[i] = tuple(buf)
270 records[i] = tuple(buf)
267 return records[tb_offset:]
271 return records[tb_offset:]
268
272
269 # Helper function -- largely belongs to VerboseTB, but we need the same
273 # Helper function -- largely belongs to VerboseTB, but we need the same
270 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
274 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
271 # can be recognized properly by ipython.el's py-traceback-line-re
275 # can be recognized properly by ipython.el's py-traceback-line-re
272 # (SyntaxErrors have to be treated specially because they have no traceback)
276 # (SyntaxErrors have to be treated specially because they have no traceback)
273
277
274 _parser = PyColorize.Parser()
278 _parser = PyColorize.Parser()
275
279
276 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None):
280 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None,scheme=None):
277 numbers_width = INDENT_SIZE - 1
281 numbers_width = INDENT_SIZE - 1
278 res = []
282 res = []
279 i = lnum - index
283 i = lnum - index
280
284
281 # This lets us get fully syntax-highlighted tracebacks.
285 # This lets us get fully syntax-highlighted tracebacks.
282 if scheme is None:
286 if scheme is None:
283 ipinst = get_ipython()
287 ipinst = get_ipython()
284 if ipinst is not None:
288 if ipinst is not None:
285 scheme = ipinst.colors
289 scheme = ipinst.colors
286 else:
290 else:
287 scheme = DEFAULT_SCHEME
291 scheme = DEFAULT_SCHEME
288
292
289 _line_format = _parser.format2
293 _line_format = _parser.format2
290
294
291 for line in lines:
295 for line in lines:
292 line = py3compat.cast_unicode(line)
296 line = py3compat.cast_unicode(line)
293
297
294 new_line, err = _line_format(line, 'str', scheme)
298 new_line, err = _line_format(line, 'str', scheme)
295 if not err: line = new_line
299 if not err: line = new_line
296
300
297 if i == lnum:
301 if i == lnum:
298 # This is the line with the error
302 # This is the line with the error
299 pad = numbers_width - len(str(i))
303 pad = numbers_width - len(str(i))
300 if pad >= 3:
304 if pad >= 3:
301 marker = '-'*(pad-3) + '-> '
305 marker = '-'*(pad-3) + '-> '
302 elif pad == 2:
306 elif pad == 2:
303 marker = '> '
307 marker = '> '
304 elif pad == 1:
308 elif pad == 1:
305 marker = '>'
309 marker = '>'
306 else:
310 else:
307 marker = ''
311 marker = ''
308 num = marker + str(i)
312 num = marker + str(i)
309 line = '%s%s%s %s%s' %(Colors.linenoEm, num,
313 line = '%s%s%s %s%s' %(Colors.linenoEm, num,
310 Colors.line, line, Colors.Normal)
314 Colors.line, line, Colors.Normal)
311 else:
315 else:
312 num = '%*s' % (numbers_width,i)
316 num = '%*s' % (numbers_width,i)
313 line = '%s%s%s %s' %(Colors.lineno, num,
317 line = '%s%s%s %s' %(Colors.lineno, num,
314 Colors.Normal, line)
318 Colors.Normal, line)
315
319
316 res.append(line)
320 res.append(line)
317 if lvals and i == lnum:
321 if lvals and i == lnum:
318 res.append(lvals + '\n')
322 res.append(lvals + '\n')
319 i = i + 1
323 i = i + 1
320 return res
324 return res
321
325
322
326
323 #---------------------------------------------------------------------------
327 #---------------------------------------------------------------------------
324 # Module classes
328 # Module classes
325 class TBTools(object):
329 class TBTools(object):
326 """Basic tools used by all traceback printer classes."""
330 """Basic tools used by all traceback printer classes."""
327
331
328 # Number of frames to skip when reporting tracebacks
332 # Number of frames to skip when reporting tracebacks
329 tb_offset = 0
333 tb_offset = 0
330
334
331 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None):
335 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None):
332 # Whether to call the interactive pdb debugger after printing
336 # Whether to call the interactive pdb debugger after printing
333 # tracebacks or not
337 # tracebacks or not
334 self.call_pdb = call_pdb
338 self.call_pdb = call_pdb
335
339
336 # Output stream to write to. Note that we store the original value in
340 # Output stream to write to. Note that we store the original value in
337 # a private attribute and then make the public ostream a property, so
341 # a private attribute and then make the public ostream a property, so
338 # that we can delay accessing io.stdout until runtime. The way
342 # that we can delay accessing io.stdout until runtime. The way
339 # things are written now, the io.stdout object is dynamically managed
343 # things are written now, the io.stdout object is dynamically managed
340 # so a reference to it should NEVER be stored statically. This
344 # so a reference to it should NEVER be stored statically. This
341 # property approach confines this detail to a single location, and all
345 # property approach confines this detail to a single location, and all
342 # subclasses can simply access self.ostream for writing.
346 # subclasses can simply access self.ostream for writing.
343 self._ostream = ostream
347 self._ostream = ostream
344
348
345 # Create color table
349 # Create color table
346 self.color_scheme_table = exception_colors()
350 self.color_scheme_table = exception_colors()
347
351
348 self.set_colors(color_scheme)
352 self.set_colors(color_scheme)
349 self.old_scheme = color_scheme # save initial value for toggles
353 self.old_scheme = color_scheme # save initial value for toggles
350
354
351 if call_pdb:
355 if call_pdb:
352 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
356 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
353 else:
357 else:
354 self.pdb = None
358 self.pdb = None
355
359
356 def _get_ostream(self):
360 def _get_ostream(self):
357 """Output stream that exceptions are written to.
361 """Output stream that exceptions are written to.
358
362
359 Valid values are:
363 Valid values are:
360
364
361 - None: the default, which means that IPython will dynamically resolve
365 - None: the default, which means that IPython will dynamically resolve
362 to io.stdout. This ensures compatibility with most tools, including
366 to io.stdout. This ensures compatibility with most tools, including
363 Windows (where plain stdout doesn't recognize ANSI escapes).
367 Windows (where plain stdout doesn't recognize ANSI escapes).
364
368
365 - Any object with 'write' and 'flush' attributes.
369 - Any object with 'write' and 'flush' attributes.
366 """
370 """
367 return io.stdout if self._ostream is None else self._ostream
371 return io.stdout if self._ostream is None else self._ostream
368
372
369 def _set_ostream(self, val):
373 def _set_ostream(self, val):
370 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
374 assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
371 self._ostream = val
375 self._ostream = val
372
376
373 ostream = property(_get_ostream, _set_ostream)
377 ostream = property(_get_ostream, _set_ostream)
374
378
375 def set_colors(self,*args,**kw):
379 def set_colors(self,*args,**kw):
376 """Shorthand access to the color table scheme selector method."""
380 """Shorthand access to the color table scheme selector method."""
377
381
378 # Set own color table
382 # Set own color table
379 self.color_scheme_table.set_active_scheme(*args,**kw)
383 self.color_scheme_table.set_active_scheme(*args,**kw)
380 # for convenience, set Colors to the active scheme
384 # for convenience, set Colors to the active scheme
381 self.Colors = self.color_scheme_table.active_colors
385 self.Colors = self.color_scheme_table.active_colors
382 # Also set colors of debugger
386 # Also set colors of debugger
383 if hasattr(self,'pdb') and self.pdb is not None:
387 if hasattr(self,'pdb') and self.pdb is not None:
384 self.pdb.set_colors(*args,**kw)
388 self.pdb.set_colors(*args,**kw)
385
389
386 def color_toggle(self):
390 def color_toggle(self):
387 """Toggle between the currently active color scheme and NoColor."""
391 """Toggle between the currently active color scheme and NoColor."""
388
392
389 if self.color_scheme_table.active_scheme_name == 'NoColor':
393 if self.color_scheme_table.active_scheme_name == 'NoColor':
390 self.color_scheme_table.set_active_scheme(self.old_scheme)
394 self.color_scheme_table.set_active_scheme(self.old_scheme)
391 self.Colors = self.color_scheme_table.active_colors
395 self.Colors = self.color_scheme_table.active_colors
392 else:
396 else:
393 self.old_scheme = self.color_scheme_table.active_scheme_name
397 self.old_scheme = self.color_scheme_table.active_scheme_name
394 self.color_scheme_table.set_active_scheme('NoColor')
398 self.color_scheme_table.set_active_scheme('NoColor')
395 self.Colors = self.color_scheme_table.active_colors
399 self.Colors = self.color_scheme_table.active_colors
396
400
397 def stb2text(self, stb):
401 def stb2text(self, stb):
398 """Convert a structured traceback (a list) to a string."""
402 """Convert a structured traceback (a list) to a string."""
399 return '\n'.join(stb)
403 return '\n'.join(stb)
400
404
401 def text(self, etype, value, tb, tb_offset=None, context=5):
405 def text(self, etype, value, tb, tb_offset=None, context=5):
402 """Return formatted traceback.
406 """Return formatted traceback.
403
407
404 Subclasses may override this if they add extra arguments.
408 Subclasses may override this if they add extra arguments.
405 """
409 """
406 tb_list = self.structured_traceback(etype, value, tb,
410 tb_list = self.structured_traceback(etype, value, tb,
407 tb_offset, context)
411 tb_offset, context)
408 return self.stb2text(tb_list)
412 return self.stb2text(tb_list)
409
413
410 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
414 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
411 context=5, mode=None):
415 context=5, mode=None):
412 """Return a list of traceback frames.
416 """Return a list of traceback frames.
413
417
414 Must be implemented by each class.
418 Must be implemented by each class.
415 """
419 """
416 raise NotImplementedError()
420 raise NotImplementedError()
417
421
418
422
419 #---------------------------------------------------------------------------
423 #---------------------------------------------------------------------------
420 class ListTB(TBTools):
424 class ListTB(TBTools):
421 """Print traceback information from a traceback list, with optional color.
425 """Print traceback information from a traceback list, with optional color.
422
426
423 Calling requires 3 arguments: (etype, evalue, elist)
427 Calling requires 3 arguments: (etype, evalue, elist)
424 as would be obtained by::
428 as would be obtained by::
425
429
426 etype, evalue, tb = sys.exc_info()
430 etype, evalue, tb = sys.exc_info()
427 if tb:
431 if tb:
428 elist = traceback.extract_tb(tb)
432 elist = traceback.extract_tb(tb)
429 else:
433 else:
430 elist = None
434 elist = None
431
435
432 It can thus be used by programs which need to process the traceback before
436 It can thus be used by programs which need to process the traceback before
433 printing (such as console replacements based on the code module from the
437 printing (such as console replacements based on the code module from the
434 standard library).
438 standard library).
435
439
436 Because they are meant to be called without a full traceback (only a
440 Because they are meant to be called without a full traceback (only a
437 list), instances of this class can't call the interactive pdb debugger."""
441 list), instances of this class can't call the interactive pdb debugger."""
438
442
439 def __init__(self,color_scheme = 'NoColor', call_pdb=False, ostream=None):
443 def __init__(self,color_scheme = 'NoColor', call_pdb=False, ostream=None):
440 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
444 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
441 ostream=ostream)
445 ostream=ostream)
442
446
443 def __call__(self, etype, value, elist):
447 def __call__(self, etype, value, elist):
444 self.ostream.flush()
448 self.ostream.flush()
445 self.ostream.write(self.text(etype, value, elist))
449 self.ostream.write(self.text(etype, value, elist))
446 self.ostream.write('\n')
450 self.ostream.write('\n')
447
451
448 def structured_traceback(self, etype, value, elist, tb_offset=None,
452 def structured_traceback(self, etype, value, elist, tb_offset=None,
449 context=5):
453 context=5):
450 """Return a color formatted string with the traceback info.
454 """Return a color formatted string with the traceback info.
451
455
452 Parameters
456 Parameters
453 ----------
457 ----------
454 etype : exception type
458 etype : exception type
455 Type of the exception raised.
459 Type of the exception raised.
456
460
457 value : object
461 value : object
458 Data stored in the exception
462 Data stored in the exception
459
463
460 elist : list
464 elist : list
461 List of frames, see class docstring for details.
465 List of frames, see class docstring for details.
462
466
463 tb_offset : int, optional
467 tb_offset : int, optional
464 Number of frames in the traceback to skip. If not given, the
468 Number of frames in the traceback to skip. If not given, the
465 instance value is used (set in constructor).
469 instance value is used (set in constructor).
466
470
467 context : int, optional
471 context : int, optional
468 Number of lines of context information to print.
472 Number of lines of context information to print.
469
473
470 Returns
474 Returns
471 -------
475 -------
472 String with formatted exception.
476 String with formatted exception.
473 """
477 """
474 tb_offset = self.tb_offset if tb_offset is None else tb_offset
478 tb_offset = self.tb_offset if tb_offset is None else tb_offset
475 Colors = self.Colors
479 Colors = self.Colors
476 out_list = []
480 out_list = []
477 if elist:
481 if elist:
478
482
479 if tb_offset and len(elist) > tb_offset:
483 if tb_offset and len(elist) > tb_offset:
480 elist = elist[tb_offset:]
484 elist = elist[tb_offset:]
481
485
482 out_list.append('Traceback %s(most recent call last)%s:' %
486 out_list.append('Traceback %s(most recent call last)%s:' %
483 (Colors.normalEm, Colors.Normal) + '\n')
487 (Colors.normalEm, Colors.Normal) + '\n')
484 out_list.extend(self._format_list(elist))
488 out_list.extend(self._format_list(elist))
485 # The exception info should be a single entry in the list.
489 # The exception info should be a single entry in the list.
486 lines = ''.join(self._format_exception_only(etype, value))
490 lines = ''.join(self._format_exception_only(etype, value))
487 out_list.append(lines)
491 out_list.append(lines)
488
492
489 # Note: this code originally read:
493 # Note: this code originally read:
490
494
491 ## for line in lines[:-1]:
495 ## for line in lines[:-1]:
492 ## out_list.append(" "+line)
496 ## out_list.append(" "+line)
493 ## out_list.append(lines[-1])
497 ## out_list.append(lines[-1])
494
498
495 # This means it was indenting everything but the last line by a little
499 # This means it was indenting everything but the last line by a little
496 # bit. I've disabled this for now, but if we see ugliness somewhre we
500 # bit. I've disabled this for now, but if we see ugliness somewhre we
497 # can restore it.
501 # can restore it.
498
502
499 return out_list
503 return out_list
500
504
501 def _format_list(self, extracted_list):
505 def _format_list(self, extracted_list):
502 """Format a list of traceback entry tuples for printing.
506 """Format a list of traceback entry tuples for printing.
503
507
504 Given a list of tuples as returned by extract_tb() or
508 Given a list of tuples as returned by extract_tb() or
505 extract_stack(), return a list of strings ready for printing.
509 extract_stack(), return a list of strings ready for printing.
506 Each string in the resulting list corresponds to the item with the
510 Each string in the resulting list corresponds to the item with the
507 same index in the argument list. Each string ends in a newline;
511 same index in the argument list. Each string ends in a newline;
508 the strings may contain internal newlines as well, for those items
512 the strings may contain internal newlines as well, for those items
509 whose source text line is not None.
513 whose source text line is not None.
510
514
511 Lifted almost verbatim from traceback.py
515 Lifted almost verbatim from traceback.py
512 """
516 """
513
517
514 Colors = self.Colors
518 Colors = self.Colors
515 list = []
519 list = []
516 for filename, lineno, name, line in extracted_list[:-1]:
520 for filename, lineno, name, line in extracted_list[:-1]:
517 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
521 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
518 (Colors.filename, filename, Colors.Normal,
522 (Colors.filename, filename, Colors.Normal,
519 Colors.lineno, lineno, Colors.Normal,
523 Colors.lineno, lineno, Colors.Normal,
520 Colors.name, name, Colors.Normal)
524 Colors.name, name, Colors.Normal)
521 if line:
525 if line:
522 item += ' %s\n' % line.strip()
526 item += ' %s\n' % line.strip()
523 list.append(item)
527 list.append(item)
524 # Emphasize the last entry
528 # Emphasize the last entry
525 filename, lineno, name, line = extracted_list[-1]
529 filename, lineno, name, line = extracted_list[-1]
526 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
530 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
527 (Colors.normalEm,
531 (Colors.normalEm,
528 Colors.filenameEm, filename, Colors.normalEm,
532 Colors.filenameEm, filename, Colors.normalEm,
529 Colors.linenoEm, lineno, Colors.normalEm,
533 Colors.linenoEm, lineno, Colors.normalEm,
530 Colors.nameEm, name, Colors.normalEm,
534 Colors.nameEm, name, Colors.normalEm,
531 Colors.Normal)
535 Colors.Normal)
532 if line:
536 if line:
533 item += '%s %s%s\n' % (Colors.line, line.strip(),
537 item += '%s %s%s\n' % (Colors.line, line.strip(),
534 Colors.Normal)
538 Colors.Normal)
535 list.append(item)
539 list.append(item)
536 #from pprint import pformat; print 'LISTTB', pformat(list) # dbg
540 #from pprint import pformat; print 'LISTTB', pformat(list) # dbg
537 return list
541 return list
538
542
539 def _format_exception_only(self, etype, value):
543 def _format_exception_only(self, etype, value):
540 """Format the exception part of a traceback.
544 """Format the exception part of a traceback.
541
545
542 The arguments are the exception type and value such as given by
546 The arguments are the exception type and value such as given by
543 sys.exc_info()[:2]. The return value is a list of strings, each ending
547 sys.exc_info()[:2]. The return value is a list of strings, each ending
544 in a newline. Normally, the list contains a single string; however,
548 in a newline. Normally, the list contains a single string; however,
545 for SyntaxError exceptions, it contains several lines that (when
549 for SyntaxError exceptions, it contains several lines that (when
546 printed) display detailed information about where the syntax error
550 printed) display detailed information about where the syntax error
547 occurred. The message indicating which exception occurred is the
551 occurred. The message indicating which exception occurred is the
548 always last string in the list.
552 always last string in the list.
549
553
550 Also lifted nearly verbatim from traceback.py
554 Also lifted nearly verbatim from traceback.py
551 """
555 """
552 have_filedata = False
556 have_filedata = False
553 Colors = self.Colors
557 Colors = self.Colors
554 list = []
558 list = []
555 stype = Colors.excName + etype.__name__ + Colors.Normal
559 stype = Colors.excName + etype.__name__ + Colors.Normal
556 if value is None:
560 if value is None:
557 # Not sure if this can still happen in Python 2.6 and above
561 # Not sure if this can still happen in Python 2.6 and above
558 list.append( py3compat.cast_unicode(stype) + '\n')
562 list.append( py3compat.cast_unicode(stype) + '\n')
559 else:
563 else:
560 if issubclass(etype, SyntaxError):
564 if issubclass(etype, SyntaxError):
561 have_filedata = True
565 have_filedata = True
562 #print 'filename is',filename # dbg
566 #print 'filename is',filename # dbg
563 if not value.filename: value.filename = "<string>"
567 if not value.filename: value.filename = "<string>"
564 if value.lineno:
568 if value.lineno:
565 lineno = value.lineno
569 lineno = value.lineno
566 textline = ulinecache.getline(value.filename, value.lineno)
570 textline = ulinecache.getline(value.filename, value.lineno)
567 else:
571 else:
568 lineno = 'unknown'
572 lineno = 'unknown'
569 textline = ''
573 textline = ''
570 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
574 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
571 (Colors.normalEm,
575 (Colors.normalEm,
572 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
576 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
573 Colors.linenoEm, lineno, Colors.Normal ))
577 Colors.linenoEm, lineno, Colors.Normal ))
574 if textline == '':
578 if textline == '':
575 textline = py3compat.cast_unicode(value.text, "utf-8")
579 textline = py3compat.cast_unicode(value.text, "utf-8")
576
580
577 if textline is not None:
581 if textline is not None:
578 i = 0
582 i = 0
579 while i < len(textline) and textline[i].isspace():
583 while i < len(textline) and textline[i].isspace():
580 i += 1
584 i += 1
581 list.append('%s %s%s\n' % (Colors.line,
585 list.append('%s %s%s\n' % (Colors.line,
582 textline.strip(),
586 textline.strip(),
583 Colors.Normal))
587 Colors.Normal))
584 if value.offset is not None:
588 if value.offset is not None:
585 s = ' '
589 s = ' '
586 for c in textline[i:value.offset-1]:
590 for c in textline[i:value.offset-1]:
587 if c.isspace():
591 if c.isspace():
588 s += c
592 s += c
589 else:
593 else:
590 s += ' '
594 s += ' '
591 list.append('%s%s^%s\n' % (Colors.caret, s,
595 list.append('%s%s^%s\n' % (Colors.caret, s,
592 Colors.Normal) )
596 Colors.Normal) )
593
597
594 try:
598 try:
595 s = value.msg
599 s = value.msg
596 except Exception:
600 except Exception:
597 s = self._some_str(value)
601 s = self._some_str(value)
598 if s:
602 if s:
599 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
603 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
600 Colors.Normal, s))
604 Colors.Normal, s))
601 else:
605 else:
602 list.append('%s\n' % str(stype))
606 list.append('%s\n' % str(stype))
603
607
604 # sync with user hooks
608 # sync with user hooks
605 if have_filedata:
609 if have_filedata:
606 ipinst = get_ipython()
610 ipinst = get_ipython()
607 if ipinst is not None:
611 if ipinst is not None:
608 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
612 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
609
613
610 return list
614 return list
611
615
612 def get_exception_only(self, etype, value):
616 def get_exception_only(self, etype, value):
613 """Only print the exception type and message, without a traceback.
617 """Only print the exception type and message, without a traceback.
614
618
615 Parameters
619 Parameters
616 ----------
620 ----------
617 etype : exception type
621 etype : exception type
618 value : exception value
622 value : exception value
619 """
623 """
620 return ListTB.structured_traceback(self, etype, value, [])
624 return ListTB.structured_traceback(self, etype, value, [])
621
625
622
626
623 def show_exception_only(self, etype, evalue):
627 def show_exception_only(self, etype, evalue):
624 """Only print the exception type and message, without a traceback.
628 """Only print the exception type and message, without a traceback.
625
629
626 Parameters
630 Parameters
627 ----------
631 ----------
628 etype : exception type
632 etype : exception type
629 value : exception value
633 value : exception value
630 """
634 """
631 # This method needs to use __call__ from *this* class, not the one from
635 # This method needs to use __call__ from *this* class, not the one from
632 # a subclass whose signature or behavior may be different
636 # a subclass whose signature or behavior may be different
633 ostream = self.ostream
637 ostream = self.ostream
634 ostream.flush()
638 ostream.flush()
635 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
639 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
636 ostream.flush()
640 ostream.flush()
637
641
638 def _some_str(self, value):
642 def _some_str(self, value):
639 # Lifted from traceback.py
643 # Lifted from traceback.py
640 try:
644 try:
641 return str(value)
645 return str(value)
642 except:
646 except:
643 return '<unprintable %s object>' % type(value).__name__
647 return '<unprintable %s object>' % type(value).__name__
644
648
645 #----------------------------------------------------------------------------
649 #----------------------------------------------------------------------------
646 class VerboseTB(TBTools):
650 class VerboseTB(TBTools):
647 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
651 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
648 of HTML. Requires inspect and pydoc. Crazy, man.
652 of HTML. Requires inspect and pydoc. Crazy, man.
649
653
650 Modified version which optionally strips the topmost entries from the
654 Modified version which optionally strips the topmost entries from the
651 traceback, to be used with alternate interpreters (because their own code
655 traceback, to be used with alternate interpreters (because their own code
652 would appear in the traceback)."""
656 would appear in the traceback)."""
653
657
654 def __init__(self,color_scheme = 'Linux', call_pdb=False, ostream=None,
658 def __init__(self,color_scheme = 'Linux', call_pdb=False, ostream=None,
655 tb_offset=0, long_header=False, include_vars=True,
659 tb_offset=0, long_header=False, include_vars=True,
656 check_cache=None):
660 check_cache=None):
657 """Specify traceback offset, headers and color scheme.
661 """Specify traceback offset, headers and color scheme.
658
662
659 Define how many frames to drop from the tracebacks. Calling it with
663 Define how many frames to drop from the tracebacks. Calling it with
660 tb_offset=1 allows use of this handler in interpreters which will have
664 tb_offset=1 allows use of this handler in interpreters which will have
661 their own code at the top of the traceback (VerboseTB will first
665 their own code at the top of the traceback (VerboseTB will first
662 remove that frame before printing the traceback info)."""
666 remove that frame before printing the traceback info)."""
663 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
667 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
664 ostream=ostream)
668 ostream=ostream)
665 self.tb_offset = tb_offset
669 self.tb_offset = tb_offset
666 self.long_header = long_header
670 self.long_header = long_header
667 self.include_vars = include_vars
671 self.include_vars = include_vars
668 # By default we use linecache.checkcache, but the user can provide a
672 # By default we use linecache.checkcache, but the user can provide a
669 # different check_cache implementation. This is used by the IPython
673 # different check_cache implementation. This is used by the IPython
670 # kernel to provide tracebacks for interactive code that is cached,
674 # kernel to provide tracebacks for interactive code that is cached,
671 # by a compiler instance that flushes the linecache but preserves its
675 # by a compiler instance that flushes the linecache but preserves its
672 # own code cache.
676 # own code cache.
673 if check_cache is None:
677 if check_cache is None:
674 check_cache = linecache.checkcache
678 check_cache = linecache.checkcache
675 self.check_cache = check_cache
679 self.check_cache = check_cache
676
680
677 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
681 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
678 context=5):
682 context=5):
679 """Return a nice text document describing the traceback."""
683 """Return a nice text document describing the traceback."""
680
684
681 tb_offset = self.tb_offset if tb_offset is None else tb_offset
685 tb_offset = self.tb_offset if tb_offset is None else tb_offset
682
686
683 # some locals
687 # some locals
684 try:
688 try:
685 etype = etype.__name__
689 etype = etype.__name__
686 except AttributeError:
690 except AttributeError:
687 pass
691 pass
688 Colors = self.Colors # just a shorthand + quicker name lookup
692 Colors = self.Colors # just a shorthand + quicker name lookup
689 ColorsNormal = Colors.Normal # used a lot
693 ColorsNormal = Colors.Normal # used a lot
690 col_scheme = self.color_scheme_table.active_scheme_name
694 col_scheme = self.color_scheme_table.active_scheme_name
691 indent = ' '*INDENT_SIZE
695 indent = ' '*INDENT_SIZE
692 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
696 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
693 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
697 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
694 exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal)
698 exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal)
695
699
696 # some internal-use functions
700 # some internal-use functions
697 def text_repr(value):
701 def text_repr(value):
698 """Hopefully pretty robust repr equivalent."""
702 """Hopefully pretty robust repr equivalent."""
699 # this is pretty horrible but should always return *something*
703 # this is pretty horrible but should always return *something*
700 try:
704 try:
701 return pydoc.text.repr(value)
705 return pydoc.text.repr(value)
702 except KeyboardInterrupt:
706 except KeyboardInterrupt:
703 raise
707 raise
704 except:
708 except:
705 try:
709 try:
706 return repr(value)
710 return repr(value)
707 except KeyboardInterrupt:
711 except KeyboardInterrupt:
708 raise
712 raise
709 except:
713 except:
710 try:
714 try:
711 # all still in an except block so we catch
715 # all still in an except block so we catch
712 # getattr raising
716 # getattr raising
713 name = getattr(value, '__name__', None)
717 name = getattr(value, '__name__', None)
714 if name:
718 if name:
715 # ick, recursion
719 # ick, recursion
716 return text_repr(name)
720 return text_repr(name)
717 klass = getattr(value, '__class__', None)
721 klass = getattr(value, '__class__', None)
718 if klass:
722 if klass:
719 return '%s instance' % text_repr(klass)
723 return '%s instance' % text_repr(klass)
720 except KeyboardInterrupt:
724 except KeyboardInterrupt:
721 raise
725 raise
722 except:
726 except:
723 return 'UNRECOVERABLE REPR FAILURE'
727 return 'UNRECOVERABLE REPR FAILURE'
724 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
728 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
725 def nullrepr(value, repr=text_repr): return ''
729 def nullrepr(value, repr=text_repr): return ''
726
730
727 # meat of the code begins
731 # meat of the code begins
728 try:
732 try:
729 etype = etype.__name__
733 etype = etype.__name__
730 except AttributeError:
734 except AttributeError:
731 pass
735 pass
732
736
733 if self.long_header:
737 if self.long_header:
734 # Header with the exception type, python version, and date
738 # Header with the exception type, python version, and date
735 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
739 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
736 date = time.ctime(time.time())
740 date = time.ctime(time.time())
737
741
738 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
742 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
739 exc, ' '*(75-len(str(etype))-len(pyver)),
743 exc, ' '*(75-len(str(etype))-len(pyver)),
740 pyver, date.rjust(75) )
744 pyver, date.rjust(75) )
741 head += "\nA problem occured executing Python code. Here is the sequence of function"\
745 head += "\nA problem occured executing Python code. Here is the sequence of function"\
742 "\ncalls leading up to the error, with the most recent (innermost) call last."
746 "\ncalls leading up to the error, with the most recent (innermost) call last."
743 else:
747 else:
744 # Simplified header
748 # Simplified header
745 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
749 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
746 'Traceback (most recent call last)'.\
750 'Traceback (most recent call last)'.\
747 rjust(75 - len(str(etype)) ) )
751 rjust(75 - len(str(etype)) ) )
748 frames = []
752 frames = []
749 # Flush cache before calling inspect. This helps alleviate some of the
753 # Flush cache before calling inspect. This helps alleviate some of the
750 # problems with python 2.3's inspect.py.
754 # problems with python 2.3's inspect.py.
751 ##self.check_cache()
755 ##self.check_cache()
752 # Drop topmost frames if requested
756 # Drop topmost frames if requested
753 try:
757 try:
754 # Try the default getinnerframes and Alex's: Alex's fixes some
758 # Try the default getinnerframes and Alex's: Alex's fixes some
755 # problems, but it generates empty tracebacks for console errors
759 # problems, but it generates empty tracebacks for console errors
756 # (5 blanks lines) where none should be returned.
760 # (5 blanks lines) where none should be returned.
757 #records = inspect.getinnerframes(etb, context)[tb_offset:]
761 #records = inspect.getinnerframes(etb, context)[tb_offset:]
758 #print 'python records:', records # dbg
762 #print 'python records:', records # dbg
759 records = _fixed_getinnerframes(etb, context, tb_offset)
763 records = _fixed_getinnerframes(etb, context, tb_offset)
760 #print 'alex records:', records # dbg
764 #print 'alex records:', records # dbg
761 except:
765 except:
762
766
763 # FIXME: I've been getting many crash reports from python 2.3
767 # FIXME: I've been getting many crash reports from python 2.3
764 # users, traceable to inspect.py. If I can find a small test-case
768 # users, traceable to inspect.py. If I can find a small test-case
765 # to reproduce this, I should either write a better workaround or
769 # to reproduce this, I should either write a better workaround or
766 # file a bug report against inspect (if that's the real problem).
770 # file a bug report against inspect (if that's the real problem).
767 # So far, I haven't been able to find an isolated example to
771 # So far, I haven't been able to find an isolated example to
768 # reproduce the problem.
772 # reproduce the problem.
769 inspect_error()
773 inspect_error()
770 traceback.print_exc(file=self.ostream)
774 traceback.print_exc(file=self.ostream)
771 info('\nUnfortunately, your original traceback can not be constructed.\n')
775 info('\nUnfortunately, your original traceback can not be constructed.\n')
772 return ''
776 return ''
773
777
774 # build some color string templates outside these nested loops
778 # build some color string templates outside these nested loops
775 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
779 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
776 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
780 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
777 ColorsNormal)
781 ColorsNormal)
778 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
782 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
779 (Colors.vName, Colors.valEm, ColorsNormal)
783 (Colors.vName, Colors.valEm, ColorsNormal)
780 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
784 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
781 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
785 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
782 Colors.vName, ColorsNormal)
786 Colors.vName, ColorsNormal)
783 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
787 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
784 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
788 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
785 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
789 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
786 ColorsNormal)
790 ColorsNormal)
787
791
788 # now, loop over all records printing context and info
792 # now, loop over all records printing context and info
789 abspath = os.path.abspath
793 abspath = os.path.abspath
790 for frame, file, lnum, func, lines, index in records:
794 for frame, file, lnum, func, lines, index in records:
791 #print '*** record:',file,lnum,func,lines,index # dbg
795 #print '*** record:',file,lnum,func,lines,index # dbg
792 if not file:
796 if not file:
793 file = '?'
797 file = '?'
794 elif not(file.startswith(str("<")) and file.endswith(str(">"))):
798 elif not(file.startswith(str("<")) and file.endswith(str(">"))):
795 # Guess that filenames like <string> aren't real filenames, so
799 # Guess that filenames like <string> aren't real filenames, so
796 # don't call abspath on them.
800 # don't call abspath on them.
797 try:
801 try:
798 file = abspath(file)
802 file = abspath(file)
799 except OSError:
803 except OSError:
800 # Not sure if this can still happen: abspath now works with
804 # Not sure if this can still happen: abspath now works with
801 # file names like <string>
805 # file names like <string>
802 pass
806 pass
803 file = py3compat.cast_unicode(file, util_path.fs_encoding)
807 file = py3compat.cast_unicode(file, util_path.fs_encoding)
804 link = tpl_link % file
808 link = tpl_link % file
805 args, varargs, varkw, locals = inspect.getargvalues(frame)
809 args, varargs, varkw, locals = inspect.getargvalues(frame)
806
810
807 if func == '?':
811 if func == '?':
808 call = ''
812 call = ''
809 else:
813 else:
810 # Decide whether to include variable details or not
814 # Decide whether to include variable details or not
811 var_repr = self.include_vars and eqrepr or nullrepr
815 var_repr = self.include_vars and eqrepr or nullrepr
812 try:
816 try:
813 call = tpl_call % (func,inspect.formatargvalues(args,
817 call = tpl_call % (func,inspect.formatargvalues(args,
814 varargs, varkw,
818 varargs, varkw,
815 locals,formatvalue=var_repr))
819 locals,formatvalue=var_repr))
816 except KeyError:
820 except KeyError:
817 # This happens in situations like errors inside generator
821 # This happens in situations like errors inside generator
818 # expressions, where local variables are listed in the
822 # expressions, where local variables are listed in the
819 # line, but can't be extracted from the frame. I'm not
823 # line, but can't be extracted from the frame. I'm not
820 # 100% sure this isn't actually a bug in inspect itself,
824 # 100% sure this isn't actually a bug in inspect itself,
821 # but since there's no info for us to compute with, the
825 # but since there's no info for us to compute with, the
822 # best we can do is report the failure and move on. Here
826 # best we can do is report the failure and move on. Here
823 # we must *not* call any traceback construction again,
827 # we must *not* call any traceback construction again,
824 # because that would mess up use of %debug later on. So we
828 # because that would mess up use of %debug later on. So we
825 # simply report the failure and move on. The only
829 # simply report the failure and move on. The only
826 # limitation will be that this frame won't have locals
830 # limitation will be that this frame won't have locals
827 # listed in the call signature. Quite subtle problem...
831 # listed in the call signature. Quite subtle problem...
828 # I can't think of a good way to validate this in a unit
832 # I can't think of a good way to validate this in a unit
829 # test, but running a script consisting of:
833 # test, but running a script consisting of:
830 # dict( (k,v.strip()) for (k,v) in range(10) )
834 # dict( (k,v.strip()) for (k,v) in range(10) )
831 # will illustrate the error, if this exception catch is
835 # will illustrate the error, if this exception catch is
832 # disabled.
836 # disabled.
833 call = tpl_call_fail % func
837 call = tpl_call_fail % func
834
838
835 # Don't attempt to tokenize binary files.
839 # Don't attempt to tokenize binary files.
836 if file.endswith(('.so', '.pyd', '.dll')):
840 if file.endswith(('.so', '.pyd', '.dll')):
837 frames.append('%s %s\n' % (link,call))
841 frames.append('%s %s\n' % (link,call))
838 continue
842 continue
839 elif file.endswith(('.pyc','.pyo')):
843 elif file.endswith(('.pyc','.pyo')):
840 # Look up the corresponding source file.
844 # Look up the corresponding source file.
841 file = openpy.source_from_cache(file)
845 file = openpy.source_from_cache(file)
842
846
843 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
847 def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
844 line = getline(file, lnum[0])
848 line = getline(file, lnum[0])
845 lnum[0] += 1
849 lnum[0] += 1
846 return line
850 return line
847
851
848 # Build the list of names on this line of code where the exception
852 # Build the list of names on this line of code where the exception
849 # occurred.
853 # occurred.
850 try:
854 try:
851 names = []
855 names = []
852 name_cont = False
856 name_cont = False
853
857
854 for token_type, token, start, end, line in generate_tokens(linereader):
858 for token_type, token, start, end, line in generate_tokens(linereader):
855 # build composite names
859 # build composite names
856 if token_type == tokenize.NAME and token not in keyword.kwlist:
860 if token_type == tokenize.NAME and token not in keyword.kwlist:
857 if name_cont:
861 if name_cont:
858 # Continuation of a dotted name
862 # Continuation of a dotted name
859 try:
863 try:
860 names[-1].append(token)
864 names[-1].append(token)
861 except IndexError:
865 except IndexError:
862 names.append([token])
866 names.append([token])
863 name_cont = False
867 name_cont = False
864 else:
868 else:
865 # Regular new names. We append everything, the caller
869 # Regular new names. We append everything, the caller
866 # will be responsible for pruning the list later. It's
870 # will be responsible for pruning the list later. It's
867 # very tricky to try to prune as we go, b/c composite
871 # very tricky to try to prune as we go, b/c composite
868 # names can fool us. The pruning at the end is easy
872 # names can fool us. The pruning at the end is easy
869 # to do (or the caller can print a list with repeated
873 # to do (or the caller can print a list with repeated
870 # names if so desired.
874 # names if so desired.
871 names.append([token])
875 names.append([token])
872 elif token == '.':
876 elif token == '.':
873 name_cont = True
877 name_cont = True
874 elif token_type == tokenize.NEWLINE:
878 elif token_type == tokenize.NEWLINE:
875 break
879 break
876
880
877 except (IndexError, UnicodeDecodeError):
881 except (IndexError, UnicodeDecodeError):
878 # signals exit of tokenizer
882 # signals exit of tokenizer
879 pass
883 pass
880 except tokenize.TokenError as msg:
884 except tokenize.TokenError as msg:
881 _m = ("An unexpected error occurred while tokenizing input\n"
885 _m = ("An unexpected error occurred while tokenizing input\n"
882 "The following traceback may be corrupted or invalid\n"
886 "The following traceback may be corrupted or invalid\n"
883 "The error message is: %s\n" % msg)
887 "The error message is: %s\n" % msg)
884 error(_m)
888 error(_m)
885
889
886 # Join composite names (e.g. "dict.fromkeys")
890 # Join composite names (e.g. "dict.fromkeys")
887 names = ['.'.join(n) for n in names]
891 names = ['.'.join(n) for n in names]
888 # prune names list of duplicates, but keep the right order
892 # prune names list of duplicates, but keep the right order
889 unique_names = uniq_stable(names)
893 unique_names = uniq_stable(names)
890
894
891 # Start loop over vars
895 # Start loop over vars
892 lvals = []
896 lvals = []
893 if self.include_vars:
897 if self.include_vars:
894 for name_full in unique_names:
898 for name_full in unique_names:
895 name_base = name_full.split('.',1)[0]
899 name_base = name_full.split('.',1)[0]
896 if name_base in frame.f_code.co_varnames:
900 if name_base in frame.f_code.co_varnames:
897 if name_base in locals:
901 if name_base in locals:
898 try:
902 try:
899 value = repr(eval(name_full,locals))
903 value = repr(eval(name_full,locals))
900 except:
904 except:
901 value = undefined
905 value = undefined
902 else:
906 else:
903 value = undefined
907 value = undefined
904 name = tpl_local_var % name_full
908 name = tpl_local_var % name_full
905 else:
909 else:
906 if name_base in frame.f_globals:
910 if name_base in frame.f_globals:
907 try:
911 try:
908 value = repr(eval(name_full,frame.f_globals))
912 value = repr(eval(name_full,frame.f_globals))
909 except:
913 except:
910 value = undefined
914 value = undefined
911 else:
915 else:
912 value = undefined
916 value = undefined
913 name = tpl_global_var % name_full
917 name = tpl_global_var % name_full
914 lvals.append(tpl_name_val % (name,value))
918 lvals.append(tpl_name_val % (name,value))
915 if lvals:
919 if lvals:
916 lvals = '%s%s' % (indent,em_normal.join(lvals))
920 lvals = '%s%s' % (indent,em_normal.join(lvals))
917 else:
921 else:
918 lvals = ''
922 lvals = ''
919
923
920 level = '%s %s\n' % (link,call)
924 level = '%s %s\n' % (link,call)
921
925
922 if index is None:
926 if index is None:
923 frames.append(level)
927 frames.append(level)
924 else:
928 else:
925 frames.append('%s%s' % (level,''.join(
929 frames.append('%s%s' % (level,''.join(
926 _format_traceback_lines(lnum,index,lines,Colors,lvals,
930 _format_traceback_lines(lnum,index,lines,Colors,lvals,
927 col_scheme))))
931 col_scheme))))
928
932
929 # Get (safely) a string form of the exception info
933 # Get (safely) a string form of the exception info
930 try:
934 try:
931 etype_str,evalue_str = map(str,(etype,evalue))
935 etype_str,evalue_str = map(str,(etype,evalue))
932 except:
936 except:
933 # User exception is improperly defined.
937 # User exception is improperly defined.
934 etype,evalue = str,sys.exc_info()[:2]
938 etype,evalue = str,sys.exc_info()[:2]
935 etype_str,evalue_str = map(str,(etype,evalue))
939 etype_str,evalue_str = map(str,(etype,evalue))
936 # ... and format it
940 # ... and format it
937 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
941 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
938 ColorsNormal, py3compat.cast_unicode(evalue_str))]
942 ColorsNormal, py3compat.cast_unicode(evalue_str))]
939 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
943 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
940 try:
944 try:
941 names = [w for w in dir(evalue) if isinstance(w, basestring)]
945 names = [w for w in dir(evalue) if isinstance(w, basestring)]
942 except:
946 except:
943 # Every now and then, an object with funny inernals blows up
947 # Every now and then, an object with funny inernals blows up
944 # when dir() is called on it. We do the best we can to report
948 # when dir() is called on it. We do the best we can to report
945 # the problem and continue
949 # the problem and continue
946 _m = '%sException reporting error (object with broken dir())%s:'
950 _m = '%sException reporting error (object with broken dir())%s:'
947 exception.append(_m % (Colors.excName,ColorsNormal))
951 exception.append(_m % (Colors.excName,ColorsNormal))
948 etype_str,evalue_str = map(str,sys.exc_info()[:2])
952 etype_str,evalue_str = map(str,sys.exc_info()[:2])
949 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
953 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
950 ColorsNormal, py3compat.cast_unicode(evalue_str)))
954 ColorsNormal, py3compat.cast_unicode(evalue_str)))
951 names = []
955 names = []
952 for name in names:
956 for name in names:
953 value = text_repr(getattr(evalue, name))
957 value = text_repr(getattr(evalue, name))
954 exception.append('\n%s%s = %s' % (indent, name, value))
958 exception.append('\n%s%s = %s' % (indent, name, value))
955
959
956 # vds: >>
960 # vds: >>
957 if records:
961 if records:
958 filepath, lnum = records[-1][1:3]
962 filepath, lnum = records[-1][1:3]
959 #print "file:", str(file), "linenb", str(lnum) # dbg
963 #print "file:", str(file), "linenb", str(lnum) # dbg
960 filepath = os.path.abspath(filepath)
964 filepath = os.path.abspath(filepath)
961 ipinst = get_ipython()
965 ipinst = get_ipython()
962 if ipinst is not None:
966 if ipinst is not None:
963 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
967 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
964 # vds: <<
968 # vds: <<
965
969
966 # return all our info assembled as a single string
970 # return all our info assembled as a single string
967 # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
971 # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
968 return [head] + frames + [''.join(exception[0])]
972 return [head] + frames + [''.join(exception[0])]
969
973
970 def debugger(self,force=False):
974 def debugger(self,force=False):
971 """Call up the pdb debugger if desired, always clean up the tb
975 """Call up the pdb debugger if desired, always clean up the tb
972 reference.
976 reference.
973
977
974 Keywords:
978 Keywords:
975
979
976 - force(False): by default, this routine checks the instance call_pdb
980 - force(False): by default, this routine checks the instance call_pdb
977 flag and does not actually invoke the debugger if the flag is false.
981 flag and does not actually invoke the debugger if the flag is false.
978 The 'force' option forces the debugger to activate even if the flag
982 The 'force' option forces the debugger to activate even if the flag
979 is false.
983 is false.
980
984
981 If the call_pdb flag is set, the pdb interactive debugger is
985 If the call_pdb flag is set, the pdb interactive debugger is
982 invoked. In all cases, the self.tb reference to the current traceback
986 invoked. In all cases, the self.tb reference to the current traceback
983 is deleted to prevent lingering references which hamper memory
987 is deleted to prevent lingering references which hamper memory
984 management.
988 management.
985
989
986 Note that each call to pdb() does an 'import readline', so if your app
990 Note that each call to pdb() does an 'import readline', so if your app
987 requires a special setup for the readline completers, you'll have to
991 requires a special setup for the readline completers, you'll have to
988 fix that by hand after invoking the exception handler."""
992 fix that by hand after invoking the exception handler."""
989
993
990 if force or self.call_pdb:
994 if force or self.call_pdb:
991 if self.pdb is None:
995 if self.pdb is None:
992 self.pdb = debugger.Pdb(
996 self.pdb = debugger.Pdb(
993 self.color_scheme_table.active_scheme_name)
997 self.color_scheme_table.active_scheme_name)
994 # the system displayhook may have changed, restore the original
998 # the system displayhook may have changed, restore the original
995 # for pdb
999 # for pdb
996 display_trap = DisplayTrap(hook=sys.__displayhook__)
1000 display_trap = DisplayTrap(hook=sys.__displayhook__)
997 with display_trap:
1001 with display_trap:
998 self.pdb.reset()
1002 self.pdb.reset()
999 # Find the right frame so we don't pop up inside ipython itself
1003 # Find the right frame so we don't pop up inside ipython itself
1000 if hasattr(self,'tb') and self.tb is not None:
1004 if hasattr(self,'tb') and self.tb is not None:
1001 etb = self.tb
1005 etb = self.tb
1002 else:
1006 else:
1003 etb = self.tb = sys.last_traceback
1007 etb = self.tb = sys.last_traceback
1004 while self.tb is not None and self.tb.tb_next is not None:
1008 while self.tb is not None and self.tb.tb_next is not None:
1005 self.tb = self.tb.tb_next
1009 self.tb = self.tb.tb_next
1006 if etb and etb.tb_next:
1010 if etb and etb.tb_next:
1007 etb = etb.tb_next
1011 etb = etb.tb_next
1008 self.pdb.botframe = etb.tb_frame
1012 self.pdb.botframe = etb.tb_frame
1009 self.pdb.interaction(self.tb.tb_frame, self.tb)
1013 self.pdb.interaction(self.tb.tb_frame, self.tb)
1010
1014
1011 if hasattr(self,'tb'):
1015 if hasattr(self,'tb'):
1012 del self.tb
1016 del self.tb
1013
1017
1014 def handler(self, info=None):
1018 def handler(self, info=None):
1015 (etype, evalue, etb) = info or sys.exc_info()
1019 (etype, evalue, etb) = info or sys.exc_info()
1016 self.tb = etb
1020 self.tb = etb
1017 ostream = self.ostream
1021 ostream = self.ostream
1018 ostream.flush()
1022 ostream.flush()
1019 ostream.write(self.text(etype, evalue, etb))
1023 ostream.write(self.text(etype, evalue, etb))
1020 ostream.write('\n')
1024 ostream.write('\n')
1021 ostream.flush()
1025 ostream.flush()
1022
1026
1023 # Changed so an instance can just be called as VerboseTB_inst() and print
1027 # Changed so an instance can just be called as VerboseTB_inst() and print
1024 # out the right info on its own.
1028 # out the right info on its own.
1025 def __call__(self, etype=None, evalue=None, etb=None):
1029 def __call__(self, etype=None, evalue=None, etb=None):
1026 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1030 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1027 if etb is None:
1031 if etb is None:
1028 self.handler()
1032 self.handler()
1029 else:
1033 else:
1030 self.handler((etype, evalue, etb))
1034 self.handler((etype, evalue, etb))
1031 try:
1035 try:
1032 self.debugger()
1036 self.debugger()
1033 except KeyboardInterrupt:
1037 except KeyboardInterrupt:
1034 print "\nKeyboardInterrupt"
1038 print "\nKeyboardInterrupt"
1035
1039
1036 #----------------------------------------------------------------------------
1040 #----------------------------------------------------------------------------
1037 class FormattedTB(VerboseTB, ListTB):
1041 class FormattedTB(VerboseTB, ListTB):
1038 """Subclass ListTB but allow calling with a traceback.
1042 """Subclass ListTB but allow calling with a traceback.
1039
1043
1040 It can thus be used as a sys.excepthook for Python > 2.1.
1044 It can thus be used as a sys.excepthook for Python > 2.1.
1041
1045
1042 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1046 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1043
1047
1044 Allows a tb_offset to be specified. This is useful for situations where
1048 Allows a tb_offset to be specified. This is useful for situations where
1045 one needs to remove a number of topmost frames from the traceback (such as
1049 one needs to remove a number of topmost frames from the traceback (such as
1046 occurs with python programs that themselves execute other python code,
1050 occurs with python programs that themselves execute other python code,
1047 like Python shells). """
1051 like Python shells). """
1048
1052
1049 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1053 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1050 ostream=None,
1054 ostream=None,
1051 tb_offset=0, long_header=False, include_vars=False,
1055 tb_offset=0, long_header=False, include_vars=False,
1052 check_cache=None):
1056 check_cache=None):
1053
1057
1054 # NEVER change the order of this list. Put new modes at the end:
1058 # NEVER change the order of this list. Put new modes at the end:
1055 self.valid_modes = ['Plain','Context','Verbose']
1059 self.valid_modes = ['Plain','Context','Verbose']
1056 self.verbose_modes = self.valid_modes[1:3]
1060 self.verbose_modes = self.valid_modes[1:3]
1057
1061
1058 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1062 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1059 ostream=ostream, tb_offset=tb_offset,
1063 ostream=ostream, tb_offset=tb_offset,
1060 long_header=long_header, include_vars=include_vars,
1064 long_header=long_header, include_vars=include_vars,
1061 check_cache=check_cache)
1065 check_cache=check_cache)
1062
1066
1063 # Different types of tracebacks are joined with different separators to
1067 # Different types of tracebacks are joined with different separators to
1064 # form a single string. They are taken from this dict
1068 # form a single string. They are taken from this dict
1065 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1069 self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
1066 # set_mode also sets the tb_join_char attribute
1070 # set_mode also sets the tb_join_char attribute
1067 self.set_mode(mode)
1071 self.set_mode(mode)
1068
1072
1069 def _extract_tb(self,tb):
1073 def _extract_tb(self,tb):
1070 if tb:
1074 if tb:
1071 return traceback.extract_tb(tb)
1075 return traceback.extract_tb(tb)
1072 else:
1076 else:
1073 return None
1077 return None
1074
1078
1075 def structured_traceback(self, etype, value, tb, tb_offset=None, context=5):
1079 def structured_traceback(self, etype, value, tb, tb_offset=None, context=5):
1076 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1080 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1077 mode = self.mode
1081 mode = self.mode
1078 if mode in self.verbose_modes:
1082 if mode in self.verbose_modes:
1079 # Verbose modes need a full traceback
1083 # Verbose modes need a full traceback
1080 return VerboseTB.structured_traceback(
1084 return VerboseTB.structured_traceback(
1081 self, etype, value, tb, tb_offset, context
1085 self, etype, value, tb, tb_offset, context
1082 )
1086 )
1083 else:
1087 else:
1084 # We must check the source cache because otherwise we can print
1088 # We must check the source cache because otherwise we can print
1085 # out-of-date source code.
1089 # out-of-date source code.
1086 self.check_cache()
1090 self.check_cache()
1087 # Now we can extract and format the exception
1091 # Now we can extract and format the exception
1088 elist = self._extract_tb(tb)
1092 elist = self._extract_tb(tb)
1089 return ListTB.structured_traceback(
1093 return ListTB.structured_traceback(
1090 self, etype, value, elist, tb_offset, context
1094 self, etype, value, elist, tb_offset, context
1091 )
1095 )
1092
1096
1093 def stb2text(self, stb):
1097 def stb2text(self, stb):
1094 """Convert a structured traceback (a list) to a string."""
1098 """Convert a structured traceback (a list) to a string."""
1095 return self.tb_join_char.join(stb)
1099 return self.tb_join_char.join(stb)
1096
1100
1097
1101
1098 def set_mode(self,mode=None):
1102 def set_mode(self,mode=None):
1099 """Switch to the desired mode.
1103 """Switch to the desired mode.
1100
1104
1101 If mode is not specified, cycles through the available modes."""
1105 If mode is not specified, cycles through the available modes."""
1102
1106
1103 if not mode:
1107 if not mode:
1104 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
1108 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
1105 len(self.valid_modes)
1109 len(self.valid_modes)
1106 self.mode = self.valid_modes[new_idx]
1110 self.mode = self.valid_modes[new_idx]
1107 elif mode not in self.valid_modes:
1111 elif mode not in self.valid_modes:
1108 raise ValueError('Unrecognized mode in FormattedTB: <'+mode+'>\n'
1112 raise ValueError('Unrecognized mode in FormattedTB: <'+mode+'>\n'
1109 'Valid modes: '+str(self.valid_modes))
1113 'Valid modes: '+str(self.valid_modes))
1110 else:
1114 else:
1111 self.mode = mode
1115 self.mode = mode
1112 # include variable details only in 'Verbose' mode
1116 # include variable details only in 'Verbose' mode
1113 self.include_vars = (self.mode == self.valid_modes[2])
1117 self.include_vars = (self.mode == self.valid_modes[2])
1114 # Set the join character for generating text tracebacks
1118 # Set the join character for generating text tracebacks
1115 self.tb_join_char = self._join_chars[self.mode]
1119 self.tb_join_char = self._join_chars[self.mode]
1116
1120
1117 # some convenient shorcuts
1121 # some convenient shorcuts
1118 def plain(self):
1122 def plain(self):
1119 self.set_mode(self.valid_modes[0])
1123 self.set_mode(self.valid_modes[0])
1120
1124
1121 def context(self):
1125 def context(self):
1122 self.set_mode(self.valid_modes[1])
1126 self.set_mode(self.valid_modes[1])
1123
1127
1124 def verbose(self):
1128 def verbose(self):
1125 self.set_mode(self.valid_modes[2])
1129 self.set_mode(self.valid_modes[2])
1126
1130
1127 #----------------------------------------------------------------------------
1131 #----------------------------------------------------------------------------
1128 class AutoFormattedTB(FormattedTB):
1132 class AutoFormattedTB(FormattedTB):
1129 """A traceback printer which can be called on the fly.
1133 """A traceback printer which can be called on the fly.
1130
1134
1131 It will find out about exceptions by itself.
1135 It will find out about exceptions by itself.
1132
1136
1133 A brief example::
1137 A brief example::
1134
1138
1135 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1139 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1136 try:
1140 try:
1137 ...
1141 ...
1138 except:
1142 except:
1139 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1143 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1140 """
1144 """
1141
1145
1142 def __call__(self,etype=None,evalue=None,etb=None,
1146 def __call__(self,etype=None,evalue=None,etb=None,
1143 out=None,tb_offset=None):
1147 out=None,tb_offset=None):
1144 """Print out a formatted exception traceback.
1148 """Print out a formatted exception traceback.
1145
1149
1146 Optional arguments:
1150 Optional arguments:
1147 - out: an open file-like object to direct output to.
1151 - out: an open file-like object to direct output to.
1148
1152
1149 - tb_offset: the number of frames to skip over in the stack, on a
1153 - tb_offset: the number of frames to skip over in the stack, on a
1150 per-call basis (this overrides temporarily the instance's tb_offset
1154 per-call basis (this overrides temporarily the instance's tb_offset
1151 given at initialization time. """
1155 given at initialization time. """
1152
1156
1153
1157
1154 if out is None:
1158 if out is None:
1155 out = self.ostream
1159 out = self.ostream
1156 out.flush()
1160 out.flush()
1157 out.write(self.text(etype, evalue, etb, tb_offset))
1161 out.write(self.text(etype, evalue, etb, tb_offset))
1158 out.write('\n')
1162 out.write('\n')
1159 out.flush()
1163 out.flush()
1160 # FIXME: we should remove the auto pdb behavior from here and leave
1164 # FIXME: we should remove the auto pdb behavior from here and leave
1161 # that to the clients.
1165 # that to the clients.
1162 try:
1166 try:
1163 self.debugger()
1167 self.debugger()
1164 except KeyboardInterrupt:
1168 except KeyboardInterrupt:
1165 print "\nKeyboardInterrupt"
1169 print "\nKeyboardInterrupt"
1166
1170
1167 def structured_traceback(self, etype=None, value=None, tb=None,
1171 def structured_traceback(self, etype=None, value=None, tb=None,
1168 tb_offset=None, context=5):
1172 tb_offset=None, context=5):
1169 if etype is None:
1173 if etype is None:
1170 etype,value,tb = sys.exc_info()
1174 etype,value,tb = sys.exc_info()
1171 self.tb = tb
1175 self.tb = tb
1172 return FormattedTB.structured_traceback(
1176 return FormattedTB.structured_traceback(
1173 self, etype, value, tb, tb_offset, context)
1177 self, etype, value, tb, tb_offset, context)
1174
1178
1175 #---------------------------------------------------------------------------
1179 #---------------------------------------------------------------------------
1176
1180
1177 # A simple class to preserve Nathan's original functionality.
1181 # A simple class to preserve Nathan's original functionality.
1178 class ColorTB(FormattedTB):
1182 class ColorTB(FormattedTB):
1179 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1183 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1180 def __init__(self,color_scheme='Linux',call_pdb=0):
1184 def __init__(self,color_scheme='Linux',call_pdb=0):
1181 FormattedTB.__init__(self,color_scheme=color_scheme,
1185 FormattedTB.__init__(self,color_scheme=color_scheme,
1182 call_pdb=call_pdb)
1186 call_pdb=call_pdb)
1183
1187
1184
1188
1185 class SyntaxTB(ListTB):
1189 class SyntaxTB(ListTB):
1186 """Extension which holds some state: the last exception value"""
1190 """Extension which holds some state: the last exception value"""
1187
1191
1188 def __init__(self,color_scheme = 'NoColor'):
1192 def __init__(self,color_scheme = 'NoColor'):
1189 ListTB.__init__(self,color_scheme)
1193 ListTB.__init__(self,color_scheme)
1190 self.last_syntax_error = None
1194 self.last_syntax_error = None
1191
1195
1192 def __call__(self, etype, value, elist):
1196 def __call__(self, etype, value, elist):
1193 self.last_syntax_error = value
1197 self.last_syntax_error = value
1194 ListTB.__call__(self,etype,value,elist)
1198 ListTB.__call__(self,etype,value,elist)
1195
1199
1196 def clear_err_state(self):
1200 def clear_err_state(self):
1197 """Return the current error state and clear it"""
1201 """Return the current error state and clear it"""
1198 e = self.last_syntax_error
1202 e = self.last_syntax_error
1199 self.last_syntax_error = None
1203 self.last_syntax_error = None
1200 return e
1204 return e
1201
1205
1202 def stb2text(self, stb):
1206 def stb2text(self, stb):
1203 """Convert a structured traceback (a list) to a string."""
1207 """Convert a structured traceback (a list) to a string."""
1204 return ''.join(stb)
1208 return ''.join(stb)
1205
1209
1206
1210
1207 #----------------------------------------------------------------------------
1211 #----------------------------------------------------------------------------
1208 # module testing (minimal)
1212 # module testing (minimal)
1209 if __name__ == "__main__":
1213 if __name__ == "__main__":
1210 def spam(c, d_e):
1214 def spam(c, d_e):
1211 (d, e) = d_e
1215 (d, e) = d_e
1212 x = c + d
1216 x = c + d
1213 y = c * d
1217 y = c * d
1214 foo(x, y)
1218 foo(x, y)
1215
1219
1216 def foo(a, b, bar=1):
1220 def foo(a, b, bar=1):
1217 eggs(a, b + bar)
1221 eggs(a, b + bar)
1218
1222
1219 def eggs(f, g, z=globals()):
1223 def eggs(f, g, z=globals()):
1220 h = f + g
1224 h = f + g
1221 i = f - g
1225 i = f - g
1222 return h / i
1226 return h / i
1223
1227
1224 print ''
1228 print ''
1225 print '*** Before ***'
1229 print '*** Before ***'
1226 try:
1230 try:
1227 print spam(1, (2, 3))
1231 print spam(1, (2, 3))
1228 except:
1232 except:
1229 traceback.print_exc()
1233 traceback.print_exc()
1230 print ''
1234 print ''
1231
1235
1232 handler = ColorTB()
1236 handler = ColorTB()
1233 print '*** ColorTB ***'
1237 print '*** ColorTB ***'
1234 try:
1238 try:
1235 print spam(1, (2, 3))
1239 print spam(1, (2, 3))
1236 except:
1240 except:
1237 handler(*sys.exc_info())
1241 handler(*sys.exc_info())
1238 print ''
1242 print ''
1239
1243
1240 handler = VerboseTB()
1244 handler = VerboseTB()
1241 print '*** VerboseTB ***'
1245 print '*** VerboseTB ***'
1242 try:
1246 try:
1243 print spam(1, (2, 3))
1247 print spam(1, (2, 3))
1244 except:
1248 except:
1245 handler(*sys.exc_info())
1249 handler(*sys.exc_info())
1246 print ''
1250 print ''
1247
1251
1 NO CONTENT: modified file
NO CONTENT: modified file
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -1,75 +1,76 b''
1 """Module that allows latex output notebooks to be conditioned before
1 """Module that allows latex output notebooks to be conditioned before
2 they are converted. Exposes a decorator (@cell_preprocessor) in
2 they are converted. Exposes a decorator (@cell_preprocessor) in
3 addition to the coalesce_streams pre-proccessor.
3 addition to the coalesce_streams pre-proccessor.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Functions
14 # Functions
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 def cell_preprocessor(function):
17 def cell_preprocessor(function):
18 """
18 """
19 Wrap a function to be executed on all cells of a notebook
19 Wrap a function to be executed on all cells of a notebook
20
20
21 Wrapped Parameters
21 Wrapped Parameters
22 ----------
22 ------------------
23
23 cell : NotebookNode cell
24 cell : NotebookNode cell
24 Notebook cell being processed
25 Notebook cell being processed
25 resources : dictionary
26 resources : dictionary
26 Additional resources used in the conversion process. Allows
27 Additional resources used in the conversion process. Allows
27 preprocessors to pass variables into the Jinja engine.
28 preprocessors to pass variables into the Jinja engine.
28 index : int
29 index : int
29 Index of the cell being processed
30 Index of the cell being processed
30 """
31 """
31
32
32 def wrappedfunc(nb, resources):
33 def wrappedfunc(nb, resources):
33 for worksheet in nb.worksheets :
34 for worksheet in nb.worksheets :
34 for index, cell in enumerate(worksheet.cells):
35 for index, cell in enumerate(worksheet.cells):
35 worksheet.cells[index], resources = function(cell, resources, index)
36 worksheet.cells[index], resources = function(cell, resources, index)
36 return nb, resources
37 return nb, resources
37 return wrappedfunc
38 return wrappedfunc
38
39
39
40
40 @cell_preprocessor
41 @cell_preprocessor
41 def coalesce_streams(cell, resources, index):
42 def coalesce_streams(cell, resources, index):
42 """
43 """
43 Merge consecutive sequences of stream output into single stream
44 Merge consecutive sequences of stream output into single stream
44 to prevent extra newlines inserted at flush calls
45 to prevent extra newlines inserted at flush calls
45
46
46 Parameters
47 Parameters
47 ----------
48 ----------
48 cell : NotebookNode cell
49 cell : NotebookNode cell
49 Notebook cell being processed
50 Notebook cell being processed
50 resources : dictionary
51 resources : dictionary
51 Additional resources used in the conversion process. Allows
52 Additional resources used in the conversion process. Allows
52 transformers to pass variables into the Jinja engine.
53 transformers to pass variables into the Jinja engine.
53 index : int
54 index : int
54 Index of the cell being processed
55 Index of the cell being processed
55 """
56 """
56
57
57 outputs = cell.get('outputs', [])
58 outputs = cell.get('outputs', [])
58 if not outputs:
59 if not outputs:
59 return cell, resources
60 return cell, resources
60
61
61 last = outputs[0]
62 last = outputs[0]
62 new_outputs = [last]
63 new_outputs = [last]
63
64
64 for output in outputs[1:]:
65 for output in outputs[1:]:
65 if (output.output_type == 'stream' and
66 if (output.output_type == 'stream' and
66 last.output_type == 'stream' and
67 last.output_type == 'stream' and
67 last.stream == output.stream
68 last.stream == output.stream
68 ):
69 ):
69 last.text += output.text
70 last.text += output.text
70 else:
71 else:
71 new_outputs.append(output)
72 new_outputs.append(output)
72 last = output
73 last = output
73
74
74 cell.outputs = new_outputs
75 cell.outputs = new_outputs
75 return cell, resources
76 return cell, resources
@@ -1,187 +1,187 b''
1 """Windows-specific implementation of process utilities.
1 """Windows-specific implementation of process utilities.
2
2
3 This file is only meant to be imported by process.py, not by end-users.
3 This file is only meant to be imported by process.py, not by end-users.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2010-2011 The IPython Development Team
7 # Copyright (C) 2010-2011 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 from __future__ import print_function
16 from __future__ import print_function
17
17
18 # stdlib
18 # stdlib
19 import os
19 import os
20 import sys
20 import sys
21 import ctypes
21 import ctypes
22
22
23 from ctypes import c_int, POINTER
23 from ctypes import c_int, POINTER
24 from ctypes.wintypes import LPCWSTR, HLOCAL
24 from ctypes.wintypes import LPCWSTR, HLOCAL
25 from subprocess import STDOUT
25 from subprocess import STDOUT
26
26
27 # our own imports
27 # our own imports
28 from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
28 from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
29 from . import py3compat
29 from . import py3compat
30 from .encoding import DEFAULT_ENCODING
30 from .encoding import DEFAULT_ENCODING
31
31
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 # Function definitions
33 # Function definitions
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 class AvoidUNCPath(object):
36 class AvoidUNCPath(object):
37 """A context manager to protect command execution from UNC paths.
37 """A context manager to protect command execution from UNC paths.
38
38
39 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
39 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
40 This context manager temporarily changes directory to the 'C:' drive on
40 This context manager temporarily changes directory to the 'C:' drive on
41 entering, and restores the original working directory on exit.
41 entering, and restores the original working directory on exit.
42
42
43 The context manager returns the starting working directory *if* it made a
43 The context manager returns the starting working directory *if* it made a
44 change and None otherwise, so that users can apply the necessary adjustment
44 change and None otherwise, so that users can apply the necessary adjustment
45 to their system calls in the event of a change.
45 to their system calls in the event of a change.
46
46
47 Example
47 Examples
48 -------
48 --------
49 ::
49 ::
50 cmd = 'dir'
50 cmd = 'dir'
51 with AvoidUNCPath() as path:
51 with AvoidUNCPath() as path:
52 if path is not None:
52 if path is not None:
53 cmd = '"pushd %s &&"%s' % (path, cmd)
53 cmd = '"pushd %s &&"%s' % (path, cmd)
54 os.system(cmd)
54 os.system(cmd)
55 """
55 """
56 def __enter__(self):
56 def __enter__(self):
57 self.path = os.getcwdu()
57 self.path = os.getcwdu()
58 self.is_unc_path = self.path.startswith(r"\\")
58 self.is_unc_path = self.path.startswith(r"\\")
59 if self.is_unc_path:
59 if self.is_unc_path:
60 # change to c drive (as cmd.exe cannot handle UNC addresses)
60 # change to c drive (as cmd.exe cannot handle UNC addresses)
61 os.chdir("C:")
61 os.chdir("C:")
62 return self.path
62 return self.path
63 else:
63 else:
64 # We return None to signal that there was no change in the working
64 # We return None to signal that there was no change in the working
65 # directory
65 # directory
66 return None
66 return None
67
67
68 def __exit__(self, exc_type, exc_value, traceback):
68 def __exit__(self, exc_type, exc_value, traceback):
69 if self.is_unc_path:
69 if self.is_unc_path:
70 os.chdir(self.path)
70 os.chdir(self.path)
71
71
72
72
73 def _find_cmd(cmd):
73 def _find_cmd(cmd):
74 """Find the full path to a .bat or .exe using the win32api module."""
74 """Find the full path to a .bat or .exe using the win32api module."""
75 try:
75 try:
76 from win32api import SearchPath
76 from win32api import SearchPath
77 except ImportError:
77 except ImportError:
78 raise ImportError('you need to have pywin32 installed for this to work')
78 raise ImportError('you need to have pywin32 installed for this to work')
79 else:
79 else:
80 PATH = os.environ['PATH']
80 PATH = os.environ['PATH']
81 extensions = ['.exe', '.com', '.bat', '.py']
81 extensions = ['.exe', '.com', '.bat', '.py']
82 path = None
82 path = None
83 for ext in extensions:
83 for ext in extensions:
84 try:
84 try:
85 path = SearchPath(PATH, cmd, ext)[0]
85 path = SearchPath(PATH, cmd, ext)[0]
86 except:
86 except:
87 pass
87 pass
88 if path is None:
88 if path is None:
89 raise OSError("command %r not found" % cmd)
89 raise OSError("command %r not found" % cmd)
90 else:
90 else:
91 return path
91 return path
92
92
93
93
94 def _system_body(p):
94 def _system_body(p):
95 """Callback for _system."""
95 """Callback for _system."""
96 enc = DEFAULT_ENCODING
96 enc = DEFAULT_ENCODING
97 for line in read_no_interrupt(p.stdout).splitlines():
97 for line in read_no_interrupt(p.stdout).splitlines():
98 line = line.decode(enc, 'replace')
98 line = line.decode(enc, 'replace')
99 print(line, file=sys.stdout)
99 print(line, file=sys.stdout)
100 for line in read_no_interrupt(p.stderr).splitlines():
100 for line in read_no_interrupt(p.stderr).splitlines():
101 line = line.decode(enc, 'replace')
101 line = line.decode(enc, 'replace')
102 print(line, file=sys.stderr)
102 print(line, file=sys.stderr)
103
103
104 # Wait to finish for returncode
104 # Wait to finish for returncode
105 return p.wait()
105 return p.wait()
106
106
107
107
108 def system(cmd):
108 def system(cmd):
109 """Win32 version of os.system() that works with network shares.
109 """Win32 version of os.system() that works with network shares.
110
110
111 Note that this implementation returns None, as meant for use in IPython.
111 Note that this implementation returns None, as meant for use in IPython.
112
112
113 Parameters
113 Parameters
114 ----------
114 ----------
115 cmd : str
115 cmd : str
116 A command to be executed in the system shell.
116 A command to be executed in the system shell.
117
117
118 Returns
118 Returns
119 -------
119 -------
120 None : we explicitly do NOT return the subprocess status code, as this
120 None : we explicitly do NOT return the subprocess status code, as this
121 utility is meant to be used extensively in IPython, where any return value
121 utility is meant to be used extensively in IPython, where any return value
122 would trigger :func:`sys.displayhook` calls.
122 would trigger :func:`sys.displayhook` calls.
123 """
123 """
124 # The controller provides interactivity with both
124 # The controller provides interactivity with both
125 # stdin and stdout
125 # stdin and stdout
126 #import _process_win32_controller
126 #import _process_win32_controller
127 #_process_win32_controller.system(cmd)
127 #_process_win32_controller.system(cmd)
128
128
129 with AvoidUNCPath() as path:
129 with AvoidUNCPath() as path:
130 if path is not None:
130 if path is not None:
131 cmd = '"pushd %s &&"%s' % (path, cmd)
131 cmd = '"pushd %s &&"%s' % (path, cmd)
132 return process_handler(cmd, _system_body)
132 return process_handler(cmd, _system_body)
133
133
134 def getoutput(cmd):
134 def getoutput(cmd):
135 """Return standard output of executing cmd in a shell.
135 """Return standard output of executing cmd in a shell.
136
136
137 Accepts the same arguments as os.system().
137 Accepts the same arguments as os.system().
138
138
139 Parameters
139 Parameters
140 ----------
140 ----------
141 cmd : str
141 cmd : str
142 A command to be executed in the system shell.
142 A command to be executed in the system shell.
143
143
144 Returns
144 Returns
145 -------
145 -------
146 stdout : str
146 stdout : str
147 """
147 """
148
148
149 with AvoidUNCPath() as path:
149 with AvoidUNCPath() as path:
150 if path is not None:
150 if path is not None:
151 cmd = '"pushd %s &&"%s' % (path, cmd)
151 cmd = '"pushd %s &&"%s' % (path, cmd)
152 out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT)
152 out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT)
153
153
154 if out is None:
154 if out is None:
155 out = b''
155 out = b''
156 return py3compat.bytes_to_str(out)
156 return py3compat.bytes_to_str(out)
157
157
158 try:
158 try:
159 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
159 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
160 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
160 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
161 CommandLineToArgvW.restype = POINTER(LPCWSTR)
161 CommandLineToArgvW.restype = POINTER(LPCWSTR)
162 LocalFree = ctypes.windll.kernel32.LocalFree
162 LocalFree = ctypes.windll.kernel32.LocalFree
163 LocalFree.res_type = HLOCAL
163 LocalFree.res_type = HLOCAL
164 LocalFree.arg_types = [HLOCAL]
164 LocalFree.arg_types = [HLOCAL]
165
165
166 def arg_split(commandline, posix=False, strict=True):
166 def arg_split(commandline, posix=False, strict=True):
167 """Split a command line's arguments in a shell-like manner.
167 """Split a command line's arguments in a shell-like manner.
168
168
169 This is a special version for windows that use a ctypes call to CommandLineToArgvW
169 This is a special version for windows that use a ctypes call to CommandLineToArgvW
170 to do the argv splitting. The posix paramter is ignored.
170 to do the argv splitting. The posix paramter is ignored.
171
171
172 If strict=False, process_common.arg_split(...strict=False) is used instead.
172 If strict=False, process_common.arg_split(...strict=False) is used instead.
173 """
173 """
174 #CommandLineToArgvW returns path to executable if called with empty string.
174 #CommandLineToArgvW returns path to executable if called with empty string.
175 if commandline.strip() == "":
175 if commandline.strip() == "":
176 return []
176 return []
177 if not strict:
177 if not strict:
178 # not really a cl-arg, fallback on _process_common
178 # not really a cl-arg, fallback on _process_common
179 return py_arg_split(commandline, posix=posix, strict=strict)
179 return py_arg_split(commandline, posix=posix, strict=strict)
180 argvn = c_int()
180 argvn = c_int()
181 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
181 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
182 result_array_type = LPCWSTR * argvn.value
182 result_array_type = LPCWSTR * argvn.value
183 result = [arg for arg in result_array_type.from_address(ctypes.addressof(result_pointer.contents))]
183 result = [arg for arg in result_array_type.from_address(ctypes.addressof(result_pointer.contents))]
184 retval = LocalFree(result_pointer)
184 retval = LocalFree(result_pointer)
185 return result
185 return result
186 except AttributeError:
186 except AttributeError:
187 arg_split = py_arg_split
187 arg_split = py_arg_split
@@ -1,574 +1,574 b''
1 """Windows-specific implementation of process utilities with direct WinAPI.
1 """Windows-specific implementation of process utilities with direct WinAPI.
2
2
3 This file is meant to be used by process.py
3 This file is meant to be used by process.py
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2010-2011 The IPython Development Team
7 # Copyright (C) 2010-2011 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 from __future__ import print_function
13 from __future__ import print_function
14
14
15 # stdlib
15 # stdlib
16 import os, sys, threading
16 import os, sys, threading
17 import ctypes, msvcrt
17 import ctypes, msvcrt
18
18
19 # Win32 API types needed for the API calls
19 # Win32 API types needed for the API calls
20 from ctypes import POINTER
20 from ctypes import POINTER
21 from ctypes.wintypes import HANDLE, HLOCAL, LPVOID, WORD, DWORD, BOOL, \
21 from ctypes.wintypes import HANDLE, HLOCAL, LPVOID, WORD, DWORD, BOOL, \
22 ULONG, LPCWSTR
22 ULONG, LPCWSTR
23 LPDWORD = POINTER(DWORD)
23 LPDWORD = POINTER(DWORD)
24 LPHANDLE = POINTER(HANDLE)
24 LPHANDLE = POINTER(HANDLE)
25 ULONG_PTR = POINTER(ULONG)
25 ULONG_PTR = POINTER(ULONG)
26 class SECURITY_ATTRIBUTES(ctypes.Structure):
26 class SECURITY_ATTRIBUTES(ctypes.Structure):
27 _fields_ = [("nLength", DWORD),
27 _fields_ = [("nLength", DWORD),
28 ("lpSecurityDescriptor", LPVOID),
28 ("lpSecurityDescriptor", LPVOID),
29 ("bInheritHandle", BOOL)]
29 ("bInheritHandle", BOOL)]
30 LPSECURITY_ATTRIBUTES = POINTER(SECURITY_ATTRIBUTES)
30 LPSECURITY_ATTRIBUTES = POINTER(SECURITY_ATTRIBUTES)
31 class STARTUPINFO(ctypes.Structure):
31 class STARTUPINFO(ctypes.Structure):
32 _fields_ = [("cb", DWORD),
32 _fields_ = [("cb", DWORD),
33 ("lpReserved", LPCWSTR),
33 ("lpReserved", LPCWSTR),
34 ("lpDesktop", LPCWSTR),
34 ("lpDesktop", LPCWSTR),
35 ("lpTitle", LPCWSTR),
35 ("lpTitle", LPCWSTR),
36 ("dwX", DWORD),
36 ("dwX", DWORD),
37 ("dwY", DWORD),
37 ("dwY", DWORD),
38 ("dwXSize", DWORD),
38 ("dwXSize", DWORD),
39 ("dwYSize", DWORD),
39 ("dwYSize", DWORD),
40 ("dwXCountChars", DWORD),
40 ("dwXCountChars", DWORD),
41 ("dwYCountChars", DWORD),
41 ("dwYCountChars", DWORD),
42 ("dwFillAttribute", DWORD),
42 ("dwFillAttribute", DWORD),
43 ("dwFlags", DWORD),
43 ("dwFlags", DWORD),
44 ("wShowWindow", WORD),
44 ("wShowWindow", WORD),
45 ("cbReserved2", WORD),
45 ("cbReserved2", WORD),
46 ("lpReserved2", LPVOID),
46 ("lpReserved2", LPVOID),
47 ("hStdInput", HANDLE),
47 ("hStdInput", HANDLE),
48 ("hStdOutput", HANDLE),
48 ("hStdOutput", HANDLE),
49 ("hStdError", HANDLE)]
49 ("hStdError", HANDLE)]
50 LPSTARTUPINFO = POINTER(STARTUPINFO)
50 LPSTARTUPINFO = POINTER(STARTUPINFO)
51 class PROCESS_INFORMATION(ctypes.Structure):
51 class PROCESS_INFORMATION(ctypes.Structure):
52 _fields_ = [("hProcess", HANDLE),
52 _fields_ = [("hProcess", HANDLE),
53 ("hThread", HANDLE),
53 ("hThread", HANDLE),
54 ("dwProcessId", DWORD),
54 ("dwProcessId", DWORD),
55 ("dwThreadId", DWORD)]
55 ("dwThreadId", DWORD)]
56 LPPROCESS_INFORMATION = POINTER(PROCESS_INFORMATION)
56 LPPROCESS_INFORMATION = POINTER(PROCESS_INFORMATION)
57
57
58 # Win32 API constants needed
58 # Win32 API constants needed
59 ERROR_HANDLE_EOF = 38
59 ERROR_HANDLE_EOF = 38
60 ERROR_BROKEN_PIPE = 109
60 ERROR_BROKEN_PIPE = 109
61 ERROR_NO_DATA = 232
61 ERROR_NO_DATA = 232
62 HANDLE_FLAG_INHERIT = 0x0001
62 HANDLE_FLAG_INHERIT = 0x0001
63 STARTF_USESTDHANDLES = 0x0100
63 STARTF_USESTDHANDLES = 0x0100
64 CREATE_SUSPENDED = 0x0004
64 CREATE_SUSPENDED = 0x0004
65 CREATE_NEW_CONSOLE = 0x0010
65 CREATE_NEW_CONSOLE = 0x0010
66 CREATE_NO_WINDOW = 0x08000000
66 CREATE_NO_WINDOW = 0x08000000
67 STILL_ACTIVE = 259
67 STILL_ACTIVE = 259
68 WAIT_TIMEOUT = 0x0102
68 WAIT_TIMEOUT = 0x0102
69 WAIT_FAILED = 0xFFFFFFFF
69 WAIT_FAILED = 0xFFFFFFFF
70 INFINITE = 0xFFFFFFFF
70 INFINITE = 0xFFFFFFFF
71 DUPLICATE_SAME_ACCESS = 0x00000002
71 DUPLICATE_SAME_ACCESS = 0x00000002
72 ENABLE_ECHO_INPUT = 0x0004
72 ENABLE_ECHO_INPUT = 0x0004
73 ENABLE_LINE_INPUT = 0x0002
73 ENABLE_LINE_INPUT = 0x0002
74 ENABLE_PROCESSED_INPUT = 0x0001
74 ENABLE_PROCESSED_INPUT = 0x0001
75
75
76 # Win32 API functions needed
76 # Win32 API functions needed
77 GetLastError = ctypes.windll.kernel32.GetLastError
77 GetLastError = ctypes.windll.kernel32.GetLastError
78 GetLastError.argtypes = []
78 GetLastError.argtypes = []
79 GetLastError.restype = DWORD
79 GetLastError.restype = DWORD
80
80
81 CreateFile = ctypes.windll.kernel32.CreateFileW
81 CreateFile = ctypes.windll.kernel32.CreateFileW
82 CreateFile.argtypes = [LPCWSTR, DWORD, DWORD, LPVOID, DWORD, DWORD, HANDLE]
82 CreateFile.argtypes = [LPCWSTR, DWORD, DWORD, LPVOID, DWORD, DWORD, HANDLE]
83 CreateFile.restype = HANDLE
83 CreateFile.restype = HANDLE
84
84
85 CreatePipe = ctypes.windll.kernel32.CreatePipe
85 CreatePipe = ctypes.windll.kernel32.CreatePipe
86 CreatePipe.argtypes = [POINTER(HANDLE), POINTER(HANDLE),
86 CreatePipe.argtypes = [POINTER(HANDLE), POINTER(HANDLE),
87 LPSECURITY_ATTRIBUTES, DWORD]
87 LPSECURITY_ATTRIBUTES, DWORD]
88 CreatePipe.restype = BOOL
88 CreatePipe.restype = BOOL
89
89
90 CreateProcess = ctypes.windll.kernel32.CreateProcessW
90 CreateProcess = ctypes.windll.kernel32.CreateProcessW
91 CreateProcess.argtypes = [LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES,
91 CreateProcess.argtypes = [LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES,
92 LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCWSTR, LPSTARTUPINFO,
92 LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCWSTR, LPSTARTUPINFO,
93 LPPROCESS_INFORMATION]
93 LPPROCESS_INFORMATION]
94 CreateProcess.restype = BOOL
94 CreateProcess.restype = BOOL
95
95
96 GetExitCodeProcess = ctypes.windll.kernel32.GetExitCodeProcess
96 GetExitCodeProcess = ctypes.windll.kernel32.GetExitCodeProcess
97 GetExitCodeProcess.argtypes = [HANDLE, LPDWORD]
97 GetExitCodeProcess.argtypes = [HANDLE, LPDWORD]
98 GetExitCodeProcess.restype = BOOL
98 GetExitCodeProcess.restype = BOOL
99
99
100 GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
100 GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
101 GetCurrentProcess.argtypes = []
101 GetCurrentProcess.argtypes = []
102 GetCurrentProcess.restype = HANDLE
102 GetCurrentProcess.restype = HANDLE
103
103
104 ResumeThread = ctypes.windll.kernel32.ResumeThread
104 ResumeThread = ctypes.windll.kernel32.ResumeThread
105 ResumeThread.argtypes = [HANDLE]
105 ResumeThread.argtypes = [HANDLE]
106 ResumeThread.restype = DWORD
106 ResumeThread.restype = DWORD
107
107
108 ReadFile = ctypes.windll.kernel32.ReadFile
108 ReadFile = ctypes.windll.kernel32.ReadFile
109 ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPVOID]
109 ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPVOID]
110 ReadFile.restype = BOOL
110 ReadFile.restype = BOOL
111
111
112 WriteFile = ctypes.windll.kernel32.WriteFile
112 WriteFile = ctypes.windll.kernel32.WriteFile
113 WriteFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPVOID]
113 WriteFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPVOID]
114 WriteFile.restype = BOOL
114 WriteFile.restype = BOOL
115
115
116 GetConsoleMode = ctypes.windll.kernel32.GetConsoleMode
116 GetConsoleMode = ctypes.windll.kernel32.GetConsoleMode
117 GetConsoleMode.argtypes = [HANDLE, LPDWORD]
117 GetConsoleMode.argtypes = [HANDLE, LPDWORD]
118 GetConsoleMode.restype = BOOL
118 GetConsoleMode.restype = BOOL
119
119
120 SetConsoleMode = ctypes.windll.kernel32.SetConsoleMode
120 SetConsoleMode = ctypes.windll.kernel32.SetConsoleMode
121 SetConsoleMode.argtypes = [HANDLE, DWORD]
121 SetConsoleMode.argtypes = [HANDLE, DWORD]
122 SetConsoleMode.restype = BOOL
122 SetConsoleMode.restype = BOOL
123
123
124 FlushConsoleInputBuffer = ctypes.windll.kernel32.FlushConsoleInputBuffer
124 FlushConsoleInputBuffer = ctypes.windll.kernel32.FlushConsoleInputBuffer
125 FlushConsoleInputBuffer.argtypes = [HANDLE]
125 FlushConsoleInputBuffer.argtypes = [HANDLE]
126 FlushConsoleInputBuffer.restype = BOOL
126 FlushConsoleInputBuffer.restype = BOOL
127
127
128 WaitForSingleObject = ctypes.windll.kernel32.WaitForSingleObject
128 WaitForSingleObject = ctypes.windll.kernel32.WaitForSingleObject
129 WaitForSingleObject.argtypes = [HANDLE, DWORD]
129 WaitForSingleObject.argtypes = [HANDLE, DWORD]
130 WaitForSingleObject.restype = DWORD
130 WaitForSingleObject.restype = DWORD
131
131
132 DuplicateHandle = ctypes.windll.kernel32.DuplicateHandle
132 DuplicateHandle = ctypes.windll.kernel32.DuplicateHandle
133 DuplicateHandle.argtypes = [HANDLE, HANDLE, HANDLE, LPHANDLE,
133 DuplicateHandle.argtypes = [HANDLE, HANDLE, HANDLE, LPHANDLE,
134 DWORD, BOOL, DWORD]
134 DWORD, BOOL, DWORD]
135 DuplicateHandle.restype = BOOL
135 DuplicateHandle.restype = BOOL
136
136
137 SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation
137 SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation
138 SetHandleInformation.argtypes = [HANDLE, DWORD, DWORD]
138 SetHandleInformation.argtypes = [HANDLE, DWORD, DWORD]
139 SetHandleInformation.restype = BOOL
139 SetHandleInformation.restype = BOOL
140
140
141 CloseHandle = ctypes.windll.kernel32.CloseHandle
141 CloseHandle = ctypes.windll.kernel32.CloseHandle
142 CloseHandle.argtypes = [HANDLE]
142 CloseHandle.argtypes = [HANDLE]
143 CloseHandle.restype = BOOL
143 CloseHandle.restype = BOOL
144
144
145 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
145 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
146 CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(ctypes.c_int)]
146 CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(ctypes.c_int)]
147 CommandLineToArgvW.restype = POINTER(LPCWSTR)
147 CommandLineToArgvW.restype = POINTER(LPCWSTR)
148
148
149 LocalFree = ctypes.windll.kernel32.LocalFree
149 LocalFree = ctypes.windll.kernel32.LocalFree
150 LocalFree.argtypes = [HLOCAL]
150 LocalFree.argtypes = [HLOCAL]
151 LocalFree.restype = HLOCAL
151 LocalFree.restype = HLOCAL
152
152
153 class AvoidUNCPath(object):
153 class AvoidUNCPath(object):
154 """A context manager to protect command execution from UNC paths.
154 """A context manager to protect command execution from UNC paths.
155
155
156 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
156 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
157 This context manager temporarily changes directory to the 'C:' drive on
157 This context manager temporarily changes directory to the 'C:' drive on
158 entering, and restores the original working directory on exit.
158 entering, and restores the original working directory on exit.
159
159
160 The context manager returns the starting working directory *if* it made a
160 The context manager returns the starting working directory *if* it made a
161 change and None otherwise, so that users can apply the necessary adjustment
161 change and None otherwise, so that users can apply the necessary adjustment
162 to their system calls in the event of a change.
162 to their system calls in the event of a change.
163
163
164 Example
164 Examples
165 -------
165 --------
166 ::
166 ::
167 cmd = 'dir'
167 cmd = 'dir'
168 with AvoidUNCPath() as path:
168 with AvoidUNCPath() as path:
169 if path is not None:
169 if path is not None:
170 cmd = '"pushd %s &&"%s' % (path, cmd)
170 cmd = '"pushd %s &&"%s' % (path, cmd)
171 os.system(cmd)
171 os.system(cmd)
172 """
172 """
173 def __enter__(self):
173 def __enter__(self):
174 self.path = os.getcwdu()
174 self.path = os.getcwdu()
175 self.is_unc_path = self.path.startswith(r"\\")
175 self.is_unc_path = self.path.startswith(r"\\")
176 if self.is_unc_path:
176 if self.is_unc_path:
177 # change to c drive (as cmd.exe cannot handle UNC addresses)
177 # change to c drive (as cmd.exe cannot handle UNC addresses)
178 os.chdir("C:")
178 os.chdir("C:")
179 return self.path
179 return self.path
180 else:
180 else:
181 # We return None to signal that there was no change in the working
181 # We return None to signal that there was no change in the working
182 # directory
182 # directory
183 return None
183 return None
184
184
185 def __exit__(self, exc_type, exc_value, traceback):
185 def __exit__(self, exc_type, exc_value, traceback):
186 if self.is_unc_path:
186 if self.is_unc_path:
187 os.chdir(self.path)
187 os.chdir(self.path)
188
188
189
189
190 class Win32ShellCommandController(object):
190 class Win32ShellCommandController(object):
191 """Runs a shell command in a 'with' context.
191 """Runs a shell command in a 'with' context.
192
192
193 This implementation is Win32-specific.
193 This implementation is Win32-specific.
194
194
195 Example:
195 Example:
196 # Runs the command interactively with default console stdin/stdout
196 # Runs the command interactively with default console stdin/stdout
197 with ShellCommandController('python -i') as scc:
197 with ShellCommandController('python -i') as scc:
198 scc.run()
198 scc.run()
199
199
200 # Runs the command using the provided functions for stdin/stdout
200 # Runs the command using the provided functions for stdin/stdout
201 def my_stdout_func(s):
201 def my_stdout_func(s):
202 # print or save the string 's'
202 # print or save the string 's'
203 write_to_stdout(s)
203 write_to_stdout(s)
204 def my_stdin_func():
204 def my_stdin_func():
205 # If input is available, return it as a string.
205 # If input is available, return it as a string.
206 if input_available():
206 if input_available():
207 return get_input()
207 return get_input()
208 # If no input available, return None after a short delay to
208 # If no input available, return None after a short delay to
209 # keep from blocking.
209 # keep from blocking.
210 else:
210 else:
211 time.sleep(0.01)
211 time.sleep(0.01)
212 return None
212 return None
213
213
214 with ShellCommandController('python -i') as scc:
214 with ShellCommandController('python -i') as scc:
215 scc.run(my_stdout_func, my_stdin_func)
215 scc.run(my_stdout_func, my_stdin_func)
216 """
216 """
217
217
218 def __init__(self, cmd, mergeout = True):
218 def __init__(self, cmd, mergeout = True):
219 """Initializes the shell command controller.
219 """Initializes the shell command controller.
220
220
221 The cmd is the program to execute, and mergeout is
221 The cmd is the program to execute, and mergeout is
222 whether to blend stdout and stderr into one output
222 whether to blend stdout and stderr into one output
223 in stdout. Merging them together in this fashion more
223 in stdout. Merging them together in this fashion more
224 reliably keeps stdout and stderr in the correct order
224 reliably keeps stdout and stderr in the correct order
225 especially for interactive shell usage.
225 especially for interactive shell usage.
226 """
226 """
227 self.cmd = cmd
227 self.cmd = cmd
228 self.mergeout = mergeout
228 self.mergeout = mergeout
229
229
230 def __enter__(self):
230 def __enter__(self):
231 cmd = self.cmd
231 cmd = self.cmd
232 mergeout = self.mergeout
232 mergeout = self.mergeout
233
233
234 self.hstdout, self.hstdin, self.hstderr = None, None, None
234 self.hstdout, self.hstdin, self.hstderr = None, None, None
235 self.piProcInfo = None
235 self.piProcInfo = None
236 try:
236 try:
237 p_hstdout, c_hstdout, p_hstderr, \
237 p_hstdout, c_hstdout, p_hstderr, \
238 c_hstderr, p_hstdin, c_hstdin = [None]*6
238 c_hstderr, p_hstdin, c_hstdin = [None]*6
239
239
240 # SECURITY_ATTRIBUTES with inherit handle set to True
240 # SECURITY_ATTRIBUTES with inherit handle set to True
241 saAttr = SECURITY_ATTRIBUTES()
241 saAttr = SECURITY_ATTRIBUTES()
242 saAttr.nLength = ctypes.sizeof(saAttr)
242 saAttr.nLength = ctypes.sizeof(saAttr)
243 saAttr.bInheritHandle = True
243 saAttr.bInheritHandle = True
244 saAttr.lpSecurityDescriptor = None
244 saAttr.lpSecurityDescriptor = None
245
245
246 def create_pipe(uninherit):
246 def create_pipe(uninherit):
247 """Creates a Windows pipe, which consists of two handles.
247 """Creates a Windows pipe, which consists of two handles.
248
248
249 The 'uninherit' parameter controls which handle is not
249 The 'uninherit' parameter controls which handle is not
250 inherited by the child process.
250 inherited by the child process.
251 """
251 """
252 handles = HANDLE(), HANDLE()
252 handles = HANDLE(), HANDLE()
253 if not CreatePipe(ctypes.byref(handles[0]),
253 if not CreatePipe(ctypes.byref(handles[0]),
254 ctypes.byref(handles[1]), ctypes.byref(saAttr), 0):
254 ctypes.byref(handles[1]), ctypes.byref(saAttr), 0):
255 raise ctypes.WinError()
255 raise ctypes.WinError()
256 if not SetHandleInformation(handles[uninherit],
256 if not SetHandleInformation(handles[uninherit],
257 HANDLE_FLAG_INHERIT, 0):
257 HANDLE_FLAG_INHERIT, 0):
258 raise ctypes.WinError()
258 raise ctypes.WinError()
259 return handles[0].value, handles[1].value
259 return handles[0].value, handles[1].value
260
260
261 p_hstdout, c_hstdout = create_pipe(uninherit=0)
261 p_hstdout, c_hstdout = create_pipe(uninherit=0)
262 # 'mergeout' signals that stdout and stderr should be merged.
262 # 'mergeout' signals that stdout and stderr should be merged.
263 # We do that by using one pipe for both of them.
263 # We do that by using one pipe for both of them.
264 if mergeout:
264 if mergeout:
265 c_hstderr = HANDLE()
265 c_hstderr = HANDLE()
266 if not DuplicateHandle(GetCurrentProcess(), c_hstdout,
266 if not DuplicateHandle(GetCurrentProcess(), c_hstdout,
267 GetCurrentProcess(), ctypes.byref(c_hstderr),
267 GetCurrentProcess(), ctypes.byref(c_hstderr),
268 0, True, DUPLICATE_SAME_ACCESS):
268 0, True, DUPLICATE_SAME_ACCESS):
269 raise ctypes.WinError()
269 raise ctypes.WinError()
270 else:
270 else:
271 p_hstderr, c_hstderr = create_pipe(uninherit=0)
271 p_hstderr, c_hstderr = create_pipe(uninherit=0)
272 c_hstdin, p_hstdin = create_pipe(uninherit=1)
272 c_hstdin, p_hstdin = create_pipe(uninherit=1)
273
273
274 # Create the process object
274 # Create the process object
275 piProcInfo = PROCESS_INFORMATION()
275 piProcInfo = PROCESS_INFORMATION()
276 siStartInfo = STARTUPINFO()
276 siStartInfo = STARTUPINFO()
277 siStartInfo.cb = ctypes.sizeof(siStartInfo)
277 siStartInfo.cb = ctypes.sizeof(siStartInfo)
278 siStartInfo.hStdInput = c_hstdin
278 siStartInfo.hStdInput = c_hstdin
279 siStartInfo.hStdOutput = c_hstdout
279 siStartInfo.hStdOutput = c_hstdout
280 siStartInfo.hStdError = c_hstderr
280 siStartInfo.hStdError = c_hstderr
281 siStartInfo.dwFlags = STARTF_USESTDHANDLES
281 siStartInfo.dwFlags = STARTF_USESTDHANDLES
282 dwCreationFlags = CREATE_SUSPENDED | CREATE_NO_WINDOW # | CREATE_NEW_CONSOLE
282 dwCreationFlags = CREATE_SUSPENDED | CREATE_NO_WINDOW # | CREATE_NEW_CONSOLE
283
283
284 if not CreateProcess(None,
284 if not CreateProcess(None,
285 u"cmd.exe /c " + cmd,
285 u"cmd.exe /c " + cmd,
286 None, None, True, dwCreationFlags,
286 None, None, True, dwCreationFlags,
287 None, None, ctypes.byref(siStartInfo),
287 None, None, ctypes.byref(siStartInfo),
288 ctypes.byref(piProcInfo)):
288 ctypes.byref(piProcInfo)):
289 raise ctypes.WinError()
289 raise ctypes.WinError()
290
290
291 # Close this process's versions of the child handles
291 # Close this process's versions of the child handles
292 CloseHandle(c_hstdin)
292 CloseHandle(c_hstdin)
293 c_hstdin = None
293 c_hstdin = None
294 CloseHandle(c_hstdout)
294 CloseHandle(c_hstdout)
295 c_hstdout = None
295 c_hstdout = None
296 if c_hstderr != None:
296 if c_hstderr != None:
297 CloseHandle(c_hstderr)
297 CloseHandle(c_hstderr)
298 c_hstderr = None
298 c_hstderr = None
299
299
300 # Transfer ownership of the parent handles to the object
300 # Transfer ownership of the parent handles to the object
301 self.hstdin = p_hstdin
301 self.hstdin = p_hstdin
302 p_hstdin = None
302 p_hstdin = None
303 self.hstdout = p_hstdout
303 self.hstdout = p_hstdout
304 p_hstdout = None
304 p_hstdout = None
305 if not mergeout:
305 if not mergeout:
306 self.hstderr = p_hstderr
306 self.hstderr = p_hstderr
307 p_hstderr = None
307 p_hstderr = None
308 self.piProcInfo = piProcInfo
308 self.piProcInfo = piProcInfo
309
309
310 finally:
310 finally:
311 if p_hstdin:
311 if p_hstdin:
312 CloseHandle(p_hstdin)
312 CloseHandle(p_hstdin)
313 if c_hstdin:
313 if c_hstdin:
314 CloseHandle(c_hstdin)
314 CloseHandle(c_hstdin)
315 if p_hstdout:
315 if p_hstdout:
316 CloseHandle(p_hstdout)
316 CloseHandle(p_hstdout)
317 if c_hstdout:
317 if c_hstdout:
318 CloseHandle(c_hstdout)
318 CloseHandle(c_hstdout)
319 if p_hstderr:
319 if p_hstderr:
320 CloseHandle(p_hstderr)
320 CloseHandle(p_hstderr)
321 if c_hstderr:
321 if c_hstderr:
322 CloseHandle(c_hstderr)
322 CloseHandle(c_hstderr)
323
323
324 return self
324 return self
325
325
326 def _stdin_thread(self, handle, hprocess, func, stdout_func):
326 def _stdin_thread(self, handle, hprocess, func, stdout_func):
327 exitCode = DWORD()
327 exitCode = DWORD()
328 bytesWritten = DWORD(0)
328 bytesWritten = DWORD(0)
329 while True:
329 while True:
330 #print("stdin thread loop start")
330 #print("stdin thread loop start")
331 # Get the input string (may be bytes or unicode)
331 # Get the input string (may be bytes or unicode)
332 data = func()
332 data = func()
333
333
334 # None signals to poll whether the process has exited
334 # None signals to poll whether the process has exited
335 if data is None:
335 if data is None:
336 #print("checking for process completion")
336 #print("checking for process completion")
337 if not GetExitCodeProcess(hprocess, ctypes.byref(exitCode)):
337 if not GetExitCodeProcess(hprocess, ctypes.byref(exitCode)):
338 raise ctypes.WinError()
338 raise ctypes.WinError()
339 if exitCode.value != STILL_ACTIVE:
339 if exitCode.value != STILL_ACTIVE:
340 return
340 return
341 # TESTING: Does zero-sized writefile help?
341 # TESTING: Does zero-sized writefile help?
342 if not WriteFile(handle, "", 0,
342 if not WriteFile(handle, "", 0,
343 ctypes.byref(bytesWritten), None):
343 ctypes.byref(bytesWritten), None):
344 raise ctypes.WinError()
344 raise ctypes.WinError()
345 continue
345 continue
346 #print("\nGot str %s\n" % repr(data), file=sys.stderr)
346 #print("\nGot str %s\n" % repr(data), file=sys.stderr)
347
347
348 # Encode the string to the console encoding
348 # Encode the string to the console encoding
349 if isinstance(data, unicode): #FIXME: Python3
349 if isinstance(data, unicode): #FIXME: Python3
350 data = data.encode('utf_8')
350 data = data.encode('utf_8')
351
351
352 # What we have now must be a string of bytes
352 # What we have now must be a string of bytes
353 if not isinstance(data, str): #FIXME: Python3
353 if not isinstance(data, str): #FIXME: Python3
354 raise RuntimeError("internal stdin function string error")
354 raise RuntimeError("internal stdin function string error")
355
355
356 # An empty string signals EOF
356 # An empty string signals EOF
357 if len(data) == 0:
357 if len(data) == 0:
358 return
358 return
359
359
360 # In a windows console, sometimes the input is echoed,
360 # In a windows console, sometimes the input is echoed,
361 # but sometimes not. How do we determine when to do this?
361 # but sometimes not. How do we determine when to do this?
362 stdout_func(data)
362 stdout_func(data)
363 # WriteFile may not accept all the data at once.
363 # WriteFile may not accept all the data at once.
364 # Loop until everything is processed
364 # Loop until everything is processed
365 while len(data) != 0:
365 while len(data) != 0:
366 #print("Calling writefile")
366 #print("Calling writefile")
367 if not WriteFile(handle, data, len(data),
367 if not WriteFile(handle, data, len(data),
368 ctypes.byref(bytesWritten), None):
368 ctypes.byref(bytesWritten), None):
369 # This occurs at exit
369 # This occurs at exit
370 if GetLastError() == ERROR_NO_DATA:
370 if GetLastError() == ERROR_NO_DATA:
371 return
371 return
372 raise ctypes.WinError()
372 raise ctypes.WinError()
373 #print("Called writefile")
373 #print("Called writefile")
374 data = data[bytesWritten.value:]
374 data = data[bytesWritten.value:]
375
375
376 def _stdout_thread(self, handle, func):
376 def _stdout_thread(self, handle, func):
377 # Allocate the output buffer
377 # Allocate the output buffer
378 data = ctypes.create_string_buffer(4096)
378 data = ctypes.create_string_buffer(4096)
379 while True:
379 while True:
380 bytesRead = DWORD(0)
380 bytesRead = DWORD(0)
381 if not ReadFile(handle, data, 4096,
381 if not ReadFile(handle, data, 4096,
382 ctypes.byref(bytesRead), None):
382 ctypes.byref(bytesRead), None):
383 le = GetLastError()
383 le = GetLastError()
384 if le == ERROR_BROKEN_PIPE:
384 if le == ERROR_BROKEN_PIPE:
385 return
385 return
386 else:
386 else:
387 raise ctypes.WinError()
387 raise ctypes.WinError()
388 # FIXME: Python3
388 # FIXME: Python3
389 s = data.value[0:bytesRead.value]
389 s = data.value[0:bytesRead.value]
390 #print("\nv: %s" % repr(s), file=sys.stderr)
390 #print("\nv: %s" % repr(s), file=sys.stderr)
391 func(s.decode('utf_8', 'replace'))
391 func(s.decode('utf_8', 'replace'))
392
392
393 def run(self, stdout_func = None, stdin_func = None, stderr_func = None):
393 def run(self, stdout_func = None, stdin_func = None, stderr_func = None):
394 """Runs the process, using the provided functions for I/O.
394 """Runs the process, using the provided functions for I/O.
395
395
396 The function stdin_func should return strings whenever a
396 The function stdin_func should return strings whenever a
397 character or characters become available.
397 character or characters become available.
398 The functions stdout_func and stderr_func are called whenever
398 The functions stdout_func and stderr_func are called whenever
399 something is printed to stdout or stderr, respectively.
399 something is printed to stdout or stderr, respectively.
400 These functions are called from different threads (but not
400 These functions are called from different threads (but not
401 concurrently, because of the GIL).
401 concurrently, because of the GIL).
402 """
402 """
403 if stdout_func == None and stdin_func == None and stderr_func == None:
403 if stdout_func == None and stdin_func == None and stderr_func == None:
404 return self._run_stdio()
404 return self._run_stdio()
405
405
406 if stderr_func != None and self.mergeout:
406 if stderr_func != None and self.mergeout:
407 raise RuntimeError("Shell command was initiated with "
407 raise RuntimeError("Shell command was initiated with "
408 "merged stdin/stdout, but a separate stderr_func "
408 "merged stdin/stdout, but a separate stderr_func "
409 "was provided to the run() method")
409 "was provided to the run() method")
410
410
411 # Create a thread for each input/output handle
411 # Create a thread for each input/output handle
412 stdin_thread = None
412 stdin_thread = None
413 threads = []
413 threads = []
414 if stdin_func:
414 if stdin_func:
415 stdin_thread = threading.Thread(target=self._stdin_thread,
415 stdin_thread = threading.Thread(target=self._stdin_thread,
416 args=(self.hstdin, self.piProcInfo.hProcess,
416 args=(self.hstdin, self.piProcInfo.hProcess,
417 stdin_func, stdout_func))
417 stdin_func, stdout_func))
418 threads.append(threading.Thread(target=self._stdout_thread,
418 threads.append(threading.Thread(target=self._stdout_thread,
419 args=(self.hstdout, stdout_func)))
419 args=(self.hstdout, stdout_func)))
420 if not self.mergeout:
420 if not self.mergeout:
421 if stderr_func == None:
421 if stderr_func == None:
422 stderr_func = stdout_func
422 stderr_func = stdout_func
423 threads.append(threading.Thread(target=self._stdout_thread,
423 threads.append(threading.Thread(target=self._stdout_thread,
424 args=(self.hstderr, stderr_func)))
424 args=(self.hstderr, stderr_func)))
425 # Start the I/O threads and the process
425 # Start the I/O threads and the process
426 if ResumeThread(self.piProcInfo.hThread) == 0xFFFFFFFF:
426 if ResumeThread(self.piProcInfo.hThread) == 0xFFFFFFFF:
427 raise ctypes.WinError()
427 raise ctypes.WinError()
428 if stdin_thread is not None:
428 if stdin_thread is not None:
429 stdin_thread.start()
429 stdin_thread.start()
430 for thread in threads:
430 for thread in threads:
431 thread.start()
431 thread.start()
432 # Wait for the process to complete
432 # Wait for the process to complete
433 if WaitForSingleObject(self.piProcInfo.hProcess, INFINITE) == \
433 if WaitForSingleObject(self.piProcInfo.hProcess, INFINITE) == \
434 WAIT_FAILED:
434 WAIT_FAILED:
435 raise ctypes.WinError()
435 raise ctypes.WinError()
436 # Wait for the I/O threads to complete
436 # Wait for the I/O threads to complete
437 for thread in threads:
437 for thread in threads:
438 thread.join()
438 thread.join()
439
439
440 # Wait for the stdin thread to complete
440 # Wait for the stdin thread to complete
441 if stdin_thread is not None:
441 if stdin_thread is not None:
442 stdin_thread.join()
442 stdin_thread.join()
443
443
444 def _stdin_raw_nonblock(self):
444 def _stdin_raw_nonblock(self):
445 """Use the raw Win32 handle of sys.stdin to do non-blocking reads"""
445 """Use the raw Win32 handle of sys.stdin to do non-blocking reads"""
446 # WARNING: This is experimental, and produces inconsistent results.
446 # WARNING: This is experimental, and produces inconsistent results.
447 # It's possible for the handle not to be appropriate for use
447 # It's possible for the handle not to be appropriate for use
448 # with WaitForSingleObject, among other things.
448 # with WaitForSingleObject, among other things.
449 handle = msvcrt.get_osfhandle(sys.stdin.fileno())
449 handle = msvcrt.get_osfhandle(sys.stdin.fileno())
450 result = WaitForSingleObject(handle, 100)
450 result = WaitForSingleObject(handle, 100)
451 if result == WAIT_FAILED:
451 if result == WAIT_FAILED:
452 raise ctypes.WinError()
452 raise ctypes.WinError()
453 elif result == WAIT_TIMEOUT:
453 elif result == WAIT_TIMEOUT:
454 print(".", end='')
454 print(".", end='')
455 return None
455 return None
456 else:
456 else:
457 data = ctypes.create_string_buffer(256)
457 data = ctypes.create_string_buffer(256)
458 bytesRead = DWORD(0)
458 bytesRead = DWORD(0)
459 print('?', end='')
459 print('?', end='')
460
460
461 if not ReadFile(handle, data, 256,
461 if not ReadFile(handle, data, 256,
462 ctypes.byref(bytesRead), None):
462 ctypes.byref(bytesRead), None):
463 raise ctypes.WinError()
463 raise ctypes.WinError()
464 # This ensures the non-blocking works with an actual console
464 # This ensures the non-blocking works with an actual console
465 # Not checking the error, so the processing will still work with
465 # Not checking the error, so the processing will still work with
466 # other handle types
466 # other handle types
467 FlushConsoleInputBuffer(handle)
467 FlushConsoleInputBuffer(handle)
468
468
469 data = data.value
469 data = data.value
470 data = data.replace('\r\n', '\n')
470 data = data.replace('\r\n', '\n')
471 data = data.replace('\r', '\n')
471 data = data.replace('\r', '\n')
472 print(repr(data) + " ", end='')
472 print(repr(data) + " ", end='')
473 return data
473 return data
474
474
475 def _stdin_raw_block(self):
475 def _stdin_raw_block(self):
476 """Use a blocking stdin read"""
476 """Use a blocking stdin read"""
477 # The big problem with the blocking read is that it doesn't
477 # The big problem with the blocking read is that it doesn't
478 # exit when it's supposed to in all contexts. An extra
478 # exit when it's supposed to in all contexts. An extra
479 # key-press may be required to trigger the exit.
479 # key-press may be required to trigger the exit.
480 try:
480 try:
481 data = sys.stdin.read(1)
481 data = sys.stdin.read(1)
482 data = data.replace('\r', '\n')
482 data = data.replace('\r', '\n')
483 return data
483 return data
484 except WindowsError as we:
484 except WindowsError as we:
485 if we.winerror == ERROR_NO_DATA:
485 if we.winerror == ERROR_NO_DATA:
486 # This error occurs when the pipe is closed
486 # This error occurs when the pipe is closed
487 return None
487 return None
488 else:
488 else:
489 # Otherwise let the error propagate
489 # Otherwise let the error propagate
490 raise we
490 raise we
491
491
492 def _stdout_raw(self, s):
492 def _stdout_raw(self, s):
493 """Writes the string to stdout"""
493 """Writes the string to stdout"""
494 print(s, end='', file=sys.stdout)
494 print(s, end='', file=sys.stdout)
495 sys.stdout.flush()
495 sys.stdout.flush()
496
496
497 def _stderr_raw(self, s):
497 def _stderr_raw(self, s):
498 """Writes the string to stdout"""
498 """Writes the string to stdout"""
499 print(s, end='', file=sys.stderr)
499 print(s, end='', file=sys.stderr)
500 sys.stderr.flush()
500 sys.stderr.flush()
501
501
502 def _run_stdio(self):
502 def _run_stdio(self):
503 """Runs the process using the system standard I/O.
503 """Runs the process using the system standard I/O.
504
504
505 IMPORTANT: stdin needs to be asynchronous, so the Python
505 IMPORTANT: stdin needs to be asynchronous, so the Python
506 sys.stdin object is not used. Instead,
506 sys.stdin object is not used. Instead,
507 msvcrt.kbhit/getwch are used asynchronously.
507 msvcrt.kbhit/getwch are used asynchronously.
508 """
508 """
509 # Disable Line and Echo mode
509 # Disable Line and Echo mode
510 #lpMode = DWORD()
510 #lpMode = DWORD()
511 #handle = msvcrt.get_osfhandle(sys.stdin.fileno())
511 #handle = msvcrt.get_osfhandle(sys.stdin.fileno())
512 #if GetConsoleMode(handle, ctypes.byref(lpMode)):
512 #if GetConsoleMode(handle, ctypes.byref(lpMode)):
513 # set_console_mode = True
513 # set_console_mode = True
514 # if not SetConsoleMode(handle, lpMode.value &
514 # if not SetConsoleMode(handle, lpMode.value &
515 # ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT)):
515 # ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT)):
516 # raise ctypes.WinError()
516 # raise ctypes.WinError()
517
517
518 if self.mergeout:
518 if self.mergeout:
519 return self.run(stdout_func = self._stdout_raw,
519 return self.run(stdout_func = self._stdout_raw,
520 stdin_func = self._stdin_raw_block)
520 stdin_func = self._stdin_raw_block)
521 else:
521 else:
522 return self.run(stdout_func = self._stdout_raw,
522 return self.run(stdout_func = self._stdout_raw,
523 stdin_func = self._stdin_raw_block,
523 stdin_func = self._stdin_raw_block,
524 stderr_func = self._stderr_raw)
524 stderr_func = self._stderr_raw)
525
525
526 # Restore the previous console mode
526 # Restore the previous console mode
527 #if set_console_mode:
527 #if set_console_mode:
528 # if not SetConsoleMode(handle, lpMode.value):
528 # if not SetConsoleMode(handle, lpMode.value):
529 # raise ctypes.WinError()
529 # raise ctypes.WinError()
530
530
531 def __exit__(self, exc_type, exc_value, traceback):
531 def __exit__(self, exc_type, exc_value, traceback):
532 if self.hstdin:
532 if self.hstdin:
533 CloseHandle(self.hstdin)
533 CloseHandle(self.hstdin)
534 self.hstdin = None
534 self.hstdin = None
535 if self.hstdout:
535 if self.hstdout:
536 CloseHandle(self.hstdout)
536 CloseHandle(self.hstdout)
537 self.hstdout = None
537 self.hstdout = None
538 if self.hstderr:
538 if self.hstderr:
539 CloseHandle(self.hstderr)
539 CloseHandle(self.hstderr)
540 self.hstderr = None
540 self.hstderr = None
541 if self.piProcInfo != None:
541 if self.piProcInfo != None:
542 CloseHandle(self.piProcInfo.hProcess)
542 CloseHandle(self.piProcInfo.hProcess)
543 CloseHandle(self.piProcInfo.hThread)
543 CloseHandle(self.piProcInfo.hThread)
544 self.piProcInfo = None
544 self.piProcInfo = None
545
545
546
546
547 def system(cmd):
547 def system(cmd):
548 """Win32 version of os.system() that works with network shares.
548 """Win32 version of os.system() that works with network shares.
549
549
550 Note that this implementation returns None, as meant for use in IPython.
550 Note that this implementation returns None, as meant for use in IPython.
551
551
552 Parameters
552 Parameters
553 ----------
553 ----------
554 cmd : str
554 cmd : str
555 A command to be executed in the system shell.
555 A command to be executed in the system shell.
556
556
557 Returns
557 Returns
558 -------
558 -------
559 None : we explicitly do NOT return the subprocess status code, as this
559 None : we explicitly do NOT return the subprocess status code, as this
560 utility is meant to be used extensively in IPython, where any return value
560 utility is meant to be used extensively in IPython, where any return value
561 would trigger :func:`sys.displayhook` calls.
561 would trigger :func:`sys.displayhook` calls.
562 """
562 """
563 with AvoidUNCPath() as path:
563 with AvoidUNCPath() as path:
564 if path is not None:
564 if path is not None:
565 cmd = '"pushd %s &&"%s' % (path, cmd)
565 cmd = '"pushd %s &&"%s' % (path, cmd)
566 with Win32ShellCommandController(cmd) as scc:
566 with Win32ShellCommandController(cmd) as scc:
567 scc.run()
567 scc.run()
568
568
569
569
570 if __name__ == "__main__":
570 if __name__ == "__main__":
571 print("Test starting!")
571 print("Test starting!")
572 #system("cmd")
572 #system("cmd")
573 system("python -i")
573 system("python -i")
574 print("Test finished!")
574 print("Test finished!")
@@ -1,173 +1,173 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 IO capturing utilities.
3 IO capturing utilities.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2013 The IPython Development Team
7 # Copyright (C) 2013 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 from __future__ import print_function
12 from __future__ import print_function
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 import sys
18 import sys
19 from StringIO import StringIO
19 from StringIO import StringIO
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Classes and functions
22 # Classes and functions
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25
25
26 class RichOutput(object):
26 class RichOutput(object):
27 def __init__(self, source="", data=None, metadata=None):
27 def __init__(self, source="", data=None, metadata=None):
28 self.source = source
28 self.source = source
29 self.data = data or {}
29 self.data = data or {}
30 self.metadata = metadata or {}
30 self.metadata = metadata or {}
31
31
32 def display(self):
32 def display(self):
33 from IPython.display import publish_display_data
33 from IPython.display import publish_display_data
34 publish_display_data(self.source, self.data, self.metadata)
34 publish_display_data(self.source, self.data, self.metadata)
35
35
36 def _repr_mime_(self, mime):
36 def _repr_mime_(self, mime):
37 if mime not in self.data:
37 if mime not in self.data:
38 return
38 return
39 data = self.data[mime]
39 data = self.data[mime]
40 if mime in self.metadata:
40 if mime in self.metadata:
41 return data, self.metadata[mime]
41 return data, self.metadata[mime]
42 else:
42 else:
43 return data
43 return data
44
44
45 def _repr_html_(self):
45 def _repr_html_(self):
46 return self._repr_mime_("text/html")
46 return self._repr_mime_("text/html")
47
47
48 def _repr_latex_(self):
48 def _repr_latex_(self):
49 return self._repr_mime_("text/latex")
49 return self._repr_mime_("text/latex")
50
50
51 def _repr_json_(self):
51 def _repr_json_(self):
52 return self._repr_mime_("application/json")
52 return self._repr_mime_("application/json")
53
53
54 def _repr_javascript_(self):
54 def _repr_javascript_(self):
55 return self._repr_mime_("application/javascript")
55 return self._repr_mime_("application/javascript")
56
56
57 def _repr_png_(self):
57 def _repr_png_(self):
58 return self._repr_mime_("image/png")
58 return self._repr_mime_("image/png")
59
59
60 def _repr_jpeg_(self):
60 def _repr_jpeg_(self):
61 return self._repr_mime_("image/jpeg")
61 return self._repr_mime_("image/jpeg")
62
62
63 def _repr_svg_(self):
63 def _repr_svg_(self):
64 return self._repr_mime_("image/svg+xml")
64 return self._repr_mime_("image/svg+xml")
65
65
66
66
67 class CapturedIO(object):
67 class CapturedIO(object):
68 """Simple object for containing captured stdout/err and rich display StringIO objects
68 """Simple object for containing captured stdout/err and rich display StringIO objects
69
69
70 Each instance `c` has three attributes:
70 Each instance `c` has three attributes:
71
71
72 c.stdout : standard output as a string
72 - ``c.stdout`` : standard output as a string
73 c.stderr : standard error as a string
73 - ``c.stderr`` : standard error as a string
74 c.outputs: a list of rich display outputs
74 - ``c.outputs``: a list of rich display outputs
75
75
76 Additionally, there's a `c.show()` method which will print all of the
76 Additionally, there's a ``c.show()`` method which will print all of the
77 above in the same order, and can be invoked simply via `c()`.
77 above in the same order, and can be invoked simply via ``c()``.
78 """
78 """
79
79
80 def __init__(self, stdout, stderr, outputs=None):
80 def __init__(self, stdout, stderr, outputs=None):
81 self._stdout = stdout
81 self._stdout = stdout
82 self._stderr = stderr
82 self._stderr = stderr
83 if outputs is None:
83 if outputs is None:
84 outputs = []
84 outputs = []
85 self._outputs = outputs
85 self._outputs = outputs
86
86
87 def __str__(self):
87 def __str__(self):
88 return self.stdout
88 return self.stdout
89
89
90 @property
90 @property
91 def stdout(self):
91 def stdout(self):
92 "Captured standard output"
92 "Captured standard output"
93 if not self._stdout:
93 if not self._stdout:
94 return ''
94 return ''
95 return self._stdout.getvalue()
95 return self._stdout.getvalue()
96
96
97 @property
97 @property
98 def stderr(self):
98 def stderr(self):
99 "Captured standard error"
99 "Captured standard error"
100 if not self._stderr:
100 if not self._stderr:
101 return ''
101 return ''
102 return self._stderr.getvalue()
102 return self._stderr.getvalue()
103
103
104 @property
104 @property
105 def outputs(self):
105 def outputs(self):
106 """A list of the captured rich display outputs, if any.
106 """A list of the captured rich display outputs, if any.
107
107
108 If you have a CapturedIO object `c`, these can be displayed in IPython
108 If you have a CapturedIO object ``c``, these can be displayed in IPython
109 using:
109 using::
110
110
111 from IPython.display import display
111 from IPython.display import display
112 for o in c.outputs:
112 for o in c.outputs:
113 display(o)
113 display(o)
114 """
114 """
115 return [ RichOutput(s, d, md) for s, d, md in self._outputs ]
115 return [ RichOutput(s, d, md) for s, d, md in self._outputs ]
116
116
117 def show(self):
117 def show(self):
118 """write my output to sys.stdout/err as appropriate"""
118 """write my output to sys.stdout/err as appropriate"""
119 sys.stdout.write(self.stdout)
119 sys.stdout.write(self.stdout)
120 sys.stderr.write(self.stderr)
120 sys.stderr.write(self.stderr)
121 sys.stdout.flush()
121 sys.stdout.flush()
122 sys.stderr.flush()
122 sys.stderr.flush()
123 for source, data, metadata in self._outputs:
123 for source, data, metadata in self._outputs:
124 RichOutput(source, data, metadata).display()
124 RichOutput(source, data, metadata).display()
125
125
126 __call__ = show
126 __call__ = show
127
127
128
128
129 class capture_output(object):
129 class capture_output(object):
130 """context manager for capturing stdout/err"""
130 """context manager for capturing stdout/err"""
131 stdout = True
131 stdout = True
132 stderr = True
132 stderr = True
133 display = True
133 display = True
134
134
135 def __init__(self, stdout=True, stderr=True, display=True):
135 def __init__(self, stdout=True, stderr=True, display=True):
136 self.stdout = stdout
136 self.stdout = stdout
137 self.stderr = stderr
137 self.stderr = stderr
138 self.display = display
138 self.display = display
139 self.shell = None
139 self.shell = None
140
140
141 def __enter__(self):
141 def __enter__(self):
142 from IPython.core.getipython import get_ipython
142 from IPython.core.getipython import get_ipython
143 from IPython.core.displaypub import CapturingDisplayPublisher
143 from IPython.core.displaypub import CapturingDisplayPublisher
144
144
145 self.sys_stdout = sys.stdout
145 self.sys_stdout = sys.stdout
146 self.sys_stderr = sys.stderr
146 self.sys_stderr = sys.stderr
147
147
148 if self.display:
148 if self.display:
149 self.shell = get_ipython()
149 self.shell = get_ipython()
150 if self.shell is None:
150 if self.shell is None:
151 self.save_display_pub = None
151 self.save_display_pub = None
152 self.display = False
152 self.display = False
153
153
154 stdout = stderr = outputs = None
154 stdout = stderr = outputs = None
155 if self.stdout:
155 if self.stdout:
156 stdout = sys.stdout = StringIO()
156 stdout = sys.stdout = StringIO()
157 if self.stderr:
157 if self.stderr:
158 stderr = sys.stderr = StringIO()
158 stderr = sys.stderr = StringIO()
159 if self.display:
159 if self.display:
160 self.save_display_pub = self.shell.display_pub
160 self.save_display_pub = self.shell.display_pub
161 self.shell.display_pub = CapturingDisplayPublisher()
161 self.shell.display_pub = CapturingDisplayPublisher()
162 outputs = self.shell.display_pub.outputs
162 outputs = self.shell.display_pub.outputs
163
163
164
164
165 return CapturedIO(stdout, stderr, outputs)
165 return CapturedIO(stdout, stderr, outputs)
166
166
167 def __exit__(self, exc_type, exc_value, traceback):
167 def __exit__(self, exc_type, exc_value, traceback):
168 sys.stdout = self.sys_stdout
168 sys.stdout = self.sys_stdout
169 sys.stderr = self.sys_stderr
169 sys.stderr = self.sys_stderr
170 if self.display and self.shell:
170 if self.display and self.shell:
171 self.shell.display_pub = self.save_display_pub
171 self.shell.display_pub = self.save_display_pub
172
172
173
173
@@ -1,71 +1,71 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Context managers for temporarily updating dictionaries.
3 Context managers for temporarily updating dictionaries.
4
4
5 Authors:
5 Authors:
6
6
7 * Bradley Froehle
7 * Bradley Froehle
8 """
8 """
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2012 The IPython Development Team
11 # Copyright (C) 2012 The IPython Development Team
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Code
18 # Code
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 class preserve_keys(object):
21 class preserve_keys(object):
22 """Preserve a set of keys in a dictionary.
22 """Preserve a set of keys in a dictionary.
23
23
24 Upon entering the context manager the current values of the keys
24 Upon entering the context manager the current values of the keys
25 will be saved. Upon exiting, the dictionary will be updated to
25 will be saved. Upon exiting, the dictionary will be updated to
26 restore the original value of the preserved keys. Preserved keys
26 restore the original value of the preserved keys. Preserved keys
27 which did not exist when entering the context manager will be
27 which did not exist when entering the context manager will be
28 deleted.
28 deleted.
29
29
30 Example
30 Examples
31 -------
31 --------
32
32
33 >>> d = {'a': 1, 'b': 2, 'c': 3}
33 >>> d = {'a': 1, 'b': 2, 'c': 3}
34 >>> with preserve_keys(d, 'b', 'c', 'd'):
34 >>> with preserve_keys(d, 'b', 'c', 'd'):
35 ... del d['a']
35 ... del d['a']
36 ... del d['b'] # will be reset to 2
36 ... del d['b'] # will be reset to 2
37 ... d['c'] = None # will be reset to 3
37 ... d['c'] = None # will be reset to 3
38 ... d['d'] = 4 # will be deleted
38 ... d['d'] = 4 # will be deleted
39 ... d['e'] = 5
39 ... d['e'] = 5
40 ... print(sorted(d.items()))
40 ... print(sorted(d.items()))
41 ...
41 ...
42 [('c', None), ('d', 4), ('e', 5)]
42 [('c', None), ('d', 4), ('e', 5)]
43 >>> print(sorted(d.items()))
43 >>> print(sorted(d.items()))
44 [('b', 2), ('c', 3), ('e', 5)]
44 [('b', 2), ('c', 3), ('e', 5)]
45 """
45 """
46
46
47 def __init__(self, dictionary, *keys):
47 def __init__(self, dictionary, *keys):
48 self.dictionary = dictionary
48 self.dictionary = dictionary
49 self.keys = keys
49 self.keys = keys
50
50
51 def __enter__(self):
51 def __enter__(self):
52 # Actions to perform upon exiting.
52 # Actions to perform upon exiting.
53 to_delete = []
53 to_delete = []
54 to_update = {}
54 to_update = {}
55
55
56 d = self.dictionary
56 d = self.dictionary
57 for k in self.keys:
57 for k in self.keys:
58 if k in d:
58 if k in d:
59 to_update[k] = d[k]
59 to_update[k] = d[k]
60 else:
60 else:
61 to_delete.append(k)
61 to_delete.append(k)
62
62
63 self.to_delete = to_delete
63 self.to_delete = to_delete
64 self.to_update = to_update
64 self.to_update = to_update
65
65
66 def __exit__(self, *exc_info):
66 def __exit__(self, *exc_info):
67 d = self.dictionary
67 d = self.dictionary
68
68
69 for k in self.to_delete:
69 for k in self.to_delete:
70 d.pop(k, None)
70 d.pop(k, None)
71 d.update(self.to_update)
71 d.update(self.to_update)
@@ -1,167 +1,169 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Utilities for getting information about IPython and the system it's running in.
3 Utilities for getting information about IPython and the system it's running in.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 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 import os
17 import os
18 import platform
18 import platform
19 import pprint
19 import pprint
20 import sys
20 import sys
21 import subprocess
21 import subprocess
22
22
23 from IPython.core import release
23 from IPython.core import release
24 from IPython.utils import py3compat, _sysinfo, encoding
24 from IPython.utils import py3compat, _sysinfo, encoding
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Code
27 # Code
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 def pkg_commit_hash(pkg_path):
30 def pkg_commit_hash(pkg_path):
31 """Get short form of commit hash given directory `pkg_path`
31 """Get short form of commit hash given directory `pkg_path`
32
32
33 We get the commit hash from (in order of preference):
33 We get the commit hash from (in order of preference):
34
34
35 * IPython.utils._sysinfo.commit
35 * IPython.utils._sysinfo.commit
36 * git output, if we are in a git repository
36 * git output, if we are in a git repository
37
37
38 If these fail, we return a not-found placeholder tuple
38 If these fail, we return a not-found placeholder tuple
39
39
40 Parameters
40 Parameters
41 ----------
41 ----------
42 pkg_path : str
42 pkg_path : str
43 directory containing package
43 directory containing package
44 only used for getting commit from active repo
44 only used for getting commit from active repo
45
45
46 Returns
46 Returns
47 -------
47 -------
48 hash_from : str
48 hash_from : str
49 Where we got the hash from - description
49 Where we got the hash from - description
50 hash_str : str
50 hash_str : str
51 short form of hash
51 short form of hash
52 """
52 """
53 # Try and get commit from written commit text file
53 # Try and get commit from written commit text file
54 if _sysinfo.commit:
54 if _sysinfo.commit:
55 return "installation", _sysinfo.commit
55 return "installation", _sysinfo.commit
56
56
57 # maybe we are in a repository
57 # maybe we are in a repository
58 proc = subprocess.Popen('git rev-parse --short HEAD',
58 proc = subprocess.Popen('git rev-parse --short HEAD',
59 stdout=subprocess.PIPE,
59 stdout=subprocess.PIPE,
60 stderr=subprocess.PIPE,
60 stderr=subprocess.PIPE,
61 cwd=pkg_path, shell=True)
61 cwd=pkg_path, shell=True)
62 repo_commit, _ = proc.communicate()
62 repo_commit, _ = proc.communicate()
63 if repo_commit:
63 if repo_commit:
64 return 'repository', repo_commit.strip()
64 return 'repository', repo_commit.strip()
65 return '(none found)', '<not found>'
65 return '(none found)', '<not found>'
66
66
67
67
68 def pkg_info(pkg_path):
68 def pkg_info(pkg_path):
69 """Return dict describing the context of this package
69 """Return dict describing the context of this package
70
70
71 Parameters
71 Parameters
72 ----------
72 ----------
73 pkg_path : str
73 pkg_path : str
74 path containing __init__.py for package
74 path containing __init__.py for package
75
75
76 Returns
76 Returns
77 -------
77 -------
78 context : dict
78 context : dict
79 with named parameters of interest
79 with named parameters of interest
80 """
80 """
81 src, hsh = pkg_commit_hash(pkg_path)
81 src, hsh = pkg_commit_hash(pkg_path)
82 return dict(
82 return dict(
83 ipython_version=release.version,
83 ipython_version=release.version,
84 ipython_path=pkg_path,
84 ipython_path=pkg_path,
85 codename=release.codename,
85 codename=release.codename,
86 commit_source=src,
86 commit_source=src,
87 commit_hash=hsh,
87 commit_hash=hsh,
88 sys_version=sys.version,
88 sys_version=sys.version,
89 sys_executable=sys.executable,
89 sys_executable=sys.executable,
90 sys_platform=sys.platform,
90 sys_platform=sys.platform,
91 platform=platform.platform(),
91 platform=platform.platform(),
92 os_name=os.name,
92 os_name=os.name,
93 default_encoding=encoding.DEFAULT_ENCODING,
93 default_encoding=encoding.DEFAULT_ENCODING,
94 )
94 )
95
95
96
96
97 @py3compat.doctest_refactor_print
97 @py3compat.doctest_refactor_print
98 def sys_info():
98 def sys_info():
99 """Return useful information about IPython and the system, as a string.
99 """Return useful information about IPython and the system, as a string.
100
100
101 Example
101 Examples
102 -------
102 --------
103 ::
104
103 In [2]: print sys_info()
105 In [2]: print sys_info()
104 {'commit_hash': '144fdae', # random
106 {'commit_hash': '144fdae', # random
105 'commit_source': 'repository',
107 'commit_source': 'repository',
106 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
108 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
107 'ipython_version': '0.11.dev',
109 'ipython_version': '0.11.dev',
108 'os_name': 'posix',
110 'os_name': 'posix',
109 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
111 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
110 'sys_executable': '/usr/bin/python',
112 'sys_executable': '/usr/bin/python',
111 'sys_platform': 'linux2',
113 'sys_platform': 'linux2',
112 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
114 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'}
113 """
115 """
114 p = os.path
116 p = os.path
115 path = p.dirname(p.abspath(p.join(__file__, '..')))
117 path = p.dirname(p.abspath(p.join(__file__, '..')))
116 return pprint.pformat(pkg_info(path))
118 return pprint.pformat(pkg_info(path))
117
119
118
120
119 def _num_cpus_unix():
121 def _num_cpus_unix():
120 """Return the number of active CPUs on a Unix system."""
122 """Return the number of active CPUs on a Unix system."""
121 return os.sysconf("SC_NPROCESSORS_ONLN")
123 return os.sysconf("SC_NPROCESSORS_ONLN")
122
124
123
125
124 def _num_cpus_darwin():
126 def _num_cpus_darwin():
125 """Return the number of active CPUs on a Darwin system."""
127 """Return the number of active CPUs on a Darwin system."""
126 p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
128 p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
127 return p.stdout.read()
129 return p.stdout.read()
128
130
129
131
130 def _num_cpus_windows():
132 def _num_cpus_windows():
131 """Return the number of active CPUs on a Windows system."""
133 """Return the number of active CPUs on a Windows system."""
132 return os.environ.get("NUMBER_OF_PROCESSORS")
134 return os.environ.get("NUMBER_OF_PROCESSORS")
133
135
134
136
135 def num_cpus():
137 def num_cpus():
136 """Return the effective number of CPUs in the system as an integer.
138 """Return the effective number of CPUs in the system as an integer.
137
139
138 This cross-platform function makes an attempt at finding the total number of
140 This cross-platform function makes an attempt at finding the total number of
139 available CPUs in the system, as returned by various underlying system and
141 available CPUs in the system, as returned by various underlying system and
140 python calls.
142 python calls.
141
143
142 If it can't find a sensible answer, it returns 1 (though an error *may* make
144 If it can't find a sensible answer, it returns 1 (though an error *may* make
143 it return a large positive number that's actually incorrect).
145 it return a large positive number that's actually incorrect).
144 """
146 """
145
147
146 # Many thanks to the Parallel Python project (http://www.parallelpython.com)
148 # Many thanks to the Parallel Python project (http://www.parallelpython.com)
147 # for the names of the keys we needed to look up for this function. This
149 # for the names of the keys we needed to look up for this function. This
148 # code was inspired by their equivalent function.
150 # code was inspired by their equivalent function.
149
151
150 ncpufuncs = {'Linux':_num_cpus_unix,
152 ncpufuncs = {'Linux':_num_cpus_unix,
151 'Darwin':_num_cpus_darwin,
153 'Darwin':_num_cpus_darwin,
152 'Windows':_num_cpus_windows,
154 'Windows':_num_cpus_windows,
153 # On Vista, python < 2.5.2 has a bug and returns 'Microsoft'
155 # On Vista, python < 2.5.2 has a bug and returns 'Microsoft'
154 # See http://bugs.python.org/issue1082 for details.
156 # See http://bugs.python.org/issue1082 for details.
155 'Microsoft':_num_cpus_windows,
157 'Microsoft':_num_cpus_windows,
156 }
158 }
157
159
158 ncpufunc = ncpufuncs.get(platform.system(),
160 ncpufunc = ncpufuncs.get(platform.system(),
159 # default to unix version (Solaris, AIX, etc)
161 # default to unix version (Solaris, AIX, etc)
160 _num_cpus_unix)
162 _num_cpus_unix)
161
163
162 try:
164 try:
163 ncpus = max(1,int(ncpufunc()))
165 ncpus = max(1,int(ncpufunc()))
164 except:
166 except:
165 ncpus = 1
167 ncpus = 1
166 return ncpus
168 return ncpus
167
169
General Comments 0
You need to be logged in to leave comments. Login now