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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 12 | /* Fields with this name have only a field index, not a field name. |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 13 | They are only allowed for indices < n_visible_fields. */ |
| 14 | char *PyStructSequence_UnnamedField = "unnamed field"; |
| 15 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 16 | #define VISIBLE_SIZE(op) Py_SIZE(op) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 17 | #define VISIBLE_SIZE_TP(tp) PyLong_AsLong( \ |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 18 | PyDict_GetItemString((tp)->tp_dict, visible_length_key)) |
| 19 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 20 | #define REAL_SIZE_TP(tp) PyLong_AsLong( \ |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 21 | PyDict_GetItemString((tp)->tp_dict, real_length_key)) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +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 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 24 | #define UNNAMED_FIELDS_TP(tp) PyLong_AsLong( \ |
Martin v. Löwis | ceaa77c | 2002-10-16 19:10:03 +0000 | [diff] [blame] | 25 | PyDict_GetItemString((tp)->tp_dict, unnamed_fields_key)) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | PyStructSequence *obj; |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 33 | Py_ssize_t size = REAL_SIZE_TP(type), i; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 34 | |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 35 | obj = PyObject_GC_NewVar(PyStructSequence, type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | if (obj == NULL) |
| 37 | return NULL; |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 38 | for (i = 0; i < size; i++) |
| 39 | obj->ob_item[i] = NULL; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 40 | |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 41 | return (PyObject*)obj; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static PyObject * |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 45 | structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 46 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | PyObject *arg = NULL; |
| 48 | PyObject *dict = NULL; |
| 49 | PyObject *ob; |
| 50 | PyStructSequence *res = NULL; |
| 51 | Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; |
| 52 | static char *kwlist[] = {"sequence", "dict", 0}; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq", |
| 55 | kwlist, &arg, &dict)) |
| 56 | return NULL; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | arg = PySequence_Fast(arg, "constructor requires a sequence"); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | if (!arg) { |
| 61 | return NULL; |
| 62 | } |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 63 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | if (dict && !PyDict_Check(dict)) { |
| 65 | PyErr_Format(PyExc_TypeError, |
| 66 | "%.500s() takes a dict as second arg, if any", |
| 67 | type->tp_name); |
| 68 | Py_DECREF(arg); |
| 69 | return NULL; |
| 70 | } |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | len = PySequence_Fast_GET_SIZE(arg); |
| 73 | min_len = VISIBLE_SIZE_TP(type); |
| 74 | max_len = REAL_SIZE_TP(type); |
| 75 | n_unnamed_fields = UNNAMED_FIELDS_TP(type); |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 76 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | if (min_len != max_len) { |
| 78 | if (len < min_len) { |
| 79 | PyErr_Format(PyExc_TypeError, |
| 80 | "%.500s() takes an at least %zd-sequence (%zd-sequence given)", |
| 81 | type->tp_name, min_len, len); |
| 82 | Py_DECREF(arg); |
| 83 | return NULL; |
| 84 | } |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 85 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | if (len > max_len) { |
| 87 | PyErr_Format(PyExc_TypeError, |
| 88 | "%.500s() takes an at most %zd-sequence (%zd-sequence given)", |
| 89 | type->tp_name, max_len, len); |
| 90 | Py_DECREF(arg); |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
| 94 | else { |
| 95 | if (len != min_len) { |
| 96 | PyErr_Format(PyExc_TypeError, |
| 97 | "%.500s() takes a %zd-sequence (%zd-sequence given)", |
| 98 | type->tp_name, min_len, len); |
| 99 | Py_DECREF(arg); |
| 100 | return NULL; |
| 101 | } |
| 102 | } |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | res = (PyStructSequence*) PyStructSequence_New(type); |
| 105 | if (res == NULL) { |
| 106 | return NULL; |
| 107 | } |
| 108 | for (i = 0; i < len; ++i) { |
| 109 | PyObject *v = PySequence_Fast_GET_ITEM(arg, i); |
| 110 | Py_INCREF(v); |
| 111 | res->ob_item[i] = v; |
| 112 | } |
| 113 | for (; i < max_len; ++i) { |
| 114 | if (dict && (ob = PyDict_GetItemString( |
| 115 | dict, type->tp_members[i-n_unnamed_fields].name))) { |
| 116 | } |
| 117 | else { |
| 118 | ob = Py_None; |
| 119 | } |
| 120 | Py_INCREF(ob); |
| 121 | res->ob_item[i] = ob; |
| 122 | } |
| 123 | |
| 124 | Py_DECREF(arg); |
| 125 | return (PyObject*) res; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 128 | |
| 129 | static PyObject * |
| 130 | structseq_repr(PyStructSequence *obj) |
| 131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | /* buffer and type size were chosen well considered. */ |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 133 | #define REPR_BUFFER_SIZE 512 |
| 134 | #define TYPE_MAXSIZE 100 |
| 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | PyTypeObject *typ = Py_TYPE(obj); |
| 137 | int i, removelast = 0; |
| 138 | Py_ssize_t len; |
| 139 | char buf[REPR_BUFFER_SIZE]; |
| 140 | char *endofbuf, *pbuf = buf; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | /* pointer to end of writeable buffer; safes space for "...)\0" */ |
| 143 | endofbuf= &buf[REPR_BUFFER_SIZE-5]; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | /* "typename(", limited to TYPE_MAXSIZE */ |
| 146 | len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE : |
| 147 | strlen(typ->tp_name); |
| 148 | strncpy(pbuf, typ->tp_name, len); |
| 149 | pbuf += len; |
| 150 | *pbuf++ = '('; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | for (i=0; i < VISIBLE_SIZE(obj); i++) { |
| 153 | PyObject *val, *repr; |
| 154 | char *cname, *crepr; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | cname = typ->tp_members[i].name; |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 157 | if (cname == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | return NULL; |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 159 | val = PyStructSequence_GET_ITEM(obj, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | repr = PyObject_Repr(val); |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 161 | if (repr == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | crepr = _PyUnicode_AsString(repr); |
| 164 | if (crepr == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | Py_DECREF(repr); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | /* + 3: keep space for "=" and ", " */ |
| 170 | len = strlen(cname) + strlen(crepr) + 3; |
| 171 | if ((pbuf+len) <= endofbuf) { |
| 172 | strcpy(pbuf, cname); |
| 173 | pbuf += strlen(cname); |
| 174 | *pbuf++ = '='; |
| 175 | strcpy(pbuf, crepr); |
| 176 | pbuf += strlen(crepr); |
| 177 | *pbuf++ = ','; |
| 178 | *pbuf++ = ' '; |
| 179 | removelast = 1; |
| 180 | Py_DECREF(repr); |
| 181 | } |
| 182 | else { |
| 183 | strcpy(pbuf, "..."); |
| 184 | pbuf += 3; |
| 185 | removelast = 0; |
| 186 | Py_DECREF(repr); |
| 187 | break; |
| 188 | } |
| 189 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | if (removelast) { |
| 191 | /* overwrite last ", " */ |
| 192 | pbuf-=2; |
| 193 | } |
| 194 | *pbuf++ = ')'; |
| 195 | *pbuf = '\0'; |
| 196 | |
| 197 | return PyUnicode_FromString(buf); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static PyObject * |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 201 | structseq_reduce(PyStructSequence* self) |
| 202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | PyObject* tup; |
| 204 | PyObject* dict; |
| 205 | PyObject* result; |
| 206 | Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields; |
| 207 | int i; |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | n_fields = REAL_SIZE(self); |
| 210 | n_visible_fields = VISIBLE_SIZE(self); |
| 211 | n_unnamed_fields = UNNAMED_FIELDS(self); |
| 212 | tup = PyTuple_New(n_visible_fields); |
| 213 | if (!tup) { |
| 214 | return NULL; |
| 215 | } |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | dict = PyDict_New(); |
| 218 | if (!dict) { |
| 219 | Py_DECREF(tup); |
| 220 | return NULL; |
| 221 | } |
Michael W. Hudson | ce358e3 | 2002-03-06 17:07:49 +0000 | [diff] [blame] | 222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | for (i = 0; i < n_visible_fields; i++) { |
| 224 | Py_INCREF(self->ob_item[i]); |
| 225 | PyTuple_SET_ITEM(tup, i, self->ob_item[i]); |
| 226 | } |
Michael W. Hudson | 70ffddf | 2002-03-07 15:13:40 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | for (; i < n_fields; i++) { |
| 229 | char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name; |
| 230 | PyDict_SetItemString(dict, n, |
| 231 | self->ob_item[i]); |
| 232 | } |
Michael W. Hudson | 70ffddf | 2002-03-07 15:13:40 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict); |
| 235 | |
| 236 | Py_DECREF(tup); |
| 237 | Py_DECREF(dict); |
| 238 | |
| 239 | return result; |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 242 | static PyMethodDef structseq_methods[] = { |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 243 | {"__reduce__", (PyCFunction)structseq_reduce, METH_NOARGS, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | {NULL, NULL} |
Michael W. Hudson | 7bb466a | 2002-03-05 13:27:58 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 247 | static PyTypeObject _struct_sequence_template = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 249 | NULL, /* tp_name */ |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 250 | sizeof(PyStructSequence) - sizeof(PyObject *), /* tp_basicsize */ |
| 251 | sizeof(PyObject *), /* tp_itemsize */ |
| 252 | 0, /* tp_dealloc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | 0, /* tp_print */ |
| 254 | 0, /* tp_getattr */ |
| 255 | 0, /* tp_setattr */ |
| 256 | 0, /* tp_reserved */ |
| 257 | (reprfunc)structseq_repr, /* tp_repr */ |
| 258 | 0, /* tp_as_number */ |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 259 | 0, /* tp_as_sequence */ |
| 260 | 0, /* tp_as_mapping */ |
| 261 | 0, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | 0, /* tp_call */ |
| 263 | 0, /* tp_str */ |
| 264 | 0, /* tp_getattro */ |
| 265 | 0, /* tp_setattro */ |
| 266 | 0, /* tp_as_buffer */ |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 267 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | NULL, /* tp_doc */ |
| 269 | 0, /* tp_traverse */ |
| 270 | 0, /* tp_clear */ |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 271 | 0, /* tp_richcompare */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | 0, /* tp_weaklistoffset */ |
| 273 | 0, /* tp_iter */ |
| 274 | 0, /* tp_iternext */ |
| 275 | structseq_methods, /* tp_methods */ |
| 276 | NULL, /* tp_members */ |
| 277 | 0, /* tp_getset */ |
| 278 | 0, /* tp_base */ |
| 279 | 0, /* tp_dict */ |
| 280 | 0, /* tp_descr_get */ |
| 281 | 0, /* tp_descr_set */ |
| 282 | 0, /* tp_dictoffset */ |
| 283 | 0, /* tp_init */ |
| 284 | 0, /* tp_alloc */ |
| 285 | structseq_new, /* tp_new */ |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | void |
| 289 | PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) |
| 290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | PyObject *dict; |
| 292 | PyMemberDef* members; |
| 293 | int n_members, n_unnamed_members, i, k; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 294 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 295 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | /* if the type object was chained, unchain it first |
| 297 | before overwriting its storage */ |
| 298 | if (type->ob_base.ob_base._ob_next) { |
| 299 | _Py_ForgetReference((PyObject*)type); |
| 300 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 301 | #endif |
| 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | n_unnamed_members = 0; |
| 304 | for (i = 0; desc->fields[i].name != NULL; ++i) |
| 305 | if (desc->fields[i].name == PyStructSequence_UnnamedField) |
| 306 | n_unnamed_members++; |
| 307 | n_members = i; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject)); |
Benjamin Peterson | ccabcd4 | 2010-07-07 20:54:01 +0000 | [diff] [blame^] | 310 | type->tp_base = &PyTuple_Type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | type->tp_name = desc->name; |
| 312 | type->tp_doc = desc->doc; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1); |
| 315 | if (members == NULL) |
| 316 | return; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | for (i = k = 0; i < n_members; ++i) { |
| 319 | if (desc->fields[i].name == PyStructSequence_UnnamedField) |
| 320 | continue; |
| 321 | members[k].name = desc->fields[i].name; |
| 322 | members[k].type = T_OBJECT; |
| 323 | members[k].offset = offsetof(PyStructSequence, ob_item) |
| 324 | + i * sizeof(PyObject*); |
| 325 | members[k].flags = READONLY; |
| 326 | members[k].doc = desc->fields[i].doc; |
| 327 | k++; |
| 328 | } |
| 329 | members[k].name = NULL; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | type->tp_members = members; |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | if (PyType_Ready(type) < 0) |
| 334 | return; |
| 335 | Py_INCREF(type); |
Neal Norwitz | 2f99b24 | 2008-08-24 05:48:10 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | dict = type->tp_dict; |
| 338 | #define SET_DICT_FROM_INT(key, value) \ |
| 339 | do { \ |
| 340 | PyObject *v = PyLong_FromLong((long) value); \ |
| 341 | if (v != NULL) { \ |
| 342 | PyDict_SetItemString(dict, key, v); \ |
| 343 | Py_DECREF(v); \ |
| 344 | } \ |
| 345 | } while (0) |
| 346 | |
| 347 | SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence); |
| 348 | SET_DICT_FROM_INT(real_length_key, n_members); |
| 349 | SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members); |
Guido van Rossum | e82f75a | 2001-10-18 20:47:51 +0000 | [diff] [blame] | 350 | } |