Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Objects/clinic/codeobject.c.h
Line
Count
Source (jump to first uncovered line)
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6
#  include "pycore_gc.h"          // PyGC_Head
7
#  include "pycore_runtime.h"     // _Py_ID()
8
#endif
9
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
10
11
PyDoc_STRVAR(code_new__doc__,
12
"code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n"
13
"     flags, codestring, constants, names, varnames, filename, name,\n"
14
"     qualname, firstlineno, linetable, exceptiontable, freevars=(),\n"
15
"     cellvars=(), /)\n"
16
"--\n"
17
"\n"
18
"Create a code object.  Not for the faint of heart.");
19
20
static PyObject *
21
code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
22
              int kwonlyargcount, int nlocals, int stacksize, int flags,
23
              PyObject *code, PyObject *consts, PyObject *names,
24
              PyObject *varnames, PyObject *filename, PyObject *name,
25
              PyObject *qualname, int firstlineno, PyObject *linetable,
26
              PyObject *exceptiontable, PyObject *freevars,
27
              PyObject *cellvars);
28
29
static PyObject *
30
code_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
31
0
{
32
0
    PyObject *return_value = NULL;
33
0
    PyTypeObject *base_tp = &PyCode_Type;
34
0
    int argcount;
35
0
    int posonlyargcount;
36
0
    int kwonlyargcount;
37
0
    int nlocals;
38
0
    int stacksize;
39
0
    int flags;
40
0
    PyObject *code;
41
0
    PyObject *consts;
42
0
    PyObject *names;
43
0
    PyObject *varnames;
44
0
    PyObject *filename;
45
0
    PyObject *name;
46
0
    PyObject *qualname;
47
0
    int firstlineno;
48
0
    PyObject *linetable;
49
0
    PyObject *exceptiontable;
50
0
    PyObject *freevars = NULL;
51
0
    PyObject *cellvars = NULL;
52
53
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
54
0
        !_PyArg_NoKeywords("code", kwargs)) {
55
0
        goto exit;
56
0
    }
57
0
    if (!_PyArg_CheckPositional("code", PyTuple_GET_SIZE(args), 16, 18)) {
58
0
        goto exit;
59
0
    }
60
0
    argcount = PyLong_AsInt(PyTuple_GET_ITEM(args, 0));
61
0
    if (argcount == -1 && PyErr_Occurred()) {
62
0
        goto exit;
63
0
    }
64
0
    posonlyargcount = PyLong_AsInt(PyTuple_GET_ITEM(args, 1));
65
0
    if (posonlyargcount == -1 && PyErr_Occurred()) {
66
0
        goto exit;
67
0
    }
68
0
    kwonlyargcount = PyLong_AsInt(PyTuple_GET_ITEM(args, 2));
69
0
    if (kwonlyargcount == -1 && PyErr_Occurred()) {
70
0
        goto exit;
71
0
    }
72
0
    nlocals = PyLong_AsInt(PyTuple_GET_ITEM(args, 3));
73
0
    if (nlocals == -1 && PyErr_Occurred()) {
74
0
        goto exit;
75
0
    }
76
0
    stacksize = PyLong_AsInt(PyTuple_GET_ITEM(args, 4));
77
0
    if (stacksize == -1 && PyErr_Occurred()) {
78
0
        goto exit;
79
0
    }
80
0
    flags = PyLong_AsInt(PyTuple_GET_ITEM(args, 5));
81
0
    if (flags == -1 && PyErr_Occurred()) {
82
0
        goto exit;
83
0
    }
84
0
    if (!PyBytes_Check(PyTuple_GET_ITEM(args, 6))) {
85
0
        _PyArg_BadArgument("code", "argument 7", "bytes", PyTuple_GET_ITEM(args, 6));
86
0
        goto exit;
87
0
    }
88
0
    code = PyTuple_GET_ITEM(args, 6);
89
0
    if (!PyTuple_Check(PyTuple_GET_ITEM(args, 7))) {
90
0
        _PyArg_BadArgument("code", "argument 8", "tuple", PyTuple_GET_ITEM(args, 7));
91
0
        goto exit;
92
0
    }
93
0
    consts = PyTuple_GET_ITEM(args, 7);
94
0
    if (!PyTuple_Check(PyTuple_GET_ITEM(args, 8))) {
95
0
        _PyArg_BadArgument("code", "argument 9", "tuple", PyTuple_GET_ITEM(args, 8));
96
0
        goto exit;
97
0
    }
