Skip to content

Commit 24b7ac4

Browse files
gh-85943: Fix BytesWarning in the struct format cache under -bb
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>
1 parent dd2faeb commit 24b7ac4

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

Lib/test/test_struct.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ def test_calcsize(self):
182182
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('i'))
183183
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('P'))
184184

185+
def test_cache_bytes_vs_str_bb(self):
186+
# Mixing str and bytes formats must not raise BytesWarning under -bb.
187+
code = (
188+
'import struct\n'
189+
'struct.calcsize(b"!d"); struct.calcsize("!d")\n'
190+
'struct.calcsize(">d"); struct.calcsize(b">d")\n'
191+
'struct.Struct(b"i"); struct.Struct("i")\n'
192+
)
193+
assert_python_ok('-bb', '-c', code)
194+
185195
def test_integers(self):
186196
# Integer tests (bBhHiIlLqQnN).
187197
import binascii
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :mod:`struct` functions raising :exc:`BytesWarning` under the ``-bb``
2+
command line option when a :class:`str` format is used after an equal
3+
:class:`bytes` format (or vice versa). The internal format cache no longer
4+
mixes :class:`str` and :class:`bytes` keys.

Modules/_struct.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,32 +2641,50 @@ static PyType_Spec PyStructType_spec = {
26412641
static int
26422642
cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
26432643
{
2644-
PyObject * s_object;
2644+
PyObject *s_object;
2645+
PyObject *key;
26452646
_structmodulestate *state = get_struct_state(module);
26462647

26472648
if (fmt == NULL) {
26482649
Py_SETREF(*ptr, NULL);
26492650
return 1;
26502651
}
26512652

2652-
if (PyDict_GetItemRef(state->cache, fmt, &s_object) < 0) {
2653+
/* Use a str cache key: an equal str and bytes would collide and be
2654+
compared, raising BytesWarning under -bb. */
2655+
if (PyBytes_Check(fmt)) {
2656+
key = PyUnicode_DecodeASCII(PyBytes_AS_STRING(fmt),
2657+
PyBytes_GET_SIZE(fmt), "surrogateescape");
2658+
if (key == NULL) {
2659+
return 0;
2660+
}
2661+
}
2662+
else {
2663+
key = Py_NewRef(fmt);
2664+
}
2665+
2666+
if (PyDict_GetItemRef(state->cache, key, &s_object) < 0) {
2667+
Py_DECREF(key);
26532668
return 0;
26542669
}
26552670
if (s_object != NULL) {
2671+
Py_DECREF(key);
26562672
*ptr = PyStructObject_CAST(s_object);
26572673
return Py_CLEANUP_SUPPORTED;
26582674
}
26592675

2660-
s_object = PyObject_CallOneArg(state->PyStructType, fmt);
2676+
s_object = PyObject_CallOneArg(state->PyStructType, key);
26612677
if (s_object != NULL) {
26622678
if (PyDict_GET_SIZE(state->cache) >= MAXCACHE)
26632679
PyDict_Clear(state->cache);
26642680
/* Attempt to cache the result */
2665-
if (PyDict_SetItem(state->cache, fmt, s_object) == -1)
2681+
if (PyDict_SetItem(state->cache, key, s_object) == -1)
26662682
PyErr_Clear();
2683+
Py_DECREF(key);
26672684
*ptr = (PyStructObject *)s_object;
26682685
return Py_CLEANUP_SUPPORTED;
26692686
}
2687+
Py_DECREF(key);
26702688
return 0;
26712689
}
26722690

0 commit comments

Comments
 (0)