blob: e48165dcd0087f5c34c99a8947260ea04246681e [file] [log] [blame]
Guido van Rossume82f75a2001-10-18 20:47:51 +00001/* 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 Rossume82f75a2001-10-18 20:47:51 +00006
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02007static const char visible_length_key[] = "n_sequence_fields";
8static const char real_length_key[] = "n_fields";
9static const char unnamed_fields_key[] = "n_unnamed_fields";
Guido van Rossume82f75a2001-10-18 20:47:51 +000010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011/* Fields with this name have only a field index, not a field name.
Martin v. Löwisf607bda2002-10-16 18:27:39 +000012 They are only allowed for indices < n_visible_fields. */
13char *PyStructSequence_UnnamedField = "unnamed field";
Victor Stinner26f91992013-07-17 01:22:45 +020014_Py_IDENTIFIER(n_sequence_fields);
15_Py_IDENTIFIER(n_fields);
16_Py_IDENTIFIER(n_unnamed_fields);
Martin v. Löwisf607bda2002-10-16 18:27:39 +000017
Christian Heimes90aa7642007-12-19 02:45:37 +000018#define VISIBLE_SIZE(op) Py_SIZE(op)
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030019#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \
Victor Stinner26f91992013-07-17 01:22:45 +020020 _PyDict_GetItemId((tp)->tp_dict, &PyId_n_sequence_fields))
Guido van Rossume82f75a2001-10-18 20:47:51 +000021
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030022#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \
Victor Stinner26f91992013-07-17 01:22:45 +020023 _PyDict_GetItemId((tp)->tp_dict, &PyId_n_fields))
Christian Heimes90aa7642007-12-19 02:45:37 +000024#define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
Guido van Rossume82f75a2001-10-18 20:47:51 +000025
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030026#define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \
Victor Stinner26f91992013-07-17 01:22:45 +020027 _PyDict_GetItemId((tp)->tp_dict, &PyId_n_unnamed_fields))
Christian Heimes90aa7642007-12-19 02:45:37 +000028#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
Martin v. Löwisceaa77c2002-10-16 19:10:03 +000029
Guido van Rossume82f75a2001-10-18 20:47:51 +000030
31PyObject *
32PyStructSequence_New(PyTypeObject *type)
33{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 PyStructSequence *obj;
Benjamin Petersonccabcd42010-07-07 20:54:01 +000035 Py_ssize_t size = REAL_SIZE_TP(type), i;
Christian Heimesd32ed6f2008-01-14 18:49:24 +000036
Benjamin Petersonccabcd42010-07-07 20:54:01 +000037 obj = PyObject_GC_NewVar(PyStructSequence, type, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 if (obj == NULL)
39 return NULL;
Benjamin Petersond02441e2010-07-08 22:33:03 +000040 /* 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 Petersonccabcd42010-07-07 20:54:01 +000043 for (i = 0; i < size; i++)
44 obj->ob_item[i] = NULL;
Guido van Rossume82f75a2001-10-18 20:47:51 +000045
Benjamin Petersonccabcd42010-07-07 20:54:01 +000046 return (PyObject*)obj;
Thomas Woutersed03b412007-08-28 21:37:11 +000047}
48
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000049void
50PyStructSequence_SetItem(PyObject* op, Py_ssize_t i, PyObject* v)
51{
52 PyStructSequence_SET_ITEM(op, i, v);
53}
54
55PyObject*
56PyStructSequence_GetItem(PyObject* op, Py_ssize_t i)
57{
58 return PyStructSequence_GET_ITEM(op, i);
59}
60
Benjamin Petersond02441e2010-07-08 22:33:03 +000061static void
62structseq_dealloc(PyStructSequence *obj)
63{
64 Py_ssize_t i, size;
Victor Stinner26f91992013-07-17 01:22:45 +020065
Benjamin Petersond02441e2010-07-08 22:33:03 +000066 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
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020073/*[clinic input]
74class structseq "PyStructSequence *" "NULL"
75[clinic start generated code]*/
76/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9d781c6922c77752]*/
77
78#include "clinic/structseq.c.h"
79
80/*[clinic input]
81@classmethod
82structseq.__new__ as structseq_new
83 sequence as arg: object
84 dict: object = NULL
85[clinic start generated code]*/
86
Thomas Woutersed03b412007-08-28 21:37:11 +000087static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020088structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
89/*[clinic end generated code: output=baa082e788b171da input=9b44810243907377]*/
Guido van Rossume82f75a2001-10-18 20:47:51 +000090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 PyObject *ob;
92 PyStructSequence *res = NULL;
93 Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
Guido van Rossume82f75a2001-10-18 20:47:51 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 arg = PySequence_Fast(arg, "constructor requires a sequence");
Michael W. Hudsonce358e32002-03-06 17:07:49 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 if (!arg) {
98 return NULL;
99 }
Guido van Rossume82f75a2001-10-18 20:47:51 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (dict && !PyDict_Check(dict)) {
102 PyErr_Format(PyExc_TypeError,
103 "%.500s() takes a dict as second arg, if any",
104 type->tp_name);
105 Py_DECREF(arg);
106 return NULL;
107 }
Guido van Rossume82f75a2001-10-18 20:47:51 +0000108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 len = PySequence_Fast_GET_SIZE(arg);
110 min_len = VISIBLE_SIZE_TP(type);
111 max_len = REAL_SIZE_TP(type);
112 n_unnamed_fields = UNNAMED_FIELDS_TP(type);
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (min_len != max_len) {
115 if (len < min_len) {
116 PyErr_Format(PyExc_TypeError,
Antoine Pitrou4b3c7842012-02-15 02:52:58 +0100117 "%.500s() takes an at least %zd-sequence (%zd-sequence given)",
118 type->tp_name, min_len, len);
119 Py_DECREF(arg);
120 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 }
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (len > max_len) {
124 PyErr_Format(PyExc_TypeError,
Antoine Pitrou4b3c7842012-02-15 02:52:58 +0100125 "%.500s() takes an at most %zd-sequence (%zd-sequence given)",
126 type->tp_name, max_len, len);
127 Py_DECREF(arg);
128 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 }
130 }
131 else {
132 if (len != min_len) {
133 PyErr_Format(PyExc_TypeError,
Antoine Pitrou4b3c7842012-02-15 02:52:58 +0100134 "%.500s() takes a %zd-sequence (%zd-sequence given)",
135 type->tp_name, min_len, len);
136 Py_DECREF(arg);
137 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 }
139 }
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 res = (PyStructSequence*) PyStructSequence_New(type);
142 if (res == NULL) {
Antoine Pitrou37784ba2012-02-15 02:51:43 +0100143 Py_DECREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 return NULL;
145 }
146 for (i = 0; i < len; ++i) {
147 PyObject *v = PySequence_Fast_GET_ITEM(arg, i);
148 Py_INCREF(v);
149 res->ob_item[i] = v;
150 }
151 for (; i < max_len; ++i) {
152 if (dict && (ob = PyDict_GetItemString(
153 dict, type->tp_members[i-n_unnamed_fields].name))) {
154 }
155 else {
156 ob = Py_None;
157 }
158 Py_INCREF(ob);
159 res->ob_item[i] = ob;
160 }
161
162 Py_DECREF(arg);
163 return (PyObject*) res;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000164}
165
Guido van Rossume82f75a2001-10-18 20:47:51 +0000166
167static PyObject *
168structseq_repr(PyStructSequence *obj)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 /* buffer and type size were chosen well considered. */
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000171#define REPR_BUFFER_SIZE 512
172#define TYPE_MAXSIZE 100
173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PyTypeObject *typ = Py_TYPE(obj);
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300175 Py_ssize_t i;
176 int removelast = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 Py_ssize_t len;
178 char buf[REPR_BUFFER_SIZE];
179 char *endofbuf, *pbuf = buf;
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 /* pointer to end of writeable buffer; safes space for "...)\0" */
182 endofbuf= &buf[REPR_BUFFER_SIZE-5];
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 /* "typename(", limited to TYPE_MAXSIZE */
Victor Stinnerea3592d2019-03-20 00:32:11 +0100185 assert(TYPE_MAXSIZE < sizeof(buf));
186 len = strlen(typ->tp_name);
187 if (len <= TYPE_MAXSIZE) {
188 strcpy(pbuf, typ->tp_name);
189 pbuf += len;
190 }
191 else {
192 strncpy(pbuf, typ->tp_name, TYPE_MAXSIZE);
193 pbuf += TYPE_MAXSIZE;
194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 *pbuf++ = '(';
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 for (i=0; i < VISIBLE_SIZE(obj); i++) {
198 PyObject *val, *repr;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200199 const char *cname, *crepr;
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 cname = typ->tp_members[i].name;
Benjamin Petersond02441e2010-07-08 22:33:03 +0000202 if (cname == NULL) {
Serhiy Storchaka783bed42019-03-14 10:47:27 +0200203 PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %zd name is NULL"
Benjamin Petersond02441e2010-07-08 22:33:03 +0000204 " for type %.500s", i, typ->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 return NULL;
Benjamin Petersond02441e2010-07-08 22:33:03 +0000206 }
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000207 val = PyStructSequence_GET_ITEM(obj, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 repr = PyObject_Repr(val);
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000209 if (repr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200211 crepr = PyUnicode_AsUTF8(repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (crepr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 Py_DECREF(repr);
214 return NULL;
215 }
216
217 /* + 3: keep space for "=" and ", " */
218 len = strlen(cname) + strlen(crepr) + 3;
219 if ((pbuf+len) <= endofbuf) {
220 strcpy(pbuf, cname);
221 pbuf += strlen(cname);
222 *pbuf++ = '=';
223 strcpy(pbuf, crepr);
224 pbuf += strlen(crepr);
225 *pbuf++ = ',';
226 *pbuf++ = ' ';
227 removelast = 1;
228 Py_DECREF(repr);
229 }
230 else {
231 strcpy(pbuf, "...");
232 pbuf += 3;
233 removelast = 0;
234 Py_DECREF(repr);
235 break;
236 }
237 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (removelast) {
239 /* overwrite last ", " */
240 pbuf-=2;
241 }
242 *pbuf++ = ')';
243 *pbuf = '\0';
244
245 return PyUnicode_FromString(buf);
Guido van Rossume82f75a2001-10-18 20:47:51 +0000246}
247
248static PyObject *
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000249structseq_reduce(PyStructSequence* self)
250{
Victor Stinner9a146ee2013-07-17 13:41:39 +0200251 PyObject* tup = NULL;
252 PyObject* dict = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 PyObject* result;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300254 Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i;
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 n_fields = REAL_SIZE(self);
257 n_visible_fields = VISIBLE_SIZE(self);
258 n_unnamed_fields = UNNAMED_FIELDS(self);
259 tup = PyTuple_New(n_visible_fields);
Victor Stinner9a146ee2013-07-17 13:41:39 +0200260 if (!tup)
261 goto error;
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 dict = PyDict_New();
Victor Stinner9a146ee2013-07-17 13:41:39 +0200264 if (!dict)
265 goto error;
Michael W. Hudsonce358e32002-03-06 17:07:49 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 for (i = 0; i < n_visible_fields; i++) {
268 Py_INCREF(self->ob_item[i]);
269 PyTuple_SET_ITEM(tup, i, self->ob_item[i]);
270 }
Michael W. Hudson70ffddf2002-03-07 15:13:40 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 for (; i < n_fields; i++) {
Serhiy Storchaka007d7ff2016-11-22 07:58:08 +0200273 const char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
Victor Stinner9a146ee2013-07-17 13:41:39 +0200274 if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0)
275 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 }
Michael W. Hudson70ffddf2002-03-07 15:13:40 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
279
280 Py_DECREF(tup);
281 Py_DECREF(dict);
282
283 return result;
Victor Stinner9a146ee2013-07-17 13:41:39 +0200284
285error:
286 Py_XDECREF(tup);
287 Py_XDECREF(dict);
288 return NULL;
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000289}
290
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000291static PyMethodDef structseq_methods[] = {
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000292 {"__reduce__", (PyCFunction)structseq_reduce, METH_NOARGS, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 {NULL, NULL}
Michael W. Hudson7bb466a2002-03-05 13:27:58 +0000294};
295
Guido van Rossume82f75a2001-10-18 20:47:51 +0000296static PyTypeObject _struct_sequence_template = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyVarObject_HEAD_INIT(&PyType_Type, 0)
298 NULL, /* tp_name */
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000299 sizeof(PyStructSequence) - sizeof(PyObject *), /* tp_basicsize */
300 sizeof(PyObject *), /* tp_itemsize */
Benjamin Petersond02441e2010-07-08 22:33:03 +0000301 (destructor)structseq_dealloc, /* tp_dealloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 0, /* tp_print */
303 0, /* tp_getattr */
304 0, /* tp_setattr */
305 0, /* tp_reserved */
306 (reprfunc)structseq_repr, /* tp_repr */
307 0, /* tp_as_number */
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000308 0, /* tp_as_sequence */
309 0, /* tp_as_mapping */
310 0, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 0, /* tp_call */
312 0, /* tp_str */
313 0, /* tp_getattro */
314 0, /* tp_setattro */
315 0, /* tp_as_buffer */
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000316 Py_TPFLAGS_DEFAULT, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 NULL, /* tp_doc */
318 0, /* tp_traverse */
319 0, /* tp_clear */
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000320 0, /* tp_richcompare */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 0, /* tp_weaklistoffset */
322 0, /* tp_iter */
323 0, /* tp_iternext */
324 structseq_methods, /* tp_methods */
325 NULL, /* tp_members */
326 0, /* tp_getset */
327 0, /* tp_base */
328 0, /* tp_dict */
329 0, /* tp_descr_get */
330 0, /* tp_descr_set */
331 0, /* tp_dictoffset */
332 0, /* tp_init */
333 0, /* tp_alloc */
334 structseq_new, /* tp_new */
Guido van Rossume82f75a2001-10-18 20:47:51 +0000335};
336
Victor Stinner1c8f0592013-07-22 22:24:54 +0200337int
338PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
Guido van Rossume82f75a2001-10-18 20:47:51 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 PyObject *dict;
341 PyMemberDef* members;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300342 Py_ssize_t n_members, n_unnamed_members, i, k;
Victor Stinner1c8f0592013-07-22 22:24:54 +0200343 PyObject *v;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000344
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 /* if the type object was chained, unchain it first
347 before overwriting its storage */
348 if (type->ob_base.ob_base._ob_next) {
349 _Py_ForgetReference((PyObject*)type);
350 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351#endif
352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 n_unnamed_members = 0;
354 for (i = 0; desc->fields[i].name != NULL; ++i)
355 if (desc->fields[i].name == PyStructSequence_UnnamedField)
356 n_unnamed_members++;
357 n_members = i;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject));
Benjamin Petersonccabcd42010-07-07 20:54:01 +0000360 type->tp_base = &PyTuple_Type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 type->tp_name = desc->name;
362 type->tp_doc = desc->doc;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1);
Victor Stinner1c8f0592013-07-22 22:24:54 +0200365 if (members == NULL) {
366 PyErr_NoMemory();
367 return -1;
368 }
Guido van Rossume82f75a2001-10-18 20:47:51 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 for (i = k = 0; i < n_members; ++i) {
371 if (desc->fields[i].name == PyStructSequence_UnnamedField)
372 continue;
373 members[k].name = desc->fields[i].name;
374 members[k].type = T_OBJECT;
375 members[k].offset = offsetof(PyStructSequence, ob_item)
376 + i * sizeof(PyObject*);
377 members[k].flags = READONLY;
378 members[k].doc = desc->fields[i].doc;
379 k++;
380 }
381 members[k].name = NULL;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 type->tp_members = members;
Guido van Rossume82f75a2001-10-18 20:47:51 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (PyType_Ready(type) < 0)
Victor Stinner1c8f0592013-07-22 22:24:54 +0200386 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 Py_INCREF(type);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 dict = type->tp_dict;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300390#define SET_DICT_FROM_SIZE(key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 do { \
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300392 v = PyLong_FromSsize_t(value); \
Victor Stinner1c8f0592013-07-22 22:24:54 +0200393 if (v == NULL) \
394 return -1; \
395 if (PyDict_SetItemString(dict, key, v) < 0) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 Py_DECREF(v); \
Victor Stinner1c8f0592013-07-22 22:24:54 +0200397 return -1; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 } \
Victor Stinner1c8f0592013-07-22 22:24:54 +0200399 Py_DECREF(v); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 } while (0)
401
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300402 SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence);
403 SET_DICT_FROM_SIZE(real_length_key, n_members);
404 SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members);
Victor Stinner1c8f0592013-07-22 22:24:54 +0200405
406 return 0;
407}
408
409void
410PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
411{
412 (void)PyStructSequence_InitType2(type, desc);
Guido van Rossume82f75a2001-10-18 20:47:51 +0000413}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000414
415PyTypeObject*
416PyStructSequence_NewType(PyStructSequence_Desc *desc)
417{
Victor Stinner26f91992013-07-17 01:22:45 +0200418 PyTypeObject *result;
419
420 result = (PyTypeObject*)PyType_GenericAlloc(&PyType_Type, 0);
Victor Stinner1c8f0592013-07-22 22:24:54 +0200421 if (result == NULL)
422 return NULL;
423 if (PyStructSequence_InitType2(result, desc) < 0) {
424 Py_DECREF(result);
425 return NULL;
Stefan Krah6b962862012-08-19 11:20:41 +0200426 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000427 return result;
428}
Victor Stinner26f91992013-07-17 01:22:45 +0200429
430int _PyStructSequence_Init(void)
431{
432 if (_PyUnicode_FromId(&PyId_n_sequence_fields) == NULL
433 || _PyUnicode_FromId(&PyId_n_fields) == NULL
434 || _PyUnicode_FromId(&PyId_n_unnamed_fields) == NULL)
435 return -1;
436
437 return 0;
438}