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