[pypy-svn] pypy default: "from .os import open" must not succeed: don't try absolute import

amauryfa commits-noreply at bitbucket.org
Wed Jan 26 19:09:30 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41367:6ecfd7e966b8
Date: 2011-01-26 17:32 +0100
http://bitbucket.org/pypy/pypy/changeset/6ecfd7e966b8/

Log:	"from .os import open" must not succeed: don't try absolute import
	after an explicit relative import has been tried

diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -49,6 +49,7 @@
              absolute   = "from __future__ import absolute_import\nimport string",
              relative_b = "from __future__ import absolute_import\nfrom . import string",
              relative_c = "from __future__ import absolute_import\nfrom .string import inpackage",
+             relative_f = "from .os import sep",
              )
     setuppkg("pkg.pkg1", 
              a          = '',
@@ -276,7 +277,7 @@
     def test_import_relative_partial_success(self):
         def imp():
             import pkg_r.inpkg
-        raises(ImportError,imp)
+        raises(ImportError, imp)
 
     def test_import_builtin_inpackage(self):
         def imp():
@@ -354,6 +355,12 @@
         from pkg import relative_b
         assert relative_b.string.inpackage == 1
 
+    def test_no_relative_import(self):
+        def imp():
+            from pkg import relative_f
+        exc = raises(ImportError, imp)
+        assert exc.value.message == "No module named pkg.os"
+
     def test_future_relative_import_level_1(self):
         from pkg import relative_c
         assert relative_c.inpackage == 1

diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -152,17 +152,19 @@
                     w_mod = check_sys_modules(space, w(rel_modulename))
                     if (w_mod is None or
                         not space.is_w(w_mod, space.w_None)):
+                        tentative = level < 0
                         w_mod = absolute_import(space, rel_modulename,
                                                 baselevel,
-                                                fromlist_w, tentative=1)
+                                                fromlist_w,
+                                                tentative=tentative)
                         if w_mod is not None:
                             space.timer.stop_name("importhook", modulename)
                             return w_mod
                     else:
                         rel_modulename = None
-    if level > 0:
-        msg = "Attempted relative import in non-package"
-        raise OperationError(space.w_ValueError, w(msg))
+        if level > 0:
+            msg = "Attempted relative import in non-package"
+            raise OperationError(space.w_ValueError, w(msg))
     w_mod = absolute_import_try(space, modulename, 0, fromlist_w)
     if w_mod is None or space.is_w(w_mod, space.w_None):
         w_mod = absolute_import(space, modulename, 0, fromlist_w, tentative=0)


More information about the Pypy-commit mailing list