Skip to content

Commit bad9691

Browse files
committed
Add comments explaining exception catching strategy
Explain why we catch broad Exception for import_main_path() (it uses runpy.run_path() which can raise any exception) vs only ImportError for regular __import__() calls.
1 parent 30c2cf8 commit bad9691

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

Lib/multiprocessing/forkserver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ def _handle_preload(preload, main_path=None, sys_path=None, on_error='ignore'):
239239
try:
240240
spawn.import_main_path(main_path)
241241
except Exception as e:
242+
# Catch broad Exception because import_main_path() uses
243+
# runpy.run_path() which executes the script and can raise
244+
# any exception, not just ImportError
242245
match on_error:
243246
case 'fail':
244247
raise
@@ -258,6 +261,7 @@ def _handle_preload(preload, main_path=None, sys_path=None, on_error='ignore'):
258261
try:
259262
__import__(modname)
260263
except ImportError as e:
264+
# Only catch ImportError for regular module imports
261265
match on_error:
262266
case 'fail':
263267
raise

0 commit comments

Comments
 (0)