blob: 603477f594e87e9e5933ca4204cef15eda4a24fe [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";
Martin v. Löwisceaa77c2002-10-16 19:10:03 +000010static char unnamed_fields_key[] = "n_unnamed_fields";
Guido van Rossume82f75a2001-10-18 20:47:51 +000011
Martin v. Löwisf607bda2002-10-16 18:27:39 +000012/* Fields with this name have only a field index, not a field name.
13 They are only allowed for indices < n_visible_fields. */
14char *PyStructSequence_UnnamedField = "unnamed field";
15
Guido van Rossume82f75a2001-10-18 20:47:51 +000016#define VISIBLE_SIZE(op) ((op)->ob_size)
17#define VISIBLE_SIZE_TP(tp) PyInt_AsLong( \
18 PyDict_GetItemString((tp)->tp_dict, visible_length_key))
19
20#define REAL_SIZE_TP(tp) PyInt_AsLong( \
21 PyDict_GetItemString((tp)->tp_dict, real_length_key))
22#define REAL_SIZE(op) REAL_SIZE_TP((op)->ob_type)
23
Martin v. Löwisceaa77c2002-10-16 19:10:03 +000024#define UNNAMED_FIELDS_TP(tp) PyInt_AsLong( \
25 PyDict_GetItemString((tp)->tp_dict, unnamed_fields_key))
26#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP((op)->ob_type)
27
Guido van Rossume82f75a2001-10-18 20:47:51 +000028
29PyObject *
30PyStructSequence_New(PyTypeObject *type)
31{
32 PyStructSequence *obj;
33
Neil Schemenauer7465ad22002-04-12 03:05:37 +000034 obj = PyObject_New(PyStructSequence, type);
Guido van Rossume82f75a2001-10-18 20:47:51 +000035 obj->ob_size = VISIBLE_SIZE_TP(type);
36
37 return (PyObject*) obj;
38}
39
40static void
41structseq_dealloc(PyStructSequence *obj)
42{
43 int i, size;
44
45 size = REAL_SIZE(obj);
46 for (i = 0; i < size; ++i) {
47 Py_XDECREF(obj->ob_item[i]);
48 }
Neil Schemenauer7465ad22002-04-12 03:05:37 +000049 PyObject_Del(obj);
Guido van Rossume82f75a2001-10-18 20:47:51 +000050}
51
52static int
53structseq_length(PyStructSequence *obj)
54{
55 return VISIBLE_SIZE(obj);
56}
57
58static PyObject*
59structseq_item(PyStructSequence *obj, int i)
60{
61 if (i < 0 || i >= VISIBLE_SIZE(obj)) {
62 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
63 return NULL;
64 }
65 Py_INCREF(obj->ob_item[i]);
66 return obj->ob_item[i];
67}
68
69static PyObject*
70structseq_slice(PyStructSequence *obj, int low, int high)
71{
72 PyTupleObject *np;
73 int i;
74
75 if (low < 0)
76 low = 0;
77 if (high > VISIBLE_SIZE(obj))
78 high = VISIBLE_SIZE(obj);
79 if (high < low)
80 high = low;
81 np = (PyTupleObject *)PyTuple_New(high-low);
82 if (np == NULL)
83 return NULL;
84 for(i = low; i < high; ++i) {
85 PyObject *v = obj->ob_item[i];
86 Py_INCREF(v);
Tim Petersc2fe6182001-10-30 23:20:46 +000087 PyTuple_SET_ITEM(np, i-low, v);
Guido van Rossume82f75a2001-10-18 20:47:51 +000088 }
89 return (PyObject *) np;
90}
91
92static PyObject *
93structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
94{
95 PyObject *arg = NULL;
Michael W. Hudsonce358e32002-03-06 17:07:49 +000096 PyObject *dict = NULL;
97 PyObject *ob;
Guido van Rossume82f75a2001-10-18 20:47:51 +000098 PyStructSequence *res = NULL;
Martin v. Löwisceaa77c2002-10-16 19:10:03 +000099 int len, min_len, max_len, i, n_unnamed_fields;
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000100 static char *kwlist[] = {"sequence", "dict", 0};
Guido van Rossume82f75a2001-10-18 20:47:51 +0000101
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000102 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq",
103 kwlist, &arg, &dict))
Guido van Rossume82f75a2001-10-18 20:47:51 +0000104 return NULL;
105
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000106 arg = PySequence_Fast(arg, "constructor requires a sequence");
107
108 if (!arg) {
Guido van Rossume82f75a2001-10-18 20:47:51 +0000109 return NULL;
110 }
111
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000112 if (dict && !PyDict_Check(dict)) {
113 PyErr_Format(PyExc_TypeError,
114 "%.500s() takes a dict as second arg, if any",
115 type->tp_name);
116 Py_DECREF(arg);
Guido van Rossume82f75a2001-10-18 20:47:51 +0000117 return NULL;
118 }
119
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000120 len = PySequence_Fast_GET_SIZE(arg);
121 min_len = VISIBLE_SIZE_TP(type);
122 max_len = REAL_SIZE_TP(type);
Martin v. Löwisceaa77c2002-10-16 19:10:03 +0000123 n_unnamed_fields = UNNAMED_FIELDS_TP(type);
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000124
125 if (min_len != max_len) {
126 if (len < min_len) {
127 PyErr_Format(PyExc_TypeError,
128 "%.500s() takes an at least %d-sequence (%d-sequence given)",
129 type->tp_name, min_len, len);
130 Py_DECREF(arg);
131 return NULL;
132 }
133
134 if (len > max_len) {
135 PyErr_Format(PyExc_TypeError,
136 "%.500s() takes an at most %d-sequence (%d-sequence given)",
137 type->tp_name, max_len, len);
138 Py_DECREF(arg);
139 return NULL;
140 }
141 }
142 else {
143 if (len != min_len) {
144 PyErr_Format(PyExc_TypeError,
145 "%.500s() takes a %d-sequence (%d-sequence given)",
146 type->tp_name, min_len, len);
147 Py_DECREF(arg);
148 return NULL;
149 }
150 }
151
Guido van Rossume82f75a2001-10-18 20:47:51 +0000152 res = (PyStructSequence*) PyStructSequence_New(type);
Neal Norwitz8feeabb2002-12-18 23:20:39 +0000153 if (res == NULL) {
154 return NULL;
155 }
Guido van Rossume82f75a2001-10-18 20:47:51 +0000156 for (i = 0; i < len; ++i) {
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000157 PyObject *v = PySequence_Fast_GET_ITEM(arg, i);
158 Py_INCREF(v);
159 res->ob_item[i] = v;
160 }
161 for (; i < max_len; ++i) {
162 if (dict && (ob = PyDict_GetItemString(
Martin v. Löwisceaa77c2002-10-16 19:10:03 +0000163 dict, type->tp_members[i-n_unnamed_fields].name))) {
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000164 }
165 else {
166 ob = Py_None;
167 }
168 Py_INCREF(ob);
169 res->ob_item[i] = ob;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000170 }
171
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000172 Py_DECREF(arg);
Guido van Rossume82f75a2001-10-18 20:47:51 +0000173 return (PyObject*) res;
174}
175
176static PyObject *
177make_tuple(PyStructSequence *obj)
178{
179 return structseq_slice(obj, 0, VISIBLE_SIZE(obj));
180}
181
182static PyObject *
183structseq_repr(PyStructSequence *obj)
184{
185 PyObject *tup, *str;
186 tup = make_tuple(obj);
187 str = PyObject_Repr(tup);
188 Py_DECREF(tup);
189 return str;
190}
191
192static PyObject *
193structseq_concat(PyStructSequence *obj, PyObject *b)
194{
195 PyObject *tup, *result;
196 tup = make_tuple(obj);
197 result = PySequence_Concat(tup, b);
198 Py_DECREF(tup);
199 return result;
200}
201
202static PyObject *
203structseq_repeat(PyStructSequence *obj, int n)
204{
205 PyObject *tup, *result;
206 tup = make_tuple(obj);
207 result = PySequence_Repeat(tup, n);
208 Py_DECREF(tup);
209 return result;
210}
211
212static int
213structseq_contains(PyStructSequence *obj, PyObject *o)
214{
215 PyObject *tup;
216 int result;
217 tup = make_tuple(obj);
218 result = PySequence_Contains(tup, o);
219 Py_DECREF(tup);
220 return result;
221}
222
223static long
224structseq_hash(PyObject *obj)
225{
226 PyObject *tup;
227 long result;
228 tup = make_tuple((PyStructSequence*) obj);
229 result = PyObject_Hash(tup);
230 Py_DECREF(tup);
231 return result;
232}
233
234static PyObject *
235structseq_richcompare(PyObject *obj, PyObject *o2, int op)
236{
237 PyObject *tup, *result;
238 tup = make_tuple((PyStructSequence*) obj);
239 result = PyObject_RichCompare(tup, o2, op);
240 Py_DECREF(tup);
241 return result;
242}
243
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000244static PyObject *
245structseq_reduce(PyStructSequence* self)
246{
247 PyObject* tup;
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000248 PyObject* dict;
Michael W. Hudson70ffddf2002-03-07 15:13:40 +0000249 PyObject* result;
Martin v. Löwisceaa77c2002-10-16 19:10:03 +0000250 long n_fields, n_visible_fields, n_unnamed_fields;
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000251 int i;
252
253 n_fields = REAL_SIZE(self);
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000254 n_visible_fields = VISIBLE_SIZE(self);
Martin v. Löwisceaa77c2002-10-16 19:10:03 +0000255 n_unnamed_fields = UNNAMED_FIELDS(self);
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000256 tup = PyTuple_New(n_visible_fields);
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000257 if (!tup) {
258 return NULL;
259 }
260
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000261 dict = PyDict_New();
262 if (!dict) {
263 Py_DECREF(tup);
264 return NULL;
265 }
266
267 for (i = 0; i < n_visible_fields; i++) {
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000268 Py_INCREF(self->ob_item[i]);
269 PyTuple_SET_ITEM(tup, i, self->ob_item[i]);
270 }
271
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000272 for (; i < n_fields; i++) {
Martin v. Löwisceaa77c2002-10-16 19:10:03 +0000273 char *n = self->ob_type->tp_members[i-n_unnamed_fields].name;
274 PyDict_SetItemString(dict, n,
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000275 self->ob_item[i]);
276 }
277
Michael W. Hudson70ffddf2002-03-07 15:13:40 +0000278 result = Py_BuildValue("(O(OO))", self->ob_type, tup, dict);
279
280 Py_DECREF(tup);
281 Py_DECREF(dict);
282
283 return result;
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000284}
285
Guido van Rossume82f75a2001-10-18 20:47:51 +0000286static PySequenceMethods structseq_as_sequence = {
287 (inquiry)structseq_length,
288 (binaryfunc)structseq_concat, /* sq_concat */
289 (intargfunc)structseq_repeat, /* sq_repeat */
290 (intargfunc)structseq_item, /* sq_item */
291 (intintargfunc)structseq_slice, /* sq_slice */
292 0, /* sq_ass_item */
293 0, /* sq_ass_slice */
294 (objobjproc)structseq_contains, /* sq_contains */
295};
296
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000297static PyMethodDef structseq_methods[] = {
298 {"__reduce__", (PyCFunction)structseq_reduce,
299 METH_NOARGS, NULL},
300 {NULL, NULL}
301};
302
Guido van Rossume82f75a2001-10-18 20:47:51 +0000303static PyTypeObject _struct_sequence_template = {
304 PyObject_HEAD_INIT(&PyType_Type)
305 0, /* ob_size */
306 NULL, /* tp_name */
307 0, /* tp_basicsize */
308 0, /* tp_itemsize */
309 (destructor)structseq_dealloc, /* tp_dealloc */
310 0, /* tp_print */
311 0, /* tp_getattr */
312 0, /* tp_setattr */
313 0, /* tp_compare */
314 (reprfunc)structseq_repr, /* tp_repr */
315 0, /* tp_as_number */
316 &structseq_as_sequence, /* tp_as_sequence */
317 0, /* tp_as_mapping */
318 (hashfunc)structseq_hash, /* tp_hash */
319 0, /* tp_call */
320 0, /* tp_str */
321 0, /* tp_getattro */
322 0, /* tp_setattro */
323 0, /* tp_as_buffer */
324 Py_TPFLAGS_DEFAULT, /* tp_flags */
325 NULL, /* tp_doc */
326 0, /* tp_traverse */
327 0, /* tp_clear */
328 structseq_richcompare, /* tp_richcompare */
329 0, /* tp_weaklistoffset */
330 0, /* tp_iter */
331 0, /* tp_iternext */
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000332 structseq_methods, /* tp_methods */
Guido van Rossume82f75a2001-10-18 20:47:51 +0000333 NULL, /* tp_members */
334 0, /* tp_getset */
335 0, /* tp_base */
336 0, /* tp_dict */
337 0, /* tp_descr_get */
338 0, /* tp_descr_set */
339 0, /* tp_dictoffset */
340 0, /* tp_init */
341 0, /* tp_alloc */
342 structseq_new, /* tp_new */
343};
344
345void
346PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
347{
348 PyObject *dict;
349 PyMemberDef* members;
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000350 int n_members, n_unnamed_members, i, k;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000351
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000352 n_unnamed_members = 0;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000353 for (i = 0; desc->fields[i].name != NULL; ++i)
Martin v. Löwisceaa77c2002-10-16 19:10:03 +0000354 if (desc->fields[i].name == PyStructSequence_UnnamedField)
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000355 n_unnamed_members++;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000356 n_members = i;
357
358 memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject));
359 type->tp_name = desc->name;
360 type->tp_doc = desc->doc;
361 type->tp_basicsize = sizeof(PyStructSequence)+
362 sizeof(PyObject*)*(n_members-1);
363 type->tp_itemsize = 0;
364
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000365 members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1);
Neal Norwitz8feeabb2002-12-18 23:20:39 +0000366 if (members == NULL)
367 return;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000368
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000369 for (i = k = 0; i < n_members; ++i) {
370 if (desc->fields[i].name == PyStructSequence_UnnamedField)
371 continue;
372 members[k].name = desc->fields[i].name;
373 members[k].type = T_OBJECT;
374 members[k].offset = offsetof(PyStructSequence, ob_item)
Guido van Rossume82f75a2001-10-18 20:47:51 +0000375 + i * sizeof(PyObject*);
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000376 members[k].flags = READONLY;
377 members[k].doc = desc->fields[i].doc;
378 k++;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000379 }
Martin v. Löwisf607bda2002-10-16 18:27:39 +0000380 members[k].name = NULL;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000381
382 type->tp_members = members;
383
384 if (PyType_Ready(type) < 0)
385 return;
386 Py_INCREF(type);
387
388 dict = type->tp_dict;
389 PyDict_SetItemString(dict, visible_length_key,
390 PyInt_FromLong((long) desc->n_in_sequence));
391 PyDict_SetItemString(dict, real_length_key,
392 PyInt_FromLong((long) n_members));
Martin v. Löwisceaa77c2002-10-16 19:10:03 +0000393 PyDict_SetItemString(dict, unnamed_fields_key,
394 PyInt_FromLong((long) n_unnamed_members));
Guido van Rossume82f75a2001-10-18 20:47:51 +0000395}