Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
PyObject **newargs;
Py_ssize_t totalargs = nargs + nkwargs;
- newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
- if (newargs == NULL) {
- PyErr_NoMemory();
- return NULL;
+ PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
+ if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
+ newargs = newargs_stack;
+ }
+ else {
+ newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
+ if (newargs == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
}
/* use borrowed references */
newargs[0] = self;
memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
}
result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
- PyMem_Free(newargs);
+ if (newargs != newargs_stack) {
+ PyMem_Free(newargs);
+ }
}
return result;
}