Skip to content

gh-159184: Do not compare set elements when marshalling a set - #159196

Merged
gpshead merged 1 commit into
python:mainfrom
gpshead:marshal-set-sort-bytes
Oct 11, 2026
Merged

gpshead merged 1 commit into
python:mainfrom
gpshead:marshal-set-sort-bytes

Conversation

@gpshead

@gpshead gpshead commented Oct 11, 2026

Copy link
Copy Markdown
Member

marshal writes the elements of a set in the order of their dumps, so that the output does not depend on hash order. It did that by sorting (dump, element) pairs, which compares the elements whenever two of them have the same dump. That happens for NaNs that are distinct objects, and complex numbers cannot be ordered:

>>> marshal.dumps(frozenset({complex('nan'), complex('nan')}))
TypeError: '<' not supported between instances of 'complex' and 'complex'

>>> code = compile("x in {(1e999-1e999)*1j, (1e999-1e999)*2j}", "m.py", "exec")
>>> marshal.dumps(code)       # so no .pyc can be written for this module
TypeError: '<' not supported between instances of 'complex' and 'complex'

Change

flowchart TB
  subgraph before["main"]
    direction TB
    b1["list of (dump, element)"] --> b2["PyList_Sort()"]
    b2 --> b3{"equal dumps?"}
    b3 -- no --> b4["ordered by dump"]
    b3 -- yes --> b5["compares the elements"]
    b5 -- complex --> b6["TypeError"]
    b5 -- "float NaN" --> b7["neither is smaller:<br>left in set order"]
  end
  subgraph after["this PR"]
    direction TB
    a1["list of (dump, index, element)<br>index = position in the set"] --> a2["PyList_Sort()"]
    a2 --> a3{"equal dumps?"}
    a3 -- no --> a4["ordered by dump"]
    a3 -- yes --> a5["compares the indexes,<br>which always differ:<br>left in set order"]
  end
Loading

The output is unchanged wherever marshalling worked before: elements with different dumps are ordered by dump, and float NaNs with equal dumps stay in set order as they did. The only difference is that elements are never compared.

This is the small part of gh-159184 that can be backported. The other part, that the order of elements with equal dumps follows their addresses, needs #159192 and is a separate PR.

This touches the same block as the PR for gh-159185; whichever is merged second needs a one-line conflict resolved.

Tests

test_unorderable_set_items and test_unorderable_set_items_in_code replace test_error_in_set_item, which asserted the TypeError.

Set elements are written in the order of their dumps.  They were sorted
as (dump, element) pairs, so elements with the same dump were compared.
That raised TypeError for a set of several complex NaNs, which the
compiler can produce by constant folding, making the module impossible
to marshal.

Sort (dump, index, element) tuples, in which the indexes are all
different.  Elements with the same dump stay in the order of the set,
as float NaNs did before.
@gpshead
gpshead force-pushed the marshal-set-sort-bytes branch from 9ae5920 to 910690f Compare October 11, 2026 22:00
@gpshead
gpshead merged commit 1967a87 into python:main Oct 11, 2026
55 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @gpshead, I could not cleanly backport this to 3.15 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 1967a87ad395552dbd1e16fd3f13d3b4b2f694de 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @gpshead, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 1967a87ad395552dbd1e16fd3f13d3b4b2f694de 3.14

@bedevere-app

bedevere-app Bot commented Oct 11, 2026

Copy link
Copy Markdown

GH-159205 is a backport of this pull request to the 3.15 branch.

@bedevere-app

bedevere-app Bot commented Oct 11, 2026

Copy link
Copy Markdown

GH-159206 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 11, 2026
gpshead added a commit that referenced this pull request Oct 11, 2026
…H-159196) (#159206)

Set elements are written in the order of their dumps.  They were sorted
as (dump, element) pairs, so elements with the same dump were compared.
That failed for a set of several complex NaNs, which the compiler can
produce by constant folding, making the module impossible to marshal.
On this branch the failure was reported as MemoryError.

Sort (dump, index, element) tuples, in which the indexes are all
different.  Elements with the same dump stay in the order of the set,
as float NaNs did before.

(cherry picked from commit 1967a87)
gpshead added a commit that referenced this pull request Oct 11, 2026
…H-159196) (#159205)

Set elements are written in the order of their dumps.  They were sorted
as (dump, element) pairs, so elements with the same dump were compared.
That failed for a set of several complex NaNs, which the compiler can
produce by constant folding, making the module impossible to marshal.
On this branch the failure was reported as MemoryError.

Sort (dump, index, element) tuples, in which the indexes are all
different.  Elements with the same dump stay in the order of the set,
as float NaNs did before.

(cherry picked from commit 1967a87)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant