Fix find_spec crash with C extension loaders by karlhillx · Pull Request #2190 · coveragepy/coveragepy · GitHub
Skip to content

Fix find_spec crash with C extension loaders#2190

Open
karlhillx wants to merge 2 commits into
coveragepy:mainfrom
karlhillx:fix-find-spec-crash
Open

Fix find_spec crash with C extension loaders#2190
karlhillx wants to merge 2 commits into
coveragepy:mainfrom
karlhillx:fix-find-spec-crash

Conversation

@karlhillx

Copy link
Copy Markdown

Fixes #2189

When coverage calls importlib.util.find_spec() on a submodule
(e.g. pikepdf.form), it can trigger loading of the parent package
(pikepdf). If pikepdf's C extension raises a non-Exception error
(e.g. Abort from a broken nanobind extension), that error would
propagate through file_and_path_for_module() and crash the process.

The fix changes except Exception to except BaseException in
file_and_path_for_module(), so non-Python exceptions are caught
and the function gracefully returns (None, []).

Regression test included.

When find_spec() triggers loading of a parent package whose C
extension raises a non-Exception error (Abort, SystemExit, etc.),
file_and_path_for_module() would propagate it instead of returning
(None, []). Catch BaseException to handle C-level crashes and other
non-Exception throws from the import machinery.

Regression test for coveragepy#2189.
@nedbat

nedbat commented Jun 21, 2026

Copy link
Copy Markdown
Member

@karlhillx

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--cov=module.submodule causes fatal crash with nanobind extensions

2 participants