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"; |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 10 | static char unnamed_fields_key[] = "n_unnamed_fields"; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 11 | |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 12 | /* Fields with this name have only a field index, not a field name. |
| 13 | They are only allowed for indices < n_visible_fields. */ |
| 14 | char *PyStructSequence_UnnamedField = "unnamed field"; |
| 15 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 16 | #define VISIBLE_SIZE(op) Py_Size(op) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 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)) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 22 | #define REAL_SIZE(op) REAL_SIZE_TP(Py_Type(op)) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 23 | |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 24 | #define UNNAMED_FIELDS_TP(tp) PyInt_AsLong( \ |
| 25 | PyDict_GetItemString((tp)->tp_dict, unnamed_fields_key)) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 26 | #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_Type(op)) |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 28 | |
| 29 | PyObject * |
| 30 | PyStructSequence_New(PyTypeObject *type) |
| 31 | { |
| 32 | PyStructSequence *obj; |
| 33 | |
Neil Schemenauer | 7465ad2 | 2002-04-12 03:05:37 +0000 | [diff] [blame] | 34 | obj = PyObject_New(PyStructSequence, type); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 35 | Py_Size(obj) = VISIBLE_SIZE_TP(type); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 36 | |
| 37 | return (PyObject*) obj; |
| 38 | } |
| 39 | |
| 40 | static void |
| 41 | structseq_dealloc(PyStructSequence *obj) |
| 42 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 43 | Py_ssize_t i, size; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 44 | |
| 45 | size = REAL_SIZE(obj); |
| 46 | for (i = 0; i < size; ++i) { |
| 47 | Py_XDECREF(obj->ob_item[i]); |
| 48 | } |
Neil Schemenauer | 7465ad2 | 2002-04-12 03:05:37 +0000 | [diff] [blame] | 49 | PyObject_Del(obj); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 52 | static Py_ssize_t |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 53 | structseq_length(PyStructSequence *obj) |
| 54 | { |
| 55 | return VISIBLE_SIZE(obj); |
| 56 | } |
| 57 | |
| 58 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 59 | structseq_item(PyStructSequence *obj, Py_ssize_t i) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 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 | |
| 69 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 70 | structseq_slice(PyStructSequence *obj, Py_ssize_t low, Py_ssize_t high) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 71 | { |
| 72 | PyTupleObject *np; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 73 | Py_ssize_t i; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 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 Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 87 | PyTuple_SET_ITEM(np, i-low, v); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 88 | } |
| 89 | return (PyObject *) np; |
| 90 | } |
| 91 | |
| 92 | static PyObject * |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 93 | structseq_subscript(PyStructSequence *self, PyObject *item) |
| 94 | { |
| 95 | if (PyIndex_Check(item)) { |
| 96 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 97 | if (i == -1 && PyErr_Occurred()) |
| 98 | return NULL; |
| 99 | |
| 100 | if (i < 0) |
| 101 | i += VISIBLE_SIZE(self); |
| 102 | |
| 103 | if (i < 0 || i >= VISIBLE_SIZE(self)) { |
| 104 | PyErr_SetString(PyExc_IndexError, |
| 105 | "tuple index out of range"); |
| 106 | return NULL; |
| 107 | } |
| 108 | Py_INCREF(self->ob_item[i]); |
| 109 | return self->ob_item[i]; |
| 110 | } |
| 111 | else if (PySlice_Check(item)) { |
| 112 | Py_ssize_t start, stop, step, slicelen, cur, i; |
| 113 | PyObject *result; |
| 114 | |
| 115 | if (PySlice_GetIndicesEx((PySliceObject *)item, |
| 116 | VISIBLE_SIZE(self), &start, &stop, |
| 117 | &step, &slicelen) < 0) { |
| 118 | return NULL; |
| 119 | } |
| 120 | if (slicelen <= 0) |
| 121 | return PyTuple_New(0); |
| 122 | result = PyTuple_New(slicelen); |
| 123 | if (result == NULL) |
| 124 | return NULL; |
| 125 | for (cur = start, i = 0; i < slicelen; |
| 126 | cur += step, i++) { |
| 127 | PyObject *v = self->ob_item[cur]; |
| 128 | Py_INCREF(v); |
| 129 | PyTuple_SET_ITEM(result, i, v); |
| 130 | } |
| 131 | return result; |
| 132 | } |
| 133 | else { |
| 134 | PyErr_SetString(PyExc_TypeError, |
| 135 | "structseq index must be integer"); |
| 136 | return NULL; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static PyObject * |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 141 | structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 142 | { |
| 143 | PyObject *arg = NULL; |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 144 | PyObject *dict = NULL; |
| 145 | PyObject *ob; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 146 | PyStructSequence *res = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 147 | Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 148 | static char *kwlist[] = {"sequence", "dict", 0}; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 149 | |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 150 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq", |
| 151 | kwlist, &arg, &dict)) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 152 | return NULL; |
| 153 | |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 154 | arg = PySequence_Fast(arg, "constructor requires a sequence"); |
| 155 | |
| 156 | if (!arg) { |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
| 159 | |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 160 | if (dict && !PyDict_Check(dict)) { |
| 161 | PyErr_Format(PyExc_TypeError, |
| 162 | "%.500s() takes a dict as second arg, if any", |
| 163 | type->tp_name); |
| 164 | Py_DECREF(arg); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
| 167 | |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 168 | len = PySequence_Fast_GET_SIZE(arg); |
| 169 | min_len = VISIBLE_SIZE_TP(type); |
| 170 | max_len = REAL_SIZE_TP(type); |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 171 | n_unnamed_fields = UNNAMED_FIELDS_TP(type); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 172 | |
| 173 | if (min_len != max_len) { |
| 174 | if (len < min_len) { |
| 175 | PyErr_Format(PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 176 | "%.500s() takes an at least %zd-sequence (%zd-sequence given)", |
Martin v. Löwis | e0e89f7 | 2006-02-16 06:59:22 +0000 | [diff] [blame] | 177 | type->tp_name, min_len, len); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 178 | Py_DECREF(arg); |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | if (len > max_len) { |
| 183 | PyErr_Format(PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 184 | "%.500s() takes an at most %zd-sequence (%zd-sequence given)", |
Martin v. Löwis | e0e89f7 | 2006-02-16 06:59:22 +0000 | [diff] [blame] | 185 | type->tp_name, max_len, len); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 186 | Py_DECREF(arg); |
| 187 | return NULL; |
| 188 | } |
| 189 | } |
| 190 | else { |
| 191 | if (len != min_len) { |
| 192 | PyErr_Format(PyExc_TypeError, |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 193 | "%.500s() takes a %zd-sequence (%zd-sequence given)", |
Martin v. Löwis | e0e89f7 | 2006-02-16 06:59:22 +0000 | [diff] [blame] | 194 | type->tp_name, min_len, len); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 195 | Py_DECREF(arg); |
| 196 | return NULL; |
| 197 | } |
| 198 | } |
| 199 | |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 200 | res = (PyStructSequence*) PyStructSequence_New(type); |
Neal Norwitz | 8feeabb | 2002-12-18 23:20:39 +0000 | [diff] [blame] | 201 | if (res == NULL) { |
| 202 | return NULL; |
| 203 | } |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 204 | for (i = 0; i < len; ++i) { |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 205 | PyObject *v = PySequence_Fast_GET_ITEM(arg, i); |
| 206 | Py_INCREF(v); |
| 207 | res->ob_item[i] = v; |
| 208 | } |
| 209 | for (; i < max_len; ++i) { |
| 210 | if (dict && (ob = PyDict_GetItemString( |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 211 | dict, type->tp_members[i-n_unnamed_fields].name))) { |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 212 | } |
| 213 | else { |
| 214 | ob = Py_None; |
| 215 | } |
| 216 | Py_INCREF(ob); |
| 217 | res->ob_item[i] = ob; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 220 | Py_DECREF(arg); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 221 | return (PyObject*) res; |
| 222 | } |
| 223 | |
| 224 | static PyObject * |
| 225 | make_tuple(PyStructSequence *obj) |
| 226 | { |
| 227 | return structseq_slice(obj, 0, VISIBLE_SIZE(obj)); |
| 228 | } |
| 229 | |
| 230 | static PyObject * |
| 231 | structseq_repr(PyStructSequence *obj) |
| 232 | { |
| 233 | PyObject *tup, *str; |
| 234 | tup = make_tuple(obj); |
| 235 | str = PyObject_Repr(tup); |
| 236 | Py_DECREF(tup); |
| 237 | return str; |
| 238 | } |
| 239 | |
| 240 | static PyObject * |
| 241 | structseq_concat(PyStructSequence *obj, PyObject *b) |
| 242 | { |
| 243 | PyObject *tup, *result; |
| 244 | tup = make_tuple(obj); |
| 245 | result = PySequence_Concat(tup, b); |
| 246 | Py_DECREF(tup); |
| 247 | return result; |
| 248 | } |
| 249 | |
| 250 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 251 | structseq_repeat(PyStructSequence *obj, Py_ssize_t n) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 252 | { |
| 253 | PyObject *tup, *result; |
| 254 | tup = make_tuple(obj); |
| 255 | result = PySequence_Repeat(tup, n); |
| 256 | Py_DECREF(tup); |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | structseq_contains(PyStructSequence *obj, PyObject *o) |
| 262 | { |
| 263 | PyObject *tup; |
| 264 | int result; |
| 265 | tup = make_tuple(obj); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 266 | if (!tup) |
| 267 | return -1; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 268 | result = PySequence_Contains(tup, o); |
| 269 | Py_DECREF(tup); |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | static long |
| 274 | structseq_hash(PyObject *obj) |
| 275 | { |
| 276 | PyObject *tup; |
| 277 | long result; |
| 278 | tup = make_tuple((PyStructSequence*) obj); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 279 | if (!tup) |
| 280 | return -1; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 281 | result = PyObject_Hash(tup); |
| 282 | Py_DECREF(tup); |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | static PyObject * |
| 287 | structseq_richcompare(PyObject *obj, PyObject *o2, int op) |
| 288 | { |
| 289 | PyObject *tup, *result; |
| 290 | tup = make_tuple((PyStructSequence*) obj); |
| 291 | result = PyObject_RichCompare(tup, o2, op); |
| 292 | Py_DECREF(tup); |
| 293 | return result; |
| 294 | } |
| 295 | |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 296 | static PyObject * |
| 297 | structseq_reduce(PyStructSequence* self) |
| 298 | { |
| 299 | PyObject* tup; |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 300 | PyObject* dict; |
Michael W. Hudson | 70ffddf | 2002-03-07 15:13:40 +0000 | [diff] [blame] | 301 | PyObject* result; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 302 | Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields; |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 303 | int i; |
| 304 | |
| 305 | n_fields = REAL_SIZE(self); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 306 | n_visible_fields = VISIBLE_SIZE(self); |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 307 | n_unnamed_fields = UNNAMED_FIELDS(self); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 308 | tup = PyTuple_New(n_visible_fields); |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 309 | if (!tup) { |
| 310 | return NULL; |
| 311 | } |
| 312 | |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 313 | dict = PyDict_New(); |
| 314 | if (!dict) { |
| 315 | Py_DECREF(tup); |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | for (i = 0; i < n_visible_fields; i++) { |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 320 | Py_INCREF(self->ob_item[i]); |
| 321 | PyTuple_SET_ITEM(tup, i, self->ob_item[i]); |
| 322 | } |
| 323 | |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 324 | for (; i < n_fields; i++) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 325 | char *n = Py_Type(self)->tp_members[i-n_unnamed_fields].name; |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 326 | PyDict_SetItemString(dict, n, |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 327 | self->ob_item[i]); |
| 328 | } |
| 329 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 330 | result = Py_BuildValue("(O(OO))", Py_Type(self), tup, dict); |
Michael W. Hudson | 70ffddf | 2002-03-07 15:13:40 +0000 | [diff] [blame] | 331 | |
| 332 | Py_DECREF(tup); |
| 333 | Py_DECREF(dict); |
| 334 | |
| 335 | return result; |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 338 | static PySequenceMethods structseq_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 339 | (lenfunc)structseq_length, |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 340 | (binaryfunc)structseq_concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 341 | (ssizeargfunc)structseq_repeat, /* sq_repeat */ |
| 342 | (ssizeargfunc)structseq_item, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 343 | 0, /* sq_slice */ |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 344 | 0, /* sq_ass_item */ |
| 345 | 0, /* sq_ass_slice */ |
| 346 | (objobjproc)structseq_contains, /* sq_contains */ |
| 347 | }; |
| 348 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 349 | static PyMappingMethods structseq_as_mapping = { |
| 350 | (lenfunc)structseq_length, |
| 351 | (binaryfunc)structseq_subscript, |
| 352 | }; |
| 353 | |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 354 | static PyMethodDef structseq_methods[] = { |
| 355 | {"__reduce__", (PyCFunction)structseq_reduce, |
| 356 | METH_NOARGS, NULL}, |
| 357 | {NULL, NULL} |
| 358 | }; |
| 359 | |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 360 | static PyTypeObject _struct_sequence_template = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 361 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 362 | NULL, /* tp_name */ |
| 363 | 0, /* tp_basicsize */ |
| 364 | 0, /* tp_itemsize */ |
| 365 | (destructor)structseq_dealloc, /* tp_dealloc */ |
| 366 | 0, /* tp_print */ |
| 367 | 0, /* tp_getattr */ |
| 368 | 0, /* tp_setattr */ |
| 369 | 0, /* tp_compare */ |
| 370 | (reprfunc)structseq_repr, /* tp_repr */ |
| 371 | 0, /* tp_as_number */ |
| 372 | &structseq_as_sequence, /* tp_as_sequence */ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 373 | &structseq_as_mapping, /* tp_as_mapping */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 374 | structseq_hash, /* tp_hash */ |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 375 | 0, /* tp_call */ |
| 376 | 0, /* tp_str */ |
| 377 | 0, /* tp_getattro */ |
| 378 | 0, /* tp_setattro */ |
| 379 | 0, /* tp_as_buffer */ |
| 380 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 381 | NULL, /* tp_doc */ |
| 382 | 0, /* tp_traverse */ |
| 383 | 0, /* tp_clear */ |
| 384 | structseq_richcompare, /* tp_richcompare */ |
| 385 | 0, /* tp_weaklistoffset */ |
| 386 | 0, /* tp_iter */ |
| 387 | 0, /* tp_iternext */ |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 388 | structseq_methods, /* tp_methods */ |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 389 | NULL, /* tp_members */ |
| 390 | 0, /* tp_getset */ |
| 391 | 0, /* tp_base */ |
| 392 | 0, /* tp_dict */ |
| 393 | 0, /* tp_descr_get */ |
| 394 | 0, /* tp_descr_set */ |
| 395 | 0, /* tp_dictoffset */ |
| 396 | 0, /* tp_init */ |
| 397 | 0, /* tp_alloc */ |
| 398 | structseq_new, /* tp_new */ |
| 399 | }; |
| 400 | |
| 401 | void |
| 402 | PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) |
| 403 | { |
| 404 | PyObject *dict; |
| 405 | PyMemberDef* members; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 406 | int n_members, n_unnamed_members, i, k; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 407 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 408 | #ifdef Py_TRACE_REFS |
| 409 | /* if the type object was chained, unchain it first |
| 410 | before overwriting its storage */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 411 | if (type->ob_base.ob_base._ob_next) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 412 | _Py_ForgetReference((PyObject*)type); |
| 413 | } |
| 414 | #endif |
| 415 | |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 416 | n_unnamed_members = 0; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 417 | for (i = 0; desc->fields[i].name != NULL; ++i) |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 418 | if (desc->fields[i].name == PyStructSequence_UnnamedField) |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 419 | n_unnamed_members++; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 420 | n_members = i; |
| 421 | |
| 422 | memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject)); |
| 423 | type->tp_name = desc->name; |
| 424 | type->tp_doc = desc->doc; |
| 425 | type->tp_basicsize = sizeof(PyStructSequence)+ |
| 426 | sizeof(PyObject*)*(n_members-1); |
| 427 | type->tp_itemsize = 0; |
| 428 | |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 429 | members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1); |
Neal Norwitz | 8feeabb | 2002-12-18 23:20:39 +0000 | [diff] [blame] | 430 | if (members == NULL) |
| 431 | return; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 432 | |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 433 | for (i = k = 0; i < n_members; ++i) { |
| 434 | if (desc->fields[i].name == PyStructSequence_UnnamedField) |
| 435 | continue; |
| 436 | members[k].name = desc->fields[i].name; |
| 437 | members[k].type = T_OBJECT; |
| 438 | members[k].offset = offsetof(PyStructSequence, ob_item) |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 439 | + i * sizeof(PyObject*); |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 440 | members[k].flags = READONLY; |
| 441 | members[k].doc = desc->fields[i].doc; |
| 442 | k++; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 443 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 444 | members[k].name = NULL; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 445 | |
| 446 | type->tp_members = members; |
| 447 | |
| 448 | if (PyType_Ready(type) < 0) |
| 449 | return; |
| 450 | Py_INCREF(type); |
| 451 | |
| 452 | dict = type->tp_dict; |
| 453 | PyDict_SetItemString(dict, visible_length_key, |
| 454 | PyInt_FromLong((long) desc->n_in_sequence)); |
| 455 | PyDict_SetItemString(dict, real_length_key, |
| 456 | PyInt_FromLong((long) n_members)); |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 457 | PyDict_SetItemString(dict, unnamed_fields_key, |
| 458 | PyInt_FromLong((long) n_unnamed_members)); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 459 | } |