Debian Codegraph @

mypyc/lib-rt/list_ops.c

raw
1 // List primitive operations
2 //
3 // These are registered in mypyc.primitives.list_ops.
4
5 #include <Python.h>
6 #include "CPy.h"
7
8 #ifndef Py_TPFLAGS_SEQUENCE
9 #define Py_TPFLAGS_SEQUENCE (1 << 5)
10 #endif
11
12 PyObject *CPyList_Build(Py_ssize_t len, ...) {
13 Py_ssize_t i;
14
15 PyObject *res = PyList_New(len);
16 if (res == NULL) {
17 return NULL;
18 }
19
20 va_list args;
21 va_start(args, len);
22 for (i = 0; i < len; i++) {
23 // Steals the reference
24 PyObject *value = va_arg(args, PyObject *);
25 PyList_SET_ITEM(res, i, value);
26 }
27 va_end(args);
28
29 return res;
30 }
31
32 char CPyList_Clear(PyObject *list) {
33 if (PyList_CheckExact(list)) {
34 PyList_Clear(list);
35 } else {
36 PyObject *res = PyObject_CallMethodNoArgs(list, mypyc_interned_str.clear);
37 if (res == NULL) {
38 return 0;
39 }
40 Py_DECREF(res);
41 }
42 return 1;
43 }
44
45 PyObject *CPyList_Copy(PyObject *list) {
46 if(PyList_CheckExact(list)) {
47 return PyList_GetSlice(list, 0, PyList_GET_SIZE(list));
48 }
49 return PyObject_CallMethodNoArgs(list, mypyc_interned_str.copy);
50 }
51
52 PyObject *CPyList_GetItemShort(PyObject *list, CPyTagged index) {
53 Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
54 Py_ssize_t size = PyList_GET_SIZE(list);
55 if (n >= 0) {
56 if (n >= size) {
57 PyErr_SetString(PyExc_IndexError, "list index out of range");
58 return NULL;
59 }
60 } else {
61 n += size;
62 if (n < 0) {
63 PyErr_SetString(PyExc_IndexError, "list index out of range");
64 return NULL;
65 }
66 }
67 PyObject *result = PyList_GET_ITEM(list, n);
68 Py_INCREF(result);
69 return result;
70 }
71
72 PyObject *CPyList_GetItemShortBorrow(PyObject *list, CPyTagged index) {
73 Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
74 Py_ssize_t size = PyList_GET_SIZE(list);
75 if (n >= 0) {
76 if (n >= size) {
77 PyErr_SetString(PyExc_IndexError, "list index out of range");
78 return NULL;
79 }
80 } else {
81 n += size;
82 if (n < 0) {
83 PyErr_SetString(PyExc_IndexError, "list index out of range");
84 return NULL;
85 }
86 }
87 return PyList_GET_ITEM(list, n);
88 }
89
90 PyObject *CPyList_GetItem(PyObject *list, CPyTagged index) {
91 if (CPyTagged_CheckShort(index)) {
92 Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
93 Py_ssize_t size = PyList_GET_SIZE(list);
94 if (n >= 0) {
95 if (n >= size) {
96 PyErr_SetString(PyExc_IndexError, "list index out of range");
97 return NULL;
98 }
99 } else {
100 n += size;
101 if (n < 0) {
102 PyErr_SetString(PyExc_IndexError, "list index out of range");
103 return NULL;
104 }
105 }
106 PyObject *result = PyList_GET_ITEM(list, n);
107 Py_INCREF(result);
108 return result;
109 } else {
110 PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
111 return NULL;
112 }
113 }
114
115 PyObject *CPyList_GetItemBorrow(PyObject *list, CPyTagged index) {
116 if (CPyTagged_CheckShort(index)) {
117 Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
118 Py_ssize_t size = PyList_GET_SIZE(list);
119 if (n >= 0) {
120 if (n >= size) {
121 PyErr_SetString(PyExc_IndexError, "list index out of range");
122 return NULL;
123 }
124 } else {
125 n += size;
126 if (n < 0) {
127 PyErr_SetString(PyExc_IndexError, "list index out of range");
128 return NULL;
129 }
130 }
131 return PyList_GET_ITEM(list, n);
132 } else {
133 PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
134 return NULL;
135 }
136 }
137
138 PyObject *CPyList_GetItemInt64(PyObject *list, int64_t index) {
139 size_t size = PyList_GET_SIZE(list);
140 if (likely((uint64_t)index < size)) {
141 PyObject *result = PyList_GET_ITEM(list, index);
142 Py_INCREF(result);
143 return result;
144 }
145 if (index >= 0) {
146 PyErr_SetString(PyExc_IndexError, "list index out of range");
147 return NULL;
148 }
149 index += size;
150 if (index < 0) {
151 PyErr_SetString(PyExc_IndexError, "list index out of range");
152 return NULL;
153 }
154 PyObject *result = PyList_GET_ITEM(list, index);
155 Py_INCREF(result);
156 return result;
157 }
158
159 PyObject *CPyList_GetItemInt64Borrow(PyObject *list, int64_t index) {
160 size_t size = PyList_GET_SIZE(list);
161 if (likely((uint64_t)index < size)) {
162 return PyList_GET_ITEM(list, index);
163 }
164 if (index >= 0) {
165 PyErr_SetString(PyExc_IndexError, "list index out of range");
166 return NULL;
167 }
168 index += size;
169 if (index < 0) {
170 PyErr_SetString(PyExc_IndexError, "list index out of range");
171 return NULL;
172 }
173 return PyList_GET_ITEM(list, index);
174 }
175
176 bool CPyList_SetItem(PyObject *list, CPyTagged index, PyObject *value) {
177 if (CPyTagged_CheckShort(index)) {
178 Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
179 Py_ssize_t size = PyList_GET_SIZE(list);
180 if (n >= 0) {
181 if (n >= size) {
182 PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
183 return false;
184 }
185 } else {
186 n += size;
187 if (n < 0) {
188 PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
189 return false;
190 }
191 }
192 // PyList_SET_ITEM doesn't decref the old element, so we do
193 Py_DECREF(PyList_GET_ITEM(list, n));
194 // N.B: Steals reference
195 PyList_SET_ITEM(list, n, value);
196 return true;
197 } else {
198 PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
199 return false;
200 }
201 }
202
203 bool CPyList_SetItemInt64(PyObject *list, int64_t index, PyObject *value) {
204 size_t size = PyList_GET_SIZE(list);
205 if (unlikely((uint64_t)index >= size)) {
206 if (index > 0) {
207 PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
208 return false;
209 }
210 index += size;
211 if (index < 0) {
212 PyErr_SetString(PyExc_IndexError, "list assignment index out of range");
213 return false;
214 }
215 }
216 // PyList_SET_ITEM doesn't decref the old element, so we do
217 Py_DECREF(PyList_GET_ITEM(list, index));
218 // N.B: Steals reference
219 PyList_SET_ITEM(list, index, value);
220 return true;
221 }
222
223 // This function should only be used to fill in brand new lists.
224 void CPyList_SetItemUnsafe(PyObject *list, Py_ssize_t index, PyObject *value) {
225 PyList_SET_ITEM(list, index, value);
226 }
227
228 #ifdef Py_GIL_DISABLED
229 // The original optimized list.pop implementation doesn't work on free-threaded
230 // builds, so provide an alternative that is a bit slower but works.
231 //
232 // Note that this implementation isn't intended to be atomic.
233 static inline PyObject *list_pop_index(PyObject *list, Py_ssize_t index) {
234 PyObject *item = PyList_GetItemRef(list, index);
235 if (item == NULL) {
236 return NULL;
237 }
238 if (PySequence_DelItem(list, index) < 0) {
239 Py_DECREF(item);
240 return NULL;
241 }
242 return item;
243 }
244 #endif
245
246 PyObject *CPyList_PopLast(PyObject *list)
247 {
248 #ifdef Py_GIL_DISABLED
249 // The other implementation causes segfaults on a free-threaded Python 3.14b4 build.
250 Py_ssize_t index = PyList_GET_SIZE(list) - 1;
251 return list_pop_index(list, index);
252 #else
253 // I tried a specalized version of pop_impl for just removing the
254 // last element and it wasn't any faster in microbenchmarks than
255 // the generic one so I ditched it.
256 return list_pop_impl((PyListObject *)list, -1);
257 #endif
258 }
259
260 PyObject *CPyList_Pop(PyObject *obj, CPyTagged index)
261 {
262 if (CPyTagged_CheckShort(index)) {
263 Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
264 #ifdef Py_GIL_DISABLED
265 // We must use a slower implementation on free-threaded builds.
266 if (n < 0) {
267 n += PyList_GET_SIZE(obj);
268 }
269 return list_pop_index(obj, n);
270 #else
271 return list_pop_impl((PyListObject *)obj, n);
272 #endif
273 } else {
274 PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
275 return NULL;
276 }
277 }
278
279 CPyTagged CPyList_Count(PyObject *obj, PyObject *value)
280 {
281 return list_count((PyListObject *)obj, value);
282 }
283
284 int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value)
285 {
286 if (CPyTagged_CheckShort(index)) {
287 Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
288 return PyList_Insert(list, n, value);
289 }
290 // The max range doesn't exactly coincide with ssize_t, but we still
291 // want to keep the error message compatible with CPython.
292 PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
293 return -1;
294 }
295
296 PyObject *CPyList_Extend(PyObject *o1, PyObject *o2) {
297 if (PyList_Extend(o1, o2) < 0) {
298 return NULL;
299 }
300 Py_RETURN_NONE;
301 }
302
303 // Return -2 or error, -1 if not found, or index of first match otherwise.
304 //
305 // The caller must hold a critical section on the list.
306 static Py_ssize_t _CPyList_Find(PyObject *list, PyObject *obj) {
307 Py_ssize_t i;
308 for (i = 0; i < Py_SIZE(list); i++) {
309 PyObject *item = PyList_GET_ITEM(list, i);
310 Py_INCREF(item);
311 int cmp = PyObject_RichCompareBool(item, obj, Py_EQ);
312 Py_DECREF(item);
313 if (cmp != 0) {
314 if (cmp > 0) {
315 return i;
316 } else {
317 return -2;
318 }
319 }
320 }
321 return -1;
322 }
323
324 int CPyList_Remove(PyObject *list, PyObject *obj) {
325 int retval;
326 Py_BEGIN_CRITICAL_SECTION(list);
327 Py_ssize_t index = _CPyList_Find(list, obj);
328 if (index == -2) {
329 retval = -1;
330 } else if (index == -1) {
331 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
332 retval = -1;
333 } else {
334 retval = PyList_SetSlice(list, index, index + 1, NULL);
335 }
336 Py_END_CRITICAL_SECTION();
337 return retval;
338 }
339
340 CPyTagged CPyList_Index(PyObject *list, PyObject *obj) {
341 CPyTagged retval;
342 Py_BEGIN_CRITICAL_SECTION(list);
343 Py_ssize_t index = _CPyList_Find(list, obj);
344 if (index == -2) {
345 retval = CPY_INT_TAG;
346 } else if (index == -1) {
347 PyErr_SetString(PyExc_ValueError, "value is not in list");
348 retval = CPY_INT_TAG;
349 } else {
350 retval = index << 1;
351 }
352 Py_END_CRITICAL_SECTION();
353 return retval;
354 }
355
356 PyObject *CPySequence_Sort(PyObject *seq) {
357 PyObject *newlist = PySequence_List(seq);
358 if (newlist == NULL)
359 return NULL;
360 int res = PyList_Sort(newlist);
361 if (res < 0) {
362 Py_DECREF(newlist);
363 return NULL;
364 }
365 return newlist;
366 }
367
368 PyObject *CPySequence_Multiply(PyObject *seq, CPyTagged t_size) {
369 Py_ssize_t size = CPyTagged_AsSsize_t(t_size);
370 if (size == -1 && PyErr_Occurred()) {
371 return NULL;
372 }
373 return PySequence_Repeat(seq, size);
374 }
375
376 PyObject *CPySequence_RMultiply(CPyTagged t_size, PyObject *seq) {
377 return CPySequence_Multiply(seq, t_size);
378 }
379
380 PyObject *CPySequence_InPlaceMultiply(PyObject *seq, CPyTagged t_size) {
381 Py_ssize_t size = CPyTagged_AsSsize_t(t_size);
382 if (size == -1 && PyErr_Occurred()) {
383 return NULL;
384 }
385 return PySequence_InPlaceRepeat(seq, size);
386 }
387
388 PyObject *CPyList_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
389 if (likely(PyList_CheckExact(obj)
390 && CPyTagged_CheckShort(start) && CPyTagged_CheckShort(end))) {
391 Py_ssize_t startn = CPyTagged_ShortAsSsize_t(start);
392 Py_ssize_t endn = CPyTagged_ShortAsSsize_t(end);
393 if (startn < 0) {
394 startn += PyList_GET_SIZE(obj);
395 }
396 if (endn < 0) {
397 endn += PyList_GET_SIZE(obj);
398 }
399 return PyList_GetSlice(obj, startn, endn);
400 }
401 return CPyObject_GetSlice(obj, start, end);
402 }
403
404 int CPySequence_Check(PyObject *obj) {
405 return Py_TYPE(obj)->tp_flags & Py_TPFLAGS_SEQUENCE;
406 }