1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
From 1ae65be0f7e621002e2e29921e0252c1b57a170c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny@gentoo.org>
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
|