Main coordinator function. This function coordinates the startup of a target Python process with the sample profiler by signaling when the process is ready to be profiled.
()
| 197 | |
| 198 | |
| 199 | def main() -> NoReturn: |
| 200 | """ |
| 201 | Main coordinator function. |
| 202 | |
| 203 | This function coordinates the startup of a target Python process |
| 204 | with the sample profiler by signaling when the process is ready |
| 205 | to be profiled. |
| 206 | """ |
| 207 | # Phase 1: Parse arguments and set up environment |
| 208 | # Errors here are coordinator errors, not script errors |
| 209 | try: |
| 210 | # Parse and validate arguments |
| 211 | sync_port, cwd, target_args = _validate_arguments(sys.argv) |
| 212 | |
| 213 | # Set up execution environment |
| 214 | _setup_environment(cwd) |
| 215 | |
| 216 | # Determine execution type and validate target exists |
| 217 | is_module = target_args[0] == "-m" |
| 218 | if is_module: |
| 219 | if len(target_args) < 2: |
| 220 | raise ArgumentError("Module name required after -m") |
| 221 | module_name = target_args[1] |
| 222 | module_args = target_args[2:] |
| 223 | |
| 224 | if importlib.util.find_spec(module_name) is None: |
| 225 | raise TargetError(f"Module not found: {module_name}") |
| 226 | else: |
| 227 | script_path = target_args[0] |
| 228 | script_args = target_args[1:] |
| 229 | # Match the path resolution logic in _execute_script |
| 230 | check_path = script_path if os.path.isabs(script_path) else os.path.join(cwd, script_path) |
| 231 | if not os.path.isfile(check_path): |
| 232 | raise TargetError(f"Script not found: {script_path}") |
| 233 | |
| 234 | # Signal readiness to profiler |
| 235 | _signal_readiness(sync_port) |
| 236 | |
| 237 | except CoordinatorError as e: |
| 238 | print(f"Profiler coordinator error: {e}", file=sys.stderr) |
| 239 | sys.exit(1) |
| 240 | except KeyboardInterrupt: |
| 241 | print("Interrupted", file=sys.stderr) |
| 242 | sys.exit(1) |
| 243 | |
| 244 | # Phase 2: Execute the target script/module |
| 245 | # Let exceptions propagate naturally so Python prints full tracebacks |
| 246 | if is_module: |
| 247 | _execute_module(module_name, module_args) |
| 248 | else: |
| 249 | _execute_script(script_path, script_args, cwd) |
| 250 | |
| 251 | # Normal exit |
| 252 | sys.exit(0) |
| 253 | |
| 254 | |
| 255 | if __name__ == "__main__": |
no test coverage detected
searching dependent graphs…