diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-28-16-46-17.gh-issue-145376.lG5u1a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-28-16-46-17.gh-issue-145376.lG5u1a.rst new file mode 100644 index 00000000000000..a5a6908757e458 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-28-16-46-17.gh-issue-145376.lG5u1a.rst @@ -0,0 +1 @@ +Fix reference leaks in various unusual error scenarios. diff --git a/Modules/main.c b/Modules/main.c index 9b2ee103d52662..15f51acbcf551f 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -509,6 +509,7 @@ pymain_run_interactive_hook(int *exitcode) } if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) { + Py_DECREF(hook); goto error; } diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 80772fcedb46e6..3b6c0c6e1eda4b 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -609,6 +609,7 @@ check_missing___main___attr(PyObject *exc) // Get the error message. PyObject *args = PyException_GetArgs(exc); if (args == NULL || args == Py_None || PyObject_Size(args) < 1) { + Py_XDECREF(args); assert(!PyErr_Occurred()); return 0; } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index e005a509f6f061..91c81ac0d8d237 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1151,6 +1151,7 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) "traceback", "_print_exception_bltin"); if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) { + Py_XDECREF(print_exception_fn); goto fallback; } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 545b130836e26c..c97042c99fa71e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1759,7 +1759,7 @@ sys_getwindowsversion_impl(PyObject *module) PyObject *realVersion = _sys_getwindowsversion_from_kernel32(); if (!realVersion) { if (!PyErr_ExceptionMatches(PyExc_WindowsError)) { - return NULL; + goto error; } PyErr_Clear();