diff options
author | James Booth <boothj5@gmail.com> | 2016-07-19 01:02:33 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2016-07-19 01:02:33 +0100 |
commit | 376811a960a7c91ca33d4665bbf08e9ab9cee665 (patch) | |
tree | 5167a6f38a64b97b0c2e8c81c4b5ec056c62e7ea /src | |
parent | 71ccfcc09fed24bb3b9115efe3a827b862d366de (diff) | |
download | profani-tty-376811a960a7c91ca33d4665bbf08e9ab9cee665.tar.gz |
Tidy python3 conditional code
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/python_api.c | 57 | ||||
-rw-r--r-- | src/plugins/python_api.h | 2 | ||||
-rw-r--r-- | src/plugins/python_plugins.c | 289 |
3 files changed, 76 insertions, 272 deletions
diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c index 01f7f8bd..08060f3f 100644 --- a/src/plugins/python_api.c +++ b/src/plugins/python_api.c @@ -56,7 +56,6 @@ python_api_cons_alert(PyObject *self, PyObject *args) disable_python_threads(); return Py_BuildValue(""); - } static PyObject* @@ -130,11 +129,7 @@ python_api_register_command(PyObject *self, PyObject *args) Py_ssize_t i = 0; for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(synopsis, i); -#if PY_MAJOR_VERSION >= 3 - char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item)); -#else - char *c_item = PyString_AsString(item); -#endif + char *c_item = python_object_to_string(item); c_synopsis[i] = c_item; } c_synopsis[len] = NULL; @@ -149,19 +144,11 @@ python_api_register_command(PyObject *self, PyObject *args) return Py_BuildValue(""); } PyObject *arg = PyList_GetItem(item, 0); -#if PY_MAJOR_VERSION >= 3 - char *c_arg = PyBytes_AS_STRING(PyUnicode_AsUTF8String(arg)); -#else - char *c_arg = PyString_AsString(arg); -#endif - PyObject *desc = PyList_GetItem(item, 1); - -#if PY_MAJOR_VERSION >= 3 - char *c_desc = PyBytes_AS_STRING(PyUnicode_AsUTF8String(desc)); -#else - char *c_desc = PyString_AsString(desc); -#endif + char *c_arg = python_object_to_string(arg); c_arguments[i][0] = c_arg; + + PyObject *desc = PyList_GetItem(item, 1); + char *c_desc = python_object_to_string(desc); c_arguments[i][1] = c_desc; } @@ -173,11 +160,7 @@ python_api_register_command(PyObject *self, PyObject *args) i = 0; for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(examples, i); -#if PY_MAJOR_VERSION >= 3 - char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item)); -#else - char *c_item = PyString_AsString(item); -#endif + char *c_item = python_object_to_string(item); c_examples[i] = c_item; } c_examples[len] = NULL; @@ -236,11 +219,7 @@ python_api_completer_add(PyObject *self, PyObject *args) Py_ssize_t i = 0; for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(items, i); -#if PY_MAJOR_VERSION >= 3 - char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item)); -#else - char *c_item = PyString_AsString(item); -#endif + char *c_item = python_object_to_string(item); c_items[i] = c_item; } c_items[len] = NULL; @@ -273,11 +252,7 @@ python_api_completer_remove(PyObject *self, PyObject *args) Py_ssize_t i = 0; for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(items, i); -#if PY_MAJOR_VERSION >= 3 - char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item)); -#else - char *c_item = PyString_AsString(item); -#endif + char *c_item = python_object_to_string(item); c_items[i] = c_item; } c_items[len] = NULL; @@ -891,14 +866,20 @@ _python_plugin_name(void) { PyThreadState *ts = PyThreadState_Get(); PyFrameObject *frame = ts->frame; -#if PY_MAJOR_VERSION >= 3 - char const *filename = PyBytes_AS_STRING(PyUnicode_AsUTF8String(frame->f_code->co_filename)); -#else - char const* filename = PyString_AsString(frame->f_code->co_filename); -#endif + char const* filename = python_object_to_string(frame->f_code->co_filename); gchar **split = g_strsplit(filename, "/", 0); char *plugin_name = strdup(split[g_strv_length(split)-1]); g_strfreev(split); return plugin_name; } + +char* +python_object_to_string(void *obj) +{ +#if PY_MAJOR_VERSION >= 3 + return PyBytes_AS_STRING(PyUnicode_AsUTF8String(obj)); +#else + return PyString_AsString(obj); +#endif +} diff --git a/src/plugins/python_api.h b/src/plugins/python_api.h index 79280cc6..dbc2b979 100644 --- a/src/plugins/python_api.h +++ b/src/plugins/python_api.h @@ -43,4 +43,6 @@ void python_command_callback(PluginCommand *command, gchar **args); void python_timed_callback(PluginTimedFunction *timed_function); void python_window_callback(PluginWindowCallback *window_callback, char *tag, char *line); +char* python_object_to_string(void *obj); + #endif diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index 15e244e1..c7f59956 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -47,6 +47,8 @@ static PyThreadState *thread_state; static GHashTable *loaded_modules; +static char* _python_parse_string_result(PyObject *result); + void allow_python_threads() { @@ -284,33 +286,10 @@ python_pre_chat_message_display_hook(ProfPlugin *plugin, const char *const jid, PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -354,33 +333,10 @@ python_pre_chat_message_send_hook(ProfPlugin *plugin, const char * const jid, co PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -424,33 +380,10 @@ python_pre_room_message_display_hook(ProfPlugin *plugin, const char * const room PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -495,33 +428,10 @@ python_pre_room_message_send_hook(ProfPlugin *plugin, const char *const room, co PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -588,33 +498,10 @@ python_pre_priv_message_display_hook(ProfPlugin *plugin, const char *const room, PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -660,33 +547,10 @@ python_pre_priv_message_send_hook(ProfPlugin *plugin, const char *const room, co PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -731,33 +595,10 @@ python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text) PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -809,33 +650,10 @@ python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text) PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -887,33 +705,10 @@ python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text) PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + char *result_str = _python_parse_string_result(result); + allow_python_threads(); -#if PY_MAJOR_VERSION >= 3 - if (result != Py_None) { - char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#else - if (PyUnicode_Check(result)) { - char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else if (result != Py_None) { - char *result_str = strdup(PyString_AsString(result)); - Py_XDECREF(result); - allow_python_threads(); - return result_str; - } else { - allow_python_threads(); - return NULL; - } -#endif + return result_str; } } @@ -1063,3 +858,29 @@ python_shutdown(void) g_hash_table_destroy(loaded_modules); Py_Finalize(); } + +static char* +_python_parse_string_result(PyObject *result) +{ +#if PY_MAJOR_VERSION >= 3 + if (result != Py_None) { + char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result))); + Py_XDECREF(result); + return result_str; + } else { + return NULL; + } +#else + if (PyUnicode_Check(result)) { + char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); + Py_XDECREF(result); + return result_str; + } else if (result != Py_None) { + char *result_str = strdup(PyString_AsString(result)); + Py_XDECREF(result); + return result_str; + } else { + return NULL; + } +#endif +} |