Back to index

python3.2  3.2.2
Public Member Functions
importlib.test.frozen.test_loader.LoaderTests Class Reference
Inheritance diagram for importlib.test.frozen.test_loader.LoaderTests:
Inheritance graph
[legend]
Collaboration diagram for importlib.test.frozen.test_loader.LoaderTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_module
def test_package
def test_lacking_parent
def test_module_reuse
def test_state_after_failure
def test_unloadable
def __new__
def register
def __instancecheck__
def __subclasscheck__

Detailed Description

Definition at line 8 of file test_loader.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.__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.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:

A loader should not be dependent on it's parent package being
imported.

Reimplemented from importlib.test.abc.LoaderTests.

Definition at line 32 of file test_loader.py.

00032 
00033     def test_lacking_parent(self):
00034         with util.uncache('__phello__', '__phello__.spam'), \
00035              captured_stdout() as stdout:
00036             module = machinery.FrozenImporter.load_module('__phello__.spam')
00037             check = {'__name__': '__phello__.spam', '__file__': '<frozen>',
00038                     '__package__': '__phello__',
00039                     '__loader__': machinery.FrozenImporter}
00040             for attr, value in check.items():
00041                 attr_value = getattr(module, attr)
00042                 self.assertEqual(attr_value, value,
00043                                  "for __phello__.spam.%s, %r != %r" %
00044                                  (attr, attr_value, value))
00045             self.assertEqual(stdout.getvalue(), 'Hello world!\n')

Here is the call graph for this function:

A module should load without issue.

After the loader returns the module should be in sys.modules.

Attributes to verify:

    * __file__
    * __loader__
    * __name__
    * No __path__

Reimplemented from importlib.test.abc.LoaderTests.

Definition at line 10 of file test_loader.py.

00010 
00011     def test_module(self):
00012         with util.uncache('__hello__'), captured_stdout() as stdout:
00013             module = machinery.FrozenImporter.load_module('__hello__')
00014             check = {'__name__': '__hello__', '__file__': '<frozen>',
00015                     '__package__': '', '__loader__': machinery.FrozenImporter}
00016             for attr, value in check.items():
00017                 self.assertEqual(getattr(module, attr), value)
00018             self.assertEqual(stdout.getvalue(), 'Hello world!\n')

Here is the call graph for this function:

If a module is already in sys.modules, it should be reused.

Reimplemented from importlib.test.abc.LoaderTests.

Definition at line 46 of file test_loader.py.

00046 
00047     def test_module_reuse(self):
00048         with util.uncache('__hello__'), captured_stdout() as stdout:
00049             module1 = machinery.FrozenImporter.load_module('__hello__')
00050             module2 = machinery.FrozenImporter.load_module('__hello__')
00051             self.assertTrue(module1 is module2)
00052             self.assertEqual(stdout.getvalue(),
00053                              'Hello world!\nHello world!\n')

Here is the call graph for this function:

Loading a package should work.

After the loader returns the module should be in sys.modules.

Attributes to verify:

    * __name__
    * __file__
    * __package__
    * __path__
    * __loader__

Reimplemented from importlib.test.abc.LoaderTests.

Definition at line 19 of file test_loader.py.

00019 
00020     def test_package(self):
00021         with util.uncache('__phello__'),  captured_stdout() as stdout:
00022             module = machinery.FrozenImporter.load_module('__phello__')
00023             check = {'__name__': '__phello__', '__file__': '<frozen>',
00024                      '__package__': '__phello__', '__path__': ['__phello__'],
00025                      '__loader__': machinery.FrozenImporter}
00026             for attr, value in check.items():
00027                 attr_value = getattr(module, attr)
00028                 self.assertEqual(attr_value, value,
00029                                  "for __phello__.%s, %r != %r" %
00030                                  (attr, attr_value, value))
00031             self.assertEqual(stdout.getvalue(), 'Hello world!\n')

Here is the call graph for this function:

If a module is already in sys.modules and a reload fails
(e.g. a SyntaxError), the module should be in the state it was before
the reload began.

Reimplemented from importlib.test.abc.LoaderTests.

Definition at line 54 of file test_loader.py.

00054 
00055     def test_state_after_failure(self):
00056         # No way to trigger an error in a frozen module.
00057         pass

Test ImportError is raised when the loader is asked to load a module
it can't.

Reimplemented from importlib.test.abc.LoaderTests.

Definition at line 58 of file test_loader.py.

00058 
00059     def test_unloadable(self):
00060         assert machinery.FrozenImporter.find_module('_not_real') is None
00061         with self.assertRaises(ImportError):
00062             machinery.FrozenImporter.load_module('_not_real')
00063 

Here is the call graph for this function:


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