From e87be0bf6cc097b6d9b9155541b0a5050e7402a0 Mon Sep 17 00:00:00 2001 From: Christian Marangi Date: Sun, 28 Apr 2024 21:50:53 +0200 Subject: [PATCH] ndiff: Fix support for Python 3.12 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 https://github.com/python/cpython/issues/104212. Signed-off-by: Christian Marangi --- ndiff/ndifftest.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ndiff/ndifftest.py b/ndiff/ndifftest.py index 27fc5256e8..5f2b308057 100755 --- a/ndiff/ndifftest.py +++ b/ndiff/ndifftest.py @@ -12,10 +12,23 @@ import xml.dom.minidom -import imp +# import os +import importlib.machinery + +# Suggested conversion for imp.load_module from +# https://github.com/python/cpython/issues/104212 +def load_module(module_name, filename): + # script_path = os.path.abspath(filename) + loader = importlib.machinery.SourceFileLoader(module_name, filename) + module = types.ModuleType(loader.name) + module.__file__ = filename + sys.modules[module.__name__] = module + loader.exec_module(module) + return module + dont_write_bytecode = sys.dont_write_bytecode sys.dont_write_bytecode = True -ndiff = imp.load_source("ndiff", "ndiff.py") +ndiff = load_module("ndiff", "ndiff.py") for x in dir(ndiff): if not x.startswith("_"): globals()[x] = getattr(ndiff, x)