blob: d41ac105f62d7fcc6d57756059de4e8f859f012e [file] [log] [blame]
Guido van Rossumf2d125b1996-07-30 16:45:48 +00001/*
2Written by Jim Hugunin and Chris Chase.
3
Guido van Rossume449af71996-10-11 16:25:41 +00004This includes both the singular ellipsis object and slice objects.
Guido van Rossumf2d125b1996-07-30 16:45:48 +00005
6Guido, feel free to do whatever you want in the way of copyrights
7for this file.
8*/
9
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010/*
Guido van Rossume449af71996-10-11 16:25:41 +000011Py_Ellipsis encodes the '...' rubber index token. It is similar to
Guido van Rossumf2d125b1996-07-30 16:45:48 +000012the Py_NoneStruct in that there is no way to create other objects of
13this type and there is exactly one in existence.
14*/
15
16#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000017#include "structmember.h"
Guido van Rossumf2d125b1996-07-30 16:45:48 +000018
19static PyObject *
Benjamin Petersonc4607ae2011-07-29 18:19:43 -050020ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
21{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +020022 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -050023 PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments");
24 return NULL;
25 }
26 Py_INCREF(Py_Ellipsis);
27 return Py_Ellipsis;
28}
29
30static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000031ellipsis_repr(PyObject *op)
Guido van Rossumf2d125b1996-07-30 16:45:48 +000032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 return PyUnicode_FromString("Ellipsis");
Guido van Rossumf2d125b1996-07-30 16:45:48 +000034}
35
Alexandre Vassalottic49477b2013-11-24 02:53:45 -080036static PyObject *
37ellipsis_reduce(PyObject *op)
38{
39 return PyUnicode_FromString("Ellipsis");
40}
41
42static PyMethodDef ellipsis_methods[] = {
43 {"__reduce__", (PyCFunction)ellipsis_reduce, METH_NOARGS, NULL},
44 {NULL, NULL}
45};
46
Benjamin Petersonfd838e62009-04-20 02:09:13 +000047PyTypeObject PyEllipsis_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 PyVarObject_HEAD_INIT(&PyType_Type, 0)
49 "ellipsis", /* tp_name */
50 0, /* tp_basicsize */
51 0, /* tp_itemsize */
52 0, /*never called*/ /* tp_dealloc */
53 0, /* tp_print */
54 0, /* tp_getattr */
55 0, /* tp_setattr */
56 0, /* tp_reserved */
57 ellipsis_repr, /* tp_repr */
58 0, /* tp_as_number */
59 0, /* tp_as_sequence */
60 0, /* tp_as_mapping */
61 0, /* tp_hash */
62 0, /* tp_call */
63 0, /* tp_str */
64 PyObject_GenericGetAttr, /* tp_getattro */
65 0, /* tp_setattro */
66 0, /* tp_as_buffer */
67 Py_TPFLAGS_DEFAULT, /* tp_flags */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -050068 0, /* tp_doc */
69 0, /* tp_traverse */
70 0, /* tp_clear */
71 0, /* tp_richcompare */
72 0, /* tp_weaklistoffset */
73 0, /* tp_iter */
74 0, /* tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -080075 ellipsis_methods, /* tp_methods */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -050076 0, /* tp_members */
77 0, /* tp_getset */
78 0, /* tp_base */
79 0, /* tp_dict */
80 0, /* tp_descr_get */
81 0, /* tp_descr_set */
82 0, /* tp_dictoffset */
83 0, /* tp_init */
84 0, /* tp_alloc */
85 ellipsis_new, /* tp_new */
Guido van Rossumf2d125b1996-07-30 16:45:48 +000086};
87
Guido van Rossume449af71996-10-11 16:25:41 +000088PyObject _Py_EllipsisObject = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 _PyObject_EXTRA_INIT
90 1, &PyEllipsis_Type
Guido van Rossumf2d125b1996-07-30 16:45:48 +000091};
92
93
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +010094/* Slice object implementation */
Guido van Rossumf2d125b1996-07-30 16:45:48 +000095
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +010096/* Using a cache is very effective since typically only a single slice is
97 * created and then deleted again
98 */
99static PySliceObject *slice_cache = NULL;
100void PySlice_Fini(void)
101{
102 PySliceObject *obj = slice_cache;
103 if (obj != NULL) {
104 slice_cache = NULL;
Benjamin Petersonb0c04cb2016-04-16 15:12:29 -0700105 PyObject_GC_Del(obj);
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100106 }
107}
108
109/* start, stop, and step are python objects with None indicating no
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000110 index is present.
111*/
112
113PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000114PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000115{
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100116 PySliceObject *obj;
117 if (slice_cache != NULL) {
118 obj = slice_cache;
119 slice_cache = NULL;
120 _Py_NewReference((PyObject *)obj);
121 } else {
Benjamin Peterson2b601d32016-04-16 14:47:12 -0700122 obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100123 if (obj == NULL)
124 return NULL;
125 }
Guido van Rossumadf54102000-12-14 15:09:46 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 if (step == NULL) step = Py_None;
128 Py_INCREF(step);
129 if (start == NULL) start = Py_None;
130 Py_INCREF(start);
131 if (stop == NULL) stop = Py_None;
132 Py_INCREF(stop);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 obj->step = step;
135 obj->start = start;
136 obj->stop = stop;
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000137
Benjamin Peterson2b601d32016-04-16 14:47:12 -0700138 _PyObject_GC_TRACK(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return (PyObject *) obj;
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000140}
141
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142PyObject *
143_PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 PyObject *start, *end, *slice;
146 start = PyLong_FromSsize_t(istart);
147 if (!start)
148 return NULL;
149 end = PyLong_FromSsize_t(istop);
150 if (!end) {
151 Py_DECREF(start);
152 return NULL;
153 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 slice = PySlice_New(start, end, NULL);
156 Py_DECREF(start);
157 Py_DECREF(end);
158 return slice;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159}
160
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000161int
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000162PySlice_GetIndices(PyObject *_r, Py_ssize_t length,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000163 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000164{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000165 PySliceObject *r = (PySliceObject*)_r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 /* XXX support long ints */
167 if (r->step == Py_None) {
168 *step = 1;
169 } else {
170 if (!PyLong_Check(r->step)) return -1;
171 *step = PyLong_AsSsize_t(r->step);
172 }
173 if (r->start == Py_None) {
174 *start = *step < 0 ? length-1 : 0;
175 } else {
176 if (!PyLong_Check(r->start)) return -1;
177 *start = PyLong_AsSsize_t(r->start);
178 if (*start < 0) *start += length;
179 }
180 if (r->stop == Py_None) {
181 *stop = *step < 0 ? -1 : length;
182 } else {
183 if (!PyLong_Check(r->stop)) return -1;
184 *stop = PyLong_AsSsize_t(r->stop);
185 if (*stop < 0) *stop += length;
186 }
187 if (*stop > length) return -1;
188 if (*start >= length) return -1;
189 if (*step == 0) return -1;
190 return 0;
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000191}
192
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000193int
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200194PySlice_Unpack(PyObject *_r,
195 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000196{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000197 PySliceObject *r = (PySliceObject*)_r;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* this is harder to get right than you might think */
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (r->step == Py_None) {
201 *step = 1;
202 }
203 else {
204 if (!_PyEval_SliceIndex(r->step, step)) return -1;
205 if (*step == 0) {
206 PyErr_SetString(PyExc_ValueError,
207 "slice step cannot be zero");
208 return -1;
209 }
Mark Dickinsone6fc7402010-08-06 18:55:26 +0000210 /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it
211 * with -PY_SSIZE_T_MAX. This doesn't affect the semantics, and it
212 * guards against later undefined behaviour resulting from code that
213 * does "step = -step" as part of a slice reversal.
214 */
215 if (*step < -PY_SSIZE_T_MAX)
216 *step = -PY_SSIZE_T_MAX;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if (r->start == Py_None) {
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200220 *start = *step < 0 ? PY_SSIZE_T_MAX-1 : 0;;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 }
222 else {
223 if (!_PyEval_SliceIndex(r->start, start)) return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (r->stop == Py_None) {
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200227 *stop = *step < 0 ? -PY_SSIZE_T_MAX : PY_SSIZE_T_MAX;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 }
229 else {
230 if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 }
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000232
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200233 return 0;
234}
235
236Py_ssize_t
237PySlice_AdjustIndices(Py_ssize_t length,
238 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
239{
240 /* this is harder to get right than you might think */
241
242 assert(step != 0);
243 assert(step >= -PY_SSIZE_T_MAX);
244
245 if (*start < 0) {
246 *start += length;
247 if (*start < 0) {
248 *start = (step < 0) ? -1 : 0;
249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 }
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200251 else if (*start >= length) {
252 *start = (step < 0) ? length - 1 : length;
253 }
254
255 if (*stop < 0) {
256 *stop += length;
257 if (*stop < 0) {
258 *stop = (step < 0) ? -1 : 0;
259 }
260 }
261 else if (*stop >= length) {
262 *stop = (step < 0) ? length - 1 : length;
263 }
264
265 if (step < 0) {
266 if (*stop < *start) {
267 return (*start - *stop - 1) / (-step) + 1;
268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 }
270 else {
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200271 if (*start < *stop) {
272 return (*stop - *start - 1) / step + 1;
273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 }
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200275 return 0;
276}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000277
Serhiy Storchakab2a5be02017-01-25 13:23:05 +0200278#undef PySlice_GetIndicesEx
279
280int
281PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length,
282 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
283 Py_ssize_t *slicelength)
284{
285 if (PySlice_Unpack(_r, start, stop, step) < 0)
286 return -1;
287 *slicelength = PySlice_AdjustIndices(length, start, stop, *step);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 return 0;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000289}
290
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000291static PyObject *
292slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 PyObject *start, *stop, *step;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 start = stop = step = NULL;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (!_PyArg_NoKeywords("slice()", kw))
299 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
302 return NULL;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 /* This swapping of stop and start is to maintain similarity with
305 range(). */
306 if (stop == NULL) {
307 stop = start;
308 start = NULL;
309 }
310 return PySlice_New(start, stop, step);
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000311}
312
313PyDoc_STRVAR(slice_doc,
Chris Jerdonek83fe2e12012-10-07 14:48:36 -0700314"slice(stop)\n\
315slice(start, stop[, step])\n\
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000316\n\
317Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
318
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000319static void
Fred Drake45cfbcc2000-07-09 06:21:27 +0000320slice_dealloc(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000321{
Benjamin Peterson2b601d32016-04-16 14:47:12 -0700322 _PyObject_GC_UNTRACK(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 Py_DECREF(r->step);
324 Py_DECREF(r->start);
325 Py_DECREF(r->stop);
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100326 if (slice_cache == NULL)
327 slice_cache = r;
328 else
Benjamin Peterson2b601d32016-04-16 14:47:12 -0700329 PyObject_GC_Del(r);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000330}
331
332static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000333slice_repr(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000336}
337
Guido van Rossum6f799372001-09-20 20:46:19 +0000338static PyMemberDef slice_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
340 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
341 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
342 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000343};
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000344
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000345/* Helper function to convert a slice argument to a PyLong, and raise TypeError
346 with a suitable message on failure. */
347
348static PyObject*
349evaluate_slice_index(PyObject *v)
350{
351 if (PyIndex_Check(v)) {
352 return PyNumber_Index(v);
353 }
354 else {
355 PyErr_SetString(PyExc_TypeError,
356 "slice indices must be integers or "
357 "None or have an __index__ method");
358 return NULL;
359 }
360}
361
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000362/* Compute slice indices given a slice and length. Return -1 on failure. Used
363 by slice.indices and rangeobject slicing. Assumes that `len` is a
364 nonnegative instance of PyLong. */
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000365
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000366int
367_PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
368 PyObject **start_ptr, PyObject **stop_ptr,
369 PyObject **step_ptr)
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000370{
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000371 PyObject *start=NULL, *stop=NULL, *step=NULL;
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000372 PyObject *upper=NULL, *lower=NULL;
373 int step_is_negative, cmp_result;
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000374
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000375 /* Convert step to an integer; raise for zero step. */
376 if (self->step == Py_None) {
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000377 step = PyLong_FromLong(1L);
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000378 if (step == NULL)
379 goto error;
380 step_is_negative = 0;
381 }
382 else {
383 int step_sign;
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000384 step = evaluate_slice_index(self->step);
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000385 if (step == NULL)
386 goto error;
387 step_sign = _PyLong_Sign(step);
388 if (step_sign == 0) {
389 PyErr_SetString(PyExc_ValueError,
390 "slice step cannot be zero");
391 goto error;
392 }
393 step_is_negative = step_sign < 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 }
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000395
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000396 /* Find lower and upper bounds for start and stop. */
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000397 if (step_is_negative) {
398 lower = PyLong_FromLong(-1L);
399 if (lower == NULL)
400 goto error;
401
402 upper = PyNumber_Add(length, lower);
403 if (upper == NULL)
404 goto error;
405 }
406 else {
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000407 lower = PyLong_FromLong(0L);
408 if (lower == NULL)
409 goto error;
410
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000411 upper = length;
412 Py_INCREF(upper);
413 }
414
415 /* Compute start. */
416 if (self->start == Py_None) {
417 start = step_is_negative ? upper : lower;
418 Py_INCREF(start);
419 }
420 else {
421 start = evaluate_slice_index(self->start);
422 if (start == NULL)
423 goto error;
424
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000425 if (_PyLong_Sign(start) < 0) {
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000426 /* start += length */
427 PyObject *tmp = PyNumber_Add(start, length);
428 Py_DECREF(start);
429 start = tmp;
430 if (start == NULL)
431 goto error;
432
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000433 cmp_result = PyObject_RichCompareBool(start, lower, Py_LT);
434 if (cmp_result < 0)
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000435 goto error;
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000436 if (cmp_result) {
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000437 Py_INCREF(lower);
438 Py_DECREF(start);
439 start = lower;
440 }
441 }
442 else {
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000443 cmp_result = PyObject_RichCompareBool(start, upper, Py_GT);
444 if (cmp_result < 0)
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000445 goto error;
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000446 if (cmp_result) {
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000447 Py_INCREF(upper);
448 Py_DECREF(start);
449 start = upper;
450 }
451 }
452 }
453
454 /* Compute stop. */
455 if (self->stop == Py_None) {
456 stop = step_is_negative ? lower : upper;
457 Py_INCREF(stop);
458 }
459 else {
460 stop = evaluate_slice_index(self->stop);
461 if (stop == NULL)
462 goto error;
463
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000464 if (_PyLong_Sign(stop) < 0) {
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000465 /* stop += length */
466 PyObject *tmp = PyNumber_Add(stop, length);
467 Py_DECREF(stop);
468 stop = tmp;
469 if (stop == NULL)
470 goto error;
471
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000472 cmp_result = PyObject_RichCompareBool(stop, lower, Py_LT);
473 if (cmp_result < 0)
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000474 goto error;
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000475 if (cmp_result) {
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000476 Py_INCREF(lower);
477 Py_DECREF(stop);
478 stop = lower;
479 }
480 }
481 else {
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000482 cmp_result = PyObject_RichCompareBool(stop, upper, Py_GT);
483 if (cmp_result < 0)
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000484 goto error;
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000485 if (cmp_result) {
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000486 Py_INCREF(upper);
487 Py_DECREF(stop);
488 stop = upper;
489 }
490 }
491 }
492
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000493 *start_ptr = start;
494 *stop_ptr = stop;
495 *step_ptr = step;
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000496 Py_DECREF(upper);
497 Py_DECREF(lower);
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000498 return 0;
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000499
500 error:
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000501 *start_ptr = *stop_ptr = *step_ptr = NULL;
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000502 Py_XDECREF(start);
503 Py_XDECREF(stop);
504 Py_XDECREF(step);
505 Py_XDECREF(upper);
506 Py_XDECREF(lower);
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000507 return -1;
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000508}
509
Mark Dickinsonffdb2c22012-11-17 19:18:10 +0000510/* Implementation of slice.indices. */
511
512static PyObject*
513slice_indices(PySliceObject* self, PyObject* len)
514{
515 PyObject *start, *stop, *step;
516 PyObject *length;
517 int error;
518
519 /* Convert length to an integer if necessary; raise for negative length. */
520 length = PyNumber_Index(len);
521 if (length == NULL)
522 return NULL;
523
524 if (_PyLong_Sign(length) < 0) {
525 PyErr_SetString(PyExc_ValueError,
526 "length should not be negative");
527 Py_DECREF(length);
528 return NULL;
529 }
530
531 error = _PySlice_GetLongIndices(self, length, &start, &stop, &step);
532 Py_DECREF(length);
533 if (error == -1)
534 return NULL;
535 else
536 return Py_BuildValue("(NNN)", start, stop, step);
537}
Mark Dickinsonc8a69672012-11-10 14:52:10 +0000538
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000539PyDoc_STRVAR(slice_indices_doc,
540"S.indices(len) -> (start, stop, stride)\n\
541\n\
542Assuming a sequence of length len, calculate the start and stop\n\
543indices, and the stride length of the extended slice described by\n\
544S. Out of bounds indices are clipped in a manner consistent with the\n\
545handling of normal slices.");
546
Guido van Rossumd8faa362007-04-27 19:54:29 +0000547static PyObject *
548slice_reduce(PySliceObject* self)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000551}
552
553PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
554
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000555static PyMethodDef slice_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 {"indices", (PyCFunction)slice_indices,
557 METH_O, slice_indices_doc},
558 {"__reduce__", (PyCFunction)slice_reduce,
559 METH_NOARGS, reduce_doc},
560 {NULL, NULL}
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000561};
562
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000563static PyObject *
564slice_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossuma1351fb2001-03-20 12:41:34 +0000565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject *t1;
567 PyObject *t2;
568 PyObject *res;
Guido van Rossuma1351fb2001-03-20 12:41:34 +0000569
Brian Curtindfc80e32011-08-10 20:28:54 -0500570 if (!PySlice_Check(v) || !PySlice_Check(w))
571 Py_RETURN_NOTIMPLEMENTED;
Thomas Wouters3e57b522007-08-28 23:07:26 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (v == w) {
574 /* XXX Do we really need this shortcut?
575 There's a unit test for it, but is that fair? */
576 switch (op) {
577 case Py_EQ:
578 case Py_LE:
579 case Py_GE:
580 res = Py_True;
581 break;
582 default:
583 res = Py_False;
584 break;
585 }
586 Py_INCREF(res);
587 return res;
588 }
Guido van Rossuma1351fb2001-03-20 12:41:34 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 t1 = PyTuple_New(3);
Benjamin Peterson2963fe02011-10-17 13:09:27 -0400591 if (t1 == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 return NULL;
Benjamin Peterson2963fe02011-10-17 13:09:27 -0400593 t2 = PyTuple_New(3);
594 if (t2 == NULL) {
595 Py_DECREF(t1);
596 return NULL;
597 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start);
600 PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
601 PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
602 PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
603 PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
604 PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 res = PyObject_RichCompare(t1, t2, op);
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyTuple_SET_ITEM(t1, 0, NULL);
609 PyTuple_SET_ITEM(t1, 1, NULL);
610 PyTuple_SET_ITEM(t1, 2, NULL);
611 PyTuple_SET_ITEM(t2, 0, NULL);
612 PyTuple_SET_ITEM(t2, 1, NULL);
613 PyTuple_SET_ITEM(t2, 2, NULL);
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 Py_DECREF(t1);
616 Py_DECREF(t2);
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return res;
Guido van Rossuma1351fb2001-03-20 12:41:34 +0000619}
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000620
Benjamin Peterson2b601d32016-04-16 14:47:12 -0700621static int
622slice_traverse(PySliceObject *v, visitproc visit, void *arg)
623{
624 Py_VISIT(v->start);
625 Py_VISIT(v->stop);
626 Py_VISIT(v->step);
627 return 0;
628}
629
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000630PyTypeObject PySlice_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyVarObject_HEAD_INIT(&PyType_Type, 0)
632 "slice", /* Name of this type */
633 sizeof(PySliceObject), /* Basic object size */
634 0, /* Item size for varobject */
635 (destructor)slice_dealloc, /* tp_dealloc */
636 0, /* tp_print */
637 0, /* tp_getattr */
638 0, /* tp_setattr */
639 0, /* tp_reserved */
640 (reprfunc)slice_repr, /* tp_repr */
641 0, /* tp_as_number */
642 0, /* tp_as_sequence */
643 0, /* tp_as_mapping */
Benjamin Peterson23d05c12010-10-17 20:13:05 +0000644 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 0, /* tp_call */
646 0, /* tp_str */
647 PyObject_GenericGetAttr, /* tp_getattro */
648 0, /* tp_setattro */
649 0, /* tp_as_buffer */
Benjamin Peterson2b601d32016-04-16 14:47:12 -0700650 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 slice_doc, /* tp_doc */
Benjamin Peterson2b601d32016-04-16 14:47:12 -0700652 (traverseproc)slice_traverse, /* tp_traverse */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 0, /* tp_clear */
654 slice_richcompare, /* tp_richcompare */
655 0, /* tp_weaklistoffset */
656 0, /* tp_iter */
657 0, /* tp_iternext */
658 slice_methods, /* tp_methods */
659 slice_members, /* tp_members */
660 0, /* tp_getset */
661 0, /* tp_base */
662 0, /* tp_dict */
663 0, /* tp_descr_get */
664 0, /* tp_descr_set */
665 0, /* tp_dictoffset */
666 0, /* tp_init */
667 0, /* tp_alloc */
668 slice_new, /* tp_new */
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000669};