Skip to content

Commit ae5cc7f

Browse files
committed
review comments
1 parent 5f8b1f2 commit ae5cc7f

File tree

5 files changed

+50
-55
lines changed

5 files changed

+50
-55
lines changed

Lib/test/test_capi/test_set.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import unittest
23

34
from test.support import import_helper
@@ -220,6 +221,32 @@ def test_clear(self):
220221
# CRASHES: clear(NULL)
221222

222223

224+
class TestPySet_Add(unittest.TestCase):
225+
def test_set(self):
226+
# Test the PySet_Add c-api for set objects
227+
s = set()
228+
self.assertEqual(_testlimitedcapi.pyset_add(s, 1), {1})
229+
self.assertRaises(TypeError, _testlimitedcapi.pyset_add, s, [])
230+
231+
def test_frozenset(self):
232+
# Test the PySet_Add c-api for frozenset objects
233+
self.assertEqual(_testlimitedcapi.pyset_add(frozenset(), 1), frozenset([1]))
234+
frozen_set = frozenset()
235+
# if the argument to PySet_Add is a frozenset that is not uniquely references an error is generated
236+
self.assertRaises(SystemError, _testlimitedcapi.pyset_add, frozen_set, 1)
237+
238+
def test_frozenset_gc_tracking(self):
239+
# see gh-140234
240+
class TrackedHashableClass():
241+
pass
242+
243+
a = TrackedHashableClass()
244+
result_set = _testlimitedcapi.pyset_add(frozenset(), 1)
245+
self.assertFalse(gc.is_tracked(result_set))
246+
result_set = _testlimitedcapi.pyset_add(frozenset(), a)
247+
self.assertTrue(gc.is_tracked(result_set))
248+
249+
223250
class TestInternalCAPI(BaseSetTests, unittest.TestCase):
224251
def test_set_update(self):
225252
update = _testinternalcapi.set_update

Lib/test/test_set.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import warnings
1010
import weakref
1111
from random import randrange, shuffle
12-
import _testcapi
1312
from test import support
1413
from test.support import warnings_helper
1514

@@ -2213,32 +2212,6 @@ def test_cuboctahedron(self):
22132212
for cubevert in edge:
22142213
self.assertIn(cubevert, g)
22152214

2216-
class TestPySet_Add(unittest.TestCase):
2217-
def test_set(self):
2218-
# Test the PySet_Add c-api for set objects
2219-
s = set()
2220-
self.assertEqual(_testcapi.pyset_add(s, 1), {1})
2221-
self.assertRaises(TypeError, _testcapi.pyset_add, s, [])
2222-
2223-
def test_frozenset(self):
2224-
# Test the PySet_Add c-api for frozenset objects
2225-
self.assertEqual(_testcapi.pyset_add(frozenset(), 1), frozenset([1]))
2226-
frozen_set = frozenset()
2227-
# if the argument to PySet_Add is a frozenset that is not uniquely references an error is generated
2228-
self.assertRaises(SystemError, _testcapi.pyset_add, frozen_set, 1)
2229-
2230-
def test_frozenset_gc_tracking(self):
2231-
# see gh-140234
2232-
class TrackedHashableClass():
2233-
pass
2234-
2235-
a = TrackedHashableClass()
2236-
result_set = _testcapi.pyset_add(frozenset(), 1)
2237-
self.assertFalse(gc.is_tracked(result_set))
2238-
result_set = _testcapi.pyset_add(frozenset(), a)
2239-
self.assertTrue(gc.is_tracked(result_set))
2240-
2241-
22422215
#==============================================================================
22432216

22442217
if __name__ == "__main__":

Modules/_testcapimodule.c

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

2437-
2438-
2439-
static PyObject *
2440-
// Interface to PySet_Add, returning the set
2441-
pyset_add(PyObject* self, PyObject* const* args, Py_ssize_t nargsf)
2442-
{
2443-
Py_ssize_t nargs = _PyVectorcall_NARGS(nargsf);
2444-
if (nargs != 2) {
2445-
PyErr_SetString(PyExc_ValueError, "pyset_add requires exactly two arguments");
2446-
return NULL;
2447-
}
2448-
PyObject *set = args[0];
2449-
PyObject *item = args[1];
2450-
2451-
int return_value = PySet_Add(set, item);
2452-
if (return_value < 0) {
2453-
return NULL;
2454-
}
2455-
return Py_NewRef(set);
2456-
}
2457-
24582437
// Used by `finalize_thread_hang`.
24592438
#if defined(_POSIX_THREADS) && !defined(__wasi__)
24602439
static void finalize_thread_hang_cleanup_callback(void *Py_UNUSED(arg)) {
@@ -2699,7 +2678,6 @@ static PyMethodDef TestMethods[] = {
26992678
{"gen_get_code", gen_get_code, METH_O, NULL},
27002679
{"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
27012680
{"test_code_api", test_code_api, METH_NOARGS, NULL},
2702-
{"pyset_add", _PyCFunction_CAST(pyset_add), METH_FASTCALL, NULL},
27032681
{"settrace_to_error", settrace_to_error, METH_O, NULL},
27042682
{"settrace_to_record", settrace_to_record, METH_O, NULL},
27052683
{"test_macros", test_macros, METH_NOARGS, NULL},

Modules/_testlimitedcapi/set.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ test_set_contains_does_not_convert_unhashable_key(PyObject *self, PyObject *Py_U
200200
return NULL;
201201
}
202202

203+
// Interface to PySet_Add, returning the set
204+
static PyObject *
205+
pyset_add(PyObject *self, PyObject *args)
206+
{
207+
PyObject *set, *item;
208+
if (!PyArg_ParseTuple(args, "OO", &set, &item)) {
209+
return NULL;
210+
}
211+
212+
int return_value = PySet_Add(set, item);
213+
if (return_value < 0) {
214+
return NULL;
215+
}
216+
return Py_NewRef(set);
217+
}
218+
203219
static PyMethodDef test_methods[] = {
204220
{"set_check", set_check, METH_O},
205221
{"set_checkexact", set_checkexact, METH_O},
@@ -221,6 +237,7 @@ static PyMethodDef test_methods[] = {
221237
{"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
222238
{"test_set_contains_does_not_convert_unhashable_key",
223239
test_set_contains_does_not_convert_unhashable_key, METH_NOARGS},
240+
{"pyset_add", pyset_add, METH_VARARGS},
224241

225242
{NULL},
226243
};

Programs/test_frozenmain.h

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)