Skip to content

Conversation

kxrob
Copy link

@kxrob kxrob commented Feb 22, 2021

pyclbr.readmodule_ex() should not die when traversing
an "import main" statement or an import of another
module without spec attribute.

https://bugs.python.org/issue43299

https://bugs.python.org/issue43299

pyclbr.readmodule_ex() should not die when traversing
an "import __main__" statement or an import of another
module without __spec__ attribute.
@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@kxrob

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@github-actions
Copy link

github-actions bot commented Apr 4, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Apr 4, 2021
@kxrob
Copy link
Author

kxrob commented Apr 4, 2021

Is there a chance this bug can be solved? @vstinner

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Apr 5, 2021
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
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
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.

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.

5 participants