Issue #16803: Have test.test_importlib.builtin test both frozen and
source importlib.
This commit is contained in:
parent
b3d6afff2b
commit
ffd33c290b
|
@ -1,19 +1,21 @@
|
||||||
from importlib import machinery
|
|
||||||
from .. import abc
|
from .. import abc
|
||||||
from .. import util
|
from .. import util
|
||||||
from . import util as builtin_util
|
from . import util as builtin_util
|
||||||
|
|
||||||
|
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
class FinderTests(unittest.TestCase, abc.FinderTests):
|
|
||||||
|
class FinderTests(abc.FinderTests):
|
||||||
|
|
||||||
"""Test find_module() for built-in modules."""
|
"""Test find_module() for built-in modules."""
|
||||||
|
|
||||||
def test_module(self):
|
def test_module(self):
|
||||||
# Common case.
|
# Common case.
|
||||||
with util.uncache(builtin_util.NAME):
|
with util.uncache(builtin_util.NAME):
|
||||||
found = machinery.BuiltinImporter.find_module(builtin_util.NAME)
|
found = self.machinery.BuiltinImporter.find_module(builtin_util.NAME)
|
||||||
self.assertTrue(found)
|
self.assertTrue(found)
|
||||||
|
|
||||||
def test_package(self):
|
def test_package(self):
|
||||||
|
@ -34,16 +36,18 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
|
||||||
|
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
assert 'importlib' not in sys.builtin_module_names
|
assert 'importlib' not in sys.builtin_module_names
|
||||||
loader = machinery.BuiltinImporter.find_module('importlib')
|
loader = self.machinery.BuiltinImporter.find_module('importlib')
|
||||||
self.assertIsNone(loader)
|
self.assertIsNone(loader)
|
||||||
|
|
||||||
def test_ignore_path(self):
|
def test_ignore_path(self):
|
||||||
# The value for 'path' should always trigger a failed import.
|
# The value for 'path' should always trigger a failed import.
|
||||||
with util.uncache(builtin_util.NAME):
|
with util.uncache(builtin_util.NAME):
|
||||||
loader = machinery.BuiltinImporter.find_module(builtin_util.NAME,
|
loader = self.machinery.BuiltinImporter.find_module(builtin_util.NAME,
|
||||||
['pkg'])
|
['pkg'])
|
||||||
self.assertIsNone(loader)
|
self.assertIsNone(loader)
|
||||||
|
|
||||||
|
Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
|
||||||
|
machinery=[frozen_machinery, source_machinery])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -1,20 +1,21 @@
|
||||||
import importlib
|
|
||||||
from importlib import machinery
|
|
||||||
from .. import abc
|
from .. import abc
|
||||||
from .. import util
|
from .. import util
|
||||||
from . import util as builtin_util
|
from . import util as builtin_util
|
||||||
|
|
||||||
|
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
class LoaderTests(abc.LoaderTests):
|
||||||
|
|
||||||
"""Test load_module() for built-in modules."""
|
"""Test load_module() for built-in modules."""
|
||||||
|
|
||||||
verification = {'__name__': 'errno', '__package__': '',
|
def setUp(self):
|
||||||
'__loader__': machinery.BuiltinImporter}
|
self.verification = {'__name__': 'errno', '__package__': '',
|
||||||
|
'__loader__': self.machinery.BuiltinImporter}
|
||||||
|
|
||||||
def verify(self, module):
|
def verify(self, module):
|
||||||
"""Verify that the module matches against what it should have."""
|
"""Verify that the module matches against what it should have."""
|
||||||
|
@ -23,8 +24,8 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
||||||
self.assertEqual(getattr(module, attr), value)
|
self.assertEqual(getattr(module, attr), value)
|
||||||
self.assertIn(module.__name__, sys.modules)
|
self.assertIn(module.__name__, sys.modules)
|
||||||
|
|
||||||
load_module = staticmethod(lambda name:
|
def load_module(self, name):
|
||||||
machinery.BuiltinImporter.load_module(name))
|
return self.machinery.BuiltinImporter.load_module(name)
|
||||||
|
|
||||||
def test_module(self):
|
def test_module(self):
|
||||||
# Common case.
|
# Common case.
|
||||||
|
@ -61,45 +62,47 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
||||||
def test_already_imported(self):
|
def test_already_imported(self):
|
||||||
# Using the name of a module already imported but not a built-in should
|
# Using the name of a module already imported but not a built-in should
|
||||||
# still fail.
|
# still fail.
|
||||||
assert hasattr(importlib, '__file__') # Not a built-in.
|
assert hasattr(unittest, '__file__') # Not a built-in.
|
||||||
with self.assertRaises(ImportError) as cm:
|
with self.assertRaises(ImportError) as cm:
|
||||||
self.load_module('importlib')
|
self.load_module('unittest')
|
||||||
self.assertEqual(cm.exception.name, 'importlib')
|
self.assertEqual(cm.exception.name, 'unittest')
|
||||||
|
|
||||||
|
|
||||||
class InspectLoaderTests(unittest.TestCase):
|
Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,
|
||||||
|
machinery=[frozen_machinery, source_machinery])
|
||||||
|
|
||||||
|
|
||||||
|
class InspectLoaderTests:
|
||||||
|
|
||||||
"""Tests for InspectLoader methods for BuiltinImporter."""
|
"""Tests for InspectLoader methods for BuiltinImporter."""
|
||||||
|
|
||||||
def test_get_code(self):
|
def test_get_code(self):
|
||||||
# There is no code object.
|
# There is no code object.
|
||||||
result = machinery.BuiltinImporter.get_code(builtin_util.NAME)
|
result = self.machinery.BuiltinImporter.get_code(builtin_util.NAME)
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
|
||||||
def test_get_source(self):
|
def test_get_source(self):
|
||||||
# There is no source.
|
# There is no source.
|
||||||
result = machinery.BuiltinImporter.get_source(builtin_util.NAME)
|
result = self.machinery.BuiltinImporter.get_source(builtin_util.NAME)
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
|
||||||
def test_is_package(self):
|
def test_is_package(self):
|
||||||
# Cannot be a package.
|
# Cannot be a package.
|
||||||
result = machinery.BuiltinImporter.is_package(builtin_util.NAME)
|
result = self.machinery.BuiltinImporter.is_package(builtin_util.NAME)
|
||||||
self.assertTrue(not result)
|
self.assertTrue(not result)
|
||||||
|
|
||||||
def test_not_builtin(self):
|
def test_not_builtin(self):
|
||||||
# Modules not built-in should raise ImportError.
|
# Modules not built-in should raise ImportError.
|
||||||
for meth_name in ('get_code', 'get_source', 'is_package'):
|
for meth_name in ('get_code', 'get_source', 'is_package'):
|
||||||
method = getattr(machinery.BuiltinImporter, meth_name)
|
method = getattr(self.machinery.BuiltinImporter, meth_name)
|
||||||
with self.assertRaises(ImportError) as cm:
|
with self.assertRaises(ImportError) as cm:
|
||||||
method(builtin_util.BAD_NAME)
|
method(builtin_util.BAD_NAME)
|
||||||
self.assertRaises(builtin_util.BAD_NAME)
|
self.assertRaises(builtin_util.BAD_NAME)
|
||||||
|
|
||||||
|
Frozen_InspectLoaderTests, Source_InspectLoaderTests = util.test_both(
|
||||||
|
InspectLoaderTests,
|
||||||
def test_main():
|
machinery=[frozen_machinery, source_machinery])
|
||||||
from test.support import run_unittest
|
|
||||||
run_unittest(LoaderTests, InspectLoaderTests)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test_main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue