Back to index

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

List of all members.

Public Member Functions

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

Public Attributes

 module_paths
 path_to_module

Static Public Attributes

tuple source

Detailed Description

Definition at line 68 of file test_abc_loader.py.


Constructor & Destructor Documentation

Take a dict of 'module_name: path' pairings.

Paths should have no file extension, allowing packages to be denoted by
ending in '__init__'.

Definition at line 74 of file test_abc_loader.py.

00074 
00075     def __init__(self, data):
00076         """Take a dict of 'module_name: path' pairings.
00077 
00078         Paths should have no file extension, allowing packages to be denoted by
00079         ending in '__init__'.
00080 
00081         """
00082         self.module_paths = data
00083         self.path_to_module = {val:key for key,val in data.items()}

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

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

Definition at line 84 of file test_abc_loader.py.

00084 
00085     def get_data(self, path):
00086         if path not in self.path_to_module:
00087             raise IOError
00088         return self.source

Here is the caller graph for this function:

Silence deprecation warning.

Reimplemented from importlib.abc.PyLoader.

Reimplemented in importlib.test.source.test_abc_loader.PyLoaderCompatMock.

Definition at line 99 of file test_abc_loader.py.

00099 
00100     def get_filename(self, name):
00101         """Silence deprecation warning."""
00102         with warnings.catch_warnings(record=True) as w:
00103             warnings.simplefilter("always")
00104             path = super().get_filename(name)
00105             assert len(w) == 1
00106             assert issubclass(w[0].category, PendingDeprecationWarning)
00107             return path
00108 

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:

Concrete implementation of InspectLoader.is_package by checking if
the path returned by get_filename has a filename of '__init__.py'.

Reimplemented from importlib.abc.PyLoader.

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

Definition at line 89 of file test_abc_loader.py.

00089 
00090     def is_package(self, name):
00091         filename = os.path.basename(self.get_filename(name))
00092         return os.path.splitext(filename)[0] == '__init__'

Here is the call 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:

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 

Abstract method.  Accepts a str module name and returns the path to
the source code for the module.

Reimplemented from importlib.abc.PyLoader.

Reimplemented in importlib.test.source.test_abc_loader.PyLoaderCompatMock.

Definition at line 93 of file test_abc_loader.py.

00093 
00094     def source_path(self, name):
00095         try:
00096             return self.module_paths[name]
00097         except KeyError:
00098             raise ImportError


Member Data Documentation

Definition at line 81 of file test_abc_loader.py.

Definition at line 82 of file test_abc_loader.py.

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

Definition at line 71 of file test_abc_loader.py.


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