Skip to content

gh-155905: Fix error handling in _testcapi helpers - #155906

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-155905-testcapi-fopen
Open

gh-155905: Fix error handling in _testcapi helpers#155906
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-155905-testcapi-fopen

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 16, 2026

Copy link
Copy Markdown
Member

Py_fopen() sets an exception and returns NULL on error.

The pyobject_print*() helpers in Modules/_testcapi/object.c did not check the result and passed a NULL file pointer to PyObject_Print(), which segfaulted. The pymarshal_*() helpers in Modules/_testcapimodule.c called PyErr_SetFromErrno() on top of the exception which Py_fopen() had already set, which raised SystemError on a release build and aborted a debug build.

The pyobject_print*() helpers which take a single argument are converted to METH_O, and the unchecked result of PyUnicode_FromString() is checked.

Py_fopen() sets an exception and returns NULL on error.  The
pyobject_print*() helpers did not check the result and crashed, and the
pymarshal_*() helpers set a second exception on top of it.

The pyobject_print*() helpers which take a single argument now use
METH_O, and the result of PyUnicode_FromString() is now checked.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka added tests Tests in the Lib/test dir type-crash A hard crash of the interpreter, possibly with a core dump skip news needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes skip news tests Tests in the Lib/test dir type-crash A hard crash of the interpreter, possibly with a core dump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant