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