1from . import util as test_util 2 3init = test_util.import_importlib('importlib') 4machinery = test_util.import_importlib('importlib.machinery') 5util = test_util.import_importlib('importlib.util') 6 7import os.path 8import pathlib 9from test.support.import_helper import CleanImport 10import unittest 11import sys 12import warnings 13 14 15 16class TestLoader: 17 18 def __init__(self, path=None, is_package=None): 19 self.path = path 20 self.package = is_package 21 22 def __repr__(self): 23 return '<TestLoader object>' 24 25 def __getattr__(self, name): 26 if name == 'get_filename' and self.path is not None: 27 return self._get_filename 28 if name == 'is_package': 29 return self._is_package 30 raise AttributeError(name) 31 32 def _get_filename(self, name): 33 return self.path 34 35 def _is_package(self, name): 36 return self.package 37 38 def create_module(self, spec): 39 return None 40 41 42class NewLoader(TestLoader): 43 44 EGGS = 1 45 46 def exec_module(self, module): 47 module.eggs = self.EGGS 48 49 50class LegacyLoader(TestLoader): 51 52 HAM = -1 53 54 with warnings.catch_warnings(): 55 warnings.simplefilter("ignore", DeprecationWarning) 56 57 frozen_util = util['Frozen'] 58 59 @frozen_util.module_for_loader 60 def load_module(self, module): 61 module.ham = self.HAM 62 return module 63 64 65class ModuleSpecTests: 66 67 def setUp(self): 68 self.name = 'spam' 69 self.path = 'spam.py' 70 self.cached = self.util.cache_from_source(self.path) 71 self.loader = TestLoader() 72 self.spec = self.machinery.ModuleSpec(self.name, self.loader) 73 self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader, 74 origin=self.path) 75 self.loc_spec._set_fileattr = True 76 77 def test_default(self): 78 spec = self.machinery.ModuleSpec(self.name, self.loader) 79 80 self.assertEqual(spec.name, self.name) 81 self.assertEqual(spec.loader, self.loader) 82 self.assertIs(spec.origin, None) 83 self.assertIs(spec.loader_state, None) 84 self.assertIs(spec.submodule_search_locations, None) 85 self.assertIs(spec.cached, None) 86 self.assertFalse(spec.has_location) 87 88 def test_default_no_loader(self): 89 spec = self.machinery.ModuleSpec(self.name, None) 90 91 self.assertEqual(spec.name, self.name) 92 self.assertIs(spec.loader, None) 93 self.assertIs(spec.origin, None) 94 self.assertIs(spec.loader_state, None) 95 self.assertIs(spec.submodule_search_locations, None) 96 self.assertIs(spec.cached, None) 97 self.assertFalse(spec.has_location) 98 99 def test_default_is_package_false(self): 100 spec = self.machinery.ModuleSpec(self.name, self.loader, 101 is_package=False) 102 103 self.assertEqual(spec.name, self.name) 104 self.assertEqual(spec.loader, self.loader) 105 self.assertIs(spec.origin, None) 106 self.assertIs(spec.loader_state, None) 107 self.assertIs(spec.submodule_search_locations, None) 108 self.assertIs(spec.cached, None) 109 self.assertFalse(spec.has_location) 110 111 def test_default_is_package_true(self): 112 spec = self.machinery.ModuleSpec(self.name, self.loader, 113 is_package=True) 114 115 self.assertEqual(spec.name, self.name) 116 self.assertEqual(spec.loader, self.loader) 117 self.assertIs(spec.origin, None) 118 self.assertIs(spec.loader_state, None) 119 self.assertEqual(spec.submodule_search_locations, []) 120 self.assertIs(spec.cached, None) 121 self.assertFalse(spec.has_location) 122 123 def test_has_location_setter(self): 124 spec = self.machinery.ModuleSpec(self.name, self.loader, 125 origin='somewhere') 126 self.assertFalse(spec.has_location) 127 spec.has_location = True 128 self.assertTrue(spec.has_location) 129 130 def test_equality(self): 131 other = type(sys.implementation)(name=self.name, 132 loader=self.loader, 133 origin=None, 134 submodule_search_locations=None, 135 has_location=False, 136 cached=None, 137 ) 138 139 self.assertTrue(self.spec == other) 140 141 def test_equality_location(self): 142 other = type(sys.implementation)(name=self.name, 143 loader=self.loader, 144 origin=self.path, 145 submodule_search_locations=None, 146 has_location=True, 147 cached=self.cached, 148 ) 149 150 self.assertEqual(self.loc_spec, other) 151 152 def test_inequality(self): 153 other = type(sys.implementation)(name='ham', 154 loader=self.loader, 155 origin=None, 156 submodule_search_locations=None, 157 has_location=False, 158 cached=None, 159 ) 160 161 self.assertNotEqual(self.spec, other) 162 163 def test_inequality_incomplete(self): 164 other = type(sys.implementation)(name=self.name, 165 loader=self.loader, 166 ) 167 168 self.assertNotEqual(self.spec, other) 169 170 def test_package(self): 171 spec = self.machinery.ModuleSpec('spam.eggs', self.loader) 172 173 self.assertEqual(spec.parent, 'spam') 174 175 def test_package_is_package(self): 176 spec = self.machinery.ModuleSpec('spam.eggs', self.loader, 177 is_package=True) 178 179 self.assertEqual(spec.parent, 'spam.eggs') 180 181 # cached 182 183 def test_cached_set(self): 184 before = self.spec.cached 185 self.spec.cached = 'there' 186 after = self.spec.cached 187 188 self.assertIs(before, None) 189 self.assertEqual(after, 'there') 190 191 def test_cached_no_origin(self): 192 spec = self.machinery.ModuleSpec(self.name, self.loader) 193 194 self.assertIs(spec.cached, None) 195 196 def test_cached_with_origin_not_location(self): 197 spec = self.machinery.ModuleSpec(self.name, self.loader, 198 origin=self.path) 199 200 self.assertIs(spec.cached, None) 201 202 def test_cached_source(self): 203 expected = self.util.cache_from_source(self.path) 204 205 self.assertEqual(self.loc_spec.cached, expected) 206 207 def test_cached_source_unknown_suffix(self): 208 self.loc_spec.origin = 'spam.spamspamspam' 209 210 self.assertIs(self.loc_spec.cached, None) 211 212 def test_cached_source_missing_cache_tag(self): 213 original = sys.implementation.cache_tag 214 sys.implementation.cache_tag = None 215 try: 216 cached = self.loc_spec.cached 217 finally: 218 sys.implementation.cache_tag = original 219 220 self.assertIs(cached, None) 221 222 def test_cached_sourceless(self): 223 self.loc_spec.origin = 'spam.pyc' 224 225 self.assertEqual(self.loc_spec.cached, 'spam.pyc') 226 227 228(Frozen_ModuleSpecTests, 229 Source_ModuleSpecTests 230 ) = test_util.test_both(ModuleSpecTests, util=util, machinery=machinery) 231 232 233class ModuleSpecMethodsTests: 234 235 @property 236 def bootstrap(self): 237 return self.init._bootstrap 238 239 def setUp(self): 240 self.name = 'spam' 241 self.path = 'spam.py' 242 self.cached = self.util.cache_from_source(self.path) 243 self.loader = TestLoader() 244 self.spec = self.machinery.ModuleSpec(self.name, self.loader) 245 self.loc_spec = self.machinery.ModuleSpec(self.name, self.loader, 246 origin=self.path) 247 self.loc_spec._set_fileattr = True 248 249 # exec() 250 251 def test_exec(self): 252 self.spec.loader = NewLoader() 253 module = self.util.module_from_spec(self.spec) 254 sys.modules[self.name] = module 255 self.assertFalse(hasattr(module, 'eggs')) 256 self.bootstrap._exec(self.spec, module) 257 258 self.assertEqual(module.eggs, 1) 259 260 # load() 261 262 def test_load(self): 263 self.spec.loader = NewLoader() 264 with CleanImport(self.spec.name): 265 loaded = self.bootstrap._load(self.spec) 266 installed = sys.modules[self.spec.name] 267 268 self.assertEqual(loaded.eggs, 1) 269 self.assertIs(loaded, installed) 270 271 def test_load_replaced(self): 272 replacement = object() 273 class ReplacingLoader(TestLoader): 274 def exec_module(self, module): 275 sys.modules[module.__name__] = replacement 276 self.spec.loader = ReplacingLoader() 277 with CleanImport(self.spec.name): 278 loaded = self.bootstrap._load(self.spec) 279 installed = sys.modules[self.spec.name] 280 281 self.assertIs(loaded, replacement) 282 self.assertIs(installed, replacement) 283 284 def test_load_failed(self): 285 class FailedLoader(TestLoader): 286 def exec_module(self, module): 287 raise RuntimeError 288 self.spec.loader = FailedLoader() 289 with CleanImport(self.spec.name): 290 with self.assertRaises(RuntimeError): 291 loaded = self.bootstrap._load(self.spec) 292 self.assertNotIn(self.spec.name, sys.modules) 293 294 def test_load_failed_removed(self): 295 class FailedLoader(TestLoader): 296 def exec_module(self, module): 297 del sys.modules[module.__name__] 298 raise RuntimeError 299 self.spec.loader = FailedLoader() 300 with CleanImport(self.spec.name): 301 with self.assertRaises(RuntimeError): 302 loaded = self.bootstrap._load(self.spec) 303 self.assertNotIn(self.spec.name, sys.modules) 304 305 def test_load_legacy(self): 306 with warnings.catch_warnings(): 307 warnings.simplefilter("ignore", ImportWarning) 308 self.spec.loader = LegacyLoader() 309 with CleanImport(self.spec.name): 310 loaded = self.bootstrap._load(self.spec) 311 312 self.assertEqual(loaded.ham, -1) 313 314 def test_load_legacy_attributes(self): 315 with warnings.catch_warnings(): 316 warnings.simplefilter("ignore", ImportWarning) 317 self.spec.loader = LegacyLoader() 318 with CleanImport(self.spec.name): 319 loaded = self.bootstrap._load(self.spec) 320 321 self.assertIs(loaded.__loader__, self.spec.loader) 322 self.assertEqual(loaded.__package__, self.spec.parent) 323 self.assertIs(loaded.__spec__, self.spec) 324 325 def test_load_legacy_attributes_immutable(self): 326 module = object() 327 with warnings.catch_warnings(): 328 warnings.simplefilter("ignore", ImportWarning) 329 class ImmutableLoader(TestLoader): 330 def load_module(self, name): 331 sys.modules[name] = module 332 return module 333 self.spec.loader = ImmutableLoader() 334 with CleanImport(self.spec.name): 335 loaded = self.bootstrap._load(self.spec) 336 337 self.assertIs(sys.modules[self.spec.name], module) 338 339 # reload() 340 341 def test_reload(self): 342 self.spec.loader = NewLoader() 343 with CleanImport(self.spec.name): 344 loaded = self.bootstrap._load(self.spec) 345 reloaded = self.bootstrap._exec(self.spec, loaded) 346 installed = sys.modules[self.spec.name] 347 348 self.assertEqual(loaded.eggs, 1) 349 self.assertIs(reloaded, loaded) 350 self.assertIs(installed, loaded) 351 352 def test_reload_modified(self): 353 self.spec.loader = NewLoader() 354 with CleanImport(self.spec.name): 355 loaded = self.bootstrap._load(self.spec) 356 loaded.eggs = 2 357 reloaded = self.bootstrap._exec(self.spec, loaded) 358 359 self.assertEqual(loaded.eggs, 1) 360 self.assertIs(reloaded, loaded) 361 362 def test_reload_extra_attributes(self): 363 self.spec.loader = NewLoader() 364 with CleanImport(self.spec.name): 365 loaded = self.bootstrap._load(self.spec) 366 loaded.available = False 367 reloaded = self.bootstrap._exec(self.spec, loaded) 368 369 self.assertFalse(loaded.available) 370 self.assertIs(reloaded, loaded) 371 372 def test_reload_init_module_attrs(self): 373 self.spec.loader = NewLoader() 374 with CleanImport(self.spec.name): 375 loaded = self.bootstrap._load(self.spec) 376 loaded.__name__ = 'ham' 377 del loaded.__loader__ 378 del loaded.__package__ 379 del loaded.__spec__ 380 self.bootstrap._exec(self.spec, loaded) 381 382 self.assertEqual(loaded.__name__, self.spec.name) 383 self.assertIs(loaded.__loader__, self.spec.loader) 384 self.assertEqual(loaded.__package__, self.spec.parent) 385 self.assertIs(loaded.__spec__, self.spec) 386 self.assertFalse(hasattr(loaded, '__path__')) 387 self.assertFalse(hasattr(loaded, '__file__')) 388 self.assertFalse(hasattr(loaded, '__cached__')) 389 390 def test_reload_legacy(self): 391 with warnings.catch_warnings(): 392 warnings.simplefilter("ignore", ImportWarning) 393 self.spec.loader = LegacyLoader() 394 with CleanImport(self.spec.name): 395 loaded = self.bootstrap._load(self.spec) 396 reloaded = self.bootstrap._exec(self.spec, loaded) 397 installed = sys.modules[self.spec.name] 398 399 self.assertEqual(loaded.ham, -1) 400 self.assertIs(reloaded, loaded) 401 self.assertIs(installed, loaded) 402 403 404(Frozen_ModuleSpecMethodsTests, 405 Source_ModuleSpecMethodsTests 406 ) = test_util.test_both(ModuleSpecMethodsTests, init=init, util=util, 407 machinery=machinery) 408 409 410class ModuleReprTests: 411 412 @property 413 def bootstrap(self): 414 return self.init._bootstrap 415 416 def setUp(self): 417 self.module = type(os)('spam') 418 self.spec = self.machinery.ModuleSpec('spam', TestLoader()) 419 420 def test_module___loader___module_repr(self): 421 class Loader: 422 def module_repr(self, module): 423 return '<delicious {}>'.format(module.__name__) 424 self.module.__loader__ = Loader() 425 modrepr = self.bootstrap._module_repr(self.module) 426 427 self.assertEqual(modrepr, '<delicious spam>') 428 429 def test_module___loader___module_repr_bad(self): 430 class Loader(TestLoader): 431 def module_repr(self, module): 432 raise Exception 433 self.module.__loader__ = Loader() 434 modrepr = self.bootstrap._module_repr(self.module) 435 436 self.assertEqual(modrepr, 437 '<module {!r} (<TestLoader object>)>'.format('spam')) 438 439 def test_module___spec__(self): 440 origin = 'in a hole, in the ground' 441 self.spec.origin = origin 442 self.module.__spec__ = self.spec 443 modrepr = self.bootstrap._module_repr(self.module) 444 445 self.assertEqual(modrepr, '<module {!r} ({})>'.format('spam', origin)) 446 447 def test_module___spec___location(self): 448 location = 'in_a_galaxy_far_far_away.py' 449 self.spec.origin = location 450 self.spec._set_fileattr = True 451 self.module.__spec__ = self.spec 452 modrepr = self.bootstrap._module_repr(self.module) 453 454 self.assertEqual(modrepr, 455 '<module {!r} from {!r}>'.format('spam', location)) 456 457 def test_module___spec___no_origin(self): 458 self.spec.loader = TestLoader() 459 self.module.__spec__ = self.spec 460 modrepr = self.bootstrap._module_repr(self.module) 461 462 self.assertEqual(modrepr, 463 '<module {!r} (<TestLoader object>)>'.format('spam')) 464 465 def test_module___spec___no_origin_no_loader(self): 466 self.spec.loader = None 467 self.module.__spec__ = self.spec 468 modrepr = self.bootstrap._module_repr(self.module) 469 470 self.assertEqual(modrepr, '<module {!r}>'.format('spam')) 471 472 def test_module_no_name(self): 473 del self.module.__name__ 474 modrepr = self.bootstrap._module_repr(self.module) 475 476 self.assertEqual(modrepr, '<module {!r}>'.format('?')) 477 478 def test_module_with_file(self): 479 filename = 'e/i/e/i/o/spam.py' 480 self.module.__file__ = filename 481 modrepr = self.bootstrap._module_repr(self.module) 482 483 self.assertEqual(modrepr, 484 '<module {!r} from {!r}>'.format('spam', filename)) 485 486 def test_module_no_file(self): 487 self.module.__loader__ = TestLoader() 488 modrepr = self.bootstrap._module_repr(self.module) 489 490 self.assertEqual(modrepr, 491 '<module {!r} (<TestLoader object>)>'.format('spam')) 492 493 def test_module_no_file_no_loader(self): 494 modrepr = self.bootstrap._module_repr(self.module) 495 496 self.assertEqual(modrepr, '<module {!r}>'.format('spam')) 497 498 499(Frozen_ModuleReprTests, 500 Source_ModuleReprTests 501 ) = test_util.test_both(ModuleReprTests, init=init, util=util, 502 machinery=machinery) 503 504 505class FactoryTests: 506 507 def setUp(self): 508 self.name = 'spam' 509 self.path = os.path.abspath('spam.py') 510 self.cached = self.util.cache_from_source(self.path) 511 self.loader = TestLoader() 512 self.fileloader = TestLoader(self.path) 513 self.pkgloader = TestLoader(self.path, True) 514 515 # spec_from_loader() 516 517 def test_spec_from_loader_default(self): 518 spec = self.util.spec_from_loader(self.name, self.loader) 519 520 self.assertEqual(spec.name, self.name) 521 self.assertEqual(spec.loader, self.loader) 522 self.assertIs(spec.origin, None) 523 self.assertIs(spec.loader_state, None) 524 self.assertIs(spec.submodule_search_locations, None) 525 self.assertIs(spec.cached, None) 526 self.assertFalse(spec.has_location) 527 528 def test_spec_from_loader_default_with_bad_is_package(self): 529 class Loader: 530 def is_package(self, name): 531 raise ImportError 532 loader = Loader() 533 spec = self.util.spec_from_loader(self.name, loader) 534 535 self.assertEqual(spec.name, self.name) 536 self.assertEqual(spec.loader, loader) 537 self.assertIs(spec.origin, None) 538 self.assertIs(spec.loader_state, None) 539 self.assertIs(spec.submodule_search_locations, None) 540 self.assertIs(spec.cached, None) 541 self.assertFalse(spec.has_location) 542 543 def test_spec_from_loader_origin(self): 544 origin = 'somewhere over the rainbow' 545 spec = self.util.spec_from_loader(self.name, self.loader, 546 origin=origin) 547 548 self.assertEqual(spec.name, self.name) 549 self.assertEqual(spec.loader, self.loader) 550 self.assertIs(spec.origin, origin) 551 self.assertIs(spec.loader_state, None) 552 self.assertIs(spec.submodule_search_locations, None) 553 self.assertIs(spec.cached, None) 554 self.assertFalse(spec.has_location) 555 556 def test_spec_from_loader_is_package_false(self): 557 spec = self.util.spec_from_loader(self.name, self.loader, 558 is_package=False) 559 560 self.assertEqual(spec.name, self.name) 561 self.assertEqual(spec.loader, self.loader) 562 self.assertIs(spec.origin, None) 563 self.assertIs(spec.loader_state, None) 564 self.assertIs(spec.submodule_search_locations, None) 565 self.assertIs(spec.cached, None) 566 self.assertFalse(spec.has_location) 567 568 def test_spec_from_loader_is_package_true(self): 569 spec = self.util.spec_from_loader(self.name, self.loader, 570 is_package=True) 571 572 self.assertEqual(spec.name, self.name) 573 self.assertEqual(spec.loader, self.loader) 574 self.assertIs(spec.origin, None) 575 self.assertIs(spec.loader_state, None) 576 self.assertEqual(spec.submodule_search_locations, []) 577 self.assertIs(spec.cached, None) 578 self.assertFalse(spec.has_location) 579 580 def test_spec_from_loader_origin_and_is_package(self): 581 origin = 'where the streets have no name' 582 spec = self.util.spec_from_loader(self.name, self.loader, 583 origin=origin, is_package=True) 584 585 self.assertEqual(spec.name, self.name) 586 self.assertEqual(spec.loader, self.loader) 587 self.assertIs(spec.origin, origin) 588 self.assertIs(spec.loader_state, None) 589 self.assertEqual(spec.submodule_search_locations, []) 590 self.assertIs(spec.cached, None) 591 self.assertFalse(spec.has_location) 592 593 def test_spec_from_loader_is_package_with_loader_false(self): 594 loader = TestLoader(is_package=False) 595 spec = self.util.spec_from_loader(self.name, loader) 596 597 self.assertEqual(spec.name, self.name) 598 self.assertEqual(spec.loader, loader) 599 self.assertIs(spec.origin, None) 600 self.assertIs(spec.loader_state, None) 601 self.assertIs(spec.submodule_search_locations, None) 602 self.assertIs(spec.cached, None) 603 self.assertFalse(spec.has_location) 604 605 def test_spec_from_loader_is_package_with_loader_true(self): 606 loader = TestLoader(is_package=True) 607 spec = self.util.spec_from_loader(self.name, loader) 608 609 self.assertEqual(spec.name, self.name) 610 self.assertEqual(spec.loader, loader) 611 self.assertIs(spec.origin, None) 612 self.assertIs(spec.loader_state, None) 613 self.assertEqual(spec.submodule_search_locations, []) 614 self.assertIs(spec.cached, None) 615 self.assertFalse(spec.has_location) 616 617 def test_spec_from_loader_default_with_file_loader(self): 618 spec = self.util.spec_from_loader(self.name, self.fileloader) 619 620 self.assertEqual(spec.name, self.name) 621 self.assertEqual(spec.loader, self.fileloader) 622 self.assertEqual(spec.origin, self.path) 623 self.assertIs(spec.loader_state, None) 624 self.assertIs(spec.submodule_search_locations, None) 625 self.assertEqual(spec.cached, self.cached) 626 self.assertTrue(spec.has_location) 627 628 def test_spec_from_loader_is_package_false_with_fileloader(self): 629 spec = self.util.spec_from_loader(self.name, self.fileloader, 630 is_package=False) 631 632 self.assertEqual(spec.name, self.name) 633 self.assertEqual(spec.loader, self.fileloader) 634 self.assertEqual(spec.origin, self.path) 635 self.assertIs(spec.loader_state, None) 636 self.assertIs(spec.submodule_search_locations, None) 637 self.assertEqual(spec.cached, self.cached) 638 self.assertTrue(spec.has_location) 639 640 def test_spec_from_loader_is_package_true_with_fileloader(self): 641 spec = self.util.spec_from_loader(self.name, self.fileloader, 642 is_package=True) 643 644 self.assertEqual(spec.name, self.name) 645 self.assertEqual(spec.loader, self.fileloader) 646 self.assertEqual(spec.origin, self.path) 647 self.assertIs(spec.loader_state, None) 648 self.assertEqual(spec.submodule_search_locations, [os.getcwd()]) 649 self.assertEqual(spec.cached, self.cached) 650 self.assertTrue(spec.has_location) 651 652 # spec_from_file_location() 653 654 def test_spec_from_file_location_default(self): 655 spec = self.util.spec_from_file_location(self.name, self.path) 656 657 self.assertEqual(spec.name, self.name) 658 # Need to use a circuitous route to get at importlib.machinery to make 659 # sure the same class object is used in the isinstance() check as 660 # would have been used to create the loader. 661 SourceFileLoader = self.util.spec_from_file_location.__globals__['SourceFileLoader'] 662 self.assertIsInstance(spec.loader, SourceFileLoader) 663 self.assertEqual(spec.loader.name, self.name) 664 self.assertEqual(spec.loader.path, self.path) 665 self.assertEqual(spec.origin, self.path) 666 self.assertIs(spec.loader_state, None) 667 self.assertIs(spec.submodule_search_locations, None) 668 self.assertEqual(spec.cached, self.cached) 669 self.assertTrue(spec.has_location) 670 671 def test_spec_from_file_location_path_like_arg(self): 672 spec = self.util.spec_from_file_location(self.name, 673 pathlib.PurePath(self.path)) 674 self.assertEqual(spec.origin, self.path) 675 676 def test_spec_from_file_location_default_without_location(self): 677 spec = self.util.spec_from_file_location(self.name) 678 679 self.assertIs(spec, None) 680 681 def test_spec_from_file_location_default_bad_suffix(self): 682 spec = self.util.spec_from_file_location(self.name, 'spam.eggs') 683 684 self.assertIs(spec, None) 685 686 def test_spec_from_file_location_loader_no_location(self): 687 spec = self.util.spec_from_file_location(self.name, 688 loader=self.fileloader) 689 690 self.assertEqual(spec.name, self.name) 691 self.assertEqual(spec.loader, self.fileloader) 692 self.assertEqual(spec.origin, self.path) 693 self.assertIs(spec.loader_state, None) 694 self.assertIs(spec.submodule_search_locations, None) 695 self.assertEqual(spec.cached, self.cached) 696 self.assertTrue(spec.has_location) 697 698 def test_spec_from_file_location_loader_no_location_no_get_filename(self): 699 spec = self.util.spec_from_file_location(self.name, 700 loader=self.loader) 701 702 self.assertEqual(spec.name, self.name) 703 self.assertEqual(spec.loader, self.loader) 704 self.assertEqual(spec.origin, '<unknown>') 705 self.assertIs(spec.loader_state, None) 706 self.assertIs(spec.submodule_search_locations, None) 707 self.assertIs(spec.cached, None) 708 self.assertTrue(spec.has_location) 709 710 def test_spec_from_file_location_loader_no_location_bad_get_filename(self): 711 class Loader: 712 def get_filename(self, name): 713 raise ImportError 714 loader = Loader() 715 spec = self.util.spec_from_file_location(self.name, loader=loader) 716 717 self.assertEqual(spec.name, self.name) 718 self.assertEqual(spec.loader, loader) 719 self.assertEqual(spec.origin, '<unknown>') 720 self.assertIs(spec.loader_state, None) 721 self.assertIs(spec.submodule_search_locations, None) 722 self.assertIs(spec.cached, None) 723 self.assertTrue(spec.has_location) 724 725 def test_spec_from_file_location_smsl_none(self): 726 spec = self.util.spec_from_file_location(self.name, self.path, 727 loader=self.fileloader, 728 submodule_search_locations=None) 729 730 self.assertEqual(spec.name, self.name) 731 self.assertEqual(spec.loader, self.fileloader) 732 self.assertEqual(spec.origin, self.path) 733 self.assertIs(spec.loader_state, None) 734 self.assertIs(spec.submodule_search_locations, None) 735 self.assertEqual(spec.cached, self.cached) 736 self.assertTrue(spec.has_location) 737 738 def test_spec_from_file_location_smsl_empty(self): 739 spec = self.util.spec_from_file_location(self.name, self.path, 740 loader=self.fileloader, 741 submodule_search_locations=[]) 742 743 self.assertEqual(spec.name, self.name) 744 self.assertEqual(spec.loader, self.fileloader) 745 self.assertEqual(spec.origin, self.path) 746 self.assertIs(spec.loader_state, None) 747 self.assertEqual(spec.submodule_search_locations, [os.getcwd()]) 748 self.assertEqual(spec.cached, self.cached) 749 self.assertTrue(spec.has_location) 750 751 def test_spec_from_file_location_smsl_not_empty(self): 752 spec = self.util.spec_from_file_location(self.name, self.path, 753 loader=self.fileloader, 754 submodule_search_locations=['eggs']) 755 756 self.assertEqual(spec.name, self.name) 757 self.assertEqual(spec.loader, self.fileloader) 758 self.assertEqual(spec.origin, self.path) 759 self.assertIs(spec.loader_state, None) 760 self.assertEqual(spec.submodule_search_locations, ['eggs']) 761 self.assertEqual(spec.cached, self.cached) 762 self.assertTrue(spec.has_location) 763 764 def test_spec_from_file_location_smsl_default(self): 765 spec = self.util.spec_from_file_location(self.name, self.path, 766 loader=self.pkgloader) 767 768 self.assertEqual(spec.name, self.name) 769 self.assertEqual(spec.loader, self.pkgloader) 770 self.assertEqual(spec.origin, self.path) 771 self.assertIs(spec.loader_state, None) 772 self.assertEqual(spec.submodule_search_locations, [os.getcwd()]) 773 self.assertEqual(spec.cached, self.cached) 774 self.assertTrue(spec.has_location) 775 776 def test_spec_from_file_location_smsl_default_not_package(self): 777 class Loader: 778 def is_package(self, name): 779 return False 780 loader = Loader() 781 spec = self.util.spec_from_file_location(self.name, self.path, 782 loader=loader) 783 784 self.assertEqual(spec.name, self.name) 785 self.assertEqual(spec.loader, loader) 786 self.assertEqual(spec.origin, self.path) 787 self.assertIs(spec.loader_state, None) 788 self.assertIs(spec.submodule_search_locations, None) 789 self.assertEqual(spec.cached, self.cached) 790 self.assertTrue(spec.has_location) 791 792 def test_spec_from_file_location_smsl_default_no_is_package(self): 793 spec = self.util.spec_from_file_location(self.name, self.path, 794 loader=self.fileloader) 795 796 self.assertEqual(spec.name, self.name) 797 self.assertEqual(spec.loader, self.fileloader) 798 self.assertEqual(spec.origin, self.path) 799 self.assertIs(spec.loader_state, None) 800 self.assertIs(spec.submodule_search_locations, None) 801 self.assertEqual(spec.cached, self.cached) 802 self.assertTrue(spec.has_location) 803 804 def test_spec_from_file_location_smsl_default_bad_is_package(self): 805 class Loader: 806 def is_package(self, name): 807 raise ImportError 808 loader = Loader() 809 spec = self.util.spec_from_file_location(self.name, self.path, 810 loader=loader) 811 812 self.assertEqual(spec.name, self.name) 813 self.assertEqual(spec.loader, loader) 814 self.assertEqual(spec.origin, self.path) 815 self.assertIs(spec.loader_state, None) 816 self.assertIs(spec.submodule_search_locations, None) 817 self.assertEqual(spec.cached, self.cached) 818 self.assertTrue(spec.has_location) 819 820 def test_spec_from_file_location_relative_path(self): 821 spec = self.util.spec_from_file_location(self.name, 822 os.path.basename(self.path), loader=self.fileloader) 823 824 self.assertEqual(spec.name, self.name) 825 self.assertEqual(spec.loader, self.fileloader) 826 self.assertEqual(spec.origin, self.path) 827 self.assertIs(spec.loader_state, None) 828 self.assertIs(spec.submodule_search_locations, None) 829 self.assertEqual(spec.cached, self.cached) 830 self.assertTrue(spec.has_location) 831 832(Frozen_FactoryTests, 833 Source_FactoryTests 834 ) = test_util.test_both(FactoryTests, util=util, machinery=machinery) 835 836 837if __name__ == '__main__': 838 unittest.main() 839