From 34ecd94ee470e0a146100448e15018e9ddea6d4d Mon Sep 17 00:00:00 2001 From: furkanonder Date: Thu, 16 Mar 2023 21:58:04 +0300 Subject: [PATCH 1/3] Implement __subclasshook__() for Finders and Loaders --- Lib/importlib/_abc.py | 8 ++++++++ Lib/importlib/abc.py | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) 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 8fa9a0f3bc1e4b..710faafd595b42 100644 --- a/Lib/importlib/abc.py +++ b/Lib/importlib/abc.py @@ -88,6 +88,13 @@ class MetaPathFinder(metaclass=abc.ABCMeta): # We don't define find_spec() here since that would break # hasattr checks we do to support backward compatibility. + @classmethod + def __subclasshook__(cls, C): + if cls is MetaPathFinder: + if any('find_module' in B.__dict__ for B in C.__mro__): + return True + return NotImplemented + def find_module(self, fullname, path): """Return a loader for the module. @@ -125,6 +132,13 @@ class PathEntryFinder(metaclass=abc.ABCMeta): # We don't define find_spec() here since that would break # hasattr checks we do to support backward compatibility. + @classmethod + def __subclasshook__(cls, C): + if cls is PathEntryFinder: + if any('find_module' in B.__dict__ for B in C.__mro__): + return True + return NotImplemented + def find_loader(self, fullname): """Return (loader, namespace portion) for the path entry. @@ -176,6 +190,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 @@ -192,11 +214,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 From 8bd3d714a4463e903263887072f69fcdc5b96f90 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 17 Mar 2023 09:53:27 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-03-17-09-53-23.gh-issue-63062.0gwxz6.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-17-09-53-23.gh-issue-63062.0gwxz6.rst 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..0acc5593a8ddca --- /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. From 05f245d6154df691221a6270065837efc70d7be8 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Fri, 31 Mar 2023 13:51:47 +0300 Subject: [PATCH 3/3] Mention the authors --- .../next/Library/2023-03-17-09-53-23.gh-issue-63062.0gwxz6.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 0acc5593a8ddca..392d228efbd605 100644 --- 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 @@ -1 +1 @@ -__subclasshook__() implemented for the Finders and Loaders In the abc lib. +__subclasshook__() implemented for the Finders and Loaders In the abc lib. Patched by Furkan Onder and Eric Snow.