Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(808)

Delta Between Two Patch Sets: Lib/importlib/_bootstrap.py

Issue 18864: Implementation for PEP 451 (importlib.machinery.ModuleSpec)
Left Patch Set: Created 5 years, 7 months ago
Right Patch Set: Created 5 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/importlib.rst ('k') | Lib/test/test_importlib/test_spec.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Core implementation of import. 1 """Core implementation of import.
2 2
3 This module is NOT meant to be directly imported! It has been designed such 3 This module is NOT meant to be directly imported! It has been designed such
4 that it can be bootstrapped into Python as the implementation of import. As 4 that it can be bootstrapped into Python as the implementation of import. As
5 such it requires the injection of specific modules and attributes in order to 5 such it requires the injection of specific modules and attributes in order to
6 work. One should use importlib as the public-facing version of this module. 6 work. One should use importlib as the public-facing version of this module.
7 7
8 """ 8 """
9 # 9 #
10 # IMPORTANT: Whenever making changes to this module, be sure to run 10 # IMPORTANT: Whenever making changes to this module, be sure to run
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 if len(path_separators) == 1: 58 if len(path_separators) == 1:
59 front, _, tail = path.rpartition(path_sep) 59 front, _, tail = path.rpartition(path_sep)
60 return front, tail 60 return front, tail
61 for x in reversed(path): 61 for x in reversed(path):
62 if x in path_separators: 62 if x in path_separators:
63 front, tail = path.rsplit(x, maxsplit=1) 63 front, tail = path.rsplit(x, maxsplit=1)
64 return front, tail 64 return front, tail
65 return '', path 65 return '', path
66 66
67 67
68 def _path_stat(path):
69 """Stat the path.
70
71 Made a separate function to make it easier to override in experiments
72 (e.g. cache stat results).
73
74 """
75 return _os.stat(path)
76
77
68 def _path_is_mode_type(path, mode): 78 def _path_is_mode_type(path, mode):
69 """Test whether the path is the specified mode type.""" 79 """Test whether the path is the specified mode type."""
70 try: 80 try:
71 stat_info = _os.stat(path) 81 stat_info = _path_stat(path)
72 except OSError: 82 except OSError:
73 return False 83 return False
74 return (stat_info.st_mode & 0o170000) == mode 84 return (stat_info.st_mode & 0o170000) == mode
75 85
76 86
77 # XXX Could also expose Modules/getpath.c:isfile()
78 def _path_isfile(path): 87 def _path_isfile(path):
79 """Replacement for os.path.isfile.""" 88 """Replacement for os.path.isfile."""
80 return _path_is_mode_type(path, 0o100000) 89 return _path_is_mode_type(path, 0o100000)
81 90
82 91
83 # XXX Could also expose Modules/getpath.c:isdir()
84 def _path_isdir(path): 92 def _path_isdir(path):
85 """Replacement for os.path.isdir.""" 93 """Replacement for os.path.isdir."""
86 if not path: 94 if not path:
87 path = _os.getcwd() 95 path = _os.getcwd()
88 return _path_is_mode_type(path, 0o040000) 96 return _path_is_mode_type(path, 0o040000)
89 97
90 98
91 def _write_atomic(path, data, mode=0o666): 99 def _write_atomic(path, data, mode=0o666):
92 """Best-effort function to write data to a path atomically. 100 """Best-effort function to write data to a path atomically.
93 Be prepared to handle a FileExistsError if concurrent writing of the 101 Be prepared to handle a FileExistsError if concurrent writing of the
(...skipping 17 matching lines...) Expand all
111 119
112 120
113 def _wrap(new, old): 121 def _wrap(new, old):
114 """Simple substitute for functools.update_wrapper.""" 122 """Simple substitute for functools.update_wrapper."""
115 for replace in ['__module__', '__name__', '__qualname__', '__doc__']: 123 for replace in ['__module__', '__name__', '__qualname__', '__doc__']:
116 if hasattr(old, replace): 124 if hasattr(old, replace):
117 setattr(new, replace, getattr(old, replace)) 125 setattr(new, replace, getattr(old, replace))
118 new.__dict__.update(old.__dict__) 126 new.__dict__.update(old.__dict__)
119 127
120 128
129 def _new_module(name):
130 return type(sys)(name)
131
132
121 _code_type = type(_wrap.__code__) 133 _code_type = type(_wrap.__code__)
122 134
135
136
137 class _ManageReload:
138
139 """Manages the possible clean-up of sys.modules for load_module()."""
140
141 def __init__(self, name):
142 self._name = name
143
144 def __enter__(self):
145 self._is_reload = self._name in sys.modules
146
147 def __exit__(self, *args):
148 if any(arg is not None for arg in args) and not self._is_reload:
149 try:
150 del sys.modules[self._name]
151 except KeyError:
152 pass
123 153
124 # Module-level locking ######################################################## 154 # Module-level locking ########################################################
125 155
126 # A dict mapping module names to weakrefs of _ModuleLock instances 156 # A dict mapping module names to weakrefs of _ModuleLock instances
127 _module_locks = {} 157 _module_locks = {}
128 # A dict mapping thread ids to _ModuleLock instances 158 # A dict mapping thread ids to _ModuleLock instances
129 _blocking_on = {} 159 _blocking_on = {}
130 160
131 161
132 class _DeadlockError(RuntimeError): 162 class _DeadlockError(RuntimeError):
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 # Python 3.3a0 3200 (__qualname__ added) 409 # Python 3.3a0 3200 (__qualname__ added)
380 # 3210 (added size modulo 2**32 to the pyc header) 410 # 3210 (added size modulo 2**32 to the pyc header)
381 # Python 3.3a1 3220 (changed PEP 380 implementation) 411 # Python 3.3a1 3220 (changed PEP 380 implementation)
382 # Python 3.3a4 3230 (revert changes to implicit __class__ closure) 412 # Python 3.3a4 3230 (revert changes to implicit __class__ closure)
383 # Python 3.4a1 3250 (evaluate positional default arguments before 413 # Python 3.4a1 3250 (evaluate positional default arguments before
384 # keyword-only defaults) 414 # keyword-only defaults)
385 # Python 3.4a1 3260 (add LOAD_CLASSDEREF; allow locals of class to override 415 # Python 3.4a1 3260 (add LOAD_CLASSDEREF; allow locals of class to override
386 # free vars) 416 # free vars)
387 # Python 3.4a1 3270 (various tweaks to the __class__ closure) 417 # Python 3.4a1 3270 (various tweaks to the __class__ closure)
388 # Python 3.4a1 3280 (remove implicit class argument) 418 # Python 3.4a1 3280 (remove implicit class argument)
419 # Python 3.4a4 3290 (changes to __qualname__ computation)
420 # Python 3.4a4 3300 (more changes to __qualname__ computation)
389 # 421 #
390 # MAGIC must change whenever the bytecode emitted by the compiler may no 422 # MAGIC must change whenever the bytecode emitted by the compiler may no
391 # longer be understood by older implementations of the eval loop (usually 423 # longer be understood by older implementations of the eval loop (usually
392 # due to the addition of new opcodes). 424 # due to the addition of new opcodes).
393 425
394 MAGIC_NUMBER = (3280).to_bytes(2, 'little') + b'\r\n' 426 MAGIC_NUMBER = (3300).to_bytes(2, 'little') + b'\r\n'
395 _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c 427 _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
396 428
397 _PYCACHE = '__pycache__' 429 _PYCACHE = '__pycache__'
398 430
399 SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed. 431 SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed.
400 432
401 DEBUG_BYTECODE_SUFFIXES = ['.pyc'] 433 DEBUG_BYTECODE_SUFFIXES = ['.pyc']
402 OPTIMIZED_BYTECODE_SUFFIXES = ['.pyo'] 434 OPTIMIZED_BYTECODE_SUFFIXES = ['.pyo']
403 435
404 def cache_from_source(path, debug_override=None): 436 def cache_from_source(path, debug_override=None):
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 try: 498 try:
467 source_path = source_from_cache(bytecode_path) 499 source_path = source_from_cache(bytecode_path)
468 except (NotImplementedError, ValueError): 500 except (NotImplementedError, ValueError):
469 source_path = bytecode_path[:-1] 501 source_path = bytecode_path[:-1]
470 return source_path if _path_isfile(source_path) else bytecode_path 502 return source_path if _path_isfile(source_path) else bytecode_path
471 503
472 504
473 def _calc_mode(path): 505 def _calc_mode(path):
474 """Calculate the mode permissions for a bytecode file.""" 506 """Calculate the mode permissions for a bytecode file."""
475 try: 507 try:
476 mode = _os.stat(path).st_mode 508 mode = _path_stat(path).st_mode
477 except OSError: 509 except OSError:
478 mode = 0o666 510 mode = 0o666
479 # We always ensure write access so we can update cached files 511 # We always ensure write access so we can update cached files
480 # later even when the source files are read-only on Windows (#6074) 512 # later even when the source files are read-only on Windows (#6074)
481 mode |= 0o200 513 mode |= 0o200
482 return mode 514 return mode
483 515
484 516
485 def _verbose_message(message, *args, verbosity=1): 517 def _verbose_message(message, *args, verbosity=1):
486 """Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" 518 """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
487 if sys.flags.verbose >= verbosity: 519 if sys.flags.verbose >= verbosity:
488 if not message.startswith(('#', 'import ')): 520 if not message.startswith(('#', 'import ')):
489 message = '# ' + message 521 message = '# ' + message
490 print(message.format(*args), file=sys.stderr) 522 print(message.format(*args), file=sys.stderr)
491
492
493 # XXX Remove.
494 class _ManageReload:
495
496 def __init__(self, name):
497 self._name = name
498
499 def __enter__(self):
500 self._is_reload = self._name in sys.modules
501
502 def __exit__(self, *args):
503 if any(arg is not None for arg in args) and not self._is_reload:
504 try:
505 del sys.modules[self._name]
506 except KeyError:
507 pass
508
509
510 # XXX Remove.
511 # Written as a class only because contextlib is not available.
512 class _ModuleManager(_ManageReload):
513
514 """Context manager which returns the module to be loaded.
515
516 Does the proper unloading from sys.modules upon failure.
517
518 """
519
520 def __init__(self, name, *, reset_name=True):
521 """Prepare the context manager.
522
523 The reset_name argument specifies whether to unconditionally reset
524 the __name__ attribute if the module is found to be a reload.
525 """
526 super().__init__(name)
527 self._reset_name = reset_name
528
529 def __enter__(self):
530 super().__enter__()
531 self._module = sys.modules.get(self._name)
532 if not self._is_reload:
533 # This must be done before open() is called as the 'io' module
534 # implicitly imports 'locale' and would otherwise trigger an
535 # infinite loop.
536 self._module = type(_io)(self._name)
537 # This must be done before putting the module in sys.modules
538 # (otherwise an optimization shortcut in import.c becomes wrong)
539 self._module.__initializing__ = True
540 sys.modules[self._name] = self._module
541 elif self._reset_name:
542 try:
543 self._module.__name__ = self._name
544 except AttributeError:
545 pass
546 return self._module
547
548 def __exit__(self, *args):
549 self._module.__initializing__ = False
550 del self._module
551 super().__exit__(*args)
552
553
554 # XXX Remove.
555 def module_to_load(name, *, reset_name=True):
556 """Return a context manager which provides the module object to load.
557
558 If reset_name is true, reset the module's __name__ to 'name'.
559 """
560 # Hiding _ModuleManager behind a function for better naming.
561 return _ModuleManager(name, reset_name=reset_name)
562
563
564 # XXX Remove.
565 def _init_package_attrs(loader, module):
566 """Set __package__ and __path__ based on what loader.is_package() says."""
567 name = module.__name__
568 try:
569 is_package = loader.is_package(name)
570 except ImportError:
571 pass
572 else:
573 if is_package:
574 module.__package__ = name
575 module.__path__ = []
576 else:
577 module.__package__ = name.rpartition('.')[0]
578
579
580 # XXX Remove.
581 def _init_file_attrs(loader, module):
582 """Set __file__ and __path__ based on loader.get_filename()."""
583 try:
584 module.__file__ = loader.get_filename(module.__name__)
585 except ImportError:
586 pass
587 else:
588 if module.__name__ == module.__package__:
589 module.__path__.append(_path_split(module.__file__)[0])
590
591
592 # XXX Can be moved to importlib.util (and deprecated).
593 def set_package(fxn):
594 """Set __package__ on the returned module."""
595 def set_package_wrapper(*args, **kwargs):
596 module = fxn(*args, **kwargs)
597 if getattr(module, '__package__', None) is None:
598 module.__package__ = module.__name__
599 if not hasattr(module, '__path__'):
600 module.__package__ = module.__package__.rpartition('.')[0]
601 return module
602 _wrap(set_package_wrapper, fxn)
603 return set_package_wrapper
604
605
606 # XXX Can be moved to importlib.util (and deprecated).
607 def set_loader(fxn):
608 """Set __loader__ on the returned module."""
609 def set_loader_wrapper(self, *args, **kwargs):
610 module = fxn(self, *args, **kwargs)
611 if getattr(module, '__loader__', None) is None:
612 module.__loader__ = self
613 return module
614 _wrap(set_loader_wrapper, fxn)
615 return set_loader_wrapper
616 523
617 524
618 def _check_name(method): 525 def _check_name(method):
619 """Decorator to verify that the module being requested matches the one the 526 """Decorator to verify that the module being requested matches the one the
620 loader can handle. 527 loader can handle.
621 528
622 The first argument (self) must define _name which the second argument is 529 The first argument (self) must define _name which the second argument is
623 compared against. If the comparison fails then ImportError is raised. 530 compared against. If the comparison fails then ImportError is raised.
624 531
625 """ 532 """
626 def _check_name_wrapper(self, name=None, *args, **kwargs): 533 def _check_name_wrapper(self, name=None, *args, **kwargs):
627 if name is None: 534 if name is None:
628 name = self.name 535 name = self.name
629 elif self.name != name: 536 elif self.name != name:
630 raise ImportError('loader cannot handle %s' % name, name=name) 537 raise ImportError('loader cannot handle %s' % name, name=name)
631 return method(self, name, *args, **kwargs) 538 return method(self, name, *args, **kwargs)
632 _wrap(_check_name_wrapper, method) 539 _wrap(_check_name_wrapper, method)
633 return _check_name_wrapper 540 return _check_name_wrapper
634 541
635 542
636 def _requires_builtin(fxn): 543 def _requires_builtin(fxn):
637 """Decorator to verify the named module is built-in.""" 544 """Decorator to verify the named module is built-in."""
638 def _requires_builtin_wrapper(self, fullname): 545 def _requires_builtin_wrapper(self, fullname):
639 if fullname not in sys.builtin_module_names: 546 if fullname not in sys.builtin_module_names:
640 raise ImportError('{} is not a built-in module'.format(fullname), 547 raise ImportError('{!r} is not a built-in module'.format(fullname),
641 name=fullname) 548 name=fullname)
642 return fxn(self, fullname) 549 return fxn(self, fullname)
643 _wrap(_requires_builtin_wrapper, fxn) 550 _wrap(_requires_builtin_wrapper, fxn)
644 return _requires_builtin_wrapper 551 return _requires_builtin_wrapper
645 552
646 553
647 def _requires_frozen(fxn): 554 def _requires_frozen(fxn):
648 """Decorator to verify the named module is frozen.""" 555 """Decorator to verify the named module is frozen."""
649 def _requires_frozen_wrapper(self, fullname): 556 def _requires_frozen_wrapper(self, fullname):
650 if not _imp.is_frozen(fullname): 557 if not _imp.is_frozen(fullname):
651 raise ImportError('{} is not a frozen module'.format(fullname), 558 raise ImportError('{!r} is not a frozen module'.format(fullname),
652 name=fullname) 559 name=fullname)
653 return fxn(self, fullname) 560 return fxn(self, fullname)
654 _wrap(_requires_frozen_wrapper, fxn) 561 _wrap(_requires_frozen_wrapper, fxn)
655 return _requires_frozen_wrapper 562 return _requires_frozen_wrapper
656 563
657 564
658 def _find_module_shim(self, fullname): 565 def _find_module_shim(self, fullname):
659 """Try to find a loader for the specified module by delegating to 566 """Try to find a loader for the specified module by delegating to
660 self.find_loader().""" 567 self.find_loader()."""
661 # Call find_loader(). If it returns a string (indicating this 568 # Call find_loader(). If it returns a string (indicating this
662 # is a namespace package portion), generate a warning and 569 # is a namespace package portion), generate a warning and
663 # return None. 570 # return None.
664 loader, portions = self.find_loader(fullname) 571 loader, portions = self.find_loader(fullname)
665 if loader is None and len(portions): 572 if loader is None and len(portions):
666 msg = 'Not importing directory {}: missing __init__' 573 msg = 'Not importing directory {}: missing __init__'
667 _warnings.warn(msg.format(portions[0]), ImportWarning) 574 _warnings.warn(msg.format(portions[0]), ImportWarning)
668 return loader 575 return loader
576
577
578 def _load_module_shim(self, fullname):
579 """Load the specified module into sys.modules and return it."""
580 # XXX Deprecation Warning here...
581 spec = spec_from_loader(fullname, self)
582 methods = _SpecMethods(spec)
583 if fullname in sys.modules:
584 module = sys.modules[fullname]
585 methods.exec(module)
586 return sys.modules[fullname]
587 else:
588 return methods.load()
669 589
670 590
671 def _validate_bytecode_header(data, source_stats=None, name=None, path=None): 591 def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
672 """Validate the header of the passed-in bytecode against source_stats (if 592 """Validate the header of the passed-in bytecode against source_stats (if
673 given) and returning the bytecode that can be compiled by compile(). 593 given) and returning the bytecode that can be compiled by compile().
674 594
675 All other arguments are used to enhance error reporting. 595 All other arguments are used to enhance error reporting.
676 596
677 ImportError is raised when the magic number is incorrect or the bytecode is 597 ImportError is raised when the magic number is incorrect or the bytecode is
678 found to be stale. EOFError is raised when the data is found to be 598 found to be stale. EOFError is raised when the data is found to be
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 """ 672 """
753 import tokenize # To avoid bootstrap issues. 673 import tokenize # To avoid bootstrap issues.
754 source_bytes_readline = _io.BytesIO(source_bytes).readline 674 source_bytes_readline = _io.BytesIO(source_bytes).readline
755 encoding = tokenize.detect_encoding(source_bytes_readline) 675 encoding = tokenize.detect_encoding(source_bytes_readline)
756 newline_decoder = _io.IncrementalNewlineDecoder(None, True) 676 newline_decoder = _io.IncrementalNewlineDecoder(None, True)
757 return newline_decoder.decode(source_bytes.decode(encoding[0])) 677 return newline_decoder.decode(source_bytes.decode(encoding[0]))
758 678
759 679
760 # Module specifications ####################################################### 680 # Module specifications #######################################################
761 681
682 def _module_repr(module):
683 # The implementation of ModuleType__repr__().
684 loader = getattr(module, '__loader__', None)
685 if hasattr(loader, 'module_repr'):
686 # XXX Deprecation Warning here...
687 try:
688 return loader.module_repr(module)
689 except Exception:
690 pass
691 try:
692 spec = module.__spec__
693 except AttributeError:
694 pass
695 else:
696 if spec is not None:
697 return _SpecMethods(spec).module_repr()
698
699 # We could use module.__class__.__name__ instead of 'module' in the
700 # various repr permutations.
701 try:
702 name = module.__name__
703 except AttributeError:
704 name = '?'
705 try:
706 filename = module.__file__
707 except AttributeError:
708 if loader is None:
709 return '<module {!r}>'.format(name)
710 else:
711 return '<module {!r} ({!r})>'.format(name, loader)
712 else:
713 return '<module {!r} from {!r}>'.format(name, filename)
714
715
762 class _installed_safely: 716 class _installed_safely:
763 717
764 def __init__(self, module): 718 def __init__(self, module):
765 self._module = module 719 self._module = module
766 self._spec = module.__spec__ 720 self._spec = module.__spec__
767 721
768 def __enter__(self): 722 def __enter__(self):
769 # This must be done before putting the module in sys.modules 723 # This must be done before putting the module in sys.modules
770 # (otherwise an optimization shortcut in import.c becomes 724 # (otherwise an optimization shortcut in import.c becomes
771 # wrong) 725 # wrong)
772 self._spec._initializing = True 726 self._spec._initializing = True
773 sys.modules[self._spec.name] = self._module 727 sys.modules[self._spec.name] = self._module
774 728
775 def __exit__(self, *args): 729 def __exit__(self, *args):
776 try: 730 try:
777 spec = self._spec 731 spec = self._spec
778 if any(arg is not None for arg in args): 732 if any(arg is not None for arg in args):
779 try: 733 try:
780 del sys.modules[spec.name] 734 del sys.modules[spec.name]
781 except KeyError: 735 except KeyError:
782 pass 736 pass
783 else: 737 else:
784 _verbose_message('import {!r} # {!r}', spec._name, spec.loader) 738 _verbose_message('import {!r} # {!r}', spec.name, spec.loader)
785 finally: 739 finally:
786 self._spec._initializing = False 740 self._spec._initializing = False
787 741
788 742
789 class ModuleSpec: 743 class ModuleSpec:
790 """The specification for a module, used for loading. 744 """The specification for a module, used for loading.
791 745
792 A module's spec is the source for information about the module. For 746 A module's spec is the source for information about the module. For
793 data associated with the module, including source, use the spec's 747 data associated with the module, including source, use the spec's
794 loader. 748 loader.
795 749
796 `name` is the absolute name of the module. `loader` is the loader 750 `name` is the absolute name of the module. `loader` is the loader
797 to use when loading the module. `package` is the name of the 751 to use when loading the module. `parent` is the name of the
798 package the module is in. The package is derived from the name. 752 package the module is in. The parent is derived from the name.
799 753
800 `is_package` determines if the module is considered a package or 754 `is_package` determines if the module is considered a package or
801 not. On modules this is reflected by the `__path__` attribute. 755 not. On modules this is reflected by the `__path__` attribute.
802 756
803 `origin` is the specific location used by the loader from which to 757 `origin` is the specific location used by the loader from which to
804 load the module, if that information is available. When filename is 758 load the module, if that information is available. When filename is
805 set, origin will match. 759 set, origin will match.
806 760
807 `filename` is the file from which the module is loaded, if 761 `has_location` indicates that a spec's "origin" reflects a location.
808 available. It corresponds to the `__file__` attribute of modules. 762 When this is True, `__file__` attribute of the module is set.
809 763
810 `cached` is the location of the cached bytecode file, if any. It 764 `cached` is the location of the cached bytecode file, if any. It
811 corresponds to the `__cached__` attribute. 765 corresponds to the `__cached__` attribute.
812 766
813 `path` is the sequence of path entries to search when importing 767 `submodule_search_locations` is the sequence of path entries to
814 submodules. If set, is_package should be True--and False otherwise. 768 search when importing submodules. If set, is_package should be
815 769 True--and False otherwise.
816 If `infer_values` is true, values are calculated where possible
817 using the available data, including optional loader APIs (see PEP
818 302).
819 770
820 Packages are simply modules that (may) have submodules. If a spec 771 Packages are simply modules that (may) have submodules. If a spec
821 has a non-None value in `submodule_search_locations`, the import 772 has a non-None value in `submodule_search_locations`, the import
822 system will consider modules loaded from the spec as packages. 773 system will consider modules loaded from the spec as packages.
823 774
824 Only finders (see importlib.abc.MetaPathFinder and 775 Only finders (see importlib.abc.MetaPathFinder and
825 importlib.abc.PathEntryFinder) should modify ModuleSpec instances. 776 importlib.abc.PathEntryFinder) should modify ModuleSpec instances.
826 777
827 """ 778 """
828
829 @classmethod
830 def _module_repr(cls, module):
831 # The implementation of ModuleType__repr__().
832 loader = getattr(module, '__loader__', None)
833 if hasattr(loader, 'module_repr'):
834 # XXX Deprecation Warning here...
835 try:
836 return loader.module_repr(module)
837 except Exception:
838 pass
839 try:
840 spec = module.__spec__
841 except AttributeError:
842 # XXX Use module.__class__.__name__ instead of 'module'?
843 try:
844 name = module.__name__
845 except AttributeError:
846 name = '?'
847 try:
848 filename = module.__file__
849 except AttributeError:
850 if loader is None:
851 return '<module {!r}>'.format(name)
852 else:
853 return '<module {!r} ({!r})>'.format(name, loader)
854 else:
855 return '<module {!r} from {!r}>'.format(name, filename)
856 else:
857 return spec.module_repr()
858 779
859 def __init__(self, name, loader, *, origin=None, loader_state=None, 780 def __init__(self, name, loader, *, origin=None, loader_state=None,
860 is_package=None): 781 is_package=None):
861 self.name = name 782 self.name = name
862 self.loader = loader 783 self.loader = loader
863 self.origin = origin 784 self.origin = origin
864 self.loader_state = loader_state 785 self.loader_state = loader_state
865 self.submodule_search_locations = [] if is_package else None 786 self.submodule_search_locations = [] if is_package else None
866 787
867 # file-location attributes 788 # file-location attributes
868 self._set_fileattr = False 789 self._set_fileattr = False
869 self._cached = None 790 self._cached = None
870 791
871 def __repr__(self): 792 def __repr__(self):
872 args = ['name={!r}'.format(self.name), 793 args = ['name={!r}'.format(self.name),
873 'loader={!r}'.format(self.loader)] 794 'loader={!r}'.format(self.loader)]
874 if self.origin is not None: 795 if self.origin is not None:
875 args.append('origin={!r}'.format(self.origin)) 796 args.append('origin={!r}'.format(self.origin))
876 if self.submodule_search_locations is not None: 797 if self.submodule_search_locations is not None:
877 args.append('submodule_search_locations={}' 798 args.append('submodule_search_locations={}'
878 .format(self.submodule_search_locations)) 799 .format(self.submodule_search_locations))
879 return '{}({})'.format(self.__class__.__name__, ', '.join(args)) 800 return '{}({})'.format(self.__class__.__name__, ', '.join(args))
801
802 def __eq__(self, other):
803 smsl = self.submodule_search_locations
804 try:
805 return (self.name == other.name and
806 self.loader == other.loader and
807 self.origin == other.origin and
808 smsl == other.submodule_search_locations and
809 self.cached == other.cached and
810 self.has_location == other.has_location)
811 except AttributeError:
812 return False
880 813
881 @property 814 @property
882 def cached(self): 815 def cached(self):
883 if self._cached is None: 816 if self._cached is None:
884 if self.origin is not None and self._set_fileattr: 817 if self.origin is not None and self._set_fileattr:
885 filename = self.origin 818 filename = self.origin
886 if filename.endswith(tuple(SOURCE_SUFFIXES)): 819 if filename.endswith(tuple(SOURCE_SUFFIXES)):
887 try: 820 try:
888 self._cached = cache_from_source(filename) 821 self._cached = cache_from_source(filename)
889 except NotImplementedError: 822 except NotImplementedError:
890 pass 823 pass
891 elif filename.endswith(tuple(BYTECODE_SUFFIXES)): 824 elif filename.endswith(tuple(BYTECODE_SUFFIXES)):
892 self._cached = filename 825 self._cached = filename
893 return self._cached 826 return self._cached
894 827
895 @cached.setter 828 @cached.setter
896 def cached(self, cached): 829 def cached(self, cached):
897 self._cached = cached 830 self._cached = cached
898 831
899 @property 832 @property
900 def package(self): 833 def parent(self):
901 """The name of the module's parent.""" 834 """The name of the module's parent."""
902 if self.submodule_search_locations is None: 835 if self.submodule_search_locations is None:
903 return self.name.rpartition('.')[0] 836 return self.name.rpartition('.')[0]
904 else: 837 else:
905 return self.name 838 return self.name
906 839
907 @property 840 @property
908 def has_location(self): 841 def has_location(self):
909 return self._set_fileattr 842 return self._set_fileattr
910 843
911 def module_repr(self): 844 @has_location.setter
912 """Return the repr to use for the module.""" 845 def has_location(self, value):
913 # We mostly replicate _module_repr() using the spec attributes. 846 self._set_fileattr = bool(value)
914 name = '?' if self.name is None else self.name
915 if self.origin is None:
916 if self.loader is None:
917 return '<module {!r}>'.format(name)
918 else:
919 return '<module {!r} ({!r})>'.format(name, self.loader)
920 else:
921 if self._set_fileattr:
922 return '<module {!r} from {!r}>'.format(name, self.origin)
923 else:
924 return '<module {!r} ({})>'.format(self.name, self.origin)
925
926 def init_module_attrs(self, module, *, _override=False, _force_name=True):
927 """Set the module's attributes.
928
929 All missing import-related module attributes will be set. Here
930 is how the spec attributes map onto the module:
931
932 spec.name -> module.__name__
933 spec.loader -> module.__loader__
934 spec.package -> module.__package__
935 spec -> module.__spec__
936
937 Optional:
938 spec.origin -> module.__file__ (if spec.set_fileattr is true)
939 spec.cached -> module.__cached__ (if __file__ also set)
940 spec.submodule_search_locations -> module.__path__ (if set)
941
942 """
943 # XXX Make _override public?
944 spec = self
945
946 # The passed in module may be not support attribute assignment,
947 # in which case we simply don't set the attributes.
948
949 # __name__
950 if (_override or _force_name or
951 getattr(module, '__name__', None) is None):
952 try:
953 module.__name__ = self.name
954 except AttributeError:
955 pass
956
957 # __loader__
958 if _override or getattr(module, '__loader__', None) is None:
959 try:
960 module.__loader__ = self.loader
961 except AttributeError:
962 pass
963
964 # __package__
965 if _override or getattr(module, '__package__', None) is None:
966 try:
967 module.__package__ = self.package
968 except AttributeError:
969 pass
970
971 # __spec__
972 try:
973 module.__spec__ = spec
974 except AttributeError:
975 pass
976
977 # __path__
978 if _override or getattr(module, '__path__', None) is None:
979 if self.submodule_search_locations is not None:
980 try:
981 module.__path__ = self.submodule_search_locations
982 except AttributeError:
983 pass
984
985 if self._set_fileattr:
986 # __file__
987 if _override or getattr(module, '__file__', None) is None:
988 try:
989 module.__file__ = self.origin
990 except AttributeError:
991 pass
992
993 # __cached__
994 if _override or getattr(module, '__cached__', None) is None:
995 if self.cached is not None:
996 try:
997 module.__cached__ = self.cached
998 except AttributeError:
999 pass
1000
1001 # Under normal circumstances _create(), _exec(), _load_unlocked(),
1002 # _load(), and # _reload() should not be called directly. So they
1003 # have underscore # names (a.k.a. private API).
1004
1005 def _create(self):
1006 """Return a new module to be loaded.
1007
1008 The import-related module attributes are also set with the
1009 appropriate values from the spec.
1010
1011 """
1012 # Typically loaders will not implement create_module().
1013 if hasattr(self.loader, 'create_module'):
1014 # If create_module() returns `None` it means the default
1015 # module creation should be used.
1016 module = self.loader.create_module(self)
1017 else:
1018 module = None
1019 if module is None:
1020 # This must be done before open() is ever called as the 'io'
1021 # module implicitly imports 'locale' and would otherwise
1022 # trigger an infinite loop.
1023 module = type(_io)(self.name)
1024 self.init_module_attrs(module)
1025 return module
1026
1027 def _exec(self, module):
1028 """Do everything necessary to execute the module.
1029
1030 The namespace of `module` is used as the target of execution.
1031 This method uses the loader's `exec_module()` method.
1032
1033 """
1034 self.loader.exec_module(module)
1035
1036 def _load_backward_compatible(self):
1037 # XXX DeprecationWarning goes here...
1038 self.loader.load_module(self.name)
1039 module = sys.modules[self.name]
1040 if getattr(module, '__loader__', None) is None:
1041 try:
1042 module.__loader__ = self.loader
1043 except AttributeError:
1044 pass
1045 if getattr(module, '__package__', None) is None:
1046 try:
1047 # Since module.__path__ may not line up with
1048 # self.submodule_search_paths, we can't necessarily rely
1049 # on self.package here.
1050 module.__package__ = module.__name__
1051 if not hasattr(module, '__path__'):
1052 module.__package__ = self.name.rpartition('.')[0]
1053 except AttributeError:
1054 pass
1055 if getattr(module, '__spec__', None) is None:
1056 try:
1057 module.__spec__ = self
1058 except AttributeError:
1059 pass
1060 return module
1061
1062 def _load_unlocked(self):
1063 # A helper for direct use by the import system.
1064 if not hasattr(self.loader, 'exec_module'):
1065 return self._load_backward_compatible()
1066
1067 module = self._create()
1068 with _installed_safely(module):
1069 loaded = self._exec(module)
1070
1071 # We don't ensure that the import-related module attributes get
1072 # set in the sys.modules replacement case. Such modules are on
1073 # their own.
1074 return sys.modules[self.name]
1075
1076 def _load_existing(self, module):
1077 """Exec the spec'ed module into an existing module's namespace."""
1078 # For use by runpy.
1079 with _installed_safely(module):
1080 loaded = self._exec(module)
1081
1082 def _load(self):
1083 """Return a new module object, loaded by the spec's loader.
1084
1085 The module is not added to its parent.
1086
1087 """
1088 # XXX Chance for deadlock with circular import? Reload only?
1089 # XXX Check to see if already in sys.modules?
1090 _imp.acquire_lock()
1091 with _ModuleLockManager(self.name):
1092 return self._load_unlocked()
1093
1094 def _reload(self, module):
1095 """Reload the module, leaving it in sys.modules.
1096
1097 This is a simple reload of the module. `importlib.reload()`
1098 provides a richer reload experience.
1099
1100 """
1101 _imp.acquire_lock()
1102 with _ModuleLockManager(self.name):
1103 self.init_module_attrs(module, _force_name=True)
1104 if not hasattr(self.loader, 'exec_module'):
1105 # XXX DeprecationWarning goes here...
1106 self.loader.load_module(self.name)
1107 return sys.modules[self.name]
1108 self._exec(module)
1109 return sys.modules[self.name]
1110 847
1111 848
1112 def spec_from_loader(name, loader, *, origin=None, is_package=None): 849 def spec_from_loader(name, loader, *, origin=None, is_package=None):
1113 """Return a module spec based on various loader methods.""" 850 """Return a module spec based on various loader methods."""
1114 851 if hasattr(loader, 'get_filename'):
1115 if hasattr(loader, 'get_data'): 852 if is_package is None:
853 return spec_from_file_location(name, loader=loader)
1116 search = [] if is_package else None 854 search = [] if is_package else None
1117 return spec_from_file_location(name, loader=loader, 855 return spec_from_file_location(name, loader=loader,
1118 submodule_search_locations=search) 856 submodule_search_locations=search)
1119 857
1120 if is_package is None: 858 if is_package is None:
1121 if hasattr(loader, 'is_package'): 859 if hasattr(loader, 'is_package'):
1122 try: 860 try:
1123 is_package = loader.is_package(name) 861 is_package = loader.is_package(name)
1124 except ImportError: 862 except ImportError:
1125 is_package = None # aka, undefined 863 is_package = None # aka, undefined
1126 else: 864 else:
1127 # the default 865 # the default
1128 is_package = False 866 is_package = False
1129 867
1130 return ModuleSpec(name, loader, origin=origin, is_package=is_package) 868 return ModuleSpec(name, loader, origin=origin, is_package=is_package)
1131 869
1132 870
871 _POPULATE = object()
872
873
1133 def spec_from_file_location(name, location=None, *, loader=None, 874 def spec_from_file_location(name, location=None, *, loader=None,
1134 submodule_search_locations=None): 875 submodule_search_locations=_POPULATE):
1135 """Return a module spec based on a file location. 876 """Return a module spec based on a file location.
1136 877
1137 To indicate that the module is a package, set 878 To indicate that the module is a package, set
1138 submodule_search_locations to a list of directory paths. An 879 submodule_search_locations to a list of directory paths. An
1139 empty list is sufficient, though its not otherwise useful to the 880 empty list is sufficient, though its not otherwise useful to the
1140 import system. 881 import system.
1141 882
1142 The loader must take a spec as its only __init__() arg. 883 The loader must take a spec as its only __init__() arg.
1143 884
1144 """ 885 """
1145 if location is None: 886 if location is None:
887 # The caller may simply want a partially populated location-
888 # oriented spec. So we set the location to a bogus value and
889 # fill in as much as we can.
890 location = '<unknown>'
1146 if hasattr(loader, 'get_filename'): 891 if hasattr(loader, 'get_filename'):
892 # ExecutionLoader
1147 try: 893 try:
1148 location = loader.get_filename(name) 894 location = loader.get_filename(name)
1149 except ImportError: 895 except ImportError:
1150 # XXX Return None? 896 pass
1151 location = '<unknown>' 897
1152 else: 898 # If the location is on the filesystem, but doesn't actually exist,
1153 # XXX Return None? 899 # we could return None here, indicating that the location is not
1154 location = '<unknown>' 900 # valid. However, we don't have a good way of testing since an
1155 # XXX This breaks for zipimport (and any indirect location). 901 # indirect location (e.g. a zip file or URL) will look like a
1156 #try: 902 # non-existent file relative to the filesystem.
1157 # _os.stat(location)
1158 #except OSError:
1159 # return None
1160 903
1161 spec = ModuleSpec(name, loader, origin=location) 904 spec = ModuleSpec(name, loader, origin=location)
1162 spec._set_fileattr = True 905 spec._set_fileattr = True
1163 spec.submodule_search_locations = submodule_search_locations 906
1164 907 # Pick a loader if one wasn't provided.
1165 if loader is None: 908 if loader is None:
1166 for loader_class, suffixes in _get_supported_file_loaders(): 909 for loader_class, suffixes in _get_supported_file_loaders():
1167 if location.endswith(tuple(suffixes)): 910 if location.endswith(tuple(suffixes)):
1168 loader = loader_class(name, location) 911 loader = loader_class(name, location)
1169 spec.loader = loader 912 spec.loader = loader
1170 break 913 break
1171 else: 914 else:
1172 return None 915 return None
1173 916
1174 # Make sure we got submodule_search_paths right. 917 # Set submodule_search_paths appropriately.
1175 if spec.submodule_search_locations is None: 918 if submodule_search_locations is _POPULATE:
1176 # Check the loader. 919 # Check the loader.
1177 if hasattr(loader, 'is_package'): 920 if hasattr(loader, 'is_package'):
1178 try: 921 try:
1179 is_package = loader.is_package(name) 922 is_package = loader.is_package(name)
1180 except ImportError: 923 except ImportError:
1181 pass 924 pass
1182 else: 925 else:
1183 if is_package: 926 if is_package:
1184 spec.submodule_search_locations = [] 927 spec.submodule_search_locations = []
928 else:
929 spec.submodule_search_locations = submodule_search_locations
1185 if spec.submodule_search_locations == []: 930 if spec.submodule_search_locations == []:
1186 if location: 931 if location:
1187 dirname = _path_split(location)[0] 932 dirname = _path_split(location)[0]
1188 spec.submodule_search_locations.append(dirname) 933 spec.submodule_search_locations.append(dirname)
1189 934
1190 return spec 935 return spec
1191 936
1192 937
1193 def _spec_from_module(module, loader=None, origin=None): 938 def _spec_from_module(module, loader=None, origin=None):
939 # This function is meant for use in _setup().
1194 try: 940 try:
1195 return module.__spec__ 941 spec = module.__spec__
1196 except AttributeError: 942 except AttributeError:
1197 pass 943 pass
944 else:
945 if spec is not None:
946 return spec
1198 947
1199 name = module.__name__ 948 name = module.__name__
1200 if loader is None: 949 if loader is None:
1201 try: 950 try:
1202 loader = module.__loader__ 951 loader = module.__loader__
1203 except AttributeError: 952 except AttributeError:
1204 loader = None 953 # loader will stay None.
954 pass
1205 try: 955 try:
1206 location = module.__file__ 956 location = module.__file__
1207 except AttributeError: 957 except AttributeError:
1208 location = None 958 location = None
1209 if origin is None: 959 if origin is None:
1210 if location is None: 960 if location is None:
1211 try: 961 try:
1212 origin = loader._ORIGIN 962 origin = loader._ORIGIN
1213 except AttributeError: 963 except AttributeError:
1214 origin = None 964 origin = None
1215 else: 965 else:
1216 origin = location 966 origin = location
1217 try: 967 try:
1218 cached = module.__cached__ 968 cached = module.__cached__
1219 except AttributeError: 969 except AttributeError:
1220 cached = None 970 cached = None
1221 try: 971 try:
1222 submodule_search_locations = list(module.__path__) 972 submodule_search_locations = list(module.__path__)
1223 except AttributeError: 973 except AttributeError:
1224 submodule_search_locations = None 974 submodule_search_locations = None
1225 975
1226 spec = ModuleSpec(name, loader, origin=origin) 976 spec = ModuleSpec(name, loader, origin=origin)
1227 spec._set_fileattr = False if location is None else True 977 spec._set_fileattr = False if location is None else True
1228 spec.cached = cached 978 spec.cached = cached
1229 spec.submodule_search_locations = submodule_search_locations 979 spec.submodule_search_locations = submodule_search_locations
1230 return spec 980 return spec
1231 981
1232 982
983 class _SpecMethods:
984
985 """Convenience wrapper around spec objects to provide spec-specific
986 methods."""
987
988 def __init__(self, spec):
989 self.spec = spec
990
991 @classmethod
992 def from_module(cls, module):
993 """Create a spec from a module's attributes."""
994 try:
995 spec = module.__spec__
996 except AttributeError:
997 try:
998 loader = spec.__loader__
999 except AttributeError:
1000 spec = _find_spec(module.__name__)
1001 if spec is None:
1002 spec = spec_from_loader(module.__name__, loader)
1003 else:
1004 spec = spec_from_loader(module.__name__, loader)
1005 return cls(spec)
1006
1007 def module_repr(self):
1008 """Return the repr to use for the module."""
1009 # We mostly replicate _module_repr() using the spec attributes.
1010 spec = self.spec
1011 name = '?' if spec.name is None else spec.name
1012 if spec.origin is None:
1013 if spec.loader is None:
1014 return '<module {!r}>'.format(name)
1015 else:
1016 return '<module {!r} ({!r})>'.format(name, spec.loader)
1017 else:
1018 if spec.has_location:
1019 return '<module {!r} from {!r}>'.format(name, spec.origin)
1020 else:
1021 return '<module {!r} ({})>'.format(spec.name, spec.origin)
1022
1023 def init_module_attrs(self, module, *, _override=False, _force_name=True):
1024 """Set the module's attributes.
1025
1026 All missing import-related module attributes will be set. Here
1027 is how the spec attributes map onto the module:
1028
1029 spec.name -> module.__name__
1030 spec.loader -> module.__loader__
1031 spec.parent -> module.__package__
1032 spec -> module.__spec__
1033
1034 Optional:
1035 spec.origin -> module.__file__ (if spec.set_fileattr is true)
1036 spec.cached -> module.__cached__ (if __file__ also set)
1037 spec.submodule_search_locations -> module.__path__ (if set)
1038
1039 """
1040 spec = self.spec
1041
1042 # The passed in module may be not support attribute assignment,
1043 # in which case we simply don't set the attributes.
1044
1045 # __name__
1046 if (_override or _force_name or
1047 getattr(module, '__name__', None) is None):
1048 try:
1049 module.__name__ = spec.name
1050 except AttributeError:
1051 pass
1052
1053 # __loader__
1054 if _override or getattr(module, '__loader__', None) is None:
1055 loader = spec.loader
1056 if loader is None:
1057 # A backward compatibility hack.
1058 if spec.submodule_search_locations is not None:
1059 loader = _NamespaceLoader.__new__(_NamespaceLoader)
1060 loader._path = spec.submodule_search_locations
1061 try:
1062 module.__loader__ = loader
1063 except AttributeError:
1064 pass
1065
1066 # __package__
1067 if _override or getattr(module, '__package__', None) is None:
1068 try:
1069 module.__package__ = spec.parent
1070 except AttributeError:
1071 pass
1072
1073 # __spec__
1074 try:
1075 module.__spec__ = spec
1076 except AttributeError:
1077 pass
1078
1079 # __path__
1080 if _override or getattr(module, '__path__', None) is None:
1081 if spec.submodule_search_locations is not None:
1082 try:
1083 module.__path__ = spec.submodule_search_locations
1084 except AttributeError:
1085 pass
1086
1087 if spec.has_location:
1088 # __file__
1089 if _override or getattr(module, '__file__', None) is None:
1090 try:
1091 module.__file__ = spec.origin
1092 except AttributeError:
1093 pass
1094
1095 # __cached__
1096 if _override or getattr(module, '__cached__', None) is None:
1097 if spec.cached is not None:
1098 try:
1099 module.__cached__ = spec.cached
1100 except AttributeError:
1101 pass
1102
1103 def create(self):
1104 """Return a new module to be loaded.
1105
1106 The import-related module attributes are also set with the
1107 appropriate values from the spec.
1108
1109 """
1110 spec = self.spec
1111 # Typically loaders will not implement create_module().
1112 if hasattr(spec.loader, 'create_module'):
1113 # If create_module() returns `None` it means the default
1114 # module creation should be used.
1115 module = spec.loader.create_module(spec)
1116 else:
1117 module = None
1118 if module is None:
1119 # This must be done before open() is ever called as the 'io'
1120 # module implicitly imports 'locale' and would otherwise
1121 # trigger an infinite loop.
1122 module = _new_module(spec.name)
1123 self.init_module_attrs(module)
1124 return module
1125
1126 def _exec(self, module):
1127 """Do everything necessary to execute the module.
1128
1129 The namespace of `module` is used as the target of execution.
1130 This method uses the loader's `exec_module()` method.
1131
1132 """
1133 self.spec.loader.exec_module(module)
1134
1135 # Used by importlib.reload() and _load_module_shim().
1136 def exec(self, module):
1137 """Execute the spec in an existing module's namespace."""
1138 name = self.spec.name
1139 _imp.acquire_lock()
1140 with _ModuleLockManager(name):
1141 if sys.modules.get(name) is not module:
1142 msg = 'module {!r} not in sys.modules'.format(name)
1143 raise ImportError(msg, name=name)
1144 if self.spec.loader is None:
1145 if self.spec.submodule_search_locations is None:
1146 raise ImportError('missing loader', name=self.spec.name)
1147 # namespace package
1148 self.init_module_attrs(module, _override=True)
1149 return module
1150 self.init_module_attrs(module, _override=True)
1151 if not hasattr(self.spec.loader, 'exec_module'):
1152 # XXX DeprecationWarning goes here...
1153 self.spec.loader.load_module(name)
1154 else:
1155 self._exec(module)
1156 return sys.modules[name]
1157
1158 def _load_backward_compatible(self):
1159 # XXX DeprecationWarning goes here...
1160 spec = self.spec
1161 # The module must be in sys.modules!
1162 spec.loader.load_module(spec.name)
1163 module = sys.modules[spec.name]
1164 if getattr(module, '__loader__', None) is None:
1165 try:
1166 module.__loader__ = spec.loader
1167 except AttributeError:
1168 pass
1169 if getattr(module, '__package__', None) is None:
1170 try:
1171 # Since module.__path__ may not line up with
1172 # spec.submodule_search_paths, we can't necessarily rely
1173 # on spec.parent here.
1174 module.__package__ = module.__name__
1175 if not hasattr(module, '__path__'):
1176 module.__package__ = spec.name.rpartition('.')[0]
1177 except AttributeError:
1178 pass
1179 if getattr(module, '__spec__', None) is None:
1180 try:
1181 module.__spec__ = spec
1182 except AttributeError:
1183 pass
1184 return module
1185
1186 # XXX If we don't end up using this for pythonrun.c/runpy, we should
1187 # get rid of it.
1188 def _load_existing(self, module):
1189 """Exec the spec'ed module into an existing module's namespace."""
1190 # For use by runpy.
1191 with _installed_safely(module):
1192 loaded = self.exec(module)
1193 return loaded
1194
1195 def _load_unlocked(self):
1196 # A helper for direct use by the import system.
1197 if self.spec.loader is not None:
1198 # not a namespace package
1199 if not hasattr(self.spec.loader, 'exec_module'):
1200 return self._load_backward_compatible()
1201
1202 module = self.create()
1203 with _installed_safely(module):
1204 if self.spec.loader is None:
1205 if self.spec.submodule_search_locations is None:
1206 raise ImportError('missing loader', name=self.spec.name)
1207 # A namespace package so do nothing.
1208 else:
1209 self._exec(module)
1210
1211 # We don't ensure that the import-related module attributes get
1212 # set in the sys.modules replacement case. Such modules are on
1213 # their own.
1214 return sys.modules[self.spec.name]
1215
1216 # A method used during testing of _load_unlocked() and by
1217 # _load_module_shim().
1218 def load(self):
1219 """Return a new module object, loaded by the spec's loader.
1220
1221 The module is not added to its parent.
1222
1223 If a module is already in sys.modules, that existing module gets
1224 clobbered.
1225
1226 """
1227 _imp.acquire_lock()
1228 with _ModuleLockManager(self.spec.name):
1229 return self._load_unlocked()
1230
1231
1233 # Loaders ##################################################################### 1232 # Loaders #####################################################################
1234 1233
1235 class BuiltinImporter: 1234 class BuiltinImporter:
1236 1235
1237 """Meta path import for built-in modules. 1236 """Meta path import for built-in modules.
1238 1237
1239 All methods are either class or static methods to avoid the need to 1238 All methods are either class or static methods to avoid the need to
1240 instantiate the class. 1239 instantiate the class.
1241 1240
1242 """ 1241 """
1243 _ORIGIN = 'built-in' 1242
1244 1243 @staticmethod
1245 @classmethod 1244 def module_repr(module):
1246 def _get_spec(cls, fullname): 1245 # XXX deprecate
1247 return spec_from_loader(fullname, cls, origin=cls._ORIGIN)
1248
1249 # XXX BC issues with removing?
1250 @classmethod
1251 def module_repr(cls, module):
1252 return '<module {!r} (built-in)>'.format(module.__name__) 1246 return '<module {!r} (built-in)>'.format(module.__name__)
1253 1247
1254 # XXX Add shims for find_module and load_module. 1248 @classmethod
1255 1249 def find_spec(cls, fullname, path=None, target=None):
1256 # XXX Change to find_spec(). 1250 if path is not None:
1251 return None
1252 if _imp.is_builtin(fullname):
1253 return spec_from_loader(fullname, cls, origin='built-in')
1254 else:
1255 return None
1256
1257 @classmethod 1257 @classmethod
1258 def find_module(cls, fullname, path=None): 1258 def find_module(cls, fullname, path=None):
1259 """Find the built-in module. 1259 """Find the built-in module.
1260 1260
1261 If 'path' is ever specified then the search is considered a failure. 1261 If 'path' is ever specified then the search is considered a failure.
1262 1262
1263 """ 1263 """
1264 if path is not None: 1264 spec = cls.find_spec(fullname, path)
1265 return None 1265 return spec.loader if spec is not None else None
1266 return cls if _imp.is_builtin(fullname) else None 1266
1267 1267 @classmethod
1268 # XXX Change to exec_module().
1269 @classmethod
1270 @set_package
1271 @set_loader
1272 @_requires_builtin 1268 @_requires_builtin
1273 def load_module(cls, fullname): 1269 def load_module(cls, fullname):
1274 """Load a built-in module.""" 1270 """Load a built-in module."""
1275 with _ManageReload(fullname): 1271 with _ManageReload(fullname):
1276 return _call_with_frames_removed(_imp.init_builtin, fullname) 1272 module = _call_with_frames_removed(_imp.init_builtin, fullname)
1273 module.__loader__ = cls
1274 module.__package__ = ''
1275 return module
1277 1276
1278 @classmethod 1277 @classmethod
1279 @_requires_builtin 1278 @_requires_builtin
1280 def get_code(cls, fullname): 1279 def get_code(cls, fullname):
1281 """Return None as built-in modules do not have code objects.""" 1280 """Return None as built-in modules do not have code objects."""
1282 return None 1281 return None
1283 1282
1284 @classmethod 1283 @classmethod
1285 @_requires_builtin 1284 @_requires_builtin
1286 def get_source(cls, fullname): 1285 def get_source(cls, fullname):
1287 """Return None as built-in modules do not have source code.""" 1286 """Return None as built-in modules do not have source code."""
1288 return None 1287 return None
1289 1288
1290 @classmethod 1289 @classmethod
1291 @_requires_builtin 1290 @_requires_builtin
1292 def is_package(cls, fullname): 1291 def is_package(cls, fullname):
1293 """Return False as built-in modules are never packages.""" 1292 """Return False as built-in modules are never packages."""
1294 # XXX DeprecationWarning here... 1293 # XXX DeprecationWarning here...
1295 return False 1294 return False
1296 1295
1297 1296
1298 class FrozenImporter: 1297 class FrozenImporter:
1299 1298
1300 """Meta path import for frozen modules. 1299 """Meta path import for frozen modules.
1301 1300
1302 All methods are either class or static methods to avoid the need to 1301 All methods are either class or static methods to avoid the need to
1303 instantiate the class. 1302 instantiate the class.
1304 1303
1305 """ 1304 """
1306 _ORIGIN = 'frozen' 1305
1307 1306 @staticmethod
1308 # XXX BC issues with removing? 1307 def module_repr(m):
1309 @classmethod 1308 # XXX deprecate
1310 def module_repr(cls, m):
1311 return '<module {!r} (frozen)>'.format(m.__name__) 1309 return '<module {!r} (frozen)>'.format(m.__name__)
1312 1310
1313 # XXX Add shims for find_module and load_module. 1311 @classmethod
1314 1312 def find_spec(cls, fullname, path=None, target=None):
1315 # XXX Change to find_spec(). 1313 if _imp.is_frozen(fullname):
1314 return spec_from_loader(fullname, cls, origin='frozen')
1315 else:
1316 return None
1317
1316 @classmethod 1318 @classmethod
1317 def find_module(cls, fullname, path=None): 1319 def find_module(cls, fullname, path=None):
1318 """Find a frozen module.""" 1320 """Find a frozen module."""
1319 return cls if _imp.is_frozen(fullname) else None 1321 return cls if _imp.is_frozen(fullname) else None
1320 1322
1321 # XXX Change to exec_module(). 1323 @staticmethod
1322 @classmethod 1324 def exec_module(module):
1323 @set_package 1325 name = module.__spec__.name
1324 @set_loader 1326 if not _imp.is_frozen(name):
1325 @_requires_frozen 1327 raise ImportError('{!r} is not a frozen module'.format(name),
1328 name=name)
1329 code = _call_with_frames_removed(_imp.get_frozen_object, name)
1330 exec(code, module.__dict__)
1331
1332 @classmethod
1326 def load_module(cls, fullname): 1333 def load_module(cls, fullname):
1327 """Load a frozen module.""" 1334 """Load a frozen module."""
1328 with _ManageReload(fullname): 1335 return _load_module_shim(cls, fullname)
1329 m = _call_with_frames_removed(_imp.init_frozen, fullname)
1330 # Let our own module_repr() method produce a suitable repr.
1331 del m.__file__
1332 return m
1333 1336
1334 @classmethod 1337 @classmethod
1335 @_requires_frozen 1338 @_requires_frozen
1336 def get_code(cls, fullname): 1339 def get_code(cls, fullname):
1337 """Return the code object for the frozen module.""" 1340 """Return the code object for the frozen module."""
1338 return _imp.get_frozen_object(fullname) 1341 return _imp.get_frozen_object(fullname)
1339 1342
1340 @classmethod 1343 @classmethod
1341 @_requires_frozen 1344 @_requires_frozen
1342 def get_source(cls, fullname): 1345 def get_source(cls, fullname):
1343 """Return None as frozen modules do not have source code.""" 1346 """Return None as frozen modules do not have source code."""
1344 return None 1347 return None
1345 1348
1346 @classmethod 1349 @classmethod
1347 @_requires_frozen 1350 @_requires_frozen
1348 def is_package(cls, fullname): 1351 def is_package(cls, fullname):
1349 """Return True if the frozen module is a package.""" 1352 """Return True if the frozen module is a package."""
1350 return _imp.is_frozen_package(fullname) 1353 return _imp.is_frozen_package(fullname)
1351 1354
1352 1355
1353 class WindowsRegistryFinder: 1356 class WindowsRegistryFinder:
1354 1357
1355 """Meta path finder for modules declared in the Windows registry. 1358 """Meta path finder for modules declared in the Windows registry."""
1356 """
1357 1359
1358 REGISTRY_KEY = ( 1360 REGISTRY_KEY = (
1359 'Software\\Python\\PythonCore\\{sys_version}' 1361 'Software\\Python\\PythonCore\\{sys_version}'
1360 '\\Modules\\{fullname}') 1362 '\\Modules\\{fullname}')
1361 REGISTRY_KEY_DEBUG = ( 1363 REGISTRY_KEY_DEBUG = (
1362 'Software\\Python\\PythonCore\\{sys_version}' 1364 'Software\\Python\\PythonCore\\{sys_version}'
1363 '\\Modules\\{fullname}\\Debug') 1365 '\\Modules\\{fullname}\\Debug')
1364 DEBUG_BUILD = False # Changed in _setup() 1366 DEBUG_BUILD = False # Changed in _setup()
1365
1366 # XXX Add shim for find_module().
1367 1367
1368 @classmethod 1368 @classmethod
1369 def _open_registry(cls, key): 1369 def _open_registry(cls, key):
1370 try: 1370 try:
1371 return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key) 1371 return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key)
1372 except OSError: 1372 except OSError:
1373 return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key) 1373 return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
1374 1374
1375 @classmethod 1375 @classmethod
1376 def _search_registry(cls, fullname): 1376 def _search_registry(cls, fullname):
1377 if cls.DEBUG_BUILD: 1377 if cls.DEBUG_BUILD:
1378 registry_key = cls.REGISTRY_KEY_DEBUG 1378 registry_key = cls.REGISTRY_KEY_DEBUG
1379 else: 1379 else:
1380 registry_key = cls.REGISTRY_KEY 1380 registry_key = cls.REGISTRY_KEY
1381 key = registry_key.format(fullname=fullname, 1381 key = registry_key.format(fullname=fullname,
1382 sys_version=sys.version[:3]) 1382 sys_version=sys.version[:3])
1383 try: 1383 try:
1384 with cls._open_registry(key) as hkey: 1384 with cls._open_registry(key) as hkey:
1385 filepath = _winreg.QueryValue(hkey, '') 1385 filepath = _winreg.QueryValue(hkey, '')
1386 except OSError: 1386 except OSError:
1387 return None 1387 return None
1388 return filepath 1388 return filepath
1389 1389
1390 @classmethod 1390 @classmethod
1391 def find_module(cls, fullname, path=None): 1391 def find_spec(cls, fullname, path=None, target=None):
1392 """Find module named in the registry.""" 1392 # XXX untested! Need a Windows person to write tests (otherwise mock out appropriately)
1393 filepath = cls._search_registry(fullname) 1393 filepath = cls._search_registry(fullname)
1394 if filepath is None: 1394 if filepath is None:
1395 return None 1395 return None
1396 try: 1396 try:
1397 _os.stat(filepath) 1397 _path_stat(filepath)
1398 except OSError: 1398 except OSError:
1399 return None 1399 return None
1400 for loader, suffixes in _get_supported_file_loaders(): 1400 for loader, suffixes in _get_supported_file_loaders():
1401 if filepath.endswith(tuple(suffixes)): 1401 if filepath.endswith(tuple(suffixes)):
1402 return loader(fullname, filepath) 1402 spec = spec_from_loader(fullname, loader(fullname, filepath),
1403 origin=filepath)
1404 return spec
1405
1406 @classmethod
1407 def find_module(cls, fullname, path=None):
1408 """Find module named in the registry."""
1409 spec = self.find_spec(fullname, path)
1410 if spec is not None:
1411 return spec.loader
1412 else:
1413 return None
1403 1414
1404 1415
1405 class _LoaderBasics: 1416 class _LoaderBasics:
1406 1417
1407 """Base class of common code needed by both SourceLoader and 1418 """Base class of common code needed by both SourceLoader and
1408 SourcelessFileLoader.""" 1419 SourcelessFileLoader."""
1409
1410 # XXX Add shim for load_module().
1411 1420
1412 # XXX deprecate? 1421 # XXX deprecate?
1413 def is_package(self, fullname): 1422 def is_package(self, fullname):
1414 """Concrete implementation of InspectLoader.is_package by checking if 1423 """Concrete implementation of InspectLoader.is_package by checking if
1415 the path returned by get_filename has a filename of '__init__.py'.""" 1424 the path returned by get_filename has a filename of '__init__.py'."""
1416 filename = _path_split(self.get_filename(fullname))[1] 1425 filename = _path_split(self.get_filename(fullname))[1]
1417 filename_base = filename.rsplit('.', 1)[0] 1426 filename_base = filename.rsplit('.', 1)[0]
1418 tail_name = fullname.rpartition('.')[2] 1427 tail_name = fullname.rpartition('.')[2]
1419 return filename_base == '__init__' and tail_name != '__init__' 1428 return filename_base == '__init__' and tail_name != '__init__'
1420 1429
1421 # XXX Remove. 1430 def exec_module(self, module):
1422 def init_module_attrs(self, module): 1431 """Execute the module."""
1423 """Set various attributes on the module. 1432 code = self.get_code(module.__name__)
1424 1433 if code is None:
1425 ExecutionLoader.init_module_attrs() is used to set __loader__, 1434 raise ImportError('cannot load module {!r} when get_code() '
1426 __package__, __file__, and optionally __path__. The __cached__ attribute 1435 'returns None'.format(module.__name__))
1427 is set using imp.cache_from_source() and __file__. 1436 _call_with_frames_removed(exec, code, module.__dict__)
1428 """ 1437
1429 module.__loader__ = self # Loader 1438 load_module = _load_module_shim
1430 _init_package_attrs(self, module) # InspectLoader
1431 _init_file_attrs(self, module) # ExecutionLoader
1432 if hasattr(module, '__file__'): # SourceLoader
1433 try:
1434 module.__cached__ = cache_from_source(module.__file__)
1435 except NotImplementedError:
1436 pass
1437
1438 # XXX Change to exec_module().
1439 def load_module(self, fullname):
1440 """Load the specified module into sys.modules and return it."""
1441 with module_to_load(fullname) as module:
1442 self.init_module_attrs(module)
1443 code = self.get_code(fullname)
1444 if code is None:
1445 raise ImportError('cannot load module {!r} when get_code() '
1446 'returns None'.format(fullname))
1447 _call_with_frames_removed(exec, code, module.__dict__)
1448 return module
1449 1439
1450 1440
1451 class SourceLoader(_LoaderBasics): 1441 class SourceLoader(_LoaderBasics):
1452 1442
1453 def path_mtime(self, path): 1443 def path_mtime(self, path):
1454 """Optional method that returns the modification time (an int) for the 1444 """Optional method that returns the modification time (an int) for the
1455 specified path, where path is a str. 1445 specified path, where path is a str.
1456 1446
1457 Raises IOError when the path cannot be handled. 1447 Raises IOError when the path cannot be handled.
1458 """ 1448 """
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1556 except NotImplementedError: 1546 except NotImplementedError:
1557 pass 1547 pass
1558 return code_object 1548 return code_object
1559 1549
1560 1550
1561 class FileLoader: 1551 class FileLoader:
1562 1552
1563 """Base file loader class which implements the loader protocol methods that 1553 """Base file loader class which implements the loader protocol methods that
1564 require file system usage.""" 1554 require file system usage."""
1565 1555
1566 # XXX Change to take spec as arg.
1567 def __init__(self, fullname, path): 1556 def __init__(self, fullname, path):
1568 """Cache the module name and the path to the file found by the 1557 """Cache the module name and the path to the file found by the
1569 finder.""" 1558 finder."""
1570 self.name = fullname 1559 self.name = fullname
1571 self.path = path 1560 self.path = path
1572 1561
1573 # XXX Change to exec_module().
1574 @_check_name 1562 @_check_name
1575 def load_module(self, fullname): 1563 def load_module(self, fullname):
1576 """Load a module from a file.""" 1564 """Load a module from a file."""
1577 # Issue #14857: Avoid the zero-argument form so the implementation 1565 # The only reason for this method is for the name check.
1566
1567 # Issue #14857: Avoid the zero-argument form of super so the implementat ion
1578 # of that form can be updated without breaking the frozen module 1568 # of that form can be updated without breaking the frozen module
1579 return super(FileLoader, self).load_module(fullname) 1569 return super(FileLoader, self).load_module(fullname)
1580 1570
1581 @_check_name 1571 @_check_name
1582 def get_filename(self, fullname): 1572 def get_filename(self, fullname):
1583 """Return the path to the source file as found by the finder.""" 1573 """Return the path to the source file as found by the finder."""
1584 return self.path 1574 return self.path
1585 1575
1586 def get_data(self, path): 1576 def get_data(self, path):
1587 """Return the data from path as raw bytes.""" 1577 """Return the data from path as raw bytes."""
1588 with _io.FileIO(path, 'r') as file: 1578 with _io.FileIO(path, 'r') as file:
1589 return file.read() 1579 return file.read()
1590 1580
1591 1581
1592 class SourceFileLoader(FileLoader, SourceLoader): 1582 class SourceFileLoader(FileLoader, SourceLoader):
1593 1583
1594 """Concrete implementation of SourceLoader using the file system.""" 1584 """Concrete implementation of SourceLoader using the file system."""
1595 1585
1596 def path_stats(self, path): 1586 def path_stats(self, path):
1597 """Return the metadata for the path.""" 1587 """Return the metadata for the path."""
1598 st = _os.stat(path) 1588 st = _path_stat(path)
1599 return {'mtime': st.st_mtime, 'size': st.st_size} 1589 return {'mtime': st.st_mtime, 'size': st.st_size}
1600 1590
1601 def _cache_bytecode(self, source_path, bytecode_path, data): 1591 def _cache_bytecode(self, source_path, bytecode_path, data):
1602 # Adapt between the two APIs 1592 # Adapt between the two APIs
1603 mode = _calc_mode(source_path) 1593 mode = _calc_mode(source_path)
1604 return self.set_data(bytecode_path, data, _mode=mode) 1594 return self.set_data(bytecode_path, data, _mode=mode)
1605 1595
1606 def set_data(self, path, data, *, _mode=0o666): 1596 def set_data(self, path, data, *, _mode=0o666):
1607 """Write bytes data to a file.""" 1597 """Write bytes data to a file."""
1608 parent, filename = _path_split(path) 1598 parent, filename = _path_split(path)
(...skipping 20 matching lines...) Expand all
1629 _verbose_message('created {!r}', path) 1619 _verbose_message('created {!r}', path)
1630 except OSError as exc: 1620 except OSError as exc:
1631 # Same as above: just don't write the bytecode. 1621 # Same as above: just don't write the bytecode.
1632 _verbose_message('could not create {!r}: {!r}', path, exc) 1622 _verbose_message('could not create {!r}: {!r}', path, exc)
1633 1623
1634 1624
1635 class SourcelessFileLoader(FileLoader, _LoaderBasics): 1625 class SourcelessFileLoader(FileLoader, _LoaderBasics):
1636 1626
1637 """Loader which handles sourceless file imports.""" 1627 """Loader which handles sourceless file imports."""
1638 1628
1639 # XXX Merge into finder.
1640 def init_module_attrs(self, module):
1641 super().init_module_attrs(module)
1642 module.__cached__ = module.__file__
1643
1644 def get_code(self, fullname): 1629 def get_code(self, fullname):
1645 path = self.get_filename(fullname) 1630 path = self.get_filename(fullname)
1646 data = self.get_data(path) 1631 data = self.get_data(path)
1647 bytes_data = _validate_bytecode_header(data, name=fullname, path=path) 1632 bytes_data = _validate_bytecode_header(data, name=fullname, path=path)
1648 return _compile_bytecode(bytes_data, name=fullname, bytecode_path=path) 1633 return _compile_bytecode(bytes_data, name=fullname, bytecode_path=path)
1649 1634
1650 def get_source(self, fullname): 1635 def get_source(self, fullname):
1651 """Return None as there is no source code.""" 1636 """Return None as there is no source code."""
1652 return None 1637 return None
1653 1638
1654 1639
1655 # Filled in by _setup(). 1640 # Filled in by _setup().
1656 EXTENSION_SUFFIXES = [] 1641 EXTENSION_SUFFIXES = []
1657 1642
1658 1643
1659 class ExtensionFileLoader: 1644 class ExtensionFileLoader:
1660 1645
1661 """Loader for extension modules. 1646 """Loader for extension modules.
1662 1647
1663 The constructor is designed to work with FileFinder. 1648 The constructor is designed to work with FileFinder.
1664 1649
1665 """ 1650 """
1666 1651
1667 # XXX Change to take spec as arg.
1668 def __init__(self, name, path): 1652 def __init__(self, name, path):
1669 self.name = name 1653 self.name = name
1670 self.path = path 1654 self.path = path
1671 1655
1672 # XXX Add shim for load_module().
1673
1674 # XXX Change tp exec_module().
1675 @_check_name 1656 @_check_name
1676 @set_package
1677 @set_loader
1678 def load_module(self, fullname): 1657 def load_module(self, fullname):
1679 """Load an extension module.""" 1658 """Load an extension module."""
1680 with _ManageReload(fullname): 1659 with _ManageReload(fullname):
1681 module = _call_with_frames_removed(_imp.load_dynamic, 1660 module = _call_with_frames_removed(_imp.load_dynamic,
1682 fullname, self.path) 1661 fullname, self.path)
1683 _verbose_message('extension module loaded from {!r}', self.path) 1662 _verbose_message('extension module loaded from {!r}', self.path)
1684 if self.is_package(fullname) and not hasattr(module, '__path__'): 1663 is_package = self.is_package(fullname)
1685 module.__path__ = [_path_split(self.path)[0]] 1664 if is_package and not hasattr(module, '__path__'):
1686 return module 1665 module.__path__ = [_path_split(self.path)[0]]
1666 module.__loader__ = self
1667 module.__package__ = module.__name__
1668 if not is_package:
1669 module.__package__ = module.__package__.rpartition('.')[0]
1670 return module
1687 1671
1688 def is_package(self, fullname): 1672 def is_package(self, fullname):
1689 """Return True if the extension module is a package.""" 1673 """Return True if the extension module is a package."""
1690 file_name = _path_split(self.path)[1] 1674 file_name = _path_split(self.path)[1]
1691 return any(file_name == '__init__' + suffix 1675 return any(file_name == '__init__' + suffix
1692 for suffix in EXTENSION_SUFFIXES) 1676 for suffix in EXTENSION_SUFFIXES)
1693 1677
1694 def get_code(self, fullname): 1678 def get_code(self, fullname):
1695 """Return None as an extension module cannot create a code object.""" 1679 """Return None as an extension module cannot create a code object."""
1696 return None 1680 return None
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1729 return parent, '__path__' 1713 return parent, '__path__'
1730 1714
1731 def _get_parent_path(self): 1715 def _get_parent_path(self):
1732 parent_module_name, path_attr_name = self._find_parent_path_names() 1716 parent_module_name, path_attr_name = self._find_parent_path_names()
1733 return getattr(sys.modules[parent_module_name], path_attr_name) 1717 return getattr(sys.modules[parent_module_name], path_attr_name)
1734 1718
1735 def _recalculate(self): 1719 def _recalculate(self):
1736 # If the parent's path has changed, recalculate _path 1720 # If the parent's path has changed, recalculate _path
1737 parent_path = tuple(self._get_parent_path()) # Make a copy 1721 parent_path = tuple(self._get_parent_path()) # Make a copy
1738 if parent_path != self._last_parent_path: 1722 if parent_path != self._last_parent_path:
1739 loader, new_path = self._path_finder(self._name, parent_path) 1723 spec = self._path_finder(self._name, parent_path)
1740 # Note that no changes are made if a loader is returned, but we 1724 # Note that no changes are made if a loader is returned, but we
1741 # do remember the new parent path 1725 # do remember the new parent path
1742 if loader is None: 1726 if spec is not None and spec.loader is None:
1743 self._path = new_path 1727 if spec.submodule_search_locations:
1728 self._path = spec.submodule_search_locations
1744 self._last_parent_path = parent_path # Save the copy 1729 self._last_parent_path = parent_path # Save the copy
1745 return self._path 1730 return self._path
1746 1731
1747 def __iter__(self): 1732 def __iter__(self):
1748 return iter(self._recalculate()) 1733 return iter(self._recalculate())
1749 1734
1750 def __len__(self): 1735 def __len__(self):
1751 return len(self._recalculate()) 1736 return len(self._recalculate())
1752 1737
1753 def __repr__(self): 1738 def __repr__(self):
1754 return '_NamespacePath({!r})'.format(self._path) 1739 return '_NamespacePath({!r})'.format(self._path)
1755 1740
1756 def __contains__(self, item): 1741 def __contains__(self, item):
1757 return item in self._recalculate() 1742 return item in self._recalculate()
1758 1743
1759 def append(self, item): 1744 def append(self, item):
1760 self._path.append(item) 1745 self._path.append(item)
1761 1746
1762 1747
1763 class NamespaceLoader: 1748 # We use this exclusively in init_module_attrs() for backward-compatibility.
1764 """A loader that implements the InspectLoader API.""" 1749 class _NamespaceLoader:
1765
1766 # XXX Change to take spec as arg.
1767 def __init__(self, name, path, path_finder): 1750 def __init__(self, name, path, path_finder):
1768 self._path = _NamespacePath(name, path, path_finder) 1751 self._path = _NamespacePath(name, path, path_finder)
1769 1752
1770 # XXX Add shim for load_module(). 1753 # XXX Deprecate
1754 @classmethod
1755 def module_repr(cls, module):
1756 return '<module {!r} (namespace)>'.format(module.__name__)
1771 1757
1772 def is_package(self, fullname): 1758 def is_package(self, fullname):
1773 return True 1759 return True
1774 1760
1775 def get_source(self, fullname): 1761 def get_source(self, fullname):
1776 return '' 1762 return ''
1777 1763
1778 def get_code(self, fullname): 1764 def get_code(self, fullname):
1779 return compile('', '<string>', 'exec', dont_inherit=True) 1765 return compile('', '<string>', 'exec', dont_inherit=True)
1780 1766
1781 # XXX Remove. 1767 # XXX Deprecate
1782 def init_module_attrs(self, module):
1783 module.__loader__ = self
1784 module.__package__ = module.__name__
1785
1786 # XXX Change to exec_module().
1787 def load_module(self, fullname): 1768 def load_module(self, fullname):
1788 """Load a namespace module.""" 1769 """Load a namespace module."""
1789 _verbose_message('namespace module loaded with path {!r}', self._path) 1770 _verbose_message('namespace module loaded with path {!r}', self._path)
1790 with module_to_load(fullname) as module: 1771 return _load_module_shim(self, fullname)
1791 self.init_module_attrs(module)
1792 module.__path__ = self._path
1793 return module
1794 1772
1795 1773
1796 # Finders ##################################################################### 1774 # Finders #####################################################################
1797 1775
1798 class PathFinder: 1776 class PathFinder:
1799 1777
1800 """Meta path finder for sys.path and package __path__ attributes.""" 1778 """Meta path finder for sys.path and package __path__ attributes."""
1801
1802 # XXX Add shim for find_module().
1803 1779
1804 @classmethod 1780 @classmethod
1805 def invalidate_caches(cls): 1781 def invalidate_caches(cls):
1806 """Call the invalidate_caches() method on all path entry finders 1782 """Call the invalidate_caches() method on all path entry finders
1807 stored in sys.path_importer_caches (where implemented).""" 1783 stored in sys.path_importer_caches (where implemented)."""
1808 for finder in sys.path_importer_cache.values(): 1784 for finder in sys.path_importer_cache.values():
1809 if hasattr(finder, 'invalidate_caches'): 1785 if hasattr(finder, 'invalidate_caches'):
1810 finder.invalidate_caches() 1786 finder.invalidate_caches()
1811 1787
1812 @classmethod 1788 @classmethod
(...skipping 15 matching lines...) Expand all
1828 1804
1829 @classmethod 1805 @classmethod
1830 def _path_importer_cache(cls, path): 1806 def _path_importer_cache(cls, path):
1831 """Get the finder for the path entry from sys.path_importer_cache. 1807 """Get the finder for the path entry from sys.path_importer_cache.
1832 1808
1833 If the path entry is not in the cache, find the appropriate finder 1809 If the path entry is not in the cache, find the appropriate finder
1834 and cache it. If no finder is available, store None. 1810 and cache it. If no finder is available, store None.
1835 1811
1836 """ 1812 """
1837 if path == '': 1813 if path == '':
1838 path = '.' 1814 path = _os.getcwd()
1839 try: 1815 try:
1840 finder = sys.path_importer_cache[path] 1816 finder = sys.path_importer_cache[path]
1841 except KeyError: 1817 except KeyError:
1842 finder = cls._path_hooks(path) 1818 finder = cls._path_hooks(path)
1843 sys.path_importer_cache[path] = finder 1819 sys.path_importer_cache[path] = finder
1844 return finder 1820 return finder
1845 1821
1846 @classmethod 1822 @classmethod
1847 def _get_loader(cls, fullname, path): 1823 def _legacy_get_spec(cls, fullname, finder):
1824 if hasattr(finder, 'find_loader'):
1825 loader, portions = finder.find_loader(fullname)
1826 else:
1827 loader = finder.find_module(fullname)
1828 portions = None
1829 if loader is not None:
1830 return spec_from_loader(fullname, loader)
1831 spec = ModuleSpec(fullname, None)
1832 spec.submodule_search_locations = portions
1833 return spec
1834
1835 @classmethod
1836 def _get_spec(cls, fullname, path, target=None):
1848 """Find the loader or namespace_path for this module/package name.""" 1837 """Find the loader or namespace_path for this module/package name."""
1849 # If this ends up being a namespace package, namespace_path is 1838 # If this ends up being a namespace package, namespace_path is
1850 # the list of paths that will become its __path__ 1839 # the list of paths that will become its __path__
1851 namespace_path = [] 1840 namespace_path = []
1852 for entry in path: 1841 for entry in path:
1853 if not isinstance(entry, (str, bytes)): 1842 if not isinstance(entry, (str, bytes)):
1854 continue 1843 continue
1855 finder = cls._path_importer_cache(entry) 1844 finder = cls._path_importer_cache(entry)
1856 if finder is not None: 1845 if finder is not None:
1857 if hasattr(finder, 'find_loader'): 1846 if hasattr(finder, 'find_spec'):
1858 loader, portions = finder.find_loader(fullname) 1847 spec = finder.find_spec(fullname, target)
1859 else: 1848 else:
1860 loader = finder.find_module(fullname) 1849 spec = cls._legacy_get_spec(fullname, finder)
1861 portions = [] 1850 if spec is None:
1862 if loader is not None: 1851 continue
1863 # We found a loader: return it immediately. 1852 if spec.loader is not None:
1864 return loader, namespace_path 1853 return spec
1854 portions = spec.submodule_search_locations
1855 if portions is None:
1856 raise ImportError('spec missing loader')
1865 # This is possibly part of a namespace package. 1857 # This is possibly part of a namespace package.
1866 # Remember these path entries (if any) for when we 1858 # Remember these path entries (if any) for when we
1867 # create a namespace package, and continue iterating 1859 # create a namespace package, and continue iterating
1868 # on path. 1860 # on path.
1869 namespace_path.extend(portions) 1861 namespace_path.extend(portions)
1870 else: 1862 else:
1871 return None, namespace_path 1863 spec = ModuleSpec(fullname, None)
1872 1864 spec.submodule_search_locations = namespace_path
1873 @classmethod 1865 return spec
1874 def find_spec(cls, fullname, path=None): 1866
1867 @classmethod
1868 def find_spec(cls, fullname, path=None, target=None):
1875 """find the module on sys.path or 'path' based on sys.path_hooks and 1869 """find the module on sys.path or 'path' based on sys.path_hooks and
1876 sys.path_importer_cache.""" 1870 sys.path_importer_cache."""
1877 if path is None: 1871 if path is None:
1878 path = sys.path 1872 path = sys.path
1879 loader, namespace_path = cls._get_loader(fullname, path) 1873 spec = cls._get_spec(fullname, path, target)
1880 if loader is None: 1874 if spec is None:
1875 return None
1876 elif spec.loader is None:
1877 namespace_path = spec.submodule_search_locations
1881 if namespace_path: 1878 if namespace_path:
1882 # We found at least one namespace path. Return a 1879 # We found at least one namespace path. Return a
1883 # loader which can create the namespace package. 1880 # spec which can create the namespace package.
1884 loader = NamespaceLoader(fullname, namespace_path, 1881 spec.origin = 'namespace'
1885 cls._get_loader) 1882 spec.submodule_search_locations = _NamespacePath(fullname, names pace_path, cls._get_spec)
1886 return spec_from_loader(fullname, loader, origin='namespace', 1883 return spec
1887 is_package=True)
1888 else: 1884 else:
1889 return None 1885 return None
1890 else: 1886 else:
1891 # Defer to loader.get_filename() for the location. 1887 return spec
1892 return spec_from_file_location(fullname, loader=loader) 1888
1893
1894 # XXX Deprecate.
1895 @classmethod 1889 @classmethod
1896 def find_module(cls, fullname, path=None): 1890 def find_module(cls, fullname, path=None):
1897 """find the module on sys.path or 'path' based on sys.path_hooks and 1891 """find the module on sys.path or 'path' based on sys.path_hooks and
1898 sys.path_importer_cache.""" 1892 sys.path_importer_cache."""
1893 # XXX Deprecation warning here.
1899 spec = cls.find_spec(fullname, path) 1894 spec = cls.find_spec(fullname, path)
1900 if spec is None: 1895 if spec is None:
1901 return None 1896 return None
1902 return spec.loader 1897 return spec.loader
1903 1898
1904 1899
1905 class FileFinder: 1900 class FileFinder:
1906 1901
1907 """File-based finder. 1902 """File-based finder.
1908 1903
(...skipping 13 matching lines...) Expand all
1922 # Base (directory) path 1917 # Base (directory) path
1923 self.path = path or '.' 1918 self.path = path or '.'
1924 self._path_mtime = -1 1919 self._path_mtime = -1
1925 self._path_cache = set() 1920 self._path_cache = set()
1926 self._relaxed_path_cache = set() 1921 self._relaxed_path_cache = set()
1927 1922
1928 def invalidate_caches(self): 1923 def invalidate_caches(self):
1929 """Invalidate the directory mtime.""" 1924 """Invalidate the directory mtime."""
1930 self._path_mtime = -1 1925 self._path_mtime = -1
1931 1926
1932 # XXX Use a different shim.
1933 find_module = _find_module_shim 1927 find_module = _find_module_shim
1934 1928
1935 # XXX Change to find_spec().
1936 def find_loader(self, fullname): 1929 def find_loader(self, fullname):
1930 """Try to find a loader for the specified module, or the namespace
1931 package portions. Returns (loader, list-of-portions)."""
1932 spec = self.find_spec(fullname)
1933 if spec is None:
1934 return None, []
1935 return spec.loader, spec.submodule_search_locations or []
1936
1937 def _get_spec(self, loader_class, fullname, path, submodule_search_locations , target):
1938 loader = loader_class(fullname, path)
1939 try:
1940 get_spec = loader._get_spec
1941 except AttributeError:
1942 return spec_from_file_location(fullname, path, loader=loader,
1943 submodule_search_locations=submodule_ search_locations)
1944 else:
1945 return get_spec(fullname, path, submodule_search_locations, target)
1946
1947 def find_spec(self, fullname, target=None):
1937 """Try to find a loader for the specified module, or the namespace 1948 """Try to find a loader for the specified module, or the namespace
1938 package portions. Returns (loader, list-of-portions).""" 1949 package portions. Returns (loader, list-of-portions)."""
1939 is_namespace = False 1950 is_namespace = False
1940 tail_module = fullname.rpartition('.')[2] 1951 tail_module = fullname.rpartition('.')[2]
1941 try: 1952 try:
1942 mtime = _os.stat(self.path).st_mtime 1953 mtime = _path_stat(self.path or _os.getcwd()).st_mtime
1943 except OSError: 1954 except OSError:
1944 mtime = -1 1955 mtime = -1
1945 if mtime != self._path_mtime: 1956 if mtime != self._path_mtime:
1946 self._fill_cache() 1957 self._fill_cache()
1947 self._path_mtime = mtime 1958 self._path_mtime = mtime
1948 # tail_module keeps the original casing, for __file__ and friends 1959 # tail_module keeps the original casing, for __file__ and friends
1949 if _relax_case(): 1960 if _relax_case():
1950 cache = self._relaxed_path_cache 1961 cache = self._relaxed_path_cache
1951 cache_module = tail_module.lower() 1962 cache_module = tail_module.lower()
1952 else: 1963 else:
1953 cache = self._path_cache 1964 cache = self._path_cache
1954 cache_module = tail_module 1965 cache_module = tail_module
1955 # Check if the module is the name of a directory (and thus a package). 1966 # Check if the module is the name of a directory (and thus a package).
1956 if cache_module in cache: 1967 if cache_module in cache:
1957 base_path = _path_join(self.path, tail_module) 1968 base_path = _path_join(self.path, tail_module)
1958 if _path_isdir(base_path): 1969 for suffix, loader_class in self._loaders:
1959 for suffix, loader in self._loaders: 1970 init_filename = '__init__' + suffix
1960 init_filename = '__init__' + suffix 1971 full_path = _path_join(base_path, init_filename)
1961 full_path = _path_join(base_path, init_filename) 1972 if _path_isfile(full_path):
1962 if _path_isfile(full_path): 1973 return self._get_spec(loader_class, fullname, full_path, [ba se_path], target)
1963 return (loader(fullname, full_path), [base_path]) 1974 else:
1964 else: 1975 # If a namespace package, return the path if we don't
1965 # A namespace package, return the path if we don't also 1976 # find a module in the next section.
1966 # find a module in the next section. 1977 is_namespace = _path_isdir(base_path)
1967 is_namespace = True
1968 # Check for a file w/ a proper suffix exists. 1978 # Check for a file w/ a proper suffix exists.
1969 for suffix, loader in self._loaders: 1979 for suffix, loader_class in self._loaders:
1970 full_path = _path_join(self.path, tail_module + suffix) 1980 full_path = _path_join(self.path, tail_module + suffix)
1971 _verbose_message('trying {}'.format(full_path), verbosity=2) 1981 _verbose_message('trying {}'.format(full_path), verbosity=2)
1972 if cache_module + suffix in cache: 1982 if cache_module + suffix in cache:
1973 if _path_isfile(full_path): 1983 if _path_isfile(full_path):
1974 return (loader(fullname, full_path), []) 1984 return self._get_spec(loader_class, fullname, full_path, Non e, target)
1975 if is_namespace: 1985 if is_namespace:
1976 _verbose_message('possible namespace for {}'.format(base_path)) 1986 _verbose_message('possible namespace for {}'.format(base_path))
1977 return (None, [base_path]) 1987 spec = ModuleSpec(fullname, None)
1978 return (None, []) 1988 spec.submodule_search_locations = [base_path]
1989 return spec
1990 return None
1979 1991
1980 def _fill_cache(self): 1992 def _fill_cache(self):
1981 """Fill the cache of potential modules and packages for this directory." "" 1993 """Fill the cache of potential modules and packages for this directory." ""
1982 path = self.path 1994 path = self.path
1983 try: 1995 try:
1984 contents = _os.listdir(path) 1996 contents = _os.listdir(path or _os.getcwd())
1985 except (FileNotFoundError, PermissionError, NotADirectoryError): 1997 except (FileNotFoundError, PermissionError, NotADirectoryError):
1986 # Directory has either been removed, turned into a file, or made 1998 # Directory has either been removed, turned into a file, or made
1987 # unreadable. 1999 # unreadable.
1988 contents = [] 2000 contents = []
1989 # We store two cached versions, to handle runtime changes of the 2001 # We store two cached versions, to handle runtime changes of the
1990 # PYTHONCASEOK environment variable. 2002 # PYTHONCASEOK environment variable.
1991 if not sys.platform.startswith('win'): 2003 if not sys.platform.startswith('win'):
1992 self._path_cache = set(contents) 2004 self._path_cache = set(contents)
1993 else: 2005 else:
1994 # Windows users can import modules with case-insensitive file 2006 # Windows users can import modules with case-insensitive file
1995 # suffixes (for legacy reasons). Make the suffix lowercase here 2007 # suffixes (for legacy reasons). Make the suffix lowercase here
1996 # so it's done once instead of for every import. This is safe as 2008 # so it's done once instead of for every import. This is safe as
1997 # the specified suffixes to check against are always specified in a 2009 # the specified suffixes to check against are always specified in a
1998 # case-sensitive manner. 2010 # case-sensitive manner.
1999 lower_suffix_contents = set() 2011 lower_suffix_contents = set()
2000 for item in contents: 2012 for item in contents:
2001 name, dot, suffix = item.partition('.') 2013 name, dot, suffix = item.partition('.')
2002 if dot: 2014 if dot:
2003 new_name = '{}.{}'.format(name, suffix.lower()) 2015 new_name = '{}.{}'.format(name, suffix.lower())
2004 else: 2016 else:
2005 new_name = name 2017 new_name = name
2006 lower_suffix_contents.add(new_name) 2018 lower_suffix_contents.add(new_name)
2007 self._path_cache = lower_suffix_contents 2019 self._path_cache = lower_suffix_contents
2008 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS): 2020 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
2009 self._relaxed_path_cache = {fn.lower() for fn in contents} 2021 self._relaxed_path_cache = {fn.lower() for fn in contents}
2010 2022
2011 # XXX See _get_supported_file_loaders().
2012 @classmethod 2023 @classmethod
2013 def path_hook(cls, *loader_details): 2024 def path_hook(cls, *loader_details):
2014 """A class method which returns a closure to use on sys.path_hook 2025 """A class method which returns a closure to use on sys.path_hook
2015 which will return an instance using the specified loaders and the path 2026 which will return an instance using the specified loaders and the path
2016 called on the closure. 2027 called on the closure.
2017 2028
2018 If the path called on the closure is not a directory, ImportError is 2029 If the path called on the closure is not a directory, ImportError is
2019 raised. 2030 raised.
2020 2031
2021 """ 2032 """
(...skipping 26 matching lines...) Expand all
2048 2059
2049 def _resolve_name(name, package, level): 2060 def _resolve_name(name, package, level):
2050 """Resolve a relative module name to an absolute one.""" 2061 """Resolve a relative module name to an absolute one."""
2051 bits = package.rsplit('.', level - 1) 2062 bits = package.rsplit('.', level - 1)
2052 if len(bits) < level: 2063 if len(bits) < level:
2053 raise ValueError('attempted relative import beyond top-level package') 2064 raise ValueError('attempted relative import beyond top-level package')
2054 base = bits[0] 2065 base = bits[0]
2055 return '{}.{}'.format(base, name) if name else base 2066 return '{}.{}'.format(base, name) if name else base
2056 2067
2057 2068
2058 def _find_spec(name, path): 2069 def _find_spec(name, path, target=None):
2059 """Find a module's loader.""" 2070 """Find a module's loader."""
2060 if not sys.meta_path: 2071 if not sys.meta_path:
2061 _warnings.warn('sys.meta_path is empty', ImportWarning) 2072 _warnings.warn('sys.meta_path is empty', ImportWarning)
2073 # We check sys.modules here for the reload case. While a passed-in
2074 # target will usually indicate a reload there is no guarantee, whereas
2075 # sys.modules provides one.
2076 is_reload = name in sys.modules
2062 for finder in sys.meta_path: 2077 for finder in sys.meta_path:
2063 with _ImportLockContext(): 2078 with _ImportLockContext():
2064 try: 2079 try:
2065 find_spec = finder.find_spec 2080 find_spec = finder.find_spec
2066 except AttributeError: 2081 except AttributeError:
2067 loader = finder.find_module(name, path) 2082 loader = finder.find_module(name, path)
2068 if loader is None: 2083 if loader is None:
2069 continue 2084 continue
2070 spec = spec_from_loader(name, loader) 2085 spec = spec_from_loader(name, loader)
2071 else: 2086 else:
2072 spec = find_spec(name, path) 2087 spec = find_spec(name, path, target)
2073 if spec is not None: 2088 if spec is not None:
2074 # The parent import may have already imported this module. 2089 # The parent import may have already imported this module.
2075 if name in sys.modules: 2090 if not is_reload and name in sys.modules:
2076 module = sys.modules[name] 2091 module = sys.modules[name]
2077 try: 2092 try:
2078 return module.__spec__ 2093 __spec__ = module.__spec__
2079 except AttributeError: 2094 except AttributeError:
2080 # XXX Use _spec_from_module() here? 2095 # We use the found spec since that is the one that
2096 # we would have used if the parent module hadn't
2097 # beaten us to the punch.
2081 return spec 2098 return spec
2099 else:
2100 if __spec__ is None:
2101 return spec
2102 else:
2103 return __spec__
2082 else: 2104 else:
2083 return spec 2105 return spec
2084 else: 2106 else:
2085 return None 2107 return None
2086 2108
2087 2109
2088 def _sanity_check(name, package, level): 2110 def _sanity_check(name, package, level):
2089 """Verify arguments are "sane".""" 2111 """Verify arguments are "sane"."""
2090 if not isinstance(name, str): 2112 if not isinstance(name, str):
2091 raise TypeError('module name must be str, not {}'.format(type(name))) 2113 raise TypeError('module name must be str, not {}'.format(type(name)))
(...skipping 15 matching lines...) Expand all
2107 2129
2108 def _find_and_load_unlocked(name, import_): 2130 def _find_and_load_unlocked(name, import_):
2109 path = None 2131 path = None
2110 parent = name.rpartition('.')[0] 2132 parent = name.rpartition('.')[0]
2111 if parent: 2133 if parent:
2112 if parent not in sys.modules: 2134 if parent not in sys.modules:
2113 _call_with_frames_removed(import_, parent) 2135 _call_with_frames_removed(import_, parent)
2114 # Crazy side-effects! 2136 # Crazy side-effects!
2115 if name in sys.modules: 2137 if name in sys.modules:
2116 return sys.modules[name] 2138 return sys.modules[name]
2117 # Backwards-compatibility; be nicer to skip the dict lookup.
2118 parent_module = sys.modules[parent] 2139 parent_module = sys.modules[parent]
2119 try: 2140 try:
2120 path = parent_module.__path__ 2141 path = parent_module.__path__
2121 except AttributeError: 2142 except AttributeError:
2122 msg = (_ERR_MSG + '; {} is not a package').format(name, parent) 2143 msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent)
2123 raise ImportError(msg, name=name) 2144 raise ImportError(msg, name=name)
2124 spec = _find_spec(name, path) 2145 spec = _find_spec(name, path)
2125 if spec is None: 2146 if spec is None:
2126 raise ImportError(_ERR_MSG.format(name), name=name) 2147 raise ImportError(_ERR_MSG.format(name), name=name)
2127 else: 2148 else:
2128 module = spec._load_unlocked() 2149 module = _SpecMethods(spec)._load_unlocked()
2129 if parent: 2150 if parent:
2130 # Set the module as an attribute on its parent. 2151 # Set the module as an attribute on its parent.
2131 parent_module = sys.modules[parent] 2152 parent_module = sys.modules[parent]
2132 setattr(parent_module, name.rpartition('.')[2], module) 2153 setattr(parent_module, name.rpartition('.')[2], module)
2133 return module 2154 return module
2134 2155
2135 2156
2136 # XXX Eliminate?
2137 def _find_and_load(name, import_): 2157 def _find_and_load(name, import_):
2138 """Find and load the module, and release the import lock.""" 2158 """Find and load the module, and release the import lock."""
2139 with _ModuleLockManager(name): 2159 with _ModuleLockManager(name):
2140 return _find_and_load_unlocked(name, import_) 2160 return _find_and_load_unlocked(name, import_)
2141 2161
2142 2162
2143 def _gcd_import(name, package=None, level=0): 2163 def _gcd_import(name, package=None, level=0):
2144 """Import and return the module based on its name, the package the call is 2164 """Import and return the module based on its name, the package the call is
2145 being made from, and the level adjustment. 2165 being made from, and the level adjustment.
2146 2166
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
2250 # Figure out where to slice the module's name up to the first dot 2270 # Figure out where to slice the module's name up to the first dot
2251 # in 'name'. 2271 # in 'name'.
2252 cut_off = len(name) - len(name.partition('.')[0]) 2272 cut_off = len(name) - len(name.partition('.')[0])
2253 # Slice end needs to be positive to alleviate need to special-case 2273 # Slice end needs to be positive to alleviate need to special-case
2254 # when ``'.' not in name``. 2274 # when ``'.' not in name``.
2255 return sys.modules[module.__name__[:len(module.__name__)-cut_off]] 2275 return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
2256 else: 2276 else:
2257 return _handle_fromlist(module, fromlist, _gcd_import) 2277 return _handle_fromlist(module, fromlist, _gcd_import)
2258 2278
2259 2279
2280 def _builtin_from_name(name):
2281 spec = BuiltinImporter.find_spec(name)
2282 if spec is None:
2283 raise ImportError('no built-in module named ' + name)
2284 methods = _SpecMethods(spec)
2285 return methods._load_unlocked()
2286
2287
2260 def _setup(sys_module, _imp_module): 2288 def _setup(sys_module, _imp_module):
2261 """Setup importlib by importing needed built-in modules and injecting them 2289 """Setup importlib by importing needed built-in modules and injecting them
2262 into the global namespace. 2290 into the global namespace.
2263 2291
2264 As sys is needed for sys.modules access and _imp is needed to load built-in 2292 As sys is needed for sys.modules access and _imp is needed to load built-in
2265 modules, those two modules must be explicitly passed in. 2293 modules, those two modules must be explicitly passed in.
2266 2294
2267 """ 2295 """
2268 global _imp, sys, BYTECODE_SUFFIXES 2296 global _imp, sys, BYTECODE_SUFFIXES
2269 _imp = _imp_module 2297 _imp = _imp_module
2270 sys = sys_module 2298 sys = sys_module
2271 2299
2272 if sys.flags.optimize: 2300 if sys.flags.optimize:
2273 BYTECODE_SUFFIXES = OPTIMIZED_BYTECODE_SUFFIXES 2301 BYTECODE_SUFFIXES = OPTIMIZED_BYTECODE_SUFFIXES
2274 else: 2302 else:
2275 BYTECODE_SUFFIXES = DEBUG_BYTECODE_SUFFIXES 2303 BYTECODE_SUFFIXES = DEBUG_BYTECODE_SUFFIXES
2276 2304
2277 # Set up the spec for existing builtin/frozen modules. 2305 # Set up the spec for existing builtin/frozen modules.
2278 module_type = type(sys) 2306 module_type = type(sys)
2279 for name, module in sys.modules.items(): 2307 for name, module in sys.modules.items():
2280 if isinstance(module, module_type): 2308 if isinstance(module, module_type):
2281 if name in sys.builtin_module_names: 2309 if name in sys.builtin_module_names:
2282 loader = BuiltinImporter 2310 loader = BuiltinImporter
2283 elif _imp.is_frozen(name): 2311 elif _imp.is_frozen(name):
2284 loader = FrozenImporter 2312 loader = FrozenImporter
2285 else: 2313 else:
2286 continue 2314 continue
2287 spec = _spec_from_module(module, loader) 2315 spec = _spec_from_module(module, loader)
2288 spec.init_module_attrs(module) 2316 methods = _SpecMethods(spec)
2317 methods.init_module_attrs(module)
2289 2318
2290 # Directly load built-in modules needed during bootstrap. 2319 # Directly load built-in modules needed during bootstrap.
2291 self_module = sys.modules[__name__] 2320 self_module = sys.modules[__name__]
2292 for builtin_name in ('_io', '_warnings', 'builtins', 'marshal'): 2321 for builtin_name in ('_io', '_warnings', 'builtins', 'marshal'):
2293 if builtin_name not in sys.modules: 2322 if builtin_name not in sys.modules:
2294 spec = BuiltinImporter._get_spec(builtin_name) 2323 builtin_module = _builtin_from_name(builtin_name)
2295 builtin_module = spec._load_unlocked()
2296 else: 2324 else:
2297 builtin_module = sys.modules[builtin_name] 2325 builtin_module = sys.modules[builtin_name]
2298 setattr(self_module, builtin_name, builtin_module) 2326 setattr(self_module, builtin_name, builtin_module)
2299 2327
2300 # Directly load the os module (needed during bootstrap). 2328 # Directly load the os module (needed during bootstrap).
2301 os_details = ('posix', ['/']), ('nt', ['\\', '/']) 2329 os_details = ('posix', ['/']), ('nt', ['\\', '/'])
2302 for builtin_os, path_separators in os_details: 2330 for builtin_os, path_separators in os_details:
2303 # Assumption made in _path_join() 2331 # Assumption made in _path_join()
2304 assert all(len(sep) == 1 for sep in path_separators) 2332 assert all(len(sep) == 1 for sep in path_separators)
2305 path_sep = path_separators[0] 2333 path_sep = path_separators[0]
2306 if builtin_os in sys.modules: 2334 if builtin_os in sys.modules:
2307 os_module = sys.modules[builtin_os] 2335 os_module = sys.modules[builtin_os]
2308 break 2336 break
2309 else: 2337 else:
2310 try: 2338 try:
2311 spec = BuiltinImporter._get_spec(builtin_os) 2339 os_module = _builtin_from_name(builtin_os)
2312 os_module = spec._load_unlocked()
2313 break 2340 break
2314 except ImportError: 2341 except ImportError:
2315 continue 2342 continue
2316 else: 2343 else:
2317 raise ImportError('importlib requires posix or nt') 2344 raise ImportError('importlib requires posix or nt')
2318 setattr(self_module, '_os', os_module) 2345 setattr(self_module, '_os', os_module)
2319 setattr(self_module, 'path_sep', path_sep) 2346 setattr(self_module, 'path_sep', path_sep)
2320 setattr(self_module, 'path_separators', ''.join(path_separators)) 2347 setattr(self_module, 'path_separators', ''.join(path_separators))
2321 2348
2322 # Directly load the _thread module (needed during bootstrap). 2349 # Directly load the _thread module (needed during bootstrap).
2323 try: 2350 try:
2324 spec = BuiltinImporter._get_spec('_thread') 2351 thread_module = _builtin_from_name('_thread')
2325 thread_module = spec._load_unlocked()
2326 except ImportError: 2352 except ImportError:
2327 # Python was built without threads 2353 # Python was built without threads
2328 thread_module = None 2354 thread_module = None
2329 setattr(self_module, '_thread', thread_module) 2355 setattr(self_module, '_thread', thread_module)
2330 2356
2331 # Directly load the _weakref module (needed during bootstrap). 2357 # Directly load the _weakref module (needed during bootstrap).
2332 spec = BuiltinImporter._get_spec('_weakref') 2358 weakref_module = _builtin_from_name('_weakref')
2333 weakref_module = spec._load_unlocked()
2334 setattr(self_module, '_weakref', weakref_module) 2359 setattr(self_module, '_weakref', weakref_module)
2335 2360
2336 # Directly load the winreg module (needed during bootstrap). 2361 # Directly load the winreg module (needed during bootstrap).
2337 if builtin_os == 'nt': 2362 if builtin_os == 'nt':
2338 spec = BuiltinImporter._get_spec('winreg') 2363 winreg_module = _builtin_from_name('winreg')
2339 winreg_module = spec._load_unlocked()
2340 setattr(self_module, '_winreg', winreg_module) 2364 setattr(self_module, '_winreg', winreg_module)
2341 2365
2342 # Constants 2366 # Constants
2343 setattr(self_module, '_relax_case', _make_relax_case()) 2367 setattr(self_module, '_relax_case', _make_relax_case())
2344 EXTENSION_SUFFIXES.extend(_imp.extension_suffixes()) 2368 EXTENSION_SUFFIXES.extend(_imp.extension_suffixes())
2345 if builtin_os == 'nt': 2369 if builtin_os == 'nt':
2346 SOURCE_SUFFIXES.append('.pyw') 2370 SOURCE_SUFFIXES.append('.pyw')
2347 if '_d.pyd' in EXTENSION_SUFFIXES: 2371 if '_d.pyd' in EXTENSION_SUFFIXES:
2348 WindowsRegistryFinder.DEBUG_BUILD = True 2372 WindowsRegistryFinder.DEBUG_BUILD = True
2349 2373
2350 2374
2351 def _install(sys_module, _imp_module): 2375 def _install(sys_module, _imp_module):
2352 """Install importlib as the implementation of import.""" 2376 """Install importlib as the implementation of import."""
2353 _setup(sys_module, _imp_module) 2377 _setup(sys_module, _imp_module)
2354 supported_loaders = _get_supported_file_loaders() 2378 supported_loaders = _get_supported_file_loaders()
2355 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)]) 2379 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
2356 sys.meta_path.append(BuiltinImporter) 2380 sys.meta_path.append(BuiltinImporter)
2357 sys.meta_path.append(FrozenImporter) 2381 sys.meta_path.append(FrozenImporter)
2358 if _os.__name__ == 'nt': 2382 if _os.__name__ == 'nt':
2359 sys.meta_path.append(WindowsRegistryFinder) 2383 sys.meta_path.append(WindowsRegistryFinder)
2360 sys.meta_path.append(PathFinder) 2384 sys.meta_path.append(PathFinder)
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+