Skip to content

Conversation

@LuTiFlekSSer
Copy link
Contributor

Some setter functions in snap7.util fail when writing to a memoryview.
Added conversion of tuples returned by struct.unpack into bytes.
Also replaced some loops with slice assignments.

@gijzelaerr
Copy link
Owner

can you update this pr with the changes in master? i've fixed the test suite.

@LuTiFlekSSer
Copy link
Contributor Author

done

@gijzelaerr
Copy link
Owner

Code Review

I took a deep look at this PR. Here's my analysis:

The Good

The core change is valid and improves code quality. The old pattern was unnecessarily convoluted:

# Old: pack to bytes, unpack to tuple, assign (converts back to bytes)
_bytes = struct.unpack("2B", struct.pack(">H", _int))
bytearray_[byte_index : byte_index + 2] = _bytes

# New: just pack to bytes and assign directly
bytearray_[byte_index : byte_index + 2] = struct.pack(">H", _int)

The memoryview claim is also verified - tuples don't implement the buffer protocol, so the old code fails with memoryview while the new code works:

mv = memoryview(bytearray(4))
mv[0:2] = (255, 255)    # TypeError: a bytes-like object is required
mv[0:2] = b'\xff\xff'   # Works

Issue to Fix

In set_time, there's a redundant bytes() wrapper:

bytes_array = time_int.to_bytes(4, byteorder="big", signed=True)
bytearray_[byte_index : byte_index + 4] = bytes(bytes_array)  # bytes() is unnecessary

int.to_bytes() already returns bytes, so this just creates an unnecessary copy. Please remove the bytes() wrapper.

Summary

  • ✅ Code simplification is genuine and welcome
  • ✅ Memoryview compatibility fix is valid
  • ❌ Remove redundant bytes() in set_time

Once that's fixed, this is good to merge.

@gijzelaerr gijzelaerr merged commit 31fa8a6 into gijzelaerr:master Dec 29, 2025
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants