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

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

#countcontent
1n/a"""Test that sys.modules is used properly by import."""
2n/afrom .. import util
3n/aimport sys
4n/afrom types import MethodType
5n/aimport unittest
6n/a
7n/a
8n/aclass UseCache:
9n/a
10n/a """When it comes to sys.modules, import prefers it over anything else.
11n/a
12n/a Once a name has been resolved, sys.modules is checked to see if it contains
13n/a the module desired. If so, then it is returned [use cache]. If it is not
14n/a found, then the proper steps are taken to perform the import, but
15n/a sys.modules is still used to return the imported module (e.g., not what a
16n/a loader returns) [from cache on return]. This also applies to imports of
17n/a things contained within a package and thus get assigned as an attribute
18n/a [from cache to attribute] or pulled in thanks to a fromlist import
19n/a [from cache for fromlist]. But if sys.modules contains None then
20n/a ImportError is raised [None in cache].
21n/a
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('some_module'):
28n/a sys.modules['some_module'] = module_to_use
29n/a module = self.__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 self.__import__(name)
39n/a self.assertEqual(cm.exception.name, name)
40n/a
41n/a
42n/a(Frozen_UseCache,
43n/a Source_UseCache
44n/a ) = util.test_both(UseCache, __import__=util.__import__)
45n/a
46n/a
47n/aclass ImportlibUseCache(UseCache, unittest.TestCase):
48n/a
49n/a # Pertinent only to PEP 302; exec_module() doesn't return a module.
50n/a
51n/a __import__ = util.__import__['Source']
52n/a
53n/a def create_mock(self, *names, return_=None):
54n/a mock = util.mock_modules(*names)
55n/a original_load = mock.load_module
56n/a def load_module(self, fullname):
57n/a original_load(fullname)
58n/a return return_
59n/a mock.load_module = MethodType(load_module, mock)
60n/a return mock
61n/a
62n/a # __import__ inconsistent between loaders and built-in import when it comes
63n/a # to when to use the module in sys.modules and when not to.
64n/a def test_using_cache_after_loader(self):
65n/a # [from cache on return]
66n/a with self.create_mock('module') as mock:
67n/a with util.import_state(meta_path=[mock]):
68n/a module = self.__import__('module')
69n/a self.assertEqual(id(module), id(sys.modules['module']))
70n/a
71n/a # See test_using_cache_after_loader() for reasoning.
72n/a def test_using_cache_for_assigning_to_attribute(self):
73n/a # [from cache to attribute]
74n/a with self.create_mock('pkg.__init__', 'pkg.module') as importer:
75n/a with util.import_state(meta_path=[importer]):
76n/a module = self.__import__('pkg.module')
77n/a self.assertTrue(hasattr(module, 'module'))
78n/a self.assertEqual(id(module.module),
79n/a id(sys.modules['pkg.module']))
80n/a
81n/a # See test_using_cache_after_loader() for reasoning.
82n/a def test_using_cache_for_fromlist(self):
83n/a # [from cache for fromlist]
84n/a with self.create_mock('pkg.__init__', 'pkg.module') as importer:
85n/a with util.import_state(meta_path=[importer]):
86n/a module = self.__import__('pkg', fromlist=['module'])
87n/a self.assertTrue(hasattr(module, 'module'))
88n/a self.assertEqual(id(module.module),
89n/a id(sys.modules['pkg.module']))
90n/a
91n/a
92n/aif __name__ == '__main__':
93n/a unittest.main()