ยปCore Development>Code coverage>Lib/importlib/test/import_/test_caching.py

Python code coverage for Lib/importlib/test/import_/test_caching.py

#countcontent
1n/a"""Test that sys.modules is used properly by import."""
2n/afrom .. import util
3n/afrom . import util as import_util
4n/aimport sys
5n/afrom types import MethodType
6n/aimport unittest
7n/a
8n/a
9n/aclass UseCache(unittest.TestCase):
10n/a
11n/a """When it comes to sys.modules, import prefers it over anything else.
12n/a
13n/a Once a name has been resolved, sys.modules is checked to see if it contains
14n/a the module desired. If so, then it is returned [use cache]. If it is not
15n/a found, then the proper steps are taken to perform the import, but
16n/a sys.modules is still used to return the imported module (e.g., not what a
17n/a loader returns) [from cache on return]. This also applies to imports of
18n/a things contained within a package and thus get assigned as an attribute
19n/a [from cache to attribute] or pulled in thanks to a fromlist import
20n/a [from cache for fromlist]. But if sys.modules contains None then
21n/a ImportError is raised [None in cache].
22n/a
23n/a """
24n/a def test_using_cache(self):
25n/a # [use cache]
26n/a module_to_use = "some module found!"
27n/a with util.uncache(module_to_use):
28n/a sys.modules['some_module'] = module_to_use
29n/a module = import_util.import_('some_module')
30n/a self.assertEqual(id(module_to_use), id(module))
31n/a
32n/a def test_None_in_cache(self):
33n/a #[None in cache]
34n/a name = 'using_None'
35n/a with util.uncache(name):
36n/a sys.modules[name] = None
37n/a with self.assertRaises(ImportError) as cm:
38n/a import_util.import_(name)
39n/a self.assertEqual(cm.exception.name, name)
40n/a
41n/a def create_mock(self, *names, return_=None):
42n/a mock = util.mock_modules(*names)
43n/a original_load = mock.load_module
44n/a def load_module(self, fullname):
45n/a original_load(fullname)
46n/a return return_
47n/a mock.load_module = MethodType(load_module, mock)
48n/a return mock
49n/a
50n/a # __import__ inconsistent between loaders and built-in import when it comes
51n/a # to when to use the module in sys.modules and when not to.
52n/a @import_util.importlib_only
53n/a def test_using_cache_after_loader(self):
54n/a # [from cache on return]
55n/a with self.create_mock('module') as mock:
56n/a with util.import_state(meta_path=[mock]):
57n/a module = import_util.import_('module')
58n/a self.assertEqual(id(module), id(sys.modules['module']))
59n/a
60n/a # See test_using_cache_after_loader() for reasoning.
61n/a @import_util.importlib_only
62n/a def test_using_cache_for_assigning_to_attribute(self):
63n/a # [from cache to attribute]
64n/a with self.create_mock('pkg.__init__', 'pkg.module') as importer:
65n/a with util.import_state(meta_path=[importer]):
66n/a module = import_util.import_('pkg.module')
67n/a self.assertTrue(hasattr(module, 'module'))
68n/a self.assertEqual(id(module.module),
69n/a id(sys.modules['pkg.module']))
70n/a
71n/a # See test_using_cache_after_loader() for reasoning.
72n/a @import_util.importlib_only
73n/a def test_using_cache_for_fromlist(self):
74n/a # [from cache for fromlist]
75n/a with self.create_mock('pkg.__init__', 'pkg.module') as importer:
76n/a with util.import_state(meta_path=[importer]):
77n/a module = import_util.import_('pkg', fromlist=['module'])
78n/a self.assertTrue(hasattr(module, 'module'))
79n/a self.assertEqual(id(module.module),
80n/a id(sys.modules['pkg.module']))
81n/a
82n/a
83n/adef test_main():
84n/a from test.support import run_unittest
85n/a run_unittest(UseCache)
86n/a
87n/aif __name__ == '__main__':
88n/a test_main()