cpython: 8e733e30edf6 (original) (raw)

--- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -153,6 +153,10 @@ ABC hierarchy:: module. Originally specified in :pep:302, this method was meant for use in :data:sys.meta_path and in the path-based import subsystem.

+ .. class:: MetaPathFinder @@ -169,12 +173,19 @@ ABC hierarchy:: will be the value of :attr:__path__ from the parent package. If a loader cannot be found, None is returned.

+ .. method:: invalidate_caches() An optional method which, when called, should invalidate any internal cache used by the finder. Used by :func:importlib.invalidate_caches when invalidating the caches of all finders on :data:sys.meta_path.

+ .. class:: PathEntryFinder @@ -182,7 +193,7 @@ ABC hierarchy:: it bears some similarities to :class:MetaPathFinder, PathEntryFinder is meant for use only within the path-based import subsystem provided by :class:PathFinder. This ABC is a subclass of :class:Finder for

@@ -194,9 +205,12 @@ ABC hierarchy:: package. The loader may be None while specifying portion to signify the contribution of the file system locations to a namespace package. An empty list can be used for portion to signify the loader

+

.. method:: find_module(fullname): @@ -249,21 +263,29 @@ ABC hierarchy:: - :attr:__package__ The parent package for the module/package. If the module is top-level then it has a value of the empty string. The

- :attr:__loader__

+

.. method:: module_repr(module)

.. versionadded: 3.3

+ .. class:: ResourceLoader @@ -281,6 +303,9 @@ ABC hierarchy:: be found. The path is expected to be constructed using a module's :attr:__file__ attribute or an item from a package's :attr:__path__.

+ .. class:: InspectLoader @@ -297,6 +322,9 @@ ABC hierarchy:: .. index:: single: universal newlines; importlib.abc.InspectLoader.get_source method

+ .. method:: get_source(fullname) An abstract method to return the source of a module. It is returned as @@ -305,12 +333,18 @@ ABC hierarchy:: if no source is available (e.g. a built-in module). Raises :exc:ImportError if the loader cannot find the module specified.

+ .. method:: is_package(fullname) An abstract method to return a true value if the module is a package, a false value otherwise. :exc:ImportError is raised if the :term:loader cannot find the module.

+ .. class:: ExecutionLoader @@ -328,6 +362,9 @@ ABC hierarchy:: the source file, regardless of whether a bytecode was used to load the module.

+ .. class:: FileLoader(fullname, path) @@ -392,10 +429,13 @@ ABC hierarchy:: - 'size' (optional): the size in bytes of the source code. Any other keys in the dictionary are ignored, to allow for future

.. versionadded:: 3.3

+ .. method:: path_mtime(path) Optional abstract method which returns the modification time for the @@ -404,7 +444,10 @@ ABC hierarchy:: .. deprecated:: 3.3 This method is deprecated in favour of :meth:path_stats. You don't have to implement it, but it is still available for compatibility

+

.. method:: set_data(path, data) @@ -415,6 +458,9 @@ ABC hierarchy:: When writing to the path fails because the path is read-only (:attr:errno.EACCES), do not propagate the exception.

+ .. method:: source_to_code(data, path) Create a code object from Python source.

--- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -893,8 +893,10 @@ class SourceLoader(_LoaderBasics): def path_mtime(self, path): """Optional method that returns the modification time (an int) for the specified path, where path is a str. +

def path_stats(self, path): """Optional method returning a metadata dict for the specified path @@ -905,6 +907,7 @@ class SourceLoader(_LoaderBasics): - 'size' (optional) is the size in bytes of the source code. Implementing this method allows the loader to read bytecode files.

@@ -922,9 +925,7 @@ class SourceLoader(_LoaderBasics): """Optional method which writes data (bytes) to a file path (a str). Implementing this method allows for the writing of bytecode files. - """

def get_source(self, fullname): @@ -973,7 +974,7 @@ class SourceLoader(_LoaderBasics): else: try: st = self.path_stats(source_path)

--- a/Lib/importlib/abc.py +++ b/Lib/importlib/abc.py @@ -37,9 +37,8 @@ class Finder(metaclass=abc.ABCMeta): def find_module(self, fullname, path=None): """An abstract method that should find a module. The fullname is a str and the optional path is a str or None.

class MetaPathFinder(Finder): @@ -49,16 +48,14 @@ class MetaPathFinder(Finder): @abc.abstractmethod def find_module(self, fullname, path): """Abstract method which, when implemented, should find a module.

def invalidate_caches(self): """An optional method for clearing the finder's cache, if any. This method is used by importlib.invalidate_caches(). """

_register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter, machinery.PathFinder, machinery.WindowsRegistryFinder) @@ -70,13 +67,14 @@ class PathEntryFinder(Finder): @abc.abstractmethod def find_loader(self, fullname):

find_module = _bootstrap._find_module_shim @@ -84,25 +82,34 @@ class PathEntryFinder(Finder): """An optional method for clearing the finder's cache, if any. This method is used by PathFinder.invalidate_caches(). """

_register(PathEntryFinder, machinery.FileFinder) class Loader(metaclass=abc.ABCMeta):

+

+

@abc.abstractmethod def load_module(self, fullname): """Abstract method which when implemented should load a module.

+

+

@@ -119,7 +126,7 @@ class ResourceLoader(Loader): def get_data(self, path): """Abstract method which when implemented should return the bytes for the specified path. The path must be a str."""

class InspectLoader(Loader): @@ -134,20 +141,29 @@ class InspectLoader(Loader): @abc.abstractmethod def is_package(self, fullname): """Abstract method which when implemented should return whether the

+

@abc.abstractmethod def get_code(self, fullname): """Abstract method which when implemented should return the code object

+

@abc.abstractmethod def get_source(self, fullname): """Abstract method which should return the source code for the

+

_register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter, machinery.ExtensionFileLoader) @@ -165,8 +181,11 @@ class ExecutionLoader(InspectLoader): @abc.abstractmethod def get_filename(self, fullname): """Abstract method which should return the value that file is to be

+

class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader): @@ -198,7 +217,7 @@ class SourceLoader(_bootstrap.SourceLoad def path_mtime(self, path): """Return the (int) modification time for the path (str).""" if self.path_stats.func is SourceLoader.path_stats:

def path_stats(self, path): @@ -209,7 +228,7 @@ class SourceLoader(_bootstrap.SourceLoad - 'size' (optional) is the size in bytes of the source code. """ if self.path_mtime.func is SourceLoader.path_mtime:

def set_data(self, path, data): @@ -220,8 +239,6 @@ class SourceLoader(_bootstrap.SourceLoad Any needed intermediary directories are to be created. If for some reason the file cannot be written because of permissions, fail silently. - """

_register(SourceLoader, machinery.SourceFileLoader)

deleted file mode 100644 --- a/Lib/test/test_importlib/source/test_abc_loader.py +++ /dev/null @@ -1,410 +0,0 @@ -import importlib -from importlib import abc - -from .. import abc as testing_abc -from .. import util -from . import util as source_util - -import imp -import inspect -import io -import marshal -import os -import sys -import types -import unittest -import warnings - - -class SourceOnlyLoaderMock(abc.SourceLoader): -

-

-

-

-

- - -class SourceLoaderMock(SourceOnlyLoaderMock): -

-

-

-

-

- - -def raise_ImportError(*args, **kwargs):

- - -class SourceLoaderTestHarness(unittest.TestCase): -

-

-

- - -class SourceOnlyLoaderTests(SourceLoaderTestHarness): -

-

-

-

-

-

-

-

-

-

-

- - -@unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true") -class SourceLoaderBytecodeTests(SourceLoaderTestHarness): -

-

-

-

-

-

-

-

-

-

-

-

-

- - -class SourceLoaderGetSourceTests(unittest.TestCase): -

-

-

-

- - -class AbstractMethodImplTests(unittest.TestCase): -

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

-

- - -def test_main():

- - -if name == 'main':

--- a/Lib/test/test_importlib/test_abc.py +++ b/Lib/test/test_importlib/test_abc.py @@ -1,9 +1,18 @@ +import importlib from importlib import abc from importlib import machinery + +import imp import inspect +import io +import marshal +import os +import sys import unittest +from . import util +##### Inheritance class InheritanceTests: """Test that the specified class is a subclass/superclass of the expected @@ -72,16 +81,422 @@ class SourceLoader(InheritanceTests, uni subclasses = [machinery.SourceFileLoader] -def test_main():

+##### Default semantics +class MetaPathFinderSubclass(abc.MetaPathFinder): +

+ + +class MetaPathFinderDefaultsTests(unittest.TestCase): +

+

+

+ + +class PathEntryFinderSubclass(abc.PathEntryFinder): +

+ + +class PathEntryFinderDefaultsTests(unittest.TestCase): +

+

+

+

+ + +class LoaderSubclass(abc.Loader): +

+ + +class LoaderDefaultsTests(unittest.TestCase): +

+

+

+ + +class ResourceLoaderSubclass(LoaderSubclass, abc.ResourceLoader): +

+ + +class ResourceLoaderDefaultsTests(unittest.TestCase): +

+

+ + +class InspectLoaderSubclass(LoaderSubclass, abc.InspectLoader): +

+

+

+ + +class InspectLoaderDefaultsTests(unittest.TestCase): +

+

+

+

+ + +class ExecutionLoaderSubclass(InspectLoaderSubclass, abc.ExecutionLoader): +

+ + +class ExecutionLoaderDefaultsTests(unittest.TestCase): +

+

+ + +##### SourceLoader +class SourceOnlyLoaderMock(abc.SourceLoader): +

+

+

+

+

+ + +class SourceLoaderMock(SourceOnlyLoaderMock): +

+

+

+

+

+ + +class SourceLoaderTestHarness(unittest.TestCase): +

+

+

+ + +class SourceOnlyLoaderTests(SourceLoaderTestHarness): +

+

+

+

+

+

+

+

+

+

+

+ + +@unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true") +class SourceLoaderBytecodeTests(SourceLoaderTestHarness): +

+

+

+

+

+

+

+

+

+

+

+

+

+ + +class SourceLoaderGetSourceTests(unittest.TestCase): +

+

+

+

if name == 'main':

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -30,6 +30,13 @@ Core and Builtins Library ------- +- Issue #17093: Make the ABCs in importlib.abc provide default values or raise

--- a/Python/importlib.h +++ b/Python/importlib.h @@ -1700,7 +1700,7 @@ const unsigned char _Py_M__importlib[] = 0,0,0,244,12,0,0,0,83,111,117,114,99,101,76,111, 97,100,101,114,99,2,0,0,0,0,0,0,0,2,0,0, 0,1,0,0,0,67,0,0,0,115,10,0,0,0,116,0,

};