| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 1 | /* 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 | |
| 8 | static char visible_length_key[] = "n_sequence_fields"; |
| 9 | static char real_length_key[] = "n_fields"; |
| 10 | |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 11 | /* Fields with this name have only a field index, not a field name. |
| 12 | They are only allowed for indices < n_visible_fields. */ |
| 13 | char *PyStructSequence_UnnamedField = "unnamed field"; |
| 14 | |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 15 | #define VISIBLE_SIZE(op) ((op)->ob_size) |
| 16 | #define VISIBLE_SIZE_TP(tp) PyInt_AsLong( \ |
| 17 | PyDict_GetItemString((tp)->tp_dict, visible_length_key)) |
| 18 | |
| 19 | #define REAL_SIZE_TP(tp) PyInt_AsLong( \ |
| 20 | PyDict_GetItemString((tp)->tp_dict, real_length_key)) |
| 21 | #define REAL_SIZE(op) REAL_SIZE_TP((op)->ob_type) |
| 22 | |
| 23 | |
| 24 | PyObject * |
| 25 | PyStructSequence_New(PyTypeObject *type) |
| 26 | { |
| 27 | PyStructSequence *obj; |
| 28 | |
| Neil Schemenauer | 7465ad2 | 2002-04-12 03:05:37 +0000 | [diff] [blame] | 29 | obj = PyObject_New(PyStructSequence, type); |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 30 | obj->ob_size = VISIBLE_SIZE_TP(type); |
| 31 | |
| 32 | return (PyObject*) obj; |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | structseq_dealloc(PyStructSequence *obj) |
| 37 | { |
| 38 | int i, size; |
| 39 | |
| 40 | size = REAL_SIZE(obj); |
| 41 | for (i = 0; i < size; ++i) { |
| 42 | Py_XDECREF(obj->ob_item[i]); |
| 43 | } |
| Neil Schemenauer | 7465ad2 | 2002-04-12 03:05:37 +0000 | [diff] [blame] | 44 | PyObject_Del(obj); |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int |
| 48 | structseq_length(PyStructSequence *obj) |
| 49 | { |
| 50 | return VISIBLE_SIZE(obj); |
| 51 | } |
| 52 | |
| 53 | static PyObject* |
| 54 | structseq_item(PyStructSequence *obj, int i) |
| 55 | { |
| 56 | if (i < 0 || i >= VISIBLE_SIZE(obj)) { |
| 57 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 58 | return NULL; |
| 59 | } |
| 60 | Py_INCREF(obj->ob_item[i]); |
| 61 | return obj->ob_item[i]; |
| 62 | } |
| 63 | |
| 64 | static PyObject* |
| 65 | structseq_slice(PyStructSequence *obj, int low, int high) |
| 66 | { |
| 67 | PyTupleObject *np; |
| 68 | int i; |
| 69 | |
| 70 | if (low < 0) |
| 71 | low = 0; |
| 72 | if (high > VISIBLE_SIZE(obj)) |
| 73 | high = VISIBLE_SIZE(obj); |
| 74 | if (high < low) |
| 75 | high = low; |
| 76 | np = (PyTupleObject *)PyTuple_New(high-low); |
| 77 | if (np == NULL) |
| 78 | return NULL; |
| 79 | for(i = low; i < high; ++i) { |
| 80 | PyObject *v = obj->ob_item[i]; |
| 81 | Py_INCREF(v); |
| Tim Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 82 | PyTuple_SET_ITEM(np, i-low, v); |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 83 | } |
| 84 | return (PyObject *) np; |
| 85 | } |
| 86 | |
| 87 | static PyObject * |
| 88 | structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 89 | { |
| 90 | PyObject *arg = NULL; |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 91 | PyObject *dict = NULL; |
| 92 | PyObject *ob; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 93 | PyStructSequence *res = NULL; |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 94 | int len, min_len, max_len, i; |
| 95 | static char *kwlist[] = {"sequence", "dict", 0}; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 96 | |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 97 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq", |
| 98 | kwlist, &arg, &dict)) |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 99 | return NULL; |
| 100 | |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 101 | arg = PySequence_Fast(arg, "constructor requires a sequence"); |
| 102 | |
| 103 | if (!arg) { |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
| 106 | |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 107 | if (dict && !PyDict_Check(dict)) { |
| 108 | PyErr_Format(PyExc_TypeError, |
| 109 | "%.500s() takes a dict as second arg, if any", |
| 110 | type->tp_name); |
| 111 | Py_DECREF(arg); |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
| 114 | |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 115 | len = PySequence_Fast_GET_SIZE(arg); |
| 116 | min_len = VISIBLE_SIZE_TP(type); |
| 117 | max_len = REAL_SIZE_TP(type); |
| 118 | |
| 119 | if (min_len != max_len) { |
| 120 | if (len < min_len) { |
| 121 | PyErr_Format(PyExc_TypeError, |
| 122 | "%.500s() takes an at least %d-sequence (%d-sequence given)", |
| 123 | type->tp_name, min_len, len); |
| 124 | Py_DECREF(arg); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | if (len > max_len) { |
| 129 | PyErr_Format(PyExc_TypeError, |
| 130 | "%.500s() takes an at most %d-sequence (%d-sequence given)", |
| 131 | type->tp_name, max_len, len); |
| 132 | Py_DECREF(arg); |
| 133 | return NULL; |
| 134 | } |
| 135 | } |
| 136 | else { |
| 137 | if (len != min_len) { |
| 138 | PyErr_Format(PyExc_TypeError, |
| 139 | "%.500s() takes a %d-sequence (%d-sequence given)", |
| 140 | type->tp_name, min_len, len); |
| 141 | Py_DECREF(arg); |
| 142 | return NULL; |
| 143 | } |
| 144 | } |
| 145 | |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 146 | res = (PyStructSequence*) PyStructSequence_New(type); |
| 147 | for (i = 0; i < len; ++i) { |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 148 | PyObject *v = PySequence_Fast_GET_ITEM(arg, i); |
| 149 | Py_INCREF(v); |
| 150 | res->ob_item[i] = v; |
| 151 | } |
| 152 | for (; i < max_len; ++i) { |
| 153 | if (dict && (ob = PyDict_GetItemString( |
| 154 | dict, type->tp_members[i].name))) { |
| 155 | } |
| 156 | else { |
| 157 | ob = Py_None; |
| 158 | } |
| 159 | Py_INCREF(ob); |
| 160 | res->ob_item[i] = ob; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 163 | Py_DECREF(arg); |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 164 | return (PyObject*) res; |
| 165 | } |
| 166 | |
| 167 | static PyObject * |
| 168 | make_tuple(PyStructSequence *obj) |
| 169 | { |
| 170 | return structseq_slice(obj, 0, VISIBLE_SIZE(obj)); |
| 171 | } |
| 172 | |
| 173 | static PyObject * |
| 174 | structseq_repr(PyStructSequence *obj) |
| 175 | { |
| 176 | PyObject *tup, *str; |
| 177 | tup = make_tuple(obj); |
| 178 | str = PyObject_Repr(tup); |
| 179 | Py_DECREF(tup); |
| 180 | return str; |
| 181 | } |
| 182 | |
| 183 | static PyObject * |
| 184 | structseq_concat(PyStructSequence *obj, PyObject *b) |
| 185 | { |
| 186 | PyObject *tup, *result; |
| 187 | tup = make_tuple(obj); |
| 188 | result = PySequence_Concat(tup, b); |
| 189 | Py_DECREF(tup); |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | static PyObject * |
| 194 | structseq_repeat(PyStructSequence *obj, int n) |
| 195 | { |
| 196 | PyObject *tup, *result; |
| 197 | tup = make_tuple(obj); |
| 198 | result = PySequence_Repeat(tup, n); |
| 199 | Py_DECREF(tup); |
| 200 | return result; |
| 201 | } |
| 202 | |
| 203 | static int |
| 204 | structseq_contains(PyStructSequence *obj, PyObject *o) |
| 205 | { |
| 206 | PyObject *tup; |
| 207 | int result; |
| 208 | tup = make_tuple(obj); |
| 209 | result = PySequence_Contains(tup, o); |
| 210 | Py_DECREF(tup); |
| 211 | return result; |
| 212 | } |
| 213 | |
| 214 | static long |
| 215 | structseq_hash(PyObject *obj) |
| 216 | { |
| 217 | PyObject *tup; |
| 218 | long result; |
| 219 | tup = make_tuple((PyStructSequence*) obj); |
| 220 | result = PyObject_Hash(tup); |
| 221 | Py_DECREF(tup); |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | static PyObject * |
| 226 | structseq_richcompare(PyObject *obj, PyObject *o2, int op) |
| 227 | { |
| 228 | PyObject *tup, *result; |
| 229 | tup = make_tuple((PyStructSequence*) obj); |
| 230 | result = PyObject_RichCompare(tup, o2, op); |
| 231 | Py_DECREF(tup); |
| 232 | return result; |
| 233 | } |
| 234 | |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 235 | static PyObject * |
| 236 | structseq_reduce(PyStructSequence* self) |
| 237 | { |
| 238 | PyObject* tup; |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 239 | PyObject* dict; |
| Michael W. Hudson | 70ffddf | 2002-03-07 15:13:40 +0000 | [diff] [blame] | 240 | PyObject* result; |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 241 | long n_fields, n_visible_fields; |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 242 | int i; |
| 243 | |
| 244 | n_fields = REAL_SIZE(self); |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 245 | n_visible_fields = VISIBLE_SIZE(self); |
| 246 | tup = PyTuple_New(n_visible_fields); |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 247 | if (!tup) { |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 251 | dict = PyDict_New(); |
| 252 | if (!dict) { |
| 253 | Py_DECREF(tup); |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | for (i = 0; i < n_visible_fields; i++) { |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 258 | Py_INCREF(self->ob_item[i]); |
| 259 | PyTuple_SET_ITEM(tup, i, self->ob_item[i]); |
| 260 | } |
| 261 | |
| Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 262 | for (; i < n_fields; i++) { |
| 263 | PyDict_SetItemString(dict, self->ob_type->tp_members[i].name, |
| 264 | self->ob_item[i]); |
| 265 | } |
| 266 | |
| Michael W. Hudson | 70ffddf | 2002-03-07 15:13:40 +0000 | [diff] [blame] | 267 | result = Py_BuildValue("(O(OO))", self->ob_type, tup, dict); |
| 268 | |
| 269 | Py_DECREF(tup); |
| 270 | Py_DECREF(dict); |
| 271 | |
| 272 | return result; |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 275 | static PySequenceMethods structseq_as_sequence = { |
| 276 | (inquiry)structseq_length, |
| 277 | (binaryfunc)structseq_concat, /* sq_concat */ |
| 278 | (intargfunc)structseq_repeat, /* sq_repeat */ |
| 279 | (intargfunc)structseq_item, /* sq_item */ |
| 280 | (intintargfunc)structseq_slice, /* sq_slice */ |
| 281 | 0, /* sq_ass_item */ |
| 282 | 0, /* sq_ass_slice */ |
| 283 | (objobjproc)structseq_contains, /* sq_contains */ |
| 284 | }; |
| 285 | |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 286 | static PyMethodDef structseq_methods[] = { |
| 287 | {"__reduce__", (PyCFunction)structseq_reduce, |
| 288 | METH_NOARGS, NULL}, |
| 289 | {NULL, NULL} |
| 290 | }; |
| 291 | |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 292 | static PyTypeObject _struct_sequence_template = { |
| 293 | PyObject_HEAD_INIT(&PyType_Type) |
| 294 | 0, /* ob_size */ |
| 295 | NULL, /* tp_name */ |
| 296 | 0, /* tp_basicsize */ |
| 297 | 0, /* tp_itemsize */ |
| 298 | (destructor)structseq_dealloc, /* tp_dealloc */ |
| 299 | 0, /* tp_print */ |
| 300 | 0, /* tp_getattr */ |
| 301 | 0, /* tp_setattr */ |
| 302 | 0, /* tp_compare */ |
| 303 | (reprfunc)structseq_repr, /* tp_repr */ |
| 304 | 0, /* tp_as_number */ |
| 305 | &structseq_as_sequence, /* tp_as_sequence */ |
| 306 | 0, /* tp_as_mapping */ |
| 307 | (hashfunc)structseq_hash, /* tp_hash */ |
| 308 | 0, /* tp_call */ |
| 309 | 0, /* tp_str */ |
| 310 | 0, /* tp_getattro */ |
| 311 | 0, /* tp_setattro */ |
| 312 | 0, /* tp_as_buffer */ |
| 313 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 314 | NULL, /* tp_doc */ |
| 315 | 0, /* tp_traverse */ |
| 316 | 0, /* tp_clear */ |
| 317 | structseq_richcompare, /* tp_richcompare */ |
| 318 | 0, /* tp_weaklistoffset */ |
| 319 | 0, /* tp_iter */ |
| 320 | 0, /* tp_iternext */ |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 321 | structseq_methods, /* tp_methods */ |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 322 | NULL, /* tp_members */ |
| 323 | 0, /* tp_getset */ |
| 324 | 0, /* tp_base */ |
| 325 | 0, /* tp_dict */ |
| 326 | 0, /* tp_descr_get */ |
| 327 | 0, /* tp_descr_set */ |
| 328 | 0, /* tp_dictoffset */ |
| 329 | 0, /* tp_init */ |
| 330 | 0, /* tp_alloc */ |
| 331 | structseq_new, /* tp_new */ |
| 332 | }; |
| 333 | |
| 334 | void |
| 335 | PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) |
| 336 | { |
| 337 | PyObject *dict; |
| 338 | PyMemberDef* members; |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 339 | int n_members, n_unnamed_members, i, k; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 340 | |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 341 | n_unnamed_members = 0; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 342 | for (i = 0; desc->fields[i].name != NULL; ++i) |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 343 | if (desc->fields[0].name == PyStructSequence_UnnamedField) |
| 344 | n_unnamed_members++; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 345 | n_members = i; |
| 346 | |
| 347 | memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject)); |
| 348 | type->tp_name = desc->name; |
| 349 | type->tp_doc = desc->doc; |
| 350 | type->tp_basicsize = sizeof(PyStructSequence)+ |
| 351 | sizeof(PyObject*)*(n_members-1); |
| 352 | type->tp_itemsize = 0; |
| 353 | |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 354 | members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1); |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 355 | |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 356 | for (i = k = 0; i < n_members; ++i) { |
| 357 | if (desc->fields[i].name == PyStructSequence_UnnamedField) |
| 358 | continue; |
| 359 | members[k].name = desc->fields[i].name; |
| 360 | members[k].type = T_OBJECT; |
| 361 | members[k].offset = offsetof(PyStructSequence, ob_item) |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 362 | + i * sizeof(PyObject*); |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 363 | members[k].flags = READONLY; |
| 364 | members[k].doc = desc->fields[i].doc; |
| 365 | k++; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 366 | } |
| Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 367 | members[k].name = NULL; |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 368 | |
| 369 | type->tp_members = members; |
| 370 | |
| 371 | if (PyType_Ready(type) < 0) |
| 372 | return; |
| 373 | Py_INCREF(type); |
| 374 | |
| 375 | dict = type->tp_dict; |
| 376 | PyDict_SetItemString(dict, visible_length_key, |
| 377 | PyInt_FromLong((long) desc->n_in_sequence)); |
| 378 | PyDict_SetItemString(dict, real_length_key, |
| 379 | PyInt_FromLong((long) n_members)); |
| Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 380 | PyDict_SetItemString(dict, "__safe_for_unpickling__", |
| 381 | PyInt_FromLong(1)); |
| Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 382 | } |