@@ -155,6 +155,72 @@ test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
155155 return NULL ;
156156}
157157
158+ static PyObject *
159+ raiseTestError (const char * test_name , const char * msg )
160+ {
161+ PyErr_Format (PyExc_AssertionError , "%s: %s" , test_name , msg );
162+ return NULL ;
163+ }
164+
165+ static PyObject *
166+ test_frozenset_add_in_capi_tracking_immutable (PyObject * self , PyObject * Py_UNUSED (ignored ))
167+ {
168+ // Test: GC tracking - frozenset with only immutable items should not be tracked
169+ PyObject * frozenset = PyFrozenSet_New (NULL );
170+ if (frozenset == NULL ) {
171+ return NULL ;
172+ }
173+ if (PySet_Add (frozenset , Py_True ) < 0 ) {
174+ Py_DECREF (frozenset );
175+ return NULL ;
176+ }
177+ if (PyObject_GC_IsTracked (frozenset )) {
178+ Py_DECREF (frozenset );
179+ return raiseTestError ("test_frozenset_add_in_capi_tracking_immutable" ,
180+ "frozenset with only bool should not be GC tracked" );
181+ }
182+ Py_DECREF (frozenset );
183+ Py_RETURN_NONE ;
184+ }
185+
186+ static PyObject *
187+ test_frozenset_add_in_capi_tracking (PyObject * self , PyObject * Py_UNUSED (ignored ))
188+ {
189+ // Test: GC tracking - frozenset with tracked object should be tracked
190+ PyObject * frozenset = PyFrozenSet_New (NULL );
191+ if (frozenset == NULL ) {
192+ return NULL ;
193+ }
194+
195+ PyObject * tracked_obj = PyErr_NewException ("_testlimitedcapi.py_set_add" , NULL , NULL );
196+ if (tracked_obj == NULL ) {
197+ goto error ;
198+ }
199+ if (!PyObject_GC_IsTracked (tracked_obj )) {
200+ Py_DECREF (frozenset );
201+ Py_DECREF (tracked_obj );
202+ return raiseTestError ("test_frozenset_add_in_capi_tracking" ,
203+ "test object should be tracked" );
204+ }
205+ if (PySet_Add (frozenset , tracked_obj ) < 0 ) {
206+ goto error ;
207+ }
208+ Py_DECREF (tracked_obj );
209+ if (!PyObject_GC_IsTracked (frozenset )) {
210+ Py_DECREF (frozenset );
211+ return raiseTestError ("test_frozenset_add_in_capi_tracking" ,
212+ "frozenset with with GC tracked object should be tracked" );
213+ }
214+ Py_DECREF (frozenset );
215+ Py_RETURN_NONE ;
216+
217+ error :
218+ Py_DECREF (frozenset );
219+ Py_XDECREF (tracked_obj );
220+ return NULL ;
221+ }
222+
223+
158224static PyObject *
159225test_set_contains_does_not_convert_unhashable_key (PyObject * self , PyObject * Py_UNUSED (obj ))
160226{
@@ -219,6 +285,8 @@ static PyMethodDef test_methods[] = {
219285 {"set_clear" , set_clear , METH_O },
220286
221287 {"test_frozenset_add_in_capi" , test_frozenset_add_in_capi , METH_NOARGS },
288+ {"test_frozenset_add_in_capi_tracking" , test_frozenset_add_in_capi_tracking , METH_NOARGS },
289+ {"test_frozenset_add_in_capi_tracking_immutable" , test_frozenset_add_in_capi_tracking_immutable , METH_NOARGS },
222290 {"test_set_contains_does_not_convert_unhashable_key" ,
223291 test_set_contains_does_not_convert_unhashable_key , METH_NOARGS },
224292
0 commit comments