Skip to content

gh-151830: Raise ValueError instead of SystemError for invalid code objects in marshal.loads()#151831

Open
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-marshal-invalid-code-valueerror
Open

gh-151830: Raise ValueError instead of SystemError for invalid code objects in marshal.loads()#151831
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-marshal-invalid-code-valueerror

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

marshal.loads() already raises ValueError with a "bad marshal data (...)" message for malformed input, but a code object whose internal fields
are inconsistent (for example a localsplusnames tuple whose length differs
from the localspluskinds bytes) makes _PyCode_Validate() call
PyErr_BadInternalCall(), which surfaces as
SystemError: bad argument to internal function. This converts that one
case to ValueError: bad marshal data (invalid code object), so the
code-object path is consistent with marshal's existing bad-marshal-data
errors. The change is narrowly scoped to the SystemError from
PyErr_BadInternalCall() and leaves the other _PyCode_Validate() failures
(which already raise ValueError/OverflowError) unchanged.

A regression test was added to Lib/test/test_marshal.py and a NEWS entry
was added under Misc/NEWS.d/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant