mirror of https://github.com/fail2ban/fail2ban
RF: Replace old fashioned "except E , e" with "except E as e" (Closes #1537)
parent
0ab042fcce
commit
b875e51cd7
|
@ -176,7 +176,7 @@ class Fail2banClient:
|
||||||
if showRet:
|
if showRet:
|
||||||
self.__logSocketError()
|
self.__logSocketError()
|
||||||
return False
|
return False
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
if showRet:
|
if showRet:
|
||||||
logSys.error(e)
|
logSys.error(e)
|
||||||
return False
|
return False
|
||||||
|
@ -429,7 +429,7 @@ class Fail2banClient:
|
||||||
elif not cmd == "":
|
elif not cmd == "":
|
||||||
try:
|
try:
|
||||||
self.__processCommand(shlex.split(cmd))
|
self.__processCommand(shlex.split(cmd))
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
logSys.error(e)
|
logSys.error(e)
|
||||||
except (EOFError, KeyboardInterrupt):
|
except (EOFError, KeyboardInterrupt):
|
||||||
print
|
print
|
||||||
|
@ -451,7 +451,7 @@ class Fail2banClient:
|
||||||
ret = self.__configurator.getOptions(jail)
|
ret = self.__configurator.getOptions(jail)
|
||||||
self.__configurator.convertToProtocol()
|
self.__configurator.convertToProtocol()
|
||||||
self.__stream = self.__configurator.getConfigStream()
|
self.__stream = self.__configurator.getConfigStream()
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
logSys.error("Failed during configuration: %s" % e)
|
logSys.error("Failed during configuration: %s" % e)
|
||||||
ret = False
|
ret = False
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -127,7 +127,7 @@ class Fail2banServer:
|
||||||
self.__conf["pidfile"],
|
self.__conf["pidfile"],
|
||||||
self.__conf["force"])
|
self.__conf["force"])
|
||||||
return True
|
return True
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
logSys.exception(e)
|
logSys.exception(e)
|
||||||
self.__server.quit()
|
self.__server.quit()
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -168,7 +168,7 @@ after = 1.conf
|
||||||
parser, i = self._getSharedSCPWI(resource)
|
parser, i = self._getSharedSCPWI(resource)
|
||||||
if not i:
|
if not i:
|
||||||
return []
|
return []
|
||||||
except UnicodeDecodeError, e:
|
except UnicodeDecodeError as e:
|
||||||
logSys.error("Error decoding config file '%s': %s" % (resource, e))
|
logSys.error("Error decoding config file '%s': %s" % (resource, e))
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
|
@ -221,7 +221,7 @@ class ConfigReaderUnshared(SafeConfigParserWithIncludes):
|
||||||
if not pOptions is None and option[1] in pOptions:
|
if not pOptions is None and option[1] in pOptions:
|
||||||
continue
|
continue
|
||||||
values[option[1]] = v
|
values[option[1]] = v
|
||||||
except NoSectionError, e:
|
except NoSectionError as e:
|
||||||
# No "Definition" section or wrong basedir
|
# No "Definition" section or wrong basedir
|
||||||
logSys.error(e)
|
logSys.error(e)
|
||||||
values[option[1]] = option[2]
|
values[option[1]] = option[2]
|
||||||
|
|
|
@ -329,7 +329,7 @@ class Fail2banRegex(object):
|
||||||
if ret is not None:
|
if ret is not None:
|
||||||
found = True
|
found = True
|
||||||
regex = self._ignoreregex[ret].inc()
|
regex = self._ignoreregex[ret].inc()
|
||||||
except RegexException, e:
|
except RegexException as e:
|
||||||
output( e )
|
output( e )
|
||||||
return False
|
return False
|
||||||
return found
|
return found
|
||||||
|
@ -346,7 +346,7 @@ class Fail2banRegex(object):
|
||||||
regex = self._failregex[match[0]]
|
regex = self._failregex[match[0]]
|
||||||
regex.inc()
|
regex.inc()
|
||||||
regex.appendIP(match)
|
regex.appendIP(match)
|
||||||
except RegexException, e:
|
except RegexException as e:
|
||||||
output( e )
|
output( e )
|
||||||
return False
|
return False
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
@ -510,7 +510,7 @@ class Fail2banRegex(object):
|
||||||
output( "Use log file : %s" % cmd_log )
|
output( "Use log file : %s" % cmd_log )
|
||||||
output( "Use encoding : %s" % self.encoding )
|
output( "Use encoding : %s" % self.encoding )
|
||||||
test_lines = self.file_lines_gen(hdlr)
|
test_lines = self.file_lines_gen(hdlr)
|
||||||
except IOError, e:
|
except IOError as e:
|
||||||
output( e )
|
output( e )
|
||||||
return False
|
return False
|
||||||
elif cmd_log == "systemd-journal": # pragma: no cover
|
elif cmd_log == "systemd-journal": # pragma: no cover
|
||||||
|
|
|
@ -171,7 +171,7 @@ class JailReader(ConfigReader):
|
||||||
self.__actions.append(action)
|
self.__actions.append(action)
|
||||||
else:
|
else:
|
||||||
raise AttributeError("Unable to read action")
|
raise AttributeError("Unable to read action")
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
logSys.error("Error in action definition " + act)
|
logSys.error("Error in action definition " + act)
|
||||||
logSys.debug("Caught exception: %s" % (e,))
|
logSys.debug("Caught exception: %s" % (e,))
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -42,7 +42,7 @@ if sys.version_info >= (3,):
|
||||||
try:
|
try:
|
||||||
x = json.dumps(x, ensure_ascii=False).encode(
|
x = json.dumps(x, ensure_ascii=False).encode(
|
||||||
locale.getpreferredencoding(), 'replace')
|
locale.getpreferredencoding(), 'replace')
|
||||||
except Exception, e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
logSys.error('json dumps failed: %s', e)
|
logSys.error('json dumps failed: %s', e)
|
||||||
x = '{}'
|
x = '{}'
|
||||||
return x
|
return x
|
||||||
|
@ -51,7 +51,7 @@ if sys.version_info >= (3,):
|
||||||
try:
|
try:
|
||||||
x = json.loads(x.decode(
|
x = json.loads(x.decode(
|
||||||
locale.getpreferredencoding(), 'replace'))
|
locale.getpreferredencoding(), 'replace'))
|
||||||
except Exception, e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
logSys.error('json loads failed: %s', e)
|
logSys.error('json loads failed: %s', e)
|
||||||
x = {}
|
x = {}
|
||||||
return x
|
return x
|
||||||
|
@ -70,7 +70,7 @@ else:
|
||||||
try:
|
try:
|
||||||
x = json.dumps(_normalize(x), ensure_ascii=False).decode(
|
x = json.dumps(_normalize(x), ensure_ascii=False).decode(
|
||||||
locale.getpreferredencoding(), 'replace')
|
locale.getpreferredencoding(), 'replace')
|
||||||
except Exception, e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
logSys.error('json dumps failed: %s', e)
|
logSys.error('json dumps failed: %s', e)
|
||||||
x = '{}'
|
x = '{}'
|
||||||
return x
|
return x
|
||||||
|
@ -79,7 +79,7 @@ else:
|
||||||
try:
|
try:
|
||||||
x = _normalize(json.loads(x.decode(
|
x = _normalize(json.loads(x.decode(
|
||||||
locale.getpreferredencoding(), 'replace')))
|
locale.getpreferredencoding(), 'replace')))
|
||||||
except Exception, e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
logSys.error('json loads failed: %s', e)
|
logSys.error('json loads failed: %s', e)
|
||||||
x = {}
|
x = {}
|
||||||
return x
|
return x
|
||||||
|
@ -175,7 +175,7 @@ class Fail2BanDb(object):
|
||||||
|
|
||||||
logSys.info(
|
logSys.info(
|
||||||
"Connected to fail2ban persistent database '%s'", filename)
|
"Connected to fail2ban persistent database '%s'", filename)
|
||||||
except sqlite3.OperationalError, e:
|
except sqlite3.OperationalError as e:
|
||||||
logSys.error(
|
logSys.error(
|
||||||
"Error connecting to fail2ban persistent database '%s': %s",
|
"Error connecting to fail2ban persistent database '%s': %s",
|
||||||
filename, e.args[0])
|
filename, e.args[0])
|
||||||
|
|
|
@ -105,7 +105,7 @@ class Filter(JailThread):
|
||||||
logSys.warning(
|
logSys.warning(
|
||||||
"Mutliline regex set for jail '%s' "
|
"Mutliline regex set for jail '%s' "
|
||||||
"but maxlines not greater than 1")
|
"but maxlines not greater than 1")
|
||||||
except RegexException, e:
|
except RegexException as e:
|
||||||
logSys.error(e)
|
logSys.error(e)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ class Filter(JailThread):
|
||||||
try:
|
try:
|
||||||
regex = Regex(value)
|
regex = Regex(value)
|
||||||
self.__ignoreRegex.append(regex)
|
self.__ignoreRegex.append(regex)
|
||||||
except RegexException, e:
|
except RegexException as e:
|
||||||
logSys.error(e)
|
logSys.error(e)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
@ -536,7 +536,7 @@ class Filter(JailThread):
|
||||||
failRegex.getMatchedLines()])
|
failRegex.getMatchedLines()])
|
||||||
if not checkAllRegex:
|
if not checkAllRegex:
|
||||||
break
|
break
|
||||||
except RegexException, e: # pragma: no cover - unsure if reachable
|
except RegexException as e: # pragma: no cover - unsure if reachable
|
||||||
logSys.error(e)
|
logSys.error(e)
|
||||||
return failList
|
return failList
|
||||||
|
|
||||||
|
@ -660,15 +660,15 @@ class FileFilter(Filter):
|
||||||
try:
|
try:
|
||||||
has_content = log.open()
|
has_content = log.open()
|
||||||
# see http://python.org/dev/peps/pep-3151/
|
# see http://python.org/dev/peps/pep-3151/
|
||||||
except IOError, e:
|
except IOError as e:
|
||||||
logSys.error("Unable to open %s" % filename)
|
logSys.error("Unable to open %s" % filename)
|
||||||
logSys.exception(e)
|
logSys.exception(e)
|
||||||
return False
|
return False
|
||||||
except OSError, e: # pragma: no cover - requires race condition to tigger this
|
except OSError as e: # pragma: no cover - requires race condition to tigger this
|
||||||
logSys.error("Error opening %s" % filename)
|
logSys.error("Error opening %s" % filename)
|
||||||
logSys.exception(e)
|
logSys.exception(e)
|
||||||
return False
|
return False
|
||||||
except Exception, e: # pragma: no cover - Requires implemention error in FileContainer to generate
|
except Exception as e: # pragma: no cover - Requires implemention error in FileContainer to generate
|
||||||
logSys.error("Internal errror in FileContainer open method - please report as a bug to https://github.com/fail2ban/fail2ban/issues")
|
logSys.error("Internal errror in FileContainer open method - please report as a bug to https://github.com/fail2ban/fail2ban/issues")
|
||||||
logSys.exception(e)
|
logSys.exception(e)
|
||||||
return False
|
return False
|
||||||
|
@ -858,11 +858,11 @@ class DNSUtils:
|
||||||
# retrieve ip (todo: use AF_INET6 for IPv6)
|
# retrieve ip (todo: use AF_INET6 for IPv6)
|
||||||
try:
|
try:
|
||||||
return set([i[4][0] for i in socket.getaddrinfo(dns, None, socket.AF_INET, 0, socket.IPPROTO_TCP)])
|
return set([i[4][0] for i in socket.getaddrinfo(dns, None, socket.AF_INET, 0, socket.IPPROTO_TCP)])
|
||||||
except socket.error, e:
|
except socket.error as e:
|
||||||
logSys.warning("Unable to find a corresponding IP address for %s: %s"
|
logSys.warning("Unable to find a corresponding IP address for %s: %s"
|
||||||
% (dns, e))
|
% (dns, e))
|
||||||
return list()
|
return list()
|
||||||
except socket.error, e:
|
except socket.error as e:
|
||||||
logSys.warning("Socket error raised trying to resolve hostname %s: %s"
|
logSys.warning("Socket error raised trying to resolve hostname %s: %s"
|
||||||
% (dns, e))
|
% (dns, e))
|
||||||
return list()
|
return list()
|
||||||
|
@ -871,7 +871,7 @@ class DNSUtils:
|
||||||
def ipToName(ip):
|
def ipToName(ip):
|
||||||
try:
|
try:
|
||||||
return socket.gethostbyaddr(ip)[0]
|
return socket.gethostbyaddr(ip)[0]
|
||||||
except socket.error, e:
|
except socket.error as e:
|
||||||
logSys.debug("Unable to find a name for the IP %s: %s" % (ip, e))
|
logSys.debug("Unable to find a name for the IP %s: %s" % (ip, e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -138,7 +138,7 @@ class FilterPoll(FileFilter):
|
||||||
logSys.debug("%s has been modified", filename)
|
logSys.debug("%s has been modified", filename)
|
||||||
self.__prevStats[filename] = stats
|
self.__prevStats[filename] = stats
|
||||||
return True
|
return True
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
logSys.error("Unable to get stat on %s because of: %s"
|
logSys.error("Unable to get stat on %s because of: %s"
|
||||||
% (filename, e))
|
% (filename, e))
|
||||||
self.__file404Cnt[filename] += 1
|
self.__file404Cnt[filename] += 1
|
||||||
|
|
|
@ -44,7 +44,7 @@ if not hasattr(pyinotify, '__version__') \
|
||||||
try:
|
try:
|
||||||
manager = pyinotify.WatchManager()
|
manager = pyinotify.WatchManager()
|
||||||
del manager
|
del manager
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
raise ImportError("Pyinotify is probably not functional on this system: %s"
|
raise ImportError("Pyinotify is probably not functional on this system: %s"
|
||||||
% str(e))
|
% str(e))
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ class Jail:
|
||||||
logSys.info("Initiated %r backend" % b)
|
logSys.info("Initiated %r backend" % b)
|
||||||
self.__actions = Actions(self)
|
self.__actions = Actions(self)
|
||||||
return # we are done
|
return # we are done
|
||||||
except ImportError, e:
|
except ImportError as e:
|
||||||
# Log debug if auto, but error if specific
|
# Log debug if auto, but error if specific
|
||||||
logSys.log(
|
logSys.log(
|
||||||
logging.DEBUG if backend == "auto" else logging.ERROR,
|
logging.DEBUG if backend == "auto" else logging.ERROR,
|
||||||
|
|
|
@ -108,20 +108,20 @@ class Server:
|
||||||
pidFile = open(pidfile, 'w')
|
pidFile = open(pidfile, 'w')
|
||||||
pidFile.write("%s\n" % os.getpid())
|
pidFile.write("%s\n" % os.getpid())
|
||||||
pidFile.close()
|
pidFile.close()
|
||||||
except IOError, e:
|
except IOError as e:
|
||||||
logSys.error("Unable to create PID file: %s" % e)
|
logSys.error("Unable to create PID file: %s" % e)
|
||||||
|
|
||||||
# Start the communication
|
# Start the communication
|
||||||
logSys.debug("Starting communication")
|
logSys.debug("Starting communication")
|
||||||
try:
|
try:
|
||||||
self.__asyncServer.start(sock, force)
|
self.__asyncServer.start(sock, force)
|
||||||
except AsyncServerException, e:
|
except AsyncServerException as e:
|
||||||
logSys.error("Could not start server: %s", e)
|
logSys.error("Could not start server: %s", e)
|
||||||
# Removes the PID file.
|
# Removes the PID file.
|
||||||
try:
|
try:
|
||||||
logSys.debug("Remove PID file %s" % pidfile)
|
logSys.debug("Remove PID file %s" % pidfile)
|
||||||
os.remove(pidfile)
|
os.remove(pidfile)
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
logSys.error("Unable to remove PID file: %s" % e)
|
logSys.error("Unable to remove PID file: %s" % e)
|
||||||
logSys.info("Exiting Fail2ban")
|
logSys.info("Exiting Fail2ban")
|
||||||
|
|
||||||
|
@ -543,7 +543,7 @@ class Server:
|
||||||
# the child gets a new PID, making it impossible for its PID to equal its
|
# the child gets a new PID, making it impossible for its PID to equal its
|
||||||
# PGID.
|
# PGID.
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
||||||
|
|
||||||
if pid == 0: # The first child.
|
if pid == 0: # The first child.
|
||||||
|
@ -564,7 +564,7 @@ class Server:
|
||||||
# fork guarantees that the child is no longer a session leader, thus
|
# fork guarantees that the child is no longer a session leader, thus
|
||||||
# preventing the daemon from ever acquiring a controlling terminal.
|
# preventing the daemon from ever acquiring a controlling terminal.
|
||||||
pid = os.fork() # Fork a second child.
|
pid = os.fork() # Fork a second child.
|
||||||
except OSError, e:
|
except OSError as e:
|
||||||
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
||||||
|
|
||||||
if (pid == 0): # The second child.
|
if (pid == 0): # The second child.
|
||||||
|
|
|
@ -56,7 +56,7 @@ class Transmitter:
|
||||||
try:
|
try:
|
||||||
ret = self.__commandHandler(command)
|
ret = self.__commandHandler(command)
|
||||||
ack = 0, ret
|
ack = 0, ret
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
logSys.warning("Command %r has failed. Received %r"
|
logSys.warning("Command %r has failed. Received %r"
|
||||||
% (command, e))
|
% (command, e))
|
||||||
ack = 1, e
|
ack = 1, e
|
||||||
|
|
|
@ -410,7 +410,7 @@ class FilterReaderTest(unittest.TestCase):
|
||||||
# from testcase01
|
# from testcase01
|
||||||
filterReader.get('Definition', 'failregex')
|
filterReader.get('Definition', 'failregex')
|
||||||
filterReader.get('Definition', 'ignoreregex')
|
filterReader.get('Definition', 'ignoreregex')
|
||||||
except Exception, e: # pragma: no cover - failed if reachable
|
except Exception as e: # pragma: no cover - failed if reachable
|
||||||
self.fail('unexpected options after readexplicit: %s' % (e))
|
self.fail('unexpected options after readexplicit: %s' % (e))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,7 @@ def testSampleRegexsFactory(name, basedir):
|
||||||
if jsonREMatch:
|
if jsonREMatch:
|
||||||
try:
|
try:
|
||||||
faildata = json.loads(jsonREMatch.group(1))
|
faildata = json.loads(jsonREMatch.group(1))
|
||||||
except ValueError, e:
|
except ValueError as e:
|
||||||
raise ValueError("%s: %s:%i" %
|
raise ValueError("%s: %s:%i" %
|
||||||
(e, logFile.filename(), logFile.filelineno()))
|
(e, logFile.filename(), logFile.filelineno()))
|
||||||
line = next(logFile)
|
line = next(logFile)
|
||||||
|
|
|
@ -184,13 +184,13 @@ def gatherTests(regexps=None, no_network=False):
|
||||||
try:
|
try:
|
||||||
from ..server.filtergamin import FilterGamin
|
from ..server.filtergamin import FilterGamin
|
||||||
filters.append(FilterGamin)
|
filters.append(FilterGamin)
|
||||||
except Exception, e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
logSys.warning("Skipping gamin backend testing. Got exception '%s'" % e)
|
logSys.warning("Skipping gamin backend testing. Got exception '%s'" % e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from ..server.filterpyinotify import FilterPyinotify
|
from ..server.filterpyinotify import FilterPyinotify
|
||||||
filters.append(FilterPyinotify)
|
filters.append(FilterPyinotify)
|
||||||
except Exception, e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
logSys.warning("I: Skipping pyinotify backend testing. Got exception '%s'" % e)
|
logSys.warning("I: Skipping pyinotify backend testing. Got exception '%s'" % e)
|
||||||
|
|
||||||
for Filter_ in filters:
|
for Filter_ in filters:
|
||||||
|
@ -199,7 +199,7 @@ def gatherTests(regexps=None, no_network=False):
|
||||||
try: # pragma: systemd no cover
|
try: # pragma: systemd no cover
|
||||||
from ..server.filtersystemd import FilterSystemd
|
from ..server.filtersystemd import FilterSystemd
|
||||||
tests.addTest(unittest.makeSuite(filtertestcase.get_monitor_failures_journal_testcase(FilterSystemd)))
|
tests.addTest(unittest.makeSuite(filtertestcase.get_monitor_failures_journal_testcase(FilterSystemd)))
|
||||||
except Exception, e: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
logSys.warning("I: Skipping systemd backend testing. Got exception '%s'" % e)
|
logSys.warning("I: Skipping systemd backend testing. Got exception '%s'" % e)
|
||||||
|
|
||||||
# Server test for logging elements which break logging used to support
|
# Server test for logging elements which break logging used to support
|
||||||
|
|
Loading…
Reference in New Issue