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