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
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.
17 #include "structmember.h"
20 ellipsis_repr(PyObject
*op
)
22 return PyString_FromString("Ellipsis");
25 static PyTypeObject PyEllipsis_Type
= {
26 PyObject_HEAD_INIT(&PyType_Type
)
28 "ellipsis", /* tp_name */
31 0, /*never called*/ /* tp_dealloc */
36 (reprfunc
)ellipsis_repr
, /* tp_repr */
38 0, /* tp_as_sequence */
39 0, /* tp_as_mapping */
43 PyObject_GenericGetAttr
, /* tp_getattro */
46 Py_TPFLAGS_DEFAULT
, /* tp_flags */
49 PyObject _Py_EllipsisObject
= {
50 PyObject_HEAD_INIT(&PyEllipsis_Type
)
54 /* Slice object implementation
56 start, stop, and step are python objects with None indicating no
61 PySlice_New(PyObject
*start
, PyObject
*stop
, PyObject
*step
)
63 PySliceObject
*obj
= PyObject_New(PySliceObject
, &PySlice_Type
);
68 if (step
== NULL
) step
= Py_None
;
70 if (start
== NULL
) start
= Py_None
;
72 if (stop
== NULL
) stop
= Py_None
;
79 return (PyObject
*) obj
;
83 PySlice_GetIndices(PySliceObject
*r
, int length
,
84 int *start
, int *stop
, int *step
)
86 if (r
->step
== Py_None
) {
89 if (!PyInt_Check(r
->step
)) return -1;
90 *step
= PyInt_AsLong(r
->step
);
92 if (r
->start
== Py_None
) {
93 *start
= *step
< 0 ? length
-1 : 0;
95 if (!PyInt_Check(r
->start
)) return -1;
96 *start
= PyInt_AsLong(r
->start
);
97 if (*start
< 0) *start
+= length
;
99 if (r
->stop
== Py_None
) {
100 *stop
= *step
< 0 ? -1 : length
;
102 if (!PyInt_Check(r
->stop
)) return -1;
103 *stop
= PyInt_AsLong(r
->stop
);
104 if (*stop
< 0) *stop
+= length
;
106 if (*stop
> length
) return -1;
107 if (*start
>= length
) return -1;
108 if (*step
== 0) return -1;
113 PySlice_GetIndicesEx(PySliceObject
*r
, int length
,
114 int *start
, int *stop
, int *step
, int *slicelength
)
116 /* this is harder to get right than you might think */
117 int defstart
, defstop
;
119 if (r
->step
== Py_None
) {
122 *step
= PyInt_AsLong(r
->step
);
123 if (*step
== -1 && PyErr_Occurred()) {
126 else if (*step
== 0) {
127 PyErr_SetString(PyExc_ValueError
,
128 "slice step cannot be zero");
133 defstart
= *step
< 0 ? length
-1 : 0;
134 defstop
= *step
< 0 ? -1 : length
;
136 if (r
->start
== Py_None
) {
139 if (!_PyEval_SliceIndex(r
->start
, start
)) return -1;
140 if (*start
< 0) *start
+= length
;
141 if (*start
< 0) *start
= (*step
< 0) ? -1 : 0;
142 if (*start
>= length
)
143 *start
= (*step
< 0) ? length
- 1 : length
;
146 if (r
->stop
== Py_None
) {
149 if (!_PyEval_SliceIndex(r
->stop
, stop
)) return -1;
150 if (*stop
< 0) *stop
+= length
;
151 if (*stop
< 0) *stop
= -1;
152 if (*stop
> length
) *stop
= length
;
155 if ((*stop
- *start
)*(*step
) <= 0) {
158 else if (*step
< 0) {
159 *slicelength
= (*stop
-*start
+1)/(*step
)+1;
161 *slicelength
= (*stop
-*start
-1)/(*step
)+1;
168 slice_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kw
)
170 PyObject
*start
, *stop
, *step
;
172 start
= stop
= step
= NULL
;
174 if (!PyArg_ParseTuple(args
, "O|OO:slice", &start
, &stop
, &step
))
177 /* This swapping of stop and start is to maintain similarity with
183 return PySlice_New(start
, stop
, step
);
186 PyDoc_STRVAR(slice_doc
,
187 "slice([start,] stop[, step])\n\
189 Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
192 slice_dealloc(PySliceObject
*r
)
201 slice_repr(PySliceObject
*r
)
205 s
= PyString_FromString("slice(");
206 comma
= PyString_FromString(", ");
207 PyString_ConcatAndDel(&s
, PyObject_Repr(r
->start
));
208 PyString_Concat(&s
, comma
);
209 PyString_ConcatAndDel(&s
, PyObject_Repr(r
->stop
));
210 PyString_Concat(&s
, comma
);
211 PyString_ConcatAndDel(&s
, PyObject_Repr(r
->step
));
212 PyString_ConcatAndDel(&s
, PyString_FromString(")"));
217 static PyMemberDef slice_members
[] = {
218 {"start", T_OBJECT
, offsetof(PySliceObject
, start
), READONLY
},
219 {"stop", T_OBJECT
, offsetof(PySliceObject
, stop
), READONLY
},
220 {"step", T_OBJECT
, offsetof(PySliceObject
, step
), READONLY
},
225 slice_compare(PySliceObject
*v
, PySliceObject
*w
)
232 if (PyObject_Cmp(v
->start
, w
->start
, &result
) < 0)
236 if (PyObject_Cmp(v
->stop
, w
->stop
, &result
) < 0)
240 if (PyObject_Cmp(v
->step
, w
->step
, &result
) < 0)
245 PyTypeObject PySlice_Type
= {
246 PyObject_HEAD_INIT(&PyType_Type
)
247 0, /* Number of items for varobject */
248 "slice", /* Name of this type */
249 sizeof(PySliceObject
), /* Basic object size */
250 0, /* Item size for varobject */
251 (destructor
)slice_dealloc
, /* tp_dealloc */
255 (cmpfunc
)slice_compare
, /* tp_compare */
256 (reprfunc
)slice_repr
, /* tp_repr */
257 0, /* tp_as_number */
258 0, /* tp_as_sequence */
259 0, /* tp_as_mapping */
263 PyObject_GenericGetAttr
, /* tp_getattro */
265 0, /* tp_as_buffer */
266 Py_TPFLAGS_DEFAULT
, /* tp_flags */
267 slice_doc
, /* tp_doc */
270 0, /* tp_richcompare */
271 0, /* tp_weaklistoffset */
275 slice_members
, /* tp_members */
279 0, /* tp_descr_get */
280 0, /* tp_descr_set */
281 0, /* tp_dictoffset */
284 slice_new
, /* tp_new */