demandimport: fix compatibility with meta path finders w/o find_spec() method stable
authorManuel Jacob <me@manueljacob.de>
Tue, 12 May 2020 01:03:12 +0200
branchstable
changeset 44819 a6e12d477595
parent 44818 9d7d53771e5f
child 44820 f71c8eea7161
demandimport: fix compatibility with meta path finders w/o find_spec() method Meta path finders got a find_spec() method in Python version 3.4. The sys.meta_path documentation says that the deprecated find_module() method is used as a fallback. Setuptool’s VendorImporter still doesn’t have the find_spec() method, which resulted in a crash e.g. within a virtual environment. For reference, I opened an issue for that: https://github.com/pypa/setuptools/issues/2104. An alternative implementation would have been to implement a wrapper for find_module() itself and raise an AttributeError when accessing find_spec() if the wrapped finder doesn’t have it.
hgdemandimport/demandimportpy3.py
--- a/hgdemandimport/demandimportpy3.py	Thu May 07 23:40:05 2020 +0200
+++ b/hgdemandimport/demandimportpy3.py	Tue May 12 01:03:12 2020 +0200
@@ -97,9 +97,18 @@
     def __setattr__(self, name, value):
         return setattr(object.__getattribute__(self, "_finder"), name, value)
 
-    def find_spec(self, *args, **kwargs):
+    def find_spec(self, fullname, path, target=None):
         finder = object.__getattribute__(self, "_finder")
-        spec = finder.find_spec(*args, **kwargs)
+        try:
+            find_spec = finder.find_spec
+        except AttributeError:
+            loader = finder.find_module(fullname, path)
+            if loader is None:
+                spec = None
+            else:
+                spec = importlib.util.spec_from_loader(fullname, loader)
+        else:
+            spec = find_spec(fullname, path, target)
 
         # Lazy loader requires exec_module().
         if (