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