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