From 860b23448c30532d86160514287c0c16ec1f6dff Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Mon, 11 Aug 2014 15:33:29 +0200 Subject: [PATCH] Allow any callable as linphone core callbacks and log handler in the Python wrapper. --- tools/python/apixml2python/handwritten.mustache | 14 ++++++++++---- tools/python/apixml2python/linphone.py | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tools/python/apixml2python/handwritten.mustache b/tools/python/apixml2python/handwritten.mustache index dff7a574b..54ad63314 100644 --- a/tools/python/apixml2python/handwritten.mustache +++ b/tools/python/apixml2python/handwritten.mustache @@ -17,7 +17,7 @@ static void pylinphone_log(const char *level, int indent, const char *fmt, va_li linphone_module = PyImport_ImportModule("linphone"); if ((linphone_module != NULL) && PyObject_HasAttrString(linphone_module, "__log_handler")) { PyObject *log_handler = PyObject_GetAttrString(linphone_module, "__log_handler"); - if ((log_handler != NULL) && PyFunction_Check(log_handler)) { + if ((log_handler != NULL) && PyCallable_Check(log_handler)) { char logstr[4096]; int i = 0; if (indent == -1) current_indent--; @@ -29,7 +29,9 @@ static void pylinphone_log(const char *level, int indent, const char *fmt, va_li } if (indent == 1) current_indent++; if (vsnprintf(logstr + i, sizeof(logstr) - i, fmt, args) > 0) { - PyEval_CallFunction(log_handler, "ss", level, logstr); + if (PyEval_CallObject(log_handler, Py_BuildValue("ss", level, logstr)) == NULL) { + PyErr_Print(); + } } Py_DECREF(log_handler); } @@ -73,10 +75,10 @@ static void pylinphone_module_log_handler(OrtpLogLevel lev, const char *fmt, va_ level = pylinphone_ortp_log_level_to_string(lev); if ((linphone_module != NULL) && PyObject_HasAttrString(linphone_module, "__log_handler")) { PyObject *log_handler = PyObject_GetAttrString(linphone_module, "__log_handler"); - if ((log_handler != NULL) && PyFunction_Check(log_handler)) { + if ((log_handler != NULL) && PyCallable_Check(log_handler)) { char logstr[4096]; if (vsnprintf(logstr, sizeof(logstr), fmt, args) > 0) { - if (PyEval_CallFunction(log_handler, "ss", level, logstr) == NULL) { + if (PyEval_CallObject(log_handler, Py_BuildValue("ss", level, logstr)) == NULL) { PyErr_Print(); } } @@ -100,6 +102,10 @@ static PyObject * pylinphone_module_method_set_log_handler(PyObject *self, PyObj if (!PyArg_ParseTuple(args, "O", &callback)) { return NULL; } + if (!PyCallable_Check(callback)) { + PyErr_SetString(PyExc_TypeError, "The argument must be a callable"); + return NULL; + } if (linphone_module != NULL) { Py_INCREF(callback); PyObject_SetAttrString(linphone_module, "__log_handler", callback); diff --git a/tools/python/apixml2python/linphone.py b/tools/python/apixml2python/linphone.py index 75ef733ad..1e3b44492 100644 --- a/tools/python/apixml2python/linphone.py +++ b/tools/python/apixml2python/linphone.py @@ -635,8 +635,8 @@ class EventCallbackMethodDefinition(MethodDefinition): args.append(arg_name) args=', '.join(args) return \ -""" if ((func != NULL) && PyFunction_Check(func)) {{ - if (PyEval_CallFunction(func, "{fmt}", {args}) == NULL) {{ +""" if ((func != NULL) && PyCallable_Check(func)) {{ + if (PyEval_CallObject(func, Py_BuildValue("{fmt}", {args})) == NULL) {{ PyErr_Print(); }} }}