[Python-checkins] cpython: Issues #18058, 18057: Make importlib._bootstrap.NamespaceLoader

brett.cannon python-checkins at python.org
Sun Jun 16 20:57:07 CEST 2013


http://hg.python.org/cpython/rev/ebec625b13f9
changeset:   84169:ebec625b13f9
user:        Brett Cannon <brett at python.org>
date:        Sun Jun 16 14:56:58 2013 -0400
summary:
  Issues #18058, 18057: Make importlib._bootstrap.NamespaceLoader
conform the the InspectLoader ABC. Perk of this is that runpy/-m can
now work with namespace packages.

files:
  Lib/importlib/_bootstrap.py     |    15 +-
  Lib/importlib/abc.py            |     2 +-
  Lib/test/test_namespace_pkgs.py |    29 +-
  Misc/NEWS                       |     4 +
  Python/importlib.h              |  2185 +++++++++---------
  5 files changed, 1156 insertions(+), 1079 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1238,12 +1238,25 @@
     def module_repr(cls, module):
         return "<module '{}' (namespace)>".format(module.__name__)
 
+    def is_package(self, fullname):
+        return True
+
+    def get_source(self, fullname):
+        return ''
+
+    def get_code(self, fullname):
+        return compile('', '<string>', 'exec', dont_inherit=True)
+
+    def init_module_attrs(self, module):
+        module.__loader__ = self
+        module.__package__ = module.__name__
+
     def load_module(self, fullname):
         """Load a namespace module."""
         _verbose_message('namespace module loaded with path {!r}', self._path)
         with module_to_load(fullname) as module:
+            self.init_module_attrs(module)
             module.__path__ = self._path
-            module.__package__ = fullname
             return module
 
 
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -188,7 +188,7 @@
     load_module = _bootstrap._LoaderBasics.load_module
 
 _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter,
-            machinery.ExtensionFileLoader)
+            machinery.ExtensionFileLoader, _bootstrap.NamespaceLoader)
 
 
 class ExecutionLoader(InspectLoader):
diff --git a/Lib/test/test_namespace_pkgs.py b/Lib/test/test_namespace_pkgs.py
--- a/Lib/test/test_namespace_pkgs.py
+++ b/Lib/test/test_namespace_pkgs.py
@@ -1,7 +1,11 @@
+import contextlib
+from importlib._bootstrap import NamespaceLoader
+import importlib.abc
+import importlib.machinery
+import os
 import sys
-import contextlib
+import types
 import unittest
-import os
 
 from test.test_importlib import util
 from test.support import run_unittest
@@ -286,9 +290,24 @@
         self.assertEqual(a_test.attr, 'in module')
 
 
-def test_main():
-    run_unittest(*NamespacePackageTest.__subclasses__())
+class ABCTests(unittest.TestCase):
+
+    def setUp(self):
+        self.loader = NamespaceLoader('foo', ['pkg'],
+                                      importlib.machinery.PathFinder)
+
+    def test_is_package(self):
+        self.assertTrue(self.loader.is_package('foo'))
+
+    def test_get_code(self):
+        self.assertTrue(isinstance(self.loader.get_code('foo'), types.CodeType))
+
+    def test_get_source(self):
+        self.assertEqual(self.loader.get_source('foo'), '')
+
+    def test_abc_isinstance(self):
+        self.assertTrue(isinstance(self.loader, importlib.abc.InspectLoader))
 
 
 if __name__ == "__main__":
-    test_main()
+    unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,10 @@
 Library
 -------
 
+- Issue #18058, 18057: Make the namespace package loader meet the
+  importlib.abc.InspectLoader ABC, allowing for namespace packages to work with
+  runpy.
+
 - Issue #17177: The imp module is pending deprecation.
 
 - subprocess: Prevent a possible double close of parent pipe fds when the
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list