From cf53a834f7b1ed623e5ff81cb6a4ffedcc1d48e4 Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 15 Feb 2017 18:46:00 +0100 Subject: [PATCH] python-3.6 compatibility: - dynamical string replacement within call of regexp.sub fixed with lambda-replacement (otherwise "sre_constants.error: bad escape \s at position"); - optional arguments (3.6 has more agrs by calling of SMTPServer.process_message); - implicit convert byte to string, because python3.6 fails on binary data (test_smtp). --- fail2ban/server/datedetector.py | 2 +- fail2ban/tests/action_d/test_smtp.py | 8 ++++---- fail2ban/tests/servertestcase.py | 2 +- fail2ban/tests/utils.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/fail2ban/server/datedetector.py b/fail2ban/server/datedetector.py index cccbf71a..dd5d198f 100644 --- a/fail2ban/server/datedetector.py +++ b/fail2ban/server/datedetector.py @@ -284,7 +284,7 @@ class DateDetector(object): if preMatch is not None: # get cached or create a copy with modified name/pattern, using preMatch replacement for {DATE}: template = _getAnchoredTemplate(template, - wrap=lambda s: RE_DATE_PREMATCH.sub(s, preMatch)) + wrap=lambda s: RE_DATE_PREMATCH.sub(lambda m: s, preMatch)) # append date detector template (ignore duplicate if some was added before default): self._appendTemplate(template, ignoreDup=ignoreDup) diff --git a/fail2ban/tests/action_d/test_smtp.py b/fail2ban/tests/action_d/test_smtp.py index 8334f948..bf8e83a2 100644 --- a/fail2ban/tests/action_d/test_smtp.py +++ b/fail2ban/tests/action_d/test_smtp.py @@ -30,7 +30,7 @@ else: from ..dummyjail import DummyJail -from ..utils import CONFIG_DIR, asyncserver, Utils +from ..utils import CONFIG_DIR, asyncserver, Utils, uni_decode class TestSMTPServer(smtpd.SMTPServer): @@ -38,13 +38,13 @@ class TestSMTPServer(smtpd.SMTPServer): smtpd.SMTPServer.__init__(self, *args) self.ready = False - def process_message(self, peer, mailfrom, rcpttos, data): + def process_message(self, peer, mailfrom, rcpttos, data, **kwargs): self.peer = peer self.mailfrom = mailfrom self.rcpttos = rcpttos self.org_data = data - # replace new line (with tab or space) for possible mime translations (word wrap): - self.data = re.sub(r"\n[\t ]", " ", data) + # replace new line (with tab or space) for possible mime translations (word wrap), + self.data = re.sub(r"\n[\t ]", " ", uni_decode(data)) self.ready = True diff --git a/fail2ban/tests/servertestcase.py b/fail2ban/tests/servertestcase.py index 9423ee12..2f1f5451 100644 --- a/fail2ban/tests/servertestcase.py +++ b/fail2ban/tests/servertestcase.py @@ -1647,7 +1647,7 @@ class ServerConfigReaderTests(LogCaptureTestCase): r' echo mail \1 ) | cat', realCmd) # replace abuse retrieving (possible no-network): realCmd = re.sub(r'[^\n]+\bADDRESSES=\$\(dig\s[^\n]+', - 'ADDRESSES="abuse-1@abuse-test-server, abuse-2@abuse-test-server"', realCmd) + lambda m: 'ADDRESSES="abuse-1@abuse-test-server, abuse-2@abuse-test-server"', realCmd) # execute action: return _actions.CommandAction.executeCmd(realCmd, timeout=timeout) diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index ae543e3d..78a42d09 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -37,7 +37,7 @@ import unittest from cStringIO import StringIO from functools import wraps -from ..helpers import getLogger, str2LogLevel, getVerbosityFormat +from ..helpers import getLogger, str2LogLevel, getVerbosityFormat, uni_decode from ..server.ipdns import DNSUtils from ..server.mytime import MyTime from ..server.utils import Utils