Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
importlib.test.source.test_abc_loader.SourceLoaderMock Class Reference
Inheritance diagram for importlib.test.source.test_abc_loader.SourceLoaderMock:
Inheritance graph
[legend]
Collaboration diagram for importlib.test.source.test_abc_loader.SourceLoaderMock:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def get_data
def path_mtime
def set_data
def get_filename
def get_source
def get_source
def get_code
def get_code
def load_module
def load_module
def load_module
def is_package
def is_package
def __new__
def __new__
def register
def register
def __instancecheck__
def __instancecheck__
def __subclasscheck__
def __subclasscheck__

Public Attributes

 bytecode_path
 bytecode
 written
 path

Static Public Attributes

int source_mtime = 1
tuple source

Detailed Description

Definition at line 36 of file test_abc_loader.py.


Constructor & Destructor Documentation

def importlib.test.source.test_abc_loader.SourceLoaderMock.__init__ (   self,
  path,
  magic = imp.get_magic() 
)

Definition at line 40 of file test_abc_loader.py.

00040 
00041     def __init__(self, path, magic=imp.get_magic()):
00042         super().__init__(path)
00043         self.bytecode_path = imp.cache_from_source(self.path)
00044         data = bytearray(magic)
00045         data.extend(marshal._w_long(self.source_mtime))
00046         code_object = compile(self.source, self.path, 'exec',
00047                                 dont_inherit=True)
00048         data.extend(marshal.dumps(code_object))
00049         self.bytecode = bytes(data)
00050         self.written = {}

Here is the caller graph for this function:


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.InspectLoader.get_code (   self,
  fullname 
) [inherited]
Abstract method which when implemented should return the code object
for the module.  The fullname is a str.  Returns a types.CodeType.

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

Definition at line 76 of file abc.py.

00076 
00077     def get_code(self, fullname):
00078         """Abstract method which when implemented should return the code object
00079         for the module.  The fullname is a str.  Returns a types.CodeType."""
00080         raise NotImplementedError

Here is the caller graph for this function:

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

Reading of bytecode requires path_mtime to be implemented. To write
bytecode, set_data must also be implemented.

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

Definition at line 378 of file _bootstrap.py.

00378 
00379     def get_code(self, fullname):
00380         """Concrete implementation of InspectLoader.get_code.
00381 
00382         Reading of bytecode requires path_mtime to be implemented. To write
00383         bytecode, set_data must also be implemented.
00384 
00385         """
00386         source_path = self.get_filename(fullname)
00387         bytecode_path = imp.cache_from_source(source_path)
00388         source_mtime = None
00389         if bytecode_path is not None:
00390             try:
00391                 source_mtime = self.path_mtime(source_path)
00392             except NotImplementedError:
00393                 pass
00394             else:
00395                 try:
00396                     data = self.get_data(bytecode_path)
00397                 except IOError:
00398                     pass
00399                 else:
00400                     try:
00401                         bytes_data = self._bytes_from_bytecode(fullname, data,
00402                                                                source_mtime)
00403                     except (ImportError, EOFError):
00404                         pass
00405                     else:
00406                         found = marshal.loads(bytes_data)
00407                         if isinstance(found, code_type):
00408                             return found
00409                         else:
00410                             msg = "Non-code object in {}"
00411                             raise ImportError(msg.format(bytecode_path))
00412         source_bytes = self.get_data(source_path)
00413         code_object = compile(source_bytes, source_path, 'exec',
00414                                 dont_inherit=True)
00415         if (not sys.dont_write_bytecode and bytecode_path is not None and
00416                 source_mtime is not None):
00417             # If e.g. Jython ever implements imp.cache_from_source to have
00418             # their own cached file format, this block of code will most likely
00419             # throw an exception.
00420             data = bytearray(imp.get_magic())
00421             data.extend(marshal._w_long(source_mtime))
00422             data.extend(marshal.dumps(code_object))
00423             try:
00424                 self.set_data(bytecode_path, data)
00425             except NotImplementedError:
00426                 pass
00427         return code_object

Here is the call graph for this function:

Here is the caller graph for this function:

Abstract method which when implemented should return the bytes for
the specified path.  The path must be a str.

Reimplemented from importlib.test.source.test_abc_loader.SourceOnlyLoaderMock.

Definition at line 51 of file test_abc_loader.py.

00051 
00052     def get_data(self, path):
00053         if path == self.path:
00054             return super().get_data(path)
00055         elif path == self.bytecode_path:
00056             return self.bytecode
00057         else:
00058             raise IOError

Here is the caller graph for this function:

Abstract method which should return the value that __file__ is to be
set to.

Reimplemented from importlib.abc.ExecutionLoader.

Definition at line 32 of file test_abc_loader.py.

00032 
00033     def get_filename(self, fullname):
00034         return self.path
00035 

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.InspectLoader.is_package (   self,
  fullname 
) [inherited]
Abstract method which when implemented should return whether the
module is a package.  The fullname is a str.  Returns a bool.

Reimplemented in importlib.test.source.test_abc_loader.AbstractMethodImplTests.InspectLoader, importlib.test.source.test_abc_loader.PyPycLoaderMock, importlib.abc.PyLoader, and importlib.test.source.test_abc_loader.PyLoaderMock.

Definition at line 70 of file abc.py.

00070 
00071     def is_package(self, fullname):
00072         """Abstract method which when implemented should return whether the
00073         module is a package.  The fullname is a str.  Returns a bool."""
00074         raise NotImplementedError

def importlib._bootstrap._LoaderBasics.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 in importlib.test.source.test_abc_loader.PyPycLoaderMock, importlib.abc.PyLoader, and importlib.test.source.test_abc_loader.PyLoaderMock.

Definition at line 299 of file _bootstrap.py.

00299 
00300     def is_package(self, fullname):
00301         """Concrete implementation of InspectLoader.is_package by checking if
00302         the path returned by get_filename has a filename of '__init__.py'."""
00303         filename = self.get_filename(fullname).rpartition(path_sep)[2]
00304         return filename.rsplit('.', 1)[0] == '__init__'

Here is the call graph for this function:

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.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:

Return the (int) modification time for the path (str).

Reimplemented from importlib.abc.SourceLoader.

Definition at line 59 of file test_abc_loader.py.

00059 
00060     def path_mtime(self, path):
00061         assert path == self.path
00062         return self.source_mtime

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:

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.abc.SourceLoader.

Definition at line 63 of file test_abc_loader.py.

00063 
00064     def set_data(self, path, data):
00065         self.written[path] = bytes(data)
00066         return path == self.bytecode_path
00067 


Member Data Documentation

Definition at line 48 of file test_abc_loader.py.

Definition at line 42 of file test_abc_loader.py.

Definition at line 26 of file test_abc_loader.py.

Initial value:
(b"_ = '::'.join([__name__, __file__, __cached__, __package__, "
              b"repr(__loader__)])")

Definition at line 22 of file test_abc_loader.py.

Definition at line 38 of file test_abc_loader.py.

Definition at line 49 of file test_abc_loader.py.


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