MCPcopy Index your code
hub / github.com/ipython/ipython / _run_with_profiler

Method _run_with_profiler

IPython/core/magics/execution.py:328–402  ·  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

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

Callers 2

prunMethod · 0.95
runMethod · 0.95

Calls 3

StructClass · 0.90
mergeMethod · 0.80
pageMethod · 0.80

Tested by

no test coverage detected