@@ -429,6 +429,23 @@ PyCursesPanel_New(_curses_panel_state *state, PANEL *pan,
429429    return  (PyObject  * )po ;
430430}
431431
432+ static  int 
433+ PyCursesPanel_Clear (PyObject  * op )
434+ {
435+     PyCursesPanelObject  * self  =  _PyCursesPanelObject_CAST (op );
436+     PyObject  * extra  =  (PyObject  * )panel_userptr (self -> pan );
437+     if  (extra  !=  NULL ) {
438+         Py_CLEAR (extra );
439+         if  (set_panel_userptr (self -> pan , NULL ) ==  ERR ) {
440+             curses_panel_panel_set_error (self , "set_panel_userptr" , NULL );
441+             // Do not add a PyErr_FormatUnraisable() because the GC 
442+             // is responsible for handling exceptions in tp_clear. 
443+         }
444+     }
445+     // do NOT clear self->wo yet as there is no cycle to break with it 
446+     return  0 ;
447+ }
448+ 
432449static  void 
433450PyCursesPanel_Dealloc (PyObject  * self )
434451{
@@ -464,6 +481,7 @@ PyCursesPanel_Traverse(PyObject *op, visitproc visit, void *arg)
464481{
465482    PyCursesPanelObject  * self  =  _PyCursesPanelObject_CAST (op );
466483    Py_VISIT (Py_TYPE (op ));
484+     Py_VISIT (panel_userptr (self -> pan ));
467485    Py_VISIT (self -> wo );
468486    return  0 ;
469487}
@@ -659,6 +677,7 @@ static PyMethodDef PyCursesPanel_Methods[] = {
659677/* -------------------------------------------------------*/ 
660678
661679static  PyType_Slot  PyCursesPanel_Type_slots [] =  {
680+     {Py_tp_clear , PyCursesPanel_Clear },
662681    {Py_tp_dealloc , PyCursesPanel_Dealloc },
663682    {Py_tp_traverse , PyCursesPanel_Traverse },
664683    {Py_tp_methods , PyCursesPanel_Methods },
0 commit comments