Back to index

python3.2  3.2.2
Public Member Functions
importlib.abc.PyPycLoader Class Reference
Inheritance diagram for importlib.abc.PyPycLoader:
Inheritance graph
[legend]
Collaboration diagram for importlib.abc.PyPycLoader:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def get_filename
def get_code
def source_mtime
def bytecode_path
def write_bytecode
def is_package
def source_path
def path_mtime
def set_data
def get_source
def get_source
def load_module
def load_module
def load_module
def get_data
def __new__
def __new__
def register
def register
def __instancecheck__
def __instancecheck__
def __subclasscheck__
def __subclasscheck__

Detailed Description

Abstract base class to assist in loading source and bytecode by
requiring only back-end storage methods to be implemented.

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.

The methods get_code, get_source, and load_module are implemented for the
user.

Definition at line 206 of file abc.py.


Member Function Documentation

def abc.ABCMeta.__instancecheck__ (   cls,
  instance 
) [inherited]
Override for isinstance(instance, cls).

Definition at line 158 of file abc.py.

00158 
00159     def __instancecheck__(cls, instance):
00160         """Override for isinstance(instance, cls)."""
00161         # Inline the cache checking
00162         subclass = instance.__class__
00163         if subclass in cls._abc_cache:
00164             return True
00165         subtype = type(instance)
00166         if subtype is subclass:
00167             if (cls._abc_negative_cache_version ==
00168                 ABCMeta._abc_invalidation_counter and
00169                 subclass in cls._abc_negative_cache):
00170                 return False
00171             # Fall back to the subclass check.
00172             return cls.__subclasscheck__(subclass)
00173         return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

Here is the call graph for this function:

def abc.ABCMeta.__instancecheck__ (   cls,
  instance 
) [inherited]
Override for isinstance(instance, cls).

Definition at line 158 of file abc.py.

00158 
00159     def __instancecheck__(cls, instance):
00160         """Override for isinstance(instance, cls)."""
00161         # Inline the cache checking
00162         subclass = instance.__class__
00163         if subclass in cls._abc_cache:
00164             return True
00165         subtype = type(instance)
00166         if subtype is subclass:
00167             if (cls._abc_negative_cache_version ==
00168                 ABCMeta._abc_invalidation_counter and
00169                 subclass in cls._abc_negative_cache):
00170                 return False
00171             # Fall back to the subclass check.
00172             return cls.__subclasscheck__(subclass)
00173         return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

Here is the call graph for this function:

def abc.ABCMeta.__new__ (   mcls,
  name,
  bases,
  namespace 
) [inherited]

Definition at line 116 of file abc.py.

00116 
00117     def __new__(mcls, name, bases, namespace):
00118         cls = super().__new__(mcls, name, bases, namespace)
00119         # Compute set of abstract method names
00120         abstracts = {name
00121                      for name, value in namespace.items()
00122                      if getattr(value, "__isabstractmethod__", False)}
00123         for base in bases:
00124             for name in getattr(base, "__abstractmethods__", set()):
00125                 value = getattr(cls, name, None)
00126                 if getattr(value, "__isabstractmethod__", False):
00127                     abstracts.add(name)
00128         cls.__abstractmethods__ = frozenset(abstracts)
00129         # Set up inheritance registry
00130         cls._abc_registry = WeakSet()
00131         cls._abc_cache = WeakSet()
00132         cls._abc_negative_cache = WeakSet()
00133         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00134         return cls

Here is the call graph for this function:

def abc.ABCMeta.__new__ (   mcls,
  name,
  bases,
  namespace 
) [inherited]

Definition at line 116 of file abc.py.

00116 
00117     def __new__(mcls, name, bases, namespace):
00118         cls = super().__new__(mcls, name, bases, namespace)
00119         # Compute set of abstract method names
00120         abstracts = {name
00121                      for name, value in namespace.items()
00122                      if getattr(value, "__isabstractmethod__", False)}
00123         for base in bases:
00124             for name in getattr(base, "__abstractmethods__", set()):
00125                 value = getattr(cls, name, None)
00126                 if getattr(value, "__isabstractmethod__", False):
00127                     abstracts.add(name)
00128         cls.__abstractmethods__ = frozenset(abstracts)
00129         # Set up inheritance registry
00130         cls._abc_registry = WeakSet()
00131         cls._abc_cache = WeakSet()
00132         cls._abc_negative_cache = WeakSet()
00133         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00134         return cls

