| 1 |
// Need limited C API version 3.13 for PyList_GetItemRef() |
| 2 |
#include "pyconfig.h" // Py_GIL_DISABLED |
| 3 |
#if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API) |
| 4 |
# define Py_LIMITED_API 0x030d0000 |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "parts.h" |
| 8 |
#include "util.h" |
| 9 |
|
| 10 |
static PyObject * |
| 11 |
list_check(PyObject* Py_UNUSED(module), PyObject *obj) |
| 12 |
{ |
| 13 |
NULLABLE(obj); |
| 14 |
return PyLong_FromLong(PyList_Check(obj)); |
| 15 |
} |
| 16 |
|
| 17 |
static PyObject * |
| 18 |
list_check_exact(PyObject* Py_UNUSED(module), PyObject *obj) |
| 19 |
{ |
| 20 |
NULLABLE(obj); |
| 21 |
return PyLong_FromLong(PyList_CheckExact(obj)); |
| 22 |
} |
| 23 |
|
| 24 |
static PyObject * |
| 25 |
list_new(PyObject* Py_UNUSED(module), PyObject *obj) |
| 26 |
{ |
| 27 |
return PyList_New(PyLong_AsSsize_t(obj)); |
| 28 |
} |
| 29 |
|
| 30 |
static PyObject * |
| 31 |
list_size(PyObject *Py_UNUSED(module), PyObject *obj) |
| 32 |
{ |
| 33 |
NULLABLE(obj); |
| 34 |
RETURN_SIZE(PyList_Size(obj)); |
| 35 |
} |
| 36 |
|
| 37 |
static PyObject * |
| 38 |
list_getitem(PyObject *Py_UNUSED(module), PyObject *args) |
| 39 |
{ |
| 40 |
PyObject *obj; |
| 41 |
Py_ssize_t i; |
| 42 |
if (!PyArg_ParseTuple(args, "On", &obj, &i)) { |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
NULLABLE(obj); |
| 46 |
return Py_XNewRef(PyList_GetItem(obj, i)); |
| 47 |
} |
| 48 |
|
| 49 |
static PyObject * |
| 50 |
list_get_item_ref(PyObject *Py_UNUSED(module), PyObject *args) |
| 51 |
{ |
| 52 |
PyObject *obj; |
| 53 |
Py_ssize_t i; |
| 54 |
if (!PyArg_ParseTuple(args, "On", &obj, &i)) { |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
NULLABLE(obj); |
| 58 |
return PyList_GetItemRef(obj, i); |
| 59 |
} |
| 60 |
|
| 61 |
static PyObject * |
| 62 |
list_setitem(PyObject *Py_UNUSED(module), PyObject *args) |
| 63 |
{ |
| 64 |
PyObject *obj, *value; |
| 65 |
Py_ssize_t i; |
| 66 |
if (!PyArg_ParseTuple(args, "OnO", &obj, &i, &value)) { |
| 67 |
return NULL; |
| 68 |
} |
| 69 |
NULLABLE(obj); |
| 70 |
NULLABLE(value); |
| 71 |
RETURN_INT(PyList_SetItem(obj, i, Py_XNewRef(value))); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
static PyObject * |
| 76 |
list_insert(PyObject *Py_UNUSED(module), PyObject *args) |
| 77 |
{ |
| 78 |
PyObject *obj, *value; |
| 79 |
Py_ssize_t where; |
| 80 |
if (!PyArg_ParseTuple(args, "OnO", &obj, &where, &value)) { |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
NULLABLE(obj); |
| 84 |
NULLABLE(value); |
| 85 |
RETURN_INT(PyList_Insert(obj, where, Py_XNewRef(value))); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
static PyObject * |
| 90 |
list_append(PyObject *Py_UNUSED(module), PyObject *args) |
| 91 |
{ |
| 92 |
PyObject *obj, *value; |
| 93 |
if (!PyArg_ParseTuple(args, "OO", &obj, &value)) { |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
NULLABLE(obj); |
| 97 |
NULLABLE(value); |
| 98 |
RETURN_INT(PyList_Append(obj, value)); |
| 99 |
} |
| 100 |
|
| 101 |
static PyObject * |
| 102 |
list_getslice(PyObject *Py_UNUSED(module), PyObject *args) |
| 103 |
{ |
| 104 |
PyObject *obj; |
| 105 |
Py_ssize_t ilow, ihigh; |
| 106 |
if (!PyArg_ParseTuple(args, "Onn", &obj, &ilow, &ihigh)) { |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
NULLABLE(obj); |
| 110 |
return PyList_GetSlice(obj, ilow, ihigh); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
static PyObject * |
| 115 |
list_setslice(PyObject *Py_UNUSED(module), PyObject *args) |
| 116 |
{ |
| 117 |
PyObject *obj, *value; |
| 118 |
Py_ssize_t ilow, ihigh; |
| 119 |
if (!PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh, &value)) { |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
NULLABLE(obj); |
| 123 |
NULLABLE(value); |
| 124 |
RETURN_INT(PyList_SetSlice(obj, ilow, ihigh, value)); |
| 125 |
} |
| 126 |
|
| 127 |
static PyObject * |
| 128 |
list_sort(PyObject* Py_UNUSED(module), PyObject *obj) |
| 129 |
{ |
| 130 |
NULLABLE(obj); |
| 131 |
RETURN_INT(PyList_Sort(obj)); |
| 132 |
} |
| 133 |
|
| 134 |
static PyObject * |
| 135 |
list_reverse(PyObject* Py_UNUSED(module), PyObject *obj) |
| 136 |
{ |
| 137 |
NULLABLE(obj); |
| 138 |
RETURN_INT(PyList_Reverse(obj)); |
| 139 |
} |
| 140 |
|
| 141 |
static PyObject * |
| 142 |
list_astuple(PyObject* Py_UNUSED(module), PyObject *obj) |
| 143 |
{ |
| 144 |
NULLABLE(obj); |
| 145 |
return PyList_AsTuple(obj); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
static PyMethodDef test_methods[] = { |
| 150 |
{"list_check", list_check, METH_O}, |
| 151 |
{"list_check_exact", list_check_exact, METH_O}, |
| 152 |
{"list_new", list_new, METH_O}, |
| 153 |
{"list_size", list_size, METH_O}, |
| 154 |
{"list_getitem", list_getitem, METH_VARARGS}, |
| 155 |
{"list_get_item_ref", list_get_item_ref, METH_VARARGS}, |
| 156 |
{"list_setitem", list_setitem, METH_VARARGS}, |
| 157 |
{"list_insert", list_insert, METH_VARARGS}, |
| 158 |
{"list_append", list_append, METH_VARARGS}, |
| 159 |
{"list_getslice", list_getslice, METH_VARARGS}, |
| 160 |
{"list_setslice", list_setslice, METH_VARARGS}, |
| 161 |
{"list_sort", list_sort, METH_O}, |
| 162 |
{"list_reverse", list_reverse, METH_O}, |
| 163 |
{"list_astuple", list_astuple, METH_O}, |
| 164 |
{NULL}, |
| 165 |
}; |
| 166 |
|
| 167 |
int |
| 168 |
_PyTestLimitedCAPI_Init_List(PyObject *m) |
| 169 |
{ |
| 170 |
if (PyModule_AddFunctions(m, test_methods) < 0) { |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
return 0; |
| 175 |
} |