mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Issue #16803: test.test_importlib.frozen now runs both frozen and source code
This commit is contained in:
parent
8ea86509ab
commit
a3c6963467
2 changed files with 33 additions and 31 deletions
|
|
@ -1,15 +1,17 @@
|
||||||
from importlib import machinery
|
|
||||||
from .. import abc
|
from .. import abc
|
||||||
|
from .. import util
|
||||||
|
|
||||||
|
machinery = util.import_importlib('importlib.machinery')
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class FinderTests(unittest.TestCase, abc.FinderTests):
|
class FinderTests(abc.FinderTests):
|
||||||
|
|
||||||
"""Test finding frozen modules."""
|
"""Test finding frozen modules."""
|
||||||
|
|
||||||
def find(self, name, path=None):
|
def find(self, name, path=None):
|
||||||
finder = machinery.FrozenImporter
|
finder = self.machinery.FrozenImporter
|
||||||
return finder.find_module(name, path)
|
return finder.find_module(name, path)
|
||||||
|
|
||||||
def test_module(self):
|
def test_module(self):
|
||||||
|
|
@ -37,11 +39,9 @@ class FinderTests(unittest.TestCase, abc.FinderTests):
|
||||||
loader = self.find('<not real>')
|
loader = self.find('<not real>')
|
||||||
self.assertIsNone(loader)
|
self.assertIsNone(loader)
|
||||||
|
|
||||||
|
Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
|
||||||
def test_main():
|
machinery=machinery)
|
||||||
from test.support import run_unittest
|
|
||||||
run_unittest(FinderTests)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test_main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,21 @@
|
||||||
from .. import abc
|
from .. import abc
|
||||||
from .. import util
|
from .. import util
|
||||||
|
|
||||||
from importlib import machinery
|
machinery = util.import_importlib('importlib.machinery')
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from test.support import captured_stdout
|
from test.support import captured_stdout
|
||||||
import types
|
import types
|
||||||
|
|
||||||
|
|
||||||
class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
class LoaderTests(abc.LoaderTests):
|
||||||
|
|
||||||
def test_module(self):
|
def test_module(self):
|
||||||
with util.uncache('__hello__'), captured_stdout() as stdout:
|
with util.uncache('__hello__'), captured_stdout() as stdout:
|
||||||
module = machinery.FrozenImporter.load_module('__hello__')
|
module = self.machinery.FrozenImporter.load_module('__hello__')
|
||||||
check = {'__name__': '__hello__',
|
check = {'__name__': '__hello__',
|
||||||
'__package__': '',
|
'__package__': '',
|
||||||
'__loader__': machinery.FrozenImporter,
|
'__loader__': self.machinery.FrozenImporter,
|
||||||
}
|
}
|
||||||
for attr, value in check.items():
|
for attr, value in check.items():
|
||||||
self.assertEqual(getattr(module, attr), value)
|
self.assertEqual(getattr(module, attr), value)
|
||||||
|
|
@ -23,11 +24,11 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
||||||
|
|
||||||
def test_package(self):
|
def test_package(self):
|
||||||
with util.uncache('__phello__'), captured_stdout() as stdout:
|
with util.uncache('__phello__'), captured_stdout() as stdout:
|
||||||
module = machinery.FrozenImporter.load_module('__phello__')
|
module = self.machinery.FrozenImporter.load_module('__phello__')
|
||||||
check = {'__name__': '__phello__',
|
check = {'__name__': '__phello__',
|
||||||
'__package__': '__phello__',
|
'__package__': '__phello__',
|
||||||
'__path__': [],
|
'__path__': [],
|
||||||
'__loader__': machinery.FrozenImporter,
|
'__loader__': self.machinery.FrozenImporter,
|
||||||
}
|
}
|
||||||
for attr, value in check.items():
|
for attr, value in check.items():
|
||||||
attr_value = getattr(module, attr)
|
attr_value = getattr(module, attr)
|
||||||
|
|
@ -40,10 +41,10 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
||||||
def test_lacking_parent(self):
|
def test_lacking_parent(self):
|
||||||
with util.uncache('__phello__', '__phello__.spam'), \
|
with util.uncache('__phello__', '__phello__.spam'), \
|
||||||
captured_stdout() as stdout:
|
captured_stdout() as stdout:
|
||||||
module = machinery.FrozenImporter.load_module('__phello__.spam')
|
module = self.machinery.FrozenImporter.load_module('__phello__.spam')
|
||||||
check = {'__name__': '__phello__.spam',
|
check = {'__name__': '__phello__.spam',
|
||||||
'__package__': '__phello__',
|
'__package__': '__phello__',
|
||||||
'__loader__': machinery.FrozenImporter,
|
'__loader__': self.machinery.FrozenImporter,
|
||||||
}
|
}
|
||||||
for attr, value in check.items():
|
for attr, value in check.items():
|
||||||
attr_value = getattr(module, attr)
|
attr_value = getattr(module, attr)
|
||||||
|
|
@ -55,15 +56,15 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
||||||
|
|
||||||
def test_module_reuse(self):
|
def test_module_reuse(self):
|
||||||
with util.uncache('__hello__'), captured_stdout() as stdout:
|
with util.uncache('__hello__'), captured_stdout() as stdout:
|
||||||
module1 = machinery.FrozenImporter.load_module('__hello__')
|
module1 = self.machinery.FrozenImporter.load_module('__hello__')
|
||||||
module2 = machinery.FrozenImporter.load_module('__hello__')
|
module2 = self.machinery.FrozenImporter.load_module('__hello__')
|
||||||
self.assertIs(module1, module2)
|
self.assertIs(module1, module2)
|
||||||
self.assertEqual(stdout.getvalue(),
|
self.assertEqual(stdout.getvalue(),
|
||||||
'Hello world!\nHello world!\n')
|
'Hello world!\nHello world!\n')
|
||||||
|
|
||||||
def test_module_repr(self):
|
def test_module_repr(self):
|
||||||
with util.uncache('__hello__'), captured_stdout():
|
with util.uncache('__hello__'), captured_stdout():
|
||||||
module = machinery.FrozenImporter.load_module('__hello__')
|
module = self.machinery.FrozenImporter.load_module('__hello__')
|
||||||
self.assertEqual(repr(module),
|
self.assertEqual(repr(module),
|
||||||
"<module '__hello__' (frozen)>")
|
"<module '__hello__' (frozen)>")
|
||||||
|
|
||||||
|
|
@ -72,13 +73,16 @@ class LoaderTests(unittest.TestCase, abc.LoaderTests):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_unloadable(self):
|
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:
|
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')
|
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."""
|
"""Tests for the InspectLoader methods for FrozenImporter."""
|
||||||
|
|
||||||
|
|
@ -86,7 +90,7 @@ class InspectLoaderTests(unittest.TestCase):
|
||||||
# Make sure that the code object is good.
|
# Make sure that the code object is good.
|
||||||
name = '__hello__'
|
name = '__hello__'
|
||||||
with captured_stdout() as stdout:
|
with captured_stdout() as stdout:
|
||||||
code = machinery.FrozenImporter.get_code(name)
|
code = self.machinery.FrozenImporter.get_code(name)
|
||||||
mod = types.ModuleType(name)
|
mod = types.ModuleType(name)
|
||||||
exec(code, mod.__dict__)
|
exec(code, mod.__dict__)
|
||||||
self.assertTrue(hasattr(mod, 'initialized'))
|
self.assertTrue(hasattr(mod, 'initialized'))
|
||||||
|
|
@ -94,7 +98,7 @@ class InspectLoaderTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_source(self):
|
def test_get_source(self):
|
||||||
# Should always return None.
|
# Should always return None.
|
||||||
result = machinery.FrozenImporter.get_source('__hello__')
|
result = self.machinery.FrozenImporter.get_source('__hello__')
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
|
||||||
def test_is_package(self):
|
def test_is_package(self):
|
||||||
|
|
@ -102,22 +106,20 @@ class InspectLoaderTests(unittest.TestCase):
|
||||||
test_for = (('__hello__', False), ('__phello__', True),
|
test_for = (('__hello__', False), ('__phello__', True),
|
||||||
('__phello__.spam', False))
|
('__phello__.spam', False))
|
||||||
for name, is_package in test_for:
|
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)
|
self.assertEqual(bool(result), is_package)
|
||||||
|
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
# Raise ImportError for modules that are not frozen.
|
# Raise ImportError for modules that are not frozen.
|
||||||
for meth_name in ('get_code', 'get_source', 'is_package'):
|
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:
|
with self.assertRaises(ImportError) as cm:
|
||||||
method('importlib')
|
method('importlib')
|
||||||
self.assertEqual(cm.exception.name, 'importlib')
|
self.assertEqual(cm.exception.name, 'importlib')
|
||||||
|
|
||||||
|
Frozen_ILTests, Source_ILTests = util.test_both(InspectLoaderTests,
|
||||||
def test_main():
|
machinery=machinery)
|
||||||
from test.support import run_unittest
|
|
||||||
run_unittest(LoaderTests, InspectLoaderTests)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test_main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue