Skip to content

Commit

Permalink
remove remaining tweaks for obsolete python
Browse files Browse the repository at this point in the history
  • Loading branch information
sebres committed Dec 31, 2023
1 parent e1b7720 commit dd4431c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 65 deletions.
27 changes: 9 additions & 18 deletions fail2ban/client/configparserinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,14 @@ class SafeConfigParserWithIncludes(SafeConfigParser):

CONDITIONAL_RE = re.compile(r"^(\w+)(\?.+)$")

if sys.version_info >= (3,2):
# overload constructor only for fancy new Python3's
def __init__(self, share_config=None, *args, **kwargs):
kwargs = kwargs.copy()
kwargs['interpolation'] = BasicInterpolationWithName()
kwargs['inline_comment_prefixes'] = ";"
super(SafeConfigParserWithIncludes, self).__init__(
*args, **kwargs)
self._cfg_share = share_config

else:
def __init__(self, share_config=None, *args, **kwargs):
SafeConfigParser.__init__(self, *args, **kwargs)
self._cfg_share = share_config
# overload constructor only for fancy new Python3's
def __init__(self, share_config=None, *args, **kwargs):
kwargs = kwargs.copy()
kwargs['interpolation'] = BasicInterpolationWithName()
kwargs['inline_comment_prefixes'] = ";"
super(SafeConfigParserWithIncludes, self).__init__(
*args, **kwargs)
self._cfg_share = share_config

def get_ex(self, section, option, raw=False, vars={}):
"""Get an option value for a given section.
Expand Down Expand Up @@ -372,10 +366,7 @@ def read(self, filenames, get_includes=True):
if logSys.getEffectiveLevel() <= logLevel:
logSys.log(logLevel, " Reading file: %s", fileNamesFull[0])
# read file(s) :
if sys.version_info >= (3,2): # pragma: no cover
return SafeConfigParser.read(self, fileNamesFull, encoding='utf-8')
else:
return SafeConfigParser.read(self, fileNamesFull)
return SafeConfigParser.read(self, fileNamesFull, encoding='utf-8')

def merge_section(self, section, options, pref=None):
alls = self.get_sections()
Expand Down
36 changes: 7 additions & 29 deletions fail2ban/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,8 @@ def setLogTarget(self, target):
# Remove the handler.
logger.removeHandler(handler)
# And try to close -- it might be closed already
try:
handler.flush()
handler.close()
except (ValueError, KeyError): # pragma: no cover
# Is known to be thrown after logging was shutdown once
# with older Pythons -- seems to be safe to ignore there
if sys.version_info < (3,) or sys.version_info >= (3, 2):
raise
handler.flush()
handler.close()
# detailed format by deep log levels (as DEBUG=10):
if logger.getEffectiveLevel() <= logging.DEBUG: # pragma: no cover
if self.__verbose is None:
Expand Down Expand Up @@ -931,32 +925,16 @@ def __createDaemon(self): # pragma: no cover
# the default value (configurable).
try:
fdlist = self.__get_fdlist()
maxfd = -1
except:
try:
maxfd = os.sysconf("SC_OPEN_MAX")
except (AttributeError, ValueError):
maxfd = 256 # default maximum
fdlist = range(maxfd+1)

# urandom should not be closed in Python 3.4.0. Fixed in 3.4.1
# http://bugs.python.org/issue21207
if sys.version_info[0:3] == (3, 4, 0): # pragma: no cover
urandom_fd = os.open("/dev/urandom", os.O_RDONLY)
for fd in fdlist:
try:
if not os.path.sameopenfile(urandom_fd, fd):
os.close(fd)
except OSError: # ERROR (ignore)
pass
os.close(urandom_fd)
elif maxfd == -1:
for fd in fdlist:
try:
os.close(fd)
except OSError: # ERROR (ignore)
pass
else:
except:
try:
maxfd = os.sysconf("SC_OPEN_MAX")
except (AttributeError, ValueError):
maxfd = 256 # default maximum
os.closerange(0, maxfd)

# Redirect the standard file descriptors to /dev/null.
Expand Down
13 changes: 3 additions & 10 deletions fail2ban/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
from ..helpers import getLogger, _merge_dicts, uni_decode
from collections import OrderedDict

if sys.version_info >= (3, 3):
import importlib.machinery
else:
import imp
import importlib.machinery

# Gets the instance of the logger.
logSys = getLogger(__name__)
Expand Down Expand Up @@ -355,10 +352,6 @@ def pid_exists(pid):
def load_python_module(pythonModule):
pythonModuleName = os.path.splitext(
os.path.basename(pythonModule))[0]
if sys.version_info >= (3, 3):
mod = importlib.machinery.SourceFileLoader(
pythonModuleName, pythonModule).load_module()
else:
mod = imp.load_source(
pythonModuleName, pythonModule)
mod = importlib.machinery.SourceFileLoader(
pythonModuleName, pythonModule).load_module()
return mod
5 changes: 1 addition & 4 deletions fail2ban/tests/action_d/test_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import unittest
import re
import sys
if sys.version_info >= (3, 3):
import importlib
else:
import imp
import importlib

from ..dummyjail import DummyJail
from ..utils import CONFIG_DIR, asyncserver, Utils, uni_decode
Expand Down
6 changes: 2 additions & 4 deletions fail2ban/tests/misctestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,10 @@ def testmbasename(self):

def testUniConverters(self):
self.assertRaises(Exception, uni_decode,
(b'test' if sys.version_info >= (3,) else 'test'), 'f2b-test::non-existing-encoding')
uni_decode((b'test\xcf' if sys.version_info >= (3,) else 'test\xcf'))
b'test', 'f2b-test::non-existing-encoding')
uni_decode(b'test\xcf')
uni_string(b'test\xcf')
uni_string('test\xcf')
if sys.version_info < (3,) and 'PyPy' not in sys.version:
uni_string('test\xcf')

def testSafeLogging(self):
# logging should be exception-safe, to avoid possible errors (concat, str. conversion, representation failures, etc)
Expand Down

0 comments on commit dd4431c

Please sign in to comment.