]> granicus.if.org Git - python/commitdiff
[3.8] bpo-37233: use _PY_FASTCALL_SMALL_STACK in method_vectorcall (GH-13974)
authorInada Naoki <songofacandy@gmail.com>
Wed, 3 Jul 2019 12:46:07 +0000 (21:46 +0900)
committerGitHub <noreply@github.com>
Wed, 3 Jul 2019 12:46:07 +0000 (21:46 +0900)
(cherry picked from commit 988e6aa322fb61651812fa5a61ec73316c71b041)

Co-authored-by: Jeroen Demeyer <J.Demeyer@UGent.be>
Objects/classobject.c

index 2415ed14cb1506c406e4bd2474a6b82c57196230..efdb18ef86daaf721032ea2c00ac4a674095aff5 100644 (file)
@@ -64,10 +64,16 @@ method_vectorcall(PyObject *method, PyObject *const *args,
         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;
@@ -77,7 +83,9 @@ method_vectorcall(PyObject *method, PyObject *const *args,
             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;
 }