You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
marshal.dumps() raises TypeError for a set containing several complex NaNs #159184
marshal writes the elements of a set in the order of a sort of (marshalled bytes, value) pairs. When two elements have the same
marshalled bytes, the sort compares the values. Complex numbers are not
orderable, so this fails:
A related problem in the same code: float NaNs do compare, but neither way,
so elements with equal bytes stay in set iteration order, which depends on
their addresses. If one of them is also referenced elsewhere in the dump,
the output differs for the same value:
importmarshalnans= [float('nan') for_inrange(3)]
# A list of a NaN and a set of three NaNs, one of which is that NaN.print(len({marshal.dumps([n, set(nans)]) forninnans})) # 3, expected 1
Proposed fix
Sort the elements on their marshalled bytes only and never compare the
values. That fixes the TypeError on its own. Making the order of equal
elements independent of addresses additionally needs the writer to know
which objects it has already written; that part depends on the fix for gh-129724
(coming).
Discovered using Claude while doing a deep dive on pyc compilation determinism.
Activity
pythongh-159184: Do not compare set elements when marshalling a set
pythongh-159184: Do not compare set elements when marshalling a set
pythongh-159184: Make the marshal order of set elements with equal du…
gh-159184: Do not compare set elements when marshalling a set (GH-159196
pythongh-159184: Make the marshal order of set elements with equal du…
[3.14] gh-159184: Do not compare set elements when marshalling a set (G…
[3.15] gh-159184: Do not compare set elements when marshalling a set (G…
gpshead commentedon Oct 11, 2026
#159197 is needed to fix this but requires #159192 first.
pythongh-159184: Make the marshal order of set elements with equal du…