summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2009-03-15 01:41:33 (GMT)
committerBrett Cannon <bcannon@gmail.com>2009-03-15 01:41:33 (GMT)
commita113ac58be3ee73d1382af3bfbf47b7ed5e5f5a9 (patch)
tree78b2a6b9730046c61c3d76c7cb7b7f195c695136 /Lib/importlib
parent7aa21f75c1d9ac36f1ab7e00aa9d0048ef61476d (diff)
downloadcpython-a113ac58be3ee73d1382af3bfbf47b7ed5e5f5a9.zip
cpython-a113ac58be3ee73d1382af3bfbf47b7ed5e5f5a9.tar.gz
cpython-a113ac58be3ee73d1382af3bfbf47b7ed5e5f5a9.tar.bz2
Implement InspectLoader for BuiltinImporter.
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/NOTES4
-rw-r--r--Lib/importlib/_bootstrap.py31
-rw-r--r--Lib/importlib/abc.py3
-rw-r--r--Lib/importlib/test/builtin/test_finder.py14
-rw-r--r--Lib/importlib/test/builtin/test_loader.py43
-rw-r--r--Lib/importlib/test/builtin/util.py7
6 files changed, 80 insertions, 22 deletions
diff --git a/Lib/importlib/NOTES b/Lib/importlib/NOTES
index 2220a16..44cfc09 100644
--- a/Lib/importlib/NOTES
+++ b/Lib/importlib/NOTES
@@ -1,9 +1,7 @@
to do
/////
-* Fill in docstrings for ABCs.
-
-* Implement InspectLoader for BuiltinImporter and FrozenImporter.
+* Implement InspectLoader for FrozenImporter.
+ Expose function to see if a frozen module is a package.
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index e94c1d2..736e1b6 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -173,6 +173,16 @@ def _check_name(method):
return inner
+def _requires_builtin(fxn):
+ """Decorator to verify the named module is built-in."""
+ def wrapper(self, fullname):
+ if fullname not in sys.builtin_module_names:
+ raise ImportError("{0} is not a built-in module".format(fullname))
+ return fxn(self, fullname)
+ _wrap(wrapper, fxn)
+ return wrapper
+
+
def _suffix_list(suffix_type):
"""Return a list of file suffixes based on the imp file type."""
return [suffix[0] for suffix in imp.get_suffixes()
@@ -204,10 +214,9 @@ class BuiltinImporter:
@classmethod
@set_package
@set_loader
+ @_requires_builtin
def load_module(cls, fullname):
"""Load a built-in module."""
- if fullname not in sys.builtin_module_names:
- raise ImportError("{0} is not a built-in module".format(fullname))
is_reload = fullname in sys.modules
try:
return imp.init_builtin(fullname)
@@ -216,6 +225,24 @@ class BuiltinImporter:
del sys.modules[fullname]
raise
+ @classmethod
+ @_requires_builtin
+ def get_code(cls, fullname):
+ """Return None as built-in modules do not have code objects."""
+ return None
+
+ @classmethod
+ @_requires_builtin
+ def get_source(cls, fullname):
+ """Return None as built-in modules do not have source code."""
+ return None
+
+ @classmethod
+ @_requires_builtin
+ def is_package(cls, fullname):
+ """Return None as built-in module are never packages."""
+ return False
+
class FrozenImporter:
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
index 6b00569..c9c542a 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -14,7 +14,6 @@ class Loader(metaclass=abc.ABCMeta):
"""Abstract method which when implemented should load a module."""
raise NotImplementedError
-Loader.register(machinery.BuiltinImporter)
Loader.register(machinery.FrozenImporter)
@@ -75,6 +74,8 @@ class InspectLoader(Loader):
module."""
return NotImplementedError
+InspectLoader.register(machinery.BuiltinImporter)
+
class PyLoader(_bootstrap.PyLoader, InspectLoader):
diff --git a/Lib/importlib/test/builtin/test_finder.py b/Lib/importlib/test/builtin/test_finder.py
index eacc36c..ef8e8fe 100644
--- a/Lib/importlib/test/builtin/test_finder.py
+++ b/Lib/importlib/test/builtin/test_finder.py
@@ -1,6 +1,7 @@
from importlib import machinery
from .. import abc
from .. import util
+from . import util as builtin_util
import sys
import unittest
@@ -9,13 +10,11 @@ class FinderTests(abc.FinderTests):
"""Test find_module() for built-in modules."""
- assert 'errno' in sys.builtin_module_names
- name = 'errno'
-
def test_module(self):
# Common case.
- with util.uncache(self.name):
- self.assert_(machinery.BuiltinImporter.find_module(self.name))
+ with util.uncache(builtin_util.NAME):
+ found = machinery.BuiltinImporter.find_module(builtin_util.NAME)
+ self.assert_(found)
def test_package(self):
# Built-in modules cannot be a package.
@@ -40,8 +39,9 @@ class FinderTests(abc.FinderTests):
def test_ignore_path(self):
# The value for 'path' should always trigger a failed import.
- with util.uncache(self.name):
- loader = machinery.BuiltinImporter.find_module(self.name, ['pkg'])
+ with util.uncache(builtin_util.NAME):
+ loader = machinery.BuiltinImporter.find_module(builtin_util.NAME,
+ ['pkg'])
self.assert_(loader is None)
diff --git a/Lib/importlib/test/builtin/test_loader.py b/Lib/importlib/test/builtin/test_loader.py
index 8a69d04..cfedb5d 100644
--- a/Lib/importlib/test/builtin/test_loader.py
+++ b/Lib/importlib/test/builtin/test_loader.py
@@ -2,6 +2,7 @@ import importlib
from importlib import machinery
from .. import abc
from .. import util
+from . import util as builtin_util
import sys
import types
@@ -12,9 +13,6 @@ class LoaderTests(abc.LoaderTests):
"""Test load_module() for built-in modules."""
- assert 'errno' in sys.builtin_module_names
- name = 'errno'
-
verification = {'__name__': 'errno', '__package__': '',
'__loader__': machinery.BuiltinImporter}
@@ -30,8 +28,8 @@ class LoaderTests(abc.LoaderTests):
def test_module(self):
# Common case.
- with util.uncache(self.name):
- module = self.load_module(self.name)
+ with util.uncache(builtin_util.NAME):
+ module = self.load_module(builtin_util.NAME)
self.verify(module)
def test_package(self):
@@ -48,9 +46,9 @@ class LoaderTests(abc.LoaderTests):
def test_module_reuse(self):
# Test that the same module is used in a reload.
- with util.uncache(self.name):
- module1 = self.load_module(self.name)
- module2 = self.load_module(self.name)
+ with util.uncache(builtin_util.NAME):
+ module1 = self.load_module(builtin_util.NAME)
+ module2 = self.load_module(builtin_util.NAME)
self.assert_(module1 is module2)
def test_unloadable(self):
@@ -65,9 +63,36 @@ class LoaderTests(abc.LoaderTests):
self.assertRaises(ImportError, self.load_module, 'importlib')
+class InspectLoaderTests(unittest.TestCase):
+
+ """Tests for InspectLoader methods for BuiltinImporter."""
+
+ def test_get_code(self):
+ # There is no code object.
+ result = machinery.BuiltinImporter.get_code(builtin_util.NAME)
+ self.assert_(result is None)
+
+ def test_get_source(self):
+ # There is no source.
+ result = machinery.BuiltinImporter.get_source(builtin_util.NAME)
+ self.assert_(result is None)
+
+ def test_is_package(self):
+ # Cannot be a package.
+ result = machinery.BuiltinImporter.is_package(builtin_util.NAME)
+ self.assert_(not result)
+
+ def test_not_builtin(self):
+ # Modules not built-in should raise ImportError.
+ for meth_name in ('get_code', 'get_source', 'is_package'):
+ method = getattr(machinery.BuiltinImporter, meth_name)
+ self.assertRaises(ImportError, method, builtin_util.BAD_NAME)
+
+
+
def test_main():
from test.support import run_unittest
- run_unittest(LoaderTests)
+ run_unittest(LoaderTests, InspectLoaderTests)
if __name__ == '__main__':
diff --git a/Lib/importlib/test/builtin/util.py b/Lib/importlib/test/builtin/util.py
new file mode 100644
index 0000000..5704699
--- /dev/null
+++ b/Lib/importlib/test/builtin/util.py
@@ -0,0 +1,7 @@
+import sys
+
+assert 'errno' in sys.builtin_module_names
+NAME = 'errno'
+
+assert 'importlib' not in sys.builtin_module_names
+BAD_NAME = 'importlib'