(root)/
Python-3.11.7/
Objects/
funcobject.c
       1  
       2  /* Function object implementation */
       3  
       4  #include "Python.h"
       5  #include "pycore_ceval.h"         // _PyEval_BuiltinsFromGlobals()
       6  #include "pycore_object.h"        // _PyObject_GC_UNTRACK()
       7  #include "pycore_pyerrors.h"      // _PyErr_Occurred()
       8  #include "structmember.h"         // PyMemberDef
       9  
      10  static uint32_t next_func_version = 1;
      11  
      12  PyFunctionObject *
      13  _PyFunction_FromConstructor(PyFrameConstructor *constr)
      14  {
      15  
      16      PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
      17      if (op == NULL) {
      18          return NULL;
      19      }
      20      Py_INCREF(constr->fc_globals);
      21      op->func_globals = constr->fc_globals;
      22      Py_INCREF(constr->fc_builtins);
      23      op->func_builtins = constr->fc_builtins;
      24      Py_INCREF(constr->fc_name);
      25      op->func_name = constr->fc_name;
      26      Py_INCREF(constr->fc_qualname);
      27      op->func_qualname = constr->fc_qualname;
      28      Py_INCREF(constr->fc_code);
      29      op->func_code = constr->fc_code;
      30      Py_XINCREF(constr->fc_defaults);
      31      op->func_defaults = constr->fc_defaults;
      32      Py_XINCREF(constr->fc_kwdefaults);
      33      op->func_kwdefaults = constr->fc_kwdefaults;
      34      Py_XINCREF(constr->fc_closure);
      35      op->func_closure = constr->fc_closure;
      36      Py_INCREF(Py_None);
      37      op->func_doc = Py_None;
      38      op->func_dict = NULL;
      39      op->func_weakreflist = NULL;
      40      op->func_module = NULL;
      41      op->func_annotations = NULL;
      42      op->vectorcall = _PyFunction_Vectorcall;
      43      op->func_version = 0;
      44      _PyObject_GC_TRACK(op);
      45      return op;
      46  }
      47  
      48  PyObject *
      49  PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
      50  {
      51      assert(globals != NULL);
      52      assert(PyDict_Check(globals));
      53      Py_INCREF(globals);
      54  
      55      PyThreadState *tstate = _PyThreadState_GET();
      56  
      57      PyCodeObject *code_obj = (PyCodeObject *)code;
      58      Py_INCREF(code_obj);
      59  
      60      PyObject *name = code_obj->co_name;
      61      assert(name != NULL);
      62      Py_INCREF(name);
      63  
      64      if (!qualname) {
      65          qualname = code_obj->co_qualname;
      66      }
      67      assert(qualname != NULL);
      68      Py_INCREF(qualname);
      69  
      70      PyObject *consts = code_obj->co_consts;
      71      assert(PyTuple_Check(consts));
      72      PyObject *doc;
      73      if (PyTuple_Size(consts) >= 1) {
      74          doc = PyTuple_GetItem(consts, 0);
      75          if (!PyUnicode_Check(doc)) {
      76              doc = Py_None;
      77          }
      78      }
      79      else {
      80          doc = Py_None;
      81      }
      82      Py_INCREF(doc);
      83  
      84      // __module__: Use globals['__name__'] if it exists, or NULL.
      85      PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
      86      PyObject *builtins = NULL;
      87      if (module == NULL && _PyErr_Occurred(tstate)) {
      88          goto error;
      89      }
      90      Py_XINCREF(module);
      91  
      92      builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
      93      if (builtins == NULL) {
      94          goto error;
      95      }
      96      Py_INCREF(builtins);
      97  
      98      PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
      99      if (op == NULL) {
     100          goto error;
     101      }
     102      /* Note: No failures from this point on, since func_dealloc() does not
     103         expect a partially-created object. */
     104  
     105      op->func_globals = globals;
     106      op->func_builtins = builtins;
     107      op->func_name = name;
     108      op->func_qualname = qualname;
     109      op->func_code = (PyObject*)code_obj;
     110      op->func_defaults = NULL;    // No default positional arguments
     111      op->func_kwdefaults = NULL;  // No default keyword arguments
     112      op->func_closure = NULL;
     113      op->func_doc = doc;
     114      op->func_dict = NULL;
     115      op->func_weakreflist = NULL;
     116      op->func_module = module;
     117      op->func_annotations = NULL;
     118      op->vectorcall = _PyFunction_Vectorcall;
     119      op->func_version = 0;
     120      _PyObject_GC_TRACK(op);
     121      return (PyObject *)op;
     122  
     123  error:
     124      Py_DECREF(globals);
     125      Py_DECREF(code_obj);
     126      Py_DECREF(name);
     127      Py_DECREF(qualname);
     128      Py_DECREF(doc);
     129      Py_XDECREF(module);
     130      Py_XDECREF(builtins);
     131      return NULL;
     132  }
     133  
     134  uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
     135  {
     136      if (func->func_version != 0) {
     137          return func->func_version;
     138      }
     139      if (next_func_version == 0) {
     140          return 0;
     141      }
     142      uint32_t v = next_func_version++;
     143      func->func_version = v;
     144      return v;
     145  }
     146  
     147  PyObject *
     148  PyFunction_New(PyObject *code, PyObject *globals)
     149  {
     150      return PyFunction_NewWithQualName(code, globals, NULL);
     151  }
     152  
     153  PyObject *
     154  PyFunction_GetCode(PyObject *op)
     155  {
     156      if (!PyFunction_Check(op)) {
     157          PyErr_BadInternalCall();
     158          return NULL;
     159      }
     160      return ((PyFunctionObject *) op) -> func_code;
     161  }
     162  
     163  PyObject *
     164  PyFunction_GetGlobals(PyObject *op)
     165  {
     166      if (!PyFunction_Check(op)) {
     167          PyErr_BadInternalCall();
     168          return NULL;
     169      }
     170      return ((PyFunctionObject *) op) -> func_globals;
     171  }
     172  
     173  PyObject *
     174  PyFunction_GetModule(PyObject *op)
     175  {
     176      if (!PyFunction_Check(op)) {
     177          PyErr_BadInternalCall();
     178          return NULL;
     179      }
     180      return ((PyFunctionObject *) op) -> func_module;
     181  }
     182  
     183  PyObject *
     184  PyFunction_GetDefaults(PyObject *op)
     185  {
     186      if (!PyFunction_Check(op)) {
     187          PyErr_BadInternalCall();
     188          return NULL;
     189      }
     190      return ((PyFunctionObject *) op) -> func_defaults;
     191  }
     192  
     193  int
     194  PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
     195  {
     196      if (!PyFunction_Check(op)) {
     197          PyErr_BadInternalCall();
     198          return -1;
     199      }
     200      if (defaults == Py_None)
     201          defaults = NULL;
     202      else if (defaults && PyTuple_Check(defaults)) {
     203          Py_INCREF(defaults);
     204      }
     205      else {
     206          PyErr_SetString(PyExc_SystemError, "non-tuple default args");
     207          return -1;
     208      }
     209      ((PyFunctionObject *)op)->func_version = 0;
     210      Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
     211      return 0;
     212  }
     213  
     214  PyObject *
     215  PyFunction_GetKwDefaults(PyObject *op)
     216  {
     217      if (!PyFunction_Check(op)) {
     218          PyErr_BadInternalCall();
     219          return NULL;
     220      }
     221      return ((PyFunctionObject *) op) -> func_kwdefaults;
     222  }
     223  
     224  int
     225  PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
     226  {
     227      if (!PyFunction_Check(op)) {
     228          PyErr_BadInternalCall();
     229          return -1;
     230      }
     231      if (defaults == Py_None)
     232          defaults = NULL;
     233      else if (defaults && PyDict_Check(defaults)) {
     234          Py_INCREF(defaults);
     235      }
     236      else {
     237          PyErr_SetString(PyExc_SystemError,
     238                          "non-dict keyword only default args");
     239          return -1;
     240      }
     241      ((PyFunctionObject *)op)->func_version = 0;
     242      Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
     243      return 0;
     244  }
     245  
     246  PyObject *
     247  PyFunction_GetClosure(PyObject *op)
     248  {
     249      if (!PyFunction_Check(op)) {
     250          PyErr_BadInternalCall();
     251          return NULL;
     252      }
     253      return ((PyFunctionObject *) op) -> func_closure;
     254  }
     255  
     256  int
     257  PyFunction_SetClosure(PyObject *op, PyObject *closure)
     258  {
     259      if (!PyFunction_Check(op)) {
     260          PyErr_BadInternalCall();
     261          return -1;
     262      }
     263      if (closure == Py_None)
     264          closure = NULL;
     265      else if (PyTuple_Check(closure)) {
     266          Py_INCREF(closure);
     267      }
     268      else {
     269          PyErr_Format(PyExc_SystemError,
     270                       "expected tuple for closure, got '%.100s'",
     271                       Py_TYPE(closure)->tp_name);
     272          return -1;
     273      }
     274      ((PyFunctionObject *)op)->func_version = 0;
     275      Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
     276      return 0;
     277  }
     278  
     279  static PyObject *
     280  func_get_annotation_dict(PyFunctionObject *op)
     281  {
     282      if (op->func_annotations == NULL) {
     283          return NULL;
     284      }
     285      if (PyTuple_CheckExact(op->func_annotations)) {
     286          PyObject *ann_tuple = op->func_annotations;
     287          PyObject *ann_dict = PyDict_New();
     288          if (ann_dict == NULL) {
     289              return NULL;
     290          }
     291  
     292          assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
     293  
     294          for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
     295              int err = PyDict_SetItem(ann_dict,
     296                                       PyTuple_GET_ITEM(ann_tuple, i),
     297                                       PyTuple_GET_ITEM(ann_tuple, i + 1));
     298  
     299              if (err < 0) {
     300                  return NULL;
     301              }
     302          }
     303          Py_SETREF(op->func_annotations, ann_dict);
     304      }
     305      assert(PyDict_Check(op->func_annotations));
     306      return op->func_annotations;
     307  }
     308  
     309  PyObject *
     310  PyFunction_GetAnnotations(PyObject *op)
     311  {
     312      if (!PyFunction_Check(op)) {
     313          PyErr_BadInternalCall();
     314          return NULL;
     315      }
     316      return func_get_annotation_dict((PyFunctionObject *)op);
     317  }
     318  
     319  int
     320  PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
     321  {
     322      if (!PyFunction_Check(op)) {
     323          PyErr_BadInternalCall();
     324          return -1;
     325      }
     326      if (annotations == Py_None)
     327          annotations = NULL;
     328      else if (annotations && PyDict_Check(annotations)) {
     329          Py_INCREF(annotations);
     330      }
     331      else {
     332          PyErr_SetString(PyExc_SystemError,
     333                          "non-dict annotations");
     334          return -1;
     335      }
     336      ((PyFunctionObject *)op)->func_version = 0;
     337      Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
     338      return 0;
     339  }
     340  
     341  /* Methods */
     342  
     343  #define OFF(x) offsetof(PyFunctionObject, x)
     344  
     345  static PyMemberDef func_memberlist[] = {
     346      {"__closure__",   T_OBJECT,     OFF(func_closure), READONLY},
     347      {"__doc__",       T_OBJECT,     OFF(func_doc), 0},
     348      {"__globals__",   T_OBJECT,     OFF(func_globals), READONLY},
     349      {"__module__",    T_OBJECT,     OFF(func_module), 0},
     350      {"__builtins__",  T_OBJECT,     OFF(func_builtins), READONLY},
     351      {NULL}  /* Sentinel */
     352  };
     353  
     354  static PyObject *
     355  func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
     356  {
     357      if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
     358          return NULL;
     359      }
     360  
     361      Py_INCREF(op->func_code);
     362      return op->func_code;
     363  }
     364  
     365  static int
     366  func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
     367  {
     368      Py_ssize_t nclosure;
     369      int nfree;
     370  
     371      /* Not legal to del f.func_code or to set it to anything
     372       * other than a code object. */
     373      if (value == NULL || !PyCode_Check(value)) {
     374          PyErr_SetString(PyExc_TypeError,
     375                          "__code__ must be set to a code object");
     376          return -1;
     377      }
     378  
     379      if (PySys_Audit("object.__setattr__", "OsO",
     380                      op, "__code__", value) < 0) {
     381          return -1;
     382      }
     383  
     384      nfree = ((PyCodeObject *)value)->co_nfreevars;
     385      nclosure = (op->func_closure == NULL ? 0 :
     386              PyTuple_GET_SIZE(op->func_closure));
     387      if (nclosure != nfree) {
     388          PyErr_Format(PyExc_ValueError,
     389                       "%U() requires a code object with %zd free vars,"
     390                       " not %zd",
     391                       op->func_name,
     392                       nclosure, nfree);
     393          return -1;
     394      }
     395      op->func_version = 0;
     396      Py_INCREF(value);
     397      Py_XSETREF(op->func_code, value);
     398      return 0;
     399  }
     400  
     401  static PyObject *
     402  func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
     403  {
     404      Py_INCREF(op->func_name);
     405      return op->func_name;
     406  }
     407  
     408  static int
     409  func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
     410  {
     411      /* Not legal to del f.func_name or to set it to anything
     412       * other than a string object. */
     413      if (value == NULL || !PyUnicode_Check(value)) {
     414          PyErr_SetString(PyExc_TypeError,
     415                          "__name__ must be set to a string object");
     416          return -1;
     417      }
     418      Py_INCREF(value);
     419      Py_XSETREF(op->func_name, value);
     420      return 0;
     421  }
     422  
     423  static PyObject *
     424  func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
     425  {
     426      Py_INCREF(op->func_qualname);
     427      return op->func_qualname;
     428  }
     429  
     430  static int
     431  func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
     432  {
     433      /* Not legal to del f.__qualname__ or to set it to anything
     434       * other than a string object. */
     435      if (value == NULL || !PyUnicode_Check(value)) {
     436          PyErr_SetString(PyExc_TypeError,
     437                          "__qualname__ must be set to a string object");
     438          return -1;
     439      }
     440      Py_INCREF(value);
     441      Py_XSETREF(op->func_qualname, value);
     442      return 0;
     443  }
     444  
     445  static PyObject *
     446  func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
     447  {
     448      if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
     449          return NULL;
     450      }
     451      if (op->func_defaults == NULL) {
     452          Py_RETURN_NONE;
     453      }
     454      Py_INCREF(op->func_defaults);
     455      return op->func_defaults;
     456  }
     457  
     458  static int
     459  func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
     460  {
     461      /* Legal to del f.func_defaults.
     462       * Can only set func_defaults to NULL or a tuple. */
     463      if (value == Py_None)
     464          value = NULL;
     465      if (value != NULL && !PyTuple_Check(value)) {
     466          PyErr_SetString(PyExc_TypeError,
     467                          "__defaults__ must be set to a tuple object");
     468          return -1;
     469      }
     470      if (value) {
     471          if (PySys_Audit("object.__setattr__", "OsO",
     472                          op, "__defaults__", value) < 0) {
     473              return -1;
     474          }
     475      } else if (PySys_Audit("object.__delattr__", "Os",
     476                             op, "__defaults__") < 0) {
     477          return -1;
     478      }
     479  
     480      op->func_version = 0;
     481      Py_XINCREF(value);
     482      Py_XSETREF(op->func_defaults, value);
     483      return 0;
     484  }
     485  
     486  static PyObject *
     487  func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
     488  {
     489      if (PySys_Audit("object.__getattr__", "Os",
     490                      op, "__kwdefaults__") < 0) {
     491          return NULL;
     492      }
     493      if (op->func_kwdefaults == NULL) {
     494          Py_RETURN_NONE;
     495      }
     496      Py_INCREF(op->func_kwdefaults);
     497      return op->func_kwdefaults;
     498  }
     499  
     500  static int
     501  func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
     502  {
     503      if (value == Py_None)
     504          value = NULL;
     505      /* Legal to del f.func_kwdefaults.
     506       * Can only set func_kwdefaults to NULL or a dict. */
     507      if (value != NULL && !PyDict_Check(value)) {
     508          PyErr_SetString(PyExc_TypeError,
     509              "__kwdefaults__ must be set to a dict object");
     510          return -1;
     511      }
     512      if (value) {
     513          if (PySys_Audit("object.__setattr__", "OsO",
     514                          op, "__kwdefaults__", value) < 0) {
     515              return -1;
     516          }
     517      } else if (PySys_Audit("object.__delattr__", "Os",
     518                             op, "__kwdefaults__") < 0) {
     519          return -1;
     520      }
     521  
     522      op->func_version = 0;
     523      Py_XINCREF(value);
     524      Py_XSETREF(op->func_kwdefaults, value);
     525      return 0;
     526  }
     527  
     528  static PyObject *
     529  func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
     530  {
     531      if (op->func_annotations == NULL) {
     532          op->func_annotations = PyDict_New();
     533          if (op->func_annotations == NULL)
     534              return NULL;
     535      }
     536      PyObject *d = func_get_annotation_dict(op);
     537      if (d) {
     538          Py_INCREF(d);
     539      }
     540      return d;
     541  }
     542  
     543  static int
     544  func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
     545  {
     546      if (value == Py_None)
     547          value = NULL;
     548      /* Legal to del f.func_annotations.
     549       * Can only set func_annotations to NULL (through C api)
     550       * or a dict. */
     551      if (value != NULL && !PyDict_Check(value)) {
     552          PyErr_SetString(PyExc_TypeError,
     553              "__annotations__ must be set to a dict object");
     554          return -1;
     555      }
     556      op->func_version = 0;
     557      Py_XINCREF(value);
     558      Py_XSETREF(op->func_annotations, value);
     559      return 0;
     560  }
     561  
     562  static PyGetSetDef func_getsetlist[] = {
     563      {"__code__", (getter)func_get_code, (setter)func_set_code},
     564      {"__defaults__", (getter)func_get_defaults,
     565       (setter)func_set_defaults},
     566      {"__kwdefaults__", (getter)func_get_kwdefaults,
     567       (setter)func_set_kwdefaults},
     568      {"__annotations__", (getter)func_get_annotations,
     569       (setter)func_set_annotations},
     570      {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
     571      {"__name__", (getter)func_get_name, (setter)func_set_name},
     572      {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
     573      {NULL} /* Sentinel */
     574  };
     575  
     576  /*[clinic input]
     577  class function "PyFunctionObject *" "&PyFunction_Type"
     578  [clinic start generated code]*/
     579  /*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
     580  
     581  #include "clinic/funcobject.c.h"
     582  
     583  /* function.__new__() maintains the following invariants for closures.
     584     The closure must correspond to the free variables of the code object.
     585  
     586     if len(code.co_freevars) == 0:
     587         closure = NULL
     588     else:
     589         len(closure) == len(code.co_freevars)
     590     for every elt in closure, type(elt) == cell
     591  */
     592  
     593  /*[clinic input]
     594  @classmethod
     595  function.__new__ as func_new
     596      code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
     597          a code object
     598      globals: object(subclass_of="&PyDict_Type")
     599          the globals dictionary
     600      name: object = None
     601          a string that overrides the name from the code object
     602      argdefs as defaults: object = None
     603          a tuple that specifies the default argument values
     604      closure: object = None
     605          a tuple that supplies the bindings for free variables
     606  
     607  Create a function object.
     608  [clinic start generated code]*/
     609  
     610  static PyObject *
     611  func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
     612                PyObject *name, PyObject *defaults, PyObject *closure)
     613  /*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
     614  {
     615      PyFunctionObject *newfunc;
     616      Py_ssize_t nclosure;
     617  
     618      if (name != Py_None && !PyUnicode_Check(name)) {
     619          PyErr_SetString(PyExc_TypeError,
     620                          "arg 3 (name) must be None or string");
     621          return NULL;
     622      }
     623      if (defaults != Py_None && !PyTuple_Check(defaults)) {
     624          PyErr_SetString(PyExc_TypeError,
     625                          "arg 4 (defaults) must be None or tuple");
     626          return NULL;
     627      }
     628      if (!PyTuple_Check(closure)) {
     629          if (code->co_nfreevars && closure == Py_None) {
     630              PyErr_SetString(PyExc_TypeError,
     631                              "arg 5 (closure) must be tuple");
     632              return NULL;
     633          }
     634          else if (closure != Py_None) {
     635              PyErr_SetString(PyExc_TypeError,
     636                  "arg 5 (closure) must be None or tuple");
     637              return NULL;
     638          }
     639      }
     640  
     641      /* check that the closure is well-formed */
     642      nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
     643      if (code->co_nfreevars != nclosure)
     644          return PyErr_Format(PyExc_ValueError,
     645                              "%U requires closure of length %zd, not %zd",
     646                              code->co_name, code->co_nfreevars, nclosure);
     647      if (nclosure) {
     648          Py_ssize_t i;
     649          for (i = 0; i < nclosure; i++) {
     650              PyObject *o = PyTuple_GET_ITEM(closure, i);
     651              if (!PyCell_Check(o)) {
     652                  return PyErr_Format(PyExc_TypeError,
     653                      "arg 5 (closure) expected cell, found %s",
     654                                      Py_TYPE(o)->tp_name);
     655              }
     656          }
     657      }
     658      if (PySys_Audit("function.__new__", "O", code) < 0) {
     659          return NULL;
     660      }
     661  
     662      newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
     663                                                   globals);
     664      if (newfunc == NULL) {
     665          return NULL;
     666      }
     667      if (name != Py_None) {
     668          Py_INCREF(name);
     669          Py_SETREF(newfunc->func_name, name);
     670      }
     671      if (defaults != Py_None) {
     672          Py_INCREF(defaults);
     673          newfunc->func_defaults  = defaults;
     674      }
     675      if (closure != Py_None) {
     676          Py_INCREF(closure);
     677          newfunc->func_closure = closure;
     678      }
     679  
     680      return (PyObject *)newfunc;
     681  }
     682  
     683  static int
     684  func_clear(PyFunctionObject *op)
     685  {
     686      op->func_version = 0;
     687      Py_CLEAR(op->func_globals);
     688      Py_CLEAR(op->func_builtins);
     689      Py_CLEAR(op->func_module);
     690      Py_CLEAR(op->func_defaults);
     691      Py_CLEAR(op->func_kwdefaults);
     692      Py_CLEAR(op->func_doc);
     693      Py_CLEAR(op->func_dict);
     694      Py_CLEAR(op->func_closure);
     695      Py_CLEAR(op->func_annotations);
     696      // Don't Py_CLEAR(op->func_code), since code is always required
     697      // to be non-NULL. Similarly, name and qualname shouldn't be NULL.
     698      // However, name and qualname could be str subclasses, so they
     699      // could have reference cycles. The solution is to replace them
     700      // with a genuinely immutable string.
     701      Py_SETREF(op->func_name, Py_NewRef(&_Py_STR(empty)));
     702      Py_SETREF(op->func_qualname, Py_NewRef(&_Py_STR(empty)));
     703      return 0;
     704  }
     705  
     706  static void
     707  func_dealloc(PyFunctionObject *op)
     708  {
     709      _PyObject_GC_UNTRACK(op);
     710      if (op->func_weakreflist != NULL) {
     711          PyObject_ClearWeakRefs((PyObject *) op);
     712      }
     713      (void)func_clear(op);
     714      // These aren't cleared by func_clear().
     715      Py_DECREF(op->func_code);
     716      Py_DECREF(op->func_name);
     717      Py_DECREF(op->func_qualname);
     718      PyObject_GC_Del(op);
     719  }
     720  
     721  static PyObject*
     722  func_repr(PyFunctionObject *op)
     723  {
     724      return PyUnicode_FromFormat("<function %U at %p>",
     725                                  op->func_qualname, op);
     726  }
     727  
     728  static int
     729  func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
     730  {
     731      Py_VISIT(f->func_code);
     732      Py_VISIT(f->func_globals);
     733      Py_VISIT(f->func_builtins);
     734      Py_VISIT(f->func_module);
     735      Py_VISIT(f->func_defaults);
     736      Py_VISIT(f->func_kwdefaults);
     737      Py_VISIT(f->func_doc);
     738      Py_VISIT(f->func_name);
     739      Py_VISIT(f->func_dict);
     740      Py_VISIT(f->func_closure);
     741      Py_VISIT(f->func_annotations);
     742      Py_VISIT(f->func_qualname);
     743      return 0;
     744  }
     745  
     746  /* Bind a function to an object */
     747  static PyObject *
     748  func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
     749  {
     750      if (obj == Py_None || obj == NULL) {
     751          Py_INCREF(func);
     752          return func;
     753      }
     754      return PyMethod_New(func, obj);
     755  }
     756  
     757  PyTypeObject PyFunction_Type = {
     758      PyVarObject_HEAD_INIT(&PyType_Type, 0)
     759      "function",
     760      sizeof(PyFunctionObject),
     761      0,
     762      (destructor)func_dealloc,                   /* tp_dealloc */
     763      offsetof(PyFunctionObject, vectorcall),     /* tp_vectorcall_offset */
     764      0,                                          /* tp_getattr */
     765      0,                                          /* tp_setattr */
     766      0,                                          /* tp_as_async */
     767      (reprfunc)func_repr,                        /* tp_repr */
     768      0,                                          /* tp_as_number */
     769      0,                                          /* tp_as_sequence */
     770      0,                                          /* tp_as_mapping */
     771      0,                                          /* tp_hash */
     772      PyVectorcall_Call,                          /* tp_call */
     773      0,                                          /* tp_str */
     774      0,                                          /* tp_getattro */
     775      0,                                          /* tp_setattro */
     776      0,                                          /* tp_as_buffer */
     777      Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
     778      Py_TPFLAGS_HAVE_VECTORCALL |
     779      Py_TPFLAGS_METHOD_DESCRIPTOR,               /* tp_flags */
     780      func_new__doc__,                            /* tp_doc */
     781      (traverseproc)func_traverse,                /* tp_traverse */
     782      (inquiry)func_clear,                        /* tp_clear */
     783      0,                                          /* tp_richcompare */
     784      offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
     785      0,                                          /* tp_iter */
     786      0,                                          /* tp_iternext */
     787      0,                                          /* tp_methods */
     788      func_memberlist,                            /* tp_members */
     789      func_getsetlist,                            /* tp_getset */
     790      0,                                          /* tp_base */
     791      0,                                          /* tp_dict */
     792      func_descr_get,                             /* tp_descr_get */
     793      0,                                          /* tp_descr_set */
     794      offsetof(PyFunctionObject, func_dict),      /* tp_dictoffset */
     795      0,                                          /* tp_init */
     796      0,                                          /* tp_alloc */
     797      func_new,                                   /* tp_new */
     798  };
     799  
     800  
     801  static int
     802  functools_copy_attr(PyObject *wrapper, PyObject *wrapped, PyObject *name)
     803  {
     804      PyObject *value = PyObject_GetAttr(wrapped, name);
     805      if (value == NULL) {
     806          if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
     807              PyErr_Clear();
     808              return 0;
     809          }
     810          return -1;
     811      }
     812  
     813      int res = PyObject_SetAttr(wrapper, name, value);
     814      Py_DECREF(value);
     815      return res;
     816  }
     817  
     818  // Similar to functools.wraps(wrapper, wrapped)
     819  static int
     820  functools_wraps(PyObject *wrapper, PyObject *wrapped)
     821  {
     822  #define COPY_ATTR(ATTR) \
     823      do { \
     824          if (functools_copy_attr(wrapper, wrapped, &_Py_ID(ATTR)) < 0) { \
     825              return -1; \
     826          } \
     827      } while (0) \
     828  
     829      COPY_ATTR(__module__);
     830      COPY_ATTR(__name__);
     831      COPY_ATTR(__qualname__);
     832      COPY_ATTR(__doc__);
     833      COPY_ATTR(__annotations__);
     834      return 0;
     835  
     836  #undef COPY_ATTR
     837  }
     838  
     839  
     840  /* Class method object */
     841  
     842  /* A class method receives the class as implicit first argument,
     843     just like an instance method receives the instance.
     844     To declare a class method, use this idiom:
     845  
     846       class C:
     847           @classmethod
     848           def f(cls, arg1, arg2, argN):
     849               ...
     850  
     851     It can be called either on the class (e.g. C.f()) or on an instance
     852     (e.g. C().f()); the instance is ignored except for its class.
     853     If a class method is called for a derived class, the derived class
     854     object is passed as the implied first argument.
     855  
     856     Class methods are different than C++ or Java static methods.
     857     If you want those, see static methods below.
     858  */
     859  
     860  typedef struct {
     861      PyObject_HEAD
     862      PyObject *cm_callable;
     863      PyObject *cm_dict;
     864  } classmethod;
     865  
     866  static void
     867  cm_dealloc(classmethod *cm)
     868  {
     869      _PyObject_GC_UNTRACK((PyObject *)cm);
     870      Py_XDECREF(cm->cm_callable);
     871      Py_XDECREF(cm->cm_dict);
     872      Py_TYPE(cm)->tp_free((PyObject *)cm);
     873  }
     874  
     875  static int
     876  cm_traverse(classmethod *cm, visitproc visit, void *arg)
     877  {
     878      Py_VISIT(cm->cm_callable);
     879      Py_VISIT(cm->cm_dict);
     880      return 0;
     881  }
     882  
     883  static int
     884  cm_clear(classmethod *cm)
     885  {
     886      Py_CLEAR(cm->cm_callable);
     887      Py_CLEAR(cm->cm_dict);
     888      return 0;
     889  }
     890  
     891  
     892  static PyObject *
     893  cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
     894  {
     895      classmethod *cm = (classmethod *)self;
     896  
     897      if (cm->cm_callable == NULL) {
     898          PyErr_SetString(PyExc_RuntimeError,
     899                          "uninitialized classmethod object");
     900          return NULL;
     901      }
     902      if (type == NULL)
     903          type = (PyObject *)(Py_TYPE(obj));
     904      if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
     905          return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
     906                                                        type);
     907      }
     908      return PyMethod_New(cm->cm_callable, type);
     909  }
     910  
     911  static int
     912  cm_init(PyObject *self, PyObject *args, PyObject *kwds)
     913  {
     914      classmethod *cm = (classmethod *)self;
     915      PyObject *callable;
     916  
     917      if (!_PyArg_NoKeywords("classmethod", kwds))
     918          return -1;
     919      if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
     920          return -1;
     921      Py_INCREF(callable);
     922      Py_XSETREF(cm->cm_callable, callable);
     923  
     924      if (functools_wraps((PyObject *)cm, cm->cm_callable) < 0) {
     925          return -1;
     926      }
     927      return 0;
     928  }
     929  
     930  static PyMemberDef cm_memberlist[] = {
     931      {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
     932      {"__wrapped__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
     933      {NULL}  /* Sentinel */
     934  };
     935  
     936  static PyObject *
     937  cm_get___isabstractmethod__(classmethod *cm, void *closure)
     938  {
     939      int res = _PyObject_IsAbstract(cm->cm_callable);
     940      if (res == -1) {
     941          return NULL;
     942      }
     943      else if (res) {
     944          Py_RETURN_TRUE;
     945      }
     946      Py_RETURN_FALSE;
     947  }
     948  
     949  static PyGetSetDef cm_getsetlist[] = {
     950      {"__isabstractmethod__",
     951       (getter)cm_get___isabstractmethod__, NULL, NULL, NULL},
     952      {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
     953      {NULL} /* Sentinel */
     954  };
     955  
     956  static PyObject*
     957  cm_repr(classmethod *cm)
     958  {
     959      return PyUnicode_FromFormat("<classmethod(%R)>", cm->cm_callable);
     960  }
     961  
     962  PyDoc_STRVAR(classmethod_doc,
     963  "classmethod(function) -> method\n\
     964  \n\
     965  Convert a function to be a class method.\n\
     966  \n\
     967  A class method receives the class as implicit first argument,\n\
     968  just like an instance method receives the instance.\n\
     969  To declare a class method, use this idiom:\n\
     970  \n\
     971    class C:\n\
     972        @classmethod\n\
     973        def f(cls, arg1, arg2, argN):\n\
     974            ...\n\
     975  \n\
     976  It can be called either on the class (e.g. C.f()) or on an instance\n\
     977  (e.g. C().f()).  The instance is ignored except for its class.\n\
     978  If a class method is called for a derived class, the derived class\n\
     979  object is passed as the implied first argument.\n\
     980  \n\
     981  Class methods are different than C++ or Java static methods.\n\
     982  If you want those, see the staticmethod builtin.");
     983  
     984  PyTypeObject PyClassMethod_Type = {
     985      PyVarObject_HEAD_INIT(&PyType_Type, 0)
     986      "classmethod",
     987      sizeof(classmethod),
     988      0,
     989      (destructor)cm_dealloc,                     /* tp_dealloc */
     990      0,                                          /* tp_vectorcall_offset */
     991      0,                                          /* tp_getattr */
     992      0,                                          /* tp_setattr */
     993      0,                                          /* tp_as_async */
     994      (reprfunc)cm_repr,                          /* tp_repr */
     995      0,                                          /* tp_as_number */
     996      0,                                          /* tp_as_sequence */
     997      0,                                          /* tp_as_mapping */
     998      0,                                          /* tp_hash */
     999      0,                                          /* tp_call */
    1000      0,                                          /* tp_str */
    1001      0,                                          /* tp_getattro */
    1002      0,                                          /* tp_setattro */
    1003      0,                                          /* tp_as_buffer */
    1004      Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
    1005      classmethod_doc,                            /* tp_doc */
    1006      (traverseproc)cm_traverse,                  /* tp_traverse */
    1007      (inquiry)cm_clear,                          /* tp_clear */
    1008      0,                                          /* tp_richcompare */
    1009      0,                                          /* tp_weaklistoffset */
    1010      0,                                          /* tp_iter */
    1011      0,                                          /* tp_iternext */
    1012      0,                                          /* tp_methods */
    1013      cm_memberlist,              /* tp_members */
    1014      cm_getsetlist,                              /* tp_getset */
    1015      0,                                          /* tp_base */
    1016      0,                                          /* tp_dict */
    1017      cm_descr_get,                               /* tp_descr_get */
    1018      0,                                          /* tp_descr_set */
    1019      offsetof(classmethod, cm_dict),             /* tp_dictoffset */
    1020      cm_init,                                    /* tp_init */
    1021      PyType_GenericAlloc,                        /* tp_alloc */
    1022      PyType_GenericNew,                          /* tp_new */
    1023      PyObject_GC_Del,                            /* tp_free */
    1024  };
    1025  
    1026  PyObject *
    1027  PyClassMethod_New(PyObject *callable)
    1028  {
    1029      classmethod *cm = (classmethod *)
    1030          PyType_GenericAlloc(&PyClassMethod_Type, 0);
    1031      if (cm != NULL) {
    1032          Py_INCREF(callable);
    1033          cm->cm_callable = callable;
    1034      }
    1035      return (PyObject *)cm;
    1036  }
    1037  
    1038  
    1039  /* Static method object */
    1040  
    1041  /* A static method does not receive an implicit first argument.
    1042     To declare a static method, use this idiom:
    1043  
    1044       class C:
    1045           @staticmethod
    1046           def f(arg1, arg2, argN):
    1047               ...
    1048  
    1049     It can be called either on the class (e.g. C.f()) or on an instance
    1050     (e.g. C().f()). Both the class and the instance are ignored, and
    1051     neither is passed implicitly as the first argument to the method.
    1052  
    1053     Static methods in Python are similar to those found in Java or C++.
    1054     For a more advanced concept, see class methods above.
    1055  */
    1056  
    1057  typedef struct {
    1058      PyObject_HEAD
    1059      PyObject *sm_callable;
    1060      PyObject *sm_dict;
    1061  } staticmethod;
    1062  
    1063  static void
    1064  sm_dealloc(staticmethod *sm)
    1065  {
    1066      _PyObject_GC_UNTRACK((PyObject *)sm);
    1067      Py_XDECREF(sm->sm_callable);
    1068      Py_XDECREF(sm->sm_dict);
    1069      Py_TYPE(sm)->tp_free((PyObject *)sm);
    1070  }
    1071  
    1072  static int
    1073  sm_traverse(staticmethod *sm, visitproc visit, void *arg)
    1074  {
    1075      Py_VISIT(sm->sm_callable);
    1076      Py_VISIT(sm->sm_dict);
    1077      return 0;
    1078  }
    1079  
    1080  static int
    1081  sm_clear(staticmethod *sm)
    1082  {
    1083      Py_CLEAR(sm->sm_callable);
    1084      Py_CLEAR(sm->sm_dict);
    1085      return 0;
    1086  }
    1087  
    1088  static PyObject *
    1089  sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
    1090  {
    1091      staticmethod *sm = (staticmethod *)self;
    1092  
    1093      if (sm->sm_callable == NULL) {
    1094          PyErr_SetString(PyExc_RuntimeError,
    1095                          "uninitialized staticmethod object");
    1096          return NULL;
    1097      }
    1098      Py_INCREF(sm->sm_callable);
    1099      return sm->sm_callable;
    1100  }
    1101  
    1102  static int
    1103  sm_init(PyObject *self, PyObject *args, PyObject *kwds)
    1104  {
    1105      staticmethod *sm = (staticmethod *)self;
    1106      PyObject *callable;
    1107  
    1108      if (!_PyArg_NoKeywords("staticmethod", kwds))
    1109          return -1;
    1110      if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
    1111          return -1;
    1112      Py_INCREF(callable);
    1113      Py_XSETREF(sm->sm_callable, callable);
    1114  
    1115      if (functools_wraps((PyObject *)sm, sm->sm_callable) < 0) {
    1116          return -1;
    1117      }
    1118      return 0;
    1119  }
    1120  
    1121  static PyObject*
    1122  sm_call(PyObject *callable, PyObject *args, PyObject *kwargs)
    1123  {
    1124      staticmethod *sm = (staticmethod *)callable;
    1125      return PyObject_Call(sm->sm_callable, args, kwargs);
    1126  }
    1127  
    1128  static PyMemberDef sm_memberlist[] = {
    1129      {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
    1130      {"__wrapped__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
    1131      {NULL}  /* Sentinel */
    1132  };
    1133  
    1134  static PyObject *
    1135  sm_get___isabstractmethod__(staticmethod *sm, void *closure)
    1136  {
    1137      int res = _PyObject_IsAbstract(sm->sm_callable);
    1138      if (res == -1) {
    1139          return NULL;
    1140      }
    1141      else if (res) {
    1142          Py_RETURN_TRUE;
    1143      }
    1144      Py_RETURN_FALSE;
    1145  }
    1146  
    1147  static PyGetSetDef sm_getsetlist[] = {
    1148      {"__isabstractmethod__",
    1149       (getter)sm_get___isabstractmethod__, NULL, NULL, NULL},
    1150      {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
    1151      {NULL} /* Sentinel */
    1152  };
    1153  
    1154  static PyObject*
    1155  sm_repr(staticmethod *sm)
    1156  {
    1157      return PyUnicode_FromFormat("<staticmethod(%R)>", sm->sm_callable);
    1158  }
    1159  
    1160  PyDoc_STRVAR(staticmethod_doc,
    1161  "staticmethod(function) -> method\n\
    1162  \n\
    1163  Convert a function to be a static method.\n\
    1164  \n\
    1165  A static method does not receive an implicit first argument.\n\
    1166  To declare a static method, use this idiom:\n\
    1167  \n\
    1168       class C:\n\
    1169           @staticmethod\n\
    1170           def f(arg1, arg2, argN):\n\
    1171               ...\n\
    1172  \n\
    1173  It can be called either on the class (e.g. C.f()) or on an instance\n\
    1174  (e.g. C().f()). Both the class and the instance are ignored, and\n\
    1175  neither is passed implicitly as the first argument to the method.\n\
    1176  \n\
    1177  Static methods in Python are similar to those found in Java or C++.\n\
    1178  For a more advanced concept, see the classmethod builtin.");
    1179  
    1180  PyTypeObject PyStaticMethod_Type = {
    1181      PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1182      "staticmethod",
    1183      sizeof(staticmethod),
    1184      0,
    1185      (destructor)sm_dealloc,                     /* tp_dealloc */
    1186      0,                                          /* tp_vectorcall_offset */
    1187      0,                                          /* tp_getattr */
    1188      0,                                          /* tp_setattr */
    1189      0,                                          /* tp_as_async */
    1190      (reprfunc)sm_repr,                          /* tp_repr */
    1191      0,                                          /* tp_as_number */
    1192      0,                                          /* tp_as_sequence */
    1193      0,                                          /* tp_as_mapping */
    1194      0,                                          /* tp_hash */
    1195      sm_call,                                    /* tp_call */
    1196      0,                                          /* tp_str */
    1197      0,                                          /* tp_getattro */
    1198      0,                                          /* tp_setattro */
    1199      0,                                          /* tp_as_buffer */
    1200      Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
    1201      staticmethod_doc,                           /* tp_doc */
    1202      (traverseproc)sm_traverse,                  /* tp_traverse */
    1203      (inquiry)sm_clear,                          /* tp_clear */
    1204      0,                                          /* tp_richcompare */
    1205      0,                                          /* tp_weaklistoffset */
    1206      0,                                          /* tp_iter */
    1207      0,                                          /* tp_iternext */
    1208      0,                                          /* tp_methods */
    1209      sm_memberlist,              /* tp_members */
    1210      sm_getsetlist,                              /* tp_getset */
    1211      0,                                          /* tp_base */
    1212      0,                                          /* tp_dict */
    1213      sm_descr_get,                               /* tp_descr_get */
    1214      0,                                          /* tp_descr_set */
    1215      offsetof(staticmethod, sm_dict),            /* tp_dictoffset */
    1216      sm_init,                                    /* tp_init */
    1217      PyType_GenericAlloc,                        /* tp_alloc */
    1218      PyType_GenericNew,                          /* tp_new */
    1219      PyObject_GC_Del,                            /* tp_free */
    1220  };
    1221  
    1222  PyObject *
    1223  PyStaticMethod_New(PyObject *callable)
    1224  {
    1225      staticmethod *sm = (staticmethod *)
    1226          PyType_GenericAlloc(&PyStaticMethod_Type, 0);
    1227      if (sm != NULL) {
    1228          Py_INCREF(callable);
    1229          sm->sm_callable = callable;
    1230      }
    1231      return (PyObject *)sm;
    1232  }