Skip to content

Commit 448a2a8

Browse files
authored
Remove old method from Python2/3 transition (#1559)
1 parent df6571e commit 448a2a8

File tree

3 files changed

+19
-38
lines changed

3 files changed

+19
-38
lines changed

pymodbus/payload.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
# pylint: disable=missing-type-doc
1313
from struct import pack, unpack
14+
from typing import List
1415

1516
from pymodbus.constants import Endian
1617
from pymodbus.exceptions import ParameterException
1718
from pymodbus.logging import Log
1819
from pymodbus.utilities import (
19-
make_byte_string,
2020
pack_bitstring,
2121
unpack_bitstring,
2222
)
@@ -92,7 +92,7 @@ def __str__(self) -> str:
9292
"""
9393
return self.encode().decode("utf-8")
9494

95-
def reset(self):
95+
def reset(self) -> None:
9696
"""Reset the payload buffer."""
9797
self._payload = []
9898

@@ -111,7 +111,7 @@ def to_registers(self):
111111
Log.debug("{}", payload)
112112
return payload
113113

114-
def to_coils(self):
114+
def to_coils(self) -> List[bool]:
115115
"""Convert the payload buffer into a coil layout that can be used as a context block.
116116
117117
:returns: The coil layout to use as a block
@@ -120,7 +120,7 @@ def to_coils(self):
120120
coils = [bool(int(bit)) for reg in payload for bit in format(reg, "016b")]
121121
return coils
122122

123-
def build(self):
123+
def build(self) -> List[bytes]:
124124
"""Return the payload buffer as a list.
125125
126126
This list is two bytes per element and can
@@ -133,7 +133,7 @@ def build(self):
133133
buffer += b"\x00" * (length % 2)
134134
return [buffer[i : i + 2] for i in range(0, length, 2)]
135135

136-
def add_bits(self, values):
136+
def add_bits(self, values: List[bool]) -> None:
137137
"""Add a collection of bits to be encoded.
138138
139139
If these are less than a multiple of eight,
@@ -145,23 +145,23 @@ def add_bits(self, values):
145145
value = pack_bitstring(values)
146146
self._payload.append(value)
147147

148-
def add_8bit_uint(self, value):
148+
def add_8bit_uint(self, value: int) -> None:
149149
"""Add a 8 bit unsigned int to the buffer.
150150
151151
:param value: The value to add to the buffer
152152
"""
153153
fstring = self._byteorder + "B"
154154
self._payload.append(pack(fstring, value))
155155

156-
def add_16bit_uint(self, value):
156+
def add_16bit_uint(self, value: int) -> None:
157157
"""Add a 16 bit unsigned int to the buffer.
158158
159159
:param value: The value to add to the buffer
160160
"""
161161
fstring = self._byteorder + "H"
162162
self._payload.append(pack(fstring, value))
163163

164-
def add_32bit_uint(self, value):
164+
def add_32bit_uint(self, value: int) -> None:
165165
"""Add a 32 bit unsigned int to the buffer.
166166
167167
:param value: The value to add to the buffer
@@ -171,7 +171,7 @@ def add_32bit_uint(self, value):
171171
p_string = self._pack_words(fstring, value)
172172
self._payload.append(p_string)
173173

174-
def add_64bit_uint(self, value):
174+
def add_64bit_uint(self, value: int) -> None:
175175
"""Add a 64 bit unsigned int to the buffer.
176176
177177
:param value: The value to add to the buffer
@@ -180,23 +180,23 @@ def add_64bit_uint(self, value):
180180
p_string = self._pack_words(fstring, value)
181181
self._payload.append(p_string)
182182

183-
def add_8bit_int(self, value):
183+
def add_8bit_int(self, value: int) -> None:
184184
"""Add a 8 bit signed int to the buffer.
185185
186186
:param value: The value to add to the buffer
187187
"""
188188
fstring = self._byteorder + "b"
189189
self._payload.append(pack(fstring, value))
190190

191-
def add_16bit_int(self, value):
191+
def add_16bit_int(self, value: int) -> None:
192192
"""Add a 16 bit signed int to the buffer.
193193
194194
:param value: The value to add to the buffer
195195
"""
196196
fstring = self._byteorder + "h"
197197
self._payload.append(pack(fstring, value))
198198

199-
def add_32bit_int(self, value):
199+
def add_32bit_int(self, value: int) -> None:
200200
"""Add a 32 bit signed int to the buffer.
201201
202202
:param value: The value to add to the buffer
@@ -205,7 +205,7 @@ def add_32bit_int(self, value):
205205
p_string = self._pack_words(fstring, value)
206206
self._payload.append(p_string)
207207

208-
def add_64bit_int(self, value):
208+
def add_64bit_int(self, value: int) -> None:
209209
"""Add a 64 bit signed int to the buffer.
210210
211211
:param value: The value to add to the buffer
@@ -214,7 +214,7 @@ def add_64bit_int(self, value):
214214
p_string = self._pack_words(fstring, value)
215215
self._payload.append(p_string)
216216

217-
def add_16bit_float(self, value):
217+
def add_16bit_float(self, value: float) -> None:
218218
"""Add a 16 bit float to the buffer.
219219
220220
:param value: The value to add to the buffer
@@ -223,7 +223,7 @@ def add_16bit_float(self, value):
223223
p_string = self._pack_words(fstring, value)
224224
self._payload.append(p_string)
225225

226-
def add_32bit_float(self, value):
226+
def add_32bit_float(self, value: float) -> None:
227227
"""Add a 32 bit float to the buffer.
228228
229229
:param value: The value to add to the buffer
@@ -232,7 +232,7 @@ def add_32bit_float(self, value):
232232
p_string = self._pack_words(fstring, value)
233233
self._payload.append(p_string)
234234

235-
def add_64bit_float(self, value):
235+
def add_64bit_float(self, value: float) -> None:
236236
"""Add a 64 bit float(double) to the buffer.
237237
238238
:param value: The value to add to the buffer
@@ -241,14 +241,13 @@ def add_64bit_float(self, value):
241241
p_string = self._pack_words(fstring, value)
242242
self._payload.append(p_string)
243243

244-
def add_string(self, value):
244+
def add_string(self, value: str) -> None:
245245
"""Add a string to the buffer.
246246
247247
:param value: The value to add to the buffer
248248
"""
249-
value = make_byte_string(value)
250249
fstring = self._byteorder + str(len(value)) + "s"
251-
self._payload.append(pack(fstring, value))
250+
self._payload.append(pack(fstring, value.encode()))
252251

253252

254253
class BinaryPayloadDecoder:
@@ -339,7 +338,6 @@ def _unpack_words(self, fstring, handle):
339338
:param handle: Value to be unpacked
340339
:return:
341340
"""
342-
handle = make_byte_string(handle)
343341
wc_value = WC.get(fstring.lower()) // 2
344342
handle = unpack(f"!{wc_value}H", handle)
345343
if self._wordorder == Endian.Little:
@@ -360,30 +358,26 @@ def decode_8bit_uint(self):
360358
self._pointer += 1
361359
fstring = self._byteorder + "B"
362360
handle = self._payload[self._pointer - 1 : self._pointer]
363-
handle = make_byte_string(handle)
364361
return unpack(fstring, handle)[0]
365362

366363
def decode_bits(self, package_len=1):
367364
"""Decode a byte worth of bits from the buffer."""
368365
self._pointer += package_len
369366
# fstring = self._endian + "B"
370367
handle = self._payload[self._pointer - 1 : self._pointer]
371-
handle = make_byte_string(handle)
372368
return unpack_bitstring(handle)
373369

374370
def decode_16bit_uint(self):
375371
"""Decode a 16 bit unsigned int from the buffer."""
376372
self._pointer += 2
377373
fstring = self._byteorder + "H"
378374
handle = self._payload[self._pointer - 2 : self._pointer]
379-
handle = make_byte_string(handle)
380375
return unpack(fstring, handle)[0]
381376

382377
def decode_32bit_uint(self):
383378
"""Decode a 32 bit unsigned int from the buffer."""
384379
self._pointer += 4
385380
fstring = "I"
386-
# fstring = "I"
387381
handle = self._payload[self._pointer - 4 : self._pointer]
388382
handle = self._unpack_words(fstring, handle)
389383
return unpack("!" + fstring, handle)[0]
@@ -401,15 +395,13 @@ def decode_8bit_int(self):
401395
self._pointer += 1
402396
fstring = self._byteorder + "b"
403397
handle = self._payload[self._pointer - 1 : self._pointer]
404-
handle = make_byte_string(handle)
405398
return unpack(fstring, handle)[0]
406399

407400
def decode_16bit_int(self):
408401
"""Decode a 16 bit signed int from the buffer."""
409402
self._pointer += 2
410403
fstring = self._byteorder + "h"
411404
handle = self._payload[self._pointer - 2 : self._pointer]
412-
handle = make_byte_string(handle)
413405
return unpack(fstring, handle)[0]
414406

415407
def decode_32bit_int(self):

pymodbus/utilities.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,6 @@ def unpack_bitstring(string):
153153
return bits
154154

155155

156-
def make_byte_string(byte_string):
157-
"""Return byte string from a given string, python3 specific fix.
158-
159-
:param byte_string:
160-
:return:
161-
"""
162-
if isinstance(byte_string, str):
163-
byte_string = byte_string.encode()
164-
return byte_string
165-
166-
167156
# --------------------------------------------------------------------------- #
168157
# Error Detection Functions
169158
# --------------------------------------------------------------------------- #

test/test_payload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_little_endian_payload_builder(self):
5757
builder.add_32bit_float(1.25)
5858
builder.add_64bit_float(6.25)
5959
builder.add_16bit_uint(1) # placeholder
60-
builder.add_string(b"test")
60+
builder.add_string("test")
6161
builder.add_bits(self.bitstring)
6262
assert self.little_endian_payload == builder.encode()
6363

0 commit comments

Comments
 (0)