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

Side by Side Diff: Lib/importlib/_bootstrap.py

Issue 18864: Implementation for PEP 451 (importlib.machinery.ModuleSpec)
Patch Set: Created 5 years, 7 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:
View unified diff | Download patch
OLDNEW
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 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 self.count += 1 213 self.count += 1
214 return True 214 return True
215 215
216 def release(self): 216 def release(self):
217 if self.count == 0: 217 if self.count == 0:
218 raise RuntimeError('cannot release un-acquired lock') 218 raise RuntimeError('cannot release un-acquired lock')
219 self.count -= 1 219 self.count -= 1
220 220
221 def __repr__(self): 221 def __repr__(self):
222 return '_DummyModuleLock({!r}) at {}'.format(self.name, id(self)) 222 return '_DummyModuleLock({!r}) at {}'.format(self.name, id(self))
223
224
225 class _ModuleLockManager:
226
227 def __init__(self, name):
228 self._name = name
229 self._lock = None
230
231 def __enter__(self):
232 try:
233 self._lock = _get_module_lock(self._name)
234 finally:
235 _imp.release_lock()
236 self._lock.acquire()
237
238 def __exit__(self, *args, **kwargs):
239 self._lock.release()
223 240
224 241
225 # The following two functions are for consumption by Python/import.c. 242 # The following two functions are for consumption by Python/import.c.
226 243
227 def _get_module_lock(name): 244 def _get_module_lock(name):
228 """Get or create the module lock for a given module name. 245 """Get or create the module lock for a given module name.
229 246
230 Should only be called with the import lock taken.""" 247 Should only be called with the import lock taken."""
231 lock = None 248 lock = None
232 try: 249 try:
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 485
469 486
470 def _verbose_message(message, *args, verbosity=1): 487 def _verbose_message(message, *args, verbosity=1):
471 """Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" 488 """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
472 if sys.flags.verbose >= verbosity: 489 if sys.flags.verbose >= verbosity:
473 if not message.startswith(('#', 'import ')): 490 if not message.startswith(('#', 'import ')):
474 message = '# ' + message 491 message = '# ' + message
475 print(message.format(*args), file=sys.stderr) 492 print(message.format(*args), file=sys.stderr)
476 493
477 494
495 # XXX Remove.
478 class _ManageReload: 496 class _ManageReload:
479 497
480 def __init__(self, name): 498 def __init__(self, name):
481 self._name = name 499 self._name = name
482 500
483 def __enter__(self): 501 def __enter__(self):
484 self._is_reload = self._name in sys.modules 502 self._is_reload = self._name in sys.modules
485 503
486 def __exit__(self, *args): 504 def __exit__(self, *args):
487 if any(arg is not None for arg in args) and not self._is_reload: 505 if any(arg is not None for arg in args) and not self._is_reload:
488 try: 506 try:
489 del sys.modules[self._name] 507 del sys.modules[self._name]
490 except KeyError: 508 except KeyError:
491 pass 509 pass
492 510
493 511
512 # XXX Remove.
494 # Written as a class only because contextlib is not available. 513 # Written as a class only because contextlib is not available.
495 class _ModuleManager(_ManageReload): 514 class _ModuleManager(_ManageReload):
496 515
497 """Context manager which returns the module to be loaded. 516 """Context manager which returns the module to be loaded.
498 517
499 Does the proper unloading from sys.modules upon failure. 518 Does the proper unloading from sys.modules upon failure.
500 519
501 """ 520 """
502 521
503 def __init__(self, name, *, reset_name=True): 522 def __init__(self, name, *, reset_name=True):
(...skipping 23 matching lines...) Expand all
527 except AttributeError: 546 except AttributeError:
528 pass 547 pass
529 return self._module 548 return self._module
530 549
531 def __exit__(self, *args): 550 def __exit__(self, *args):
532 self._module.__initializing__ = False 551 self._module.__initializing__ = False
533 del self._module 552 del self._module
534 super().__exit__(*args) 553 super().__exit__(*args)
535 554
536 555
556 # XXX Remove.
537 def module_to_load(name, *, reset_name=True): 557 def module_to_load(name, *, reset_name=True):
538 """Return a context manager which provides the module object to load. 558 """Return a context manager which provides the module object to load.
539 559
540 If reset_name is true, reset the module's __name__ to 'name'. 560 If reset_name is true, reset the module's __name__ to 'name'.
541 """ 561 """
542 # Hiding _ModuleManager behind a function for better naming. 562 # Hiding _ModuleManager behind a function for better naming.
543 return _ModuleManager(name, reset_name=reset_name) 563 return _ModuleManager(name, reset_name=reset_name)
544 564
545 565
566 # XXX Remove.
546 def _init_package_attrs(loader, module): 567 def _init_package_attrs(loader, module):
547 """Set __package__ and __path__ based on what loader.is_package() says.""" 568 """Set __package__ and __path__ based on what loader.is_package() says."""
548 name = module.__name__ 569 name = module.__name__
549 try: 570 try:
550 is_package = loader.is_package(name) 571 is_package = loader.is_package(name)
551 except ImportError: 572 except ImportError:
552 pass 573 pass
553 else: 574 else:
554 if is_package: 575 if is_package:
555 module.__package__ = name 576 module.__package__ = name
556 module.__path__ = [] 577 module.__path__ = []
557 else: 578 else:
558 module.__package__ = name.rpartition('.')[0] 579 module.__package__ = name.rpartition('.')[0]
559 580
560 581
582 # XXX Remove.
561 def _init_file_attrs(loader, module): 583 def _init_file_attrs(loader, module):
562 """Set __file__ and __path__ based on loader.get_filename().""" 584 """Set __file__ and __path__ based on loader.get_filename()."""
563 try: 585 try:
564 module.__file__ = loader.get_filename(module.__name__) 586 module.__file__ = loader.get_filename(module.__name__)
565 except ImportError: 587 except ImportError:
566 pass 588 pass
567 else: 589 else:
568 if module.__name__ == module.__package__: 590 if module.__name__ == module.__package__:
569 module.__path__.append(_path_split(module.__file__)[0]) 591 module.__path__.append(_path_split(module.__file__)[0])
570 592
571 593
594 # XXX Can be moved to importlib.util (and deprecated).
572 def set_package(fxn): 595 def set_package(fxn):
573 """Set __package__ on the returned module.""" 596 """Set __package__ on the returned module."""
574 def set_package_wrapper(*args, **kwargs): 597 def set_package_wrapper(*args, **kwargs):
575 module = fxn(*args, **kwargs) 598 module = fxn(*args, **kwargs)
576 if getattr(module, '__package__', None) is None: 599 if getattr(module, '__package__', None) is None:
577 module.__package__ = module.__name__ 600 module.__package__ = module.__name__
578 if not hasattr(module, '__path__'): 601 if not hasattr(module, '__path__'):
579 module.__package__ = module.__package__.rpartition('.')[0] 602 module.__package__ = module.__package__.rpartition('.')[0]
580 return module 603 return module
581 _wrap(set_package_wrapper, fxn) 604 _wrap(set_package_wrapper, fxn)
582 return set_package_wrapper 605 return set_package_wrapper
583 606
584 607
608 # XXX Can be moved to importlib.util (and deprecated).
585 def set_loader(fxn): 609 def set_loader(fxn):
586 """Set __loader__ on the returned module.""" 610 """Set __loader__ on the returned module."""
587 def set_loader_wrapper(self, *args, **kwargs): 611 def set_loader_wrapper(self, *args, **kwargs):
588 module = fxn(self, *args, **kwargs) 612 module = fxn(self, *args, **kwargs)
589 if getattr(module, '__loader__', None) is None: 613 if getattr(module, '__loader__', None) is None:
590 module.__loader__ = self 614 module.__loader__ = self
591 return module 615 return module
592 _wrap(set_loader_wrapper, fxn) 616 _wrap(set_loader_wrapper, fxn)
593 return set_loader_wrapper 617 return set_loader_wrapper
594 618
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 752
729 Universal newline support is used in the decoding. 753 Universal newline support is used in the decoding.
730 """ 754 """
731 import tokenize # To avoid bootstrap issues. 755 import tokenize # To avoid bootstrap issues.
732 source_bytes_readline = _io.BytesIO(source_bytes).readline 756 source_bytes_readline = _io.BytesIO(source_bytes).readline
733 encoding = tokenize.detect_encoding(source_bytes_readline) 757 encoding = tokenize.detect_encoding(source_bytes_readline)
734 newline_decoder = _io.IncrementalNewlineDecoder(None, True) 758 newline_decoder = _io.IncrementalNewlineDecoder(None, True)
735 return newline_decoder.decode(source_bytes.decode(encoding[0])) 759 return newline_decoder.decode(source_bytes.decode(encoding[0]))
736 760
737 761
762 # Module specifications #######################################################
763
764 def _module_repr(module):
765 # The implementation of ModuleType__repr__().
766 loader = getattr(module, '__loader__', None)
767 if hasattr(loader, 'module_repr'):
768 # XXX Deprecation Warning here...
769 try:
770 return loader.module_repr(module)
771 except Exception:
772 pass
773 try:
774 spec = module.__spec__
775 except AttributeError:
776 # XXX Use module.__class__.__name__ instead of 'module'?
777 try:
778 name = module.__name__
779 except AttributeError:
780 name = '?'
781 try:
782 filename = module.__file__
783 except AttributeError:
784 if loader is None:
785 return '<module {!r}>'.format(name)
786 else:
787 return '<module {!r} ({!r})>'.format(name, loader)
788 else:
789 return '<module {!r} from {!r}>'.format(name, filename)
790 else:
791 return _SpecMethods(spec).module_repr()
792
793
794 class _installed_safely:
795
796 def __init__(self, module):
797 self._module = module
798 self._spec = module.__spec__
799
800 def __enter__(self):
801 # This must be done before putting the module in sys.modules
802 # (otherwise an optimization shortcut in import.c becomes
803 # wrong)
804 self._spec._initializing = True
805 sys.modules[self._spec.name] = self._module
806
807 def __exit__(self, *args):
808 try:
809 spec = self._spec
810 if any(arg is not None for arg in args):
811 try:
812 del sys.modules[spec.name]
813 except KeyError:
814 pass
815 else:
816 _verbose_message('import {!r} # {!r}', spec.name, spec.loader)
817 finally:
818 self._spec._initializing = False
819
820
821 class ModuleSpec:
822 """The specification for a module, used for loading.
823
824 A module's spec is the source for information about the module. For
825 data associated with the module, including source, use the spec's
826 loader.
827
828 `name` is the absolute name of the module. `loader` is the loader
829 to use when loading the module. `parent` is the name of the
830 package the module is in. The parent is derived from the name.
831
832 `is_package` determines if the module is considered a package or
833 not. On modules this is reflected by the `__path__` attribute.
834
835 `origin` is the specific location used by the loader from which to
836 load the module, if that information is available. When filename is
837 set, origin will match.
838
839 `has_location` indicates that a spec's "origin" reflects a location.
840 When this is True, `__file__` attribute of the module is set.
841
842 `cached` is the location of the cached bytecode file, if any. It
843 corresponds to the `__cached__` attribute.
844
845 `submodule_search_locations` is the sequence of path entries to
846 search when importing submodules. If set, is_package should be
847 True--and False otherwise.
848
849 Packages are simply modules that (may) have submodules. If a spec
850 has a non-None value in `submodule_search_locations`, the import
851 system will consider modules loaded from the spec as packages.
852
853 Only finders (see importlib.abc.MetaPathFinder and
854 importlib.abc.PathEntryFinder) should modify ModuleSpec instances.
855
856 """
857
858 def __init__(self, name, loader, *, origin=None, loader_state=None,
859 is_package=None):
860 self.name = name
861 self.loader = loader
862 self.origin = origin
863 self.loader_state = loader_state
864 self.submodule_search_locations = [] if is_package else None
865
866 # file-location attributes
867 self._set_fileattr = False
868 self._cached = None
869
870 def __repr__(self):
871 args = ['name={!r}'.format(self.name),
872 'loader={!r}'.format(self.loader)]
873 if self.origin is not None:
874 args.append('origin={!r}'.format(self.origin))
875 if self.submodule_search_locations is not None:
876 args.append('submodule_search_locations={}'
877 .format(self.submodule_search_locations))
878 return '{}({})'.format(self.__class__.__name__, ', '.join(args))
879
880 @property
881 def cached(self):
882 if self._cached is None:
883 if self.origin is not None and self._set_fileattr:
884 filename = self.origin
885 if filename.endswith(tuple(SOURCE_SUFFIXES)):
886 try:
887 self._cached = cache_from_source(filename)
888 except NotImplementedError:
889 pass
890 elif filename.endswith(tuple(BYTECODE_SUFFIXES)):
891 self._cached = filename
892 return self._cached
893
894 @cached.setter
895 def cached(self, cached):
896 self._cached = cached
897
898 @property
899 def parent(self):
900 """The name of the module's parent."""
901 if self.submodule_search_locations is None:
902 return self.name.rpartition('.')[0]
903 else:
904 return self.name
905
906 @property
907 def has_location(self):
908 return self._set_fileattr
909
910
911 def spec_from_loader(name, loader, *, origin=None, is_package=None):
912 """Return a module spec based on various loader methods."""
913
914 # if hasattr(loader, 'get_data'):
915 if hasattr(loader, 'get_filename'):
916 if is_package is None:
917 return spec_from_file_location(name, loader=loader)
918 search = [] if is_package else None
919 return spec_from_file_location(name, loader=loader,
920 submodule_search_locations=search)
921
922 if is_package is None:
923 if hasattr(loader, 'is_package'):
924 try:
925 is_package = loader.is_package(name)
926 except ImportError:
927 is_package = None # aka, undefined
928 else:
929 # the default
930 is_package = False
931
932 return ModuleSpec(name, loader, origin=origin, is_package=is_package)
933
934
935 _POPULATE = object()
936
937
938 def spec_from_file_location(name, location=None, *, loader=None,
939 submodule_search_locations=_POPULATE):
940 """Return a module spec based on a file location.
941
942 To indicate that the module is a package, set
943 submodule_search_locations to a list of directory paths. An
944 empty list is sufficient, though its not otherwise useful to the
945 import system.
946
947 The loader must take a spec as its only __init__() arg.
948
949 """
950 if location is None:
951 if hasattr(loader, 'get_filename'):
952 try:
953 location = loader.get_filename(name)
954 except ImportError:
955 # XXX Return None?
956 location = '<unknown>'
957 else:
958 # XXX Return None?
959 location = '<unknown>'
960 # XXX This breaks for zipimport (and any indirect location).
961 #try:
962 # _os.stat(location)
963 #except OSError:
964 # return None
965
966 spec = ModuleSpec(name, loader, origin=location)
967 spec._set_fileattr = True
968
969 # Pick a loader if one wasn't provided.
970 if loader is None:
971 for loader_class, suffixes in _get_supported_file_loaders():
972 if location.endswith(tuple(suffixes)):
973 loader = loader_class(name, location)
974 spec.loader = loader
975 break
976 else:
977 return None
978
979 # Set submodule_search_paths appropriately.
980 if submodule_search_locations is _POPULATE:
981 # Check the loader.
982 if hasattr(loader, 'is_package'):
983 try:
984 is_package = loader.is_package(name)
985 except ImportError:
986 pass
987 else:
988 if is_package:
989 spec.submodule_search_locations = []
990 else:
991 spec.submodule_search_locations = submodule_search_locations
992 if spec.submodule_search_locations == []:
993 # if location:
994 dirname = _path_split(location)[0]
995 spec.submodule_search_locations.append(dirname)
996
997 return spec
998
999
1000 def _spec_from_module(module, loader=None, origin=None):
1001 try:
1002 return module.__spec__
1003 except AttributeError:
1004 pass
1005
1006 name = module.__name__
1007 if loader is None:
1008 try:
1009 loader = module.__loader__
1010 except AttributeError:
1011 loader = None
Jim.J.Jewett 2013/10/30 03:32:01 If I'm reading this correctly, loader was None (or
eric.snow 2013/11/01 01:07:38 I think it's just a holdover from an earlier versi
1012 try:
1013 location = module.__file__
1014 except AttributeError:
1015 location = None
1016 if origin is None:
1017 if location is None:
1018 try:
1019 origin = loader._ORIGIN
1020 except AttributeError:
1021 origin = None
1022 else:
1023 origin = location
1024 try:
1025 cached = module.__cached__
1026 except AttributeError:
1027 cached = None
1028 try:
1029 submodule_search_locations = list(module.__path__)
1030 except AttributeError:
1031 submodule_search_locations = None
1032
1033 spec = ModuleSpec(name, loader, origin=origin)
1034 spec._set_fileattr = False if location is None else True
1035 spec.cached = cached
1036 spec.submodule_search_locations = submodule_search_locations
1037 return spec
1038
1039
1040 class _SpecMethods:
1041
1042 @classmethod
1043 def from_module(cls, module):
1044 try:
1045 spec = module.__spec__
1046 except AttributeError:
1047 try:
1048 loader = spec.__loader__
1049 except AttributeError:
1050 spec = _find_spec(module.__name__)
1051 else:
1052 spec = spec_from_loader(module.__name__, loader)
1053 return cls(spec)
1054
1055 def __init__(self, spec):
1056 self.spec = spec
1057
1058 def module_repr(self):
1059 """Return the repr to use for the module."""
1060 # We mostly replicate _module_repr() using the spec attributes.
1061 spec = self.spec
1062 name = '?' if spec.name is None else spec.name
1063 if spec.origin is None:
1064 if spec.loader is None:
1065 return '<module {!r}>'.format(name)
1066 else:
1067 return '<module {!r} ({!r})>'.format(name, spec.loader)
1068 else:
1069 if spec.has_location:
1070 return '<module {!r} from {!r}>'.format(name, spec.origin)
1071 else:
1072 return '<module {!r} ({})>'.format(spec.name, spec.origin)
1073
1074 def init_module_attrs(self, module, *, _override=False, _force_name=True):
1075 """Set the module's attributes.
1076
1077 All missing import-related module attributes will be set. Here
1078 is how the spec attributes map onto the module:
1079
1080 spec.name -> module.__name__
1081 spec.loader -> module.__loader__
1082 spec.parent -> module.__package__
1083 spec -> module.__spec__
1084
1085 Optional:
1086 spec.origin -> module.__file__ (if spec.set_fileattr is true)
1087 spec.cached -> module.__cached__ (if __file__ also set)
1088 spec.submodule_search_locations -> module.__path__ (if set)
1089
1090 """
1091 # XXX Make _override public?
1092 spec = self.spec
1093
1094 # The passed in module may be not support attribute assignment,
1095 # in which case we simply don't set the attributes.
1096
1097 # __name__
1098 if (_override or _force_name or
1099 getattr(module, '__name__', None) is None):
1100 try:
1101 module.__name__ = spec.name
1102 except AttributeError:
1103 pass
1104
1105 # __loader__
1106 if _override or getattr(module, '__loader__', None) is None:
1107 try:
1108 module.__loader__ = spec.loader
1109 except AttributeError:
1110 pass
1111
1112 # __package__
1113 if _override or getattr(module, '__package__', None) is None:
1114 try:
1115 module.__package__ = spec.parent
1116 except AttributeError:
1117 pass
1118
1119 # __spec__
1120 try:
1121 module.__spec__ = spec
1122 except AttributeError:
1123 pass
1124
1125 # __path__
1126 if _override or getattr(module, '__path__', None) is None:
1127 if spec.submodule_search_locations is not None:
1128 try:
1129 module.__path__ = spec.submodule_search_locations
1130 except AttributeError:
1131 pass
1132
1133 if spec.has_location:
1134 # __file__
1135 if _override or getattr(module, '__file__', None) is None:
1136 try:
1137 module.__file__ = spec.origin
1138 except AttributeError:
1139 pass
1140
1141 # __cached__
1142 if _override or getattr(module, '__cached__', None) is None:
1143 if spec.cached is not None:
1144 try:
1145 module.__cached__ = spec.cached
1146 except AttributeError:
1147 pass
1148
1149 def _update_module_attrs(self, module, limited=True):
1150 try:
1151 cached.__name__ = name
1152 except AttributeError:
1153 pass
1154 if not hasattr(cached, '__spec__'):
1155 try:
1156 cached.__spec__ = self.spec
1157 except AttributeError:
1158 pass
1159 if not limited:
1160 raise NotImplementedError
1161
1162 # Under normal circumstances _create(), _exec(), _load_unlocked(),
1163 # _load(), and # _reload() should not be called directly. So they
1164 # have underscore # names (a.k.a. private API).
1165
Jim.J.Jewett 2013/10/30 03:32:01 I think the above comment is out of date, as I don
eric.snow 2013/11/01 01:07:38 I'll yank it. They used to be methods on ModuleSp
1166 def create(self):
1167 """Return a new module to be loaded.
1168
1169 The import-related module attributes are also set with the
1170 appropriate values from the spec.
1171
1172 """
1173 spec = self.spec
1174 # Typically loaders will not implement create_module().
1175 if hasattr(spec.loader, 'create_module'):
1176 # If create_module() returns `None` it means the default
1177 # module creation should be used.
1178 module = spec.loader.create_module(spec)
1179 else:
1180 module = None
1181 if module is None:
1182 # This must be done before open() is ever called as the 'io'
1183 # module implicitly imports 'locale' and would otherwise
1184 # trigger an infinite loop.
1185 module = type(_io)(spec.name)
1186 self.init_module_attrs(module)
1187 return module
1188
1189 def _exec(self, module):
1190 """Do everything necessary to execute the module.
1191
1192 The namespace of `module` is used as the target of execution.
1193 This method uses the loader's `exec_module()` method.
1194
1195 """
1196 if self.spec.loader is None:
1197 # namespace package
1198 return
1199 self.spec.loader.exec_module(module)
1200
1201 def exec(self, module):
1202 """Execute the spec in an existing module's namespace."""
1203 name = self.spec.name
1204 _imp.acquire_lock()
1205 with _ModuleLockManager(name):
1206 if self.spec.loader is None:
1207 # namespace package
1208 self.init_module_attrs(module)
1209 return module
1210 if hasattr(self.spec.loader, 'supports_reload'):
1211 if not self.spec.loader.supports_reload(name):
1212 raise ImportError('loader does not support reloading',
1213 name=name)
1214 if sys.modules.get(name) is not module:
1215 raise ImportError('module not in sys.modules')
1216 self.init_module_attrs(module)
1217 if not hasattr(self.spec.loader, 'exec_module'):
1218 # XXX DeprecationWarning goes here...
1219 self.spec.loader.load_module(name)
1220 return sys.modules[name]
1221 module.__spec__ = self.spec # Must be set.
1222 self._exec(module)
1223 return sys.modules[name]
1224
1225 def _load_backward_compatible(self):
1226 # XXX DeprecationWarning goes here...
1227 spec = self.spec
1228 # The module must be in sys.modules!
1229 spec.loader.load_module(spec.name)
1230 module = sys.modules[spec.name]
1231 if getattr(module, '__loader__', None) is None:
1232 try:
1233 module.__loader__ = spec.loader
1234 except AttributeError:
1235 pass
1236 if getattr(module, '__package__', None) is None:
1237 try:
1238 # Since module.__path__ may not line up with
1239 # spec.submodule_search_paths, we can't necessarily rely
1240 # on spec.parent here.
1241 module.__package__ = module.__name__
1242 if not hasattr(module, '__path__'):
1243 module.__package__ = spec.name.rpartition('.')[0]
1244 except AttributeError:
1245 pass
1246 if getattr(module, '__spec__', None) is None:
1247 try:
1248 module.__spec__ = spec
1249 except AttributeError:
1250 pass
1251 return module
1252
1253 def _load_existing(self, module):
1254 """Exec the spec'ed module into an existing module's namespace."""
1255 # For use by runpy.
1256 with _installed_safely(module):
1257 loaded = self.exec(module)
1258 return loaded
1259
1260 def _load_unlocked(self):
1261 # A helper for direct use by the import system.
1262 if self.spec.loader is None:
1263 pass
1264 elif not hasattr(self.spec.loader, 'exec_module'):
1265 return self._load_backward_compatible()
1266
1267 module = self.create()
1268 with _installed_safely(module):
1269 loaded = self._exec(module)
1270
1271 # We don't ensure that the import-related module attributes get
1272 # set in the sys.modules replacement case. Such modules are on
1273 # their own.
1274 return sys.modules[self.spec.name]
1275
1276 def load(self):
1277 """Return a new module object, loaded by the spec's loader.
1278
1279 The module is not added to its parent.
1280
1281 If a module is already in sys.modules, that existing module gets
1282 clobbered.
1283
1284 """
1285 # XXX Chance for deadlock with circular import? Reload only?
1286 # XXX Check to see if already in sys.modules?
1287 _imp.acquire_lock()
1288 with _ModuleLockManager(self.spec.name):
1289 return self._load_unlocked()
1290
1291 # def reload(self, module):
Jim.J.Jewett 2013/10/30 03:32:01 I assume this will be either uncommented or delete
eric.snow 2013/11/01 01:07:38 Yep. Probably deleted. I always sweep up any cod
1292 # """Reload the module, leaving it in sys.modules.
1293 #
1294 # This is a simple reload of the module. `importlib.reload()`
1295 # provides a richer reload experience.
1296 #
1297 # """
1298 # _imp.acquire_lock()
1299 # with _ModuleLockManager(spec.name):
1300 # cls.init_module_attrs(spec, module, _force_name=True)
1301 # if not hasattr(spec.loader, 'exec_module'):
1302 # # XXX DeprecationWarning goes here...
1303 # spec.loader.load_module(spec.name)
1304 # return sys.modules[spec.name]
1305 # cls.exec(spec, module)
1306 # return sys.modules[spec.name]
1307
1308
738 # Loaders ##################################################################### 1309 # Loaders #####################################################################
739 1310
740 class BuiltinImporter: 1311 class BuiltinImporter:
741 1312
742 """Meta path import for built-in modules. 1313 """Meta path import for built-in modules.
743 1314
744 All methods are either class or static methods to avoid the need to 1315 All methods are either class or static methods to avoid the need to
745 instantiate the class. 1316 instantiate the class.
746 1317
747 """ 1318 """
1319 _ORIGIN = 'built-in'
748 1320
1321 @classmethod
1322 def _get_spec(cls, fullname):
1323 return spec_from_loader(fullname, cls, origin=cls._ORIGIN)
1324
1325 # XXX BC issues with removing?
749 @classmethod 1326 @classmethod
750 def module_repr(cls, module): 1327 def module_repr(cls, module):
751 return '<module {!r} (built-in)>'.format(module.__name__) 1328 return '<module {!r} (built-in)>'.format(module.__name__)
752 1329
1330 # XXX Add shims for find_module and load_module.
1331
1332 # XXX Change to find_spec().
753 @classmethod 1333 @classmethod
754 def find_module(cls, fullname, path=None): 1334 def find_module(cls, fullname, path=None):
755 """Find the built-in module. 1335 """Find the built-in module.
756 1336
757 If 'path' is ever specified then the search is considered a failure. 1337 If 'path' is ever specified then the search is considered a failure.
758 1338
759 """ 1339 """
760 if path is not None: 1340 if path is not None:
761 return None 1341 return None
762 return cls if _imp.is_builtin(fullname) else None 1342 return cls if _imp.is_builtin(fullname) else None
763 1343
1344 # XXX Change to exec_module().
764 @classmethod 1345 @classmethod
765 @set_package 1346 @set_package
766 @set_loader 1347 @set_loader
767 @_requires_builtin 1348 @_requires_builtin
768 def load_module(cls, fullname): 1349 def load_module(cls, fullname):
769 """Load a built-in module.""" 1350 """Load a built-in module."""
770 with _ManageReload(fullname): 1351 with _ManageReload(fullname):
771 return _call_with_frames_removed(_imp.init_builtin, fullname) 1352 return _call_with_frames_removed(_imp.init_builtin, fullname)
772 1353
773 @classmethod 1354 @classmethod
774 @_requires_builtin 1355 @_requires_builtin
775 def get_code(cls, fullname): 1356 def get_code(cls, fullname):
776 """Return None as built-in modules do not have code objects.""" 1357 """Return None as built-in modules do not have code objects."""
777 return None 1358 return None
778 1359
779 @classmethod 1360 @classmethod
780 @_requires_builtin 1361 @_requires_builtin
781 def get_source(cls, fullname): 1362 def get_source(cls, fullname):
782 """Return None as built-in modules do not have source code.""" 1363 """Return None as built-in modules do not have source code."""
783 return None 1364 return None
784 1365
785 @classmethod 1366 @classmethod
786 @_requires_builtin 1367 @_requires_builtin
787 def is_package(cls, fullname): 1368 def is_package(cls, fullname):
788 """Return False as built-in modules are never packages.""" 1369 """Return False as built-in modules are never packages."""
1370 # XXX DeprecationWarning here...
789 return False 1371 return False
790 1372
791 1373
792 class FrozenImporter: 1374 class FrozenImporter:
793 1375
794 """Meta path import for frozen modules. 1376 """Meta path import for frozen modules.
795 1377
796 All methods are either class or static methods to avoid the need to 1378 All methods are either class or static methods to avoid the need to
797 instantiate the class. 1379 instantiate the class.
798 1380
799 """ 1381 """
1382 _ORIGIN = 'frozen'
800 1383
1384 # XXX BC issues with removing?
801 @classmethod 1385 @classmethod
802 def module_repr(cls, m): 1386 def module_repr(cls, m):
803 return '<module {!r} (frozen)>'.format(m.__name__) 1387 return '<module {!r} (frozen)>'.format(m.__name__)
804 1388
1389 # XXX Add shims for find_module and load_module.
1390
1391 # XXX Change to find_spec().
805 @classmethod 1392 @classmethod
806 def find_module(cls, fullname, path=None): 1393 def find_module(cls, fullname, path=None):
807 """Find a frozen module.""" 1394 """Find a frozen module."""
808 return cls if _imp.is_frozen(fullname) else None 1395 return cls if _imp.is_frozen(fullname) else None
809 1396
1397 # XXX Change to exec_module().
810 @classmethod 1398 @classmethod
811 @set_package 1399 @set_package
812 @set_loader 1400 @set_loader
813 @_requires_frozen 1401 @_requires_frozen
814 def load_module(cls, fullname): 1402 def load_module(cls, fullname):
815 """Load a frozen module.""" 1403 """Load a frozen module."""
816 with _ManageReload(fullname): 1404 with _ManageReload(fullname):
817 m = _call_with_frames_removed(_imp.init_frozen, fullname) 1405 m = _call_with_frames_removed(_imp.init_frozen, fullname)
818 # Let our own module_repr() method produce a suitable repr. 1406 # Let our own module_repr() method produce a suitable repr.
819 del m.__file__ 1407 del m.__file__
(...skipping 23 matching lines...) Expand all
843 """Meta path finder for modules declared in the Windows registry. 1431 """Meta path finder for modules declared in the Windows registry.
844 """ 1432 """
845 1433
846 REGISTRY_KEY = ( 1434 REGISTRY_KEY = (
847 'Software\\Python\\PythonCore\\{sys_version}' 1435 'Software\\Python\\PythonCore\\{sys_version}'
848 '\\Modules\\{fullname}') 1436 '\\Modules\\{fullname}')
849 REGISTRY_KEY_DEBUG = ( 1437 REGISTRY_KEY_DEBUG = (
850 'Software\\Python\\PythonCore\\{sys_version}' 1438 'Software\\Python\\PythonCore\\{sys_version}'
851 '\\Modules\\{fullname}\\Debug') 1439 '\\Modules\\{fullname}\\Debug')
852 DEBUG_BUILD = False # Changed in _setup() 1440 DEBUG_BUILD = False # Changed in _setup()
1441
1442 # XXX Add shim for find_module().
853 1443
854 @classmethod 1444 @classmethod
855 def _open_registry(cls, key): 1445 def _open_registry(cls, key):
856 try: 1446 try:
857 return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key) 1447 return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key)
858 except OSError: 1448 except OSError:
859 return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key) 1449 return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
860 1450
861 @classmethod 1451 @classmethod
862 def _search_registry(cls, fullname): 1452 def _search_registry(cls, fullname):
(...skipping 23 matching lines...) Expand all
886 for loader, suffixes in _get_supported_file_loaders(): 1476 for loader, suffixes in _get_supported_file_loaders():
887 if filepath.endswith(tuple(suffixes)): 1477 if filepath.endswith(tuple(suffixes)):
888 return loader(fullname, filepath) 1478 return loader(fullname, filepath)
889 1479
890 1480
891 class _LoaderBasics: 1481 class _LoaderBasics:
892 1482
893 """Base class of common code needed by both SourceLoader and 1483 """Base class of common code needed by both SourceLoader and
894 SourcelessFileLoader.""" 1484 SourcelessFileLoader."""
895 1485
1486 # XXX Add shim for load_module().
1487
1488 # XXX deprecate?
896 def is_package(self, fullname): 1489 def is_package(self, fullname):
897 """Concrete implementation of InspectLoader.is_package by checking if 1490 """Concrete implementation of InspectLoader.is_package by checking if
898 the path returned by get_filename has a filename of '__init__.py'.""" 1491 the path returned by get_filename has a filename of '__init__.py'."""
899 filename = _path_split(self.get_filename(fullname))[1] 1492 filename = _path_split(self.get_filename(fullname))[1]
900 filename_base = filename.rsplit('.', 1)[0] 1493 filename_base = filename.rsplit('.', 1)[0]
901 tail_name = fullname.rpartition('.')[2] 1494 tail_name = fullname.rpartition('.')[2]
902 return filename_base == '__init__' and tail_name != '__init__' 1495 return filename_base == '__init__' and tail_name != '__init__'
903 1496
1497 # XXX Remove.
904 def init_module_attrs(self, module): 1498 def init_module_attrs(self, module):
905 """Set various attributes on the module. 1499 """Set various attributes on the module.
906 1500
907 ExecutionLoader.init_module_attrs() is used to set __loader__, 1501 ExecutionLoader.init_module_attrs() is used to set __loader__,
908 __package__, __file__, and optionally __path__. The __cached__ attribute 1502 __package__, __file__, and optionally __path__. The __cached__ attribute
909 is set using imp.cache_from_source() and __file__. 1503 is set using imp.cache_from_source() and __file__.
910 """ 1504 """
911 module.__loader__ = self # Loader 1505 module.__loader__ = self # Loader
912 _init_package_attrs(self, module) # InspectLoader 1506 _init_package_attrs(self, module) # InspectLoader
913 _init_file_attrs(self, module) # ExecutionLoader 1507 _init_file_attrs(self, module) # ExecutionLoader
914 if hasattr(module, '__file__'): # SourceLoader 1508 if hasattr(module, '__file__'): # SourceLoader
915 try: 1509 try:
916 module.__cached__ = cache_from_source(module.__file__) 1510 module.__cached__ = cache_from_source(module.__file__)
917 except NotImplementedError: 1511 except NotImplementedError:
918 pass 1512 pass
919 1513
1514 # XXX Change to exec_module().
920 def load_module(self, fullname): 1515 def load_module(self, fullname):
921 """Load the specified module into sys.modules and return it.""" 1516 """Load the specified module into sys.modules and return it."""
922 with module_to_load(fullname) as module: 1517 with module_to_load(fullname) as module:
923 self.init_module_attrs(module) 1518 self.init_module_attrs(module)
924 code = self.get_code(fullname) 1519 code = self.get_code(fullname)
925 if code is None: 1520 if code is None:
926 raise ImportError('cannot load module {!r} when get_code() ' 1521 raise ImportError('cannot load module {!r} when get_code() '
927 'returns None'.format(fullname)) 1522 'returns None'.format(fullname))
928 _call_with_frames_removed(exec, code, module.__dict__) 1523 _call_with_frames_removed(exec, code, module.__dict__)
929 return module 1524 return module
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 1638
1044 """Base file loader class which implements the loader protocol methods that 1639 """Base file loader class which implements the loader protocol methods that
1045 require file system usage.""" 1640 require file system usage."""
1046 1641
1047 def __init__(self, fullname, path): 1642 def __init__(self, fullname, path):
1048 """Cache the module name and the path to the file found by the 1643 """Cache the module name and the path to the file found by the
1049 finder.""" 1644 finder."""
1050 self.name = fullname 1645 self.name = fullname
1051 self.path = path 1646 self.path = path
1052 1647
1648 # XXX Change to exec_module().
1053 @_check_name 1649 @_check_name
1054 def load_module(self, fullname): 1650 def load_module(self, fullname):
1055 """Load a module from a file.""" 1651 """Load a module from a file."""
1056 # Issue #14857: Avoid the zero-argument form so the implementation 1652 # Issue #14857: Avoid the zero-argument form so the implementation
1057 # of that form can be updated without breaking the frozen module 1653 # of that form can be updated without breaking the frozen module
1058 return super(FileLoader, self).load_module(fullname) 1654 return super(FileLoader, self).load_module(fullname)
1059 1655
1060 @_check_name 1656 @_check_name
1061 def get_filename(self, fullname): 1657 def get_filename(self, fullname):
1062 """Return the path to the source file as found by the finder.""" 1658 """Return the path to the source file as found by the finder."""
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 _verbose_message('created {!r}', path) 1704 _verbose_message('created {!r}', path)
1109 except OSError as exc: 1705 except OSError as exc:
1110 # Same as above: just don't write the bytecode. 1706 # Same as above: just don't write the bytecode.
1111 _verbose_message('could not create {!r}: {!r}', path, exc) 1707 _verbose_message('could not create {!r}: {!r}', path, exc)
1112 1708
1113 1709
1114 class SourcelessFileLoader(FileLoader, _LoaderBasics): 1710 class SourcelessFileLoader(FileLoader, _LoaderBasics):
1115 1711
1116 """Loader which handles sourceless file imports.""" 1712 """Loader which handles sourceless file imports."""
1117 1713
1714 # XXX Merge into finder.
1118 def init_module_attrs(self, module): 1715 def init_module_attrs(self, module):
1119 super().init_module_attrs(module) 1716 super().init_module_attrs(module)
1120 module.__cached__ = module.__file__ 1717 module.__cached__ = module.__file__
1121 1718
1122 def get_code(self, fullname): 1719 def get_code(self, fullname):
1123 path = self.get_filename(fullname) 1720 path = self.get_filename(fullname)
1124 data = self.get_data(path) 1721 data = self.get_data(path)
1125 bytes_data = _validate_bytecode_header(data, name=fullname, path=path) 1722 bytes_data = _validate_bytecode_header(data, name=fullname, path=path)
1126 return _compile_bytecode(bytes_data, name=fullname, bytecode_path=path) 1723 return _compile_bytecode(bytes_data, name=fullname, bytecode_path=path)
1127 1724
(...skipping 11 matching lines...) Expand all
1139 """Loader for extension modules. 1736 """Loader for extension modules.
1140 1737
1141 The constructor is designed to work with FileFinder. 1738 The constructor is designed to work with FileFinder.
1142 1739
1143 """ 1740 """
1144 1741
1145 def __init__(self, name, path): 1742 def __init__(self, name, path):
1146 self.name = name 1743 self.name = name
1147 self.path = path 1744 self.path = path
1148 1745
1746 # XXX Add shim for load_module().
1747
1748 # XXX Change to exec_module().
1149 @_check_name 1749 @_check_name
1150 @set_package 1750 @set_package
1151 @set_loader 1751 @set_loader
1152 def load_module(self, fullname): 1752 def load_module(self, fullname):
1153 """Load an extension module.""" 1753 """Load an extension module."""
1154 with _ManageReload(fullname): 1754 with _ManageReload(fullname):
1155 module = _call_with_frames_removed(_imp.load_dynamic, 1755 module = _call_with_frames_removed(_imp.load_dynamic,
1156 fullname, self.path) 1756 fullname, self.path)
1157 _verbose_message('extension module loaded from {!r}', self.path) 1757 _verbose_message('extension module loaded from {!r}', self.path)
1158 if self.is_package(fullname) and not hasattr(module, '__path__'): 1758 if self.is_package(fullname) and not hasattr(module, '__path__'):
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1227 def __repr__(self): 1827 def __repr__(self):
1228 return '_NamespacePath({!r})'.format(self._path) 1828 return '_NamespacePath({!r})'.format(self._path)
1229 1829
1230 def __contains__(self, item): 1830 def __contains__(self, item):
1231 return item in self._recalculate() 1831 return item in self._recalculate()
1232 1832
1233 def append(self, item): 1833 def append(self, item):
1234 self._path.append(item) 1834 self._path.append(item)
1235 1835
1236 1836
1837 # XXX Merge into ModuleSpec.
1237 class NamespaceLoader: 1838 class NamespaceLoader:
1839
1238 def __init__(self, name, path, path_finder): 1840 def __init__(self, name, path, path_finder):
1239 self._path = _NamespacePath(name, path, path_finder) 1841 self._path = _NamespacePath(name, path, path_finder)
1240 1842
1843 # XXX Remove.
1241 @classmethod 1844 @classmethod
1242 def module_repr(cls, module): 1845 def module_repr(cls, module):
1243 return '<module {!r} (namespace)>'.format(module.__name__) 1846 return '<module {!r} (namespace)>'.format(module.__name__)
1244 1847
1848 # XXX Add shim for load_module().
1849
1245 def is_package(self, fullname): 1850 def is_package(self, fullname):
1246 return True 1851 return True
1247 1852
1248 def get_source(self, fullname): 1853 def get_source(self, fullname):
1249 return '' 1854 return ''
1250 1855
1251 def get_code(self, fullname): 1856 def get_code(self, fullname):
1252 return compile('', '<string>', 'exec', dont_inherit=True) 1857 return compile('', '<string>', 'exec', dont_inherit=True)
1253 1858
1859 # XXX Remove.
1254 def init_module_attrs(self, module): 1860 def init_module_attrs(self, module):
1255 module.__loader__ = self 1861 module.__loader__ = self
1256 module.__package__ = module.__name__ 1862 module.__package__ = module.__name__
1257 1863
1864 # XXX Change to exec_module().
1258 def load_module(self, fullname): 1865 def load_module(self, fullname):
1259 """Load a namespace module.""" 1866 """Load a namespace module."""
1260 _verbose_message('namespace module loaded with path {!r}', self._path) 1867 _verbose_message('namespace module loaded with path {!r}', self._path)
1261 with module_to_load(fullname) as module: 1868 with module_to_load(fullname) as module:
1262 self.init_module_attrs(module) 1869 self.init_module_attrs(module)
1263 module.__path__ = self._path 1870 module.__path__ = self._path
1264 return module 1871 return module
1265 1872
1266 1873
1267 # Finders ##################################################################### 1874 # Finders #####################################################################
1268 1875
1269 class PathFinder: 1876 class PathFinder:
1270 1877
1271 """Meta path finder for sys.path and package __path__ attributes.""" 1878 """Meta path finder for sys.path and package __path__ attributes."""
1879
1880 # XXX Add shim for find_module().
1272 1881
1273 @classmethod 1882 @classmethod
1274 def invalidate_caches(cls): 1883 def invalidate_caches(cls):
1275 """Call the invalidate_caches() method on all path entry finders 1884 """Call the invalidate_caches() method on all path entry finders
1276 stored in sys.path_importer_caches (where implemented).""" 1885 stored in sys.path_importer_caches (where implemented)."""
1277 for finder in sys.path_importer_cache.values(): 1886 for finder in sys.path_importer_cache.values():
1278 if hasattr(finder, 'invalidate_caches'): 1887 if hasattr(finder, 'invalidate_caches'):
1279 finder.invalidate_caches() 1888 finder.invalidate_caches()
1280 1889
1281 @classmethod 1890 @classmethod
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 # This is possibly part of a namespace package. 1943 # This is possibly part of a namespace package.
1335 # Remember these path entries (if any) for when we 1944 # Remember these path entries (if any) for when we
1336 # create a namespace package, and continue iterating 1945 # create a namespace package, and continue iterating
1337 # on path. 1946 # on path.
1338 namespace_path.extend(portions) 1947 namespace_path.extend(portions)
1339 else: 1948 else:
1340 return None, namespace_path 1949 return None, namespace_path
1341 1950
1342 @classmethod 1951 @classmethod
1343 def find_module(cls, fullname, path=None): 1952 def find_module(cls, fullname, path=None):
1344 """Find the module on sys.path or 'path' based on sys.path_hooks and 1953 """find the module on sys.path or 'path' based on sys.path_hooks and
1345 sys.path_importer_cache.""" 1954 sys.path_importer_cache."""
1346 if path is None: 1955 if path is None:
1347 path = sys.path 1956 path = sys.path
1348 loader, namespace_path = cls._get_loader(fullname, path) 1957 loader, namespace_path = cls._get_loader(fullname, path)
1349 if loader is not None: 1958 if loader is None:
1350 return loader
1351 else:
1352 if namespace_path: 1959 if namespace_path:
1353 # We found at least one namespace path. Return a 1960 # We found at least one namespace path. Return a
1354 # loader which can create the namespace package. 1961 # loader which can create the namespace package.
1355 return NamespaceLoader(fullname, namespace_path, cls._get_loader ) 1962 return NamespaceLoader(fullname, namespace_path, cls._get_loader )
1356 else: 1963 else:
1357 return None 1964 return None
1965 else:
1966 return loader
1967
1968 # @classmethod
1969 # def find_spec(cls, fullname, path=None):
1970 # """find the module on sys.path or 'path' based on sys.path_hooks and
1971 # sys.path_importer_cache."""
1972 # if path is None:
1973 # path = sys.path
1974 # loader, namespace_path = cls._get_loader(fullname, path)
1975 # if loader is None:
1976 # if namespace_path:
1977 # # We found at least one namespace path. Return a
1978 # # loader which can create the namespace package.
1979 # loader = NamespaceLoader(fullname, namespace_path,
1980 # cls._get_loader)
1981 # return spec_from_loader(fullname, loader, origin='namespace',
1982 # is_package=True)
1983 # else:
1984 # return None
1985 # else:
1986 # # Defer to loader.get_filename() for the location.
1987 # return spec_from_file_location(fullname, loader=loader)
1988 #
1989 # # XXX Deprecate.
1990 # @classmethod
1991 # def find_module(cls, fullname, path=None):
1992 # """find the module on sys.path or 'path' based on sys.path_hooks and
Jim.J.Jewett 2013/10/30 03:32:01 Is this an already existing method despite the "ne
eric.snow 2013/11/01 01:07:38 This was the next step in the implementation that
1993 # sys.path_importer_cache."""
1994 # spec = cls.find_spec(fullname, path)
1995 # if spec is None:
1996 # return None
1997 # return spec.loader
1358 1998
1359 1999
1360 class FileFinder: 2000 class FileFinder:
1361 2001
1362 """File-based finder. 2002 """File-based finder.
1363 2003
1364 Interactions with the file system are cached for performance, being 2004 Interactions with the file system are cached for performance, being
1365 refreshed when the directory the finder is handling has been modified. 2005 refreshed when the directory the finder is handling has been modified.
1366 2006
1367 """ 2007 """
1368 2008
1369 def __init__(self, path, *loader_details): 2009 def __init__(self, path, *loader_details):
1370 """Initialize with the path to search on and a variable number of 2010 """Initialize with the path to search on and a variable number of
1371 2-tuples containing the loader and the file suffixes the loader 2011 2-tuples containing the loader and the file suffixes the loader
1372 recognizes.""" 2012 recognizes."""
1373 loaders = [] 2013 loaders = []
1374 for loader, suffixes in loader_details: 2014 for loader, suffixes in loader_details:
1375 loaders.extend((suffix, loader) for suffix in suffixes) 2015 loaders.extend((suffix, loader) for suffix in suffixes)
1376 self._loaders = loaders 2016 self._loaders = loaders
1377 # Base (directory) path 2017 # Base (directory) path
1378 self.path = path 2018 self.path = path
1379 self._path_mtime = -1 2019 self._path_mtime = -1
1380 self._path_cache = set() 2020 self._path_cache = set()
1381 self._relaxed_path_cache = set() 2021 self._relaxed_path_cache = set()
1382 2022
1383 def invalidate_caches(self): 2023 def invalidate_caches(self):
1384 """Invalidate the directory mtime.""" 2024 """Invalidate the directory mtime."""
1385 self._path_mtime = -1 2025 self._path_mtime = -1
1386 2026
2027 # XXX Use a different shim.
1387 find_module = _find_module_shim 2028 find_module = _find_module_shim
1388 2029
2030 # XXX Change to find_spec().
1389 def find_loader(self, fullname): 2031 def find_loader(self, fullname):
1390 """Try to find a loader for the specified module, or the namespace 2032 """Try to find a loader for the specified module, or the namespace
1391 package portions. Returns (loader, list-of-portions).""" 2033 package portions. Returns (loader, list-of-portions)."""
1392 is_namespace = False 2034 is_namespace = False
1393 tail_module = fullname.rpartition('.')[2] 2035 tail_module = fullname.rpartition('.')[2]
1394 try: 2036 try:
1395 mtime = _os.stat(self.path or _os.getcwd()).st_mtime 2037 mtime = _os.stat(self.path or _os.getcwd()).st_mtime
1396 except OSError: 2038 except OSError:
1397 mtime = -1 2039 mtime = -1
1398 if mtime != self._path_mtime: 2040 if mtime != self._path_mtime:
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1453 name, dot, suffix = item.partition('.') 2095 name, dot, suffix = item.partition('.')
1454 if dot: 2096 if dot:
1455 new_name = '{}.{}'.format(name, suffix.lower()) 2097 new_name = '{}.{}'.format(name, suffix.lower())
1456 else: 2098 else:
1457 new_name = name 2099 new_name = name
1458 lower_suffix_contents.add(new_name) 2100 lower_suffix_contents.add(new_name)
1459 self._path_cache = lower_suffix_contents 2101 self._path_cache = lower_suffix_contents
1460 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS): 2102 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
1461 self._relaxed_path_cache = {fn.lower() for fn in contents} 2103 self._relaxed_path_cache = {fn.lower() for fn in contents}
1462 2104
2105 # XXX See _get_supported_file_loaders().
1463 @classmethod 2106 @classmethod
1464 def path_hook(cls, *loader_details): 2107 def path_hook(cls, *loader_details):
1465 """A class method which returns a closure to use on sys.path_hook 2108 """A class method which returns a closure to use on sys.path_hook
1466 which will return an instance using the specified loaders and the path 2109 which will return an instance using the specified loaders and the path
1467 called on the closure. 2110 called on the closure.
1468 2111
1469 If the path called on the closure is not a directory, ImportError is 2112 If the path called on the closure is not a directory, ImportError is
1470 raised. 2113 raised.
1471 2114
1472 """ 2115 """
(...skipping 26 matching lines...) Expand all
1499 2142
1500 def _resolve_name(name, package, level): 2143 def _resolve_name(name, package, level):
1501 """Resolve a relative module name to an absolute one.""" 2144 """Resolve a relative module name to an absolute one."""
1502 bits = package.rsplit('.', level - 1) 2145 bits = package.rsplit('.', level - 1)
1503 if len(bits) < level: 2146 if len(bits) < level:
1504 raise ValueError('attempted relative import beyond top-level package') 2147 raise ValueError('attempted relative import beyond top-level package')
1505 base = bits[0] 2148 base = bits[0]
1506 return '{}.{}'.format(base, name) if name else base 2149 return '{}.{}'.format(base, name) if name else base
1507 2150
1508 2151
1509 def _find_module(name, path): 2152 def _find_spec(name, path):
1510 """Find a module's loader.""" 2153 """Find a module's loader."""
1511 if not sys.meta_path: 2154 if not sys.meta_path:
1512 _warnings.warn('sys.meta_path is empty', ImportWarning) 2155 _warnings.warn('sys.meta_path is empty', ImportWarning)
1513 for finder in sys.meta_path: 2156 for finder in sys.meta_path:
1514 with _ImportLockContext(): 2157 with _ImportLockContext():
1515 loader = finder.find_module(name, path) 2158 try:
1516 if loader is not None: 2159 find_spec = finder.find_spec
2160 except AttributeError:
2161 loader = finder.find_module(name, path)
2162 if loader is None:
2163 continue
2164 spec = spec_from_loader(name, loader)
2165 else:
2166 spec = find_spec(name, path)
2167 if spec is not None:
1517 # The parent import may have already imported this module. 2168 # The parent import may have already imported this module.
1518 if name not in sys.modules: 2169 if name in sys.modules:
1519 return loader 2170 module = sys.modules[name]
2171 try:
2172 return module.__spec__
2173 except AttributeError:
2174 # XXX Use _spec_from_module() here?
2175 return spec
1520 else: 2176 else:
1521 return sys.modules[name].__loader__ 2177 return spec
1522 else: 2178 else:
1523 return None 2179 return None
1524 2180
1525 2181
1526 def _sanity_check(name, package, level): 2182 def _sanity_check(name, package, level):
1527 """Verify arguments are "sane".""" 2183 """Verify arguments are "sane"."""
1528 if not isinstance(name, str): 2184 if not isinstance(name, str):
1529 raise TypeError('module name must be str, not {}'.format(type(name))) 2185 raise TypeError('module name must be str, not {}'.format(type(name)))
1530 if level < 0: 2186 if level < 0:
1531 raise ValueError('level must be >= 0') 2187 raise ValueError('level must be >= 0')
(...skipping 20 matching lines...) Expand all
1552 # Crazy side-effects! 2208 # Crazy side-effects!
1553 if name in sys.modules: 2209 if name in sys.modules:
1554 return sys.modules[name] 2210 return sys.modules[name]
1555 # Backwards-compatibility; be nicer to skip the dict lookup. 2211 # Backwards-compatibility; be nicer to skip the dict lookup.
1556 parent_module = sys.modules[parent] 2212 parent_module = sys.modules[parent]
1557 try: 2213 try:
1558 path = parent_module.__path__ 2214 path = parent_module.__path__
1559 except AttributeError: 2215 except AttributeError:
1560 msg = (_ERR_MSG + '; {} is not a package').format(name, parent) 2216 msg = (_ERR_MSG + '; {} is not a package').format(name, parent)
1561 raise ImportError(msg, name=name) 2217 raise ImportError(msg, name=name)
1562 loader = _find_module(name, path) 2218 spec = _find_spec(name, path)
1563 if loader is None: 2219 if spec is None:
1564 raise ImportError(_ERR_MSG.format(name), name=name) 2220 raise ImportError(_ERR_MSG.format(name), name=name)
1565 elif name not in sys.modules: 2221 else:
1566 # The parent import may have already imported this module. 2222 module = _SpecMethods(spec)._load_unlocked()
1567 loader.load_module(name)
1568 _verbose_message('import {!r} # {!r}', name, loader)
1569 # Backwards-compatibility; be nicer to skip the dict lookup.
1570 module = sys.modules[name]
1571 if parent: 2223 if parent:
1572 # Set the module as an attribute on its parent. 2224 # Set the module as an attribute on its parent.
1573 parent_module = sys.modules[parent] 2225 parent_module = sys.modules[parent]
1574 setattr(parent_module, name.rpartition('.')[2], module) 2226 setattr(parent_module, name.rpartition('.')[2], module)
1575 # Set __package__ if the loader did not.
1576 if getattr(module, '__package__', None) is None:
1577 try:
1578 module.__package__ = module.__name__
1579 if not hasattr(module, '__path__'):
1580 module.__package__ = module.__package__.rpartition('.')[0]
1581 except AttributeError:
1582 pass
1583 # Set loader if need be.
1584 if getattr(module, '__loader__', None) is None:
1585 try:
1586 module.__loader__ = loader
1587 except AttributeError:
1588 pass
1589 return module 2227 return module
1590 2228
1591 2229
2230 # XXX Eliminate?
1592 def _find_and_load(name, import_): 2231 def _find_and_load(name, import_):
1593 """Find and load the module, and release the import lock.""" 2232 """Find and load the module, and release the import lock."""
1594 try: 2233 with _ModuleLockManager(name):
1595 lock = _get_module_lock(name)
1596 finally:
1597 _imp.release_lock()
1598 lock.acquire()
1599 try:
1600 return _find_and_load_unlocked(name, import_) 2234 return _find_and_load_unlocked(name, import_)
1601 finally:
1602 lock.release()
1603 2235
1604 2236
1605 def _gcd_import(name, package=None, level=0): 2237 def _gcd_import(name, package=None, level=0):
1606 """Import and return the module based on its name, the package the call is 2238 """Import and return the module based on its name, the package the call is
1607 being made from, and the level adjustment. 2239 being made from, and the level adjustment.
1608 2240
1609 This function represents the greatest common denominator of functionality 2241 This function represents the greatest common denominator of functionality
1610 between import_module and __import__. This includes setting __package__ if 2242 between import_module and __import__. This includes setting __package__ if
1611 the loader did not. 2243 the loader did not.
1612 2244
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1711 else: 2343 else:
1712 # Figure out where to slice the module's name up to the first dot 2344 # Figure out where to slice the module's name up to the first dot
1713 # in 'name'. 2345 # in 'name'.
1714 cut_off = len(name) - len(name.partition('.')[0]) 2346 cut_off = len(name) - len(name.partition('.')[0])
1715 # Slice end needs to be positive to alleviate need to special-case 2347 # Slice end needs to be positive to alleviate need to special-case
1716 # when ``'.' not in name``. 2348 # when ``'.' not in name``.
1717 return sys.modules[module.__name__[:len(module.__name__)-cut_off]] 2349 return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
1718 else: 2350 else:
1719 return _handle_fromlist(module, fromlist, _gcd_import) 2351 return _handle_fromlist(module, fromlist, _gcd_import)
1720 2352
2353
2354 def _builtin_from_name(name):
2355 spec = BuiltinImporter._get_spec(name)
2356 methods = _SpecMethods(spec)
2357 return methods._load_unlocked()
1721 2358
1722 2359
1723 def _setup(sys_module, _imp_module): 2360 def _setup(sys_module, _imp_module):
1724 """Setup importlib by importing needed built-in modules and injecting them 2361 """Setup importlib by importing needed built-in modules and injecting them
1725 into the global namespace. 2362 into the global namespace.
1726 2363
1727 As sys is needed for sys.modules access and _imp is needed to load built-in 2364 As sys is needed for sys.modules access and _imp is needed to load built-in
1728 modules, those two modules must be explicitly passed in. 2365 modules, those two modules must be explicitly passed in.
1729 2366
1730 """ 2367 """
1731 global _imp, sys, BYTECODE_SUFFIXES 2368 global _imp, sys, BYTECODE_SUFFIXES
1732 _imp = _imp_module 2369 _imp = _imp_module
1733 sys = sys_module 2370 sys = sys_module
1734 2371
1735 if sys.flags.optimize: 2372 if sys.flags.optimize:
1736 BYTECODE_SUFFIXES = OPTIMIZED_BYTECODE_SUFFIXES 2373 BYTECODE_SUFFIXES = OPTIMIZED_BYTECODE_SUFFIXES
1737 else: 2374 else:
1738 BYTECODE_SUFFIXES = DEBUG_BYTECODE_SUFFIXES 2375 BYTECODE_SUFFIXES = DEBUG_BYTECODE_SUFFIXES
1739 2376
2377 # Set up the spec for existing builtin/frozen modules.
1740 module_type = type(sys) 2378 module_type = type(sys)
1741 for name, module in sys.modules.items(): 2379 for name, module in sys.modules.items():
1742 if isinstance(module, module_type): 2380 if isinstance(module, module_type):
1743 if getattr(module, '__loader__', None) is None: 2381 if name in sys.builtin_module_names:
1744 if name in sys.builtin_module_names: 2382 loader = BuiltinImporter
1745 module.__loader__ = BuiltinImporter 2383 elif _imp.is_frozen(name):
1746 elif _imp.is_frozen(name): 2384 loader = FrozenImporter
1747 module.__loader__ = FrozenImporter 2385 else:
2386 continue
2387 spec = _spec_from_module(module, loader)
2388 methods = _SpecMethods(spec)
2389 methods.init_module_attrs(module)
1748 2390
2391 # Directly load built-in modules needed during bootstrap.
1749 self_module = sys.modules[__name__] 2392 self_module = sys.modules[__name__]
1750 for builtin_name in ('_io', '_warnings', 'builtins', 'marshal'): 2393 for builtin_name in ('_io', '_warnings', 'builtins', 'marshal'):
1751 if builtin_name not in sys.modules: 2394 if builtin_name not in sys.modules:
1752 builtin_module = BuiltinImporter.load_module(builtin_name) 2395 builtin_module = _builtin_from_name(builtin_name)
1753 else: 2396 else:
1754 builtin_module = sys.modules[builtin_name] 2397 builtin_module = sys.modules[builtin_name]
1755 setattr(self_module, builtin_name, builtin_module) 2398 setattr(self_module, builtin_name, builtin_module)
1756 2399
2400 # Directly load the os module (needed during bootstrap).
1757 os_details = ('posix', ['/']), ('nt', ['\\', '/']) 2401 os_details = ('posix', ['/']), ('nt', ['\\', '/'])
1758 for builtin_os, path_separators in os_details: 2402 for builtin_os, path_separators in os_details:
1759 # Assumption made in _path_join() 2403 # Assumption made in _path_join()
1760 assert all(len(sep) == 1 for sep in path_separators) 2404 assert all(len(sep) == 1 for sep in path_separators)
1761 path_sep = path_separators[0] 2405 path_sep = path_separators[0]
1762 if builtin_os in sys.modules: 2406 if builtin_os in sys.modules:
1763 os_module = sys.modules[builtin_os] 2407 os_module = sys.modules[builtin_os]
1764 break 2408 break
1765 else: 2409 else:
1766 try: 2410 try:
1767 os_module = BuiltinImporter.load_module(builtin_os) 2411 os_module = _builtin_from_name(builtin_os)
1768 break 2412 break
1769 except ImportError: 2413 except ImportError:
1770 continue 2414 continue
1771 else: 2415 else:
1772 raise ImportError('importlib requires posix or nt') 2416 raise ImportError('importlib requires posix or nt')
2417 setattr(self_module, '_os', os_module)
2418 setattr(self_module, 'path_sep', path_sep)
2419 setattr(self_module, 'path_separators', ''.join(path_separators))
1773 2420
2421 # Directly load the _thread module (needed during bootstrap).
1774 try: 2422 try:
1775 thread_module = BuiltinImporter.load_module('_thread') 2423 thread_module = _builtin_from_name('_thread')
1776 except ImportError: 2424 except ImportError:
1777 # Python was built without threads 2425 # Python was built without threads
1778 thread_module = None 2426 thread_module = None
1779 weakref_module = BuiltinImporter.load_module('_weakref') 2427 setattr(self_module, '_thread', thread_module)
1780 2428
2429 # Directly load the _weakref module (needed during bootstrap).
2430 weakref_module = _builtin_from_name('_weakref')
2431 setattr(self_module, '_weakref', weakref_module)
2432
2433 # Directly load the winreg module (needed during bootstrap).
1781 if builtin_os == 'nt': 2434 if builtin_os == 'nt':
1782 winreg_module = BuiltinImporter.load_module('winreg') 2435 winreg_module = _builtin_from_name('winreg')
1783 setattr(self_module, '_winreg', winreg_module) 2436 setattr(self_module, '_winreg', winreg_module)
1784 2437
1785 setattr(self_module, '_os', os_module)
1786 setattr(self_module, '_thread', thread_module)
1787 setattr(self_module, '_weakref', weakref_module)
1788 setattr(self_module, 'path_sep', path_sep)
1789 setattr(self_module, 'path_separators', ''.join(path_separators))
1790 # Constants 2438 # Constants
1791 setattr(self_module, '_relax_case', _make_relax_case()) 2439 setattr(self_module, '_relax_case', _make_relax_case())
1792 EXTENSION_SUFFIXES.extend(_imp.extension_suffixes()) 2440 EXTENSION_SUFFIXES.extend(_imp.extension_suffixes())
1793 if builtin_os == 'nt': 2441 if builtin_os == 'nt':
1794 SOURCE_SUFFIXES.append('.pyw') 2442 SOURCE_SUFFIXES.append('.pyw')
1795 if '_d.pyd' in EXTENSION_SUFFIXES: 2443 if '_d.pyd' in EXTENSION_SUFFIXES:
1796 WindowsRegistryFinder.DEBUG_BUILD = True 2444 WindowsRegistryFinder.DEBUG_BUILD = True
1797 2445
1798 2446
1799 def _install(sys_module, _imp_module): 2447 def _install(sys_module, _imp_module):
1800 """Install importlib as the implementation of import.""" 2448 """Install importlib as the implementation of import."""
1801 _setup(sys_module, _imp_module) 2449 _setup(sys_module, _imp_module)
1802 supported_loaders = _get_supported_file_loaders() 2450 supported_loaders = _get_supported_file_loaders()
1803 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)]) 2451 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
1804 sys.meta_path.append(BuiltinImporter) 2452 sys.meta_path.append(BuiltinImporter)
1805 sys.meta_path.append(FrozenImporter) 2453 sys.meta_path.append(FrozenImporter)
1806 if _os.__name__ == 'nt': 2454 if _os.__name__ == 'nt':
1807 sys.meta_path.append(WindowsRegistryFinder) 2455 sys.meta_path.append(WindowsRegistryFinder)
1808 sys.meta_path.append(PathFinder) 2456 sys.meta_path.append(PathFinder)
OLDNEW

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