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

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

Issue 18864: Implementation for PEP 451 (importlib.machinery.ModuleSpec)
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 8 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 77
78 def _path_is_mode_type(path, mode): 78 def _path_is_mode_type(path, mode):
79 """Test whether the path is the specified mode type.""" 79 """Test whether the path is the specified mode type."""
80 try: 80 try:
81 stat_info = _path_stat(path) 81 stat_info = _path_stat(path)
82 except OSError: 82 except OSError:
83 return False 83 return False
84 return (stat_info.st_mode & 0o170000) == mode 84 return (stat_info.st_mode & 0o170000) == mode
85 85
86 86
87 # XXX Could also expose Modules/getpath.c:isfile()
88 def _path_isfile(path): 87 def _path_isfile(path):
89 """Replacement for os.path.isfile.""" 88 """Replacement for os.path.isfile."""
90 return _path_is_mode_type(path, 0o100000) 89 return _path_is_mode_type(path, 0o100000)
91 90
92 91
93 # XXX Could also expose Modules/getpath.c:isdir()
94 def _path_isdir(path): 92 def _path_isdir(path):
95 """Replacement for os.path.isdir.""" 93 """Replacement for os.path.isdir."""
96 if not path: 94 if not path:
97 path = _os.getcwd() 95 path = _os.getcwd()
98 return _path_is_mode_type(path, 0o040000) 96 return _path_is_mode_type(path, 0o040000)
99 97
100 98
101 def _write_atomic(path, data, mode=0o666): 99 def _write_atomic(path, data, mode=0o666):
102 """Best-effort function to write data to a path atomically. 100 """Best-effort function to write data to a path atomically.
103 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
121 119
122 120
123 def _wrap(new, old): 121 def _wrap(new, old):
124 """Simple substitute for functools.update_wrapper.""" 122 """Simple substitute for functools.update_wrapper."""
125 for replace in ['__module__', '__name__', '__qualname__', '__doc__']: 123 for replace in ['__module__', '__name__', '__qualname__', '__doc__']:
126 if hasattr(old, replace): 124 if hasattr(old, replace):
127 setattr(new, replace, getattr(old, replace)) 125 setattr(new, replace, getattr(old, replace))
128 new.__dict__.update(old.__dict__) 126 new.__dict__.update(old.__dict__)
129 127
130 128
129 def _new_module(name):
130 return type(sys)(name)
131
132
131 _code_type = type(_wrap.__code__) 133 _code_type = type(_wrap.__code__)
132 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
133 153
134 # Module-level locking ######################################################## 154 # Module-level locking ########################################################
135 155
136 # A dict mapping module names to weakrefs of _ModuleLock instances 156 # A dict mapping module names to weakrefs of _ModuleLock instances
137 _module_locks = {} 157 _module_locks = {}
138 # A dict mapping thread ids to _ModuleLock instances 158 # A dict mapping thread ids to _ModuleLock instances
139 _blocking_on = {} 159 _blocking_on = {}
140 160
141 161
142 class _DeadlockError(RuntimeError): 162 class _DeadlockError(RuntimeError):
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 515
496 516
497 def _verbose_message(message, *args, verbosity=1): 517 def _verbose_message(message, *args, verbosity=1):
498 """Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" 518 """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
499 if sys.flags.verbose >= verbosity: 519 if sys.flags.verbose >= verbosity:
500 if not message.startswith(('#', 'import ')): 520 if not message.startswith(('#', 'import ')):
501 message = '# ' + message 521 message = '# ' + message
502 print(message.format(*args), file=sys.stderr) 522 print(message.format(*args), file=sys.stderr)
503 523
504 524
505 # XXX Remove.
506 class _ManageReload:
507
508 def __init__(self, name):
509 self._name = name
510
511 def __enter__(self):
512 self._is_reload = self._name in sys.modules
513
514 def __exit__(self, *args):
515 if any(arg is not None for arg in args) and not self._is_reload:
516 try:
517 del sys.modules[self._name]
518 except KeyError:
519 pass
520
521
522 # XXX Remove.
523 # Written as a class only because contextlib is not available.
524 class _ModuleManager(_ManageReload):
525
526 """Context manager which returns the module to be loaded.
527
528 Does the proper unloading from sys.modules upon failure.
529
530 """
531
532 def __init__(self, name, *, reset_name=True):
533 """Prepare the context manager.
534
535 The reset_name argument specifies whether to unconditionally reset
536 the __name__ attribute if the module is found to be a reload.
537 """
538 super().__init__(name)
539 self._reset_name = reset_name
540
541 def __enter__(self):
542 super().__enter__()
543 self._module = sys.modules.get(self._name)
544 if not self._is_reload:
545 # This must be done before open() is called as the 'io' module
546 # implicitly imports 'locale' and would otherwise trigger an
547 # infinite loop.
548 self._module = type(_io)(self._name)
549 # This must be done before putting the module in sys.modules
550 # (otherwise an optimization shortcut in import.c becomes wrong)
551 self._module.__initializing__ = True
552 sys.modules[self._name] = self._module
553 elif self._reset_name:
554 try:
555 self._module.__name__ = self._name
556 except AttributeError:
557 pass
558 return self._module
559
560 def __exit__(self, *args):
561 self._module.__initializing__ = False
562 del self._module
563 super().__exit__(*args)
564
565
566 # XXX Remove.
567 def module_to_load(name, *, reset_name=True):
568 """Return a context manager which provides the module object to load.
569
570 If reset_name is true, reset the module's __name__ to 'name'.
571 """
572 # Hiding _ModuleManager behind a function for better naming.
573 return _ModuleManager(name, reset_name=reset_name)
574
575
576 # XXX Remove.
577 def _init_package_attrs(loader, module):
578 """Set __package__ and __path__ based on what loader.is_package() says."""
579 name = module.__name__
580 try:
581 is_package = loader.is_package(name)
582 except ImportError:
583 pass
584 else:
585 if is_package:
586 module.__package__ = name
587 module.__path__ = []
588 else:
589 module.__package__ = name.rpartition('.')[0]
590
591
592 # XXX Remove.
593 def _init_file_attrs(loader, module):
594 """Set __file__ and __path__ based on loader.get_filename()."""
595 try:
596 module.__file__ = loader.get_filename(module.__name__)
597 except ImportError:
598 pass
599 else:
600 if module.__name__ == module.__package__:
601 module.__path__.append(_path_split(module.__file__)[0])
602
603
604 # XXX Can be moved to importlib.util (and deprecated).
605 def set_package(fxn):
606 """Set __package__ on the returned module."""
607 def set_package_wrapper(*args, **kwargs):
608 module = fxn(*args, **kwargs)
609 if getattr(module, '__package__', None) is None:
610 module.__package__ = module.__name__
611 if not hasattr(module, '__path__'):
612 module.__package__ = module.__package__.rpartition('.')[0]
613 return module
614 _wrap(set_package_wrapper, fxn)
615 return set_package_wrapper
616
617
618 # XXX Can be moved to importlib.util (and deprecated).
619 def set_loader(fxn):
620 """Set __loader__ on the returned module."""
621 def set_loader_wrapper(self, *args, **kwargs):
622 module = fxn(self, *args, **kwargs)
623 if getattr(module, '__loader__', None) is None:
624 module.__loader__ = self
625 return module
626 _wrap(set_loader_wrapper, fxn)
627 return set_loader_wrapper
628
629
630 def _check_name(method): 525 def _check_name(method):
631 """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
632 loader can handle. 527 loader can handle.
633 528
634 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
635 compared against. If the comparison fails then ImportError is raised. 530 compared against. If the comparison fails then ImportError is raised.
636 531
637 """ 532 """
638 def _check_name_wrapper(self, name=None, *args, **kwargs): 533 def _check_name_wrapper(self, name=None, *args, **kwargs):
639 if name is None: 534 if name is None:
640 name = self.name 535 name = self.name
641 elif self.name != name: 536 elif self.name != name:
642 raise ImportError('loader cannot handle %s' % name, name=name) 537 raise ImportError('loader cannot handle %s' % name, name=name)
643 return method(self, name, *args, **kwargs) 538 return method(self, name, *args, **kwargs)
644 _wrap(_check_name_wrapper, method) 539 _wrap(_check_name_wrapper, method)
645 return _check_name_wrapper 540 return _check_name_wrapper
646 541
647 542
648 def _requires_builtin(fxn): 543 def _requires_builtin(fxn):
649 """Decorator to verify the named module is built-in.""" 544 """Decorator to verify the named module is built-in."""
650 def _requires_builtin_wrapper(self, fullname): 545 def _requires_builtin_wrapper(self, fullname):
651 if fullname not in sys.builtin_module_names: 546 if fullname not in sys.builtin_module_names:
652 raise ImportError('{} is not a built-in module'.format(fullname), 547 raise ImportError('{!r} is not a built-in module'.format(fullname),
653 name=fullname) 548 name=fullname)
654 return fxn(self, fullname) 549 return fxn(self, fullname)
655 _wrap(_requires_builtin_wrapper, fxn) 550 _wrap(_requires_builtin_wrapper, fxn)
656 return _requires_builtin_wrapper 551 return _requires_builtin_wrapper
657 552
658 553
659 def _requires_frozen(fxn): 554 def _requires_frozen(fxn):
660 """Decorator to verify the named module is frozen.""" 555 """Decorator to verify the named module is frozen."""
661 def _requires_frozen_wrapper(self, fullname): 556 def _requires_frozen_wrapper(self, fullname):
662 if not _imp.is_frozen(fullname): 557 if not _imp.is_frozen(fullname):
663 raise ImportError('{} is not a frozen module'.format(fullname), 558 raise ImportError('{!r} is not a frozen module'.format(fullname),
664 name=fullname) 559 name=fullname)
665 return fxn(self, fullname) 560 return fxn(self, fullname)
666 _wrap(_requires_frozen_wrapper, fxn) 561 _wrap(_requires_frozen_wrapper, fxn)
667 return _requires_frozen_wrapper 562 return _requires_frozen_wrapper
668 563
669 564
670 def _find_module_shim(self, fullname): 565 def _find_module_shim(self, fullname):
671 """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
672 self.find_loader().""" 567 self.find_loader()."""
673 # Call find_loader(). If it returns a string (indicating this 568 # Call find_loader(). If it returns a string (indicating this
674 # is a namespace package portion), generate a warning and 569 # is a namespace package portion), generate a warning and
675 # return None. 570 # return None.
676 loader, portions = self.find_loader(fullname) 571 loader, portions = self.find_loader(fullname)
677 if loader is None and len(portions): 572 if loader is None and len(portions):
678 msg = 'Not importing directory {}: missing __init__' 573 msg = 'Not importing directory {}: missing __init__'
679 _warnings.warn(msg.format(portions[0]), ImportWarning) 574 _warnings.warn(msg.format(portions[0]), ImportWarning)
680 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()
681 589
682 590
683 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):
684 """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
685 given) and returning the bytecode that can be compiled by compile(). 593 given) and returning the bytecode that can be compiled by compile().
686 594
687 All other arguments are used to enhance error reporting. 595 All other arguments are used to enhance error reporting.
688 596
689 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
690 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 loader = getattr(module, '__loader__', None) 684 loader = getattr(module, '__loader__', None)
777 if hasattr(loader, 'module_repr'): 685 if hasattr(loader, 'module_repr'):
778 # XXX Deprecation Warning here... 686 # XXX Deprecation Warning here...
779 try: 687 try:
780 return loader.module_repr(module) 688 return loader.module_repr(module)
781 except Exception: 689 except Exception:
782 pass 690 pass
783 try: 691 try:
784 spec = module.__spec__ 692 spec = module.__spec__
785 except AttributeError: 693 except AttributeError:
786 # XXX Use module.__class__.__name__ instead of 'module'? 694 pass
787 try:
788 name = module.__name__
789 except AttributeError:
790 name = '?'
791 try:
792 filename = module.__file__
793 except AttributeError:
794 if loader is None:
795 return '<module {!r}>'.format(name)
796 else:
797 return '<module {!r} ({!r})>'.format(name, loader)
798 else:
799 return '<module {!r} from {!r}>'.format(name, filename)
800 else: 695 else:
801 return _SpecMethods(spec).module_repr() 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)
802 714
803 715
804 class _installed_safely: 716 class _installed_safely:
805 717
806 def __init__(self, module): 718 def __init__(self, module):
807 self._module = module 719 self._module = module
808 self._spec = module.__spec__ 720 self._spec = module.__spec__
809 721
810 def __enter__(self): 722 def __enter__(self):
811 # This must be done before putting the module in sys.modules 723 # This must be done before putting the module in sys.modules
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
879 791
880 def __repr__(self): 792 def __repr__(self):
881 args = ['name={!r}'.format(self.name), 793 args = ['name={!r}'.format(self.name),
882 'loader={!r}'.format(self.loader)] 794 'loader={!r}'.format(self.loader)]
883 if self.origin is not None: 795 if self.origin is not None:
884 args.append('origin={!r}'.format(self.origin)) 796 args.append('origin={!r}'.format(self.origin))
885 if self.submodule_search_locations is not None: 797 if self.submodule_search_locations is not None:
886 args.append('submodule_search_locations={}' 798 args.append('submodule_search_locations={}'
887 .format(self.submodule_search_locations)) 799 .format(self.submodule_search_locations))
888 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
889 813
890 @property 814 @property
891 def cached(self): 815 def cached(self):
892 if self._cached is None: 816 if self._cached is None:
893 if self.origin is not None and self._set_fileattr: 817 if self.origin is not None and self._set_fileattr:
894 filename = self.origin 818 filename = self.origin
895 if filename.endswith(tuple(SOURCE_SUFFIXES)): 819 if filename.endswith(tuple(SOURCE_SUFFIXES)):
896 try: 820 try:
897 self._cached = cache_from_source(filename) 821 self._cached = cache_from_source(filename)
898 except NotImplementedError: 822 except NotImplementedError:
(...skipping 11 matching lines...) Expand all
910 """The name of the module's parent.""" 834 """The name of the module's parent."""
911 if self.submodule_search_locations is None: 835 if self.submodule_search_locations is None:
912 return self.name.rpartition('.')[0] 836 return self.name.rpartition('.')[0]
913 else: 837 else:
914 return self.name 838 return self.name
915 839
916 @property 840 @property
917 def has_location(self): 841 def has_location(self):
918 return self._set_fileattr 842 return self._set_fileattr
919 843
844 @has_location.setter
845 def has_location(self, value):
846 self._set_fileattr = bool(value)
847
920 848
921 def spec_from_loader(name, loader, *, origin=None, is_package=None): 849 def spec_from_loader(name, loader, *, origin=None, is_package=None):
922 """Return a module spec based on various loader methods.""" 850 """Return a module spec based on various loader methods."""
923
924 # if hasattr(loader, 'get_data'):
925 if hasattr(loader, 'get_filename'): 851 if hasattr(loader, 'get_filename'):
926 if is_package is None: 852 if is_package is None:
927 return spec_from_file_location(name, loader=loader) 853 return spec_from_file_location(name, loader=loader)
928 search = [] if is_package else None 854 search = [] if is_package else None
929 return spec_from_file_location(name, loader=loader, 855 return spec_from_file_location(name, loader=loader,
930 submodule_search_locations=search) 856 submodule_search_locations=search)
931 857
932 if is_package is None: 858 if is_package is None:
933 if hasattr(loader, 'is_package'): 859 if hasattr(loader, 'is_package'):
934 try: 860 try:
(...skipping 16 matching lines...) Expand all
951 877
952 To indicate that the module is a package, set 878 To indicate that the module is a package, set
953 submodule_search_locations to a list of directory paths. An 879 submodule_search_locations to a list of directory paths. An
954 empty list is sufficient, though its not otherwise useful to the 880 empty list is sufficient, though its not otherwise useful to the
955 import system. 881 import system.
956 882
957 The loader must take a spec as its only __init__() arg. 883 The loader must take a spec as its only __init__() arg.
958 884
959 """ 885 """
960 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>'
961 if hasattr(loader, 'get_filename'): 891 if hasattr(loader, 'get_filename'):
892 # ExecutionLoader
962 try: 893 try:
963 location = loader.get_filename(name) 894 location = loader.get_filename(name)
964 except ImportError: 895 except ImportError:
965 # XXX Return None? 896 pass
966 location = '<unknown>' 897
967 else: 898 # If the location is on the filesystem, but doesn't actually exist,
968 # XXX Return None? 899 # we could return None here, indicating that the location is not
969 location = '<unknown>' 900 # valid. However, we don't have a good way of testing since an
970 # XXX This breaks for zipimport (and any indirect location). 901 # indirect location (e.g. a zip file or URL) will look like a
971 #try: 902 # non-existent file relative to the filesystem.
972 # _os.stat(location)
973 #except OSError:
974 # return None
975 903
976 spec = ModuleSpec(name, loader, origin=location) 904 spec = ModuleSpec(name, loader, origin=location)
977 spec._set_fileattr = True 905 spec._set_fileattr = True
978 906
979 # Pick a loader if one wasn't provided. 907 # Pick a loader if one wasn't provided.
980 if loader is None: 908 if loader is None:
981 for loader_class, suffixes in _get_supported_file_loaders(): 909 for loader_class, suffixes in _get_supported_file_loaders():
982 if location.endswith(tuple(suffixes)): 910 if location.endswith(tuple(suffixes)):
983 loader = loader_class(name, location) 911 loader = loader_class(name, location)
984 spec.loader = loader 912 spec.loader = loader
985 break 913 break
986 else: 914 else:
987 return None 915 return None
988 916
989 # Set submodule_search_paths appropriately. 917 # Set submodule_search_paths appropriately.
990 if submodule_search_locations is _POPULATE: 918 if submodule_search_locations is _POPULATE:
991 # Check the loader. 919 # Check the loader.
992 if hasattr(loader, 'is_package'): 920 if hasattr(loader, 'is_package'):
993 try: 921 try:
994 is_package = loader.is_package(name) 922 is_package = loader.is_package(name)
995 except ImportError: 923 except ImportError:
996 pass 924 pass
997 else: 925 else:
998 if is_package: 926 if is_package:
999 spec.submodule_search_locations = [] 927 spec.submodule_search_locations = []
1000 else: 928 else:
1001 spec.submodule_search_locations = submodule_search_locations 929 spec.submodule_search_locations = submodule_search_locations
1002 if spec.submodule_search_locations == []: 930 if spec.submodule_search_locations == []:
1003 # if location: 931 if location:
1004 dirname = _path_split(location)[0] 932 dirname = _path_split(location)[0]
1005 spec.submodule_search_locations.append(dirname) 933 spec.submodule_search_locations.append(dirname)
1006 934
1007 return spec 935 return spec
1008 936
1009 937
1010 def _spec_from_module(module, loader=None, origin=None): 938 def _spec_from_module(module, loader=None, origin=None):
1011 # This function is meant for use in _setup(). 939 # This function is meant for use in _setup().
1012 try: 940 try:
1013 return module.__spec__ 941 spec = module.__spec__
1014 except AttributeError: 942 except AttributeError:
1015 pass 943 pass
944 else:
945 if spec is not None:
946 return spec
1016 947
1017 name = module.__name__ 948 name = module.__name__
1018 if loader is None: 949 if loader is None:
1019 try: 950 try:
1020 loader = module.__loader__ 951 loader = module.__loader__
1021 except AttributeError: 952 except AttributeError:
1022 # loader will stay None. 953 # loader will stay None.
1023 pass 954 pass
1024 try: 955 try:
1025 location = module.__file__ 956 location = module.__file__
(...skipping 18 matching lines...) Expand all
1044 975
1045 spec = ModuleSpec(name, loader, origin=origin) 976 spec = ModuleSpec(name, loader, origin=origin)
1046 spec._set_fileattr = False if location is None else True 977 spec._set_fileattr = False if location is None else True
1047 spec.cached = cached 978 spec.cached = cached
1048 spec.submodule_search_locations = submodule_search_locations 979 spec.submodule_search_locations = submodule_search_locations
1049 return spec 980 return spec
1050 981
1051 982
1052 class _SpecMethods: 983 class _SpecMethods:
1053 984
985 """Convenience wrapper around spec objects to provide spec-specific
986 methods."""
987
988 def __init__(self, spec):
989 self.spec = spec
990
1054 @classmethod 991 @classmethod
1055 def from_module(cls, module): 992 def from_module(cls, module):
993 """Create a spec from a module's attributes."""
1056 try: 994 try:
1057 spec = module.__spec__ 995 spec = module.__spec__
1058 except AttributeError: 996 except AttributeError:
1059 try: 997 try:
1060 loader = spec.__loader__ 998 loader = spec.__loader__
1061 except AttributeError: 999 except AttributeError:
1062 spec = _find_spec(module.__name__) 1000 spec = _find_spec(module.__name__)
1001 if spec is None:
1002 spec = spec_from_loader(module.__name__, loader)
1063 else: 1003 else:
1064 spec = spec_from_loader(module.__name__, loader) 1004 spec = spec_from_loader(module.__name__, loader)
1065 return cls(spec) 1005 return cls(spec)
1066
1067 def __init__(self, spec):
1068 self.spec = spec
1069 1006
1070 def module_repr(self): 1007 def module_repr(self):
1071 """Return the repr to use for the module.""" 1008 """Return the repr to use for the module."""
1072 # We mostly replicate _module_repr() using the spec attributes. 1009 # We mostly replicate _module_repr() using the spec attributes.
1073 spec = self.spec 1010 spec = self.spec
1074 name = '?' if spec.name is None else spec.name 1011 name = '?' if spec.name is None else spec.name
1075 if spec.origin is None: 1012 if spec.origin is None:
1076 if spec.loader is None: 1013 if spec.loader is None:
1077 return '<module {!r}>'.format(name) 1014 return '<module {!r}>'.format(name)
1078 else: 1015 else:
(...skipping 14 matching lines...) Expand all
1093 spec.loader -> module.__loader__ 1030 spec.loader -> module.__loader__
1094 spec.parent -> module.__package__ 1031 spec.parent -> module.__package__
1095 spec -> module.__spec__ 1032 spec -> module.__spec__
1096 1033
1097 Optional: 1034 Optional:
1098 spec.origin -> module.__file__ (if spec.set_fileattr is true) 1035 spec.origin -> module.__file__ (if spec.set_fileattr is true)
1099 spec.cached -> module.__cached__ (if __file__ also set) 1036 spec.cached -> module.__cached__ (if __file__ also set)
1100 spec.submodule_search_locations -> module.__path__ (if set) 1037 spec.submodule_search_locations -> module.__path__ (if set)
1101 1038
1102 """ 1039 """
1103 # XXX Make _override public?
1104 spec = self.spec 1040 spec = self.spec
1105 1041
1106 # The passed in module may be not support attribute assignment, 1042 # The passed in module may be not support attribute assignment,
1107 # in which case we simply don't set the attributes. 1043 # in which case we simply don't set the attributes.
1108 1044
1109 # __name__ 1045 # __name__
1110 if (_override or _force_name or 1046 if (_override or _force_name or
1111 getattr(module, '__name__', None) is None): 1047 getattr(module, '__name__', None) is None):
1112 try: 1048 try:
1113 module.__name__ = spec.name 1049 module.__name__ = spec.name
1114 except AttributeError: 1050 except AttributeError:
1115 pass 1051 pass
1116 1052
1117 # __loader__ 1053 # __loader__
1118 if _override or getattr(module, '__loader__', None) is None: 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
1119 try: 1061 try:
1120 module.__loader__ = spec.loader 1062 module.__loader__ = loader
1121 except AttributeError: 1063 except AttributeError:
1122 pass 1064 pass
1123 1065
1124 # __package__ 1066 # __package__
1125 if _override or getattr(module, '__package__', None) is None: 1067 if _override or getattr(module, '__package__', None) is None:
1126 try: 1068 try:
1127 module.__package__ = spec.parent 1069 module.__package__ = spec.parent
1128 except AttributeError: 1070 except AttributeError:
1129 pass 1071 pass
1130 1072
(...skipping 20 matching lines...) Expand all
1151 pass 1093 pass
1152 1094
1153 # __cached__ 1095 # __cached__
1154 if _override or getattr(module, '__cached__', None) is None: 1096 if _override or getattr(module, '__cached__', None) is None:
1155 if spec.cached is not None: 1097 if spec.cached is not None:
1156 try: 1098 try:
1157 module.__cached__ = spec.cached 1099 module.__cached__ = spec.cached
1158 except AttributeError: 1100 except AttributeError:
1159 pass 1101 pass
1160 1102
1161 def _update_module_attrs(self, module, limited=True):
1162 try:
1163 cached.__name__ = name
1164 except AttributeError:
1165 pass
1166 if not hasattr(cached, '__spec__'):
1167 try:
1168 cached.__spec__ = self.spec
1169 except AttributeError:
1170 pass
1171 if not limited:
1172 raise NotImplementedError
1173
1174 # Under normal circumstances _create(), _exec(), _load_unlocked(),
1175 # _load(), and # _reload() should not be called directly. So they
1176 # have underscore # names (a.k.a. private API).
1177
1178 def create(self): 1103 def create(self):
1179 """Return a new module to be loaded. 1104 """Return a new module to be loaded.
1180 1105
1181 The import-related module attributes are also set with the 1106 The import-related module attributes are also set with the
1182 appropriate values from the spec. 1107 appropriate values from the spec.
1183 1108
1184 """ 1109 """
1185 spec = self.spec 1110 spec = self.spec
1186 # Typically loaders will not implement create_module(). 1111 # Typically loaders will not implement create_module().
1187 if hasattr(spec.loader, 'create_module'): 1112 if hasattr(spec.loader, 'create_module'):
1188 # If create_module() returns `None` it means the default 1113 # If create_module() returns `None` it means the default
1189 # module creation should be used. 1114 # module creation should be used.
1190 module = spec.loader.create_module(spec) 1115 module = spec.loader.create_module(spec)
1191 else: 1116 else:
1192 module = None 1117 module = None
1193 if module is None: 1118 if module is None:
1194 # This must be done before open() is ever called as the 'io' 1119 # This must be done before open() is ever called as the 'io'
1195 # module implicitly imports 'locale' and would otherwise 1120 # module implicitly imports 'locale' and would otherwise
1196 # trigger an infinite loop. 1121 # trigger an infinite loop.
1197 module = type(_io)(spec.name) 1122 module = _new_module(spec.name)
1198 self.init_module_attrs(module) 1123 self.init_module_attrs(module)
1199 return module 1124 return module
1200 1125
1201 def _exec(self, module): 1126 def _exec(self, module):
1202 """Do everything necessary to execute the module. 1127 """Do everything necessary to execute the module.
1203 1128
1204 The namespace of `module` is used as the target of execution. 1129 The namespace of `module` is used as the target of execution.
1205 This method uses the loader's `exec_module()` method. 1130 This method uses the loader's `exec_module()` method.
1206 1131
1207 """ 1132 """
1208 self.spec.loader.exec_module(module) 1133 self.spec.loader.exec_module(module)
1209 1134
1135 # Used by importlib.reload() and _load_module_shim().
1210 def exec(self, module): 1136 def exec(self, module):
1211 """Execute the spec in an existing module's namespace.""" 1137 """Execute the spec in an existing module's namespace."""
1212 name = self.spec.name 1138 name = self.spec.name
1213 _imp.acquire_lock() 1139 _imp.acquire_lock()
1214 with _ModuleLockManager(name): 1140 with _ModuleLockManager(name):
1215 if sys.modules.get(name) is not module: 1141 if sys.modules.get(name) is not module:
1216 msg = 'module {} not in sys.modules'.format(name) 1142 msg = 'module {!r} not in sys.modules'.format(name)
1217 raise ImportError(msg, name=name) 1143 raise ImportError(msg, name=name)
1218 if self.spec.loader is None: 1144 if self.spec.loader is None:
1219 if self.spec.submodule_search_locations is None: 1145 if self.spec.submodule_search_locations is None:
1220 raise ImportError('missing loader', name=self.spec.name) 1146 raise ImportError('missing loader', name=self.spec.name)
1221 # namespace package 1147 # namespace package
1222 self.init_module_attrs(module) 1148 self.init_module_attrs(module, _override=True)
1223 return module 1149 return module
1224 self.init_module_attrs(module) 1150 self.init_module_attrs(module, _override=True)
1225 if not hasattr(self.spec.loader, 'exec_module'): 1151 if not hasattr(self.spec.loader, 'exec_module'):
1226 # XXX DeprecationWarning goes here... 1152 # XXX DeprecationWarning goes here...
1227 self.spec.loader.load_module(name) 1153 self.spec.loader.load_module(name)
1228 return sys.modules[name] 1154 else:
1229 module.__spec__ = self.spec # Must be set. 1155 self._exec(module)
1230 self._exec(module)
1231 return sys.modules[name] 1156 return sys.modules[name]
1232 1157
1233 def _load_backward_compatible(self): 1158 def _load_backward_compatible(self):
1234 # XXX DeprecationWarning goes here... 1159 # XXX DeprecationWarning goes here...
1235 spec = self.spec 1160 spec = self.spec
1236 # The module must be in sys.modules! 1161 # The module must be in sys.modules!
1237 spec.loader.load_module(spec.name) 1162 spec.loader.load_module(spec.name)
1238 module = sys.modules[spec.name] 1163 module = sys.modules[spec.name]
1239 if getattr(module, '__loader__', None) is None: 1164 if getattr(module, '__loader__', None) is None:
1240 try: 1165 try:
(...skipping 10 matching lines...) Expand all
1251 module.__package__ = spec.name.rpartition('.')[0] 1176 module.__package__ = spec.name.rpartition('.')[0]
1252 except AttributeError: 1177 except AttributeError:
1253 pass 1178 pass
1254 if getattr(module, '__spec__', None) is None: 1179 if getattr(module, '__spec__', None) is None:
1255 try: 1180 try:
1256 module.__spec__ = spec 1181 module.__spec__ = spec
1257 except AttributeError: 1182 except AttributeError:
1258 pass 1183 pass
1259 return module 1184 return module
1260 1185
1186 # XXX If we don't end up using this for pythonrun.c/runpy, we should
1187 # get rid of it.
1261 def _load_existing(self, module): 1188 def _load_existing(self, module):
1262 """Exec the spec'ed module into an existing module's namespace.""" 1189 """Exec the spec'ed module into an existing module's namespace."""
1263 # For use by runpy. 1190 # For use by runpy.
1264 with _installed_safely(module): 1191 with _installed_safely(module):
1265 loaded = self.exec(module) 1192 loaded = self.exec(module)
1266 return loaded 1193 return loaded
1267 1194
1268 def _load_unlocked(self): 1195 def _load_unlocked(self):
1269 # A helper for direct use by the import system. 1196 # A helper for direct use by the import system.
1270 if self.spec.loader is not None: 1197 if self.spec.loader is not None:
1271 # not a namespace package 1198 # not a namespace package
1272 if not hasattr(self.spec.loader, 'exec_module'): 1199 if not hasattr(self.spec.loader, 'exec_module'):
1273 return self._load_backward_compatible() 1200 return self._load_backward_compatible()
1274 1201
1275 module = self.create() 1202 module = self.create()
1276 with _installed_safely(module): 1203 with _installed_safely(module):
1277 if self.spec.loader is None: 1204 if self.spec.loader is None:
1278 if self.spec.submodule_search_locations is None: 1205 if self.spec.submodule_search_locations is None:
1279 raise ImportError('missing loader', name=self.spec.name) 1206 raise ImportError('missing loader', name=self.spec.name)
1280 # namespace package 1207 # A namespace package so do nothing.
1281 else: 1208 else:
1282 self._exec(module) 1209 self._exec(module)
1283 1210
1284 # We don't ensure that the import-related module attributes get 1211 # We don't ensure that the import-related module attributes get
1285 # set in the sys.modules replacement case. Such modules are on 1212 # set in the sys.modules replacement case. Such modules are on
1286 # their own. 1213 # their own.
1287 return sys.modules[self.spec.name] 1214 return sys.modules[self.spec.name]
1288 1215
1216 # A method used during testing of _load_unlocked() and by
1217 # _load_module_shim().
1289 def load(self): 1218 def load(self):
1290 """Return a new module object, loaded by the spec's loader. 1219 """Return a new module object, loaded by the spec's loader.
1291 1220
1292 The module is not added to its parent. 1221 The module is not added to its parent.
1293 1222
1294 If a module is already in sys.modules, that existing module gets 1223 If a module is already in sys.modules, that existing module gets
1295 clobbered. 1224 clobbered.
1296 1225
1297 """ 1226 """
1298 # XXX Chance for deadlock with circular import? Reload only?
1299 # XXX Check to see if already in sys.modules?
1300 _imp.acquire_lock() 1227 _imp.acquire_lock()
1301 with _ModuleLockManager(self.spec.name): 1228 with _ModuleLockManager(self.spec.name):
1302 return self._load_unlocked() 1229 return self._load_unlocked()
1303 1230
1304 1231
1305 # Loaders ##################################################################### 1232 # Loaders #####################################################################
1306 1233
1307 class BuiltinImporter: 1234 class BuiltinImporter:
1308 1235
1309 """Meta path import for built-in modules. 1236 """Meta path import for built-in modules.
1310 1237
1311 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
1312 instantiate the class. 1239 instantiate the class.
1313 1240
1314 """ 1241 """
1315 _ORIGIN = 'built-in' 1242
1316 1243 @staticmethod
1317 @classmethod 1244 def module_repr(module):
1318 def _get_spec(cls, fullname): 1245 # XXX deprecate
1319 return spec_from_loader(fullname, cls, origin=cls._ORIGIN)
1320
1321 # XXX BC issues with removing?
1322 @classmethod
1323 def module_repr(cls, module):
1324 return '<module {!r} (built-in)>'.format(module.__name__) 1246 return '<module {!r} (built-in)>'.format(module.__name__)
1325 1247
1326 # XXX Add shims for find_module and load_module. 1248 @classmethod
1327 1249 def find_spec(cls, fullname, path=None, target=None):
1328 # 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
1329 @classmethod 1257 @classmethod
1330 def find_module(cls, fullname, path=None): 1258 def find_module(cls, fullname, path=None):
1331 """Find the built-in module. 1259 """Find the built-in module.
1332 1260
1333 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.
1334 1262
1335 """ 1263 """
1336 if path is not None: 1264 spec = cls.find_spec(fullname, path)
1337 return None 1265 return spec.loader if spec is not None else None
1338 return cls if _imp.is_builtin(fullname) else None 1266
1339 1267 @classmethod
1340 # XXX Change to exec_module().
1341 @classmethod
1342 @set_package
1343 @set_loader
1344 @_requires_builtin 1268 @_requires_builtin
1345 def load_module(cls, fullname): 1269 def load_module(cls, fullname):
1346 """Load a built-in module.""" 1270 """Load a built-in module."""
1347 with _ManageReload(fullname): 1271 with _ManageReload(fullname):
1348 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
1349 1276
1350 @classmethod 1277 @classmethod
1351 @_requires_builtin 1278 @_requires_builtin
1352 def get_code(cls, fullname): 1279 def get_code(cls, fullname):
1353 """Return None as built-in modules do not have code objects.""" 1280 """Return None as built-in modules do not have code objects."""
1354 return None 1281 return None
1355 1282
1356 @classmethod 1283 @classmethod
1357 @_requires_builtin 1284 @_requires_builtin
1358 def get_source(cls, fullname): 1285 def get_source(cls, fullname):
1359 """Return None as built-in modules do not have source code.""" 1286 """Return None as built-in modules do not have source code."""
1360 return None 1287 return None
1361 1288
1362 @classmethod 1289 @classmethod
1363 @_requires_builtin 1290 @_requires_builtin
1364 def is_package(cls, fullname): 1291 def is_package(cls, fullname):
1365 """Return False as built-in modules are never packages.""" 1292 """Return False as built-in modules are never packages."""
1366 # XXX DeprecationWarning here... 1293 # XXX DeprecationWarning here...
1367 return False 1294 return False
1368 1295
1369 1296
1370 class FrozenImporter: 1297 class FrozenImporter:
1371 1298
1372 """Meta path import for frozen modules. 1299 """Meta path import for frozen modules.
1373 1300
1374 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
1375 instantiate the class. 1302 instantiate the class.
1376 1303
1377 """ 1304 """
1378 _ORIGIN = 'frozen' 1305
1379 1306 @staticmethod
1380 # XXX BC issues with removing? 1307 def module_repr(m):
1381 @classmethod 1308 # XXX deprecate
1382 def module_repr(cls, m):
1383 return '<module {!r} (frozen)>'.format(m.__name__) 1309 return '<module {!r} (frozen)>'.format(m.__name__)
1384 1310
1385 # XXX Add shims for find_module and load_module. 1311 @classmethod
1386 1312 def find_spec(cls, fullname, path=None, target=None):
1387 # 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
1388 @classmethod 1318 @classmethod
1389 def find_module(cls, fullname, path=None): 1319 def find_module(cls, fullname, path=None):
1390 """Find a frozen module.""" 1320 """Find a frozen module."""
1391 return cls if _imp.is_frozen(fullname) else None 1321 return cls if _imp.is_frozen(fullname) else None
1392 1322
1393 # XXX Change to exec_module(). 1323 @staticmethod
1394 @classmethod 1324 def exec_module(module):
1395 @set_package 1325 name = module.__spec__.name
1396 @set_loader 1326 if not _imp.is_frozen(name):
1397 @_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
1398 def load_module(cls, fullname): 1333 def load_module(cls, fullname):
1399 """Load a frozen module.""" 1334 """Load a frozen module."""
1400 with _ManageReload(fullname): 1335 return _load_module_shim(cls, fullname)
1401 m = _call_with_frames_removed(_imp.init_frozen, fullname)
1402 # Let our own module_repr() method produce a suitable repr.
1403 del m.__file__
1404 return m
1405 1336
1406 @classmethod 1337 @classmethod
1407 @_requires_frozen 1338 @_requires_frozen
1408 def get_code(cls, fullname): 1339 def get_code(cls, fullname):
1409 """Return the code object for the frozen module.""" 1340 """Return the code object for the frozen module."""
1410 return _imp.get_frozen_object(fullname) 1341 return _imp.get_frozen_object(fullname)
1411 1342
1412 @classmethod 1343 @classmethod
1413 @_requires_frozen 1344 @_requires_frozen
1414 def get_source(cls, fullname): 1345 def get_source(cls, fullname):
1415 """Return None as frozen modules do not have source code.""" 1346 """Return None as frozen modules do not have source code."""
1416 return None 1347 return None
1417 1348
1418 @classmethod 1349 @classmethod
1419 @_requires_frozen 1350 @_requires_frozen
1420 def is_package(cls, fullname): 1351 def is_package(cls, fullname):
1421 """Return True if the frozen module is a package.""" 1352 """Return True if the frozen module is a package."""
1422 return _imp.is_frozen_package(fullname) 1353 return _imp.is_frozen_package(fullname)
1423 1354
1424 1355
1425 class WindowsRegistryFinder: 1356 class WindowsRegistryFinder:
1426 1357
1427 """Meta path finder for modules declared in the Windows registry. 1358 """Meta path finder for modules declared in the Windows registry."""
1428 """
1429 1359
1430 REGISTRY_KEY = ( 1360 REGISTRY_KEY = (
1431 'Software\\Python\\PythonCore\\{sys_version}' 1361 'Software\\Python\\PythonCore\\{sys_version}'
1432 '\\Modules\\{fullname}') 1362 '\\Modules\\{fullname}')
1433 REGISTRY_KEY_DEBUG = ( 1363 REGISTRY_KEY_DEBUG = (
1434 'Software\\Python\\PythonCore\\{sys_version}' 1364 'Software\\Python\\PythonCore\\{sys_version}'
1435 '\\Modules\\{fullname}\\Debug') 1365 '\\Modules\\{fullname}\\Debug')
1436 DEBUG_BUILD = False # Changed in _setup() 1366 DEBUG_BUILD = False # Changed in _setup()
1437
1438 # XXX Add shim for find_module().
1439 1367
1440 @classmethod 1368 @classmethod
1441 def _open_registry(cls, key): 1369 def _open_registry(cls, key):
1442 try: 1370 try:
1443 return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key) 1371 return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key)
1444 except OSError: 1372 except OSError:
1445 return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key) 1373 return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
1446 1374
1447 @classmethod 1375 @classmethod
1448 def _search_registry(cls, fullname): 1376 def _search_registry(cls, fullname):
1449 if cls.DEBUG_BUILD: 1377 if cls.DEBUG_BUILD:
1450 registry_key = cls.REGISTRY_KEY_DEBUG 1378 registry_key = cls.REGISTRY_KEY_DEBUG
1451 else: 1379 else:
1452 registry_key = cls.REGISTRY_KEY 1380 registry_key = cls.REGISTRY_KEY
1453 key = registry_key.format(fullname=fullname, 1381 key = registry_key.format(fullname=fullname,
1454 sys_version=sys.version[:3]) 1382 sys_version=sys.version[:3])
1455 try: 1383 try:
1456 with cls._open_registry(key) as hkey: 1384 with cls._open_registry(key) as hkey:
1457 filepath = _winreg.QueryValue(hkey, '') 1385 filepath = _winreg.QueryValue(hkey, '')
1458 except OSError: 1386 except OSError:
1459 return None 1387 return None
1460 return filepath 1388 return filepath
1461 1389
1462 @classmethod 1390 @classmethod
1463 def find_module(cls, fullname, path=None): 1391 def find_spec(cls, fullname, path=None, target=None):
1464 """Find module named in the registry.""" 1392 # XXX untested! Need a Windows person to write tests (otherwise mock out appropriately)
1465 filepath = cls._search_registry(fullname) 1393 filepath = cls._search_registry(fullname)
1466 if filepath is None: 1394 if filepath is None:
1467 return None 1395 return None
1468 try: 1396 try:
1469 _path_stat(filepath) 1397 _path_stat(filepath)
1470 except OSError: 1398 except OSError:
1471 return None 1399 return None
1472 for loader, suffixes in _get_supported_file_loaders(): 1400 for loader, suffixes in _get_supported_file_loaders():
1473 if filepath.endswith(tuple(suffixes)): 1401 if filepath.endswith(tuple(suffixes)):
1474 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
1475 1414
1476 1415
1477 class _LoaderBasics: 1416 class _LoaderBasics:
1478 1417
1479 """Base class of common code needed by both SourceLoader and 1418 """Base class of common code needed by both SourceLoader and
1480 SourcelessFileLoader.""" 1419 SourcelessFileLoader."""
1481
1482 # XXX Add shim for load_module().
1483 1420
1484 # XXX deprecate? 1421 # XXX deprecate?
1485 def is_package(self, fullname): 1422 def is_package(self, fullname):
1486 """Concrete implementation of InspectLoader.is_package by checking if 1423 """Concrete implementation of InspectLoader.is_package by checking if
1487 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'."""
1488 filename = _path_split(self.get_filename(fullname))[1] 1425 filename = _path_split(self.get_filename(fullname))[1]
1489 filename_base = filename.rsplit('.', 1)[0] 1426 filename_base = filename.rsplit('.', 1)[0]
1490 tail_name = fullname.rpartition('.')[2] 1427 tail_name = fullname.rpartition('.')[2]
1491 return filename_base == '__init__' and tail_name != '__init__' 1428 return filename_base == '__init__' and tail_name != '__init__'
1492 1429
1493 # XXX Remove. 1430 def exec_module(self, module):
1494 def init_module_attrs(self, module): 1431 """Execute the module."""
1495 """Set various attributes on the module. 1432 code = self.get_code(module.__name__)
1496 1433 if code is None:
1497 ExecutionLoader.init_module_attrs() is used to set __loader__, 1434 raise ImportError('cannot load module {!r} when get_code() '
1498 __package__, __file__, and optionally __path__. The __cached__ attribute 1435 'returns None'.format(module.__name__))
1499 is set using imp.cache_from_source() and __file__. 1436 _call_with_frames_removed(exec, code, module.__dict__)
1500 """ 1437
1501 module.__loader__ = self # Loader 1438 load_module = _load_module_shim
1502 _init_package_attrs(self, module) # InspectLoader
1503 _init_file_attrs(self, module) # ExecutionLoader
1504 if hasattr(module, '__file__'): # SourceLoader
1505 try:
1506 module.__cached__ = cache_from_source(module.__file__)
1507 except NotImplementedError:
1508 pass
1509
1510 # XXX Change to exec_module().
1511 def load_module(self, fullname):
1512 """Load the specified module into sys.modules and return it."""
1513 with module_to_load(fullname) as module:
1514 self.init_module_attrs(module)
1515 code = self.get_code(fullname)
1516 if code is None:
1517 raise ImportError('cannot load module {!r} when get_code() '
1518 'returns None'.format(fullname))
1519 _call_with_frames_removed(exec, code, module.__dict__)
1520 return module
1521 1439
1522 1440
1523 class SourceLoader(_LoaderBasics): 1441 class SourceLoader(_LoaderBasics):
1524 1442
1525 def path_mtime(self, path): 1443 def path_mtime(self, path):
1526 """Optional method that returns the modification time (an int) for the 1444 """Optional method that returns the modification time (an int) for the
1527 specified path, where path is a str. 1445 specified path, where path is a str.
1528 1446
1529 Raises IOError when the path cannot be handled. 1447 Raises IOError when the path cannot be handled.
1530 """ 1448 """
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1634 1552
1635 """Base file loader class which implements the loader protocol methods that 1553 """Base file loader class which implements the loader protocol methods that
1636 require file system usage.""" 1554 require file system usage."""
1637 1555
1638 def __init__(self, fullname, path): 1556 def __init__(self, fullname, path):
1639 """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
1640 finder.""" 1558 finder."""
1641 self.name = fullname 1559 self.name = fullname
1642 self.path = path 1560 self.path = path
1643 1561
1644 # XXX Change to exec_module().
1645 @_check_name 1562 @_check_name
1646 def load_module(self, fullname): 1563 def load_module(self, fullname):
1647 """Load a module from a file.""" 1564 """Load a module from a file."""
1648 # 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
1649 # of that form can be updated without breaking the frozen module 1568 # of that form can be updated without breaking the frozen module
1650 return super(FileLoader, self).load_module(fullname) 1569 return super(FileLoader, self).load_module(fullname)
1651 1570
1652 @_check_name 1571 @_check_name
1653 def get_filename(self, fullname): 1572 def get_filename(self, fullname):
1654 """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."""
1655 return self.path 1574 return self.path
1656 1575
1657 def get_data(self, path): 1576 def get_data(self, path):
1658 """Return the data from path as raw bytes.""" 1577 """Return the data from path as raw bytes."""
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1700 _verbose_message('created {!r}', path) 1619 _verbose_message('created {!r}', path)
1701 except OSError as exc: 1620 except OSError as exc:
1702 # Same as above: just don't write the bytecode. 1621 # Same as above: just don't write the bytecode.
1703 _verbose_message('could not create {!r}: {!r}', path, exc) 1622 _verbose_message('could not create {!r}: {!r}', path, exc)
1704 1623
1705 1624
1706 class SourcelessFileLoader(FileLoader, _LoaderBasics): 1625 class SourcelessFileLoader(FileLoader, _LoaderBasics):
1707 1626
1708 """Loader which handles sourceless file imports.""" 1627 """Loader which handles sourceless file imports."""
1709 1628
1710 # XXX Merge into finder.
1711 def init_module_attrs(self, module):
1712 super().init_module_attrs(module)
1713 module.__cached__ = module.__file__
1714
1715 def get_code(self, fullname): 1629 def get_code(self, fullname):
1716 path = self.get_filename(fullname) 1630 path = self.get_filename(fullname)
1717 data = self.get_data(path) 1631 data = self.get_data(path)
1718 bytes_data = _validate_bytecode_header(data, name=fullname, path=path) 1632 bytes_data = _validate_bytecode_header(data, name=fullname, path=path)
1719 return _compile_bytecode(bytes_data, name=fullname, bytecode_path=path) 1633 return _compile_bytecode(bytes_data, name=fullname, bytecode_path=path)
1720 1634
1721 def get_source(self, fullname): 1635 def get_source(self, fullname):
1722 """Return None as there is no source code.""" 1636 """Return None as there is no source code."""
1723 return None 1637 return None
1724 1638
1725 1639
1726 # Filled in by _setup(). 1640 # Filled in by _setup().
1727 EXTENSION_SUFFIXES = [] 1641 EXTENSION_SUFFIXES = []
1728 1642
1729 1643
1730 class ExtensionFileLoader: 1644 class ExtensionFileLoader:
1731 1645
1732 """Loader for extension modules. 1646 """Loader for extension modules.
1733 1647
1734 The constructor is designed to work with FileFinder. 1648 The constructor is designed to work with FileFinder.
1735 1649
1736 """ 1650 """
1737 1651
1738 def __init__(self, name, path): 1652 def __init__(self, name, path):
1739 self.name = name 1653 self.name = name
1740 self.path = path 1654 self.path = path
1741 1655
1742 # XXX Add shim for load_module().
1743
1744 # XXX Change to exec_module().
1745 @_check_name 1656 @_check_name
1746 @set_package
1747 @set_loader
1748 def load_module(self, fullname): 1657 def load_module(self, fullname):
1749 """Load an extension module.""" 1658 """Load an extension module."""
1750 with _ManageReload(fullname): 1659 with _ManageReload(fullname):
1751 module = _call_with_frames_removed(_imp.load_dynamic, 1660 module = _call_with_frames_removed(_imp.load_dynamic,
1752 fullname, self.path) 1661 fullname, self.path)
1753 _verbose_message('extension module loaded from {!r}', self.path) 1662 _verbose_message('extension module loaded from {!r}', self.path)
1754 if self.is_package(fullname) and not hasattr(module, '__path__'): 1663 is_package = self.is_package(fullname)
1755 module.__path__ = [_path_split(self.path)[0]] 1664 if is_package and not hasattr(module, '__path__'):
1756 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
1757 1671
1758 def is_package(self, fullname): 1672 def is_package(self, fullname):
1759 """Return True if the extension module is a package.""" 1673 """Return True if the extension module is a package."""
1760 file_name = _path_split(self.path)[1] 1674 file_name = _path_split(self.path)[1]
1761 return any(file_name == '__init__' + suffix 1675 return any(file_name == '__init__' + suffix
1762 for suffix in EXTENSION_SUFFIXES) 1676 for suffix in EXTENSION_SUFFIXES)
1763 1677
1764 def get_code(self, fullname): 1678 def get_code(self, fullname):
1765 """Return None as an extension module cannot create a code object.""" 1679 """Return None as an extension module cannot create a code object."""
1766 return None 1680 return None
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1824 def __repr__(self): 1738 def __repr__(self):
1825 return '_NamespacePath({!r})'.format(self._path) 1739 return '_NamespacePath({!r})'.format(self._path)
1826 1740
1827 def __contains__(self, item): 1741 def __contains__(self, item):
1828 return item in self._recalculate() 1742 return item in self._recalculate()
1829 1743
1830 def append(self, item): 1744 def append(self, item):
1831 self._path.append(item) 1745 self._path.append(item)
1832 1746
1833 1747
1748 # We use this exclusively in init_module_attrs() for backward-compatibility.
1749 class _NamespaceLoader:
1750 def __init__(self, name, path, path_finder):
1751 self._path = _NamespacePath(name, path, path_finder)
1752
1753 # XXX Deprecate
1754 @classmethod
1755 def module_repr(cls, module):
1756 return '<module {!r} (namespace)>'.format(module.__name__)
1757
1758 def is_package(self, fullname):
1759 return True
1760
1761 def get_source(self, fullname):
1762 return ''
1763
1764 def get_code(self, fullname):
1765 return compile('', '<string>', 'exec', dont_inherit=True)
1766
1767 # XXX Deprecate
1768 def load_module(self, fullname):
1769 """Load a namespace module."""
1770 _verbose_message('namespace module loaded with path {!r}', self._path)
1771 return _load_module_shim(self, fullname)
1772
1773
1834 # Finders ##################################################################### 1774 # Finders #####################################################################
1835 1775
1836 class PathFinder: 1776 class PathFinder:
1837 1777
1838 """Meta path finder for sys.path and package __path__ attributes.""" 1778 """Meta path finder for sys.path and package __path__ attributes."""
1839
1840 # XXX Add shim for find_module().
1841 1779
1842 @classmethod 1780 @classmethod
1843 def invalidate_caches(cls): 1781 def invalidate_caches(cls):
1844 """Call the invalidate_caches() method on all path entry finders 1782 """Call the invalidate_caches() method on all path entry finders
1845 stored in sys.path_importer_caches (where implemented).""" 1783 stored in sys.path_importer_caches (where implemented)."""
1846 for finder in sys.path_importer_cache.values(): 1784 for finder in sys.path_importer_cache.values():
1847 if hasattr(finder, 'invalidate_caches'): 1785 if hasattr(finder, 'invalidate_caches'):
1848 finder.invalidate_caches() 1786 finder.invalidate_caches()
1849 1787
1850 @classmethod 1788 @classmethod
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1888 else: 1826 else:
1889 loader = finder.find_module(fullname) 1827 loader = finder.find_module(fullname)
1890 portions = None 1828 portions = None
1891 if loader is not None: 1829 if loader is not None:
1892 return spec_from_loader(fullname, loader) 1830 return spec_from_loader(fullname, loader)
1893 spec = ModuleSpec(fullname, None) 1831 spec = ModuleSpec(fullname, None)
1894 spec.submodule_search_locations = portions 1832 spec.submodule_search_locations = portions
1895 return spec 1833 return spec
1896 1834
1897 @classmethod 1835 @classmethod
1898 def _get_spec(cls, fullname, path, existing=None): 1836 def _get_spec(cls, fullname, path, target=None):
1899 """Find the loader or namespace_path for this module/package name.""" 1837 """Find the loader or namespace_path for this module/package name."""
1900 # If this ends up being a namespace package, namespace_path is 1838 # If this ends up being a namespace package, namespace_path is
1901 # the list of paths that will become its __path__ 1839 # the list of paths that will become its __path__
1902 namespace_path = [] 1840 namespace_path = []
1903 for entry in path: 1841 for entry in path:
1904 if not isinstance(entry, (str, bytes)): 1842 if not isinstance(entry, (str, bytes)):
1905 continue 1843 continue
1906 finder = cls._path_importer_cache(entry) 1844 finder = cls._path_importer_cache(entry)
1907 if finder is not None: 1845 if finder is not None:
1908 if hasattr(finder, 'find_spec'): 1846 if hasattr(finder, 'find_spec'):
1909 spec = finder.find_spec(fullname, existing) 1847 spec = finder.find_spec(fullname, target)
1910 else: 1848 else:
1911 spec = cls._legacy_get_spec(fullname, finder) 1849 spec = cls._legacy_get_spec(fullname, finder)
1912 if spec is None: 1850 if spec is None:
1913 continue 1851 continue
1914 if spec.loader is not None: 1852 if spec.loader is not None:
1915 return spec 1853 return spec
1916 portions = spec.submodule_search_locations 1854 portions = spec.submodule_search_locations
1917 if portions is None: 1855 if portions is None:
1918 raise ImportError('spec missing loader') 1856 raise ImportError('spec missing loader')
1919 # This is possibly part of a namespace package. 1857 # This is possibly part of a namespace package.
1920 # Remember these path entries (if any) for when we 1858 # Remember these path entries (if any) for when we
1921 # create a namespace package, and continue iterating 1859 # create a namespace package, and continue iterating
1922 # on path. 1860 # on path.
1923 namespace_path.extend(portions) 1861 namespace_path.extend(portions)
1924 else: 1862 else:
1925 spec = ModuleSpec(fullname, None) 1863 spec = ModuleSpec(fullname, None)
1926 spec.submodule_search_locations = namespace_path 1864 spec.submodule_search_locations = namespace_path
1927 return spec 1865 return spec
1928 1866
1929 @classmethod 1867 @classmethod
1930 def find_spec(cls, fullname, path=None, existing=None): 1868 def find_spec(cls, fullname, path=None, target=None):
1931 """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
1932 sys.path_importer_cache.""" 1870 sys.path_importer_cache."""
1933 if path is None: 1871 if path is None:
1934 path = sys.path 1872 path = sys.path
1935 spec = cls._get_spec(fullname, path, existing) 1873 spec = cls._get_spec(fullname, path, target)
1936 if spec is None: 1874 if spec is None:
1937 return None 1875 return None
1938 elif spec.loader is None: 1876 elif spec.loader is None:
1939 namespace_path = spec.submodule_search_locations 1877 namespace_path = spec.submodule_search_locations
1940 if namespace_path: 1878 if namespace_path:
1941 # We found at least one namespace path. Return a 1879 # We found at least one namespace path. Return a
1942 # spec which can create the namespace package. 1880 # spec which can create the namespace package.
1943 spec.origin = 'namespace' 1881 spec.origin = 'namespace'
1944 spec.submodule_search_locations = _NamespacePath(fullname, names pace_path, cls._get_spec) 1882 spec.submodule_search_locations = _NamespacePath(fullname, names pace_path, cls._get_spec)
1945 return spec 1883 return spec
1946 else: 1884 else:
1947 return None 1885 return None
1948 else: 1886 else:
1949 return spec 1887 return spec
1950 1888
1951 # XXX Replace with a new shim?
1952 @classmethod 1889 @classmethod
1953 def find_module(cls, fullname, path=None): 1890 def find_module(cls, fullname, path=None):
1954 """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
1955 sys.path_importer_cache.""" 1892 sys.path_importer_cache."""
1956 # XXX Deprecation warning here. 1893 # XXX Deprecation warning here.
1957 spec = cls.find_spec(fullname, path) 1894 spec = cls.find_spec(fullname, path)
1958 if spec is None: 1895 if spec is None:
1959 return None 1896 return None
1960 return spec.loader 1897 return spec.loader
1961 1898
(...skipping 18 matching lines...) Expand all
1980 # Base (directory) path 1917 # Base (directory) path
1981 self.path = path or '.' 1918 self.path = path or '.'
1982 self._path_mtime = -1 1919 self._path_mtime = -1
1983 self._path_cache = set() 1920 self._path_cache = set()
1984 self._relaxed_path_cache = set() 1921 self._relaxed_path_cache = set()
1985 1922
1986 def invalidate_caches(self): 1923 def invalidate_caches(self):
1987 """Invalidate the directory mtime.""" 1924 """Invalidate the directory mtime."""
1988 self._path_mtime = -1 1925 self._path_mtime = -1
1989 1926
1990 # XXX Use a different shim.
1991 find_module = _find_module_shim 1927 find_module = _find_module_shim
1992 1928
1993 # XXX use a shim
1994 def find_loader(self, fullname): 1929 def find_loader(self, fullname):
1995 """Try to find a loader for the specified module, or the namespace 1930 """Try to find a loader for the specified module, or the namespace
1996 package portions. Returns (loader, list-of-portions).""" 1931 package portions. Returns (loader, list-of-portions)."""
1997 spec = self.find_spec(fullname) 1932 spec = self.find_spec(fullname)
1998 if spec is None: 1933 if spec is None:
1999 return None, [] 1934 return None, []
2000 return spec.loader, spec.submodule_search_locations or [] 1935 return spec.loader, spec.submodule_search_locations or []
2001 1936
2002 def _get_spec(self, loader_class, fullname, path, submodule_search_locations , existing): 1937 def _get_spec(self, loader_class, fullname, path, submodule_search_locations , target):
2003 loader = loader_class(fullname, path) 1938 loader = loader_class(fullname, path)
2004 try: 1939 try:
2005 get_spec = loader._get_spec 1940 get_spec = loader._get_spec
2006 except AttributeError: 1941 except AttributeError:
2007 return spec_from_file_location(fullname, path, loader=loader, 1942 return spec_from_file_location(fullname, path, loader=loader,
2008 submodule_search_locations=submodule_ search_locations) 1943 submodule_search_locations=submodule_ search_locations)
2009 else: 1944 else:
2010 return get_spec(fullname, path, submodule_search_locations, existing ) 1945 return get_spec(fullname, path, submodule_search_locations, target)
2011 1946
2012 def find_spec(self, fullname, existing=None): 1947 def find_spec(self, fullname, target=None):
2013 """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
2014 package portions. Returns (loader, list-of-portions).""" 1949 package portions. Returns (loader, list-of-portions)."""
2015 is_namespace = False 1950 is_namespace = False
2016 tail_module = fullname.rpartition('.')[2] 1951 tail_module = fullname.rpartition('.')[2]
2017 try: 1952 try:
2018 mtime = _path_stat(self.path or _os.getcwd()).st_mtime 1953 mtime = _path_stat(self.path or _os.getcwd()).st_mtime
2019 except OSError: 1954 except OSError:
2020 mtime = -1 1955 mtime = -1
2021 if mtime != self._path_mtime: 1956 if mtime != self._path_mtime:
2022 self._fill_cache() 1957 self._fill_cache()
2023 self._path_mtime = mtime 1958 self._path_mtime = mtime
2024 # tail_module keeps the original casing, for __file__ and friends 1959 # tail_module keeps the original casing, for __file__ and friends
2025 if _relax_case(): 1960 if _relax_case():
2026 cache = self._relaxed_path_cache 1961 cache = self._relaxed_path_cache
2027 cache_module = tail_module.lower() 1962 cache_module = tail_module.lower()
2028 else: 1963 else:
2029 cache = self._path_cache 1964 cache = self._path_cache
2030 cache_module = tail_module 1965 cache_module = tail_module
2031 # 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).
2032 if cache_module in cache: 1967 if cache_module in cache:
2033 base_path = _path_join(self.path, tail_module) 1968 base_path = _path_join(self.path, tail_module)
2034 for suffix, loader_class in self._loaders: 1969 for suffix, loader_class in self._loaders:
2035 init_filename = '__init__' + suffix 1970 init_filename = '__init__' + suffix
2036 full_path = _path_join(base_path, init_filename) 1971 full_path = _path_join(base_path, init_filename)
2037 if _path_isfile(full_path): 1972 if _path_isfile(full_path):
2038 return self._get_spec(loader_class, fullname, full_path, [ba se_path], existing) 1973 return self._get_spec(loader_class, fullname, full_path, [ba se_path], target)
2039 else: 1974 else:
2040 # If a namespace package, return the path if we don't 1975 # If a namespace package, return the path if we don't
2041 # find a module in the next section. 1976 # find a module in the next section.
2042 is_namespace = _path_isdir(base_path) 1977 is_namespace = _path_isdir(base_path)
2043 # Check for a file w/ a proper suffix exists. 1978 # Check for a file w/ a proper suffix exists.
2044 for suffix, loader_class in self._loaders: 1979 for suffix, loader_class in self._loaders:
2045 full_path = _path_join(self.path, tail_module + suffix) 1980 full_path = _path_join(self.path, tail_module + suffix)
2046 _verbose_message('trying {}'.format(full_path), verbosity=2) 1981 _verbose_message('trying {}'.format(full_path), verbosity=2)
2047 if cache_module + suffix in cache: 1982 if cache_module + suffix in cache:
2048 if _path_isfile(full_path): 1983 if _path_isfile(full_path):
2049 return self._get_spec(loader_class, fullname, full_path, Non e, existing) 1984 return self._get_spec(loader_class, fullname, full_path, Non e, target)
2050 if is_namespace: 1985 if is_namespace:
2051 _verbose_message('possible namespace for {}'.format(base_path)) 1986 _verbose_message('possible namespace for {}'.format(base_path))
2052 spec = ModuleSpec(fullname, None) 1987 spec = ModuleSpec(fullname, None)
2053 spec.submodule_search_locations = [base_path] 1988 spec.submodule_search_locations = [base_path]
2054 return spec 1989 return spec
2055 return None 1990 return None
2056 1991
2057 def _fill_cache(self): 1992 def _fill_cache(self):
2058 """Fill the cache of potential modules and packages for this directory." "" 1993 """Fill the cache of potential modules and packages for this directory." ""
2059 path = self.path 1994 path = self.path
(...skipping 18 matching lines...) Expand all
2078 name, dot, suffix = item.partition('.') 2013 name, dot, suffix = item.partition('.')
2079 if dot: 2014 if dot:
2080 new_name = '{}.{}'.format(name, suffix.lower()) 2015 new_name = '{}.{}'.format(name, suffix.lower())
2081 else: 2016 else:
2082 new_name = name 2017 new_name = name
2083 lower_suffix_contents.add(new_name) 2018 lower_suffix_contents.add(new_name)
2084 self._path_cache = lower_suffix_contents 2019 self._path_cache = lower_suffix_contents
2085 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS): 2020 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
2086 self._relaxed_path_cache = {fn.lower() for fn in contents} 2021 self._relaxed_path_cache = {fn.lower() for fn in contents}
2087 2022
2088 # XXX See _get_supported_file_loaders().
2089 @classmethod 2023 @classmethod
2090 def path_hook(cls, *loader_details): 2024 def path_hook(cls, *loader_details):
2091 """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
2092 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
2093 called on the closure. 2027 called on the closure.
2094 2028
2095 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
2096 raised. 2030 raised.
2097 2031
2098 """ 2032 """
(...skipping 26 matching lines...) Expand all
2125 2059
2126 def _resolve_name(name, package, level): 2060 def _resolve_name(name, package, level):
2127 """Resolve a relative module name to an absolute one.""" 2061 """Resolve a relative module name to an absolute one."""
2128 bits = package.rsplit('.', level - 1) 2062 bits = package.rsplit('.', level - 1)
2129 if len(bits) < level: 2063 if len(bits) < level:
2130 raise ValueError('attempted relative import beyond top-level package') 2064 raise ValueError('attempted relative import beyond top-level package')
2131 base = bits[0] 2065 base = bits[0]
2132 return '{}.{}'.format(base, name) if name else base 2066 return '{}.{}'.format(base, name) if name else base
2133 2067
2134 2068
2135 def _find_spec(name, path, existing=None): 2069 def _find_spec(name, path, target=None):
2136 """Find a module's loader.""" 2070 """Find a module's loader."""
2137 if not sys.meta_path: 2071 if not sys.meta_path:
2138 _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.
2139 is_reload = name in sys.modules 2076 is_reload = name in sys.modules
2140 for finder in sys.meta_path: 2077 for finder in sys.meta_path:
2141 with _ImportLockContext(): 2078 with _ImportLockContext():
2142 try: 2079 try:
2143 find_spec = finder.find_spec 2080 find_spec = finder.find_spec
2144 except AttributeError: 2081 except AttributeError:
2145 loader = finder.find_module(name, path) 2082 loader = finder.find_module(name, path)
2146 if loader is None: 2083 if loader is None:
2147 continue 2084 continue
2148 spec = spec_from_loader(name, loader) 2085 spec = spec_from_loader(name, loader)
2149 else: 2086 else:
2150 spec = find_spec(name, path, existing) 2087 spec = find_spec(name, path, target)
2151 if spec is not None: 2088 if spec is not None:
2152 # The parent import may have already imported this module. 2089 # The parent import may have already imported this module.
2153 if not is_reload and name in sys.modules: 2090 if not is_reload and name in sys.modules:
2154 module = sys.modules[name] 2091 module = sys.modules[name]
2155 try: 2092 try:
2156 return module.__spec__ 2093 __spec__ = module.__spec__
2157 except AttributeError: 2094 except AttributeError:
2158 # 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.
2159 return spec 2098 return spec
2099 else:
2100 if __spec__ is None:
2101 return spec
2102 else:
2103 return __spec__
2160 else: 2104 else:
2161 return spec 2105 return spec
2162 else: 2106 else:
2163 return None 2107 return None
2164 2108
2165 2109
2166 def _sanity_check(name, package, level): 2110 def _sanity_check(name, package, level):
2167 """Verify arguments are "sane".""" 2111 """Verify arguments are "sane"."""
2168 if not isinstance(name, str): 2112 if not isinstance(name, str):
2169 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
2185 2129
2186 def _find_and_load_unlocked(name, import_): 2130 def _find_and_load_unlocked(name, import_):
2187 path = None 2131 path = None
2188 parent = name.rpartition('.')[0] 2132 parent = name.rpartition('.')[0]
2189 if parent: 2133 if parent:
2190 if parent not in sys.modules: 2134 if parent not in sys.modules:
2191 _call_with_frames_removed(import_, parent) 2135 _call_with_frames_removed(import_, parent)
2192 # Crazy side-effects! 2136 # Crazy side-effects!
2193 if name in sys.modules: 2137 if name in sys.modules:
2194 return sys.modules[name] 2138 return sys.modules[name]
2195 # Backwards-compatibility; be nicer to skip the dict lookup.
2196 parent_module = sys.modules[parent] 2139 parent_module = sys.modules[parent]
2197 try: 2140 try:
2198 path = parent_module.__path__ 2141 path = parent_module.__path__
2199 except AttributeError: 2142 except AttributeError:
2200 msg = (_ERR_MSG + '; {} is not a package').format(name, parent) 2143 msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent)
2201 raise ImportError(msg, name=name) 2144 raise ImportError(msg, name=name)
2202 spec = _find_spec(name, path) 2145 spec = _find_spec(name, path)
2203 if spec is None: 2146 if spec is None:
2204 raise ImportError(_ERR_MSG.format(name), name=name) 2147 raise ImportError(_ERR_MSG.format(name), name=name)
2205 else: 2148 else:
2206 module = _SpecMethods(spec)._load_unlocked() 2149 module = _SpecMethods(spec)._load_unlocked()
2207 if parent: 2150 if parent:
2208 # Set the module as an attribute on its parent. 2151 # Set the module as an attribute on its parent.
2209 parent_module = sys.modules[parent] 2152 parent_module = sys.modules[parent]
2210 setattr(parent_module, name.rpartition('.')[2], module) 2153 setattr(parent_module, name.rpartition('.')[2], module)
2211 return module 2154 return module
2212 2155
2213 2156
2214 # XXX Eliminate?
2215 def _find_and_load(name, import_): 2157 def _find_and_load(name, import_):
2216 """Find and load the module, and release the import lock.""" 2158 """Find and load the module, and release the import lock."""
2217 with _ModuleLockManager(name): 2159 with _ModuleLockManager(name):
2218 return _find_and_load_unlocked(name, import_) 2160 return _find_and_load_unlocked(name, import_)
2219 2161
2220 2162
2221 def _gcd_import(name, package=None, level=0): 2163 def _gcd_import(name, package=None, level=0):
2222 """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
2223 being made from, and the level adjustment. 2165 being made from, and the level adjustment.
2224 2166
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
2329 # in 'name'. 2271 # in 'name'.
2330 cut_off = len(name) - len(name.partition('.')[0]) 2272 cut_off = len(name) - len(name.partition('.')[0])
2331 # 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
2332 # when ``'.' not in name``. 2274 # when ``'.' not in name``.
2333 return sys.modules[module.__name__[:len(module.__name__)-cut_off]] 2275 return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
2334 else: 2276 else:
2335 return _handle_fromlist(module, fromlist, _gcd_import) 2277 return _handle_fromlist(module, fromlist, _gcd_import)
2336 2278
2337 2279
2338 def _builtin_from_name(name): 2280 def _builtin_from_name(name):
2339 spec = BuiltinImporter._get_spec(name) 2281 spec = BuiltinImporter.find_spec(name)
2282 if spec is None:
2283 raise ImportError('no built-in module named ' + name)
2340 methods = _SpecMethods(spec) 2284 methods = _SpecMethods(spec)
2341 return methods._load_unlocked() 2285 return methods._load_unlocked()
2342 2286
2343 2287
2344 def _setup(sys_module, _imp_module): 2288 def _setup(sys_module, _imp_module):
2345 """Setup importlib by importing needed built-in modules and injecting them 2289 """Setup importlib by importing needed built-in modules and injecting them
2346 into the global namespace. 2290 into the global namespace.
2347 2291
2348 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
2349 modules, those two modules must be explicitly passed in. 2293 modules, those two modules must be explicitly passed in.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
2431 def _install(sys_module, _imp_module): 2375 def _install(sys_module, _imp_module):
2432 """Install importlib as the implementation of import.""" 2376 """Install importlib as the implementation of import."""
2433 _setup(sys_module, _imp_module) 2377 _setup(sys_module, _imp_module)
2434 supported_loaders = _get_supported_file_loaders() 2378 supported_loaders = _get_supported_file_loaders()
2435 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)]) 2379 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
2436 sys.meta_path.append(BuiltinImporter) 2380 sys.meta_path.append(BuiltinImporter)
2437 sys.meta_path.append(FrozenImporter) 2381 sys.meta_path.append(FrozenImporter)
2438 if _os.__name__ == 'nt': 2382 if _os.__name__ == 'nt':
2439 sys.meta_path.append(WindowsRegistryFinder) 2383 sys.meta_path.append(WindowsRegistryFinder)
2440 sys.meta_path.append(PathFinder) 2384 sys.meta_path.append(PathFinder)
LEFTRIGHT

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