This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: importlib.__import__ does not fail for invalid relative import
Type: behavior Stage: patch review
Components: Versions: Python 3.6, Python 3.3, Python 3.4, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, eric.snow, mjacob, ncoghlan, python-dev, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2016-02-16 00:59 by mjacob, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
relimport-3.5.patch mjacob, 2016-02-17 00:19 Patch against 3.5 branch (without changes in Python/importlib.h)
relimport-3.6.patch mjacob, 2016-02-17 00:20 Patch against default branch (without changes in Python/importlib.h) review
Messages (11)
msg260338 - (view) Author: Manuel Jacob (mjacob) * Date: 2016-02-16 00:59
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/'>
>>> __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).
msg260461 - (view) Author: Manuel Jacob (mjacob) * Date: 2016-02-18 14:44
(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 issue18018.
msg260484 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-02-18 22:53
The corresponding code in C that actually does the check:

Manuel, can you sign the CLA at ? Once you sign it I can officially review your patch, otherwise I technically can't touch your code.
msg260486 - (view) Author: Manuel Jacob (mjacob) * Date: 2016-02-18 23:07
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?
msg260491 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-02-19 00:06
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).
msg260540 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-02-20 00:19
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.
msg260575 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-02-20 20:59
New changeset e523efd47418 by Brett Cannon in branch '3.5':
Issue #26367: Have importlib.__init__() raise RuntimeError when

New changeset 8f72bf88f471 by Brett Cannon in branch 'default':
Merge for issue #26367
msg260576 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-02-20 21:00
Thanks for the patch, Manuel!
msg260599 - (view) Author: Manuel Jacob (mjacob) * Date: 2016-02-21 09:29
I think the "What's New" entry has two typos.  It should be `importlib.__import__()` instead of `importlib.__init__()` and SystemError instead of RuntimeError.
msg260623 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-02-21 17:23
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 (
msg298891 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-07-23 06:44
New changeset c824cc8426a16dd9f3949a3ed135523d37787bae by Serhiy Storchaka in branch '3.5':
[3.5] Backport bpo-30876 (GH-2639), bpo-18018 and bpo-26367. (#2677)
Date User Action Args
2022-04-11 14:58:27adminsetgithub: 70555
2017-07-23 06:44:07serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg298891
2016-02-21 17:23:08brett.cannonsetmessages: + msg260623
2016-02-21 09:29:39mjacobsetmessages: + msg260599
2016-02-20 21:00:13brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg260576
2016-02-20 20:59:44python-devsetnosy: + python-dev
messages: + msg260575
2016-02-20 00:19:36terry.reedysetnosy: + terry.reedy
messages: + msg260540
2016-02-19 00:06:38brett.cannonsetmessages: + msg260491
2016-02-18 23:07:12mjacobsetmessages: + msg260486
2016-02-18 22:53:43brett.cannonsetmessages: + msg260484
stage: patch review
2016-02-18 14:44:03mjacobsetmessages: + msg260461
2016-02-17 19:44:31brett.cannonsetassignee: brett.cannon
2016-02-17 00:20:00mjacobsetfiles: + relimport-3.6.patch
2016-02-17 00:19:45mjacobsetfiles: + relimport-3.5.patch
keywords: + patch
2016-02-16 00:59:01mjacobcreate