Skip to content

Commit 075b582

Browse files
committed
cleanup tests
1 parent e7dd248 commit 075b582

3 files changed

Lines changed: 161 additions & 160 deletions

File tree

Lib/test/test_capi/test_set.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,6 @@ def test_clear(self):
220220
# CRASHES: clear(NULL)
221221

222222

223-
class TestPySet_Add(unittest.TestCase):
224-
def test_pyset_add_exact_set(self):
225-
_testcapi.test_pyset_add_exact_set()
226-
227-
def test_pyset_add_frozenset(self):
228-
_testcapi.test_pyset_add_frozenset()
229-
230-
231-
232223
class TestInternalCAPI(BaseSetTests, unittest.TestCase):
233224
def test_set_update(self):
234225
update = _testinternalcapi.set_update

Modules/_testcapimodule.c

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,153 +2434,6 @@ test_critical_sections(PyObject *module, PyObject *Py_UNUSED(args))
24342434
Py_RETURN_NONE;
24352435
}
24362436

2437-
static PyObject *
2438-
test_pyset_add_exact_set(PyObject *self, PyObject *Py_UNUSED(ignored))
2439-
{
2440-
// Test: Adding to a regular set
2441-
PyObject *set = PySet_New(NULL);
2442-
if (set == NULL) {
2443-
return NULL;
2444-
}
2445-
PyObject *one = PyLong_FromLong(1);
2446-
assert(one);
2447-
2448-
if (PySet_Add(set, one) < 0) {
2449-
Py_DECREF(set);
2450-
return raiseTestError(self, "test_pyset_add_exact_set",
2451-
"PySet_Add to empty set failed");
2452-
}
2453-
if (PySet_Size(set) != 1) {
2454-
Py_DECREF(set);
2455-
return raiseTestError(self, "test_pyset_add_exact_set",
2456-
"set size should be 1 after adding one element");
2457-
}
2458-
if (PySet_Contains(set, one) != 1) {
2459-
Py_DECREF(set);
2460-
return raiseTestError(self, "test_pyset_add_exact_set",
2461-
"set should contain the added element");
2462-
}
2463-
Py_DECREF(set);
2464-
2465-
// Test: Adding unhashable item should raise TypeError
2466-
set = PySet_New(NULL);
2467-
if (set == NULL) {
2468-
return NULL;
2469-
}
2470-
PyObject *unhashable = PyList_New(0);
2471-
if (unhashable == NULL) {
2472-
Py_DECREF(set);
2473-
return NULL;
2474-
}
2475-
if (PySet_Add(set, unhashable) != -1) {
2476-
Py_DECREF(unhashable);
2477-
Py_DECREF(set);
2478-
return raiseTestError(self, "test_pyset_add_exact_set",
2479-
"PySet_Add with unhashable should fail");
2480-
}
2481-
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2482-
Py_DECREF(unhashable);
2483-
Py_DECREF(set);
2484-
return raiseTestError(self, "test_pyset_add_exact_set",
2485-
"PySet_Add with unhashable should raise TypeError");
2486-
}
2487-
PyErr_Clear();
2488-
Py_DECREF(unhashable);
2489-
Py_DECREF(set);
2490-
2491-
Py_RETURN_NONE;
2492-
}
2493-
2494-
static PyObject *
2495-
test_pyset_add_frozenset(PyObject *self, PyObject *Py_UNUSED(ignored))
2496-
{
2497-
PyObject *one = PyLong_FromLong(1);
2498-
assert(one);
2499-
2500-
// Test: Adding to uniquely-referenced frozenset should succeed
2501-
PyObject *frozenset = PyFrozenSet_New(NULL);
2502-
if (frozenset == NULL) {
2503-
return NULL;
2504-
}
2505-
2506-
// frozenset is uniquely referenced here, so PySet_Add should work
2507-
if (PySet_Add(frozenset, one) < 0) {
2508-
Py_DECREF(frozenset);
2509-
return raiseTestError(self, "test_pyset_add_frozenset",
2510-
"PySet_Add to uniquely-referenced frozenset failed");
2511-
}
2512-
Py_DECREF(frozenset);
2513-
2514-
// Test: Adding to non-uniquely-referenced frozenset should raise SystemError
2515-
frozenset = PyFrozenSet_New(NULL);
2516-
if (frozenset == NULL) {
2517-
return NULL;
2518-
}
2519-
Py_INCREF(frozenset); // Make it non-uniquely referenced
2520-
2521-
if (PySet_Add(frozenset, one) != -1) {
2522-
Py_DECREF(frozenset);
2523-
Py_DECREF(frozenset);
2524-
return raiseTestError(self, "test_pyset_add_frozenset",
2525-
"PySet_Add to non-uniquely-referenced frozenset should fail");
2526-
}
2527-
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
2528-
Py_DECREF(frozenset);
2529-
Py_DECREF(frozenset);
2530-
return raiseTestError(self, "test_pyset_add_frozenset",
2531-
"PySet_Add to non-uniquely-referenced frozenset should raise SystemError");
2532-
}
2533-
PyErr_Clear();
2534-
Py_DECREF(frozenset);
2535-
Py_DECREF(frozenset);
2536-
2537-
// Test: GC tracking - frozenset with only immutable items should not be tracked
2538-
frozenset = PyFrozenSet_New(NULL);
2539-
if (frozenset == NULL) {
2540-
return NULL;
2541-
}
2542-
if (PySet_Add(frozenset, one) < 0) {
2543-
Py_DECREF(frozenset);
2544-
return NULL;
2545-
}
2546-
if (PyObject_GC_IsTracked(frozenset)) {
2547-
Py_DECREF(frozenset);
2548-
return raiseTestError(self, "test_pyset_add_frozenset",
2549-
"frozenset with only int should not be GC tracked");
2550-
}
2551-
Py_DECREF(frozenset);
2552-
2553-
// Test: GC tracking - frozenset with tracked object should be tracked
2554-
frozenset = PyFrozenSet_New(NULL);
2555-
if (frozenset == NULL) {
2556-
return NULL;
2557-
}
2558-
2559-
PyObject *tracked_obj = PyErr_NewException("hashable_and_tracked", NULL, NULL);
2560-
if (tracked_obj == NULL) {
2561-
Py_DECREF(frozenset);
2562-
return NULL;
2563-
}
2564-
if (!PyObject_GC_IsTracked(tracked_obj)) {
2565-
return raiseTestError(self, "test_pyset_add_frozenset",
2566-
"test object should be tracked");
2567-
}
2568-
if (PySet_Add(frozenset, tracked_obj) < 0) {
2569-
Py_DECREF(frozenset);
2570-
Py_DECREF(tracked_obj);
2571-
return NULL;
2572-
}
2573-
2574-
if (!PyObject_GC_IsTracked(frozenset)) {
2575-
Py_DECREF(frozenset);
2576-
Py_DECREF(tracked_obj);
2577-
return raiseTestError(self, "test_pyset_add_frozenset",
2578-
"frozenset with with GC tracked object should be tracked");
2579-
}
2580-
2581-
Py_RETURN_NONE;
2582-
}
2583-
25842437

25852438
// Used by `finalize_thread_hang`.
25862439
#if defined(_POSIX_THREADS) && !defined(__wasi__)
@@ -2832,8 +2685,6 @@ static PyMethodDef TestMethods[] = {
28322685
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
28332686
{"function_set_warning", function_set_warning, METH_NOARGS},
28342687
{"test_critical_sections", test_critical_sections, METH_NOARGS},
2835-
{"test_pyset_add_exact_set", test_pyset_add_exact_set, METH_NOARGS},
2836-
{"test_pyset_add_frozenset", test_pyset_add_frozenset, METH_NOARGS},
28372688
{"finalize_thread_hang", finalize_thread_hang, METH_O, NULL},
28382689
{"test_atexit", test_atexit, METH_NOARGS},
28392690
{"code_offset_to_line", _PyCFunction_CAST(code_offset_to_line), METH_FASTCALL},

Modules/_testlimitedcapi/set.c

Lines changed: 161 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "parts.h"
99
#include "util.h"
1010

11-
1211
static PyObject *
1312
set_check(PyObject *self, PyObject *obj)
1413
{
@@ -128,6 +127,73 @@ set_clear(PyObject *self, PyObject *obj)
128127
RETURN_INT(PySet_Clear(obj));
129128
}
130129

130+
/* Raise AssertionError with test_name + ": " + msg, and return NULL. */
131+
132+
static PyObject *
133+
raiseTestError(const char* test_name, const char* msg)
134+
{
135+
PyObject *exc = PyErr_GetRaisedException();
136+
PyErr_Format(exc, "%s: %s", test_name, msg);
137+
return NULL;
138+
}
139+
140+
static PyObject *
141+
test_pyset_add_exact_set(PyObject *self, PyObject *Py_UNUSED(ignored))
142+
{
143+
// Test: Adding to a regular set
144+
PyObject *set = PySet_New(NULL);
145+
if (set == NULL) {
146+
return NULL;
147+
}
148+
PyObject *one = PyLong_FromLong(1);
149+
assert(one);
150+
151+
if (PySet_Add(set, one) < 0) {
152+
Py_DECREF(set);
153+
return raiseTestError("test_pyset_add_exact_set",
154+
"PySet_Add to empty set failed");
155+
}
156+
if (PySet_Size(set) != 1) {
157+
Py_DECREF(set);
158+
return raiseTestError("test_pyset_add_exact_set",
159+
"set size should be 1 after adding one element");
160+
}
161+
if (PySet_Contains(set, one) != 1) {
162+
Py_DECREF(set);
163+
return raiseTestError("test_pyset_add_exact_set",
164+
"set should contain the added element");
165+
}
166+
Py_DECREF(set);
167+
168+
// Test: Adding unhashable item should raise TypeError
169+
set = PySet_New(NULL);
170+
if (set == NULL) {
171+
return NULL;
172+
}
173+
PyObject *unhashable = PyList_New(0);
174+
if (unhashable == NULL) {
175+
Py_DECREF(set);
176+
return NULL;
177+
}
178+
if (PySet_Add(set, unhashable) != -1) {
179+
Py_DECREF(unhashable);
180+
Py_DECREF(set);
181+
return raiseTestError("test_pyset_add_exact_set",
182+
"PySet_Add with unhashable should fail");
183+
}
184+
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
185+
Py_DECREF(unhashable);
186+
Py_DECREF(set);
187+
return raiseTestError("test_pyset_add_exact_set",
188+
"PySet_Add with unhashable should raise TypeError");
189+
}
190+
PyErr_Clear();
191+
Py_DECREF(unhashable);
192+
Py_DECREF(set);
193+
194+
Py_RETURN_NONE;
195+
}
196+
131197
static PyObject *
132198
test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
133199
{
@@ -163,6 +229,98 @@ test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
163229
return NULL;
164230
}
165231

