[pypy-commit] pypy default: merge default

timfel noreply at buildbot.pypy.org
Thu Jul 2 12:50:42 CEST 2015


Author: Tim Felgentreff <timfelgentreff at gmail.com>
Branch: 
Changeset: r78410:b22f13cfe74d
Date: 2015-07-02 11:52 +0200
http://bitbucket.org/pypy/pypy/changeset/b22f13cfe74d/

Log:	merge default

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
@@ -349,11 +349,14 @@
                 w_all = try_getattr(space, w_mod, space.wrap('__all__'))
                 if w_all is not None:
                     fromlist_w = space.fixedview(w_all)
-            else:
-                # this only runs if fromlist_w != ['*']
-                for w_name in fromlist_w:
-                    if try_getattr(space, w_mod, w_name) is None:
-                        return None
+                else:
+                    fromlist_w = []
+                    # "from x import *" with x already imported and no x.__all__
+                    # always succeeds without doing more imports.  It will
+                    # just copy everything from x.__dict__ as it is now.
+            for w_name in fromlist_w:
+                if try_getattr(space, w_mod, w_name) is None:
+                    return None
         return w_mod
     return first
 
@@ -391,12 +394,12 @@
                 w_all = try_getattr(space, w_mod, w('__all__'))
                 if w_all is not None:
                     fromlist_w = space.fixedview(w_all)
-            else:
-                # this only runs if fromlist_w != ['*']
-                for w_name in fromlist_w:
-                    if try_getattr(space, w_mod, w_name) is None:
-                        load_part(space, w_path, prefix, space.str0_w(w_name),
-                                  w_mod, tentative=1)
+                else:
+                    fromlist_w = []
+            for w_name in fromlist_w:
+                if try_getattr(space, w_mod, w_name) is None:
+                    load_part(space, w_path, prefix, space.str0_w(w_name),
+                              w_mod, tentative=1)
         return w_mod
     else:
         return first
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
@@ -66,6 +66,14 @@
              b          = "insubpackage = 1",
              )
     setuppkg("pkg.pkg2", a='', b='')
+    setuppkg("pkg.withall",
+             __init__  = "__all__ = ['foobar']",
+             foobar    = "found = 123")
+    setuppkg("pkg.withoutall",
+             __init__  = "",
+             foobar    = "found = 123")
+    setuppkg("pkg.bogusall",
+             __init__  = "__all__ = 42")
     setuppkg("pkg_r", inpkg = "import x.y")
     setuppkg("pkg_r.x")
     setuppkg("x", y='')
@@ -677,6 +685,32 @@
         import imp
         raises(ValueError, imp.load_module, "", "", "", [1, 2, 3, 4])
 
+    def test_import_star_finds_submodules_with___all__(self):
+        for case in ["not-imported-yet", "already-imported"]:
+            d = {}
+            exec "from pkg.withall import *" in d
+            assert d["foobar"].found == 123
+
+    def test_import_star_does_not_find_submodules_without___all__(self):
+        for case in ["not-imported-yet", "already-imported"]:
+            d = {}
+            exec "from pkg.withoutall import *" in d
+            assert "foobar" not in d
+        import pkg.withoutall.foobar     # <- import it here only
+        for case in ["not-imported-yet", "already-imported"]:
+            d = {}
+            exec "from pkg.withoutall import *" in d
+            assert d["foobar"].found == 123
+
+    def test_import_star_with_bogus___all__(self):
+        for case in ["not-imported-yet", "already-imported"]:
+            try:
+                exec "from pkg.bogusall import *" in {}
+            except TypeError:
+                pass    # 'int' object does not support indexing
+            else:
+                raise AssertionError("should have failed")
+
 
 class TestAbi:
     def test_abi_tag(self):


More information about the pypy-commit mailing list