Skip to content

Commit bb4d137

Browse files
committed
tools.checksum: Fix CRC32
The name `data` was being reused for a separate value an causing a crash. Fixes the error: Traceback (most recent call last): File "/home/david/work/pybricks/pybricksdev/.venv/bin/pybricksdev", line 5, in <module> main() File "/home/david/work/pybricks/pybricksdev/pybricksdev/cli/__init__.py", line 413, in main asyncio.run(subparsers.choices[args.tool].tool.run(args)) File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete return future.result() File "/home/david/work/pybricks/pybricksdev/pybricksdev/cli/__init__.py", line 242, in run firmware, metadata = await create_firmware(args.firmware, args.name) File "/home/david/work/pybricks/pybricksdev/pybricksdev/flash.py", line 102, in create_firmware crc32_checksum(io.BytesIO(firmware), metadata["max-firmware-size"] - 4), File "/home/david/work/pybricks/pybricksdev/pybricksdev/tools/checksum.py", line 129, in crc32_checksum data = int.from_bytes(data[index : index + 4], "little") TypeError: 'int' object is not subscriptable Also add type hints while we are touching this.
1 parent cc6988e commit bb4d137

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Fixed
10+
- Fixed crash in CRC32 checksum.
11+
712
## [1.0.0-alpha.12]
813

914
### Changed

pybricksdev/tools/checksum.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ def sum_complement(data: BytesIO, max_size: int) -> int:
8686
)
8787

8888

89-
def _dword(value):
89+
def _dword(value: int) -> int:
9090
return value & 0xFFFFFFFF
9191

9292

93-
def _crc32_fast(crc, data):
93+
def _crc32_fast(crc, data: int) -> int:
9494
crc, data = _dword(crc), _dword(data)
9595
crc ^= data
9696
for _ in range(8):
@@ -126,6 +126,6 @@ def crc32_checksum(data: BytesIO, max_size: int) -> int:
126126

127127
crc = 0xFFFFFFFF
128128
for index in range(0, len(data), 4):
129-
data = int.from_bytes(data[index : index + 4], "little")
130-
crc = _crc32_fast(crc, data)
129+
word = int.from_bytes(data[index : index + 4], "little")
130+
crc = _crc32_fast(crc, word)
131131
return crc

0 commit comments

Comments
 (0)