Python 3.12 compatibility: Replace imp by importlib

This commit is contained in:
Miro Hrončok 2023-06-15 12:05:32 +02:00
parent 2229af8cb8
commit e7bc3c25d5
2 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,113 @@
diff --git a/lib/dbtexmf/core/dbtex.py b/lib/dbtexmf/core/dbtex.py
index b3ec732..adac781 100644
--- a/lib/dbtexmf/core/dbtex.py
+++ b/lib/dbtexmf/core/dbtex.py
@@ -15,7 +15,7 @@ try:
except ImportError:
from urllib.request import pathname2url
import glob
-import imp
+import importlib
from optparse import OptionParser
from io import open
@@ -540,15 +540,14 @@ class DbTexCommand:
def load_plugin(self, pathname):
moddir, modname = os.path.split(pathname)
- try:
- filemod, path, descr = imp.find_module(modname, [moddir])
- except ImportError:
- try:
- filemod, path, descr = imp.find_module(modname)
- except ImportError:
- failed_exit("Error: '%s' module not found" % modname)
- mod = imp.load_module(modname, filemod, path, descr)
- filemod.close()
+ spec = importlib.machinery.PathFinder.find_spec(modname, [moddir])
+ if not spec:
+ spec = importlib.machinery.PathFinder.find_spec(modname)
+ if not spec:
+ failed_exit("Error: '%s' module not found" % modname)
+ mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(mod)
+ sys.modules[modname] = mod
return mod
def run_setup(self, options):
diff --git a/lib/dbtexmf/dblatex/grubber/plugins.py b/lib/dbtexmf/dblatex/grubber/plugins.py
index 9e333c9..047f2bb 100644
--- a/lib/dbtexmf/dblatex/grubber/plugins.py
+++ b/lib/dbtexmf/dblatex/grubber/plugins.py
@@ -4,7 +4,7 @@
Mechanisms to dynamically load extra modules to help the LaTeX compilation.
All the modules must be derived from the TexModule class.
"""
-import imp
+import importlib
from os.path import *
from dbtexmf.dblatex.grubber.msg import _, msg
@@ -108,17 +108,16 @@ class Plugins (object):
"""
if name in self.modules:
return 2
- try:
- file, path, descr = imp.find_module(name, [""])
- except ImportError:
+ spec = importlib.machinery.PathFinder.find_spec(name, [""])
+ if not spec:
if not self.path:
return 0
- try:
- file, path, descr = imp.find_module(name, self.path)
- except ImportError:
- return 0
- module = imp.load_module(name, file, path, descr)
- file.close()
+ spec = importlib.machinery.PathFinder.find_spec(name, self.path)
+ if not spec:
+ return 0
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ sys.modules[name] = module
self.modules[name] = module
return 1
diff --git a/lib/dbtexmf/xslt/xslt.py b/lib/dbtexmf/xslt/xslt.py
index 0350e30..57c99a2 100644
--- a/lib/dbtexmf/xslt/xslt.py
+++ b/lib/dbtexmf/xslt/xslt.py
@@ -2,20 +2,21 @@
# Very simple plugin loader for Xslt classes
#
import os
-import imp
+import importlib
import glob
+import sys
def load(modname):
- try:
- file, path, descr = imp.find_module(modname, [""])
- except ImportError:
- try:
- file, path, descr = imp.find_module(modname,
- [os.path.dirname(__file__)])
- except ImportError:
- raise ValueError("Xslt '%s' not found" % modname)
- mod = imp.load_module(modname, file, path, descr)
- file.close()
+ spec = importlib.machinery.PathFinder.find_spec(modname, [""])
+ if not spec:
+ spec = importlib.machinery.PathFinder.find_spec(modname,
+ [os.path.dirname(__file__)])
+ if not spec:
+ raise ValueError("Xslt '%s' not found" % modname)
+
+ mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(mod)
+ sys.modules[modname] = mod
o = mod.Xslt()
return o

View File

@ -17,6 +17,8 @@ Source1: COPYING-docbook-xsl
Patch0: dblatex-0.3.11-disable-debian.patch Patch0: dblatex-0.3.11-disable-debian.patch
Patch1: dblatex-0.3.11-which-shutil.patch Patch1: dblatex-0.3.11-which-shutil.patch
Patch2: dblatex-0.3.11-replace-inkscape-by-rsvg.patch Patch2: dblatex-0.3.11-replace-inkscape-by-rsvg.patch
# Patch3 sent upstream: https://sourceforge.net/p/dblatex/patches/12/
Patch3: dblatex-0.3.12-replace-imp-by-importlib.patch
BuildRequires: python3-devel BuildRequires: python3-devel
BuildRequires: python3-setuptools BuildRequires: python3-setuptools