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