FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-63062: Implement __subclasshook__() for Finders and Loaders in abc lib by furkanonder · Pull Request #102763 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 8 additions & 0 deletions Lib/importlib/_abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
25 changes: 23 additions & 2 deletions Lib/importlib/abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__subclasshook__() implemented for the Finders and Loaders In the abc lib. Patched by Furkan Onder and Eric Snow.

Back | FazBrowse Home | New Git URL