| 1 |
#include "parts.h" |
| 2 |
#include "util.h" |
| 3 |
|
| 4 |
|
| 5 |
static PyObject * |
| 6 |
list_get_size(PyObject *Py_UNUSED(module), PyObject *obj) |
| 7 |
{ |
| 8 |
NULLABLE(obj); |
| 9 |
RETURN_SIZE(PyList_GET_SIZE(obj)); |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
static PyObject * |
| 14 |
list_get_item(PyObject *Py_UNUSED(module), PyObject *args) |
| 15 |
{ |
| 16 |
PyObject *obj; |
| 17 |
Py_ssize_t i; |
| 18 |
if (!PyArg_ParseTuple(args, "On", &obj, &i)) { |
| 19 |
return NULL; |
| 20 |
} |
| 21 |
NULLABLE(obj); |
| 22 |
return Py_XNewRef(PyList_GET_ITEM(obj, i)); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
static PyObject * |
| 27 |
list_set_item(PyObject *Py_UNUSED(module), PyObject *args) |
| 28 |
{ |
| 29 |
PyObject *obj, *value; |
| 30 |
Py_ssize_t i; |
| 31 |
if (!PyArg_ParseTuple(args, "OnO", &obj, &i, &value)) { |
| 32 |
return NULL; |
| 33 |
} |
| 34 |
NULLABLE(obj); |
| 35 |
NULLABLE(value); |
| 36 |
PyList_SET_ITEM(obj, i, Py_XNewRef(value)); |
| 37 |
Py_RETURN_NONE; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
static PyObject * |
| 43 |
list_clear(PyObject* Py_UNUSED(module), PyObject *obj) |
| 44 |
{ |
| 45 |
NULLABLE(obj); |
| 46 |
RETURN_INT(PyList_Clear(obj)); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
static PyObject * |
| 51 |
list_extend(PyObject* Py_UNUSED(module), PyObject *args) |
| 52 |
{ |
| 53 |
PyObject *obj, *arg; |
| 54 |
if (!PyArg_ParseTuple(args, "OO", &obj, &arg)) { |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
NULLABLE(obj); |
| 58 |
NULLABLE(arg); |
| 59 |
RETURN_INT(PyList_Extend(obj, arg)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
static PyObject* |
| 64 |
test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 65 |
{ |
| 66 |
PyObject* list; |
| 67 |
int i; |
| 68 |
|
| 69 |
/* SF bug 132008: PyList_Reverse segfaults */ |
| 70 |
#define NLIST 30 |
| 71 |
list = PyList_New(NLIST); |
| 72 |
if (list == (PyObject*)NULL) |
| 73 |
return (PyObject*)NULL; |
| 74 |
/* list = range(NLIST) */ |
| 75 |
for (i = 0; i < NLIST; ++i) { |
| 76 |
PyObject* anint = PyLong_FromLong(i); |
| 77 |
if (anint == (PyObject*)NULL) { |
| 78 |
Py_DECREF(list); |
| 79 |
return (PyObject*)NULL; |
| 80 |
} |
| 81 |
PyList_SET_ITEM(list, i, anint); |
| 82 |
} |
| 83 |
/* list.reverse(), via PyList_Reverse() */ |
| 84 |
i = PyList_Reverse(list); /* should not blow up! */ |
| 85 |
if (i != 0) { |
| 86 |
Py_DECREF(list); |
| 87 |
return (PyObject*)NULL; |
| 88 |
} |
| 89 |
/* Check that list == range(29, -1, -1) now */ |
| 90 |
for (i = 0; i < NLIST; ++i) { |
| 91 |
PyObject* anint = PyList_GET_ITEM(list, i); |
| 92 |
if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 93 |
PyErr_SetString(PyExc_AssertionError, |
| 94 |
"test_list_api: reverse screwed up"); |
| 95 |
Py_DECREF(list); |
| 96 |
return (PyObject*)NULL; |
| 97 |
} |
| 98 |
} |
| 99 |
Py_DECREF(list); |
| 100 |
#undef NLIST |
| 101 |
|
| 102 |
Py_RETURN_NONE; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
static PyMethodDef test_methods[] = { |
| 107 |
{"list_get_size", list_get_size, METH_O}, |
| 108 |
{"list_get_item", list_get_item, METH_VARARGS}, |
| 109 |
{"list_set_item", list_set_item, METH_VARARGS}, |
| 110 |
{"list_clear", list_clear, METH_O}, |
| 111 |
{"list_extend", list_extend, METH_VARARGS}, |
| 112 |
{"test_list_api", test_list_api, METH_NOARGS}, |
| 113 |
{NULL}, |
| 114 |
}; |
| 115 |
|
| 116 |
int |
| 117 |
_PyTestCapi_Init_List(PyObject *m) |
| 118 |
{ |
| 119 |
return PyModule_AddFunctions(m, test_methods); |
| 120 |
} |