Coverage Report

Created: 2025-07-11 06:59

/src/Python-3.8.3/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_tupleobject.h"
12
#include "pycore_object.h"
13
#include "structmember.h"
14
15
static const char visible_length_key[] = "n_sequence_fields";
16
static const char real_length_key[] = "n_fields";
17
static const char unnamed_fields_key[] = "n_unnamed_fields";
18
19
/* Fields with this name have only a field index, not a field name.
20
   They are only allowed for indices < n_visible_fields. */
21
char *PyStructSequence_UnnamedField = "unnamed field";
22
_Py_IDENTIFIER(n_sequence_fields);
23
_Py_IDENTIFIER(n_fields);
24
_Py_IDENTIFIER(n_unnamed_fields);
25
26
0
#define VISIBLE_SIZE(op) Py_SIZE(op)
27
952
#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \
28
952
                      _PyDict_GetItemId((tp)->tp_dict, &PyId_n_sequence_fields))
29
30
1.80k
#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \
31
1.80k
                      _PyDict_GetItemId((tp)->tp_dict, &PyId_n_fields))
32
854
#define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
33
34
0
#define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \
35
0
                      _PyDict_GetItemId((tp)->tp_dict, &PyId_n_unnamed_fields))
36
0
#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
37
38
39
PyObject *
40
PyStructSequence_New(PyTypeObject *type)
41
952
{
42
952
    PyStructSequence *obj;
43
952
    Py_ssize_t size = REAL_SIZE_TP(type), i;
44
45
952
    obj = PyObject_GC_NewVar(PyStructSequence, type, size);
46
952
    if (obj == NULL)
47
0
        return NULL;
48
    /* Hack the size of the variable object, so invisible fields don't appear
49
     to Python code. */
50
952
    Py_SIZE(obj) = VISIBLE_SIZE_TP(type);
51
18.0k
    for (i = 0; i < size; i++)
52
17.0k
        obj->ob_item[i] = NULL;
53
54
952
    return (PyObject*)obj;
55
952
}
56
57
void
58
PyStructSequence_SetItem(PyObject* op, Py_ssize_t i, PyObject* v)
59
0
{
60
0
    PyStructSequence_SET_ITEM(op, i, v);
61
0
}
62
63
PyObject*
64
PyStructSequence_GetItem(PyObject* op, Py_ssize_t i)
65
0
{
66
0
    return PyStructSequence_GET_ITEM(op, i);
67
0
}
68
69
70
static int
71
structseq_traverse(PyStructSequence *obj, visitproc visit, void *arg)
72
0
{
73
0
    Py_ssize_t i, size;
74
0
    size = REAL_SIZE(obj);
75
0
    for (i = 0; i < size; ++i) {
76
0
        Py_VISIT(obj->ob_item[i]);
77
0
    }
78
0
    return 0;
79
0
}
80
81
static void
82
structseq_dealloc(PyStructSequence *obj)
83
854
{
84
854
    Py_ssize_t i, size;
85
854
    PyTypeObject *tp;
86
854
    PyObject_GC_UnTrack(obj);
87
88
854
    tp = (PyTypeObject *) Py_TYPE(obj);
89
854
    size = REAL_SIZE(obj);
90
17.0k
    for (i = 0; i < size; ++i) {
91
16.2k
        Py_XDECREF(obj->ob_item[i]);
92
16.2k
    }
93
854
    PyObject_GC_Del(obj);
94
854
    if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
95
854
        Py_DECREF(tp);
96
854
    }
97
854
}
98
99
/*[clinic input]
100
class structseq "PyStructSequence *" "NULL"
101
[clinic start generated code]*/
102
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9d781c6922c77752]*/
103
104
#include "clinic/structseq.c.h"
105
106
/*[clinic input]
107
@classmethod
108
structseq.__new__ as structseq_new
109
    sequence as arg: object
110
    dict: object(c_default="NULL") = {}
111
[clinic start generated code]*/
112
113
static PyObject *
114
structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
115
/*[clinic end generated code: output=baa082e788b171da input=90532511101aa3fb]*/
116
0
{
117
0
    PyObject *ob;
118
0
    PyStructSequence *res = NULL;
119
0
    Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
120
121
0
    arg = PySequence_Fast(arg, "constructor requires a sequence");
122
123
0
    if (!arg) {
124
0
        return NULL;
125
0
    }
126
127
0
    if (dict && !PyDict_Check(dict)) {
128
0
        PyErr_Format(PyExc_TypeError,
129
0
                     "%.500s() takes a dict as second arg, if any",
130
0
                     type->tp_name);
131
0
        Py_DECREF(arg);
132
0
        return NULL;
133
0
    }
134
135
0
    len = PySequence_Fast_GET_SIZE(arg);
136
0
    min_len = VISIBLE_SIZE_TP(type);
137
0
    max_len = REAL_SIZE_TP(type);
138
0
    n_unnamed_fields = UNNAMED_FIELDS_TP(type);
139
140
0
    if (min_len != max_len) {
141
0
        if (len < min_len) {
142
0
            PyErr_Format(PyExc_TypeError,
143
0
                "%.500s() takes an at least %zd-sequence (%zd-sequence given)",
144
0
                type->tp_name, min_len, len);
145
0
            Py_DECREF(arg);
146
0
            return NULL;
147
0
        }
148
149
0
        if (len > max_len) {
150
0
            PyErr_Format(PyExc_TypeError,
151
0
                "%.500s() takes an at most %zd-sequence (%zd-sequence given)",
152
0
                type->tp_name, max_len, len);
153
0
            Py_DECREF(arg);
154
0
            return NULL;
155
0
        }
156
0
    }
157
0
    else {
158
0
        if (len != min_len) {
159
0
            PyErr_Format(PyExc_TypeError,
160
0
                         "%.500s() takes a %zd-sequence (%zd-sequence given)",
161
0
                         type->tp_name, min_len, len);
162
0
            Py_DECREF(arg);
163
0
            return NULL;
164
0
        }
165
0
    }
166
167
0
    res = (PyStructSequence*) PyStructSequence_New(type);
168
0
    if (res == NULL) {
169
0
        Py_DECREF(arg);
170
0
        return NULL;
171
0
    }
172
0
    for (i = 0; i < len; ++i) {
173
0
        PyObject *v = PySequence_Fast_GET_ITEM(arg, i);
174
0
        Py_INCREF(v);
175
0
        res->ob_item[i] = v;
176
0
    }
177
0
    for (; i < max_len; ++i) {
178
0
        if (dict && (ob = PyDict_GetItemString(
179
0
            dict, type->tp_members[i-n_unnamed_fields].name))) {
180
0
        }
181
0
        else {
182
0
            ob = Py_None;
183
0
        }
184
0
        Py_INCREF(ob);
185
0
        res->ob_item[i] = ob;
186
0
    }
187
188
0
    Py_DECREF(arg);
189
0
    _PyObject_GC_TRACK(res);
190
0
    return (PyObject*) res;
191
0
}
192
193
194
static PyObject *
195
structseq_repr(PyStructSequence *obj)
196
0
{
197
0
    PyTypeObject *typ = Py_TYPE(obj);
198
0
    _PyUnicodeWriter writer;
199
200
    /* Write "typename(" */
201
0
    PyObject *type_name = PyUnicode_DecodeUTF8(typ->tp_name,
202
0
                                               strlen(typ->tp_name),
203
0
                                               NULL);
204
0
    if (type_name == NULL) {
205
0
        return NULL;
206
0
    }
207
208
0
    _PyUnicodeWriter_Init(&writer);
209
0
    writer.overallocate = 1;
210
    /* count 5 characters per item: "x=1, " */
211
0
    writer.min_length = (PyUnicode_GET_LENGTH(type_name) + 1
212
0
                         + VISIBLE_SIZE(obj) * 5 + 1);
213
214
0
    if (_PyUnicodeWriter_WriteStr(&writer, type_name) < 0) {
215
0
        Py_DECREF(type_name);
216
0
        goto error;
217
0
    }
218
0
    Py_DECREF(type_name);
219
220
0
    if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) {
221
0
        goto error;
222
0
    }
