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.
This commit is contained in:
Victor Stinner 2024-03-18 22:03:55 +01:00 committed by GitHub
parent 4159644177
commit ecb4a2b711
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 404 additions and 351 deletions

View file

@ -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

View file

@ -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},
};

View file

@ -1,75 +1,6 @@
#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_get_size(PyObject *self, PyObject *obj)
{
@ -77,111 +8,8 @@ set_get_size(PyObject *self, PyObject *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},
};

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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);

View file

@ -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;
}