bpo-41604: Don't decrement the reference count of the previous user_ptr when set_panel_usertpr fails (GH-21933)
diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c
index d782ccd..94caf8c 100644
--- a/Modules/_curses_panel.c
+++ b/Modules/_curses_panel.c
@@ -456,7 +456,9 @@ _curses_panel_panel_set_userptr_impl(PyCursesPanelObject *self,
/* In case of an ncurses error, decref the new object again */
Py_DECREF(obj);
}
- Py_XDECREF(oldobj);
+ else {
+ Py_XDECREF(oldobj);
+ }
_curses_panel_state *state = PyType_GetModuleState(cls);
return PyCursesCheckERR(state, rc, "set_panel_userptr");