Debian Codegraph @

Modules/_testlimitedcapi/tuple.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4
5 static PyObject *
6 tuple_check(PyObject* Py_UNUSED(module), PyObject *obj)
7 {
8 NULLABLE(obj);
9 return PyLong_FromLong(PyTuple_Check(obj));
10 }
11
12 static PyObject *
13 tuple_checkexact(PyObject* Py_UNUSED(module), PyObject *obj)
14 {
15 NULLABLE(obj);
16 return PyLong_FromLong(PyTuple_CheckExact(obj));
17 }
18
19 static PyObject *
20 tuple_new(PyObject* Py_UNUSED(module), PyObject *len)
21 {
22 return PyTuple_New(PyLong_AsSsize_t(len));
23 }
24
25 static PyObject *
26 tuple_pack(PyObject *Py_UNUSED(module), PyObject *args)
27 {
28 PyObject *arg1 = NULL, *arg2 = NULL;
29 Py_ssize_t size;
30
31 if (!PyArg_ParseTuple(args, "n|OO", &size, &arg1, &arg2)) {
32 return NULL;
33 }
34 if (arg1) {
35 NULLABLE(arg1);
36 if (arg2) {
37 NULLABLE(arg2);
38 return PyTuple_Pack(size, arg1, arg2);
39 }
40 return PyTuple_Pack(size, arg1);
41 }
42 return PyTuple_Pack(size);
43 }
44
45 static PyObject *
46 tuple_size(PyObject *Py_UNUSED(module), PyObject *obj)
47 {
48 NULLABLE(obj);
49 RETURN_SIZE(PyTuple_Size(obj));
50 }
51
52 static PyObject *
53 tuple_getitem(PyObject *Py_UNUSED(module), PyObject *args)
54 {
55 PyObject *obj;
56 Py_ssize_t i;
57 if (!PyArg_ParseTuple(args, "On", &obj, &i)) {
58 return NULL;
59 }
60 NULLABLE(obj);
61 return Py_XNewRef(PyTuple_GetItem(obj, i));
62 }
63
64 static PyObject *
65 tuple_getslice(PyObject *Py_UNUSED(module), PyObject *args)
66 {
67 PyObject *obj;
68 Py_ssize_t ilow, ihigh;
69 if (!PyArg_ParseTuple(args, "Onn", &obj, &ilow, &ihigh)) {
70 return NULL;
71 }
72 NULLABLE(obj);
73 return PyTuple_GetSlice(obj, ilow, ihigh);
74 }
75
76 static PyObject *
77 tuple_setitem(PyObject *Py_UNUSED(module), PyObject *args)
78 {
79 PyObject *obj, *value, *newtuple = NULL;
80 Py_ssize_t i;
81 if (!PyArg_ParseTuple(args, "OnO", &obj, &i, &value)) {
82 return NULL;
83 }
84 NULLABLE(value);
85 if (PyTuple_CheckExact(obj)) {
86 Py_ssize_t size = PyTuple_Size(obj);
87 newtuple = PyTuple_New(size);
88 if (!newtuple) {
89 return NULL;
90 }
91 for (Py_ssize_t n = 0; n < size; n++) {
92 if (PyTuple_SetItem(newtuple, n,
93 Py_XNewRef(PyTuple_GetItem(obj, n))) == -1) {
94 Py_DECREF(newtuple);
95 return NULL;
96 }
97 }
98
99 if (PyTuple_SetItem(newtuple, i, Py_XNewRef(value)) == -1) {
100 Py_DECREF(newtuple);
101 return NULL;
102 }
103 return newtuple;
104 }
105 else {
106 NULLABLE(obj);
107
108 if (PyTuple_SetItem(obj, i, Py_XNewRef(value)) == -1) {
109 return NULL;
110 }
111 return Py_XNewRef(obj);
112 }
113 }
114
115
116 static PyMethodDef test_methods[] = {
117 {"tuple_check", tuple_check, METH_O},
118 {"tuple_checkexact", tuple_checkexact, METH_O},
119 {"tuple_new", tuple_new, METH_O},
120 {"tuple_pack", tuple_pack, METH_VARARGS},
121 {"tuple_size", tuple_size, METH_O},
122 {"tuple_getitem", tuple_getitem, METH_VARARGS},
123 {"tuple_getslice", tuple_getslice, METH_VARARGS},
124 {"tuple_setitem", tuple_setitem, METH_VARARGS},
125 {NULL},
126 };
127
128 int
129 _PyTestLimitedCAPI_Init_Tuple(PyObject *m)
130 {
131 if (PyModule_AddFunctions(m, test_methods) < 0) {
132 return -1;
133 }
134
135 return 0;
136 }