Skip to content

Commit 8d864ef

Browse files
committed
rework
1 parent 3dd4c95 commit 8d864ef

2 files changed

Lines changed: 7 additions & 95 deletions

File tree

Lib/test/test_capi/test_set.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ def test_add(self):
166166
# CRASHES: add(instance, NULL)
167167
# CRASHES: add(NULL, NULL)
168168

169+
def test_add_frozenset(self):
170+
add = _testlimitedcapi.set_add
171+
frozen_set = frozenset()
172+
# test adding an element to a non-uniquely referenced frozenset throws an exception
173+
self.assertRaises(SystemError, add, frozen_set, 1)
174+
169175
def test_discard(self):
170176
discard = _testlimitedcapi.set_discard
171177
for cls in (set, set_subclass):

Modules/_testlimitedcapi/set.c

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -130,62 +130,6 @@ raiseTestError(const char* test_name, const char* msg)
130130
return NULL;
131131
}
132132

133-
static PyObject *
134-
test_pyset_add_exact_set(PyObject *self, PyObject *Py_UNUSED(ignored))
135-
{
136-
// Test: Adding to a regular set
137-
PyObject *set = PySet_New(NULL);
138-
if (set == NULL) {
139-
return NULL;
140-
}
141-
PyObject *one = PyLong_FromLong(1);
142-
assert(one);
143-
144-
if (PySet_Add(set, one) < 0) {
145-
Py_DECREF(set);
146-
return raiseTestError("test_pyset_add_exact_set",
147-
"PySet_Add to empty set failed");
148-
}
149-
if (PySet_Size(set) != 1) {
150-
Py_DECREF(set);
151-
return raiseTestError("test_pyset_add_exact_set",
152-
"set size should be 1 after adding one element");
153-
}
154-
if (PySet_Contains(set, one) != 1) {
155-
Py_DECREF(set);
156-
return raiseTestError("test_pyset_add_exact_set",
157-
"set should contain the added element");
158-
}
159-
Py_DECREF(set);
160-
161-
// Test: Adding unhashable item should raise TypeError
162-
set = PySet_New(NULL);
163-
if (set == NULL) {
164-
return NULL;
165-
}
166-
PyObject *unhashable = PyList_New(0);
167-
if (unhashable == NULL) {
168-
Py_DECREF(set);
169-
return NULL;
170-
}
171-
if (PySet_Add(set, unhashable) != -1) {
172-
Py_DECREF(unhashable);
173-
Py_DECREF(set);
174-
return raiseTestError("test_pyset_add_exact_set",
175-
"PySet_Add with unhashable should fail");
176-
}
177-
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
178-
Py_DECREF(unhashable);
179-
Py_DECREF(set);
180-
return raiseTestError("test_pyset_add_exact_set",
181-
"PySet_Add with unhashable should raise TypeError");
182-
}
183-
PyErr_Clear();
184-
Py_DECREF(unhashable);
185-
Py_DECREF(set);
186-
187-
Py_RETURN_NONE;
188-
}
189133

190134
static PyObject *
191135
test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
@@ -223,48 +167,11 @@ test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
223167
}
224168

225169
static PyObject *
226-
test_pyset_add_frozenset(PyObject *self, PyObject *Py_UNUSED(ignored))
170+
test_frozenset_add_in_capi_tracking(PyObject *self, PyObject *Py_UNUSED(ignored))
227171
{
228172
PyObject *one = PyLong_FromLong(1);
229173
assert(one);
230174

231-
// Test: Adding to uniquely-referenced frozenset should succeed
232-
PyObject *frozenset = PyFrozenSet_New(NULL);
233-
if (frozenset == NULL) {
234-
return NULL;
235-
}
236-
237-
// frozenset is uniquely referenced here, so PySet_Add should work
238-
if (PySet_Add(frozenset, one) < 0) {
239-
Py_DECREF(frozenset);
240-
return raiseTestError("test_pyset_add_frozenset",
241-
"PySet_Add to uniquely-referenced frozenset failed");
242-
}
243-
Py_DECREF(frozenset);
244-
245-
// Test: Adding to non-uniquely-referenced frozenset should raise SystemError
246-
frozenset = PyFrozenSet_New(NULL);
247-
if (frozenset == NULL) {
248-
return NULL;
249-
}
250-
Py_INCREF(frozenset); // Make it non-uniquely referenced
251-
252-
if (PySet_Add(frozenset, one) != -1) {
253-
Py_DECREF(frozenset);
254-
Py_DECREF(frozenset);
255-
return raiseTestError("test_pyset_add_frozenset",
256-
"PySet_Add to non-uniquely-referenced frozenset should fail");
257-
}
258-
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
259-
Py_DECREF(frozenset);
260-
Py_DECREF(frozenset);
261-
return raiseTestError("test_pyset_add_frozenset",
262-
"PySet_Add to non-uniquely-referenced frozenset should raise SystemError");
263-
}
264-
PyErr_Clear();
265-
Py_DECREF(frozenset);
266-
Py_DECREF(frozenset);
267-
268175
// Test: GC tracking - frozenset with only immutable items should not be tracked
269176
frozenset = PyFrozenSet_New(NULL);
270177
if (frozenset == NULL) {
@@ -379,7 +286,6 @@ static PyMethodDef test_methods[] = {
379286
{"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
380287
{"test_set_contains_does_not_convert_unhashable_key",
381288
test_set_contains_does_not_convert_unhashable_key, METH_NOARGS},
382-
{"test_pyset_add_exact_set", test_pyset_add_exact_set, METH_NOARGS},
383289
{"test_pyset_add_frozenset", test_pyset_add_frozenset, METH_NOARGS},
384290

385291
{NULL},

0 commit comments

Comments
 (0)