diff --git a/fail2ban/client/configparserinc.py b/fail2ban/client/configparserinc.py index 7a93696e..11b9a461 100644 --- a/fail2ban/client/configparserinc.py +++ b/fail2ban/client/configparserinc.py @@ -116,20 +116,14 @@ after = 1.conf 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. @@ -372,10 +366,7 @@ after = 1.conf 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() diff --git a/fail2ban/server/server.py b/fail2ban/server/server.py index 996ca633..7c6fc2f7 100644 --- a/fail2ban/server/server.py +++ b/fail2ban/server/server.py @@ -720,14 +720,8 @@ class Server: # 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: @@ -931,32 +925,16 @@ class Server: # 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. diff --git a/fail2ban/server/utils.py b/fail2ban/server/utils.py index 729ee43f..02a6bc6d 100644 --- a/fail2ban/server/utils.py +++ b/fail2ban/server/utils.py @@ -32,10 +32,7 @@ import time 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__) @@ -355,10 +352,6 @@ class Utils(): 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 diff --git a/fail2ban/tests/action_d/test_smtp.py b/fail2ban/tests/action_d/test_smtp.py index 902b9321..6ad99978 100644 --- a/fail2ban/tests/action_d/test_smtp.py +++ b/fail2ban/tests/action_d/test_smtp.py @@ -22,10 +22,7 @@ import threading 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 diff --git a/fail2ban/tests/misctestcase.py b/fail2ban/tests/misctestcase.py index 43fe35a8..1776028d 100644 --- a/fail2ban/tests/misctestcase.py +++ b/fail2ban/tests/misctestcase.py @@ -190,12 +190,10 @@ class TestsUtilsTest(LogCaptureTestCase): 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)