Skip to content

gh-85943: Fix BytesWarning in the struct format cache under -bb#153627

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:fix-struct-cache-byteswarning
Open

gh-85943: Fix BytesWarning in the struct format cache under -bb#153627
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:fix-struct-cache-byteswarning

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

The struct module caches compiled Struct objects in a dict keyed by the
format string. Since an ASCII str and the equal bytes object have the same
hash, using a str format after an equal bytes format (or vice versa) makes
the dict compare keys of different types, which raises BytesWarning under the
-bb command line option:

$ ./python -bb
>>> import struct
>>> struct.calcsize(b'!d')
8
>>> struct.calcsize('!d')
BytesWarning: Comparison between bytes and string

Fix it by decoding a bytes format to str (the same way Struct() already
does) before using it as the cache key, so equal str and bytes formats map
to a single entry and are never compared to each other.

Normalize bytes format strings to str before using them as the cache key,
so that equal str and bytes formats no longer collide and get compared.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant