]> granicus.if.org Git - python/commitdiff
Fix importlib.machinery.FrozenImporter.load_module() to set __package__
authorBrett Cannon <bcannon@gmail.com>
Sun, 1 Feb 2009 01:34:13 +0000 (01:34 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sun, 1 Feb 2009 01:34:13 +0000 (01:34 +0000)
properly. Discovered by also moving the loader tests over to
importlib.test.abc.LoaderTests.

Lib/importlib/NOTES
Lib/importlib/_bootstrap.py
Lib/importlib/test/frozen/test_loader.py

index 9d5fd6959de0103ec604188c52b0f91f38a64f70..38277130b9aa98e82ab2d673bc385d509110f2bc 100644 (file)
@@ -3,7 +3,6 @@ to do
 
 * Use test.abc.LoaderTests
 
-    + frozen
     + source
 
 * Reorganize support code.
index c1d09dd2f7d07ac6f3453ed0457aa021222cb999..d2d5d3403a4e71837b1302dec748181a2b094ffd 100644 (file)
@@ -137,7 +137,12 @@ class FrozenImporter:
         """Load a frozen module."""
         if cls.find_module(fullname) is None:
             raise ImportError("{0} is not a frozen module".format(fullname))
-        return imp.init_frozen(fullname)
+        module = imp.init_frozen(fullname)
+        if hasattr(module, '__path__'):
+            module.__package__ = module.__name__
+        elif '.' in module.__name__:
+            module.__package__ = module.__name__.rsplit('.', 1)[0]
+        return module
 
 
 class ChainedImporter(object):
index 95854f2ac83f0db31f1433a47eb69204530ea260..63a9742ad436e397c87c3801e42dd6d29791b75f 100644 (file)
@@ -1,26 +1,59 @@
 from importlib import machinery
-from ..builtin import test_loader
-
-
-class LoaderTests(test_loader.LoaderTests):
-
-    name = '__phello__'
-    load_module = staticmethod(lambda name:
-                                machinery.FrozenImporter.load_module(name))
-    verification = {'__name__': '__phello__', '__file__': '<frozen>',
-                    '__package__': None, '__path__': ['__phello__']}
-
-
-class SubmoduleLoaderTests(LoaderTests):
-
-    name = '__phello__.spam'
-    verification = {'__name__': '__phello__.spam', '__file__': '<frozen>',
-                    '__package__': None}
+from .. import abc
+from .. import support
+
+
+class LoaderTests(abc.LoaderTests):
+
+    def test_module(self):
+        with support.uncache('__hello__'):
+            module = machinery.FrozenImporter.load_module('__hello__')
+            check = {'__name__': '__hello__', '__file__': '<frozen>',
+                        '__package__': None}
+            for attr, value in check.items():
+                self.assertEqual(getattr(module, attr), value)
+
+    def test_package(self):
+        with support.uncache('__phello__'):
+            module = machinery.FrozenImporter.load_module('__phello__')
+            check = {'__name__': '__phello__', '__file__': '<frozen>',
+                     '__package__': '__phello__', '__path__': ['__phello__']}
+            for attr, value in check.items():
+                attr_value = getattr(module, attr)
+                self.assertEqual(attr_value, value,
+                                 "for __phello__.%s, %r != %r" %
+                                 (attr, attr_value, value))
+
+    def test_lacking_parent(self):
+        with support.uncache('__phello__', '__phello__.spam'):
+            module = machinery.FrozenImporter.load_module('__phello__.spam')
+            check = {'__name__': '__phello__.spam', '__file__': '<frozen>',
+                     '__package__': '__phello__'}
+            for attr, value in check.items():
+                attr_value = getattr(module, attr)
+                self.assertEqual(attr_value, value,
+                                 "for __phello__.spam.%s, %r != %r" %
+                                 (attr, attr_value, value))
+
+    def test_module_reuse(self):
+        with support.uncache('__hello__'):
+            module1 = machinery.FrozenImporter.load_module('__hello__')
+            module2 = machinery.FrozenImporter.load_module('__hello__')
+            self.assert_(module1 is module2)
+
+    def test_state_after_failure(self):
+        # No way to trigger an error in a frozen module.
+        pass
+
+    def test_unloadable(self):
+        assert machinery.FrozenImporter.find_module('_not_real') is None
+        self.assertRaises(ImportError, machinery.FrozenImporter.load_module,
+                            '_not_real')
 
 
 def test_main():
     from test.support import run_unittest
-    run_unittest(LoaderTests, SubmoduleLoaderTests)
+    run_unittest(LoaderTests)
 
 
 if __name__ == '__main__':