Mercurial > cpython-lru_cache-weakref
annotate Objects/sliceobject.c @ 17581:c1937faabb23 2.1
Added support for optional explanation parameter to the \versionadded
macro.
Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most
of the work in a helper function, with the do_cmd_*() wrappers just supplying
a portion of the replacement text.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 18 Apr 2001 17:29:14 +0000 |
| parents | 52cb0c5fdf23 |
| children | 09df3254b49d |
| rev | line source |
|---|---|
| 3837 | 1 /* |
| 2 Written by Jim Hugunin and Chris Chase. | |
| 3 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
4 This includes both the singular ellipsis object and slice objects. |
| 3837 | 5 |
| 6 Guido, feel free to do whatever you want in the way of copyrights | |
| 7 for this file. | |
| 8 */ | |
| 9 | |
| 10 /* | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
11 Py_Ellipsis encodes the '...' rubber index token. It is similar to |
| 3837 | 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 | |
| 18 static PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
19 ellipsis_repr(PyObject *op) |
| 3837 | 20 { |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
21 return PyString_FromString("Ellipsis"); |
| 3837 | 22 } |
| 23 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
24 static PyTypeObject PyEllipsis_Type = { |
| 3837 | 25 PyObject_HEAD_INIT(&PyType_Type) |
| 26 0, | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
27 "ellipsis", |
| 3837 | 28 0, |
| 29 0, | |
| 30 0, /*tp_dealloc*/ /*never called*/ | |
| 31 0, /*tp_print*/ | |
| 32 0, /*tp_getattr*/ | |
| 33 0, /*tp_setattr*/ | |
| 34 0, /*tp_compare*/ | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
35 (reprfunc)ellipsis_repr, /*tp_repr*/ |
| 3837 | 36 0, /*tp_as_number*/ |
| 37 0, /*tp_as_sequence*/ | |
| 38 0, /*tp_as_mapping*/ | |
| 39 0, /*tp_hash */ | |
| 40 }; | |
| 41 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
42 PyObject _Py_EllipsisObject = { |
|
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
43 PyObject_HEAD_INIT(&PyEllipsis_Type) |
| 3837 | 44 }; |
| 45 | |
| 46 | |
| 47 /* Slice object implementation | |
| 48 | |
| 49 start, stop, and step are python objects with None indicating no | |
| 50 index is present. | |
| 51 */ | |
| 52 | |
| 53 PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
54 PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
| 3837 | 55 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
4403
diff
changeset
|
56 PySliceObject *obj = PyObject_NEW(PySliceObject, &PySlice_Type); |
| 3837 | 57 |
|
15563
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
58 if (obj == NULL) |
|
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
59 return NULL; |
|
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
60 |
| 3837 | 61 if (step == NULL) step = Py_None; |
| 62 Py_INCREF(step); | |
| 63 if (start == NULL) start = Py_None; | |
| 64 Py_INCREF(start); | |
| 65 if (stop == NULL) stop = Py_None; | |
| 66 Py_INCREF(stop); | |
| 67 | |
| 68 obj->step = step; | |
| 69 obj->start = start; | |
| 70 obj->stop = stop; | |
| 71 | |
| 72 return (PyObject *) obj; | |
| 73 } | |
| 74 | |
| 75 int | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
76 PySlice_GetIndices(PySliceObject *r, int length, |
|
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
77 int *start, int *stop, int *step) |
| 3837 | 78 { |
| 79 if (r->step == Py_None) { | |
| 80 *step = 1; | |
| 81 } else { | |
| 82 if (!PyInt_Check(r->step)) return -1; | |
| 83 *step = PyInt_AsLong(r->step); | |
| 84 } | |
| 85 if (r->start == Py_None) { | |
| 86 *start = *step < 0 ? length-1 : 0; | |
| 87 } else { | |
| 88 if (!PyInt_Check(r->start)) return -1; | |
| 89 *start = PyInt_AsLong(r->start); | |
| 90 if (*start < 0) *start += length; | |
| 91 } | |
| 92 if (r->stop == Py_None) { | |
| 93 *stop = *step < 0 ? -1 : length; | |
| 94 } else { | |
| 95 if (!PyInt_Check(r->stop)) return -1; | |
| 96 *stop = PyInt_AsLong(r->stop); | |
| 97 if (*stop < 0) *stop += length; | |
| 98 } | |
| 99 if (*stop > length) return -1; | |
| 100 if (*start >= length) return -1; | |
| 101 if (*step == 0) return -1; | |
| 102 return 0; | |
| 103 } | |
| 104 | |
| 105 static void | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
106 slice_dealloc(PySliceObject *r) |
| 3837 | 107 { |
| 108 Py_DECREF(r->step); | |
| 109 Py_DECREF(r->start); | |
| 110 Py_DECREF(r->stop); | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
4403
diff
changeset
|
111 PyObject_DEL(r); |
| 3837 | 112 } |
| 113 | |
| 114 static PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
115 slice_repr(PySliceObject *r) |
| 3837 | 116 { |
| 117 PyObject *s, *comma; | |
| 118 | |
| 119 s = PyString_FromString("slice("); | |
| 120 comma = PyString_FromString(", "); | |
| 121 PyString_ConcatAndDel(&s, PyObject_Repr(r->start)); | |
| 122 PyString_Concat(&s, comma); | |
| 123 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop)); | |
| 124 PyString_Concat(&s, comma); | |
| 125 PyString_ConcatAndDel(&s, PyObject_Repr(r->step)); | |
| 126 PyString_ConcatAndDel(&s, PyString_FromString(")")); | |
| 127 Py_DECREF(comma); | |
| 128 return s; | |
| 129 } | |
| 130 | |
| 131 | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
132 static PyObject *slice_getattr(PySliceObject *self, char *name) |
| 3837 | 133 { |
| 134 PyObject *ret; | |
| 135 | |
| 136 ret = NULL; | |
| 137 if (strcmp(name, "start") == 0) { | |
| 138 ret = self->start; | |
| 139 } | |
| 140 else if (strcmp(name, "stop") == 0) { | |
| 141 ret = self->stop; | |
| 142 } | |
| 143 else if (strcmp(name, "step") == 0) { | |
| 144 ret = self->step; | |
| 145 } | |
| 146 else if (strcmp(name, "__members__") == 0) { | |
| 147 return Py_BuildValue("[sss]", | |
| 148 "start", "stop", "step"); | |
| 149 } | |
| 150 else { | |
| 151 PyErr_SetString(PyExc_AttributeError, name); | |
| 152 return NULL; | |
| 153 } | |
| 154 Py_INCREF(ret); | |
| 155 return ret; | |
| 156 } | |
| 157 | |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
158 static int |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
159 slice_compare(PySliceObject *v, PySliceObject *w) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
160 { |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
161 int result = 0; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
162 |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
163 if (v == w) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
164 return 0; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
165 |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
166 if (PyObject_Cmp(v->start, w->start, &result) < 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
167 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
168 if (result != 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
169 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
170 if (PyObject_Cmp(v->stop, w->stop, &result) < 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
171 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
172 if (result != 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
173 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
174 if (PyObject_Cmp(v->step, w->step, &result) < 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
175 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
176 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
177 } |
| 3837 | 178 |
| 179 PyTypeObject PySlice_Type = { | |
| 180 PyObject_HEAD_INIT(&PyType_Type) | |
| 181 0, /* Number of items for varobject */ | |
| 182 "slice", /* Name of this type */ | |
| 183 sizeof(PySliceObject), /* Basic object size */ | |
| 184 0, /* Item size for varobject */ | |
| 185 (destructor)slice_dealloc, /*tp_dealloc*/ | |
| 186 0, /*tp_print*/ | |
| 187 (getattrfunc)slice_getattr, /*tp_getattr*/ | |
| 188 0, /*tp_setattr*/ | |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
189 (cmpfunc)slice_compare, /*tp_compare*/ |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
190 (reprfunc)slice_repr, /*tp_repr*/ |
| 3837 | 191 0, /*tp_as_number*/ |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
192 0, /*tp_as_sequence*/ |
| 3837 | 193 0, /*tp_as_mapping*/ |
| 194 }; |
