| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | """Abstract base classes related to import.""" | 
					
						
							|  |  |  | from . import _bootstrap | 
					
						
							|  |  |  | from . import machinery | 
					
						
							| 
									
										
										
										
											2012-04-22 19:58:33 -04:00
										 |  |  | try: | 
					
						
							|  |  |  |     import _frozen_importlib | 
					
						
							|  |  |  | except ImportError as exc: | 
					
						
							|  |  |  |     if exc.name != '_frozen_importlib': | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  |     _frozen_importlib = None | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | import abc | 
					
						
							| 
									
										
										
										
											2010-06-27 23:57:46 +00:00
										 |  |  | import imp | 
					
						
							|  |  |  | import marshal | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import tokenize | 
					
						
							|  |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-22 19:58:33 -04:00
										 |  |  | def _register(abstract_cls, *classes): | 
					
						
							|  |  |  |     for cls in classes: | 
					
						
							|  |  |  |         abstract_cls.register(cls) | 
					
						
							|  |  |  |         if _frozen_importlib is not None: | 
					
						
							|  |  |  |             frozen_cls = getattr(_frozen_importlib, cls.__name__) | 
					
						
							|  |  |  |             abstract_cls.register(frozen_cls) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  | class Finder(metaclass=abc.ABCMeta): | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |     """Legacy abstract base class for import finders.
 | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |     It may be subclassed for compatibility with legacy third party | 
					
						
							|  |  |  |     reimplementations of the import system.  Otherwise, finder | 
					
						
							|  |  |  |     implementations should derive from the more specific MetaPathFinder | 
					
						
							|  |  |  |     or PathEntryFinder ABCs. | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |     def find_module(self, fullname, path=None): | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |         """An abstract method that should find a module.
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |         The fullname is a str and the optional path is a str or None. | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         Returns a Loader object or None. | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MetaPathFinder(Finder): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Abstract base class for import finders on sys.meta_path.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-31 16:39:43 -04:00
										 |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |     def find_module(self, fullname, path): | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |         """Abstract method which, when implemented, should find a module.
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         The fullname is a str and the path is a list of strings or None. | 
					
						
							|  |  |  |         Returns a Loader object or None. | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2012-07-29 16:36:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |     def invalidate_caches(self): | 
					
						
							|  |  |  |         """An optional method for clearing the finder's cache, if any.
 | 
					
						
							|  |  |  |         This method is used by importlib.invalidate_caches(). | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  | _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, | 
					
						
							| 
									
										
										
										
											2012-08-02 21:45:24 +10:00
										 |  |  |           machinery.PathFinder, machinery.WindowsRegistryFinder) | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  | class PathEntryFinder(Finder): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Abstract base class for path entry finders used by PathFinder.""" | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-31 16:39:43 -04:00
										 |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2012-07-29 16:36:17 -04:00
										 |  |  |     def find_loader(self, fullname): | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         """Abstract method which, when implemented, returns a module loader or
 | 
					
						
							|  |  |  |         a possible part of a namespace. | 
					
						
							| 
									
										
										
										
											2012-07-29 16:36:17 -04:00
										 |  |  |         The fullname is a str.  Returns a 2-tuple of (Loader, portion) where | 
					
						
							|  |  |  |         portion is a sequence of file system locations contributing to part of | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  |         a namespace package.  The sequence may be empty and the loader may be | 
					
						
							|  |  |  |         None. | 
					
						
							| 
									
										
										
										
											2012-07-29 16:36:17 -04:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         return None, [] | 
					
						
							| 
									
										
										
										
											2012-07-29 16:36:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-10 17:41:23 -04:00
										 |  |  |     find_module = _bootstrap._find_module_shim | 
					
						
							| 
									
										
										
										
											2012-08-10 12:21:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def invalidate_caches(self): | 
					
						
							|  |  |  |         """An optional method for clearing the finder's cache, if any.
 | 
					
						
							|  |  |  |         This method is used by PathFinder.invalidate_caches(). | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  | _register(PathEntryFinder, machinery.FileFinder) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Loader(metaclass=abc.ABCMeta): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |     """Abstract base class for import loaders.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The optional method module_repr(module) may be defined to provide a | 
					
						
							|  |  |  |     repr for a module when appropriate (see PEP 420). The __repr__() method on | 
					
						
							|  |  |  |     the module type will use the method as appropriate. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |     def load_module(self, fullname): | 
					
						
							|  |  |  |         """Abstract method which when implemented should load a module.
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         The fullname is a str. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ImportError is raised on failure. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise ImportError | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |     def module_repr(self, module): | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         """Return a module's repr.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Used by the module type when implemented without raising an exception. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2012-08-02 21:26:03 +10:00
										 |  |  |         raise NotImplementedError | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResourceLoader(Loader): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-15 00:53:05 +00:00
										 |  |  |     """Abstract base class for loaders which can return data from their
 | 
					
						
							|  |  |  |     back-end storage. | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     This ABC represents one of the optional protocols specified by PEP 302. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2011-01-13 02:31:25 +00:00
										 |  |  |     def get_data(self, path): | 
					
						
							| 
									
										
										
										
											2009-03-15 00:53:05 +00:00
										 |  |  |         """Abstract method which when implemented should return the bytes for
 | 
					
						
							| 
									
										
										
										
											2011-01-13 19:08:04 +00:00
										 |  |  |         the specified path.  The path must be a str."""
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         raise IOError | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class InspectLoader(Loader): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-15 00:53:05 +00:00
										 |  |  |     """Abstract base class for loaders which support inspection about the
 | 
					
						
							|  |  |  |     modules they can load. | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     This ABC represents one of the optional protocols specified by PEP 302. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2011-01-13 02:31:25 +00:00
										 |  |  |     def is_package(self, fullname): | 
					
						
							| 
									
										
										
										
											2009-03-15 00:53:05 +00:00
										 |  |  |         """Abstract method which when implemented should return whether the
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         module is a package.  The fullname is a str.  Returns a bool. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Raises ImportError is the module cannot be found. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise ImportError | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2011-01-13 02:31:25 +00:00
										 |  |  |     def get_code(self, fullname): | 
					
						
							| 
									
										
										
										
											2009-03-15 00:53:05 +00:00
										 |  |  |         """Abstract method which when implemented should return the code object
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         for the module.  The fullname is a str.  Returns a types.CodeType. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Raises ImportError if the module cannot be found. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise ImportError | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2011-01-13 02:31:25 +00:00
										 |  |  |     def get_source(self, fullname): | 
					
						
							| 
									
										
										
										
											2009-03-15 00:53:05 +00:00
										 |  |  |         """Abstract method which should return the source code for the
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         module.  The fullname is a str.  Returns a str. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Raises ImportError if the module cannot be found. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise ImportError | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-22 19:58:33 -04:00
										 |  |  | _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, | 
					
						
							|  |  |  |             machinery.ExtensionFileLoader) | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-20 04:23:48 +00:00
										 |  |  | class ExecutionLoader(InspectLoader): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Abstract base class for loaders that wish to support the execution of
 | 
					
						
							|  |  |  |     modules as scripts. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This ABC represents one of the optional protocols specified in PEP 302. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abc.abstractmethod | 
					
						
							| 
									
										
										
										
											2011-01-13 02:31:25 +00:00
										 |  |  |     def get_filename(self, fullname): | 
					
						
							| 
									
										
										
										
											2009-07-20 04:23:48 +00:00
										 |  |  |         """Abstract method which should return the value that __file__ is to be
 | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |         set to. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Raises ImportError if the module cannot be found. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         raise ImportError | 
					
						
							| 
									
										
										
										
											2009-07-20 04:23:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-22 19:58:33 -04:00
										 |  |  | class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Abstract base class partially implementing the ResourceLoader and
 | 
					
						
							|  |  |  |     ExecutionLoader ABCs."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _register(FileLoader, machinery.SourceFileLoader, | 
					
						
							| 
									
										
										
										
											2012-04-25 02:31:37 +02:00
										 |  |  |             machinery.SourcelessFileLoader) | 
					
						
							| 
									
										
										
										
											2012-04-22 19:58:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-28 04:57:24 +00:00
										 |  |  | class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader): | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-27 23:57:46 +00:00
										 |  |  |     """Abstract base class for loading source code (and optionally any
 | 
					
						
							|  |  |  |     corresponding bytecode). | 
					
						
							| 
									
										
										
										
											2009-03-09 03:35:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-27 23:57:46 +00:00
										 |  |  |     To support loading from source code, the abstractmethods inherited from | 
					
						
							|  |  |  |     ResourceLoader and ExecutionLoader need to be implemented. To also support | 
					
						
							|  |  |  |     loading from bytecode, the optional methods specified directly by this ABC | 
					
						
							|  |  |  |     is required. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Inherited abstractmethods not implemented in this ABC: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         * ResourceLoader.get_data | 
					
						
							|  |  |  |         * ExecutionLoader.get_filename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-13 02:31:25 +00:00
										 |  |  |     def path_mtime(self, path): | 
					
						
							| 
									
										
										
										
											2011-01-13 19:08:04 +00:00
										 |  |  |         """Return the (int) modification time for the path (str).""" | 
					
						
							| 
									
										
										
										
											2012-01-13 18:52:16 +01:00
										 |  |  |         if self.path_stats.__func__ is SourceLoader.path_stats: | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |             raise IOError | 
					
						
							| 
									
										
										
										
											2012-01-13 18:52:16 +01:00
										 |  |  |         return int(self.path_stats(path)['mtime']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def path_stats(self, path): | 
					
						
							|  |  |  |         """Return a metadata dict for the source pointed to by the path (str).
 | 
					
						
							|  |  |  |         Possible keys: | 
					
						
							|  |  |  |         - 'mtime' (mandatory) is the numeric timestamp of last source | 
					
						
							|  |  |  |           code modification; | 
					
						
							|  |  |  |         - 'size' (optional) is the size in bytes of the source code. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if self.path_mtime.__func__ is SourceLoader.path_mtime: | 
					
						
							| 
									
										
										
										
											2013-04-09 16:59:39 -04:00
										 |  |  |             raise IOError | 
					
						
							| 
									
										
										
										
											2012-01-13 18:52:16 +01:00
										 |  |  |         return {'mtime': self.path_mtime(path)} | 
					
						
							| 
									
										
										
										
											2010-08-22 20:38:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-13 02:31:25 +00:00
										 |  |  |     def set_data(self, path, data): | 
					
						
							| 
									
										
										
										
											2010-08-22 20:38:47 +00:00
										 |  |  |         """Write the bytes to the path (if possible).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-13 19:08:04 +00:00
										 |  |  |         Accepts a str path and data as bytes. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-22 20:38:47 +00:00
										 |  |  |         Any needed intermediary directories are to be created. If for some | 
					
						
							|  |  |  |         reason the file cannot be written because of permissions, fail | 
					
						
							|  |  |  |         silently. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-22 19:58:33 -04:00
										 |  |  | _register(SourceLoader, machinery.SourceFileLoader) |