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