Skip to content

Commit 1d8c0e1

Browse files
committed
ndiff: convert ndifftest to importlib for Python 3.12 support
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
1 parent f5ef3a4 commit 1d8c0e1

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

‎ndiff/ndifftest.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@
1212

1313
import xml.dom.minidom
1414

15-
import imp
15+
import types
16+
import importlib.machinery
17+
18+
# Suggested conversion for imp.load_module from
19+
# https://github.com/python/cpython/issues/104212
20+
def load_module(module_name, filename):
21+
loader = importlib.machinery.SourceFileLoader(module_name, filename)
22+
module = types.ModuleType(loader.name)
23+
module.__file__ = filename
24+
sys.modules[module.__name__] = module
25+
loader.exec_module(module)
26+
return module
27+
1628
dont_write_bytecode = sys.dont_write_bytecode
1729
sys.dont_write_bytecode = True
18-
ndiff = imp.load_source("ndiff", "ndiff.py")
30+
ndiff = load_module("ndiff", "ndiff.py")
1931
for x in dir(ndiff):
2032
if not x.startswith("_"):
2133
globals()[x] = getattr(ndiff, x)

0 commit comments

Comments
 (0)