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

Function _handle_run

Lib/profiling/sampling/cli.py:1004–1091  ·  view source on GitHub ↗

Handle the 'run' command.

(args)

Source from the content-addressed store, hash-verified

1002
1003
1004def _handle_run(args):
1005 """Handle the 'run' command."""
1006 # Validate target exists before launching subprocess
1007 if args.module:
1008 # Temporarily add cwd to sys.path so we can find modules in the
1009 # current directory, matching the coordinator's behavior
1010 cwd = os.getcwd()
1011 added_cwd = False
1012 if cwd not in sys.path:
1013 sys.path.insert(0, cwd)
1014 added_cwd = True
1015 try:
1016 if importlib.util.find_spec(args.target) is None:
1017 raise SamplingModuleNotFoundError(args.target)
1018 finally:
1019 if added_cwd:
1020 sys.path.remove(cwd)
1021 else:
1022 if not os.path.exists(args.target):
1023 raise SamplingScriptNotFoundError(args.target)
1024
1025 # Check if live mode is requested
1026 if args.live:
1027 _handle_live_run(args)
1028 return
1029
1030 # Build the command to run
1031 if args.module:
1032 cmd = (sys.executable, "-m", args.target, *args.args)
1033 else:
1034 cmd = (sys.executable, args.target, *args.args)
1035
1036 # Run with synchronization
1037 try:
1038 process = _run_with_sync(cmd, suppress_output=False)
1039 except RuntimeError as e:
1040 sys.exit(f"Error: {e}")
1041
1042 # Use PROFILING_MODE_ALL for gecko format
1043 mode = (
1044 PROFILING_MODE_ALL
1045 if args.format == "gecko"
1046 else _parse_mode(args.mode)
1047 )
1048
1049 # Determine skip_idle based on mode
1050 skip_idle = (
1051 mode != PROFILING_MODE_WALL if mode != PROFILING_MODE_ALL else False
1052 )
1053
1054 output_file = None
1055 if args.format == "binary":
1056 output_file = args.outfile or _generate_output_filename(args.format, process.pid)
1057
1058 # Create the appropriate collector
1059 collector = _create_collector(
1060 args.format, args.sample_interval_usec, skip_idle, args.opcodes,
1061 output_file=output_file,

Callers

nothing calls this directly

Calls 15

sampleFunction · 0.90
_handle_live_runFunction · 0.85
_run_with_syncFunction · 0.85
_parse_modeFunction · 0.85
_create_collectorFunction · 0.85
_handle_outputFunction · 0.85
insertMethod · 0.45
find_specMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…