-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-146632: make traceback handle illformed ModuleNotFoundError
#146633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1907,26 +1907,29 @@ def _find_incompatible_extension_module(module_name): | |
| import importlib.machinery | ||
| import importlib.resources.readers | ||
|
|
||
| if not module_name or not importlib.machinery.EXTENSION_SUFFIXES: | ||
| if not isinstance(module_name, str) or not importlib.machinery.EXTENSION_SUFFIXES: | ||
| return | ||
|
|
||
| # We assume the last extension is untagged (eg. .so, .pyd)! | ||
| # tests.test_traceback.MiscTest.test_find_incompatible_extension_modules | ||
| # tests that assumption. | ||
| untagged_suffix = importlib.machinery.EXTENSION_SUFFIXES[-1] | ||
| # On Windows the debug tag is part of the module file stem, instead of the | ||
| # extension (eg. foo_d.pyd), so let's remove it and just look for .pyd. | ||
| if os.name == 'nt': | ||
| untagged_suffix = untagged_suffix.removeprefix('_d') | ||
|
|
||
| parent, _, child = module_name.rpartition('.') | ||
| if parent: | ||
| traversable = importlib.resources.files(parent) | ||
| else: | ||
| traversable = importlib.resources.readers.MultiplexedPath( | ||
| *map(pathlib.Path, filter(os.path.isdir, sys.path)) | ||
| ) | ||
| try: | ||
| untagged_suffix = importlib.machinery.EXTENSION_SUFFIXES[-1] | ||
| # On Windows the debug tag is part of the module file stem, instead of the | ||
| # extension (eg. foo_d.pyd), so let's remove it and just look for .pyd. | ||
| if os.name == 'nt': | ||
| untagged_suffix = untagged_suffix.removeprefix('_d') | ||
|
|
||
| parent, _, child = module_name.rpartition('.') | ||
| if parent: | ||
| traversable = importlib.resources.files(parent) | ||
| else: | ||
| traversable = importlib.resources.readers.MultiplexedPath( | ||
| *map(pathlib.Path, filter(os.path.isdir, sys.path)) | ||
| ) | ||
|
|
||
| for entry in traversable.iterdir(): | ||
| if entry.name.startswith(child + '.') and entry.name.endswith(untagged_suffix): | ||
| return entry.name | ||
| for entry in traversable.iterdir(): | ||
| if entry.name.startswith(child + '.') and entry.name.endswith(untagged_suffix): | ||
| return entry.name | ||
| except Exception: | ||
| return | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignoring silently exceptions sounds like a bad pattern. If you expect specific exceptions, use more precise
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is impossible to use precise
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, I sugegst that we let the exception propagate. If this code path fails it's something we want to know. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add type check and use try-except to fix traceback crash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep
not module_nametest.