summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiro HronĨok <miro@hroncok.cz>2021-05-18 12:31:51 +0200
committerChristoph Reiter <reiter.christoph@gmail.com>2021-09-19 22:16:34 +0200
commitf4350ea8c1a752791517b8093778ede51b11b26c (patch)
tree86680282a5538fff9efbf8b06796930c6d366a73
parent66d60f7ab665b0da29e41098a1eef52c8a5b08b4 (diff)
downloadpygobject-f4350ea8c1a752791517b8093778ede51b11b26c.tar.gz
Implement DynamicImporter.find_spec()
On Python 3.10, the code raised an ImportWarning: ImportWarning: DynamicImporter.find_spec() not found; falling back to find_module() See https://docs.python.org/3.10/whatsnew/3.10.html#deprecated > Starting in this release, there will be a concerted effort to begin cleaning > up old import semantics that were kept for Python 2.7 compatibility. > Specifically, find_loader()/find_module() (superseded by find_spec()), > load_module() (superseded by exec_module()), module_repr() > (which the import system takes care of for you), > the __package__ attribute (superseded by __spec__.parent), > the __loader__ attribute (superseded by __spec__.loader), > and the __cached__ attribute (superseded by __spec__.cached) > will slowly be removed (as well as other classes and methods in importlib). > ImportWarning and/or DeprecationWarning will be raised as appropriate to help > identify code which needs updating during this transition. Fixes https://gitlab.gnome.org/GNOME/pygobject/-/issues/473
-rw-r--r--gi/importer.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/gi/importer.py b/gi/importer.py
index 32967974..63788776 100644
--- a/gi/importer.py
+++ b/gi/importer.py
@@ -107,15 +107,20 @@ class DynamicImporter(object):
def __init__(self, path):
self.path = path
- def find_module(self, fullname, path=None):
+ def _find_module_check(self, fullname):
if not fullname.startswith(self.path):
- return
+ return False
path, namespace = fullname.rsplit('.', 1)
- if path != self.path:
- return
+ return path == self.path
+
+ def find_spec(self, fullname, path=None, target=None):
+ if self._find_module_check(fullname):
+ return importlib.util.spec_from_loader(fullname, self)
- return self
+ def find_module(self, fullname, path=None):
+ if self._find_module_check(fullname):
+ return self
def load_module(self, fullname):
if fullname in sys.modules: