diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2016-12-08 23:22:56 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2016-12-08 23:22:56 (GMT) |
commit | e83aab12b0f5281ad0c1eba2fcee27f9789cb1db (patch) | |
tree | f483b99278102c954a38fd4f9d85593610b551f8 /Objects | |
parent | 3bb711998dc3f411b4f8189a8b6fd41901ed6362 (diff) | |
download | cpython-e83aab12b0f5281ad0c1eba2fcee27f9789cb1db.zip cpython-e83aab12b0f5281ad0c1eba2fcee27f9789cb1db.tar.gz cpython-e83aab12b0f5281ad0c1eba2fcee27f9789cb1db.tar.bz2 |
Add _PyObject_CallFunctionVa() helper
Issue #28915: Add _PyObject_CallFunctionVa() helper to factorize code of
functions:
* PyObject_CallFunction()
* _PyObject_CallFunction_SizeT()
* callmethod()
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 83 |
1 files changed, 29 insertions, 54 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index b2cf07c..4f59f04 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -2519,20 +2519,39 @@ _PyObject_FastCallKeywords(PyObject *callable, PyObject **stack, Py_ssize_t narg } } -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; + PyObject *args, *result; - assert(args != NULL); + 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 * 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 * _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 @@ callmethod(PyObject* callable, const char *format, va_list va, int is_size_t) 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 * |