##// END OF EJS Templates
Refactor ExecutionMagics._run_with_profiler
Takafumi Arakaki -
Show More
@@ -177,41 +177,15 b' python-profiler package from non-free.""")'
177
177
178 In [1]: import profile; profile.help()
178 In [1]: import profile; profile.help()
179 """
179 """
180 return self._run_with_profiler(parameter_s, cell)
180 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
181
181 list_all=True, posix=False)
182 def _run_with_profiler(
182 if cell is not None:
183 self, parameter_s='', cell=None, user_mode=True,
183 arg_str += '\n' + cell
184 opts=None, arg_lst=None, prog_ns=None, namespace=None):
184 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
185
186 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
187
188 if user_mode: # regular user call
189 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
190 list_all=True, posix=False)
191 namespace = self.shell.user_ns
192 if cell is not None:
193 arg_str += '\n' + cell
194 elif namespace is not None: # called to run a program by %run -p
195 arg_str = cell
196 else:
197 try:
198 filename = get_py_filename(arg_lst[0])
199 except IOError as e:
200 try:
201 msg = str(e)
202 except UnicodeError:
203 msg = e.message
204 error(msg)
205 return
206
185
207 arg_str = 'execfile(filename,prog_ns)'
186 def _run_with_profiler(self, arg_str, opts, namespace):
208 namespace = {
209 'execfile': self.shell.safe_execfile,
210 'prog_ns': prog_ns,
211 'filename': filename
212 }
213
187
214 opts.merge(opts_def)
188 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
215
189
216 prof = profile.Profile()
190 prof = profile.Profile()
217 try:
191 try:
@@ -567,9 +541,7 b' python-profiler package from non-free.""")'
567 stats = None
541 stats = None
568 with self.shell.readline_no_record:
542 with self.shell.readline_no_record:
569 if 'p' in opts:
543 if 'p' in opts:
570 stats = self._run_with_profiler(
544 stats = self._run_with_profiler(code, opts, code_ns)
571 cell=code, user_mode=False, opts=opts,
572 namespace=code_ns)
573 else:
545 else:
574 if 'd' in opts:
546 if 'd' in opts:
575 self._run_with_debugger(
547 self._run_with_debugger(
General Comments 0
You need to be logged in to leave comments. Login now