1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-23 21:45:30 +00:00

Added PYTHON3 define

This commit is contained in:
James Booth 2016-07-14 00:00:46 +01:00
parent 10814b044d
commit ace2715cf7
4 changed files with 168 additions and 3 deletions

View File

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
./configure CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' --enable-python-plugins $@ ./configure PYTHON_VERSION=3 CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' --enable-python-plugins $@

View File

@ -96,6 +96,7 @@ elif test "x$enable_python_plugins" = xyes; then
fi fi
fi fi
AS_IF([test "x$PLATFORM" = xosx], [rm -f Python.framework]) AS_IF([test "x$PLATFORM" = xosx], [rm -f Python.framework])
AS_IF([test "x$PYTHON_VERSION" = x3], [AC_DEFINE([PYTHON3], [1], [Python 3 build])])
else else
AM_CONDITIONAL([BUILD_PYTHON_API], [false]) AM_CONDITIONAL([BUILD_PYTHON_API], [false])
fi fi

View File

@ -32,6 +32,8 @@
* *
*/ */
#include "config.h"
#include <Python.h> #include <Python.h>
#include <frameobject.h> #include <frameobject.h>
@ -128,7 +130,11 @@ python_api_register_command(PyObject *self, PyObject *args)
Py_ssize_t i = 0; Py_ssize_t i = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyObject *item = PyList_GetItem(synopsis, i); PyObject *item = PyList_GetItem(synopsis, i);
#ifdef PYTHON3
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
#else
char *c_item = PyString_AsString(item); char *c_item = PyString_AsString(item);
#endif
c_synopsis[i] = c_item; c_synopsis[i] = c_item;
} }
c_synopsis[len] = NULL; c_synopsis[len] = NULL;
@ -143,10 +149,18 @@ python_api_register_command(PyObject *self, PyObject *args)
return Py_BuildValue(""); return Py_BuildValue("");
} }
PyObject *arg = PyList_GetItem(item, 0); PyObject *arg = PyList_GetItem(item, 0);
#ifdef PYTHON3
char *c_arg = PyBytes_AS_STRING(PyUnicode_AsUTF8String(arg));
#else
char *c_arg = PyString_AsString(arg); char *c_arg = PyString_AsString(arg);
#endif
PyObject *desc = PyList_GetItem(item, 1); PyObject *desc = PyList_GetItem(item, 1);
char *c_desc = PyString_AsString(desc);
#ifdef PYTHON3
char *c_desc = PyBytes_AS_STRING(PyUnicode_AsUTF8String(desc));
#else
char *c_desc = PyString_AsString(desc);
#endif
c_arguments[i][0] = c_arg; c_arguments[i][0] = c_arg;
c_arguments[i][1] = c_desc; c_arguments[i][1] = c_desc;
} }
@ -159,7 +173,11 @@ python_api_register_command(PyObject *self, PyObject *args)
i = 0; i = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyObject *item = PyList_GetItem(examples, i); PyObject *item = PyList_GetItem(examples, i);
#ifdef PYTHON3
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
#else
char *c_item = PyString_AsString(item); char *c_item = PyString_AsString(item);
#endif
c_examples[i] = c_item; c_examples[i] = c_item;
} }
c_examples[len] = NULL; c_examples[len] = NULL;
@ -218,7 +236,11 @@ python_api_completer_add(PyObject *self, PyObject *args)
Py_ssize_t i = 0; Py_ssize_t i = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyObject *item = PyList_GetItem(items, i); PyObject *item = PyList_GetItem(items, i);
#ifdef PYTHON3
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
#else
char *c_item = PyString_AsString(item); char *c_item = PyString_AsString(item);
#endif
c_items[i] = c_item; c_items[i] = c_item;
} }
c_items[len] = NULL; c_items[len] = NULL;
@ -251,7 +273,11 @@ python_api_completer_remove(PyObject *self, PyObject *args)
Py_ssize_t i = 0; Py_ssize_t i = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
PyObject *item = PyList_GetItem(items, i); PyObject *item = PyList_GetItem(items, i);
#ifdef PYTHON3
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
#else
char *c_item = PyString_AsString(item); char *c_item = PyString_AsString(item);
#endif
c_items[i] = c_item; c_items[i] = c_item;
} }
c_items[len] = NULL; c_items[len] = NULL;
@ -783,7 +809,7 @@ python_window_callback(PluginWindowCallback *window_callback, char *tag, char *l
static PyMethodDef apiMethods[] = { static PyMethodDef apiMethods[] = {
{ "cons_alert", python_api_cons_alert, METH_NOARGS, "Highlight the console window in the status bar." }, { "cons_alert", python_api_cons_alert, METH_NOARGS, "Highlight the console window in the status bar." },
{ "cons_show", python_api_cons_show, METH_VARARGS, "Print a line to the console." }, { "cons_show", (PyCFunction)python_api_cons_show, METH_VARARGS, "Print a line to the console." },
{ "cons_show_themed", python_api_cons_show_themed, METH_VARARGS, "Print a themed line to the console" }, { "cons_show_themed", python_api_cons_show_themed, METH_VARARGS, "Print a themed line to the console" },
{ "cons_bad_cmd_usage", python_api_cons_bad_cmd_usage, METH_VARARGS, "Show invalid command message in console" }, { "cons_bad_cmd_usage", python_api_cons_bad_cmd_usage, METH_VARARGS, "Show invalid command message in console" },
{ "register_command", python_api_register_command, METH_VARARGS, "Register a command." }, { "register_command", python_api_register_command, METH_VARARGS, "Register a command." },
@ -819,10 +845,25 @@ static PyMethodDef apiMethods[] = {
{ NULL, NULL, 0, NULL } { NULL, NULL, 0, NULL }
}; };
#ifdef PYTHON3
static struct PyModuleDef profModule =
{
PyModuleDef_HEAD_INIT,
"prof", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
apiMethods
};
#endif
void void
python_api_init(void) python_api_init(void)
{ {
#ifdef PYTHON3
PyModule_Create(&profModule);
#else
Py_InitModule("prof", apiMethods); Py_InitModule("prof", apiMethods);
#endif
} }
static char* static char*
@ -830,7 +871,11 @@ _python_plugin_name(void)
{ {
PyThreadState *ts = PyThreadState_Get(); PyThreadState *ts = PyThreadState_Get();
PyFrameObject *frame = ts->frame; PyFrameObject *frame = ts->frame;
#ifdef PYTHON3
char const *filename = PyBytes_AS_STRING(PyUnicode_AsUTF8String(frame->f_code->co_filename));
#else
char const* filename = PyString_AsString(frame->f_code->co_filename); char const* filename = PyString_AsString(frame->f_code->co_filename);
#endif
gchar **split = g_strsplit(filename, "/", 0); gchar **split = g_strsplit(filename, "/", 0);
char *plugin_name = strdup(split[g_strv_length(split)-1]); char *plugin_name = strdup(split[g_strv_length(split)-1]);
g_strfreev(split); g_strfreev(split);

View File

@ -34,6 +34,8 @@
#include <Python.h> #include <Python.h>
#include "config.h"
#include "config/preferences.h" #include "config/preferences.h"
#include "plugins/api.h" #include "plugins/api.h"
#include "plugins/callbacks.h" #include "plugins/callbacks.h"
@ -277,6 +279,18 @@ python_pre_chat_message_display_hook(ProfPlugin *plugin, const char *const jid,
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -291,6 +305,7 @@ python_pre_chat_message_display_hook(ProfPlugin *plugin, const char *const jid,
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -334,6 +349,18 @@ python_pre_chat_message_send_hook(ProfPlugin *plugin, const char * const jid, co
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -348,6 +375,7 @@ python_pre_chat_message_send_hook(ProfPlugin *plugin, const char * const jid, co
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -391,6 +419,18 @@ python_pre_room_message_display_hook(ProfPlugin *plugin, const char * const room
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -405,6 +445,7 @@ python_pre_room_message_display_hook(ProfPlugin *plugin, const char * const room
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -449,6 +490,18 @@ python_pre_room_message_send_hook(ProfPlugin *plugin, const char *const room, co
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -463,6 +516,7 @@ python_pre_room_message_send_hook(ProfPlugin *plugin, const char *const room, co
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -529,6 +583,18 @@ python_pre_priv_message_display_hook(ProfPlugin *plugin, const char *const room,
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -543,6 +609,7 @@ python_pre_priv_message_display_hook(ProfPlugin *plugin, const char *const room,
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -588,6 +655,18 @@ python_pre_priv_message_send_hook(ProfPlugin *plugin, const char *const room, co
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -602,6 +681,7 @@ python_pre_priv_message_send_hook(ProfPlugin *plugin, const char *const room, co
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -646,6 +726,18 @@ python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text)
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -660,6 +752,7 @@ python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text)
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -711,6 +804,18 @@ python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text)
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -725,6 +830,7 @@ python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text)
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }
@ -776,6 +882,18 @@ python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text)
PyObject *result = PyObject_CallObject(p_function, p_args); PyObject *result = PyObject_CallObject(p_function, p_args);
python_check_error(); python_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
#ifdef PYTHON3
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)) { if (PyUnicode_Check(result)) {
char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result))); char *result_str = strdup(PyString_AsString(PyUnicode_AsUTF8String(result)));
Py_XDECREF(result); Py_XDECREF(result);
@ -790,6 +908,7 @@ python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text)
allow_python_threads(); allow_python_threads();
return NULL; return NULL;
} }
#endif
} }
} }