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