Commit 860b2344 authored by Ghislain MARY's avatar Ghislain MARY
Browse files

Allow any callable as linphone core callbacks and log handler in the Python wrapper.

parent 87eb75d3
......@@ -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);
......
......@@ -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();
}}
}}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment