Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 1 | from importlib import util |
| 2 | from . import util as test_util |
| 3 | import imp |
| 4 | import sys |
| 5 | import types |
| 6 | import unittest |
| 7 | |
| 8 | |
| 9 | class ModuleForLoaderTests(unittest.TestCase): |
| 10 | |
| 11 | """Tests for importlib.util.module_for_loader.""" |
| 12 | |
| 13 | def return_module(self, name): |
| 14 | fxn = util.module_for_loader(lambda self, module: module) |
| 15 | return fxn(self, name) |
| 16 | |
| 17 | def raise_exception(self, name): |
| 18 | def to_wrap(self, module): |
| 19 | raise ImportError |
| 20 | fxn = util.module_for_loader(to_wrap) |
| 21 | try: |
| 22 | fxn(self, name) |
| 23 | except ImportError: |
| 24 | pass |
| 25 | |
| 26 | def test_new_module(self): |
| 27 | # Test that when no module exists in sys.modules a new module is |
| 28 | # created. |
| 29 | module_name = 'a.b.c' |
| 30 | with test_util.uncache(module_name): |
| 31 | module = self.return_module(module_name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 32 | self.assertIn(module_name, sys.modules) |
| 33 | self.assertIsInstance(module, types.ModuleType) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 34 | self.assertEqual(module.__name__, module_name) |
| 35 | |
| 36 | def test_reload(self): |
| 37 | # Test that a module is reused if already in sys.modules. |
| 38 | name = 'a.b.c' |
| 39 | module = imp.new_module('a.b.c') |
| 40 | with test_util.uncache(name): |
| 41 | sys.modules[name] = module |
| 42 | returned_module = self.return_module(name) |
Brett Cannon | c56b094 | 2010-06-21 02:49:35 +0000 | [diff] [blame] | 43 | self.assertIs(returned_module, sys.modules[name]) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 44 | |
| 45 | def test_new_module_failure(self): |
| 46 | # Test that a module is removed from sys.modules if added but an |
| 47 | # exception is raised. |
| 48 | name = 'a.b.c' |
| 49 | with test_util.uncache(name): |
| 50 | self.raise_exception(name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 51 | self.assertNotIn(name, sys.modules) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 52 | |
| 53 | def test_reload_failure(self): |
| 54 | # Test that a failure on reload leaves the module in-place. |
| 55 | name = 'a.b.c' |
| 56 | module = imp.new_module(name) |
| 57 | with test_util.uncache(name): |
| 58 | sys.modules[name] = module |
| 59 | self.raise_exception(name) |
Brett Cannon | c56b094 | 2010-06-21 02:49:35 +0000 | [diff] [blame] | 60 | self.assertIs(module, sys.modules[name]) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 61 | |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 62 | def test_decorator_attrs(self): |
| 63 | def fxn(self, module): pass |
| 64 | wrapped = util.module_for_loader(fxn) |
| 65 | self.assertEqual(wrapped.__name__, fxn.__name__) |
| 66 | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 67 | |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 68 | def test_false_module(self): |
| 69 | # If for some odd reason a module is considered false, still return it |
| 70 | # from sys.modules. |
| 71 | class FalseModule(types.ModuleType): |
| 72 | def __bool__(self): return False |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 73 | |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 74 | name = 'mod' |
| 75 | module = FalseModule(name) |
| 76 | with test_util.uncache(name): |
| 77 | self.assertFalse(module) |
| 78 | sys.modules[name] = module |
| 79 | given = self.return_module(name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 80 | self.assertIs(given, module) |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 81 | |
Brett Cannon | efad00d | 2012-04-27 17:27:14 -0400 | [diff] [blame] | 82 | def test_attributes_set(self): |
| 83 | # __name__, __loader__, and __package__ should be set (when |
| 84 | # is_package() is defined; undefined implicitly tested elsewhere). |
| 85 | class FakeLoader: |
| 86 | def __init__(self, is_package): |
| 87 | self._pkg = is_package |
| 88 | def is_package(self, name): |
| 89 | return self._pkg |
| 90 | @util.module_for_loader |
| 91 | def load_module(self, module): |
| 92 | return module |
| 93 | |
| 94 | name = 'pkg.mod' |
| 95 | with test_util.uncache(name): |
| 96 | loader = FakeLoader(False) |
| 97 | module = loader.load_module(name) |
| 98 | self.assertEqual(module.__name__, name) |
| 99 | self.assertIs(module.__loader__, loader) |
| 100 | self.assertEqual(module.__package__, 'pkg') |
| 101 | |
| 102 | name = 'pkg.sub' |
| 103 | with test_util.uncache(name): |
| 104 | loader = FakeLoader(True) |
| 105 | module = loader.load_module(name) |
| 106 | self.assertEqual(module.__name__, name) |
| 107 | self.assertIs(module.__loader__, loader) |
| 108 | self.assertEqual(module.__package__, name) |
| 109 | |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 110 | |
| 111 | class SetPackageTests(unittest.TestCase): |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 112 | |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 113 | """Tests for importlib.util.set_package.""" |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 114 | |
| 115 | def verify(self, module, expect): |
| 116 | """Verify the module has the expected value for __package__ after |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 117 | passing through set_package.""" |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 118 | fxn = lambda: module |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 119 | wrapped = util.set_package(fxn) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 120 | wrapped() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 121 | self.assertTrue(hasattr(module, '__package__')) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 122 | self.assertEqual(expect, module.__package__) |
| 123 | |
| 124 | def test_top_level(self): |
| 125 | # __package__ should be set to the empty string if a top-level module. |
| 126 | # Implicitly tests when package is set to None. |
| 127 | module = imp.new_module('module') |
| 128 | module.__package__ = None |
| 129 | self.verify(module, '') |
| 130 | |
| 131 | def test_package(self): |
| 132 | # Test setting __package__ for a package. |
| 133 | module = imp.new_module('pkg') |
| 134 | module.__path__ = ['<path>'] |
| 135 | module.__package__ = None |
| 136 | self.verify(module, 'pkg') |
| 137 | |
| 138 | def test_submodule(self): |
| 139 | # Test __package__ for a module in a package. |
| 140 | module = imp.new_module('pkg.mod') |
| 141 | module.__package__ = None |
| 142 | self.verify(module, 'pkg') |
| 143 | |
| 144 | def test_setting_if_missing(self): |
| 145 | # __package__ should be set if it is missing. |
| 146 | module = imp.new_module('mod') |
| 147 | if hasattr(module, '__package__'): |
| 148 | delattr(module, '__package__') |
| 149 | self.verify(module, '') |
| 150 | |
| 151 | def test_leaving_alone(self): |
| 152 | # If __package__ is set and not None then leave it alone. |
| 153 | for value in (True, False): |
| 154 | module = imp.new_module('mod') |
| 155 | module.__package__ = value |
| 156 | self.verify(module, value) |
| 157 | |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 158 | def test_decorator_attrs(self): |
| 159 | def fxn(module): pass |
| 160 | wrapped = util.set_package(fxn) |
| 161 | self.assertEqual(wrapped.__name__, fxn.__name__) |
| 162 | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 163 | |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 164 | |
| 165 | class ResolveNameTests(unittest.TestCase): |
| 166 | |
| 167 | """Tests importlib.util.resolve_name().""" |
| 168 | |
| 169 | def test_absolute(self): |
| 170 | # bacon |
| 171 | self.assertEqual('bacon', util.resolve_name('bacon', None)) |
| 172 | |
| 173 | def test_aboslute_within_package(self): |
| 174 | # bacon in spam |
| 175 | self.assertEqual('bacon', util.resolve_name('bacon', 'spam')) |
| 176 | |
| 177 | def test_no_package(self): |
| 178 | # .bacon in '' |
| 179 | with self.assertRaises(ValueError): |
| 180 | util.resolve_name('.bacon', '') |
| 181 | |
| 182 | def test_in_package(self): |
| 183 | # .bacon in spam |
| 184 | self.assertEqual('spam.eggs.bacon', |
| 185 | util.resolve_name('.bacon', 'spam.eggs')) |
| 186 | |
| 187 | def test_other_package(self): |
| 188 | # ..bacon in spam.bacon |
| 189 | self.assertEqual('spam.bacon', |
| 190 | util.resolve_name('..bacon', 'spam.eggs')) |
| 191 | |
| 192 | def test_escape(self): |
| 193 | # ..bacon in spam |
| 194 | with self.assertRaises(ValueError): |
| 195 | util.resolve_name('..bacon', 'spam') |
| 196 | |
| 197 | |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 198 | def test_main(): |
| 199 | from test import support |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 200 | support.run_unittest( |
| 201 | ModuleForLoaderTests, |
| 202 | SetPackageTests, |
| 203 | ResolveNameTests |
| 204 | ) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 205 | |
| 206 | |
| 207 | if __name__ == '__main__': |
| 208 | test_main() |