Tidy python3 conditional code

This commit is contained in:
James Booth
2016-07-19 01:02:33 +01:00
parent 71ccfcc09f
commit 376811a960
4 changed files with 78 additions and 274 deletions

View File

@@ -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
}