From fc11da8069703832862746a25adb9493131110b5 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Fri, 5 Sep 2014 14:34:01 +0200 Subject: [PATCH] Fix some refcounting issues in the Python wrapper. --- .../handwritten_definitions.mustache | 58 +++++++++++-------- .../apixml2python/linphone_module.mustache | 2 + 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/tools/python/apixml2python/handwritten_definitions.mustache b/tools/python/apixml2python/handwritten_definitions.mustache index a4b3e579f..46457beb2 100644 --- a/tools/python/apixml2python/handwritten_definitions.mustache +++ b/tools/python/apixml2python/handwritten_definitions.mustache @@ -16,25 +16,29 @@ static void pylinphone_log(const char *level, int indent, const char *fmt, va_li gstate = PyGILState_Ensure(); if (gstate != PyGILState_LOCKED) return; linphone_module = PyImport_ImportModule("linphone.linphone"); - if ((linphone_module != NULL) && PyObject_HasAttrString(linphone_module, "__log_handler")) { - PyObject *log_handler = PyObject_GetAttrString(linphone_module, "__log_handler"); - if ((log_handler != NULL) && PyCallable_Check(log_handler)) { - char logstr[4096]; - int i = 0; - if (indent == -1) current_indent--; - if (current_indent < 1) current_indent = 1; - if ((indent >= -1) && (indent <= 1)) { - for (i = 0; i < current_indent; i++) { - logstr[i] = '\t'; + if (linphone_module != NULL) { + if (PyObject_HasAttrString(linphone_module, "__log_handler")) { + PyObject *log_handler = PyObject_GetAttrString(linphone_module, "__log_handler"); + if (log_handler != NULL) { + if (PyCallable_Check(log_handler)) { + char logstr[4096]; + int i = 0; + if (indent == -1) current_indent--; + if (current_indent < 1) current_indent = 1; + if ((indent >= -1) && (indent <= 1)) { + for (i = 0; i < current_indent; i++) { + logstr[i] = '\t'; + } + } + if (indent == 1) current_indent++; + if (vsnprintf(logstr + i, sizeof(logstr) - i, fmt, args) > 0) { + if (PyEval_CallObject(log_handler, Py_BuildValue("ss", level, logstr)) == NULL) { + PyErr_Print(); + } + } } + Py_DECREF(log_handler); } - if (indent == 1) current_indent++; - if (vsnprintf(logstr + i, sizeof(logstr) - i, fmt, args) > 0) { - if (PyEval_CallObject(log_handler, Py_BuildValue("ss", level, logstr)) == NULL) { - PyErr_Print(); - } - } - Py_DECREF(log_handler); } Py_DECREF(linphone_module); } @@ -75,16 +79,20 @@ static void pylinphone_module_log_handler(OrtpLogLevel lev, const char *fmt, va_ if (gstate != PyGILState_LOCKED) return; linphone_module = PyImport_ImportModule("linphone.linphone"); 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) && PyCallable_Check(log_handler)) { - char logstr[4096]; - if (vsnprintf(logstr, sizeof(logstr), fmt, args) > 0) { - if (PyEval_CallObject(log_handler, Py_BuildValue("ss", level, logstr)) == NULL) { - PyErr_Print(); + if (linphone_module != NULL) { + if (PyObject_HasAttrString(linphone_module, "__log_handler")) { + PyObject *log_handler = PyObject_GetAttrString(linphone_module, "__log_handler"); + if (log_handler != NULL) { + if (PyCallable_Check(log_handler)) { + char logstr[4096]; + if (vsnprintf(logstr, sizeof(logstr), fmt, args) > 0) { + if (PyEval_CallObject(log_handler, Py_BuildValue("ss", level, logstr)) == NULL) { + PyErr_Print(); + } + } } + Py_DECREF(log_handler); } - Py_DECREF(log_handler); } Py_DECREF(linphone_module); } diff --git a/tools/python/apixml2python/linphone_module.mustache b/tools/python/apixml2python/linphone_module.mustache index 5a8f83b00..bfb6ccfd1 100644 --- a/tools/python/apixml2python/linphone_module.mustache +++ b/tools/python/apixml2python/linphone_module.mustache @@ -293,6 +293,7 @@ PyMODINIT_FUNC initlinphone(void) { {{#enums}} menum = Py_InitModule3("{{enum_name}}", pylinphone_{{enum_name}}_ModuleMethods, "{{{enum_doc}}}"); if (menum == NULL) return; + Py_INCREF(menum); if (PyModule_AddObject(m, "{{enum_name}}", menum) < 0) return; {{#enum_values}} if (PyModule_AddIntConstant(menum, "{{enum_value_name}}", {{enum_value_cname}}) < 0) return; @@ -301,6 +302,7 @@ PyMODINIT_FUNC initlinphone(void) { menum = Py_InitModule3("PayloadTypeType", pylinphone_PayloadTypeType_ModuleMethods, "Type of linphone.PayloadType."); if (menum == NULL) return; + Py_INCREF(menum); if (PyModule_AddObject(m, "PayloadTypeType", menum) < 0) return; if (PyModule_AddIntConstant(menum, "PAYLOAD_AUDIO_CONTINUOUS", PAYLOAD_AUDIO_CONTINUOUS) < 0) return; if (PyModule_AddIntConstant(menum, "PAYLOAD_AUDIO_PACKETIZED", PAYLOAD_AUDIO_PACKETIZED) < 0) return;