Here is the call graph for this function:

def abc.ABCMeta.__subclasscheck__ (   cls,
  subclass 
) [inherited]
Override for issubclass(subclass, cls).

Definition at line 174 of file abc.py.

00174 
00175     def __subclasscheck__(cls, subclass):
00176         """Override for issubclass(subclass, cls)."""
00177         # Check cache
00178         if subclass in cls._abc_cache:
00179             return True
00180         # Check negative cache; may have to invalidate
00181         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
00182             # Invalidate the negative cache
00183             cls._abc_negative_cache = WeakSet()
00184             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00185         elif subclass in cls._abc_negative_cache:
00186             return False
00187         # Check the subclass hook
00188         ok = cls.__subclasshook__(subclass)
00189         if ok is not NotImplemented:
00190             assert isinstance(ok, bool)
00191             if ok:
00192                 cls._abc_cache.add(subclass)
00193             else:
00194                 cls._abc_negative_cache.add(subclass)
00195             return ok
00196         # Check if it's a direct subclass
00197         if cls in getattr(subclass, '__mro__', ()):
00198             cls._abc_cache.add(subclass)
00199             return True
00200         # Check if it's a subclass of a registered class (recursive)
00201         for rcls in cls._abc_registry:
00202             if issubclass(subclass, rcls):
00203                 cls._abc_cache.add(subclass)
00204                 return True
00205         # Check if it's a subclass of a subclass (recursive)
00206         for scls in cls.__subclasses__():
00207             if issubclass(subclass, scls):
00208                 cls._abc_cache.add(subclass)
00209                 return True
00210         # No dice; update negative cache
00211         cls._abc_negative_cache.add(subclass)
00212         return False

Here is the call graph for this function:

def abc.ABCMeta.__subclasscheck__ (   cls,
  subclass 
) [inherited]
Override for issubclass(subclass, cls).

Definition at line 174 of file abc.py.

00174 
00175     def __subclasscheck__(cls, subclass):
00176         """Override for issubclass(subclass, cls)."""
00177         # Check cache
00178         if subclass in cls._abc_cache:
00179             return True
00180         # Check negative cache; may have to invalidate
00181         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
00182             # Invalidate the negative cache
00183             cls._abc_negative_cache = WeakSet()
00184             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
00185         elif subclass in cls._abc_negative_cache:
00186             return False
00187         # Check the subclass hook
00188         ok = cls.__subclasshook__(subclass)
00189         if ok is not NotImplemented:
00190             assert isinstance(ok, bool)
00191             if ok:
00192                 cls._abc_cache.add(subclass)
00193             else:
00194                 cls._abc_negative_cache.add(subclass)
00195             return ok
00196         # Check if it's a direct subclass
00197         if cls in getattr(subclass, '__mro__', ()):
00198             cls._abc_cache.add(subclass)
00199             return True
00200         # Check if it's a subclass of a registered class (recursive)
00201         for rcls in cls._abc_registry:
00202             if issubclass(subclass, rcls):
00203                 cls._abc_cache.add(subclass)
00204                 return True
00205         # Check if it's a subclass of a subclass (recursive)
00206         for scls in cls.__subclasses__():
00207             if issubclass(subclass, scls):
00208                 cls._abc_cache.add(subclass)
00209                 return True
00210         # No dice; update negative cache
00211         cls._abc_negative_cache.add(subclass)
00212         return False

Here is the call graph for this function:

def importlib.abc.PyPycLoader.bytecode_path (   self,
  fullname 
)
Abstract method. Accepts a str filename and returns the str pathname
to the bytecode for the module.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.PyPycLoader, and importlib.test.source.test_abc_loader.PyPycLoaderMock.

