Repository navigation
Commit 7e90e24
Fix NamedTuple hash returning 0 on Python 3.14+ (#980)
* 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 #967
* Fixup
---------
Co-authored-by: Jim Crist-Harif <[email protected]>1 parent 719a216 commit 7e90e24
3 files changed
Lines changed: 53 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
25 | 35 | | |
26 | 36 | | |
27 | 37 | | |
| |||
15637 | 15647 | | |
15638 | 15648 | | |
15639 | 15649 | | |
| 15650 | + | |
15640 | 15651 | | |
15641 | 15652 | | |
15642 | 15653 | | |
| |||
17916 | 17927 | | |
17917 | 17928 | | |
17918 | 17929 | | |
| 17930 | + | |
17919 | 17931 | | |
17920 | 17932 | | |
17921 | 17933 | | |
| |||
21079 | 21091 | | |
21080 | 21092 | | |
21081 | 21093 | | |
| 21094 | + | |
21082 | 21095 | | |
21083 | 21096 | | |
21084 | 21097 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2492 | 2492 | | |
2493 | 2493 | | |
2494 | 2494 | | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
2495 | 2518 | | |
2496 | 2519 | | |
2497 | 2520 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1113 | 1113 | | |
1114 | 1114 | | |
1115 | 1115 | | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
1116 | 1133 | | |
1117 | 1134 | | |
1118 | 1135 | | |
| |||
0 commit comments