diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index 8cd721df..13278750 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -900,6 +900,8 @@ _python_type_error(ProfPlugin* plugin, char* hook, char* type) g_string_free(err_msg, TRUE); } +// Converts a Python string or None result to a C string and decreases the Python object's reference count. +// If the result is NULL, or not a string/unicode/bytes/None, logs an error and returns NULL. static char* _handle_string_or_none_result(ProfPlugin* plugin, PyObject* result, char* hook) { @@ -910,18 +912,16 @@ _handle_string_or_none_result(ProfPlugin* plugin, PyObject* result, char* hook) } #ifdef PY_IS_PYTHON3 if (result != Py_None && !PyUnicode_Check(result) && !PyBytes_Check(result)) { - allow_python_threads(); - _python_type_error(plugin, hook, "string, unicode or None"); - return NULL; - } #else if (result != Py_None && !PyUnicode_Check(result) && !PyString_Check(result)) { +#endif + Py_XDECREF(result); allow_python_threads(); _python_type_error(plugin, hook, "string, unicode or None"); return NULL; } -#endif char* result_str = python_str_or_unicode_to_string(result); + Py_XDECREF(result); allow_python_threads(); return result_str; }