Definition at line 294 of file abc.py.

00294 
00295     def bytecode_path(self, fullname):
00296         """Abstract method. Accepts a str filename and returns the str pathname
00297         to the bytecode for the module."""
00298         raise NotImplementedError

Here is the caller graph for this function:

def importlib.abc.PyPycLoader.get_code (   self,
  fullname 
)
Get a code object from source or bytecode.

Reimplemented from importlib._bootstrap.SourceLoader.

Reimplemented in importlib.test.source.test_abc_loader.PyPycLoaderMock.

Definition at line 231 of file abc.py.

00231 
00232     def get_code(self, fullname):
00233         """Get a code object from source or bytecode."""
00234         warnings.warn("importlib.abc.PyPycLoader is deprecated and slated for "
00235                             "removal in Python 3.4; use SourceLoader instead. "
00236                             "If Python 3.1 compatibility is required, see the "
00237                             "latest documentation for PyLoader.",
00238                         PendingDeprecationWarning)
00239         source_timestamp = self.source_mtime(fullname)
00240         # Try to use bytecode if it is available.
00241         bytecode_path = self.bytecode_path(fullname)
00242         if bytecode_path:
00243             data = self.get_data(bytecode_path)
00244             try:
00245                 magic = data[:4]
00246                 if len(magic) < 4:
00247                     raise ImportError("bad magic number in {}".format(fullname))
00248                 raw_timestamp = data[4:8]
00249                 if len(raw_timestamp) < 4:
00250                     raise EOFError("bad timestamp in {}".format(fullname))
00251                 pyc_timestamp = marshal._r_long(raw_timestamp)
00252                 bytecode = data[8:]
00253                 # Verify that the magic number is valid.
00254                 if imp.get_magic() != magic:
00255                     raise ImportError("bad magic number in {}".format(fullname))
00256                 # Verify that the bytecode is not stale (only matters when
00257                 # there is source to fall back on.
00258                 if source_timestamp:
00259                     if pyc_timestamp < source_timestamp:
00260                         raise ImportError("bytecode is stale")
00261             except (ImportError, EOFError):
00262                 # If source is available give it a shot.
00263                 if source_timestamp is not None:
00264                     pass
00265                 else:
00266                     raise
00267             else:
00268                 # Bytecode seems fine, so try to use it.
00269                 return marshal.loads(bytecode)
00270         elif source_timestamp is None:
00271             raise ImportError("no source or bytecode available to create code "
00272                                 "object for {0!r}".format(fullname))
00273         # Use the source.
00274         source_path = self.source_path(fullname)
00275         if source_path is None:
00276             message = "a source path must exist to load {0}".format(fullname)
00277             raise ImportError(message)
00278         source = self.get_data(source_path)
00279         code_object = compile(source, source_path, 'exec', dont_inherit=True)
00280         # Generate bytecode and write it out.
00281         if not sys.dont_write_bytecode:
00282             data = bytearray(imp.get_magic())
00283             data.extend(marshal._w_long(source_timestamp))
00284             data.extend(marshal.dumps(code_object))
00285             self.write_bytecode(fullname, data)
00286         return code_object

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib.abc.ResourceLoader.get_data (   self,
  path 
) [inherited]
Abstract method which when implemented should return the bytes for
the specified path.  The path must be a str.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.ResourceLoader, importlib.test.source.test_abc_loader.PyPycLoaderMock, importlib.test.source.test_abc_loader.PyLoaderMock, importlib.test.source.test_abc_loader.SourceLoaderMock, and importlib.test.source.test_abc_loader.SourceOnlyLoaderMock.

Definition at line 54 of file abc.py.

00054 
00055     def get_data(self, path):
00056         """Abstract method which when implemented should return the bytes for
00057         the specified path.  The path must be a str."""
00058         raise NotImplementedError
00059 

Here is the caller graph for this function:

def importlib.abc.PyPycLoader.get_filename (   self,
  fullname 
)
Return the source or bytecode file path.

Reimplemented from importlib.abc.PyLoader.

Definition at line 220 of file abc.py.

