Debian Codegraph @

Lib/test/test_importlib/import_/test_caching.py

raw
1 """Test that sys.modules is used properly by import."""
2 from test.test_importlib import util
3 from test.support.testcase import ExtraAssertions
4 import sys
5 from types import MethodType
6 import unittest
7 import warnings
8
9
10 class UseCache:
11
12 """When it comes to sys.modules, import prefers it over anything else.
13
14 Once a name has been resolved, sys.modules is checked to see if it contains
15 the module desired. If so, then it is returned [use cache]. If it is not
16 found, then the proper steps are taken to perform the import, but
17 sys.modules is still used to return the imported module (e.g., not what a
18 loader returns) [from cache on return]. This also applies to imports of
19 things contained within a package and thus get assigned as an attribute
20 [from cache to attribute] or pulled in thanks to a fromlist import
21 [from cache for fromlist]. But if sys.modules contains None then
22 ImportError is raised [None in cache].
23
24 """
25
26 def test_using_cache(self):
27 # [use cache]
28 module_to_use = "some module found!"
29 with util.uncache('some_module'):
30 sys.modules['some_module'] = module_to_use
31 module = self.__import__('some_module')
32 self.assertEqual(id(module_to_use), id(module))
33
34 def test_None_in_cache(self):
35 #[None in cache]
36 name = 'using_None'
37 with util.uncache(name):
38 sys.modules[name] = None
39 with self.assertRaises(ImportError) as cm:
40 self.__import__(name)
41 self.assertEqual(cm.exception.name, name)
42
43
44 (Frozen_UseCache,
45 Source_UseCache
46 ) = util.test_both(UseCache, __import__=util.__import__)
47
48
49 class ImportlibUseCache(UseCache, unittest.TestCase, ExtraAssertions):
50
51 # Pertinent only to PEP 302; exec_module() doesn't return a module.
52
53 __import__ = util.__import__['Source']
54
55 def create_mock(self, *names, return_=None):
56 mock = util.mock_spec(*names)
57 original_spec = mock.find_spec
58 def find_spec(self, fullname, path, target=None):
59 return original_spec(fullname)
60 mock.find_spec = MethodType(find_spec, mock)
61 return mock
62
63 # __import__ inconsistent between loaders and built-in import when it comes
64 # to when to use the module in sys.modules and when not to.
65 def test_using_cache_after_loader(self):
66 # [from cache on return]
67 with warnings.catch_warnings():
68 warnings.simplefilter("ignore", ImportWarning)
69 with self.create_mock('module') as mock:
70 with util.import_state(meta_path=[mock]):
71 module = self.__import__('module')
72 self.assertEqual(id(module), id(sys.modules['module']))
73
74 # See test_using_cache_after_loader() for reasoning.
75 def test_using_cache_for_assigning_to_attribute(self):
76 # [from cache to attribute]
77 with warnings.catch_warnings():
78 warnings.simplefilter("ignore", ImportWarning)
79 with self.create_mock('pkg.__init__', 'pkg.module') as importer:
80 with util.import_state(meta_path=[importer]):
81 module = self.__import__('pkg.module')
82 self.assertHasAttr(module, 'module')
83 self.assertEqual(id(module.module),
84 id(sys.modules['pkg.module']))
85
86 # See test_using_cache_after_loader() for reasoning.
87 def test_using_cache_for_fromlist(self):
88 # [from cache for fromlist]
89 with self.create_mock('pkg.__init__', 'pkg.module') as importer:
90 with util.import_state(meta_path=[importer]):
91 module = self.__import__('pkg', fromlist=['module'])
92 self.assertHasAttr(module, 'module')
93 self.assertEqual(id(module.module),
94 id(sys.modules['pkg.module']))
95
96
97 if __name__ == '__main__':
98 unittest.main()