about summary refs log tree commit diff stats
path: root/src/plugins/python_plugins.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/python_plugins.c')
-rw-r--r--src/plugins/python_plugins.c140
1 files changed, 137 insertions, 3 deletions
diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c
index 61a4c479..670dd46f 100644
--- a/src/plugins/python_plugins.c
+++ b/src/plugins/python_plugins.c
@@ -34,6 +34,8 @@
 
 #include <Python.h>
 
+#include "config.h"
+
 #include "config/preferences.h"
 #include "plugins/api.h"
 #include "plugins/callbacks.h"
@@ -66,22 +68,34 @@ _unref_module(PyObject *module)
 void
 python_env_init(void)
 {
+
     loaded_modules = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_unref_module);
 
+#if PY_MAJOR_VERSION >= 3
+    PyImport_AppendInittab("prof", python_api_init);
+    Py_Initialize();
+    PyEval_InitThreads();
+#else
     Py_Initialize();
     PyEval_InitThreads();
     python_api_init();
+#endif
+
+    const char *ver = Py_GetVersion();
+    cons_show("PYTHON: %s", ver);
 
+    gchar *plugins_dir = plugins_get_dir();
     GString *path = g_string_new("import sys\n");
     g_string_append(path, "sys.path.append(\"");
-    gchar *plugins_dir = plugins_get_dir();
     g_string_append(path, plugins_dir);
-    g_free(plugins_dir);
     g_string_append(path, "/\")\n");
 
     PyRun_SimpleString(path->str);
+    python_check_error();
 
     g_string_free(path, TRUE);
+    g_free(plugins_dir);
+
     allow_python_threads();
 }
 
@@ -96,7 +110,9 @@ python_plugin_create(const char *const filename)
     } else {
         gchar *module_name = g_strndup(filename, strlen(filename) - 3);
         p_module = PyImport_ImportModule(module_name);
-        g_hash_table_insert(loaded_modules, strdup(filename), p_module);
+        if (p_module) {
+            g_hash_table_insert(loaded_modules, strdup(filename), p_module);
+        }
         g_free(module_name);
     }
 
@@ -277,6 +293,18 @@ 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);
+
+#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);
@@ -291,6 +319,7 @@ python_pre_chat_message_display_hook(ProfPlugin *plugin, const char *const jid,
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -334,6 +363,18 @@ 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);
+
+#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);
@@ -348,6 +389,7 @@ python_pre_chat_message_send_hook(ProfPlugin *plugin, const char * const jid, co
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -391,6 +433,18 @@ 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);
+
+#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);
@@ -405,6 +459,7 @@ python_pre_room_message_display_hook(ProfPlugin *plugin, const char * const room
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -449,6 +504,18 @@ 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);
+
+#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);
@@ -463,6 +530,7 @@ python_pre_room_message_send_hook(ProfPlugin *plugin, const char *const room, co
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -529,6 +597,18 @@ 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);
+
+#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);
@@ -543,6 +623,7 @@ python_pre_priv_message_display_hook(ProfPlugin *plugin, const char *const room,
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -588,6 +669,18 @@ 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);
+
+#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);
@@ -602,6 +695,7 @@ python_pre_priv_message_send_hook(ProfPlugin *plugin, const char *const room, co
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -646,6 +740,18 @@ 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);
+
+#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);
@@ -660,6 +766,7 @@ python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text)
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -711,6 +818,18 @@ 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);
+
+#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);
@@ -725,6 +844,7 @@ python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text)
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -776,6 +896,18 @@ 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);
+
+#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);
@@ -790,6 +922,7 @@ python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text)
                 allow_python_threads();
                 return NULL;
             }
+#endif
         }
     }
 
@@ -917,6 +1050,7 @@ python_check_error(void)
 {
     if (PyErr_Occurred()) {
         PyErr_Print();
+        PyRun_SimpleString("import sys\nsys.stdout.flush()");
         PyErr_Clear();
     }
 }