Repository navigation
Closed
Labels
3.8 (EOL)end of life3.9 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
Description
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:
assignee = 'https://github.com/brettcannon'
closed_at = <Date 2019-09-11.13:04:52.828>
created_at = <Date 2019-06-26.09:11:26.702>
labels = ['interpreter-core', 'type-bug', '3.8', '3.9']
title = 'relative import without parent succeeds with builtins.__import__'
updated_at = <Date 2019-09-12.09:31:18.058>
user = 'https://github.com/benjimin'bugs.python.org fields:
activity = <Date 2019-09-12.09:31:18.058>
actor = 'brett.cannon'
assignee = 'brett.cannon'
closed = True
closed_date = <Date 2019-09-11.13:04:52.828>
closer = 'brett.cannon'
components = ['Interpreter Core']
creation = <Date 2019-06-26.09:11:26.702>
creator = 'Ben Lewis2'
dependencies = []
files = []
hgrepos = []
issue_num = 37409
keywords = ['patch']
message_count = 14.0
messages = ['346593', '346654', '346710', '346781', '346856', '346857', '346861', '346866', '351794', '351832', '351848', '351948', '352004', '352075']
nosy_count = 6.0
nosy_names = ['brett.cannon', 'eric.smith', 'serhiy.storchaka', 'miss-islington', 'xtreak', 'Ben Lewis2']
pr_nums = ['14465', '14956', '15913', '15925', '16003']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue37409'
versions = ['Python 3.8', 'Python 3.9']Metadata
Metadata
Assignees
Labels
3.8 (EOL)end of life3.9 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
Activity
benjimin commentedon Jun 26, 2019
The second line should raise an ImportError but instead succeeds (tested cpython 3.6.7, 3.7.0 and 3.7.3, and from interactive interpreter and scripts). Specifically, builtins.__import__ does not reproduce the behaviour of importlib._bootstrap.__import__; maybe ceval.c:import_from is neglecting to check that there are parent packages when attempting a relative import?
More details here: https://stackoverflow.com/a/56768129/5104777
brettcannon commentedon Jun 26, 2019
I can't reproduce:
Did you happen to do an
import *prior to this in your REPL? If so that might explain it as you could have pulled in package and such and that's used to resolve relative imports.benjimin commentedon Jun 27, 2019
After further investigation, the problem is that builtins.__import__ (the c port version) does not replicate the behaviour of importlib.__import__ (the python reference version):
A further discrepancy is that for deeper relative imports, builtins.__import__ raises a ValueError instead of ImportError (contrary to expectation/spec):
A simple work around uses the python implementation to restore expected behaviour:
PS: Brett Cannon, to replicate please copy and paste lines in correct order :-)
[-]relative import_from without parent[/-][+]relative import without parent[/+]brettcannon commentedon Jun 27, 2019
Please open a separate issue for the relative import issue.
brettcannon commentedon Jun 28, 2019
I opened bpo-37444 for the relative import beyond top-level package issue.
brettcannon commentedon Jun 28, 2019
The code doing the sanity check for importlib can be found at
cpython/Lib/importlib/_bootstrap.py
Lines 943 to 948 in f9f8e3c
[-]relative import without parent[/-][+]relative import without parent succeeds with builtins.__import__[/+]brettcannon commentedon Jun 28, 2019
If you run with
-Xdev/warnings turned on you get an idea of what's happening:The check is being done in resolve_name() in import.c (
cpython/Python/import.c
Line 1543 in f9f8e3c
benjimin commentedon Jun 28, 2019
I'll look into this further and open a PR initially for the regression tests.
brettcannon commentedon Sep 11, 2019
New changeset 92420b3 by Brett Cannon (Ben Lewis) in branch 'master':
bpo-37409: fix relative import with no parent (bpo-14956)
92420b3
brettcannon commentedon Sep 11, 2019
New changeset 0a6693a by Brett Cannon in branch '3.8':
[3.8] bpo-37409: fix relative import with no parent (GH-14956) (GH-15913)
0a6693a
miss-islington commentedon Sep 11, 2019
New changeset f3480ad by Miss Islington (bot) in branch '3.7':
[3.8] bpo-37409: fix relative import with no parent (GH-14956) (GH-15913)
f3480ad
tirkarthi commentedon Sep 11, 2019
The test added seems to have created an ImportWarning in test_builtin.BuiltinTest.test_import .
./python.exe -Wall -m unittest -v test.test_builtin.BuiltinTest.test_import
test_import (test.test_builtin.BuiltinTest) ... /Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/case.py:202: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
callable_obj(*args, **kwargs)
ok
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
tirkarthi commentedon Sep 11, 2019
The test is same as below and given that __spec__ an __name__ are passed as None where ImportWarning is raised in Lib/importlib/_bootstrap.py 1074 . can we just use self.assertWarns(ImportWarning) in the test?
brettcannon commentedon Sep 12, 2019
Thanks for catching the warning and the fix, Karthikeyan!