(root)/
Python-3.12.0/
Objects/
call.c
       1  #include "Python.h"
       2  #include "pycore_call.h"          // _PyObject_CallNoArgsTstate()
       3  #include "pycore_ceval.h"         // _Py_EnterRecursiveCallTstate()
       4  #include "pycore_dict.h"          // _PyDict_FromItems()
       5  #include "pycore_object.h"        // _PyCFunctionWithKeywords_TrampolineCall()
       6  #include "pycore_pyerrors.h"      // _PyErr_Occurred()
       7  #include "pycore_pystate.h"       // _PyThreadState_GET()
       8  #include "pycore_tuple.h"         // _PyTuple_ITEMS()
       9  
      10  
      11  static PyObject *
      12  null_error(PyThreadState *tstate)
      13  {
      14      if (!_PyErr_Occurred(tstate)) {
      15          _PyErr_SetString(tstate, PyExc_SystemError,
      16                           "null argument to internal routine");
      17      }
      18      return NULL;
      19  }
      20  
      21  
      22  PyObject*
      23  _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
      24                          PyObject *result, const char *where)
      25  {
      26      assert((callable != NULL) ^ (where != NULL));
      27  
      28      if (result == NULL) {
      29          if (!_PyErr_Occurred(tstate)) {
      30              if (callable)
      31                  _PyErr_Format(tstate, PyExc_SystemError,
      32                                "%R returned NULL without setting an exception",
      33                                callable);
      34              else
      35                  _PyErr_Format(tstate, PyExc_SystemError,
      36                                "%s returned NULL without setting an exception",
      37                                where);
      38  #ifdef Py_DEBUG
      39              /* Ensure that the bug is caught in debug mode.
      40                 Py_FatalError() logs the SystemError exception raised above. */
      41              Py_FatalError("a function returned NULL without setting an exception");
      42  #endif
      43              return NULL;
      44          }
      45      }
      46      else {
      47          if (_PyErr_Occurred(tstate)) {
      48              Py_DECREF(result);
      49  
      50              if (callable) {
      51                  _PyErr_FormatFromCauseTstate(
      52                      tstate, PyExc_SystemError,
      53                      "%R returned a result with an exception set", callable);
      54              }
      55              else {
      56                  _PyErr_FormatFromCauseTstate(
      57                      tstate, PyExc_SystemError,
      58                      "%s returned a result with an exception set", where);
      59              }
      60  #ifdef Py_DEBUG
      61              /* Ensure that the bug is caught in debug mode.
      62                 Py_FatalError() logs the SystemError exception raised above. */
      63              Py_FatalError("a function returned a result with an exception set");
      64  #endif
      65              return NULL;
      66          }
      67      }
      68      return result;
      69  }
      70  
      71  
      72  int
      73  _Py_CheckSlotResult(PyObject *obj, const char *slot_name, int success)
      74  {
      75      PyThreadState *tstate = _PyThreadState_GET();
      76      if (!success) {
      77          if (!_PyErr_Occurred(tstate)) {
      78              _Py_FatalErrorFormat(__func__,
      79                                   "Slot %s of type %s failed "
      80                                   "without setting an exception",
      81                                   slot_name, Py_TYPE(obj)->tp_name);
      82          }
      83      }
      84      else {
      85          if (_PyErr_Occurred(tstate)) {
      86              _Py_FatalErrorFormat(__func__,
      87                                   "Slot %s of type %s succeeded "
      88                                   "with an exception set",
      89                                   slot_name, Py_TYPE(obj)->tp_name);
      90          }
      91      }
      92      return 1;
      93  }
      94  
      95  
      96  /* --- Core PyObject call functions ------------------------------- */
      97  
      98  /* Call a callable Python object without any arguments */
      99  PyObject *
     100  PyObject_CallNoArgs(PyObject *func)
     101  {
     102      EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
     103      PyThreadState *tstate = _PyThreadState_GET();
     104      return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
     105  }
     106  
     107  
     108  PyObject *
     109  _PyObject_FastCallDictTstate(PyThreadState *tstate, PyObject *callable,
     110                               PyObject *const *args, size_t nargsf,
     111                               PyObject *kwargs)
     112  {
     113      assert(callable != NULL);
     114  
     115      /* PyObject_VectorcallDict() must not be called with an exception set,
     116         because it can clear it (directly or indirectly) and so the
     117         caller loses its exception */
     118      assert(!_PyErr_Occurred(tstate));
     119  
     120      Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
     121      assert(nargs >= 0);
     122      assert(nargs == 0 || args != NULL);
     123      assert(kwargs == NULL || PyDict_Check(kwargs));
     124  
     125      vectorcallfunc func = _PyVectorcall_Function(callable);
     126      if (func == NULL) {
     127          /* Use tp_call instead */
     128          return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwargs);
     129      }
     130  
     131      PyObject *res;
     132      if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
     133          res = func(callable, args, nargsf, NULL);
     134      }
     135      else {
     136          PyObject *kwnames;
     137          PyObject *const *newargs;
     138          newargs = _PyStack_UnpackDict(tstate,
     139                                        args, nargs,
     140                                        kwargs, &kwnames);
     141          if (newargs == NULL) {
     142              return NULL;
     143          }
     144          res = func(callable, newargs,
     145                     nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
     146          _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
     147      }
     148      return _Py_CheckFunctionResult(tstate, callable, res, NULL);
     149  }
     150  
     151  
     152  PyObject *
     153  PyObject_VectorcallDict(PyObject *callable, PyObject *const *args,
     154                         size_t nargsf, PyObject *kwargs)
     155  {
     156      PyThreadState *tstate = _PyThreadState_GET();
     157      return _PyObject_FastCallDictTstate(tstate, callable, args, nargsf, kwargs);
     158  }
     159  
     160  static void
     161  object_is_not_callable(PyThreadState *tstate, PyObject *callable)
     162  {
     163      if (Py_IS_TYPE(callable, &PyModule_Type)) {
     164          // >>> import pprint
     165          // >>> pprint(thing)
     166          // Traceback (most recent call last):
     167          //   File "<stdin>", line 1, in <module>
     168          // TypeError: 'module' object is not callable. Did you mean: 'pprint.pprint(...)'?
     169          PyObject *name = PyModule_GetNameObject(callable);
     170          if (name == NULL) {
     171              _PyErr_Clear(tstate);
     172              goto basic_type_error;
     173          }
     174          PyObject *attr;
     175          int res = _PyObject_LookupAttr(callable, name, &attr);
     176          if (res < 0) {
     177              _PyErr_Clear(tstate);
     178          }
     179          else if (res > 0 && PyCallable_Check(attr)) {
     180              _PyErr_Format(tstate, PyExc_TypeError,
     181                            "'%.200s' object is not callable. "
     182                            "Did you mean: '%U.%U(...)'?",
     183                            Py_TYPE(callable)->tp_name, name, name);
     184              Py_DECREF(attr);
     185              Py_DECREF(name);
     186              return;
     187          }
     188          Py_XDECREF(attr);
     189          Py_DECREF(name);
     190      }
     191  basic_type_error:
     192      _PyErr_Format(tstate, PyExc_TypeError, "'%.200s' object is not callable",
     193                    Py_TYPE(callable)->tp_name);
     194  }
     195  
     196  
     197  PyObject *
     198  _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
     199                       PyObject *const *args, Py_ssize_t nargs,
     200                       PyObject *keywords)
     201  {
     202      assert(nargs >= 0);
     203      assert(nargs == 0 || args != NULL);
     204      assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
     205  
     206      /* Slow path: build a temporary tuple for positional arguments and a
     207       * temporary dictionary for keyword arguments (if any) */
     208      ternaryfunc call = Py_TYPE(callable)->tp_call;
     209      if (call == NULL) {
     210          object_is_not_callable(tstate, callable);
     211          return NULL;
     212      }
     213  
     214      PyObject *argstuple = _PyTuple_FromArray(args, nargs);
     215      if (argstuple == NULL) {
     216          return NULL;
     217      }
     218  
     219      PyObject *kwdict;
     220      if (keywords == NULL || PyDict_Check(keywords)) {
     221          kwdict = keywords;
     222      }
     223      else {
     224          if (PyTuple_GET_SIZE(keywords)) {
     225              assert(args != NULL);
     226              kwdict = _PyStack_AsDict(args + nargs, keywords);
     227              if (kwdict == NULL) {
     228                  Py_DECREF(argstuple);
     229                  return NULL;
     230              }
     231          }
     232          else {
     233              keywords = kwdict = NULL;
     234          }
     235      }
     236  
     237      PyObject *result = NULL;
     238      if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object") == 0)
     239      {
     240          result = _PyCFunctionWithKeywords_TrampolineCall(
     241              (PyCFunctionWithKeywords)call, callable, argstuple, kwdict);
     242          _Py_LeaveRecursiveCallTstate(tstate);
     243      }
     244  
     245      Py_DECREF(argstuple);
     246      if (kwdict != keywords) {
     247          Py_DECREF(kwdict);
     248      }
     249  
     250      return _Py_CheckFunctionResult(tstate, callable, result, NULL);
     251  }
     252  
     253  
     254  vectorcallfunc
     255  PyVectorcall_Function(PyObject *callable)
     256  {
     257      return _PyVectorcall_FunctionInline(callable);
     258  }
     259  
     260  
     261  static PyObject *
     262  _PyVectorcall_Call(PyThreadState *tstate, vectorcallfunc func,
     263                     PyObject *callable, PyObject *tuple, PyObject *kwargs)
     264  {
     265      assert(func != NULL);
     266  
     267      Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
     268  
     269      /* Fast path for no keywords */
     270      if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
     271          return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
     272      }
     273  
     274      /* Convert arguments & call */
     275      PyObject *const *args;
     276      PyObject *kwnames;
     277      args = _PyStack_UnpackDict(tstate,
     278                                 _PyTuple_ITEMS(tuple), nargs,
     279                                 kwargs, &kwnames);
     280      if (args == NULL) {
     281          return NULL;
     282      }
     283      PyObject *result = func(callable, args,
     284                              nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
     285      _PyStack_UnpackDict_Free(args, nargs, kwnames);
     286  
     287      return _Py_CheckFunctionResult(tstate, callable, result, NULL);
     288  }
     289  
     290  
     291  PyObject *
     292  PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
     293  {
     294      PyThreadState *tstate = _PyThreadState_GET();
     295  
     296      /* get vectorcallfunc as in _PyVectorcall_Function, but without
     297       * the Py_TPFLAGS_HAVE_VECTORCALL check */
     298      Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
     299      if (offset <= 0) {
     300          _PyErr_Format(tstate, PyExc_TypeError,
     301                        "'%.200s' object does not support vectorcall",
     302                        Py_TYPE(callable)->tp_name);
     303          return NULL;
     304      }
     305      assert(PyCallable_Check(callable));
     306  
     307      vectorcallfunc func;
     308      memcpy(&func, (char *) callable + offset, sizeof(func));
     309      if (func == NULL) {
     310          _PyErr_Format(tstate, PyExc_TypeError,
     311                        "'%.200s' object does not support vectorcall",
     312                        Py_TYPE(callable)->tp_name);
     313          return NULL;
     314      }
     315  
     316      return _PyVectorcall_Call(tstate, func, callable, tuple, kwargs);
     317  }
     318  
     319  
     320  PyObject *
     321  PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
     322                       size_t nargsf, PyObject *kwnames)
     323  {
     324      PyThreadState *tstate = _PyThreadState_GET();
     325      return _PyObject_VectorcallTstate(tstate, callable,
     326                                        args, nargsf, kwnames);
     327  }
     328  
     329  
     330  PyObject *
     331  _PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
     332  {
     333      PyThreadState *tstate = _PyThreadState_GET();
     334      return _PyObject_FastCallTstate(tstate, func, args, nargs);
     335  }
     336  
     337  
     338  PyObject *
     339  _PyObject_Call(PyThreadState *tstate, PyObject *callable,
     340                 PyObject *args, PyObject *kwargs)
     341  {
     342      ternaryfunc call;
     343      PyObject *result;
     344  
     345      /* PyObject_Call() must not be called with an exception set,
     346         because it can clear it (directly or indirectly) and so the
     347         caller loses its exception */
     348      assert(!_PyErr_Occurred(tstate));
     349      assert(PyTuple_Check(args));
     350      assert(kwargs == NULL || PyDict_Check(kwargs));
     351      EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
     352      vectorcallfunc vector_func = _PyVectorcall_Function(callable);
     353      if (vector_func != NULL) {
     354          return _PyVectorcall_Call(tstate, vector_func, callable, args, kwargs);
     355      }
     356      else {
     357          call = Py_TYPE(callable)->tp_call;
     358          if (call == NULL) {
     359              object_is_not_callable(tstate, callable);
     360              return NULL;
     361          }
     362  
     363          if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
     364              return NULL;
     365          }
     366  
     367          result = (*call)(callable, args, kwargs);
     368  
     369          _Py_LeaveRecursiveCallTstate(tstate);
     370  
     371          return _Py_CheckFunctionResult(tstate, callable, result, NULL);
     372      }
     373  }
     374  
     375  PyObject *
     376  PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
     377  {
     378      PyThreadState *tstate = _PyThreadState_GET();
     379      return _PyObject_Call(tstate, callable, args, kwargs);
     380  }
     381  
     382  
     383  PyObject *
     384  PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
     385  {
     386      PyThreadState *tstate = _PyThreadState_GET();
     387      return _PyObject_Call(tstate, callable, args, kwargs);
     388  }
     389  
     390  
     391  PyObject *
     392  PyObject_CallOneArg(PyObject *func, PyObject *arg)
     393  {
     394      EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
     395      assert(arg != NULL);
     396      PyObject *_args[2];
     397      PyObject **args = _args + 1;  // For PY_VECTORCALL_ARGUMENTS_OFFSET
     398      args[0] = arg;
     399      PyThreadState *tstate = _PyThreadState_GET();
     400      size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
     401      return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
     402  }
     403  
     404  
     405  /* --- PyFunction call functions ---------------------------------- */
     406  
     407  PyObject *
     408  _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
     409                         size_t nargsf, PyObject *kwnames)
     410  {
     411      assert(PyFunction_Check(func));
     412      PyFunctionObject *f = (PyFunctionObject *)func;
     413      Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
     414      assert(nargs >= 0);
     415      PyThreadState *tstate = _PyThreadState_GET();
     416      assert(nargs == 0 || stack != NULL);
     417      EVAL_CALL_STAT_INC(EVAL_CALL_FUNCTION_VECTORCALL);
     418      if (((PyCodeObject *)f->func_code)->co_flags & CO_OPTIMIZED) {
     419          return _PyEval_Vector(tstate, f, NULL, stack, nargs, kwnames);
     420      }
     421      else {
     422          return _PyEval_Vector(tstate, f, f->func_globals, stack, nargs, kwnames);
     423      }
     424  }
     425  
     426  /* --- More complex call functions -------------------------------- */
     427  
     428  /* External interface to call any callable object.
     429     The args must be a tuple or NULL.  The kwargs must be a dict or NULL. */
     430  PyObject *
     431  PyEval_CallObjectWithKeywords(PyObject *callable,
     432                                PyObject *args, PyObject *kwargs)
     433  {
     434      PyThreadState *tstate = _PyThreadState_GET();
     435  #ifdef Py_DEBUG
     436      /* PyEval_CallObjectWithKeywords() must not be called with an exception
     437         set. It raises a new exception if parameters are invalid or if
     438         PyTuple_New() fails, and so the original exception is lost. */
     439      assert(!_PyErr_Occurred(tstate));
     440  #endif
     441  
     442      if (args != NULL && !PyTuple_Check(args)) {
     443          _PyErr_SetString(tstate, PyExc_TypeError,
     444                           "argument list must be a tuple");
     445          return NULL;
     446      }
     447  
     448      if (kwargs != NULL && !PyDict_Check(kwargs)) {
     449          _PyErr_SetString(tstate, PyExc_TypeError,
     450                           "keyword list must be a dictionary");
     451          return NULL;
     452      }
     453  
     454      if (args == NULL) {
     455          return _PyObject_FastCallDictTstate(tstate, callable, NULL, 0, kwargs);
     456      }
     457      else {
     458          return _PyObject_Call(tstate, callable, args, kwargs);
     459      }
     460  }
     461  
     462  
     463  PyObject *
     464  PyObject_CallObject(PyObject *callable, PyObject *args)
     465  {
     466      PyThreadState *tstate = _PyThreadState_GET();
     467      assert(!_PyErr_Occurred(tstate));
     468      if (args == NULL) {
     469          return _PyObject_CallNoArgsTstate(tstate, callable);
     470      }
     471      if (!PyTuple_Check(args)) {
     472          _PyErr_SetString(tstate, PyExc_TypeError,
     473                           "argument list must be a tuple");
     474          return NULL;
     475      }
     476      return _PyObject_Call(tstate, callable, args, NULL);
     477  }
     478  
     479  
     480  /* Call callable(obj, *args, **kwargs). */
     481  PyObject *
     482  _PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable,
     483                         PyObject *obj, PyObject *args, PyObject *kwargs)
     484  {
     485      assert(PyTuple_Check(args));
     486  
     487      PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
     488      PyObject **stack;
     489  
     490      Py_ssize_t argcount = PyTuple_GET_SIZE(args);
     491      if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
     492          stack = small_stack;
     493      }
     494      else {
     495          stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
     496          if (stack == NULL) {
     497              PyErr_NoMemory();
     498              return NULL;
     499          }
     500      }
     501  
     502      /* use borrowed references */
     503      stack[0] = obj;
     504      memcpy(&stack[1],
     505             _PyTuple_ITEMS(args),
     506             argcount * sizeof(PyObject *));
     507  
     508      PyObject *result = _PyObject_FastCallDictTstate(tstate, callable,
     509                                                      stack, argcount + 1,
     510                                                      kwargs);
     511      if (stack != small_stack) {
     512          PyMem_Free(stack);
     513      }
     514      return result;
     515  }
     516  
     517  
     518  /* --- Call with a format string ---------------------------------- */
     519  
     520  static PyObject *
     521  _PyObject_CallFunctionVa(PyThreadState *tstate, PyObject *callable,
     522                           const char *format, va_list va, int is_size_t)
     523  {
     524      PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
     525      const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
     526      PyObject **stack;
     527      Py_ssize_t nargs, i;
     528      PyObject *result;
     529  
     530      if (callable == NULL) {
     531          return null_error(tstate);
     532      }
     533  
     534      if (!format || !*format) {
     535          return _PyObject_CallNoArgsTstate(tstate, callable);
     536      }
     537  
     538      if (is_size_t) {
     539          stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
     540                                         format, va, &nargs);
     541      }
     542      else {
     543          stack = _Py_VaBuildStack(small_stack, small_stack_len,
     544                                   format, va, &nargs);
     545      }
     546      if (stack == NULL) {
     547          return NULL;
     548      }
     549      EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
     550      if (nargs == 1 && PyTuple_Check(stack[0])) {
     551          /* Special cases for backward compatibility:
     552             - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
     553             - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
     554               func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
     555          PyObject *args = stack[0];
     556          result = _PyObject_VectorcallTstate(tstate, callable,
     557                                              _PyTuple_ITEMS(args),
     558                                              PyTuple_GET_SIZE(args),
     559                                              NULL);
     560      }
     561      else {
     562          result = _PyObject_VectorcallTstate(tstate, callable,
     563                                              stack, nargs, NULL);
     564      }
     565  
     566      for (i = 0; i < nargs; ++i) {
     567          Py_DECREF(stack[i]);
     568      }
     569      if (stack != small_stack) {
     570          PyMem_Free(stack);
     571      }
     572      return result;
     573  }
     574  
     575  
     576  PyObject *
     577  PyObject_CallFunction(PyObject *callable, const char *format, ...)
     578  {
     579      va_list va;
     580      PyObject *result;
     581      PyThreadState *tstate = _PyThreadState_GET();
     582  
     583      va_start(va, format);
     584      result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
     585      va_end(va);
     586  
     587      return result;
     588  }
     589  
     590  
     591  /* PyEval_CallFunction is exact copy of PyObject_CallFunction.
     592   * This function is kept for backward compatibility.
     593   */
     594  PyObject *
     595  PyEval_CallFunction(PyObject *callable, const char *format, ...)
     596  {
     597      va_list va;
     598      PyObject *result;
     599      PyThreadState *tstate = _PyThreadState_GET();
     600  
     601      va_start(va, format);
     602      result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
     603      va_end(va);
     604  
     605      return result;
     606  }
     607  
     608  
     609  PyObject *
     610  _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
     611  {
     612      PyThreadState *tstate = _PyThreadState_GET();
     613  
     614      va_list va;
     615      va_start(va, format);
     616      PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va, 1);
     617      va_end(va);
     618  
     619      return result;
     620  }
     621  
     622  
     623  static PyObject*
     624  callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va, int is_size_t)
     625  {
     626      assert(callable != NULL);
     627      if (!PyCallable_Check(callable)) {
     628          _PyErr_Format(tstate, PyExc_TypeError,
     629                        "attribute of type '%.200s' is not callable",
     630                        Py_TYPE(callable)->tp_name);
     631          return NULL;
     632      }
     633  
     634      return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t);
     635  }
     636  
     637  PyObject *
     638  PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
     639  {
     640      PyThreadState *tstate = _PyThreadState_GET();
     641  
     642      if (obj == NULL || name == NULL) {
     643          return null_error(tstate);
     644      }
     645  
     646      PyObject *callable = PyObject_GetAttrString(obj, name);
     647      if (callable == NULL) {
     648          return NULL;
     649      }
     650  
     651      va_list va;
     652      va_start(va, format);
     653      PyObject *retval = callmethod(tstate, callable, format, va, 0);
     654      va_end(va);
     655  
     656      Py_DECREF(callable);
     657      return retval;
     658  }
     659  
     660  
     661  /* PyEval_CallMethod is exact copy of PyObject_CallMethod.
     662   * This function is kept for backward compatibility.
     663   */
     664  PyObject *
     665  PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
     666  {
     667      PyThreadState *tstate = _PyThreadState_GET();
     668      if (obj == NULL || name == NULL) {
     669          return null_error(tstate);
     670      }
     671  
     672      PyObject *callable = PyObject_GetAttrString(obj, name);
     673      if (callable == NULL) {
     674          return NULL;
     675      }
     676  
     677      va_list va;
     678      va_start(va, format);
     679      PyObject *retval = callmethod(tstate, callable, format, va, 0);
     680      va_end(va);
     681  
     682      Py_DECREF(callable);
     683      return retval;
     684  }
     685  
     686  
     687  PyObject *
     688  _PyObject_CallMethod(PyObject *obj, PyObject *name,
     689                       const char *format, ...)
     690  {
     691      PyThreadState *tstate = _PyThreadState_GET();
     692      if (obj == NULL || name == NULL) {
     693          return null_error(tstate);
     694      }
     695  
     696      PyObject *callable = PyObject_GetAttr(obj, name);
     697      if (callable == NULL) {
     698          return NULL;
     699      }
     700  
     701      va_list va;
     702      va_start(va, format);
     703      PyObject *retval = callmethod(tstate, callable, format, va, 1);
     704      va_end(va);
     705  
     706      Py_DECREF(callable);
     707      return retval;
     708  }
     709  
     710  
     711  PyObject *
     712  _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
     713                         const char *format, ...)
     714  {
     715      PyThreadState *tstate = _PyThreadState_GET();
     716      if (obj == NULL || name == NULL) {
     717          return null_error(tstate);
     718      }
     719  
     720      PyObject *callable = _PyObject_GetAttrId(obj, name);
     721      if (callable == NULL) {
     722          return NULL;
     723      }
     724  
     725      va_list va;
     726      va_start(va, format);
     727      PyObject *retval = callmethod(tstate, callable, format, va, 0);
     728      va_end(va);
     729  
     730      Py_DECREF(callable);
     731      return retval;
     732  }
     733  
     734  
     735  PyObject * _PyObject_CallMethodFormat(PyThreadState *tstate, PyObject *callable,
     736                                        const char *format, ...)
     737  {
     738      va_list va;
     739      va_start(va, format);
     740      PyObject *retval = callmethod(tstate, callable, format, va, 0);
     741      va_end(va);
     742      return retval;
     743  }
     744  
     745  
     746  PyObject *
     747  _PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
     748                             const char *format, ...)
     749  {
     750      PyThreadState *tstate = _PyThreadState_GET();
     751      if (obj == NULL || name == NULL) {
     752          return null_error(tstate);
     753      }
     754  
     755      PyObject *callable = PyObject_GetAttrString(obj, name);
     756      if (callable == NULL) {
     757          return NULL;
     758      }
     759  
     760      va_list va;
     761      va_start(va, format);
     762      PyObject *retval = callmethod(tstate, callable, format, va, 1);
     763      va_end(va);
     764  
     765      Py_DECREF(callable);
     766      return retval;
     767  }
     768  
     769  
     770  PyObject *
     771  _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
     772                               const char *format, ...)
     773  {
     774      PyThreadState *tstate = _PyThreadState_GET();
     775      if (obj == NULL || name == NULL) {
     776          return null_error(tstate);
     777      }
     778  
     779      PyObject *callable = _PyObject_GetAttrId(obj, name);
     780      if (callable == NULL) {
     781          return NULL;
     782      }
     783  
     784      va_list va;
     785      va_start(va, format);
     786      PyObject *retval = callmethod(tstate, callable, format, va, 1);
     787      va_end(va);
     788  
     789      Py_DECREF(callable);
     790      return retval;
     791  }
     792  
     793  
     794  /* --- Call with "..." arguments ---------------------------------- */
     795  
     796  static PyObject *
     797  object_vacall(PyThreadState *tstate, PyObject *base,
     798                PyObject *callable, va_list vargs)
     799  {
     800      PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
     801      PyObject **stack;
     802      Py_ssize_t nargs;
     803      PyObject *result;
     804      Py_ssize_t i;
     805      va_list countva;
     806  
     807      if (callable == NULL) {
     808          return null_error(tstate);
     809      }
     810  
     811      /* Count the number of arguments */
     812      va_copy(countva, vargs);
     813      nargs = base ? 1 : 0;
     814      while (1) {
     815          PyObject *arg = va_arg(countva, PyObject *);
     816          if (arg == NULL) {
     817              break;
     818          }
     819          nargs++;
     820      }
     821      va_end(countva);
     822  
     823      /* Copy arguments */
     824      if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
     825          stack = small_stack;
     826      }
     827      else {
     828          stack = PyMem_Malloc(nargs * sizeof(stack[0]));
     829          if (stack == NULL) {
     830              PyErr_NoMemory();
     831              return NULL;
     832          }
     833      }
     834  
     835      i = 0;
     836      if (base) {
     837          stack[i++] = base;
     838      }
     839  
     840      for (; i < nargs; ++i) {
     841          stack[i] = va_arg(vargs, PyObject *);
     842      }
     843  
     844  #ifdef Py_STATS
     845      if (PyFunction_Check(callable)) {
     846          EVAL_CALL_STAT_INC(EVAL_CALL_API);
     847      }
     848  #endif
     849      /* Call the function */
     850      result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL);
     851  
     852      if (stack != small_stack) {
     853          PyMem_Free(stack);
     854      }
     855      return result;
     856  }
     857  
     858  
     859  PyObject *
     860  PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
     861                             size_t nargsf, PyObject *kwnames)
     862  {
     863      assert(name != NULL);
     864      assert(args != NULL);
     865      assert(PyVectorcall_NARGS(nargsf) >= 1);
     866  
     867      PyThreadState *tstate = _PyThreadState_GET();
     868      PyObject *callable = NULL;
     869      /* Use args[0] as "self" argument */
     870      int unbound = _PyObject_GetMethod(args[0], name, &callable);
     871      if (callable == NULL) {
     872          return NULL;
     873      }
     874  
     875      if (unbound) {
     876          /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
     877           * that would be interpreted as allowing to change args[-1] */
     878          nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
     879      }
     880      else {
     881          /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
     882           * args[-1] in the onward call is args[0] here. */
     883          args++;
     884          nargsf--;
     885      }
     886      EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_METHOD, callable);
     887      PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
     888                                                    args, nargsf, kwnames);
     889      Py_DECREF(callable);
     890      return result;
     891  }
     892  
     893  
     894  PyObject *
     895  PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
     896  {
     897      PyThreadState *tstate = _PyThreadState_GET();
     898      if (obj == NULL || name == NULL) {
     899          return null_error(tstate);
     900      }
     901  
     902      PyObject *callable = NULL;
     903      int is_method = _PyObject_GetMethod(obj, name, &callable);
     904      if (callable == NULL) {
     905          return NULL;
     906      }
     907      obj = is_method ? obj : NULL;
     908  
     909      va_list vargs;
     910      va_start(vargs, name);
     911      PyObject *result = object_vacall(tstate, obj, callable, vargs);
     912      va_end(vargs);
     913  
     914      Py_DECREF(callable);
     915      return result;
     916  }
     917  
     918  
     919  PyObject *
     920  _PyObject_CallMethodIdObjArgs(PyObject *obj, _Py_Identifier *name, ...)
     921  {
     922      PyThreadState *tstate = _PyThreadState_GET();
     923      if (obj == NULL || name == NULL) {
     924          return null_error(tstate);
     925      }
     926  
     927      PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
     928      if (!oname) {
     929          return NULL;
     930      }
     931  
     932      PyObject *callable = NULL;
     933      int is_method = _PyObject_GetMethod(obj, oname, &callable);
     934      if (callable == NULL) {
     935          return NULL;
     936      }
     937      obj = is_method ? obj : NULL;
     938  
     939      va_list vargs;
     940      va_start(vargs, name);
     941      PyObject *result = object_vacall(tstate, obj, callable, vargs);
     942      va_end(vargs);
     943  
     944      Py_DECREF(callable);
     945      return result;
     946  }
     947  
     948  
     949  PyObject *
     950  PyObject_CallFunctionObjArgs(PyObject *callable, ...)
     951  {
     952      PyThreadState *tstate = _PyThreadState_GET();
     953      va_list vargs;
     954      PyObject *result;
     955  
     956      va_start(vargs, callable);
     957      result = object_vacall(tstate, NULL, callable, vargs);
     958      va_end(vargs);
     959  
     960      return result;
     961  }
     962  
     963  
     964  /* --- PyStack functions ------------------------------------------ */
     965  
     966  PyObject *
     967  _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
     968  {
     969      Py_ssize_t nkwargs;
     970  
     971      assert(kwnames != NULL);
     972      nkwargs = PyTuple_GET_SIZE(kwnames);
     973      return _PyDict_FromItems(&PyTuple_GET_ITEM(kwnames, 0), 1,
     974                               values, 1, nkwargs);
     975  }
     976  
     977  
     978  /* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
     979  
     980     Allocate a new argument vector and keyword names tuple. Return the argument
     981     vector; return NULL with exception set on error. Return the keyword names
     982     tuple in *p_kwnames.
     983  
     984     This also checks that all keyword names are strings. If not, a TypeError is
     985     raised.
     986  
     987     The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
     988  
     989     When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
     990  PyObject *const *
     991  _PyStack_UnpackDict(PyThreadState *tstate,
     992                      PyObject *const *args, Py_ssize_t nargs,
     993                      PyObject *kwargs, PyObject **p_kwnames)
     994  {
     995      assert(nargs >= 0);
     996      assert(kwargs != NULL);
     997      assert(PyDict_Check(kwargs));
     998  
     999      Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
    1000      /* Check for overflow in the PyMem_Malloc() call below. The subtraction
    1001       * in this check cannot overflow: both maxnargs and nkwargs are
    1002       * non-negative signed integers, so their difference fits in the type. */
    1003      Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
    1004      if (nargs > maxnargs - nkwargs) {
    1005          _PyErr_NoMemory(tstate);
    1006          return NULL;
    1007      }
    1008  
    1009      /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
    1010      PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
    1011      if (stack == NULL) {
    1012          _PyErr_NoMemory(tstate);
    1013          return NULL;
    1014      }
    1015  
    1016      PyObject *kwnames = PyTuple_New(nkwargs);
    1017      if (kwnames == NULL) {
    1018          PyMem_Free(stack);
    1019          return NULL;
    1020      }
    1021  
    1022      stack++;  /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
    1023  
    1024      /* Copy positional arguments */
    1025      for (Py_ssize_t i = 0; i < nargs; i++) {
    1026          stack[i] = Py_NewRef(args[i]);
    1027      }
    1028  
    1029      PyObject **kwstack = stack + nargs;
    1030      /* This loop doesn't support lookup function mutating the dictionary
    1031         to change its size. It's a deliberate choice for speed, this function is
    1032         called in the performance critical hot code. */
    1033      Py_ssize_t pos = 0, i = 0;
    1034      PyObject *key, *value;
    1035      unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
    1036      while (PyDict_Next(kwargs, &pos, &key, &value)) {
    1037          keys_are_strings &= Py_TYPE(key)->tp_flags;
    1038          PyTuple_SET_ITEM(kwnames, i, Py_NewRef(key));
    1039          kwstack[i] = Py_NewRef(value);
    1040          i++;
    1041      }
    1042  
    1043      /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that
    1044       * flag is set for all keys. Otherwise, keys_are_strings equals 0.
    1045       * We do this check once at the end instead of inside the loop above
    1046       * because it simplifies the deallocation in the failing case.
    1047       * It happens to also make the loop above slightly more efficient. */
    1048      if (!keys_are_strings) {
    1049          _PyErr_SetString(tstate, PyExc_TypeError,
    1050                           "keywords must be strings");
    1051          _PyStack_UnpackDict_Free(stack, nargs, kwnames);
    1052          return NULL;
    1053      }
    1054  
    1055      *p_kwnames = kwnames;
    1056      return stack;
    1057  }
    1058  
    1059  void
    1060  _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
    1061                           PyObject *kwnames)
    1062  {
    1063      Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
    1064      for (Py_ssize_t i = 0; i < n; i++) {
    1065          Py_DECREF(stack[i]);
    1066      }
    1067      _PyStack_UnpackDict_FreeNoDecRef(stack, kwnames);
    1068  }
    1069  
    1070  void
    1071  _PyStack_UnpackDict_FreeNoDecRef(PyObject *const *stack, PyObject *kwnames)
    1072  {
    1073      PyMem_Free((PyObject **)stack - 1);
    1074      Py_DECREF(kwnames);
    1075  }
    1076  
    1077  // Export for the stable ABI
    1078  #undef PyVectorcall_NARGS
    1079  Py_ssize_t
    1080  PyVectorcall_NARGS(size_t n)
    1081  {
    1082      return _PyVectorcall_NARGS(n);
    1083  }