From 1ae65be0f7e621002e2e29921e0252c1b57a170c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sun, 20 Jun 2021 17:54:52 +0200 Subject: [PATCH] Implement PEP451 semantics for DynamicImporter Make DynamicImporter PEP451-compliant in order to silence ImportWarnings in Python 3.10. This is mostly based on six._SixMetaPathImporter. Fixes #476 --- gi/importer.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gi/importer.py b/gi/importer.py index 32967974..006cf464 100644 --- a/gi/importer.py +++ b/gi/importer.py @@ -24,6 +24,7 @@ import sys import warnings import importlib +import importlib.util from contextlib import contextmanager import gi @@ -103,6 +104,7 @@ def get_import_stacklevel(import_hook): class DynamicImporter(object): # Note: see PEP302 for the Importer Protocol implemented below. + # PEP451 for A ModuleSpec Type for the Import System. def __init__(self, path): self.path = path @@ -117,6 +119,16 @@ class DynamicImporter(object): return self + def find_spec(self, fullname, path, target=None): + if not fullname.startswith(self.path): + return None + + path, namespace = fullname.rsplit('.', 1) + if path != self.path: + return None + + return importlib.util.spec_from_loader(fullname, self) + def load_module(self, fullname): if fullname in sys.modules: return sys.modules[fullname] @@ -149,3 +161,9 @@ class DynamicImporter(object): sys.modules[fullname] = dynamic_module return dynamic_module + + def create_module(self, spec): + return self.load_module(spec.name) + + def exec_module(self, module): + pass -- 2.32.0