MCPcopy Index your code
hub / github.com/python/cpython / _handle_output

Function _handle_output

Lib/profiling/sampling/cli.py:653–694  ·  view source on GitHub ↗

Handle output for the collector based on format and arguments. Args: collector: The collector instance with profiling data args: Parsed command-line arguments pid: Process ID (for generating filenames) mode: Profiling mode used

(collector, args, pid, mode)

Source from the content-addressed store, hash-verified

651
652
653def _handle_output(collector, args, pid, mode):
654 """Handle output for the collector based on format and arguments.
655
656 Args:
657 collector: The collector instance with profiling data
658 args: Parsed command-line arguments
659 pid: Process ID (for generating filenames)
660 mode: Profiling mode used
661 """
662 if args.format == "binary":
663 # Binary format already wrote to file incrementally, just finalize
664 collector.export(None)
665 filename = collector.filename
666 print(f"Binary profile written to {filename} ({collector.total_samples} samples)")
667 elif args.format == "pstats":
668 if args.outfile:
669 # If outfile is a directory, generate filename inside it
670 if os.path.isdir(args.outfile):
671 filename = os.path.join(args.outfile, _generate_output_filename(args.format, pid))
672 collector.export(filename)
673 else:
674 collector.export(args.outfile)
675 else:
676 # Print to stdout with defaults applied
677 sort_choice = args.sort if args.sort is not None else "nsamples"
678 limit = args.limit if args.limit is not None else 15
679 sort_mode = _sort_to_mode(sort_choice)
680 collector.print_stats(
681 sort_mode, limit, not args.no_summary, mode
682 )
683 else:
684 # Export to file
685 if args.outfile and os.path.isdir(args.outfile):
686 # If outfile is a directory, generate filename inside it
687 filename = os.path.join(args.outfile, _generate_output_filename(args.format, pid))
688 else:
689 filename = args.outfile or _generate_output_filename(args.format, pid)
690 collector.export(filename)
691
692 # Auto-open browser for HTML output if --browser flag is set
693 if args.format in ('flamegraph', 'diff_flamegraph', 'heatmap') and getattr(args, 'browser', False):
694 _open_in_browser(filename)
695
696
697def _validate_args(args, parser):

Callers 2

_handle_attachFunction · 0.85
_handle_runFunction · 0.85

Calls 7

_sort_to_modeFunction · 0.85
_open_in_browserFunction · 0.85
exportMethod · 0.45
isdirMethod · 0.45
joinMethod · 0.45
print_statsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…