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 | c83ea13 | 2010-05-09 14:46:46 +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" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 17 | #include "structmember.h" |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 18 | |
| 19 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 20 | ellipsis_repr(PyObject *op) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 21 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 22 | return PyString_FromString("Ellipsis"); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Benjamin Peterson | 01c6e6f | 2009-04-18 22:15:26 +0000 | [diff] [blame] | 25 | PyTypeObject PyEllipsis_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 26 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 27 | "ellipsis", /* tp_name */ |
| 28 | 0, /* tp_basicsize */ |
| 29 | 0, /* tp_itemsize */ |
| 30 | 0, /*never called*/ /* tp_dealloc */ |
| 31 | 0, /* tp_print */ |
| 32 | 0, /* tp_getattr */ |
| 33 | 0, /* tp_setattr */ |
| 34 | 0, /* tp_compare */ |
| 35 | ellipsis_repr, /* tp_repr */ |
| 36 | 0, /* tp_as_number */ |
| 37 | 0, /* tp_as_sequence */ |
| 38 | 0, /* tp_as_mapping */ |
| 39 | 0, /* tp_hash */ |
| 40 | 0, /* tp_call */ |
| 41 | 0, /* tp_str */ |
| 42 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 43 | 0, /* tp_setattro */ |
| 44 | 0, /* tp_as_buffer */ |
| 45 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 48 | PyObject _Py_EllipsisObject = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 49 | _PyObject_EXTRA_INIT |
| 50 | 1, &PyEllipsis_Type |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | |
| 54 | /* Slice object implementation |
| 55 | |
| 56 | start, stop, and step are python objects with None indicating no |
| 57 | index is present. |
| 58 | */ |
| 59 | |
| 60 | PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 61 | PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 62 | { |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 63 | PySliceObject *obj = PyObject_GC_New(PySliceObject, &PySlice_Type); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 65 | if (obj == NULL) |
| 66 | return NULL; |
Guido van Rossum | adf5410 | 2000-12-14 15:09:46 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 68 | if (step == NULL) step = Py_None; |
| 69 | Py_INCREF(step); |
| 70 | if (start == NULL) start = Py_None; |
| 71 | Py_INCREF(start); |
| 72 | if (stop == NULL) stop = Py_None; |
| 73 | Py_INCREF(stop); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 74 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 75 | obj->step = step; |
| 76 | obj->start = start; |
| 77 | obj->stop = stop; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 78 | |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 79 | _PyObject_GC_TRACK(obj); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 80 | return (PyObject *) obj; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 83 | PyObject * |
| 84 | _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop) |
| 85 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 86 | PyObject *start, *end, *slice; |
| 87 | start = PyInt_FromSsize_t(istart); |
| 88 | if (!start) |
| 89 | return NULL; |
| 90 | end = PyInt_FromSsize_t(istop); |
| 91 | if (!end) { |
| 92 | Py_DECREF(start); |
| 93 | return NULL; |
| 94 | } |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 96 | slice = PySlice_New(start, end, NULL); |
| 97 | Py_DECREF(start); |
| 98 | Py_DECREF(end); |
| 99 | return slice; |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 102 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 103 | PySlice_GetIndices(PySliceObject *r, Py_ssize_t length, |
| 104 | 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] | 105 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 106 | /* XXX support long ints */ |
| 107 | if (r->step == Py_None) { |
| 108 | *step = 1; |
| 109 | } else { |
| 110 | if (!PyInt_Check(r->step) && !PyLong_Check(r->step)) return -1; |
| 111 | *step = PyInt_AsSsize_t(r->step); |
| 112 | } |
| 113 | if (r->start == Py_None) { |
| 114 | *start = *step < 0 ? length-1 : 0; |
| 115 | } else { |
| 116 | if (!PyInt_Check(r->start) && !PyLong_Check(r->step)) return -1; |
| 117 | *start = PyInt_AsSsize_t(r->start); |
| 118 | if (*start < 0) *start += length; |
| 119 | } |
| 120 | if (r->stop == Py_None) { |
| 121 | *stop = *step < 0 ? -1 : length; |
| 122 | } else { |
| 123 | if (!PyInt_Check(r->stop) && !PyLong_Check(r->step)) return -1; |
| 124 | *stop = PyInt_AsSsize_t(r->stop); |
| 125 | if (*stop < 0) *stop += length; |
| 126 | } |
| 127 | if (*stop > length) return -1; |
| 128 | if (*start >= length) return -1; |
| 129 | if (*step == 0) return -1; |
| 130 | return 0; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 133 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 134 | PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 135 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 136 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 137 | /* this is harder to get right than you might think */ |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 139 | Py_ssize_t defstart, defstop; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 141 | if (r->step == Py_None) { |
| 142 | *step = 1; |
| 143 | } |
| 144 | else { |
| 145 | if (!_PyEval_SliceIndex(r->step, step)) return -1; |
| 146 | if (*step == 0) { |
| 147 | PyErr_SetString(PyExc_ValueError, |
| 148 | "slice step cannot be zero"); |
| 149 | return -1; |
| 150 | } |
| 151 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 153 | defstart = *step < 0 ? length-1 : 0; |
| 154 | defstop = *step < 0 ? -1 : length; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 155 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 156 | if (r->start == Py_None) { |
| 157 | *start = defstart; |
| 158 | } |
| 159 | else { |
| 160 | if (!_PyEval_SliceIndex(r->start, start)) return -1; |
| 161 | if (*start < 0) *start += length; |
| 162 | if (*start < 0) *start = (*step < 0) ? -1 : 0; |
| 163 | if (*start >= length) |
| 164 | *start = (*step < 0) ? length - 1 : length; |
| 165 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 166 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 167 | if (r->stop == Py_None) { |
| 168 | *stop = defstop; |
| 169 | } |
| 170 | else { |
| 171 | if (!_PyEval_SliceIndex(r->stop, stop)) return -1; |
| 172 | if (*stop < 0) *stop += length; |
| 173 | if (*stop < 0) *stop = (*step < 0) ? -1 : 0; |
| 174 | if (*stop >= length) |
| 175 | *stop = (*step < 0) ? length - 1 : length; |
| 176 | } |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 178 | if ((*step < 0 && *stop >= *start) |
| 179 | || (*step > 0 && *start >= *stop)) { |
| 180 | *slicelength = 0; |
| 181 | } |
| 182 | else if (*step < 0) { |
| 183 | *slicelength = (*stop-*start+1)/(*step)+1; |
| 184 | } |
| 185 | else { |
| 186 | *slicelength = (*stop-*start-1)/(*step)+1; |
| 187 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 189 | return 0; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 192 | static PyObject * |
| 193 | slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 194 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 195 | PyObject *start, *stop, *step; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 197 | start = stop = step = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 199 | if (!_PyArg_NoKeywords("slice()", kw)) |
| 200 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 201 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 202 | if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step)) |
| 203 | return NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 205 | /* This swapping of stop and start is to maintain similarity with |
| 206 | range(). */ |
| 207 | if (stop == NULL) { |
| 208 | stop = start; |
| 209 | start = NULL; |
| 210 | } |
| 211 | return PySlice_New(start, stop, step); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | PyDoc_STRVAR(slice_doc, |
Chris Jerdonek | ad4b000 | 2012-10-07 20:37:54 -0700 | [diff] [blame] | 215 | "slice(stop)\n\ |
| 216 | slice(start, stop[, step])\n\ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 217 | \n\ |
| 218 | Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); |
| 219 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 220 | static void |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 221 | slice_dealloc(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 222 | { |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 223 | _PyObject_GC_UNTRACK(r); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 224 | Py_DECREF(r->step); |
| 225 | Py_DECREF(r->start); |
| 226 | Py_DECREF(r->stop); |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 227 | PyObject_GC_Del(r); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 231 | slice_repr(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 232 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 233 | PyObject *s, *comma; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 235 | s = PyString_FromString("slice("); |
| 236 | comma = PyString_FromString(", "); |
| 237 | PyString_ConcatAndDel(&s, PyObject_Repr(r->start)); |
| 238 | PyString_Concat(&s, comma); |
| 239 | PyString_ConcatAndDel(&s, PyObject_Repr(r->stop)); |
| 240 | PyString_Concat(&s, comma); |
| 241 | PyString_ConcatAndDel(&s, PyObject_Repr(r->step)); |
| 242 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
| 243 | Py_DECREF(comma); |
| 244 | return s; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 247 | static PyMemberDef slice_members[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 248 | {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
| 249 | {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
| 250 | {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
| 251 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 252 | }; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 253 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 254 | static PyObject* |
| 255 | slice_indices(PySliceObject* self, PyObject* len) |
| 256 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 257 | Py_ssize_t ilen, start, stop, step, slicelength; |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 259 | ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError); |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 260 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 261 | if (ilen == -1 && PyErr_Occurred()) { |
| 262 | return NULL; |
| 263 | } |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 265 | if (PySlice_GetIndicesEx(self, ilen, &start, &stop, |
| 266 | &step, &slicelength) < 0) { |
| 267 | return NULL; |
| 268 | } |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 270 | return Py_BuildValue("(nnn)", start, stop, step); |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | PyDoc_STRVAR(slice_indices_doc, |
| 274 | "S.indices(len) -> (start, stop, stride)\n\ |
| 275 | \n\ |
| 276 | Assuming a sequence of length len, calculate the start and stop\n\ |
| 277 | indices, and the stride length of the extended slice described by\n\ |
| 278 | S. Out of bounds indices are clipped in a manner consistent with the\n\ |
| 279 | handling of normal slices."); |
| 280 | |
Raymond Hettinger | 1393669 | 2007-04-11 18:40:58 +0000 | [diff] [blame] | 281 | static PyObject * |
| 282 | slice_reduce(PySliceObject* self) |
| 283 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 284 | return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step); |
Raymond Hettinger | 1393669 | 2007-04-11 18:40:58 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 288 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 289 | static PyMethodDef slice_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 290 | {"indices", (PyCFunction)slice_indices, |
| 291 | METH_O, slice_indices_doc}, |
| 292 | {"__reduce__", (PyCFunction)slice_reduce, |
| 293 | METH_NOARGS, reduce_doc}, |
| 294 | {NULL, NULL} |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 297 | static int |
| 298 | slice_compare(PySliceObject *v, PySliceObject *w) |
| 299 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 300 | int result = 0; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 302 | if (v == w) |
| 303 | return 0; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 305 | if (PyObject_Cmp(v->start, w->start, &result) < 0) |
| 306 | return -2; |
| 307 | if (result != 0) |
| 308 | return result; |
| 309 | if (PyObject_Cmp(v->stop, w->stop, &result) < 0) |
| 310 | return -2; |
| 311 | if (result != 0) |
| 312 | return result; |
| 313 | if (PyObject_Cmp(v->step, w->step, &result) < 0) |
| 314 | return -2; |
| 315 | return result; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 316 | } |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 317 | |
Raymond Hettinger | b859c07 | 2003-09-05 14:27:30 +0000 | [diff] [blame] | 318 | static long |
| 319 | slice_hash(PySliceObject *v) |
| 320 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 321 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 322 | return -1L; |
Raymond Hettinger | b859c07 | 2003-09-05 14:27:30 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 325 | static int |
| 326 | slice_traverse(PySliceObject *v, visitproc visit, void *arg) |
| 327 | { |
| 328 | Py_VISIT(v->start); |
| 329 | Py_VISIT(v->stop); |
| 330 | Py_VISIT(v->step); |
| 331 | return 0; |
| 332 | } |
| 333 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 334 | PyTypeObject PySlice_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 335 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 336 | "slice", /* Name of this type */ |
| 337 | sizeof(PySliceObject), /* Basic object size */ |
| 338 | 0, /* Item size for varobject */ |
| 339 | (destructor)slice_dealloc, /* tp_dealloc */ |
| 340 | 0, /* tp_print */ |
| 341 | 0, /* tp_getattr */ |
| 342 | 0, /* tp_setattr */ |
| 343 | (cmpfunc)slice_compare, /* tp_compare */ |
| 344 | (reprfunc)slice_repr, /* tp_repr */ |
| 345 | 0, /* tp_as_number */ |
| 346 | 0, /* tp_as_sequence */ |
| 347 | 0, /* tp_as_mapping */ |
| 348 | (hashfunc)slice_hash, /* tp_hash */ |
| 349 | 0, /* tp_call */ |
| 350 | 0, /* tp_str */ |
| 351 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 352 | 0, /* tp_setattro */ |
| 353 | 0, /* tp_as_buffer */ |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 354 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 355 | slice_doc, /* tp_doc */ |
Benjamin Peterson | 414f8b9 | 2016-04-16 14:47:12 -0700 | [diff] [blame] | 356 | (traverseproc)slice_traverse, /* tp_traverse */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 357 | 0, /* tp_clear */ |
| 358 | 0, /* tp_richcompare */ |
| 359 | 0, /* tp_weaklistoffset */ |
| 360 | 0, /* tp_iter */ |
| 361 | 0, /* tp_iternext */ |
| 362 | slice_methods, /* tp_methods */ |
| 363 | slice_members, /* tp_members */ |
| 364 | 0, /* tp_getset */ |
| 365 | 0, /* tp_base */ |
| 366 | 0, /* tp_dict */ |
| 367 | 0, /* tp_descr_get */ |
| 368 | 0, /* tp_descr_set */ |
| 369 | 0, /* tp_dictoffset */ |
| 370 | 0, /* tp_init */ |
| 371 | 0, /* tp_alloc */ |
| 372 | slice_new, /* tp_new */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 373 | }; |