blob: 69466b2e771ddd5331f3368576cac199f056364b [file] [log] [blame]
Brett Cannon2a17bde2014-05-30 14:55:29 -04001from . import util
2abc = util.import_importlib('importlib.abc')
3init = util.import_importlib('importlib')
4machinery = util.import_importlib('importlib.machinery')
5importlib_util = util.import_importlib('importlib.util')
Brett Cannonef888022013-06-15 18:39:21 -04006
Brett Cannona3c96152013-06-14 22:26:30 -04007import os
Brett Cannonf299abd2015-04-13 14:21:02 -04008import string
Brett Cannond2e7b332009-02-17 02:45:03 +00009import sys
Brett Cannona3687f02013-05-28 17:29:34 -040010from test import support
Brett Cannond2e7b332009-02-17 02:45:03 +000011import types
12import unittest
Brett Cannon0dbb4c72013-05-31 18:56:47 -040013import warnings
Brett Cannond2e7b332009-02-17 02:45:03 +000014
15
Brett Cannon13400492013-10-18 15:40:11 -040016class DecodeSourceBytesTests:
Brett Cannonf24fecd2013-06-16 18:37:53 -040017
18 source = "string ='ΓΌ'"
19
20 def test_ut8_default(self):
21 source_bytes = self.source.encode('utf-8')
Brett Cannon13400492013-10-18 15:40:11 -040022 self.assertEqual(self.util.decode_source(source_bytes), self.source)
Brett Cannonf24fecd2013-06-16 18:37:53 -040023
24 def test_specified_encoding(self):
25 source = '# coding=latin-1\n' + self.source
26 source_bytes = source.encode('latin-1')
27 assert source_bytes != source.encode('utf-8')
Brett Cannon13400492013-10-18 15:40:11 -040028 self.assertEqual(self.util.decode_source(source_bytes), source)
Brett Cannonf24fecd2013-06-16 18:37:53 -040029
30 def test_universal_newlines(self):
31 source = '\r\n'.join([self.source, self.source])
32 source_bytes = source.encode('utf-8')
Brett Cannon13400492013-10-18 15:40:11 -040033 self.assertEqual(self.util.decode_source(source_bytes),
Brett Cannonf24fecd2013-06-16 18:37:53 -040034 '\n'.join([self.source, self.source]))
35
Eric Snow3497c0b2014-05-16 11:40:40 -060036
37(Frozen_DecodeSourceBytesTests,
38 Source_DecodeSourceBytesTests
Brett Cannon2a17bde2014-05-30 14:55:29 -040039 ) = util.test_both(DecodeSourceBytesTests, util=importlib_util)
40
41
42class ModuleFromSpecTests:
43
44 def test_no_create_module(self):
Brett Cannon02d84542015-01-09 11:39:21 -050045 class Loader:
46 def exec_module(self, module):
47 pass
Brett Cannon2a17bde2014-05-30 14:55:29 -040048 spec = self.machinery.ModuleSpec('test', Loader())
Brett Cannon02d84542015-01-09 11:39:21 -050049 with warnings.catch_warnings(record=True) as w:
50 warnings.simplefilter('always')
51 module = self.util.module_from_spec(spec)
52 self.assertEqual(1, len(w))
53 self.assertTrue(issubclass(w[0].category, DeprecationWarning))
54 self.assertIn('create_module', str(w[0].message))
Brett Cannon2a17bde2014-05-30 14:55:29 -040055 self.assertIsInstance(module, types.ModuleType)
56 self.assertEqual(module.__name__, spec.name)
57
58 def test_create_module_returns_None(self):
59 class Loader(self.abc.Loader):
60 def create_module(self, spec):
61 return None
62 spec = self.machinery.ModuleSpec('test', Loader())
63 module = self.util.module_from_spec(spec)
64 self.assertIsInstance(module, types.ModuleType)
65 self.assertEqual(module.__name__, spec.name)
66
67 def test_create_module(self):
68 name = 'already set'
69 class CustomModule(types.ModuleType):
70 pass
71 class Loader(self.abc.Loader):
72 def create_module(self, spec):
73 module = CustomModule(spec.name)
74 module.__name__ = name
75 return module
76 spec = self.machinery.ModuleSpec('test', Loader())
77 module = self.util.module_from_spec(spec)
78 self.assertIsInstance(module, CustomModule)
79 self.assertEqual(module.__name__, name)
80
81 def test___name__(self):
82 spec = self.machinery.ModuleSpec('test', object())
83 module = self.util.module_from_spec(spec)
84 self.assertEqual(module.__name__, spec.name)
85
86 def test___spec__(self):
87 spec = self.machinery.ModuleSpec('test', object())
88 module = self.util.module_from_spec(spec)
89 self.assertEqual(module.__spec__, spec)
90
91 def test___loader__(self):
92 loader = object()
93 spec = self.machinery.ModuleSpec('test', loader)
94 module = self.util.module_from_spec(spec)
95 self.assertIs(module.__loader__, loader)
96
97 def test___package__(self):
98 spec = self.machinery.ModuleSpec('test.pkg', object())
99 module = self.util.module_from_spec(spec)
100 self.assertEqual(module.__package__, spec.parent)
101
102 def test___path__(self):
103 spec = self.machinery.ModuleSpec('test', object(), is_package=True)
104 module = self.util.module_from_spec(spec)
105 self.assertEqual(module.__path__, spec.submodule_search_locations)
106
107 def test___file__(self):
108 spec = self.machinery.ModuleSpec('test', object(), origin='some/path')
109 spec.has_location = True
110 module = self.util.module_from_spec(spec)
111 self.assertEqual(module.__file__, spec.origin)
112
113 def test___cached__(self):
114 spec = self.machinery.ModuleSpec('test', object())
115 spec.cached = 'some/path'
116 spec.has_location = True
117 module = self.util.module_from_spec(spec)
118 self.assertEqual(module.__cached__, spec.cached)
119
120(Frozen_ModuleFromSpecTests,
121 Source_ModuleFromSpecTests
122) = util.test_both(ModuleFromSpecTests, abc=abc, machinery=machinery,
123 util=importlib_util)
Brett Cannonf24fecd2013-06-16 18:37:53 -0400124
Brett Cannon13400492013-10-18 15:40:11 -0400125
Brett Cannon13400492013-10-18 15:40:11 -0400126class ModuleForLoaderTests:
Brett Cannond2e7b332009-02-17 02:45:03 +0000127
128 """Tests for importlib.util.module_for_loader."""
129
Brett Cannon13400492013-10-18 15:40:11 -0400130 @classmethod
131 def module_for_loader(cls, func):
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400132 with warnings.catch_warnings():
Eric Snow1500d492014-01-06 20:49:04 -0700133 warnings.simplefilter('ignore', DeprecationWarning)
Brett Cannon13400492013-10-18 15:40:11 -0400134 return cls.util.module_for_loader(func)
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400135
136 def test_warning(self):
137 # Should raise a PendingDeprecationWarning when used.
138 with warnings.catch_warnings():
Eric Snow1500d492014-01-06 20:49:04 -0700139 warnings.simplefilter('error', DeprecationWarning)
140 with self.assertRaises(DeprecationWarning):
Brett Cannon13400492013-10-18 15:40:11 -0400141 func = self.util.module_for_loader(lambda x: x)
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400142
Brett Cannond2e7b332009-02-17 02:45:03 +0000143 def return_module(self, name):
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400144 fxn = self.module_for_loader(lambda self, module: module)
Brett Cannond2e7b332009-02-17 02:45:03 +0000145 return fxn(self, name)
146
147 def raise_exception(self, name):
148 def to_wrap(self, module):
149 raise ImportError
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400150 fxn = self.module_for_loader(to_wrap)
Brett Cannond2e7b332009-02-17 02:45:03 +0000151 try:
152 fxn(self, name)
153 except ImportError:
154 pass
155
156 def test_new_module(self):
157 # Test that when no module exists in sys.modules a new module is
158 # created.
159 module_name = 'a.b.c'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400160 with util.uncache(module_name):
Brett Cannond2e7b332009-02-17 02:45:03 +0000161 module = self.return_module(module_name)
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400162 self.assertIn(module_name, sys.modules)
163 self.assertIsInstance(module, types.ModuleType)
Brett Cannond2e7b332009-02-17 02:45:03 +0000164 self.assertEqual(module.__name__, module_name)
165
166 def test_reload(self):
167 # Test that a module is reused if already in sys.modules.
Brett Cannon3dc48d62013-05-28 18:35:54 -0400168 class FakeLoader:
169 def is_package(self, name):
170 return True
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400171 @self.module_for_loader
Brett Cannon3dc48d62013-05-28 18:35:54 -0400172 def load_module(self, module):
173 return module
Brett Cannond2e7b332009-02-17 02:45:03 +0000174 name = 'a.b.c'
Brett Cannonef888022013-06-15 18:39:21 -0400175 module = types.ModuleType('a.b.c')
Brett Cannon3dc48d62013-05-28 18:35:54 -0400176 module.__loader__ = 42
177 module.__package__ = 42
Brett Cannon2a17bde2014-05-30 14:55:29 -0400178 with util.uncache(name):
Brett Cannond2e7b332009-02-17 02:45:03 +0000179 sys.modules[name] = module
Brett Cannon3dc48d62013-05-28 18:35:54 -0400180 loader = FakeLoader()
181 returned_module = loader.load_module(name)
Brett Cannonc56b0942010-06-21 02:49:35 +0000182 self.assertIs(returned_module, sys.modules[name])
Brett Cannon3dc48d62013-05-28 18:35:54 -0400183 self.assertEqual(module.__loader__, loader)
184 self.assertEqual(module.__package__, name)
Brett Cannond2e7b332009-02-17 02:45:03 +0000185
186 def test_new_module_failure(self):
187 # Test that a module is removed from sys.modules if added but an
188 # exception is raised.
189 name = 'a.b.c'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400190 with util.uncache(name):
Brett Cannond2e7b332009-02-17 02:45:03 +0000191 self.raise_exception(name)
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400192 self.assertNotIn(name, sys.modules)
Brett Cannond2e7b332009-02-17 02:45:03 +0000193
194 def test_reload_failure(self):
195 # Test that a failure on reload leaves the module in-place.
196 name = 'a.b.c'
Brett Cannonef888022013-06-15 18:39:21 -0400197 module = types.ModuleType(name)
Brett Cannon2a17bde2014-05-30 14:55:29 -0400198 with util.uncache(name):
Brett Cannond2e7b332009-02-17 02:45:03 +0000199 sys.modules[name] = module
200 self.raise_exception(name)
Brett Cannonc56b0942010-06-21 02:49:35 +0000201 self.assertIs(module, sys.modules[name])
Brett Cannond2e7b332009-02-17 02:45:03 +0000202
Meador Inge96ff0842011-12-14 22:53:13 -0600203 def test_decorator_attrs(self):
204 def fxn(self, module): pass
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400205 wrapped = self.module_for_loader(fxn)
Meador Inge96ff0842011-12-14 22:53:13 -0600206 self.assertEqual(wrapped.__name__, fxn.__name__)
207 self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
Brett Cannond2e7b332009-02-17 02:45:03 +0000208
Brett Cannon7bd329d2012-04-17 21:41:35 -0400209 def test_false_module(self):
210 # If for some odd reason a module is considered false, still return it
211 # from sys.modules.
212 class FalseModule(types.ModuleType):
213 def __bool__(self): return False
Brett Cannon57b46f52009-03-02 14:38:26 +0000214
Brett Cannon7bd329d2012-04-17 21:41:35 -0400215 name = 'mod'
216 module = FalseModule(name)
Brett Cannon2a17bde2014-05-30 14:55:29 -0400217 with util.uncache(name):
Brett Cannon7bd329d2012-04-17 21:41:35 -0400218 self.assertFalse(module)
219 sys.modules[name] = module
220 given = self.return_module(name)
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400221 self.assertIs(given, module)
Brett Cannon7bd329d2012-04-17 21:41:35 -0400222
Brett Cannonefad00d2012-04-27 17:27:14 -0400223 def test_attributes_set(self):
224 # __name__, __loader__, and __package__ should be set (when
225 # is_package() is defined; undefined implicitly tested elsewhere).
226 class FakeLoader:
227 def __init__(self, is_package):
228 self._pkg = is_package
229 def is_package(self, name):
230 return self._pkg
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400231 @self.module_for_loader
Brett Cannonefad00d2012-04-27 17:27:14 -0400232 def load_module(self, module):
233 return module
234
235 name = 'pkg.mod'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400236 with util.uncache(name):
Brett Cannonefad00d2012-04-27 17:27:14 -0400237 loader = FakeLoader(False)
238 module = loader.load_module(name)
239 self.assertEqual(module.__name__, name)
240 self.assertIs(module.__loader__, loader)
241 self.assertEqual(module.__package__, 'pkg')
242
243 name = 'pkg.sub'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400244 with util.uncache(name):
Brett Cannonefad00d2012-04-27 17:27:14 -0400245 loader = FakeLoader(True)
246 module = loader.load_module(name)
247 self.assertEqual(module.__name__, name)
248 self.assertIs(module.__loader__, loader)
249 self.assertEqual(module.__package__, name)
250
Eric Snow3497c0b2014-05-16 11:40:40 -0600251
252(Frozen_ModuleForLoaderTests,
253 Source_ModuleForLoaderTests
Brett Cannon2a17bde2014-05-30 14:55:29 -0400254 ) = util.test_both(ModuleForLoaderTests, util=importlib_util)
Brett Cannon7bd329d2012-04-17 21:41:35 -0400255
Brett Cannon13400492013-10-18 15:40:11 -0400256
257class SetPackageTests:
Brett Cannon57b46f52009-03-02 14:38:26 +0000258
Brett Cannon435aad82009-03-04 16:07:00 +0000259 """Tests for importlib.util.set_package."""
Brett Cannon57b46f52009-03-02 14:38:26 +0000260
261 def verify(self, module, expect):
262 """Verify the module has the expected value for __package__ after
Brett Cannon435aad82009-03-04 16:07:00 +0000263 passing through set_package."""
Brett Cannon57b46f52009-03-02 14:38:26 +0000264 fxn = lambda: module
Brett Cannon13400492013-10-18 15:40:11 -0400265 wrapped = self.util.set_package(fxn)
Eric Snow1500d492014-01-06 20:49:04 -0700266 with warnings.catch_warnings():
267 warnings.simplefilter('ignore', DeprecationWarning)
268 wrapped()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000269 self.assertTrue(hasattr(module, '__package__'))
Brett Cannon57b46f52009-03-02 14:38:26 +0000270 self.assertEqual(expect, module.__package__)
271
272 def test_top_level(self):
273 # __package__ should be set to the empty string if a top-level module.
274 # Implicitly tests when package is set to None.
Brett Cannonef888022013-06-15 18:39:21 -0400275 module = types.ModuleType('module')
Brett Cannon57b46f52009-03-02 14:38:26 +0000276 module.__package__ = None
277 self.verify(module, '')
278
279 def test_package(self):
280 # Test setting __package__ for a package.
Brett Cannonef888022013-06-15 18:39:21 -0400281 module = types.ModuleType('pkg')
Brett Cannon57b46f52009-03-02 14:38:26 +0000282 module.__path__ = ['<path>']
283 module.__package__ = None
284 self.verify(module, 'pkg')
285
286 def test_submodule(self):
287 # Test __package__ for a module in a package.
Brett Cannonef888022013-06-15 18:39:21 -0400288 module = types.ModuleType('pkg.mod')
Brett Cannon57b46f52009-03-02 14:38:26 +0000289 module.__package__ = None
290 self.verify(module, 'pkg')
291
292 def test_setting_if_missing(self):
293 # __package__ should be set if it is missing.
Brett Cannonef888022013-06-15 18:39:21 -0400294 module = types.ModuleType('mod')
Brett Cannon57b46f52009-03-02 14:38:26 +0000295 if hasattr(module, '__package__'):
296 delattr(module, '__package__')
297 self.verify(module, '')
298
299 def test_leaving_alone(self):
300 # If __package__ is set and not None then leave it alone.
301 for value in (True, False):
Brett Cannonef888022013-06-15 18:39:21 -0400302 module = types.ModuleType('mod')
Brett Cannon57b46f52009-03-02 14:38:26 +0000303 module.__package__ = value
304 self.verify(module, value)
305
Meador Inge96ff0842011-12-14 22:53:13 -0600306 def test_decorator_attrs(self):
307 def fxn(module): pass
Eric Snow1500d492014-01-06 20:49:04 -0700308 with warnings.catch_warnings():
309 warnings.simplefilter('ignore', DeprecationWarning)
310 wrapped = self.util.set_package(fxn)
Meador Inge96ff0842011-12-14 22:53:13 -0600311 self.assertEqual(wrapped.__name__, fxn.__name__)
312 self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
Brett Cannon57b46f52009-03-02 14:38:26 +0000313
Eric Snow3497c0b2014-05-16 11:40:40 -0600314
315(Frozen_SetPackageTests,
316 Source_SetPackageTests
Brett Cannon2a17bde2014-05-30 14:55:29 -0400317 ) = util.test_both(SetPackageTests, util=importlib_util)
Brett Cannond200bf52012-05-13 13:45:09 -0400318
Brett Cannon13400492013-10-18 15:40:11 -0400319
320class SetLoaderTests:
Brett Cannon4802bec2013-03-13 10:41:36 -0700321
322 """Tests importlib.util.set_loader()."""
323
Eric Snow3497c0b2014-05-16 11:40:40 -0600324 @property
325 def DummyLoader(self):
326 # Set DummyLoader on the class lazily.
327 class DummyLoader:
328 @self.util.set_loader
329 def load_module(self, module):
330 return self.module
331 self.__class__.DummyLoader = DummyLoader
332 return DummyLoader
Brett Cannon4802bec2013-03-13 10:41:36 -0700333
334 def test_no_attribute(self):
335 loader = self.DummyLoader()
Brett Cannonef888022013-06-15 18:39:21 -0400336 loader.module = types.ModuleType('blah')
Brett Cannon4802bec2013-03-13 10:41:36 -0700337 try:
338 del loader.module.__loader__
339 except AttributeError:
340 pass
Eric Snow1500d492014-01-06 20:49:04 -0700341 with warnings.catch_warnings():
342 warnings.simplefilter('ignore', DeprecationWarning)
343 self.assertEqual(loader, loader.load_module('blah').__loader__)
Brett Cannon4802bec2013-03-13 10:41:36 -0700344
345 def test_attribute_is_None(self):
346 loader = self.DummyLoader()
Brett Cannonef888022013-06-15 18:39:21 -0400347 loader.module = types.ModuleType('blah')
Brett Cannon4802bec2013-03-13 10:41:36 -0700348 loader.module.__loader__ = None
Eric Snow1500d492014-01-06 20:49:04 -0700349 with warnings.catch_warnings():
350 warnings.simplefilter('ignore', DeprecationWarning)
351 self.assertEqual(loader, loader.load_module('blah').__loader__)
Brett Cannon4802bec2013-03-13 10:41:36 -0700352
353 def test_not_reset(self):
354 loader = self.DummyLoader()
Brett Cannonef888022013-06-15 18:39:21 -0400355 loader.module = types.ModuleType('blah')
Brett Cannon4802bec2013-03-13 10:41:36 -0700356 loader.module.__loader__ = 42
Eric Snow1500d492014-01-06 20:49:04 -0700357 with warnings.catch_warnings():
358 warnings.simplefilter('ignore', DeprecationWarning)
359 self.assertEqual(42, loader.load_module('blah').__loader__)
Brett Cannon4802bec2013-03-13 10:41:36 -0700360
361
Eric Snow3497c0b2014-05-16 11:40:40 -0600362(Frozen_SetLoaderTests,
363 Source_SetLoaderTests
Brett Cannon2a17bde2014-05-30 14:55:29 -0400364 ) = util.test_both(SetLoaderTests, util=importlib_util)
Brett Cannon13400492013-10-18 15:40:11 -0400365
366
367class ResolveNameTests:
Brett Cannond200bf52012-05-13 13:45:09 -0400368
369 """Tests importlib.util.resolve_name()."""
370
371 def test_absolute(self):
372 # bacon
Brett Cannon13400492013-10-18 15:40:11 -0400373 self.assertEqual('bacon', self.util.resolve_name('bacon', None))
Brett Cannond200bf52012-05-13 13:45:09 -0400374
375 def test_aboslute_within_package(self):
376 # bacon in spam
Brett Cannon13400492013-10-18 15:40:11 -0400377 self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam'))
Brett Cannond200bf52012-05-13 13:45:09 -0400378
379 def test_no_package(self):
380 # .bacon in ''
381 with self.assertRaises(ValueError):
Brett Cannon13400492013-10-18 15:40:11 -0400382 self.util.resolve_name('.bacon', '')
Brett Cannond200bf52012-05-13 13:45:09 -0400383
384 def test_in_package(self):
385 # .bacon in spam
386 self.assertEqual('spam.eggs.bacon',
Brett Cannon13400492013-10-18 15:40:11 -0400387 self.util.resolve_name('.bacon', 'spam.eggs'))
Brett Cannond200bf52012-05-13 13:45:09 -0400388
389 def test_other_package(self):
390 # ..bacon in spam.bacon
391 self.assertEqual('spam.bacon',
Brett Cannon13400492013-10-18 15:40:11 -0400392 self.util.resolve_name('..bacon', 'spam.eggs'))
Brett Cannond200bf52012-05-13 13:45:09 -0400393
394 def test_escape(self):
395 # ..bacon in spam
396 with self.assertRaises(ValueError):
Brett Cannon13400492013-10-18 15:40:11 -0400397 self.util.resolve_name('..bacon', 'spam')
398
Eric Snow3497c0b2014-05-16 11:40:40 -0600399
400(Frozen_ResolveNameTests,
401 Source_ResolveNameTests
Brett Cannon2a17bde2014-05-30 14:55:29 -0400402 ) = util.test_both(ResolveNameTests, util=importlib_util)
Brett Cannond200bf52012-05-13 13:45:09 -0400403
404
Eric Snow6029e082014-01-25 15:32:46 -0700405class FindSpecTests:
406
407 class FakeMetaFinder:
408 @staticmethod
409 def find_spec(name, path=None, target=None): return name, path, target
410
411 def test_sys_modules(self):
412 name = 'some_mod'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400413 with util.uncache(name):
Eric Snow6029e082014-01-25 15:32:46 -0700414 module = types.ModuleType(name)
415 loader = 'a loader!'
416 spec = self.machinery.ModuleSpec(name, loader)
417 module.__loader__ = loader
418 module.__spec__ = spec
419 sys.modules[name] = module
420 found = self.util.find_spec(name)
421 self.assertEqual(found, spec)
422
423 def test_sys_modules_without___loader__(self):
424 name = 'some_mod'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400425 with util.uncache(name):
Eric Snow6029e082014-01-25 15:32:46 -0700426 module = types.ModuleType(name)
427 del module.__loader__
428 loader = 'a loader!'
429 spec = self.machinery.ModuleSpec(name, loader)
430 module.__spec__ = spec
431 sys.modules[name] = module
432 found = self.util.find_spec(name)
433 self.assertEqual(found, spec)
434
435 def test_sys_modules_spec_is_None(self):
436 name = 'some_mod'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400437 with util.uncache(name):
Eric Snow6029e082014-01-25 15:32:46 -0700438 module = types.ModuleType(name)
439 module.__spec__ = None
440 sys.modules[name] = module
441 with self.assertRaises(ValueError):
442 self.util.find_spec(name)
443
444 def test_sys_modules_loader_is_None(self):
445 name = 'some_mod'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400446 with util.uncache(name):
Eric Snow6029e082014-01-25 15:32:46 -0700447 module = types.ModuleType(name)
448 spec = self.machinery.ModuleSpec(name, None)
449 module.__spec__ = spec
450 sys.modules[name] = module
451 found = self.util.find_spec(name)
452 self.assertEqual(found, spec)
453
454 def test_sys_modules_spec_is_not_set(self):
455 name = 'some_mod'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400456 with util.uncache(name):
Eric Snow6029e082014-01-25 15:32:46 -0700457 module = types.ModuleType(name)
458 try:
459 del module.__spec__
460 except AttributeError:
461 pass
462 sys.modules[name] = module
463 with self.assertRaises(ValueError):
464 self.util.find_spec(name)
465
466 def test_success(self):
467 name = 'some_mod'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400468 with util.uncache(name):
469 with util.import_state(meta_path=[self.FakeMetaFinder]):
Eric Snow6029e082014-01-25 15:32:46 -0700470 self.assertEqual((name, None, None),
471 self.util.find_spec(name))
472
Eric Snow6029e082014-01-25 15:32:46 -0700473 def test_nothing(self):
474 # None is returned upon failure to find a loader.
475 self.assertIsNone(self.util.find_spec('nevergoingtofindthismodule'))
476
477 def test_find_submodule(self):
478 name = 'spam'
479 subname = 'ham'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400480 with util.temp_module(name, pkg=True) as pkg_dir:
481 fullname, _ = util.submodule(name, subname, pkg_dir)
Eric Snow6029e082014-01-25 15:32:46 -0700482 spec = self.util.find_spec(fullname)
483 self.assertIsNot(spec, None)
484 self.assertIn(name, sorted(sys.modules))
485 self.assertNotIn(fullname, sorted(sys.modules))
486 # Ensure successive calls behave the same.
487 spec_again = self.util.find_spec(fullname)
488 self.assertEqual(spec_again, spec)
489
490 def test_find_submodule_parent_already_imported(self):
491 name = 'spam'
492 subname = 'ham'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400493 with util.temp_module(name, pkg=True) as pkg_dir:
Eric Snow6029e082014-01-25 15:32:46 -0700494 self.init.import_module(name)
Brett Cannon2a17bde2014-05-30 14:55:29 -0400495 fullname, _ = util.submodule(name, subname, pkg_dir)
Eric Snow6029e082014-01-25 15:32:46 -0700496 spec = self.util.find_spec(fullname)
497 self.assertIsNot(spec, None)
498 self.assertIn(name, sorted(sys.modules))
499 self.assertNotIn(fullname, sorted(sys.modules))
500 # Ensure successive calls behave the same.
501 spec_again = self.util.find_spec(fullname)
502 self.assertEqual(spec_again, spec)
503
504 def test_find_relative_module(self):
505 name = 'spam'
506 subname = 'ham'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400507 with util.temp_module(name, pkg=True) as pkg_dir:
508 fullname, _ = util.submodule(name, subname, pkg_dir)
Eric Snow6029e082014-01-25 15:32:46 -0700509 relname = '.' + subname
510 spec = self.util.find_spec(relname, name)
511 self.assertIsNot(spec, None)
512 self.assertIn(name, sorted(sys.modules))
513 self.assertNotIn(fullname, sorted(sys.modules))
514 # Ensure successive calls behave the same.
515 spec_again = self.util.find_spec(fullname)
516 self.assertEqual(spec_again, spec)
517
518 def test_find_relative_module_missing_package(self):
519 name = 'spam'
520 subname = 'ham'
Brett Cannon2a17bde2014-05-30 14:55:29 -0400521 with util.temp_module(name, pkg=True) as pkg_dir:
522 fullname, _ = util.submodule(name, subname, pkg_dir)
Eric Snow6029e082014-01-25 15:32:46 -0700523 relname = '.' + subname
524 with self.assertRaises(ValueError):
525 self.util.find_spec(relname)
526 self.assertNotIn(name, sorted(sys.modules))
527 self.assertNotIn(fullname, sorted(sys.modules))
528
529
Eric Snow3497c0b2014-05-16 11:40:40 -0600530(Frozen_FindSpecTests,
531 Source_FindSpecTests
Brett Cannon2a17bde2014-05-30 14:55:29 -0400532 ) = util.test_both(FindSpecTests, init=init, util=importlib_util,
Eric Snow3497c0b2014-05-16 11:40:40 -0600533 machinery=machinery)
Eric Snow6029e082014-01-25 15:32:46 -0700534
535
Brett Cannon13400492013-10-18 15:40:11 -0400536class MagicNumberTests:
Brett Cannon05a647d2013-06-14 19:02:34 -0400537
538 def test_length(self):
539 # Should be 4 bytes.
Brett Cannon13400492013-10-18 15:40:11 -0400540 self.assertEqual(len(self.util.MAGIC_NUMBER), 4)
Brett Cannon05a647d2013-06-14 19:02:34 -0400541
542 def test_incorporates_rn(self):
543 # The magic number uses \r\n to come out wrong when splitting on lines.
Brett Cannon13400492013-10-18 15:40:11 -0400544 self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))
545
Eric Snow3497c0b2014-05-16 11:40:40 -0600546
547(Frozen_MagicNumberTests,
548 Source_MagicNumberTests
Brett Cannon2a17bde2014-05-30 14:55:29 -0400549 ) = util.test_both(MagicNumberTests, util=importlib_util)
Brett Cannon05a647d2013-06-14 19:02:34 -0400550
551
Brett Cannon13400492013-10-18 15:40:11 -0400552class PEP3147Tests:
Brett Cannona3c96152013-06-14 22:26:30 -0400553
Brett Cannon13400492013-10-18 15:40:11 -0400554 """Tests of PEP 3147-related functions: cache_from_source and source_from_cache."""
Brett Cannona3c96152013-06-14 22:26:30 -0400555
Brett Cannonef888022013-06-15 18:39:21 -0400556 tag = sys.implementation.cache_tag
Brett Cannona3c96152013-06-14 22:26:30 -0400557
558 @unittest.skipUnless(sys.implementation.cache_tag is not None,
559 'requires sys.implementation.cache_tag not be None')
560 def test_cache_from_source(self):
561 # Given the path to a .py file, return the path to its PEP 3147
562 # defined .pyc file (i.e. under __pycache__).
563 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
564 expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
565 'qux.{}.pyc'.format(self.tag))
Brett Cannonf299abd2015-04-13 14:21:02 -0400566 self.assertEqual(self.util.cache_from_source(path, optimization=''),
567 expect)
Brett Cannona3c96152013-06-14 22:26:30 -0400568
569 def test_cache_from_source_no_cache_tag(self):
570 # No cache tag means NotImplementedError.
571 with support.swap_attr(sys.implementation, 'cache_tag', None):
572 with self.assertRaises(NotImplementedError):
Brett Cannon13400492013-10-18 15:40:11 -0400573 self.util.cache_from_source('whatever.py')
Brett Cannona3c96152013-06-14 22:26:30 -0400574
575 def test_cache_from_source_no_dot(self):
576 # Directory with a dot, filename without dot.
577 path = os.path.join('foo.bar', 'file')
578 expect = os.path.join('foo.bar', '__pycache__',
579 'file{}.pyc'.format(self.tag))
Brett Cannonf299abd2015-04-13 14:21:02 -0400580 self.assertEqual(self.util.cache_from_source(path, optimization=''),
581 expect)
Brett Cannona3c96152013-06-14 22:26:30 -0400582
Brett Cannonf299abd2015-04-13 14:21:02 -0400583 def test_cache_from_source_debug_override(self):
584 # Given the path to a .py file, return the path to its PEP 3147/PEP 488
585 # defined .pyc file (i.e. under __pycache__).
Brett Cannona3c96152013-06-14 22:26:30 -0400586 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
Brett Cannonf299abd2015-04-13 14:21:02 -0400587 with warnings.catch_warnings():
588 warnings.simplefilter('ignore')
589 self.assertEqual(self.util.cache_from_source(path, False),
590 self.util.cache_from_source(path, optimization=1))
591 self.assertEqual(self.util.cache_from_source(path, True),
592 self.util.cache_from_source(path, optimization=''))
593 with warnings.catch_warnings():
594 warnings.simplefilter('error')
595 with self.assertRaises(DeprecationWarning):
596 self.util.cache_from_source(path, False)
597 with self.assertRaises(DeprecationWarning):
598 self.util.cache_from_source(path, True)
Brett Cannona3c96152013-06-14 22:26:30 -0400599
600 def test_cache_from_source_cwd(self):
601 path = 'foo.py'
602 expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
Brett Cannonf299abd2015-04-13 14:21:02 -0400603 self.assertEqual(self.util.cache_from_source(path, optimization=''),
604 expect)
Brett Cannona3c96152013-06-14 22:26:30 -0400605
606 def test_cache_from_source_override(self):
607 # When debug_override is not None, it can be any true-ish or false-ish
608 # value.
609 path = os.path.join('foo', 'bar', 'baz.py')
Brett Cannona3c96152013-06-14 22:26:30 -0400610 # However if the bool-ishness can't be determined, the exception
611 # propagates.
612 class Bearish:
613 def __bool__(self): raise RuntimeError
Brett Cannonf299abd2015-04-13 14:21:02 -0400614 with warnings.catch_warnings():
615 warnings.simplefilter('ignore')
616 self.assertEqual(self.util.cache_from_source(path, []),
617 self.util.cache_from_source(path, optimization=1))
618 self.assertEqual(self.util.cache_from_source(path, [17]),
619 self.util.cache_from_source(path, optimization=''))
620 with self.assertRaises(RuntimeError):
621 self.util.cache_from_source('/foo/bar/baz.py', Bearish())
622
623
624 def test_cache_from_source_optimization_empty_string(self):
625 # Setting 'optimization' to '' leads to no optimization tag (PEP 488).
626 path = 'foo.py'
627 expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
628 self.assertEqual(self.util.cache_from_source(path, optimization=''),
629 expect)
630
631 def test_cache_from_source_optimization_None(self):
632 # Setting 'optimization' to None uses the interpreter's optimization.
633 # (PEP 488)
634 path = 'foo.py'
635 optimization_level = sys.flags.optimize
636 almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
637 if optimization_level == 0:
638 expect = almost_expect + '.pyc'
639 elif optimization_level <= 2:
640 expect = almost_expect + '.opt-{}.pyc'.format(optimization_level)
641 else:
642 msg = '{!r} is a non-standard optimization level'.format(optimization_level)
643 self.skipTest(msg)
644 self.assertEqual(self.util.cache_from_source(path, optimization=None),
645 expect)
646
647 def test_cache_from_source_optimization_set(self):
648 # The 'optimization' parameter accepts anything that has a string repr
649 # that passes str.alnum().
650 path = 'foo.py'
651 valid_characters = string.ascii_letters + string.digits
652 almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
653 got = self.util.cache_from_source(path, optimization=valid_characters)
654 # Test all valid characters are accepted.
655 self.assertEqual(got,
656 almost_expect + '.opt-{}.pyc'.format(valid_characters))
657 # str() should be called on argument.
658 self.assertEqual(self.util.cache_from_source(path, optimization=42),
659 almost_expect + '.opt-42.pyc')
660 # Invalid characters raise ValueError.
661 with self.assertRaises(ValueError):
662 self.util.cache_from_source(path, optimization='path/is/bad')
663
664 def test_cache_from_source_debug_override_optimization_both_set(self):
665 # Can only set one of the optimization-related parameters.
666 with warnings.catch_warnings():
667 warnings.simplefilter('ignore')
668 with self.assertRaises(TypeError):
669 self.util.cache_from_source('foo.py', False, optimization='')
Brett Cannona3c96152013-06-14 22:26:30 -0400670
671 @unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
672 'test meaningful only where os.altsep is defined')
673 def test_sep_altsep_and_sep_cache_from_source(self):
674 # Windows path and PEP 3147 where sep is right of altsep.
675 self.assertEqual(
Brett Cannonf299abd2015-04-13 14:21:02 -0400676 self.util.cache_from_source('\\foo\\bar\\baz/qux.py', optimization=''),
Brett Cannona3c96152013-06-14 22:26:30 -0400677 '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
678
679 @unittest.skipUnless(sys.implementation.cache_tag is not None,
680 'requires sys.implementation.cache_tag to not be '
681 'None')
682 def test_source_from_cache(self):
683 # Given the path to a PEP 3147 defined .pyc file, return the path to
684 # its source. This tests the good path.
685 path = os.path.join('foo', 'bar', 'baz', '__pycache__',
686 'qux.{}.pyc'.format(self.tag))
687 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
Brett Cannon13400492013-10-18 15:40:11 -0400688 self.assertEqual(self.util.source_from_cache(path), expect)
Brett Cannona3c96152013-06-14 22:26:30 -0400689
690 def test_source_from_cache_no_cache_tag(self):
691 # If sys.implementation.cache_tag is None, raise NotImplementedError.
692 path = os.path.join('blah', '__pycache__', 'whatever.pyc')
693 with support.swap_attr(sys.implementation, 'cache_tag', None):
694 with self.assertRaises(NotImplementedError):
Brett Cannon13400492013-10-18 15:40:11 -0400695 self.util.source_from_cache(path)
Brett Cannona3c96152013-06-14 22:26:30 -0400696
697 def test_source_from_cache_bad_path(self):
698 # When the path to a pyc file is not in PEP 3147 format, a ValueError
699 # is raised.
700 self.assertRaises(
Brett Cannon13400492013-10-18 15:40:11 -0400701 ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')
Brett Cannona3c96152013-06-14 22:26:30 -0400702
703 def test_source_from_cache_no_slash(self):
704 # No slashes at all in path -> ValueError
705 self.assertRaises(
Brett Cannon13400492013-10-18 15:40:11 -0400706 ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')
Brett Cannona3c96152013-06-14 22:26:30 -0400707
708 def test_source_from_cache_too_few_dots(self):
709 # Too few dots in final path component -> ValueError
710 self.assertRaises(
Brett Cannon13400492013-10-18 15:40:11 -0400711 ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')
Brett Cannona3c96152013-06-14 22:26:30 -0400712
713 def test_source_from_cache_too_many_dots(self):
Brett Cannonf299abd2015-04-13 14:21:02 -0400714 with self.assertRaises(ValueError):
715 self.util.source_from_cache(
716 '__pycache__/foo.cpython-32.opt-1.foo.pyc')
717
718 def test_source_from_cache_not_opt(self):
719 # Non-`opt-` path component -> ValueError
Brett Cannona3c96152013-06-14 22:26:30 -0400720 self.assertRaises(
Brett Cannon13400492013-10-18 15:40:11 -0400721 ValueError, self.util.source_from_cache,
Brett Cannona3c96152013-06-14 22:26:30 -0400722 '__pycache__/foo.cpython-32.foo.pyc')
723
724 def test_source_from_cache_no__pycache__(self):
725 # Another problem with the path -> ValueError
726 self.assertRaises(
Brett Cannon13400492013-10-18 15:40:11 -0400727 ValueError, self.util.source_from_cache,
Brett Cannona3c96152013-06-14 22:26:30 -0400728 '/foo/bar/foo.cpython-32.foo.pyc')
729
Brett Cannonf299abd2015-04-13 14:21:02 -0400730 def test_source_from_cache_optimized_bytecode(self):
731 # Optimized bytecode is not an issue.
732 path = os.path.join('__pycache__', 'foo.{}.opt-1.pyc'.format(self.tag))
733 self.assertEqual(self.util.source_from_cache(path), 'foo.py')
734
735 def test_source_from_cache_missing_optimization(self):
736 # An empty optimization level is a no-no.
737 path = os.path.join('__pycache__', 'foo.{}.opt-.pyc'.format(self.tag))
738 with self.assertRaises(ValueError):
739 self.util.source_from_cache(path)
740
Eric Snow3497c0b2014-05-16 11:40:40 -0600741
742(Frozen_PEP3147Tests,
743 Source_PEP3147Tests
Brett Cannon2a17bde2014-05-30 14:55:29 -0400744 ) = util.test_both(PEP3147Tests, util=importlib_util)
Brett Cannon13400492013-10-18 15:40:11 -0400745
Brett Cannona3c96152013-06-14 22:26:30 -0400746
Brett Cannond2e7b332009-02-17 02:45:03 +0000747if __name__ == '__main__':
Brett Cannon4802bec2013-03-13 10:41:36 -0700748 unittest.main()