##// END OF EJS Templates
For timeit magic, no s at "loops" if only one loop...
Lilian Besson -
Show More
@@ -1,1354 +1,1362 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Implementation of execution-related magic functions."""
2 """Implementation of execution-related magic functions."""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
7 from __future__ import print_function
8
8
9 import ast
9 import ast
10 import bdb
10 import bdb
11 import gc
11 import gc
12 import itertools
12 import itertools
13 import os
13 import os
14 import sys
14 import sys
15 import time
15 import time
16 import timeit
16 import timeit
17 from pdb import Restart
17 from pdb import Restart
18
18
19 # cProfile was added in Python2.5
19 # cProfile was added in Python2.5
20 try:
20 try:
21 import cProfile as profile
21 import cProfile as profile
22 import pstats
22 import pstats
23 except ImportError:
23 except ImportError:
24 # profile isn't bundled by default in Debian for license reasons
24 # profile isn't bundled by default in Debian for license reasons
25 try:
25 try:
26 import profile, pstats
26 import profile, pstats
27 except ImportError:
27 except ImportError:
28 profile = pstats = None
28 profile = pstats = None
29
29
30 from IPython.core import debugger, oinspect
30 from IPython.core import debugger, oinspect
31 from IPython.core import magic_arguments
31 from IPython.core import magic_arguments
32 from IPython.core import page
32 from IPython.core import page
33 from IPython.core.error import UsageError
33 from IPython.core.error import UsageError
34 from IPython.core.macro import Macro
34 from IPython.core.macro import Macro
35 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
35 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
36 line_cell_magic, on_off, needs_local_scope)
36 line_cell_magic, on_off, needs_local_scope)
37 from IPython.testing.skipdoctest import skip_doctest
37 from IPython.testing.skipdoctest import skip_doctest
38 from IPython.utils import py3compat
38 from IPython.utils import py3compat
39 from IPython.utils.py3compat import builtin_mod, iteritems, PY3
39 from IPython.utils.py3compat import builtin_mod, iteritems, PY3
40 from IPython.utils.contexts import preserve_keys
40 from IPython.utils.contexts import preserve_keys
41 from IPython.utils.capture import capture_output
41 from IPython.utils.capture import capture_output
42 from IPython.utils.ipstruct import Struct
42 from IPython.utils.ipstruct import Struct
43 from IPython.utils.module_paths import find_mod
43 from IPython.utils.module_paths import find_mod
44 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
44 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
45 from IPython.utils.timing import clock, clock2
45 from IPython.utils.timing import clock, clock2
46 from IPython.utils.warn import warn, error
46 from IPython.utils.warn import warn, error
47
47
48 if PY3:
48 if PY3:
49 from io import StringIO
49 from io import StringIO
50 else:
50 else:
51 from StringIO import StringIO
51 from StringIO import StringIO
52
52
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54 # Magic implementation classes
54 # Magic implementation classes
55 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
56
56
57
57
58 class TimeitResult(object):
58 class TimeitResult(object):
59 """
59 """
60 Object returned by the timeit magic with info about the run.
60 Object returned by the timeit magic with info about the run.
61
61
62 Contain the following attributes :
62 Contain the following attributes :
63
63
64 loops: (int) number of loop done per measurement
64 loops: (int) number of loop done per measurement
65 repeat: (int) number of time the mesurement has been repeated
65 repeat: (int) number of time the mesurement has been repeated
66 best: (float) best execusion time / number
66 best: (float) best execusion time / number
67 all_runs: (list of float) execusion time of each run (in s)
67 all_runs: (list of float) execusion time of each run (in s)
68 compile_time: (float) time of statement compilation (s)
68 compile_time: (float) time of statement compilation (s)
69
69
70 """
70 """
71
71
72 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
72 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
73 self.loops = loops
73 self.loops = loops
74 self.repeat = repeat
74 self.repeat = repeat
75 self.best = best
75 self.best = best
76 self.worst = worst
76 self.worst = worst
77 self.all_runs = all_runs
77 self.all_runs = all_runs
78 self.compile_time = compile_time
78 self.compile_time = compile_time
79 self._precision = precision
79 self._precision = precision
80
80
81 def _repr_pretty_(self, p , cycle):
81 def _repr_pretty_(self, p , cycle):
82 if self.loops == 1: # No s at "loops" if only one loop
83 unic = u"%d loop, best of %d: %s per loop" % (self.loops, self.repeat,
84 _format_time(self.best, self._precision))
85 else:
82 unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat,
86 unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat,
83 _format_time(self.best, self._precision))
87 _format_time(self.best, self._precision))
84 p.text(u'<TimeitResult : '+unic+u'>')
88 p.text(u'<TimeitResult : '+unic+u'>')
85
89
86
90
87 class TimeitTemplateFiller(ast.NodeTransformer):
91 class TimeitTemplateFiller(ast.NodeTransformer):
88 """Fill in the AST template for timing execution.
92 """Fill in the AST template for timing execution.
89
93
90 This is quite closely tied to the template definition, which is in
94 This is quite closely tied to the template definition, which is in
91 :meth:`ExecutionMagics.timeit`.
95 :meth:`ExecutionMagics.timeit`.
92 """
96 """
93 def __init__(self, ast_setup, ast_stmt):
97 def __init__(self, ast_setup, ast_stmt):
94 self.ast_setup = ast_setup
98 self.ast_setup = ast_setup
95 self.ast_stmt = ast_stmt
99 self.ast_stmt = ast_stmt
96
100
97 def visit_FunctionDef(self, node):
101 def visit_FunctionDef(self, node):
98 "Fill in the setup statement"
102 "Fill in the setup statement"
99 self.generic_visit(node)
103 self.generic_visit(node)
100 if node.name == "inner":
104 if node.name == "inner":
101 node.body[:1] = self.ast_setup.body
105 node.body[:1] = self.ast_setup.body
102
106
103 return node
107 return node
104
108
105 def visit_For(self, node):
109 def visit_For(self, node):
106 "Fill in the statement to be timed"
110 "Fill in the statement to be timed"
107 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
111 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
108 node.body = self.ast_stmt.body
112 node.body = self.ast_stmt.body
109 return node
113 return node
110
114
111
115
112 class Timer(timeit.Timer):
116 class Timer(timeit.Timer):
113 """Timer class that explicitly uses self.inner
117 """Timer class that explicitly uses self.inner
114
118
115 which is an undocumented implementation detail of CPython,
119 which is an undocumented implementation detail of CPython,
116 not shared by PyPy.
120 not shared by PyPy.
117 """
121 """
118 # Timer.timeit copied from CPython 3.4.2
122 # Timer.timeit copied from CPython 3.4.2
119 def timeit(self, number=timeit.default_number):
123 def timeit(self, number=timeit.default_number):
120 """Time 'number' executions of the main statement.
124 """Time 'number' executions of the main statement.
121
125
122 To be precise, this executes the setup statement once, and
126 To be precise, this executes the setup statement once, and
123 then returns the time it takes to execute the main statement
127 then returns the time it takes to execute the main statement
124 a number of times, as a float measured in seconds. The
128 a number of times, as a float measured in seconds. The
125 argument is the number of times through the loop, defaulting
129 argument is the number of times through the loop, defaulting
126 to one million. The main statement, the setup statement and
130 to one million. The main statement, the setup statement and
127 the timer function to be used are passed to the constructor.
131 the timer function to be used are passed to the constructor.
128 """
132 """
129 it = itertools.repeat(None, number)
133 it = itertools.repeat(None, number)
130 gcold = gc.isenabled()
134 gcold = gc.isenabled()
131 gc.disable()
135 gc.disable()
132 try:
136 try:
133 timing = self.inner(it, self.timer)
137 timing = self.inner(it, self.timer)
134 finally:
138 finally:
135 if gcold:
139 if gcold:
136 gc.enable()
140 gc.enable()
137 return timing
141 return timing
138
142
139
143
140 @magics_class
144 @magics_class
141 class ExecutionMagics(Magics):
145 class ExecutionMagics(Magics):
142 """Magics related to code execution, debugging, profiling, etc.
146 """Magics related to code execution, debugging, profiling, etc.
143
147
144 """
148 """
145
149
146 def __init__(self, shell):
150 def __init__(self, shell):
147 super(ExecutionMagics, self).__init__(shell)
151 super(ExecutionMagics, self).__init__(shell)
148 if profile is None:
152 if profile is None:
149 self.prun = self.profile_missing_notice
153 self.prun = self.profile_missing_notice
150 # Default execution function used to actually run user code.
154 # Default execution function used to actually run user code.
151 self.default_runner = None
155 self.default_runner = None
152
156
153 def profile_missing_notice(self, *args, **kwargs):
157 def profile_missing_notice(self, *args, **kwargs):
154 error("""\
158 error("""\
155 The profile module could not be found. It has been removed from the standard
159 The profile module could not be found. It has been removed from the standard
156 python packages because of its non-free license. To use profiling, install the
160 python packages because of its non-free license. To use profiling, install the
157 python-profiler package from non-free.""")
161 python-profiler package from non-free.""")
158
162
159 @skip_doctest
163 @skip_doctest
160 @line_cell_magic
164 @line_cell_magic
161 def prun(self, parameter_s='', cell=None):
165 def prun(self, parameter_s='', cell=None):
162
166
163 """Run a statement through the python code profiler.
167 """Run a statement through the python code profiler.
164
168
165 Usage, in line mode:
169 Usage, in line mode:
166 %prun [options] statement
170 %prun [options] statement
167
171
168 Usage, in cell mode:
172 Usage, in cell mode:
169 %%prun [options] [statement]
173 %%prun [options] [statement]
170 code...
174 code...
171 code...
175 code...
172
176
173 In cell mode, the additional code lines are appended to the (possibly
177 In cell mode, the additional code lines are appended to the (possibly
174 empty) statement in the first line. Cell mode allows you to easily
178 empty) statement in the first line. Cell mode allows you to easily
175 profile multiline blocks without having to put them in a separate
179 profile multiline blocks without having to put them in a separate
176 function.
180 function.
177
181
178 The given statement (which doesn't require quote marks) is run via the
182 The given statement (which doesn't require quote marks) is run via the
179 python profiler in a manner similar to the profile.run() function.
183 python profiler in a manner similar to the profile.run() function.
180 Namespaces are internally managed to work correctly; profile.run
184 Namespaces are internally managed to work correctly; profile.run
181 cannot be used in IPython because it makes certain assumptions about
185 cannot be used in IPython because it makes certain assumptions about
182 namespaces which do not hold under IPython.
186 namespaces which do not hold under IPython.
183
187
184 Options:
188 Options:
185
189
186 -l <limit>
190 -l <limit>
187 you can place restrictions on what or how much of the
191 you can place restrictions on what or how much of the
188 profile gets printed. The limit value can be:
192 profile gets printed. The limit value can be:
189
193
190 * A string: only information for function names containing this string
194 * A string: only information for function names containing this string
191 is printed.
195 is printed.
192
196
193 * An integer: only these many lines are printed.
197 * An integer: only these many lines are printed.
194
198
195 * A float (between 0 and 1): this fraction of the report is printed
199 * A float (between 0 and 1): this fraction of the report is printed
196 (for example, use a limit of 0.4 to see the topmost 40% only).
200 (for example, use a limit of 0.4 to see the topmost 40% only).
197
201
198 You can combine several limits with repeated use of the option. For
202 You can combine several limits with repeated use of the option. For
199 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
203 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
200 information about class constructors.
204 information about class constructors.
201
205
202 -r
206 -r
203 return the pstats.Stats object generated by the profiling. This
207 return the pstats.Stats object generated by the profiling. This
204 object has all the information about the profile in it, and you can
208 object has all the information about the profile in it, and you can
205 later use it for further analysis or in other functions.
209 later use it for further analysis or in other functions.
206
210
207 -s <key>
211 -s <key>
208 sort profile by given key. You can provide more than one key
212 sort profile by given key. You can provide more than one key
209 by using the option several times: '-s key1 -s key2 -s key3...'. The
213 by using the option several times: '-s key1 -s key2 -s key3...'. The
210 default sorting key is 'time'.
214 default sorting key is 'time'.
211
215
212 The following is copied verbatim from the profile documentation
216 The following is copied verbatim from the profile documentation
213 referenced below:
217 referenced below:
214
218
215 When more than one key is provided, additional keys are used as
219 When more than one key is provided, additional keys are used as
216 secondary criteria when the there is equality in all keys selected
220 secondary criteria when the there is equality in all keys selected
217 before them.
221 before them.
218
222
219 Abbreviations can be used for any key names, as long as the
223 Abbreviations can be used for any key names, as long as the
220 abbreviation is unambiguous. The following are the keys currently
224 abbreviation is unambiguous. The following are the keys currently
221 defined:
225 defined:
222
226
223 ============ =====================
227 ============ =====================
224 Valid Arg Meaning
228 Valid Arg Meaning
225 ============ =====================
229 ============ =====================
226 "calls" call count
230 "calls" call count
227 "cumulative" cumulative time
231 "cumulative" cumulative time
228 "file" file name
232 "file" file name
229 "module" file name
233 "module" file name
230 "pcalls" primitive call count
234 "pcalls" primitive call count
231 "line" line number
235 "line" line number
232 "name" function name
236 "name" function name
233 "nfl" name/file/line
237 "nfl" name/file/line
234 "stdname" standard name
238 "stdname" standard name
235 "time" internal time
239 "time" internal time
236 ============ =====================
240 ============ =====================
237
241
238 Note that all sorts on statistics are in descending order (placing
242 Note that all sorts on statistics are in descending order (placing
239 most time consuming items first), where as name, file, and line number
243 most time consuming items first), where as name, file, and line number
240 searches are in ascending order (i.e., alphabetical). The subtle
244 searches are in ascending order (i.e., alphabetical). The subtle
241 distinction between "nfl" and "stdname" is that the standard name is a
245 distinction between "nfl" and "stdname" is that the standard name is a
242 sort of the name as printed, which means that the embedded line
246 sort of the name as printed, which means that the embedded line
243 numbers get compared in an odd way. For example, lines 3, 20, and 40
247 numbers get compared in an odd way. For example, lines 3, 20, and 40
244 would (if the file names were the same) appear in the string order
248 would (if the file names were the same) appear in the string order
245 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
249 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
246 line numbers. In fact, sort_stats("nfl") is the same as
250 line numbers. In fact, sort_stats("nfl") is the same as
247 sort_stats("name", "file", "line").
251 sort_stats("name", "file", "line").
248
252
249 -T <filename>
253 -T <filename>
250 save profile results as shown on screen to a text
254 save profile results as shown on screen to a text
251 file. The profile is still shown on screen.
255 file. The profile is still shown on screen.
252
256
253 -D <filename>
257 -D <filename>
254 save (via dump_stats) profile statistics to given
258 save (via dump_stats) profile statistics to given
255 filename. This data is in a format understood by the pstats module, and
259 filename. This data is in a format understood by the pstats module, and
256 is generated by a call to the dump_stats() method of profile
260 is generated by a call to the dump_stats() method of profile
257 objects. The profile is still shown on screen.
261 objects. The profile is still shown on screen.
258
262
259 -q
263 -q
260 suppress output to the pager. Best used with -T and/or -D above.
264 suppress output to the pager. Best used with -T and/or -D above.
261
265
262 If you want to run complete programs under the profiler's control, use
266 If you want to run complete programs under the profiler's control, use
263 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
267 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
264 contains profiler specific options as described here.
268 contains profiler specific options as described here.
265
269
266 You can read the complete documentation for the profile module with::
270 You can read the complete documentation for the profile module with::
267
271
268 In [1]: import profile; profile.help()
272 In [1]: import profile; profile.help()
269 """
273 """
270 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
274 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
271 list_all=True, posix=False)
275 list_all=True, posix=False)
272 if cell is not None:
276 if cell is not None:
273 arg_str += '\n' + cell
277 arg_str += '\n' + cell
274 arg_str = self.shell.input_splitter.transform_cell(arg_str)
278 arg_str = self.shell.input_splitter.transform_cell(arg_str)
275 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
279 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
276
280
277 def _run_with_profiler(self, code, opts, namespace):
281 def _run_with_profiler(self, code, opts, namespace):
278 """
282 """
279 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
283 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
280
284
281 Parameters
285 Parameters
282 ----------
286 ----------
283 code : str
287 code : str
284 Code to be executed.
288 Code to be executed.
285 opts : Struct
289 opts : Struct
286 Options parsed by `self.parse_options`.
290 Options parsed by `self.parse_options`.
287 namespace : dict
291 namespace : dict
288 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
292 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
289
293
290 """
294 """
291
295
292 # Fill default values for unspecified options:
296 # Fill default values for unspecified options:
293 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
297 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
294
298
295 prof = profile.Profile()
299 prof = profile.Profile()
296 try:
300 try:
297 prof = prof.runctx(code, namespace, namespace)
301 prof = prof.runctx(code, namespace, namespace)
298 sys_exit = ''
302 sys_exit = ''
299 except SystemExit:
303 except SystemExit:
300 sys_exit = """*** SystemExit exception caught in code being profiled."""
304 sys_exit = """*** SystemExit exception caught in code being profiled."""
301
305
302 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
306 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
303
307
304 lims = opts.l
308 lims = opts.l
305 if lims:
309 if lims:
306 lims = [] # rebuild lims with ints/floats/strings
310 lims = [] # rebuild lims with ints/floats/strings
307 for lim in opts.l:
311 for lim in opts.l:
308 try:
312 try:
309 lims.append(int(lim))
313 lims.append(int(lim))
310 except ValueError:
314 except ValueError:
311 try:
315 try:
312 lims.append(float(lim))
316 lims.append(float(lim))
313 except ValueError:
317 except ValueError:
314 lims.append(lim)
318 lims.append(lim)
315
319
316 # Trap output.
320 # Trap output.
317 stdout_trap = StringIO()
321 stdout_trap = StringIO()
318 stats_stream = stats.stream
322 stats_stream = stats.stream
319 try:
323 try:
320 stats.stream = stdout_trap
324 stats.stream = stdout_trap
321 stats.print_stats(*lims)
325 stats.print_stats(*lims)
322 finally:
326 finally:
323 stats.stream = stats_stream
327 stats.stream = stats_stream
324
328
325 output = stdout_trap.getvalue()
329 output = stdout_trap.getvalue()
326 output = output.rstrip()
330 output = output.rstrip()
327
331
328 if 'q' not in opts:
332 if 'q' not in opts:
329 page.page(output)
333 page.page(output)
330 print(sys_exit, end=' ')
334 print(sys_exit, end=' ')
331
335
332 dump_file = opts.D[0]
336 dump_file = opts.D[0]
333 text_file = opts.T[0]
337 text_file = opts.T[0]
334 if dump_file:
338 if dump_file:
335 dump_file = unquote_filename(dump_file)
339 dump_file = unquote_filename(dump_file)
336 prof.dump_stats(dump_file)
340 prof.dump_stats(dump_file)
337 print('\n*** Profile stats marshalled to file',\
341 print('\n*** Profile stats marshalled to file',\
338 repr(dump_file)+'.',sys_exit)
342 repr(dump_file)+'.',sys_exit)
339 if text_file:
343 if text_file:
340 text_file = unquote_filename(text_file)
344 text_file = unquote_filename(text_file)
341 pfile = open(text_file,'w')
345 pfile = open(text_file,'w')
342 pfile.write(output)
346 pfile.write(output)
343 pfile.close()
347 pfile.close()
344 print('\n*** Profile printout saved to text file',\
348 print('\n*** Profile printout saved to text file',\
345 repr(text_file)+'.',sys_exit)
349 repr(text_file)+'.',sys_exit)
346
350
347 if 'r' in opts:
351 if 'r' in opts:
348 return stats
352 return stats
349 else:
353 else:
350 return None
354 return None
351
355
352 @line_magic
356 @line_magic
353 def pdb(self, parameter_s=''):
357 def pdb(self, parameter_s=''):
354 """Control the automatic calling of the pdb interactive debugger.
358 """Control the automatic calling of the pdb interactive debugger.
355
359
356 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
360 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
357 argument it works as a toggle.
361 argument it works as a toggle.
358
362
359 When an exception is triggered, IPython can optionally call the
363 When an exception is triggered, IPython can optionally call the
360 interactive pdb debugger after the traceback printout. %pdb toggles
364 interactive pdb debugger after the traceback printout. %pdb toggles
361 this feature on and off.
365 this feature on and off.
362
366
363 The initial state of this feature is set in your configuration
367 The initial state of this feature is set in your configuration
364 file (the option is ``InteractiveShell.pdb``).
368 file (the option is ``InteractiveShell.pdb``).
365
369
366 If you want to just activate the debugger AFTER an exception has fired,
370 If you want to just activate the debugger AFTER an exception has fired,
367 without having to type '%pdb on' and rerunning your code, you can use
371 without having to type '%pdb on' and rerunning your code, you can use
368 the %debug magic."""
372 the %debug magic."""
369
373
370 par = parameter_s.strip().lower()
374 par = parameter_s.strip().lower()
371
375
372 if par:
376 if par:
373 try:
377 try:
374 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
378 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
375 except KeyError:
379 except KeyError:
376 print ('Incorrect argument. Use on/1, off/0, '
380 print ('Incorrect argument. Use on/1, off/0, '
377 'or nothing for a toggle.')
381 'or nothing for a toggle.')
378 return
382 return
379 else:
383 else:
380 # toggle
384 # toggle
381 new_pdb = not self.shell.call_pdb
385 new_pdb = not self.shell.call_pdb
382
386
383 # set on the shell
387 # set on the shell
384 self.shell.call_pdb = new_pdb
388 self.shell.call_pdb = new_pdb
385 print('Automatic pdb calling has been turned',on_off(new_pdb))
389 print('Automatic pdb calling has been turned',on_off(new_pdb))
386
390
387 @skip_doctest
391 @skip_doctest
388 @magic_arguments.magic_arguments()
392 @magic_arguments.magic_arguments()
389 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
393 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
390 help="""
394 help="""
391 Set break point at LINE in FILE.
395 Set break point at LINE in FILE.
392 """
396 """
393 )
397 )
394 @magic_arguments.argument('statement', nargs='*',
398 @magic_arguments.argument('statement', nargs='*',
395 help="""
399 help="""
396 Code to run in debugger.
400 Code to run in debugger.
397 You can omit this in cell magic mode.
401 You can omit this in cell magic mode.
398 """
402 """
399 )
403 )
400 @line_cell_magic
404 @line_cell_magic
401 def debug(self, line='', cell=None):
405 def debug(self, line='', cell=None):
402 """Activate the interactive debugger.
406 """Activate the interactive debugger.
403
407
404 This magic command support two ways of activating debugger.
408 This magic command support two ways of activating debugger.
405 One is to activate debugger before executing code. This way, you
409 One is to activate debugger before executing code. This way, you
406 can set a break point, to step through the code from the point.
410 can set a break point, to step through the code from the point.
407 You can use this mode by giving statements to execute and optionally
411 You can use this mode by giving statements to execute and optionally
408 a breakpoint.
412 a breakpoint.
409
413
410 The other one is to activate debugger in post-mortem mode. You can
414 The other one is to activate debugger in post-mortem mode. You can
411 activate this mode simply running %debug without any argument.
415 activate this mode simply running %debug without any argument.
412 If an exception has just occurred, this lets you inspect its stack
416 If an exception has just occurred, this lets you inspect its stack
413 frames interactively. Note that this will always work only on the last
417 frames interactively. Note that this will always work only on the last
414 traceback that occurred, so you must call this quickly after an
418 traceback that occurred, so you must call this quickly after an
415 exception that you wish to inspect has fired, because if another one
419 exception that you wish to inspect has fired, because if another one
416 occurs, it clobbers the previous one.
420 occurs, it clobbers the previous one.
417
421
418 If you want IPython to automatically do this on every exception, see
422 If you want IPython to automatically do this on every exception, see
419 the %pdb magic for more details.
423 the %pdb magic for more details.
420 """
424 """
421 args = magic_arguments.parse_argstring(self.debug, line)
425 args = magic_arguments.parse_argstring(self.debug, line)
422
426
423 if not (args.breakpoint or args.statement or cell):
427 if not (args.breakpoint or args.statement or cell):
424 self._debug_post_mortem()
428 self._debug_post_mortem()
425 else:
429 else:
426 code = "\n".join(args.statement)
430 code = "\n".join(args.statement)
427 if cell:
431 if cell:
428 code += "\n" + cell
432 code += "\n" + cell
429 self._debug_exec(code, args.breakpoint)
433 self._debug_exec(code, args.breakpoint)
430
434
431 def _debug_post_mortem(self):
435 def _debug_post_mortem(self):
432 self.shell.debugger(force=True)
436 self.shell.debugger(force=True)
433
437
434 def _debug_exec(self, code, breakpoint):
438 def _debug_exec(self, code, breakpoint):
435 if breakpoint:
439 if breakpoint:
436 (filename, bp_line) = breakpoint.split(':', 1)
440 (filename, bp_line) = breakpoint.split(':', 1)
437 bp_line = int(bp_line)
441 bp_line = int(bp_line)
438 else:
442 else:
439 (filename, bp_line) = (None, None)
443 (filename, bp_line) = (None, None)
440 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
444 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
441
445
442 @line_magic
446 @line_magic
443 def tb(self, s):
447 def tb(self, s):
444 """Print the last traceback with the currently active exception mode.
448 """Print the last traceback with the currently active exception mode.
445
449
446 See %xmode for changing exception reporting modes."""
450 See %xmode for changing exception reporting modes."""
447 self.shell.showtraceback()
451 self.shell.showtraceback()
448
452
449 @skip_doctest
453 @skip_doctest
450 @line_magic
454 @line_magic
451 def run(self, parameter_s='', runner=None,
455 def run(self, parameter_s='', runner=None,
452 file_finder=get_py_filename):
456 file_finder=get_py_filename):
453 """Run the named file inside IPython as a program.
457 """Run the named file inside IPython as a program.
454
458
455 Usage::
459 Usage::
456
460
457 %run [-n -i -e -G]
461 %run [-n -i -e -G]
458 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
462 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
459 ( -m mod | file ) [args]
463 ( -m mod | file ) [args]
460
464
461 Parameters after the filename are passed as command-line arguments to
465 Parameters after the filename are passed as command-line arguments to
462 the program (put in sys.argv). Then, control returns to IPython's
466 the program (put in sys.argv). Then, control returns to IPython's
463 prompt.
467 prompt.
464
468
465 This is similar to running at a system prompt ``python file args``,
469 This is similar to running at a system prompt ``python file args``,
466 but with the advantage of giving you IPython's tracebacks, and of
470 but with the advantage of giving you IPython's tracebacks, and of
467 loading all variables into your interactive namespace for further use
471 loading all variables into your interactive namespace for further use
468 (unless -p is used, see below).
472 (unless -p is used, see below).
469
473
470 The file is executed in a namespace initially consisting only of
474 The file is executed in a namespace initially consisting only of
471 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
475 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
472 sees its environment as if it were being run as a stand-alone program
476 sees its environment as if it were being run as a stand-alone program
473 (except for sharing global objects such as previously imported
477 (except for sharing global objects such as previously imported
474 modules). But after execution, the IPython interactive namespace gets
478 modules). But after execution, the IPython interactive namespace gets
475 updated with all variables defined in the program (except for __name__
479 updated with all variables defined in the program (except for __name__
476 and sys.argv). This allows for very convenient loading of code for
480 and sys.argv). This allows for very convenient loading of code for
477 interactive work, while giving each program a 'clean sheet' to run in.
481 interactive work, while giving each program a 'clean sheet' to run in.
478
482
479 Arguments are expanded using shell-like glob match. Patterns
483 Arguments are expanded using shell-like glob match. Patterns
480 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
484 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
481 tilde '~' will be expanded into user's home directory. Unlike
485 tilde '~' will be expanded into user's home directory. Unlike
482 real shells, quotation does not suppress expansions. Use
486 real shells, quotation does not suppress expansions. Use
483 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
487 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
484 To completely disable these expansions, you can use -G flag.
488 To completely disable these expansions, you can use -G flag.
485
489
486 Options:
490 Options:
487
491
488 -n
492 -n
489 __name__ is NOT set to '__main__', but to the running file's name
493 __name__ is NOT set to '__main__', but to the running file's name
490 without extension (as python does under import). This allows running
494 without extension (as python does under import). This allows running
491 scripts and reloading the definitions in them without calling code
495 scripts and reloading the definitions in them without calling code
492 protected by an ``if __name__ == "__main__"`` clause.
496 protected by an ``if __name__ == "__main__"`` clause.
493
497
494 -i
498 -i
495 run the file in IPython's namespace instead of an empty one. This
499 run the file in IPython's namespace instead of an empty one. This
496 is useful if you are experimenting with code written in a text editor
500 is useful if you are experimenting with code written in a text editor
497 which depends on variables defined interactively.
501 which depends on variables defined interactively.
498
502
499 -e
503 -e
500 ignore sys.exit() calls or SystemExit exceptions in the script
504 ignore sys.exit() calls or SystemExit exceptions in the script
501 being run. This is particularly useful if IPython is being used to
505 being run. This is particularly useful if IPython is being used to
502 run unittests, which always exit with a sys.exit() call. In such
506 run unittests, which always exit with a sys.exit() call. In such
503 cases you are interested in the output of the test results, not in
507 cases you are interested in the output of the test results, not in
504 seeing a traceback of the unittest module.
508 seeing a traceback of the unittest module.
505
509
506 -t
510 -t
507 print timing information at the end of the run. IPython will give
511 print timing information at the end of the run. IPython will give
508 you an estimated CPU time consumption for your script, which under
512 you an estimated CPU time consumption for your script, which under
509 Unix uses the resource module to avoid the wraparound problems of
513 Unix uses the resource module to avoid the wraparound problems of
510 time.clock(). Under Unix, an estimate of time spent on system tasks
514 time.clock(). Under Unix, an estimate of time spent on system tasks
511 is also given (for Windows platforms this is reported as 0.0).
515 is also given (for Windows platforms this is reported as 0.0).
512
516
513 If -t is given, an additional ``-N<N>`` option can be given, where <N>
517 If -t is given, an additional ``-N<N>`` option can be given, where <N>
514 must be an integer indicating how many times you want the script to
518 must be an integer indicating how many times you want the script to
515 run. The final timing report will include total and per run results.
519 run. The final timing report will include total and per run results.
516
520
517 For example (testing the script uniq_stable.py)::
521 For example (testing the script uniq_stable.py)::
518
522
519 In [1]: run -t uniq_stable
523 In [1]: run -t uniq_stable
520
524
521 IPython CPU timings (estimated):
525 IPython CPU timings (estimated):
522 User : 0.19597 s.
526 User : 0.19597 s.
523 System: 0.0 s.
527 System: 0.0 s.
524
528
525 In [2]: run -t -N5 uniq_stable
529 In [2]: run -t -N5 uniq_stable
526
530
527 IPython CPU timings (estimated):
531 IPython CPU timings (estimated):
528 Total runs performed: 5
532 Total runs performed: 5
529 Times : Total Per run
533 Times : Total Per run
530 User : 0.910862 s, 0.1821724 s.
534 User : 0.910862 s, 0.1821724 s.
531 System: 0.0 s, 0.0 s.
535 System: 0.0 s, 0.0 s.
532
536
533 -d
537 -d
534 run your program under the control of pdb, the Python debugger.
538 run your program under the control of pdb, the Python debugger.
535 This allows you to execute your program step by step, watch variables,
539 This allows you to execute your program step by step, watch variables,
536 etc. Internally, what IPython does is similar to calling::
540 etc. Internally, what IPython does is similar to calling::
537
541
538 pdb.run('execfile("YOURFILENAME")')
542 pdb.run('execfile("YOURFILENAME")')
539
543
540 with a breakpoint set on line 1 of your file. You can change the line
544 with a breakpoint set on line 1 of your file. You can change the line
541 number for this automatic breakpoint to be <N> by using the -bN option
545 number for this automatic breakpoint to be <N> by using the -bN option
542 (where N must be an integer). For example::
546 (where N must be an integer). For example::
543
547
544 %run -d -b40 myscript
548 %run -d -b40 myscript
545
549
546 will set the first breakpoint at line 40 in myscript.py. Note that
550 will set the first breakpoint at line 40 in myscript.py. Note that
547 the first breakpoint must be set on a line which actually does
551 the first breakpoint must be set on a line which actually does
548 something (not a comment or docstring) for it to stop execution.
552 something (not a comment or docstring) for it to stop execution.
549
553
550 Or you can specify a breakpoint in a different file::
554 Or you can specify a breakpoint in a different file::
551
555
552 %run -d -b myotherfile.py:20 myscript
556 %run -d -b myotherfile.py:20 myscript
553
557
554 When the pdb debugger starts, you will see a (Pdb) prompt. You must
558 When the pdb debugger starts, you will see a (Pdb) prompt. You must
555 first enter 'c' (without quotes) to start execution up to the first
559 first enter 'c' (without quotes) to start execution up to the first
556 breakpoint.
560 breakpoint.
557
561
558 Entering 'help' gives information about the use of the debugger. You
562 Entering 'help' gives information about the use of the debugger. You
559 can easily see pdb's full documentation with "import pdb;pdb.help()"
563 can easily see pdb's full documentation with "import pdb;pdb.help()"
560 at a prompt.
564 at a prompt.
561
565
562 -p
566 -p
563 run program under the control of the Python profiler module (which
567 run program under the control of the Python profiler module (which
564 prints a detailed report of execution times, function calls, etc).
568 prints a detailed report of execution times, function calls, etc).
565
569
566 You can pass other options after -p which affect the behavior of the
570 You can pass other options after -p which affect the behavior of the
567 profiler itself. See the docs for %prun for details.
571 profiler itself. See the docs for %prun for details.
568
572
569 In this mode, the program's variables do NOT propagate back to the
573 In this mode, the program's variables do NOT propagate back to the
570 IPython interactive namespace (because they remain in the namespace
574 IPython interactive namespace (because they remain in the namespace
571 where the profiler executes them).
575 where the profiler executes them).
572
576
573 Internally this triggers a call to %prun, see its documentation for
577 Internally this triggers a call to %prun, see its documentation for
574 details on the options available specifically for profiling.
578 details on the options available specifically for profiling.
575
579
576 There is one special usage for which the text above doesn't apply:
580 There is one special usage for which the text above doesn't apply:
577 if the filename ends with .ipy[nb], the file is run as ipython script,
581 if the filename ends with .ipy[nb], the file is run as ipython script,
578 just as if the commands were written on IPython prompt.
582 just as if the commands were written on IPython prompt.
579
583
580 -m
584 -m
581 specify module name to load instead of script path. Similar to
585 specify module name to load instead of script path. Similar to
582 the -m option for the python interpreter. Use this option last if you
586 the -m option for the python interpreter. Use this option last if you
583 want to combine with other %run options. Unlike the python interpreter
587 want to combine with other %run options. Unlike the python interpreter
584 only source modules are allowed no .pyc or .pyo files.
588 only source modules are allowed no .pyc or .pyo files.
585 For example::
589 For example::
586
590
587 %run -m example
591 %run -m example
588
592
589 will run the example module.
593 will run the example module.
590
594
591 -G
595 -G
592 disable shell-like glob expansion of arguments.
596 disable shell-like glob expansion of arguments.
593
597
594 """
598 """
595
599
596 # get arguments and set sys.argv for program to be run.
600 # get arguments and set sys.argv for program to be run.
597 opts, arg_lst = self.parse_options(parameter_s,
601 opts, arg_lst = self.parse_options(parameter_s,
598 'nidtN:b:pD:l:rs:T:em:G',
602 'nidtN:b:pD:l:rs:T:em:G',
599 mode='list', list_all=1)
603 mode='list', list_all=1)
600 if "m" in opts:
604 if "m" in opts:
601 modulename = opts["m"][0]
605 modulename = opts["m"][0]
602 modpath = find_mod(modulename)
606 modpath = find_mod(modulename)
603 if modpath is None:
607 if modpath is None:
604 warn('%r is not a valid modulename on sys.path'%modulename)
608 warn('%r is not a valid modulename on sys.path'%modulename)
605 return
609 return
606 arg_lst = [modpath] + arg_lst
610 arg_lst = [modpath] + arg_lst
607 try:
611 try:
608 filename = file_finder(arg_lst[0])
612 filename = file_finder(arg_lst[0])
609 except IndexError:
613 except IndexError:
610 warn('you must provide at least a filename.')
614 warn('you must provide at least a filename.')
611 print('\n%run:\n', oinspect.getdoc(self.run))
615 print('\n%run:\n', oinspect.getdoc(self.run))
612 return
616 return
613 except IOError as e:
617 except IOError as e:
614 try:
618 try:
615 msg = str(e)
619 msg = str(e)
616 except UnicodeError:
620 except UnicodeError:
617 msg = e.message
621 msg = e.message
618 error(msg)
622 error(msg)
619 return
623 return
620
624
621 if filename.lower().endswith(('.ipy', '.ipynb')):
625 if filename.lower().endswith(('.ipy', '.ipynb')):
622 with preserve_keys(self.shell.user_ns, '__file__'):
626 with preserve_keys(self.shell.user_ns, '__file__'):
623 self.shell.user_ns['__file__'] = filename
627 self.shell.user_ns['__file__'] = filename
624 self.shell.safe_execfile_ipy(filename)
628 self.shell.safe_execfile_ipy(filename)
625 return
629 return
626
630
627 # Control the response to exit() calls made by the script being run
631 # Control the response to exit() calls made by the script being run
628 exit_ignore = 'e' in opts
632 exit_ignore = 'e' in opts
629
633
630 # Make sure that the running script gets a proper sys.argv as if it
634 # Make sure that the running script gets a proper sys.argv as if it
631 # were run from a system shell.
635 # were run from a system shell.
632 save_argv = sys.argv # save it for later restoring
636 save_argv = sys.argv # save it for later restoring
633
637
634 if 'G' in opts:
638 if 'G' in opts:
635 args = arg_lst[1:]
639 args = arg_lst[1:]
636 else:
640 else:
637 # tilde and glob expansion
641 # tilde and glob expansion
638 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
642 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
639
643
640 sys.argv = [filename] + args # put in the proper filename
644 sys.argv = [filename] + args # put in the proper filename
641 # protect sys.argv from potential unicode strings on Python 2:
645 # protect sys.argv from potential unicode strings on Python 2:
642 if not py3compat.PY3:
646 if not py3compat.PY3:
643 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
647 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
644
648
645 if 'i' in opts:
649 if 'i' in opts:
646 # Run in user's interactive namespace
650 # Run in user's interactive namespace
647 prog_ns = self.shell.user_ns
651 prog_ns = self.shell.user_ns
648 __name__save = self.shell.user_ns['__name__']
652 __name__save = self.shell.user_ns['__name__']
649 prog_ns['__name__'] = '__main__'
653 prog_ns['__name__'] = '__main__'
650 main_mod = self.shell.user_module
654 main_mod = self.shell.user_module
651
655
652 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
656 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
653 # set the __file__ global in the script's namespace
657 # set the __file__ global in the script's namespace
654 # TK: Is this necessary in interactive mode?
658 # TK: Is this necessary in interactive mode?
655 prog_ns['__file__'] = filename
659 prog_ns['__file__'] = filename
656 else:
660 else:
657 # Run in a fresh, empty namespace
661 # Run in a fresh, empty namespace
658 if 'n' in opts:
662 if 'n' in opts:
659 name = os.path.splitext(os.path.basename(filename))[0]
663 name = os.path.splitext(os.path.basename(filename))[0]
660 else:
664 else:
661 name = '__main__'
665 name = '__main__'
662
666
663 # The shell MUST hold a reference to prog_ns so after %run
667 # The shell MUST hold a reference to prog_ns so after %run
664 # exits, the python deletion mechanism doesn't zero it out
668 # exits, the python deletion mechanism doesn't zero it out
665 # (leaving dangling references). See interactiveshell for details
669 # (leaving dangling references). See interactiveshell for details
666 main_mod = self.shell.new_main_mod(filename, name)
670 main_mod = self.shell.new_main_mod(filename, name)
667 prog_ns = main_mod.__dict__
671 prog_ns = main_mod.__dict__
668
672
669 # pickle fix. See interactiveshell for an explanation. But we need to
673 # pickle fix. See interactiveshell for an explanation. But we need to
670 # make sure that, if we overwrite __main__, we replace it at the end
674 # make sure that, if we overwrite __main__, we replace it at the end
671 main_mod_name = prog_ns['__name__']
675 main_mod_name = prog_ns['__name__']
672
676
673 if main_mod_name == '__main__':
677 if main_mod_name == '__main__':
674 restore_main = sys.modules['__main__']
678 restore_main = sys.modules['__main__']
675 else:
679 else:
676 restore_main = False
680 restore_main = False
677
681
678 # This needs to be undone at the end to prevent holding references to
682 # This needs to be undone at the end to prevent holding references to
679 # every single object ever created.
683 # every single object ever created.
680 sys.modules[main_mod_name] = main_mod
684 sys.modules[main_mod_name] = main_mod
681
685
682 if 'p' in opts or 'd' in opts:
686 if 'p' in opts or 'd' in opts:
683 if 'm' in opts:
687 if 'm' in opts:
684 code = 'run_module(modulename, prog_ns)'
688 code = 'run_module(modulename, prog_ns)'
685 code_ns = {
689 code_ns = {
686 'run_module': self.shell.safe_run_module,
690 'run_module': self.shell.safe_run_module,
687 'prog_ns': prog_ns,
691 'prog_ns': prog_ns,
688 'modulename': modulename,
692 'modulename': modulename,
689 }
693 }
690 else:
694 else:
691 if 'd' in opts:
695 if 'd' in opts:
692 # allow exceptions to raise in debug mode
696 # allow exceptions to raise in debug mode
693 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
697 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
694 else:
698 else:
695 code = 'execfile(filename, prog_ns)'
699 code = 'execfile(filename, prog_ns)'
696 code_ns = {
700 code_ns = {
697 'execfile': self.shell.safe_execfile,
701 'execfile': self.shell.safe_execfile,
698 'prog_ns': prog_ns,
702 'prog_ns': prog_ns,
699 'filename': get_py_filename(filename),
703 'filename': get_py_filename(filename),
700 }
704 }
701
705
702 try:
706 try:
703 stats = None
707 stats = None
704 with self.shell.readline_no_record:
708 with self.shell.readline_no_record:
705 if 'p' in opts:
709 if 'p' in opts:
706 stats = self._run_with_profiler(code, opts, code_ns)
710 stats = self._run_with_profiler(code, opts, code_ns)
707 else:
711 else:
708 if 'd' in opts:
712 if 'd' in opts:
709 bp_file, bp_line = parse_breakpoint(
713 bp_file, bp_line = parse_breakpoint(
710 opts.get('b', ['1'])[0], filename)
714 opts.get('b', ['1'])[0], filename)
711 self._run_with_debugger(
715 self._run_with_debugger(
712 code, code_ns, filename, bp_line, bp_file)
716 code, code_ns, filename, bp_line, bp_file)
713 else:
717 else:
714 if 'm' in opts:
718 if 'm' in opts:
715 def run():
719 def run():
716 self.shell.safe_run_module(modulename, prog_ns)
720 self.shell.safe_run_module(modulename, prog_ns)
717 else:
721 else:
718 if runner is None:
722 if runner is None:
719 runner = self.default_runner
723 runner = self.default_runner
720 if runner is None:
724 if runner is None:
721 runner = self.shell.safe_execfile
725 runner = self.shell.safe_execfile
722
726
723 def run():
727 def run():
724 runner(filename, prog_ns, prog_ns,
728 runner(filename, prog_ns, prog_ns,
725 exit_ignore=exit_ignore)
729 exit_ignore=exit_ignore)
726
730
727 if 't' in opts:
731 if 't' in opts:
728 # timed execution
732 # timed execution
729 try:
733 try:
730 nruns = int(opts['N'][0])
734 nruns = int(opts['N'][0])
731 if nruns < 1:
735 if nruns < 1:
732 error('Number of runs must be >=1')
736 error('Number of runs must be >=1')
733 return
737 return
734 except (KeyError):
738 except (KeyError):
735 nruns = 1
739 nruns = 1
736 self._run_with_timing(run, nruns)
740 self._run_with_timing(run, nruns)
737 else:
741 else:
738 # regular execution
742 # regular execution
739 run()
743 run()
740
744
741 if 'i' in opts:
745 if 'i' in opts:
742 self.shell.user_ns['__name__'] = __name__save
746 self.shell.user_ns['__name__'] = __name__save
743 else:
747 else:
744 # update IPython interactive namespace
748 # update IPython interactive namespace
745
749
746 # Some forms of read errors on the file may mean the
750 # Some forms of read errors on the file may mean the
747 # __name__ key was never set; using pop we don't have to
751 # __name__ key was never set; using pop we don't have to
748 # worry about a possible KeyError.
752 # worry about a possible KeyError.
749 prog_ns.pop('__name__', None)
753 prog_ns.pop('__name__', None)
750
754
751 with preserve_keys(self.shell.user_ns, '__file__'):
755 with preserve_keys(self.shell.user_ns, '__file__'):
752 self.shell.user_ns.update(prog_ns)
756 self.shell.user_ns.update(prog_ns)
753 finally:
757 finally:
754 # It's a bit of a mystery why, but __builtins__ can change from
758 # It's a bit of a mystery why, but __builtins__ can change from
755 # being a module to becoming a dict missing some key data after
759 # being a module to becoming a dict missing some key data after
756 # %run. As best I can see, this is NOT something IPython is doing
760 # %run. As best I can see, this is NOT something IPython is doing
757 # at all, and similar problems have been reported before:
761 # at all, and similar problems have been reported before:
758 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
762 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
759 # Since this seems to be done by the interpreter itself, the best
763 # Since this seems to be done by the interpreter itself, the best
760 # we can do is to at least restore __builtins__ for the user on
764 # we can do is to at least restore __builtins__ for the user on
761 # exit.
765 # exit.
762 self.shell.user_ns['__builtins__'] = builtin_mod
766 self.shell.user_ns['__builtins__'] = builtin_mod
763
767
764 # Ensure key global structures are restored
768 # Ensure key global structures are restored
765 sys.argv = save_argv
769 sys.argv = save_argv
766 if restore_main:
770 if restore_main:
767 sys.modules['__main__'] = restore_main
771 sys.modules['__main__'] = restore_main
768 else:
772 else:
769 # Remove from sys.modules the reference to main_mod we'd
773 # Remove from sys.modules the reference to main_mod we'd
770 # added. Otherwise it will trap references to objects
774 # added. Otherwise it will trap references to objects
771 # contained therein.
775 # contained therein.
772 del sys.modules[main_mod_name]
776 del sys.modules[main_mod_name]
773
777
774 return stats
778 return stats
775
779
776 def _run_with_debugger(self, code, code_ns, filename=None,
780 def _run_with_debugger(self, code, code_ns, filename=None,
777 bp_line=None, bp_file=None):
781 bp_line=None, bp_file=None):
778 """
782 """
779 Run `code` in debugger with a break point.
783 Run `code` in debugger with a break point.
780
784
781 Parameters
785 Parameters
782 ----------
786 ----------
783 code : str
787 code : str
784 Code to execute.
788 Code to execute.
785 code_ns : dict
789 code_ns : dict
786 A namespace in which `code` is executed.
790 A namespace in which `code` is executed.
787 filename : str
791 filename : str
788 `code` is ran as if it is in `filename`.
792 `code` is ran as if it is in `filename`.
789 bp_line : int, optional
793 bp_line : int, optional
790 Line number of the break point.
794 Line number of the break point.
791 bp_file : str, optional
795 bp_file : str, optional
792 Path to the file in which break point is specified.
796 Path to the file in which break point is specified.
793 `filename` is used if not given.
797 `filename` is used if not given.
794
798
795 Raises
799 Raises
796 ------
800 ------
797 UsageError
801 UsageError
798 If the break point given by `bp_line` is not valid.
802 If the break point given by `bp_line` is not valid.
799
803
800 """
804 """
801 deb = debugger.Pdb(self.shell.colors)
805 deb = debugger.Pdb(self.shell.colors)
802 # reset Breakpoint state, which is moronically kept
806 # reset Breakpoint state, which is moronically kept
803 # in a class
807 # in a class
804 bdb.Breakpoint.next = 1
808 bdb.Breakpoint.next = 1
805 bdb.Breakpoint.bplist = {}
809 bdb.Breakpoint.bplist = {}
806 bdb.Breakpoint.bpbynumber = [None]
810 bdb.Breakpoint.bpbynumber = [None]
807 if bp_line is not None:
811 if bp_line is not None:
808 # Set an initial breakpoint to stop execution
812 # Set an initial breakpoint to stop execution
809 maxtries = 10
813 maxtries = 10
810 bp_file = bp_file or filename
814 bp_file = bp_file or filename
811 checkline = deb.checkline(bp_file, bp_line)
815 checkline = deb.checkline(bp_file, bp_line)
812 if not checkline:
816 if not checkline:
813 for bp in range(bp_line + 1, bp_line + maxtries + 1):
817 for bp in range(bp_line + 1, bp_line + maxtries + 1):
814 if deb.checkline(bp_file, bp):
818 if deb.checkline(bp_file, bp):
815 break
819 break
816 else:
820 else:
817 msg = ("\nI failed to find a valid line to set "
821 msg = ("\nI failed to find a valid line to set "
818 "a breakpoint\n"
822 "a breakpoint\n"
819 "after trying up to line: %s.\n"
823 "after trying up to line: %s.\n"
820 "Please set a valid breakpoint manually "
824 "Please set a valid breakpoint manually "
821 "with the -b option." % bp)
825 "with the -b option." % bp)
822 raise UsageError(msg)
826 raise UsageError(msg)
823 # if we find a good linenumber, set the breakpoint
827 # if we find a good linenumber, set the breakpoint
824 deb.do_break('%s:%s' % (bp_file, bp_line))
828 deb.do_break('%s:%s' % (bp_file, bp_line))
825
829
826 if filename:
830 if filename:
827 # Mimic Pdb._runscript(...)
831 # Mimic Pdb._runscript(...)
828 deb._wait_for_mainpyfile = True
832 deb._wait_for_mainpyfile = True
829 deb.mainpyfile = deb.canonic(filename)
833 deb.mainpyfile = deb.canonic(filename)
830
834
831 # Start file run
835 # Start file run
832 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
836 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
833 try:
837 try:
834 if filename:
838 if filename:
835 # save filename so it can be used by methods on the deb object
839 # save filename so it can be used by methods on the deb object
836 deb._exec_filename = filename
840 deb._exec_filename = filename
837 while True:
841 while True:
838 try:
842 try:
839 deb.run(code, code_ns)
843 deb.run(code, code_ns)
840 except Restart:
844 except Restart:
841 print("Restarting")
845 print("Restarting")
842 if filename:
846 if filename:
843 deb._wait_for_mainpyfile = True
847 deb._wait_for_mainpyfile = True
844 deb.mainpyfile = deb.canonic(filename)
848 deb.mainpyfile = deb.canonic(filename)
845 continue
849 continue
846 else:
850 else:
847 break
851 break
848
852
849
853
850 except:
854 except:
851 etype, value, tb = sys.exc_info()
855 etype, value, tb = sys.exc_info()
852 # Skip three frames in the traceback: the %run one,
856 # Skip three frames in the traceback: the %run one,
853 # one inside bdb.py, and the command-line typed by the
857 # one inside bdb.py, and the command-line typed by the
854 # user (run by exec in pdb itself).
858 # user (run by exec in pdb itself).
855 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
859 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
856
860
857 @staticmethod
861 @staticmethod
858 def _run_with_timing(run, nruns):
862 def _run_with_timing(run, nruns):
859 """
863 """
860 Run function `run` and print timing information.
864 Run function `run` and print timing information.
861
865
862 Parameters
866 Parameters
863 ----------
867 ----------
864 run : callable
868 run : callable
865 Any callable object which takes no argument.
869 Any callable object which takes no argument.
866 nruns : int
870 nruns : int
867 Number of times to execute `run`.
871 Number of times to execute `run`.
868
872
869 """
873 """
870 twall0 = time.time()
874 twall0 = time.time()
871 if nruns == 1:
875 if nruns == 1:
872 t0 = clock2()
876 t0 = clock2()
873 run()
877 run()
874 t1 = clock2()
878 t1 = clock2()
875 t_usr = t1[0] - t0[0]
879 t_usr = t1[0] - t0[0]
876 t_sys = t1[1] - t0[1]
880 t_sys = t1[1] - t0[1]
877 print("\nIPython CPU timings (estimated):")
881 print("\nIPython CPU timings (estimated):")
878 print(" User : %10.2f s." % t_usr)
882 print(" User : %10.2f s." % t_usr)
879 print(" System : %10.2f s." % t_sys)
883 print(" System : %10.2f s." % t_sys)
880 else:
884 else:
881 runs = range(nruns)
885 runs = range(nruns)
882 t0 = clock2()
886 t0 = clock2()
883 for nr in runs:
887 for nr in runs:
884 run()
888 run()
885 t1 = clock2()
889 t1 = clock2()
886 t_usr = t1[0] - t0[0]
890 t_usr = t1[0] - t0[0]
887 t_sys = t1[1] - t0[1]
891 t_sys = t1[1] - t0[1]
888 print("\nIPython CPU timings (estimated):")
892 print("\nIPython CPU timings (estimated):")
889 print("Total runs performed:", nruns)
893 print("Total runs performed:", nruns)
890 print(" Times : %10s %10s" % ('Total', 'Per run'))
894 print(" Times : %10s %10s" % ('Total', 'Per run'))
891 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
895 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
892 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
896 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
893 twall1 = time.time()
897 twall1 = time.time()
894 print("Wall time: %10.2f s." % (twall1 - twall0))
898 print("Wall time: %10.2f s." % (twall1 - twall0))
895
899
896 @skip_doctest
900 @skip_doctest
897 @line_cell_magic
901 @line_cell_magic
898 def timeit(self, line='', cell=None):
902 def timeit(self, line='', cell=None):
899 """Time execution of a Python statement or expression
903 """Time execution of a Python statement or expression
900
904
901 Usage, in line mode:
905 Usage, in line mode:
902 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
906 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
903 or in cell mode:
907 or in cell mode:
904 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
908 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
905 code
909 code
906 code...
910 code...
907
911
908 Time execution of a Python statement or expression using the timeit
912 Time execution of a Python statement or expression using the timeit
909 module. This function can be used both as a line and cell magic:
913 module. This function can be used both as a line and cell magic:
910
914
911 - In line mode you can time a single-line statement (though multiple
915 - In line mode you can time a single-line statement (though multiple
912 ones can be chained with using semicolons).
916 ones can be chained with using semicolons).
913
917
914 - In cell mode, the statement in the first line is used as setup code
918 - In cell mode, the statement in the first line is used as setup code
915 (executed but not timed) and the body of the cell is timed. The cell
919 (executed but not timed) and the body of the cell is timed. The cell
916 body has access to any variables created in the setup code.
920 body has access to any variables created in the setup code.
917
921
918 Options:
922 Options:
919 -n<N>: execute the given statement <N> times in a loop. If this value
923 -n<N>: execute the given statement <N> times in a loop. If this value
920 is not given, a fitting value is chosen.
924 is not given, a fitting value is chosen.
921
925
922 -r<R>: repeat the loop iteration <R> times and take the best result.
926 -r<R>: repeat the loop iteration <R> times and take the best result.
923 Default: 3
927 Default: 3
924
928
925 -t: use time.time to measure the time, which is the default on Unix.
929 -t: use time.time to measure the time, which is the default on Unix.
926 This function measures wall time.
930 This function measures wall time.
927
931
928 -c: use time.clock to measure the time, which is the default on
932 -c: use time.clock to measure the time, which is the default on
929 Windows and measures wall time. On Unix, resource.getrusage is used
933 Windows and measures wall time. On Unix, resource.getrusage is used
930 instead and returns the CPU user time.
934 instead and returns the CPU user time.
931
935
932 -p<P>: use a precision of <P> digits to display the timing result.
936 -p<P>: use a precision of <P> digits to display the timing result.
933 Default: 3
937 Default: 3
934
938
935 -q: Quiet, do not print result.
939 -q: Quiet, do not print result.
936
940
937 -o: return a TimeitResult that can be stored in a variable to inspect
941 -o: return a TimeitResult that can be stored in a variable to inspect
938 the result in more details.
942 the result in more details.
939
943
940
944
941 Examples
945 Examples
942 --------
946 --------
943 ::
947 ::
944
948
945 In [1]: %timeit pass
949 In [1]: %timeit pass
946 10000000 loops, best of 3: 53.3 ns per loop
950 10000000 loops, best of 3: 53.3 ns per loop
947
951
948 In [2]: u = None
952 In [2]: u = None
949
953
950 In [3]: %timeit u is None
954 In [3]: %timeit u is None
951 10000000 loops, best of 3: 184 ns per loop
955 10000000 loops, best of 3: 184 ns per loop
952
956
953 In [4]: %timeit -r 4 u == None
957 In [4]: %timeit -r 4 u == None
954 1000000 loops, best of 4: 242 ns per loop
958 1000000 loops, best of 4: 242 ns per loop
955
959
956 In [5]: import time
960 In [5]: import time
957
961
958 In [6]: %timeit -n1 time.sleep(2)
962 In [6]: %timeit -n1 time.sleep(2)
959 1 loops, best of 3: 2 s per loop
963 1 loop, best of 3: 2 s per loop
960
964
961
965
962 The times reported by %timeit will be slightly higher than those
966 The times reported by %timeit will be slightly higher than those
963 reported by the timeit.py script when variables are accessed. This is
967 reported by the timeit.py script when variables are accessed. This is
964 due to the fact that %timeit executes the statement in the namespace
968 due to the fact that %timeit executes the statement in the namespace
965 of the shell, compared with timeit.py, which uses a single setup
969 of the shell, compared with timeit.py, which uses a single setup
966 statement to import function or create variables. Generally, the bias
970 statement to import function or create variables. Generally, the bias
967 does not matter as long as results from timeit.py are not mixed with
971 does not matter as long as results from timeit.py are not mixed with
968 those from %timeit."""
972 those from %timeit."""
969
973
970 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
974 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
971 posix=False, strict=False)
975 posix=False, strict=False)
972 if stmt == "" and cell is None:
976 if stmt == "" and cell is None:
973 return
977 return
974
978
975 timefunc = timeit.default_timer
979 timefunc = timeit.default_timer
976 number = int(getattr(opts, "n", 0))
980 number = int(getattr(opts, "n", 0))
977 repeat = int(getattr(opts, "r", timeit.default_repeat))
981 repeat = int(getattr(opts, "r", timeit.default_repeat))
978 precision = int(getattr(opts, "p", 3))
982 precision = int(getattr(opts, "p", 3))
979 quiet = 'q' in opts
983 quiet = 'q' in opts
980 return_result = 'o' in opts
984 return_result = 'o' in opts
981 if hasattr(opts, "t"):
985 if hasattr(opts, "t"):
982 timefunc = time.time
986 timefunc = time.time
983 if hasattr(opts, "c"):
987 if hasattr(opts, "c"):
984 timefunc = clock
988 timefunc = clock
985
989
986 timer = Timer(timer=timefunc)
990 timer = Timer(timer=timefunc)
987 # this code has tight coupling to the inner workings of timeit.Timer,
991 # this code has tight coupling to the inner workings of timeit.Timer,
988 # but is there a better way to achieve that the code stmt has access
992 # but is there a better way to achieve that the code stmt has access
989 # to the shell namespace?
993 # to the shell namespace?
990 transform = self.shell.input_splitter.transform_cell
994 transform = self.shell.input_splitter.transform_cell
991
995
992 if cell is None:
996 if cell is None:
993 # called as line magic
997 # called as line magic
994 ast_setup = self.shell.compile.ast_parse("pass")
998 ast_setup = self.shell.compile.ast_parse("pass")
995 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
999 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
996 else:
1000 else:
997 ast_setup = self.shell.compile.ast_parse(transform(stmt))
1001 ast_setup = self.shell.compile.ast_parse(transform(stmt))
998 ast_stmt = self.shell.compile.ast_parse(transform(cell))
1002 ast_stmt = self.shell.compile.ast_parse(transform(cell))
999
1003
1000 ast_setup = self.shell.transform_ast(ast_setup)
1004 ast_setup = self.shell.transform_ast(ast_setup)
1001 ast_stmt = self.shell.transform_ast(ast_stmt)
1005 ast_stmt = self.shell.transform_ast(ast_stmt)
1002
1006
1003 # This codestring is taken from timeit.template - we fill it in as an
1007 # This codestring is taken from timeit.template - we fill it in as an
1004 # AST, so that we can apply our AST transformations to the user code
1008 # AST, so that we can apply our AST transformations to the user code
1005 # without affecting the timing code.
1009 # without affecting the timing code.
1006 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1010 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1007 ' setup\n'
1011 ' setup\n'
1008 ' _t0 = _timer()\n'
1012 ' _t0 = _timer()\n'
1009 ' for _i in _it:\n'
1013 ' for _i in _it:\n'
1010 ' stmt\n'
1014 ' stmt\n'
1011 ' _t1 = _timer()\n'
1015 ' _t1 = _timer()\n'
1012 ' return _t1 - _t0\n')
1016 ' return _t1 - _t0\n')
1013
1017
1014 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1018 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1015 timeit_ast = ast.fix_missing_locations(timeit_ast)
1019 timeit_ast = ast.fix_missing_locations(timeit_ast)
1016
1020
1017 # Track compilation time so it can be reported if too long
1021 # Track compilation time so it can be reported if too long
1018 # Minimum time above which compilation time will be reported
1022 # Minimum time above which compilation time will be reported
1019 tc_min = 0.1
1023 tc_min = 0.1
1020
1024
1021 t0 = clock()
1025 t0 = clock()
1022 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1026 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1023 tc = clock()-t0
1027 tc = clock()-t0
1024
1028
1025 ns = {}
1029 ns = {}
1026 exec(code, self.shell.user_ns, ns)
1030 exec(code, self.shell.user_ns, ns)
1027 timer.inner = ns["inner"]
1031 timer.inner = ns["inner"]
1028
1032
1029 # This is used to check if there is a huge difference between the
1033 # This is used to check if there is a huge difference between the
1030 # best and worst timings.
1034 # best and worst timings.
1031 # Issue: https://github.com/ipython/ipython/issues/6471
1035 # Issue: https://github.com/ipython/ipython/issues/6471
1032 worst_tuning = 0
1036 worst_tuning = 0
1033 if number == 0:
1037 if number == 0:
1034 # determine number so that 0.2 <= total time < 2.0
1038 # determine number so that 0.2 <= total time < 2.0
1035 number = 1
1039 number = 1
1036 for _ in range(1, 10):
1040 for _ in range(1, 10):
1037 time_number = timer.timeit(number)
1041 time_number = timer.timeit(number)
1038 worst_tuning = max(worst_tuning, time_number / number)
1042 worst_tuning = max(worst_tuning, time_number / number)
1039 if time_number >= 0.2:
1043 if time_number >= 0.2:
1040 break
1044 break
1041 number *= 10
1045 number *= 10
1042 all_runs = timer.repeat(repeat, number)
1046 all_runs = timer.repeat(repeat, number)
1043 best = min(all_runs) / number
1047 best = min(all_runs) / number
1044
1048
1045 worst = max(all_runs) / number
1049 worst = max(all_runs) / number
1046 if worst_tuning:
1050 if worst_tuning:
1047 worst = max(worst, worst_tuning)
1051 worst = max(worst, worst_tuning)
1048
1052
1049 if not quiet :
1053 if not quiet :
1050 # Check best timing is greater than zero to avoid a
1054 # Check best timing is greater than zero to avoid a
1051 # ZeroDivisionError.
1055 # ZeroDivisionError.
1052 # In cases where the slowest timing is lesser than a micosecond
1056 # In cases where the slowest timing is lesser than a micosecond
1053 # we assume that it does not really matter if the fastest
1057 # we assume that it does not really matter if the fastest
1054 # timing is 4 times faster than the slowest timing or not.
1058 # timing is 4 times faster than the slowest timing or not.
1055 if worst > 4 * best and best > 0 and worst > 1e-6:
1059 if worst > 4 * best and best > 0 and worst > 1e-6:
1056 print("The slowest run took %0.2f times longer than the "
1060 print("The slowest run took %0.2f times longer than the "
1057 "fastest. This could mean that an intermediate result "
1061 "fastest. This could mean that an intermediate result "
1058 "is being cached " % (worst / best))
1062 "is being cached." % (worst / best))
1063 if number == 1: # No s at "loops" if only one loop
1064 print(u"%d loop, best of %d: %s per loop" % (number, repeat,
1065 _format_time(best, precision)))
1066 else:
1059 print(u"%d loops, best of %d: %s per loop" % (number, repeat,
1067 print(u"%d loops, best of %d: %s per loop" % (number, repeat,
1060 _format_time(best, precision)))
1068 _format_time(best, precision)))
1061 if tc > tc_min:
1069 if tc > tc_min:
1062 print("Compiler time: %.2f s" % tc)
1070 print("Compiler time: %.2f s" % tc)
1063 if return_result:
1071 if return_result:
1064 return TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1072 return TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1065
1073
1066 @skip_doctest
1074 @skip_doctest
1067 @needs_local_scope
1075 @needs_local_scope
1068 @line_cell_magic
1076 @line_cell_magic
1069 def time(self,line='', cell=None, local_ns=None):
1077 def time(self,line='', cell=None, local_ns=None):
1070 """Time execution of a Python statement or expression.
1078 """Time execution of a Python statement or expression.
1071
1079
1072 The CPU and wall clock times are printed, and the value of the
1080 The CPU and wall clock times are printed, and the value of the
1073 expression (if any) is returned. Note that under Win32, system time
1081 expression (if any) is returned. Note that under Win32, system time
1074 is always reported as 0, since it can not be measured.
1082 is always reported as 0, since it can not be measured.
1075
1083
1076 This function can be used both as a line and cell magic:
1084 This function can be used both as a line and cell magic:
1077
1085
1078 - In line mode you can time a single-line statement (though multiple
1086 - In line mode you can time a single-line statement (though multiple
1079 ones can be chained with using semicolons).
1087 ones can be chained with using semicolons).
1080
1088
1081 - In cell mode, you can time the cell body (a directly
1089 - In cell mode, you can time the cell body (a directly
1082 following statement raises an error).
1090 following statement raises an error).
1083
1091
1084 This function provides very basic timing functionality. Use the timeit
1092 This function provides very basic timing functionality. Use the timeit
1085 magic for more control over the measurement.
1093 magic for more control over the measurement.
1086
1094
1087 Examples
1095 Examples
1088 --------
1096 --------
1089 ::
1097 ::
1090
1098
1091 In [1]: %time 2**128
1099 In [1]: %time 2**128
1092 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1100 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1093 Wall time: 0.00
1101 Wall time: 0.00
1094 Out[1]: 340282366920938463463374607431768211456L
1102 Out[1]: 340282366920938463463374607431768211456L
1095
1103
1096 In [2]: n = 1000000
1104 In [2]: n = 1000000
1097
1105
1098 In [3]: %time sum(range(n))
1106 In [3]: %time sum(range(n))
1099 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1107 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1100 Wall time: 1.37
1108 Wall time: 1.37
1101 Out[3]: 499999500000L
1109 Out[3]: 499999500000L
1102
1110
1103 In [4]: %time print 'hello world'
1111 In [4]: %time print 'hello world'
1104 hello world
1112 hello world
1105 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1113 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1106 Wall time: 0.00
1114 Wall time: 0.00
1107
1115
1108 Note that the time needed by Python to compile the given expression
1116 Note that the time needed by Python to compile the given expression
1109 will be reported if it is more than 0.1s. In this example, the
1117 will be reported if it is more than 0.1s. In this example, the
1110 actual exponentiation is done by Python at compilation time, so while
1118 actual exponentiation is done by Python at compilation time, so while
1111 the expression can take a noticeable amount of time to compute, that
1119 the expression can take a noticeable amount of time to compute, that
1112 time is purely due to the compilation:
1120 time is purely due to the compilation:
1113
1121
1114 In [5]: %time 3**9999;
1122 In [5]: %time 3**9999;
1115 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1123 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1116 Wall time: 0.00 s
1124 Wall time: 0.00 s
1117
1125
1118 In [6]: %time 3**999999;
1126 In [6]: %time 3**999999;
1119 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1127 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1120 Wall time: 0.00 s
1128 Wall time: 0.00 s
1121 Compiler : 0.78 s
1129 Compiler : 0.78 s
1122 """
1130 """
1123
1131
1124 # fail immediately if the given expression can't be compiled
1132 # fail immediately if the given expression can't be compiled
1125
1133
1126 if line and cell:
1134 if line and cell:
1127 raise UsageError("Can't use statement directly after '%%time'!")
1135 raise UsageError("Can't use statement directly after '%%time'!")
1128
1136
1129 if cell:
1137 if cell:
1130 expr = self.shell.input_transformer_manager.transform_cell(cell)
1138 expr = self.shell.input_transformer_manager.transform_cell(cell)
1131 else:
1139 else:
1132 expr = self.shell.input_transformer_manager.transform_cell(line)
1140 expr = self.shell.input_transformer_manager.transform_cell(line)
1133
1141
1134 # Minimum time above which parse time will be reported
1142 # Minimum time above which parse time will be reported
1135 tp_min = 0.1
1143 tp_min = 0.1
1136
1144
1137 t0 = clock()
1145 t0 = clock()
1138 expr_ast = self.shell.compile.ast_parse(expr)
1146 expr_ast = self.shell.compile.ast_parse(expr)
1139 tp = clock()-t0
1147 tp = clock()-t0
1140
1148
1141 # Apply AST transformations
1149 # Apply AST transformations
1142 expr_ast = self.shell.transform_ast(expr_ast)
1150 expr_ast = self.shell.transform_ast(expr_ast)
1143
1151
1144 # Minimum time above which compilation time will be reported
1152 # Minimum time above which compilation time will be reported
1145 tc_min = 0.1
1153 tc_min = 0.1
1146
1154
1147 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1155 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1148 mode = 'eval'
1156 mode = 'eval'
1149 source = '<timed eval>'
1157 source = '<timed eval>'
1150 expr_ast = ast.Expression(expr_ast.body[0].value)
1158 expr_ast = ast.Expression(expr_ast.body[0].value)
1151 else:
1159 else:
1152 mode = 'exec'
1160 mode = 'exec'
1153 source = '<timed exec>'
1161 source = '<timed exec>'
1154 t0 = clock()
1162 t0 = clock()
1155 code = self.shell.compile(expr_ast, source, mode)
1163 code = self.shell.compile(expr_ast, source, mode)
1156 tc = clock()-t0
1164 tc = clock()-t0
1157
1165
1158 # skew measurement as little as possible
1166 # skew measurement as little as possible
1159 glob = self.shell.user_ns
1167 glob = self.shell.user_ns
1160 wtime = time.time
1168 wtime = time.time
1161 # time execution
1169 # time execution
1162 wall_st = wtime()
1170 wall_st = wtime()
1163 if mode=='eval':
1171 if mode=='eval':
1164 st = clock2()
1172 st = clock2()
1165 out = eval(code, glob, local_ns)
1173 out = eval(code, glob, local_ns)
1166 end = clock2()
1174 end = clock2()
1167 else:
1175 else:
1168 st = clock2()
1176 st = clock2()
1169 exec(code, glob, local_ns)
1177 exec(code, glob, local_ns)
1170 end = clock2()
1178 end = clock2()
1171 out = None
1179 out = None
1172 wall_end = wtime()
1180 wall_end = wtime()
1173 # Compute actual times and report
1181 # Compute actual times and report
1174 wall_time = wall_end-wall_st
1182 wall_time = wall_end-wall_st
1175 cpu_user = end[0]-st[0]
1183 cpu_user = end[0]-st[0]
1176 cpu_sys = end[1]-st[1]
1184 cpu_sys = end[1]-st[1]
1177 cpu_tot = cpu_user+cpu_sys
1185 cpu_tot = cpu_user+cpu_sys
1178 # On windows cpu_sys is always zero, so no new information to the next print
1186 # On windows cpu_sys is always zero, so no new information to the next print
1179 if sys.platform != 'win32':
1187 if sys.platform != 'win32':
1180 print("CPU times: user %s, sys: %s, total: %s" % \
1188 print("CPU times: user %s, sys: %s, total: %s" % \
1181 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1189 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1182 print("Wall time: %s" % _format_time(wall_time))
1190 print("Wall time: %s" % _format_time(wall_time))
1183 if tc > tc_min:
1191 if tc > tc_min:
1184 print("Compiler : %s" % _format_time(tc))
1192 print("Compiler : %s" % _format_time(tc))
1185 if tp > tp_min:
1193 if tp > tp_min:
1186 print("Parser : %s" % _format_time(tp))
1194 print("Parser : %s" % _format_time(tp))
1187 return out
1195 return out
1188
1196
1189 @skip_doctest
1197 @skip_doctest
1190 @line_magic
1198 @line_magic
1191 def macro(self, parameter_s=''):
1199 def macro(self, parameter_s=''):
1192 """Define a macro for future re-execution. It accepts ranges of history,
1200 """Define a macro for future re-execution. It accepts ranges of history,
1193 filenames or string objects.
1201 filenames or string objects.
1194
1202
1195 Usage:\\
1203 Usage:\\
1196 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1204 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1197
1205
1198 Options:
1206 Options:
1199
1207
1200 -r: use 'raw' input. By default, the 'processed' history is used,
1208 -r: use 'raw' input. By default, the 'processed' history is used,
1201 so that magics are loaded in their transformed version to valid
1209 so that magics are loaded in their transformed version to valid
1202 Python. If this option is given, the raw input as typed at the
1210 Python. If this option is given, the raw input as typed at the
1203 command line is used instead.
1211 command line is used instead.
1204
1212
1205 -q: quiet macro definition. By default, a tag line is printed
1213 -q: quiet macro definition. By default, a tag line is printed
1206 to indicate the macro has been created, and then the contents of
1214 to indicate the macro has been created, and then the contents of
1207 the macro are printed. If this option is given, then no printout
1215 the macro are printed. If this option is given, then no printout
1208 is produced once the macro is created.
1216 is produced once the macro is created.
1209
1217
1210 This will define a global variable called `name` which is a string
1218 This will define a global variable called `name` which is a string
1211 made of joining the slices and lines you specify (n1,n2,... numbers
1219 made of joining the slices and lines you specify (n1,n2,... numbers
1212 above) from your input history into a single string. This variable
1220 above) from your input history into a single string. This variable
1213 acts like an automatic function which re-executes those lines as if
1221 acts like an automatic function which re-executes those lines as if
1214 you had typed them. You just type 'name' at the prompt and the code
1222 you had typed them. You just type 'name' at the prompt and the code
1215 executes.
1223 executes.
1216
1224
1217 The syntax for indicating input ranges is described in %history.
1225 The syntax for indicating input ranges is described in %history.
1218
1226
1219 Note: as a 'hidden' feature, you can also use traditional python slice
1227 Note: as a 'hidden' feature, you can also use traditional python slice
1220 notation, where N:M means numbers N through M-1.
1228 notation, where N:M means numbers N through M-1.
1221
1229
1222 For example, if your history contains (print using %hist -n )::
1230 For example, if your history contains (print using %hist -n )::
1223
1231
1224 44: x=1
1232 44: x=1
1225 45: y=3
1233 45: y=3
1226 46: z=x+y
1234 46: z=x+y
1227 47: print x
1235 47: print x
1228 48: a=5
1236 48: a=5
1229 49: print 'x',x,'y',y
1237 49: print 'x',x,'y',y
1230
1238
1231 you can create a macro with lines 44 through 47 (included) and line 49
1239 you can create a macro with lines 44 through 47 (included) and line 49
1232 called my_macro with::
1240 called my_macro with::
1233
1241
1234 In [55]: %macro my_macro 44-47 49
1242 In [55]: %macro my_macro 44-47 49
1235
1243
1236 Now, typing `my_macro` (without quotes) will re-execute all this code
1244 Now, typing `my_macro` (without quotes) will re-execute all this code
1237 in one pass.
1245 in one pass.
1238
1246
1239 You don't need to give the line-numbers in order, and any given line
1247 You don't need to give the line-numbers in order, and any given line
1240 number can appear multiple times. You can assemble macros with any
1248 number can appear multiple times. You can assemble macros with any
1241 lines from your input history in any order.
1249 lines from your input history in any order.
1242
1250
1243 The macro is a simple object which holds its value in an attribute,
1251 The macro is a simple object which holds its value in an attribute,
1244 but IPython's display system checks for macros and executes them as
1252 but IPython's display system checks for macros and executes them as
1245 code instead of printing them when you type their name.
1253 code instead of printing them when you type their name.
1246
1254
1247 You can view a macro's contents by explicitly printing it with::
1255 You can view a macro's contents by explicitly printing it with::
1248
1256
1249 print macro_name
1257 print macro_name
1250
1258
1251 """
1259 """
1252 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1260 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1253 if not args: # List existing macros
1261 if not args: # List existing macros
1254 return sorted(k for k,v in iteritems(self.shell.user_ns) if\
1262 return sorted(k for k,v in iteritems(self.shell.user_ns) if\
1255 isinstance(v, Macro))
1263 isinstance(v, Macro))
1256 if len(args) == 1:
1264 if len(args) == 1:
1257 raise UsageError(
1265 raise UsageError(
1258 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1266 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1259 name, codefrom = args[0], " ".join(args[1:])
1267 name, codefrom = args[0], " ".join(args[1:])
1260
1268
1261 #print 'rng',ranges # dbg
1269 #print 'rng',ranges # dbg
1262 try:
1270 try:
1263 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1271 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1264 except (ValueError, TypeError) as e:
1272 except (ValueError, TypeError) as e:
1265 print(e.args[0])
1273 print(e.args[0])
1266 return
1274 return
1267 macro = Macro(lines)
1275 macro = Macro(lines)
1268 self.shell.define_macro(name, macro)
1276 self.shell.define_macro(name, macro)
1269 if not ( 'q' in opts) :
1277 if not ( 'q' in opts) :
1270 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1278 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1271 print('=== Macro contents: ===')
1279 print('=== Macro contents: ===')
1272 print(macro, end=' ')
1280 print(macro, end=' ')
1273
1281
1274 @magic_arguments.magic_arguments()
1282 @magic_arguments.magic_arguments()
1275 @magic_arguments.argument('output', type=str, default='', nargs='?',
1283 @magic_arguments.argument('output', type=str, default='', nargs='?',
1276 help="""The name of the variable in which to store output.
1284 help="""The name of the variable in which to store output.
1277 This is a utils.io.CapturedIO object with stdout/err attributes
1285 This is a utils.io.CapturedIO object with stdout/err attributes
1278 for the text of the captured output.
1286 for the text of the captured output.
1279
1287
1280 CapturedOutput also has a show() method for displaying the output,
1288 CapturedOutput also has a show() method for displaying the output,
1281 and __call__ as well, so you can use that to quickly display the
1289 and __call__ as well, so you can use that to quickly display the
1282 output.
1290 output.
1283
1291
1284 If unspecified, captured output is discarded.
1292 If unspecified, captured output is discarded.
1285 """
1293 """
1286 )
1294 )
1287 @magic_arguments.argument('--no-stderr', action="store_true",
1295 @magic_arguments.argument('--no-stderr', action="store_true",
1288 help="""Don't capture stderr."""
1296 help="""Don't capture stderr."""
1289 )
1297 )
1290 @magic_arguments.argument('--no-stdout', action="store_true",
1298 @magic_arguments.argument('--no-stdout', action="store_true",
1291 help="""Don't capture stdout."""
1299 help="""Don't capture stdout."""
1292 )
1300 )
1293 @magic_arguments.argument('--no-display', action="store_true",
1301 @magic_arguments.argument('--no-display', action="store_true",
1294 help="""Don't capture IPython's rich display."""
1302 help="""Don't capture IPython's rich display."""
1295 )
1303 )
1296 @cell_magic
1304 @cell_magic
1297 def capture(self, line, cell):
1305 def capture(self, line, cell):
1298 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1306 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1299 args = magic_arguments.parse_argstring(self.capture, line)
1307 args = magic_arguments.parse_argstring(self.capture, line)
1300 out = not args.no_stdout
1308 out = not args.no_stdout
1301 err = not args.no_stderr
1309 err = not args.no_stderr
1302 disp = not args.no_display
1310 disp = not args.no_display
1303 with capture_output(out, err, disp) as io:
1311 with capture_output(out, err, disp) as io:
1304 self.shell.run_cell(cell)
1312 self.shell.run_cell(cell)
1305 if args.output:
1313 if args.output:
1306 self.shell.user_ns[args.output] = io
1314 self.shell.user_ns[args.output] = io
1307
1315
1308 def parse_breakpoint(text, current_file):
1316 def parse_breakpoint(text, current_file):
1309 '''Returns (file, line) for file:line and (current_file, line) for line'''
1317 '''Returns (file, line) for file:line and (current_file, line) for line'''
1310 colon = text.find(':')
1318 colon = text.find(':')
1311 if colon == -1:
1319 if colon == -1:
1312 return current_file, int(text)
1320 return current_file, int(text)
1313 else:
1321 else:
1314 return text[:colon], int(text[colon+1:])
1322 return text[:colon], int(text[colon+1:])
1315
1323
1316 def _format_time(timespan, precision=3):
1324 def _format_time(timespan, precision=3):
1317 """Formats the timespan in a human readable form"""
1325 """Formats the timespan in a human readable form"""
1318 import math
1326 import math
1319
1327
1320 if timespan >= 60.0:
1328 if timespan >= 60.0:
1321 # we have more than a minute, format that in a human readable form
1329 # we have more than a minute, format that in a human readable form
1322 # Idea from http://snipplr.com/view/5713/
1330 # Idea from http://snipplr.com/view/5713/
1323 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1331 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1324 time = []
1332 time = []
1325 leftover = timespan
1333 leftover = timespan
1326 for suffix, length in parts:
1334 for suffix, length in parts:
1327 value = int(leftover / length)
1335 value = int(leftover / length)
1328 if value > 0:
1336 if value > 0:
1329 leftover = leftover % length
1337 leftover = leftover % length
1330 time.append(u'%s%s' % (str(value), suffix))
1338 time.append(u'%s%s' % (str(value), suffix))
1331 if leftover < 1:
1339 if leftover < 1:
1332 break
1340 break
1333 return " ".join(time)
1341 return " ".join(time)
1334
1342
1335
1343
1336 # Unfortunately the unicode 'micro' symbol can cause problems in
1344 # Unfortunately the unicode 'micro' symbol can cause problems in
1337 # certain terminals.
1345 # certain terminals.
1338 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1346 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1339 # Try to prevent crashes by being more secure than it needs to
1347 # Try to prevent crashes by being more secure than it needs to
1340 # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set.
1348 # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set.
1341 units = [u"s", u"ms",u'us',"ns"] # the save value
1349 units = [u"s", u"ms",u'us',"ns"] # the save value
1342 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1350 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1343 try:
1351 try:
1344 u'\xb5'.encode(sys.stdout.encoding)
1352 u'\xb5'.encode(sys.stdout.encoding)
1345 units = [u"s", u"ms",u'\xb5s',"ns"]
1353 units = [u"s", u"ms",u'\xb5s',"ns"]
1346 except:
1354 except:
1347 pass
1355 pass
1348 scaling = [1, 1e3, 1e6, 1e9]
1356 scaling = [1, 1e3, 1e6, 1e9]
1349
1357
1350 if timespan > 0.0:
1358 if timespan > 0.0:
1351 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1359 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1352 else:
1360 else:
1353 order = 3
1361 order = 3
1354 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
1362 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
General Comments 0
You need to be logged in to leave comments. Login now