Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 556d800

Browse files
committed
Merged revisions 81380 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r81380 | brett.cannon | 2010-05-20 13:37:55 -0500 (Thu, 20 May 2010) | 8 lines Turned out that if you used explicit relative import syntax (e.g. from .os import sep) and it failed, import would still try the implicit relative import semantics of an absolute import (from os import sep). That's not right, so when level is negative, only do explicit relative import semantics. Fixes issue #7902. Thanks to Meador Inge for the patch. ........
1 parent d7c3ed5 commit 556d800

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_import.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,17 @@ def check_relative():
468468
ns = dict(__package__=object())
469469
self.assertRaises(ValueError, check_relative)
470470

471+
def test_absolute_import_without_future(self):
472+
# If absolute import syntax is used, then do not try to perform
473+
# a relative import in the face of failure.
474+
# Issue #7902.
475+
try:
476+
from .os import sep
477+
except ImportError:
478+
pass
479+
else:
480+
self.fail("explicit relative import triggered an "
481+
"implicit relative import")
471482

472483
class OverridingImportBuiltinTests(unittest.TestCase):
473484
def test_override_builtin(self):

Python/import.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,8 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
23852385
if (parent == NULL)
23862386
return NULL;
23872387

2388-
head = load_next(parent, Py_None, &name, buf, &buflen);
2388+
head = load_next(parent, level < 0 ? Py_None : parent, &name, buf,
2389+
&buflen);
23892390
if (head == NULL)
23902391
return NULL;
23912392

0 commit comments

Comments
 (0)