summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-06-16 14:56:58 -0400
committerBrett Cannon <brett@python.org>2013-06-16 14:56:58 -0400
commit780dc5a8f4fb3323ae313d6b9a336750787ab6fd (patch)
treed2a0ba1acf9a08e02ba6a337caf3e5100ac5f840 /Lib
parent693ab1bbb6a5c0292e1312caf5c66d15bf061f87 (diff)
downloadcpython-780dc5a8f4fb3323ae313d6b9a336750787ab6fd.tar.gz
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/importlib/_bootstrap.py15
-rw-r--r--Lib/importlib/abc.py2
-rw-r--r--Lib/test/test_namespace_pkgs.py29
3 files changed, 39 insertions, 7 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 1276ff1a62..e32afec0ba 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1238,12 +1238,25 @@ class NamespaceLoader:
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
index cf20e9fe1a..b53a1a53f4 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -188,7 +188,7 @@ class InspectLoader(Loader):
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
index 7067b12e8f..4570bee568 100644
--- a/Lib/test/test_namespace_pkgs.py
+++ b/Lib/test/test_namespace_pkgs.py
@@ -1,7 +1,11 @@
-import sys
import contextlib
-import unittest
+from importlib._bootstrap import NamespaceLoader
+import importlib.abc
+import importlib.machinery
import os
+import sys
+import types
+import unittest
from test.test_importlib import util
from test.support import run_unittest
@@ -286,9 +290,24 @@ class ModuleAndNamespacePackageInSameDir(NamespacePackageTest):
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()