00220 
00221     def get_filename(self, fullname):
00222         """Return the source or bytecode file path."""
00223         path = self.source_path(fullname)
00224         if path is not None:
00225             return path
00226         path = self.bytecode_path(fullname)
00227         if path is not None:
00228             return path
00229         raise ImportError("no source or bytecode path available for "
00230                             "{0!r}".format(fullname))

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib.abc.InspectLoader.get_source (   self,
  fullname 
) [inherited]
Abstract method which should return the source code for the
module.  The fullname is a str.  Returns a str.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.InspectLoader.

Definition at line 82 of file abc.py.

00082 
00083     def get_source(self, fullname):
00084         """Abstract method which should return the source code for the
00085         module.  The fullname is a str.  Returns a str."""
00086         raise NotImplementedError
00087 
00088 InspectLoader.register(machinery.BuiltinImporter)
00089 InspectLoader.register(machinery.FrozenImporter)
00090 

Here is the caller graph for this function:

def importlib._bootstrap.SourceLoader.get_source (   self,
  fullname 
) [inherited]
Concrete implementation of InspectLoader.get_source.

Definition at line 366 of file _bootstrap.py.

00366 
00367     def get_source(self, fullname):
00368         """Concrete implementation of InspectLoader.get_source."""
00369         import tokenize
00370         path = self.get_filename(fullname)
00371         try:
00372             source_bytes = self.get_data(path)
00373         except IOError:
00374             raise ImportError("source not available through get_data()")
00375         encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline)
00376         newline_decoder = _io.IncrementalNewlineDecoder(None, True)
00377         return newline_decoder.decode(source_bytes.decode(encoding[0]))

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib.abc.PyLoader.is_package (   self,
  fullname 
) [inherited]
Concrete implementation of InspectLoader.is_package by checking if
the path returned by get_filename has a filename of '__init__.py'.

Reimplemented from importlib._bootstrap._LoaderBasics.

Reimplemented in importlib.test.source.test_abc_loader.PyPycLoaderMock, and importlib.test.source.test_abc_loader.PyLoaderMock.

Definition at line 175 of file abc.py.

00175 
00176     def is_package(self, fullname):
00177         raise NotImplementedError

def importlib.abc.Loader.load_module (   self,
  fullname 
) [inherited]
Abstract method which when implemented should load a module.
The fullname is a str.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.Loader.

Definition at line 21 of file abc.py.

00021 
00022     def load_module(self, fullname):
00023         """Abstract method which when implemented should load a module.
00024         The fullname is a str."""
00025         raise NotImplementedError
00026 

Here is the caller graph for this function:

def importlib.abc.Loader.load_module (   self,
  fullname 
) [inherited]
Abstract method which when implemented should load a module.
The fullname is a str.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.Loader.

Definition at line 21 of file abc.py.

00021 
00022     def load_module(self, fullname):
00023         """Abstract method which when implemented should load a module.
00024         The fullname is a str."""
00025         raise NotImplementedError
00026 

Here is the caller graph for this function:

def importlib._bootstrap.SourceLoader.load_module (   self,
  fullname 
) [inherited]
Concrete implementation of Loader.load_module.

Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be
implemented to load source code. Use of bytecode is dictated by whether
get_code uses/writes bytecode.

Definition at line 428 of file _bootstrap.py.

00428 
00429     def load_module(self, fullname):
00430         """Concrete implementation of Loader.load_module.
00431 
00432         Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be
00433         implemented to load source code. Use of bytecode is dictated by whether
00434         get_code uses/writes bytecode.
00435 
00436         """
00437         return self._load_module(fullname)
00438 

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib.abc.SourceLoader.path_mtime (   self,
  path 
) [inherited]
Return the (int) modification time for the path (str).

Reimplemented from importlib._bootstrap.SourceLoader.

Reimplemented in importlib.test.source.test_abc_loader.SourceLoaderMock.

Definition at line 124 of file abc.py.

00124 
00125     def path_mtime(self, path):
00126         """Return the (int) modification time for the path (str)."""
00127         raise NotImplementedError