98
0
    names = PyTuple_GET_ITEM(args, 8);
99
0
    if (!PyTuple_Check(PyTuple_GET_ITEM(args, 9))) {
100
0
        _PyArg_BadArgument("code", "argument 10", "tuple", PyTuple_GET_ITEM(args, 9));
101
0
        goto exit;
102
0
    }
103
0
    varnames = PyTuple_GET_ITEM(args, 9);
104
0
    if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 10))) {
105
0
        _PyArg_BadArgument("code", "argument 11", "str", PyTuple_GET_ITEM(args, 10));
106
0
        goto exit;
107
0
    }
108
0
    filename = PyTuple_GET_ITEM(args, 10);
109
0
    if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 11))) {
110
0
        _PyArg_BadArgument("code", "argument 12", "str", PyTuple_GET_ITEM(args, 11));
111
0
        goto exit;
112
0
    }
113
0
    name = PyTuple_GET_ITEM(args, 11);
114
0
    if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 12))) {
115
0
        _PyArg_BadArgument("code", "argument 13", "str", PyTuple_GET_ITEM(args, 12));
116
0
        goto exit;
117
0
    }
118
0
    qualname = PyTuple_GET_ITEM(args, 12);
119
0
    firstlineno = PyLong_AsInt(PyTuple_GET_ITEM(args, 13));
120
0
    if (firstlineno == -1 && PyErr_Occurred()) {
121
0
        goto exit;
122
0
    }
123
0
    if (!PyBytes_Check(PyTuple_GET_ITEM(args, 14))) {
124
0
        _PyArg_BadArgument("code", "argument 15", "bytes", PyTuple_GET_ITEM(args, 14));
125
0
        goto exit;
126
0
    }
127
0
    linetable = PyTuple_GET_ITEM(args, 14);
128
0
    if (!PyBytes_Check(PyTuple_GET_ITEM(args, 15))) {
129
0
        _PyArg_BadArgument("code", "argument 16", "bytes", PyTuple_GET_ITEM(args, 15));
130
0
        goto exit;
131
0
    }
132
0
    exceptiontable = PyTuple_GET_ITEM(args, 15);
133
0
    if (PyTuple_GET_SIZE(args) < 17) {
134
0
        goto skip_optional;
135
0
    }
136
0
    if (!PyTuple_Check(PyTuple_GET_ITEM(args, 16))) {
137
0
        _PyArg_BadArgument("code", "argument 17", "tuple", PyTuple_GET_ITEM(args, 16));
138
0
        goto exit;
139
0
    }
140
0
    freevars = PyTuple_GET_ITEM(args, 16);
141
0
    if (PyTuple_GET_SIZE(args) < 18) {
142
0
        goto skip_optional;
143
0
    }
144
0
    if (!PyTuple_Check(PyTuple_GET_ITEM(args, 17))) {
145
0
        _PyArg_BadArgument("code", "argument 18", "tuple", PyTuple_GET_ITEM(args, 17));
146
0
        goto exit;
147
0
    }
148
0
    cellvars = PyTuple_GET_ITEM(args, 17);
149
0
skip_optional:
150
0
    return_value = code_new_impl(type, argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, qualname, firstlineno, linetable, exceptiontable, freevars, cellvars);
151
152
0
exit:
153
0
    return return_value;
154
0
}
155
156
PyDoc_STRVAR(code_replace__doc__,
157
"replace($self, /, **changes)\n"
158
"--\n"
159
"\n"
160
"Return a copy of the code object with new values for the specified fields.");
161
162
#define CODE_REPLACE_METHODDEF    \
163
    {"replace", _PyCFunction_CAST(code_replace), METH_FASTCALL|METH_KEYWORDS, code_replace__doc__},
164
165
static PyObject *
166
code_replace_impl(PyCodeObject *self, int co_argcount,
167
                  int co_posonlyargcount, int co_kwonlyargcount,
168
                  int co_nlocals, int co_stacksize, int co_flags,
169
                  int co_firstlineno, PyObject *co_code, PyObject *co_consts,
170
                  PyObject *co_names, PyObject *co_varnames,
171
                  PyObject *co_freevars, PyObject *co_cellvars,
172
                  PyObject *co_filename, PyObject *co_name,
173
                  PyObject *co_qualname, PyObject *co_linetable,
174
                  PyObject *co_exceptiontable);
