GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-159184: Do not compare set elements when marshalling a set by gpshead · Pull Request #159196 · python/cpython

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gpshead gpshead self-assigned this Oct 11, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Oct 11, 2026
marshal.dumps() raises TypeError for a set containing several complex NaNs #159184
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Oct 11, 2026
@gpshead gpshead added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 11, 2026
@gpshead gpshead mentioned this pull request Oct 11, 2026
gh-159184: Make the marshal order of set elements with equal dumps stable #159197
Merged
@gpshead
pythongh-159184: Do not compare set elements when marshalling a set
910690f
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

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Oct 11, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Oct 11, 2026
@miss-islington-app

miss-islington-app Bot commented Oct 11, 2026

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Oct 11, 2026

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead gpshead added the type-bug An unexpected behavior, bug, or error label Oct 11, 2026
@gpshead gpshead mentioned this pull request Oct 11, 2026
[3.15] gh-159184: Do not compare set elements when marshalling a set (GH-159196) #159205
Merged
@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 11, 2026
@gpshead gpshead mentioned this pull request Oct 11, 2026
[3.14] gh-159184: Do not compare set elements when marshalling a set (GH-159196) #159206
Merged
@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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
@gpshead
[3.14] gh-159184: Do not compare set elements when marshalling a set (G…
758278c
…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
@gpshead
[3.15] gh-159184: Do not compare set elements when marshalling a set (G…
8c7ac57
…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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@gpshead gpshead

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@gpshead
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.