-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
importlib.__import__ does not fail for invalid relative import #70555
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
Comments
Python 3.6.0a0 (default:6c6f7dff597b, Feb 16 2016, 01:24:51)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.__import__('array', globals(), locals(), level=1)
<module '.array' from '/home/manu/vcs/cpython/build/lib.linux-x86_64-3.6/array.cpython-36m-x86_64-linux-gnu.so'>
>>> __import__('array', globals(), locals(), level=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: attempted relative import with no known parent package Instead of failing, importlib.__import__ returns a module with a wrong name. This happens with both built-in modules and pure python modules. However it fails when replacing 'array' with 'time' (this seems to be related to whether the module is in Modules/Setup.dist). |
(For some reason, I forgot to submit the previous comment). The attached patches fix the issue (one for the 3.5 branch, one for the default branch) by bringing importlib.__import__ closer to the builtin __import__. The extra code in the default / 3.6 branch is because of the changes from bpo-18018. |
The corresponding code in C that actually does the check: https://hg.python.org/cpython/file/default/Python/import.c#l1498 Manuel, can you sign the CLA at https://www.python.org/psf/contrib/contrib-form/ ? Once you sign it I can officially review your patch, otherwise I technically can't touch your code. |
Done. I'm a bit surprised this wasn't necessary for my previous two patches, but they were even more trival than this one. ;) Do we have to wait until my tracker profile is updated? |
If a contribution is insignificant enough then technically a CLA isn't necessary, but I just make it a habit to ask for one regardless. And your profile will probably get updated fairly quickly, so I wouldn't worry about it holding anything up (I'm hoping to commit the code tomorrow; I already did a review and it LGTM). |
Manuel, welcome to the tracker and thank you for submitting patches. I agree that 5 lines is past the threshhold. Your profile has been CLA-updated, so we are good to go. |
New changeset e523efd47418 by Brett Cannon in branch '3.5': New changeset 8f72bf88f471 by Brett Cannon in branch 'default': |
Thanks for the patch, Manuel! |
I think the "What's New" entry has two typos. It should be |
Thanks for spotting those! And just to clarify on terminology, the typos were in the NEWS file, not "What's New" which is a different thing (https://hg.python.org/cpython/file/default/Doc/whatsnew). |
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: