Skip to content

Commit b2419ef

Browse files
[3.12] gh-110525: Cover PySet_Add corner case with frozenset objects (GH-110544) (GH-110554)
(cherry picked from commit ea39c87) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 2362446 commit b2419ef

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
@@ -129,6 +129,41 @@ set_clear(PyObject *self, PyObject *obj)
129129
RETURN_INT(PySet_Clear(obj));
130130
}
131131

132+
static PyObject *
133+
test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
134+
{
135+
// Test that `frozenset` can be used with `PySet_Add`,
136+
// when frozenset is just created in CAPI.
137+
PyObject *fs = PyFrozenSet_New(NULL);
138+
if (fs == NULL) {
139+
return NULL;
140+
}
141+
PyObject *num = PyLong_FromLong(1);
142+
if (num == NULL) {
143+
goto error;
144+
}
145+
if (PySet_Add(fs, num) < 0) {
146+
goto error;
147+
}
148+
int contains = PySet_Contains(fs, num);
149+
if (contains < 0) {
150+
goto error;
151+
}
152+
else if (contains == 0) {
153+
goto unexpected;
154+
}
155+
Py_DECREF(fs);
156+
Py_DECREF(num);
157+
Py_RETURN_NONE;
158+
159+
unexpected:
160+
PyErr_SetString(PyExc_ValueError, "set does not contain expected value");
161+
error:
162+
Py_DECREF(fs);
163+
Py_XDECREF(num);
164+
return NULL;
165+
}
166+
132167
static PyMethodDef test_methods[] = {
133168
{"set_check", set_check, METH_O},
134169
{"set_checkexact", set_checkexact, METH_O},
@@ -148,6 +183,8 @@ static PyMethodDef test_methods[] = {
148183
{"set_pop", set_pop, METH_O},
149184
{"set_clear", set_clear, METH_O},
150185

186+
{"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
187+
151188
{NULL},
152189
};
153190

0 commit comments

Comments
 (0)