Repository navigation
Conversation
Documentation build overview
|
gpshead
force-pushed
the
marshal-set-tie-order
branch
from
October 11, 2026 22:00
6df4e3f to
0e89222
Compare
The marshal writer decided whether an object can be referenced later (FLAG_REF) from its reference count, so the output for the same value depended on what else in the process held a reference to its parts, and differed between the default and the free-threaded builds' view of the same objects. This is the remaining cause of pythongh-129724 and the cause of pythongh-156504. FLAG_REF is now set exactly on the objects which are written more than once in the dump. The writer still makes one pass: it records the position of the type byte of each object it remembers and of each TYPE_REF, and when the dump is complete it sets the flags and renumbers the references. A reference count of one is only used to skip the hashtable. Interned strings are no longer flagged unconditionally. The reader is unchanged and the format version is not bumped: both the old and the new output are loaded by old and new readers. PyMarshal_WriteObjectToFile() now builds the data in memory and writes it with one fwrite(), since the flags are only known at the end. Nothing is written if the object cannot be marshalled.
…mps stable Set elements with the same dump (NaNs which are distinct objects) were written in set iteration order, which depends on their addresses. When one of them is also referenced elsewhere in the dump, the position of the reference among them changed from run to run. Elements with equal dumps are now ordered by when they were first written in this dump; floats and complex numbers not written yet are interchangeable, and which one is which is decided when one of them is referenced again. This relies on the writer knowing exactly which objects it has written (pythongh-129724). The elements are sorted with qsort() on the bytes of their dumps, in an array instead of a list of tuples.
gpshead
force-pushed
the
marshal-set-tie-order
branch
from
October 11, 2026 23:05
0e89222 to
a4b8ab9
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Draft: stacked on #159192. This branch contains that PR's commit; only the last commit is new. It stays a draft until #159192 is merged, and will then be rebased onto main.
This is the second half of gh-159184. The first half, #159196 (merged), stopped
marshalfrom comparing set elements, which fixed theTypeErrorfor complex NaNs. This PR fixes the order such elements are written in.Set elements with the same dump are NaNs that are distinct objects.
marshalleaves them in set iteration order, which is the order of their addresses. That is invisible unless one of them is also referenced elsewhere in the dump; then the reference lands at a different place among them from run to run:Change
flowchart TB subgraph before["main"] direction TB b1["list of (dump, index, element)"] --> b2["PyList_Sort()"] b2 --> b3{"run of equal dumps?"} b3 -- no --> b4["ordered by dump"] b3 -- yes --> b5["left in set order<br>= address order"] end subgraph after["this PR"] direction TB a1["array of {element, dump}"] --> a2["qsort() on the dump bytes"] a2 --> a3{"run of equal dumps?"} a3 -- no --> a4["ordered by dump"] a3 -- yes --> a5["w_same_set_items()"] a5 --> a6["already written in this dump:<br>first, in the order written"] a5 --> a7["floats and complex not written yet:<br>interchangeable; which one is which<br>is decided when one is referenced again"] endw_complex_object()intow_set_items(). It keeps the checks for a set whose size changes while it is marshalled (gh-159185: Fix marshalling of a set modified while its elements are marshalled #159195, merged).Not covered
Elements that are containers with equal dumps, not written before the set (for example
{(nan1,), (nan2,)}with one of the NaNs also used elsewhere), are still written in set order. A canonical order for that case amounts to graph canonisation. The compiler cannot produce it: every folded NaN is a new object.Tests
ReferenceTestCase.test_nan_in_set_orderchecks exact bytes for sets of float and complex NaNs with references before, after and through other sets.test_nan_in_setalso covers complex NaNs.marshal.dumps()raisesTypeErrorfor a set containing several complex NaNs #159184