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