Skip to content

Commit 31fa8a6

Browse files
authored
Fix PLC setter functions for memoryview compatibility (#575)
* Fix setter slice assignment for memoryview: convert unpacked tuples to bytes * Use struct.pack directly instead of unpack * Remove redundant type conversion in set_time
1 parent 9ab5c72 commit 31fa8a6

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

snap7/util/setters.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def set_byte(bytearray_: bytearray, byte_index: int, _int: int) -> bytearray:
5757
bytearray(b"\\xFF")
5858
"""
5959
_int = int(_int)
60-
_bytes = struct.pack("B", _int)
61-
bytearray_[byte_index : byte_index + 1] = _bytes
60+
bytearray_[byte_index : byte_index + 1] = struct.pack("B", _int)
6261
return bytearray_
6362

6463

@@ -77,8 +76,7 @@ def set_word(bytearray_: bytearray, byte_index: int, _int: int) -> bytearray:
7776
buffer with the written value
7877
"""
7978
_int = int(_int)
80-
_bytes = struct.unpack("2B", struct.pack(">H", _int))
81-
bytearray_[byte_index : byte_index + 2] = _bytes
79+
bytearray_[byte_index : byte_index + 2] = struct.pack(">H", _int)
8280
return bytearray_
8381

8482

@@ -103,8 +101,7 @@ def set_int(bytearray_: bytearray, byte_index: int, _int: int) -> bytearray:
103101
"""
104102
# make sure were dealing with an int
105103
_int = int(_int)
106-
_bytes = struct.unpack("2B", struct.pack(">h", _int))
107-
bytearray_[byte_index : byte_index + 2] = _bytes
104+
bytearray_[byte_index : byte_index + 2] = struct.pack(">h", _int)
108105
return bytearray_
109106

110107

@@ -130,8 +127,7 @@ def set_uint(bytearray_: bytearray, byte_index: int, _int: int) -> bytearray:
130127
"""
131128
# make sure were dealing with an int
132129
_int = int(_int)
133-
_bytes = struct.unpack("2B", struct.pack(">H", _int))
134-
bytearray_[byte_index : byte_index + 2] = _bytes
130+
bytearray_[byte_index : byte_index + 2] = struct.pack(">H", _int)
135131
return bytearray_
136132

137133

@@ -155,10 +151,7 @@ def set_real(bytearray_: bytearray, byte_index: int, real: Union[bool, str, floa
155151
>>> set_real(data, 0, 123.321)
156152
bytearray(b'B\\xf6\\xa4Z')
157153
"""
158-
real_packed = struct.pack(">f", float(real))
159-
_bytes = struct.unpack("4B", real_packed)
160-
for i, b in enumerate(_bytes):
161-
bytearray_[byte_index + i] = b
154+
bytearray_[byte_index : byte_index + 4] = struct.pack(">f", float(real))
162155
return bytearray_
163156

164157

@@ -274,9 +267,7 @@ def set_dword(bytearray_: bytearray, byte_index: int, dword: int) -> None:
274267
bytearray(b'\\xff\\xff\\xff\\xff')
275268
"""
276269
dword = int(dword)
277-
_bytes = struct.unpack("4B", struct.pack(">I", dword))
278-
for i, b in enumerate(_bytes):
279-
bytearray_[byte_index + i] = b
270+
bytearray_[byte_index : byte_index + 4] = struct.pack(">I", dword)
280271

281272

282273
def set_dint(bytearray_: bytearray, byte_index: int, dint: int) -> None:
@@ -299,9 +290,7 @@ def set_dint(bytearray_: bytearray, byte_index: int, dint: int) -> None:
299290
bytearray(b'\\x7f\\xff\\xff\\xff')
300291
"""
301292
dint = int(dint)
302-
_bytes = struct.unpack("4B", struct.pack(">i", dint))
303-
for i, b in enumerate(_bytes):
304-
bytearray_[byte_index + i] = b
293+
bytearray_[byte_index : byte_index + 4] = struct.pack(">i", dint)
305294

306295

307296
def set_udint(bytearray_: bytearray, byte_index: int, udint: int) -> None:
@@ -324,9 +313,7 @@ def set_udint(bytearray_: bytearray, byte_index: int, udint: int) -> None:
324313
bytearray(b'\\xff\\xff\\xff\\xff')
325314
"""
326315
udint = int(udint)
327-
_bytes = struct.unpack("4B", struct.pack(">I", udint))
328-
for i, b in enumerate(_bytes):
329-
bytearray_[byte_index + i] = b
316+
bytearray_[byte_index : byte_index + 4] = struct.pack(">I", udint)
330317

331318

332319
def set_time(bytearray_: bytearray, byte_index: int, time_string: str) -> bytearray:
@@ -398,8 +385,7 @@ def set_usint(bytearray_: bytearray, byte_index: int, _int: int) -> bytearray:
398385
bytearray(b'\\xff')
399386
"""
400387
_int = int(_int)
401-
_bytes = struct.unpack("B", struct.pack(">B", _int))
402-
bytearray_[byte_index] = _bytes[0]
388+
bytearray_[byte_index] = struct.pack(">B", _int)[0]
403389
return bytearray_
404390

405391

@@ -425,8 +411,7 @@ def set_sint(bytearray_: bytearray, byte_index: int, _int: int) -> bytearray:
425411
bytearray(b'\\x7f')
426412
"""
427413
_int = int(_int)
428-
_bytes = struct.unpack("B", struct.pack(">b", _int))
429-
bytearray_[byte_index] = _bytes[0]
414+
bytearray_[byte_index] = struct.pack(">b", _int)[0]
430415
return bytearray_
431416

432417

@@ -546,6 +531,5 @@ def set_date(bytearray_: bytearray, byte_index: int, date_: date) -> bytearray:
546531
elif date_ > date(2168, 12, 31):
547532
raise ValueError("date is higher than specification allows.")
548533
_days = (date_ - date(1990, 1, 1)).days
549-
_bytes = struct.unpack("2B", struct.pack(">h", _days))
550-
bytearray_[byte_index : byte_index + 2] = _bytes
534+
bytearray_[byte_index : byte_index + 2] = struct.pack(">h", _days)
551535
return bytearray_

0 commit comments

Comments
 (0)