FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-85943: Fix BytesWarning in the struct format cache under -bb (GH-153627) by miss-islington · Pull Request #153834 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions Lib/test/test_struct.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ def test_calcsize(self):
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('i'))
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('P'))

def test_cache_bytes_vs_str_bb(self):
# Mixing str and bytes formats must not raise BytesWarning under -bb.
code = (
'import struct\n'
'struct.calcsize(b"!d"); struct.calcsize("!d")\n'
'struct.calcsize(">d"); struct.calcsize(b">d")\n'
'struct.Struct(b"i"); struct.Struct("i")\n'
)
assert_python_ok('-bb', '-c', code)

def test_integers(self):
# Integer tests (bBhHiIlLqQnN).
import binascii
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :mod:`struct` functions raising :exc:`BytesWarning` under the ``-bb``
command line option when a :class:`str` format is used after an equal
:class:`bytes` format (or vice versa). The internal format cache no longer
mixes :class:`str` and :class:`bytes` keys.
26 changes: 22 additions & 4 deletions Modules/_struct.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2613,32 +2613,50 @@ static PyType_Spec PyStructType_spec = {
static int
cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
{
PyObject * s_object;
PyObject *s_object;
PyObject *key;
_structmodulestate *state = get_struct_state(module);

if (fmt == NULL) {
Py_SETREF(*ptr, NULL);
return 1;
}

if (PyDict_GetItemRef(state->cache, fmt, &s_object) < 0) {
/* Use a str cache key: an equal str and bytes would collide and be
compared, raising BytesWarning under -bb. */
if (PyBytes_Check(fmt)) {
key = PyUnicode_DecodeASCII(PyBytes_AS_STRING(fmt),
PyBytes_GET_SIZE(fmt), "surrogateescape");
if (key == NULL) {
return 0;
}
}
else {
key = Py_NewRef(fmt);
}

if (PyDict_GetItemRef(state->cache, key, &s_object) < 0) {
Py_DECREF(key);
return 0;
}
if (s_object != NULL) {
Py_DECREF(key);
*ptr = PyStructObject_CAST(s_object);
return Py_CLEANUP_SUPPORTED;
}

s_object = PyObject_CallOneArg(state->PyStructType, fmt);
s_object = PyObject_CallOneArg(state->PyStructType, key);
if (s_object != NULL) {
if (PyDict_GET_SIZE(state->cache) >= MAXCACHE)
PyDict_Clear(state->cache);
/* Attempt to cache the result */
if (PyDict_SetItem(state->cache, fmt, s_object) == -1)
if (PyDict_SetItem(state->cache, key, s_object) == -1)
PyErr_Clear();
Py_DECREF(key);
*ptr = (PyStructObject *)s_object;
return Py_CLEANUP_SUPPORTED;
}
Py_DECREF(key);
return 0;
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL