[Python-checkins] cpython: Add _PyObject_CallFunctionVa() helper

victor.stinner python-checkins at python.org
Thu Dec 8 20:16:59 EST 2016


https://hg.python.org/cpython/rev/455169e87bb3
changeset:   105533:455169e87bb3
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Dec 09 00:22:56 2016 +0100
summary:
  Add _PyObject_CallFunctionVa() helper

Issue #28915: Add _PyObject_CallFunctionVa() helper to factorize code of
functions:

* PyObject_CallFunction()
* _PyObject_CallFunction_SizeT()
* callmethod()

files:
  Objects/abstract.c |  89 ++++++++++++---------------------
  1 files changed, 32 insertions(+), 57 deletions(-)


diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2519,20 +2519,39 @@
     }
 }
 
-static PyObject*
-call_function_tail(PyObject *callable, PyObject *args)
+static PyObject *
+_PyObject_CallFunctionVa(PyObject *callable, const char *format,
+                         va_list va, int is_size_t)
 {
-    PyObject *result;
-
-    assert(args != NULL);
+    PyObject *args, *result;
+
+    if (callable == NULL) {
+        return null_error();
+    }
+
+    if (!format || !*format) {
+        return _PyObject_CallNoArg(callable);
+    }
+
+    if (is_size_t) {
+        args = Py_VaBuildValue(format, va);
+    }
+    else {
+        args = _Py_VaBuildValue_SizeT(format, va);
+    }
+    if (args == NULL) {
+        return NULL;
+    }
 
     if (!PyTuple_Check(args)) {
-        result = PyObject_CallFunctionObjArgs(callable, args, NULL);
+        PyObject *stack[1] = {args};
+        result = _PyObject_FastCall(callable, stack, 1);
     }
     else {
         result = PyObject_Call(callable, args, NULL);
     }
 
+    Py_DECREF(args);
     return result;
 }
 
@@ -2540,25 +2559,12 @@
 PyObject_CallFunction(PyObject *callable, const char *format, ...)
 {
     va_list va;
-    PyObject *args, *result;
-
-    if (callable == NULL) {
-        return null_error();
-    }
-
-    if (!format || !*format) {
-        return _PyObject_CallNoArg(callable);
-    }
+    PyObject *result;
 
     va_start(va, format);
-    args = Py_VaBuildValue(format, va);
+    result = _PyObject_CallFunctionVa(callable, format, va, 0);
     va_end(va);
-    if (args == NULL) {
-        return NULL;
-    }
-
-    result = call_function_tail(callable, args);
-    Py_DECREF(args);
+
     return result;
 }
 
@@ -2566,33 +2572,18 @@
 _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
 {
     va_list va;
-    PyObject *args, *result;
-
-    if (callable == NULL) {
-        return null_error();
-    }
-
-    if (!format || !*format) {
-        return _PyObject_CallNoArg(callable);
-    }
+    PyObject *result;
 
     va_start(va, format);
-    args = _Py_VaBuildValue_SizeT(format, va);
+    result = _PyObject_CallFunctionVa(callable, format, va, 1);
     va_end(va);
-    if (args == NULL) {
-        return NULL;
-    }
-
-    result = call_function_tail(callable, args);
-    Py_DECREF(args);
+
     return result;
 }
 
 static PyObject*
 callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
 {
-    PyObject *args, *result;
-
     assert(callable != NULL);
 
     if (!PyCallable_Check(callable)) {
@@ -2600,23 +2591,7 @@
         return NULL;
     }
 
-    if (!format || !*format) {
-        return _PyObject_CallNoArg(callable);
-    }
-
-    if (is_size_t) {
-        args = _Py_VaBuildValue_SizeT(format, va);
-    }
-    else {
-        args = Py_VaBuildValue(format, va);
-    }
-    if (args == NULL) {
-        return NULL;
-    }
-
-    result = call_function_tail(callable, args);
-    Py_DECREF(args);
-    return result;
+    return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
 }
 
 PyObject *

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list