1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
From 1fb4701628f844eca0f73f29b502457d4b7e4d3c Mon Sep 17 00:00:00 2001
From: Borut Mrak <borut.mrak@acex.si>
Date: Thu, 5 Jun 2025 21:16:59 +0200
Subject: replace the imp library import for compatibility with python 3.12+
--- a/policyd_rate_limit/utils.py
+++ b/policyd_rate_limit/utils.py
@@ -15,7 +15,8 @@
import collections
import ipaddress
import time
-import imp
+import importlib.util
+import importlib.machinery
import pwd
import grp
import warnings
@@ -35,6 +36,18 @@
return ipaddress.IPv6Network(ip_str)
+
+def load_source(modname, filename):
+ # Replacing imp.load_source() according to https://docs.python.org/3/whatsnew/3.12.html#imp
+ loader = importlib.machinery.SourceFileLoader(modname, filename)
+ spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
+ module = importlib.util.module_from_spec(spec)
+ # The module is always executed and not cached in sys.modules.
+ # Uncomment the following line to cache the module.
+ # sys.modules[module.__name__] = module
+ loader.exec_module(module)
+ return module
+
class Exit(Exception):
pass
@@ -63,7 +76,7 @@
try:
# compatibility with old config style in a python module
if config_file.endswith(".conf"): # pragma: no cover (deprecated)
- self._config = imp.load_source('config', config_file)
+ self._config = load_source('config', config_file)
warnings.warn(
(
"New configuration use a .yaml file. "
@@ -71,7 +84,7 @@
),
stacklevel=3
)
- cache_file = imp.cache_from_source(config_file)
+ cache_file = importlib.util.cache_from_source(config_file)
# remove the config pyc file
try:
os.remove(cache_file)
|