Skip to content

Fixed KeyError on bare crc16 and crc codecs#47

Open
gaoflow wants to merge 1 commit into
dhondta:mainfrom
gaoflow:fix-crc-bare-name-defaults
Open

Fixed KeyError on bare crc16 and crc codecs#47
gaoflow wants to merge 1 commit into
dhondta:mainfrom
gaoflow:fix-crc-bare-name-defaults

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

codext.encode(data, "crc16") — the most common CRC width — raises KeyError: '', and so does a bare crc, while every other width resolves fine with a bare name:

>>> codext.encode(b"123456789", "crc16")
KeyError: ''
>>> codext.encode(b"123456789", "crc8")
b'f4'

Root cause: in checksums/crc.py a bare crcN resolves to the '' key of that width's variant table. Every width defines that '' default except width 16, and the top-level crc-a table only has 'a' — so those two are the only bare names that crash.

I checked the rest of the table is sound: crc("123456789", …) matches the stored check value for all 162 registered variants (0 mismatches), so the only defect is the two missing defaults. This adds them:

  • crc16 → CRC-16/ARC (poly 0x8005) = reveng's canonical "CRC-16", identical to the existing arc/ibm/lha entries → bb3d
  • crc → CRC-A, identical to crcabf05

The test extends the existing checksum test with an invariant that every registered crc<width> (and bare crc) resolves without a variant suffix and equals its default check, so a future width added without a '' default would be caught.

If you'd rather keep bare crc16 explicit, the alternative is to raise a clear ValueError ("ambiguous, specify a variant") instead of KeyError — I defaulted it to stay consistent with the other 16 widths.

The width-16 and crc-a variant tables were the only ones without a ''
default entry, so encode(data, "crc16") and bare "crc" raised
KeyError: '' instead of resolving like every other width. Default them
to CRC-16/ARC and CRC-A respectively (both already present as named
aliases).
@mergify

mergify Bot commented Jul 19, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

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.

1 participant