Skip to content

Commit 7050798

Browse files
committed
Fix NamedTuple hash always returning 0 on Python 3.14+
In Python 3.14, CPython added hash caching to tuples (gh-131525). PyTupleObject gained an ob_hash field that is checked by tuple_hash() before computing: if ob_hash != -1, the cached value is returned immediately. When msgspec allocates NamedTuples via tp_alloc (which zero-initializes memory via calloc), ob_hash is set to 0 — a valid cached hash value. This causes hash() to return 0 for all msgspec-deserialized NamedTuples, breaking their use as dictionary keys and in sets. The fix resets ob_hash to -1 ("not yet computed") immediately after tp_alloc in all three NamedTuple construction sites: msgpack decoder, JSON decoder, and convert. The reset is conditional on PY314_PLUS and compiles to a no-op on earlier Python versions. Fixes msgspec#967
1 parent a9ed8f1 commit 7050798

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎src/msgspec/_core.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
#define PY313_PLUS (PY_VERSION_HEX >= 0x030d0000)
2323
#define PY314_PLUS (PY_VERSION_HEX >= 0x030e0000)
2424

25+
/* In Python 3.14+, tuples cache their hash value in ob_hash. When a tuple is
26+
* allocated via tp_alloc (which zero-initializes memory), ob_hash is 0 — a
27+
* valid cached hash — so tuple_hash() returns 0 without computing the real
28+
* hash. Reset it to -1 ("not yet computed") after allocation. */
29+
#if PY314_PLUS
30+
#define MS_TUPLE_RESET_HASH(op) (((PyTupleObject *)(op))->ob_hash = -1)
31+
#else
32+
#define MS_TUPLE_RESET_HASH(op) ((void)0)
33+
#endif
34+
2535
/* Hint to the compiler not to store `x` in a register since it is likely to
2636
* change. Results in much higher performance on GCC, with smaller benefits on
2737
* clang */
@@ -15630,6 +15640,7 @@ mpack_decode_namedtuple(
1563015640
PyTypeObject *nt_type = (PyTypeObject *)(info->class);
1563115641
PyObject *res = nt_type->tp_alloc(nt_type, nfields);
1563215642
if (res == NULL) goto error;
15643+
MS_TUPLE_RESET_HASH(res);
1563315644
for (Py_ssize_t i = 0; i < nfields; i++) {
1563415645
PyTuple_SET_ITEM(res, i, NULL);
1563515646
}
@@ -17909,6 +17920,7 @@ json_decode_namedtuple(JSONDecoderState *self, TypeNode *type, PathNode *path) {
1790917920
PyTypeObject *nt_type = (PyTypeObject *)(info->class);
1791017921
PyObject *out = nt_type->tp_alloc(nt_type, nfields);
1791117922
if (out == NULL) goto error;
17923+
MS_TUPLE_RESET_HASH(out);
1791217924
for (Py_ssize_t i = 0; i < nfields; i++) {
1791317925
PyTuple_SET_ITEM(out, i, NULL);
1791417926
}
@@ -21072,6 +21084,7 @@ convert_seq_to_namedtuple(
2107221084
PyTypeObject *nt_type = (PyTypeObject *)(info->class);
2107321085
PyObject *out = nt_type->tp_alloc(nt_type, nfields);
2107421086
if (out == NULL) goto error;
21087+
MS_TUPLE_RESET_HASH(out);
2107521088
for (Py_ssize_t i = 0; i < nfields; i++) {
2107621089
PyTuple_SET_ITEM(out, i, NULL);
2107721090
}

‎tests/unit/test_common.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,30 @@ class Ex(NamedTuple, Generic[T]):
24932493
assert "Expected `int`, got `str`" in str(rec.value)
24942494

24952495

2496+
def test_namedtuple_hash_preserved_after_roundtrip(self, proto):
2497+
"""Hash of deserialized NamedTuple matches hash of original.
2498+
2499+
Regression test for gh-967. In Python 3.14+, tuples cache their hash
2500+
in ob_hash. When msgspec allocates NamedTuples via tp_alloc (which
2501+
zero-initializes memory), ob_hash was left at 0 — a valid cached hash
2502+
— so hash() returned 0 for all deserialized NamedTuples."""
2503+
2504+
class Key(NamedTuple):
2505+
value: int
2506+
2507+
original = Key(value=42)
2508+
encoded = proto.encode(original)
2509+
decoded = proto.decode(encoded, type=Key)
2510+
2511+
assert original == decoded
2512+
assert hash(original) == hash(decoded)
2513+
2514+
# Verify dict lookup works (requires matching hash)
2515+
d = {original: "found"}
2516+
assert decoded in d
2517+
assert d[decoded] == "found"
2518+
2519+
24962520
class TestDataclass:
24972521
def test_encode_dataclass_err_invalid_dataclass_fields(self, proto):
24982522
@dataclass

‎tests/unit/test_convert.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,23 @@ class Ex3(NamedTuple):
11131113
with pytest.raises(ValidationError, match="Expected `str`, got `int`"):
11141114
convert(msg, Ex3)
11151115

1116+
def test_namedtuple_hash_preserved(self):
1117+
"""Hash of converted NamedTuple matches hash of original.
1118+
1119+
Regression test for gh-967."""
1120+
1121+
class Key(NamedTuple):
1122+
value: int
1123+
1124+
original = Key(value=42)
1125+
converted = convert([42], Key)
1126+
1127+
assert original == converted
1128+
assert hash(original) == hash(converted)
1129+
1130+
d = {original: "found"}
1131+
assert converted in d
1132+
11161133

11171134
class TestDict:
11181135
def test_any_dict(self, dictcls):

0 commit comments

Comments
 (0)