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