Issue #16803: test.test_importlib.frozen now runs both frozen and source code

This commit is contained in:
Brett Cannon 2013-11-08 11:10:41 -05:00
parent 8ea86509ab
commit a3c6963467
2 changed files with 33 additions and 31 deletions

View File

@ -1,15 +1,17 @@
from importlib import machinery
from .. import abc
from .. import util
machinery = util.import_importlib('importlib.machinery')
import unittest
class FinderTests(unittest.TestCase, abc.FinderTests):
class FinderTests(abc.FinderTests):
"""Test finding frozen modules."""
def find(self, name, path=None):
finder = machinery.FrozenImporter
finder = self.machinery.FrozenImporter
return finder.find_module(name, path)
def test_module(self):
@ -37,11 +39,9 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
loader = self.find('<not real>')
self.assertIsNone(loader)
def test_main():
from test.support import run_unittest
run_unittest(FinderTests)
Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
machinery=machinery)
if __name__ == '__main__':
test_main()
unittest.main()

View File

@ -1,20 +1,21 @@
from .. import abc
from .. import util
from importlib import machinery
machinery = util.import_importlib('importlib.machinery')
import unittest
from test.support import captured_stdout
import types
class LoaderTests(unittest.TestCase, abc.LoaderTests):
class LoaderTests(abc.LoaderTests):
def test_module(self):
with util.uncache('__hello__'), captured_stdout() as stdout:
module = machinery.FrozenImporter.load_module('__hello__')
module = self.machinery.FrozenImporter.load_module('__hello__')
check = {'__name__': '__hello__',
'__package__': '',
'__loader__': machinery.FrozenImporter,
'__loader__': self.machinery.FrozenImporter,
}
for attr, value in check.items():
self.assertEqual(getattr(module, attr), value)
@ -23,11 +24,11 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
def test_package(self):
with util.uncache('__phello__'), captured_stdout() as stdout:
module = machinery.FrozenImporter.load_module('__phello__')
module = self.machinery.FrozenImporter.load_module('__phello__')
check = {'__name__': '__phello__',
'__package__': '__phello__',
'__path__': [],
'__loader__': machinery.FrozenImporter,
'__loader__': self.machinery.FrozenImporter,
}
for attr, value in check.items():
attr_value = getattr(module, attr)
@ -40,10 +41,10 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
def test_lacking_parent(self):
with util.uncache('__phello__', '__phello__.spam'), \
captured_stdout() as stdout:
module = machinery.FrozenImporter.load_module('__phello__.spam')
module = self.machinery.FrozenImporter.load_module('__phello__.spam')
check = {'__name__': '__phello__.spam',
'__package__': '__phello__',
'__loader__': machinery.FrozenImporter,
'__loader__': self.machinery.FrozenImporter,
}
for attr, value in check.items():
attr_value = getattr(module, attr)
@ -55,15 +56,15 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
def test_module_reuse(self):
with util.uncache('__hello__'), captured_stdout() as stdout:
module1 = machinery.FrozenImporter.load_module('__hello__')
module2 = machinery.FrozenImporter.load_module('__hello__')
module1 = self.machinery.FrozenImporter.load_module('__hello__')
module2 = self.machinery.FrozenImporter.load_module('__hello__')
self.assertIs(module1, module2)
self.assertEqual(stdout.getvalue(),
'Hello world!\nHello world!\n')
def test_module_repr(self):
with util.uncache('__hello__'), captured_stdout():
module = machinery.FrozenImporter.load_module('__hello__')
module = self.machinery.FrozenImporter.load_module('__hello__')
self.assertEqual(repr(module),
"<module '__hello__' (frozen)>")
@ -72,13 +73,16 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
pass
def test_unloadable(self):
assert machinery.FrozenImporter.find_module('_not_real') is None
assert self.machinery.FrozenImporter.find_module('_not_real') is None
with self.assertRaises(ImportError) as cm:
machinery.FrozenImporter.load_module('_not_real')
self.machinery.FrozenImporter.load_module('_not_real')
self.assertEqual(cm.exception.name, '_not_real')
Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,
machinery=machinery)
class InspectLoaderTests(unittest.TestCase):
class InspectLoaderTests:
"""Tests for the InspectLoader methods for FrozenImporter."""
@ -86,7 +90,7 @@ class InspectLoaderTests(unittest.TestCase):
# Make sure that the code object is good.
name = '__hello__'
with captured_stdout() as stdout:
code = machinery.FrozenImporter.get_code(name)
code = self.machinery.FrozenImporter.get_code(name)
mod = types.ModuleType(name)
exec(code, mod.__dict__)
self.assertTrue(hasattr(mod, 'initialized'))
@ -94,7 +98,7 @@ class InspectLoaderTests(unittest.TestCase):
def test_get_source(self):
# Should always return None.
result = machinery.FrozenImporter.get_source('__hello__')
result = self.machinery.FrozenImporter.get_source('__hello__')
self.assertIsNone(result)
def test_is_package(self):
@ -102,22 +106,20 @@ class InspectLoaderTests(unittest.TestCase):
test_for = (('__hello__', False), ('__phello__', True),
('__phello__.spam', False))
for name, is_package in test_for:
result = machinery.FrozenImporter.is_package(name)
result = self.machinery.FrozenImporter.is_package(name)
self.assertEqual(bool(result), is_package)
def test_failure(self):
# Raise ImportError for modules that are not frozen.
for meth_name in ('get_code', 'get_source', 'is_package'):
method = getattr(machinery.FrozenImporter, meth_name)
method = getattr(self.machinery.FrozenImporter, meth_name)
with self.assertRaises(ImportError) as cm:
method('importlib')
self.assertEqual(cm.exception.name, 'importlib')
def test_main():
from test.support import run_unittest
run_unittest(LoaderTests, InspectLoaderTests)
Frozen_ILTests, Source_ILTests = util.test_both(InspectLoaderTests,
machinery=machinery)
if __name__ == '__main__':
test_main()
unittest.main()