Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit c7570d4

Browse files
methanejdemeyer
andauthored
[3.8] bpo-37233: use _PY_FASTCALL_SMALL_STACK in method_vectorcall (pythonGH-13974)
(cherry picked from commit 988e6aa) Co-authored-by: Jeroen Demeyer <[email protected]>
1 parent 0aefba7 commit c7570d4

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Objects/classobject.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,16 @@ method_vectorcall(PyObject *method, PyObject *const *args,
6464
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
6565
PyObject **newargs;
6666
Py_ssize_t totalargs = nargs + nkwargs;
67-
newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
68-
if (newargs == NULL) {
69-
PyErr_NoMemory();
70-
return NULL;
67+
PyObject *newargs_stack[_PY_FASTCALL_SMALL_STACK];
68+
if (totalargs <= (Py_ssize_t)Py_ARRAY_LENGTH(newargs_stack) - 1) {
69+
newargs = newargs_stack;
70+
}
71+
else {
72+
newargs = PyMem_Malloc((totalargs+1) * sizeof(PyObject *));
73+
if (newargs == NULL) {
74+
PyErr_NoMemory();
75+
return NULL;
76+
}
7177
}
7278
/* use borrowed references */
7379
newargs[0] = self;
@@ -77,7 +83,9 @@ method_vectorcall(PyObject *method, PyObject *const *args,
7783
memcpy(newargs + 1, args, totalargs * sizeof(PyObject *));
7884
}
7985
result = _PyObject_Vectorcall(func, newargs, nargs+1, kwnames);
80-
PyMem_Free(newargs);
86+
if (newargs != newargs_stack) {
87+
PyMem_Free(newargs);
88+
}
8189
}
8290
return result;
8391
}

0 commit comments

Comments
 (0)