From 4db77ebf41dd4f1991094d3f568e9b9ab4dfa0ab Mon Sep 17 00:00:00 2001 From: Steven Hiscocks Date: Mon, 15 Apr 2013 20:47:37 +0100 Subject: [PATCH] ENH: Clarify use of bytes in csocket and asyncserver for python3 --- fail2ban/client/csocket.py | 12 ++++++++---- fail2ban/server/asyncserver.py | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/fail2ban/client/csocket.py b/fail2ban/client/csocket.py index 283d6357..346c7a90 100644 --- a/fail2ban/client/csocket.py +++ b/fail2ban/client/csocket.py @@ -31,6 +31,13 @@ __license__ = "GPL" from pickle import dumps, loads, HIGHEST_PROTOCOL import socket, sys +if sys.version_info >= (3,): + # b"" causes SyntaxError in python <= 2.5, so below implements equivalent + EMPTY_BYTES = bytes("", encoding="ascii") +else: + # python 2.x, string type is equivalent to bytes. + EMPTY_BYTES = "" + class CSocket: if sys.version_info >= (3,): @@ -55,10 +62,7 @@ class CSocket: #@staticmethod def receive(sock): - if sys.version_info >= (3,): - msg = bytes("", encoding='ascii') - else: - msg = '' + msg = EMPTY_BYTES while msg.rfind(CSocket.END_STRING) == -1: chunk = sock.recv(6) if chunk == '': diff --git a/fail2ban/server/asyncserver.py b/fail2ban/server/asyncserver.py index 1f46d936..9d506a66 100644 --- a/fail2ban/server/asyncserver.py +++ b/fail2ban/server/asyncserver.py @@ -35,6 +35,13 @@ from fail2ban import helpers # Gets the instance of the logger. logSys = logging.getLogger(__name__) +if sys.version_info >= (3,): + # b"" causes SyntaxError in python <= 2.5, so below implements equivalent + EMPTY_BYTES = bytes("", encoding="ascii") +else: + # python 2.x, string type is equivalent to bytes. + EMPTY_BYTES = "" + ## # Request handler class. # @@ -66,10 +73,7 @@ class RequestHandler(asynchat.async_chat): def found_terminator(self): # Joins the buffer items. - if sys.version_info >= (3,): - message = loads(bytes("", encoding="ascii").join(self.__buffer)) - else: - message = loads("".join(self.__buffer)) + message = loads(EMPTY_BYTES.join(self.__buffer)) # Gives the message to the transmitter. message = self.__transmitter.proceed(message) # Serializes the response.