@@ -127,6 +127,41 @@ set_clear(PyObject *self, PyObject *obj)
127127 RETURN_INT (PySet_Clear (obj ));
128128}
129129
130+ static PyObject *
131+ test_frozenset_add_in_capi (PyObject * self , PyObject * Py_UNUSED (obj ))
132+ {
133+ // Test that `frozenset` can be used with `PySet_Add`,
134+ // when frozenset is just created in CAPI.
135+ PyObject * fs = PyFrozenSet_New (NULL );
136+ if (fs == NULL ) {
137+ return NULL ;
138+ }
139+ PyObject * num = PyLong_FromLong (1 );
140+ if (num == NULL ) {
141+ goto error ;
142+ }
143+ if (PySet_Add (fs , num ) < 0 ) {
144+ goto error ;
145+ }
146+ int contains = PySet_Contains (fs , num );
147+ if (contains < 0 ) {
148+ goto error ;
149+ }
150+ else if (contains == 0 ) {
151+ goto unexpected ;
152+ }
153+ Py_DECREF (fs );
154+ Py_DECREF (num );
155+ Py_RETURN_NONE ;
156+
157+ unexpected :
158+ PyErr_SetString (PyExc_ValueError , "set does not contain expected value" );
159+ error :
160+ Py_DECREF (fs );
161+ Py_XDECREF (num );
162+ return NULL ;
163+ }
164+
130165static PyMethodDef test_methods [] = {
131166 {"set_check" , set_check , METH_O },
132167 {"set_checkexact" , set_checkexact , METH_O },
@@ -146,6 +181,8 @@ static PyMethodDef test_methods[] = {
146181 {"set_pop" , set_pop , METH_O },
147182 {"set_clear" , set_clear , METH_O },
148183
184+ {"test_frozenset_add_in_capi" , test_frozenset_add_in_capi , METH_NOARGS },
185+
149186 {NULL },
150187};
151188
0 commit comments