Debian Codegraph @

Objects/clinic/codeobject.c.h

raw
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 {
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, /, **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 {
179 PyObject *return_value = NULL;
180 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
181
182 #define NUM_KEYWORDS 18
183 static struct {
184 PyGC_Head _this_is_not_used;
185 PyObject_VAR_HEAD
186 Py_hash_t ob_hash;
187 PyObject *ob_item[NUM_KEYWORDS];
188 } _kwtuple = {
189 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
190 .ob_hash = -1,
191 .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 };
193 #undef NUM_KEYWORDS
194 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
195
196 #else // !Py_BUILD_CORE
197 # define KWTUPLE NULL
198 #endif // !Py_BUILD_CORE
199
200 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 static _PyArg_Parser _parser = {
202 .keywords = _keywords,
203 .fname = "replace",
204 .kwtuple = KWTUPLE,
205 };
206 #undef KWTUPLE
207 PyObject *argsbuf[18];
208 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
209 int co_argcount = ((PyCodeObject *)self)->co_argcount;
210 int co_posonlyargcount = ((PyCodeObject *)self)->co_posonlyargcount;
211 int co_kwonlyargcount = ((PyCodeObject *)self)->co_kwonlyargcount;
212 int co_nlocals = ((PyCodeObject *)self)->co_nlocals;
213 int co_stacksize = ((PyCodeObject *)self)->co_stacksize;
214 int co_flags = ((PyCodeObject *)self)->co_flags;
215 int co_firstlineno = ((PyCodeObject *)self)->co_firstlineno;
216 PyObject *co_code = NULL;
217 PyObject *co_consts = ((PyCodeObject *)self)->co_consts;
218 PyObject *co_names = ((PyCodeObject *)self)->co_names;
219 PyObject *co_varnames = NULL;
220 PyObject *co_freevars = NULL;
221 PyObject *co_cellvars = NULL;
222 PyObject *co_filename = ((PyCodeObject *)self)->co_filename;
223 PyObject *co_name = ((PyCodeObject *)self)->co_name;
224 PyObject *co_qualname = ((PyCodeObject *)self)->co_qualname;
225 PyObject *co_linetable = ((PyCodeObject *)self)->co_linetable;
226 PyObject *co_exceptiontable = ((PyCodeObject *)self)->co_exceptiontable;
227
228 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
229 /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
230 if (!args) {
231 goto exit;
232 }
233 if (!noptargs) {
234 goto skip_optional_kwonly;
235 }
236 if (args[0]) {
237 co_argcount = PyLong_AsInt(args[0]);
238 if (co_argcount == -1 && PyErr_Occurred()) {
239 goto exit;
240 }
241 if (!--noptargs) {
242 goto skip_optional_kwonly;
243 }
244 }
245 if (args[1]) {
246 co_posonlyargcount = PyLong_AsInt(args[1]);
247 if (co_posonlyargcount == -1 && PyErr_Occurred()) {
248 goto exit;
249 }
250 if (!--noptargs) {
251 goto skip_optional_kwonly;
252 }
253 }
254 if (args[2]) {
255 co_kwonlyargcount = PyLong_AsInt(args[2]);
256 if (co_kwonlyargcount == -1 && PyErr_Occurred()) {
257 goto exit;
258 }
259 if (!--noptargs) {
260 goto skip_optional_kwonly;
261 }
262 }
263 if (args[3]) {
264 co_nlocals = PyLong_AsInt(args[3]);
265 if (co_nlocals == -1 && PyErr_Occurred()) {
266 goto exit;
267 }
268 if (!--noptargs) {
269 goto skip_optional_kwonly;
270 }
271 }
272 if (args[4]) {
273 co_stacksize = PyLong_AsInt(args[4]);
274 if (co_stacksize == -1 && PyErr_Occurred()) {
275 goto exit;
276 }
277 if (!--noptargs) {
278 goto skip_optional_kwonly;
279 }
280 }
281 if (args[5]) {
282 co_flags = PyLong_AsInt(args[5]);
283 if (co_flags == -1 && PyErr_Occurred()) {
284 goto exit;
285 }
286 if (!--noptargs) {
287 goto skip_optional_kwonly;
288 }
289 }
290 if (args[6]) {
291 co_firstlineno = PyLong_AsInt(args[6]);
292 if (co_firstlineno == -1 && PyErr_Occurred()) {
293 goto exit;
294 }
295 if (!--noptargs) {
296 goto skip_optional_kwonly;
297 }
298 }
299 if (args[7]) {
300 if (!PyBytes_Check(args[7])) {
301 _PyArg_BadArgument("replace", "argument 'co_code'", "bytes", args[7]);
302 goto exit;
303 }
304 co_code = args[7];
305 if (!--noptargs) {
306 goto skip_optional_kwonly;
307 }
308 }
309 if (args[8]) {
310 if (!PyTuple_Check(args[8])) {
311 _PyArg_BadArgument("replace", "argument 'co_consts'", "tuple", args[8]);
312 goto exit;
313 }
314 co_consts = args[8];
315 if (!--noptargs) {
316 goto skip_optional_kwonly;
317 }
318 }
319 if (args[9]) {
320 if (!PyTuple_Check(args[9])) {
321 _PyArg_BadArgument("replace", "argument 'co_names'", "tuple", args[9]);
322 goto exit;
323 }
324 co_names = args[9];
325 if (!--noptargs) {
326 goto skip_optional_kwonly;
327 }
328 }
329 if (args[10]) {
330 if (!PyTuple_Check(args[10])) {
331 _PyArg_BadArgument("replace", "argument 'co_varnames'", "tuple", args[10]);
332 goto exit;
333 }
334 co_varnames = args[10];
335 if (!--noptargs) {
336 goto skip_optional_kwonly;
337 }
338 }
339 if (args[11]) {
340 if (!PyTuple_Check(args[11])) {
341 _PyArg_BadArgument("replace", "argument 'co_freevars'", "tuple", args[11]);
342 goto exit;
343 }
344 co_freevars = args[11];
345 if (!--noptargs) {
346 goto skip_optional_kwonly;
347 }
348 }
349 if (args[12]) {
350 if (!PyTuple_Check(args[12])) {
351 _PyArg_BadArgument("replace", "argument 'co_cellvars'", "tuple", args[12]);
352 goto exit;
353 }
354 co_cellvars = args[12];
355 if (!--noptargs) {
356 goto skip_optional_kwonly;
357 }
358 }
359 if (args[13]) {
360 if (!PyUnicode_Check(args[13])) {
361 _PyArg_BadArgument("replace", "argument 'co_filename'", "str", args[13]);
362 goto exit;
363 }
364 co_filename = args[13];
365 if (!--noptargs) {
366 goto skip_optional_kwonly;
367 }
368 }
369 if (args[14]) {
370 if (!PyUnicode_Check(args[14])) {
371 _PyArg_BadArgument("replace", "argument 'co_name'", "str", args[14]);
372 goto exit;
373 }
374 co_name = args[14];
375 if (!--noptargs) {
376 goto skip_optional_kwonly;
377 }
378 }
379 if (args[15]) {
380 if (!PyUnicode_Check(args[15])) {
381 _PyArg_BadArgument("replace", "argument 'co_qualname'", "str", args[15]);
382 goto exit;
383 }
384 co_qualname = args[15];
385 if (!--noptargs) {
386 goto skip_optional_kwonly;
387 }
388 }
389 if (args[16]) {
390 if (!PyBytes_Check(args[16])) {
391 _PyArg_BadArgument("replace", "argument 'co_linetable'", "bytes", args[16]);
392 goto exit;
393 }
394 co_linetable = args[16];
395 if (!--noptargs) {
396 goto skip_optional_kwonly;
397 }
398 }
399 if (!PyBytes_Check(args[17])) {
400 _PyArg_BadArgument("replace", "argument 'co_exceptiontable'", "bytes", args[17]);
401 goto exit;
402 }
403 co_exceptiontable = args[17];
404 skip_optional_kwonly:
405 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 exit:
408 return return_value;
409 }
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 {
428 PyObject *return_value = NULL;
429 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
430
431 #define NUM_KEYWORDS 1
432 static struct {
433 PyGC_Head _this_is_not_used;
434 PyObject_VAR_HEAD
435 Py_hash_t ob_hash;
436 PyObject *ob_item[NUM_KEYWORDS];
437 } _kwtuple = {
438 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
439 .ob_hash = -1,
440 .ob_item = { &_Py_ID(oparg), },
441 };
442 #undef NUM_KEYWORDS
443 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
444
445 #else // !Py_BUILD_CORE
446 # define KWTUPLE NULL
447 #endif // !Py_BUILD_CORE
448
449 static const char * const _keywords[] = {"oparg", NULL};
450 static _PyArg_Parser _parser = {
451 .keywords = _keywords,
452 .fname = "_varname_from_oparg",
453 .kwtuple = KWTUPLE,
454 };
455 #undef KWTUPLE
456 PyObject *argsbuf[1];
457 int oparg;
458
459 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
460 /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 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((PyCodeObject *)self, oparg);
469
470 exit:
471 return return_value;
472 }
473 /*[clinic end generated code: output=c5c6e40fc357defe input=a9049054013a1b77]*/