Title: _testcapi mishandles Py_fopen() failures · Issue #155905 · python/cpython · GitHub
Open Graph Title: _testcapi mishandles Py_fopen() failures · Issue #155905 · python/cpython
X Title: _testcapi mishandles Py_fopen() failures · Issue #155905 · python/cpython
Description: Bug report Py_fopen() sets an exception and returns NULL on error, but several _testcapi helpers ignore both halves of that contract. Modules/_testcapi/object.c does not check the result at all. All four helpers pass it straight to PyObj...
Open Graph Description: Bug report Py_fopen() sets an exception and returns NULL on error, but several _testcapi helpers ignore both halves of that contract. Modules/_testcapi/object.c does not check the result at all. Al...
X Description: Bug report Py_fopen() sets an exception and returns NULL on error, but several _testcapi helpers ignore both halves of that contract. Modules/_testcapi/object.c does not check the result at all. Al...
Opengraph URL: https://github.com/python/cpython/issues/155905
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_testcapi mishandles Py_fopen() failures","articleBody":"# Bug report\n\n`Py_fopen()` sets an exception and returns `NULL` on error, but several `_testcapi` helpers ignore both halves of that contract.\n\n**`Modules/_testcapi/object.c` does not check the result at all.** All four helpers pass it straight to `PyObject_Print()`:\n\n```c\n fp = Py_fopen(filename, \"w+\");\n\n if (PyObject_Print(object, fp, flags) \u003c 0) {\n```\n\nso a `NULL` file pointer reaches `fprintf()` and the interpreter crashes:\n\n```pycon\n>>> import _testcapi\n>>> _testcapi.call_pyobject_print('x', '/nonexistent-dir/out.txt', False)\nSegmentation fault\n```\n\nThe same happens with `pyobject_print_null()`, `pyobject_print_noref_object()` and `pyobject_print_os_error()`.\n\n**`Modules/_testcapimodule.c` sets a second exception.** The six `pymarshal_*` helpers do:\n\n```c\n fp = Py_fopen(filename, \"rb\");\n if (fp == NULL) {\n PyErr_SetFromErrno(PyExc_OSError);\n return NULL;\n }\n```\n\nso `OSError` is instantiated while `Py_fopen()`'s exception is still pending:\n\n```pycon\n>>> _testcapi.pymarshal_read_object_from_file('nonexistent')\nSystemError: \u003cclass 'OSError'> returned a result with an exception set\n```\n\nOn a debug build it aborts instead: ``Objects/call.c:342: _PyObject_Call: Assertion `!_PyErr_Occurred(tstate)' failed.``\n\nBoth date from f89e5e20cb8 (gh-127350), which converted these helpers from `fopen()` to `Py_fopen()`. They affect 3.14 and later.\n\nRelated cleanup: these helpers also close the file with `fclose()` rather than `Py_fclose()`, which the documentation requires -- 8 sites in `object.c`, 6 in `_testcapimodule.c` and 14 in `run.c`. It is harmless today, since `Py_fclose()` is just a wrapper.\n\nAll other `Py_fopen()` call sites in the tree check the result correctly.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-155906\n* gh-156580\n* gh-156581\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2026-08-16T17:31:21Z","url":"https://github.com/python/cpython/issues/155905"}
| 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:2522eeca-2ecf-b825-5b1f-e2db14bf8c6f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C622:62F48:D6B4AA:11B858B:6B01FE9A |
| html-safe-nonce | 22f95e669e68343b6d7e00fd81473749384264a69a580e598b39043566fbeaff |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjIyOjYyRjQ4OkQ2QjRBQToxMUI4NThCOjZCMDFGRTlBIiwidmlzaXRvcl9pZCI6IjE3NzI2MDA1NDMyMTUyMjI0MjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c1df697a00021ce178bbb8aaf9b066e8a8becd04e95582ed87538b5052dc1111 |
| hovercard-subject-tag | issue:5165483512 |
| 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/155905/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6426ff7b83cb1e723704aaffcb422053bbaf6ffbdf9bf245946a34b5a2c268f3/python/cpython/issues/155905 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6426ff7b83cb1e723704aaffcb422053bbaf6ffbdf9bf245946a34b5a2c268f3/python/cpython/issues/155905 |
| og:image:alt | Bug report Py_fopen() sets an exception and returns NULL on error, but several _testcapi helpers ignore both halves of that contract. Modules/_testcapi/object.c does not check the result at all. Al... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | serhiy-storchaka |
| 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