2017-02-12 14:27:05 -04:00
|
|
|
#include "Python.h"
|
2018-11-21 17:27:47 -04:00
|
|
|
#include "pycore_object.h"
|
2018-11-12 11:53:38 -04:00
|
|
|
#include "pycore_pystate.h"
|
2018-11-25 18:56:17 -04:00
|
|
|
#include "pycore_tupleobject.h"
|
2017-02-12 14:27:05 -04:00
|
|
|
#include "frameobject.h"
|
|
|
|
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
static PyObject *
|
|
|
|
cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs);
|
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
static PyObject *const *
|
|
|
|
_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
|
|
|
|
PyObject **p_kwnames);
|
|
|
|
|
|
|
|
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 *
|
|
|
|
null_error(void)
|
|
|
|
{
|
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"null argument to internal routine");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject*
|
|
|
|
_Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
|
|
|
|
{
|
|
|
|
int err_occurred = (PyErr_Occurred() != NULL);
|
|
|
|
|
|
|
|
assert((callable != NULL) ^ (where != NULL));
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
if (!err_occurred) {
|
|
|
|
if (callable)
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"%R returned NULL without setting an error",
|
|
|
|
callable);
|
|
|
|
else
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"%s returned NULL without setting an error",
|
|
|
|
where);
|
|
|
|
#ifdef Py_DEBUG
|
|
|
|
/* Ensure that the bug is caught in debug mode */
|
|
|
|
Py_FatalError("a function returned NULL without setting an error");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (err_occurred) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
|
|
|
|
if (callable) {
|
|
|
|
_PyErr_FormatFromCause(PyExc_SystemError,
|
|
|
|
"%R returned a result with an error set",
|
|
|
|
callable);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_PyErr_FormatFromCause(PyExc_SystemError,
|
|
|
|
"%s returned a result with an error set",
|
|
|
|
where);
|
|
|
|
}
|
|
|
|
#ifdef Py_DEBUG
|
|
|
|
/* Ensure that the bug is caught in debug mode */
|
|
|
|
Py_FatalError("a function returned a result with an error set");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- 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)
|
|
|
|
{
|
|
|
|
return _PyObject_CallNoArg(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *
|
2019-05-29 15:31:52 -03:00
|
|
|
_PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
|
|
|
|
size_t nargsf, PyObject *kwargs)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
|
|
|
/* _PyObject_FastCallDict() 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());
|
|
|
|
assert(callable != NULL);
|
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));
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
vectorcallfunc func = _PyVectorcall_Function(callable);
|
|
|
|
if (func == NULL) {
|
|
|
|
/* Use tp_call instead */
|
|
|
|
return _PyObject_MakeTpCall(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-07-02 06:49:40 -03:00
|
|
|
newargs = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames);
|
|
|
|
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-05-29 15:31:52 -03:00
|
|
|
return _Py_CheckFunctionResult(callable, res, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
2019-05-29 15:31:52 -03:00
|
|
|
_PyObject_MakeTpCall(PyObject *callable, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
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) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
|
|
|
|
Py_TYPE(callable)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
assert(nargs >= 0);
|
2019-05-29 15:31:52 -03:00
|
|
|
assert(nargs == 0 || args != NULL);
|
|
|
|
assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
|
|
|
|
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;
|
|
|
|
if (Py_EnterRecursiveCall(" while calling a Python object") == 0)
|
|
|
|
{
|
|
|
|
result = call(callable, argstuple, kwdict);
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
}
|
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-05-29 15:31:52 -03:00
|
|
|
result = _Py_CheckFunctionResult(callable, result, NULL);
|
|
|
|
return result;
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
PyObject *
|
|
|
|
PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
|
|
|
|
{
|
2019-06-02 18:52:20 -03:00
|
|
|
/* get vectorcallfunc as in _PyVectorcall_Function, but without
|
|
|
|
* the _Py_TPFLAGS_HAVE_VECTORCALL check */
|
|
|
|
Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
|
2019-06-24 07:41:05 -03:00
|
|
|
if (offset <= 0) {
|
2019-06-02 18:52:20 -03:00
|
|
|
PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
|
|
|
|
Py_TYPE(callable)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
|
2019-05-29 15:31:52 -03:00
|
|
|
if (func == NULL) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
|
|
|
|
Py_TYPE(callable)->tp_name);
|
|
|
|
return NULL;
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
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-07-02 06:49:40 -03:00
|
|
|
args = _PyStack_UnpackDict(_PyTuple_ITEMS(tuple), nargs, kwargs, &kwnames);
|
|
|
|
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-07-05 09:48:24 -03:00
|
|
|
return _Py_CheckFunctionResult(callable, result, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_Call(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());
|
|
|
|
assert(PyTuple_Check(args));
|
|
|
|
assert(kwargs == NULL || PyDict_Check(kwargs));
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
if (_PyVectorcall_Function(callable) != NULL) {
|
|
|
|
return PyVectorcall_Call(callable, args, kwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else if (PyCFunction_Check(callable)) {
|
2019-05-29 15:31:52 -03:00
|
|
|
/* This must be a METH_VARARGS function, otherwise we would be
|
|
|
|
* in the previous case */
|
|
|
|
return cfunction_call_varargs(callable, args, kwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
call = callable->ob_type->tp_call;
|
|
|
|
if (call == NULL) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
|
|
|
|
callable->ob_type->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Py_EnterRecursiveCall(" while calling a Python object"))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
result = (*call)(callable, args, kwargs);
|
|
|
|
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
|
|
|
|
return _Py_CheckFunctionResult(callable, result, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- PyFunction call functions ---------------------------------- */
|
|
|
|
|
|
|
|
static PyObject* _Py_HOT_FUNCTION
|
2017-12-15 07:11:11 -04:00
|
|
|
function_code_fastcall(PyCodeObject *co, PyObject *const *args, Py_ssize_t nargs,
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject *globals)
|
|
|
|
{
|
|
|
|
PyFrameObject *f;
|
2018-10-31 21:51:40 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
2017-02-12 14:27:05 -04:00
|
|
|
PyObject **fastlocals;
|
|
|
|
Py_ssize_t i;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
assert(globals != NULL);
|
|
|
|
/* XXX Perhaps we should create a specialized
|
|
|
|
_PyFrame_New_NoTrack() that doesn't take locals, but does
|
|
|
|
take builtins without sanity checking them.
|
|
|
|
*/
|
|
|
|
assert(tstate != NULL);
|
|
|
|
f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
|
|
|
|
if (f == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fastlocals = f->f_localsplus;
|
|
|
|
|
|
|
|
for (i = 0; i < nargs; i++) {
|
|
|
|
Py_INCREF(*args);
|
|
|
|
fastlocals[i] = *args++;
|
|
|
|
}
|
|
|
|
result = PyEval_EvalFrameEx(f,0);
|
|
|
|
|
|
|
|
if (Py_REFCNT(f) > 1) {
|
|
|
|
Py_DECREF(f);
|
|
|
|
_PyObject_GC_TRACK(f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++tstate->recursion_depth;
|
|
|
|
Py_DECREF(f);
|
|
|
|
--tstate->recursion_depth;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
|
|
|
|
PyObject *globals = PyFunction_GET_GLOBALS(func);
|
|
|
|
PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
|
|
|
|
PyObject *kwdefs, *closure, *name, *qualname;
|
|
|
|
PyObject **d;
|
|
|
|
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
|
|
|
|
Py_ssize_t nd;
|
|
|
|
|
|
|
|
assert(PyFunction_Check(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);
|
|
|
|
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
|
|
|
|
assert((nargs == 0 && nkwargs == 0) || stack != NULL);
|
|
|
|
/* kwnames must only contains str strings, no subclass, and all keys must
|
|
|
|
be unique */
|
|
|
|
|
|
|
|
if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
|
2017-10-25 09:26:17 -03:00
|
|
|
(co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
|
2017-02-12 14:27:05 -04:00
|
|
|
{
|
2019-06-01 14:08:04 -03:00
|
|
|
if (argdefs == NULL && co->co_argcount == nargs) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return function_code_fastcall(co, stack, nargs, globals);
|
|
|
|
}
|
|
|
|
else if (nargs == 0 && argdefs != NULL
|
2019-06-01 14:08:04 -03:00
|
|
|
&& co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
|
2017-02-12 14:27:05 -04:00
|
|
|
/* function called with no arguments, but all parameters have
|
|
|
|
a default value: use default values as arguments .*/
|
2018-11-09 11:56:48 -04:00
|
|
|
stack = _PyTuple_ITEMS(argdefs);
|
2017-03-21 03:53:25 -03:00
|
|
|
return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
|
|
|
|
globals);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kwdefs = PyFunction_GET_KW_DEFAULTS(func);
|
|
|
|
closure = PyFunction_GET_CLOSURE(func);
|
|
|
|
name = ((PyFunctionObject *)func) -> func_name;
|
|
|
|
qualname = ((PyFunctionObject *)func) -> func_qualname;
|
|
|
|
|
|
|
|
if (argdefs != NULL) {
|
2018-11-09 11:56:48 -04:00
|
|
|
d = _PyTuple_ITEMS(argdefs);
|
2017-03-21 03:53:25 -03:00
|
|
|
nd = PyTuple_GET_SIZE(argdefs);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
d = NULL;
|
|
|
|
nd = 0;
|
|
|
|
}
|
|
|
|
return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
|
|
|
|
stack, nargs,
|
2018-11-09 11:56:48 -04:00
|
|
|
nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
|
2017-02-12 14:27:05 -04:00
|
|
|
stack + nargs,
|
|
|
|
nkwargs, 1,
|
|
|
|
d, (int)nd, kwdefs,
|
|
|
|
closure, name, qualname);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- PyCFunction call functions --------------------------------- */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
assert(!PyErr_Occurred());
|
2017-06-06 12:45:22 -03:00
|
|
|
assert(kwargs == NULL || PyDict_Check(kwargs));
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
PyCFunction meth = PyCFunction_GET_FUNCTION(func);
|
|
|
|
PyObject *self = PyCFunction_GET_SELF(func);
|
|
|
|
PyObject *result;
|
|
|
|
|
2019-05-29 15:31:52 -03:00
|
|
|
assert(PyCFunction_GET_FLAGS(func) & METH_VARARGS);
|
2017-02-12 14:27:05 -04:00
|
|
|
if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
|
|
|
|
if (Py_EnterRecursiveCall(" while calling a Python object")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:27:31 -04:00
|
|
|
result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
}
|
|
|
|
else {
|
2017-06-06 12:45:22 -03:00
|
|
|
if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
|
2017-02-12 14:27:05 -04:00
|
|
|
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
|
|
|
|
((PyCFunctionObject*)func)->m_ml->ml_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Py_EnterRecursiveCall(" while calling a Python object")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = (*meth)(self, args);
|
|
|
|
|
|
|
|
Py_LeaveRecursiveCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _Py_CheckFunctionResult(func, result, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
2019-05-29 15:31:52 -03:00
|
|
|
/* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
|
|
|
|
* is NULL. This is intentional, since vectorcall would be slower. */
|
2017-02-12 14:27:05 -04:00
|
|
|
if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
|
|
|
|
return cfunction_call_varargs(func, args, kwargs);
|
|
|
|
}
|
2019-05-29 15:31:52 -03:00
|
|
|
return PyVectorcall_Call(func, args, kwargs);
|
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)
|
|
|
|
{
|
|
|
|
#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());
|
|
|
|
#endif
|
|
|
|
|
2017-03-01 07:41:03 -04:00
|
|
|
if (args != NULL && !PyTuple_Check(args)) {
|
2017-02-12 14:27:05 -04:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"argument list must be a tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kwargs != NULL && !PyDict_Check(kwargs)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"keyword list must be a dictionary");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-01 07:41:03 -04:00
|
|
|
if (args == NULL) {
|
|
|
|
return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return PyObject_Call(callable, args, kwargs);
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_CallObject(PyObject *callable, PyObject *args)
|
|
|
|
{
|
2019-07-11 12:57:32 -03:00
|
|
|
assert(!PyErr_Occurred());
|
|
|
|
if (args == NULL) {
|
|
|
|
return _PyObject_CallNoArg(callable);
|
|
|
|
}
|
|
|
|
if (!PyTuple_Check(args)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"argument list must be a tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyObject_Call(callable, args, NULL);
|
2017-02-12 14:27:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Call callable(obj, *args, **kwargs). */
|
|
|
|
PyObject *
|
|
|
|
_PyObject_Call_Prepend(PyObject *callable,
|
|
|
|
PyObject *obj, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
|
|
|
|
PyObject **stack;
|
|
|
|
Py_ssize_t argcount;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
assert(PyTuple_Check(args));
|
|
|
|
|
|
|
|
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],
|
2018-11-09 11:56:48 -04:00
|
|
|
_PyTuple_ITEMS(args),
|
|
|
|
argcount * sizeof(PyObject *));
|
2017-02-12 14:27:05 -04:00
|
|
|
|
|
|
|
result = _PyObject_FastCallDict(callable,
|
|
|
|
stack, argcount + 1,
|
|
|
|
kwargs);
|
|
|
|
if (stack != small_stack) {
|
|
|
|
PyMem_Free(stack);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- Call with a format string ---------------------------------- */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_PyObject_CallFunctionVa(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!format || !*format) {
|
|
|
|
return _PyObject_CallNoArg(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_FastCall(callable,
|
2018-11-09 11:56:48 -04:00
|
|
|
_PyTuple_ITEMS(args),
|
2017-02-12 14:27:05 -04:00
|
|
|
PyTuple_GET_SIZE(args));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = _PyObject_FastCall(callable, stack, nargs);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
result = _PyObject_CallFunctionVa(callable, format, va, 0);
|
|
|
|
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;
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
va_start(va, format);
|
|
|
|
result = _PyObject_CallFunctionVa(callable, format, va, 0);
|
|
|
|
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, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
result = _PyObject_CallFunctionVa(callable, format, va, 1);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
|
|
|
|
{
|
|
|
|
assert(callable != NULL);
|
|
|
|
|
|
|
|
if (!PyCallable_Check(callable)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"attribute of type '%.200s' is not callable",
|
|
|
|
Py_TYPE(callable)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
PyObject *callable, *retval;
|
|
|
|
|
|
|
|
if (obj == NULL || name == NULL) {
|
|
|
|
return null_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
callable = PyObject_GetAttrString(obj, name);
|
|
|
|
if (callable == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
retval = callmethod(callable, format, va, 0);
|
|
|
|
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, ...)
|
|
|
|
{
|
2017-03-14 06:00:59 -03:00
|
|
|
va_list va;
|
|
|
|
PyObject *callable, *retval;
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
if (obj == NULL || name == NULL) {
|
|
|
|
return null_error();
|
|
|
|
}
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
callable = PyObject_GetAttrString(obj, name);
|
|
|
|
if (callable == NULL)
|
2017-02-12 14:27:05 -04:00
|
|
|
return NULL;
|
|
|
|
|
2017-03-14 06:00:59 -03:00
|
|
|
va_start(va, format);
|
|
|
|
retval = callmethod(callable, format, va, 0);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
PyObject *callable, *retval;
|
|
|
|
|
|
|
|
if (obj == NULL || name == NULL) {
|
|
|
|
return null_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
callable = _PyObject_GetAttrId(obj, name);
|
|
|
|
if (callable == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
retval = callmethod(callable, format, va, 0);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
PyObject *callable, *retval;
|
|
|
|
|
|
|
|
if (obj == NULL || name == NULL) {
|
|
|
|
return null_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
callable = PyObject_GetAttrString(obj, name);
|
|
|
|
if (callable == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
retval = callmethod(callable, format, va, 1);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
PyObject *callable, *retval;
|
|
|
|
|
|
|
|
if (obj == NULL || name == NULL) {
|
|
|
|
return null_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
callable = _PyObject_GetAttrId(obj, name);
|
|
|
|
if (callable == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
retval = callmethod(callable, format, va, 1);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --- Call with "..." arguments ---------------------------------- */
|
|
|
|
|
|
|
|
static PyObject *
|
2019-05-26 04:23:34 -03:00
|
|
|
object_vacall(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) {
|
|
|
|
return null_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 *);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the function */
|
|
|
|
result = _PyObject_FastCall(callable, stack, nargs);
|
|
|
|
|
|
|
|
if (stack != small_stack) {
|
|
|
|
PyMem_Free(stack);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-28 06:49:00 -03:00
|
|
|
PyObject *
|
|
|
|
_PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
|
|
|
|
size_t nargsf, PyObject *kwnames)
|
|
|
|
{
|
|
|
|
assert(name != NULL);
|
|
|
|
assert(args != NULL);
|
|
|
|
assert(PyVectorcall_NARGS(nargsf) >= 1);
|
|
|
|
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
PyObject *result = _PyObject_Vectorcall(callable, args, nargsf, kwnames);
|
|
|
|
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-05-26 04:23:34 -03:00
|
|
|
if (obj == NULL || name == NULL) {
|
2017-02-12 14:27:05 -04:00
|
|
|
return null_error();
|
|
|
|
}
|
|
|
|
|
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-05-26 04:23:34 -03:00
|
|
|
PyObject *result = object_vacall(obj, callable, vargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyObject_CallMethodIdObjArgs(PyObject *obj,
|
|
|
|
struct _Py_Identifier *name, ...)
|
|
|
|
{
|
|
|
|
if (obj == NULL || name == NULL) {
|
|
|
|
return null_error();
|
|
|
|
}
|
|
|
|
|
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-05-26 04:23:34 -03:00
|
|
|
PyObject *result = object_vacall(obj, callable, vargs);
|
2017-02-12 14:27:05 -04:00
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
Py_DECREF(callable);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyObject_CallFunctionObjArgs(PyObject *callable, ...)
|
|
|
|
{
|
|
|
|
va_list vargs;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
va_start(vargs, callable);
|
2019-05-26 04:23:34 -03:00
|
|
|
result = object_vacall(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;
|
|
|
|
PyObject *kwdict;
|
|
|
|
Py_ssize_t i;
|
|
|
|
|
|
|
|
assert(kwnames != NULL);
|
|
|
|
nkwargs = PyTuple_GET_SIZE(kwnames);
|
|
|
|
kwdict = _PyDict_NewPresized(nkwargs);
|
|
|
|
if (kwdict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nkwargs; i++) {
|
|
|
|
PyObject *key = PyTuple_GET_ITEM(kwnames, i);
|
|
|
|
PyObject *value = *values++;
|
|
|
|
/* If key already exists, replace it with the new value */
|
|
|
|
if (PyDict_SetItem(kwdict, key, value)) {
|
|
|
|
Py_DECREF(kwdict);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kwdict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-07-02 06:49:40 -03:00
|
|
|
The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
|
|
|
|
|
|
|
|
When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames)
|
2017-02-12 14:27:05 -04:00
|
|
|
|
2019-07-02 06:49:40 -03:00
|
|
|
The type of keyword keys is not checked, these checks should be done
|
|
|
|
later (ex: _PyArg_ParseStackAndKeywords). */
|
|
|
|
static PyObject *const *
|
|
|
|
_PyStack_UnpackDict(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) {
|
2017-02-12 14:27:05 -04:00
|
|
|
PyErr_NoMemory();
|
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) {
|
|
|
|
PyErr_NoMemory();
|
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;
|
2017-02-12 14:27:05 -04:00
|
|
|
while (PyDict_Next(kwargs, &pos, &key, &value)) {
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*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
|
|
|
}
|