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" |
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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | return PyUnicode_FromString("Ellipsis"); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 25 | PyTypeObject PyEllipsis_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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_reserved */ |
| 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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | return (PyObject *) obj; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 82 | PyObject * |
| 83 | _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop) |
| 84 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | PyObject *start, *end, *slice; |
| 86 | start = PyLong_FromSsize_t(istart); |
| 87 | if (!start) |
| 88 | return NULL; |
| 89 | end = PyLong_FromSsize_t(istop); |
| 90 | if (!end) { |
| 91 | Py_DECREF(start); |
| 92 | return NULL; |
| 93 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | slice = PySlice_New(start, end, NULL); |
| 96 | Py_DECREF(start); |
| 97 | Py_DECREF(end); |
| 98 | return slice; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | /* XXX support long ints */ |
| 106 | if (r->step == Py_None) { |
| 107 | *step = 1; |
| 108 | } else { |
| 109 | if (!PyLong_Check(r->step)) return -1; |
| 110 | *step = PyLong_AsSsize_t(r->step); |
| 111 | } |
| 112 | if (r->start == Py_None) { |
| 113 | *start = *step < 0 ? length-1 : 0; |
| 114 | } else { |
| 115 | if (!PyLong_Check(r->start)) return -1; |
| 116 | *start = PyLong_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 (!PyLong_Check(r->stop)) return -1; |
| 123 | *stop = PyLong_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, |
Mark Dickinson | e6fc740 | 2010-08-06 18:55:26 +0000 | [diff] [blame] | 134 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, |
| 135 | Py_ssize_t *slicelength) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | } |
Mark Dickinson | e6fc740 | 2010-08-06 18:55:26 +0000 | [diff] [blame] | 151 | /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it |
| 152 | * with -PY_SSIZE_T_MAX. This doesn't affect the semantics, and it |
| 153 | * guards against later undefined behaviour resulting from code that |
| 154 | * does "step = -step" as part of a slice reversal. |
| 155 | */ |
| 156 | if (*step < -PY_SSIZE_T_MAX) |
| 157 | *step = -PY_SSIZE_T_MAX; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | defstart = *step < 0 ? length-1 : 0; |
| 161 | defstop = *step < 0 ? -1 : length; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | if (r->start == Py_None) { |
| 164 | *start = defstart; |
| 165 | } |
| 166 | else { |
| 167 | if (!_PyEval_SliceIndex(r->start, start)) return -1; |
| 168 | if (*start < 0) *start += length; |
| 169 | if (*start < 0) *start = (*step < 0) ? -1 : 0; |
| 170 | if (*start >= length) |
| 171 | *start = (*step < 0) ? length - 1 : length; |
| 172 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | if (r->stop == Py_None) { |
| 175 | *stop = defstop; |
| 176 | } |
| 177 | else { |
| 178 | if (!_PyEval_SliceIndex(r->stop, stop)) return -1; |
| 179 | if (*stop < 0) *stop += length; |
| 180 | if (*stop < 0) *stop = (*step < 0) ? -1 : 0; |
| 181 | if (*stop >= length) |
| 182 | *stop = (*step < 0) ? length - 1 : length; |
| 183 | } |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | if ((*step < 0 && *stop >= *start) |
| 186 | || (*step > 0 && *start >= *stop)) { |
| 187 | *slicelength = 0; |
| 188 | } |
| 189 | else if (*step < 0) { |
| 190 | *slicelength = (*stop-*start+1)/(*step)+1; |
| 191 | } |
| 192 | else { |
| 193 | *slicelength = (*stop-*start-1)/(*step)+1; |
| 194 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | return 0; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 199 | static PyObject * |
| 200 | slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | PyObject *start, *stop, *step; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | start = stop = step = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | if (!_PyArg_NoKeywords("slice()", kw)) |
| 207 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step)) |
| 210 | return NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | /* This swapping of stop and start is to maintain similarity with |
| 213 | range(). */ |
| 214 | if (stop == NULL) { |
| 215 | stop = start; |
| 216 | start = NULL; |
| 217 | } |
| 218 | return PySlice_New(start, stop, step); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | PyDoc_STRVAR(slice_doc, |
| 222 | "slice([start,] stop[, step])\n\ |
| 223 | \n\ |
| 224 | Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); |
| 225 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 226 | static void |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 227 | slice_dealloc(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | Py_DECREF(r->step); |
| 230 | Py_DECREF(r->start); |
| 231 | Py_DECREF(r->stop); |
| 232 | PyObject_Del(r); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 236 | slice_repr(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | 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] | 239 | } |
| 240 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 241 | static PyMemberDef slice_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
| 243 | {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
| 244 | {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
| 245 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 246 | }; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 247 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 248 | static PyObject* |
| 249 | slice_indices(PySliceObject* self, PyObject* len) |
| 250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | Py_ssize_t ilen, start, stop, step, slicelength; |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError); |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | if (ilen == -1 && PyErr_Occurred()) { |
| 256 | return NULL; |
| 257 | } |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | if (PySlice_GetIndicesEx(self, ilen, &start, &stop, |
| 260 | &step, &slicelength) < 0) { |
| 261 | return NULL; |
| 262 | } |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | return Py_BuildValue("(nnn)", start, stop, step); |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | PyDoc_STRVAR(slice_indices_doc, |
| 268 | "S.indices(len) -> (start, stop, stride)\n\ |
| 269 | \n\ |
| 270 | Assuming a sequence of length len, calculate the start and stop\n\ |
| 271 | indices, and the stride length of the extended slice described by\n\ |
| 272 | S. Out of bounds indices are clipped in a manner consistent with the\n\ |
| 273 | handling of normal slices."); |
| 274 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 275 | static PyObject * |
| 276 | slice_reduce(PySliceObject* self) |
| 277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | 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] | 279 | } |
| 280 | |
| 281 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 282 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 283 | static PyMethodDef slice_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | {"indices", (PyCFunction)slice_indices, |
| 285 | METH_O, slice_indices_doc}, |
| 286 | {"__reduce__", (PyCFunction)slice_reduce, |
| 287 | METH_NOARGS, reduce_doc}, |
| 288 | {NULL, NULL} |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 291 | static PyObject * |
| 292 | slice_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | PyObject *t1; |
| 295 | PyObject *t2; |
| 296 | PyObject *res; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | if (!PySlice_Check(v) || !PySlice_Check(w)) { |
| 299 | Py_INCREF(Py_NotImplemented); |
| 300 | return Py_NotImplemented; |
| 301 | } |
Thomas Wouters | 3e57b52 | 2007-08-28 23:07:26 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | if (v == w) { |
| 304 | /* XXX Do we really need this shortcut? |
| 305 | There's a unit test for it, but is that fair? */ |
| 306 | switch (op) { |
| 307 | case Py_EQ: |
| 308 | case Py_LE: |
| 309 | case Py_GE: |
| 310 | res = Py_True; |
| 311 | break; |
| 312 | default: |
| 313 | res = Py_False; |
| 314 | break; |
| 315 | } |
| 316 | Py_INCREF(res); |
| 317 | return res; |
| 318 | } |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | t1 = PyTuple_New(3); |
| 321 | t2 = PyTuple_New(3); |
| 322 | if (t1 == NULL || t2 == NULL) |
| 323 | return NULL; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start); |
| 326 | PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop); |
| 327 | PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step); |
| 328 | PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start); |
| 329 | PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop); |
| 330 | PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | res = PyObject_RichCompare(t1, t2, op); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | PyTuple_SET_ITEM(t1, 0, NULL); |
| 335 | PyTuple_SET_ITEM(t1, 1, NULL); |
| 336 | PyTuple_SET_ITEM(t1, 2, NULL); |
| 337 | PyTuple_SET_ITEM(t2, 0, NULL); |
| 338 | PyTuple_SET_ITEM(t2, 1, NULL); |
| 339 | PyTuple_SET_ITEM(t2, 2, NULL); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | Py_DECREF(t1); |
| 342 | Py_DECREF(t2); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | return res; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 345 | } |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 346 | |
Raymond Hettinger | b859c07 | 2003-09-05 14:27:30 +0000 | [diff] [blame] | 347 | static long |
| 348 | slice_hash(PySliceObject *v) |
| 349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 351 | return -1L; |
Raymond Hettinger | b859c07 | 2003-09-05 14:27:30 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 354 | PyTypeObject PySlice_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 356 | "slice", /* Name of this type */ |
| 357 | sizeof(PySliceObject), /* Basic object size */ |
| 358 | 0, /* Item size for varobject */ |
| 359 | (destructor)slice_dealloc, /* tp_dealloc */ |
| 360 | 0, /* tp_print */ |
| 361 | 0, /* tp_getattr */ |
| 362 | 0, /* tp_setattr */ |
| 363 | 0, /* tp_reserved */ |
| 364 | (reprfunc)slice_repr, /* tp_repr */ |
| 365 | 0, /* tp_as_number */ |
| 366 | 0, /* tp_as_sequence */ |
| 367 | 0, /* tp_as_mapping */ |
| 368 | (hashfunc)slice_hash, /* tp_hash */ |
| 369 | 0, /* tp_call */ |
| 370 | 0, /* tp_str */ |
| 371 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 372 | 0, /* tp_setattro */ |
| 373 | 0, /* tp_as_buffer */ |
| 374 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 375 | slice_doc, /* tp_doc */ |
| 376 | 0, /* tp_traverse */ |
| 377 | 0, /* tp_clear */ |
| 378 | slice_richcompare, /* tp_richcompare */ |
| 379 | 0, /* tp_weaklistoffset */ |
| 380 | 0, /* tp_iter */ |
| 381 | 0, /* tp_iternext */ |
| 382 | slice_methods, /* tp_methods */ |
| 383 | slice_members, /* tp_members */ |
| 384 | 0, /* tp_getset */ |
| 385 | 0, /* tp_base */ |
| 386 | 0, /* tp_dict */ |
| 387 | 0, /* tp_descr_get */ |
| 388 | 0, /* tp_descr_set */ |
| 389 | 0, /* tp_dictoffset */ |
| 390 | 0, /* tp_init */ |
| 391 | 0, /* tp_alloc */ |
| 392 | slice_new, /* tp_new */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 393 | }; |