| 1 | /*
|
|---|
| 2 | Written by Jim Hugunin and Chris Chase.
|
|---|
| 3 |
|
|---|
| 4 | This includes both the singular ellipsis object and slice objects.
|
|---|
| 5 |
|
|---|
| 6 | Guido, feel free to do whatever you want in the way of copyrights
|
|---|
| 7 | for this file.
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | /*
|
|---|
| 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.
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | #include "Python.h"
|
|---|
| 17 | #include "structmember.h"
|
|---|
| 18 |
|
|---|
| 19 | static PyObject *
|
|---|
| 20 | ellipsis_repr(PyObject *op)
|
|---|
| 21 | {
|
|---|
| 22 | return PyString_FromString("Ellipsis");
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | static PyTypeObject PyEllipsis_Type = {
|
|---|
| 26 | PyObject_HEAD_INIT(&PyType_Type)
|
|---|
| 27 | 0, /* ob_size */
|
|---|
| 28 | "ellipsis", /* tp_name */
|
|---|
| 29 | 0, /* tp_basicsize */
|
|---|
| 30 | 0, /* tp_itemsize */
|
|---|
| 31 | 0, /*never called*/ /* tp_dealloc */
|
|---|
| 32 | 0, /* tp_print */
|
|---|
| 33 | 0, /* tp_getattr */
|
|---|
| 34 | 0, /* tp_setattr */
|
|---|
| 35 | 0, /* tp_compare */
|
|---|
| 36 | ellipsis_repr, /* tp_repr */
|
|---|
| 37 | 0, /* tp_as_number */
|
|---|
| 38 | 0, /* tp_as_sequence */
|
|---|
| 39 | 0, /* tp_as_mapping */
|
|---|
| 40 | 0, /* tp_hash */
|
|---|
| 41 | 0, /* tp_call */
|
|---|
| 42 | 0, /* tp_str */
|
|---|
| 43 | PyObject_GenericGetAttr, /* tp_getattro */
|
|---|
| 44 | 0, /* tp_setattro */
|
|---|
| 45 | 0, /* tp_as_buffer */
|
|---|
| 46 | Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | PyObject _Py_EllipsisObject = {
|
|---|
| 50 | PyObject_HEAD_INIT(&PyEllipsis_Type)
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /* Slice object implementation
|
|---|
| 55 |
|
|---|
| 56 | start, stop, and step are python objects with None indicating no
|
|---|
| 57 | index is present.
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | PyObject *
|
|---|
| 61 | PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
|
|---|
| 62 | {
|
|---|
| 63 | PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
|
|---|
| 64 |
|
|---|
| 65 | if (obj == NULL)
|
|---|
| 66 | return NULL;
|
|---|
| 67 |
|
|---|
| 68 | if (step == NULL) step = Py_None;
|
|---|
| 69 | Py_INCREF(step);
|
|---|
| 70 | if (start == NULL) start = Py_None;
|
|---|
| 71 | Py_INCREF(start);
|
|---|
| 72 | if (stop == NULL) stop = Py_None;
|
|---|
| 73 | Py_INCREF(stop);
|
|---|
| 74 |
|
|---|
| 75 | obj->step = step;
|
|---|
| 76 | obj->start = start;
|
|---|
| 77 | obj->stop = stop;
|
|---|
| 78 |
|
|---|
| 79 | return (PyObject *) obj;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | PyObject *
|
|---|
| 83 | _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
|
|---|
| 84 | {
|
|---|
| 85 | PyObject *start, *end, *slice;
|
|---|
| 86 | start = PyInt_FromSsize_t(istart);
|
|---|
| 87 | if (!start)
|
|---|
| 88 | return NULL;
|
|---|
| 89 | end = PyInt_FromSsize_t(istop);
|
|---|
| 90 | if (!end) {
|
|---|
| 91 | Py_DECREF(start);
|
|---|
| 92 | return NULL;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | slice = PySlice_New(start, end, NULL);
|
|---|
| 96 | Py_DECREF(start);
|
|---|
| 97 | Py_DECREF(end);
|
|---|
| 98 | return slice;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | int
|
|---|
| 102 | PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
|
|---|
| 103 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
|
|---|
| 104 | {
|
|---|
| 105 | /* XXX support long ints */
|
|---|
| 106 | if (r->step == Py_None) {
|
|---|
| 107 | *step = 1;
|
|---|
| 108 | } else {
|
|---|
| 109 | if (!PyInt_Check(r->step) && !PyLong_Check(r->step)) return -1;
|
|---|
| 110 | *step = PyInt_AsSsize_t(r->step);
|
|---|
| 111 | }
|
|---|
| 112 | if (r->start == Py_None) {
|
|---|
| 113 | *start = *step < 0 ? length-1 : 0;
|
|---|
| 114 | } else {
|
|---|
| 115 | if (!PyInt_Check(r->start) && !PyLong_Check(r->step)) return -1;
|
|---|
| 116 | *start = PyInt_AsSsize_t(r->start);
|
|---|
| 117 | if (*start < 0) *start += length;
|
|---|
| 118 | }
|
|---|
| 119 | if (r->stop == Py_None) {
|
|---|
| 120 | *stop = *step < 0 ? -1 : length;
|
|---|
|
|---|