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