223
224
0
    for (Py_ssize_t i=0; i < VISIBLE_SIZE(obj); i++) {
225
0
        if (i > 0) {
226
            /* Write ", " */
227
0
            if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
228
0
                goto error;
229
0
            }
230
0
        }
231
232
        /* Write "name=repr" */
233
0
        const char *name_utf8 = typ->tp_members[i].name;
234
0
        if (name_utf8 == NULL) {
235
0
            PyErr_Format(PyExc_SystemError, "In structseq_repr(), member %zd name is NULL"
236
0
                         " for type %.500s", i, typ->tp_name);
237
0
            goto error;
238
0
        }
239
240
0
        PyObject *name = PyUnicode_DecodeUTF8(name_utf8, strlen(name_utf8), NULL);
241
0
        if (name == NULL) {
242
0
            goto error;
243
0
        }
244
0
        if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
245
0
            Py_DECREF(name);
246
0
            goto error;
247
0
        }
248
0
        Py_DECREF(name);
249
250
0
        if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) {
251
0
            goto error;
252
0
        }
253
254
0
        PyObject *value = PyStructSequence_GET_ITEM(obj, i);
255
0
        assert(value != NULL);
256
0
        PyObject *repr = PyObject_Repr(value);
257
0
        if (repr == NULL) {
258
0
            goto error;
259
0
        }
260
0
        if (_PyUnicodeWriter_WriteStr(&writer, repr) < 0) {
261
0
            Py_DECREF(repr);
262
0
            goto error;
263
0
        }
264
0
        Py_DECREF(repr);
265
0
    }
266
267
0
    if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) {
268
0
        goto error;
269
0
    }
270
271
0
    return _PyUnicodeWriter_Finish(&writer);
272
273
0
error:
274
0
    _PyUnicodeWriter_Dealloc(&writer);
275
0
    return NULL;
276
0
}
277
278
279
static PyObject *
280
structseq_reduce(PyStructSequence* self, PyObject *Py_UNUSED(ignored))
281
0
{
282
0
    PyObject* tup = NULL;
283
0
    PyObject* dict = NULL;
284
0
    PyObject* result;
285
0
    Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i;
286
287
0
    n_fields = REAL_SIZE(self);
288
0
    n_visible_fields = VISIBLE_SIZE(self);
289
0
    n_unnamed_fields = UNNAMED_FIELDS(self);
290
0
    tup = _PyTuple_FromArray(self->ob_item, n_visible_fields);
291
0
    if (!tup)
292
0
        goto error;
293
294
0
    dict = PyDict_New();
295
0
    if (!dict)
296
0
        goto error;
297
298
0
    for (i = n_visible_fields; i < n_fields; i++) {
299
0
        const char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
300
0
        if (PyDict_SetItemString(dict, n, self->ob_item[i]) < 0)
301
0
            goto error;
302
0
    }
303
304
0
    result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
305
306
0
    Py_DECREF(tup);
307
0
    Py_DECREF(dict);
308
309
0
    return result;
310
311
0
error:
312
0
    Py_XDECREF(tup);
313
0
    Py_XDECREF(dict);
314
0
    return NULL;
315
0
}
316
317
static PyMethodDef structseq_methods[] = {
318
    {"__reduce__", (PyCFunction)structseq_reduce, METH_NOARGS, NULL},
319
    {NULL, NULL}
320
};
321
322
static Py_ssize_t
323
238
count_members(PyStructSequence_Desc *desc, Py_ssize_t *n_unnamed_members) {
324
238
    Py_ssize_t i;
325
326
238
    *n_unnamed_members = 0;
327
1.84k
    for (i = 0; desc->fields[i].name != NULL; ++i) {
328
1.61k
        if (desc->fields[i].name == PyStructSequence_UnnamedField) {
329
42
            (*n_unnamed_members)++;
330
42
        }
331
1.61k
    }
332
238
    return i;
333
238
}
334
335
static int
336
initialize_structseq_dict(PyStructSequence_Desc *desc, PyObject* dict,
337
238
                          Py_ssize_t n_members, Py_ssize_t n_unnamed_members) {
338
238
    PyObject *v;
339
340
238
#define SET_DICT_FROM_SIZE(key, value)                                         \
341
714
    do {                                                                       \
342
714
        v = PyLong_FromSsize_t(value);                                         \
343
714
        if (v == NULL) {                                                       \
344
0
            return -1;                                                         \
345
0
        }                                                                      \
346
714
        if (PyDict_SetItemString(dict, key, v) < 0) {                          \
347
0
            Py_DECREF(v);                                                      \
348
0
            return -1;                                                         \
349
0
        }                                                                      \
350
714
        Py_DECREF(v);                                                          \
351
714
    } while (0)
352
353
238
    SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence);