175
176
static PyObject *
177
code_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
178
0
{
179
0
    PyObject *return_value = NULL;
180
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
181
182
0
    #define NUM_KEYWORDS 18
183
0
    static struct {
184
0
        PyGC_Head _this_is_not_used;
185
0
        PyObject_VAR_HEAD
186
0
        Py_hash_t ob_hash;
187
0
        PyObject *ob_item[NUM_KEYWORDS];
188
0
    } _kwtuple = {
189
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
190
0
        .ob_hash = -1,
191
0
        .ob_item = { &_Py_ID(co_argcount), &_Py_ID(co_posonlyargcount), &_Py_ID(co_kwonlyargcount), &_Py_ID(co_nlocals), &_Py_ID(co_stacksize), &_Py_ID(co_flags), &_Py_ID(co_firstlineno), &_Py_ID(co_code), &_Py_ID(co_consts), &_Py_ID(co_names), &_Py_ID(co_varnames), &_Py_ID(co_freevars), &_Py_ID(co_cellvars), &_Py_ID(co_filename), &_Py_ID(co_name), &_Py_ID(co_qualname), &_Py_ID(co_linetable), &_Py_ID(co_exceptiontable), },
192
0
    };
193
0
    #undef NUM_KEYWORDS
194
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
195
196
    #else  // !Py_BUILD_CORE
197
    #  define KWTUPLE NULL
198
    #endif  // !Py_BUILD_CORE
199
200
0
    static const char * const _keywords[] = {"co_argcount", "co_posonlyargcount", "co_kwonlyargcount", "co_nlocals", "co_stacksize", "co_flags", "co_firstlineno", "co_code", "co_consts", "co_names", "co_varnames", "co_freevars", "co_cellvars", "co_filename", "co_name", "co_qualname", "co_linetable", "co_exceptiontable", NULL};
201
0
    static _PyArg_Parser _parser = {
202
0
        .keywords = _keywords,
203
0
        .fname = "replace",
204
0
        .kwtuple = KWTUPLE,
205
0
    };
206
0
    #undef KWTUPLE
207
0
    PyObject *argsbuf[18];
208
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
209
0
    int co_argcount = ((PyCodeObject *)self)->co_argcount;
210
0
    int co_posonlyargcount = ((PyCodeObject *)self)->co_posonlyargcount;
211
0
    int co_kwonlyargcount = ((PyCodeObject *)self)->co_kwonlyargcount;
212
0
    int co_nlocals = ((PyCodeObject *)self)->co_nlocals;
213
0
    int co_stacksize = ((PyCodeObject *)self)->co_stacksize;
214
0
    int co_flags = ((PyCodeObject *)self)->co_flags;
215
0
    int co_firstlineno = ((PyCodeObject *)self)->co_firstlineno;
216
0
    PyObject *co_code = NULL;
217
0
    PyObject *co_consts = ((PyCodeObject *)self)->co_consts;
218
0
    PyObject *co_names = ((PyCodeObject *)self)->co_names;
219
0
    PyObject *co_varnames = NULL;
220
0
    PyObject *co_freevars = NULL;
221
0
    PyObject *co_cellvars = NULL;
222
0
    PyObject *co_filename = ((PyCodeObject *)self)->co_filename;
223
0
    PyObject *co_name = ((PyCodeObject *)self)->co_name;
224
0
    PyObject *co_qualname = ((PyCodeObject *)self)->co_qualname;
225
0
    PyObject *co_linetable = ((PyCodeObject *)self)->co_linetable;
226
0
    PyObject *co_exceptiontable = ((PyCodeObject *)self)->co_exceptiontable;
227
228
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
229
0
            /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
230
0
    if (!args) {
231
0
        goto exit;
232
0
    }
233
0
    if (!noptargs) {
234
0
        goto skip_optional_kwonly;
235
0
    }
236
0
    if (args[0]) {
237
0
        co_argcount = PyLong_AsInt(args[0]);
238
0
        if (co_argcount == -1 && PyErr_Occurred()) {
239
0
            goto exit;
240
0
        }
241
0
        if (!--noptargs) {
242
0
            goto skip_optional_kwonly;
243
0
        }
244
0
    }
245
0
    if (args[1]) {
246
0
        co_posonlyargcount = PyLong_AsInt(args[1]);
247
0
        if (co_posonlyargcount == -1 && PyErr_Occurred()) {
248
0
            goto exit;
249
0
        }
250
0
        if (!--noptargs) {
251
0
            goto skip_optional_kwonly;
252
0
        }
253
0
    }
254
0
    if (args[2]) {
255
0
        co_kwonlyargcount = PyLong_AsInt(args[2]);
256
0
        if (co_kwonlyargcount == -1 && PyErr_Occurred()) {
257
0
            goto exit;
258
0
        }
259
0
        if (!--noptargs) {
260
0
            goto skip_optional_kwonly;
261
0
        }
262
0
    }
263
0
    if (args[3]) {
264
0
        co_nlocals = PyLong_AsInt(args[3]);
265
0
        if (co_nlocals == -1 && PyErr_Occurred()) {
266
0
            goto exit;
267
0
        }
268
0
        if (!--noptargs) {
269
0
            goto skip_optional_kwonly;
270
0
        }
271
0
    }
272
0
    if (args[4]) {
273
0
        co_stacksize = PyLong_AsInt(args[4]);
274
0
        if (co_stacksize == -1 && PyErr_Occurred()) {
275
0
            goto exit;
276
0
        }
277
0
        if (!--noptargs) {
278
0
            goto skip_optional_kwonly;
279
0
        }
280
0
    }
281
0
    if (args[5]) {
282
0
        co_flags = PyLong_AsInt(args[5]);
283
0
        if (co_flags == -1 && PyErr_Occurred()) {
284
0
            goto exit;
285
0
        }
286
0
        if (!--noptargs) {
287
0
            goto skip_optional_kwonly;
288
0
        }
289
0
    }
290
0
    if (args[6]) {
291
0
        co_firstlineno = PyLong_AsInt(args[6]);
292
0
        if (co_firstlineno == -1 && PyErr_Occurred()) {
293
0
            goto exit;
294
0
        }
295
0
        if (!--noptargs) {
296
0
            goto skip_optional_kwonly;
297
0
        }
298
0
    }
299
0
    if (args[7]) {
300
0
        if (!PyBytes_Check(args[7])) {
301
0
            _PyArg_BadArgument("replace", "argument 'co_code'", "bytes", args[7]);
302
0
            goto exit;
303
0
        }
304
0
        co_code = args[7];
305
0
        if (!--noptargs) {
306
0
            goto skip_optional_kwonly;
307
0
        }
308
0
    }
309
0
    if (args[8]) {
310
0
        if (!PyTuple_Check(args[8])) {
311
0
            _PyArg_BadArgument("replace", "argument 'co_consts'", "tuple", args[8]);
312
0
            goto exit;
313
0
        }
314
0
        co_consts = args[8];
315
0
        if (!--noptargs) {
316
0
            goto skip_optional_kwonly;
317
0
        }
318
0
    }
319
0
    if (args[9]) {
320
0
        if (!PyTuple_Check(args[9])) {
321
0
            _PyArg_BadArgument("replace", "argument 'co_names'", "tuple", args[9]);
322
0
            goto exit;
323
0
        }
324
0
        co_names = args[9];
325
0
        if (!--noptargs) {
326
0
            goto skip_optional_kwonly;
327
0
        }
328
0
    }
329
0
    if (args[10]) {
330
0
        if (!PyTuple_Check(args[10])) {
331
0
            _PyArg_BadArgument("replace", "argument 'co_varnames'", "tuple", args[10]);
332
0
            goto exit;
333
0
        }
334
0
        co_varnames = args[10];
335
0
        if (!--noptargs) {
336
0
            goto skip_optional_kwonly;
337
0
        }
338
0
    }
339
0
    if (args[11]) {
340
0
        if (!PyTuple_Check(args[11])) {
341
0
            _PyArg_BadArgument("replace", "argument 'co_freevars'", "tuple", args[11]);
342
0
            goto exit;
343
0
        }
344
0
        co_freevars = args[11];
345
0
        if (!--noptargs) {
346
0
            goto skip_optional_kwonly;
347
0
        }
348
0
    }
349
0
    if (args[12]) {
350
0
        if (!PyTuple_Check(args[12])) {
351
0
            _PyArg_BadArgument("replace", "argument 'co_cellvars'", "tuple", args[12]);
352
0
            goto exit;
353
0
        }
354
0
        co_cellvars = args[12];
355
0
        if (!--noptargs) {
356
0
            goto skip_optional_kwonly;
357
0
        }
358
0
    }
359
0
    if (args[13]) {
360
0
        if (!PyUnicode_Check(args[13])) {
361
0
            _PyArg_BadArgument("replace", "argument 'co_filename'", "str", args[13]);
362
0
            goto exit;
363
0
        }
364
0
        co_filename = args[13];
365
0
        if (!--noptargs) {
366
0
            goto skip_optional_kwonly;
367
0
        }
368
0
    }
369
0
    if (args[14]) {
370
0
        if (!PyUnicode_Check(args[14])) {
371
0
            _PyArg_BadArgument("replace", "argument 'co_name'", "str", args[14]);
372
0
            goto exit;
373
0
        }
374
0
        co_name = args[14];
375
0
        if (!--noptargs) {
376
0
            goto skip_optional_kwonly;
377
0
        }
378
0
    }
379
0
    if (args[15]) {
380
0
        if (!PyUnicode_Check(args[15])) {
381
0
            _PyArg_BadArgument("replace", "argument 'co_qualname'", "str", args[15]);
382
0
            goto exit;
383
0
        }
384
0
        co_qualname = args[15];
385
0
        if (!--noptargs) {
386
0
            goto skip_optional_kwonly;
387
0
        }
388
0
    }
389
0
    if (args[16]) {
390
0
        if (!PyBytes_Check(args[16])) {
391
0
            _PyArg_BadArgument("replace", "argument 'co_linetable'", "bytes", args[16]);
392
0
            goto exit;
393
0
        }
394
0
        co_linetable = args[16];
395
0
        if (!--noptargs) {
396
0
            goto skip_optional_kwonly;
397
0
        }
398
0
    }
399
0
    if (!PyBytes_Check(args[17])) {
400
0
        _PyArg_BadArgument("replace", "argument 'co_exceptiontable'", "bytes", args[17]);
401
0
        goto exit;
402
0
    }
403
0
    co_exceptiontable = args[17];
404
0
skip_optional_kwonly:
405
0
    return_value = code_replace_impl((PyCodeObject *)self, co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals, co_stacksize, co_flags, co_firstlineno, co_code, co_consts, co_names, co_varnames, co_freevars, co_cellvars, co_filename, co_name, co_qualname, co_linetable, co_exceptiontable);
406
407
0
exit:
408
0
    return return_value;
409
0
}
410
411
PyDoc_STRVAR(code__varname_from_oparg__doc__,
412
"_varname_from_oparg($self, /, oparg)\n"
413
"--\n"
414
"\n"
415
"(internal-only) Return the local variable name for the given oparg.\n"
416
"\n"
417
"WARNING: this method is for internal use only and may change or go away.");
418
419
#define CODE__VARNAME_FROM_OPARG_METHODDEF    \
420
    {"_varname_from_oparg", _PyCFunction_CAST(code__varname_from_oparg), METH_FASTCALL|METH_KEYWORDS, code__varname_from_oparg__doc__},