232+
static PyObject *
233+
test_pyset_add_frozenset(PyObject *self, PyObject *Py_UNUSED(ignored))
234+
{
235+
PyObject *one = PyLong_FromLong(1);
236+
assert(one);
237+
238+
// Test: Adding to uniquely-referenced frozenset should succeed
239+
PyObject *frozenset = PyFrozenSet_New(NULL);
240+
if (frozenset == NULL) {
241+
return NULL;
242+
}
243+
244+
// frozenset is uniquely referenced here, so PySet_Add should work
245+
if (PySet_Add(frozenset, one) < 0) {
246+
Py_DECREF(frozenset);
247+
return raiseTestError("test_pyset_add_frozenset",
248+
"PySet_Add to uniquely-referenced frozenset failed");
249+
}
250+
Py_DECREF(frozenset);
251+
252+
// Test: Adding to non-uniquely-referenced frozenset should raise SystemError
253+
frozenset = PyFrozenSet_New(NULL);
254+
if (frozenset == NULL) {
255+
return NULL;
256+
}
257+
Py_INCREF(frozenset); // Make it non-uniquely referenced
258+
259+
if (PySet_Add(frozenset, one) != -1) {
260+
Py_DECREF(frozenset);
261+
Py_DECREF(frozenset);
262+
return raiseTestError("test_pyset_add_frozenset",
263+
"PySet_Add to non-uniquely-referenced frozenset should fail");
264+
}
265+
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
266+
Py_DECREF(frozenset);
267+
Py_DECREF(frozenset);
268+
return raiseTestError("test_pyset_add_frozenset",
269+
"PySet_Add to non-uniquely-referenced frozenset should raise SystemError");
270+
}
271+
PyErr_Clear();
272+
Py_DECREF(frozenset);
273+
Py_DECREF(frozenset);
274+
275+
// Test: GC tracking - frozenset with only immutable items should not be tracked
276+
frozenset = PyFrozenSet_New(NULL);
277+
if (frozenset == NULL) {
278+
return NULL;
279+
}
280+
if (PySet_Add(frozenset, one) < 0) {
281+
Py_DECREF(frozenset);
282+
return NULL;
283+
}
284+
if (PyObject_GC_IsTracked(frozenset)) {
285+
Py_DECREF(frozenset);
286+
return raiseTestError("test_pyset_add_frozenset",
287+
"frozenset with only int should not be GC tracked");
288+
}
289+
Py_DECREF(frozenset);
290+
291+
// Test: GC tracking - frozenset with tracked object should be tracked
292+
frozenset = PyFrozenSet_New(NULL);
293+
if (frozenset == NULL) {
294+
return NULL;
295+
}
296+
297+
PyObject *tracked_obj = PyErr_NewException("_testlimitedcapi.py_set_add", NULL, NULL);
298+
if (tracked_obj == NULL) {
299+
Py_DECREF(frozenset);
300+
return NULL;
301+
}
302+
if (!PyObject_GC_IsTracked(tracked_obj)) {
303+
return raiseTestError("test_pyset_add_frozenset",
304+
"test object should be tracked");
305+
}
306+
Py_RETURN_NONE;
307+
if (PySet_Add(frozenset, tracked_obj) < 0) {
308+
Py_DECREF(frozenset);
309+
Py_DECREF(tracked_obj);
310+
return NULL;
311+
}
312+
313+
if (!PyObject_GC_IsTracked(frozenset)) {
314+
Py_DECREF(frozenset);
315+
Py_DECREF(tracked_obj);
316+
return raiseTestError("test_pyset_add_frozenset",
317+
"frozenset with with GC tracked object should be tracked");
318+
}
319+
320+
Py_RETURN_NONE;
321+
}
322+
323+
166324
static PyObject *
167325
test_set_contains_does_not_convert_unhashable_key(PyObject *self, PyObject *Py_UNUSED(obj))
168326
{
@@ -248,7 +406,8 @@ static PyMethodDef test_methods[] = {
248406
{"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
249407
{"test_set_contains_does_not_convert_unhashable_key",
250408
test_set_contains_does_not_convert_unhashable_key, METH_NOARGS},
251-
{"pyset_add", _PyCFunction_CAST(pyset_add), METH_FASTCALL},
409+
{"test_pyset_add_exact_set", test_pyset_add_exact_set, METH_NOARGS},
410+
{"test_pyset_add_frozenset", test_pyset_add_frozenset, METH_NOARGS},
252411

253412
{NULL},
254413
};

0 commit comments

Comments
 (0)