blob: efc8977fb46158b6c6c27282f40e2a8986f49318 [file] [log] [blame]
Brett Cannond2e7b332009-02-17 02:45:03 +00001from importlib import util
2from . import util as test_util
3import imp
4import sys
5import types
6import unittest
7
8
9class 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. Smithfaae3ad2012-06-27 15:26:26 -040032 self.assertIn(module_name, sys.modules)
33 self.assertIsInstance(module, types.ModuleType)
Brett Cannond2e7b332009-02-17 02:45:03 +000034 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 Cannonc56b0942010-06-21 02:49:35 +000043 self.assertIs(returned_module, sys.modules[name])
Brett Cannond2e7b332009-02-17 02:45:03 +000044
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. Smithfaae3ad2012-06-27 15:26:26 -040051 self.assertNotIn(name, sys.modules)
Brett Cannond2e7b332009-02-17 02:45:03 +000052
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 Cannonc56b0942010-06-21 02:49:35 +000060 self.assertIs(module, sys.modules[name])
Brett Cannond2e7b332009-02-17 02:45:03 +000061
Meador Inge96ff0842011-12-14 22:53:13 -060062 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 Cannond2e7b332009-02-17 02:45:03 +000067
Brett Cannon7bd329d2012-04-17 21:41:35 -040068 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 Cannon57b46f52009-03-02 14:38:26 +000073
Brett Cannon7bd329d2012-04-17 21:41:35 -040074 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. Smithfaae3ad2012-06-27 15:26:26 -040080 self.assertIs(given, module)
Brett Cannon7bd329d2012-04-17 21:41:35 -040081
Brett Cannonefad00d2012-04-27 17:27:14 -040082 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 Cannon7bd329d2012-04-17 21:41:35 -0400110
111class SetPackageTests(unittest.TestCase):
Brett Cannon57b46f52009-03-02 14:38:26 +0000112
Brett Cannon435aad82009-03-04 16:07:00 +0000113 """Tests for importlib.util.set_package."""
Brett Cannon57b46f52009-03-02 14:38:26 +0000114
115 def verify(self, module, expect):
116 """Verify the module has the expected value for __package__ after
Brett Cannon435aad82009-03-04 16:07:00 +0000117 passing through set_package."""
Brett Cannon57b46f52009-03-02 14:38:26 +0000118 fxn = lambda: module
Brett Cannon435aad82009-03-04 16:07:00 +0000119 wrapped = util.set_package(fxn)
Brett Cannon57b46f52009-03-02 14:38:26 +0000120 wrapped()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000121 self.assertTrue(hasattr(module, '__package__'))
Brett Cannon57b46f52009-03-02 14:38:26 +0000122 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 Inge96ff0842011-12-14 22:53:13 -0600158 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 Cannon57b46f52009-03-02 14:38:26 +0000163
Brett Cannond200bf52012-05-13 13:45:09 -0400164
165class 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 Cannond2e7b332009-02-17 02:45:03 +0000198def test_main():
199 from test import support
Brett Cannond200bf52012-05-13 13:45:09 -0400200 support.run_unittest(
201 ModuleForLoaderTests,
202 SetPackageTests,
203 ResolveNameTests
204 )
Brett Cannond2e7b332009-02-17 02:45:03 +0000205
206
207if __name__ == '__main__':
208 test_main()