Back to index

python3.2  3.2.2
Public Member Functions
importlib.test.source.test_finder.FinderTests Class Reference
Inheritance diagram for importlib.test.source.test_finder.FinderTests:
Inheritance graph
[legend]
Collaboration diagram for importlib.test.source.test_finder.FinderTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def import_
def run_test
def test_module
def test_package
def test_module_in_package
def test_package_in_package
def test_empty_sub_directory
def test_package_over_module
def test_failure
def test_empty_dir
def test_empty_string_for_dir
def __new__
def register
def __instancecheck__
def __subclasscheck__

Detailed Description

For a top-level module, it should just be found directly in the
directory being searched. This is true for a directory with source
[top-level source], bytecode [top-level bc], or both [top-level both].
There is also the possibility that it is a package [top-level package], in
which case there will be a directory with the module name and an
__init__.py file. If there is a directory without an __init__.py an
ImportWarning is returned [empty dir].

For sub-modules and sub-packages, the same happens as above but only use
the tail end of the name [sub module] [sub package] [sub empty].

When there is a conflict between a package and module having the same name
in the same directory, the package wins out [package over module]. This is
so that imports of modules within the package can occur rather than trigger
an import error.

When there is a package and module with the same name, always pick the
package over the module [package over module]. This is so that imports from
the package have the possibility of succeeding.

Definition at line 12 of file test_finder.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 importlib.test.source.test_finder.FinderTests.import_ (   self,
  root,
  module 
)

Definition at line 36 of file test_finder.py.

00036 
00037     def import_(self, root, module):
00038         finder = _bootstrap._FileFinder(root,
00039                                         _bootstrap._SourceFinderDetails(),
00040                                         _bootstrap._SourcelessFinderDetails())
00041         return finder.find_module(module)

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.test.source.test_finder.FinderTests.run_test (   self,
  test,
  create = None,
  compile_ = None,
  unlink = None 
)
Test the finding of 'test' with the creation of modules listed in
'create'.

Any names listed in 'compile_' are byte-compiled. Modules
listed in 'unlink' have their source files deleted.

Definition at line 42 of file test_finder.py.

00042 
00043     def run_test(self, test, create=None, *, compile_=None, unlink=None):
00044         """Test the finding of 'test' with the creation of modules listed in
00045         'create'.
00046 
00047         Any names listed in 'compile_' are byte-compiled. Modules
00048         listed in 'unlink' have their source files deleted.
00049 
00050         """
00051         if create is None:
00052             create = {test}
00053         with source_util.create_modules(*create) as mapping:
00054             if compile_:
00055                 for name in compile_:
00056                     py_compile.compile(mapping[name])
00057             if unlink:
00058                 for name in unlink:
00059                     os.unlink(mapping[name])
00060                     try:
00061                         make_legacy_pyc(mapping[name])
00062                     except OSError as error:
00063                         # Some tests do not set compile_=True so the source
00064                         # module will not get compiled and there will be no
00065                         # PEP 3147 pyc file to rename.
00066                         if error.errno != errno.ENOENT:
00067                             raise
00068             loader = self.import_(mapping['.root'], test)
00069             self.assertTrue(hasattr(loader, 'load_module'))
00070             return loader

Here is the call graph for this function:

Definition at line 129 of file test_finder.py.

00129 
00130     def test_empty_dir(self):
00131         with warnings.catch_warnings():
00132             warnings.simplefilter("error", ImportWarning)
00133             with self.assertRaises(ImportWarning):
00134                 self.run_test('pkg', {'pkg.__init__'}, unlink={'pkg.__init__'})

Here is the call graph for this function:

Definition at line 135 of file test_finder.py.

00135 
00136     def test_empty_string_for_dir(self):
00137         # The empty string from sys.path means to search in the cwd.
00138         finder = _bootstrap._FileFinder('', _bootstrap._SourceFinderDetails())
00139         with open('mod.py', 'w') as file:
00140             file.write("# test file for importlib")
00141         try:
00142             loader = finder.find_module('mod')
00143             self.assertTrue(hasattr(loader, 'load_module'))
00144         finally:
00145             os.unlink('mod.py')
00146 

Here is the call graph for this function:

Definition at line 106 of file test_finder.py.

00106 
00107     def test_empty_sub_directory(self):
00108         context = source_util.create_modules('pkg.__init__', 'pkg.sub.__init__')
00109         with warnings.catch_warnings():
00110             warnings.simplefilter("error", ImportWarning)
00111             with context as mapping:
00112                 os.unlink(mapping['pkg.sub.__init__'])
00113                 pkg_dir = os.path.dirname(mapping['pkg.__init__'])
00114                 with self.assertRaises(ImportWarning):
00115                     self.import_(pkg_dir, 'pkg.sub')

Here is the call graph for this function:

Reimplemented from importlib.test.abc.FinderTests.

Definition at line 123 of file test_finder.py.

00123 
00124     def test_failure(self):
00125         with source_util.create_modules('blah') as mapping:
00126             nothing = self.import_(mapping['.root'], 'sdfsadsadf')
00127             self.assertTrue(nothing is None)

Here is the call graph for this function:

Reimplemented from importlib.test.abc.FinderTests.

Definition at line 71 of file test_finder.py.

00071 
00072     def test_module(self):
00073         # [top-level source]
00074         self.run_test('top_level')
00075         # [top-level bc]
00076         self.run_test('top_level', compile_={'top_level'},
00077                       unlink={'top_level'})
00078         # [top-level both]
00079         self.run_test('top_level', compile_={'top_level'})

Here is the call graph for this function:

Reimplemented from importlib.test.abc.FinderTests.

Definition at line 91 of file test_finder.py.

00091 
00092     def test_module_in_package(self):
00093         with source_util.create_modules('pkg.__init__', 'pkg.sub') as mapping:
00094             pkg_dir = os.path.dirname(mapping['pkg.__init__'])
00095             loader = self.import_(pkg_dir, 'pkg.sub')
00096             self.assertTrue(hasattr(loader, 'load_module'))

Here is the call graph for this function:

Reimplemented from importlib.test.abc.FinderTests.

Definition at line 81 of file test_finder.py.

00081 
00082     def test_package(self):
00083         # Source.
00084         self.run_test('pkg', {'pkg.__init__'})
00085         # Bytecode.
00086         self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'},
00087                 unlink={'pkg.__init__'})
00088         # Both.
00089         self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'})

Here is the call graph for this function:

Reimplemented from importlib.test.abc.FinderTests.

Definition at line 98 of file test_finder.py.

00098 
00099     def test_package_in_package(self):
00100         context = source_util.create_modules('pkg.__init__', 'pkg.sub.__init__')
00101         with context as mapping:
00102             pkg_dir = os.path.dirname(mapping['pkg.__init__'])
00103             loader = self.import_(pkg_dir, 'pkg.sub')
00104             self.assertTrue(hasattr(loader, 'load_module'))

Here is the call graph for this function:

Reimplemented from importlib.test.abc.FinderTests.

Definition at line 117 of file test_finder.py.

00117 
00118     def test_package_over_module(self):
00119         name = '_temp'
00120         loader = self.run_test(name, {'{0}.__init__'.format(name), name})
00121         self.assertTrue('__init__' in loader.get_filename(name))
00122 

Here is the call graph for this function:


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