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