blob: 530b1a06dfb508b268aa89e0a5a11c609091a79e [file] [log] [blame]
Brett Cannon23cbd8a2009-01-18 00:24:28 +00001"""Test that sys.modules is used properly by import."""
Brett Cannonbcb26c52009-02-01 04:00:05 +00002from .. import util
Brett Cannond720b362009-02-01 04:28:04 +00003from . import util as import_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00004import sys
5from types import MethodType
6import unittest
7
8
9class UseCache(unittest.TestCase):
10
11 """When it comes to sys.modules, import prefers it over anything else.
12
13 Once a name has been resolved, sys.modules is checked to see if it contains
14 the module desired. If so, then it is returned [use cache]. If it is not
15 found, then the proper steps are taken to perform the import, but
16 sys.modules is still used to return the imported module (e.g., not what a
17 loader returns) [from cache on return]. This also applies to imports of
18 things contained within a package and thus get assigned as an attribute
19 [from cache to attribute] or pulled in thanks to a fromlist import
Brett Cannon4d75fc12009-08-30 03:47:36 +000020 [from cache for fromlist]. But if sys.modules contains None then
21 ImportError is raised [None in cache].
Brett Cannon23cbd8a2009-01-18 00:24:28 +000022
23 """
24 def test_using_cache(self):
25 # [use cache]
26 module_to_use = "some module found!"
Brett Cannon4d75fc12009-08-30 03:47:36 +000027 with util.uncache(module_to_use):
28 sys.modules['some_module'] = module_to_use
29 module = import_util.import_('some_module')
30 self.assertEqual(id(module_to_use), id(module))
31
32 def test_None_in_cache(self):
33 #[None in cache]
34 name = 'using_None'
35 with util.uncache(name):
36 sys.modules[name] = None
37 with self.assertRaises(ImportError):
38 import_util.import_(name)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000039
40 def create_mock(self, *names, return_=None):
Brett Cannonbcb26c52009-02-01 04:00:05 +000041 mock = util.mock_modules(*names)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000042 original_load = mock.load_module
43 def load_module(self, fullname):
44 original_load(fullname)
45 return return_
46 mock.load_module = MethodType(load_module, mock)
47 return mock
48
49 # __import__ inconsistent between loaders and built-in import when it comes
50 # to when to use the module in sys.modules and when not to.
Brett Cannond720b362009-02-01 04:28:04 +000051 @import_util.importlib_only
Brett Cannon23cbd8a2009-01-18 00:24:28 +000052 def test_using_cache_after_loader(self):
53 # [from cache on return]
54 with self.create_mock('module') as mock:
Brett Cannonbcb26c52009-02-01 04:00:05 +000055 with util.import_state(meta_path=[mock]):
Brett Cannond720b362009-02-01 04:28:04 +000056 module = import_util.import_('module')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000057 self.assertEquals(id(module), id(sys.modules['module']))
58
59 # See test_using_cache_after_loader() for reasoning.
Brett Cannond720b362009-02-01 04:28:04 +000060 @import_util.importlib_only
Brett Cannon23cbd8a2009-01-18 00:24:28 +000061 def test_using_cache_for_assigning_to_attribute(self):
62 # [from cache to attribute]
63 with self.create_mock('pkg.__init__', 'pkg.module') as importer:
Brett Cannonbcb26c52009-02-01 04:00:05 +000064 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000065 module = import_util.import_('pkg.module')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000066 self.assertTrue(hasattr(module, 'module'))
67 self.assertTrue(id(module.module), id(sys.modules['pkg.module']))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000068
69 # See test_using_cache_after_loader() for reasoning.
Brett Cannond720b362009-02-01 04:28:04 +000070 @import_util.importlib_only
Brett Cannon23cbd8a2009-01-18 00:24:28 +000071 def test_using_cache_for_fromlist(self):
72 # [from cache for fromlist]
73 with self.create_mock('pkg.__init__', 'pkg.module') as importer:
Brett Cannonbcb26c52009-02-01 04:00:05 +000074 with util.import_state(meta_path=[importer]):
Brett Cannond720b362009-02-01 04:28:04 +000075 module = import_util.import_('pkg', fromlist=['module'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000076 self.assertTrue(hasattr(module, 'module'))
Brett Cannon2c318a12009-02-07 01:15:27 +000077 self.assertEquals(id(module.module),
78 id(sys.modules['pkg.module']))
Brett Cannon23cbd8a2009-01-18 00:24:28 +000079
80
81def test_main():
82 from test.support import run_unittest
83 run_unittest(UseCache)
84
85if __name__ == '__main__':
86 test_main()