354
238
    SET_DICT_FROM_SIZE(real_length_key, n_members);
355
238
    SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members);
356
238
    return 0;
357
238
}
358
359
static void
360
initialize_members(PyStructSequence_Desc *desc, PyMemberDef* members,
361
238
                   Py_ssize_t n_members) {
362
238
    Py_ssize_t i, k;
363
364
1.84k
    for (i = k = 0; i < n_members; ++i) {
365
1.61k
        if (desc->fields[i].name == PyStructSequence_UnnamedField) {
366
42
            continue;
367
42
        }
368
369
        /* The names and docstrings in these MemberDefs are statically */
370
        /* allocated so it is expected that they'll outlive the MemberDef */
371
1.56k
        members[k].name = desc->fields[i].name;
372
1.56k
        members[k].type = T_OBJECT;
373
1.56k
        members[k].offset = offsetof(PyStructSequence, ob_item)
374
1.56k
          + i * sizeof(PyObject*);
375
1.56k
        members[k].flags = READONLY;
376
1.56k
        members[k].doc = desc->fields[i].doc;
377
1.56k
        k++;
378
1.56k
    }
379
238
    members[k].name = NULL;
380
238
}
381
382
int
383
PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
384
140
{
385
140
    PyMemberDef *members;
386
140
    Py_ssize_t n_members, n_unnamed_members;
387
388
#ifdef Py_TRACE_REFS
389
    /* if the type object was chained, unchain it first
390
       before overwriting its storage */
391
    if (type->ob_base.ob_base._ob_next) {
392
        _Py_ForgetReference((PyObject *)type);
393
    }
394
#endif
395
396
    /* PyTypeObject has already been initialized */
397
140
    if (Py_REFCNT(type) != 0) {
398
0
        PyErr_BadInternalCall();
399
0
        return -1;
400
0
    }
401
402
140
    type->tp_name = desc->name;
403
140
    type->tp_basicsize = sizeof(PyStructSequence) - sizeof(PyObject *);
404
140
    type->tp_itemsize = sizeof(PyObject *);
405
140
    type->tp_dealloc = (destructor)structseq_dealloc;
406
140
    type->tp_repr = (reprfunc)structseq_repr;
407
140
    type->tp_doc = desc->doc;
408
140
    type->tp_base = &PyTuple_Type;
409
140
    type->tp_methods = structseq_methods;
410
140
    type->tp_new = structseq_new;
411
140
    type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC;
412
140
    type->tp_traverse = (traverseproc) structseq_traverse;
413
414
140
    n_members = count_members(desc, &n_unnamed_members);
415
140
    members = PyMem_NEW(PyMemberDef, n_members - n_unnamed_members + 1);
416
140
    if (members == NULL) {
417
0
        PyErr_NoMemory();
418
0
        return -1;
419
0
    }
420
140
    initialize_members(desc, members, n_members);
421
140
    type->tp_members = members;
422
423
140
    if (PyType_Ready(type) < 0) {
424
0
        PyMem_FREE(members);
425
0
        return -1;
426
0
    }
427
140
    Py_INCREF(type);
428
429
140
    if (initialize_structseq_dict(
430
140
            desc, type->tp_dict, n_members, n_unnamed_members) < 0) {
431
0
        PyMem_FREE(members);
432
0
        Py_DECREF(type);
433
0
        return -1;
434
0
    }
435
436
140
    return 0;
437
140
}
438
439
void
440
PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
441
0
{
442
0
    (void)PyStructSequence_InitType2(type, desc);
443
0
}
444
445
PyTypeObject *
446
PyStructSequence_NewType(PyStructSequence_Desc *desc)
447
98
{
448
98
    PyMemberDef *members;
449
98
    PyObject *bases;
450
98
    PyTypeObject *type;
451
98
    PyType_Slot slots[8];
452
98
    PyType_Spec spec;
453
98
    Py_ssize_t n_members, n_unnamed_members;
454
455
    /* Initialize MemberDefs */
456
98
    n_members = count_members(desc, &n_unnamed_members);
457
98
    members = PyMem_NEW(PyMemberDef, n_members - n_unnamed_members + 1);
458
98
    if (members == NULL) {
459
0
        PyErr_NoMemory();
460
0
        return NULL;
461
0
    }
462
98
    initialize_members(desc, members, n_members);
463
464
    /* Initialize Slots */
465
98
    slots[0] = (PyType_Slot){Py_tp_dealloc, (destructor)structseq_dealloc};
466
98
    slots[1] = (PyType_Slot){Py_tp_repr, (reprfunc)structseq_repr};
467
98
    slots[2] = (PyType_Slot){Py_tp_doc, (void *)desc->doc};
468
98
    slots[3] = (PyType_Slot){Py_tp_methods, structseq_methods};
469
98
    slots[4] = (PyType_Slot){Py_tp_new, structseq_new};
470
98
    slots[5] = (PyType_Slot){Py_tp_members, members};
471
98
    slots[6] = (PyType_Slot){Py_tp_traverse, (traverseproc)structseq_traverse};
472
98
    slots[7] = (PyType_Slot){0, 0};
473
474
    /* Initialize Spec */
475
    /* The name in this PyType_Spec is statically allocated so it is */
476
    /* expected that it'll outlive the PyType_Spec */
477
98
    spec.name = desc->name;
478
98
    spec.basicsize = sizeof(PyStructSequence) - sizeof(PyObject *);
479
98
    spec.itemsize = sizeof(PyObject *);
480
98
    spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC;
481
98
    spec.slots = slots;
482
483
98
    bases = PyTuple_Pack(1, &PyTuple_Type);
484
98
    if (bases == NULL) {
485
0
        PyMem_FREE(members);
486
0
        return NULL;
487
0
    }
488
98
    type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, bases);
489
98
    Py_DECREF(bases);
490
98
    PyMem_FREE(members);
491
98
    if (type == NULL) {
492
0
        return NULL;
493
0
    }
494
495
98
    if (initialize_structseq_dict(
496
98
            desc, type->tp_dict, n_members, n_unnamed_members) < 0) {
497
0
        Py_DECREF(type);
498
0
        return NULL;
499
0
    }
500
501
98
    return type;
502
98
}
503
504
int _PyStructSequence_Init(void)
505
14
{
506
14
    if (_PyUnicode_FromId(&PyId_n_sequence_fields) == NULL
507
14
        || _PyUnicode_FromId(&PyId_n_fields) == NULL
508
14
        || _PyUnicode_FromId(&PyId_n_unnamed_fields) == NULL)
509
0
        return -1;
510
511
14
    return 0;
512
14
}