123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083 |
- #include "Python.h"
- #include "pycore_call.h" // _PyObject_CallNoArgsTstate()
- #include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
- #include "pycore_dict.h" // _PyDict_FromItems()
- #include "pycore_object.h" // _PyCFunctionWithKeywords_TrampolineCall()
- #include "pycore_pyerrors.h" // _PyErr_Occurred()
- #include "pycore_pystate.h" // _PyThreadState_GET()
- #include "pycore_tuple.h" // _PyTuple_ITEMS()
- static PyObject *
- null_error(PyThreadState *tstate)
- {
- if (!_PyErr_Occurred(tstate)) {
- _PyErr_SetString(tstate, PyExc_SystemError,
- "null argument to internal routine");
- }
- return NULL;
- }
- PyObject*
- _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
- PyObject *result, const char *where)
- {
- assert((callable != NULL) ^ (where != NULL));
- if (result == NULL) {
- if (!_PyErr_Occurred(tstate)) {
- if (callable)
- _PyErr_Format(tstate, PyExc_SystemError,
- "%R returned NULL without setting an exception",
- callable);
- else
- _PyErr_Format(tstate, PyExc_SystemError,
- "%s returned NULL without setting an exception",
- where);
- #ifdef Py_DEBUG
- /* Ensure that the bug is caught in debug mode.
- Py_FatalError() logs the SystemError exception raised above. */
- Py_FatalError("a function returned NULL without setting an exception");
- #endif
- return NULL;
- }
- }
- else {
- if (_PyErr_Occurred(tstate)) {
- Py_DECREF(result);
- if (callable) {
- _PyErr_FormatFromCauseTstate(
- tstate, PyExc_SystemError,
- "%R returned a result with an exception set", callable);
- }
- else {
- _PyErr_FormatFromCauseTstate(
- tstate, PyExc_SystemError,
- "%s returned a result with an exception set", where);
- }
- #ifdef Py_DEBUG
- /* Ensure that the bug is caught in debug mode.
- Py_FatalError() logs the SystemError exception raised above. */
- Py_FatalError("a function returned a result with an exception set");
- #endif
- return NULL;
- }
- }
- return result;
- }
- int
- _Py_CheckSlotResult(PyObject *obj, const char *slot_name, int success)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (!success) {
- if (!_PyErr_Occurred(tstate)) {
- _Py_FatalErrorFormat(__func__,
- "Slot %s of type %s failed "
- "without setting an exception",
- slot_name, Py_TYPE(obj)->tp_name);
- }
- }
- else {
- if (_PyErr_Occurred(tstate)) {
- _Py_FatalErrorFormat(__func__,
- "Slot %s of type %s succeeded "
- "with an exception set",
- slot_name, Py_TYPE(obj)->tp_name);
- }
- }
- return 1;
- }
- /* --- Core PyObject call functions ------------------------------- */
- /* Call a callable Python object without any arguments */
- PyObject *
- PyObject_CallNoArgs(PyObject *func)
- {
- EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
- PyThreadState *tstate = _PyThreadState_GET();
- return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
- }
- PyObject *
- _PyObject_FastCallDictTstate(PyThreadState *tstate, PyObject *callable,
- PyObject *const *args, size_t nargsf,
- PyObject *kwargs)
- {
- assert(callable != NULL);
- /* PyObject_VectorcallDict() must not be called with an exception set,
- because it can clear it (directly or indirectly) and so the
- caller loses its exception */
- assert(!_PyErr_Occurred(tstate));
- Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
- assert(nargs >= 0);
- assert(nargs == 0 || args != NULL);
- assert(kwargs == NULL || PyDict_Check(kwargs));
- vectorcallfunc func = _PyVectorcall_Function(callable);
- if (func == NULL) {
- /* Use tp_call instead */
- return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwargs);
- }
- PyObject *res;
- if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
- res = func(callable, args, nargsf, NULL);
- }
- else {
- PyObject *kwnames;
- PyObject *const *newargs;
- newargs = _PyStack_UnpackDict(tstate,
- args, nargs,
- kwargs, &kwnames);
- if (newargs == NULL) {
- return NULL;
- }
- res = func(callable, newargs,
- nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
- _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
- }
- return _Py_CheckFunctionResult(tstate, callable, res, NULL);
- }
- PyObject *
- PyObject_VectorcallDict(PyObject *callable, PyObject *const *args,
- size_t nargsf, PyObject *kwargs)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- return _PyObject_FastCallDictTstate(tstate, callable, args, nargsf, kwargs);
- }
- static void
- object_is_not_callable(PyThreadState *tstate, PyObject *callable)
- {
- if (Py_IS_TYPE(callable, &PyModule_Type)) {
- // >>> import pprint
- // >>> pprint(thing)
- // Traceback (most recent call last):
- // File "<stdin>", line 1, in <module>
- // TypeError: 'module' object is not callable. Did you mean: 'pprint.pprint(...)'?
- PyObject *name = PyModule_GetNameObject(callable);
- if (name == NULL) {
- _PyErr_Clear(tstate);
- goto basic_type_error;
- }
- PyObject *attr;
- int res = _PyObject_LookupAttr(callable, name, &attr);
- if (res < 0) {
- _PyErr_Clear(tstate);
- }
- else if (res > 0 && PyCallable_Check(attr)) {
- _PyErr_Format(tstate, PyExc_TypeError,
- "'%.200s' object is not callable. "
- "Did you mean: '%U.%U(...)'?",
- Py_TYPE(callable)->tp_name, name, name);
- Py_DECREF(attr);
- Py_DECREF(name);
- return;
- }
- Py_XDECREF(attr);
- Py_DECREF(name);
- }
- basic_type_error:
- _PyErr_Format(tstate, PyExc_TypeError, "'%.200s' object is not callable",
- Py_TYPE(callable)->tp_name);
- }
- PyObject *
- _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
- PyObject *const *args, Py_ssize_t nargs,
- PyObject *keywords)
- {
- assert(nargs >= 0);
- assert(nargs == 0 || args != NULL);
- assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
- /* Slow path: build a temporary tuple for positional arguments and a
- * temporary dictionary for keyword arguments (if any) */
- ternaryfunc call = Py_TYPE(callable)->tp_call;
- if (call == NULL) {
- object_is_not_callable(tstate, callable);
- return NULL;
- }
- PyObject *argstuple = _PyTuple_FromArray(args, nargs);
- if (argstuple == NULL) {
- return NULL;
- }
- PyObject *kwdict;
- if (keywords == NULL || PyDict_Check(keywords)) {
- kwdict = keywords;
- }
- else {
- if (PyTuple_GET_SIZE(keywords)) {
- assert(args != NULL);
- kwdict = _PyStack_AsDict(args + nargs, keywords);
- if (kwdict == NULL) {
- Py_DECREF(argstuple);
- return NULL;
- }
- }
- else {
- keywords = kwdict = NULL;
- }
- }
- PyObject *result = NULL;
- if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object") == 0)
- {
- result = _PyCFunctionWithKeywords_TrampolineCall(
- (PyCFunctionWithKeywords)call, callable, argstuple, kwdict);
- _Py_LeaveRecursiveCallTstate(tstate);
- }
- Py_DECREF(argstuple);
- if (kwdict != keywords) {
- Py_DECREF(kwdict);
- }
- return _Py_CheckFunctionResult(tstate, callable, result, NULL);
- }
- vectorcallfunc
- PyVectorcall_Function(PyObject *callable)
- {
- return _PyVectorcall_FunctionInline(callable);
- }
- static PyObject *
- _PyVectorcall_Call(PyThreadState *tstate, vectorcallfunc func,
- PyObject *callable, PyObject *tuple, PyObject *kwargs)
- {
- assert(func != NULL);
- Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
- /* Fast path for no keywords */
- if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
- return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
- }
- /* Convert arguments & call */
- PyObject *const *args;
- PyObject *kwnames;
- args = _PyStack_UnpackDict(tstate,
- _PyTuple_ITEMS(tuple), nargs,
- kwargs, &kwnames);
- if (args == NULL) {
- return NULL;
- }
- PyObject *result = func(callable, args,
- nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
- _PyStack_UnpackDict_Free(args, nargs, kwnames);
- return _Py_CheckFunctionResult(tstate, callable, result, NULL);
- }
- PyObject *
- PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- /* get vectorcallfunc as in _PyVectorcall_Function, but without
- * the Py_TPFLAGS_HAVE_VECTORCALL check */
- Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
- if (offset <= 0) {
- _PyErr_Format(tstate, PyExc_TypeError,
- "'%.200s' object does not support vectorcall",
- Py_TYPE(callable)->tp_name);
- return NULL;
- }
- assert(PyCallable_Check(callable));
- vectorcallfunc func;
- memcpy(&func, (char *) callable + offset, sizeof(func));
- if (func == NULL) {
- _PyErr_Format(tstate, PyExc_TypeError,
- "'%.200s' object does not support vectorcall",
- Py_TYPE(callable)->tp_name);
- return NULL;
- }
- return _PyVectorcall_Call(tstate, func, callable, tuple, kwargs);
- }
- PyObject *
- PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
- size_t nargsf, PyObject *kwnames)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- return _PyObject_VectorcallTstate(tstate, callable,
- args, nargsf, kwnames);
- }
- PyObject *
- _PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- return _PyObject_FastCallTstate(tstate, func, args, nargs);
- }
- PyObject *
- _PyObject_Call(PyThreadState *tstate, PyObject *callable,
- PyObject *args, PyObject *kwargs)
- {
- ternaryfunc call;
- PyObject *result;
- /* PyObject_Call() must not be called with an exception set,
- because it can clear it (directly or indirectly) and so the
- caller loses its exception */
- assert(!_PyErr_Occurred(tstate));
- assert(PyTuple_Check(args));
- assert(kwargs == NULL || PyDict_Check(kwargs));
- EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
- vectorcallfunc vector_func = _PyVectorcall_Function(callable);
- if (vector_func != NULL) {
- return _PyVectorcall_Call(tstate, vector_func, callable, args, kwargs);
- }
- else {
- call = Py_TYPE(callable)->tp_call;
- if (call == NULL) {
- object_is_not_callable(tstate, callable);
- return NULL;
- }
- if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
- return NULL;
- }
- result = (*call)(callable, args, kwargs);
- _Py_LeaveRecursiveCallTstate(tstate);
- return _Py_CheckFunctionResult(tstate, callable, result, NULL);
- }
- }
- PyObject *
- PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- return _PyObject_Call(tstate, callable, args, kwargs);
- }
- PyObject *
- PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- return _PyObject_Call(tstate, callable, args, kwargs);
- }
- PyObject *
- PyObject_CallOneArg(PyObject *func, PyObject *arg)
- {
- EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
- assert(arg != NULL);
- PyObject *_args[2];
- PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
- args[0] = arg;
- PyThreadState *tstate = _PyThreadState_GET();
- size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
- return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
- }
- /* --- PyFunction call functions ---------------------------------- */
- PyObject *
- _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
- size_t nargsf, PyObject *kwnames)
- {
- assert(PyFunction_Check(func));
- PyFunctionObject *f = (PyFunctionObject *)func;
- Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
- assert(nargs >= 0);
- PyThreadState *tstate = _PyThreadState_GET();
- assert(nargs == 0 || stack != NULL);
- EVAL_CALL_STAT_INC(EVAL_CALL_FUNCTION_VECTORCALL);
- if (((PyCodeObject *)f->func_code)->co_flags & CO_OPTIMIZED) {
- return _PyEval_Vector(tstate, f, NULL, stack, nargs, kwnames);
- }
- else {
- return _PyEval_Vector(tstate, f, f->func_globals, stack, nargs, kwnames);
- }
- }
- /* --- More complex call functions -------------------------------- */
- /* External interface to call any callable object.
- The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
- PyObject *
- PyEval_CallObjectWithKeywords(PyObject *callable,
- PyObject *args, PyObject *kwargs)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- #ifdef Py_DEBUG
- /* PyEval_CallObjectWithKeywords() must not be called with an exception
- set. It raises a new exception if parameters are invalid or if
- PyTuple_New() fails, and so the original exception is lost. */
- assert(!_PyErr_Occurred(tstate));
- #endif
- if (args != NULL && !PyTuple_Check(args)) {
- _PyErr_SetString(tstate, PyExc_TypeError,
- "argument list must be a tuple");
- return NULL;
- }
- if (kwargs != NULL && !PyDict_Check(kwargs)) {
- _PyErr_SetString(tstate, PyExc_TypeError,
- "keyword list must be a dictionary");
- return NULL;
- }
- if (args == NULL) {
- return _PyObject_FastCallDictTstate(tstate, callable, NULL, 0, kwargs);
- }
- else {
- return _PyObject_Call(tstate, callable, args, kwargs);
- }
- }
- PyObject *
- PyObject_CallObject(PyObject *callable, PyObject *args)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- assert(!_PyErr_Occurred(tstate));
- if (args == NULL) {
- return _PyObject_CallNoArgsTstate(tstate, callable);
- }
- if (!PyTuple_Check(args)) {
- _PyErr_SetString(tstate, PyExc_TypeError,
- "argument list must be a tuple");
- return NULL;
- }
- return _PyObject_Call(tstate, callable, args, NULL);
- }
- /* Call callable(obj, *args, **kwargs). */
- PyObject *
- _PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable,
- PyObject *obj, PyObject *args, PyObject *kwargs)
- {
- assert(PyTuple_Check(args));
- PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
- PyObject **stack;
- Py_ssize_t argcount = PyTuple_GET_SIZE(args);
- if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
- stack = small_stack;
- }
- else {
- stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
- if (stack == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- }
- /* use borrowed references */
- stack[0] = obj;
- memcpy(&stack[1],
- _PyTuple_ITEMS(args),
- argcount * sizeof(PyObject *));
- PyObject *result = _PyObject_FastCallDictTstate(tstate, callable,
- stack, argcount + 1,
- kwargs);
- if (stack != small_stack) {
- PyMem_Free(stack);
- }
- return result;
- }
- /* --- Call with a format string ---------------------------------- */
- static PyObject *
- _PyObject_CallFunctionVa(PyThreadState *tstate, PyObject *callable,
- const char *format, va_list va, int is_size_t)
- {
- PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
- const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
- PyObject **stack;
- Py_ssize_t nargs, i;
- PyObject *result;
- if (callable == NULL) {
- return null_error(tstate);
- }
- if (!format || !*format) {
- return _PyObject_CallNoArgsTstate(tstate, callable);
- }
- if (is_size_t) {
- stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
- format, va, &nargs);
- }
- else {
- stack = _Py_VaBuildStack(small_stack, small_stack_len,
- format, va, &nargs);
- }
- if (stack == NULL) {
- return NULL;
- }
- EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
- if (nargs == 1 && PyTuple_Check(stack[0])) {
- /* Special cases for backward compatibility:
- - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
- - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
- func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
- PyObject *args = stack[0];
- result = _PyObject_VectorcallTstate(tstate, callable,
- _PyTuple_ITEMS(args),
- PyTuple_GET_SIZE(args),
- NULL);
- }
- else {
- result = _PyObject_VectorcallTstate(tstate, callable,
- stack, nargs, NULL);
- }
- for (i = 0; i < nargs; ++i) {
- Py_DECREF(stack[i]);
- }
- if (stack != small_stack) {
- PyMem_Free(stack);
- }
- return result;
- }
- PyObject *
- PyObject_CallFunction(PyObject *callable, const char *format, ...)
- {
- va_list va;
- PyObject *result;
- PyThreadState *tstate = _PyThreadState_GET();
- va_start(va, format);
- result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
- va_end(va);
- return result;
- }
- /* PyEval_CallFunction is exact copy of PyObject_CallFunction.
- * This function is kept for backward compatibility.
- */
- PyObject *
- PyEval_CallFunction(PyObject *callable, const char *format, ...)
- {
- va_list va;
- PyObject *result;
- PyThreadState *tstate = _PyThreadState_GET();
- va_start(va, format);
- result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
- va_end(va);
- return result;
- }
- PyObject *
- _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- va_list va;
- va_start(va, format);
- PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va, 1);
- va_end(va);
- return result;
- }
- static PyObject*
- callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va, int is_size_t)
- {
- assert(callable != NULL);
- if (!PyCallable_Check(callable)) {
- _PyErr_Format(tstate, PyExc_TypeError,
- "attribute of type '%.200s' is not callable",
- Py_TYPE(callable)->tp_name);
- return NULL;
- }
- return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t);
- }
- PyObject *
- PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *callable = PyObject_GetAttrString(obj, name);
- if (callable == NULL) {
- return NULL;
- }
- va_list va;
- va_start(va, format);
- PyObject *retval = callmethod(tstate, callable, format, va, 0);
- va_end(va);
- Py_DECREF(callable);
- return retval;
- }
- /* PyEval_CallMethod is exact copy of PyObject_CallMethod.
- * This function is kept for backward compatibility.
- */
- PyObject *
- PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *callable = PyObject_GetAttrString(obj, name);
- if (callable == NULL) {
- return NULL;
- }
- va_list va;
- va_start(va, format);
- PyObject *retval = callmethod(tstate, callable, format, va, 0);
- va_end(va);
- Py_DECREF(callable);
- return retval;
- }
- PyObject *
- _PyObject_CallMethod(PyObject *obj, PyObject *name,
- const char *format, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *callable = PyObject_GetAttr(obj, name);
- if (callable == NULL) {
- return NULL;
- }
- va_list va;
- va_start(va, format);
- PyObject *retval = callmethod(tstate, callable, format, va, 1);
- va_end(va);
- Py_DECREF(callable);
- return retval;
- }
- PyObject *
- _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
- const char *format, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *callable = _PyObject_GetAttrId(obj, name);
- if (callable == NULL) {
- return NULL;
- }
- va_list va;
- va_start(va, format);
- PyObject *retval = callmethod(tstate, callable, format, va, 0);
- va_end(va);
- Py_DECREF(callable);
- return retval;
- }
- PyObject * _PyObject_CallMethodFormat(PyThreadState *tstate, PyObject *callable,
- const char *format, ...)
- {
- va_list va;
- va_start(va, format);
- PyObject *retval = callmethod(tstate, callable, format, va, 0);
- va_end(va);
- return retval;
- }
- PyObject *
- _PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
- const char *format, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *callable = PyObject_GetAttrString(obj, name);
- if (callable == NULL) {
- return NULL;
- }
- va_list va;
- va_start(va, format);
- PyObject *retval = callmethod(tstate, callable, format, va, 1);
- va_end(va);
- Py_DECREF(callable);
- return retval;
- }
- PyObject *
- _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
- const char *format, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *callable = _PyObject_GetAttrId(obj, name);
- if (callable == NULL) {
- return NULL;
- }
- va_list va;
- va_start(va, format);
- PyObject *retval = callmethod(tstate, callable, format, va, 1);
- va_end(va);
- Py_DECREF(callable);
- return retval;
- }
- /* --- Call with "..." arguments ---------------------------------- */
- static PyObject *
- object_vacall(PyThreadState *tstate, PyObject *base,
- PyObject *callable, va_list vargs)
- {
- PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
- PyObject **stack;
- Py_ssize_t nargs;
- PyObject *result;
- Py_ssize_t i;
- va_list countva;
- if (callable == NULL) {
- return null_error(tstate);
- }
- /* Count the number of arguments */
- va_copy(countva, vargs);
- nargs = base ? 1 : 0;
- while (1) {
- PyObject *arg = va_arg(countva, PyObject *);
- if (arg == NULL) {
- break;
- }
- nargs++;
- }
- va_end(countva);
- /* Copy arguments */
- if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
- stack = small_stack;
- }
- else {
- stack = PyMem_Malloc(nargs * sizeof(stack[0]));
- if (stack == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- }
- i = 0;
- if (base) {
- stack[i++] = base;
- }
- for (; i < nargs; ++i) {
- stack[i] = va_arg(vargs, PyObject *);
- }
- #ifdef Py_STATS
- if (PyFunction_Check(callable)) {
- EVAL_CALL_STAT_INC(EVAL_CALL_API);
- }
- #endif
- /* Call the function */
- result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL);
- if (stack != small_stack) {
- PyMem_Free(stack);
- }
- return result;
- }
- PyObject *
- PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
- size_t nargsf, PyObject *kwnames)
- {
- assert(name != NULL);
- assert(args != NULL);
- assert(PyVectorcall_NARGS(nargsf) >= 1);
- PyThreadState *tstate = _PyThreadState_GET();
- PyObject *callable = NULL;
- /* Use args[0] as "self" argument */
- int unbound = _PyObject_GetMethod(args[0], name, &callable);
- if (callable == NULL) {
- return NULL;
- }
- if (unbound) {
- /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
- * that would be interpreted as allowing to change args[-1] */
- nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
- }
- else {
- /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
- * args[-1] in the onward call is args[0] here. */
- args++;
- nargsf--;
- }
- EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_METHOD, callable);
- PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
- args, nargsf, kwnames);
- Py_DECREF(callable);
- return result;
- }
- PyObject *
- PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *callable = NULL;
- int is_method = _PyObject_GetMethod(obj, name, &callable);
- if (callable == NULL) {
- return NULL;
- }
- obj = is_method ? obj : NULL;
- va_list vargs;
- va_start(vargs, name);
- PyObject *result = object_vacall(tstate, obj, callable, vargs);
- va_end(vargs);
- Py_DECREF(callable);
- return result;
- }
- PyObject *
- _PyObject_CallMethodIdObjArgs(PyObject *obj, _Py_Identifier *name, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- if (obj == NULL || name == NULL) {
- return null_error(tstate);
- }
- PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
- if (!oname) {
- return NULL;
- }
- PyObject *callable = NULL;
- int is_method = _PyObject_GetMethod(obj, oname, &callable);
- if (callable == NULL) {
- return NULL;
- }
- obj = is_method ? obj : NULL;
- va_list vargs;
- va_start(vargs, name);
- PyObject *result = object_vacall(tstate, obj, callable, vargs);
- va_end(vargs);
- Py_DECREF(callable);
- return result;
- }
- PyObject *
- PyObject_CallFunctionObjArgs(PyObject *callable, ...)
- {
- PyThreadState *tstate = _PyThreadState_GET();
- va_list vargs;
- PyObject *result;
- va_start(vargs, callable);
- result = object_vacall(tstate, NULL, callable, vargs);
- va_end(vargs);
- return result;
- }
- /* --- PyStack functions ------------------------------------------ */
- PyObject *
- _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
- {
- Py_ssize_t nkwargs;
- assert(kwnames != NULL);
- nkwargs = PyTuple_GET_SIZE(kwnames);
- return _PyDict_FromItems(&PyTuple_GET_ITEM(kwnames, 0), 1,
- values, 1, nkwargs);
- }
- /* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
- Allocate a new argument vector and keyword names tuple. Return the argument
- vector; return NULL with exception set on error. Return the keyword names
- tuple in *p_kwnames.
- This also checks that all keyword names are strings. If not, a TypeError is
- raised.
- The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
- When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
- PyObject *const *
- _PyStack_UnpackDict(PyThreadState *tstate,
- PyObject *const *args, Py_ssize_t nargs,
- PyObject *kwargs, PyObject **p_kwnames)
- {
- assert(nargs >= 0);
- assert(kwargs != NULL);
- assert(PyDict_Check(kwargs));
- Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
- /* Check for overflow in the PyMem_Malloc() call below. The subtraction
- * in this check cannot overflow: both maxnargs and nkwargs are
- * non-negative signed integers, so their difference fits in the type. */
- Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
- if (nargs > maxnargs - nkwargs) {
- _PyErr_NoMemory(tstate);
- return NULL;
- }
- /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
- PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
- if (stack == NULL) {
- _PyErr_NoMemory(tstate);
- return NULL;
- }
- PyObject *kwnames = PyTuple_New(nkwargs);
- if (kwnames == NULL) {
- PyMem_Free(stack);
- return NULL;
- }
- stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
- /* Copy positional arguments */
- for (Py_ssize_t i = 0; i < nargs; i++) {
- stack[i] = Py_NewRef(args[i]);
- }
- PyObject **kwstack = stack + nargs;
- /* This loop doesn't support lookup function mutating the dictionary
- to change its size. It's a deliberate choice for speed, this function is
- called in the performance critical hot code. */
- Py_ssize_t pos = 0, i = 0;
- PyObject *key, *value;
- unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
- while (PyDict_Next(kwargs, &pos, &key, &value)) {
- keys_are_strings &= Py_TYPE(key)->tp_flags;
- PyTuple_SET_ITEM(kwnames, i, Py_NewRef(key));
- kwstack[i] = Py_NewRef(value);
- i++;
- }
- /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that
- * flag is set for all keys. Otherwise, keys_are_strings equals 0.
- * We do this check once at the end instead of inside the loop above
- * because it simplifies the deallocation in the failing case.
- * It happens to also make the loop above slightly more efficient. */
- if (!keys_are_strings) {
- _PyErr_SetString(tstate, PyExc_TypeError,
- "keywords must be strings");
- _PyStack_UnpackDict_Free(stack, nargs, kwnames);
- return NULL;
- }
- *p_kwnames = kwnames;
- return stack;
- }
- void
- _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
- PyObject *kwnames)
- {
- Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
- for (Py_ssize_t i = 0; i < n; i++) {
- Py_DECREF(stack[i]);
- }
- _PyStack_UnpackDict_FreeNoDecRef(stack, kwnames);
- }
- void
- _PyStack_UnpackDict_FreeNoDecRef(PyObject *const *stack, PyObject *kwnames)
- {
- PyMem_Free((PyObject **)stack - 1);
- Py_DECREF(kwnames);
- }
- // Export for the stable ABI
- #undef PyVectorcall_NARGS
- Py_ssize_t
- PyVectorcall_NARGS(size_t n)
- {
- return _PyVectorcall_NARGS(n);
- }
|