From a409b9099f11f67b358478df47366a00403f5aab Mon Sep 17 00:00:00 2001 From: Evgeny Kolesnikov Date: Tue, 7 Nov 2023 12:47:23 +0100 Subject: [PATCH] Fix removal of imp module in Python 3.12 --- swig/openscap_api.py | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/swig/openscap_api.py b/swig/openscap_api.py index f18e004320e..6c24b367a37 100755 --- a/swig/openscap_api.py +++ b/swig/openscap_api.py @@ -31,31 +31,23 @@ logger = logging.getLogger("openscap") -from sys import version_info -if version_info >= (2, 6, 0): - def _import_helper(): - from os.path import dirname - import imp - fp = None - try: - fp, pathname, description = imp.find_module( - '_openscap_py', [dirname(__file__)]) - except ImportError: - import _openscap_py as OSCAP - return OSCAP - if fp is not None: - try: - _mod = imp.load_module( - '_openscap_py', fp, pathname, description) - finally: - fp.close() - return _mod - OSCAP = _import_helper() - del _import_helper -else: - import _openscap_py as OSCAP - -del version_info + +def _import_helper(): + from os.path import dirname + import importlib.machinery + import importlib.util + spec = importlib.machinery.PathFinder.find_spec('_openscap_py', [dirname(__file__)]) + if not spec: + spec = importlib.machinery.PathFinder.find_spec('_openscap_py') + if not spec: + return None + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +OSCAP = _import_helper() +del _import_helper import os