Mercurial > cpython-lru_cache-weakref
annotate Objects/sliceobject.c @ 16413:8819867f14ef legacy-trunk
Patch derived from Trent's 101162: a Python/C API testing framework.
STILL NEEDS UNIX BUILD CHANGES.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Fri, 02 Feb 2001 05:57:15 +0000 |
| parents | 35e8a29e5488 |
| children | 52cb0c5fdf23 |
| 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 | |
| 158 | |
| 159 PyTypeObject PySlice_Type = { | |
| 160 PyObject_HEAD_INIT(&PyType_Type) | |
| 161 0, /* Number of items for varobject */ | |
| 162 "slice", /* Name of this type */ | |
| 163 sizeof(PySliceObject), /* Basic object size */ | |
| 164 0, /* Item size for varobject */ | |
| 165 (destructor)slice_dealloc, /*tp_dealloc*/ | |
| 166 0, /*tp_print*/ | |
| 167 (getattrfunc)slice_getattr, /*tp_getattr*/ | |
| 168 0, /*tp_setattr*/ | |
| 169 0, /*tp_compare*/ | |
| 170 (reprfunc)slice_repr, /*tp_repr*/ | |
| 171 0, /*tp_as_number*/ | |
| 172 0, /*tp_as_sequence*/ | |
| 173 0, /*tp_as_mapping*/ | |
| 174 }; |
