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 * |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 20 | ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 21 | { |
| 22 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { |
| 23 | PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments"); |
| 24 | return NULL; |
| 25 | } |
| 26 | Py_INCREF(Py_Ellipsis); |
| 27 | return Py_Ellipsis; |
| 28 | } |
| 29 | |
| 30 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 31 | ellipsis_repr(PyObject *op) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 32 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | return PyUnicode_FromString("Ellipsis"); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 36 | PyTypeObject PyEllipsis_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 38 | "ellipsis", /* tp_name */ |
| 39 | 0, /* tp_basicsize */ |
| 40 | 0, /* tp_itemsize */ |
| 41 | 0, /*never called*/ /* tp_dealloc */ |
| 42 | 0, /* tp_print */ |
| 43 | 0, /* tp_getattr */ |
| 44 | 0, /* tp_setattr */ |
| 45 | 0, /* tp_reserved */ |
| 46 | ellipsis_repr, /* tp_repr */ |
| 47 | 0, /* tp_as_number */ |
| 48 | 0, /* tp_as_sequence */ |
| 49 | 0, /* tp_as_mapping */ |
| 50 | 0, /* tp_hash */ |
| 51 | 0, /* tp_call */ |
| 52 | 0, /* tp_str */ |
| 53 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 54 | 0, /* tp_setattro */ |
| 55 | 0, /* tp_as_buffer */ |
| 56 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 57 | 0, /* tp_doc */ |
| 58 | 0, /* tp_traverse */ |
| 59 | 0, /* tp_clear */ |
| 60 | 0, /* tp_richcompare */ |
| 61 | 0, /* tp_weaklistoffset */ |
| 62 | 0, /* tp_iter */ |
| 63 | 0, /* tp_iternext */ |
| 64 | 0, /* tp_methods */ |
| 65 | 0, /* tp_members */ |
| 66 | 0, /* tp_getset */ |
| 67 | 0, /* tp_base */ |
| 68 | 0, /* tp_dict */ |
| 69 | 0, /* tp_descr_get */ |
| 70 | 0, /* tp_descr_set */ |
| 71 | 0, /* tp_dictoffset */ |
| 72 | 0, /* tp_init */ |
| 73 | 0, /* tp_alloc */ |
| 74 | ellipsis_new, /* tp_new */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 77 | PyObject _Py_EllipsisObject = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | _PyObject_EXTRA_INIT |
| 79 | 1, &PyEllipsis_Type |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | |
| 83 | /* Slice object implementation |
| 84 | |
| 85 | start, stop, and step are python objects with None indicating no |
| 86 | index is present. |
| 87 | */ |
| 88 | |
| 89 | PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 90 | PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 91 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | if (obj == NULL) |
| 95 | return NULL; |
Guido van Rossum | adf5410 | 2000-12-14 15:09:46 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (step == NULL) step = Py_None; |
| 98 | Py_INCREF(step); |
| 99 | if (start == NULL) start = Py_None; |
| 100 | Py_INCREF(start); |
| 101 | if (stop == NULL) stop = Py_None; |
| 102 | Py_INCREF(stop); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | obj->step = step; |
| 105 | obj->start = start; |
| 106 | obj->stop = stop; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | return (PyObject *) obj; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | PyObject * |
| 112 | _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop) |
| 113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | PyObject *start, *end, *slice; |
| 115 | start = PyLong_FromSsize_t(istart); |
| 116 | if (!start) |
| 117 | return NULL; |
| 118 | end = PyLong_FromSsize_t(istop); |
| 119 | if (!end) { |
| 120 | Py_DECREF(start); |
| 121 | return NULL; |
| 122 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | slice = PySlice_New(start, end, NULL); |
| 125 | Py_DECREF(start); |
| 126 | Py_DECREF(end); |
| 127 | return slice; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 130 | int |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 131 | PySlice_GetIndices(PyObject *_r, Py_ssize_t length, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 132 | 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] | 133 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 134 | PySliceObject *r = (PySliceObject*)_r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | /* XXX support long ints */ |
| 136 | if (r->step == Py_None) { |
| 137 | *step = 1; |
| 138 | } else { |
| 139 | if (!PyLong_Check(r->step)) return -1; |
| 140 | *step = PyLong_AsSsize_t(r->step); |
| 141 | } |
| 142 | if (r->start == Py_None) { |
| 143 | *start = *step < 0 ? length-1 : 0; |
| 144 | } else { |
| 145 | if (!PyLong_Check(r->start)) return -1; |
| 146 | *start = PyLong_AsSsize_t(r->start); |
| 147 | if (*start < 0) *start += length; |
| 148 | } |
| 149 | if (r->stop == Py_None) { |
| 150 | *stop = *step < 0 ? -1 : length; |
| 151 | } else { |
| 152 | if (!PyLong_Check(r->stop)) return -1; |
| 153 | *stop = PyLong_AsSsize_t(r->stop); |
| 154 | if (*stop < 0) *stop += length; |
| 155 | } |
| 156 | if (*stop > length) return -1; |
| 157 | if (*start >= length) return -1; |
| 158 | if (*step == 0) return -1; |
| 159 | return 0; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 162 | int |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 163 | PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length, |
Mark Dickinson | e6fc740 | 2010-08-06 18:55:26 +0000 | [diff] [blame] | 164 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, |
| 165 | Py_ssize_t *slicelength) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 166 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 167 | PySliceObject *r = (PySliceObject*)_r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | /* this is harder to get right than you might think */ |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | Py_ssize_t defstart, defstop; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (r->step == Py_None) { |
| 173 | *step = 1; |
| 174 | } |
| 175 | else { |
| 176 | if (!_PyEval_SliceIndex(r->step, step)) return -1; |
| 177 | if (*step == 0) { |
| 178 | PyErr_SetString(PyExc_ValueError, |
| 179 | "slice step cannot be zero"); |
| 180 | return -1; |
| 181 | } |
Mark Dickinson | e6fc740 | 2010-08-06 18:55:26 +0000 | [diff] [blame] | 182 | /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it |
| 183 | * with -PY_SSIZE_T_MAX. This doesn't affect the semantics, and it |
| 184 | * guards against later undefined behaviour resulting from code that |
| 185 | * does "step = -step" as part of a slice reversal. |
| 186 | */ |
| 187 | if (*step < -PY_SSIZE_T_MAX) |
| 188 | *step = -PY_SSIZE_T_MAX; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | defstart = *step < 0 ? length-1 : 0; |
| 192 | defstop = *step < 0 ? -1 : length; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | if (r->start == Py_None) { |
| 195 | *start = defstart; |
| 196 | } |
| 197 | else { |
| 198 | if (!_PyEval_SliceIndex(r->start, start)) return -1; |
| 199 | if (*start < 0) *start += length; |
| 200 | if (*start < 0) *start = (*step < 0) ? -1 : 0; |
| 201 | if (*start >= length) |
| 202 | *start = (*step < 0) ? length - 1 : length; |
| 203 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | if (r->stop == Py_None) { |
| 206 | *stop = defstop; |
| 207 | } |
| 208 | else { |
| 209 | if (!_PyEval_SliceIndex(r->stop, stop)) return -1; |
| 210 | if (*stop < 0) *stop += length; |
| 211 | if (*stop < 0) *stop = (*step < 0) ? -1 : 0; |
| 212 | if (*stop >= length) |
| 213 | *stop = (*step < 0) ? length - 1 : length; |
| 214 | } |
Michael W. Hudson | 173f11d | 2002-11-05 15:28:51 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if ((*step < 0 && *stop >= *start) |
| 217 | || (*step > 0 && *start >= *stop)) { |
| 218 | *slicelength = 0; |
| 219 | } |
| 220 | else if (*step < 0) { |
| 221 | *slicelength = (*stop-*start+1)/(*step)+1; |
| 222 | } |
| 223 | else { |
| 224 | *slicelength = (*stop-*start-1)/(*step)+1; |
| 225 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | return 0; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 230 | static PyObject * |
| 231 | slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | PyObject *start, *stop, *step; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | start = stop = step = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | if (!_PyArg_NoKeywords("slice()", kw)) |
| 238 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step)) |
| 241 | return NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | /* This swapping of stop and start is to maintain similarity with |
| 244 | range(). */ |
| 245 | if (stop == NULL) { |
| 246 | stop = start; |
| 247 | start = NULL; |
| 248 | } |
| 249 | return PySlice_New(start, stop, step); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | PyDoc_STRVAR(slice_doc, |
| 253 | "slice([start,] stop[, step])\n\ |
| 254 | \n\ |
| 255 | Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); |
| 256 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 257 | static void |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 258 | slice_dealloc(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 259 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | Py_DECREF(r->step); |
| 261 | Py_DECREF(r->start); |
| 262 | Py_DECREF(r->stop); |
| 263 | PyObject_Del(r); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 267 | slice_repr(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | 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] | 270 | } |
| 271 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 272 | static PyMemberDef slice_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
| 274 | {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
| 275 | {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
| 276 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 277 | }; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 278 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 279 | static PyObject* |
| 280 | slice_indices(PySliceObject* self, PyObject* len) |
| 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | Py_ssize_t ilen, start, stop, step, slicelength; |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError); |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | if (ilen == -1 && PyErr_Occurred()) { |
| 287 | return NULL; |
| 288 | } |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 289 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 290 | if (PySlice_GetIndicesEx((PyObject*)self, ilen, &start, &stop, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | &step, &slicelength) < 0) { |
| 292 | return NULL; |
| 293 | } |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | return Py_BuildValue("(nnn)", start, stop, step); |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | PyDoc_STRVAR(slice_indices_doc, |
| 299 | "S.indices(len) -> (start, stop, stride)\n\ |
| 300 | \n\ |
| 301 | Assuming a sequence of length len, calculate the start and stop\n\ |
| 302 | indices, and the stride length of the extended slice described by\n\ |
| 303 | S. Out of bounds indices are clipped in a manner consistent with the\n\ |
| 304 | handling of normal slices."); |
| 305 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 306 | static PyObject * |
| 307 | slice_reduce(PySliceObject* self) |
| 308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | 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] | 310 | } |
| 311 | |
| 312 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 313 | |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 314 | static PyMethodDef slice_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | {"indices", (PyCFunction)slice_indices, |
| 316 | METH_O, slice_indices_doc}, |
| 317 | {"__reduce__", (PyCFunction)slice_reduce, |
| 318 | METH_NOARGS, reduce_doc}, |
| 319 | {NULL, NULL} |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 322 | static PyObject * |
| 323 | slice_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | PyObject *t1; |
| 326 | PyObject *t2; |
| 327 | PyObject *res; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 328 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 329 | if (!PySlice_Check(v) || !PySlice_Check(w)) |
| 330 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 3e57b52 | 2007-08-28 23:07:26 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | if (v == w) { |
| 333 | /* XXX Do we really need this shortcut? |
| 334 | There's a unit test for it, but is that fair? */ |
| 335 | switch (op) { |
| 336 | case Py_EQ: |
| 337 | case Py_LE: |
| 338 | case Py_GE: |
| 339 | res = Py_True; |
| 340 | break; |
| 341 | default: |
| 342 | res = Py_False; |
| 343 | break; |
| 344 | } |
| 345 | Py_INCREF(res); |
| 346 | return res; |
| 347 | } |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | t1 = PyTuple_New(3); |
Benjamin Peterson | 2963fe0 | 2011-10-17 13:09:27 -0400 | [diff] [blame] | 350 | if (t1 == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | return NULL; |
Benjamin Peterson | 2963fe0 | 2011-10-17 13:09:27 -0400 | [diff] [blame] | 352 | t2 = PyTuple_New(3); |
| 353 | if (t2 == NULL) { |
| 354 | Py_DECREF(t1); |
| 355 | return NULL; |
| 356 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start); |
| 359 | PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop); |
| 360 | PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step); |
| 361 | PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start); |
| 362 | PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop); |
| 363 | PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | res = PyObject_RichCompare(t1, t2, op); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | PyTuple_SET_ITEM(t1, 0, NULL); |
| 368 | PyTuple_SET_ITEM(t1, 1, NULL); |
| 369 | PyTuple_SET_ITEM(t1, 2, NULL); |
| 370 | PyTuple_SET_ITEM(t2, 0, NULL); |
| 371 | PyTuple_SET_ITEM(t2, 1, NULL); |
| 372 | PyTuple_SET_ITEM(t2, 2, NULL); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | Py_DECREF(t1); |
| 375 | Py_DECREF(t2); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | return res; |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 378 | } |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 379 | |
| 380 | PyTypeObject PySlice_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 382 | "slice", /* Name of this type */ |
| 383 | sizeof(PySliceObject), /* Basic object size */ |
| 384 | 0, /* Item size for varobject */ |
| 385 | (destructor)slice_dealloc, /* tp_dealloc */ |
| 386 | 0, /* tp_print */ |
| 387 | 0, /* tp_getattr */ |
| 388 | 0, /* tp_setattr */ |
| 389 | 0, /* tp_reserved */ |
| 390 | (reprfunc)slice_repr, /* tp_repr */ |
| 391 | 0, /* tp_as_number */ |
| 392 | 0, /* tp_as_sequence */ |
| 393 | 0, /* tp_as_mapping */ |
Benjamin Peterson | 23d05c1 | 2010-10-17 20:13:05 +0000 | [diff] [blame] | 394 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | 0, /* tp_call */ |
| 396 | 0, /* tp_str */ |
| 397 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 398 | 0, /* tp_setattro */ |
| 399 | 0, /* tp_as_buffer */ |
| 400 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 401 | slice_doc, /* tp_doc */ |
| 402 | 0, /* tp_traverse */ |
| 403 | 0, /* tp_clear */ |
| 404 | slice_richcompare, /* tp_richcompare */ |
| 405 | 0, /* tp_weaklistoffset */ |
| 406 | 0, /* tp_iter */ |
| 407 | 0, /* tp_iternext */ |
| 408 | slice_methods, /* tp_methods */ |
| 409 | slice_members, /* tp_members */ |
| 410 | 0, /* tp_getset */ |
| 411 | 0, /* tp_base */ |
| 412 | 0, /* tp_dict */ |
| 413 | 0, /* tp_descr_get */ |
| 414 | 0, /* tp_descr_set */ |
| 415 | 0, /* tp_dictoffset */ |
| 416 | 0, /* tp_init */ |
| 417 | 0, /* tp_alloc */ |
| 418 | slice_new, /* tp_new */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 419 | }; |