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
Lib/pyclbr.py crashes when the package spec cannot be determined by importlib #80479
Comments
Hi folks! (apologies in advance if any of the code blocks come out irregular, this is my first issue) I was just exploring the I noticed the module could be run directly and after attempting to run it withan invalid source target I was running 3.7.2, although I assume this affects future versions and possibly some older versions. (I suspect the bug originates from importlib silently breaking backwards compatability) I thought it strange for a script to exit so loudly so after reading through the source I believe the intended behavior meant for an invalid target is to raise an I believe this is a bug (but I would very much appreciate a second opinion) and I've identified it as a low priority easy fix, In which case I'd be more than happy to submit a fix :) |
I finished patching pyclbr to be a complete module class and function browser in summer 2017, for use by IDLE's class browser. I verified the bug with the import API, with a twist. >>> import pyclbr
>>> pyclbr.readmodule_ex('<invalid')
...
AttributeError: 'NoneType' object has no attribute 'submodule_search_locations'
>>> pyclbr.readmodule_ex('<invalid>')
{} The relevant block in pyclbr is spec = importlib.util._find_spec_from_path(fullmodule, search_path)
_modules[fullmodule] = tree
# Is module a package?
if spec.submodule_search_locations is not None:
tree['__path__'] = spec.submodule_search_locations Caching (fullmodule, tree) before the spec access is why the repeat call above returns the empty tree. This block is preceded by a block that explicitly raises 'ImportError', even though no import is attemped. It is followed by a block that returns the tree in case of error.
So either a) the empty tree should be returned when the NoneType exception is caught, or b) the invalid names should not be cached and the AttributeError turned into an 'ImportError'. I believe we should do the latter, as you suggested, and move the caching line down to follow the new try-except. If you want, submit a PR for the master branch *after* signing the CLA. If you can, include a new assertRaises test based on the invalid call above. And check that '<invalid>' is not in pyclbr._modules with assertNotIn('<invalid>', pyclbr._modules). |
I have a fix en-route. |
Got a patch up if either of you would like to review. |
(And sorry about stealing this from you, Terry; I forgot to flip the assignment while I was looking into this.) |
Thanks :) I've submitted a review for the patch. |
Thanks, mental! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: