diff --git a/Lib/importlib/_abc.py b/Lib/importlib/_abc.py index 693b466112638f..2227d60a74d7a9 100644 --- a/Lib/importlib/_abc.py +++ b/Lib/importlib/_abc.py @@ -20,6 +20,14 @@ def create_module(self, spec): # We don't define exec_module() here since that would break # hasattr checks we do to support backward compatibility. + @classmethod + def __subclasshook__(cls, C): + if cls is Loader: + if (any('exec_module' in B.__dict__ for B in C.__mro__) or + any('load_module' in B.__dict__ for B in C.__mro__)): + return True + return NotImplemented + def load_module(self, fullname): """Return the loaded module. diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py index b56fa94eb9c135..24ca49df5fe2c0 100644 --- a/Lib/importlib/abc.py +++ b/Lib/importlib/abc.py @@ -86,6 +86,14 @@ class ResourceLoader(Loader): """ + @classmethod + def __subclasshook__(cls, C): + if cls is ResourceLoader: + if (Loader.__subclasshook__(C) and + any('get_data' in B.__dict__ for B in C.__mro__)): + return True + return NotImplemented + @abc.abstractmethod def get_data(self, path): """Abstract method which when implemented should return the bytes for @@ -102,11 +110,24 @@ class InspectLoader(Loader): """ + @classmethod + def __subclasshook__(cls, C): + if cls is InspectLoader: + if (Loader.__subclasshook__(C) and + any('is_package' in B.__dict__ for B in C.__mro__) and + any('get_code' in B.__dict__ for B in C.__mro__) and + any('get_source' in B.__dict__ for B in C.__mro__) and + any('source_to_code' in B.__dict__ for B in C.__mro__)): + return True + return NotImplemented + def is_package(self, fullname): - """Optional method which when implemented should return whether the - module is a package. The fullname is a str. Returns a bool. + """(abstract) Return whether the module is a package. + + The fullname is a str. Raises ImportError if the module cannot be found. + """ raise ImportError diff --git a/Misc/NEWS.d/next/Library/2023-03-17-09-53-23.gh-issue-63062.0gwxz6.rst b/Misc/NEWS.d/next/Library/2023-03-17-09-53-23.gh-issue-63062.0gwxz6.rst new file mode 100644 index 00000000000000..392d228efbd605 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-17-09-53-23.gh-issue-63062.0gwxz6.rst @@ -0,0 +1 @@ +__subclasshook__() implemented for the Finders and Loaders In the abc lib. Patched by Furkan Onder and Eric Snow.