annotate cos/python/Objects/tupleobject.c @ 27:7f74363f4c82

Added some files for the python port
author windel
date Tue, 27 Dec 2011 18:59:02 +0100
parents
children
rev   line source
27
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2 /* Tuple object implementation */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4 #include "Python.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
5
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
6 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
7 PyTuple_New(register Py_ssize_t size)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
8 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
9 register PyTupleObject *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
10 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
11 if (size < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
12 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
13 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
14 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
15 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
16 Py_ssize_t nbytes = size * sizeof(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
17 /* Check for overflow */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
18 if (nbytes / sizeof(PyObject *) != (size_t)size ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
19 (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
20 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
21 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
22 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
23 /* nbytes += sizeof(PyTupleObject) - sizeof(PyObject *); */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
24
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
25 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
26 if (op == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
27 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
28 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
29 for (i=0; i < size; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
30 op->ob_item[i] = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
31 _PyObject_GC_TRACK(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
32 return (PyObject *) op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
33 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
34
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
35 Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
36 PyTuple_Size(register PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
37 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
38 if (!PyTuple_Check(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
39 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
40 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
41 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
42 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
43 return Py_SIZE(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
44 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
45
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
46 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
47 PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
48 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
49 if (!PyTuple_Check(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
50 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
51 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
52 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
53 if (i < 0 || i >= Py_SIZE(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
54 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
55 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
56 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
57 return ((PyTupleObject *)op) -> ob_item[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
58 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
59
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
60 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
61 PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
62 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
63 register PyObject *olditem;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
64 register PyObject **p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
65 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
66 Py_XDECREF(newitem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
67 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
68 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
69 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
70 if (i < 0 || i >= Py_SIZE(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
71 Py_XDECREF(newitem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
72 PyErr_SetString(PyExc_IndexError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
73 "tuple assignment index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
74 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
75 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
76 p = ((PyTupleObject *)op) -> ob_item + i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
77 olditem = *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
78 *p = newitem;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
79 Py_XDECREF(olditem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
80 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
81 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
82
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
83 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
84 _PyTuple_MaybeUntrack(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
85 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
86 PyTupleObject *t;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
87 Py_ssize_t i, n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
88
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
89 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
90 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
91 t = (PyTupleObject *) op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
92 n = Py_SIZE(t);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
93 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
94 PyObject *elt = PyTuple_GET_ITEM(t, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
95 /* Tuple with NULL elements aren't
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
96 fully constructed, don't untrack
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
97 them yet. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
98 if (!elt ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
99 _PyObject_GC_MAY_BE_TRACKED(elt))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
100 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
101 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
102 _PyObject_GC_UNTRACK(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
103 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
104
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
105 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
106 PyTuple_Pack(Py_ssize_t n, ...)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
107 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
108 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
109 PyObject *o;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
110 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
111 PyObject **items;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
112 va_list vargs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
113
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
114 va_start(vargs, n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
115 result = PyTuple_New(n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
116 if (result == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
117 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
118 items = ((PyTupleObject *)result)->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
119 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
120 o = va_arg(vargs, PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
121 Py_INCREF(o);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
122 items[i] = o;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
123 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
124 va_end(vargs);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
125 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
126 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
127
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
128
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
129 /* Methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
130
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
131 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
132 tupledealloc(register PyTupleObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
133 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
134 register Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
135 register Py_ssize_t len = Py_SIZE(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
136 PyObject_GC_UnTrack(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
137 Py_TRASHCAN_SAFE_BEGIN(op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
138 if (len > 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
139 i = len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
140 while (--i >= 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
141 Py_XDECREF(op->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
142 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
143 Py_TYPE(op)->tp_free((PyObject *)op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
144 done:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
145 Py_TRASHCAN_SAFE_END(op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
146 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
147
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
148 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
149 tuplerepr(PyTupleObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
150 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
151 Py_ssize_t i, n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
152 PyObject *s = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
153 _PyAccu acc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
154 static PyObject *sep = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
155
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
156 n = Py_SIZE(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
157 if (n == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
158 return PyUnicode_FromString("()");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
159
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
160 if (sep == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
161 sep = PyUnicode_FromString(", ");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
162 if (sep == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
163 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
164 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
165
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
166 /* While not mutable, it is still possible to end up with a cycle in a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
167 tuple through an object that stores itself within a tuple (and thus
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
168 infinitely asks for the repr of itself). This should only be
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
169 possible within a type. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
170 i = Py_ReprEnter((PyObject *)v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
171 if (i != 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
172 return i > 0 ? PyUnicode_FromString("(...)") : NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
173 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
174
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
175 if (_PyAccu_Init(&acc))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
176 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
177
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
178 s = PyUnicode_FromString("(");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
179 if (s == NULL || _PyAccu_Accumulate(&acc, s))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
180 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
181 Py_CLEAR(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
182
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
183 /* Do repr() on each element. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
184 for (i = 0; i < n; ++i) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
185 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
186 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
187 s = PyObject_Repr(v->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
188 Py_LeaveRecursiveCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
189 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
190 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
191 if (s == NULL || _PyAccu_Accumulate(&acc, s))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
192 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
193 Py_CLEAR(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
194 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
195 if (n > 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
196 s = PyUnicode_FromString(")");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
197 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
198 s = PyUnicode_FromString(",)");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
199 if (s == NULL || _PyAccu_Accumulate(&acc, s))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
200 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
201 Py_CLEAR(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
202
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
203 Py_ReprLeave((PyObject *)v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
204 return _PyAccu_Finish(&acc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
205
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
206 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
207 _PyAccu_Destroy(&acc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
208 Py_XDECREF(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
209 Py_ReprLeave((PyObject *)v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
210 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
211 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
212
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
213 /* The addend 82520, was selected from the range(0, 1000000) for
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
214 generating the greatest number of prime multipliers for tuples
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
215 upto length eight:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
216
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
217 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
218 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
219 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
220
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
221 static Py_hash_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
222 tuplehash(PyTupleObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
223 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
224 register Py_uhash_t x;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
225 register Py_hash_t y;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
226 register Py_ssize_t len = Py_SIZE(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
227 register PyObject **p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
228 Py_uhash_t mult = 1000003;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
229 x = 0x345678;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
230 p = v->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
231 while (--len >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
232 y = PyObject_Hash(*p++);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
233 if (y == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
234 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
235 x = (x ^ y) * mult;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
236 /* the cast might truncate len; that doesn't change hash stability */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
237 mult += (Py_hash_t)(82520L + len + len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
238 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
239 x += 97531L;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
240 if (x == (Py_uhash_t)-1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
241 x = -2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
242 return x;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
243 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
244
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
245 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
246 tuplelength(PyTupleObject *a)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
247 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
248 return Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
249 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
250
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
251 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
252 tuplecontains(PyTupleObject *a, PyObject *el)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
253 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
254 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
255 int cmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
256
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
257 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
258 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
259 Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
260 return cmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
261 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
262
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
263 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
264 tupleitem(register PyTupleObject *a, register Py_ssize_t i)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
265 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
266 if (i < 0 || i >= Py_SIZE(a)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
267 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
268 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
269 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
270 Py_INCREF(a->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
271 return a->ob_item[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
272 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
273
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
274 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
275 tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
276 register Py_ssize_t ihigh)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
277 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
278 register PyTupleObject *np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
279 PyObject **src, **dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
280 register Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
281 Py_ssize_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
282 if (ilow < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
283 ilow = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
284 if (ihigh > Py_SIZE(a))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
285 ihigh = Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
286 if (ihigh < ilow)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
287 ihigh = ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
288 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
289 Py_INCREF(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
290 return (PyObject *)a;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
291 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
292 len = ihigh - ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
293 np = (PyTupleObject *)PyTuple_New(len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
294 if (np == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
295 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
296 src = a->ob_item + ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
297 dest = np->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
298 for (i = 0; i < len; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
299 PyObject *v = src[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
300 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
301 dest[i] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
302 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
303 return (PyObject *)np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
304 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
305
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
306 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
307 PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
308 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
309 if (op == NULL || !PyTuple_Check(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
310 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
311 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
312 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
313 return tupleslice((PyTupleObject *)op, i, j);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
314 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
315
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
316 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
317 tupleconcat(register PyTupleObject *a, register PyObject *bb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
318 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
319 register Py_ssize_t size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
320 register Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
321 PyObject **src, **dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
322 PyTupleObject *np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
323 if (!PyTuple_Check(bb)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
324 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
325 "can only concatenate tuple (not \"%.200s\") to tuple",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
326 Py_TYPE(bb)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
327 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
328 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
329 #define b ((PyTupleObject *)bb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
330 size = Py_SIZE(a) + Py_SIZE(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
331 if (size < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
332 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
333 np = (PyTupleObject *) PyTuple_New(size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
334 if (np == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
335 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
336 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
337 src = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
338 dest = np->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
339 for (i = 0; i < Py_SIZE(a); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
340 PyObject *v = src[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
341 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
342 dest[i] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
343 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
344 src = b->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
345 dest = np->ob_item + Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
346 for (i = 0; i < Py_SIZE(b); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
347 PyObject *v = src[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
348 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
349 dest[i] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
350 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
351 return (PyObject *)np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
352 #undef b
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
353 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
354
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
355 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
356 tuplerepeat(PyTupleObject *a, Py_ssize_t n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
357 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
358 Py_ssize_t i, j;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
359 Py_ssize_t size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
360 PyTupleObject *np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
361 PyObject **p, **items;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
362 if (n < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
363 n = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
364 if (Py_SIZE(a) == 0 || n == 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
365 if (PyTuple_CheckExact(a)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
366 /* Since tuples are immutable, we can return a shared
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
367 copy in this case */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
368 Py_INCREF(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
369 return (PyObject *)a;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
370 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
371 if (Py_SIZE(a) == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
372 return PyTuple_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
373 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
374 size = Py_SIZE(a) * n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
375 if (size/Py_SIZE(a) != n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
376 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
377 np = (PyTupleObject *) PyTuple_New(size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
378 if (np == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
379 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
380 p = np->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
381 items = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
382 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
383 for (j = 0; j < Py_SIZE(a); j++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
384 *p = items[j];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
385 Py_INCREF(*p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
386 p++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
387 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
388 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
389 return (PyObject *) np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
390 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
391
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
392 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
393 tupleindex(PyTupleObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
394 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
395 Py_ssize_t i, start=0, stop=Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
396 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
397
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
398 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
399 _PyEval_SliceIndex, &start,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
400 _PyEval_SliceIndex, &stop))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
401 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
402 if (start < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
403 start += Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
404 if (start < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
405 start = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
406 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
407 if (stop < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
408 stop += Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
409 if (stop < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
410 stop = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
411 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
412 for (i = start; i < stop && i < Py_SIZE(self); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
413 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
414 if (cmp > 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
415 return PyLong_FromSsize_t(i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
416 else if (cmp < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
417 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
418 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
419 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
420 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
421 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
422
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
423 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
424 tuplecount(PyTupleObject *self, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
425 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
426 Py_ssize_t count = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
427 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
428
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
429 for (i = 0; i < Py_SIZE(self); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
430 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
431 if (cmp > 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
432 count++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
433 else if (cmp < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
434 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
435 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
436 return PyLong_FromSsize_t(count);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
437 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
438
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
439 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
440 tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
441 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
442 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
443
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
444 for (i = Py_SIZE(o); --i >= 0; )
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
445 Py_VISIT(o->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
446 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
447 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
448
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
449 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
450 tuplerichcompare(PyObject *v, PyObject *w, int op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
451 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
452 PyTupleObject *vt, *wt;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
453 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
454 Py_ssize_t vlen, wlen;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
455
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
456 if (!PyTuple_Check(v) || !PyTuple_Check(w))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
457 Py_RETURN_NOTIMPLEMENTED;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
458
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
459 vt = (PyTupleObject *)v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
460 wt = (PyTupleObject *)w;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
461
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
462 vlen = Py_SIZE(vt);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
463 wlen = Py_SIZE(wt);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
464
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
465 /* Note: the corresponding code for lists has an "early out" test
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
466 * here when op is EQ or NE and the lengths differ. That pays there,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
467 * but Tim was unable to find any real code where EQ/NE tuple
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
468 * compares don't have the same length, so testing for it here would
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
469 * have cost without benefit.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
470 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
471
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
472 /* Search for the first index where items are different.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
473 * Note that because tuples are immutable, it's safe to reuse
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
474 * vlen and wlen across the comparison calls.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
475 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
476 for (i = 0; i < vlen && i < wlen; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
477 int k = PyObject_RichCompareBool(vt->ob_item[i],
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
478 wt->ob_item[i], Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
479 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
480 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
481 if (!k)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
482 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
483 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
484
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
485 if (i >= vlen || i >= wlen) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
486 /* No more items to compare -- compare sizes */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
487 int cmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
488 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
489 switch (op) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
490 case Py_LT: cmp = vlen < wlen; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
491 case Py_LE: cmp = vlen <= wlen; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
492 case Py_EQ: cmp = vlen == wlen; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
493 case Py_NE: cmp = vlen != wlen; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
494 case Py_GT: cmp = vlen > wlen; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
495 case Py_GE: cmp = vlen >= wlen; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
496 default: return NULL; /* cannot happen */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
497 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
498 if (cmp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
499 res = Py_True;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
500 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
501 res = Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
502 Py_INCREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
503 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
504 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
505
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
506 /* We have an item that differs -- shortcuts for EQ/NE */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
507 if (op == Py_EQ) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
508 Py_INCREF(Py_False);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
509 return Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
510 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
511 if (op == Py_NE) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
512 Py_INCREF(Py_True);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
513 return Py_True;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
514 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
515
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
516 /* Compare the final item again using the proper operator */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
517 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
518 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
519
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
520 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
521 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
522
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
523 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
524 tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
525 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
526 PyObject *arg = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
527 static char *kwlist[] = {"sequence", 0};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
528
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
529 if (type != &PyTuple_Type)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
530 return tuple_subtype_new(type, args, kwds);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
531 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
532 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
533
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
534 if (arg == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
535 return PyTuple_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
536 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
537 return PySequence_Tuple(arg);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
538 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
539
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
540 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
541 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
542 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
543 PyObject *tmp, *newobj, *item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
544 Py_ssize_t i, n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
545
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
546 assert(PyType_IsSubtype(type, &PyTuple_Type));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
547 tmp = tuple_new(&PyTuple_Type, args, kwds);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
548 if (tmp == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
549 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
550 assert(PyTuple_Check(tmp));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
551 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
552 if (newobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
553 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
554 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
555 item = PyTuple_GET_ITEM(tmp, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
556 Py_INCREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
557 PyTuple_SET_ITEM(newobj, i, item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
558 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
559 Py_DECREF(tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
560 return newobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
561 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
562
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
563 PyDoc_STRVAR(tuple_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
564 "tuple() -> empty tuple\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
565 tuple(iterable) -> tuple initialized from iterable's items\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
566 \n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
567 If the argument is a tuple, the return value is the same object.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
568
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
569 static PySequenceMethods tuple_as_sequence = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
570 (lenfunc)tuplelength, /* sq_length */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
571 (binaryfunc)tupleconcat, /* sq_concat */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
572 (ssizeargfunc)tuplerepeat, /* sq_repeat */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
573 (ssizeargfunc)tupleitem, /* sq_item */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
574 0, /* sq_slice */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
575 0, /* sq_ass_item */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
576 0, /* sq_ass_slice */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
577 (objobjproc)tuplecontains, /* sq_contains */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
578 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
579
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
580 static PyObject*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
581 tuplesubscript(PyTupleObject* self, PyObject* item)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
582 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
583 if (PyIndex_Check(item)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
584 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
585 if (i == -1 && PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
586 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
587 if (i < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
588 i += PyTuple_GET_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
589 return tupleitem(self, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
590 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
591 else if (PySlice_Check(item)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
592 Py_ssize_t start, stop, step, slicelength, cur, i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
593 PyObject* result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
594 PyObject* it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
595 PyObject **src, **dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
596
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
597 if (PySlice_GetIndicesEx(item,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
598 PyTuple_GET_SIZE(self),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
599 &start, &stop, &step, &slicelength) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
600 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
601 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
602
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
603 if (slicelength <= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
604 return PyTuple_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
605 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
606 else if (start == 0 && step == 1 &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
607 slicelength == PyTuple_GET_SIZE(self) &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
608 PyTuple_CheckExact(self)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
609 Py_INCREF(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
610 return (PyObject *)self;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
611 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
612 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
613 result = PyTuple_New(slicelength);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
614 if (!result) return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
615
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
616 src = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
617 dest = ((PyTupleObject *)result)->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
618 for (cur = start, i = 0; i < slicelength;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
619 cur += step, i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
620 it = src[cur];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
621 Py_INCREF(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
622 dest[i] = it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
623 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
624
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
625 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
626 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
627 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
628 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
629 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
630 "tuple indices must be integers, not %.200s",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
631 Py_TYPE(item)->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
632 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
633 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
634 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
635
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
636 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
637 tuple_getnewargs(PyTupleObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
638 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
639 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
640
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
641 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
642
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
643 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
644 tuple_sizeof(PyTupleObject *self)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
645 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
646 Py_ssize_t res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
647
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
648 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
649 return PyLong_FromSsize_t(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
650 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
651
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
652 PyDoc_STRVAR(index_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
653 "T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
654 "Raises ValueError if the value is not present."
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
655 );
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
656 PyDoc_STRVAR(count_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
657 "T.count(value) -> integer -- return number of occurrences of value");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
658 PyDoc_STRVAR(sizeof_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
659 "T.__sizeof__() -- size of T in memory, in bytes");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
660
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
661 static PyMethodDef tuple_methods[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
662 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
663 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
664 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
665 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
666 {NULL, NULL} /* sentinel */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
667 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
668
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
669 static PyMappingMethods tuple_as_mapping = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
670 (lenfunc)tuplelength,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
671 (binaryfunc)tuplesubscript,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
672 0
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
673 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
674
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
675 static PyObject *tuple_iter(PyObject *seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
676
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
677 PyTypeObject PyTuple_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
678 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
679 "tuple",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
680 sizeof(PyTupleObject) - sizeof(PyObject *),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
681 sizeof(PyObject *),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
682 (destructor)tupledealloc, /* tp_dealloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
683 0, /* tp_print */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
684 0, /* tp_getattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
685 0, /* tp_setattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
686 0, /* tp_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
687 (reprfunc)tuplerepr, /* tp_repr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
688 0, /* tp_as_number */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
689 &tuple_as_sequence, /* tp_as_sequence */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
690 &tuple_as_mapping, /* tp_as_mapping */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
691 (hashfunc)tuplehash, /* tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
692 0, /* tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
693 0, /* tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
694 PyObject_GenericGetAttr, /* tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
695 0, /* tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
696 0, /* tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
697 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
698 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
699 tuple_doc, /* tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
700 (traverseproc)tupletraverse, /* tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
701 0, /* tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
702 tuplerichcompare, /* tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
703 0, /* tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
704 tuple_iter, /* tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
705 0, /* tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
706 tuple_methods, /* tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
707 0, /* tp_members */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
708 0, /* tp_getset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
709 0, /* tp_base */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
710 0, /* tp_dict */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
711 0, /* tp_descr_get */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
712 0, /* tp_descr_set */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
713 0, /* tp_dictoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
714 0, /* tp_init */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
715 0, /* tp_alloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
716 tuple_new, /* tp_new */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
717 PyObject_GC_Del, /* tp_free */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
718 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
719
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
720 /* The following function breaks the notion that tuples are immutable:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
721 it changes the size of a tuple. We get away with this only if there
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
722 is only one module referencing the object. You can also think of it
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
723 as creating a new tuple object and destroying the old one, only more
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
724 efficiently. In any case, don't use this if the tuple may already be
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
725 known to some other part of the code. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
726
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
727 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
728 _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
729 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
730 register PyTupleObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
731 register PyTupleObject *sv;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
732 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
733 Py_ssize_t oldsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
734
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
735 v = (PyTupleObject *) *pv;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
736 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
737 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
738 *pv = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
739 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
740 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
741 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
742 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
743 oldsize = Py_SIZE(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
744 if (oldsize == newsize)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
745 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
746
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
747 if (oldsize == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
748 /* Empty tuples are often shared, so we should never
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
749 resize them in-place even if we do own the only
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
750 (current) reference */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
751 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
752 *pv = PyTuple_New(newsize);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
753 return *pv == NULL ? -1 : 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
754 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
755
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
756 /* XXX UNREF/NEWREF interface should be more symmetrical */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
757 _Py_DEC_REFTOTAL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
758 if (_PyObject_GC_IS_TRACKED(v))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
759 _PyObject_GC_UNTRACK(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
760 _Py_ForgetReference((PyObject *) v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
761 /* DECREF items deleted by shrinkage */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
762 for (i = newsize; i < oldsize; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
763 Py_XDECREF(v->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
764 v->ob_item[i] = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
765 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
766 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
767 if (sv == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
768 *pv = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
769 PyObject_GC_Del(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
770 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
771 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
772 _Py_NewReference((PyObject *) sv);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
773 /* Zero out items added by growing */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
774 if (newsize > oldsize)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
775 memset(&sv->ob_item[oldsize], 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
776 sizeof(*sv->ob_item) * (newsize - oldsize));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
777 *pv = (PyObject *) sv;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
778 _PyObject_GC_TRACK(sv);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
779 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
780 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
781
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
782 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
783 PyTuple_ClearFreeList(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
784 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
785 int freelist_size = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
786 return freelist_size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
787 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
788
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
789 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
790 PyTuple_Fini(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
791 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
792 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
793
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
794 /*********************** Tuple Iterator **************************/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
795
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
796 typedef struct {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
797 PyObject_HEAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
798 long it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
799 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
800 } tupleiterobject;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
801
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
802 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
803 tupleiter_dealloc(tupleiterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
804 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
805 _PyObject_GC_UNTRACK(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
806 Py_XDECREF(it->it_seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
807 PyObject_GC_Del(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
808 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
809
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
810 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
811 tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
812 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
813 Py_VISIT(it->it_seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
814 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
815 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
816
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
817 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
818 tupleiter_next(tupleiterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
819 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
820 PyTupleObject *seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
821 PyObject *item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
822
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
823 assert(it != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
824 seq = it->it_seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
825 if (seq == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
826 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
827 assert(PyTuple_Check(seq));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
828
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
829 if (it->it_index < PyTuple_GET_SIZE(seq)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
830 item = PyTuple_GET_ITEM(seq, it->it_index);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
831 ++it->it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
832 Py_INCREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
833 return item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
834 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
835
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
836 Py_DECREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
837 it->it_seq = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
838 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
839 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
840
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
841 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
842 tupleiter_len(tupleiterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
843 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
844 Py_ssize_t len = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
845 if (it->it_seq)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
846 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
847 return PyLong_FromSsize_t(len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
848 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
849
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
850 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
851
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
852 static PyMethodDef tupleiter_methods[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
853 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
854 {NULL, NULL} /* sentinel */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
855 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
856
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
857 PyTypeObject PyTupleIter_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
858 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
859 "tuple_iterator", /* tp_name */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
860 sizeof(tupleiterobject), /* tp_basicsize */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
861 0, /* tp_itemsize */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
862 /* methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
863 (destructor)tupleiter_dealloc, /* tp_dealloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
864 0, /* tp_print */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
865 0, /* tp_getattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
866 0, /* tp_setattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
867 0, /* tp_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
868 0, /* tp_repr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
869 0, /* tp_as_number */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
870 0, /* tp_as_sequence */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
871 0, /* tp_as_mapping */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
872 0, /* tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
873 0, /* tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
874 0, /* tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
875 PyObject_GenericGetAttr, /* tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
876 0, /* tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
877 0, /* tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
878 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
879 0, /* tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
880 (traverseproc)tupleiter_traverse, /* tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
881 0, /* tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
882 0, /* tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
883 0, /* tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
884 PyObject_SelfIter, /* tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
885 (iternextfunc)tupleiter_next, /* tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
886 tupleiter_methods, /* tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
887 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
888 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
889
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
890 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
891 tuple_iter(PyObject *seq)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
892 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
893 tupleiterobject *it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
894
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
895 if (!PyTuple_Check(seq)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
896 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
897 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
898 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
899 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
900 if (it == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
901 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
902 it->it_index = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
903 Py_INCREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
904 it->it_seq = (PyTupleObject *)seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
905 _PyObject_GC_TRACK(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
906 return (PyObject *)it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
907 }