def abc.ABCMeta.register (   cls,
  subclass 
) [inherited]
Register a virtual subclass of an ABC.

Definition at line 135 of file abc.py.

00135 
00136     def register(cls, subclass):
00137         """Register a virtual subclass of an ABC."""
00138         if not isinstance(subclass, type):
00139             raise TypeError("Can only register classes")
00140         if issubclass(subclass, cls):
00141             return  # Already a subclass
00142         # Subtle: test for cycles *after* testing for "already a subclass";
00143         # this means we allow X.register(X) and interpret it as a no-op.
00144         if issubclass(cls, subclass):
00145             # This would create a cycle, which is bad for the algorithm below
00146             raise RuntimeError("Refusing to create an inheritance cycle")
00147         cls._abc_registry.add(subclass)
00148         ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache

Here is the caller graph for this function:

def abc.ABCMeta.register (   cls,
  subclass 
) [inherited]
Register a virtual subclass of an ABC.

Definition at line 135 of file abc.py.

00135 
00136     def register(cls, subclass):
00137         """Register a virtual subclass of an ABC."""
00138         if not isinstance(subclass, type):
00139             raise TypeError("Can only register classes")
00140         if issubclass(subclass, cls):
00141             return  # Already a subclass
00142         # Subtle: test for cycles *after* testing for "already a subclass";
00143         # this means we allow X.register(X) and interpret it as a no-op.
00144         if issubclass(cls, subclass):
00145             # This would create a cycle, which is bad for the algorithm below
00146             raise RuntimeError("Refusing to create an inheritance cycle")
00147         cls._abc_registry.add(subclass)
00148         ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache

Here is the caller graph for this function:

def importlib.abc.SourceLoader.set_data (   self,
  path,
  data 
) [inherited]
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 some
reason the file cannot be written because of permissions, fail
silently.

Reimplemented from importlib._bootstrap.SourceLoader.

Reimplemented in importlib.test.source.test_abc_loader.SourceLoaderMock.

Definition at line 128 of file abc.py.

00128 
00129     def set_data(self, path, data):
00130         """Write the bytes to the path (if possible).
00131 
00132         Accepts a str path and data as bytes.
00133 
00134         Any needed intermediary directories are to be created. If for some
00135         reason the file cannot be written because of permissions, fail
00136         silently.
00137 
00138         """
00139         raise NotImplementedError
00140 

def importlib.abc.PyPycLoader.source_mtime (   self,
  fullname 
)
Abstract method. Accepts a str filename and returns an int
modification time for the source of the module.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.PyPycLoader, and importlib.test.source.test_abc_loader.PyPycLoaderMock.

Definition at line 288 of file abc.py.

00288 
00289     def source_mtime(self, fullname):
00290         """Abstract method. Accepts a str filename and returns an int
00291         modification time for the source of the module."""
00292         raise NotImplementedError

Here is the caller graph for this function:

def importlib.abc.PyLoader.source_path (   self,
  fullname 
) [inherited]
Abstract method.  Accepts a str module name and returns the path to
the source code for the module.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.PyLoader, importlib.test.source.test_abc_loader.PyLoaderCompatMock, and importlib.test.source.test_abc_loader.PyLoaderMock.

Definition at line 179 of file abc.py.

00179 
00180     def source_path(self, fullname):
00181         """Abstract method.  Accepts a str module name and returns the path to
00182         the source code for the module."""
00183         raise NotImplementedError

Here is the caller graph for this function:

def importlib.abc.PyPycLoader.write_bytecode (   self,
  fullname,
  bytecode 
)
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.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.PyPycLoader, and importlib.test.source.test_abc_loader.PyPycLoaderMock.

Definition at line 300 of file abc.py.

00300 
00301     def write_bytecode(self, fullname, bytecode):
00302         """Abstract method.  Accepts a str filename and bytes object
00303         representing the bytecode for the module.  Returns a boolean
00304         representing whether the bytecode was written or not."""
00305         raise NotImplementedError

Here is the caller graph for this function:


The documentation for this class was generated from the following file: