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