From 52e2fcd0c53d41139ad7266015fa1b27ef499f3c Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 20 Dec 2023 13:15:34 +0200 Subject: [PATCH] Remove unnecessary Python version checks Signed-off-by: Aarni Koskela --- requirements.txt | 1 - setup.py | 3 --- src/paho/mqtt/client.py | 32 +------------------------------ src/paho/mqtt/properties.py | 18 ++++------------- src/paho/mqtt/reasoncodes.py | 2 -- src/paho/mqtt/subscribeoptions.py | 7 +------ tests/test_websockets.py | 10 ++-------- 7 files changed, 8 insertions(+), 65 deletions(-) diff --git a/requirements.txt b/requirements.txt index bd0dd2ba..5dc884df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ mock==3.0.5 pylama==7.7.1 -pytest==4.6.6; python_version < '3.0' pytest==5.2.2; python_version >= '3.0' pytest-runner==5.2 tox==3.14.0 diff --git a/setup.py b/setup.py index 1f60be0a..e9580ae2 100644 --- a/setup.py +++ b/setup.py @@ -17,9 +17,6 @@ setup_requirements = ['pytest-runner'] if needs_pytest else [] extra_requirements = {'proxy': ['PySocks']} -if sys.version_info < (3, 0): - test_requirements += ['mock'] - setup( name='paho-mqtt', version=__version__, diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 28454b7b..791ff198 100644 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -81,21 +81,10 @@ else: EAGAIN = errno.EAGAIN -# Python 2.7 does not have BlockingIOError. Fall back to IOError -try: - BlockingIOError -except NameError: - BlockingIOError = IOError - MQTTv31 = 3 MQTTv311 = 4 MQTTv5 = 5 -if sys.version_info[0] >= 3: - # define some alias for python2 compatibility - unicode = str - basestring = str - # Message types CONNECT = 0x10 CONNACK = 0x20 @@ -717,14 +706,8 @@ def tls_set_context(self, context=None): if self._ssl_context is not None: raise ValueError('SSL/TLS has already been configured.') - # Assume that have SSL support, or at least that context input behaves like ssl.SSLContext - # in current versions of Python - if context is None: - if hasattr(ssl, 'create_default_context'): - context = ssl.create_default_context() - else: - raise ValueError('SSL/TLS context must be specified') + context = ssl.create_default_context() self._ssl = True self._ssl_context = context @@ -981,9 +964,6 @@ def connect_async(self, host, port=1883, keepalive=60, bind_address="", bind_por raise ValueError('Invalid port number.') if keepalive < 0: raise ValueError('Keepalive must be >=0.') - if bind_address != "" and bind_address is not None: - if sys.version_info < (2, 7) or (3, 0) < sys.version_info < (3, 2): - raise ValueError('bind_address requires Python 2.7 or 3.2.') if bind_port < 0: raise ValueError('Invalid bind port number.') @@ -3283,8 +3263,6 @@ def _handle_suback(self): props, props_len = properties.unpack(packet) reasoncodes = [] for c in packet[props_len:]: - if sys.version_info[0] < 3: - c = ord(c) reasoncodes.append(ReasonCodes(SUBACK >> 4, identifier=c)) else: pack_format = "!" + "B" * len(packet) @@ -3477,8 +3455,6 @@ def _handle_unsuback(self): props, props_len = properties.unpack(packet) reasoncodes = [] for c in packet[props_len:]: - if sys.version_info[0] < 3: - c = ord(c) reasoncodes.append(ReasonCodes(UNSUBACK >> 4, identifier=c)) if len(reasoncodes) == 1: reasoncodes = reasoncodes[0] @@ -3715,12 +3691,6 @@ def _create_socket_connection(self): addr = (self._host, self._port) source = (self._bind_address, self._bind_port) - - if sys.version_info < (2, 7) or (3, 0) < sys.version_info < (3, 2): - # Have to short-circuit here because of unsupported source_address - # param in earlier Python versions. - return socket.create_connection(addr, timeout=self._connect_timeout) - if proxy: return socks.create_connection(addr, timeout=self._connect_timeout, source_address=source, **proxy) else: diff --git a/src/paho/mqtt/properties.py b/src/paho/mqtt/properties.py index dbcf543e..575ca921 100644 --- a/src/paho/mqtt/properties.py +++ b/src/paho/mqtt/properties.py @@ -52,10 +52,8 @@ def readInt32(buf): def writeUTF(data): # data could be a string, or bytes. If string, encode into bytes with utf-8 - if sys.version_info[0] < 3: - data = bytearray(data, 'utf-8') - else: - data = data if type(data) == type(b"") else bytes(data, "utf-8") + if not isinstance(data, bytes): + data = bytes(data, "utf-8") return writeInt16(len(data)) + data @@ -109,10 +107,7 @@ def encode(x): x //= 128 if x > 0: digit |= 0x80 - if sys.version_info[0] >= 3: - buffer += bytes([digit]) - else: - buffer += bytes(chr(digit)) + buffer += bytes([digit]) if x == 0: break return buffer @@ -345,10 +340,7 @@ def writeProperty(self, identifier, type, value): buffer = b"" buffer += VariableByteIntegers.encode(identifier) # identifier if type == self.types.index("Byte"): # value - if sys.version_info[0] < 3: - buffer += chr(value) - else: - buffer += bytes([value]) + buffer += bytes([value]) elif type == self.types.index("Two Byte Integer"): buffer += writeInt16(value) elif type == self.types.index("Four Byte Integer"): @@ -412,8 +404,6 @@ def getNameFromIdent(self, identifier): return rc def unpack(self, buffer): - if sys.version_info[0] < 3: - buffer = bytearray(buffer) self.clear() # deserialize properties into attributes from buffer received from network propslen, VBIlen = VariableByteIntegers.decode(buffer) diff --git a/src/paho/mqtt/reasoncodes.py b/src/paho/mqtt/reasoncodes.py index c42e5ba9..1b54236a 100644 --- a/src/paho/mqtt/reasoncodes.py +++ b/src/paho/mqtt/reasoncodes.py @@ -162,8 +162,6 @@ def set(self, name): def unpack(self, buffer): c = buffer[0] - if sys.version_info[0] < 3: - c = ord(c) name = self.__getName__(self.packetType, c) self.value = self.getId(name) return 1 diff --git a/src/paho/mqtt/subscribeoptions.py b/src/paho/mqtt/subscribeoptions.py index 5b4f0733..a88f4c88 100644 --- a/src/paho/mqtt/subscribeoptions.py +++ b/src/paho/mqtt/subscribeoptions.py @@ -16,7 +16,6 @@ ******************************************************************* """ -import sys class MQTTException(Exception): @@ -74,11 +73,7 @@ def pack(self): retainAsPublished = 1 if self.retainAsPublished else 0 data = [(self.retainHandling << 4) | (retainAsPublished << 3) | (noLocal << 2) | self.QoS] - if sys.version_info[0] >= 3: - buffer = bytes(data) - else: - buffer = bytearray(data) - return buffer + return bytes(data) def unpack(self, buffer): b0 = buffer[0] diff --git a/tests/test_websockets.py b/tests/test_websockets.py index 6aa55f72..12d6ead9 100644 --- a/tests/test_websockets.py +++ b/tests/test_websockets.py @@ -1,10 +1,7 @@ import socket import sys -if sys.version_info < (3, 0): - from mock import Mock -else: - from unittest.mock import Mock +from unittest.mock import Mock import pytest @@ -36,10 +33,7 @@ def iter_response(): it = iter_response() def fakerecv(*args): - if sys.version_info < (3, 0): - return next(it) - else: - return bytes([next(it)]) + return bytes([next(it)]) mocksock = Mock( spec_set=socket.socket,