Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | Written by Jim Hugunin and Chris Chase. |
| 3 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 4 | This includes both the singular ellipsis object and slice objects. |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 5 | |
| 6 | Guido, feel free to do whatever you want in the way of copyrights |
| 7 | for this file. |
| 8 | */ |
| 9 | |
| 10 | /* |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 11 | Py_Ellipsis encodes the '...' rubber index token. It is similar to |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 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" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 17 | #include "structmember.h" |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 18 | |
| 19 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 20 | ellipsis_repr(PyObject *op) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 21 | { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 22 | return PyString_FromString("Ellipsis"); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 25 | static PyTypeObject PyEllipsis_Type = { |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 26 | PyObject_HEAD_INIT(&PyType_Type) |
| 27 | 0, |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 28 | "ellipsis", |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 29 | 0, |
| 30 | 0, |
| 31 | 0, /*tp_dealloc*/ /*never called*/ |
| 32 | 0, /*tp_print*/ |
| 33 | 0, /*tp_getattr*/ |
| 34 | 0, /*tp_setattr*/ |
| 35 | 0, /*tp_compare*/ |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 36 | (reprfunc)ellipsis_repr, /*tp_repr*/ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 37 | 0, /*tp_as_number*/ |
| 38 | 0, /*tp_as_sequence*/ |
| 39 | 0, /*tp_as_mapping*/ |
| 40 | 0, /*tp_hash */ |
| 41 | }; |
| 42 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 43 | PyObject _Py_EllipsisObject = { |
| 44 | PyObject_HEAD_INIT(&PyEllipsis_Type) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | |
| 48 | /* Slice object implementation |
| 49 | |
| 50 | start, stop, and step are python objects with None indicating no |
| 51 | index is present. |
| 52 | */ |
| 53 | |
| 54 | PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 55 | PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 56 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 57 | PySliceObject *obj = PyObject_NEW(PySliceObject, &PySlice_Type); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | adf5410 | 2000-12-14 15:09:46 +0000 | [diff] [blame] | 59 | if (obj == NULL) |
| 60 | return NULL; |
| 61 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 62 | if (step == NULL) step = Py_None; |
| 63 | Py_INCREF(step); |
| 64 | if (start == NULL) start = Py_None; |
| 65 | Py_INCREF(start); |
| 66 | if (stop == NULL) stop = Py_None; |
| 67 | Py_INCREF(stop); |
| 68 | |
| 69 | obj->step = step; |
| 70 | obj->start = start; |
| 71 | obj->stop = stop; |
| 72 | |
| 73 | return (PyObject *) obj; |
| 74 | } |
| 75 | |
| 76 | int |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 77 | PySlice_GetIndices(PySliceObject *r, int length, |
| 78 | int *start, int *stop, int *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 79 | { |
| 80 | if (r->step == Py_None) { |
| 81 | *step = 1; |
| 82 | } else { |
| 83 | if (!PyInt_Check(r->step)) return -1; |
| 84 | *step = PyInt_AsLong(r->step); |
| 85 | } |
| 86 | if (r->start == Py_None) { |
| 87 | *start = *step < 0 ? length-1 : 0; |
| 88 | } else { |
| 89 | if (!PyInt_Check(r->start)) return -1; |
| 90 | *start = PyInt_AsLong(r->start); |
| 91 | if (*start < 0) *start += length; |
| 92 | } |
| 93 | if (r->stop == Py_None) { |
| 94 | *stop = *step < 0 ? -1 : length; |
| 95 | } else { |
| 96 | if (!PyInt_Check(r->stop)) return -1; |
| 97 | *stop = PyInt_AsLong(r->stop); |
| 98 | if (*stop < 0) *stop += length; |
| 99 | } |
| 100 | if (*stop > length) return -1; |
| 101 | if (*start >= length) return -1; |
| 102 | if (*step == 0) return -1; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static void |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 107 | slice_dealloc(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 108 | { |
| 109 | Py_DECREF(r->step); |
| 110 | Py_DECREF(r->start); |
| 111 | Py_DECREF(r->stop); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 112 | PyObject_DEL(r); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 116 | slice_repr(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 117 | { |
| 118 | PyObject *s, *comma; |
| 119 | |
| 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(")")); |
| 128 | Py_DECREF(comma); |
| 129 | return s; |
| 130 | } |
| 131 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 132 | static PyMemberDef slice_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 133 | {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
| 134 | {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
| 135 | {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
| 136 | {0} |
| 137 | }; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 139 | static int |
| 140 | slice_compare(PySliceObject *v, PySliceObject *w) |
| 141 | { |
| 142 | int result = 0; |
| 143 | |
| 144 | if (v == w) |
| 145 | return 0; |
| 146 | |
| 147 | if (PyObject_Cmp(v->start, w->start, &result) < 0) |
| 148 | return -2; |
| 149 | if (result != 0) |
| 150 | return result; |
| 151 | if (PyObject_Cmp(v->stop, w->stop, &result) < 0) |
| 152 | return -2; |
| 153 | if (result != 0) |
| 154 | return result; |
| 155 | if (PyObject_Cmp(v->step, w->step, &result) < 0) |
| 156 | return -2; |
| 157 | return result; |
| 158 | } |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 159 | |
| 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 */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 166 | (destructor)slice_dealloc, /* tp_dealloc */ |
| 167 | 0, /* tp_print */ |
| 168 | 0, /* tp_getattr */ |
| 169 | 0, /* tp_setattr */ |
| 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 */ |
| 175 | 0, /* tp_hash */ |
| 176 | 0, /* tp_call */ |
| 177 | 0, /* tp_str */ |
| 178 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 179 | 0, /* tp_setattro */ |
| 180 | 0, /* tp_as_buffer */ |
| 181 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 182 | 0, /* tp_doc */ |
| 183 | 0, /* tp_traverse */ |
| 184 | 0, /* tp_clear */ |
| 185 | 0, /* tp_richcompare */ |
| 186 | 0, /* tp_weaklistoffset */ |
| 187 | 0, /* tp_iter */ |
| 188 | 0, /* tp_iternext */ |
| 189 | 0, /* tp_methods */ |
| 190 | slice_members, /* tp_members */ |
| 191 | 0, /* tp_getset */ |
| 192 | 0, /* tp_base */ |
| 193 | 0, /* tp_dict */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 194 | }; |