| 1 |
import io |
| 2 |
import marshal |
| 3 |
import os |
| 4 |
import sys |
| 5 |
from test.support import import_helper |
| 6 |
import types |
| 7 |
import unittest |
| 8 |
from unittest import mock |
| 9 |
import warnings |
| 10 |
|
| 11 |
from test.test_importlib import util as test_util |
| 12 |
|
| 13 |
init = test_util.import_importlib('importlib') |
| 14 |
abc = test_util.import_importlib('importlib.abc') |
| 15 |
machinery = test_util.import_importlib('importlib.machinery') |
| 16 |
util = test_util.import_importlib('importlib.util') |
| 17 |
|
| 18 |
|
| 19 |
##### Inheritance ############################################################## |
| 20 |
class InheritanceTests: |
| 21 |
|
| 22 |
"""Test that the specified class is a subclass/superclass of the expected |
| 23 |
classes.""" |
| 24 |
|
| 25 |
subclasses = [] |
| 26 |
superclasses = [] |
| 27 |
|
| 28 |
def setUp(self): |
| 29 |
self.superclasses = [getattr(self.abc, class_name) |
| 30 |
for class_name in self.superclass_names] |
| 31 |
if hasattr(self, 'subclass_names'): |
| 32 |
# Because test.support.import_fresh_module() creates a new |
| 33 |
# importlib._bootstrap per module, inheritance checks fail when |
| 34 |
# checking across module boundaries (i.e. the _bootstrap in abc is |
| 35 |
# not the same as the one in machinery). That means stealing one of |
| 36 |
# the modules from the other to make sure the same instance is used. |
| 37 |
machinery = self.abc.machinery |
| 38 |
self.subclasses = [getattr(machinery, class_name) |
| 39 |
for class_name in self.subclass_names] |
| 40 |
assert self.subclasses or self.superclasses, self.__class__ |
| 41 |
self.__test = getattr(self.abc, self._NAME) |
| 42 |
|
| 43 |
def test_subclasses(self): |
| 44 |
# Test that the expected subclasses inherit. |
| 45 |
for subclass in self.subclasses: |
| 46 |
self.assertIsSubclass(subclass, self.__test) |
| 47 |
|
| 48 |
def test_superclasses(self): |
| 49 |
# Test that the class inherits from the expected superclasses. |
| 50 |
for superclass in self.superclasses: |
| 51 |
self.assertIsSubclass(self.__test, superclass) |
| 52 |
|
| 53 |
|
| 54 |
class MetaPathFinder(InheritanceTests): |
| 55 |
superclass_names = [] |
| 56 |
subclass_names = ['BuiltinImporter', 'FrozenImporter', 'PathFinder', |
| 57 |
'WindowsRegistryFinder'] |
| 58 |
|
| 59 |
|
| 60 |
(Frozen_MetaPathFinderInheritanceTests, |
| 61 |
Source_MetaPathFinderInheritanceTests |
| 62 |
) = test_util.test_both(MetaPathFinder, abc=abc) |
| 63 |
|
| 64 |
|
| 65 |
class PathEntryFinder(InheritanceTests): |
| 66 |
superclass_names = [] |
| 67 |
subclass_names = ['FileFinder'] |
| 68 |
|
| 69 |
|
| 70 |
(Frozen_PathEntryFinderInheritanceTests, |
| 71 |
Source_PathEntryFinderInheritanceTests |
| 72 |
) = test_util.test_both(PathEntryFinder, abc=abc) |
| 73 |
|
| 74 |
|
| 75 |
class ResourceLoader(InheritanceTests): |
| 76 |
superclass_names = ['Loader'] |
| 77 |
|
| 78 |
|
| 79 |
(Frozen_ResourceLoaderInheritanceTests, |
| 80 |
Source_ResourceLoaderInheritanceTests |
| 81 |
) = test_util.test_both(ResourceLoader, abc=abc) |
| 82 |
|
| 83 |
|
| 84 |
class InspectLoader(InheritanceTests): |
| 85 |
superclass_names = ['Loader'] |
| 86 |
subclass_names = ['BuiltinImporter', 'FrozenImporter', 'ExtensionFileLoader'] |
| 87 |
|
| 88 |
|
| 89 |
(Frozen_InspectLoaderInheritanceTests, |
| 90 |
Source_InspectLoaderInheritanceTests |
| 91 |
) = test_util.test_both(InspectLoader, abc=abc) |
| 92 |
|
| 93 |
|
| 94 |
class ExecutionLoader(InheritanceTests): |
| 95 |
superclass_names = ['InspectLoader'] |
| 96 |
subclass_names = ['ExtensionFileLoader'] |
| 97 |
|
| 98 |
|
| 99 |
(Frozen_ExecutionLoaderInheritanceTests, |
| 100 |
Source_ExecutionLoaderInheritanceTests |
| 101 |
) = test_util.test_both(ExecutionLoader, abc=abc) |
| 102 |
|
| 103 |
|
| 104 |
class FileLoader(InheritanceTests): |
| 105 |
superclass_names = ['ResourceLoader', 'ExecutionLoader'] |
| 106 |
subclass_names = ['SourceFileLoader', 'SourcelessFileLoader'] |
| 107 |
|
| 108 |
|
| 109 |
(Frozen_FileLoaderInheritanceTests, |
| 110 |
Source_FileLoaderInheritanceTests |
| 111 |
) = test_util.test_both(FileLoader, abc=abc) |
| 112 |
|
| 113 |
|
| 114 |
class SourceLoader(InheritanceTests): |
| 115 |
superclass_names = ['ResourceLoader', 'ExecutionLoader'] |
| 116 |
subclass_names = ['SourceFileLoader'] |
| 117 |
|
| 118 |
|
| 119 |
(Frozen_SourceLoaderInheritanceTests, |
| 120 |
Source_SourceLoaderInheritanceTests |
| 121 |
) = test_util.test_both(SourceLoader, abc=abc) |
| 122 |
|
| 123 |
|
| 124 |
##### Default return values #################################################### |
| 125 |
|
| 126 |
def make_abc_subclasses(base_class, name=None, inst=False, **kwargs): |
| 127 |
if name is None: |
| 128 |
name = base_class.__name__ |
| 129 |
base = {kind: getattr(splitabc, name) |
| 130 |
for kind, splitabc in abc.items()} |
| 131 |
return {cls._KIND: cls() if inst else cls |
| 132 |
for cls in test_util.split_frozen(base_class, base, **kwargs)} |
| 133 |
|
| 134 |
|
| 135 |
class ABCTestHarness: |
| 136 |
|
| 137 |
@property |
| 138 |
def ins(self): |
| 139 |
# Lazily set ins on the class. |
| 140 |
cls = self.SPLIT[self._KIND] |
| 141 |
ins = cls() |
| 142 |
self.__class__.ins = ins |
| 143 |
return ins |
| 144 |
|
| 145 |
|
| 146 |
class MetaPathFinder: |
| 147 |
|
| 148 |
pass |
| 149 |
|
| 150 |
|
| 151 |
class MetaPathFinderDefaultsTests(ABCTestHarness): |
| 152 |
|
| 153 |
SPLIT = make_abc_subclasses(MetaPathFinder) |
| 154 |
|
| 155 |
def test_invalidate_caches(self): |
| 156 |
# Calling the method is a no-op. |
| 157 |
self.ins.invalidate_caches() |
| 158 |
|
| 159 |
|
| 160 |
(Frozen_MPFDefaultTests, |
| 161 |
Source_MPFDefaultTests |
| 162 |
) = test_util.test_both(MetaPathFinderDefaultsTests) |
| 163 |
|
| 164 |
|
| 165 |
class PathEntryFinder: |
| 166 |
|
| 167 |
pass |
| 168 |
|
| 169 |
|
| 170 |
class PathEntryFinderDefaultsTests(ABCTestHarness): |
| 171 |
|
| 172 |
SPLIT = make_abc_subclasses(PathEntryFinder) |
| 173 |
|
| 174 |
def test_invalidate_caches(self): |
| 175 |
# Should be a no-op. |
| 176 |
self.ins.invalidate_caches() |
| 177 |
|
| 178 |
|
| 179 |
(Frozen_PEFDefaultTests, |
| 180 |
Source_PEFDefaultTests |
| 181 |
) = test_util.test_both(PathEntryFinderDefaultsTests) |
| 182 |
|
| 183 |
|
| 184 |
class Loader: |
| 185 |
|
| 186 |
pass |
| 187 |
|
| 188 |
|
| 189 |
class LoaderDefaultsTests(ABCTestHarness): |
| 190 |
|
| 191 |
SPLIT = make_abc_subclasses(Loader) |
| 192 |
|
| 193 |
def test_create_module(self): |
| 194 |
spec = 'a spec' |
| 195 |
self.assertIsNone(self.ins.create_module(spec)) |
| 196 |
|
| 197 |
def test_load_module(self): |
| 198 |
with self.assertRaises(ImportError): |
| 199 |
self.ins.load_module('something') |
| 200 |
|
| 201 |
def test_module_repr(self): |
| 202 |
mod = types.ModuleType('blah') |
| 203 |
with warnings.catch_warnings(): |
| 204 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 205 |
original_repr = repr(mod) |
| 206 |
mod.__loader__ = self.ins |
| 207 |
# Should still return a proper repr. |
| 208 |
self.assertTrue(repr(mod)) |
| 209 |
|
| 210 |
|
| 211 |
(Frozen_LDefaultTests, |
| 212 |
SourceLDefaultTests |
| 213 |
) = test_util.test_both(LoaderDefaultsTests) |
| 214 |
|
| 215 |
|
| 216 |
class ResourceLoader(Loader): |
| 217 |
|
| 218 |
def get_data(self, path): |
| 219 |
return super().get_data(path) |
| 220 |
|
| 221 |
|
| 222 |
class ResourceLoaderDefaultsTests(ABCTestHarness): |
| 223 |
|
| 224 |
SPLIT = make_abc_subclasses(ResourceLoader) |
| 225 |
|
| 226 |
def test_get_data(self): |
| 227 |
with self.assertRaises(IOError): |
| 228 |
self.ins.get_data('/some/path') |
| 229 |
|
| 230 |
|
| 231 |
(Frozen_RLDefaultTests, |
| 232 |
Source_RLDefaultTests |
| 233 |
) = test_util.test_both(ResourceLoaderDefaultsTests) |
| 234 |
|
| 235 |
|
| 236 |
class InspectLoader(Loader): |
| 237 |
|
| 238 |
def is_package(self, fullname): |
| 239 |
return super().is_package(fullname) |
| 240 |
|
| 241 |
def get_source(self, fullname): |
| 242 |
return super().get_source(fullname) |
| 243 |
|
| 244 |
|
| 245 |
SPLIT_IL = make_abc_subclasses(InspectLoader) |
| 246 |
|
| 247 |
|
| 248 |
class InspectLoaderDefaultsTests(ABCTestHarness): |
| 249 |
|
| 250 |
SPLIT = SPLIT_IL |
| 251 |
|
| 252 |
def test_is_package(self): |
| 253 |
with self.assertRaises(ImportError): |
| 254 |
self.ins.is_package('blah') |
| 255 |
|
| 256 |
def test_get_source(self): |
| 257 |
with self.assertRaises(ImportError): |
| 258 |
self.ins.get_source('blah') |
| 259 |
|
| 260 |
|
| 261 |
(Frozen_ILDefaultTests, |
| 262 |
Source_ILDefaultTests |
| 263 |
) = test_util.test_both(InspectLoaderDefaultsTests) |
| 264 |
|
| 265 |
|
| 266 |
class ExecutionLoader(InspectLoader): |
| 267 |
|
| 268 |
def get_filename(self, fullname): |
| 269 |
return super().get_filename(fullname) |
| 270 |
|
| 271 |
|
| 272 |
SPLIT_EL = make_abc_subclasses(ExecutionLoader) |
| 273 |
|
| 274 |
|
| 275 |
class ExecutionLoaderDefaultsTests(ABCTestHarness): |
| 276 |
|
| 277 |
SPLIT = SPLIT_EL |
| 278 |
|
| 279 |
def test_get_filename(self): |
| 280 |
with self.assertRaises(ImportError): |
| 281 |
self.ins.get_filename('blah') |
| 282 |
|
| 283 |
|
| 284 |
(Frozen_ELDefaultTests, |
| 285 |
Source_ELDefaultsTests |
| 286 |
) = test_util.test_both(InspectLoaderDefaultsTests) |
| 287 |
|
| 288 |
|
| 289 |
class ResourceReader: |
| 290 |
|
| 291 |
def open_resource(self, *args, **kwargs): |
| 292 |
return super().open_resource(*args, **kwargs) |
| 293 |
|
| 294 |
def resource_path(self, *args, **kwargs): |
| 295 |
return super().resource_path(*args, **kwargs) |
| 296 |
|
| 297 |
def is_resource(self, *args, **kwargs): |
| 298 |
return super().is_resource(*args, **kwargs) |
| 299 |
|
| 300 |
def contents(self, *args, **kwargs): |
| 301 |
return super().contents(*args, **kwargs) |
| 302 |
|
| 303 |
|
| 304 |
##### MetaPathFinder concrete methods ########################################## |
| 305 |
class MetaPathFinderFindModuleTests: |
| 306 |
|
| 307 |
@classmethod |
| 308 |
def finder(cls, spec): |
| 309 |
class MetaPathSpecFinder(cls.abc.MetaPathFinder): |
| 310 |
|
| 311 |
def find_spec(self, fullname, path, target=None): |
| 312 |
self.called_for = fullname, path |
| 313 |
return spec |
| 314 |
|
| 315 |
return MetaPathSpecFinder() |
| 316 |
|
| 317 |
def test_find_spec_with_explicit_target(self): |
| 318 |
loader = object() |
| 319 |
spec = self.util.spec_from_loader('blah', loader) |
| 320 |
finder = self.finder(spec) |
| 321 |
found = finder.find_spec('blah', 'blah', None) |
| 322 |
self.assertEqual(found, spec) |
| 323 |
|
| 324 |
def test_no_spec(self): |
| 325 |
finder = self.finder(None) |
| 326 |
path = ['a', 'b', 'c'] |
| 327 |
name = 'blah' |
| 328 |
found = finder.find_spec(name, path, None) |
| 329 |
self.assertIsNone(found) |
| 330 |
self.assertEqual(name, finder.called_for[0]) |
| 331 |
self.assertEqual(path, finder.called_for[1]) |
| 332 |
|
| 333 |
def test_spec(self): |
| 334 |
loader = object() |
| 335 |
spec = self.util.spec_from_loader('blah', loader) |
| 336 |
finder = self.finder(spec) |
| 337 |
found = finder.find_spec('blah', None) |
| 338 |
self.assertIs(found, spec) |
| 339 |
|
| 340 |
|
| 341 |
(Frozen_MPFFindModuleTests, |
| 342 |
Source_MPFFindModuleTests |
| 343 |
) = test_util.test_both(MetaPathFinderFindModuleTests, abc=abc, util=util) |
| 344 |
|
| 345 |
|
| 346 |
##### Loader concrete methods ################################################## |
| 347 |
class LoaderLoadModuleTests: |
| 348 |
|
| 349 |
def loader(self): |
| 350 |
class SpecLoader(self.abc.Loader): |
| 351 |
found = None |
| 352 |
def exec_module(self, module): |
| 353 |
self.found = module |
| 354 |
|
| 355 |
def is_package(self, fullname): |
| 356 |
"""Force some non-default module state to be set.""" |
| 357 |
return True |
| 358 |
|
| 359 |
return SpecLoader() |
| 360 |
|
| 361 |
def test_fresh(self): |
| 362 |
with warnings.catch_warnings(): |
| 363 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 364 |
loader = self.loader() |
| 365 |
name = 'blah' |
| 366 |
with test_util.uncache(name): |
| 367 |
loader.load_module(name) |
| 368 |
module = loader.found |
| 369 |
self.assertIs(sys.modules[name], module) |
| 370 |
self.assertEqual(loader, module.__loader__) |
| 371 |
self.assertEqual(loader, module.__spec__.loader) |
| 372 |
self.assertEqual(name, module.__name__) |
| 373 |
self.assertEqual(name, module.__spec__.name) |
| 374 |
self.assertIsNotNone(module.__path__) |
| 375 |
self.assertIsNotNone(module.__path__, |
| 376 |
module.__spec__.submodule_search_locations) |
| 377 |
|
| 378 |
def test_reload(self): |
| 379 |
with warnings.catch_warnings(): |
| 380 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 381 |
name = 'blah' |
| 382 |
loader = self.loader() |
| 383 |
module = types.ModuleType(name) |
| 384 |
module.__spec__ = self.util.spec_from_loader(name, loader) |
| 385 |
module.__loader__ = loader |
| 386 |
with test_util.uncache(name): |
| 387 |
sys.modules[name] = module |
| 388 |
loader.load_module(name) |
| 389 |
found = loader.found |
| 390 |
self.assertIs(found, sys.modules[name]) |
| 391 |
self.assertIs(module, sys.modules[name]) |
| 392 |
|
| 393 |
|
| 394 |
(Frozen_LoaderLoadModuleTests, |
| 395 |
Source_LoaderLoadModuleTests |
| 396 |
) = test_util.test_both(LoaderLoadModuleTests, abc=abc, util=util) |
| 397 |
|
| 398 |
|
| 399 |
##### InspectLoader concrete methods ########################################### |
| 400 |
class InspectLoaderSourceToCodeTests: |
| 401 |
|
| 402 |
def source_to_module(self, data, path=None): |
| 403 |
"""Help with source_to_code() tests.""" |
| 404 |
module = types.ModuleType('blah') |
| 405 |
loader = self.InspectLoaderSubclass() |
| 406 |
if path is None: |
| 407 |
code = loader.source_to_code(data) |
| 408 |
else: |
| 409 |
code = loader.source_to_code(data, path) |
| 410 |
exec(code, module.__dict__) |
| 411 |
return module |
| 412 |
|
| 413 |
def test_source_to_code_source(self): |
| 414 |
# Since compile() can handle strings, so should source_to_code(). |
| 415 |
source = 'attr = 42' |
| 416 |
module = self.source_to_module(source) |
| 417 |
self.assertHasAttr(module, 'attr') |
| 418 |
self.assertEqual(module.attr, 42) |
| 419 |
|
| 420 |
def test_source_to_code_bytes(self): |
| 421 |
# Since compile() can handle bytes, so should source_to_code(). |
| 422 |
source = b'attr = 42' |
| 423 |
module = self.source_to_module(source) |
| 424 |
self.assertHasAttr(module, 'attr') |
| 425 |
self.assertEqual(module.attr, 42) |
| 426 |
|
| 427 |
def test_source_to_code_path(self): |
| 428 |
# Specifying a path should set it for the code object. |
| 429 |
path = 'path/to/somewhere' |
| 430 |
loader = self.InspectLoaderSubclass() |
| 431 |
code = loader.source_to_code('', path) |
| 432 |
self.assertEqual(code.co_filename, path) |
| 433 |
|
| 434 |
def test_source_to_code_no_path(self): |
| 435 |
# Not setting a path should still work and be set to <string> since that |
| 436 |
# is a pre-existing practice as a default to compile(). |
| 437 |
loader = self.InspectLoaderSubclass() |
| 438 |
code = loader.source_to_code('') |
| 439 |
self.assertEqual(code.co_filename, '<string>') |
| 440 |
|
| 441 |
|
| 442 |
(Frozen_ILSourceToCodeTests, |
| 443 |
Source_ILSourceToCodeTests |
| 444 |
) = test_util.test_both(InspectLoaderSourceToCodeTests, |
| 445 |
InspectLoaderSubclass=SPLIT_IL) |
| 446 |
|
| 447 |
|
| 448 |
class InspectLoaderGetCodeTests: |
| 449 |
|
| 450 |
def test_get_code(self): |
| 451 |
# Test success. |
| 452 |
module = types.ModuleType('blah') |
| 453 |
with mock.patch.object(self.InspectLoaderSubclass, 'get_source') as mocked: |
| 454 |
mocked.return_value = 'attr = 42' |
| 455 |
loader = self.InspectLoaderSubclass() |
| 456 |
code = loader.get_code('blah') |
| 457 |
exec(code, module.__dict__) |
| 458 |
self.assertEqual(module.attr, 42) |
| 459 |
|
| 460 |
def test_get_code_source_is_None(self): |
| 461 |
# If get_source() is None then this should be None. |
| 462 |
with mock.patch.object(self.InspectLoaderSubclass, 'get_source') as mocked: |
| 463 |
mocked.return_value = None |
| 464 |
loader = self.InspectLoaderSubclass() |
| 465 |
code = loader.get_code('blah') |
| 466 |
self.assertIsNone(code) |
| 467 |
|
| 468 |
def test_get_code_source_not_found(self): |
| 469 |
# If there is no source then there is no code object. |
| 470 |
loader = self.InspectLoaderSubclass() |
| 471 |
with self.assertRaises(ImportError): |
| 472 |
loader.get_code('blah') |
| 473 |
|
| 474 |
|
| 475 |
(Frozen_ILGetCodeTests, |
| 476 |
Source_ILGetCodeTests |
| 477 |
) = test_util.test_both(InspectLoaderGetCodeTests, |
| 478 |
InspectLoaderSubclass=SPLIT_IL) |
| 479 |
|
| 480 |
|
| 481 |
class InspectLoaderLoadModuleTests: |
| 482 |
|
| 483 |
"""Test InspectLoader.load_module().""" |
| 484 |
|
| 485 |
module_name = 'blah' |
| 486 |
|
| 487 |
def setUp(self): |
| 488 |
import_helper.unload(self.module_name) |
| 489 |
self.addCleanup(import_helper.unload, self.module_name) |
| 490 |
|
| 491 |
def load(self, loader): |
| 492 |
spec = self.util.spec_from_loader(self.module_name, loader) |
| 493 |
with warnings.catch_warnings(): |
| 494 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 495 |
return self.init._bootstrap._load_unlocked(spec) |
| 496 |
|
| 497 |
def mock_get_code(self): |
| 498 |
return mock.patch.object(self.InspectLoaderSubclass, 'get_code') |
| 499 |
|
| 500 |
def test_get_code_ImportError(self): |
| 501 |
# If get_code() raises ImportError, it should propagate. |
| 502 |
with self.mock_get_code() as mocked_get_code: |
| 503 |
mocked_get_code.side_effect = ImportError |
| 504 |
with self.assertRaises(ImportError): |
| 505 |
loader = self.InspectLoaderSubclass() |
| 506 |
self.load(loader) |
| 507 |
|
| 508 |
def test_get_code_None(self): |
| 509 |
# If get_code() returns None, raise ImportError. |
| 510 |
with self.mock_get_code() as mocked_get_code: |
| 511 |
mocked_get_code.return_value = None |
| 512 |
with self.assertRaises(ImportError): |
| 513 |
loader = self.InspectLoaderSubclass() |
| 514 |
self.load(loader) |
| 515 |
|
| 516 |
def test_module_returned(self): |
| 517 |
# The loaded module should be returned. |
| 518 |
code = compile('attr = 42', '<string>', 'exec') |
| 519 |
with self.mock_get_code() as mocked_get_code: |
| 520 |
mocked_get_code.return_value = code |
| 521 |
loader = self.InspectLoaderSubclass() |
| 522 |
module = self.load(loader) |
| 523 |
self.assertEqual(module, sys.modules[self.module_name]) |
| 524 |
|
| 525 |
|
| 526 |
(Frozen_ILLoadModuleTests, |
| 527 |
Source_ILLoadModuleTests |
| 528 |
) = test_util.test_both(InspectLoaderLoadModuleTests, |
| 529 |
InspectLoaderSubclass=SPLIT_IL, |
| 530 |
init=init, |
| 531 |
util=util) |
| 532 |
|
| 533 |
|
| 534 |
##### ExecutionLoader concrete methods ######################################### |
| 535 |
class ExecutionLoaderGetCodeTests: |
| 536 |
|
| 537 |
def mock_methods(self, *, get_source=False, get_filename=False): |
| 538 |
source_mock_context, filename_mock_context = None, None |
| 539 |
if get_source: |
| 540 |
source_mock_context = mock.patch.object(self.ExecutionLoaderSubclass, |
| 541 |
'get_source') |
| 542 |
if get_filename: |
| 543 |
filename_mock_context = mock.patch.object(self.ExecutionLoaderSubclass, |
| 544 |
'get_filename') |
| 545 |
return source_mock_context, filename_mock_context |
| 546 |
|
| 547 |
def test_get_code(self): |
| 548 |
path = 'blah.py' |
| 549 |
source_mock_context, filename_mock_context = self.mock_methods( |
| 550 |
get_source=True, get_filename=True) |
| 551 |
with source_mock_context as source_mock, filename_mock_context as name_mock: |
| 552 |
source_mock.return_value = 'attr = 42' |
| 553 |
name_mock.return_value = path |
| 554 |
loader = self.ExecutionLoaderSubclass() |
| 555 |
code = loader.get_code('blah') |
| 556 |
self.assertEqual(code.co_filename, path) |
| 557 |
module = types.ModuleType('blah') |
| 558 |
exec(code, module.__dict__) |
| 559 |
self.assertEqual(module.attr, 42) |
| 560 |
|
| 561 |
def test_get_code_source_is_None(self): |
| 562 |
# If get_source() is None then this should be None. |
| 563 |
source_mock_context, _ = self.mock_methods(get_source=True) |
| 564 |
with source_mock_context as mocked: |
| 565 |
mocked.return_value = None |
| 566 |
loader = self.ExecutionLoaderSubclass() |
| 567 |
code = loader.get_code('blah') |
| 568 |
self.assertIsNone(code) |
| 569 |
|
| 570 |
def test_get_code_source_not_found(self): |
| 571 |
# If there is no source then there is no code object. |
| 572 |
loader = self.ExecutionLoaderSubclass() |
| 573 |
with self.assertRaises(ImportError): |
| 574 |
loader.get_code('blah') |
| 575 |
|
| 576 |
def test_get_code_no_path(self): |
| 577 |
# If get_filename() raises ImportError then simply skip setting the path |
| 578 |
# on the code object. |
| 579 |
source_mock_context, filename_mock_context = self.mock_methods( |
| 580 |
get_source=True, get_filename=True) |
| 581 |
with source_mock_context as source_mock, filename_mock_context as name_mock: |
| 582 |
source_mock.return_value = 'attr = 42' |
| 583 |
name_mock.side_effect = ImportError |
| 584 |
loader = self.ExecutionLoaderSubclass() |
| 585 |
code = loader.get_code('blah') |
| 586 |
self.assertEqual(code.co_filename, '<string>') |
| 587 |
module = types.ModuleType('blah') |
| 588 |
exec(code, module.__dict__) |
| 589 |
self.assertEqual(module.attr, 42) |
| 590 |
|
| 591 |
|
| 592 |
(Frozen_ELGetCodeTests, |
| 593 |
Source_ELGetCodeTests |
| 594 |
) = test_util.test_both(ExecutionLoaderGetCodeTests, |
| 595 |
ExecutionLoaderSubclass=SPLIT_EL) |
| 596 |
|
| 597 |
|
| 598 |
##### SourceLoader concrete methods ############################################ |
| 599 |
class SourceOnlyLoader: |
| 600 |
|
| 601 |
# Globals that should be defined for all modules. |
| 602 |
source = (b"_ = '::'.join([__name__, __file__, __cached__, __package__, " |
| 603 |
b"repr(__loader__)])") |
| 604 |
|
| 605 |
def __init__(self, path): |
| 606 |
self.path = path |
| 607 |
|
| 608 |
def get_data(self, path): |
| 609 |
if path != self.path: |
| 610 |
raise IOError |
| 611 |
return self.source |
| 612 |
|
| 613 |
def get_filename(self, fullname): |
| 614 |
return self.path |
| 615 |
|
| 616 |
|
| 617 |
SPLIT_SOL = make_abc_subclasses(SourceOnlyLoader, 'SourceLoader') |
| 618 |
|
| 619 |
|
| 620 |
class SourceLoader(SourceOnlyLoader): |
| 621 |
|
| 622 |
source_mtime = 1 |
| 623 |
|
| 624 |
def __init__(self, path, magic=None): |
| 625 |
super().__init__(path) |
| 626 |
self.bytecode_path = self.util.cache_from_source(self.path) |
| 627 |
self.source_size = len(self.source) |
| 628 |
if magic is None: |
| 629 |
magic = self.util.MAGIC_NUMBER |
| 630 |
data = bytearray(magic) |
| 631 |
data.extend(self.init._pack_uint32(0)) |
| 632 |
data.extend(self.init._pack_uint32(self.source_mtime)) |
| 633 |
data.extend(self.init._pack_uint32(self.source_size)) |
| 634 |
code_object = compile(self.source, self.path, 'exec', |
| 635 |
dont_inherit=True) |
| 636 |
data.extend(marshal.dumps(code_object)) |
| 637 |
self.bytecode = bytes(data) |
| 638 |
self.written = {} |
| 639 |
|
| 640 |
def get_data(self, path): |
| 641 |
if path == self.path: |
| 642 |
return super().get_data(path) |
| 643 |
elif path == self.bytecode_path: |
| 644 |
return self.bytecode |
| 645 |
else: |
| 646 |
raise OSError |
| 647 |
|
| 648 |
def path_stats(self, path): |
| 649 |
if path != self.path: |
| 650 |
raise IOError |
| 651 |
return {'mtime': self.source_mtime, 'size': self.source_size} |
| 652 |
|
| 653 |
def set_data(self, path, data): |
| 654 |
self.written[path] = bytes(data) |
| 655 |
return path == self.bytecode_path |
| 656 |
|
| 657 |
|
| 658 |
SPLIT_SL = make_abc_subclasses(SourceLoader, util=util, init=init) |
| 659 |
|
| 660 |
|
| 661 |
class SourceLoaderTestHarness: |
| 662 |
|
| 663 |
def setUp(self, *, is_package=True, **kwargs): |
| 664 |
self.package = 'pkg' |
| 665 |
if is_package: |
| 666 |
self.path = os.path.join(self.package, '__init__.py') |
| 667 |
self.name = self.package |
| 668 |
else: |
| 669 |
module_name = 'mod' |
| 670 |
self.path = os.path.join(self.package, '.'.join(['mod', 'py'])) |
| 671 |
self.name = '.'.join([self.package, module_name]) |
| 672 |
self.cached = self.util.cache_from_source(self.path) |
| 673 |
self.loader = self.loader_mock(self.path, **kwargs) |
| 674 |
|
| 675 |
def verify_module(self, module): |
| 676 |
self.assertEqual(module.__name__, self.name) |
| 677 |
self.assertEqual(module.__file__, self.path) |
| 678 |
self.assertEqual(module.__cached__, self.cached) |
| 679 |
self.assertEqual(module.__package__, self.package) |
| 680 |
self.assertEqual(module.__loader__, self.loader) |
| 681 |
values = module._.split('::') |
| 682 |
self.assertEqual(values[0], self.name) |
| 683 |
self.assertEqual(values[1], self.path) |
| 684 |
self.assertEqual(values[2], self.cached) |
| 685 |
self.assertEqual(values[3], self.package) |
| 686 |
self.assertEqual(values[4], repr(self.loader)) |
| 687 |
|
| 688 |
def verify_code(self, code_object): |
| 689 |
module = types.ModuleType(self.name) |
| 690 |
module.__file__ = self.path |
| 691 |
module.__cached__ = self.cached |
| 692 |
module.__package__ = self.package |
| 693 |
module.__loader__ = self.loader |
| 694 |
module.__path__ = [] |
| 695 |
exec(code_object, module.__dict__) |
| 696 |
self.verify_module(module) |
| 697 |
|
| 698 |
|
| 699 |
class SourceOnlyLoaderTests(SourceLoaderTestHarness): |
| 700 |
"""Test importlib.abc.SourceLoader for source-only loading.""" |
| 701 |
|
| 702 |
def test_get_source(self): |
| 703 |
# Verify the source code is returned as a string. |
| 704 |
# If an OSError is raised by get_data then raise ImportError. |
| 705 |
expected_source = self.loader.source.decode('utf-8') |
| 706 |
self.assertEqual(self.loader.get_source(self.name), expected_source) |
| 707 |
def raise_OSError(path): |
| 708 |
raise OSError |
| 709 |
self.loader.get_data = raise_OSError |
| 710 |
with self.assertRaises(ImportError) as cm: |
| 711 |
self.loader.get_source(self.name) |
| 712 |
self.assertEqual(cm.exception.name, self.name) |
| 713 |
|
| 714 |
def test_is_package(self): |
| 715 |
# Properly detect when loading a package. |
| 716 |
self.setUp(is_package=False) |
| 717 |
self.assertFalse(self.loader.is_package(self.name)) |
| 718 |
self.setUp(is_package=True) |
| 719 |
self.assertTrue(self.loader.is_package(self.name)) |
| 720 |
self.assertFalse(self.loader.is_package(self.name + '.__init__')) |
| 721 |
|
| 722 |
def test_get_code(self): |
| 723 |
# Verify the code object is created. |
| 724 |
code_object = self.loader.get_code(self.name) |
| 725 |
self.verify_code(code_object) |
| 726 |
|
| 727 |
def test_source_to_code(self): |
| 728 |
# Verify the compiled code object. |
| 729 |
code = self.loader.source_to_code(self.loader.source, self.path) |
| 730 |
self.verify_code(code) |
| 731 |
|
| 732 |
def test_load_module(self): |
| 733 |
# Loading a module should set __name__, __loader__, __package__, |
| 734 |
# __path__ (for packages), __file__, and __cached__. |
| 735 |
# The module should also be put into sys.modules. |
| 736 |
with warnings.catch_warnings(): |
| 737 |
warnings.simplefilter("ignore", ImportWarning) |
| 738 |
with test_util.uncache(self.name): |
| 739 |
with warnings.catch_warnings(): |
| 740 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 741 |
module = self.loader.load_module(self.name) |
| 742 |
self.verify_module(module) |
| 743 |
self.assertEqual(module.__path__, [os.path.dirname(self.path)]) |
| 744 |
self.assertIn(self.name, sys.modules) |
| 745 |
|
| 746 |
def test_package_settings(self): |
| 747 |
# __package__ needs to be set, while __path__ is set on if the module |
| 748 |
# is a package. |
| 749 |
# Testing the values for a package are covered by test_load_module. |
| 750 |
with warnings.catch_warnings(): |
| 751 |
warnings.simplefilter("ignore", ImportWarning) |
| 752 |
self.setUp(is_package=False) |
| 753 |
with test_util.uncache(self.name): |
| 754 |
with warnings.catch_warnings(): |
| 755 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 756 |
module = self.loader.load_module(self.name) |
| 757 |
self.verify_module(module) |
| 758 |
self.assertNotHasAttr(module, '__path__') |
| 759 |
|
| 760 |
def test_get_source_encoding(self): |
| 761 |
# Source is considered encoded in UTF-8 by default unless otherwise |
| 762 |
# specified by an encoding line. |
| 763 |
source = "_ = 'ü'" |
| 764 |
self.loader.source = source.encode('utf-8') |
| 765 |
returned_source = self.loader.get_source(self.name) |
| 766 |
self.assertEqual(returned_source, source) |
| 767 |
source = "# coding: latin-1\n_ = ü" |
| 768 |
self.loader.source = source.encode('latin-1') |
| 769 |
returned_source = self.loader.get_source(self.name) |
| 770 |
self.assertEqual(returned_source, source) |
| 771 |
|
| 772 |
|
| 773 |
(Frozen_SourceOnlyLoaderTests, |
| 774 |
Source_SourceOnlyLoaderTests |
| 775 |
) = test_util.test_both(SourceOnlyLoaderTests, util=util, |
| 776 |
loader_mock=SPLIT_SOL) |
| 777 |
|
| 778 |
|
| 779 |
@unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true") |
| 780 |
class SourceLoaderBytecodeTests(SourceLoaderTestHarness): |
| 781 |
|
| 782 |
"""Test importlib.abc.SourceLoader's use of bytecode. |
| 783 |
|
| 784 |
Source-only testing handled by SourceOnlyLoaderTests. |
| 785 |
|
| 786 |
""" |
| 787 |
|
| 788 |
def verify_code(self, code_object, *, bytecode_written=False): |
| 789 |
super().verify_code(code_object) |
| 790 |
if bytecode_written: |
| 791 |
self.assertIn(self.cached, self.loader.written) |
| 792 |
data = bytearray(self.util.MAGIC_NUMBER) |
| 793 |
data.extend(self.init._pack_uint32(0)) |
| 794 |
data.extend(self.init._pack_uint32(self.loader.source_mtime)) |
| 795 |
data.extend(self.init._pack_uint32(self.loader.source_size)) |
| 796 |
# Make sure there's > 1 reference to code_object so that the |
| 797 |
# marshaled representation below matches the cached representation |
| 798 |
l = [code_object] |
| 799 |
data.extend(marshal.dumps(code_object)) |
| 800 |
self.assertEqual(self.loader.written[self.cached], bytes(data)) |
| 801 |
|
| 802 |
def test_code_with_everything(self): |
| 803 |
# When everything should work. |
| 804 |
code_object = self.loader.get_code(self.name) |
| 805 |
self.verify_code(code_object) |
| 806 |
|
| 807 |
def test_no_bytecode(self): |
| 808 |
# If no bytecode exists then move on to the source. |
| 809 |
self.loader.bytecode_path = "<does not exist>" |
| 810 |
# Sanity check |
| 811 |
with self.assertRaises(OSError): |
| 812 |
bytecode_path = self.util.cache_from_source(self.path) |
| 813 |
self.loader.get_data(bytecode_path) |
| 814 |
code_object = self.loader.get_code(self.name) |
| 815 |
self.verify_code(code_object, bytecode_written=True) |
| 816 |
|
| 817 |
def test_code_bad_timestamp(self): |
| 818 |
# Bytecode is only used when the timestamp matches the source EXACTLY. |
| 819 |
for source_mtime in (0, 2): |
| 820 |
assert source_mtime != self.loader.source_mtime |
| 821 |
original = self.loader.source_mtime |
| 822 |
self.loader.source_mtime = source_mtime |
| 823 |
# If bytecode is used then EOFError would be raised by marshal. |
| 824 |
self.loader.bytecode = self.loader.bytecode[8:] |
| 825 |
code_object = self.loader.get_code(self.name) |
| 826 |
self.verify_code(code_object, bytecode_written=True) |
| 827 |
self.loader.source_mtime = original |
| 828 |
|
| 829 |
def test_code_bad_magic(self): |
| 830 |
# Skip over bytecode with a bad magic number. |
| 831 |
self.setUp(magic=b'0000') |
| 832 |
# If bytecode is used then EOFError would be raised by marshal. |
| 833 |
self.loader.bytecode = self.loader.bytecode[8:] |
| 834 |
code_object = self.loader.get_code(self.name) |
| 835 |
self.verify_code(code_object, bytecode_written=True) |
| 836 |
|
| 837 |
def test_dont_write_bytecode(self): |
| 838 |
# Bytecode is not written if sys.dont_write_bytecode is true. |
| 839 |
# Can assume it is false already thanks to the skipIf class decorator. |
| 840 |
try: |
| 841 |
sys.dont_write_bytecode = True |
| 842 |
self.loader.bytecode_path = "<does not exist>" |
| 843 |
code_object = self.loader.get_code(self.name) |
| 844 |
self.assertNotIn(self.cached, self.loader.written) |
| 845 |
finally: |
| 846 |
sys.dont_write_bytecode = False |
| 847 |
|
| 848 |
def test_no_set_data(self): |
| 849 |
# If set_data is not defined, one can still read bytecode. |
| 850 |
self.setUp(magic=b'0000') |
| 851 |
original_set_data = self.loader.__class__.mro()[1].set_data |
| 852 |
try: |
| 853 |
del self.loader.__class__.mro()[1].set_data |
| 854 |
code_object = self.loader.get_code(self.name) |
| 855 |
self.verify_code(code_object) |
| 856 |
finally: |
| 857 |
self.loader.__class__.mro()[1].set_data = original_set_data |
| 858 |
|
| 859 |
def test_set_data_raises_exceptions(self): |
| 860 |
# Raising NotImplementedError or OSError is okay for set_data. |
| 861 |
def raise_exception(exc): |
| 862 |
def closure(*args, **kwargs): |
| 863 |
raise exc |
| 864 |
return closure |
| 865 |
|
| 866 |
self.setUp(magic=b'0000') |
| 867 |
self.loader.set_data = raise_exception(NotImplementedError) |
| 868 |
code_object = self.loader.get_code(self.name) |
| 869 |
self.verify_code(code_object) |
| 870 |
|
| 871 |
|
| 872 |
(Frozen_SLBytecodeTests, |
| 873 |
SourceSLBytecodeTests |
| 874 |
) = test_util.test_both(SourceLoaderBytecodeTests, init=init, util=util, |
| 875 |
loader_mock=SPLIT_SL) |
| 876 |
|
| 877 |
|
| 878 |
class SourceLoaderGetSourceTests: |
| 879 |
|
| 880 |
"""Tests for importlib.abc.SourceLoader.get_source().""" |
| 881 |
|
| 882 |
def test_default_encoding(self): |
| 883 |
# Should have no problems with UTF-8 text. |
| 884 |
name = 'mod' |
| 885 |
mock = self.SourceOnlyLoaderMock('mod.file') |
| 886 |
source = 'x = "ü"' |
| 887 |
mock.source = source.encode('utf-8') |
| 888 |
returned_source = mock.get_source(name) |
| 889 |
self.assertEqual(returned_source, source) |
| 890 |
|
| 891 |
def test_decoded_source(self): |
| 892 |
# Decoding should work. |
| 893 |
name = 'mod' |
| 894 |
mock = self.SourceOnlyLoaderMock("mod.file") |
| 895 |
source = "# coding: Latin-1\nx='ü'" |
| 896 |
assert source.encode('latin-1') != source.encode('utf-8') |
| 897 |
mock.source = source.encode('latin-1') |
| 898 |
returned_source = mock.get_source(name) |
| 899 |
self.assertEqual(returned_source, source) |
| 900 |
|
| 901 |
def test_universal_newlines(self): |
| 902 |
# PEP 302 says universal newlines should be used. |
| 903 |
name = 'mod' |
| 904 |
mock = self.SourceOnlyLoaderMock('mod.file') |
| 905 |
source = "x = 42\r\ny = -13\r\n" |
| 906 |
mock.source = source.encode('utf-8') |
| 907 |
expect = io.IncrementalNewlineDecoder(None, True).decode(source) |
| 908 |
self.assertEqual(mock.get_source(name), expect) |
| 909 |
|
| 910 |
|
| 911 |
(Frozen_SourceOnlyLoaderGetSourceTests, |
| 912 |
Source_SourceOnlyLoaderGetSourceTests |
| 913 |
) = test_util.test_both(SourceLoaderGetSourceTests, |
| 914 |
SourceOnlyLoaderMock=SPLIT_SOL) |
| 915 |
|
| 916 |
|
| 917 |
class SourceLoaderDeprecationWarningsTests(unittest.TestCase): |
| 918 |
"""Tests SourceLoader deprecation warnings.""" |
| 919 |
|
| 920 |
def test_deprecated_path_mtime(self): |
| 921 |
from importlib.abc import SourceLoader |
| 922 |
class DummySourceLoader(SourceLoader): |
| 923 |
def get_data(self, path): |
| 924 |
return b'' |
| 925 |
|
| 926 |
def get_filename(self, fullname): |
| 927 |
return 'foo.py' |
| 928 |
|
| 929 |
def path_stats(self, path): |
| 930 |
return {'mtime': 1} |
| 931 |
|
| 932 |
loader = DummySourceLoader() |
| 933 |
|
| 934 |
with self.assertWarnsRegex( |
| 935 |
DeprecationWarning, |
| 936 |
r"SourceLoader\.path_mtime is deprecated in favour of " |
| 937 |
r"SourceLoader\.path_stats\(\)\." |
| 938 |
): |
| 939 |
loader.path_mtime('foo.py') |
| 940 |
|
| 941 |
|
| 942 |
if __name__ == '__main__': |
| 943 |
unittest.main() |