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.
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(start
, stop
, step
)
61 (PySliceObject
*) PyObject_NEW(PySliceObject
, &PySlice_Type
);
63 if (step
== NULL
) step
= Py_None
;
65 if (start
== NULL
) start
= Py_None
;
67 if (stop
== NULL
) stop
= Py_None
;
74 return (PyObject
*) obj
;
78 PySlice_GetIndices(r
, length
, start
, stop
, step
)
85 if (r
->step
== Py_None
) {
88 if (!PyInt_Check(r
->step
)) return -1;
89 *step
= PyInt_AsLong(r
->step
);
91 if (r
->start
== Py_None
) {
92 *start
= *step
< 0 ? length
-1 : 0;
94 if (!PyInt_Check(r
->start
)) return -1;
95 *start
= PyInt_AsLong(r
->start
);
96 if (*start
< 0) *start
+= length
;
98 if (r
->stop
== Py_None
) {
99 *stop
= *step
< 0 ? -1 : length
;
101 if (!PyInt_Check(r
->stop
)) return -1;
102 *stop
= PyInt_AsLong(r
->stop
);
103 if (*stop
< 0) *stop
+= length
;
105 if (*stop
> length
) return -1;
106 if (*start
>= length
) return -1;
107 if (*step
== 0) return -1;
127 s
= PyString_FromString("slice(");
128 comma
= PyString_FromString(", ");
129 PyString_ConcatAndDel(&s
, PyObject_Repr(r
->start
));
130 PyString_Concat(&s
, comma
);
131 PyString_ConcatAndDel(&s
, PyObject_Repr(r
->stop
));
132 PyString_Concat(&s
, comma
);
133 PyString_ConcatAndDel(&s
, PyObject_Repr(r
->step
));
134 PyString_ConcatAndDel(&s
, PyString_FromString(")"));
140 static PyObject
*slice_getattr(self
, name
)
147 if (strcmp(name
, "start") == 0) {
150 else if (strcmp(name
, "stop") == 0) {
153 else if (strcmp(name
, "step") == 0) {
156 else if (strcmp(name
, "__members__") == 0) {
157 return Py_BuildValue("[sss]",
158 "start", "stop", "step");
161 PyErr_SetString(PyExc_AttributeError
, name
);
169 PyTypeObject PySlice_Type
= {
170 PyObject_HEAD_INIT(&PyType_Type
)
171 0, /* Number of items for varobject */
172 "slice", /* Name of this type */
173 sizeof(PySliceObject
), /* Basic object size */
174 0, /* Item size for varobject */
175 (destructor
)slice_dealloc
, /*tp_dealloc*/
177 (getattrfunc
)slice_getattr
, /*tp_getattr*/
180 (reprfunc
)slice_repr
, /*tp_repr*/
182 0, /*tp_as_sequence*/