1996-07-31 00:45:48 +08:00
|
|
|
/*
|
|
|
|
Written by Jim Hugunin and Chris Chase.
|
|
|
|
|
1996-10-12 00:25:41 +08:00
|
|
|
This includes both the singular ellipsis object and slice objects.
|
1996-07-31 00:45:48 +08:00
|
|
|
|
|
|
|
Guido, feel free to do whatever you want in the way of copyrights
|
|
|
|
for this file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1996-10-12 00:25:41 +08:00
|
|
|
Py_Ellipsis encodes the '...' rubber index token. It is similar to
|
1996-07-31 00:45:48 +08:00
|
|
|
the Py_NoneStruct in that there is no way to create other objects of
|
|
|
|
this type and there is exactly one in existence.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Python.h"
|
2001-08-02 12:15:00 +08:00
|
|
|
#include "structmember.h"
|
1996-07-31 00:45:48 +08:00
|
|
|
|
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
ellipsis_repr(PyObject *op)
|
1996-07-31 00:45:48 +08:00
|
|
|
{
|
1996-10-12 00:25:41 +08:00
|
|
|
return PyString_FromString("Ellipsis");
|
1996-07-31 00:45:48 +08:00
|
|
|
}
|
|
|
|
|
1996-10-12 00:25:41 +08:00
|
|
|
static PyTypeObject PyEllipsis_Type = {
|
1996-07-31 00:45:48 +08:00
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
2001-10-30 10:40:52 +08:00
|
|
|
0, /* ob_size */
|
|
|
|
"ellipsis", /* tp_name */
|
|
|
|
0, /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
0, /*never called*/ /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
(reprfunc)ellipsis_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
1996-07-31 00:45:48 +08:00
|
|
|
};
|
|
|
|
|
1996-10-12 00:25:41 +08:00
|
|
|
PyObject _Py_EllipsisObject = {
|
|
|
|
PyObject_HEAD_INIT(&PyEllipsis_Type)
|
1996-07-31 00:45:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Slice object implementation
|
|
|
|
|
|
|
|
start, stop, and step are python objects with None indicating no
|
|
|
|
index is present.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
|
1996-07-31 00:45:48 +08:00
|
|
|
{
|
2002-04-12 11:05:37 +08:00
|
|
|
PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
|
1996-07-31 00:45:48 +08:00
|
|
|
|
2000-12-14 23:09:46 +08:00
|
|
|
if (obj == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
1996-07-31 00:45:48 +08:00
|
|
|
if (step == NULL) step = Py_None;
|
|
|
|
Py_INCREF(step);
|
|
|
|
if (start == NULL) start = Py_None;
|
|
|
|
Py_INCREF(start);
|
|
|
|
if (stop == NULL) stop = Py_None;
|
|
|
|
Py_INCREF(stop);
|
|
|
|
|
|
|
|
obj->step = step;
|
|
|
|
obj->start = start;
|
|
|
|
obj->stop = stop;
|
|
|
|
|
|
|
|
return (PyObject *) obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 14:21:27 +08:00
|
|
|
PySlice_GetIndices(PySliceObject *r, int length,
|
|
|
|
int *start, int *stop, int *step)
|
1996-07-31 00:45:48 +08:00
|
|
|
{
|
|
|
|
if (r->step == Py_None) {
|
|
|
|
*step = 1;
|
|
|
|
} else {
|
|
|
|
if (!PyInt_Check(r->step)) return -1;
|
|
|
|
*step = PyInt_AsLong(r->step);
|
|
|
|
}
|
|
|
|
if (r->start == Py_None) {
|
|
|
|
*start = *step < 0 ? length-1 : 0;
|
|
|
|
} else {
|
|
|
|
if (!PyInt_Check(r->start)) return -1;
|
|
|
|
*start = PyInt_AsLong(r->start);
|
|
|
|
if (*start < 0) *start += length;
|
|
|
|
}
|
|
|
|
if (r->stop == Py_None) {
|
|
|
|
*stop = *step < 0 ? -1 : length;
|
|
|
|
} else {
|
|
|
|
if (!PyInt_Check(r->stop)) return -1;
|
|
|
|
*stop = PyInt_AsLong(r->stop);
|
|
|
|
if (*stop < 0) *stop += length;
|
|
|
|
}
|
|
|
|
if (*stop > length) return -1;
|
|
|
|
if (*start >= length) return -1;
|
|
|
|
if (*step == 0) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-11 18:55:12 +08:00
|
|
|
int
|
|
|
|
PySlice_GetIndicesEx(PySliceObject *r, int length,
|
|
|
|
int *start, int *stop, int *step, int *slicelength)
|
|
|
|
{
|
|
|
|
/* this is harder to get right than you might think */
|
2002-11-05 23:28:51 +08:00
|
|
|
|
2002-06-11 18:55:12 +08:00
|
|
|
int defstart, defstop;
|
|
|
|
|
|
|
|
if (r->step == Py_None) {
|
|
|
|
*step = 1;
|
2002-11-05 23:28:51 +08:00
|
|
|
}
|
|
|
|
else {
|
2002-11-06 23:17:32 +08:00
|
|
|
if (!_PyEval_SliceIndex(r->step, step)) return -1;
|
|
|
|
if (*step == 0) {
|
2002-06-11 18:55:12 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"slice step cannot be zero");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defstart = *step < 0 ? length-1 : 0;
|
|
|
|
defstop = *step < 0 ? -1 : length;
|
|
|
|
|
|
|
|
if (r->start == Py_None) {
|
|
|
|
*start = defstart;
|
2002-11-05 23:28:51 +08:00
|
|
|
}
|
|
|
|
else {
|
2002-06-11 18:55:12 +08:00
|
|
|
if (!_PyEval_SliceIndex(r->start, start)) return -1;
|
|
|
|
if (*start < 0) *start += length;
|
|
|
|
if (*start < 0) *start = (*step < 0) ? -1 : 0;
|
|
|
|
if (*start >= length)
|
|
|
|
*start = (*step < 0) ? length - 1 : length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->stop == Py_None) {
|
|
|
|
*stop = defstop;
|
2002-11-05 23:28:51 +08:00
|
|
|
}
|
|
|
|
else {
|
2002-06-11 18:55:12 +08:00
|
|
|
if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
|
|
|
|
if (*stop < 0) *stop += length;
|
|
|
|
if (*stop < 0) *stop = -1;
|
|
|
|
if (*stop > length) *stop = length;
|
|
|
|
}
|
2002-11-05 23:28:51 +08:00
|
|
|
|
|
|
|
if ((*step < 0 && *stop >= *start)
|
|
|
|
|| (*step > 0 && *start >= *stop)) {
|
2002-06-11 21:38:42 +08:00
|
|
|
*slicelength = 0;
|
|
|
|
}
|
|
|
|
else if (*step < 0) {
|
2002-06-11 18:55:12 +08:00
|
|
|
*slicelength = (*stop-*start+1)/(*step)+1;
|
2002-11-05 23:28:51 +08:00
|
|
|
}
|
|
|
|
else {
|
2002-06-11 18:55:12 +08:00
|
|
|
*slicelength = (*stop-*start-1)/(*step)+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-15 04:41:17 +08:00
|
|
|
static PyObject *
|
|
|
|
slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *start, *stop, *step;
|
|
|
|
|
|
|
|
start = stop = step = NULL;
|
|
|
|
|
2002-12-30 00:33:45 +08:00
|
|
|
if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
|
2002-06-15 04:41:17 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* This swapping of stop and start is to maintain similarity with
|
|
|
|
range(). */
|
|
|
|
if (stop == NULL) {
|
|
|
|
stop = start;
|
|
|
|
start = NULL;
|
|
|
|
}
|
|
|
|
return PySlice_New(start, stop, step);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(slice_doc,
|
|
|
|
"slice([start,] stop[, step])\n\
|
|
|
|
\n\
|
|
|
|
Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
|
|
|
|
|
1996-07-31 00:45:48 +08:00
|
|
|
static void
|
2000-07-09 14:21:27 +08:00
|
|
|
slice_dealloc(PySliceObject *r)
|
1996-07-31 00:45:48 +08:00
|
|
|
{
|
|
|
|
Py_DECREF(r->step);
|
|
|
|
Py_DECREF(r->start);
|
|
|
|
Py_DECREF(r->stop);
|
2002-04-12 11:05:37 +08:00
|
|
|
PyObject_Del(r);
|
1996-07-31 00:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-09 14:21:27 +08:00
|
|
|
slice_repr(PySliceObject *r)
|
1996-07-31 00:45:48 +08:00
|
|
|
{
|
|
|
|
PyObject *s, *comma;
|
|
|
|
|
|
|
|
s = PyString_FromString("slice(");
|
|
|
|
comma = PyString_FromString(", ");
|
|
|
|
PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
|
|
|
|
PyString_Concat(&s, comma);
|
|
|
|
PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
|
|
|
|
PyString_Concat(&s, comma);
|
|
|
|
PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
|
|
|
|
PyString_ConcatAndDel(&s, PyString_FromString(")"));
|
|
|
|
Py_DECREF(comma);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-09-21 04:46:19 +08:00
|
|
|
static PyMemberDef slice_members[] = {
|
2001-08-02 12:15:00 +08:00
|
|
|
{"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
|
|
|
|
{"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
|
|
|
|
{"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
|
|
|
|
{0}
|
|
|
|
};
|
1996-07-31 00:45:48 +08:00
|
|
|
|
2002-07-19 23:47:06 +08:00
|
|
|
static PyObject*
|
|
|
|
slice_indices(PySliceObject* self, PyObject* len)
|
|
|
|
{
|
|
|
|
int ilen, start, stop, step, slicelength;
|
|
|
|
|
|
|
|
ilen = PyInt_AsLong(len);
|
|
|
|
|
|
|
|
if (ilen == -1 && PyErr_Occurred()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
|
|
|
|
&step, &slicelength) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-09-12 17:31:30 +08:00
|
|
|
return Py_BuildValue("(iii)", start, stop, step);
|
2002-07-19 23:47:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(slice_indices_doc,
|
|
|
|
"S.indices(len) -> (start, stop, stride)\n\
|
|
|
|
\n\
|
|
|
|
Assuming a sequence of length len, calculate the start and stop\n\
|
|
|
|
indices, and the stride length of the extended slice described by\n\
|
|
|
|
S. Out of bounds indices are clipped in a manner consistent with the\n\
|
|
|
|
handling of normal slices.");
|
|
|
|
|
|
|
|
static PyMethodDef slice_methods[] = {
|
2002-07-19 23:52:38 +08:00
|
|
|
{"indices", (PyCFunction)slice_indices,
|
2002-07-19 23:47:06 +08:00
|
|
|
METH_O, slice_indices_doc},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2001-03-20 20:41:34 +08:00
|
|
|
static int
|
|
|
|
slice_compare(PySliceObject *v, PySliceObject *w)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
if (v == w)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (PyObject_Cmp(v->start, w->start, &result) < 0)
|
|
|
|
return -2;
|
|
|
|
if (result != 0)
|
|
|
|
return result;
|
|
|
|
if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
|
|
|
|
return -2;
|
|
|
|
if (result != 0)
|
|
|
|
return result;
|
|
|
|
if (PyObject_Cmp(v->step, w->step, &result) < 0)
|
|
|
|
return -2;
|
|
|
|
return result;
|
|
|
|
}
|
1996-07-31 00:45:48 +08:00
|
|
|
|
2003-09-05 22:27:30 +08:00
|
|
|
static long
|
|
|
|
slice_hash(PySliceObject *v)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "unhashable type");
|
|
|
|
return -1L;
|
|
|
|
}
|
|
|
|
|
1996-07-31 00:45:48 +08:00
|
|
|
PyTypeObject PySlice_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
0, /* Number of items for varobject */
|
|
|
|
"slice", /* Name of this type */
|
|
|
|
sizeof(PySliceObject), /* Basic object size */
|
|
|
|
0, /* Item size for varobject */
|
2001-08-02 12:15:00 +08:00
|
|
|
(destructor)slice_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
(cmpfunc)slice_compare, /* tp_compare */
|
|
|
|
(reprfunc)slice_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
2003-09-05 22:27:30 +08:00
|
|
|
(hashfunc)slice_hash, /* tp_hash */
|
2001-08-02 12:15:00 +08:00
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
2002-06-15 04:41:17 +08:00
|
|
|
slice_doc, /* tp_doc */
|
2001-08-02 12:15:00 +08:00
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
2002-07-19 23:47:06 +08:00
|
|
|
slice_methods, /* tp_methods */
|
2001-08-02 12:15:00 +08:00
|
|
|
slice_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
2002-06-15 04:41:17 +08:00
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
slice_new, /* tp_new */
|
1996-07-31 00:45:48 +08:00
|
|
|
};
|