Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 1 | from . import util |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 2 | import imp |
| 3 | import importlib |
Brett Cannon | 53089c6 | 2012-07-04 14:03:40 -0400 | [diff] [blame] | 4 | from importlib import machinery |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 5 | import sys |
Brett Cannon | 53089c6 | 2012-07-04 14:03:40 -0400 | [diff] [blame] | 6 | from test import support |
Brett Cannon | e7387b4 | 2013-02-01 14:43:59 -0500 | [diff] [blame^] | 7 | import types |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 8 | import unittest |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class ImportModuleTests(unittest.TestCase): |
| 12 | |
| 13 | """Test importlib.import_module.""" |
| 14 | |
| 15 | def test_module_import(self): |
| 16 | # Test importing a top-level module. |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 17 | with util.mock_modules('top_level') as mock: |
| 18 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 19 | module = importlib.import_module('top_level') |
| 20 | self.assertEqual(module.__name__, 'top_level') |
| 21 | |
| 22 | def test_absolute_package_import(self): |
| 23 | # Test importing a module from a package with an absolute name. |
| 24 | pkg_name = 'pkg' |
| 25 | pkg_long_name = '{0}.__init__'.format(pkg_name) |
| 26 | name = '{0}.mod'.format(pkg_name) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 27 | with util.mock_modules(pkg_long_name, name) as mock: |
| 28 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 29 | module = importlib.import_module(name) |
| 30 | self.assertEqual(module.__name__, name) |
| 31 | |
Brett Cannon | b5f03c6 | 2009-03-04 01:02:54 +0000 | [diff] [blame] | 32 | def test_shallow_relative_package_import(self): |
Brett Cannon | 2cf1585 | 2010-07-03 22:03:16 +0000 | [diff] [blame] | 33 | # Test importing a module from a package through a relative import. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 34 | pkg_name = 'pkg' |
| 35 | pkg_long_name = '{0}.__init__'.format(pkg_name) |
| 36 | module_name = 'mod' |
| 37 | absolute_name = '{0}.{1}'.format(pkg_name, module_name) |
| 38 | relative_name = '.{0}'.format(module_name) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 39 | with util.mock_modules(pkg_long_name, absolute_name) as mock: |
| 40 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 41 | importlib.import_module(pkg_name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 42 | module = importlib.import_module(relative_name, pkg_name) |
| 43 | self.assertEqual(module.__name__, absolute_name) |
| 44 | |
Brett Cannon | b5f03c6 | 2009-03-04 01:02:54 +0000 | [diff] [blame] | 45 | def test_deep_relative_package_import(self): |
| 46 | modules = ['a.__init__', 'a.b.__init__', 'a.c'] |
| 47 | with util.mock_modules(*modules) as mock: |
| 48 | with util.import_state(meta_path=[mock]): |
| 49 | importlib.import_module('a') |
| 50 | importlib.import_module('a.b') |
| 51 | module = importlib.import_module('..c', 'a.b') |
| 52 | self.assertEqual(module.__name__, 'a.c') |
| 53 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 54 | def test_absolute_import_with_package(self): |
| 55 | # Test importing a module from a package with an absolute name with |
| 56 | # the 'package' argument given. |
| 57 | pkg_name = 'pkg' |
| 58 | pkg_long_name = '{0}.__init__'.format(pkg_name) |
| 59 | name = '{0}.mod'.format(pkg_name) |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 60 | with util.mock_modules(pkg_long_name, name) as mock: |
| 61 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 2c318a1 | 2009-02-07 01:15:27 +0000 | [diff] [blame] | 62 | importlib.import_module(pkg_name) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 63 | module = importlib.import_module(name, pkg_name) |
| 64 | self.assertEqual(module.__name__, name) |
| 65 | |
| 66 | def test_relative_import_wo_package(self): |
| 67 | # Relative imports cannot happen without the 'package' argument being |
| 68 | # set. |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 69 | with self.assertRaises(TypeError): |
| 70 | importlib.import_module('.support') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 71 | |
| 72 | |
Meador Inge | 416f12d | 2011-12-14 22:23:46 -0600 | [diff] [blame] | 73 | def test_loaded_once(self): |
| 74 | # Issue #13591: Modules should only be loaded once when |
| 75 | # initializing the parent package attempts to import the |
| 76 | # module currently being imported. |
| 77 | b_load_count = 0 |
| 78 | def load_a(): |
| 79 | importlib.import_module('a.b') |
| 80 | def load_b(): |
| 81 | nonlocal b_load_count |
| 82 | b_load_count += 1 |
| 83 | code = {'a': load_a, 'a.b': load_b} |
| 84 | modules = ['a.__init__', 'a.b'] |
| 85 | with util.mock_modules(*modules, module_code=code) as mock: |
| 86 | with util.import_state(meta_path=[mock]): |
| 87 | importlib.import_module('a.b') |
| 88 | self.assertEqual(b_load_count, 1) |
| 89 | |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 90 | |
Brett Cannon | ee78a2b | 2012-05-12 17:43:17 -0400 | [diff] [blame] | 91 | class FindLoaderTests(unittest.TestCase): |
| 92 | |
| 93 | class FakeMetaFinder: |
| 94 | @staticmethod |
| 95 | def find_module(name, path=None): return name, path |
| 96 | |
| 97 | def test_sys_modules(self): |
| 98 | # If a module with __loader__ is in sys.modules, then return it. |
| 99 | name = 'some_mod' |
| 100 | with util.uncache(name): |
| 101 | module = imp.new_module(name) |
| 102 | loader = 'a loader!' |
| 103 | module.__loader__ = loader |
| 104 | sys.modules[name] = module |
| 105 | found = importlib.find_loader(name) |
| 106 | self.assertEqual(loader, found) |
| 107 | |
| 108 | def test_sys_modules_loader_is_None(self): |
| 109 | # If sys.modules[name].__loader__ is None, raise ValueError. |
| 110 | name = 'some_mod' |
| 111 | with util.uncache(name): |
| 112 | module = imp.new_module(name) |
| 113 | module.__loader__ = None |
| 114 | sys.modules[name] = module |
| 115 | with self.assertRaises(ValueError): |
| 116 | importlib.find_loader(name) |
| 117 | |
| 118 | def test_success(self): |
| 119 | # Return the loader found on sys.meta_path. |
| 120 | name = 'some_mod' |
| 121 | with util.uncache(name): |
| 122 | with util.import_state(meta_path=[self.FakeMetaFinder]): |
| 123 | self.assertEqual((name, None), importlib.find_loader(name)) |
| 124 | |
| 125 | def test_success_path(self): |
| 126 | # Searching on a path should work. |
| 127 | name = 'some_mod' |
| 128 | path = 'path to some place' |
| 129 | with util.uncache(name): |
| 130 | with util.import_state(meta_path=[self.FakeMetaFinder]): |
| 131 | self.assertEqual((name, path), |
| 132 | importlib.find_loader(name, path)) |
| 133 | |
| 134 | def test_nothing(self): |
| 135 | # None is returned upon failure to find a loader. |
| 136 | self.assertIsNone(importlib.find_loader('nevergoingtofindthismodule')) |
| 137 | |
| 138 | |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 139 | class InvalidateCacheTests(unittest.TestCase): |
| 140 | |
| 141 | def test_method_called(self): |
| 142 | # If defined the method should be called. |
| 143 | class InvalidatingNullFinder: |
| 144 | def __init__(self, *ignored): |
| 145 | self.called = False |
| 146 | def find_module(self, *args): |
| 147 | return None |
| 148 | def invalidate_caches(self): |
| 149 | self.called = True |
| 150 | |
| 151 | key = 'gobledeegook' |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 152 | meta_ins = InvalidatingNullFinder() |
| 153 | path_ins = InvalidatingNullFinder() |
| 154 | sys.meta_path.insert(0, meta_ins) |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 155 | self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key)) |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 156 | sys.path_importer_cache[key] = path_ins |
| 157 | self.addCleanup(lambda: sys.meta_path.remove(meta_ins)) |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 158 | importlib.invalidate_caches() |
Brett Cannon | f4dc920 | 2012-08-10 12:21:12 -0400 | [diff] [blame] | 159 | self.assertTrue(meta_ins.called) |
| 160 | self.assertTrue(path_ins.called) |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 161 | |
| 162 | def test_method_lacking(self): |
| 163 | # There should be no issues if the method is not defined. |
| 164 | key = 'gobbledeegook' |
| 165 | sys.path_importer_cache[key] = imp.NullImporter('abc') |
| 166 | self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key)) |
| 167 | importlib.invalidate_caches() # Shouldn't trigger an exception. |
| 168 | |
| 169 | |
Brett Cannon | 8e2f556 | 2012-07-02 14:53:10 -0400 | [diff] [blame] | 170 | class FrozenImportlibTests(unittest.TestCase): |
| 171 | |
| 172 | def test_no_frozen_importlib(self): |
| 173 | # Should be able to import w/o _frozen_importlib being defined. |
Brett Cannon | 53089c6 | 2012-07-04 14:03:40 -0400 | [diff] [blame] | 174 | module = support.import_fresh_module('importlib', blocked=['_frozen_importlib']) |
| 175 | self.assertFalse(isinstance(module.__loader__, |
| 176 | machinery.FrozenImporter)) |
Brett Cannon | 8e2f556 | 2012-07-02 14:53:10 -0400 | [diff] [blame] | 177 | |
| 178 | |
Brett Cannon | e7387b4 | 2013-02-01 14:43:59 -0500 | [diff] [blame^] | 179 | class StartupTests(unittest.TestCase): |
| 180 | |
| 181 | def test_everyone_has___loader__(self): |
| 182 | # Issue #17098: all modules should have __loader__ defined. |
| 183 | for name, module in sys.modules.items(): |
| 184 | if isinstance(module, types.ModuleType): |
| 185 | self.assertTrue(hasattr(module, '__loader__'), |
| 186 | '{!r} lacks a __loader__ attribute'.format(name)) |
| 187 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 188 | def test_main(): |
| 189 | from test.support import run_unittest |
Brett Cannon | 8e2f556 | 2012-07-02 14:53:10 -0400 | [diff] [blame] | 190 | run_unittest(ImportModuleTests, |
| 191 | FindLoaderTests, |
| 192 | InvalidateCacheTests, |
Brett Cannon | e7387b4 | 2013-02-01 14:43:59 -0500 | [diff] [blame^] | 193 | FrozenImportlibTests, |
| 194 | StartupTests) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 195 | |
| 196 | |
| 197 | if __name__ == '__main__': |
| 198 | test_main() |