blob: 8748fed4ce58a9a9256e5b46d98840523fe83f34 [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
10/*
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 *
Fred Drake45cfbcc2000-07-09 06:21:27 +000020ellipsis_repr(PyObject *op)
Guido van Rossumf2d125b1996-07-30 16:45:48 +000021{
Gregory P. Smithdd96db62008-06-09 04:58:54 +000022 return PyString_FromString("Ellipsis");
Guido van Rossumf2d125b1996-07-30 16:45:48 +000023}
24
Guido van Rossume449af71996-10-11 16:25:41 +000025static PyTypeObject PyEllipsis_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +000026 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Georg Brandl347b3002006-03-30 11:57:00 +000027 "ellipsis", /* tp_name */
28 0, /* tp_basicsize */
29 0, /* tp_itemsize */
30 0, /*never called*/ /* tp_dealloc */
31 0, /* tp_print */
32 0, /* tp_getattr */
33 0, /* tp_setattr */
34 0, /* tp_compare */
35 ellipsis_repr, /* tp_repr */
36 0, /* tp_as_number */
37 0, /* tp_as_sequence */
38 0, /* tp_as_mapping */
39 0, /* tp_hash */
40 0, /* tp_call */
41 0, /* tp_str */
42 PyObject_GenericGetAttr, /* tp_getattro */
43 0, /* tp_setattro */
44 0, /* tp_as_buffer */
45 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossumf2d125b1996-07-30 16:45:48 +000046};
47
Guido van Rossume449af71996-10-11 16:25:41 +000048PyObject _Py_EllipsisObject = {
Martin v. Löwis68192102007-07-21 06:55:02 +000049 _PyObject_EXTRA_INIT
50 1, &PyEllipsis_Type
Guido van Rossumf2d125b1996-07-30 16:45:48 +000051};
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
60PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000061PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
Guido van Rossumf2d125b1996-07-30 16:45:48 +000062{
Neil Schemenauer7465ad22002-04-12 03:05:37 +000063 PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
Guido van Rossumf2d125b1996-07-30 16:45:48 +000064
Guido van Rossumadf54102000-12-14 15:09:46 +000065 if (obj == NULL)
66 return NULL;
67
Guido van Rossumf2d125b1996-07-30 16:45:48 +000068 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);
74
75 obj->step = step;
76 obj->start = start;
77 obj->stop = stop;
78
79 return (PyObject *) obj;
80}
81
Neal Norwitzbadc0862006-03-23 06:03:08 +000082PyObject *
83_PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
84{
85 PyObject *start, *end, *slice;
86 start = PyInt_FromSsize_t(istart);
87 if (!start)
88 return NULL;
89 end = PyInt_FromSsize_t(istop);
90 if (!end) {
91 Py_DECREF(start);
92 return NULL;
93 }
94
95 slice = PySlice_New(start, end, NULL);
96 Py_DECREF(start);
97 Py_DECREF(end);
98 return slice;
99}
100
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000101int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000102PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
103 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000104{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000105 /* XXX support long ints */
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000106 if (r->step == Py_None) {
107 *step = 1;
108 } else {
Martin v. Löwis54b42f12006-04-03 11:38:08 +0000109 if (!PyInt_Check(r->step) && !PyLong_Check(r->step)) return -1;
Neal Norwitzd08eaf42006-04-03 04:46:04 +0000110 *step = PyInt_AsSsize_t(r->step);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000111 }
112 if (r->start == Py_None) {
113 *start = *step < 0 ? length-1 : 0;
114 } else {
Martin v. Löwis54b42f12006-04-03 11:38:08 +0000115 if (!PyInt_Check(r->start) && !PyLong_Check(r->step)) return -1;
Neal Norwitzd08eaf42006-04-03 04:46:04 +0000116 *start = PyInt_AsSsize_t(r->start);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000117 if (*start < 0) *start += length;
118 }
119 if (r->stop == Py_None) {
120 *stop = *step < 0 ? -1 : length;
121 } else {
Martin v. Löwis54b42f12006-04-03 11:38:08 +0000122 if (!PyInt_Check(r->stop) && !PyLong_Check(r->step)) return -1;
Neal Norwitzd08eaf42006-04-03 04:46:04 +0000123 *stop = PyInt_AsSsize_t(r->stop);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000124 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;
130}
131
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000132int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000133PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
134 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000135{
136 /* this is harder to get right than you might think */
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000137
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138 Py_ssize_t defstart, defstop;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000139
140 if (r->step == Py_None) {
141 *step = 1;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000142 }
143 else {
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +0000144 if (!_PyEval_SliceIndex(r->step, step)) return -1;
145 if (*step == 0) {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000146 PyErr_SetString(PyExc_ValueError,
147 "slice step cannot be zero");
148 return -1;
149 }
150 }
151
152 defstart = *step < 0 ? length-1 : 0;
153 defstop = *step < 0 ? -1 : length;
154
155 if (r->start == Py_None) {
156 *start = defstart;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000157 }
158 else {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000159 if (!_PyEval_SliceIndex(r->start, start)) return -1;
160 if (*start < 0) *start += length;
161 if (*start < 0) *start = (*step < 0) ? -1 : 0;
162 if (*start >= length)
163 *start = (*step < 0) ? length - 1 : length;
164 }
165
166 if (r->stop == Py_None) {
167 *stop = defstop;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000168 }
169 else {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000170 if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
171 if (*stop < 0) *stop += length;
Mark Dickinson1ec2fcd2008-06-20 14:53:43 +0000172 if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
173 if (*stop >= length)
174 *stop = (*step < 0) ? length - 1 : length;
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000175 }
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000176
177 if ((*step < 0 && *stop >= *start)
178 || (*step > 0 && *start >= *stop)) {
Michael W. Hudson589dc932002-06-11 13:38:42 +0000179 *slicelength = 0;
180 }
181 else if (*step < 0) {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000182 *slicelength = (*stop-*start+1)/(*step)+1;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000183 }
184 else {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000185 *slicelength = (*stop-*start-1)/(*step)+1;
186 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000187
188 return 0;
189}
190
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000191static PyObject *
192slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
193{
194 PyObject *start, *stop, *step;
195
196 start = stop = step = NULL;
197
Georg Brandl02c42872005-08-26 06:42:30 +0000198 if (!_PyArg_NoKeywords("slice()", kw))
199 return NULL;
200
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000201 if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000202 return NULL;
203
204 /* This swapping of stop and start is to maintain similarity with
205 range(). */
206 if (stop == NULL) {
207 stop = start;
208 start = NULL;
209 }
210 return PySlice_New(start, stop, step);
211}
212
213PyDoc_STRVAR(slice_doc,
214"slice([start,] stop[, step])\n\
215\n\
216Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
217
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000218static void
Fred Drake45cfbcc2000-07-09 06:21:27 +0000219slice_dealloc(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000220{
221 Py_DECREF(r->step);
222 Py_DECREF(r->start);
223 Py_DECREF(r->stop);
Neil Schemenauer7465ad22002-04-12 03:05:37 +0000224 PyObject_Del(r);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000225}
226
227static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000228slice_repr(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000229{
230 PyObject *s, *comma;
231
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000232 s = PyString_FromString("slice(");
233 comma = PyString_FromString(", ");
234 PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
235 PyString_Concat(&s, comma);
236 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
237 PyString_Concat(&s, comma);
238 PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
239 PyString_ConcatAndDel(&s, PyString_FromString(")"));
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000240 Py_DECREF(comma);
241 return s;
242}
243
Guido van Rossum6f799372001-09-20 20:46:19 +0000244static PyMemberDef slice_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000245 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
246 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
247 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
248 {0}
249};
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000250
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000251static PyObject*
252slice_indices(PySliceObject* self, PyObject* len)
253{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254 Py_ssize_t ilen, start, stop, step, slicelength;
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000255
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000256 ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError);
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000257
258 if (ilen == -1 && PyErr_Occurred()) {
259 return NULL;
260 }
261
262 if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
263 &step, &slicelength) < 0) {
264 return NULL;
265 }
266
Neal Norwitzd08eaf42006-04-03 04:46:04 +0000267 return Py_BuildValue("(nnn)", start, stop, step);
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000268}
269
270PyDoc_STRVAR(slice_indices_doc,
271"S.indices(len) -> (start, stop, stride)\n\
272\n\
273Assuming a sequence of length len, calculate the start and stop\n\
274indices, and the stride length of the extended slice described by\n\
275S. Out of bounds indices are clipped in a manner consistent with the\n\
276handling of normal slices.");
277
Raymond Hettinger13936692007-04-11 18:40:58 +0000278static PyObject *
279slice_reduce(PySliceObject* self)
280{
Christian Heimese93237d2007-12-19 02:37:44 +0000281 return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
Raymond Hettinger13936692007-04-11 18:40:58 +0000282}
283
284PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
285
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000286static PyMethodDef slice_methods[] = {
Michael W. Hudson206d8f82002-07-19 15:52:38 +0000287 {"indices", (PyCFunction)slice_indices,
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000288 METH_O, slice_indices_doc},
Raymond Hettinger13936692007-04-11 18:40:58 +0000289 {"__reduce__", (PyCFunction)slice_reduce,
290 METH_NOARGS, reduce_doc},
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000291 {NULL, NULL}
292};
293
Guido van Rossuma1351fb2001-03-20 12:41:34 +0000294static int
295slice_compare(PySliceObject *v, PySliceObject *w)
296{
297 int result = 0;
298
299 if (v == w)
300 return 0;
301
302 if (PyObject_Cmp(v->start, w->start, &result) < 0)
303 return -2;
304 if (result != 0)
305 return result;
306 if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
307 return -2;
308 if (result != 0)
309 return result;
310 if (PyObject_Cmp(v->step, w->step, &result) < 0)
311 return -2;
312 return result;
313}
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000314
Raymond Hettingerb859c072003-09-05 14:27:30 +0000315static long
316slice_hash(PySliceObject *v)
317{
318 PyErr_SetString(PyExc_TypeError, "unhashable type");
319 return -1L;
320}
321
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000322PyTypeObject PySlice_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000323 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000324 "slice", /* Name of this type */
325 sizeof(PySliceObject), /* Basic object size */
326 0, /* Item size for varobject */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000327 (destructor)slice_dealloc, /* tp_dealloc */
328 0, /* tp_print */
329 0, /* tp_getattr */
330 0, /* tp_setattr */
331 (cmpfunc)slice_compare, /* tp_compare */
332 (reprfunc)slice_repr, /* tp_repr */
333 0, /* tp_as_number */
334 0, /* tp_as_sequence */
335 0, /* tp_as_mapping */
Raymond Hettingerb859c072003-09-05 14:27:30 +0000336 (hashfunc)slice_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000337 0, /* tp_call */
338 0, /* tp_str */
339 PyObject_GenericGetAttr, /* tp_getattro */
340 0, /* tp_setattro */
341 0, /* tp_as_buffer */
342 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000343 slice_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000344 0, /* tp_traverse */
345 0, /* tp_clear */
346 0, /* tp_richcompare */
347 0, /* tp_weaklistoffset */
348 0, /* tp_iter */
349 0, /* tp_iternext */
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000350 slice_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000351 slice_members, /* tp_members */
352 0, /* tp_getset */
353 0, /* tp_base */
354 0, /* tp_dict */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000355 0, /* tp_descr_get */
356 0, /* tp_descr_set */
357 0, /* tp_dictoffset */
358 0, /* tp_init */
359 0, /* tp_alloc */
360 slice_new, /* tp_new */
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000361};