421
422
static PyObject *
423
code__varname_from_oparg_impl(PyCodeObject *self, int oparg);
424
425
static PyObject *
426
code__varname_from_oparg(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
427
0
{
428
0
    PyObject *return_value = NULL;
429
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
430
431
0
    #define NUM_KEYWORDS 1
432
0
    static struct {
433
0
        PyGC_Head _this_is_not_used;
434
0
        PyObject_VAR_HEAD
435
0
        Py_hash_t ob_hash;
436
0
        PyObject *ob_item[NUM_KEYWORDS];
437
0
    } _kwtuple = {
438
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
439
0
        .ob_hash = -1,
440
0
        .ob_item = { &_Py_ID(oparg), },
441
0
    };
442
0
    #undef NUM_KEYWORDS
443
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
444
445
    #else  // !Py_BUILD_CORE
446
    #  define KWTUPLE NULL
447
    #endif  // !Py_BUILD_CORE
448
449
0
    static const char * const _keywords[] = {"oparg", NULL};
450
0
    static _PyArg_Parser _parser = {
451
0
        .keywords = _keywords,
452
0
        .fname = "_varname_from_oparg",
453
0
        .kwtuple = KWTUPLE,
454
0
    };
455
0
    #undef KWTUPLE
456
0
    PyObject *argsbuf[1];
457
0
    int oparg;
458
459
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
460
0
            /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
461
0
    if (!args) {
462
0
        goto exit;
463
0
    }
464
0
    oparg = PyLong_AsInt(args[0]);
465
0
    if (oparg == -1 && PyErr_Occurred()) {
466
0
        goto exit;
467
0
    }
468
0
    return_value = code__varname_from_oparg_impl((PyCodeObject *)self, oparg);
469
470
0
exit:
471
0
    return return_value;
472
0
}
473
/*[clinic end generated code: output=c5c6e40fc357defe input=a9049054013a1b77]*/