Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 1 | from . import util |
| 2 | abc = util.import_importlib('importlib.abc') |
| 3 | init = util.import_importlib('importlib') |
| 4 | machinery = util.import_importlib('importlib.machinery') |
| 5 | importlib_util = util.import_importlib('importlib.util') |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 6 | |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 7 | import os |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 8 | import string |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 9 | import sys |
Brett Cannon | a3687f0 | 2013-05-28 17:29:34 -0400 | [diff] [blame] | 10 | from test import support |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 11 | import types |
| 12 | import unittest |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 13 | import warnings |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 14 | |
| 15 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 16 | class DecodeSourceBytesTests: |
Brett Cannon | f24fecd | 2013-06-16 18:37:53 -0400 | [diff] [blame] | 17 | |
| 18 | source = "string ='ΓΌ'" |
| 19 | |
| 20 | def test_ut8_default(self): |
| 21 | source_bytes = self.source.encode('utf-8') |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 22 | self.assertEqual(self.util.decode_source(source_bytes), self.source) |
Brett Cannon | f24fecd | 2013-06-16 18:37:53 -0400 | [diff] [blame] | 23 | |
| 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 Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 28 | self.assertEqual(self.util.decode_source(source_bytes), source) |
Brett Cannon | f24fecd | 2013-06-16 18:37:53 -0400 | [diff] [blame] | 29 | |
| 30 | def test_universal_newlines(self): |
| 31 | source = '\r\n'.join([self.source, self.source]) |
| 32 | source_bytes = source.encode('utf-8') |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 33 | self.assertEqual(self.util.decode_source(source_bytes), |
Brett Cannon | f24fecd | 2013-06-16 18:37:53 -0400 | [diff] [blame] | 34 | '\n'.join([self.source, self.source])) |
| 35 | |
Eric Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 36 | |
| 37 | (Frozen_DecodeSourceBytesTests, |
| 38 | Source_DecodeSourceBytesTests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 39 | ) = util.test_both(DecodeSourceBytesTests, util=importlib_util) |
| 40 | |
| 41 | |
| 42 | class ModuleFromSpecTests: |
| 43 | |
| 44 | def test_no_create_module(self): |
Brett Cannon | 02d8454 | 2015-01-09 11:39:21 -0500 | [diff] [blame] | 45 | class Loader: |
| 46 | def exec_module(self, module): |
| 47 | pass |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 48 | spec = self.machinery.ModuleSpec('test', Loader()) |
Brett Cannon | 02d8454 | 2015-01-09 11:39:21 -0500 | [diff] [blame] | 49 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 55 | 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 Cannon | f24fecd | 2013-06-16 18:37:53 -0400 | [diff] [blame] | 124 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 125 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 126 | class ModuleForLoaderTests: |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 127 | |
| 128 | """Tests for importlib.util.module_for_loader.""" |
| 129 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 130 | @classmethod |
| 131 | def module_for_loader(cls, func): |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 132 | with warnings.catch_warnings(): |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 133 | warnings.simplefilter('ignore', DeprecationWarning) |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 134 | return cls.util.module_for_loader(func) |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 135 | |
| 136 | def test_warning(self): |
| 137 | # Should raise a PendingDeprecationWarning when used. |
| 138 | with warnings.catch_warnings(): |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 139 | warnings.simplefilter('error', DeprecationWarning) |
| 140 | with self.assertRaises(DeprecationWarning): |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 141 | func = self.util.module_for_loader(lambda x: x) |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 142 | |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 143 | def return_module(self, name): |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 144 | fxn = self.module_for_loader(lambda self, module: module) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 145 | return fxn(self, name) |
| 146 | |
| 147 | def raise_exception(self, name): |
| 148 | def to_wrap(self, module): |
| 149 | raise ImportError |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 150 | fxn = self.module_for_loader(to_wrap) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 151 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 160 | with util.uncache(module_name): |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 161 | module = self.return_module(module_name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 162 | self.assertIn(module_name, sys.modules) |
| 163 | self.assertIsInstance(module, types.ModuleType) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 164 | 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 Cannon | 3dc48d6 | 2013-05-28 18:35:54 -0400 | [diff] [blame] | 168 | class FakeLoader: |
| 169 | def is_package(self, name): |
| 170 | return True |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 171 | @self.module_for_loader |
Brett Cannon | 3dc48d6 | 2013-05-28 18:35:54 -0400 | [diff] [blame] | 172 | def load_module(self, module): |
| 173 | return module |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 174 | name = 'a.b.c' |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 175 | module = types.ModuleType('a.b.c') |
Brett Cannon | 3dc48d6 | 2013-05-28 18:35:54 -0400 | [diff] [blame] | 176 | module.__loader__ = 42 |
| 177 | module.__package__ = 42 |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 178 | with util.uncache(name): |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 179 | sys.modules[name] = module |
Brett Cannon | 3dc48d6 | 2013-05-28 18:35:54 -0400 | [diff] [blame] | 180 | loader = FakeLoader() |
| 181 | returned_module = loader.load_module(name) |
Brett Cannon | c56b094 | 2010-06-21 02:49:35 +0000 | [diff] [blame] | 182 | self.assertIs(returned_module, sys.modules[name]) |
Brett Cannon | 3dc48d6 | 2013-05-28 18:35:54 -0400 | [diff] [blame] | 183 | self.assertEqual(module.__loader__, loader) |
| 184 | self.assertEqual(module.__package__, name) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 185 | |
| 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 190 | with util.uncache(name): |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 191 | self.raise_exception(name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 192 | self.assertNotIn(name, sys.modules) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 193 | |
| 194 | def test_reload_failure(self): |
| 195 | # Test that a failure on reload leaves the module in-place. |
| 196 | name = 'a.b.c' |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 197 | module = types.ModuleType(name) |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 198 | with util.uncache(name): |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 199 | sys.modules[name] = module |
| 200 | self.raise_exception(name) |
Brett Cannon | c56b094 | 2010-06-21 02:49:35 +0000 | [diff] [blame] | 201 | self.assertIs(module, sys.modules[name]) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 202 | |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 203 | def test_decorator_attrs(self): |
| 204 | def fxn(self, module): pass |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 205 | wrapped = self.module_for_loader(fxn) |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 206 | self.assertEqual(wrapped.__name__, fxn.__name__) |
| 207 | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 208 | |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 209 | 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 Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 214 | |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 215 | name = 'mod' |
| 216 | module = FalseModule(name) |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 217 | with util.uncache(name): |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 218 | self.assertFalse(module) |
| 219 | sys.modules[name] = module |
| 220 | given = self.return_module(name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 221 | self.assertIs(given, module) |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 222 | |
Brett Cannon | efad00d | 2012-04-27 17:27:14 -0400 | [diff] [blame] | 223 | 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 Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 231 | @self.module_for_loader |
Brett Cannon | efad00d | 2012-04-27 17:27:14 -0400 | [diff] [blame] | 232 | def load_module(self, module): |
| 233 | return module |
| 234 | |
| 235 | name = 'pkg.mod' |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 236 | with util.uncache(name): |
Brett Cannon | efad00d | 2012-04-27 17:27:14 -0400 | [diff] [blame] | 237 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 244 | with util.uncache(name): |
Brett Cannon | efad00d | 2012-04-27 17:27:14 -0400 | [diff] [blame] | 245 | 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 Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 251 | |
| 252 | (Frozen_ModuleForLoaderTests, |
| 253 | Source_ModuleForLoaderTests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 254 | ) = util.test_both(ModuleForLoaderTests, util=importlib_util) |
Brett Cannon | 7bd329d | 2012-04-17 21:41:35 -0400 | [diff] [blame] | 255 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 256 | |
| 257 | class SetPackageTests: |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 258 | |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 259 | """Tests for importlib.util.set_package.""" |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 260 | |
| 261 | def verify(self, module, expect): |
| 262 | """Verify the module has the expected value for __package__ after |
Brett Cannon | 435aad8 | 2009-03-04 16:07:00 +0000 | [diff] [blame] | 263 | passing through set_package.""" |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 264 | fxn = lambda: module |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 265 | wrapped = self.util.set_package(fxn) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 266 | with warnings.catch_warnings(): |
| 267 | warnings.simplefilter('ignore', DeprecationWarning) |
| 268 | wrapped() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 269 | self.assertTrue(hasattr(module, '__package__')) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 270 | 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 Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 275 | module = types.ModuleType('module') |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 276 | module.__package__ = None |
| 277 | self.verify(module, '') |
| 278 | |
| 279 | def test_package(self): |
| 280 | # Test setting __package__ for a package. |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 281 | module = types.ModuleType('pkg') |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 282 | 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 Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 288 | module = types.ModuleType('pkg.mod') |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 289 | 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 Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 294 | module = types.ModuleType('mod') |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 295 | 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 Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 302 | module = types.ModuleType('mod') |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 303 | module.__package__ = value |
| 304 | self.verify(module, value) |
| 305 | |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 306 | def test_decorator_attrs(self): |
| 307 | def fxn(module): pass |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 308 | with warnings.catch_warnings(): |
| 309 | warnings.simplefilter('ignore', DeprecationWarning) |
| 310 | wrapped = self.util.set_package(fxn) |
Meador Inge | 96ff084 | 2011-12-14 22:53:13 -0600 | [diff] [blame] | 311 | self.assertEqual(wrapped.__name__, fxn.__name__) |
| 312 | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
Brett Cannon | 57b46f5 | 2009-03-02 14:38:26 +0000 | [diff] [blame] | 313 | |
Eric Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 314 | |
| 315 | (Frozen_SetPackageTests, |
| 316 | Source_SetPackageTests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 317 | ) = util.test_both(SetPackageTests, util=importlib_util) |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 318 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 319 | |
| 320 | class SetLoaderTests: |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 321 | |
| 322 | """Tests importlib.util.set_loader().""" |
| 323 | |
Eric Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 324 | @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 Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 333 | |
| 334 | def test_no_attribute(self): |
| 335 | loader = self.DummyLoader() |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 336 | loader.module = types.ModuleType('blah') |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 337 | try: |
| 338 | del loader.module.__loader__ |
| 339 | except AttributeError: |
| 340 | pass |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 341 | with warnings.catch_warnings(): |
| 342 | warnings.simplefilter('ignore', DeprecationWarning) |
| 343 | self.assertEqual(loader, loader.load_module('blah').__loader__) |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 344 | |
| 345 | def test_attribute_is_None(self): |
| 346 | loader = self.DummyLoader() |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 347 | loader.module = types.ModuleType('blah') |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 348 | loader.module.__loader__ = None |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 349 | with warnings.catch_warnings(): |
| 350 | warnings.simplefilter('ignore', DeprecationWarning) |
| 351 | self.assertEqual(loader, loader.load_module('blah').__loader__) |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 352 | |
| 353 | def test_not_reset(self): |
| 354 | loader = self.DummyLoader() |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 355 | loader.module = types.ModuleType('blah') |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 356 | loader.module.__loader__ = 42 |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 357 | with warnings.catch_warnings(): |
| 358 | warnings.simplefilter('ignore', DeprecationWarning) |
| 359 | self.assertEqual(42, loader.load_module('blah').__loader__) |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 360 | |
| 361 | |
Eric Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 362 | (Frozen_SetLoaderTests, |
| 363 | Source_SetLoaderTests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 364 | ) = util.test_both(SetLoaderTests, util=importlib_util) |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 365 | |
| 366 | |
| 367 | class ResolveNameTests: |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 368 | |
| 369 | """Tests importlib.util.resolve_name().""" |
| 370 | |
| 371 | def test_absolute(self): |
| 372 | # bacon |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 373 | self.assertEqual('bacon', self.util.resolve_name('bacon', None)) |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 374 | |
| 375 | def test_aboslute_within_package(self): |
| 376 | # bacon in spam |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 377 | self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam')) |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 378 | |
| 379 | def test_no_package(self): |
| 380 | # .bacon in '' |
| 381 | with self.assertRaises(ValueError): |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 382 | self.util.resolve_name('.bacon', '') |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 383 | |
| 384 | def test_in_package(self): |
| 385 | # .bacon in spam |
| 386 | self.assertEqual('spam.eggs.bacon', |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 387 | self.util.resolve_name('.bacon', 'spam.eggs')) |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 388 | |
| 389 | def test_other_package(self): |
| 390 | # ..bacon in spam.bacon |
| 391 | self.assertEqual('spam.bacon', |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 392 | self.util.resolve_name('..bacon', 'spam.eggs')) |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 393 | |
| 394 | def test_escape(self): |
| 395 | # ..bacon in spam |
| 396 | with self.assertRaises(ValueError): |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 397 | self.util.resolve_name('..bacon', 'spam') |
| 398 | |
Eric Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 399 | |
| 400 | (Frozen_ResolveNameTests, |
| 401 | Source_ResolveNameTests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 402 | ) = util.test_both(ResolveNameTests, util=importlib_util) |
Brett Cannon | d200bf5 | 2012-05-13 13:45:09 -0400 | [diff] [blame] | 403 | |
| 404 | |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 405 | class 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 413 | with util.uncache(name): |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 414 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 425 | with util.uncache(name): |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 426 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 437 | with util.uncache(name): |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 438 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 446 | with util.uncache(name): |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 447 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 456 | with util.uncache(name): |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 457 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 468 | with util.uncache(name): |
| 469 | with util.import_state(meta_path=[self.FakeMetaFinder]): |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 470 | self.assertEqual((name, None, None), |
| 471 | self.util.find_spec(name)) |
| 472 | |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 473 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 480 | with util.temp_module(name, pkg=True) as pkg_dir: |
| 481 | fullname, _ = util.submodule(name, subname, pkg_dir) |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 482 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 493 | with util.temp_module(name, pkg=True) as pkg_dir: |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 494 | self.init.import_module(name) |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 495 | fullname, _ = util.submodule(name, subname, pkg_dir) |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 496 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 507 | with util.temp_module(name, pkg=True) as pkg_dir: |
| 508 | fullname, _ = util.submodule(name, subname, pkg_dir) |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 509 | 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 Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 521 | with util.temp_module(name, pkg=True) as pkg_dir: |
| 522 | fullname, _ = util.submodule(name, subname, pkg_dir) |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 523 | 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 Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 530 | (Frozen_FindSpecTests, |
| 531 | Source_FindSpecTests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 532 | ) = util.test_both(FindSpecTests, init=init, util=importlib_util, |
Eric Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 533 | machinery=machinery) |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 534 | |
| 535 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 536 | class MagicNumberTests: |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 537 | |
| 538 | def test_length(self): |
| 539 | # Should be 4 bytes. |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 540 | self.assertEqual(len(self.util.MAGIC_NUMBER), 4) |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 541 | |
| 542 | def test_incorporates_rn(self): |
| 543 | # The magic number uses \r\n to come out wrong when splitting on lines. |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 544 | self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n')) |
| 545 | |
Eric Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 546 | |
| 547 | (Frozen_MagicNumberTests, |
| 548 | Source_MagicNumberTests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 549 | ) = util.test_both(MagicNumberTests, util=importlib_util) |
Brett Cannon | 05a647d | 2013-06-14 19:02:34 -0400 | [diff] [blame] | 550 | |
| 551 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 552 | class PEP3147Tests: |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 553 | |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 554 | """Tests of PEP 3147-related functions: cache_from_source and source_from_cache.""" |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 555 | |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 556 | tag = sys.implementation.cache_tag |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 557 | |
| 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 Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 566 | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
| 567 | expect) |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 568 | |
| 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 Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 573 | self.util.cache_from_source('whatever.py') |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 574 | |
| 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 Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 580 | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
| 581 | expect) |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 582 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 583 | 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 Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 586 | path = os.path.join('foo', 'bar', 'baz', 'qux.py') |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 587 | 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 Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 599 | |
| 600 | def test_cache_from_source_cwd(self): |
| 601 | path = 'foo.py' |
| 602 | expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag)) |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 603 | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
| 604 | expect) |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 605 | |
| 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 Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 610 | # However if the bool-ishness can't be determined, the exception |
| 611 | # propagates. |
| 612 | class Bearish: |
| 613 | def __bool__(self): raise RuntimeError |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 614 | 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 Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 670 | |
| 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 Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 676 | self.util.cache_from_source('\\foo\\bar\\baz/qux.py', optimization=''), |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 677 | '\\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 Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 688 | self.assertEqual(self.util.source_from_cache(path), expect) |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 689 | |
| 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 Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 695 | self.util.source_from_cache(path) |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 696 | |
| 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 Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 701 | ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc') |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 702 | |
| 703 | def test_source_from_cache_no_slash(self): |
| 704 | # No slashes at all in path -> ValueError |
| 705 | self.assertRaises( |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 706 | ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc') |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 707 | |
| 708 | def test_source_from_cache_too_few_dots(self): |
| 709 | # Too few dots in final path component -> ValueError |
| 710 | self.assertRaises( |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 711 | ValueError, self.util.source_from_cache, '__pycache__/foo.pyc') |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 712 | |
| 713 | def test_source_from_cache_too_many_dots(self): |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 714 | 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 Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 720 | self.assertRaises( |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 721 | ValueError, self.util.source_from_cache, |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 722 | '__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 Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 727 | ValueError, self.util.source_from_cache, |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 728 | '/foo/bar/foo.cpython-32.foo.pyc') |
| 729 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 730 | 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 Snow | 3497c0b | 2014-05-16 11:40:40 -0600 | [diff] [blame] | 741 | |
| 742 | (Frozen_PEP3147Tests, |
| 743 | Source_PEP3147Tests |
Brett Cannon | 2a17bde | 2014-05-30 14:55:29 -0400 | [diff] [blame] | 744 | ) = util.test_both(PEP3147Tests, util=importlib_util) |
Brett Cannon | 1340049 | 2013-10-18 15:40:11 -0400 | [diff] [blame] | 745 | |
Brett Cannon | a3c9615 | 2013-06-14 22:26:30 -0400 | [diff] [blame] | 746 | |
Brett Cannon | d2e7b33 | 2009-02-17 02:45:03 +0000 | [diff] [blame] | 747 | if __name__ == '__main__': |
Brett Cannon | 4802bec | 2013-03-13 10:41:36 -0700 | [diff] [blame] | 748 | unittest.main() |