@@ -3310,6 +3310,73 @@ function_set_kw_defaults(PyObject *self, PyObject *args)
33103310 Py_RETURN_NONE ;
33113311}
33123312
3313+ struct gc_visit_state_basic {
3314+ PyObject * target ;
3315+ int found ;
3316+ };
3317+
3318+ static int
3319+ gc_visit_callback_basic (PyObject * obj , void * arg )
3320+ {
3321+ struct gc_visit_state_basic * state = (struct gc_visit_state_basic * )arg ;
3322+ if (obj == state -> target ) {
3323+ state -> found = 1 ;
3324+ return 0 ;
3325+ }
3326+ return 1 ;
3327+ }
3328+
3329+ static PyObject *
3330+ test_gc_visit_objects_basic (PyObject * Py_UNUSED (self ),
3331+ PyObject * Py_UNUSED (ignored ))
3332+ {
3333+ PyObject * obj ;
3334+ struct gc_visit_state_basic state ;
3335+
3336+ obj = PyList_New (0 );
3337+ if (obj == NULL ) {
3338+ return NULL ;
3339+ }
3340+ state .target = obj ;
3341+ state .found = 0 ;
3342+
3343+ PyUnstable_GC_VisitObjects (gc_visit_callback_basic , & state );
3344+ Py_DECREF (obj );
3345+ if (!state .found ) {
3346+ PyErr_SetString (
3347+ PyExc_AssertionError ,
3348+ "test_gc_visit_objects_basic: Didn't find live list" );
3349+ return NULL ;
3350+ }
3351+ Py_RETURN_NONE ;
3352+ }
3353+
3354+ static int
3355+ gc_visit_callback_exit_early (PyObject * obj , void * arg )
3356+ {
3357+ int * visited_i = (int * )arg ;
3358+ (* visited_i )++ ;
3359+ if (* visited_i == 2 ) {
3360+ return 0 ;
3361+ }
3362+ return 1 ;
3363+ }
3364+
3365+ static PyObject *
3366+ test_gc_visit_objects_exit_early (PyObject * Py_UNUSED (self ),
3367+ PyObject * Py_UNUSED (ignored ))
3368+ {
3369+ int visited_i = 0 ;
3370+ PyUnstable_GC_VisitObjects (gc_visit_callback_exit_early , & visited_i );
3371+ if (visited_i != 2 ) {
3372+ PyErr_SetString (
3373+ PyExc_AssertionError ,
3374+ "test_gc_visit_objects_exit_early: did not exit when expected" );
3375+ }
3376+ Py_RETURN_NONE ;
3377+ }
3378+
3379+
33133380static PyObject * test_buildvalue_issue38913 (PyObject * , PyObject * );
33143381
33153382static PyMethodDef TestMethods [] = {
@@ -3452,6 +3519,8 @@ static PyMethodDef TestMethods[] = {
34523519 {"function_set_defaults" , function_set_defaults , METH_VARARGS , NULL },
34533520 {"function_get_kw_defaults" , function_get_kw_defaults , METH_O , NULL },
34543521 {"function_set_kw_defaults" , function_set_kw_defaults , METH_VARARGS , NULL },
3522+ {"test_gc_visit_objects_basic" , test_gc_visit_objects_basic , METH_NOARGS , NULL },
3523+ {"test_gc_visit_objects_exit_early" , test_gc_visit_objects_exit_early , METH_NOARGS , NULL },
34553524 {NULL , NULL } /* sentinel */
34563525};
34573526
0 commit comments