From 6f471720259c671deaef4e53a791945cd5d2ff70 Mon Sep 17 00:00:00 2001 From: AZero13 Date: Mon, 8 Dec 2025 18:52:01 -0500 Subject: [PATCH] gh-142442: Increase the borrowed ref from faulthandler_get_fileno when file is not None instead of decreasing when it is --- .../Library/2025-12-08-18-54-22.gh-issue-142442.SFn1RC.rst | 3 +++ Modules/faulthandler.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-12-08-18-54-22.gh-issue-142442.SFn1RC.rst diff --git a/Misc/NEWS.d/next/Library/2025-12-08-18-54-22.gh-issue-142442.SFn1RC.rst b/Misc/NEWS.d/next/Library/2025-12-08-18-54-22.gh-issue-142442.SFn1RC.rst new file mode 100644 index 00000000000000..d0d27632461fc6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-08-18-54-22.gh-issue-142442.SFn1RC.rst @@ -0,0 +1,3 @@ +Fix faulthandler’s fallback to ``sys.stderr`` so it owns the reference it +returns, preventing an accidental decref of ``sys.stderr`` (or ``None``) when no +file is provided. diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 9b8c77e2b0401f..9457b447c46d74 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -112,9 +112,9 @@ faulthandler_get_fileno(PyObject **file_ptr) } if (file == Py_None) { PyErr_SetString(PyExc_RuntimeError, "sys.stderr is None"); - Py_DECREF(file); return -1; } + Py_INCREF(file); } else if (PyLong_Check(file)) { if (PyBool_Check(file)) {