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