MCPcopy Create free account
hub / github.com/ipython/ipython / _run_with_profiler

Method _run_with_profiler

IPython/core/magics/execution.py:319–389  ·  view source on GitHub ↗

Run `code` with profiler. Used by ``%prun`` and ``%run -p``. Parameters ---------- code : str Code to be executed. opts : Struct Options parsed by `self.parse_options`. namespace : dict A dictionary for Python nam

(self, code, opts, namespace)

Source from the content-addressed store, hash-verified

317 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
318
319 def _run_with_profiler(self, code, opts, namespace):
320 """
321 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
322
323 Parameters
324 ----------
325 code : str
326 Code to be executed.
327 opts : Struct
328 Options parsed by `self.parse_options`.
329 namespace : dict
330 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
331
332 """
333
334 # Fill default values for unspecified options:
335 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
336
337 prof = profile.Profile()
338 try:
339 prof = prof.runctx(code, namespace, namespace)
340 sys_exit = ''
341 except SystemExit:
342 sys_exit = """*** SystemExit exception caught in code being profiled."""
343
344 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
345
346 lims = opts.l
347 if lims:
348 lims = [] # rebuild lims with ints/floats/strings
349 for lim in opts.l:
350 try:
351 lims.append(int(lim))
352 except ValueError:
353 try:
354 lims.append(float(lim))
355 except ValueError:
356 lims.append(lim)
357
358 # Trap output.
359 stdout_trap = StringIO()
360 stats_stream = stats.stream
361 try:
362 stats.stream = stdout_trap
363 stats.print_stats(*lims)
364 finally:
365 stats.stream = stats_stream
366
367 output = stdout_trap.getvalue()
368 output = output.rstrip()
369
370 if 'q' not in opts:
371 page.page(output)
372 print(sys_exit, end=' ')
373
374 dump_file = opts.D[0]
375 text_file = opts.T[0]
376 if dump_file:

Callers 2

prunMethod · 0.95
runMethod · 0.95

Calls 4

StructClass · 0.90
mergeMethod · 0.80
pageMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected