Repository navigation
Commit 7050798
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#9671 parent a9ed8f1 commit 7050798
3 files changed
Lines changed: 54 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 | | |
| |||
15630 | 15640 | | |
15631 | 15641 | | |
15632 | 15642 | | |
| 15643 | + | |
15633 | 15644 | | |
15634 | 15645 | | |
15635 | 15646 | | |
| |||
17909 | 17920 | | |
17910 | 17921 | | |
17911 | 17922 | | |
| 17923 | + | |
17912 | 17924 | | |
17913 | 17925 | | |
17914 | 17926 | | |
| |||
21072 | 21084 | | |
21073 | 21085 | | |
21074 | 21086 | | |
| 21087 | + | |
21075 | 21088 | | |
21076 | 21089 | | |
21077 | 21090 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2493 | 2493 | | |
2494 | 2494 | | |
2495 | 2495 | | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
| 2518 | + | |
| 2519 | + | |
2496 | 2520 | | |
2497 | 2521 | | |
2498 | 2522 | | |
| |||
| 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