Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame^] | 5 | PyDoc_STRVAR(code_new__doc__, |
| 6 | "code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n" |
| 7 | " flags, codestring, constants, names, varnames, filename, name,\n" |
| 8 | " firstlineno, lnotab, freevars=(), cellvars=(), /)\n" |
| 9 | "--\n" |
| 10 | "\n" |
| 11 | "Create a code object. Not for the faint of heart."); |
| 12 | |
| 13 | static PyObject * |
| 14 | code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount, |
| 15 | int kwonlyargcount, int nlocals, int stacksize, int flags, |
| 16 | PyObject *code, PyObject *consts, PyObject *names, |
| 17 | PyObject *varnames, PyObject *filename, PyObject *name, |
| 18 | int firstlineno, PyObject *lnotab, PyObject *freevars, |
| 19 | PyObject *cellvars); |
| 20 | |
| 21 | static PyObject * |
| 22 | code_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 23 | { |
| 24 | PyObject *return_value = NULL; |
| 25 | int argcount; |
| 26 | int posonlyargcount; |
| 27 | int kwonlyargcount; |
| 28 | int nlocals; |
| 29 | int stacksize; |
| 30 | int flags; |
| 31 | PyObject *code; |
| 32 | PyObject *consts; |
| 33 | PyObject *names; |
| 34 | PyObject *varnames; |
| 35 | PyObject *filename; |
| 36 | PyObject *name; |
| 37 | int firstlineno; |
| 38 | PyObject *lnotab; |
| 39 | PyObject *freevars = NULL; |
| 40 | PyObject *cellvars = NULL; |
| 41 | |
| 42 | if ((type == &PyCode_Type) && |
| 43 | !_PyArg_NoKeywords("code", kwargs)) { |
| 44 | goto exit; |
| 45 | } |
| 46 | if (!_PyArg_CheckPositional("code", PyTuple_GET_SIZE(args), 14, 16)) { |
| 47 | goto exit; |
| 48 | } |
| 49 | argcount = _PyLong_AsInt(PyTuple_GET_ITEM(args, 0)); |
| 50 | if (argcount == -1 && PyErr_Occurred()) { |
| 51 | goto exit; |
| 52 | } |
| 53 | posonlyargcount = _PyLong_AsInt(PyTuple_GET_ITEM(args, 1)); |
| 54 | if (posonlyargcount == -1 && PyErr_Occurred()) { |
| 55 | goto exit; |
| 56 | } |
| 57 | kwonlyargcount = _PyLong_AsInt(PyTuple_GET_ITEM(args, 2)); |
| 58 | if (kwonlyargcount == -1 && PyErr_Occurred()) { |
| 59 | goto exit; |
| 60 | } |
| 61 | nlocals = _PyLong_AsInt(PyTuple_GET_ITEM(args, 3)); |
| 62 | if (nlocals == -1 && PyErr_Occurred()) { |
| 63 | goto exit; |
| 64 | } |
| 65 | stacksize = _PyLong_AsInt(PyTuple_GET_ITEM(args, 4)); |
| 66 | if (stacksize == -1 && PyErr_Occurred()) { |
| 67 | goto exit; |
| 68 | } |
| 69 | flags = _PyLong_AsInt(PyTuple_GET_ITEM(args, 5)); |
| 70 | if (flags == -1 && PyErr_Occurred()) { |
| 71 | goto exit; |
| 72 | } |
| 73 | if (!PyBytes_Check(PyTuple_GET_ITEM(args, 6))) { |
| 74 | _PyArg_BadArgument("code", "argument 7", "bytes", PyTuple_GET_ITEM(args, 6)); |
| 75 | goto exit; |
| 76 | } |
| 77 | code = PyTuple_GET_ITEM(args, 6); |
| 78 | if (!PyTuple_Check(PyTuple_GET_ITEM(args, 7))) { |
| 79 | _PyArg_BadArgument("code", "argument 8", "tuple", PyTuple_GET_ITEM(args, 7)); |
| 80 | goto exit; |
| 81 | } |
| 82 | consts = PyTuple_GET_ITEM(args, 7); |
| 83 | if (!PyTuple_Check(PyTuple_GET_ITEM(args, 8))) { |
| 84 | _PyArg_BadArgument("code", "argument 9", "tuple", PyTuple_GET_ITEM(args, 8)); |
| 85 | goto exit; |
| 86 | } |
| 87 | names = PyTuple_GET_ITEM(args, 8); |
| 88 | if (!PyTuple_Check(PyTuple_GET_ITEM(args, 9))) { |
| 89 | _PyArg_BadArgument("code", "argument 10", "tuple", PyTuple_GET_ITEM(args, 9)); |
| 90 | goto exit; |
| 91 | } |
| 92 | varnames = PyTuple_GET_ITEM(args, 9); |
| 93 | if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 10))) { |
| 94 | _PyArg_BadArgument("code", "argument 11", "str", PyTuple_GET_ITEM(args, 10)); |
| 95 | goto exit; |
| 96 | } |
| 97 | if (PyUnicode_READY(PyTuple_GET_ITEM(args, 10)) == -1) { |
| 98 | goto exit; |
| 99 | } |
| 100 | filename = PyTuple_GET_ITEM(args, 10); |
| 101 | if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 11))) { |
| 102 | _PyArg_BadArgument("code", "argument 12", "str", PyTuple_GET_ITEM(args, 11)); |
| 103 | goto exit; |
| 104 | } |
| 105 | if (PyUnicode_READY(PyTuple_GET_ITEM(args, 11)) == -1) { |
| 106 | goto exit; |
| 107 | } |
| 108 | name = PyTuple_GET_ITEM(args, 11); |
| 109 | firstlineno = _PyLong_AsInt(PyTuple_GET_ITEM(args, 12)); |
| 110 | if (firstlineno == -1 && PyErr_Occurred()) { |
| 111 | goto exit; |
| 112 | } |
| 113 | if (!PyBytes_Check(PyTuple_GET_ITEM(args, 13))) { |
| 114 | _PyArg_BadArgument("code", "argument 14", "bytes", PyTuple_GET_ITEM(args, 13)); |
| 115 | goto exit; |
| 116 | } |
| 117 | lnotab = PyTuple_GET_ITEM(args, 13); |
| 118 | if (PyTuple_GET_SIZE(args) < 15) { |
| 119 | goto skip_optional; |
| 120 | } |
| 121 | if (!PyTuple_Check(PyTuple_GET_ITEM(args, 14))) { |
| 122 | _PyArg_BadArgument("code", "argument 15", "tuple", PyTuple_GET_ITEM(args, 14)); |
| 123 | goto exit; |
| 124 | } |
| 125 | freevars = PyTuple_GET_ITEM(args, 14); |
| 126 | if (PyTuple_GET_SIZE(args) < 16) { |
| 127 | goto skip_optional; |
| 128 | } |
| 129 | if (!PyTuple_Check(PyTuple_GET_ITEM(args, 15))) { |
| 130 | _PyArg_BadArgument("code", "argument 16", "tuple", PyTuple_GET_ITEM(args, 15)); |
| 131 | goto exit; |
| 132 | } |
| 133 | cellvars = PyTuple_GET_ITEM(args, 15); |
| 134 | skip_optional: |
| 135 | return_value = code_new_impl(type, argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, firstlineno, lnotab, freevars, cellvars); |
| 136 | |
| 137 | exit: |
| 138 | return return_value; |
| 139 | } |
| 140 | |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 141 | PyDoc_STRVAR(code_replace__doc__, |
| 142 | "replace($self, /, *, co_argcount=-1, co_posonlyargcount=-1,\n" |
| 143 | " co_kwonlyargcount=-1, co_nlocals=-1, co_stacksize=-1,\n" |
| 144 | " co_flags=-1, co_firstlineno=-1, co_code=None, co_consts=None,\n" |
| 145 | " co_names=None, co_varnames=None, co_freevars=None,\n" |
| 146 | " co_cellvars=None, co_filename=None, co_name=None,\n" |
| 147 | " co_lnotab=None)\n" |
| 148 | "--\n" |
| 149 | "\n" |
Anthony Sottile | 22424c0 | 2020-01-01 01:11:16 -0500 | [diff] [blame] | 150 | "Return a copy of the code object with new values for the specified fields."); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 151 | |
| 152 | #define CODE_REPLACE_METHODDEF \ |
| 153 | {"replace", (PyCFunction)(void(*)(void))code_replace, METH_FASTCALL|METH_KEYWORDS, code_replace__doc__}, |
| 154 | |
| 155 | static PyObject * |
| 156 | code_replace_impl(PyCodeObject *self, int co_argcount, |
| 157 | int co_posonlyargcount, int co_kwonlyargcount, |
| 158 | int co_nlocals, int co_stacksize, int co_flags, |
| 159 | int co_firstlineno, PyBytesObject *co_code, |
| 160 | PyObject *co_consts, PyObject *co_names, |
| 161 | PyObject *co_varnames, PyObject *co_freevars, |
| 162 | PyObject *co_cellvars, PyObject *co_filename, |
| 163 | PyObject *co_name, PyBytesObject *co_lnotab); |
| 164 | |
| 165 | static PyObject * |
| 166 | code_replace(PyCodeObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 167 | { |
| 168 | PyObject *return_value = NULL; |
| 169 | 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_lnotab", NULL}; |
| 170 | static _PyArg_Parser _parser = {NULL, _keywords, "replace", 0}; |
| 171 | PyObject *argsbuf[16]; |
| 172 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
| 173 | int co_argcount = self->co_argcount; |
| 174 | int co_posonlyargcount = self->co_posonlyargcount; |
| 175 | int co_kwonlyargcount = self->co_kwonlyargcount; |
| 176 | int co_nlocals = self->co_nlocals; |
| 177 | int co_stacksize = self->co_stacksize; |
| 178 | int co_flags = self->co_flags; |
| 179 | int co_firstlineno = self->co_firstlineno; |
| 180 | PyBytesObject *co_code = (PyBytesObject *)self->co_code; |
| 181 | PyObject *co_consts = self->co_consts; |
| 182 | PyObject *co_names = self->co_names; |
| 183 | PyObject *co_varnames = self->co_varnames; |
| 184 | PyObject *co_freevars = self->co_freevars; |
| 185 | PyObject *co_cellvars = self->co_cellvars; |
| 186 | PyObject *co_filename = self->co_filename; |
| 187 | PyObject *co_name = self->co_name; |
| 188 | PyBytesObject *co_lnotab = (PyBytesObject *)self->co_lnotab; |
| 189 | |
| 190 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf); |
| 191 | if (!args) { |
| 192 | goto exit; |
| 193 | } |
| 194 | if (!noptargs) { |
| 195 | goto skip_optional_kwonly; |
| 196 | } |
| 197 | if (args[0]) { |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 198 | co_argcount = _PyLong_AsInt(args[0]); |
| 199 | if (co_argcount == -1 && PyErr_Occurred()) { |
| 200 | goto exit; |
| 201 | } |
| 202 | if (!--noptargs) { |
| 203 | goto skip_optional_kwonly; |
| 204 | } |
| 205 | } |
| 206 | if (args[1]) { |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 207 | co_posonlyargcount = _PyLong_AsInt(args[1]); |
| 208 | if (co_posonlyargcount == -1 && PyErr_Occurred()) { |
| 209 | goto exit; |
| 210 | } |
| 211 | if (!--noptargs) { |
| 212 | goto skip_optional_kwonly; |
| 213 | } |
| 214 | } |
| 215 | if (args[2]) { |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 216 | co_kwonlyargcount = _PyLong_AsInt(args[2]); |
| 217 | if (co_kwonlyargcount == -1 && PyErr_Occurred()) { |
| 218 | goto exit; |
| 219 | } |
| 220 | if (!--noptargs) { |
| 221 | goto skip_optional_kwonly; |
| 222 | } |
| 223 | } |
| 224 | if (args[3]) { |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 225 | co_nlocals = _PyLong_AsInt(args[3]); |
| 226 | if (co_nlocals == -1 && PyErr_Occurred()) { |
| 227 | goto exit; |
| 228 | } |
| 229 | if (!--noptargs) { |
| 230 | goto skip_optional_kwonly; |
| 231 | } |
| 232 | } |
| 233 | if (args[4]) { |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 234 | co_stacksize = _PyLong_AsInt(args[4]); |
| 235 | if (co_stacksize == -1 && PyErr_Occurred()) { |
| 236 | goto exit; |
| 237 | } |
| 238 | if (!--noptargs) { |
| 239 | goto skip_optional_kwonly; |
| 240 | } |
| 241 | } |
| 242 | if (args[5]) { |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 243 | co_flags = _PyLong_AsInt(args[5]); |
| 244 | if (co_flags == -1 && PyErr_Occurred()) { |
| 245 | goto exit; |
| 246 | } |
| 247 | if (!--noptargs) { |
| 248 | goto skip_optional_kwonly; |
| 249 | } |
| 250 | } |
| 251 | if (args[6]) { |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 252 | co_firstlineno = _PyLong_AsInt(args[6]); |
| 253 | if (co_firstlineno == -1 && PyErr_Occurred()) { |
| 254 | goto exit; |
| 255 | } |
| 256 | if (!--noptargs) { |
| 257 | goto skip_optional_kwonly; |
| 258 | } |
| 259 | } |
| 260 | if (args[7]) { |
| 261 | if (!PyBytes_Check(args[7])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 262 | _PyArg_BadArgument("replace", "argument 'co_code'", "bytes", args[7]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 263 | goto exit; |
| 264 | } |
| 265 | co_code = (PyBytesObject *)args[7]; |
| 266 | if (!--noptargs) { |
| 267 | goto skip_optional_kwonly; |
| 268 | } |
| 269 | } |
| 270 | if (args[8]) { |
| 271 | if (!PyTuple_Check(args[8])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 272 | _PyArg_BadArgument("replace", "argument 'co_consts'", "tuple", args[8]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 273 | goto exit; |
| 274 | } |
| 275 | co_consts = args[8]; |
| 276 | if (!--noptargs) { |
| 277 | goto skip_optional_kwonly; |
| 278 | } |
| 279 | } |
| 280 | if (args[9]) { |
| 281 | if (!PyTuple_Check(args[9])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 282 | _PyArg_BadArgument("replace", "argument 'co_names'", "tuple", args[9]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 283 | goto exit; |
| 284 | } |
| 285 | co_names = args[9]; |
| 286 | if (!--noptargs) { |
| 287 | goto skip_optional_kwonly; |
| 288 | } |
| 289 | } |
| 290 | if (args[10]) { |
| 291 | if (!PyTuple_Check(args[10])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 292 | _PyArg_BadArgument("replace", "argument 'co_varnames'", "tuple", args[10]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 293 | goto exit; |
| 294 | } |
| 295 | co_varnames = args[10]; |
| 296 | if (!--noptargs) { |
| 297 | goto skip_optional_kwonly; |
| 298 | } |
| 299 | } |
| 300 | if (args[11]) { |
| 301 | if (!PyTuple_Check(args[11])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 302 | _PyArg_BadArgument("replace", "argument 'co_freevars'", "tuple", args[11]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 303 | goto exit; |
| 304 | } |
| 305 | co_freevars = args[11]; |
| 306 | if (!--noptargs) { |
| 307 | goto skip_optional_kwonly; |
| 308 | } |
| 309 | } |
| 310 | if (args[12]) { |
| 311 | if (!PyTuple_Check(args[12])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 312 | _PyArg_BadArgument("replace", "argument 'co_cellvars'", "tuple", args[12]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 313 | goto exit; |
| 314 | } |
| 315 | co_cellvars = args[12]; |
| 316 | if (!--noptargs) { |
| 317 | goto skip_optional_kwonly; |
| 318 | } |
| 319 | } |
| 320 | if (args[13]) { |
| 321 | if (!PyUnicode_Check(args[13])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 322 | _PyArg_BadArgument("replace", "argument 'co_filename'", "str", args[13]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 323 | goto exit; |
| 324 | } |
| 325 | if (PyUnicode_READY(args[13]) == -1) { |
| 326 | goto exit; |
| 327 | } |
| 328 | co_filename = args[13]; |
| 329 | if (!--noptargs) { |
| 330 | goto skip_optional_kwonly; |
| 331 | } |
| 332 | } |
| 333 | if (args[14]) { |
| 334 | if (!PyUnicode_Check(args[14])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 335 | _PyArg_BadArgument("replace", "argument 'co_name'", "str", args[14]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 336 | goto exit; |
| 337 | } |
| 338 | if (PyUnicode_READY(args[14]) == -1) { |
| 339 | goto exit; |
| 340 | } |
| 341 | co_name = args[14]; |
| 342 | if (!--noptargs) { |
| 343 | goto skip_optional_kwonly; |
| 344 | } |
| 345 | } |
| 346 | if (!PyBytes_Check(args[15])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 347 | _PyArg_BadArgument("replace", "argument 'co_lnotab'", "bytes", args[15]); |
Victor Stinner | a9f05d6 | 2019-05-24 23:57:23 +0200 | [diff] [blame] | 348 | goto exit; |
| 349 | } |
| 350 | co_lnotab = (PyBytesObject *)args[15]; |
| 351 | skip_optional_kwonly: |
| 352 | 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_lnotab); |
| 353 | |
| 354 | exit: |
| 355 | return return_value; |
| 356 | } |
Serhiy Storchaka | 0f9aa47 | 2020-07-10 10:12:04 +0300 | [diff] [blame^] | 357 | /*[clinic end generated code: output=18c31941ec09e9ca input=a9049054013a1b77]*/ |