2017-02-12 14:27:05 -04:00
|
|
|
#include "Python.h"
|
2021-10-11 19:42:23 -03:00
|
|
|
#include "pycore_call.h" // _PyObject_CallNoArgsTstate()
|
2022-06-15 09:24:12 -03:00
|
|
|
#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
|
|
|
|
#include "pycore_object.h" // _PyCFunctionWithKeywords_TrampolineCall()
|
2020-06-22 12:27:35 -03:00
|
|
|
#include "pycore_pyerrors.h" // _PyErr_Occurred()
|
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
|
|
|
#include "pycore_tuple.h" // _PyTuple_ITEMS()
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
static PyObject *const *
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyStack_UnpackDict(PyThreadState *tstate,
|
|
|
|
PyObject *const *args, Py_ssize_t nargs,
|
|
|
|
PyObject *kwargs, PyObject **p_kwnames);
|
2019-07-02 06:49:40 -03:00
|
|
|
|
|
|
|
static void
|
|
|
|
_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
|
|
|
|
PyObject *kwnames);
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
static PyObject *
|
2019-11-14 08:36:21 -04:00
|
|
|
null_error(PyThreadState *tstate)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
if (!_PyErr_Occurred(tstate)) {
|
|
|
|
_PyErr_SetString(tstate, PyExc_SystemError,
|
|
|
|
"null argument to internal routine");
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject*
|
2019-11-04 20:22:12 -04:00
|
|
|
_Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
|
|
|
|
PyObject *result, const char *where)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
assert((callable != NULL) ^ (where != NULL));
|
|
|
|
|
|
|
|
if (result == NULL) {
|
2019-11-07 07:42:07 -04:00
|
|
|
if (!_PyErr_Occurred(tstate)) {
|
2017-02-12 14:27:05 -04:00
|
|
|
if (callable)
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_Format(tstate, PyExc_SystemError,
|
2021-01-27 12:39:16 -04:00
|
|
|
"%R returned NULL without setting an exception",
|
2019-11-04 20:22:12 -04:00
|
|
|
callable);
|
2017-02-12 14:27:05 -04:00
|
|
|
else
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_Format(tstate, PyExc_SystemError,
|
2021-01-27 12:39:16 -04:00
|
|
|
"%s returned NULL without setting an exception",
|
2019-11-04 20:22:12 -04:00
|
|
|
where);
|
2017-02-12 14:27:05 -04:00
|
|
|
#ifdef Py_DEBUG
|
2020-03-25 15:27:36 -03:00
|
|
|
/* Ensure that the bug is caught in debug mode.
|
|
|
|
Py_FatalError() logs the SystemError exception raised above. */
|
2021-01-27 12:39:16 -04:00
|
|
|
Py_FatalError("a function returned NULL without setting an exception");
|
2017-02-12 14:27:05 -04:00
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-11-07 07:42:07 -04:00
|
|
|
if (_PyErr_Occurred(tstate)) {
|
2017-02-12 14:27:05 -04:00
|
|
|
Py_DECREF(result);
|
|
|
|
|
|
|
|
if (callable) {
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_FormatFromCauseTstate(
|
|
|
|
tstate, PyExc_SystemError,
|
2021-01-27 12:39:16 -04:00
|
|
|
"%R returned a result with an exception set", callable);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_FormatFromCauseTstate(
|
|
|
|
tstate, PyExc_SystemError,
|
2021-01-27 12:39:16 -04:00
|
|
|
"%s returned a result with an exception set", where);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
#ifdef Py_DEBUG
|
2020-03-25 15:27:36 -03:00
|
|
|
/* Ensure that the bug is caught in debug mode.
|
|
|
|
Py_FatalError() logs the SystemError exception raised above. */
|
2021-01-27 12:39:16 -04:00
|
|
|
Py_FatalError("a function returned a result with an exception set");
|
2017-02-12 14:27:05 -04:00
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-27 12:39:16 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
/* --- Core PyObject call functions ------------------------------- */
|
|
|
|
|
2019-06-17 09:27:23 -03:00
|
|
|
/* Call a callable Python object without any arguments */
|
|
|
|
PyObject *
|
|
|
|
PyObject_CallNoArgs(PyObject *func)
|
|
|
|
{
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
|
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
|
2019-06-17 09:27:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyObject_FastCallDictTstate(PyThreadState *tstate, PyObject *callable,
|
|
|
|
PyObject *const *args, size_t nargsf,
|
|
|
|
PyObject *kwargs)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
2019-11-04 20:22:12 -04:00
|
|
|
assert(callable != NULL);
|
|
|
|
|
2020-02-11 12:46:57 -04:00
|
|
|
/* PyObject_VectorcallDict() must not be called with an exception set,
|
2017-02-12 14:27:05 -04:00
|
|
|
because it can clear it (directly or indirectly) and so the
|
|
|
|
caller loses its exception */
|
2019-11-04 20:22:12 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
2019-05-29 15:31:52 -03:00
|
|
|
|
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
2017-02-12 14:27:05 -04:00
|
|
|
assert(nargs >= 0);
|
|
|
|
assert(nargs == 0 || args != NULL);
|
|
|
|
assert(kwargs == NULL || PyDict_Check(kwargs));
|
|
|
|
|
2021-10-14 16:53:04 -03:00
|
|
|
vectorcallfunc func = _PyVectorcall_Function(callable);
|
2019-05-29 15:31:52 -03:00
|
|
|
if (func == NULL) {
|
|
|
|
/* Use tp_call instead */
|
2019-11-08 05:05:17 -04:00
|
|
|
return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
2019-05-29 15:31:52 -03:00
|
|
|
|
|
|
|
PyObject *res;
|
2019-07-02 06:49:40 -03:00
|
|
|
if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
|
2019-05-29 15:31:52 -03:00
|
|
|
res = func(callable, args, nargsf, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-05-29 15:31:52 -03:00
|
|
|
PyObject *kwnames;
|
|
|
|
PyObject *const *newargs;
|
2019-11-04 20:22:12 -04:00
|
|
|
newargs = _PyStack_UnpackDict(tstate,
|
|
|
|
args, nargs,
|
|
|
|
kwargs, &kwnames);
|
2019-07-02 06:49:40 -03:00
|
|
|
if (newargs == NULL) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-02 06:49:40 -03:00
|
|
|
res = func(callable, newargs,
|
|
|
|
nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
|
|
|
|
_PyStack_UnpackDict_Free(newargs, nargs, kwnames);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
2019-11-04 20:22:12 -04:00
|
|
|
return _Py_CheckFunctionResult(tstate, callable, res, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *
|
2020-02-11 12:46:57 -04:00
|
|
|
PyObject_VectorcallDict(PyObject *callable, PyObject *const *args,
|
2019-11-14 08:36:21 -04:00
|
|
|
size_t nargsf, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
return _PyObject_FastCallDictTstate(tstate, callable, args, nargsf, kwargs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
2019-11-08 05:05:17 -04:00
|
|
|
_PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
|
|
|
|
PyObject *const *args, Py_ssize_t nargs,
|
|
|
|
PyObject *keywords)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
assert(nargs >= 0);
|
|
|
|
assert(nargs == 0 || args != NULL);
|
|
|
|
assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
/* 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) {
|
2019-11-04 19:51:22 -04:00
|
|
|
_PyErr_Format(tstate, PyExc_TypeError,
|
|
|
|
"'%.200s' object is not callable",
|
|
|
|
Py_TYPE(callable)->tp_name);
|
2019-05-29 15:31:52 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
PyObject *argstuple = _PyTuple_FromArray(args, nargs);
|
|
|
|
if (argstuple == NULL) {
|
|
|
|
return NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
2019-05-29 15:31:52 -03:00
|
|
|
|
|
|
|
PyObject *kwdict;
|
|
|
|
if (keywords == NULL || PyDict_Check(keywords)) {
|
|
|
|
kwdict = keywords;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-05-29 15:31:52 -03:00
|
|
|
if (PyTuple_GET_SIZE(keywords)) {
|
|
|
|
assert(args != NULL);
|
|
|
|
kwdict = _PyStack_AsDict(args + nargs, keywords);
|
2017-02-12 14:27:05 -04:00
|
|
|
if (kwdict == NULL) {
|
|
|
|
Py_DECREF(argstuple);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-05-29 15:31:52 -03:00
|
|
|
keywords = kwdict = NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
2019-05-29 15:31:52 -03:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
PyObject *result = NULL;
|
2022-05-04 08:30:23 -03:00
|
|
|
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object") == 0)
|
2019-05-29 15:31:52 -03:00
|
|
|
{
|
2022-03-30 16:28:33 -03:00
|
|
|
result = _PyCFunctionWithKeywords_TrampolineCall(
|
|
|
|
(PyCFunctionWithKeywords)call, callable, argstuple, kwdict);
|
2022-05-04 08:30:23 -03:00
|
|
|
_Py_LeaveRecursiveCallTstate(tstate);
|
2019-05-29 15:31:52 -03:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
Py_DECREF(argstuple);
|
|
|
|
if (kwdict != keywords) {
|
|
|
|
Py_DECREF(kwdict);
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
return _Py_CheckFunctionResult(tstate, callable, result, NULL);
|
2019-05-29 15:31:52 -03:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
|
2021-10-14 16:53:04 -03:00
|
|
|
vectorcallfunc
|
|
|
|
PyVectorcall_Function(PyObject *callable)
|
|
|
|
{
|
|
|
|
return _PyVectorcall_FunctionInline(callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-11 19:18:26 -03:00
|
|
|
static PyObject *
|
|
|
|
_PyVectorcall_Call(PyThreadState *tstate, vectorcallfunc func,
|
|
|
|
PyObject *callable, PyObject *tuple, PyObject *kwargs)
|
2019-05-29 15:31:52 -03:00
|
|
|
{
|
2021-10-11 19:18:26 -03:00
|
|
|
assert(func != NULL);
|
2019-06-02 18:52:20 -03:00
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-02 18:52:20 -03:00
|
|
|
/* Convert arguments & call */
|
2019-05-29 15:31:52 -03:00
|
|
|
PyObject *const *args;
|
|
|
|
PyObject *kwnames;
|
2019-11-04 20:22:12 -04:00
|
|
|
args = _PyStack_UnpackDict(tstate,
|
|
|
|
_PyTuple_ITEMS(tuple), nargs,
|
|
|
|
kwargs, &kwnames);
|
2019-07-02 06:49:40 -03:00
|
|
|
if (args == NULL) {
|
2019-05-29 15:31:52 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-07-02 06:49:40 -03:00
|
|
|
PyObject *result = func(callable, args,
|
|
|
|
nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
|
|
|
|
_PyStack_UnpackDict_Free(args, nargs, kwnames);
|
2019-11-14 08:36:21 -04:00
|
|
|
|
2019-11-04 20:22:12 -04:00
|
|
|
return _Py_CheckFunctionResult(tstate, callable, result, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-11 19:18:26 -03:00
|
|
|
PyObject *
|
|
|
|
PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
|
2021-10-14 16:53:04 -03:00
|
|
|
/* get vectorcallfunc as in _PyVectorcall_Function, but without
|
2021-10-11 19:18:26 -03:00
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-14 16:53:04 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyObject_Call(PyThreadState *tstate, PyObject *callable,
|
|
|
|
PyObject *args, PyObject *kwargs)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
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 */
|
2019-11-04 19:51:22 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
2017-02-12 14:27:05 -04:00
|
|
|
assert(PyTuple_Check(args));
|
|
|
|
assert(kwargs == NULL || PyDict_Check(kwargs));
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
|
2021-10-14 16:53:04 -03:00
|
|
|
vectorcallfunc vector_func = _PyVectorcall_Function(callable);
|
2021-10-11 19:18:26 -03:00
|
|
|
if (vector_func != NULL) {
|
|
|
|
return _PyVectorcall_Call(tstate, vector_func, callable, args, kwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-02-06 22:04:21 -04:00
|
|
|
call = Py_TYPE(callable)->tp_call;
|
2017-02-12 14:27:05 -04:00
|
|
|
if (call == NULL) {
|
2019-11-04 19:51:22 -04:00
|
|
|
_PyErr_Format(tstate, PyExc_TypeError,
|
|
|
|
"'%.200s' object is not callable",
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(callable)->tp_name);
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-04 08:30:23 -03:00
|
|
|
if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
2019-11-04 19:51:22 -04:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
result = (*call)(callable, args, kwargs);
|
|
|
|
|
2022-05-04 08:30:23 -03:00
|
|
|
_Py_LeaveRecursiveCallTstate(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-04 20:22:12 -04:00
|
|
|
return _Py_CheckFunctionResult(tstate, callable, result, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *
|
|
|
|
PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
return _PyObject_Call(tstate, callable, args, kwargs);
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-09-11 08:01:01 -03:00
|
|
|
PyObject *
|
|
|
|
PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
return _PyObject_Call(tstate, callable, args, kwargs);
|
2019-09-11 08:01:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-14 16:53:04 -03:00
|
|
|
PyObject *
|
|
|
|
PyObject_CallOneArg(PyObject *func, PyObject *arg)
|
|
|
|
{
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
|
2021-10-14 16:53:04 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
/* --- PyFunction call functions ---------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
2019-05-30 10:11:22 -03:00
|
|
|
_PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
|
|
|
|
size_t nargsf, PyObject *kwnames)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
assert(PyFunction_Check(func));
|
2021-11-23 05:53:24 -04:00
|
|
|
PyFunctionObject *f = (PyFunctionObject *)func;
|
2019-05-29 15:31:52 -03:00
|
|
|
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
|
2017-02-12 14:27:05 -04:00
|
|
|
assert(nargs >= 0);
|
2019-11-15 20:03:22 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2021-02-01 06:42:03 -04:00
|
|
|
assert(nargs == 0 || stack != NULL);
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC(EVAL_CALL_FUNCTION_VECTORCALL);
|
2021-11-23 05:53:24 -04:00
|
|
|
if (((PyCodeObject *)f->func_code)->co_flags & CO_OPTIMIZED) {
|
2021-02-01 06:42:03 -04:00
|
|
|
return _PyEval_Vector(tstate, f, NULL, stack, nargs, kwnames);
|
|
|
|
}
|
|
|
|
else {
|
2021-11-23 05:53:24 -04:00
|
|
|
return _PyEval_Vector(tstate, f, f->func_globals, stack, nargs, kwnames);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --- 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)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
#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. */
|
2019-11-14 08:36:21 -04:00
|
|
|
assert(!_PyErr_Occurred(tstate));
|
2017-02-12 14:27:05 -04:00
|
|
|
#endif
|
|
|
|
|
2017-03-01 07:41:03 -04:00
|
|
|
if (args != NULL && !PyTuple_Check(args)) {
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyErr_SetString(tstate, PyExc_TypeError,
|
|
|
|
"argument list must be a tuple");
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwargs != NULL && !PyDict_Check(kwargs)) {
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyErr_SetString(tstate, PyExc_TypeError,
|
|
|
|
"keyword list must be a dictionary");
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-01 07:41:03 -04:00
|
|
|
if (args == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return _PyObject_FastCallDictTstate(tstate, callable, NULL, 0, kwargs);
|
2017-03-01 07:41:03 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-11-14 08:36:21 -04:00
|
|
|
return _PyObject_Call(tstate, callable, args, kwargs);
|
2017-03-01 07:41:03 -04:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_CallObject(PyObject *callable, PyObject *args)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
assert(!_PyErr_Occurred(tstate));
|
2019-07-11 12:57:32 -03:00
|
|
|
if (args == NULL) {
|
2021-10-11 19:42:23 -03:00
|
|
|
return _PyObject_CallNoArgsTstate(tstate, callable);
|
2019-07-11 12:57:32 -03:00
|
|
|
}
|
|
|
|
if (!PyTuple_Check(args)) {
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyErr_SetString(tstate, PyExc_TypeError,
|
|
|
|
"argument list must be a tuple");
|
2019-07-11 12:57:32 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-14 08:36:21 -04:00
|
|
|
return _PyObject_Call(tstate, callable, args, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Call callable(obj, *args, **kwargs). */
|
|
|
|
PyObject *
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable,
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *obj, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
assert(PyTuple_Check(args));
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
|
|
|
|
PyObject **stack;
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
Py_ssize_t argcount = PyTuple_GET_SIZE(args);
|
2017-02-12 14:27:05 -04:00
|
|
|
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],
|
2018-11-09 11:56:48 -04:00
|
|
|
_PyTuple_ITEMS(args),
|
|
|
|
argcount * sizeof(PyObject *));
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *result = _PyObject_FastCallDictTstate(tstate, callable,
|
|
|
|
stack, argcount + 1,
|
|
|
|
kwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
if (stack != small_stack) {
|
|
|
|
PyMem_Free(stack);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- Call with a format string ---------------------------------- */
|
|
|
|
|
|
|
|
static PyObject *
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyObject_CallFunctionVa(PyThreadState *tstate, PyObject *callable,
|
|
|
|
const char *format, va_list va, int is_size_t)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
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) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!format || !*format) {
|
2021-10-11 19:42:23 -03:00
|
|
|
return _PyObject_CallNoArgsTstate(tstate, callable);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
|
2017-02-12 14:27:05 -04:00
|
|
|
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];
|
2019-11-14 08:36:21 -04:00
|
|
|
result = _PyObject_VectorcallTstate(tstate, callable,
|
|
|
|
_PyTuple_ITEMS(args),
|
|
|
|
PyTuple_GET_SIZE(args),
|
|
|
|
NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-11-14 08:36:21 -04:00
|
|
|
result = _PyObject_VectorcallTstate(tstate, callable,
|
|
|
|
stack, nargs, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
|
|
|
|
* This function is kept for backward compatibility.
|
|
|
|
*/
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
|
|
|
PyEval_CallFunction(PyObject *callable, const char *format, ...)
|
|
|
|
{
|
2017-03-14 06:00:59 -03:00
|
|
|
va_list va;
|
|
|
|
PyObject *result;
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
|
2017-03-14 06:00:59 -03:00
|
|
|
va_end(va);
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
return result;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
va_list va;
|
2017-02-12 14:27:05 -04:00
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va, 1);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
2019-11-14 08:36:21 -04:00
|
|
|
callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va, int is_size_t)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
assert(callable != NULL);
|
|
|
|
if (!PyCallable_Check(callable)) {
|
2019-11-14 08:36:21 -04:00
|
|
|
_PyErr_Format(tstate, PyExc_TypeError,
|
|
|
|
"attribute of type '%.200s' is not callable",
|
|
|
|
Py_TYPE(callable)->tp_name);
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
if (obj == NULL || name == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *callable = PyObject_GetAttrString(obj, name);
|
|
|
|
if (callable == NULL) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
2019-11-14 08:36:21 -04:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
va_list va;
|
2017-02-12 14:27:05 -04:00
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *retval = callmethod(tstate, callable, format, va, 0);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
|
|
|
|
* This function is kept for backward compatibility.
|
|
|
|
*/
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
|
|
|
PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-03-14 06:00:59 -03:00
|
|
|
if (obj == NULL || name == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-03-14 06:00:59 -03:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *callable = PyObject_GetAttrString(obj, name);
|
|
|
|
if (callable == NULL) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
2019-11-14 08:36:21 -04:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
va_list va;
|
2017-03-14 06:00:59 -03:00
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *retval = callmethod(tstate, callable, format, va, 0);
|
2017-03-14 06:00:59 -03:00
|
|
|
va_end(va);
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
|
|
|
_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
if (obj == NULL || name == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *callable = _PyObject_GetAttrId(obj, name);
|
|
|
|
if (callable == NULL) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
2019-11-14 08:36:21 -04:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
va_list va;
|
2017-02-12 14:27:05 -04:00
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *retval = callmethod(tstate, callable, format, va, 0);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
|
|
|
_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
if (obj == NULL || name == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *callable = PyObject_GetAttrString(obj, name);
|
|
|
|
if (callable == NULL) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
2019-11-14 08:36:21 -04:00
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
va_list va;
|
2017-02-12 14:27:05 -04:00
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *retval = callmethod(tstate, callable, format, va, 1);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
if (obj == NULL || name == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *callable = _PyObject_GetAttrId(obj, name);
|
2017-02-12 14:27:05 -04:00
|
|
|
if (callable == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:36:21 -04:00
|
|
|
va_list va;
|
2017-02-12 14:27:05 -04:00
|
|
|
va_start(va, format);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *retval = callmethod(tstate, callable, format, va, 1);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- Call with "..." arguments ---------------------------------- */
|
|
|
|
|
|
|
|
static PyObject *
|
2019-11-14 08:36:21 -04:00
|
|
|
object_vacall(PyThreadState *tstate, PyObject *base,
|
|
|
|
PyObject *callable, va_list vargs)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
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) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Count the number of arguments */
|
|
|
|
va_copy(countva, vargs);
|
2019-05-26 04:23:34 -03:00
|
|
|
nargs = base ? 1 : 0;
|
2017-02-12 14:27:05 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 04:23:34 -03:00
|
|
|
i = 0;
|
|
|
|
if (base) {
|
|
|
|
stack[i++] = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < nargs; ++i) {
|
2017-02-12 14:27:05 -04:00
|
|
|
stack[i] = va_arg(vargs, PyObject *);
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:31:41 -03:00
|
|
|
#ifdef Py_STATS
|
|
|
|
if (PyFunction_Check(callable)) {
|
|
|
|
EVAL_CALL_STAT_INC(EVAL_CALL_API);
|
|
|
|
}
|
|
|
|
#endif
|
2017-02-12 14:27:05 -04:00
|
|
|
/* Call the function */
|
2019-11-14 08:36:21 -04:00
|
|
|
result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
if (stack != small_stack) {
|
|
|
|
PyMem_Free(stack);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-28 06:49:00 -03:00
|
|
|
PyObject *
|
2020-02-11 12:46:57 -04:00
|
|
|
PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
|
2019-06-28 06:49:00 -03:00
|
|
|
size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
|
|
|
assert(name != NULL);
|
|
|
|
assert(args != NULL);
|
|
|
|
assert(PyVectorcall_NARGS(nargsf) >= 1);
|
|
|
|
|
2019-11-08 05:05:17 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2019-06-28 06:49:00 -03:00
|
|
|
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--;
|
|
|
|
}
|
2022-05-27 12:31:41 -03:00
|
|
|
EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_METHOD, callable);
|
2019-11-08 05:05:17 -04:00
|
|
|
PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
|
|
|
|
args, nargsf, kwnames);
|
2019-06-28 06:49:00 -03:00
|
|
|
Py_DECREF(callable);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
2019-05-26 04:23:34 -03:00
|
|
|
PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2019-05-26 04:23:34 -03:00
|
|
|
if (obj == NULL || name == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-05-26 04:23:34 -03:00
|
|
|
PyObject *callable = NULL;
|
|
|
|
int is_method = _PyObject_GetMethod(obj, name, &callable);
|
2017-02-12 14:27:05 -04:00
|
|
|
if (callable == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-26 04:23:34 -03:00
|
|
|
obj = is_method ? obj : NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-05-26 04:23:34 -03:00
|
|
|
va_list vargs;
|
2017-02-12 14:27:05 -04:00
|
|
|
va_start(vargs, name);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *result = object_vacall(tstate, obj, callable, vargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
2022-02-24 12:51:59 -04:00
|
|
|
_PyObject_CallMethodIdObjArgs(PyObject *obj, _Py_Identifier *name, ...)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
if (obj == NULL || name == NULL) {
|
2019-11-14 08:36:21 -04:00
|
|
|
return null_error(tstate);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-05-26 04:23:34 -03:00
|
|
|
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
|
|
|
|
if (!oname) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *callable = NULL;
|
|
|
|
int is_method = _PyObject_GetMethod(obj, oname, &callable);
|
2017-02-12 14:27:05 -04:00
|
|
|
if (callable == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-26 04:23:34 -03:00
|
|
|
obj = is_method ? obj : NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-05-26 04:23:34 -03:00
|
|
|
va_list vargs;
|
2017-02-12 14:27:05 -04:00
|
|
|
va_start(vargs, name);
|
2019-11-14 08:36:21 -04:00
|
|
|
PyObject *result = object_vacall(tstate, obj, callable, vargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_CallFunctionObjArgs(PyObject *callable, ...)
|
|
|
|
{
|
2019-11-14 08:36:21 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
va_list vargs;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
va_start(vargs, callable);
|
2019-11-14 08:36:21 -04:00
|
|
|
result = object_vacall(tstate, NULL, callable, vargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- PyStack functions ------------------------------------------ */
|
|
|
|
|
|
|
|
PyObject *
|
2017-12-15 07:11:11 -04:00
|
|
|
_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
Py_ssize_t nkwargs;
|
|
|
|
|
|
|
|
assert(kwnames != NULL);
|
|
|
|
nkwargs = PyTuple_GET_SIZE(kwnames);
|
2022-03-01 19:09:28 -04:00
|
|
|
return _PyDict_FromItems(&PyTuple_GET_ITEM(kwnames, 0), 1,
|
|
|
|
values, 1, nkwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
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.
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-08-16 07:41:27 -03:00
|
|
|
This also checks that all keyword names are strings. If not, a TypeError is
|
|
|
|
raised.
|
2019-07-02 06:49:40 -03:00
|
|
|
|
2019-08-16 07:41:27 -03:00
|
|
|
The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-08-16 07:41:27 -03:00
|
|
|
When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
|
2019-07-02 06:49:40 -03:00
|
|
|
static PyObject *const *
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyStack_UnpackDict(PyThreadState *tstate,
|
|
|
|
PyObject *const *args, Py_ssize_t nargs,
|
|
|
|
PyObject *kwargs, PyObject **p_kwnames)
|
2019-07-02 06:49:40 -03:00
|
|
|
{
|
|
|
|
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) {
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_NoMemory(tstate);
|
2019-07-02 06:49:40 -03:00
|
|
|
return NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
/* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
|
|
|
|
PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
|
2017-02-12 14:27:05 -04:00
|
|
|
if (stack == NULL) {
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_NoMemory(tstate);
|
2019-07-02 06:49:40 -03:00
|
|
|
return NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
PyObject *kwnames = PyTuple_New(nkwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
if (kwnames == NULL) {
|
|
|
|
PyMem_Free(stack);
|
2019-07-02 06:49:40 -03:00
|
|
|
return NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
|
|
|
|
|
2019-05-22 08:09:35 -03:00
|
|
|
/* Copy positional arguments */
|
2019-07-02 06:49:40 -03:00
|
|
|
for (Py_ssize_t i = 0; i < nargs; i++) {
|
2019-05-22 08:09:35 -03:00
|
|
|
Py_INCREF(args[i]);
|
|
|
|
stack[i] = args[i];
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
PyObject **kwstack = stack + nargs;
|
2017-02-12 14:27:05 -04:00
|
|
|
/* 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. */
|
2019-07-02 06:49:40 -03:00
|
|
|
Py_ssize_t pos = 0, i = 0;
|
|
|
|
PyObject *key, *value;
|
2019-08-16 07:41:27 -03:00
|
|
|
unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
|
2017-02-12 14:27:05 -04:00
|
|
|
while (PyDict_Next(kwargs, &pos, &key, &value)) {
|
2019-08-16 07:41:27 -03:00
|
|
|
keys_are_strings &= Py_TYPE(key)->tp_flags;
|
2017-02-12 14:27:05 -04:00
|
|
|
Py_INCREF(key);
|
2019-05-22 08:09:35 -03:00
|
|
|
Py_INCREF(value);
|
2017-02-12 14:27:05 -04:00
|
|
|
PyTuple_SET_ITEM(kwnames, i, key);
|
|
|
|
kwstack[i] = value;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2019-08-16 07:41:27 -03:00
|
|
|
/* 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) {
|
2019-11-04 20:22:12 -04:00
|
|
|
_PyErr_SetString(tstate, PyExc_TypeError,
|
|
|
|
"keywords must be strings");
|
2019-08-16 07:41:27 -03:00
|
|
|
_PyStack_UnpackDict_Free(stack, nargs, kwnames);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
*p_kwnames = kwnames;
|
2019-07-02 06:49:40 -03:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
static 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]);
|
|
|
|
}
|
|
|
|
PyMem_Free((PyObject **)stack - 1);
|
|
|
|
Py_DECREF(kwnames);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|