##// END OF EJS Templates
fix for list pdb listcommand, broken for %run -d...
Jörgen Stenarson -
Show More
@@ -1,551 +1,557 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 linecache
30 import linecache
31 import sys
31 import sys
32
32
33 from IPython.utils import PyColorize, py3compat
33 from IPython.utils import PyColorize, py3compat
34 from IPython.core import ipapi
34 from IPython.core import ipapi
35 from IPython.utils import coloransi, io, openpy
35 from IPython.utils import coloransi, io, openpy
36 from IPython.core.excolors import exception_colors
36 from IPython.core.excolors import exception_colors
37
37
38 # See if we can use pydb.
38 # See if we can use pydb.
39 has_pydb = False
39 has_pydb = False
40 prompt = 'ipdb> '
40 prompt = 'ipdb> '
41 #We have to check this directly from sys.argv, config struct not yet available
41 #We have to check this directly from sys.argv, config struct not yet available
42 if '--pydb' in sys.argv:
42 if '--pydb' in sys.argv:
43 try:
43 try:
44 import pydb
44 import pydb
45 if hasattr(pydb.pydb, "runl") and pydb.version>'1.17':
45 if hasattr(pydb.pydb, "runl") and pydb.version>'1.17':
46 # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we
46 # Version 1.17 is broken, and that's what ships with Ubuntu Edgy, so we
47 # better protect against it.
47 # better protect against it.
48 has_pydb = True
48 has_pydb = True
49 except ImportError:
49 except ImportError:
50 print("Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available")
50 print("Pydb (http://bashdb.sourceforge.net/pydb/) does not seem to be available")
51
51
52 if has_pydb:
52 if has_pydb:
53 from pydb import Pdb as OldPdb
53 from pydb import Pdb as OldPdb
54 #print "Using pydb for %run -d and post-mortem" #dbg
54 #print "Using pydb for %run -d and post-mortem" #dbg
55 prompt = 'ipydb> '
55 prompt = 'ipydb> '
56 else:
56 else:
57 from pdb import Pdb as OldPdb
57 from pdb import Pdb as OldPdb
58
58
59 # Allow the set_trace code to operate outside of an ipython instance, even if
59 # Allow the set_trace code to operate outside of an ipython instance, even if
60 # it does so with some limitations. The rest of this support is implemented in
60 # it does so with some limitations. The rest of this support is implemented in
61 # the Tracer constructor.
61 # the Tracer constructor.
62 def BdbQuit_excepthook(et,ev,tb):
62 def BdbQuit_excepthook(et,ev,tb):
63 if et==bdb.BdbQuit:
63 if et==bdb.BdbQuit:
64 print('Exiting Debugger.')
64 print('Exiting Debugger.')
65 else:
65 else:
66 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
66 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
67
67
68 def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None):
68 def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None):
69 print('Exiting Debugger.')
69 print('Exiting Debugger.')
70
70
71
71
72 class Tracer(object):
72 class Tracer(object):
73 """Class for local debugging, similar to pdb.set_trace.
73 """Class for local debugging, similar to pdb.set_trace.
74
74
75 Instances of this class, when called, behave like pdb.set_trace, but
75 Instances of this class, when called, behave like pdb.set_trace, but
76 providing IPython's enhanced capabilities.
76 providing IPython's enhanced capabilities.
77
77
78 This is implemented as a class which must be initialized in your own code
78 This is implemented as a class which must be initialized in your own code
79 and not as a standalone function because we need to detect at runtime
79 and not as a standalone function because we need to detect at runtime
80 whether IPython is already active or not. That detection is done in the
80 whether IPython is already active or not. That detection is done in the
81 constructor, ensuring that this code plays nicely with a running IPython,
81 constructor, ensuring that this code plays nicely with a running IPython,
82 while functioning acceptably (though with limitations) if outside of it.
82 while functioning acceptably (though with limitations) if outside of it.
83 """
83 """
84
84
85 def __init__(self,colors=None):
85 def __init__(self,colors=None):
86 """Create a local debugger instance.
86 """Create a local debugger instance.
87
87
88 :Parameters:
88 :Parameters:
89
89
90 - `colors` (None): a string containing the name of the color scheme to
90 - `colors` (None): a string containing the name of the color scheme to
91 use, it must be one of IPython's valid color schemes. If not given, the
91 use, it must be one of IPython's valid color schemes. If not given, the
92 function will default to the current IPython scheme when running inside
92 function will default to the current IPython scheme when running inside
93 IPython, and to 'NoColor' otherwise.
93 IPython, and to 'NoColor' otherwise.
94
94
95 Usage example:
95 Usage example:
96
96
97 from IPython.core.debugger import Tracer; debug_here = Tracer()
97 from IPython.core.debugger import Tracer; debug_here = Tracer()
98
98
99 ... later in your code
99 ... later in your code
100 debug_here() # -> will open up the debugger at that point.
100 debug_here() # -> will open up the debugger at that point.
101
101
102 Once the debugger activates, you can use all of its regular commands to
102 Once the debugger activates, you can use all of its regular commands to
103 step through code, set breakpoints, etc. See the pdb documentation
103 step through code, set breakpoints, etc. See the pdb documentation
104 from the Python standard library for usage details.
104 from the Python standard library for usage details.
105 """
105 """
106
106
107 try:
107 try:
108 ip = get_ipython()
108 ip = get_ipython()
109 except NameError:
109 except NameError:
110 # Outside of ipython, we set our own exception hook manually
110 # Outside of ipython, we set our own exception hook manually
111 BdbQuit_excepthook.excepthook_ori = sys.excepthook
111 BdbQuit_excepthook.excepthook_ori = sys.excepthook
112 sys.excepthook = BdbQuit_excepthook
112 sys.excepthook = BdbQuit_excepthook
113 def_colors = 'NoColor'
113 def_colors = 'NoColor'
114 try:
114 try:
115 # Limited tab completion support
115 # Limited tab completion support
116 import readline
116 import readline
117 readline.parse_and_bind('tab: complete')
117 readline.parse_and_bind('tab: complete')
118 except ImportError:
118 except ImportError:
119 pass
119 pass
120 else:
120 else:
121 # In ipython, we use its custom exception handler mechanism
121 # In ipython, we use its custom exception handler mechanism
122 def_colors = ip.colors
122 def_colors = ip.colors
123 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
123 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
124
124
125 if colors is None:
125 if colors is None:
126 colors = def_colors
126 colors = def_colors
127
127
128 # The stdlib debugger internally uses a modified repr from the `repr`
128 # The stdlib debugger internally uses a modified repr from the `repr`
129 # module, that limits the length of printed strings to a hardcoded
129 # module, that limits the length of printed strings to a hardcoded
130 # limit of 30 characters. That much trimming is too aggressive, let's
130 # limit of 30 characters. That much trimming is too aggressive, let's
131 # at least raise that limit to 80 chars, which should be enough for
131 # at least raise that limit to 80 chars, which should be enough for
132 # most interactive uses.
132 # most interactive uses.
133 try:
133 try:
134 from repr import aRepr
134 from repr import aRepr
135 aRepr.maxstring = 80
135 aRepr.maxstring = 80
136 except:
136 except:
137 # This is only a user-facing convenience, so any error we encounter
137 # This is only a user-facing convenience, so any error we encounter
138 # here can be warned about but can be otherwise ignored. These
138 # here can be warned about but can be otherwise ignored. These
139 # printouts will tell us about problems if this API changes
139 # printouts will tell us about problems if this API changes
140 import traceback
140 import traceback
141 traceback.print_exc()
141 traceback.print_exc()
142
142
143 self.debugger = Pdb(colors)
143 self.debugger = Pdb(colors)
144
144
145 def __call__(self):
145 def __call__(self):
146 """Starts an interactive debugger at the point where called.
146 """Starts an interactive debugger at the point where called.
147
147
148 This is similar to the pdb.set_trace() function from the std lib, but
148 This is similar to the pdb.set_trace() function from the std lib, but
149 using IPython's enhanced debugger."""
149 using IPython's enhanced debugger."""
150
150
151 self.debugger.set_trace(sys._getframe().f_back)
151 self.debugger.set_trace(sys._getframe().f_back)
152
152
153
153
154 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
154 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
155 """Make new_fn have old_fn's doc string. This is particularly useful
155 """Make new_fn have old_fn's doc string. This is particularly useful
156 for the do_... commands that hook into the help system.
156 for the do_... commands that hook into the help system.
157 Adapted from from a comp.lang.python posting
157 Adapted from from a comp.lang.python posting
158 by Duncan Booth."""
158 by Duncan Booth."""
159 def wrapper(*args, **kw):
159 def wrapper(*args, **kw):
160 return new_fn(*args, **kw)
160 return new_fn(*args, **kw)
161 if old_fn.__doc__:
161 if old_fn.__doc__:
162 wrapper.__doc__ = old_fn.__doc__ + additional_text
162 wrapper.__doc__ = old_fn.__doc__ + additional_text
163 return wrapper
163 return wrapper
164
164
165
165
166 def _file_lines(fname):
166 def _file_lines(fname):
167 """Return the contents of a named file as a list of lines.
167 """Return the contents of a named file as a list of lines.
168
168
169 This function never raises an IOError exception: if the file can't be
169 This function never raises an IOError exception: if the file can't be
170 read, it simply returns an empty list."""
170 read, it simply returns an empty list."""
171
171
172 try:
172 try:
173 outfile = open(fname)
173 outfile = open(fname)
174 except IOError:
174 except IOError:
175 return []
175 return []
176 else:
176 else:
177 out = outfile.readlines()
177 out = outfile.readlines()
178 outfile.close()
178 outfile.close()
179 return out
179 return out
180
180
181
181
182 def _readline(x):
182 def _readline(x):
183 """helper to pop elements off list of string
183 """helper to pop elements off list of string
184
184
185 call with list of strings, return readline function that will pop
185 call with list of strings, return readline function that will pop
186 one line off the beginning of a copy of the list with each call.
186 one line off the beginning of a copy of the list with each call.
187 raise StopIteration when empty or on third call
187 raise StopIteration when empty or on third call
188 """
188 """
189 x = x[:2]
189 x = x[:2]
190 def readline():
190 def readline():
191 if x:
191 if x:
192 return x.pop(0)
192 return x.pop(0)
193 else:
193 else:
194 raise StopIteration
194 raise StopIteration
195 return readline
195 return readline
196
196
197
197
198 class Pdb(OldPdb):
198 class Pdb(OldPdb):
199 """Modified Pdb class, does not load readline."""
199 """Modified Pdb class, does not load readline."""
200
200
201 def __init__(self,color_scheme='NoColor',completekey=None,
201 def __init__(self,color_scheme='NoColor',completekey=None,
202 stdin=None, stdout=None):
202 stdin=None, stdout=None):
203
203
204 # Parent constructor:
204 # Parent constructor:
205 if has_pydb and completekey is None:
205 if has_pydb and completekey is None:
206 OldPdb.__init__(self,stdin=stdin,stdout=io.stdout)
206 OldPdb.__init__(self,stdin=stdin,stdout=io.stdout)
207 else:
207 else:
208 OldPdb.__init__(self,completekey,stdin,stdout)
208 OldPdb.__init__(self,completekey,stdin,stdout)
209
209
210 self.prompt = prompt # The default prompt is '(Pdb)'
210 self.prompt = prompt # The default prompt is '(Pdb)'
211
211
212 # IPython changes...
212 # IPython changes...
213 self.is_pydb = has_pydb
213 self.is_pydb = has_pydb
214
214
215 self.shell = ipapi.get()
215 self.shell = ipapi.get()
216
216
217 if self.is_pydb:
217 if self.is_pydb:
218
218
219 # interactiveshell.py's ipalias seems to want pdb's checkline
219 # interactiveshell.py's ipalias seems to want pdb's checkline
220 # which located in pydb.fn
220 # which located in pydb.fn
221 import pydb.fns
221 import pydb.fns
222 self.checkline = lambda filename, lineno: \
222 self.checkline = lambda filename, lineno: \
223 pydb.fns.checkline(self, filename, lineno)
223 pydb.fns.checkline(self, filename, lineno)
224
224
225 self.curframe = None
225 self.curframe = None
226 self.do_restart = self.new_do_restart
226 self.do_restart = self.new_do_restart
227
227
228 self.old_all_completions = self.shell.Completer.all_completions
228 self.old_all_completions = self.shell.Completer.all_completions
229 self.shell.Completer.all_completions=self.all_completions
229 self.shell.Completer.all_completions=self.all_completions
230
230
231 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
231 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
232 OldPdb.do_list)
232 OldPdb.do_list)
233 self.do_l = self.do_list
233 self.do_l = self.do_list
234 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
234 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
235 OldPdb.do_frame)
235 OldPdb.do_frame)
236
236
237 self.aliases = {}
237 self.aliases = {}
238
238
239 # Create color table: we copy the default one from the traceback
239 # Create color table: we copy the default one from the traceback
240 # module and add a few attributes needed for debugging
240 # module and add a few attributes needed for debugging
241 self.color_scheme_table = exception_colors()
241 self.color_scheme_table = exception_colors()
242
242
243 # shorthands
243 # shorthands
244 C = coloransi.TermColors
244 C = coloransi.TermColors
245 cst = self.color_scheme_table
245 cst = self.color_scheme_table
246
246
247 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
247 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
248 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
248 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
249
249
250 cst['Linux'].colors.breakpoint_enabled = C.LightRed
250 cst['Linux'].colors.breakpoint_enabled = C.LightRed
251 cst['Linux'].colors.breakpoint_disabled = C.Red
251 cst['Linux'].colors.breakpoint_disabled = C.Red
252
252
253 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
253 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
254 cst['LightBG'].colors.breakpoint_disabled = C.Red
254 cst['LightBG'].colors.breakpoint_disabled = C.Red
255
255
256 self.set_colors(color_scheme)
256 self.set_colors(color_scheme)
257
257
258 # Add a python parser so we can syntax highlight source while
258 # Add a python parser so we can syntax highlight source while
259 # debugging.
259 # debugging.
260 self.parser = PyColorize.Parser()
260 self.parser = PyColorize.Parser()
261
261
262 def set_colors(self, scheme):
262 def set_colors(self, scheme):
263 """Shorthand access to the color table scheme selector method."""
263 """Shorthand access to the color table scheme selector method."""
264 self.color_scheme_table.set_active_scheme(scheme)
264 self.color_scheme_table.set_active_scheme(scheme)
265
265
266 def interaction(self, frame, traceback):
266 def interaction(self, frame, traceback):
267 self.shell.set_completer_frame(frame)
267 self.shell.set_completer_frame(frame)
268 OldPdb.interaction(self, frame, traceback)
268 OldPdb.interaction(self, frame, traceback)
269
269
270 def new_do_up(self, arg):
270 def new_do_up(self, arg):
271 OldPdb.do_up(self, arg)
271 OldPdb.do_up(self, arg)
272 self.shell.set_completer_frame(self.curframe)
272 self.shell.set_completer_frame(self.curframe)
273 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
273 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
274
274
275 def new_do_down(self, arg):
275 def new_do_down(self, arg):
276 OldPdb.do_down(self, arg)
276 OldPdb.do_down(self, arg)
277 self.shell.set_completer_frame(self.curframe)
277 self.shell.set_completer_frame(self.curframe)
278
278
279 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
279 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
280
280
281 def new_do_frame(self, arg):
281 def new_do_frame(self, arg):
282 OldPdb.do_frame(self, arg)
282 OldPdb.do_frame(self, arg)
283 self.shell.set_completer_frame(self.curframe)
283 self.shell.set_completer_frame(self.curframe)
284
284
285 def new_do_quit(self, arg):
285 def new_do_quit(self, arg):
286
286
287 if hasattr(self, 'old_all_completions'):
287 if hasattr(self, 'old_all_completions'):
288 self.shell.Completer.all_completions=self.old_all_completions
288 self.shell.Completer.all_completions=self.old_all_completions
289
289
290
290
291 return OldPdb.do_quit(self, arg)
291 return OldPdb.do_quit(self, arg)
292
292
293 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
293 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
294
294
295 def new_do_restart(self, arg):
295 def new_do_restart(self, arg):
296 """Restart command. In the context of ipython this is exactly the same
296 """Restart command. In the context of ipython this is exactly the same
297 thing as 'quit'."""
297 thing as 'quit'."""
298 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
298 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
299 return self.do_quit(arg)
299 return self.do_quit(arg)
300
300
301 def postloop(self):
301 def postloop(self):
302 self.shell.set_completer_frame(None)
302 self.shell.set_completer_frame(None)
303
303
304 def print_stack_trace(self):
304 def print_stack_trace(self):
305 try:
305 try:
306 for frame_lineno in self.stack:
306 for frame_lineno in self.stack:
307 self.print_stack_entry(frame_lineno, context = 5)
307 self.print_stack_entry(frame_lineno, context = 5)
308 except KeyboardInterrupt:
308 except KeyboardInterrupt:
309 pass
309 pass
310
310
311 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
311 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
312 context = 3):
312 context = 3):
313 #frame, lineno = frame_lineno
313 #frame, lineno = frame_lineno
314 print(self.format_stack_entry(frame_lineno, '', context), file=io.stdout)
314 print(self.format_stack_entry(frame_lineno, '', context), file=io.stdout)
315
315
316 # vds: >>
316 # vds: >>
317 frame, lineno = frame_lineno
317 frame, lineno = frame_lineno
318 filename = frame.f_code.co_filename
318 filename = frame.f_code.co_filename
319 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
319 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
320 # vds: <<
320 # vds: <<
321
321
322 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
322 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
323 import linecache, repr
323 import linecache, repr
324
324
325 ret = []
325 ret = []
326
326
327 Colors = self.color_scheme_table.active_colors
327 Colors = self.color_scheme_table.active_colors
328 ColorsNormal = Colors.Normal
328 ColorsNormal = Colors.Normal
329 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
329 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
330 tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
330 tpl_call = '%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
331 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
331 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
332 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
332 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
333 ColorsNormal)
333 ColorsNormal)
334
334
335 frame, lineno = frame_lineno
335 frame, lineno = frame_lineno
336
336
337 return_value = ''
337 return_value = ''
338 if '__return__' in frame.f_locals:
338 if '__return__' in frame.f_locals:
339 rv = frame.f_locals['__return__']
339 rv = frame.f_locals['__return__']
340 #return_value += '->'
340 #return_value += '->'
341 return_value += repr.repr(rv) + '\n'
341 return_value += repr.repr(rv) + '\n'
342 ret.append(return_value)
342 ret.append(return_value)
343
343
344 #s = filename + '(' + `lineno` + ')'
344 #s = filename + '(' + `lineno` + ')'
345 filename = self.canonic(frame.f_code.co_filename)
345 filename = self.canonic(frame.f_code.co_filename)
346 link = tpl_link % filename
346 link = tpl_link % filename
347
347
348 if frame.f_code.co_name:
348 if frame.f_code.co_name:
349 func = frame.f_code.co_name
349 func = frame.f_code.co_name
350 else:
350 else:
351 func = "<lambda>"
351 func = "<lambda>"
352
352
353 call = ''
353 call = ''
354 if func != '?':
354 if func != '?':
355 if '__args__' in frame.f_locals:
355 if '__args__' in frame.f_locals:
356 args = repr.repr(frame.f_locals['__args__'])
356 args = repr.repr(frame.f_locals['__args__'])
357 else:
357 else:
358 args = '()'
358 args = '()'
359 call = tpl_call % (func, args)
359 call = tpl_call % (func, args)
360
360
361 # The level info should be generated in the same format pdb uses, to
361 # The level info should be generated in the same format pdb uses, to
362 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
362 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
363 if frame is self.curframe:
363 if frame is self.curframe:
364 ret.append('> ')
364 ret.append('> ')
365 else:
365 else:
366 ret.append(' ')
366 ret.append(' ')
367 ret.append('%s(%s)%s\n' % (link,lineno,call))
367 ret.append('%s(%s)%s\n' % (link,lineno,call))
368
368
369 start = lineno - 1 - context//2
369 start = lineno - 1 - context//2
370 lines = linecache.getlines(filename)
370 lines = linecache.getlines(filename)
371 try:
371 try:
372 encoding, _ = openpy.detect_encoding(_readline(lines))
372 encoding, _ = openpy.detect_encoding(_readline(lines))
373 except SyntaxError:
373 except SyntaxError:
374 encoding = "ascii"
374 encoding = "ascii"
375 start = max(start, 0)
375 start = max(start, 0)
376 start = min(start, len(lines) - context)
376 start = min(start, len(lines) - context)
377 lines = lines[start : start + context]
377 lines = lines[start : start + context]
378
378
379 for i,line in enumerate(lines):
379 for i,line in enumerate(lines):
380 show_arrow = (start + 1 + i == lineno)
380 show_arrow = (start + 1 + i == lineno)
381 linetpl = (frame is self.curframe or show_arrow) \
381 linetpl = (frame is self.curframe or show_arrow) \
382 and tpl_line_em \
382 and tpl_line_em \
383 or tpl_line
383 or tpl_line
384 ret.append(self.__format_line(linetpl, filename,
384 ret.append(self.__format_line(linetpl, filename,
385 start + 1 + i, py3compat.cast_unicode(line),
385 start + 1 + i, py3compat.cast_unicode(line),
386 arrow = show_arrow) )
386 arrow = show_arrow) )
387 return ''.join(ret)
387 return ''.join(ret)
388
388
389 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
389 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
390 bp_mark = ""
390 bp_mark = ""
391 bp_mark_color = ""
391 bp_mark_color = ""
392
392
393 scheme = self.color_scheme_table.active_scheme_name
393 scheme = self.color_scheme_table.active_scheme_name
394 new_line, err = self.parser.format2(line, 'str', scheme)
394 new_line, err = self.parser.format2(line, 'str', scheme)
395 if not err: line = new_line
395 if not err: line = new_line
396
396
397 bp = None
397 bp = None
398 if lineno in self.get_file_breaks(filename):
398 if lineno in self.get_file_breaks(filename):
399 bps = self.get_breaks(filename, lineno)
399 bps = self.get_breaks(filename, lineno)
400 bp = bps[-1]
400 bp = bps[-1]
401
401
402 if bp:
402 if bp:
403 Colors = self.color_scheme_table.active_colors
403 Colors = self.color_scheme_table.active_colors
404 bp_mark = str(bp.number)
404 bp_mark = str(bp.number)
405 bp_mark_color = Colors.breakpoint_enabled
405 bp_mark_color = Colors.breakpoint_enabled
406 if not bp.enabled:
406 if not bp.enabled:
407 bp_mark_color = Colors.breakpoint_disabled
407 bp_mark_color = Colors.breakpoint_disabled
408
408
409 numbers_width = 7
409 numbers_width = 7
410 if arrow:
410 if arrow:
411 # This is the line with the error
411 # This is the line with the error
412 pad = numbers_width - len(str(lineno)) - len(bp_mark)
412 pad = numbers_width - len(str(lineno)) - len(bp_mark)
413 if pad >= 3:
413 if pad >= 3:
414 marker = '-'*(pad-3) + '-> '
414 marker = '-'*(pad-3) + '-> '
415 elif pad == 2:
415 elif pad == 2:
416 marker = '> '
416 marker = '> '
417 elif pad == 1:
417 elif pad == 1:
418 marker = '>'
418 marker = '>'
419 else:
419 else:
420 marker = ''
420 marker = ''
421 num = '%s%s' % (marker, str(lineno))
421 num = '%s%s' % (marker, str(lineno))
422 line = tpl_line % (bp_mark_color + bp_mark, num, line)
422 line = tpl_line % (bp_mark_color + bp_mark, num, line)
423 else:
423 else:
424 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
424 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
425 line = tpl_line % (bp_mark_color + bp_mark, num, line)
425 line = tpl_line % (bp_mark_color + bp_mark, num, line)
426
426
427 return line
427 return line
428
428
429 def list_command_pydb(self, arg):
429 def list_command_pydb(self, arg):
430 """List command to use if we have a newer pydb installed"""
430 """List command to use if we have a newer pydb installed"""
431 filename, first, last = OldPdb.parse_list_cmd(self, arg)
431 filename, first, last = OldPdb.parse_list_cmd(self, arg)
432 if filename is not None:
432 if filename is not None:
433 self.print_list_lines(filename, first, last)
433 self.print_list_lines(filename, first, last)
434
434
435 def print_list_lines(self, filename, first, last):
435 def print_list_lines(self, filename, first, last):
436 """The printing (as opposed to the parsing part of a 'list'
436 """The printing (as opposed to the parsing part of a 'list'
437 command."""
437 command."""
438 try:
438 try:
439 Colors = self.color_scheme_table.active_colors
439 Colors = self.color_scheme_table.active_colors
440 ColorsNormal = Colors.Normal
440 ColorsNormal = Colors.Normal
441 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
441 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
442 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
442 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
443 src = []
443 src = []
444 lines = linecache.getlines(filename)
444 if filename == "<string>" and hasattr(self, "_exec_filename"):
445 lines = list(open(self._exec_filename))
446 else:
447 lines = linecache.getlines(filename)
445 try:
448 try:
446 encoding, _ = openpy.detect_encoding(_readline(lines))
449 encoding, _ = openpy.detect_encoding(_readline(lines))
447 except SyntaxError:
450 except SyntaxError:
448 encoding = "ascii"
451 encoding = "ascii"
452 if not lines:
453 print >>io.stdout, "No src could be located using filename: %r"%filename
454 return #Bailing out, there is nothing to see here
449 for lineno in range(first, last+1):
455 for lineno in range(first, last+1):
450 line = py3compat.cast_unicode(lines[lineno])
456 line = py3compat.cast_unicode(lines[lineno])
451 if not line:
457 if not line:
452 break
458 break
453
459
454 if lineno == self.curframe.f_lineno:
460 if lineno == self.curframe.f_lineno:
455 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
461 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
456 else:
462 else:
457 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
463 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
458
464
459 src.append(line)
465 src.append(line)
460 self.lineno = lineno
466 self.lineno = lineno
461
467
462 print(''.join(src), file=io.stdout)
468 print(''.join(src), file=io.stdout)
463
469
464 except KeyboardInterrupt:
470 except KeyboardInterrupt:
465 pass
471 pass
466
472
467 def do_list(self, arg):
473 def do_list(self, arg):
468 self.lastcmd = 'list'
474 self.lastcmd = 'list'
469 last = None
475 last = None
470 if arg:
476 if arg:
471 try:
477 try:
472 x = eval(arg, {}, {})
478 x = eval(arg, {}, {})
473 if type(x) == type(()):
479 if type(x) == type(()):
474 first, last = x
480 first, last = x
475 first = int(first)
481 first = int(first)
476 last = int(last)
482 last = int(last)
477 if last < first:
483 if last < first:
478 # Assume it's a count
484 # Assume it's a count
479 last = first + last
485 last = first + last
480 else:
486 else:
481 first = max(1, int(x) - 5)
487 first = max(1, int(x) - 5)
482 except:
488 except:
483 print('*** Error in argument:', repr(arg))
489 print('*** Error in argument:', repr(arg))
484 return
490 return
485 elif self.lineno is None:
491 elif self.lineno is None:
486 first = max(1, self.curframe.f_lineno - 5)
492 first = max(1, self.curframe.f_lineno - 5)
487 else:
493 else:
488 first = self.lineno + 1
494 first = self.lineno + 1
489 if last is None:
495 if last is None:
490 last = first + 10
496 last = first + 10
491 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
497 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
492
498
493 # vds: >>
499 # vds: >>
494 lineno = first
500 lineno = first
495 filename = self.curframe.f_code.co_filename
501 filename = self.curframe.f_code.co_filename
496 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
502 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
497 # vds: <<
503 # vds: <<
498
504
499 do_l = do_list
505 do_l = do_list
500
506
501 def do_pdef(self, arg):
507 def do_pdef(self, arg):
502 """The debugger interface to magic_pdef"""
508 """The debugger interface to magic_pdef"""
503 namespaces = [('Locals', self.curframe.f_locals),
509 namespaces = [('Locals', self.curframe.f_locals),
504 ('Globals', self.curframe.f_globals)]
510 ('Globals', self.curframe.f_globals)]
505 self.shell.find_line_magic('pdef')(arg, namespaces=namespaces)
511 self.shell.find_line_magic('pdef')(arg, namespaces=namespaces)
506
512
507 def do_pdoc(self, arg):
513 def do_pdoc(self, arg):
508 """The debugger interface to magic_pdoc"""
514 """The debugger interface to magic_pdoc"""
509 namespaces = [('Locals', self.curframe.f_locals),
515 namespaces = [('Locals', self.curframe.f_locals),
510 ('Globals', self.curframe.f_globals)]
516 ('Globals', self.curframe.f_globals)]
511 self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces)
517 self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces)
512
518
513 def do_pinfo(self, arg):
519 def do_pinfo(self, arg):
514 """The debugger equivalant of ?obj"""
520 """The debugger equivalant of ?obj"""
515 namespaces = [('Locals', self.curframe.f_locals),
521 namespaces = [('Locals', self.curframe.f_locals),
516 ('Globals', self.curframe.f_globals)]
522 ('Globals', self.curframe.f_globals)]
517 self.shell.find_line_magic('pinfo')("pinfo %s" % arg,
523 self.shell.find_line_magic('pinfo')("pinfo %s" % arg,
518 namespaces=namespaces)
524 namespaces=namespaces)
519
525
520 def checkline(self, filename, lineno):
526 def checkline(self, filename, lineno):
521 """Check whether specified line seems to be executable.
527 """Check whether specified line seems to be executable.
522
528
523 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
529 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
524 line or EOF). Warning: testing is not comprehensive.
530 line or EOF). Warning: testing is not comprehensive.
525 """
531 """
526 #######################################################################
532 #######################################################################
527 # XXX Hack! Use python-2.5 compatible code for this call, because with
533 # XXX Hack! Use python-2.5 compatible code for this call, because with
528 # all of our changes, we've drifted from the pdb api in 2.6. For now,
534 # all of our changes, we've drifted from the pdb api in 2.6. For now,
529 # changing:
535 # changing:
530 #
536 #
531 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
537 #line = linecache.getline(filename, lineno, self.curframe.f_globals)
532 # to:
538 # to:
533 #
539 #
534 line = linecache.getline(filename, lineno)
540 line = linecache.getline(filename, lineno)
535 #
541 #
536 # does the trick. But in reality, we need to fix this by reconciling
542 # does the trick. But in reality, we need to fix this by reconciling
537 # our updates with the new Pdb APIs in Python 2.6.
543 # our updates with the new Pdb APIs in Python 2.6.
538 #
544 #
539 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
545 # End hack. The rest of this method is copied verbatim from 2.6 pdb.py
540 #######################################################################
546 #######################################################################
541
547
542 if not line:
548 if not line:
543 print('End of file', file=self.stdout)
549 print('End of file', file=self.stdout)
544 return 0
550 return 0
545 line = line.strip()
551 line = line.strip()
546 # Don't allow setting breakpoint at a blank line
552 # Don't allow setting breakpoint at a blank line
547 if (not line or (line[0] == '#') or
553 if (not line or (line[0] == '#') or
548 (line[:3] == '"""') or line[:3] == "'''"):
554 (line[:3] == '"""') or line[:3] == "'''"):
549 print('*** Blank or comment', file=self.stdout)
555 print('*** Blank or comment', file=self.stdout)
550 return 0
556 return 0
551 return lineno
557 return lineno
@@ -1,1014 +1,1016 b''
1 """Implementation of execution-related magic functions.
1 """Implementation of execution-related magic functions.
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 __builtin__ as builtin_mod
16 import __builtin__ as builtin_mod
17 import bdb
17 import bdb
18 import os
18 import os
19 import sys
19 import sys
20 import time
20 import time
21 from StringIO import StringIO
21 from StringIO import StringIO
22
22
23 # cProfile was added in Python2.5
23 # cProfile was added in Python2.5
24 try:
24 try:
25 import cProfile as profile
25 import cProfile as profile
26 import pstats
26 import pstats
27 except ImportError:
27 except ImportError:
28 # profile isn't bundled by default in Debian for license reasons
28 # profile isn't bundled by default in Debian for license reasons
29 try:
29 try:
30 import profile, pstats
30 import profile, pstats
31 except ImportError:
31 except ImportError:
32 profile = pstats = None
32 profile = pstats = None
33
33
34 # Our own packages
34 # Our own packages
35 from IPython.core import debugger, oinspect
35 from IPython.core import debugger, oinspect
36 from IPython.core import magic_arguments
36 from IPython.core import magic_arguments
37 from IPython.core import page
37 from IPython.core import page
38 from IPython.core.error import UsageError
38 from IPython.core.error import UsageError
39 from IPython.core.macro import Macro
39 from IPython.core.macro import Macro
40 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
40 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
41 line_cell_magic, on_off, needs_local_scope)
41 line_cell_magic, on_off, needs_local_scope)
42 from IPython.testing.skipdoctest import skip_doctest
42 from IPython.testing.skipdoctest import skip_doctest
43 from IPython.utils import py3compat
43 from IPython.utils import py3compat
44 from IPython.utils.io import capture_output
44 from IPython.utils.io import capture_output
45 from IPython.utils.ipstruct import Struct
45 from IPython.utils.ipstruct import Struct
46 from IPython.utils.module_paths import find_mod
46 from IPython.utils.module_paths import find_mod
47 from IPython.utils.path import get_py_filename, unquote_filename
47 from IPython.utils.path import get_py_filename, unquote_filename
48 from IPython.utils.timing import clock, clock2
48 from IPython.utils.timing import clock, clock2
49 from IPython.utils.warn import warn, error
49 from IPython.utils.warn import warn, error
50
50
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52 # Magic implementation classes
52 # Magic implementation classes
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54
54
55 @magics_class
55 @magics_class
56 class ExecutionMagics(Magics):
56 class ExecutionMagics(Magics):
57 """Magics related to code execution, debugging, profiling, etc.
57 """Magics related to code execution, debugging, profiling, etc.
58
58
59 """
59 """
60
60
61 def __init__(self, shell):
61 def __init__(self, shell):
62 super(ExecutionMagics, self).__init__(shell)
62 super(ExecutionMagics, self).__init__(shell)
63 if profile is None:
63 if profile is None:
64 self.prun = self.profile_missing_notice
64 self.prun = self.profile_missing_notice
65 # Default execution function used to actually run user code.
65 # Default execution function used to actually run user code.
66 self.default_runner = None
66 self.default_runner = None
67
67
68 def profile_missing_notice(self, *args, **kwargs):
68 def profile_missing_notice(self, *args, **kwargs):
69 error("""\
69 error("""\
70 The profile module could not be found. It has been removed from the standard
70 The profile module could not be found. It has been removed from the standard
71 python packages because of its non-free license. To use profiling, install the
71 python packages because of its non-free license. To use profiling, install the
72 python-profiler package from non-free.""")
72 python-profiler package from non-free.""")
73
73
74 @skip_doctest
74 @skip_doctest
75 @line_cell_magic
75 @line_cell_magic
76 def prun(self, parameter_s='', cell=None, user_mode=True,
76 def prun(self, parameter_s='', cell=None, user_mode=True,
77 opts=None,arg_lst=None,prog_ns=None):
77 opts=None,arg_lst=None,prog_ns=None):
78
78
79 """Run a statement through the python code profiler.
79 """Run a statement through the python code profiler.
80
80
81 Usage, in line mode:
81 Usage, in line mode:
82 %prun [options] statement
82 %prun [options] statement
83
83
84 Usage, in cell mode:
84 Usage, in cell mode:
85 %%prun [options] [statement]
85 %%prun [options] [statement]
86 code...
86 code...
87 code...
87 code...
88
88
89 In cell mode, the additional code lines are appended to the (possibly
89 In cell mode, the additional code lines are appended to the (possibly
90 empty) statement in the first line. Cell mode allows you to easily
90 empty) statement in the first line. Cell mode allows you to easily
91 profile multiline blocks without having to put them in a separate
91 profile multiline blocks without having to put them in a separate
92 function.
92 function.
93
93
94 The given statement (which doesn't require quote marks) is run via the
94 The given statement (which doesn't require quote marks) is run via the
95 python profiler in a manner similar to the profile.run() function.
95 python profiler in a manner similar to the profile.run() function.
96 Namespaces are internally managed to work correctly; profile.run
96 Namespaces are internally managed to work correctly; profile.run
97 cannot be used in IPython because it makes certain assumptions about
97 cannot be used in IPython because it makes certain assumptions about
98 namespaces which do not hold under IPython.
98 namespaces which do not hold under IPython.
99
99
100 Options:
100 Options:
101
101
102 -l <limit>: you can place restrictions on what or how much of the
102 -l <limit>: you can place restrictions on what or how much of the
103 profile gets printed. The limit value can be:
103 profile gets printed. The limit value can be:
104
104
105 * A string: only information for function names containing this string
105 * A string: only information for function names containing this string
106 is printed.
106 is printed.
107
107
108 * An integer: only these many lines are printed.
108 * An integer: only these many lines are printed.
109
109
110 * A float (between 0 and 1): this fraction of the report is printed
110 * A float (between 0 and 1): this fraction of the report is printed
111 (for example, use a limit of 0.4 to see the topmost 40% only).
111 (for example, use a limit of 0.4 to see the topmost 40% only).
112
112
113 You can combine several limits with repeated use of the option. For
113 You can combine several limits with repeated use of the option. For
114 example, '-l __init__ -l 5' will print only the topmost 5 lines of
114 example, '-l __init__ -l 5' will print only the topmost 5 lines of
115 information about class constructors.
115 information about class constructors.
116
116
117 -r: return the pstats.Stats object generated by the profiling. This
117 -r: return the pstats.Stats object generated by the profiling. This
118 object has all the information about the profile in it, and you can
118 object has all the information about the profile in it, and you can
119 later use it for further analysis or in other functions.
119 later use it for further analysis or in other functions.
120
120
121 -s <key>: sort profile by given key. You can provide more than one key
121 -s <key>: sort profile by given key. You can provide more than one key
122 by using the option several times: '-s key1 -s key2 -s key3...'. The
122 by using the option several times: '-s key1 -s key2 -s key3...'. The
123 default sorting key is 'time'.
123 default sorting key is 'time'.
124
124
125 The following is copied verbatim from the profile documentation
125 The following is copied verbatim from the profile documentation
126 referenced below:
126 referenced below:
127
127
128 When more than one key is provided, additional keys are used as
128 When more than one key is provided, additional keys are used as
129 secondary criteria when the there is equality in all keys selected
129 secondary criteria when the there is equality in all keys selected
130 before them.
130 before them.
131
131
132 Abbreviations can be used for any key names, as long as the
132 Abbreviations can be used for any key names, as long as the
133 abbreviation is unambiguous. The following are the keys currently
133 abbreviation is unambiguous. The following are the keys currently
134 defined:
134 defined:
135
135
136 Valid Arg Meaning
136 Valid Arg Meaning
137 "calls" call count
137 "calls" call count
138 "cumulative" cumulative time
138 "cumulative" cumulative time
139 "file" file name
139 "file" file name
140 "module" file name
140 "module" file name
141 "pcalls" primitive call count
141 "pcalls" primitive call count
142 "line" line number
142 "line" line number
143 "name" function name
143 "name" function name
144 "nfl" name/file/line
144 "nfl" name/file/line
145 "stdname" standard name
145 "stdname" standard name
146 "time" internal time
146 "time" internal time
147
147
148 Note that all sorts on statistics are in descending order (placing
148 Note that all sorts on statistics are in descending order (placing
149 most time consuming items first), where as name, file, and line number
149 most time consuming items first), where as name, file, and line number
150 searches are in ascending order (i.e., alphabetical). The subtle
150 searches are in ascending order (i.e., alphabetical). The subtle
151 distinction between "nfl" and "stdname" is that the standard name is a
151 distinction between "nfl" and "stdname" is that the standard name is a
152 sort of the name as printed, which means that the embedded line
152 sort of the name as printed, which means that the embedded line
153 numbers get compared in an odd way. For example, lines 3, 20, and 40
153 numbers get compared in an odd way. For example, lines 3, 20, and 40
154 would (if the file names were the same) appear in the string order
154 would (if the file names were the same) appear in the string order
155 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
155 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
156 line numbers. In fact, sort_stats("nfl") is the same as
156 line numbers. In fact, sort_stats("nfl") is the same as
157 sort_stats("name", "file", "line").
157 sort_stats("name", "file", "line").
158
158
159 -T <filename>: save profile results as shown on screen to a text
159 -T <filename>: save profile results as shown on screen to a text
160 file. The profile is still shown on screen.
160 file. The profile is still shown on screen.
161
161
162 -D <filename>: save (via dump_stats) profile statistics to given
162 -D <filename>: save (via dump_stats) profile statistics to given
163 filename. This data is in a format understood by the pstats module, and
163 filename. This data is in a format understood by the pstats module, and
164 is generated by a call to the dump_stats() method of profile
164 is generated by a call to the dump_stats() method of profile
165 objects. The profile is still shown on screen.
165 objects. The profile is still shown on screen.
166
166
167 -q: suppress output to the pager. Best used with -T and/or -D above.
167 -q: suppress output to the pager. Best used with -T and/or -D above.
168
168
169 If you want to run complete programs under the profiler's control, use
169 If you want to run complete programs under the profiler's control, use
170 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
170 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
171 contains profiler specific options as described here.
171 contains profiler specific options as described here.
172
172
173 You can read the complete documentation for the profile module with::
173 You can read the complete documentation for the profile module with::
174
174
175 In [1]: import profile; profile.help()
175 In [1]: import profile; profile.help()
176 """
176 """
177
177
178 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
178 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
179
179
180 if user_mode: # regular user call
180 if user_mode: # regular user call
181 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
181 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
182 list_all=True, posix=False)
182 list_all=True, posix=False)
183 namespace = self.shell.user_ns
183 namespace = self.shell.user_ns
184 if cell is not None:
184 if cell is not None:
185 arg_str += '\n' + cell
185 arg_str += '\n' + cell
186 else: # called to run a program by %run -p
186 else: # called to run a program by %run -p
187 try:
187 try:
188 filename = get_py_filename(arg_lst[0])
188 filename = get_py_filename(arg_lst[0])
189 except IOError as e:
189 except IOError as e:
190 try:
190 try:
191 msg = str(e)
191 msg = str(e)
192 except UnicodeError:
192 except UnicodeError:
193 msg = e.message
193 msg = e.message
194 error(msg)
194 error(msg)
195 return
195 return
196
196
197 arg_str = 'execfile(filename,prog_ns)'
197 arg_str = 'execfile(filename,prog_ns)'
198 namespace = {
198 namespace = {
199 'execfile': self.shell.safe_execfile,
199 'execfile': self.shell.safe_execfile,
200 'prog_ns': prog_ns,
200 'prog_ns': prog_ns,
201 'filename': filename
201 'filename': filename
202 }
202 }
203
203
204 opts.merge(opts_def)
204 opts.merge(opts_def)
205
205
206 prof = profile.Profile()
206 prof = profile.Profile()
207 try:
207 try:
208 prof = prof.runctx(arg_str,namespace,namespace)
208 prof = prof.runctx(arg_str,namespace,namespace)
209 sys_exit = ''
209 sys_exit = ''
210 except SystemExit:
210 except SystemExit:
211 sys_exit = """*** SystemExit exception caught in code being profiled."""
211 sys_exit = """*** SystemExit exception caught in code being profiled."""
212
212
213 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
213 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
214
214
215 lims = opts.l
215 lims = opts.l
216 if lims:
216 if lims:
217 lims = [] # rebuild lims with ints/floats/strings
217 lims = [] # rebuild lims with ints/floats/strings
218 for lim in opts.l:
218 for lim in opts.l:
219 try:
219 try:
220 lims.append(int(lim))
220 lims.append(int(lim))
221 except ValueError:
221 except ValueError:
222 try:
222 try:
223 lims.append(float(lim))
223 lims.append(float(lim))
224 except ValueError:
224 except ValueError:
225 lims.append(lim)
225 lims.append(lim)
226
226
227 # Trap output.
227 # Trap output.
228 stdout_trap = StringIO()
228 stdout_trap = StringIO()
229 stats_stream = stats.stream
229 stats_stream = stats.stream
230 try:
230 try:
231 stats.stream = stdout_trap
231 stats.stream = stdout_trap
232 stats.print_stats(*lims)
232 stats.print_stats(*lims)
233 finally:
233 finally:
234 stats.stream = stats_stream
234 stats.stream = stats_stream
235
235
236 output = stdout_trap.getvalue()
236 output = stdout_trap.getvalue()
237 output = output.rstrip()
237 output = output.rstrip()
238
238
239 if 'q' not in opts:
239 if 'q' not in opts:
240 page.page(output)
240 page.page(output)
241 print sys_exit,
241 print sys_exit,
242
242
243 dump_file = opts.D[0]
243 dump_file = opts.D[0]
244 text_file = opts.T[0]
244 text_file = opts.T[0]
245 if dump_file:
245 if dump_file:
246 dump_file = unquote_filename(dump_file)
246 dump_file = unquote_filename(dump_file)
247 prof.dump_stats(dump_file)
247 prof.dump_stats(dump_file)
248 print '\n*** Profile stats marshalled to file',\
248 print '\n*** Profile stats marshalled to file',\
249 repr(dump_file)+'.',sys_exit
249 repr(dump_file)+'.',sys_exit
250 if text_file:
250 if text_file:
251 text_file = unquote_filename(text_file)
251 text_file = unquote_filename(text_file)
252 pfile = open(text_file,'w')
252 pfile = open(text_file,'w')
253 pfile.write(output)
253 pfile.write(output)
254 pfile.close()
254 pfile.close()
255 print '\n*** Profile printout saved to text file',\
255 print '\n*** Profile printout saved to text file',\
256 repr(text_file)+'.',sys_exit
256 repr(text_file)+'.',sys_exit
257
257
258 if 'r' in opts:
258 if 'r' in opts:
259 return stats
259 return stats
260 else:
260 else:
261 return None
261 return None
262
262
263 @line_magic
263 @line_magic
264 def pdb(self, parameter_s=''):
264 def pdb(self, parameter_s=''):
265 """Control the automatic calling of the pdb interactive debugger.
265 """Control the automatic calling of the pdb interactive debugger.
266
266
267 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
267 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
268 argument it works as a toggle.
268 argument it works as a toggle.
269
269
270 When an exception is triggered, IPython can optionally call the
270 When an exception is triggered, IPython can optionally call the
271 interactive pdb debugger after the traceback printout. %pdb toggles
271 interactive pdb debugger after the traceback printout. %pdb toggles
272 this feature on and off.
272 this feature on and off.
273
273
274 The initial state of this feature is set in your configuration
274 The initial state of this feature is set in your configuration
275 file (the option is ``InteractiveShell.pdb``).
275 file (the option is ``InteractiveShell.pdb``).
276
276
277 If you want to just activate the debugger AFTER an exception has fired,
277 If you want to just activate the debugger AFTER an exception has fired,
278 without having to type '%pdb on' and rerunning your code, you can use
278 without having to type '%pdb on' and rerunning your code, you can use
279 the %debug magic."""
279 the %debug magic."""
280
280
281 par = parameter_s.strip().lower()
281 par = parameter_s.strip().lower()
282
282
283 if par:
283 if par:
284 try:
284 try:
285 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
285 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
286 except KeyError:
286 except KeyError:
287 print ('Incorrect argument. Use on/1, off/0, '
287 print ('Incorrect argument. Use on/1, off/0, '
288 'or nothing for a toggle.')
288 'or nothing for a toggle.')
289 return
289 return
290 else:
290 else:
291 # toggle
291 # toggle
292 new_pdb = not self.shell.call_pdb
292 new_pdb = not self.shell.call_pdb
293
293
294 # set on the shell
294 # set on the shell
295 self.shell.call_pdb = new_pdb
295 self.shell.call_pdb = new_pdb
296 print 'Automatic pdb calling has been turned',on_off(new_pdb)
296 print 'Automatic pdb calling has been turned',on_off(new_pdb)
297
297
298 @line_magic
298 @line_magic
299 def debug(self, parameter_s=''):
299 def debug(self, parameter_s=''):
300 """Activate the interactive debugger in post-mortem mode.
300 """Activate the interactive debugger in post-mortem mode.
301
301
302 If an exception has just occurred, this lets you inspect its stack
302 If an exception has just occurred, this lets you inspect its stack
303 frames interactively. Note that this will always work only on the last
303 frames interactively. Note that this will always work only on the last
304 traceback that occurred, so you must call this quickly after an
304 traceback that occurred, so you must call this quickly after an
305 exception that you wish to inspect has fired, because if another one
305 exception that you wish to inspect has fired, because if another one
306 occurs, it clobbers the previous one.
306 occurs, it clobbers the previous one.
307
307
308 If you want IPython to automatically do this on every exception, see
308 If you want IPython to automatically do this on every exception, see
309 the %pdb magic for more details.
309 the %pdb magic for more details.
310 """
310 """
311 self.shell.debugger(force=True)
311 self.shell.debugger(force=True)
312
312
313 @line_magic
313 @line_magic
314 def tb(self, s):
314 def tb(self, s):
315 """Print the last traceback with the currently active exception mode.
315 """Print the last traceback with the currently active exception mode.
316
316
317 See %xmode for changing exception reporting modes."""
317 See %xmode for changing exception reporting modes."""
318 self.shell.showtraceback()
318 self.shell.showtraceback()
319
319
320 @skip_doctest
320 @skip_doctest
321 @line_magic
321 @line_magic
322 def run(self, parameter_s='', runner=None,
322 def run(self, parameter_s='', runner=None,
323 file_finder=get_py_filename):
323 file_finder=get_py_filename):
324 """Run the named file inside IPython as a program.
324 """Run the named file inside IPython as a program.
325
325
326 Usage:\\
326 Usage:\\
327 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
327 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
328
328
329 Parameters after the filename are passed as command-line arguments to
329 Parameters after the filename are passed as command-line arguments to
330 the program (put in sys.argv). Then, control returns to IPython's
330 the program (put in sys.argv). Then, control returns to IPython's
331 prompt.
331 prompt.
332
332
333 This is similar to running at a system prompt:\\
333 This is similar to running at a system prompt:\\
334 $ python file args\\
334 $ python file args\\
335 but with the advantage of giving you IPython's tracebacks, and of
335 but with the advantage of giving you IPython's tracebacks, and of
336 loading all variables into your interactive namespace for further use
336 loading all variables into your interactive namespace for further use
337 (unless -p is used, see below).
337 (unless -p is used, see below).
338
338
339 The file is executed in a namespace initially consisting only of
339 The file is executed in a namespace initially consisting only of
340 __name__=='__main__' and sys.argv constructed as indicated. It thus
340 __name__=='__main__' and sys.argv constructed as indicated. It thus
341 sees its environment as if it were being run as a stand-alone program
341 sees its environment as if it were being run as a stand-alone program
342 (except for sharing global objects such as previously imported
342 (except for sharing global objects such as previously imported
343 modules). But after execution, the IPython interactive namespace gets
343 modules). But after execution, the IPython interactive namespace gets
344 updated with all variables defined in the program (except for __name__
344 updated with all variables defined in the program (except for __name__
345 and sys.argv). This allows for very convenient loading of code for
345 and sys.argv). This allows for very convenient loading of code for
346 interactive work, while giving each program a 'clean sheet' to run in.
346 interactive work, while giving each program a 'clean sheet' to run in.
347
347
348 Options:
348 Options:
349
349
350 -n: __name__ is NOT set to '__main__', but to the running file's name
350 -n: __name__ is NOT set to '__main__', but to the running file's name
351 without extension (as python does under import). This allows running
351 without extension (as python does under import). This allows running
352 scripts and reloading the definitions in them without calling code
352 scripts and reloading the definitions in them without calling code
353 protected by an ' if __name__ == "__main__" ' clause.
353 protected by an ' if __name__ == "__main__" ' clause.
354
354
355 -i: run the file in IPython's namespace instead of an empty one. This
355 -i: run the file in IPython's namespace instead of an empty one. This
356 is useful if you are experimenting with code written in a text editor
356 is useful if you are experimenting with code written in a text editor
357 which depends on variables defined interactively.
357 which depends on variables defined interactively.
358
358
359 -e: ignore sys.exit() calls or SystemExit exceptions in the script
359 -e: ignore sys.exit() calls or SystemExit exceptions in the script
360 being run. This is particularly useful if IPython is being used to
360 being run. This is particularly useful if IPython is being used to
361 run unittests, which always exit with a sys.exit() call. In such
361 run unittests, which always exit with a sys.exit() call. In such
362 cases you are interested in the output of the test results, not in
362 cases you are interested in the output of the test results, not in
363 seeing a traceback of the unittest module.
363 seeing a traceback of the unittest module.
364
364
365 -t: print timing information at the end of the run. IPython will give
365 -t: print timing information at the end of the run. IPython will give
366 you an estimated CPU time consumption for your script, which under
366 you an estimated CPU time consumption for your script, which under
367 Unix uses the resource module to avoid the wraparound problems of
367 Unix uses the resource module to avoid the wraparound problems of
368 time.clock(). Under Unix, an estimate of time spent on system tasks
368 time.clock(). Under Unix, an estimate of time spent on system tasks
369 is also given (for Windows platforms this is reported as 0.0).
369 is also given (for Windows platforms this is reported as 0.0).
370
370
371 If -t is given, an additional -N<N> option can be given, where <N>
371 If -t is given, an additional -N<N> option can be given, where <N>
372 must be an integer indicating how many times you want the script to
372 must be an integer indicating how many times you want the script to
373 run. The final timing report will include total and per run results.
373 run. The final timing report will include total and per run results.
374
374
375 For example (testing the script uniq_stable.py)::
375 For example (testing the script uniq_stable.py)::
376
376
377 In [1]: run -t uniq_stable
377 In [1]: run -t uniq_stable
378
378
379 IPython CPU timings (estimated):\\
379 IPython CPU timings (estimated):\\
380 User : 0.19597 s.\\
380 User : 0.19597 s.\\
381 System: 0.0 s.\\
381 System: 0.0 s.\\
382
382
383 In [2]: run -t -N5 uniq_stable
383 In [2]: run -t -N5 uniq_stable
384
384
385 IPython CPU timings (estimated):\\
385 IPython CPU timings (estimated):\\
386 Total runs performed: 5\\
386 Total runs performed: 5\\
387 Times : Total Per run\\
387 Times : Total Per run\\
388 User : 0.910862 s, 0.1821724 s.\\
388 User : 0.910862 s, 0.1821724 s.\\
389 System: 0.0 s, 0.0 s.
389 System: 0.0 s, 0.0 s.
390
390
391 -d: run your program under the control of pdb, the Python debugger.
391 -d: run your program under the control of pdb, the Python debugger.
392 This allows you to execute your program step by step, watch variables,
392 This allows you to execute your program step by step, watch variables,
393 etc. Internally, what IPython does is similar to calling:
393 etc. Internally, what IPython does is similar to calling:
394
394
395 pdb.run('execfile("YOURFILENAME")')
395 pdb.run('execfile("YOURFILENAME")')
396
396
397 with a breakpoint set on line 1 of your file. You can change the line
397 with a breakpoint set on line 1 of your file. You can change the line
398 number for this automatic breakpoint to be <N> by using the -bN option
398 number for this automatic breakpoint to be <N> by using the -bN option
399 (where N must be an integer). For example::
399 (where N must be an integer). For example::
400
400
401 %run -d -b40 myscript
401 %run -d -b40 myscript
402
402
403 will set the first breakpoint at line 40 in myscript.py. Note that
403 will set the first breakpoint at line 40 in myscript.py. Note that
404 the first breakpoint must be set on a line which actually does
404 the first breakpoint must be set on a line which actually does
405 something (not a comment or docstring) for it to stop execution.
405 something (not a comment or docstring) for it to stop execution.
406
406
407 When the pdb debugger starts, you will see a (Pdb) prompt. You must
407 When the pdb debugger starts, you will see a (Pdb) prompt. You must
408 first enter 'c' (without quotes) to start execution up to the first
408 first enter 'c' (without quotes) to start execution up to the first
409 breakpoint.
409 breakpoint.
410
410
411 Entering 'help' gives information about the use of the debugger. You
411 Entering 'help' gives information about the use of the debugger. You
412 can easily see pdb's full documentation with "import pdb;pdb.help()"
412 can easily see pdb's full documentation with "import pdb;pdb.help()"
413 at a prompt.
413 at a prompt.
414
414
415 -p: run program under the control of the Python profiler module (which
415 -p: run program under the control of the Python profiler module (which
416 prints a detailed report of execution times, function calls, etc).
416 prints a detailed report of execution times, function calls, etc).
417
417
418 You can pass other options after -p which affect the behavior of the
418 You can pass other options after -p which affect the behavior of the
419 profiler itself. See the docs for %prun for details.
419 profiler itself. See the docs for %prun for details.
420
420
421 In this mode, the program's variables do NOT propagate back to the
421 In this mode, the program's variables do NOT propagate back to the
422 IPython interactive namespace (because they remain in the namespace
422 IPython interactive namespace (because they remain in the namespace
423 where the profiler executes them).
423 where the profiler executes them).
424
424
425 Internally this triggers a call to %prun, see its documentation for
425 Internally this triggers a call to %prun, see its documentation for
426 details on the options available specifically for profiling.
426 details on the options available specifically for profiling.
427
427
428 There is one special usage for which the text above doesn't apply:
428 There is one special usage for which the text above doesn't apply:
429 if the filename ends with .ipy, the file is run as ipython script,
429 if the filename ends with .ipy, the file is run as ipython script,
430 just as if the commands were written on IPython prompt.
430 just as if the commands were written on IPython prompt.
431
431
432 -m: specify module name to load instead of script path. Similar to
432 -m: specify module name to load instead of script path. Similar to
433 the -m option for the python interpreter. Use this option last if you
433 the -m option for the python interpreter. Use this option last if you
434 want to combine with other %run options. Unlike the python interpreter
434 want to combine with other %run options. Unlike the python interpreter
435 only source modules are allowed no .pyc or .pyo files.
435 only source modules are allowed no .pyc or .pyo files.
436 For example::
436 For example::
437
437
438 %run -m example
438 %run -m example
439
439
440 will run the example module.
440 will run the example module.
441
441
442 """
442 """
443
443
444 # get arguments and set sys.argv for program to be run.
444 # get arguments and set sys.argv for program to be run.
445 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
445 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
446 mode='list', list_all=1)
446 mode='list', list_all=1)
447 if "m" in opts:
447 if "m" in opts:
448 modulename = opts["m"][0]
448 modulename = opts["m"][0]
449 modpath = find_mod(modulename)
449 modpath = find_mod(modulename)
450 if modpath is None:
450 if modpath is None:
451 warn('%r is not a valid modulename on sys.path'%modulename)
451 warn('%r is not a valid modulename on sys.path'%modulename)
452 return
452 return
453 arg_lst = [modpath] + arg_lst
453 arg_lst = [modpath] + arg_lst
454 try:
454 try:
455 filename = file_finder(arg_lst[0])
455 filename = file_finder(arg_lst[0])
456 except IndexError:
456 except IndexError:
457 warn('you must provide at least a filename.')
457 warn('you must provide at least a filename.')
458 print '\n%run:\n', oinspect.getdoc(self.run)
458 print '\n%run:\n', oinspect.getdoc(self.run)
459 return
459 return
460 except IOError as e:
460 except IOError as e:
461 try:
461 try:
462 msg = str(e)
462 msg = str(e)
463 except UnicodeError:
463 except UnicodeError:
464 msg = e.message
464 msg = e.message
465 error(msg)
465 error(msg)
466 return
466 return
467
467
468 if filename.lower().endswith('.ipy'):
468 if filename.lower().endswith('.ipy'):
469 self.shell.safe_execfile_ipy(filename)
469 self.shell.safe_execfile_ipy(filename)
470 return
470 return
471
471
472 # Control the response to exit() calls made by the script being run
472 # Control the response to exit() calls made by the script being run
473 exit_ignore = 'e' in opts
473 exit_ignore = 'e' in opts
474
474
475 # Make sure that the running script gets a proper sys.argv as if it
475 # Make sure that the running script gets a proper sys.argv as if it
476 # were run from a system shell.
476 # were run from a system shell.
477 save_argv = sys.argv # save it for later restoring
477 save_argv = sys.argv # save it for later restoring
478
478
479 # simulate shell expansion on arguments, at least tilde expansion
479 # simulate shell expansion on arguments, at least tilde expansion
480 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
480 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
481
481
482 sys.argv = [filename] + args # put in the proper filename
482 sys.argv = [filename] + args # put in the proper filename
483 # protect sys.argv from potential unicode strings on Python 2:
483 # protect sys.argv from potential unicode strings on Python 2:
484 if not py3compat.PY3:
484 if not py3compat.PY3:
485 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
485 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
486
486
487 if 'i' in opts:
487 if 'i' in opts:
488 # Run in user's interactive namespace
488 # Run in user's interactive namespace
489 prog_ns = self.shell.user_ns
489 prog_ns = self.shell.user_ns
490 __name__save = self.shell.user_ns['__name__']
490 __name__save = self.shell.user_ns['__name__']
491 prog_ns['__name__'] = '__main__'
491 prog_ns['__name__'] = '__main__'
492 main_mod = self.shell.new_main_mod(prog_ns)
492 main_mod = self.shell.new_main_mod(prog_ns)
493 else:
493 else:
494 # Run in a fresh, empty namespace
494 # Run in a fresh, empty namespace
495 if 'n' in opts:
495 if 'n' in opts:
496 name = os.path.splitext(os.path.basename(filename))[0]
496 name = os.path.splitext(os.path.basename(filename))[0]
497 else:
497 else:
498 name = '__main__'
498 name = '__main__'
499
499
500 main_mod = self.shell.new_main_mod()
500 main_mod = self.shell.new_main_mod()
501 prog_ns = main_mod.__dict__
501 prog_ns = main_mod.__dict__
502 prog_ns['__name__'] = name
502 prog_ns['__name__'] = name
503
503
504 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
504 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
505 # set the __file__ global in the script's namespace
505 # set the __file__ global in the script's namespace
506 prog_ns['__file__'] = filename
506 prog_ns['__file__'] = filename
507
507
508 # pickle fix. See interactiveshell for an explanation. But we need to
508 # pickle fix. See interactiveshell for an explanation. But we need to
509 # make sure that, if we overwrite __main__, we replace it at the end
509 # make sure that, if we overwrite __main__, we replace it at the end
510 main_mod_name = prog_ns['__name__']
510 main_mod_name = prog_ns['__name__']
511
511
512 if main_mod_name == '__main__':
512 if main_mod_name == '__main__':
513 restore_main = sys.modules['__main__']
513 restore_main = sys.modules['__main__']
514 else:
514 else:
515 restore_main = False
515 restore_main = False
516
516
517 # This needs to be undone at the end to prevent holding references to
517 # This needs to be undone at the end to prevent holding references to
518 # every single object ever created.
518 # every single object ever created.
519 sys.modules[main_mod_name] = main_mod
519 sys.modules[main_mod_name] = main_mod
520
520
521 try:
521 try:
522 stats = None
522 stats = None
523 with self.shell.readline_no_record:
523 with self.shell.readline_no_record:
524 if 'p' in opts:
524 if 'p' in opts:
525 stats = self.prun('', None, False, opts, arg_lst, prog_ns)
525 stats = self.prun('', None, False, opts, arg_lst, prog_ns)
526 else:
526 else:
527 if 'd' in opts:
527 if 'd' in opts:
528 deb = debugger.Pdb(self.shell.colors)
528 deb = debugger.Pdb(self.shell.colors)
529 # reset Breakpoint state, which is moronically kept
529 # reset Breakpoint state, which is moronically kept
530 # in a class
530 # in a class
531 bdb.Breakpoint.next = 1
531 bdb.Breakpoint.next = 1
532 bdb.Breakpoint.bplist = {}
532 bdb.Breakpoint.bplist = {}
533 bdb.Breakpoint.bpbynumber = [None]
533 bdb.Breakpoint.bpbynumber = [None]
534 # Set an initial breakpoint to stop execution
534 # Set an initial breakpoint to stop execution
535 maxtries = 10
535 maxtries = 10
536 bp = int(opts.get('b', [1])[0])
536 bp = int(opts.get('b', [1])[0])
537 checkline = deb.checkline(filename, bp)
537 checkline = deb.checkline(filename, bp)
538 if not checkline:
538 if not checkline:
539 for bp in range(bp + 1, bp + maxtries + 1):
539 for bp in range(bp + 1, bp + maxtries + 1):
540 if deb.checkline(filename, bp):
540 if deb.checkline(filename, bp):
541 break
541 break
542 else:
542 else:
543 msg = ("\nI failed to find a valid line to set "
543 msg = ("\nI failed to find a valid line to set "
544 "a breakpoint\n"
544 "a breakpoint\n"
545 "after trying up to line: %s.\n"
545 "after trying up to line: %s.\n"
546 "Please set a valid breakpoint manually "
546 "Please set a valid breakpoint manually "
547 "with the -b option." % bp)
547 "with the -b option." % bp)
548 error(msg)
548 error(msg)
549 return
549 return
550 # if we find a good linenumber, set the breakpoint
550 # if we find a good linenumber, set the breakpoint
551 deb.do_break('%s:%s' % (filename, bp))
551 deb.do_break('%s:%s' % (filename, bp))
552 # Start file run
552 # Start file run
553 print "NOTE: Enter 'c' at the",
553 print "NOTE: Enter 'c' at the",
554 print "%s prompt to start your script." % deb.prompt
554 print "%s prompt to start your script." % deb.prompt
555 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
555 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
556 try:
556 try:
557 #save filename so it can be used by methods on the deb object
558 deb._exec_filename = filename
557 deb.run('execfile("%s", prog_ns)' % filename, ns)
559 deb.run('execfile("%s", prog_ns)' % filename, ns)
558
560
559 except:
561 except:
560 etype, value, tb = sys.exc_info()
562 etype, value, tb = sys.exc_info()
561 # Skip three frames in the traceback: the %run one,
563 # Skip three frames in the traceback: the %run one,
562 # one inside bdb.py, and the command-line typed by the
564 # one inside bdb.py, and the command-line typed by the
563 # user (run by exec in pdb itself).
565 # user (run by exec in pdb itself).
564 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
566 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
565 else:
567 else:
566 if runner is None:
568 if runner is None:
567 runner = self.default_runner
569 runner = self.default_runner
568 if runner is None:
570 if runner is None:
569 runner = self.shell.safe_execfile
571 runner = self.shell.safe_execfile
570 if 't' in opts:
572 if 't' in opts:
571 # timed execution
573 # timed execution
572 try:
574 try:
573 nruns = int(opts['N'][0])
575 nruns = int(opts['N'][0])
574 if nruns < 1:
576 if nruns < 1:
575 error('Number of runs must be >=1')
577 error('Number of runs must be >=1')
576 return
578 return
577 except (KeyError):
579 except (KeyError):
578 nruns = 1
580 nruns = 1
579 twall0 = time.time()
581 twall0 = time.time()
580 if nruns == 1:
582 if nruns == 1:
581 t0 = clock2()
583 t0 = clock2()
582 runner(filename, prog_ns, prog_ns,
584 runner(filename, prog_ns, prog_ns,
583 exit_ignore=exit_ignore)
585 exit_ignore=exit_ignore)
584 t1 = clock2()
586 t1 = clock2()
585 t_usr = t1[0] - t0[0]
587 t_usr = t1[0] - t0[0]
586 t_sys = t1[1] - t0[1]
588 t_sys = t1[1] - t0[1]
587 print "\nIPython CPU timings (estimated):"
589 print "\nIPython CPU timings (estimated):"
588 print " User : %10.2f s." % t_usr
590 print " User : %10.2f s." % t_usr
589 print " System : %10.2f s." % t_sys
591 print " System : %10.2f s." % t_sys
590 else:
592 else:
591 runs = range(nruns)
593 runs = range(nruns)
592 t0 = clock2()
594 t0 = clock2()
593 for nr in runs:
595 for nr in runs:
594 runner(filename, prog_ns, prog_ns,
596 runner(filename, prog_ns, prog_ns,
595 exit_ignore=exit_ignore)
597 exit_ignore=exit_ignore)
596 t1 = clock2()
598 t1 = clock2()
597 t_usr = t1[0] - t0[0]
599 t_usr = t1[0] - t0[0]
598 t_sys = t1[1] - t0[1]
600 t_sys = t1[1] - t0[1]
599 print "\nIPython CPU timings (estimated):"
601 print "\nIPython CPU timings (estimated):"
600 print "Total runs performed:", nruns
602 print "Total runs performed:", nruns
601 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
603 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
602 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
604 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
603 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
605 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
604 twall1 = time.time()
606 twall1 = time.time()
605 print "Wall time: %10.2f s." % (twall1 - twall0)
607 print "Wall time: %10.2f s." % (twall1 - twall0)
606
608
607 else:
609 else:
608 # regular execution
610 # regular execution
609 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
611 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
610
612
611 if 'i' in opts:
613 if 'i' in opts:
612 self.shell.user_ns['__name__'] = __name__save
614 self.shell.user_ns['__name__'] = __name__save
613 else:
615 else:
614 # The shell MUST hold a reference to prog_ns so after %run
616 # The shell MUST hold a reference to prog_ns so after %run
615 # exits, the python deletion mechanism doesn't zero it out
617 # exits, the python deletion mechanism doesn't zero it out
616 # (leaving dangling references).
618 # (leaving dangling references).
617 self.shell.cache_main_mod(prog_ns, filename)
619 self.shell.cache_main_mod(prog_ns, filename)
618 # update IPython interactive namespace
620 # update IPython interactive namespace
619
621
620 # Some forms of read errors on the file may mean the
622 # Some forms of read errors on the file may mean the
621 # __name__ key was never set; using pop we don't have to
623 # __name__ key was never set; using pop we don't have to
622 # worry about a possible KeyError.
624 # worry about a possible KeyError.
623 prog_ns.pop('__name__', None)
625 prog_ns.pop('__name__', None)
624
626
625 self.shell.user_ns.update(prog_ns)
627 self.shell.user_ns.update(prog_ns)
626 finally:
628 finally:
627 # It's a bit of a mystery why, but __builtins__ can change from
629 # It's a bit of a mystery why, but __builtins__ can change from
628 # being a module to becoming a dict missing some key data after
630 # being a module to becoming a dict missing some key data after
629 # %run. As best I can see, this is NOT something IPython is doing
631 # %run. As best I can see, this is NOT something IPython is doing
630 # at all, and similar problems have been reported before:
632 # at all, and similar problems have been reported before:
631 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
633 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
632 # Since this seems to be done by the interpreter itself, the best
634 # Since this seems to be done by the interpreter itself, the best
633 # we can do is to at least restore __builtins__ for the user on
635 # we can do is to at least restore __builtins__ for the user on
634 # exit.
636 # exit.
635 self.shell.user_ns['__builtins__'] = builtin_mod
637 self.shell.user_ns['__builtins__'] = builtin_mod
636
638
637 # Ensure key global structures are restored
639 # Ensure key global structures are restored
638 sys.argv = save_argv
640 sys.argv = save_argv
639 if restore_main:
641 if restore_main:
640 sys.modules['__main__'] = restore_main
642 sys.modules['__main__'] = restore_main
641 else:
643 else:
642 # Remove from sys.modules the reference to main_mod we'd
644 # Remove from sys.modules the reference to main_mod we'd
643 # added. Otherwise it will trap references to objects
645 # added. Otherwise it will trap references to objects
644 # contained therein.
646 # contained therein.
645 del sys.modules[main_mod_name]
647 del sys.modules[main_mod_name]
646
648
647 return stats
649 return stats
648
650
649 @skip_doctest
651 @skip_doctest
650 @line_cell_magic
652 @line_cell_magic
651 def timeit(self, line='', cell=None):
653 def timeit(self, line='', cell=None):
652 """Time execution of a Python statement or expression
654 """Time execution of a Python statement or expression
653
655
654 Usage, in line mode:
656 Usage, in line mode:
655 %timeit [-n<N> -r<R> [-t|-c]] statement
657 %timeit [-n<N> -r<R> [-t|-c]] statement
656 or in cell mode:
658 or in cell mode:
657 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
659 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
658 code
660 code
659 code...
661 code...
660
662
661 Time execution of a Python statement or expression using the timeit
663 Time execution of a Python statement or expression using the timeit
662 module. This function can be used both as a line and cell magic:
664 module. This function can be used both as a line and cell magic:
663
665
664 - In line mode you can time a single-line statement (though multiple
666 - In line mode you can time a single-line statement (though multiple
665 ones can be chained with using semicolons).
667 ones can be chained with using semicolons).
666
668
667 - In cell mode, the statement in the first line is used as setup code
669 - In cell mode, the statement in the first line is used as setup code
668 (executed but not timed) and the body of the cell is timed. The cell
670 (executed but not timed) and the body of the cell is timed. The cell
669 body has access to any variables created in the setup code.
671 body has access to any variables created in the setup code.
670
672
671 Options:
673 Options:
672 -n<N>: execute the given statement <N> times in a loop. If this value
674 -n<N>: execute the given statement <N> times in a loop. If this value
673 is not given, a fitting value is chosen.
675 is not given, a fitting value is chosen.
674
676
675 -r<R>: repeat the loop iteration <R> times and take the best result.
677 -r<R>: repeat the loop iteration <R> times and take the best result.
676 Default: 3
678 Default: 3
677
679
678 -t: use time.time to measure the time, which is the default on Unix.
680 -t: use time.time to measure the time, which is the default on Unix.
679 This function measures wall time.
681 This function measures wall time.
680
682
681 -c: use time.clock to measure the time, which is the default on
683 -c: use time.clock to measure the time, which is the default on
682 Windows and measures wall time. On Unix, resource.getrusage is used
684 Windows and measures wall time. On Unix, resource.getrusage is used
683 instead and returns the CPU user time.
685 instead and returns the CPU user time.
684
686
685 -p<P>: use a precision of <P> digits to display the timing result.
687 -p<P>: use a precision of <P> digits to display the timing result.
686 Default: 3
688 Default: 3
687
689
688
690
689 Examples
691 Examples
690 --------
692 --------
691 ::
693 ::
692
694
693 In [1]: %timeit pass
695 In [1]: %timeit pass
694 10000000 loops, best of 3: 53.3 ns per loop
696 10000000 loops, best of 3: 53.3 ns per loop
695
697
696 In [2]: u = None
698 In [2]: u = None
697
699
698 In [3]: %timeit u is None
700 In [3]: %timeit u is None
699 10000000 loops, best of 3: 184 ns per loop
701 10000000 loops, best of 3: 184 ns per loop
700
702
701 In [4]: %timeit -r 4 u == None
703 In [4]: %timeit -r 4 u == None
702 1000000 loops, best of 4: 242 ns per loop
704 1000000 loops, best of 4: 242 ns per loop
703
705
704 In [5]: import time
706 In [5]: import time
705
707
706 In [6]: %timeit -n1 time.sleep(2)
708 In [6]: %timeit -n1 time.sleep(2)
707 1 loops, best of 3: 2 s per loop
709 1 loops, best of 3: 2 s per loop
708
710
709
711
710 The times reported by %timeit will be slightly higher than those
712 The times reported by %timeit will be slightly higher than those
711 reported by the timeit.py script when variables are accessed. This is
713 reported by the timeit.py script when variables are accessed. This is
712 due to the fact that %timeit executes the statement in the namespace
714 due to the fact that %timeit executes the statement in the namespace
713 of the shell, compared with timeit.py, which uses a single setup
715 of the shell, compared with timeit.py, which uses a single setup
714 statement to import function or create variables. Generally, the bias
716 statement to import function or create variables. Generally, the bias
715 does not matter as long as results from timeit.py are not mixed with
717 does not matter as long as results from timeit.py are not mixed with
716 those from %timeit."""
718 those from %timeit."""
717
719
718 import timeit
720 import timeit
719 import math
721 import math
720
722
721 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
723 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
722 # certain terminals. Until we figure out a robust way of
724 # certain terminals. Until we figure out a robust way of
723 # auto-detecting if the terminal can deal with it, use plain 'us' for
725 # auto-detecting if the terminal can deal with it, use plain 'us' for
724 # microseconds. I am really NOT happy about disabling the proper
726 # microseconds. I am really NOT happy about disabling the proper
725 # 'micro' prefix, but crashing is worse... If anyone knows what the
727 # 'micro' prefix, but crashing is worse... If anyone knows what the
726 # right solution for this is, I'm all ears...
728 # right solution for this is, I'm all ears...
727 #
729 #
728 # Note: using
730 # Note: using
729 #
731 #
730 # s = u'\xb5'
732 # s = u'\xb5'
731 # s.encode(sys.getdefaultencoding())
733 # s.encode(sys.getdefaultencoding())
732 #
734 #
733 # is not sufficient, as I've seen terminals where that fails but
735 # is not sufficient, as I've seen terminals where that fails but
734 # print s
736 # print s
735 #
737 #
736 # succeeds
738 # succeeds
737 #
739 #
738 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
740 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
739
741
740 #units = [u"s", u"ms",u'\xb5',"ns"]
742 #units = [u"s", u"ms",u'\xb5',"ns"]
741 units = [u"s", u"ms",u'us',"ns"]
743 units = [u"s", u"ms",u'us',"ns"]
742
744
743 scaling = [1, 1e3, 1e6, 1e9]
745 scaling = [1, 1e3, 1e6, 1e9]
744
746
745 opts, stmt = self.parse_options(line,'n:r:tcp:',
747 opts, stmt = self.parse_options(line,'n:r:tcp:',
746 posix=False, strict=False)
748 posix=False, strict=False)
747 if stmt == "" and cell is None:
749 if stmt == "" and cell is None:
748 return
750 return
749 timefunc = timeit.default_timer
751 timefunc = timeit.default_timer
750 number = int(getattr(opts, "n", 0))
752 number = int(getattr(opts, "n", 0))
751 repeat = int(getattr(opts, "r", timeit.default_repeat))
753 repeat = int(getattr(opts, "r", timeit.default_repeat))
752 precision = int(getattr(opts, "p", 3))
754 precision = int(getattr(opts, "p", 3))
753 if hasattr(opts, "t"):
755 if hasattr(opts, "t"):
754 timefunc = time.time
756 timefunc = time.time
755 if hasattr(opts, "c"):
757 if hasattr(opts, "c"):
756 timefunc = clock
758 timefunc = clock
757
759
758 timer = timeit.Timer(timer=timefunc)
760 timer = timeit.Timer(timer=timefunc)
759 # this code has tight coupling to the inner workings of timeit.Timer,
761 # this code has tight coupling to the inner workings of timeit.Timer,
760 # but is there a better way to achieve that the code stmt has access
762 # but is there a better way to achieve that the code stmt has access
761 # to the shell namespace?
763 # to the shell namespace?
762 transform = self.shell.input_splitter.transform_cell
764 transform = self.shell.input_splitter.transform_cell
763 if cell is None:
765 if cell is None:
764 # called as line magic
766 # called as line magic
765 setup = 'pass'
767 setup = 'pass'
766 stmt = timeit.reindent(transform(stmt), 8)
768 stmt = timeit.reindent(transform(stmt), 8)
767 else:
769 else:
768 setup = timeit.reindent(transform(stmt), 4)
770 setup = timeit.reindent(transform(stmt), 4)
769 stmt = timeit.reindent(transform(cell), 8)
771 stmt = timeit.reindent(transform(cell), 8)
770
772
771 # From Python 3.3, this template uses new-style string formatting.
773 # From Python 3.3, this template uses new-style string formatting.
772 if sys.version_info >= (3, 3):
774 if sys.version_info >= (3, 3):
773 src = timeit.template.format(stmt=stmt, setup=setup)
775 src = timeit.template.format(stmt=stmt, setup=setup)
774 else:
776 else:
775 src = timeit.template % dict(stmt=stmt, setup=setup)
777 src = timeit.template % dict(stmt=stmt, setup=setup)
776
778
777 # Track compilation time so it can be reported if too long
779 # Track compilation time so it can be reported if too long
778 # Minimum time above which compilation time will be reported
780 # Minimum time above which compilation time will be reported
779 tc_min = 0.1
781 tc_min = 0.1
780
782
781 t0 = clock()
783 t0 = clock()
782 code = compile(src, "<magic-timeit>", "exec")
784 code = compile(src, "<magic-timeit>", "exec")
783 tc = clock()-t0
785 tc = clock()-t0
784
786
785 ns = {}
787 ns = {}
786 exec code in self.shell.user_ns, ns
788 exec code in self.shell.user_ns, ns
787 timer.inner = ns["inner"]
789 timer.inner = ns["inner"]
788
790
789 if number == 0:
791 if number == 0:
790 # determine number so that 0.2 <= total time < 2.0
792 # determine number so that 0.2 <= total time < 2.0
791 number = 1
793 number = 1
792 for i in range(1, 10):
794 for i in range(1, 10):
793 if timer.timeit(number) >= 0.2:
795 if timer.timeit(number) >= 0.2:
794 break
796 break
795 number *= 10
797 number *= 10
796
798
797 best = min(timer.repeat(repeat, number)) / number
799 best = min(timer.repeat(repeat, number)) / number
798
800
799 if best > 0.0 and best < 1000.0:
801 if best > 0.0 and best < 1000.0:
800 order = min(-int(math.floor(math.log10(best)) // 3), 3)
802 order = min(-int(math.floor(math.log10(best)) // 3), 3)
801 elif best >= 1000.0:
803 elif best >= 1000.0:
802 order = 0
804 order = 0
803 else:
805 else:
804 order = 3
806 order = 3
805 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
807 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
806 precision,
808 precision,
807 best * scaling[order],
809 best * scaling[order],
808 units[order])
810 units[order])
809 if tc > tc_min:
811 if tc > tc_min:
810 print "Compiler time: %.2f s" % tc
812 print "Compiler time: %.2f s" % tc
811
813
812 @skip_doctest
814 @skip_doctest
813 @needs_local_scope
815 @needs_local_scope
814 @line_magic
816 @line_magic
815 def time(self,parameter_s, local_ns=None):
817 def time(self,parameter_s, local_ns=None):
816 """Time execution of a Python statement or expression.
818 """Time execution of a Python statement or expression.
817
819
818 The CPU and wall clock times are printed, and the value of the
820 The CPU and wall clock times are printed, and the value of the
819 expression (if any) is returned. Note that under Win32, system time
821 expression (if any) is returned. Note that under Win32, system time
820 is always reported as 0, since it can not be measured.
822 is always reported as 0, since it can not be measured.
821
823
822 This function provides very basic timing functionality. In Python
824 This function provides very basic timing functionality. In Python
823 2.3, the timeit module offers more control and sophistication, so this
825 2.3, the timeit module offers more control and sophistication, so this
824 could be rewritten to use it (patches welcome).
826 could be rewritten to use it (patches welcome).
825
827
826 Examples
828 Examples
827 --------
829 --------
828 ::
830 ::
829
831
830 In [1]: time 2**128
832 In [1]: time 2**128
831 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
833 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
832 Wall time: 0.00
834 Wall time: 0.00
833 Out[1]: 340282366920938463463374607431768211456L
835 Out[1]: 340282366920938463463374607431768211456L
834
836
835 In [2]: n = 1000000
837 In [2]: n = 1000000
836
838
837 In [3]: time sum(range(n))
839 In [3]: time sum(range(n))
838 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
840 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
839 Wall time: 1.37
841 Wall time: 1.37
840 Out[3]: 499999500000L
842 Out[3]: 499999500000L
841
843
842 In [4]: time print 'hello world'
844 In [4]: time print 'hello world'
843 hello world
845 hello world
844 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
846 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
845 Wall time: 0.00
847 Wall time: 0.00
846
848
847 Note that the time needed by Python to compile the given expression
849 Note that the time needed by Python to compile the given expression
848 will be reported if it is more than 0.1s. In this example, the
850 will be reported if it is more than 0.1s. In this example, the
849 actual exponentiation is done by Python at compilation time, so while
851 actual exponentiation is done by Python at compilation time, so while
850 the expression can take a noticeable amount of time to compute, that
852 the expression can take a noticeable amount of time to compute, that
851 time is purely due to the compilation:
853 time is purely due to the compilation:
852
854
853 In [5]: time 3**9999;
855 In [5]: time 3**9999;
854 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
856 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
855 Wall time: 0.00 s
857 Wall time: 0.00 s
856
858
857 In [6]: time 3**999999;
859 In [6]: time 3**999999;
858 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
860 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
859 Wall time: 0.00 s
861 Wall time: 0.00 s
860 Compiler : 0.78 s
862 Compiler : 0.78 s
861 """
863 """
862
864
863 # fail immediately if the given expression can't be compiled
865 # fail immediately if the given expression can't be compiled
864
866
865 expr = self.shell.prefilter(parameter_s,False)
867 expr = self.shell.prefilter(parameter_s,False)
866
868
867 # Minimum time above which compilation time will be reported
869 # Minimum time above which compilation time will be reported
868 tc_min = 0.1
870 tc_min = 0.1
869
871
870 try:
872 try:
871 mode = 'eval'
873 mode = 'eval'
872 t0 = clock()
874 t0 = clock()
873 code = compile(expr,'<timed eval>',mode)
875 code = compile(expr,'<timed eval>',mode)
874 tc = clock()-t0
876 tc = clock()-t0
875 except SyntaxError:
877 except SyntaxError:
876 mode = 'exec'
878 mode = 'exec'
877 t0 = clock()
879 t0 = clock()
878 code = compile(expr,'<timed exec>',mode)
880 code = compile(expr,'<timed exec>',mode)
879 tc = clock()-t0
881 tc = clock()-t0
880 # skew measurement as little as possible
882 # skew measurement as little as possible
881 glob = self.shell.user_ns
883 glob = self.shell.user_ns
882 wtime = time.time
884 wtime = time.time
883 # time execution
885 # time execution
884 wall_st = wtime()
886 wall_st = wtime()
885 if mode=='eval':
887 if mode=='eval':
886 st = clock2()
888 st = clock2()
887 out = eval(code, glob, local_ns)
889 out = eval(code, glob, local_ns)
888 end = clock2()
890 end = clock2()
889 else:
891 else:
890 st = clock2()
892 st = clock2()
891 exec code in glob, local_ns
893 exec code in glob, local_ns
892 end = clock2()
894 end = clock2()
893 out = None
895 out = None
894 wall_end = wtime()
896 wall_end = wtime()
895 # Compute actual times and report
897 # Compute actual times and report
896 wall_time = wall_end-wall_st
898 wall_time = wall_end-wall_st
897 cpu_user = end[0]-st[0]
899 cpu_user = end[0]-st[0]
898 cpu_sys = end[1]-st[1]
900 cpu_sys = end[1]-st[1]
899 cpu_tot = cpu_user+cpu_sys
901 cpu_tot = cpu_user+cpu_sys
900 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
902 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
901 (cpu_user,cpu_sys,cpu_tot)
903 (cpu_user,cpu_sys,cpu_tot)
902 print "Wall time: %.2f s" % wall_time
904 print "Wall time: %.2f s" % wall_time
903 if tc > tc_min:
905 if tc > tc_min:
904 print "Compiler : %.2f s" % tc
906 print "Compiler : %.2f s" % tc
905 return out
907 return out
906
908
907 @skip_doctest
909 @skip_doctest
908 @line_magic
910 @line_magic
909 def macro(self, parameter_s=''):
911 def macro(self, parameter_s=''):
910 """Define a macro for future re-execution. It accepts ranges of history,
912 """Define a macro for future re-execution. It accepts ranges of history,
911 filenames or string objects.
913 filenames or string objects.
912
914
913 Usage:\\
915 Usage:\\
914 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
916 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
915
917
916 Options:
918 Options:
917
919
918 -r: use 'raw' input. By default, the 'processed' history is used,
920 -r: use 'raw' input. By default, the 'processed' history is used,
919 so that magics are loaded in their transformed version to valid
921 so that magics are loaded in their transformed version to valid
920 Python. If this option is given, the raw input as typed as the
922 Python. If this option is given, the raw input as typed as the
921 command line is used instead.
923 command line is used instead.
922
924
923 This will define a global variable called `name` which is a string
925 This will define a global variable called `name` which is a string
924 made of joining the slices and lines you specify (n1,n2,... numbers
926 made of joining the slices and lines you specify (n1,n2,... numbers
925 above) from your input history into a single string. This variable
927 above) from your input history into a single string. This variable
926 acts like an automatic function which re-executes those lines as if
928 acts like an automatic function which re-executes those lines as if
927 you had typed them. You just type 'name' at the prompt and the code
929 you had typed them. You just type 'name' at the prompt and the code
928 executes.
930 executes.
929
931
930 The syntax for indicating input ranges is described in %history.
932 The syntax for indicating input ranges is described in %history.
931
933
932 Note: as a 'hidden' feature, you can also use traditional python slice
934 Note: as a 'hidden' feature, you can also use traditional python slice
933 notation, where N:M means numbers N through M-1.
935 notation, where N:M means numbers N through M-1.
934
936
935 For example, if your history contains (%hist prints it)::
937 For example, if your history contains (%hist prints it)::
936
938
937 44: x=1
939 44: x=1
938 45: y=3
940 45: y=3
939 46: z=x+y
941 46: z=x+y
940 47: print x
942 47: print x
941 48: a=5
943 48: a=5
942 49: print 'x',x,'y',y
944 49: print 'x',x,'y',y
943
945
944 you can create a macro with lines 44 through 47 (included) and line 49
946 you can create a macro with lines 44 through 47 (included) and line 49
945 called my_macro with::
947 called my_macro with::
946
948
947 In [55]: %macro my_macro 44-47 49
949 In [55]: %macro my_macro 44-47 49
948
950
949 Now, typing `my_macro` (without quotes) will re-execute all this code
951 Now, typing `my_macro` (without quotes) will re-execute all this code
950 in one pass.
952 in one pass.
951
953
952 You don't need to give the line-numbers in order, and any given line
954 You don't need to give the line-numbers in order, and any given line
953 number can appear multiple times. You can assemble macros with any
955 number can appear multiple times. You can assemble macros with any
954 lines from your input history in any order.
956 lines from your input history in any order.
955
957
956 The macro is a simple object which holds its value in an attribute,
958 The macro is a simple object which holds its value in an attribute,
957 but IPython's display system checks for macros and executes them as
959 but IPython's display system checks for macros and executes them as
958 code instead of printing them when you type their name.
960 code instead of printing them when you type their name.
959
961
960 You can view a macro's contents by explicitly printing it with::
962 You can view a macro's contents by explicitly printing it with::
961
963
962 print macro_name
964 print macro_name
963
965
964 """
966 """
965 opts,args = self.parse_options(parameter_s,'r',mode='list')
967 opts,args = self.parse_options(parameter_s,'r',mode='list')
966 if not args: # List existing macros
968 if not args: # List existing macros
967 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
969 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
968 isinstance(v, Macro))
970 isinstance(v, Macro))
969 if len(args) == 1:
971 if len(args) == 1:
970 raise UsageError(
972 raise UsageError(
971 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
973 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
972 name, codefrom = args[0], " ".join(args[1:])
974 name, codefrom = args[0], " ".join(args[1:])
973
975
974 #print 'rng',ranges # dbg
976 #print 'rng',ranges # dbg
975 try:
977 try:
976 lines = self.shell.find_user_code(codefrom, 'r' in opts)
978 lines = self.shell.find_user_code(codefrom, 'r' in opts)
977 except (ValueError, TypeError) as e:
979 except (ValueError, TypeError) as e:
978 print e.args[0]
980 print e.args[0]
979 return
981 return
980 macro = Macro(lines)
982 macro = Macro(lines)
981 self.shell.define_macro(name, macro)
983 self.shell.define_macro(name, macro)
982 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
984 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
983 print '=== Macro contents: ==='
985 print '=== Macro contents: ==='
984 print macro,
986 print macro,
985
987
986 @magic_arguments.magic_arguments()
988 @magic_arguments.magic_arguments()
987 @magic_arguments.argument('output', type=str, default='', nargs='?',
989 @magic_arguments.argument('output', type=str, default='', nargs='?',
988 help="""The name of the variable in which to store output.
990 help="""The name of the variable in which to store output.
989 This is a utils.io.CapturedIO object with stdout/err attributes
991 This is a utils.io.CapturedIO object with stdout/err attributes
990 for the text of the captured output.
992 for the text of the captured output.
991
993
992 CapturedOutput also has a show() method for displaying the output,
994 CapturedOutput also has a show() method for displaying the output,
993 and __call__ as well, so you can use that to quickly display the
995 and __call__ as well, so you can use that to quickly display the
994 output.
996 output.
995
997
996 If unspecified, captured output is discarded.
998 If unspecified, captured output is discarded.
997 """
999 """
998 )
1000 )
999 @magic_arguments.argument('--no-stderr', action="store_true",
1001 @magic_arguments.argument('--no-stderr', action="store_true",
1000 help="""Don't capture stderr."""
1002 help="""Don't capture stderr."""
1001 )
1003 )
1002 @magic_arguments.argument('--no-stdout', action="store_true",
1004 @magic_arguments.argument('--no-stdout', action="store_true",
1003 help="""Don't capture stdout."""
1005 help="""Don't capture stdout."""
1004 )
1006 )
1005 @cell_magic
1007 @cell_magic
1006 def capture(self, line, cell):
1008 def capture(self, line, cell):
1007 """run the cell, capturing stdout/err"""
1009 """run the cell, capturing stdout/err"""
1008 args = magic_arguments.parse_argstring(self.capture, line)
1010 args = magic_arguments.parse_argstring(self.capture, line)
1009 out = not args.no_stdout
1011 out = not args.no_stdout
1010 err = not args.no_stderr
1012 err = not args.no_stderr
1011 with capture_output(out, err) as io:
1013 with capture_output(out, err) as io:
1012 self.shell.run_cell(cell)
1014 self.shell.run_cell(cell)
1013 if args.output:
1015 if args.output:
1014 self.shell.user_ns[args.output] = io
1016 self.shell.user_ns[args.output] = io
General Comments 0
You need to be logged in to leave comments. Login now