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