blob: a035e5faf01262b9e5367f50fc11119829e8a600 [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{
Guido van Rossume449af71996-10-11 16:25:41 +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 = {
Guido van Rossumf2d125b1996-07-30 16:45:48 +000026 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumd82fb782001-10-30 02:40:52 +000027 0, /* ob_size */
28 "ellipsis", /* tp_name */
29 0, /* tp_basicsize */
30 0, /* tp_itemsize */
31 0, /*never called*/ /* tp_dealloc */
32 0, /* tp_print */
33 0, /* tp_getattr */
34 0, /* tp_setattr */
35 0, /* tp_compare */
36 (reprfunc)ellipsis_repr, /* tp_repr */
37 0, /* tp_as_number */
38 0, /* tp_as_sequence */
39 0, /* tp_as_mapping */
40 0, /* tp_hash */
41 0, /* tp_call */
42 0, /* tp_str */
43 PyObject_GenericGetAttr, /* tp_getattro */
44 0, /* tp_setattro */
45 0, /* tp_as_buffer */
46 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossumf2d125b1996-07-30 16:45:48 +000047};
48
Guido van Rossume449af71996-10-11 16:25:41 +000049PyObject _Py_EllipsisObject = {
50 PyObject_HEAD_INIT(&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
82int
Fred Drake45cfbcc2000-07-09 06:21:27 +000083PySlice_GetIndices(PySliceObject *r, int length,
84 int *start, int *stop, int *step)
Guido van Rossumf2d125b1996-07-30 16:45:48 +000085{
86 if (r->step == Py_None) {
87 *step = 1;
88 } else {
89 if (!PyInt_Check(r->step)) return -1;
90 *step = PyInt_AsLong(r->step);
91 }
92 if (r->start == Py_None) {
93 *start = *step < 0 ? length-1 : 0;
94 } else {
95 if (!PyInt_Check(r->start)) return -1;
96 *start = PyInt_AsLong(r->start);
97 if (*start < 0) *start += length;
98 }
99 if (r->stop == Py_None) {
100 *stop = *step < 0 ? -1 : length;
101 } else {
102 if (!PyInt_Check(r->stop)) return -1;
103 *stop = PyInt_AsLong(r->stop);
104 if (*stop < 0) *stop += length;
105 }
106 if (*stop > length) return -1;
107 if (*start >= length) return -1;
108 if (*step == 0) return -1;
109 return 0;
110}
111
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000112int
113PySlice_GetIndicesEx(PySliceObject *r, int length,
114 int *start, int *stop, int *step, int *slicelength)
115{
116 /* this is harder to get right than you might think */
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000117
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000118 int defstart, defstop;
119
120 if (r->step == Py_None) {
121 *step = 1;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000122 }
123 else {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000124 *step = PyInt_AsLong(r->step);
125 if (*step == -1 && PyErr_Occurred()) {
126 return -1;
127 }
128 else if (*step == 0) {
129 PyErr_SetString(PyExc_ValueError,
130 "slice step cannot be zero");
131 return -1;
132 }
133 }
134
135 defstart = *step < 0 ? length-1 : 0;
136 defstop = *step < 0 ? -1 : length;
137
138 if (r->start == Py_None) {
139 *start = defstart;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000140 }
141 else {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000142 if (!_PyEval_SliceIndex(r->start, start)) return -1;
143 if (*start < 0) *start += length;
144 if (*start < 0) *start = (*step < 0) ? -1 : 0;
145 if (*start >= length)
146 *start = (*step < 0) ? length - 1 : length;
147 }
148
149 if (r->stop == Py_None) {
150 *stop = defstop;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000151 }
152 else {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000153 if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
154 if (*stop < 0) *stop += length;
155 if (*stop < 0) *stop = -1;
156 if (*stop > length) *stop = length;
157 }
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000158
159 if ((*step < 0 && *stop >= *start)
160 || (*step > 0 && *start >= *stop)) {
Michael W. Hudson589dc932002-06-11 13:38:42 +0000161 *slicelength = 0;
162 }
163 else if (*step < 0) {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000164 *slicelength = (*stop-*start+1)/(*step)+1;
Michael W. Hudson173f11d2002-11-05 15:28:51 +0000165 }
166 else {
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000167 *slicelength = (*stop-*start-1)/(*step)+1;
168 }
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000169
170 return 0;
171}
172
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000173static PyObject *
174slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
175{
176 PyObject *start, *stop, *step;
177
178 start = stop = step = NULL;
179
180 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
181 return NULL;
182
183 /* This swapping of stop and start is to maintain similarity with
184 range(). */
185 if (stop == NULL) {
186 stop = start;
187 start = NULL;
188 }
189 return PySlice_New(start, stop, step);
190}
191
192PyDoc_STRVAR(slice_doc,
193"slice([start,] stop[, step])\n\
194\n\
195Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
196
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000197static void
Fred Drake45cfbcc2000-07-09 06:21:27 +0000198slice_dealloc(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000199{
200 Py_DECREF(r->step);
201 Py_DECREF(r->start);
202 Py_DECREF(r->stop);
Neil Schemenauer7465ad22002-04-12 03:05:37 +0000203 PyObject_Del(r);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000204}
205
206static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000207slice_repr(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000208{
209 PyObject *s, *comma;
210
211 s = PyString_FromString("slice(");
212 comma = PyString_FromString(", ");
213 PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
214 PyString_Concat(&s, comma);
215 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
216 PyString_Concat(&s, comma);
217 PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
218 PyString_ConcatAndDel(&s, PyString_FromString(")"));
219 Py_DECREF(comma);
220 return s;
221}
222
Guido van Rossum6f799372001-09-20 20:46:19 +0000223static PyMemberDef slice_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
225 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
226 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
227 {0}
228};
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000229
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000230static PyObject*
231slice_indices(PySliceObject* self, PyObject* len)
232{
233 int ilen, start, stop, step, slicelength;
234
235 ilen = PyInt_AsLong(len);
236
237 if (ilen == -1 && PyErr_Occurred()) {
238 return NULL;
239 }
240
241 if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
242 &step, &slicelength) < 0) {
243 return NULL;
244 }
245
Michael W. Hudson5c1ad842002-09-12 09:31:30 +0000246 return Py_BuildValue("(iii)", start, stop, step);
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000247}
248
249PyDoc_STRVAR(slice_indices_doc,
250"S.indices(len) -> (start, stop, stride)\n\
251\n\
252Assuming a sequence of length len, calculate the start and stop\n\
253indices, and the stride length of the extended slice described by\n\
254S. Out of bounds indices are clipped in a manner consistent with the\n\
255handling of normal slices.");
256
257static PyMethodDef slice_methods[] = {
Michael W. Hudson206d8f82002-07-19 15:52:38 +0000258 {"indices", (PyCFunction)slice_indices,
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000259 METH_O, slice_indices_doc},
260 {NULL, NULL}
261};
262
Guido van Rossuma1351fb2001-03-20 12:41:34 +0000263static int
264slice_compare(PySliceObject *v, PySliceObject *w)
265{
266 int result = 0;
267
268 if (v == w)
269 return 0;
270
271 if (PyObject_Cmp(v->start, w->start, &result) < 0)
272 return -2;
273 if (result != 0)
274 return result;
275 if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
276 return -2;
277 if (result != 0)
278 return result;
279 if (PyObject_Cmp(v->step, w->step, &result) < 0)
280 return -2;
281 return result;
282}
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000283
284PyTypeObject PySlice_Type = {
285 PyObject_HEAD_INIT(&PyType_Type)
286 0, /* Number of items for varobject */
287 "slice", /* Name of this type */
288 sizeof(PySliceObject), /* Basic object size */
289 0, /* Item size for varobject */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000290 (destructor)slice_dealloc, /* tp_dealloc */
291 0, /* tp_print */
292 0, /* tp_getattr */
293 0, /* tp_setattr */
294 (cmpfunc)slice_compare, /* tp_compare */
295 (reprfunc)slice_repr, /* tp_repr */
296 0, /* tp_as_number */
297 0, /* tp_as_sequence */
298 0, /* tp_as_mapping */
299 0, /* tp_hash */
300 0, /* tp_call */
301 0, /* tp_str */
302 PyObject_GenericGetAttr, /* tp_getattro */
303 0, /* tp_setattro */
304 0, /* tp_as_buffer */
305 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000306 slice_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000307 0, /* tp_traverse */
308 0, /* tp_clear */
309 0, /* tp_richcompare */
310 0, /* tp_weaklistoffset */
311 0, /* tp_iter */
312 0, /* tp_iternext */
Michael W. Hudsonf0d777c2002-07-19 15:47:06 +0000313 slice_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000314 slice_members, /* tp_members */
315 0, /* tp_getset */
316 0, /* tp_base */
317 0, /* tp_dict */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000318 0, /* tp_descr_get */
319 0, /* tp_descr_set */
320 0, /* tp_dictoffset */
321 0, /* tp_init */
322 0, /* tp_alloc */
323 slice_new, /* tp_new */
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000324};