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 581c87270ff93f..9d92ca0b2f9f3f 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -511,6 +511,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/pythonrun.c b/Python/pythonrun.c index 632d2c56a67638..0acb92e96ef9c0 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1150,6 +1150,7 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) { Py_DECREF(traceback_module); + Py_XDECREF(print_exception_fn); goto fallback; } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 8d47177340d780..261d0fdc002f47 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1782,7 +1782,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();