From 304c3cd56693c9ba90ace8b11c656ac2912648ca Mon Sep 17 00:00:00 2001 From: sebres Date: Mon, 27 May 2024 16:18:26 +0200 Subject: [PATCH] improve fix with fallback to local async libraries - add path to compat folder (pyinotify module may have dependency to asyncore module, see https://github.com/fail2ban/fail2ban/issues/3487#issuecomment-2133529081); amend to 054e1d89ca3fa8b767ee21db1a3368f3d890baa8 --- fail2ban/helpers.py | 8 ++++++++ fail2ban/server/asyncserver.py | 15 +++++++-------- fail2ban/server/filterpyinotify.py | 5 +++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/fail2ban/helpers.py b/fail2ban/helpers.py index fe62ae1e..0d7d8ba9 100644 --- a/fail2ban/helpers.py +++ b/fail2ban/helpers.py @@ -40,6 +40,14 @@ except: _libcap = None +# some modules (like pyinotify, see #3487) may have dependency to asyncore, so ensure we've a path +# to compat folder, otherwise python 3.12+ could miss them: +def __extend_compat_path(): + cp = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'compat') + if cp not in sys.path: + sys.path.append(cp) +__extend_compat_path() + PREFER_ENC = locale.getpreferredencoding() # correct preferred encoding if lang not set in environment: if PREFER_ENC.startswith('ANSI_'): # pragma: no cover diff --git a/fail2ban/server/asyncserver.py b/fail2ban/server/asyncserver.py index 0c36d846..62d6cce5 100644 --- a/fail2ban/server/asyncserver.py +++ b/fail2ban/server/asyncserver.py @@ -25,14 +25,6 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" from pickle import dumps, loads, HIGHEST_PROTOCOL -try: - import asynchat -except ImportError: - from ..compat import asynchat -try: - import asyncore -except ImportError: - from ..compat import asyncore import errno import fcntl import os @@ -45,6 +37,13 @@ from .utils import Utils from ..protocol import CSPROTO from ..helpers import logging, getLogger, formatExceptionInfo +# load asyncore and asynchat after helper to ensure we've a path to compat folder: +import asynchat +if asynchat.asyncore: + asyncore = asynchat.asyncore +else: # pragma: no cover - normally unreachable + import asyncore + # Gets the instance of the logger. logSys = getLogger(__name__) diff --git a/fail2ban/server/filterpyinotify.py b/fail2ban/server/filterpyinotify.py index f2f31e6f..9fcbcb56 100644 --- a/fail2ban/server/filterpyinotify.py +++ b/fail2ban/server/filterpyinotify.py @@ -27,14 +27,15 @@ import logging import os from os.path import dirname, sep as pathsep -import pyinotify - from .failmanager import FailManagerEmpty from .filter import FileFilter from .mytime import MyTime, time from .utils import Utils from ..helpers import getLogger +# pyinotify may have dependency to asyncore, so import it after helper to ensure +# we've a path to compat folder: +import pyinotify # Verify that pyinotify is functional on this system # Even though imports -- might be dysfunctional, e.g. as on kfreebsd