Skip to content

Commit 30c8c39

Browse files
authored
Merge pull request #2048 from evgenyz/fix-python-imp
Fix removal of imp module in Python 3.12
2 parents dc2ecea + 7560152 commit 30c8c39

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

swig/openscap_api.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,24 @@
3131

3232
logger = logging.getLogger("openscap")
3333

34-
from sys import version_info
35-
if version_info >= (2, 6, 0):
36-
def _import_helper():
37-
from os.path import dirname
38-
import imp
39-
fp = None
40-
try:
41-
fp, pathname, description = imp.find_module(
42-
'_openscap_py', [dirname(__file__)])
43-
except ImportError:
44-
import _openscap_py as OSCAP
45-
return OSCAP
46-
if fp is not None:
47-
try:
48-
_mod = imp.load_module(
49-
'_openscap_py', fp, pathname, description)
50-
finally:
51-
fp.close()
52-
return _mod
53-
OSCAP = _import_helper()
54-
del _import_helper
55-
else:
56-
import _openscap_py as OSCAP
57-
58-
del version_info
34+
35+
def _import_helper():
36+
from os.path import dirname
37+
import importlib.machinery
38+
import importlib.util
39+
spec = importlib.machinery.PathFinder.find_spec('_openscap_py', [dirname(__file__)])
40+
if not spec:
41+
spec = importlib.machinery.PathFinder.find_spec('_openscap_py')
42+
if not spec:
43+
raise ImportError("Unable to import _openscap_py module, extra path: '%s'."
44+
% dirname(__file__))
45+
mod = importlib.util.module_from_spec(spec)
46+
spec.loader.exec_module(mod)
47+
return mod
48+
49+
50+
OSCAP = _import_helper()
51+
del _import_helper
5952

6053
import os
6154

0 commit comments

Comments
 (0)