Skip to content

Commit a581fcb

Browse files
authored
Merge pull request #132 from grisha/python3.12-patch
Python3.12 patch
2 parents d19b61f + 863c967 commit a581fcb

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Feb 25 2024 - 3.5.0.2 released. Address the deprecation of the imp module in
2+
Python 3.12.
3+
14
Aug 18 2023 - 3.5.0.1 released. It addresses compatibility issues with
25
Python 3.11.
36

lib/python/mod_python/cgihandler.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
from . import apache
21-
import imp
21+
import importlib.util
2222
import os
2323
import sys
2424

@@ -86,24 +86,35 @@ def handler(req):
8686
# simulate cgi environment
8787
env, si, so = apache.setup_cgi(req)
8888

89+
scriptPath = os.path.join(dir, file)
90+
91+
if not os.path.exists(scriptPath):
92+
raise apache.SERVER_RETURN(apache.HTTP_NOT_FOUND)
93+
94+
# avoid loading modules outside dir
95+
# (e.g. shenaningans like ../../../../etc/passwd)
96+
scriptPath = os.path.abspath(scriptPath)
97+
if not scriptPath.startswith(dir):
98+
raise apache.SERVER_RETURN(apache.HTTP_NOT_FOUND)
99+
89100
try:
90101
# we do not search the pythonpath (security reasons)
91-
fd, path, desc = imp.find_module(module_name, [dir])
92-
except ImportError:
102+
spec = importlib.util.spec_from_file_location(module_name, scriptPath)
103+
except (ModuleNotFoundError, ValueError):
93104
raise apache.SERVER_RETURN(apache.HTTP_NOT_FOUND)
94105

95-
# this executes the module
96-
imp.load_module(module_name, fd, path, desc)
106+
if spec is None:
107+
raise apache.SERVER_RETURN(apache.HTTP_NOT_FOUND)
108+
109+
module = importlib.util.module_from_spec(spec)
110+
sys.modules[module_name] = module
111+
spec.loader.exec_module(module)
97112

98113
return apache.OK
99114

100115
finally:
101116
# unsimulate the cgi environment
102117
apache.restore_nocgi(env, si, so)
103-
try:
104-
fd.close()
105-
except: pass
106118
os.chdir(cwd)
107119
finally:
108120
_lock.release()
109-

lib/python/mod_python/publisher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import sys
3535
import os
3636
from os.path import exists, isabs, normpath, split, isfile, join, dirname
37-
import imp
37+
import importlib.machinery
3838
import re
3939
import base64
4040

@@ -43,7 +43,7 @@
4343
import collections
4444

4545

46-
imp_suffixes = " ".join([x[0][1:] for x in imp.get_suffixes()])
46+
imp_suffixes = " ".join(x[1:] for x in importlib.machinery.all_suffixes())
4747

4848
# Python 2/3 compat workaround
4949
PY2 = sys.version[0] == '2'

0 commit comments

Comments
 (0)