diff options
author | Fred Drake <fdrake@acm.org> | 2001-10-26 16:21:32 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-10-26 16:21:32 (GMT) |
commit | b421b8c19105d08b97122b7c84eec37ad83c6de4 (patch) | |
tree | 37e00d4acbdd57916ba418dbd79989c17c741ea7 /Objects | |
parent | ef7d08a661dc28c732f4738cb328a74b32ce435b (diff) | |
download | cpython-b421b8c19105d08b97122b7c84eec37ad83c6de4.zip cpython-b421b8c19105d08b97122b7c84eec37ad83c6de4.tar.gz cpython-b421b8c19105d08b97122b7c84eec37ad83c6de4.tar.bz2 |
Added two new functions to conveniently call functions/methods from C.
PyObject_CallFunctionObArgs() and PyObject_CallMethodObArgs() have the
advantage that no format strings need to be parsed. The CallMethod
variant also avoids creating a new string object in order to retrieve
a method from an object as well.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 6b9201b..003c9a3 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1758,6 +1758,82 @@ PyObject_CallMethod(PyObject *o, char *name, char *format, ...) } +static PyObject * +obargs_mktuple(va_list va) +{ + int i, n = 0; + va_list countva; + PyObject *result, *tmp; + +#ifdef VA_LIST_IS_ARRAY + memcpy(countva, va, sizeof(va_list)); +#else + countva = va; +#endif + + while (((PyObject *)va_arg(countva, PyObject *)) != NULL) + ++n; + result = PyTuple_New(n); + if (result != NULL && n > 0) { + for (i = 0; i < n; ++i) { + tmp = (PyObject *)va_arg(va, PyObject *); + PyTuple_SET_ITEM(result, i, tmp); + Py_INCREF(tmp); + } + } + return result; +} + +PyObject * +PyObject_CallMethodObArgs(PyObject *callable, PyObject *name, ...) +{ + PyObject *args, *tmp; + va_list vargs; + + if (callable == NULL || name == NULL) + return null_error(); + + callable = PyObject_GetAttr(callable, name); + if (callable == NULL) + return NULL; + + /* count the args */ + va_start(vargs, name); + args = obargs_mktuple(vargs); + va_end(vargs); + if (args == NULL) { + Py_DECREF(callable); + return NULL; + } + tmp = PyObject_Call(callable, args, NULL); + Py_DECREF(args); + Py_DECREF(callable); + + return tmp; +} + +PyObject * +PyObject_CallFunctionObArgs(PyObject *callable, ...) +{ + PyObject *args, *tmp; + va_list vargs; + + if (callable == NULL) + return null_error(); + + /* count the args */ + va_start(vargs, callable); + args = obargs_mktuple(vargs); + va_end(vargs); + if (args == NULL) + return NULL; + tmp = PyObject_Call(callable, args, NULL); + Py_DECREF(args); + + return tmp; +} + + /* isinstance(), issubclass() */ static PyObject * |