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