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
)
31 0, /*tp_dealloc*/ /*never called*/
36 (reprfunc
)ellipsis_repr
, /*tp_repr*/
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
55 PySlice_New(PyObject
*start
, PyObject
*stop
, PyObject
*step
)
57 PySliceObject
*obj
= PyObject_NEW(PySliceObject
, &PySlice_Type
);
62 if (step
== NULL
) step
= Py_None
;
64 if (start
== NULL
) start
= Py_None
;
66 if (stop
== NULL
) stop
= Py_None
;
73 return (PyObject
*) obj
;
77 PySlice_GetIndices(PySliceObject
*r
, int length
,
78 int *start
, int *stop
, int *step
)
80 if (r
->step
== Py_None
) {
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;
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
;
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;
107 slice_dealloc(PySliceObject
*r
)
116 slice_repr(PySliceObject
*r
)
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(")"));
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
},
140 slice_compare(PySliceObject
*v
, PySliceObject
*w
)
147 if (PyObject_Cmp(v
->start
, w
->start
, &result
) < 0)
151 if (PyObject_Cmp(v
->stop
, w
->stop
, &result
) < 0)
155 if (PyObject_Cmp(v
->step
, w
->step
, &result
) < 0)
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 */
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 */
178 PyObject_GenericGetAttr
, /* tp_getattro */
180 0, /* tp_as_buffer */
181 Py_TPFLAGS_DEFAULT
, /* tp_flags */
185 0, /* tp_richcompare */
186 0, /* tp_weaklistoffset */
190 slice_members
, /* tp_members */