blob: 81517a88c905eb886c8b37335329bb250e151fd3 [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)
27 0,
Guido van Rossume449af71996-10-11 16:25:41 +000028 "ellipsis",
Guido van Rossumf2d125b1996-07-30 16:45:48 +000029 0,
30 0,
31 0, /*tp_dealloc*/ /*never called*/
32 0, /*tp_print*/
33 0, /*tp_getattr*/
34 0, /*tp_setattr*/
35 0, /*tp_compare*/
Guido van Rossume449af71996-10-11 16:25:41 +000036 (reprfunc)ellipsis_repr, /*tp_repr*/
Guido van Rossumf2d125b1996-07-30 16:45:48 +000037 0, /*tp_as_number*/
38 0, /*tp_as_sequence*/
39 0, /*tp_as_mapping*/
40 0, /*tp_hash */
41};
42
Guido van Rossume449af71996-10-11 16:25:41 +000043PyObject _Py_EllipsisObject = {
44 PyObject_HEAD_INIT(&PyEllipsis_Type)
Guido van Rossumf2d125b1996-07-30 16:45:48 +000045};
46
47
48/* Slice object implementation
49
50 start, stop, and step are python objects with None indicating no
51 index is present.
52*/
53
54PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000055PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
Guido van Rossumf2d125b1996-07-30 16:45:48 +000056{
Guido van Rossumb18618d2000-05-03 23:44:39 +000057 PySliceObject *obj = PyObject_NEW(PySliceObject, &PySlice_Type);
Guido van Rossumf2d125b1996-07-30 16:45:48 +000058
Guido van Rossumadf54102000-12-14 15:09:46 +000059 if (obj == NULL)
60 return NULL;
61
Guido van Rossumf2d125b1996-07-30 16:45:48 +000062 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);
68
69 obj->step = step;
70 obj->start = start;
71 obj->stop = stop;
72
73 return (PyObject *) obj;
74}
75
76int
Fred Drake45cfbcc2000-07-09 06:21:27 +000077PySlice_GetIndices(PySliceObject *r, int length,
78 int *start, int *stop, int *step)
Guido van Rossumf2d125b1996-07-30 16:45:48 +000079{
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);
85 }
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;
92 }
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;
99 }
100 if (*stop > length) return -1;
101 if (*start >= length) return -1;
102 if (*step == 0) return -1;
103 return 0;
104}
105
106static void
Fred Drake45cfbcc2000-07-09 06:21:27 +0000107slice_dealloc(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000108{
109 Py_DECREF(r->step);
110 Py_DECREF(r->start);
111 Py_DECREF(r->stop);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000112 PyObject_DEL(r);
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000113}
114
115static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000116slice_repr(PySliceObject *r)
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000117{
118 PyObject *s, *comma;
119
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;
130}
131
Guido van Rossum6f799372001-09-20 20:46:19 +0000132static PyMemberDef slice_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000133 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
134 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
135 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
136 {0}
137};
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000138
Guido van Rossuma1351fb2001-03-20 12:41:34 +0000139static int
140slice_compare(PySliceObject *v, PySliceObject *w)
141{
142 int result = 0;
143
144 if (v == w)
145 return 0;
146
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;
158}
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000159
160PyTypeObject 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 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166 (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 */
Guido van Rossumf2d125b1996-07-30 16:45:48 +0000194};