2009-03-09 00:35:50 -03:00
|
|
|
"""Abstract base classes related to import."""
|
|
|
|
from . import _bootstrap
|
|
|
|
from . import machinery
|
2012-04-22 20:58:33 -03:00
|
|
|
try:
|
|
|
|
import _frozen_importlib
|
|
|
|
except ImportError as exc:
|
|
|
|
if exc.name != '_frozen_importlib':
|
|
|
|
raise
|
|
|
|
_frozen_importlib = None
|
2009-03-09 00:35:50 -03:00
|
|
|
import abc
|
2010-06-27 20:57:46 -03:00
|
|
|
import imp
|
|
|
|
import marshal
|
|
|
|
import sys
|
|
|
|
import tokenize
|
|
|
|
import warnings
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
|
2012-04-22 20:58:33 -03: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)
|
|
|
|
|
|
|
|
|
2009-03-09 00:35:50 -03:00
|
|
|
class Loader(metaclass=abc.ABCMeta):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for import loaders."""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def load_module(self, fullname):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Abstract method which when implemented should load a module.
|
|
|
|
The fullname is a str."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class Finder(metaclass=abc.ABCMeta):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for import finders."""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def find_module(self, fullname, path=None):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Abstract method which when implemented should find a module.
|
|
|
|
The fullname is a str and the optional path is a str or None.
|
|
|
|
Returns a Loader object.
|
|
|
|
"""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-04-22 20:58:33 -03:00
|
|
|
_register(Finder, machinery.BuiltinImporter, machinery.FrozenImporter,
|
|
|
|
machinery.PathFinder, machinery.FileFinder)
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
class ResourceLoader(Loader):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for loaders which can return data from their
|
|
|
|
back-end storage.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
This ABC represents one of the optional protocols specified by PEP 302.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def get_data(self, path):
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the bytes for
|
2011-01-13 15:08:04 -04:00
|
|
|
the specified path. The path must be a str."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class InspectLoader(Loader):
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class for loaders which support inspection about the
|
|
|
|
modules they can load.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
This ABC represents one of the optional protocols specified by PEP 302.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def is_package(self, fullname):
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return whether the
|
2011-01-13 15:08:04 -04:00
|
|
|
module is a package. The fullname is a str. Returns a bool."""
|
2010-06-27 20:57:46 -03:00
|
|
|
raise NotImplementedError
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def get_code(self, fullname):
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which when implemented should return the code object
|
2011-01-13 15:08:04 -04:00
|
|
|
for the module. The fullname is a str. Returns a types.CodeType."""
|
2010-06-27 20:57:46 -03:00
|
|
|
raise NotImplementedError
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def get_source(self, fullname):
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract method which should return the source code for the
|
2011-01-13 15:08:04 -04:00
|
|
|
module. The fullname is a str. Returns a str."""
|
2010-06-27 20:57:46 -03:00
|
|
|
raise NotImplementedError
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2012-04-22 20:58:33 -03:00
|
|
|
_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter,
|
|
|
|
machinery.ExtensionFileLoader)
|
2009-03-14 22:41:33 -03:00
|
|
|
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-07-20 01:23:48 -03: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-12 22:31:25 -04:00
|
|
|
def get_filename(self, fullname):
|
2009-07-20 01:23:48 -03:00
|
|
|
"""Abstract method which should return the value that __file__ is to be
|
|
|
|
set to."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2012-04-22 20:58:33 -03:00
|
|
|
class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):
|
|
|
|
|
|
|
|
"""Abstract base class partially implementing the ResourceLoader and
|
|
|
|
ExecutionLoader ABCs."""
|
|
|
|
|
|
|
|
_register(FileLoader, machinery.SourceFileLoader,
|
2012-04-24 21:31:37 -03:00
|
|
|
machinery.SourcelessFileLoader)
|
2012-04-22 20:58:33 -03:00
|
|
|
|
|
|
|
|
2010-06-28 01:57:24 -03:00
|
|
|
class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
"""Abstract base class for loading source code (and optionally any
|
|
|
|
corresponding bytecode).
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2010-06-27 20:57:46 -03: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-12 22:31:25 -04:00
|
|
|
def path_mtime(self, path):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Return the (int) modification time for the path (str)."""
|
2012-01-13 13:52:16 -04:00
|
|
|
if self.path_stats.__func__ is SourceLoader.path_stats:
|
|
|
|
raise NotImplementedError
|
|
|
|
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:
|
|
|
|
raise NotImplementedError
|
|
|
|
return {'mtime': self.path_mtime(path)}
|
2010-08-22 17:38:47 -03:00
|
|
|
|
2011-01-12 22:31:25 -04:00
|
|
|
def set_data(self, path, data):
|
2010-08-22 17:38:47 -03:00
|
|
|
"""Write the bytes to the path (if possible).
|
|
|
|
|
2011-01-13 15:08:04 -04:00
|
|
|
Accepts a str path and data as bytes.
|
|
|
|
|
2010-08-22 17:38:47 -03:00
|
|
|
Any needed intermediary directories are to be created. If for some
|
|
|
|
reason the file cannot be written because of permissions, fail
|
|
|
|
silently.
|
|
|
|
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-04-22 20:58:33 -03:00
|
|
|
_register(SourceLoader, machinery.SourceFileLoader)
|
2010-06-27 20:57:46 -03:00
|
|
|
|
|
|
|
class PyLoader(SourceLoader):
|
|
|
|
|
|
|
|
"""Implement the deprecated PyLoader ABC in terms of SourceLoader.
|
|
|
|
|
|
|
|
This class has been deprecated! It is slated for removal in Python 3.4.
|
|
|
|
If compatibility with Python 3.1 is not needed then implement the
|
|
|
|
SourceLoader ABC instead of this class. If Python 3.1 compatibility is
|
|
|
|
needed, then use the following idiom to have a single class that is
|
|
|
|
compatible with Python 3.1 onwards::
|
|
|
|
|
|
|
|
try:
|
|
|
|
from importlib.abc import SourceLoader
|
|
|
|
except ImportError:
|
|
|
|
from importlib.abc import PyLoader as SourceLoader
|
|
|
|
|
|
|
|
|
|
|
|
class CustomLoader(SourceLoader):
|
|
|
|
def get_filename(self, fullname):
|
|
|
|
# Implement ...
|
|
|
|
|
|
|
|
def source_path(self, fullname):
|
|
|
|
'''Implement source_path in terms of get_filename.'''
|
|
|
|
try:
|
|
|
|
return self.get_filename(fullname)
|
|
|
|
except ImportError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def is_package(self, fullname):
|
|
|
|
filename = os.path.basename(self.get_filename(fullname))
|
|
|
|
return os.path.splitext(filename)[0] == '__init__'
|
2009-03-14 21:53:05 -03:00
|
|
|
|
|
|
|
"""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
@abc.abstractmethod
|
|
|
|
def is_package(self, fullname):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2009-03-09 00:35:50 -03:00
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def source_path(self, fullname):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Abstract method. Accepts a str module name and returns the path to
|
|
|
|
the source code for the module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
def get_filename(self, fullname):
|
|
|
|
"""Implement get_filename in terms of source_path.
|
|
|
|
|
|
|
|
As get_filename should only return a source file path there is no
|
|
|
|
chance of the path not existing but loading still being possible, so
|
|
|
|
ImportError should propagate instead of being turned into returning
|
|
|
|
None.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
"""
|
|
|
|
warnings.warn("importlib.abc.PyLoader is deprecated and is "
|
|
|
|
"slated for removal in Python 3.4; "
|
|
|
|
"use SourceLoader instead. "
|
|
|
|
"See the importlib documentation on how to be "
|
|
|
|
"compatible with Python 3.1 onwards.",
|
2011-12-10 06:07:42 -04:00
|
|
|
DeprecationWarning)
|
2010-06-27 20:57:46 -03:00
|
|
|
path = self.source_path(fullname)
|
|
|
|
if path is None:
|
2012-04-12 22:09:01 -03:00
|
|
|
raise ImportError(name=fullname)
|
2010-06-27 20:57:46 -03:00
|
|
|
else:
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
class PyPycLoader(PyLoader):
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""Abstract base class to assist in loading source and bytecode by
|
|
|
|
requiring only back-end storage methods to be implemented.
|
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
This class has been deprecated! Removal is slated for Python 3.4. Implement
|
|
|
|
the SourceLoader ABC instead. If Python 3.1 compatibility is needed, see
|
|
|
|
PyLoader.
|
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
The methods get_code, get_source, and load_module are implemented for the
|
|
|
|
user.
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-03-14 21:53:05 -03:00
|
|
|
"""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2010-06-27 20:57:46 -03:00
|
|
|
def get_filename(self, fullname):
|
|
|
|
"""Return the source or bytecode file path."""
|
|
|
|
path = self.source_path(fullname)
|
|
|
|
if path is not None:
|
|
|
|
return path
|
|
|
|
path = self.bytecode_path(fullname)
|
|
|
|
if path is not None:
|
|
|
|
return path
|
|
|
|
raise ImportError("no source or bytecode path available for "
|
2012-04-12 22:09:01 -03:00
|
|
|
"{0!r}".format(fullname), name=fullname)
|
2010-06-27 20:57:46 -03:00
|
|
|
|
|
|
|
def get_code(self, fullname):
|
|
|
|
"""Get a code object from source or bytecode."""
|
|
|
|
warnings.warn("importlib.abc.PyPycLoader is deprecated and slated for "
|
|
|
|
"removal in Python 3.4; use SourceLoader instead. "
|
|
|
|
"If Python 3.1 compatibility is required, see the "
|
|
|
|
"latest documentation for PyLoader.",
|
2011-12-10 06:07:42 -04:00
|
|
|
DeprecationWarning)
|
2010-06-27 20:57:46 -03:00
|
|
|
source_timestamp = self.source_mtime(fullname)
|
|
|
|
# Try to use bytecode if it is available.
|
|
|
|
bytecode_path = self.bytecode_path(fullname)
|
|
|
|
if bytecode_path:
|
|
|
|
data = self.get_data(bytecode_path)
|
|
|
|
try:
|
|
|
|
magic = data[:4]
|
|
|
|
if len(magic) < 4:
|
2012-02-25 01:48:17 -04:00
|
|
|
raise ImportError(
|
2012-04-12 22:09:01 -03:00
|
|
|
"bad magic number in {}".format(fullname),
|
|
|
|
name=fullname, path=bytecode_path)
|
2010-06-27 20:57:46 -03:00
|
|
|
raw_timestamp = data[4:8]
|
|
|
|
if len(raw_timestamp) < 4:
|
|
|
|
raise EOFError("bad timestamp in {}".format(fullname))
|
2012-01-25 19:58:03 -04:00
|
|
|
pyc_timestamp = _bootstrap._r_long(raw_timestamp)
|
2010-06-27 20:57:46 -03:00
|
|
|
bytecode = data[8:]
|
|
|
|
# Verify that the magic number is valid.
|
|
|
|
if imp.get_magic() != magic:
|
2012-02-25 01:48:17 -04:00
|
|
|
raise ImportError(
|
2012-04-12 22:09:01 -03:00
|
|
|
"bad magic number in {}".format(fullname),
|
|
|
|
name=fullname, path=bytecode_path)
|
2010-06-27 20:57:46 -03:00
|
|
|
# Verify that the bytecode is not stale (only matters when
|
|
|
|
# there is source to fall back on.
|
|
|
|
if source_timestamp:
|
|
|
|
if pyc_timestamp < source_timestamp:
|
2012-04-12 22:09:01 -03:00
|
|
|
raise ImportError("bytecode is stale", name=fullname,
|
|
|
|
path=bytecode_path)
|
2010-06-27 20:57:46 -03:00
|
|
|
except (ImportError, EOFError):
|
|
|
|
# If source is available give it a shot.
|
|
|
|
if source_timestamp is not None:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
# Bytecode seems fine, so try to use it.
|
|
|
|
return marshal.loads(bytecode)
|
|
|
|
elif source_timestamp is None:
|
|
|
|
raise ImportError("no source or bytecode available to create code "
|
2012-04-12 22:09:01 -03:00
|
|
|
"object for {0!r}".format(fullname),
|
|
|
|
name=fullname)
|
2010-06-27 20:57:46 -03:00
|
|
|
# Use the source.
|
|
|
|
source_path = self.source_path(fullname)
|
|
|
|
if source_path is None:
|
|
|
|
message = "a source path must exist to load {0}".format(fullname)
|
2012-04-12 22:09:01 -03:00
|
|
|
raise ImportError(message, name=fullname)
|
2010-06-27 20:57:46 -03:00
|
|
|
source = self.get_data(source_path)
|
|
|
|
code_object = compile(source, source_path, 'exec', dont_inherit=True)
|
|
|
|
# Generate bytecode and write it out.
|
|
|
|
if not sys.dont_write_bytecode:
|
|
|
|
data = bytearray(imp.get_magic())
|
2012-01-25 19:58:03 -04:00
|
|
|
data.extend(_bootstrap._w_long(source_timestamp))
|
2010-06-27 20:57:46 -03:00
|
|
|
data.extend(marshal.dumps(code_object))
|
|
|
|
self.write_bytecode(fullname, data)
|
|
|
|
return code_object
|
|
|
|
|
2009-03-09 00:35:50 -03:00
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def source_mtime(self, fullname):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Abstract method. Accepts a str filename and returns an int
|
2009-03-14 21:53:05 -03:00
|
|
|
modification time for the source of the module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def bytecode_path(self, fullname):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Abstract method. Accepts a str filename and returns the str pathname
|
|
|
|
to the bytecode for the module."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2011-01-12 22:31:25 -04:00
|
|
|
def write_bytecode(self, fullname, bytecode):
|
2011-01-13 15:08:04 -04:00
|
|
|
"""Abstract method. Accepts a str filename and bytes object
|
|
|
|
representing the bytecode for the module. Returns a boolean
|
|
|
|
representing whether the bytecode was written or not."""
|
2009-03-09 00:35:50 -03:00
|
|
|
raise NotImplementedError
|