Skip to content

gh-151631: Fix traceback when parent package is blocked in sys.modules#151835

Open
bhuvi27 wants to merge 1 commit into
python:mainfrom
bhuvi27:gh-151631-fix-traceback-sys-modules-none
Open

gh-151631: Fix traceback when parent package is blocked in sys.modules#151835
bhuvi27 wants to merge 1 commit into
python:mainfrom
bhuvi27:gh-151631-fix-traceback-sys-modules-none

Conversation

@bhuvi27

@bhuvi27 bhuvi27 commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #151631.

_find_incompatible_extension_module() calls importlib.resources.files(parent), which tries to import the parent package. That blows up when sys.modules[parent] is None — the documented way to block imports in tests.

Catch ImportError and bail out instead of raising a second ModuleNotFoundError while formatting the original one.

…modules

_find_incompatible_extension_module() must not import a parent package
blocked via sys.modules[parent] = None when formatting ModuleNotFoundError.

@hroncok hroncok left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one nitpick, but it looks good otherwise. Thank you!

import logging
with self.assertLogs('test_traceback', level='ERROR') as cm_logs:
logging.getLogger('test_traceback').exception('fail', exc_info=exc)
self.assertIn("No module named 'pkg.mod'", cm_logs.output[0])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the 3 main use cases should be 3 separate tests?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_find_incompatible_extension_module raises ModuleNotFoundError when sys.modules contains None for the parent package

2 participants