##// END OF EJS Templates
Implement %%prun as a cell magic too.
Fernando Perez -
Show More
@@ -70,15 +70,25 b' python packages because of its non-free license. To use profiling, install the'
70 python-profiler package from non-free.""")
70 python-profiler package from non-free.""")
71
71
72 @skip_doctest
72 @skip_doctest
73 @line_magic
73 @line_cell_magic
74 def prun(self, parameter_s='',user_mode=1,
74 def prun(self, parameter_s='', cell=None, user_mode=True,
75 opts=None,arg_lst=None,prog_ns=None):
75 opts=None,arg_lst=None,prog_ns=None):
76
76
77 """Run a statement through the python code profiler.
77 """Run a statement through the python code profiler.
78
78
79 Usage:
79 Usage, in line mode:
80 %prun [options] statement
80 %prun [options] statement
81
81
82 Usage, in cell mode:
83 %%prun [options] [statement]
84 code...
85 code...
86
87 In cell mode, the additional code lines are appended to the (possibly
88 empty) statement in the first line. Cell mode allows you to easily
89 profile multiline blocks without having to put them in a separate
90 function.
91
82 The given statement (which doesn't require quote marks) is run via the
92 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.
93 python profiler in a manner similar to the profile.run() function.
84 Namespaces are internally managed to work correctly; profile.run
94 Namespaces are internally managed to work correctly; profile.run
@@ -167,8 +177,10 b' python-profiler package from non-free.""")'
167
177
168 if user_mode: # regular user call
178 if user_mode: # regular user call
169 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
179 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
170 list_all=1, posix=False)
180 list_all=True, posix=False)
171 namespace = self.shell.user_ns
181 namespace = self.shell.user_ns
182 if cell is not None:
183 arg_str += '\n' + cell
172 else: # called to run a program by %run -p
184 else: # called to run a program by %run -p
173 try:
185 try:
174 filename = get_py_filename(arg_lst[0])
186 filename = get_py_filename(arg_lst[0])
@@ -516,7 +528,7 b' python-profiler package from non-free.""")'
516 stats = None
528 stats = None
517 with self.shell.readline_no_record:
529 with self.shell.readline_no_record:
518 if 'p' in opts:
530 if 'p' in opts:
519 stats = self.prun('', 0, opts, arg_lst, prog_ns)
531 stats = self.prun('', None, False, opts, arg_lst, prog_ns)
520 else:
532 else:
521 if 'd' in opts:
533 if 'd' in opts:
522 deb = debugger.Pdb(self.shell.colors)
534 deb = debugger.Pdb(self.shell.colors)
General Comments 0
You need to be logged in to leave comments. Login now