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