"""Abstract base classes related to import."""from . import _bootstrapfrom . import _bootstrap_externalfrom . import machinerytry:import _frozen_importlibexcept ImportError as exc:if exc.name != '_frozen_importlib':raise_frozen_importlib = Nonetry:import _frozen_importlib_externalexcept ImportError as exc:_frozen_importlib_external = _bootstrap_externalimport abcimport warningsdef _register(abstract_cls, *classes):for cls in classes:abstract_cls.register(cls)if _frozen_importlib is not None:try:frozen_cls = getattr(_frozen_importlib, cls.__name__)except AttributeError:frozen_cls = getattr(_frozen_importlib_external, cls.__name__)abstract_cls.register(frozen_cls)class Finder(metaclass=abc.ABCMeta):"""Legacy abstract base class for import finders.It may be subclassed for compatibility with legacy third partyreimplementations of the import system. Otherwise, finderimplementations should derive from the more specific MetaPathFinderor PathEntryFinder ABCs.Deprecated since Python 3.3"""@abc.abstractmethoddef find_module(self, fullname, path=None):"""An abstract method that should find a module.The fullname is a str and the optional path is a str or None.Returns a Loader object or None."""class MetaPathFinder(Finder):"""Abstract base class for import finders on sys.meta_path."""# We don't define find_spec() here since that would break# hasattr checks we do to support backward compatibility.def find_module(self, fullname, path):"""Return a loader for the module.If no module is found, return None. The fullname is a str andthe path is a list of strings or None.This method is deprecated since Python 3.4 in favor offinder.find_spec(). If find_spec() exists then backwards-compatiblefunctionality is provided for this method."""warnings.warn("MetaPathFinder.find_module() is deprecated since Python ""3.4 in favor of MetaPathFinder.find_spec() ""(available since 3.4)",DeprecationWarning,stacklevel=2)if not hasattr(self, 'find_spec'):return Nonefound = self.find_spec(fullname, path)return found.loader if found is not None else Nonedef invalidate_caches(self):"""An optional method for clearing the finder's cache, if any.This method is used by importlib.invalidate_caches()."""_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,machinery.PathFinder, machinery.WindowsRegistryFinder)class PathEntryFinder(Finder):"""Abstract base class for path entry finders used by PathFinder."""# We don't define find_spec() here since that would break# hasattr checks we do to support backward compatibility.def find_loader(self, fullname):"""Return (loader, namespace portion) for the path entry.The fullname is a str. The namespace portion is a sequence ofpath entries contributing to part of a namespace package. Thesequence may be empty. If loader is not None, the portion willbe ignored.The portion will be discarded if another path entry finderlocates the module as a normal module or package.This method is deprecated since Python 3.4 in favor offinder.find_spec(). If find_spec() is provided than backwards-compatiblefunctionality is provided."""warnings.warn("PathEntryFinder.find_loader() is deprecated since Python ""3.4 in favor of PathEntryFinder.find_spec() ""(available since 3.4)",DeprecationWarning,stacklevel=2)if not hasattr(self, 'find_spec'):return None, []found = self.find_spec(fullname)if found is not None:if not found.submodule_search_locations:portions = []else:portions = found.submodule_search_locationsreturn found.loader, portionselse:return None, []find_module = _bootstrap_external._find_module_shimdef invalidate_caches(self):"""An optional method for clearing the finder's cache, if any.This method is used by PathFinder.invalidate_caches()."""_register(PathEntryFinder, machinery.FileFinder)class Loader(metaclass=abc.ABCMeta):"""Abstract base class for import loaders."""def create_module(self, spec):"""Return a module to initialize and into which to load.This method should raise ImportError if anything prevents itfrom creating a new module. It may return None to indicatethat the spec should create the new module."""# By default, defer to default semantics for the new module.return None# We don't define exec_module() here since that would break# hasattr checks we do to support backward compatibility.def load_module(self, fullname):"""Return the loaded module.The module must be added to sys.modules and have import-relatedattributes set properly. The fullname is a str.ImportError is raised on failure.This method is deprecated in favor of loader.exec_module(). Ifexec_module() exists then it is used to provide a backwards-compatiblefunctionality for this method."""if not hasattr(self, 'exec_module'):raise ImportErrorreturn _bootstrap._load_module_shim(self, fullname)def module_repr(self, module):"""Return a module's repr.Used by the module type when the method does not raiseNotImplementedError.This method is deprecated."""# The exception will cause ModuleType.__repr__ to ignore this method.raise NotImplementedErrorclass ResourceLoader(Loader):"""Abstract base class for loaders which can return data from theirback-end storage.This ABC represents one of the optional protocols specified by PEP 302."""@abc.abstractmethoddef get_data(self, path):"""Abstract method which when implemented should return the bytes forthe specified path. The path must be a str."""raise OSErrorclass InspectLoader(Loader):"""Abstract base class for loaders which support inspection about themodules they can load.This ABC represents one of the optional protocols specified by PEP 302."""def is_package(self, fullname):"""Optional method which when implemented should return whether themodule is a package. The fullname is a str. Returns a bool.Raises ImportError if the module cannot be found."""raise ImportErrordef get_code(self, fullname):"""Method which returns the code object for the module.The fullname is a str. Returns a types.CodeType if possible, elsereturns None if a code object does not make sense(e.g. built-in module). Raises ImportError if the module cannot befound."""source = self.get_source(fullname)if source is None:return Nonereturn self.source_to_code(source)@abc.abstractmethoddef get_source(self, fullname):"""Abstract method which should return the source code for themodule. The fullname is a str. Returns a str.Raises ImportError if the module cannot be found."""raise ImportError@staticmethoddef source_to_code(data, path='<string>'):"""Compile 'data' into a code object.The 'data' argument can be anything that compile() can handle. The'path'argument should be where the data was retrieved (when applicable)."""return compile(data, path, 'exec', dont_inherit=True)exec_module = _bootstrap_external._LoaderBasics.exec_moduleload_module = _bootstrap_external._LoaderBasics.load_module_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)class ExecutionLoader(InspectLoader):"""Abstract base class for loaders that wish to support the execution ofmodules as scripts.This ABC represents one of the optional protocols specified in PEP 302."""@abc.abstractmethoddef get_filename(self, fullname):"""Abstract method which should return the value that __file__ is to beset to.Raises ImportError if the module cannot be found."""raise ImportErrordef get_code(self, fullname):"""Method to return the code object for fullname.Should return None if not applicable (e.g. built-in module).Raise ImportError if the module cannot be found."""source = self.get_source(fullname)if source is None:return Nonetry:path = self.get_filename(fullname)except ImportError:return self.source_to_code(source)else:return self.source_to_code(source, path)_register(ExecutionLoader, machinery.ExtensionFileLoader)class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader):"""Abstract base class partially implementing the ResourceLoader andExecutionLoader ABCs."""_register(FileLoader, machinery.SourceFileLoader,machinery.SourcelessFileLoader)class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader):"""Abstract base class for loading source code (and optionally anycorresponding bytecode).To support loading from source code, the abstractmethods inherited fromResourceLoader and ExecutionLoader need to be implemented. To also supportloading from bytecode, the optional methods specified directly by this ABCis required.Inherited abstractmethods not implemented in this ABC:* ResourceLoader.get_data* ExecutionLoader.get_filename"""def path_mtime(self, path):"""Return the (int) modification time for the path (str)."""if self.path_stats.__func__ is SourceLoader.path_stats:raise OSErrorreturn 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 sourcecode modification;- 'size' (optional) is the size in bytes of the source code."""if self.path_mtime.__func__ is SourceLoader.path_mtime:raise OSErrorreturn {'mtime': self.path_mtime(path)}def set_data(self, path, data):"""Write the bytes to the path (if possible).Accepts a str path and data as bytes.Any needed intermediary directories are to be created. If for somereason the file cannot be written because of permissions, failsilently."""_register(SourceLoader, machinery.SourceFileLoader)class ResourceReader(metaclass=abc.ABCMeta):"""Abstract base class to provide resource-reading support.Loaders that support resource reading are expected to implementthe ``get_resource_reader(fullname)`` method and have it either return Noneor an object compatible with this ABC."""@abc.abstractmethoddef open_resource(self, resource):"""Return an opened, file-like object for binary reading.The 'resource' argument is expected to represent only a file nameand thus not contain any subdirectory components.If the resource cannot be found, FileNotFoundError is raised."""raise FileNotFoundError@abc.abstractmethoddef resource_path(self, resource):"""Return the file system path to the specified resource.The 'resource' argument is expected to represent only a file nameand thus not contain any subdirectory components.If the resource does not exist on the file system, raiseFileNotFoundError."""raise FileNotFoundError@abc.abstractmethoddef is_resource(self, name):"""Return True if the named 'name' is consider a resource."""raise FileNotFoundError@abc.abstractmethoddef contents(self):"""Return an iterable of strings over the contents of the package."""return []_register(ResourceReader, machinery.SourceFileLoader)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。