summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-03-18 21:03:55 (GMT)
committerGitHub <noreply@github.com>2024-03-18 21:03:55 (GMT)
commitecb4a2b711d62f1395ddbff52576d0cca8a1b43e (patch)
tree4e4107b8792afb9a65951b0d1b99fc81b3ed18d7 /Modules
parent415964417771946dcb7a163951913adf84644b6d (diff)
downloadcpython-ecb4a2b711d62f1395ddbff52576d0cca8a1b43e.zip
cpython-ecb4a2b711d62f1395ddbff52576d0cca8a1b43e.tar.gz
cpython-ecb4a2b711d62f1395ddbff52576d0cca8a1b43e.tar.bz2
gh-116417: Move limited C API list.c tests to _testlimitedcapi (#116602)
Split list.c and set.c tests of _testcapi into two parts: limited C API tests in _testlimitedcapi and non-limited C API tests in _testcapi.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/Setup.stdlib.in2
-rw-r--r--Modules/_testcapi/list.c150
-rw-r--r--Modules/_testcapi/set.c172
-rw-r--r--Modules/_testlimitedcapi.c6
-rw-r--r--Modules/_testlimitedcapi/list.c169
-rw-r--r--Modules/_testlimitedcapi/parts.h2
-rw-r--r--Modules/_testlimitedcapi/set.c189
7 files changed, 368 insertions, 322 deletions
diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in
index deada66..d4743d3 100644
--- a/Modules/Setup.stdlib.in
+++ b/Modules/Setup.stdlib.in
@@ -163,7 +163,7 @@
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c
-@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/pyos.c _testlimitedcapi/sys.c _testlimitedcapi/vectorcall_limited.c
+@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/list.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/vectorcall_limited.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c
index 2cb6499..09cec4c 100644
--- a/Modules/_testcapi/list.c
+++ b/Modules/_testcapi/list.c
@@ -1,32 +1,6 @@
#include "parts.h"
#include "util.h"
-static PyObject *
-list_check(PyObject* Py_UNUSED(module), PyObject *obj)
-{
- NULLABLE(obj);
- return PyLong_FromLong(PyList_Check(obj));
-}
-
-static PyObject *
-list_check_exact(PyObject* Py_UNUSED(module), PyObject *obj)
-{
- NULLABLE(obj);
- return PyLong_FromLong(PyList_CheckExact(obj));
-}
-
-static PyObject *
-list_new(PyObject* Py_UNUSED(module), PyObject *obj)
-{
- return PyList_New(PyLong_AsSsize_t(obj));
-}
-
-static PyObject *
-list_size(PyObject *Py_UNUSED(module), PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_SIZE(PyList_Size(obj));
-}
static PyObject *
list_get_size(PyObject *Py_UNUSED(module), PyObject *obj)
@@ -35,17 +9,6 @@ list_get_size(PyObject *Py_UNUSED(module), PyObject *obj)
RETURN_SIZE(PyList_GET_SIZE(obj));
}
-static PyObject *
-list_getitem(PyObject *Py_UNUSED(module), PyObject *args)
-{
- PyObject *obj;
- Py_ssize_t i;
- if (!PyArg_ParseTuple(args, "On", &obj, &i)) {
- return NULL;
- }
- NULLABLE(obj);
- return Py_XNewRef(PyList_GetItem(obj, i));
-}
static PyObject *
list_get_item(PyObject *Py_UNUSED(module), PyObject *args)
@@ -59,31 +22,6 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args)
return Py_XNewRef(PyList_GET_ITEM(obj, i));
}
-static PyObject *
-list_get_item_ref(PyObject *Py_UNUSED(module), PyObject *args)
-{
- PyObject *obj;
- Py_ssize_t i;
- if (!PyArg_ParseTuple(args, "On", &obj, &i)) {
- return NULL;
- }
- NULLABLE(obj);
- return PyList_GetItemRef(obj, i);
-}
-
-static PyObject *
-list_setitem(PyObject *Py_UNUSED(module), PyObject *args)
-{
- PyObject *obj, *value;
- Py_ssize_t i;
- if (!PyArg_ParseTuple(args, "OnO", &obj, &i, &value)) {
- return NULL;
- }
- NULLABLE(obj);
- NULLABLE(value);
- RETURN_INT(PyList_SetItem(obj, i, Py_XNewRef(value)));
-
-}
static PyObject *
list_set_item(PyObject *Py_UNUSED(module), PyObject *args)
@@ -100,79 +38,6 @@ list_set_item(PyObject *Py_UNUSED(module), PyObject *args)
}
-static PyObject *
-list_insert(PyObject *Py_UNUSED(module), PyObject *args)
-{
- PyObject *obj, *value;
- Py_ssize_t where;
- if (!PyArg_ParseTuple(args, "OnO", &obj, &where, &value)) {
- return NULL;
- }
- NULLABLE(obj);
- NULLABLE(value);
- RETURN_INT(PyList_Insert(obj, where, Py_XNewRef(value)));
-
-}
-
-static PyObject *
-list_append(PyObject *Py_UNUSED(module), PyObject *args)
-{
- PyObject *obj, *value;
- if (!PyArg_ParseTuple(args, "OO", &obj, &value)) {
- return NULL;
- }
- NULLABLE(obj);
- NULLABLE(value);
- RETURN_INT(PyList_Append(obj, value));
-}
-
-static PyObject *
-list_getslice(PyObject *Py_UNUSED(module), PyObject *args)
-{
- PyObject *obj;
- Py_ssize_t ilow, ihigh;
- if (!PyArg_ParseTuple(args, "Onn", &obj, &ilow, &ihigh)) {
- return NULL;
- }
- NULLABLE(obj);
- return PyList_GetSlice(obj, ilow, ihigh);
-
-}
-
-static PyObject *
-list_setslice(PyObject *Py_UNUSED(module), PyObject *args)
-{
- PyObject *obj, *value;
- Py_ssize_t ilow, ihigh;
- if (!PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh, &value)) {
- return NULL;
- }
- NULLABLE(obj);
- NULLABLE(value);
- RETURN_INT(PyList_SetSlice(obj, ilow, ihigh, value));
-}
-
-static PyObject *
-list_sort(PyObject* Py_UNUSED(module), PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PyList_Sort(obj));
-}
-
-static PyObject *
-list_reverse(PyObject* Py_UNUSED(module), PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PyList_Reverse(obj));
-}
-
-static PyObject *
-list_astuple(PyObject* Py_UNUSED(module), PyObject *obj)
-{
- NULLABLE(obj);
- return PyList_AsTuple(obj);
-}
-
static PyObject *
list_clear(PyObject* Py_UNUSED(module), PyObject *obj)
@@ -196,25 +61,12 @@ list_extend(PyObject* Py_UNUSED(module), PyObject *args)
static PyMethodDef test_methods[] = {
- {"list_check", list_check, METH_O},
- {"list_check_exact", list_check_exact, METH_O},
- {"list_new", list_new, METH_O},
- {"list_size", list_size, METH_O},
{"list_get_size", list_get_size, METH_O},
- {"list_getitem", list_getitem, METH_VARARGS},
{"list_get_item", list_get_item, METH_VARARGS},
- {"list_get_item_ref", list_get_item_ref, METH_VARARGS},
- {"list_setitem", list_setitem, METH_VARARGS},
{"list_set_item", list_set_item, METH_VARARGS},
- {"list_insert", list_insert, METH_VARARGS},
- {"list_append", list_append, METH_VARARGS},
- {"list_getslice", list_getslice, METH_VARARGS},
- {"list_setslice", list_setslice, METH_VARARGS},
- {"list_sort", list_sort, METH_O},
- {"list_reverse", list_reverse, METH_O},
- {"list_astuple", list_astuple, METH_O},
{"list_clear", list_clear, METH_O},
{"list_extend", list_extend, METH_VARARGS},
+
{NULL},
};
diff --git a/Modules/_testcapi/set.c b/Modules/_testcapi/set.c
index 2fbd0ae..31b52ce 100644
--- a/Modules/_testcapi/set.c
+++ b/Modules/_testcapi/set.c
@@ -2,186 +2,14 @@
#include "util.h"
static PyObject *
-set_check(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PySet_Check(obj));
-}
-
-static PyObject *
-set_checkexact(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PySet_CheckExact(obj));
-}
-
-static PyObject *
-frozenset_check(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PyFrozenSet_Check(obj));
-}
-
-static PyObject *
-frozenset_checkexact(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PyFrozenSet_CheckExact(obj));
-}
-
-static PyObject *
-anyset_check(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PyAnySet_Check(obj));
-}
-
-static PyObject *
-anyset_checkexact(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PyAnySet_CheckExact(obj));
-}
-
-static PyObject *
-set_new(PyObject *self, PyObject *args)
-{
- PyObject *iterable = NULL;
- if (!PyArg_ParseTuple(args, "|O", &iterable)) {
- return NULL;
- }
- return PySet_New(iterable);
-}
-
-static PyObject *
-frozenset_new(PyObject *self, PyObject *args)
-{
- PyObject *iterable = NULL;
- if (!PyArg_ParseTuple(args, "|O", &iterable)) {
- return NULL;
- }
- return PyFrozenSet_New(iterable);
-}
-
-static PyObject *
-set_size(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_SIZE(PySet_Size(obj));
-}
-
-static PyObject *
set_get_size(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_SIZE(PySet_GET_SIZE(obj));
}
-static PyObject *
-set_contains(PyObject *self, PyObject *args)
-{
- PyObject *obj, *item;
- if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
- return NULL;
- }
- NULLABLE(obj);
- NULLABLE(item);
- RETURN_INT(PySet_Contains(obj, item));
-}
-
-static PyObject *
-set_add(PyObject *self, PyObject *args)
-{
- PyObject *obj, *item;
- if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
- return NULL;
- }
- NULLABLE(obj);
- NULLABLE(item);
- RETURN_INT(PySet_Add(obj, item));
-}
-
-static PyObject *
-set_discard(PyObject *self, PyObject *args)
-{
- PyObject *obj, *item;
- if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
- return NULL;
- }
- NULLABLE(obj);
- NULLABLE(item);
- RETURN_INT(PySet_Discard(obj, item));
-}
-
-static PyObject *
-set_pop(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- return PySet_Pop(obj);
-}
-
-static PyObject *
-set_clear(PyObject *self, PyObject *obj)
-{
- NULLABLE(obj);
- RETURN_INT(PySet_Clear(obj));
-}
-
-static PyObject *
-test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
-{
- // Test that `frozenset` can be used with `PySet_Add`,
- // when frozenset is just created in CAPI.
- PyObject *fs = PyFrozenSet_New(NULL);
- if (fs == NULL) {
- return NULL;
- }
- PyObject *num = PyLong_FromLong(1);
- if (num == NULL) {
- goto error;
- }
- if (PySet_Add(fs, num) < 0) {
- goto error;
- }
- int contains = PySet_Contains(fs, num);
- if (contains < 0) {
- goto error;
- }
- else if (contains == 0) {
- goto unexpected;
- }
- Py_DECREF(fs);
- Py_DECREF(num);
- Py_RETURN_NONE;
-
-unexpected:
- PyErr_SetString(PyExc_ValueError, "set does not contain expected value");
-error:
- Py_DECREF(fs);
- Py_XDECREF(num);
- return NULL;
-}
-
static PyMethodDef test_methods[] = {
- {"set_check", set_check, METH_O},
- {"set_checkexact", set_checkexact, METH_O},
- {"frozenset_check", frozenset_check, METH_O},
- {"frozenset_checkexact", frozenset_checkexact, METH_O},
- {"anyset_check", anyset_check, METH_O},
- {"anyset_checkexact", anyset_checkexact, METH_O},
-
- {"set_new", set_new, METH_VARARGS},
- {"frozenset_new", frozenset_new, METH_VARARGS},
-
- {"set_size", set_size, METH_O},
{"set_get_size", set_get_size, METH_O},
- {"set_contains", set_contains, METH_VARARGS},
- {"set_add", set_add, METH_VARARGS},
- {"set_discard", set_discard, METH_VARARGS},
- {"set_pop", set_pop, METH_O},
- {"set_clear", set_clear, METH_O},
-
- {"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
{NULL},
};
diff --git a/Modules/_testlimitedcapi.c b/Modules/_testlimitedcapi.c
index 49bf6a3..a25b0aa 100644
--- a/Modules/_testlimitedcapi.c
+++ b/Modules/_testlimitedcapi.c
@@ -35,9 +35,15 @@ PyInit__testlimitedcapi(void)
if (_PyTestCapi_Init_HeaptypeRelative(mod) < 0) {
return NULL;
}
+ if (_PyTestCapi_Init_List(mod) < 0) {
+ return NULL;
+ }
if (_PyTestCapi_Init_PyOS(mod) < 0) {
return NULL;
}
+ if (_PyTestCapi_Init_Set(mod) < 0) {
+ return NULL;
+ }
if (_PyTestCapi_Init_Sys(mod) < 0) {
return NULL;
}
diff --git a/Modules/_testlimitedcapi/list.c b/Modules/_testlimitedcapi/list.c
new file mode 100644
index 0000000..0917900
--- /dev/null
+++ b/Modules/_testlimitedcapi/list.c
@@ -0,0 +1,169 @@
+#include "parts.h"
+#include "util.h"
+
+static PyObject *
+list_check(PyObject* Py_UNUSED(module), PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyLong_FromLong(PyList_Check(obj));
+}
+
+static PyObject *
+list_check_exact(PyObject* Py_UNUSED(module), PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyLong_FromLong(PyList_CheckExact(obj));
+}
+
+static PyObject *
+list_new(PyObject* Py_UNUSED(module), PyObject *obj)
+{
+ return PyList_New(PyLong_AsSsize_t(obj));
+}
+
+static PyObject *
+list_size(PyObject *Py_UNUSED(module), PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_SIZE(PyList_Size(obj));
+}
+
+static PyObject *
+list_getitem(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj;
+ Py_ssize_t i;
+ if (!PyArg_ParseTuple(args, "On", &obj, &i)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ return Py_XNewRef(PyList_GetItem(obj, i));
+}
+
+static PyObject *
+list_get_item_ref(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj;
+ Py_ssize_t i;
+ if (!PyArg_ParseTuple(args, "On", &obj, &i)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ return PyList_GetItemRef(obj, i);
+}
+
+static PyObject *
+list_setitem(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj, *value;
+ Py_ssize_t i;
+ if (!PyArg_ParseTuple(args, "OnO", &obj, &i, &value)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(value);
+ RETURN_INT(PyList_SetItem(obj, i, Py_XNewRef(value)));
+
+}
+
+static PyObject *
+list_insert(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj, *value;
+ Py_ssize_t where;
+ if (!PyArg_ParseTuple(args, "OnO", &obj, &where, &value)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(value);
+ RETURN_INT(PyList_Insert(obj, where, Py_XNewRef(value)));
+
+}
+
+static PyObject *
+list_append(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj, *value;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &value)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(value);
+ RETURN_INT(PyList_Append(obj, value));
+}
+
+static PyObject *
+list_getslice(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj;
+ Py_ssize_t ilow, ihigh;
+ if (!PyArg_ParseTuple(args, "Onn", &obj, &ilow, &ihigh)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ return PyList_GetSlice(obj, ilow, ihigh);
+
+}
+
+static PyObject *
+list_setslice(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ PyObject *obj, *value;
+ Py_ssize_t ilow, ihigh;
+ if (!PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh, &value)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(value);
+ RETURN_INT(PyList_SetSlice(obj, ilow, ihigh, value));
+}
+
+static PyObject *
+list_sort(PyObject* Py_UNUSED(module), PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PyList_Sort(obj));
+}
+
+static PyObject *
+list_reverse(PyObject* Py_UNUSED(module), PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PyList_Reverse(obj));
+}
+
+static PyObject *
+list_astuple(PyObject* Py_UNUSED(module), PyObject *obj)
+{
+ NULLABLE(obj);
+ return PyList_AsTuple(obj);
+}
+
+
+static PyMethodDef test_methods[] = {
+ {"list_check", list_check, METH_O},
+ {"list_check_exact", list_check_exact, METH_O},
+ {"list_new", list_new, METH_O},
+ {"list_size", list_size, METH_O},
+ {"list_getitem", list_getitem, METH_VARARGS},
+ {"list_get_item_ref", list_get_item_ref, METH_VARARGS},
+ {"list_setitem", list_setitem, METH_VARARGS},
+ {"list_insert", list_insert, METH_VARARGS},
+ {"list_append", list_append, METH_VARARGS},
+ {"list_getslice", list_getslice, METH_VARARGS},
+ {"list_setslice", list_setslice, METH_VARARGS},
+ {"list_sort", list_sort, METH_O},
+ {"list_reverse", list_reverse, METH_O},
+ {"list_astuple", list_astuple, METH_O},
+ {NULL},
+};
+
+int
+_PyTestCapi_Init_List(PyObject *m)
+{
+ if (PyModule_AddFunctions(m, test_methods) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/Modules/_testlimitedcapi/parts.h b/Modules/_testlimitedcapi/parts.h
index 9bc5241..bb867ec 100644
--- a/Modules/_testlimitedcapi/parts.h
+++ b/Modules/_testlimitedcapi/parts.h
@@ -25,7 +25,9 @@
int _PyTestCapi_Init_ByteArray(PyObject *module);
int _PyTestCapi_Init_Bytes(PyObject *module);
int _PyTestCapi_Init_HeaptypeRelative(PyObject *module);
+int _PyTestCapi_Init_List(PyObject *module);
int _PyTestCapi_Init_PyOS(PyObject *module);
+int _PyTestCapi_Init_Set(PyObject *module);
int _PyTestCapi_Init_Sys(PyObject *module);
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
diff --git a/Modules/_testlimitedcapi/set.c b/Modules/_testlimitedcapi/set.c
new file mode 100644
index 0000000..471b515
--- /dev/null
+++ b/Modules/_testlimitedcapi/set.c
@@ -0,0 +1,189 @@
+#include "parts.h"
+#include "util.h"
+
+static PyObject *
+set_check(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PySet_Check(obj));
+}
+
+static PyObject *
+set_checkexact(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PySet_CheckExact(obj));
+}
+
+static PyObject *
+frozenset_check(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PyFrozenSet_Check(obj));
+}
+
+static PyObject *
+frozenset_checkexact(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PyFrozenSet_CheckExact(obj));
+}
+
+static PyObject *
+anyset_check(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PyAnySet_Check(obj));
+}
+
+static PyObject *
+anyset_checkexact(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PyAnySet_CheckExact(obj));
+}
+
+static PyObject *
+set_new(PyObject *self, PyObject *args)
+{
+ PyObject *iterable = NULL;
+ if (!PyArg_ParseTuple(args, "|O", &iterable)) {
+ return NULL;
+ }
+ return PySet_New(iterable);
+}
+
+static PyObject *
+frozenset_new(PyObject *self, PyObject *args)
+{
+ PyObject *iterable = NULL;
+ if (!PyArg_ParseTuple(args, "|O", &iterable)) {
+ return NULL;
+ }
+ return PyFrozenSet_New(iterable);
+}
+
+static PyObject *
+set_size(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_SIZE(PySet_Size(obj));
+}
+
+static PyObject *
+set_contains(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *item;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(item);
+ RETURN_INT(PySet_Contains(obj, item));
+}
+
+static PyObject *
+set_add(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *item;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(item);
+ RETURN_INT(PySet_Add(obj, item));
+}
+
+static PyObject *
+set_discard(PyObject *self, PyObject *args)
+{
+ PyObject *obj, *item;
+ if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
+ return NULL;
+ }
+ NULLABLE(obj);
+ NULLABLE(item);
+ RETURN_INT(PySet_Discard(obj, item));
+}
+
+static PyObject *
+set_pop(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ return PySet_Pop(obj);
+}
+
+static PyObject *
+set_clear(PyObject *self, PyObject *obj)
+{
+ NULLABLE(obj);
+ RETURN_INT(PySet_Clear(obj));
+}
+
+static PyObject *
+test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
+{
+ // Test that `frozenset` can be used with `PySet_Add`,
+ // when frozenset is just created in CAPI.
+ PyObject *fs = PyFrozenSet_New(NULL);
+ if (fs == NULL) {
+ return NULL;
+ }
+ PyObject *num = PyLong_FromLong(1);
+ if (num == NULL) {
+ goto error;
+ }
+ if (PySet_Add(fs, num) < 0) {
+ goto error;
+ }
+ int contains = PySet_Contains(fs, num);
+ if (contains < 0) {
+ goto error;
+ }
+ else if (contains == 0) {
+ goto unexpected;
+ }
+ Py_DECREF(fs);
+ Py_DECREF(num);
+ Py_RETURN_NONE;
+
+unexpected:
+ PyErr_SetString(PyExc_ValueError, "set does not contain expected value");
+error:
+ Py_DECREF(fs);
+ Py_XDECREF(num);
+ return NULL;
+}
+
+static PyMethodDef test_methods[] = {
+ {"set_check", set_check, METH_O},
+ {"set_checkexact", set_checkexact, METH_O},
+ {"frozenset_check", frozenset_check, METH_O},
+ {"frozenset_checkexact", frozenset_checkexact, METH_O},
+ {"anyset_check", anyset_check, METH_O},
+ {"anyset_checkexact", anyset_checkexact, METH_O},
+
+ {"set_new", set_new, METH_VARARGS},
+ {"frozenset_new", frozenset_new, METH_VARARGS},
+
+ {"set_size", set_size, METH_O},
+ {"set_contains", set_contains, METH_VARARGS},
+ {"set_add", set_add, METH_VARARGS},
+ {"set_discard", set_discard, METH_VARARGS},
+ {"set_pop", set_pop, METH_O},
+ {"set_clear", set_clear, METH_O},
+
+ {"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
+
+ {NULL},
+};
+
+int
+_PyTestCapi_Init_Set(PyObject *m)
+{
+ if (PyModule_AddFunctions(m, test_methods) < 0) {
+ return -1;
+ }
+
+ return 0;
+}