GeezerVison is showing github.com — [3.15] gh-129724: gh-156504: Make marshal output independent of reference counts (GH-159192) by gpshead · Pull Request #159290 · python/cpython
Conversation
gpshead
commented
Oct 13, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
(cherry picked from commit 48f18f0)
Manual backport notes
A pure cherry-pick conflicted because 3.15 does not have the marshal error handling changes of gh-156942 and gh-155907, nor gh-148653.
Python/marshal.c: keeps the WFERR_* error codes of this branch; the new allocation failures use WFERR_NOMEMORY.
PyMarshal_WriteLongToFile() and PyMarshal_WriteObjectToFile() still do not report a failure to write to the file, as before on this branch.
PyMarshal_WriteObjectToFile() now goes through the same code as marshal.dumps(), so when the object cannot be marshalled it writes nothing and sets ValueError. Before, it wrote partial data and usually returned without an error.
- Recursive tuples are not detected on this branch (Crash: marshal.loads SIGSEGV on self-referencing TYPE_TUPLE with FLAG_REF #148653), so
test_shared_recursion uses a recursive slice.
Lib/test/test_capi/test_marshal.py does not exist here: its new cases are in CAPI_TestCase of Lib/test/test_marshal.py, and the _testcapi helper returns the error instead of asserting there is none.
Doc/c-api/marshal.rst: the notes about unreported write errors no longer say "In Python 3.15 and earlier"; the versionchanged entry describes the change.
Programs/test_frozenmain.h is regenerated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…ent of reference counts (pythonGH-159192)
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.
(cherry picked from commit 48f18f0)
Reviewers
ericsnowcurrently
Awaiting requested review from ericsnowcurrently
ericsnowcurrently will be requested when the pull request is marked ready for review
ericsnowcurrently is a code owner
ZeroIntensity
Awaiting requested review from ZeroIntensity
ZeroIntensity will be requested when the pull request is marked ready for review
ZeroIntensity is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Labels
type-bug
An unexpected behavior, bug, or error
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.
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.