Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ def _readmodule(module, path, inpackage=None):
search_path = path
else:
search_path = path + sys.path
spec = importlib.util._find_spec_from_path(fullmodule, search_path)
try: spec = importlib.util._find_spec_from_path(fullmodule, search_path)
except ValueError:
# ValueError: __main__.__spec__ is None / is not set
spec = None

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.

It looks like this would just lead to ModuleNotFoundError below, which seems misleading because module does exist. Is ModuleNotFoundError caught somewhere and pyclbr proceeds to next module? Did you test if this change fixes the reported bug?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The change from ValueError to an ImportError (subclass) makes it to be processed reasonable (skip to next) in the caller https://github.com/python/cpython/blob/3.10/Lib/pyclbr.py#L243 / visit_Import() in the call stack shown by the trace in bpo 43299 (2 levels higher). And solves the bug in that case at least - and similar in other callers of _readmodule()
In that case the module (main) can indeed not be found in the filesystem for parsing. regular modules have a spec. Raising an ImportError with extra specific text could be another option.

if spec is None:
raise ModuleNotFoundError(f"no module named {fullmodule!r}", name=fullmodule)
_modules[fullmodule] = tree
Expand Down