Merged release21-maint changes.
[python/dscho.git] / Objects / sliceobject.c
blob8f52f9ec1722157a5d286a5ecd80814ec123f50e
1 /*
2 Written by Jim Hugunin and Chris Chase.
4 This includes both the singular ellipsis object and slice objects.
6 Guido, feel free to do whatever you want in the way of copyrights
7 for this file.
8 */
10 /*
11 Py_Ellipsis encodes the '...' rubber index token. It is similar to
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.
16 #include "Python.h"
17 #include "structmember.h"
19 static PyObject *
20 ellipsis_repr(PyObject *op)
22 return PyString_FromString("Ellipsis");
25 static PyTypeObject PyEllipsis_Type = {
26 PyObject_HEAD_INIT(&PyType_Type)
28 "ellipsis",
31 0, /*tp_dealloc*/ /*never called*/
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 */
43 PyObject _Py_EllipsisObject = {
44 PyObject_HEAD_INIT(&PyEllipsis_Type)
48 /* Slice object implementation
50 start, stop, and step are python objects with None indicating no
51 index is present.
54 PyObject *
55 PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
57 PySliceObject *obj = PyObject_NEW(PySliceObject, &PySlice_Type);
59 if (obj == NULL)
60 return NULL;
62 if (step == NULL) step = Py_None;
63 Py_INCREF(step);
64 if (start == NULL) start = Py_None;
65 Py_INCREF(start);
66 if (stop == NULL) stop = Py_None;
67 Py_INCREF(stop);
69 obj->step = step;
70 obj->start = start;
71 obj->stop = stop;
73 return (PyObject *) obj;
76 int
77 PySlice_GetIndices(PySliceObject *r, int length,
78 int *start, int *stop, int *step)
80 if (r->step == Py_None) {
81 *step = 1;
82 } else {
83 if (!PyInt_Check(r->step)) return -1;
84 *step = PyInt_AsLong(r->step);
86 if (r->start == Py_None) {
87 *start = *step < 0 ? length-1 : 0;
88 } else {
89 if (!PyInt_Check(r->start)) return -1;
90 *start = PyInt_AsLong(r->start);
91 if (*start < 0) *start += length;
93 if (r->stop == Py_None) {
94 *stop = *step < 0 ? -1 : length;
95 } else {
96 if (!PyInt_Check(r->stop)) return -1;
97 *stop = PyInt_AsLong(r->stop);
98 if (*stop < 0) *stop += length;
100 if (*stop > length) return -1;
101 if (*start >= length) return -1;
102 if (*step == 0) return -1;
103 return 0;
106 static void
107 slice_dealloc(PySliceObject *r)
109 Py_DECREF(r->step);
110 Py_DECREF(r->start);
111 Py_DECREF(r->stop);
112 PyObject_DEL(r);
115 static PyObject *
116 slice_repr(PySliceObject *r)
118 PyObject *s, *comma;
120 s = PyString_FromString("slice(");
121 comma = PyString_FromString(", ");
122 PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
123 PyString_Concat(&s, comma);
124 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
125 PyString_Concat(&s, comma);
126 PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
127 PyString_ConcatAndDel(&s, PyString_FromString(")"));
128 Py_DECREF(comma);
129 return s;
132 static struct memberlist slice_members[] = {
133 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
134 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
135 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
139 static int
140 slice_compare(PySliceObject *v, PySliceObject *w)
142 int result = 0;
144 if (v == w)
145 return 0;
147 if (PyObject_Cmp(v->start, w->start, &result) < 0)
148 return -2;
149 if (result != 0)
150 return result;
151 if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
152 return -2;
153 if (result != 0)
154 return result;
155 if (PyObject_Cmp(v->step, w->step, &result) < 0)
156 return -2;
157 return result;
160 PyTypeObject PySlice_Type = {
161 PyObject_HEAD_INIT(&PyType_Type)
162 0, /* Number of items for varobject */
163 "slice", /* Name of this type */
164 sizeof(PySliceObject), /* Basic object size */
165 0, /* Item size for varobject */
166 (destructor)slice_dealloc, /* tp_dealloc */
167 0, /* tp_print */
168 0, /* tp_getattr */
169 0, /* tp_setattr */
170 (cmpfunc)slice_compare, /* tp_compare */
171 (reprfunc)slice_repr, /* tp_repr */
172 0, /* tp_as_number */
173 0, /* tp_as_sequence */
174 0, /* tp_as_mapping */
175 0, /* tp_hash */
176 0, /* tp_call */
177 0, /* tp_str */
178 PyObject_GenericGetAttr, /* tp_getattro */
179 0, /* tp_setattro */
180 0, /* tp_as_buffer */
181 Py_TPFLAGS_DEFAULT, /* tp_flags */
182 0, /* tp_doc */
183 0, /* tp_traverse */
184 0, /* tp_clear */
185 0, /* tp_richcompare */
186 0, /* tp_weaklistoffset */
187 0, /* tp_iter */
188 0, /* tp_iternext */
189 0, /* tp_methods */
190 slice_members, /* tp_members */
191 0, /* tp_getset */
192 0, /* tp_base */
193 0, /* tp_dict */