/src/cpython/Objects/structseq.c
Line | Count | Source |
1 | | /* Implementation helper: a struct that looks like a tuple. |
2 | | See timemodule and posixmodule for example uses. |
3 | | |
4 | | The structseq helper is considered an internal CPython implementation |
5 | | detail. Docs for modules using structseqs should call them |
6 | | "named tuples" (be sure to include a space between the two |
7 | | words and add a link back to the term in Docs/glossary.rst). |
8 | | */ |
9 | | |
10 | | #include "Python.h" |
11 | | #include "pycore_initconfig.h" // _PyStatus_OK() |
12 | | #include "pycore_modsupport.h" // _PyArg_NoPositional() |
13 | | #include "pycore_object.h" // _PyObject_GC_TRACK() |
14 | | #include "pycore_structseq.h" // PyStructSequence_InitType() |
15 | | #include "pycore_tuple.h" // _PyTuple_RESET_HASH_CACHE() |
16 | | #include "pycore_typeobject.h" // _PyStaticType_FiniBuiltin() |
17 | | |
18 | | static const char visible_length_key[] = "n_sequence_fields"; |
19 | | static const char real_length_key[] = "n_fields"; |
20 | | static const char unnamed_fields_key[] = "n_unnamed_fields"; |
21 | | static const char match_args_key[] = "__match_args__"; |
22 | | |
23 | | /* Fields with this name have only a field index, not a field name. |
24 | | They are only allowed for indices < n_visible_fields. */ |
25 | | const char * const PyStructSequence_UnnamedField = "unnamed field"; |
26 | | |
27 | | static Py_ssize_t |
28 | | get_type_attr_as_size(PyTypeObject *tp, PyObject *name) |
29 | 1.03M | { |
30 | 1.03M | PyObject *v = PyDict_GetItemWithError(_PyType_GetDict(tp), name); |
31 | | |
32 | 1.03M | if (v == NULL) { |
33 | 0 | if (PyErr_Occurred()) { |
34 | 0 | return -1; |
35 | 0 | } |
36 | 0 | PyErr_Format(PyExc_TypeError, |
37 | 0 | "Missed attribute '%U' of type %s", |
38 | 0 | name, tp->tp_name); |
39 | 0 | return -1; |
40 | 0 | } |
41 | 1.03M | return PyLong_AsSsize_t(v); |
42 | 1.03M | } |
43 | | |
44 | 0 | #define VISIBLE_SIZE(op) Py_SIZE(op) |
45 | | #define VISIBLE_SIZE_TP(tp) \ |
46 | 513k | get_type_attr_as_size(tp, &_Py_ID(n_sequence_fields)) |
47 | | #define REAL_SIZE_TP(tp) \ |
48 | 513k | get_type_attr_as_size(tp, &_Py_ID(n_fields)) |
49 | 502k | #define REAL_SIZE(op) get_real_size((PyObject *)op) |
50 | | |
51 | | #define UNNAMED_FIELDS_TP(tp) \ |
52 | 10.9k | get_type_attr_as_size(tp, &_Py_ID(n_unnamed_fields)) |
53 | 0 | #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op)) |
54 | | |
55 | | static Py_ssize_t |
56 | | get_real_size(PyObject *op) |
57 | 502k | { |
58 | | // Compute the real size from the visible size (i.e., Py_SIZE()) and the |
59 | | // number of non-sequence fields accounted for in tp_basicsize. |
60 | 502k | Py_ssize_t hidden = Py_TYPE(op)->tp_basicsize - offsetof(PyStructSequence, ob_item); |
61 | 502k | return Py_SIZE(op) + hidden / sizeof(PyObject *); |
62 | 502k | } |
63 | | |
64 | | PyObject * |
65 | | PyStructSequence_New(PyTypeObject *type) |
66 | 502k | { |
67 | 502k | PyStructSequence *obj; |
68 | 502k | Py_ssize_t size = REAL_SIZE_TP(type), i; |
69 | 502k | if (size < 0) { |
70 | 0 | return NULL; |
71 | 0 | } |
72 | 502k | Py_ssize_t vsize = VISIBLE_SIZE_TP(type); |
73 | 502k | if (vsize < 0) { |
74 | 0 | return NULL; |
75 | 0 | } |
76 | | |
77 | 502k | obj = PyObject_GC_NewVar(PyStructSequence, type, size); |
78 | 502k | if (obj == NULL) |
79 | 0 | return NULL; |
80 | 502k | _PyTuple_RESET_HASH_CACHE(obj); |
81 | | /* Hack the size of the variable object, so invisible fields don't appear |
82 | | to Python code. */ |
83 | 502k | Py_SET_SIZE(obj, vsize); |
84 | 9.96M | for (i = 0; i < size; i++) |
85 | 9.46M | obj->ob_item[i] = NULL; |
86 | | |
87 | 502k | return (PyObject*)obj; |
88 | 502k | } |
89 | | |
90 | | void |
91 | | PyStructSequence_SetItem(PyObject *op, Py_ssize_t index, PyObject *value) |
92 | 9.34M | { |
93 | 9.34M | PyTupleObject *tuple = _PyTuple_CAST(op); |
94 | 9.34M | assert(0 <= index); |
95 | | #ifndef NDEBUG |
96 | | Py_ssize_t n_fields = REAL_SIZE(op); |
97 | | assert(n_fields >= 0); |
98 | | assert(index < n_fields); |
99 | | #endif |
100 | 9.34M | tuple->ob_item[index] = value; |
101 | 9.34M | } |
102 | | |
103 | | PyObject* |
104 | | PyStructSequence_GetItem(PyObject *op, Py_ssize_t index) |
105 | 1.96k | { |
106 | 1.96k | assert(0 <= index); |
107 | | #ifndef NDEBUG |
108 | | Py_ssize_t n_fields = REAL_SIZE(op); |
109 | | assert(n_fields >= 0); |
110 | | assert(index < n_fields); |
111 | | #endif |
112 | 1.96k | return PyTuple_GET_ITEM(op, index); |
113 | 1.96k | } |
114 | | |
115 | | |
116 | | static int |
117 | | structseq_traverse(PyObject *op, visitproc visit, void *arg) |
118 | 0 | { |
119 | 0 | PyStructSequence *obj = (PyStructSequence *)op; |
120 | 0 | if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
121 | 0 | Py_VISIT(Py_TYPE(obj)); |
122 | 0 | } |
123 | 0 | Py_ssize_t i, size; |
124 | 0 | size = REAL_SIZE(obj); |
125 | 0 | for (i = 0; i < size; ++i) { |
126 | 0 | Py_VISIT(obj->ob_item[i]); |
127 | 0 | } |
128 | 0 | return 0; |
129 | 0 | } |
130 | | |
131 | | static void |
132 | | structseq_dealloc(PyObject *op) |
133 | 502k | { |
134 | 502k | PyStructSequence *obj = (PyStructSequence *)op; |
135 | 502k | Py_ssize_t i, size; |
136 | 502k | PyObject_GC_UnTrack(obj); |
137 | | |
138 | 502k | PyTypeObject *tp = Py_TYPE(obj); |
139 | | // gh-122527: We can't use REAL_SIZE_TP() or any macros that access the |
140 | | // type's dictionary here, because the dictionary may have already been |
141 | | // cleared by the garbage collector. |
142 | 502k | size = REAL_SIZE(obj); |
143 | 9.96M | for (i = 0; i < size; ++i) { |
144 | 9.45M | Py_XDECREF(obj->ob_item[i]); |
145 | 9.45M | } |
146 | 502k | PyObject_GC_Del(obj); |
147 | 502k | if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
148 | 502k | Py_DECREF(tp); |
149 | 502k | } |
150 | 502k | } |
151 | | |
152 | | /*[clinic input] |
153 | | class structseq "PyStructSequence *" "NULL" |
154 | | [clinic start generated code]*/ |
155 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9d781c6922c77752]*/ |
156 | | |
157 | | #include "clinic/structseq.c.h" |
158 | | |
159 | | /*[clinic input] |
160 | | @classmethod |
161 | | structseq.__new__ as structseq_new |
162 | | sequence as arg: object |
163 | | dict: object(c_default="NULL") = {} |
164 | | [clinic start generated code]*/ |
165 | | |
166 | | static PyObject * |
167 | | structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) |
168 | | /*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/ |
169 | 10.9k | { |
170 | 10.9k | PyStructSequence *res = NULL; |
171 | 10.9k | Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; |
172 | | |
173 | 10.9k | min_len = VISIBLE_SIZE_TP(type); |
174 | 10.9k | if (min_len < 0) { |
175 | 0 | return NULL; |
176 | 0 | } |
177 | 10.9k | max_len = REAL_SIZE_TP(type); |
178 | 10.9k | if (max_len < 0) { |
179 | 0 | return NULL; |
180 | 0 | } |
181 | 10.9k | n_unnamed_fields = UNNAMED_FIELDS_TP(type); |
182 | 10.9k | if (n_unnamed_fields < 0) { |
183 | 0 | return NULL; |
184 | 0 | } |
185 | | |
186 | 10.9k | arg = PySequence_Fast(arg, "constructor requires a sequence"); |
187 | | |
188 | 10.9k | if (!arg) { |
189 | 0 | return NULL; |
190 | 0 | } |
191 | | |
192 | 10.9k | if (dict && !PyDict_Check(dict)) { |
193 | 0 | PyErr_Format(PyExc_TypeError, |
194 | 0 | "%.500s() takes a dict as second arg, if any", |
195 | 0 | type->tp_name); |
196 | 0 | Py_DECREF(arg); |
197 | 0 | return NULL; |
198 | 0 | } |
199 | | |
200 | 10.9k | len = PySequence_Fast_GET_SIZE(arg); |
201 | 10.9k | if (min_len != max_len) { |
202 | 10.9k | if (len < min_len) { |
203 | 0 | PyErr_Format(PyExc_TypeError, |
204 | 0 | "%.500s() takes an at least %zd-sequence (%zd-sequence given)", |
205 | 0 | type->tp_name, min_len, len); |
206 | 0 | Py_DECREF(arg); |
207 | 0 | return NULL; |
208 | 0 | } |
209 | | |
210 | 10.9k | if (len > max_len) { |
211 | 0 | PyErr_Format(PyExc_TypeError, |
212 | 0 | "%.500s() takes an at most %zd-sequence (%zd-sequence given)", |
213 | 0 | type->tp_name, max_len, len); |
214 | 0 | Py_DECREF(arg); |
215 | 0 | return NULL; |
216 | 0 | } |
217 | 10.9k | } |
218 | 0 | else { |
219 | 0 | if (len != min_len) { |
220 | 0 | PyErr_Format(PyExc_TypeError, |
221 | 0 | "%.500s() takes a %zd-sequence (%zd-sequence given)", |
222 | 0 | type->tp_name, min_len, len); |
223 | 0 | Py_DECREF(arg); |
224 | 0 | return NULL; |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | 10.9k | res = (PyStructSequence*) PyStructSequence_New(type); |
229 | 10.9k | if (res == NULL) { |
230 | 0 | Py_DECREF(arg); |
231 | 0 | return NULL; |
232 | 0 | } |
233 | 109k | for (i = 0; i < len; ++i) { |
234 | 98.6k | PyObject *v = PySequence_Fast_GET_ITEM(arg, i); |
235 | 98.6k | res->ob_item[i] = Py_NewRef(v); |
236 | 98.6k | } |
237 | 10.9k | Py_DECREF(arg); |
238 | 10.9k | if (dict != NULL && PyDict_GET_SIZE(dict) > 0) { |
239 | 0 | Py_ssize_t n_found_keys = 0; |
240 | 0 | for (i = len; i < max_len; ++i) { |
241 | 0 | PyObject *ob = NULL; |
242 | 0 | const char *name = type->tp_members[i - n_unnamed_fields].name; |
243 | 0 | if (PyDict_GetItemStringRef(dict, name, &ob) < 0) { |
244 | 0 | Py_DECREF(res); |
245 | 0 | return NULL; |
246 | 0 | } |
247 | 0 | if (ob == NULL) { |
248 | 0 | ob = Py_NewRef(Py_None); |
249 | 0 | } |
250 | 0 | else { |
251 | 0 | ++n_found_keys; |
252 | 0 | } |
253 | 0 | res->ob_item[i] = ob; |
254 | 0 | } |
255 | 0 | if (PyDict_GET_SIZE(dict) > n_found_keys) { |
256 | 0 | PyErr_Format(PyExc_TypeError, |
257 | 0 | "%.500s() got duplicate or unexpected field name(s)", |
258 | 0 | type->tp_name); |
259 | 0 | Py_DECREF(res); |
260 | 0 | return NULL; |
261 | 0 | } |
262 | 10.9k | } else { |
263 | 32.8k | for (i = len; i < max_len; ++i) { |
264 | 21.9k | res->ob_item[i] = Py_NewRef(Py_None); |
265 | 21.9k | } |
266 | 10.9k | } |
267 | | |
268 | 10.9k | _PyObject_GC_TRACK(res); |
269 | 10.9k | return (PyObject*) res; |
270 | 10.9k | } |
271 | | |
272 | | |
273 | | static PyObject * |
274 | | structseq_repr(PyObject *op) |
275 | 0 | { |
276 | 0 | PyStructSequence *obj = (PyStructSequence *)op; |
277 | 0 | PyTypeObject *typ = Py_TYPE(obj); |
278 | | |
279 | | // count 5 characters per item: "x=1, " |
280 | 0 | Py_ssize_t type_name_len = strlen(typ->tp_name); |
281 | 0 | Py_ssize_t prealloc = (type_name_len + 1 |
282 | 0 | + VISIBLE_SIZE(obj) * 5 + 1); |
283 | 0 | PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc); |
284 | 0 | if (writer == NULL) { |
285 | 0 | return NULL; |
286 | 0 | } |
287 | | |
288 | | // Write "typename(" |
289 | 0 | if (PyUnicodeWriter_WriteUTF8(writer, typ->tp_name, type_name_len) < 0) { |
290 | 0 | goto error; |
291 | 0 | } |
292 | 0 | if (PyUnicodeWriter_WriteChar(writer, '(') < 0) { |
293 | 0 | goto error; |
294 | 0 | } |
295 | | |
296 | 0 | for (Py_ssize_t i=0; i < VISIBLE_SIZE(obj); i++) { |
297 | 0 | if (i > 0) { |
298 | | // Write ", " |
299 | 0 | if (PyUnicodeWriter_WriteChar(writer, ',') < 0) { |
300 | 0 | goto error; |
301 | 0 | } |
302 | 0 | if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) { |
303 | 0 | goto error; |
304 | 0 | } |
305 | 0 | } |
306 | | |
307 | | // Write name |
308 | 0 | const char *name_utf8 = typ->tp_members[i].name; |
309 | 0 | if (name_utf8 == NULL) { |
310 | 0 | PyErr_Format(PyExc_SystemError, |
311 | 0 | "In structseq_repr(), member %zd name is NULL" |
312 | 0 | " for type %.500s", i, typ->tp_name); |
313 | 0 | goto error; |
314 | 0 | } |
315 | 0 | if (PyUnicodeWriter_WriteUTF8(writer, name_utf8, -1) < 0) { |
316 | 0 | goto error; |
317 | 0 | } |
318 | | |
319 | | // Write "=" + repr(value) |
320 | 0 | if (PyUnicodeWriter_WriteChar(writer, '=') < 0) { |
321 | 0 | goto error; |
322 | 0 | } |
323 | 0 | PyObject *value = PyStructSequence_GetItem((PyObject*)obj, i); |
324 | 0 | assert(value != NULL); |
325 | 0 | if (PyUnicodeWriter_WriteRepr(writer, value) < 0) { |
326 | 0 | goto error; |
327 | 0 | } |
328 | 0 | } |
329 | | |
330 | 0 | if (PyUnicodeWriter_WriteChar(writer, ')') < 0) { |
331 | 0 | goto error; |
332 | 0 | } |
333 | | |
334 | 0 | return PyUnicodeWriter_Finish(writer); |
335 | | |
336 | 0 | error: |
337 | 0 | PyUnicodeWriter_Discard(writer); |
338 | 0 | return NULL; |
339 | 0 | } |
340 | | |
341 | | |
342 | | static PyObject * |
343 | | structseq_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
344 | 0 | { |
345 | 0 | PyStructSequence *self = (PyStructSequence*)op; |
346 | 0 | PyObject* tup = NULL; |
347 | 0 | PyObject* dict = NULL; |
348 | 0 | PyObject* result; |
349 | 0 | Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i; |
350 | |
|
351 | 0 | n_fields = REAL_SIZE(self); |
352 | 0 | if (n_fields < 0) { |
353 | 0 | return NULL; |
354 | 0 | } |
355 | 0 | n_visible_fields = VISIBLE_SIZE(self); |
356 | 0 | n_unnamed_fields = UNNAMED_FIELDS(self); |
357 | 0 | if (n_unnamed_fields < 0) { |
358 | 0 | return NULL; |
359 | 0 | } |
360 | 0 | tup = PyTuple_FromArray(self->ob_item, n_visible_fields); |
361 | 0 | if (!tup) |
362 | 0 | goto error; |
363 | | |
364 | 0 | dict = PyDict_New(); |
365 | 0 | if (!dict) |
366 | 0 | goto error; |
367 | | |
368 | 0 | for (i = n_visible_fields; i < n_fields; i++) { |
369 | 0 | const char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name; |
370 | 0 | if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0) |
371 | 0 | goto error; |
372 | 0 | } |
373 | | |
374 | 0 | result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict); |
375 | |
|
376 | 0 | Py_DECREF(tup); |
377 | 0 | Py_DECREF(dict); |
378 | |
|
379 | 0 | return result; |
380 | | |
381 | 0 | error: |
382 | 0 | Py_XDECREF(tup); |
383 | 0 | Py_XDECREF(dict); |
384 | 0 | return NULL; |
385 | 0 | } |
386 | | |
387 | | |
388 | | static PyObject * |
389 | | structseq_replace(PyObject *op, PyObject *args, PyObject *kwargs) |
390 | 0 | { |
391 | 0 | PyStructSequence *self = (PyStructSequence*)op; |
392 | 0 | PyStructSequence *result = NULL; |
393 | 0 | Py_ssize_t n_fields, n_unnamed_fields, i; |
394 | |
|
395 | 0 | if (!_PyArg_NoPositional("__replace__", args)) { |
396 | 0 | return NULL; |
397 | 0 | } |
398 | | |
399 | 0 | n_fields = REAL_SIZE(self); |
400 | 0 | if (n_fields < 0) { |
401 | 0 | return NULL; |
402 | 0 | } |
403 | 0 | n_unnamed_fields = UNNAMED_FIELDS(self); |
404 | 0 | if (n_unnamed_fields < 0) { |
405 | 0 | return NULL; |
406 | 0 | } |
407 | 0 | if (n_unnamed_fields > 0) { |
408 | 0 | PyErr_Format(PyExc_TypeError, |
409 | 0 | "__replace__() is not supported for %.500s " |
410 | 0 | "because it has unnamed field(s)", |
411 | 0 | Py_TYPE(self)->tp_name); |
412 | 0 | return NULL; |
413 | 0 | } |
414 | | |
415 | 0 | result = (PyStructSequence *) PyStructSequence_New(Py_TYPE(self)); |
416 | 0 | if (!result) { |
417 | 0 | return NULL; |
418 | 0 | } |
419 | | |
420 | 0 | if (kwargs != NULL) { |
421 | | // We do not support types with unnamed fields, so we can iterate over |
422 | | // i >= n_visible_fields case without slicing with (i - n_unnamed_fields). |
423 | 0 | for (i = 0; i < n_fields; ++i) { |
424 | 0 | PyObject *ob; |
425 | 0 | if (PyDict_PopString(kwargs, Py_TYPE(self)->tp_members[i].name, |
426 | 0 | &ob) < 0) { |
427 | 0 | goto error; |
428 | 0 | } |
429 | 0 | if (ob == NULL) { |
430 | 0 | ob = Py_NewRef(self->ob_item[i]); |
431 | 0 | } |
432 | 0 | result->ob_item[i] = ob; |
433 | 0 | } |
434 | | // Check if there are any unexpected fields. |
435 | 0 | if (PyDict_GET_SIZE(kwargs) > 0) { |
436 | 0 | PyObject *names = PyDict_Keys(kwargs); |
437 | 0 | if (names) { |
438 | 0 | PyErr_Format(PyExc_TypeError, "Got unexpected field name(s): %R", names); |
439 | 0 | Py_DECREF(names); |
440 | 0 | } |
441 | 0 | goto error; |
442 | 0 | } |
443 | 0 | } |
444 | 0 | else |
445 | 0 | { |
446 | | // Just create a copy of the original. |
447 | 0 | for (i = 0; i < n_fields; ++i) { |
448 | 0 | result->ob_item[i] = Py_NewRef(self->ob_item[i]); |
449 | 0 | } |
450 | 0 | } |
451 | | |
452 | 0 | _PyObject_GC_TRACK(result); |
453 | 0 | return (PyObject *)result; |
454 | | |
455 | 0 | error: |
456 | 0 | Py_DECREF(result); |
457 | 0 | return NULL; |
458 | 0 | } |
459 | | |
460 | | static PyMethodDef structseq_methods[] = { |
461 | | {"__reduce__", structseq_reduce, METH_NOARGS, NULL}, |
462 | | {"__replace__", _PyCFunction_CAST(structseq_replace), METH_VARARGS | METH_KEYWORDS, |
463 | | PyDoc_STR("__replace__($self, /, **changes)\n--\n\n" |
464 | | "Return a copy of the structure with new values for the specified fields.")}, |
465 | | {NULL, NULL} // sentinel |
466 | | }; |
467 | | |
468 | | static Py_ssize_t |
469 | 664 | count_members(PyStructSequence_Desc *desc, Py_ssize_t *n_unnamed_members) { |
470 | 664 | Py_ssize_t i; |
471 | | |
472 | 664 | *n_unnamed_members = 0; |
473 | 5.46k | for (i = 0; desc->fields[i].name != NULL; ++i) { |
474 | 4.80k | if (desc->fields[i].name == PyStructSequence_UnnamedField) { |
475 | 108 | (*n_unnamed_members)++; |
476 | 108 | } |
477 | 4.80k | } |
478 | 664 | return i; |
479 | 664 | } |
480 | | |
481 | | static int |
482 | | initialize_structseq_dict(PyStructSequence_Desc *desc, PyObject* dict, |
483 | 664 | Py_ssize_t n_members, Py_ssize_t n_unnamed_members) { |
484 | 664 | PyObject *v; |
485 | | |
486 | 664 | #define SET_DICT_FROM_SIZE(key, value) \ |
487 | 1.99k | do { \ |
488 | 1.99k | v = PyLong_FromSsize_t(value); \ |
489 | 1.99k | if (v == NULL) { \ |
490 | 0 | return -1; \ |
491 | 0 | } \ |
492 | 1.99k | if (PyDict_SetItemString(dict, key, v) < 0) { \ |
493 | 0 | Py_DECREF(v); \ |
494 | 0 | return -1; \ |
495 | 0 | } \ |
496 | 1.99k | Py_DECREF(v); \ |
497 | 1.99k | } while (0) |
498 | | |
499 | 664 | SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence); |
500 | 664 | SET_DICT_FROM_SIZE(real_length_key, n_members); |
501 | 664 | SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members); |
502 | | |
503 | | // Prepare and set __match_args__ |
504 | 664 | Py_ssize_t i, k; |
505 | 664 | PyObject* keys = PyTuple_New(desc->n_in_sequence); |
506 | 664 | if (keys == NULL) { |
507 | 0 | return -1; |
508 | 0 | } |
509 | | |
510 | 4.89k | for (i = k = 0; i < desc->n_in_sequence; ++i) { |
511 | 4.22k | if (desc->fields[i].name == PyStructSequence_UnnamedField) { |
512 | 108 | continue; |
513 | 108 | } |
514 | 4.12k | PyObject* new_member = PyUnicode_FromString(desc->fields[i].name); |
515 | 4.12k | if (new_member == NULL) { |
516 | 0 | goto error; |
517 | 0 | } |
518 | 4.12k | PyTuple_SET_ITEM(keys, k, new_member); |
519 | 4.12k | k++; |
520 | 4.12k | } |
521 | | |
522 | 664 | if (_PyTuple_Resize(&keys, k) == -1) { |
523 | 0 | assert(keys == NULL); |
524 | 0 | return -1; |
525 | 0 | } |
526 | | |
527 | 664 | if (PyDict_SetItemString(dict, match_args_key, keys) < 0) { |
528 | 0 | goto error; |
529 | 0 | } |
530 | | |
531 | 664 | Py_DECREF(keys); |
532 | 664 | return 0; |
533 | | |
534 | 0 | error: |
535 | 0 | Py_DECREF(keys); |
536 | 0 | return -1; |
537 | 664 | } |
538 | | |
539 | | static PyMemberDef * |
540 | | initialize_members(PyStructSequence_Desc *desc, |
541 | | Py_ssize_t n_members, Py_ssize_t n_unnamed_members) |
542 | 664 | { |
543 | 664 | PyMemberDef *members; |
544 | | |
545 | 664 | members = PyMem_NEW(PyMemberDef, n_members - n_unnamed_members + 1); |
546 | 664 | if (members == NULL) { |
547 | 0 | PyErr_NoMemory(); |
548 | 0 | return NULL; |
549 | 0 | } |
550 | | |
551 | 664 | Py_ssize_t i, k; |
552 | 5.46k | for (i = k = 0; i < n_members; ++i) { |
553 | 4.80k | if (desc->fields[i].name == PyStructSequence_UnnamedField) { |
554 | 108 | continue; |
555 | 108 | } |
556 | | |
557 | | /* The names and docstrings in these MemberDefs are statically */ |
558 | | /* allocated so it is expected that they'll outlive the MemberDef */ |
559 | 4.69k | members[k].name = desc->fields[i].name; |
560 | 4.69k | members[k].type = _Py_T_OBJECT; |
561 | 4.69k | members[k].offset = offsetof(PyStructSequence, ob_item) |
562 | 4.69k | + i * sizeof(PyObject*); |
563 | 4.69k | members[k].flags = Py_READONLY; |
564 | 4.69k | members[k].doc = desc->fields[i].doc; |
565 | 4.69k | k++; |
566 | 4.69k | } |
567 | 664 | members[k].name = NULL; |
568 | | |
569 | 664 | return members; |
570 | 664 | } |
571 | | |
572 | | |
573 | | static void |
574 | | initialize_static_fields(PyTypeObject *type, PyStructSequence_Desc *desc, |
575 | | PyMemberDef *tp_members, Py_ssize_t n_members, |
576 | | unsigned long tp_flags) |
577 | 288 | { |
578 | 288 | type->tp_name = desc->name; |
579 | | // Account for hidden members in tp_basicsize because they are not |
580 | | // included in the variable size. |
581 | 288 | Py_ssize_t n_hidden = n_members - desc->n_in_sequence; |
582 | 288 | type->tp_basicsize = sizeof(PyStructSequence) + (n_hidden - 1) * sizeof(PyObject *); |
583 | 288 | type->tp_itemsize = sizeof(PyObject *); |
584 | 288 | type->tp_dealloc = structseq_dealloc; |
585 | 288 | type->tp_repr = structseq_repr; |
586 | 288 | type->tp_doc = desc->doc; |
587 | 288 | type->tp_base = &PyTuple_Type; |
588 | 288 | type->tp_methods = structseq_methods; |
589 | 288 | type->tp_new = structseq_new; |
590 | 288 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | tp_flags; |
591 | 288 | type->tp_traverse = structseq_traverse; |
592 | 288 | type->tp_members = tp_members; |
593 | 288 | } |
594 | | |
595 | | static int |
596 | | initialize_static_type(PyTypeObject *type, PyStructSequence_Desc *desc, |
597 | 0 | Py_ssize_t n_members, Py_ssize_t n_unnamed_members) { |
598 | | /* initialize_static_fields() should have been called already. */ |
599 | 0 | if (PyType_Ready(type) < 0) { |
600 | 0 | return -1; |
601 | 0 | } |
602 | 0 | Py_INCREF(type); |
603 | |
|
604 | 0 | if (initialize_structseq_dict( |
605 | 0 | desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) { |
606 | 0 | Py_DECREF(type); |
607 | 0 | return -1; |
608 | 0 | } |
609 | | |
610 | 0 | return 0; |
611 | 0 | } |
612 | | |
613 | | int |
614 | | _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp, |
615 | | PyTypeObject *type, |
616 | | PyStructSequence_Desc *desc, |
617 | | unsigned long tp_flags) |
618 | 288 | { |
619 | 288 | if (Py_TYPE(type) == NULL) { |
620 | 288 | Py_SET_TYPE(type, &PyType_Type); |
621 | 288 | } |
622 | 288 | Py_ssize_t n_unnamed_members; |
623 | 288 | Py_ssize_t n_members = count_members(desc, &n_unnamed_members); |
624 | 288 | PyMemberDef *members = NULL; |
625 | | |
626 | 288 | if ((type->tp_flags & Py_TPFLAGS_READY) == 0) { |
627 | 288 | assert(type->tp_name == NULL); |
628 | 288 | assert(type->tp_members == NULL); |
629 | 288 | assert(type->tp_base == NULL); |
630 | | |
631 | 288 | members = initialize_members(desc, n_members, n_unnamed_members); |
632 | 288 | if (members == NULL) { |
633 | 0 | goto error; |
634 | 0 | } |
635 | 288 | initialize_static_fields(type, desc, members, n_members, tp_flags); |
636 | | |
637 | 288 | _Py_SetImmortal((PyObject *)type); |
638 | 288 | } |
639 | | #ifndef NDEBUG |
640 | | else { |
641 | | // Ensure that the type was initialized. |
642 | | assert(type->tp_name != NULL); |
643 | | assert(type->tp_members != NULL); |
644 | | assert(type->tp_base == &PyTuple_Type); |
645 | | assert((type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN)); |
646 | | assert(_Py_IsImmortal(type)); |
647 | | } |
648 | | #endif |
649 | | |
650 | 288 | if (_PyStaticType_InitBuiltin(interp, type) < 0) { |
651 | 0 | PyErr_Format(PyExc_RuntimeError, |
652 | 0 | "Can't initialize builtin type %s", |
653 | 0 | desc->name); |
654 | 0 | goto error; |
655 | 0 | } |
656 | | |
657 | 288 | if (initialize_structseq_dict( |
658 | 288 | desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) |
659 | 0 | { |
660 | 0 | goto error; |
661 | 0 | } |
662 | | |
663 | 288 | return 0; |
664 | | |
665 | 0 | error: |
666 | 0 | if (members != NULL) { |
667 | 0 | PyMem_Free(members); |
668 | 0 | } |
669 | 0 | return -1; |
670 | 288 | } |
671 | | |
672 | | int |
673 | | PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) |
674 | 0 | { |
675 | 0 | PyMemberDef *members; |
676 | 0 | Py_ssize_t n_members, n_unnamed_members; |
677 | |
|
678 | | #ifdef Py_TRACE_REFS |
679 | | /* if the type object was traced, remove it first |
680 | | before overwriting its storage */ |
681 | | PyInterpreterState *interp = _PyInterpreterState_GET(); |
682 | | if (_PyRefchain_IsTraced(interp, (PyObject *)type)) { |
683 | | _Py_ForgetReference((PyObject *)type); |
684 | | } |
685 | | #endif |
686 | | |
687 | | /* PyTypeObject has already been initialized */ |
688 | 0 | if (Py_REFCNT(type) != 0) { |
689 | 0 | PyErr_BadInternalCall(); |
690 | 0 | return -1; |
691 | 0 | } |
692 | | |
693 | 0 | n_members = count_members(desc, &n_unnamed_members); |
694 | 0 | members = initialize_members(desc, n_members, n_unnamed_members); |
695 | 0 | if (members == NULL) { |
696 | 0 | return -1; |
697 | 0 | } |
698 | 0 | initialize_static_fields(type, desc, members, n_members, 0); |
699 | 0 | if (initialize_static_type(type, desc, n_members, n_unnamed_members) < 0) { |
700 | 0 | PyMem_Free(members); |
701 | 0 | return -1; |
702 | 0 | } |
703 | 0 | return 0; |
704 | 0 | } |
705 | | |
706 | | void |
707 | | PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc) |
708 | 0 | { |
709 | 0 | (void)PyStructSequence_InitType2(type, desc); |
710 | 0 | } |
711 | | |
712 | | |
713 | | /* This is exposed in the internal API, not the public API. |
714 | | It is only called on builtin static types, which are all |
715 | | initialized via _PyStructSequence_InitBuiltinWithFlags(). */ |
716 | | |
717 | | void |
718 | | _PyStructSequence_FiniBuiltin(PyInterpreterState *interp, PyTypeObject *type) |
719 | 0 | { |
720 | | // Ensure that the type is initialized |
721 | 0 | assert(type->tp_name != NULL); |
722 | 0 | assert(type->tp_base == &PyTuple_Type); |
723 | 0 | assert((type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN)); |
724 | 0 | assert(_Py_IsImmortal(type)); |
725 | | |
726 | | // Cannot delete a type if it still has subclasses |
727 | 0 | if (_PyType_HasSubclasses(type)) { |
728 | | // XXX Shouldn't this be an error? |
729 | 0 | return; |
730 | 0 | } |
731 | | |
732 | 0 | _PyStaticType_FiniBuiltin(interp, type); |
733 | |
|
734 | 0 | if (_Py_IsMainInterpreter(interp)) { |
735 | | // Undo _PyStructSequence_InitBuiltinWithFlags(). |
736 | 0 | type->tp_name = NULL; |
737 | 0 | PyMem_Free(type->tp_members); |
738 | 0 | type->tp_members = NULL; |
739 | 0 | type->tp_base = NULL; |
740 | 0 | } |
741 | 0 | } |
742 | | |
743 | | |
744 | | PyTypeObject * |
745 | | _PyStructSequence_NewType(PyStructSequence_Desc *desc, unsigned long tp_flags) |
746 | 376 | { |
747 | 376 | PyMemberDef *members; |
748 | 376 | PyTypeObject *type; |
749 | 376 | PyType_Slot slots[8]; |
750 | 376 | PyType_Spec spec; |
751 | 376 | Py_ssize_t n_members, n_unnamed_members; |
752 | | |
753 | | /* Initialize MemberDefs */ |
754 | 376 | n_members = count_members(desc, &n_unnamed_members); |
755 | 376 | members = initialize_members(desc, n_members, n_unnamed_members); |
756 | 376 | if (members == NULL) { |
757 | 0 | return NULL; |
758 | 0 | } |
759 | | |
760 | | /* Initialize Slots */ |
761 | 376 | slots[0] = (PyType_Slot){Py_tp_dealloc, structseq_dealloc}; |
762 | 376 | slots[1] = (PyType_Slot){Py_tp_repr, structseq_repr}; |
763 | 376 | slots[2] = (PyType_Slot){Py_tp_doc, (void *)desc->doc}; |
764 | 376 | slots[3] = (PyType_Slot){Py_tp_methods, structseq_methods}; |
765 | 376 | slots[4] = (PyType_Slot){Py_tp_new, structseq_new}; |
766 | 376 | slots[5] = (PyType_Slot){Py_tp_members, members}; |
767 | 376 | slots[6] = (PyType_Slot){Py_tp_traverse, structseq_traverse}; |
768 | 376 | slots[7] = (PyType_Slot){0, 0}; |
769 | | |
770 | | /* Initialize Spec */ |
771 | | /* The name in this PyType_Spec is statically allocated so it is */ |
772 | | /* expected that it'll outlive the PyType_Spec */ |
773 | 376 | spec.name = desc->name; |
774 | 376 | Py_ssize_t hidden = n_members - desc->n_in_sequence; |
775 | 376 | spec.basicsize = (int)(sizeof(PyStructSequence) + (hidden - 1) * sizeof(PyObject *)); |
776 | 376 | spec.itemsize = sizeof(PyObject *); |
777 | 376 | spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | tp_flags; |
778 | 376 | spec.slots = slots; |
779 | | |
780 | 376 | type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, (PyObject *)&PyTuple_Type); |
781 | 376 | PyMem_Free(members); |
782 | 376 | if (type == NULL) { |
783 | 0 | return NULL; |
784 | 0 | } |
785 | | |
786 | 376 | if (initialize_structseq_dict( |
787 | 376 | desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) { |
788 | 0 | Py_DECREF(type); |
789 | 0 | return NULL; |
790 | 0 | } |
791 | | |
792 | 376 | return type; |
793 | 376 | } |
794 | | |
795 | | |
796 | | PyTypeObject * |
797 | | PyStructSequence_NewType(PyStructSequence_Desc *desc) |
798 | 376 | { |
799 | 376 | return _PyStructSequence_NewType(desc, 0); |
800 | 376 | } |