summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/test/builtin
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2009-01-18 00:24:28 (GMT)
committerBrett Cannon <bcannon@gmail.com>2009-01-18 00:24:28 (GMT)
commit23cbd8a6564df0ac3df339f91196c45f3e626d32 (patch)
treebef5602638a1567c28738aa359dae9865e04b6cd /Lib/importlib/test/builtin
parent458ad47a2c8045080a86eaaf33bb6cd174021891 (diff)
downloadcpython-23cbd8a6564df0ac3df339f91196c45f3e626d32.zip
cpython-23cbd8a6564df0ac3df339f91196c45f3e626d32.tar.gz
cpython-23cbd8a6564df0ac3df339f91196c45f3e626d32.tar.bz2
Add initial implementation of importlib. See the NOTES files for what is
planned for the package. There are no docs yet, but they are coming once the API for the first new function, importlib.import_module() is finalized.
Diffstat (limited to 'Lib/importlib/test/builtin')
-rw-r--r--Lib/importlib/test/builtin/__init__.py12
-rw-r--r--Lib/importlib/test/builtin/test_finder.py36
-rw-r--r--Lib/importlib/test/builtin/test_loader.py52
3 files changed, 100 insertions, 0 deletions
diff --git a/Lib/importlib/test/builtin/__init__.py b/Lib/importlib/test/builtin/__init__.py
new file mode 100644
index 0000000..31a3b5f
--- /dev/null
+++ b/Lib/importlib/test/builtin/__init__.py
@@ -0,0 +1,12 @@
+import importlib.test
+import os
+
+
+def test_suite():
+ directory = os.path.dirname(__file__)
+ return importlib.test.test_suite('importlib.test.builtin', directory)
+
+
+if __name__ == '__main__':
+ from test.support import run_unittest
+ run_unittest(test_suite())
diff --git a/Lib/importlib/test/builtin/test_finder.py b/Lib/importlib/test/builtin/test_finder.py
new file mode 100644
index 0000000..5262aaa
--- /dev/null
+++ b/Lib/importlib/test/builtin/test_finder.py
@@ -0,0 +1,36 @@
+import importlib
+from .. import support
+
+import sys
+import unittest
+
+class FinderTests(unittest.TestCase):
+
+ """Test find_module() for built-in modules."""
+
+ assert 'errno' in sys.builtin_module_names
+ name = 'errno'
+
+ find_module = staticmethod(lambda name, path=None:
+ importlib.BuiltinImporter().find_module(name, path))
+
+
+ def test_find_module(self):
+ # Common case.
+ with support.uncache(self.name):
+ self.assert_(self.find_module(self.name))
+
+ def test_ignore_path(self):
+ # The value for 'path' should always trigger a failed import.
+ with support.uncache(self.name):
+ self.assert_(self.find_module(self.name, ['pkg']) is None)
+
+
+
+def test_main():
+ from test.support import run_unittest
+ run_unittest(FinderTests)
+
+
+if __name__ == '__main__':
+ test_main()
diff --git a/Lib/importlib/test/builtin/test_loader.py b/Lib/importlib/test/builtin/test_loader.py
new file mode 100644
index 0000000..5aa3d79
--- /dev/null
+++ b/Lib/importlib/test/builtin/test_loader.py
@@ -0,0 +1,52 @@
+import importlib
+from .. import support
+
+import sys
+import types
+import unittest
+
+
+class LoaderTests(unittest.TestCase):
+
+ """Test load_module() for built-in modules."""
+
+ assert 'errno' in sys.builtin_module_names
+ name = 'errno'
+
+ verification = {'__name__': 'errno', '__package__': None}
+
+ def verify(self, module):
+ """Verify that the module matches against what it should have."""
+ self.assert_(isinstance(module, types.ModuleType))
+ for attr, value in self.verification.items():
+ self.assertEqual(getattr(module, attr), value)
+ self.assert_(module.__name__ in sys.modules)
+
+ load_module = staticmethod(lambda name:
+ importlib.BuiltinImporter().load_module(name))
+
+ def test_load_module(self):
+ # Common case.
+ with support.uncache(self.name):
+ module = self.load_module(self.name)
+ self.verify(module)
+
+ def test_nonexistent(self):
+ name = 'dssdsdfff'
+ assert name not in sys.builtin_module_names
+ self.assertRaises(ImportError, self.load_module, name)
+
+ def test_already_imported(self):
+ # Using the name of a module already imported but not a built-in should
+ # still fail.
+ assert hasattr(importlib, '__file__') # Not a built-in.
+ self.assertRaises(ImportError, self.load_module, 'importlib')
+
+
+def test_main():
+ from test.support import run_unittest
+ run_unittest(LoaderTests)
+
+
+if __name__ == '__main__':
+ test_main()