Skip to content

Commit

Permalink
Remove unnecessary Python version checks
Browse files Browse the repository at this point in the history
Signed-off-by: Aarni Koskela <akx@iki.fi>
  • Loading branch information
akx committed Dec 20, 2023
1 parent 4f06670 commit 52e2fcd
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 65 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand Down
32 changes: 1 addition & 31 deletions src/paho/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.')

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 4 additions & 14 deletions src/paho/mqtt/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/paho/mqtt/reasoncodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions src/paho/mqtt/subscribeoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*******************************************************************
"""

import sys


class MQTTException(Exception):
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 2 additions & 8 deletions tests/test_websockets.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 52e2fcd

Please sign in to comment.