diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2013-11-22 16:05:39 (GMT) |
---|---|---|
committer | Eric Snow <ericsnowcurrently@gmail.com> | 2013-11-22 16:05:39 (GMT) |
commit | b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00 (patch) | |
tree | b38661db4903b7edc4042e7562b32720dd3687bf /Lib/test/test_importlib/extension | |
parent | 9e6097ebe7bb99a4a22b949ef4b1563b21ad7166 (diff) | |
download | cpython-b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00.zip cpython-b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00.tar.gz cpython-b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00.tar.bz2 |
Implement PEP 451 (ModuleSpec).
Diffstat (limited to 'Lib/test/test_importlib/extension')
-rw-r--r-- | Lib/test/test_importlib/extension/test_case_sensitivity.py | 2 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/test_finder.py | 20 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/test_loader.py | 81 |
3 files changed, 81 insertions, 22 deletions
diff --git a/Lib/test/test_importlib/extension/test_case_sensitivity.py b/Lib/test/test_importlib/extension/test_case_sensitivity.py index bb74321..bb2528e 100644 --- a/Lib/test/test_importlib/extension/test_case_sensitivity.py +++ b/Lib/test/test_importlib/extension/test_case_sensitivity.py @@ -9,6 +9,8 @@ from . import util as ext_util frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') +# XXX find_spec tests + @unittest.skipIf(ext_util.FILENAME is None, '_testcapi not available') @util.case_insensitive_tests class ExtensionModuleCaseSensitivityTest: diff --git a/Lib/test/test_importlib/extension/test_finder.py b/Lib/test/test_importlib/extension/test_finder.py index 10e78cc..d0c8bb0 100644 --- a/Lib/test/test_importlib/extension/test_finder.py +++ b/Lib/test/test_importlib/extension/test_finder.py @@ -6,6 +6,7 @@ machinery = test_util.import_importlib('importlib.machinery') import unittest +# XXX find_spec tests class FinderTests(abc.FinderTests): @@ -20,21 +21,14 @@ class FinderTests(abc.FinderTests): def test_module(self): self.assertTrue(self.find_module(util.NAME)) - def test_package(self): - # No extension module as an __init__ available for testing. - pass + # No extension module as an __init__ available for testing. + test_package = test_package_in_package = None - def test_module_in_package(self): - # No extension module in a package available for testing. - pass + # No extension module in a package available for testing. + test_module_in_package = None - def test_package_in_package(self): - # No extension module as an __init__ available for testing. - pass - - def test_package_over_module(self): - # Extension modules cannot be an __init__ for a package. - pass + # Extension modules cannot be an __init__ for a package. + test_package_over_module = None def test_failure(self): self.assertIsNone(self.find_module('asdfjkl;')) diff --git a/Lib/test/test_importlib/extension/test_loader.py b/Lib/test/test_importlib/extension/test_loader.py index 1e8afba..58bd09f 100644 --- a/Lib/test/test_importlib/extension/test_loader.py +++ b/Lib/test/test_importlib/extension/test_loader.py @@ -6,9 +6,75 @@ machinery = util.import_importlib('importlib.machinery') import os.path import sys +import types import unittest +class ExecModuleTests(abc.LoaderTests): + + """Test load_module() for extension modules.""" + + def setUp(self): + self.loader = self.machinery.ExtensionFileLoader(ext_util.NAME, + ext_util.FILEPATH) + + def exec_module(self, fullname): + module = types.ModuleType(fullname) + module.__spec__ = self.machinery.ModuleSpec(fullname, self.loader) + self.loader.exec_module(module) + return sys.modules[fullname] + + def test_exec_module_API(self): + with self.assertRaises(ImportError): + self.exec_module('XXX') + + + def test_module(self): + with util.uncache(ext_util.NAME): + module = self.exec_module(ext_util.NAME) + for attr, value in [('__name__', ext_util.NAME), + ('__file__', ext_util.FILEPATH), + ('__package__', '')]: + given = getattr(module, attr) + self.assertEqual(given, value, + '{}: {!r} != {!r}'.format(attr, given, value)) + self.assertIn(ext_util.NAME, sys.modules) + self.assertIsInstance(module.__loader__, + self.machinery.ExtensionFileLoader) + + # No extension module as __init__ available for testing. + test_package = None + + # No extension module in a package available for testing. + test_lacking_parent = None + + def test_module_reuse(self): + with util.uncache(ext_util.NAME): + module1 = self.exec_module(ext_util.NAME) + module2 = self.exec_module(ext_util.NAME) + self.assertIs(module1, module2) + + def test_state_after_failure(self): + # No easy way to trigger a failure after a successful import. + pass + + def test_unloadable(self): + name = 'asdfjkl;' + with self.assertRaises(ImportError) as cm: + self.exec_module(name) + self.assertEqual(cm.exception.name, name) + + def test_is_package(self): + self.assertFalse(self.loader.is_package(ext_util.NAME)) + for suffix in self.machinery.EXTENSION_SUFFIXES: + path = os.path.join('some', 'path', 'pkg', '__init__' + suffix) + loader = self.machinery.ExtensionFileLoader('pkg', path) + self.assertTrue(loader.is_package('pkg')) + +Frozen_ExecModuleTests, Source_ExecModuleTests = util.test_both( + ExecModuleTests, machinery=machinery) + + class LoaderTests(abc.LoaderTests): """Test load_module() for extension modules.""" @@ -39,13 +105,11 @@ class LoaderTests(abc.LoaderTests): self.assertIsInstance(module.__loader__, self.machinery.ExtensionFileLoader) - def test_package(self): - # No extension module as __init__ available for testing. - pass + # No extension module as __init__ available for testing. + test_package = None - def test_lacking_parent(self): - # No extension module in a package available for testing. - pass + # No extension module in a package available for testing. + test_lacking_parent = None def test_module_reuse(self): with util.uncache(ext_util.NAME): @@ -53,9 +117,8 @@ class LoaderTests(abc.LoaderTests): module2 = self.load_module(ext_util.NAME) self.assertIs(module1, module2) - def test_state_after_failure(self): - # No easy way to trigger a failure after a successful import. - pass + # No easy way to trigger a failure after a successful import. + test_state_after_failure = None def test_unloadable(self): name = 'asdfjkl;' |