Skip to content

Commit ea39c87

Browse files
authored
gh-110525: Cover PySet_Add corner case with frozenset objects (GH-110544)
1 parent 89df5b7 commit ea39c87

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Modules/_testcapi/set.c

+37
Original file line numberDiff line numberDiff line change
@@ -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+
130165
static 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

Comments
 (0)