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