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) |
Guido van Rossum | d82fb78 | 2001-10-30 02:40:52 +0000 | [diff] [blame] | 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 | (reprfunc)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 */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 49 | PyObject _Py_EllipsisObject = { |
| 50 | PyObject_HEAD_INIT(&PyEllipsis_Type) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 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 * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 61 | PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 62 | { |
Neil Schemenauer | dcc819a | 2002-03-22 15:33:15 +0000 | [diff] [blame] | 63 | PySliceObject *obj = PyMalloc_New(PySliceObject, &PySlice_Type); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | adf5410 | 2000-12-14 15:09:46 +0000 | [diff] [blame] | 65 | if (obj == NULL) |
| 66 | return NULL; |
| 67 | |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 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 | int |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 83 | PySlice_GetIndices(PySliceObject *r, int length, |
| 84 | int *start, int *stop, int *step) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 85 | { |
| 86 | if (r->step == Py_None) { |
| 87 | *step = 1; |
| 88 | } else { |
| 89 | if (!PyInt_Check(r->step)) return -1; |
| 90 | *step = PyInt_AsLong(r->step); |
| 91 | } |
| 92 | if (r->start == Py_None) { |
| 93 | *start = *step < 0 ? length-1 : 0; |
| 94 | } else { |
| 95 | if (!PyInt_Check(r->start)) return -1; |
| 96 | *start = PyInt_AsLong(r->start); |
| 97 | if (*start < 0) *start += length; |
| 98 | } |
| 99 | if (r->stop == Py_None) { |
| 100 | *stop = *step < 0 ? -1 : length; |
| 101 | } else { |
| 102 | if (!PyInt_Check(r->stop)) return -1; |
| 103 | *stop = PyInt_AsLong(r->stop); |
| 104 | if (*stop < 0) *stop += length; |
| 105 | } |
| 106 | if (*stop > length) return -1; |
| 107 | if (*start >= length) return -1; |
| 108 | if (*step == 0) return -1; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 113 | slice_dealloc(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 114 | { |
| 115 | Py_DECREF(r->step); |
| 116 | Py_DECREF(r->start); |
| 117 | Py_DECREF(r->stop); |
Neil Schemenauer | dcc819a | 2002-03-22 15:33:15 +0000 | [diff] [blame] | 118 | PyMalloc_Del(r); |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 122 | slice_repr(PySliceObject *r) |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 123 | { |
| 124 | PyObject *s, *comma; |
| 125 | |
| 126 | s = PyString_FromString("slice("); |
| 127 | comma = PyString_FromString(", "); |
| 128 | PyString_ConcatAndDel(&s, PyObject_Repr(r->start)); |
| 129 | PyString_Concat(&s, comma); |
| 130 | PyString_ConcatAndDel(&s, PyObject_Repr(r->stop)); |
| 131 | PyString_Concat(&s, comma); |
| 132 | PyString_ConcatAndDel(&s, PyObject_Repr(r->step)); |
| 133 | PyString_ConcatAndDel(&s, PyString_FromString(")")); |
| 134 | Py_DECREF(comma); |
| 135 | return s; |
| 136 | } |
| 137 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 138 | static PyMemberDef slice_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 139 | {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
| 140 | {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
| 141 | {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
| 142 | {0} |
| 143 | }; |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | a1351fb | 2001-03-20 12:41:34 +0000 | [diff] [blame] | 145 | static int |
| 146 | slice_compare(PySliceObject *v, PySliceObject *w) |
| 147 | { |
| 148 | int result = 0; |
| 149 | |
| 150 | if (v == w) |
| 151 | return 0; |
| 152 | |
| 153 | if (PyObject_Cmp(v->start, w->start, &result) < 0) |
| 154 | return -2; |
| 155 | if (result != 0) |
| 156 | return result; |
| 157 | if (PyObject_Cmp(v->stop, w->stop, &result) < 0) |
| 158 | return -2; |
| 159 | if (result != 0) |
| 160 | return result; |
| 161 | if (PyObject_Cmp(v->step, w->step, &result) < 0) |
| 162 | return -2; |
| 163 | return result; |
| 164 | } |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 165 | |
| 166 | PyTypeObject PySlice_Type = { |
| 167 | PyObject_HEAD_INIT(&PyType_Type) |
| 168 | 0, /* Number of items for varobject */ |
| 169 | "slice", /* Name of this type */ |
| 170 | sizeof(PySliceObject), /* Basic object size */ |
| 171 | 0, /* Item size for varobject */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 172 | (destructor)slice_dealloc, /* tp_dealloc */ |
| 173 | 0, /* tp_print */ |
| 174 | 0, /* tp_getattr */ |
| 175 | 0, /* tp_setattr */ |
| 176 | (cmpfunc)slice_compare, /* tp_compare */ |
| 177 | (reprfunc)slice_repr, /* tp_repr */ |
| 178 | 0, /* tp_as_number */ |
| 179 | 0, /* tp_as_sequence */ |
| 180 | 0, /* tp_as_mapping */ |
| 181 | 0, /* tp_hash */ |
| 182 | 0, /* tp_call */ |
| 183 | 0, /* tp_str */ |
| 184 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 185 | 0, /* tp_setattro */ |
| 186 | 0, /* tp_as_buffer */ |
| 187 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 188 | 0, /* tp_doc */ |
| 189 | 0, /* tp_traverse */ |
| 190 | 0, /* tp_clear */ |
| 191 | 0, /* tp_richcompare */ |
| 192 | 0, /* tp_weaklistoffset */ |
| 193 | 0, /* tp_iter */ |
| 194 | 0, /* tp_iternext */ |
| 195 | 0, /* tp_methods */ |
| 196 | slice_members, /* tp_members */ |
| 197 | 0, /* tp_getset */ |
| 198 | 0, /* tp_base */ |
| 199 | 0, /* tp_dict */ |
Guido van Rossum | f2d125b | 1996-07-30 16:45:48 +0000 | [diff] [blame] | 200 | }; |