Title: `marshal.dumps()` raises `TypeError` for a set containing several complex NaNs · Issue #159184 · python/cpython · GitHub
Open Graph Title: `marshal.dumps()` raises `TypeError` for a set containing several complex NaNs · Issue #159184 · python/cpython
X Title: `marshal.dumps()` raises `TypeError` for a set containing several complex NaNs · Issue #159184 · python/cpython
Description: Bug report 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: ...
Open Graph Description: Bug report 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 nu...
X Description: Bug report 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 nu...
Opengraph URL: https://github.com/python/cpython/issues/159184
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`marshal.dumps()` raises `TypeError` for a set containing several complex NaNs","articleBody":"### Bug report\n\n`marshal` writes the elements of a set in the order of a sort of\n`(marshalled bytes, value)` pairs. When two elements have the same\nmarshalled bytes, the sort compares the values. Complex numbers are not\norderable, so this fails:\n\n```python\n>>> import marshal\n>>> marshal.dumps(frozenset({complex('nan'), complex('nan')}))\nTypeError: '\u003c' not supported between instances of 'complex' and 'complex'\n```\n\nTwo distinct elements only have the same bytes when they are NaNs that are\ndifferent objects (or containers of such NaNs).\n\nThe compiler can produce such a constant by folding, so the code object\ncannot be marshalled and no `.pyc` can be written for the module:\n\n```python\n>>> code = compile(\"x in {(1e999-1e999)*1j, (1e999-1e999)*2j}\", \"m.py\", \"exec\")\n>>> code.co_consts\n(inf, frozenset({(nan+nanj), (nan+nanj)}))\n>>> marshal.dumps(code)\nTypeError: '\u003c' not supported between instances of 'complex' and 'complex'\n```\n\nA related problem in the same code: float NaNs do compare, but neither way,\nso elements with equal bytes stay in set iteration order, which depends on\ntheir addresses. If one of them is also referenced elsewhere in the dump,\nthe output differs for the same value:\n\n```python\nimport marshal\nnans = [float('nan') for _ in range(3)]\n# A list of a NaN and a set of three NaNs, one of which is that NaN.\nprint(len({marshal.dumps([n, set(nans)]) for n in nans})) # 3, expected 1\n```\n\n### Proposed fix\n\nSort the elements on their marshalled bytes only and never compare the\nvalues. That fixes the `TypeError` on its own. Making the order of equal\nelements independent of addresses additionally needs the writer to know\nwhich objects it has already written; that part depends on the fix for gh-129724\n(coming).\n\n---\n\nDiscovered using Claude while doing a deep dive on pyc compilation determinism.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159196\n* gh-159197\n* gh-159205\n* gh-159206\n* gh-161276\n* gh-161286\n* gh-161287\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2026-10-11T17:32:31Z","url":"https://github.com/python/cpython/issues/159184"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:953cb0be-37d5-95ad-6b33-4bccff7b0a50 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B8F2:2E9D3B:2BC79B:36FDF3:6B020741 |
| html-safe-nonce | b4e763abe3731d0ec00cc66bf1046891adf6f7c0d7bb86c1a4631bec58d9be0d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOEYyOjJFOUQzQjoyQkM3OUI6MzZGREYzOjZCMDIwNzQxIiwidmlzaXRvcl9pZCI6Ijg1MDI0MTI3ODEwMjk1NTgwODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 478b802f5dac2273c9f14a39392429a50908c0293ddeb5ce8abae1a46760907d |
| hovercard-subject-tag | issue:5803459096 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/159184/issue_layout |
| twitter:image | https://opengraph.githubassets.com/35f1b44f12dd6596de54898323186409fc3224ab7bb225bc3efc39bf1b6ccdbc/python/cpython/issues/159184 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/35f1b44f12dd6596de54898323186409fc3224ab7bb225bc3efc39bf1b6ccdbc/python/cpython/issues/159184 |
| og:image:alt | Bug report 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 nu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gpshead |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width