Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | Written by Jim Hugunin and Chris Chase. |
| 3 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 4 | This includes both the singular ellipsis object and slice objects. |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 5 | |
| 6 | Guido, feel free to do whatever you want in the way of copyrights |
| 7 | for this file. |
| 8 | */ |
| 9 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | /* |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 11 | Py_Ellipsis encodes the '...' rubber index token. It is similar to |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 12 | the Py_NoneStruct in that there is no way to create other objects of |
| 13 | this type and there is exactly one in existence. |
| 14 | */ |
| 15 | |
| 16 | #include "Python.h" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 17 | #include "pycore_abstract.h" // _PyIndex_Check() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 18 | #include "pycore_long.h" // _PyLong_GetZero() |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 19 | #include "pycore_object.h" // _PyObject_GC_TRACK() |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 20 | #include "structmember.h" // PyMemberDef |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 21 | |
| 22 | static PyObject * |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 23 | ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 24 | { |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 25 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 26 | PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments"); |
| 27 | return NULL; |
| 28 | } |
| 29 | Py_INCREF(Py_Ellipsis); |
| 30 | return Py_Ellipsis; |
| 31 | } |
| 32 | |
| 33 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 34 | ellipsis_repr(PyObject *op) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 35 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | return PyUnicode_FromString("Ellipsis"); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Alexandre Vassalotti | c49477b | 2013-11-24 02:53:45 -0800 | [diff] [blame] | 39 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 40 | ellipsis_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
Alexandre Vassalotti | c49477b | 2013-11-24 02:53:45 -0800 | [diff] [blame] | 41 | { |
| 42 | return PyUnicode_FromString("Ellipsis"); |
| 43 | } |
| 44 | |
| 45 | static PyMethodDef ellipsis_methods[] = { |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 46 | {"__reduce__", ellipsis_reduce, METH_NOARGS, NULL}, |
Alexandre Vassalotti | c49477b | 2013-11-24 02:53:45 -0800 | [diff] [blame] | 47 | {NULL, NULL} |
| 48 | }; |
| 49 | |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 50 | PyTypeObject PyEllipsis_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 52 | "ellipsis", /* tp_name */ |
| 53 | 0, /* tp_basicsize */ |
| 54 | 0, /* tp_itemsize */ |
| 55 | 0, /*never called*/ /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 56 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | 0, /* tp_getattr */ |
| 58 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 59 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | ellipsis_repr, /* tp_repr */ |
| 61 | 0, /* tp_as_number */ |
| 62 | 0, /* tp_as_sequence */ |
| 63 | 0, /* tp_as_mapping */ |
| 64 | 0, /* tp_hash */ |
| 65 | 0, /* tp_call */ |
| 66 | 0, /* tp_str */ |
| 67 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 68 | 0, /* tp_setattro */ |
| 69 | 0, /* tp_as_buffer */ |
| 70 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 71 | 0, /* tp_doc */ |
| 72 | 0, /* tp_traverse */ |
| 73 | 0, /* tp_clear */ |
| 74 | 0, /* tp_richcompare */ |
| 75 | 0, /* tp_weaklistoffset */ |
| 76 | 0, /* tp_iter */ |
| 77 | 0, /* tp_iternext */ |
Alexandre Vassalotti | c49477b | 2013-11-24 02:53:45 -0800 | [diff] [blame] | 78 | ellipsis_methods, /* tp_methods */ |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 79 | 0, /* tp_members */ |
| 80 | 0, /* tp_getset */ |
| 81 | 0, /* tp_base */ |
| 82 | 0, /* tp_dict */ |
| 83 | 0, /* tp_descr_get */ |
| 84 | 0, /* tp_descr_set */ |
| 85 | 0, /* tp_dictoffset */ |
| 86 | 0, /* tp_init */ |
| 87 | 0, /* tp_alloc */ |
| 88 | ellipsis_new, /* tp_new */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 91 | PyObject _Py_EllipsisObject = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | _PyObject_EXTRA_INIT |
| 93 | 1, &PyEllipsis_Type |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 97 | /* Slice object implementation */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 98 | |
Victor Stinner | bed4817 | 2019-08-27 00:12:32 +0200 | [diff] [blame] | 99 | |
Victor Stinner | bcb094b | 2021-02-19 15:10:45 +0100 | [diff] [blame] | 100 | void _PySlice_Fini(PyInterpreterState *interp) |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 101 | { |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 102 | PySliceObject *obj = interp->slice_cache; |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 103 | if (obj != NULL) { |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 104 | interp->slice_cache = NULL; |
Benjamin Peterson | b0c04cb | 2016-04-16 15:12:29 -0700 | [diff] [blame] | 105 | PyObject_GC_Del(obj); |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | /* start, stop, and step are python objects with None indicating no |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 110 | index is present. |
| 111 | */ |
| 112 | |
| 113 | PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 114 | PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 115 | { |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 116 | if (step == NULL) { |
| 117 | step = Py_None; |
| 118 | } |
| 119 | if (start == NULL) { |
| 120 | start = Py_None; |
| 121 | } |
| 122 | if (stop == NULL) { |
| 123 | stop = Py_None; |
| 124 | } |
| 125 | |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 126 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 127 | PySliceObject *obj; |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 128 | if (interp->slice_cache != NULL) { |
| 129 | obj = interp->slice_cache; |
| 130 | interp->slice_cache = NULL; |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 131 | _Py_NewReference((PyObject *)obj); |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 132 | } |
| 133 | else { |
Benjamin Peterson | 2b601d3 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 134 | obj = PyObject_GC_New(PySliceObject, &PySlice_Type); |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 135 | if (obj == NULL) { |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 136 | return NULL; |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 137 | } |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 138 | } |
Guido van Rossum | adf5410 | 2000-12-14 15:09:46 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | Py_INCREF(step); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | obj->step = step; |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 142 | Py_INCREF(start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | obj->start = start; |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 144 | Py_INCREF(stop); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | obj->stop = stop; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | 2b601d3 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 147 | _PyObject_GC_TRACK(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | return (PyObject *) obj; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 151 | PyObject * |
| 152 | _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop) |
| 153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | PyObject *start, *end, *slice; |
| 155 | start = PyLong_FromSsize_t(istart); |
| 156 | if (!start) |
| 157 | return NULL; |
| 158 | end = PyLong_FromSsize_t(istop); |
| 159 | if (!end) { |
| 160 | Py_DECREF(start); |
| 161 | return NULL; |
| 162 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | slice = PySlice_New(start, end, NULL); |
| 165 | Py_DECREF(start); |
| 166 | Py_DECREF(end); |
| 167 | return slice; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 170 | int |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 171 | PySlice_GetIndices(PyObject *_r, Py_ssize_t length, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 172 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 173 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 174 | PySliceObject *r = (PySliceObject*)_r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | /* XXX support long ints */ |
| 176 | if (r->step == Py_None) { |
| 177 | *step = 1; |
| 178 | } else { |
| 179 | if (!PyLong_Check(r->step)) return -1; |
| 180 | *step = PyLong_AsSsize_t(r->step); |
| 181 | } |
| 182 | if (r->start == Py_None) { |
| 183 | *start = *step < 0 ? length-1 : 0; |
| 184 | } else { |
| 185 | if (!PyLong_Check(r->start)) return -1; |
| 186 | *start = PyLong_AsSsize_t(r->start); |
| 187 | if (*start < 0) *start += length; |
| 188 | } |
| 189 | if (r->stop == Py_None) { |
| 190 | *stop = *step < 0 ? -1 : length; |
| 191 | } else { |
| 192 | if (!PyLong_Check(r->stop)) return -1; |
| 193 | *stop = PyLong_AsSsize_t(r->stop); |
| 194 | if (*stop < 0) *stop += length; |
| 195 | } |
| 196 | if (*stop > length) return -1; |
| 197 | if (*start >= length) return -1; |
| 198 | if (*step == 0) return -1; |
| 199 | return 0; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 202 | int |
Serhiy Storchaka | b2a5be0 | 2017-01-25 13:23:05 +0200 | [diff] [blame] | 203 | PySlice_Unpack(PyObject *_r, |
| 204 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 205 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 206 | PySliceObject *r = (PySliceObject*)_r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | /* this is harder to get right than you might think */ |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 208 | |
Xiang Zhang | 2ddf5a1 | 2017-05-10 18:19:41 +0800 | [diff] [blame] | 209 | Py_BUILD_ASSERT(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX); |
| 210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | if (r->step == Py_None) { |
| 212 | *step = 1; |
| 213 | } |
| 214 | else { |
| 215 | if (!_PyEval_SliceIndex(r->step, step)) return -1; |
| 216 | if (*step == 0) { |
| 217 | PyErr_SetString(PyExc_ValueError, |
| 218 | "slice step cannot be zero"); |
| 219 | return -1; |
| 220 | } |
Mark Dickinson | e6fc740 | 2010-08-06 18:55:26 +0000 | [diff] [blame] | 221 | /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it |
| 222 | * with -PY_SSIZE_T_MAX. This doesn't affect the semantics, and it |
| 223 | * guards against later undefined behaviour resulting from code that |
| 224 | * does "step = -step" as part of a slice reversal. |
| 225 | */ |
| 226 | if (*step < -PY_SSIZE_T_MAX) |
| 227 | *step = -PY_SSIZE_T_MAX; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | if (r->start == Py_None) { |
Xiang Zhang | 2ddf5a1 | 2017-05-10 18:19:41 +0800 | [diff] [blame] | 231 | *start = *step < 0 ? PY_SSIZE_T_MAX : 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | } |
| 233 | else { |
| 234 | if (!_PyEval_SliceIndex(r->start, start)) return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | if (r->stop == Py_None) { |
Xiang Zhang | 2ddf5a1 | 2017-05-10 18:19:41 +0800 | [diff] [blame] | 238 | *stop = *step < 0 ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | } |
| 240 | else { |
| 241 | if (!_PyEval_SliceIndex(r->stop, stop)) return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | } |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 243 | |
Serhiy Storchaka | b2a5be0 | 2017-01-25 13:23:05 +0200 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | Py_ssize_t |
| 248 | PySlice_AdjustIndices(Py_ssize_t length, |
| 249 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step) |
| 250 | { |
| 251 | /* this is harder to get right than you might think */ |
| 252 | |
| 253 | assert(step != 0); |
| 254 | assert(step >= -PY_SSIZE_T_MAX); |
| 255 | |
| 256 | if (*start < 0) { |
| 257 | *start += length; |
| 258 | if (*start < 0) { |
| 259 | *start = (step < 0) ? -1 : 0; |
| 260 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | } |
Serhiy Storchaka | b2a5be0 | 2017-01-25 13:23:05 +0200 | [diff] [blame] | 262 | else if (*start >= length) { |
| 263 | *start = (step < 0) ? length - 1 : length; |
| 264 | } |
| 265 | |
| 266 | if (*stop < 0) { |
| 267 | *stop += length; |
| 268 | if (*stop < 0) { |
| 269 | *stop = (step < 0) ? -1 : 0; |
| 270 | } |
| 271 | } |
Xiang Zhang | 2ddf5a1 | 2017-05-10 18:19:41 +0800 | [diff] [blame] | 272 | else if (*stop >= length) { |
Serhiy Storchaka | b2a5be0 | 2017-01-25 13:23:05 +0200 | [diff] [blame] | 273 | *stop = (step < 0) ? length - 1 : length; |
| 274 | } |
| 275 | |
| 276 | if (step < 0) { |
| 277 | if (*stop < *start) { |
| 278 | return (*start - *stop - 1) / (-step) + 1; |
| 279 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | } |
| 281 | else { |
Serhiy Storchaka | b2a5be0 | 2017-01-25 13:23:05 +0200 | [diff] [blame] | 282 | if (*start < *stop) { |
| 283 | return (*stop - *start - 1) / step + 1; |
| 284 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | } |
Serhiy Storchaka | b2a5be0 | 2017-01-25 13:23:05 +0200 | [diff] [blame] | 286 | return 0; |
| 287 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 288 | |
Serhiy Storchaka | b2a5be0 | 2017-01-25 13:23:05 +0200 | [diff] [blame] | 289 | #undef PySlice_GetIndicesEx |
| 290 | |
| 291 | int |
| 292 | PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length, |
| 293 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, |
| 294 | Py_ssize_t *slicelength) |
| 295 | { |
| 296 | if (PySlice_Unpack(_r, start, stop, step) < 0) |
| 297 | return -1; |
| 298 | *slicelength = PySlice_AdjustIndices(length, start, stop, *step); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | return 0; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 302 | static PyObject * |
| 303 | slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | PyObject *start, *stop, *step; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | start = stop = step = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 308 | |
Serhiy Storchaka | 6cca5c8 | 2017-06-08 14:41:19 +0300 | [diff] [blame] | 309 | if (!_PyArg_NoKeywords("slice", kw)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step)) |
| 313 | return NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | /* This swapping of stop and start is to maintain similarity with |
| 316 | range(). */ |
| 317 | if (stop == NULL) { |
| 318 | stop = start; |
| 319 | start = NULL; |
| 320 | } |
| 321 | return PySlice_New(start, stop, step); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | PyDoc_STRVAR(slice_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 325 | "slice(stop)\n\ |
| 326 | slice(start, stop[, step])\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 327 | \n\ |
| 328 | Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); |
| 329 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 330 | static void |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 331 | slice_dealloc(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 332 | { |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 333 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
Benjamin Peterson | 2b601d3 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 334 | _PyObject_GC_UNTRACK(r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | Py_DECREF(r->step); |
| 336 | Py_DECREF(r->start); |
| 337 | Py_DECREF(r->stop); |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 338 | if (interp->slice_cache == NULL) { |
| 339 | interp->slice_cache = r; |
| 340 | } |
| 341 | else { |
Benjamin Peterson | 2b601d3 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 342 | PyObject_GC_Del(r); |
Victor Stinner | 7daba6f | 2020-06-05 01:14:40 +0200 | [diff] [blame] | 343 | } |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 347 | slice_repr(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 352 | static PyMemberDef slice_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
| 354 | {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
| 355 | {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
| 356 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 357 | }; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 358 | |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 359 | /* Helper function to convert a slice argument to a PyLong, and raise TypeError |
| 360 | with a suitable message on failure. */ |
| 361 | |
| 362 | static PyObject* |
| 363 | evaluate_slice_index(PyObject *v) |
| 364 | { |
Victor Stinner | a15e260 | 2020-04-08 02:01:56 +0200 | [diff] [blame] | 365 | if (_PyIndex_Check(v)) { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 366 | return PyNumber_Index(v); |
| 367 | } |
| 368 | else { |
| 369 | PyErr_SetString(PyExc_TypeError, |
| 370 | "slice indices must be integers or " |
| 371 | "None or have an __index__ method"); |
| 372 | return NULL; |
| 373 | } |
| 374 | } |
| 375 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 376 | /* Compute slice indices given a slice and length. Return -1 on failure. Used |
| 377 | by slice.indices and rangeobject slicing. Assumes that `len` is a |
| 378 | nonnegative instance of PyLong. */ |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 379 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 380 | int |
| 381 | _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, |
| 382 | PyObject **start_ptr, PyObject **stop_ptr, |
| 383 | PyObject **step_ptr) |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 384 | { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 385 | PyObject *start=NULL, *stop=NULL, *step=NULL; |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 386 | PyObject *upper=NULL, *lower=NULL; |
| 387 | int step_is_negative, cmp_result; |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 388 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 389 | /* Convert step to an integer; raise for zero step. */ |
| 390 | if (self->step == Py_None) { |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 391 | step = _PyLong_GetOne(); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 392 | Py_INCREF(step); |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 393 | step_is_negative = 0; |
| 394 | } |
| 395 | else { |
| 396 | int step_sign; |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 397 | step = evaluate_slice_index(self->step); |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 398 | if (step == NULL) |
| 399 | goto error; |
| 400 | step_sign = _PyLong_Sign(step); |
| 401 | if (step_sign == 0) { |
| 402 | PyErr_SetString(PyExc_ValueError, |
| 403 | "slice step cannot be zero"); |
| 404 | goto error; |
| 405 | } |
| 406 | step_is_negative = step_sign < 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | } |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 408 | |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 409 | /* Find lower and upper bounds for start and stop. */ |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 410 | if (step_is_negative) { |
| 411 | lower = PyLong_FromLong(-1L); |
| 412 | if (lower == NULL) |
| 413 | goto error; |
| 414 | |
| 415 | upper = PyNumber_Add(length, lower); |
| 416 | if (upper == NULL) |
| 417 | goto error; |
| 418 | } |
| 419 | else { |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 420 | lower = _PyLong_GetZero(); |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 421 | Py_INCREF(lower); |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 422 | upper = length; |
| 423 | Py_INCREF(upper); |
| 424 | } |
| 425 | |
| 426 | /* Compute start. */ |
| 427 | if (self->start == Py_None) { |
| 428 | start = step_is_negative ? upper : lower; |
| 429 | Py_INCREF(start); |
| 430 | } |
| 431 | else { |
| 432 | start = evaluate_slice_index(self->start); |
| 433 | if (start == NULL) |
| 434 | goto error; |
| 435 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 436 | if (_PyLong_Sign(start) < 0) { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 437 | /* start += length */ |
| 438 | PyObject *tmp = PyNumber_Add(start, length); |
| 439 | Py_DECREF(start); |
| 440 | start = tmp; |
| 441 | if (start == NULL) |
| 442 | goto error; |
| 443 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 444 | cmp_result = PyObject_RichCompareBool(start, lower, Py_LT); |
| 445 | if (cmp_result < 0) |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 446 | goto error; |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 447 | if (cmp_result) { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 448 | Py_INCREF(lower); |
| 449 | Py_DECREF(start); |
| 450 | start = lower; |
| 451 | } |
| 452 | } |
| 453 | else { |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 454 | cmp_result = PyObject_RichCompareBool(start, upper, Py_GT); |
| 455 | if (cmp_result < 0) |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 456 | goto error; |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 457 | if (cmp_result) { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 458 | Py_INCREF(upper); |
| 459 | Py_DECREF(start); |
| 460 | start = upper; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /* Compute stop. */ |
| 466 | if (self->stop == Py_None) { |
| 467 | stop = step_is_negative ? lower : upper; |
| 468 | Py_INCREF(stop); |
| 469 | } |
| 470 | else { |
| 471 | stop = evaluate_slice_index(self->stop); |
| 472 | if (stop == NULL) |
| 473 | goto error; |
| 474 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 475 | if (_PyLong_Sign(stop) < 0) { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 476 | /* stop += length */ |
| 477 | PyObject *tmp = PyNumber_Add(stop, length); |
| 478 | Py_DECREF(stop); |
| 479 | stop = tmp; |
| 480 | if (stop == NULL) |
| 481 | goto error; |
| 482 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 483 | cmp_result = PyObject_RichCompareBool(stop, lower, Py_LT); |
| 484 | if (cmp_result < 0) |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 485 | goto error; |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 486 | if (cmp_result) { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 487 | Py_INCREF(lower); |
| 488 | Py_DECREF(stop); |
| 489 | stop = lower; |
| 490 | } |
| 491 | } |
| 492 | else { |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 493 | cmp_result = PyObject_RichCompareBool(stop, upper, Py_GT); |
| 494 | if (cmp_result < 0) |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 495 | goto error; |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 496 | if (cmp_result) { |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 497 | Py_INCREF(upper); |
| 498 | Py_DECREF(stop); |
| 499 | stop = upper; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 504 | *start_ptr = start; |
| 505 | *stop_ptr = stop; |
| 506 | *step_ptr = step; |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 507 | Py_DECREF(upper); |
| 508 | Py_DECREF(lower); |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 509 | return 0; |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 510 | |
| 511 | error: |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 512 | *start_ptr = *stop_ptr = *step_ptr = NULL; |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 513 | Py_XDECREF(start); |
| 514 | Py_XDECREF(stop); |
| 515 | Py_XDECREF(step); |
| 516 | Py_XDECREF(upper); |
| 517 | Py_XDECREF(lower); |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 518 | return -1; |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Mark Dickinson | ffdb2c2 | 2012-11-17 19:18:10 +0000 | [diff] [blame] | 521 | /* Implementation of slice.indices. */ |
| 522 | |
| 523 | static PyObject* |
| 524 | slice_indices(PySliceObject* self, PyObject* len) |
| 525 | { |
| 526 | PyObject *start, *stop, *step; |
| 527 | PyObject *length; |
| 528 | int error; |
| 529 | |
| 530 | /* Convert length to an integer if necessary; raise for negative length. */ |
| 531 | length = PyNumber_Index(len); |
| 532 | if (length == NULL) |
| 533 | return NULL; |
| 534 | |
| 535 | if (_PyLong_Sign(length) < 0) { |
| 536 | PyErr_SetString(PyExc_ValueError, |
| 537 | "length should not be negative"); |
| 538 | Py_DECREF(length); |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | error = _PySlice_GetLongIndices(self, length, &start, &stop, &step); |
| 543 | Py_DECREF(length); |
| 544 | if (error == -1) |
| 545 | return NULL; |
| 546 | else |
| 547 | return Py_BuildValue("(NNN)", start, stop, step); |
| 548 | } |
Mark Dickinson | c8a6967 | 2012-11-10 14:52:10 +0000 | [diff] [blame] | 549 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 550 | PyDoc_STRVAR(slice_indices_doc, |
| 551 | "S.indices(len) -> (start, stop, stride)\n\ |
| 552 | \n\ |
| 553 | Assuming a sequence of length len, calculate the start and stop\n\ |
| 554 | indices, and the stride length of the extended slice described by\n\ |
| 555 | S. Out of bounds indices are clipped in a manner consistent with the\n\ |
| 556 | handling of normal slices."); |
| 557 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 558 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 559 | slice_reduce(PySliceObject* self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 565 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 566 | static PyMethodDef slice_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | {"indices", (PyCFunction)slice_indices, |
| 568 | METH_O, slice_indices_doc}, |
| 569 | {"__reduce__", (PyCFunction)slice_reduce, |
| 570 | METH_NOARGS, reduce_doc}, |
| 571 | {NULL, NULL} |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 572 | }; |
| 573 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 574 | static PyObject * |
| 575 | slice_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 576 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 577 | if (!PySlice_Check(v) || !PySlice_Check(w)) |
| 578 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 3e57b52 | 2007-08-28 23:07:26 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | if (v == w) { |
Victor Stinner | dcb68f4 | 2019-02-13 12:31:56 +0100 | [diff] [blame] | 581 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | /* XXX Do we really need this shortcut? |
| 583 | There's a unit test for it, but is that fair? */ |
| 584 | switch (op) { |
| 585 | case Py_EQ: |
| 586 | case Py_LE: |
| 587 | case Py_GE: |
| 588 | res = Py_True; |
| 589 | break; |
| 590 | default: |
| 591 | res = Py_False; |
| 592 | break; |
| 593 | } |
| 594 | Py_INCREF(res); |
| 595 | return res; |
| 596 | } |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 597 | |
Victor Stinner | dcb68f4 | 2019-02-13 12:31:56 +0100 | [diff] [blame] | 598 | |
| 599 | PyObject *t1 = PyTuple_Pack(3, |
| 600 | ((PySliceObject *)v)->start, |
| 601 | ((PySliceObject *)v)->stop, |
| 602 | ((PySliceObject *)v)->step); |
| 603 | if (t1 == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | return NULL; |
Victor Stinner | dcb68f4 | 2019-02-13 12:31:56 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | PyObject *t2 = PyTuple_Pack(3, |
| 608 | ((PySliceObject *)w)->start, |
| 609 | ((PySliceObject *)w)->stop, |
| 610 | ((PySliceObject *)w)->step); |
Benjamin Peterson | 2963fe0 | 2011-10-17 13:09:27 -0400 | [diff] [blame] | 611 | if (t2 == NULL) { |
| 612 | Py_DECREF(t1); |
| 613 | return NULL; |
| 614 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 615 | |
Victor Stinner | dcb68f4 | 2019-02-13 12:31:56 +0100 | [diff] [blame] | 616 | PyObject *res = PyObject_RichCompare(t1, t2, op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | Py_DECREF(t1); |
| 618 | Py_DECREF(t2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | return res; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 620 | } |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 621 | |
Benjamin Peterson | 2b601d3 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 622 | static int |
| 623 | slice_traverse(PySliceObject *v, visitproc visit, void *arg) |
| 624 | { |
| 625 | Py_VISIT(v->start); |
| 626 | Py_VISIT(v->stop); |
| 627 | Py_VISIT(v->step); |
| 628 | return 0; |
| 629 | } |
| 630 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 631 | PyTypeObject PySlice_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 633 | "slice", /* Name of this type */ |
| 634 | sizeof(PySliceObject), /* Basic object size */ |
| 635 | 0, /* Item size for varobject */ |
| 636 | (destructor)slice_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 637 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | 0, /* tp_getattr */ |
| 639 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 640 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | (reprfunc)slice_repr, /* tp_repr */ |
| 642 | 0, /* tp_as_number */ |
| 643 | 0, /* tp_as_sequence */ |
| 644 | 0, /* tp_as_mapping */ |
Benjamin Peterson | 23d05c1 | 2010-10-17 20:13:05 +0000 | [diff] [blame] | 645 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | 0, /* tp_call */ |
| 647 | 0, /* tp_str */ |
| 648 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 649 | 0, /* tp_setattro */ |
| 650 | 0, /* tp_as_buffer */ |
Benjamin Peterson | 2b601d3 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 651 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | slice_doc, /* tp_doc */ |
Benjamin Peterson | 2b601d3 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 653 | (traverseproc)slice_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | 0, /* tp_clear */ |
| 655 | slice_richcompare, /* tp_richcompare */ |
| 656 | 0, /* tp_weaklistoffset */ |
| 657 | 0, /* tp_iter */ |
| 658 | 0, /* tp_iternext */ |
| 659 | slice_methods, /* tp_methods */ |
| 660 | slice_members, /* tp_members */ |
| 661 | 0, /* tp_getset */ |
| 662 | 0, /* tp_base */ |
| 663 | 0, /* tp_dict */ |
| 664 | 0, /* tp_descr_get */ |
| 665 | 0, /* tp_descr_set */ |
| 666 | 0, /* tp_dictoffset */ |
| 667 | 0, /* tp_init */ |
| 668 | 0, /* tp_alloc */ |
| 669 | slice_new, /* tp_new */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 670 | }; |