[Python-checkins] cpython: Cleanup call_function_tail()

victor.stinner python-checkins at python.org
Fri Aug 19 11:32:17 EDT 2016


https://hg.python.org/cpython/rev/34af2edface9
changeset:   102759:34af2edface9
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Aug 19 16:50:49 2016 +0200
summary:
  Cleanup call_function_tail()

Make call_function_tail() less weird: don't decrement args reference counter,
the caller is now responsible to do that. The caller now also checks if args is
NULL.

Issue #27128.

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


diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2274,8 +2274,7 @@
 {
     PyObject *result;
 
-    if (args == NULL)
-        return NULL;
+    assert(args != NULL);
 
     if (!PyTuple_Check(args)) {
         result = _PyObject_FastCall(callable, &args, 1, NULL);
@@ -2284,7 +2283,6 @@
         result = PyObject_Call(callable, args, NULL);
     }
 
-    Py_DECREF(args);
     return result;
 }
 
@@ -2292,7 +2290,7 @@
 PyObject_CallFunction(PyObject *callable, const char *format, ...)
 {
     va_list va;
-    PyObject *args;
+    PyObject *args, *result;
 
     if (callable == NULL)
         return null_error();
@@ -2302,19 +2300,23 @@
         args = Py_VaBuildValue(format, va);
         va_end(va);
     }
-    else
+    else {
         args = PyTuple_New(0);
-    if (args == NULL)
+    }
+    if (args == NULL) {
         return NULL;
-
-    return call_function_tail(callable, args);
+    }
+
+    result = call_function_tail(callable, args);
+    Py_DECREF(args);
+    return result;
 }
 
 PyObject *
 _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
 {
     va_list va;
-    PyObject *args;
+    PyObject *args, *result;
 
     if (callable == NULL)
         return null_error();
@@ -2324,21 +2326,27 @@
         args = _Py_VaBuildValue_SizeT(format, va);
         va_end(va);
     }
-    else
+    else {
         args = PyTuple_New(0);
-
-    return call_function_tail(callable, args);
+    }
+    if (args == NULL) {
+        return NULL;
+    }
+
+    result = call_function_tail(callable, args);
+    Py_DECREF(args);
+    return result;
 }
 
 static PyObject*
 callmethod(PyObject* func, const char *format, va_list va, int is_size_t)
 {
-    PyObject *retval = NULL;
-    PyObject *args;
+    PyObject *args, *result;
 
     if (!PyCallable_Check(func)) {
         type_error("attribute of type '%.200s' is not callable", func);
-        goto exit;
+        Py_XDECREF(func);
+        return NULL;
     }
 
     if (format && *format) {
@@ -2347,16 +2355,17 @@
         else
             args = Py_VaBuildValue(format, va);
     }
-    else
+    else {
         args = PyTuple_New(0);
-
-    retval = call_function_tail(func, args);
-
-  exit:
-    /* args gets consumed in call_function_tail */
+    }
+    if (args == NULL) {
+        return NULL;
+    }
+
+    result = call_function_tail(func, args);
     Py_XDECREF(func);
-
-    return retval;
+    Py_DECREF(args);
+    return result;
 }
 
 PyObject *

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


More information about the Python-checkins mailing list