Debian Codegraph @

debian/patches/python3.12-replace-imp-by-importlib.patch

raw
1 Description: Python 3.12 compatibility: Replace imp by importlib
2 Forwarded: https://sourceforge.net/p/dblatex/patches/12/
3 Author: Miro HronĨok <[email protected]>
4 Last-Update: 2023-06-15
5
6 --- a/lib/dbtexmf/core/dbtex.py
7 +++ b/lib/dbtexmf/core/dbtex.py
8 @@ -15,7 +15,7 @@
9 except ImportError:
10 from urllib.request import pathname2url
11 import glob
12 -import imp
13 +import importlib
14 from optparse import OptionParser
15 from io import open
16
17 @@ -540,15 +540,14 @@
18
19 def load_plugin(self, pathname):
20 moddir, modname = os.path.split(pathname)
21 - try:
22 - filemod, path, descr = imp.find_module(modname, [moddir])
23 - except ImportError:
24 - try:
25 - filemod, path, descr = imp.find_module(modname)
26 - except ImportError:
27 - failed_exit("Error: '%s' module not found" % modname)
28 - mod = imp.load_module(modname, filemod, path, descr)
29 - filemod.close()
30 + spec = importlib.machinery.PathFinder.find_spec(modname, [moddir])
31 + if not spec:
32 + spec = importlib.machinery.PathFinder.find_spec(modname)
33 + if not spec:
34 + failed_exit("Error: '%s' module not found" % modname)
35 + mod = importlib.util.module_from_spec(spec)
36 + spec.loader.exec_module(mod)
37 + sys.modules[modname] = mod
38 return mod
39
40 def run_setup(self, options):
41 --- a/lib/dbtexmf/dblatex/grubber/plugins.py
42 +++ b/lib/dbtexmf/dblatex/grubber/plugins.py
43 @@ -4,7 +4,7 @@
44 Mechanisms to dynamically load extra modules to help the LaTeX compilation.
45 All the modules must be derived from the TexModule class.
46 """
47 -import imp
48 +import importlib
49
50 from os.path import *
51 from dbtexmf.dblatex.grubber.msg import _, msg
52 @@ -108,17 +108,16 @@
53 """
54 if name in self.modules:
55 return 2
56 - try:
57 - file, path, descr = imp.find_module(name, [""])
58 - except ImportError:
59 + spec = importlib.machinery.PathFinder.find_spec(name, [""])
60 + if not spec:
61 if not self.path:
62 return 0
63 - try:
64 - file, path, descr = imp.find_module(name, self.path)
65 - except ImportError:
66 - return 0
67 - module = imp.load_module(name, file, path, descr)
68 - file.close()
69 + spec = importlib.machinery.PathFinder.find_spec(name, self.path)
70 + if not spec:
71 + return 0
72 + module = importlib.util.module_from_spec(spec)
73 + spec.loader.exec_module(module)
74 + sys.modules[name] = module
75 self.modules[name] = module
76 return 1
77
78 --- a/lib/dbtexmf/xslt/xslt.py
79 +++ b/lib/dbtexmf/xslt/xslt.py
80 @@ -2,20 +2,21 @@
81 # Very simple plugin loader for Xslt classes
82 #
83 import os
84 -import imp
85 +import importlib
86 import glob
87 +import sys
88
89 def load(modname):
90 - try:
91 - file, path, descr = imp.find_module(modname, [""])
92 - except ImportError:
93 - try:
94 - file, path, descr = imp.find_module(modname,
95 - [os.path.dirname(__file__)])
96 - except ImportError:
97 - raise ValueError("Xslt '%s' not found" % modname)
98 - mod = imp.load_module(modname, file, path, descr)
99 - file.close()
100 + spec = importlib.machinery.PathFinder.find_spec(modname, [""])
101 + if not spec:
102 + spec = importlib.machinery.PathFinder.find_spec(modname,
103 + [os.path.dirname(__file__)])
104 + if not spec:
105 + raise ValueError("Xslt '%s' not found" % modname)
106 +
107 + mod = importlib.util.module_from_spec(spec)
108 + spec.loader.exec_module(mod)
109 + sys.modules[modname] = mod
110 o = mod.Xslt()
111 return o
112