@@ -135,7 +135,7 @@ def _execute_module(module_name: str, module_args: List[str]) -> None:
135135 module_args: Arguments to pass to the module
136136
137137 Raises:
138- TargetError: If module execution fails
138+ TargetError: If module cannot be found
139139 """
140140 # Replace sys.argv to match how Python normally runs modules
141141 # When running 'python -m module args', sys.argv is ["__main__.py", "args"]
@@ -145,11 +145,8 @@ def _execute_module(module_name: str, module_args: List[str]) -> None:
145145 runpy .run_module (module_name , run_name = "__main__" , alter_sys = True )
146146 except ImportError as e :
147147 raise TargetError (f"Module '{ module_name } ' not found: { e } " ) from e
148- except SystemExit :
149- # SystemExit is normal for modules
150- pass
151- except Exception as e :
152- raise TargetError (f"Error executing module '{ module_name } ': { e } " ) from e
148+ # Let other exceptions (including SystemExit) propagate naturally
149+ # so Python prints the full traceback to stderr
153150
154151
155152def _execute_script (script_path : str , script_args : List [str ], cwd : str ) -> None :
@@ -183,22 +180,20 @@ def _execute_script(script_path: str, script_args: List[str], cwd: str) -> None:
183180 except PermissionError as e :
184181 raise TargetError (f"Permission denied reading script: { script_path } " ) from e
185182
186- try :
187- main_module = types .ModuleType ("__main__" )
188- main_module .__file__ = script_path
189- main_module .__builtins__ = __builtins__
190- # gh-140729: Create a __mp_main__ module to allow pickling
191- sys .modules ['__main__' ] = sys .modules ['__mp_main__' ] = main_module
183+ main_module = types .ModuleType ("__main__" )
184+ main_module .__file__ = script_path
185+ main_module .__builtins__ = __builtins__
186+ # gh-140729: Create a __mp_main__ module to allow pickling
187+ sys .modules ['__main__' ] = sys .modules ['__mp_main__' ] = main_module
192188
189+ try :
193190 code = compile (source_code , script_path , 'exec' , module = '__main__' )
194- exec (code , main_module .__dict__ )
195191 except SyntaxError as e :
196192 raise TargetError (f"Syntax error in script { script_path } : { e } " ) from e
197- except SystemExit :
198- # SystemExit is normal for scripts
199- pass
200- except Exception as e :
201- raise TargetError (f"Error executing script '{ script_path } ': { e } " ) from e
193+
194+ # Execute the script - let exceptions propagate naturally so Python
195+ # prints the full traceback to stderr
196+ exec (code , main_module .__dict__ )
202197
203198
204199def main () -> NoReturn :
@@ -209,6 +204,8 @@ def main() -> NoReturn:
209204 with the sample profiler by signaling when the process is ready
210205 to be profiled.
211206 """
207+ # Phase 1: Parse arguments and set up environment
208+ # Errors here are coordinator errors, not script errors
212209 try :
213210 # Parse and validate arguments
214211 sync_port , cwd , target_args = _validate_arguments (sys .argv )
@@ -237,21 +234,19 @@ def main() -> NoReturn:
237234 # Signal readiness to profiler
238235 _signal_readiness (sync_port )
239236
240- # Execute the target
241- if is_module :
242- _execute_module (module_name , module_args )
243- else :
244- _execute_script (script_path , script_args , cwd )
245-
246237 except CoordinatorError as e :
247238 print (f"Profiler coordinator error: { e } " , file = sys .stderr )
248239 sys .exit (1 )
249240 except KeyboardInterrupt :
250241 print ("Interrupted" , file = sys .stderr )
251242 sys .exit (1 )
252- except Exception as e :
253- print (f"Unexpected error in profiler coordinator: { e } " , file = sys .stderr )
254- 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 )
255250
256251 # Normal exit
257252 sys .exit (0 )
0 commit comments