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