| 1 |
// Tuple primitive operations |
| 2 |
// |
| 3 |
// These are registered in mypyc.primitives.tuple_ops. |
| 4 |
|
| 5 |
#include <Python.h> |
| 6 |
#include "CPy.h" |
| 7 |
|
| 8 |
PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index) { |
| 9 |
if (CPyTagged_CheckShort(index)) { |
| 10 |
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index); |
| 11 |
Py_ssize_t size = PyTuple_GET_SIZE(tuple); |
| 12 |
if (n >= 0) { |
| 13 |
if (n >= size) { |
| 14 |
PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 15 |
return NULL; |
| 16 |
} |
| 17 |
} else { |
| 18 |
n += size; |
| 19 |
if (n < 0) { |
| 20 |
PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
} |
| 24 |
PyObject *result = PyTuple_GET_ITEM(tuple, n); |
| 25 |
Py_INCREF(result); |
| 26 |
return result; |
| 27 |
} else { |
| 28 |
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG); |
| 29 |
return NULL; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) { |
| 34 |
if (likely(PyTuple_CheckExact(obj) |
| 35 |
&& CPyTagged_CheckShort(start) && CPyTagged_CheckShort(end))) { |
| 36 |
Py_ssize_t startn = CPyTagged_ShortAsSsize_t(start); |
| 37 |
Py_ssize_t endn = CPyTagged_ShortAsSsize_t(end); |
| 38 |
if (startn < 0) { |
| 39 |
startn += PyTuple_GET_SIZE(obj); |
| 40 |
} |
| 41 |
if (endn < 0) { |
| 42 |
endn += PyTuple_GET_SIZE(obj); |
| 43 |
} |
| 44 |
return PyTuple_GetSlice(obj, startn, endn); |
| 45 |
} |
| 46 |
return CPyObject_GetSlice(obj, start, end); |
| 47 |
} |
| 48 |
|
| 49 |
// No error checking |
| 50 |
PyObject *CPySequenceTuple_GetItemUnsafe(PyObject *tuple, Py_ssize_t index) |
| 51 |
{ |
| 52 |
PyObject *result = PyTuple_GET_ITEM(tuple, index); |
| 53 |
Py_INCREF(result); |
| 54 |
return result; |
| 55 |
} |
| 56 |
|
| 57 |
// PyTuple_SET_ITEM does no error checking, |
| 58 |
// and should only be used to fill in brand new tuples. |
| 59 |
void CPySequenceTuple_SetItemUnsafe(PyObject *tuple, Py_ssize_t index, PyObject *value) |
| 60 |
{ |
| 61 |
PyTuple_SET_ITEM(tuple, index, value); |
| 62 |
} |