blob: 2ab9b52055b09b4130a29fe5505cd5ef8bf29281 [file] [log] [blame]
Guido van Rossume82f75a2001-10-18 20:47:51 +00001/* Implementation helper: a struct that looks like a tuple. See timemodule
2 and posixmodule for example uses. */
3
4#include "Python.h"
5#include "structmember.h"
6#include "structseq.h"
7
8static char visible_length_key[] = "n_sequence_fields";
9static char real_length_key[] = "n_fields";
10
11#define VISIBLE_SIZE(op) ((op)->ob_size)
12#define VISIBLE_SIZE_TP(tp) PyInt_AsLong( \
13 PyDict_GetItemString((tp)->tp_dict, visible_length_key))
14
15#define REAL_SIZE_TP(tp) PyInt_AsLong( \
16 PyDict_GetItemString((tp)->tp_dict, real_length_key))
17#define REAL_SIZE(op) REAL_SIZE_TP((op)->ob_type)
18
19
20PyObject *
21PyStructSequence_New(PyTypeObject *type)
22{
23 PyStructSequence *obj;
24
25 obj = PyObject_New(PyStructSequence, type);
26 obj->ob_size = VISIBLE_SIZE_TP(type);
27
28 return (PyObject*) obj;
29}
30
31static void
32structseq_dealloc(PyStructSequence *obj)
33{
34 int i, size;
35
36 size = REAL_SIZE(obj);
37 for (i = 0; i < size; ++i) {
38 Py_XDECREF(obj->ob_item[i]);
39 }
40 PyObject_FREE(obj);
41}
42
43static int
44structseq_length(PyStructSequence *obj)
45{
46 return VISIBLE_SIZE(obj);
47}
48
49static PyObject*
50structseq_item(PyStructSequence *obj, int i)
51{
52 if (i < 0 || i >= VISIBLE_SIZE(obj)) {
53 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
54 return NULL;
55 }
56 Py_INCREF(obj->ob_item[i]);
57 return obj->ob_item[i];
58}
59
60static PyObject*
61structseq_slice(PyStructSequence *obj, int low, int high)
62{
63 PyTupleObject *np;
64 int i;
65
66 if (low < 0)
67 low = 0;
68 if (high > VISIBLE_SIZE(obj))
69 high = VISIBLE_SIZE(obj);
70 if (high < low)
71 high = low;
72 np = (PyTupleObject *)PyTuple_New(high-low);
73 if (np == NULL)
74 return NULL;
75 for(i = low; i < high; ++i) {
76 PyObject *v = obj->ob_item[i];
77 Py_INCREF(v);
Tim Petersc2fe6182001-10-30 23:20:46 +000078 PyTuple_SET_ITEM(np, i-low, v);
Guido van Rossume82f75a2001-10-18 20:47:51 +000079 }
80 return (PyObject *) np;
81}
82
83static PyObject *
84structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
85{
86 PyObject *arg = NULL;
87 PyStructSequence *res = NULL;
88 int len, required_len, i;
89 static char *kwlist[] = {"sequence", 0};
90 static char msgbuf[128];
91
92 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:structseq",
93 kwlist, &arg))
94 return NULL;
95
96 if (!PySequence_Check(arg)) {
97 PyErr_SetString(PyExc_TypeError,
98 "constructor requires a sequence");
99 return NULL;
100 }
101
102 len = PySequence_Length(arg);
103 required_len = REAL_SIZE_TP(type);
104 if (len != required_len) {
Barry Warsaw312af422001-11-28 20:56:44 +0000105 PyOS_snprintf(
106 msgbuf, sizeof(msgbuf),
Guido van Rossume82f75a2001-10-18 20:47:51 +0000107 "constructor takes exactly %d arguments (%d given)",
108 required_len,
109 len);
110 PyErr_SetString(PyExc_TypeError, msgbuf);
111 return NULL;
112 }
113
114 res = (PyStructSequence*) PyStructSequence_New(type);
115 for (i = 0; i < len; ++i) {
116 /* INCREF???? XXXX */
117 res->ob_item[i] = PySequence_GetItem(arg, i);
118 }
119
120 return (PyObject*) res;
121}
122
123static PyObject *
124make_tuple(PyStructSequence *obj)
125{
126 return structseq_slice(obj, 0, VISIBLE_SIZE(obj));
127}
128
129static PyObject *
130structseq_repr(PyStructSequence *obj)
131{
132 PyObject *tup, *str;
133 tup = make_tuple(obj);
134 str = PyObject_Repr(tup);
135 Py_DECREF(tup);
136 return str;
137}
138
139static PyObject *
140structseq_concat(PyStructSequence *obj, PyObject *b)
141{
142 PyObject *tup, *result;
143 tup = make_tuple(obj);
144 result = PySequence_Concat(tup, b);
145 Py_DECREF(tup);
146 return result;
147}
148
149static PyObject *
150structseq_repeat(PyStructSequence *obj, int n)
151{
152 PyObject *tup, *result;
153 tup = make_tuple(obj);
154 result = PySequence_Repeat(tup, n);
155 Py_DECREF(tup);
156 return result;
157}
158
159static int
160structseq_contains(PyStructSequence *obj, PyObject *o)
161{
162 PyObject *tup;
163 int result;
164 tup = make_tuple(obj);
165 result = PySequence_Contains(tup, o);
166 Py_DECREF(tup);
167 return result;
168}
169
170static long
171structseq_hash(PyObject *obj)
172{
173 PyObject *tup;
174 long result;
175 tup = make_tuple((PyStructSequence*) obj);
176 result = PyObject_Hash(tup);
177 Py_DECREF(tup);
178 return result;
179}
180
181static PyObject *
182structseq_richcompare(PyObject *obj, PyObject *o2, int op)
183{
184 PyObject *tup, *result;
185 tup = make_tuple((PyStructSequence*) obj);
186 result = PyObject_RichCompare(tup, o2, op);
187 Py_DECREF(tup);
188 return result;
189}
190
191static PySequenceMethods structseq_as_sequence = {
192 (inquiry)structseq_length,
193 (binaryfunc)structseq_concat, /* sq_concat */
194 (intargfunc)structseq_repeat, /* sq_repeat */
195 (intargfunc)structseq_item, /* sq_item */
196 (intintargfunc)structseq_slice, /* sq_slice */
197 0, /* sq_ass_item */
198 0, /* sq_ass_slice */
199 (objobjproc)structseq_contains, /* sq_contains */
200};
201
202static PyTypeObject _struct_sequence_template = {
203 PyObject_HEAD_INIT(&PyType_Type)
204 0, /* ob_size */
205 NULL, /* tp_name */
206 0, /* tp_basicsize */
207 0, /* tp_itemsize */
208 (destructor)structseq_dealloc, /* tp_dealloc */
209 0, /* tp_print */
210 0, /* tp_getattr */
211 0, /* tp_setattr */
212 0, /* tp_compare */
213 (reprfunc)structseq_repr, /* tp_repr */
214 0, /* tp_as_number */
215 &structseq_as_sequence, /* tp_as_sequence */
216 0, /* tp_as_mapping */
217 (hashfunc)structseq_hash, /* tp_hash */
218 0, /* tp_call */
219 0, /* tp_str */
220 0, /* tp_getattro */
221 0, /* tp_setattro */
222 0, /* tp_as_buffer */
223 Py_TPFLAGS_DEFAULT, /* tp_flags */
224 NULL, /* tp_doc */
225 0, /* tp_traverse */
226 0, /* tp_clear */
227 structseq_richcompare, /* tp_richcompare */
228 0, /* tp_weaklistoffset */
229 0, /* tp_iter */
230 0, /* tp_iternext */
231 0, /* tp_methods */
232 NULL, /* tp_members */
233 0, /* tp_getset */
234 0, /* tp_base */
235 0, /* tp_dict */
236 0, /* tp_descr_get */
237 0, /* tp_descr_set */
238 0, /* tp_dictoffset */
239 0, /* tp_init */
240 0, /* tp_alloc */
241 structseq_new, /* tp_new */
242};
243
244void
245PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
246{
247 PyObject *dict;
248 PyMemberDef* members;
249 int n_members, i;
250
251 for (i = 0; desc->fields[i].name != NULL; ++i)
252 ;
253 n_members = i;
254
255 memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject));
256 type->tp_name = desc->name;
257 type->tp_doc = desc->doc;
258 type->tp_basicsize = sizeof(PyStructSequence)+
259 sizeof(PyObject*)*(n_members-1);
260 type->tp_itemsize = 0;
261
262 members = PyMem_NEW(PyMemberDef, n_members+1);
263
264 for (i = 0; i < n_members; ++i) {
265 members[i].name = desc->fields[i].name;
266 members[i].type = T_OBJECT;
267 members[i].offset = offsetof(PyStructSequence, ob_item)
268 + i * sizeof(PyObject*);
269 members[i].flags = READONLY;
270 members[i].doc = desc->fields[i].doc;
271 }
272 members[n_members].name = NULL;
273
274 type->tp_members = members;
275
276 if (PyType_Ready(type) < 0)
277 return;
278 Py_INCREF(type);
279
280 dict = type->tp_dict;
281 PyDict_SetItemString(dict, visible_length_key,
282 PyInt_FromLong((long) desc->n_in_sequence));
283 PyDict_SetItemString(dict, real_length_key,
284 PyInt_FromLong((long) n_members));
285}