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 util
|
||||
from . import util as builtin_util
|
||||
|
||||
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
class FinderTests(unittest.TestCase, abc.FinderTests):
|
||||
|
||||
class FinderTests(abc.FinderTests):
|
||||
|
||||
"""Test find_module() for built-in modules."""
|
||||
|
||||
def test_module(self):
|
||||
# Common case.
|
||||
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)
|
||||
|
||||
def test_package(self):
|
||||
|
@ -34,16 +36,18 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
|
|||
|
||||
def test_failure(self):
|
||||
assert 'importlib' not in sys.builtin_module_names
|
||||
loader = machinery.BuiltinImporter.find_module('importlib')
|
||||
loader = self.machinery.BuiltinImporter.find_module('importlib')
|
||||
self.assertIsNone(loader)
|
||||
|
||||
def test_ignore_path(self):
|
||||
# The value for 'path' should always trigger a failed import.
|
||||
with util.uncache(builtin_util.NAME):
|
||||
loader = machinery.BuiltinImporter.find_module(builtin_util.NAME,
|
||||
loader = self.machinery.BuiltinImporter.find_module(builtin_util.NAME,
|
||||
['pkg'])
|
||||
self.assertIsNone(loader)
|
||||
|
||||
Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
|
||||
machinery=[frozen_machinery, source_machinery])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -1,20 +1,21 @@
|
|||
import importlib
|
||||
from importlib import machinery
|
||||
from .. import abc
|
||||
from .. import util
|
||||
from . import util as builtin_util
|
||||
|
||||
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
|
||||
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
|
||||
|
||||
class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
||||
class LoaderTests(abc.LoaderTests):
|
||||
|
||||
"""Test load_module() for built-in modules."""
|
||||
|
||||
verification = {'__name__': 'errno', '__package__': '',
|
||||
'__loader__': machinery.BuiltinImporter}
|
||||
def setUp(self):
|
||||
self.verification = {'__name__': 'errno', '__package__': '',
|
||||
'__loader__': self.machinery.BuiltinImporter}
|
||||
|
||||
def verify(self, module):
|
||||
"""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.assertIn(module.__name__, sys.modules)
|
||||
|
||||
load_module = staticmethod(lambda name:
|
||||
machinery.BuiltinImporter.load_module(name))
|
||||
def load_module(self, name):
|
||||
return self.machinery.BuiltinImporter.load_module(name)
|
||||
|
||||
def test_module(self):
|
||||
# Common case.
|
||||
|
@ -61,45 +62,47 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
|||
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.
|
||||
assert hasattr(unittest, '__file__') # Not a built-in.
|
||||
with self.assertRaises(ImportError) as cm:
|
||||
self.load_module('importlib')
|
||||
self.assertEqual(cm.exception.name, 'importlib')
|
||||
self.load_module('unittest')
|
||||
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."""
|
||||
|
||||
def test_get_code(self):
|
||||
# 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)
|
||||
|
||||
def test_get_source(self):
|
||||
# There is no source.
|
||||
result = machinery.BuiltinImporter.get_source(builtin_util.NAME)
|
||||
result = self.machinery.BuiltinImporter.get_source(builtin_util.NAME)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_is_package(self):
|
||||
# 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)
|
||||
|
||||
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)
|
||||
method = getattr(self.machinery.BuiltinImporter, meth_name)
|
||||
with self.assertRaises(ImportError) as cm:
|
||||
method(builtin_util.BAD_NAME)
|
||||
self.assertRaises(builtin_util.BAD_NAME)
|
||||
|
||||
|
||||
|
||||
def test_main():
|
||||
from test.support import run_unittest
|
||||
run_unittest(LoaderTests, InspectLoaderTests)
|
||||
Frozen_InspectLoaderTests, Source_InspectLoaderTests = util.test_both(
|
||||
InspectLoaderTests,
|
||||
machinery=[frozen_machinery, source_machinery])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_main()
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue