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.
This commit is contained in:
Brett Cannon 2010-05-20 18:37:55 +00:00
parent b1556c537d
commit eb3cd301ae
3 changed files with 17 additions and 1 deletions

View File

@ -431,6 +431,18 @@ class RelativeImportTests(unittest.TestCase):
self.assertRaises(ValueError, check_absolute)
self.assertRaises(ValueError, check_relative)
def test_absolute_import_without_future(self):
# If absolute import syntax is used, then do not try to perform
# a relative import in the face of failure.
# Issue #7902.
try:
from .os import sep
except ImportError:
pass
else:
self.fail("explicit relative import triggered an "
"implicit relative import")
def test_main(verbose=None):
run_unittest(ImportTests, PycRewritingTests, PathsTests, RelativeImportTests)

View File

@ -12,6 +12,9 @@ What's New in Python 2.7 Release Candidate 1?
Core and Builtins
-----------------
- Issue #7902: When using explicit relative import syntax, don't try
implicit relative import semantics.
- Issue #7079: Fix a possible crash when closing a file object while using
it from another thread. Patch by Daniel Stutzbach.

View File

@ -2134,7 +2134,8 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
if (parent == NULL)
return NULL;
head = load_next(parent, Py_None, &name, buf, &buflen);
head = load_next(parent, level < 0 ? Py_None : parent, &name, buf,
&buflen);
if (head == NULL)
return NULL;