Mercurial > cpython-lru_cache-weakref
annotate Objects/sliceobject.c @ 20737:00a581bdedeb legacy-trunk
When overriding __str__ or __repr__, set the tp_print slot to NULL.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 29 Oct 2001 14:33:44 +0000 |
| parents | 3faabf54b8b8 |
| children | a4bf3c5cc45a |
| 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" | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
17 #include "structmember.h" |
| 3837 | 18 |
| 19 static PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
20 ellipsis_repr(PyObject *op) |
| 3837 | 21 { |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
22 return PyString_FromString("Ellipsis"); |
| 3837 | 23 } |
| 24 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
25 static PyTypeObject PyEllipsis_Type = { |
| 3837 | 26 PyObject_HEAD_INIT(&PyType_Type) |
| 27 0, | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
28 "ellipsis", |
| 3837 | 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*/ | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
36 (reprfunc)ellipsis_repr, /*tp_repr*/ |
| 3837 | 37 0, /*tp_as_number*/ |
| 38 0, /*tp_as_sequence*/ | |
| 39 0, /*tp_as_mapping*/ | |
| 40 0, /*tp_hash */ | |
| 41 }; | |
| 42 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
43 PyObject _Py_EllipsisObject = { |
|
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
44 PyObject_HEAD_INIT(&PyEllipsis_Type) |
| 3837 | 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 * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
55 PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
| 3837 | 56 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
4403
diff
changeset
|
57 PySliceObject *obj = PyObject_NEW(PySliceObject, &PySlice_Type); |
| 3837 | 58 |
|
15563
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
59 if (obj == NULL) |
|
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
60 return NULL; |
|
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
61 |
| 3837 | 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 | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
77 PySlice_GetIndices(PySliceObject *r, int length, |
|
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
78 int *start, int *stop, int *step) |
| 3837 | 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 | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
107 slice_dealloc(PySliceObject *r) |
| 3837 | 108 { |
| 109 Py_DECREF(r->step); | |
| 110 Py_DECREF(r->start); | |
| 111 Py_DECREF(r->stop); | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
4403
diff
changeset
|
112 PyObject_DEL(r); |
| 3837 | 113 } |
| 114 | |
| 115 static PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
116 slice_repr(PySliceObject *r) |
| 3837 | 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 | |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
132 static PyMemberDef slice_members[] = { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
133 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
134 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
135 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
136 {0} |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
137 }; |
| 3837 | 138 |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
139 static int |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
140 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
|
141 { |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
142 int result = 0; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
143 |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
144 if (v == w) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
145 return 0; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
146 |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
147 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
|
148 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
149 if (result != 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
150 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
151 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
|
152 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
153 if (result != 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
154 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
155 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
|
156 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
157 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
158 } |
| 3837 | 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 */ | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
166 (destructor)slice_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
167 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
168 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
169 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
170 (cmpfunc)slice_compare, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
171 (reprfunc)slice_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
172 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
173 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
174 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
175 0, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
176 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
177 0, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
178 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
179 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
180 0, /* tp_as_buffer */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
181 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
182 0, /* tp_doc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
183 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
184 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
185 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
186 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
187 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
188 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
189 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
190 slice_members, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
191 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
192 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
193 0, /* tp_dict */ |
| 3837 | 194 }; |
