mirror of
https://github.com/profanity-im/profanity.git
synced 2025-02-02 15:08:15 -05:00
Fix prof module init for python3
This commit is contained in:
parent
ace2715cf7
commit
7d742f704f
@ -1,3 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
./configure PYTHON_VERSION=3 CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' --enable-python-plugins $@
|
./configure PYTHON_VERSION=3 CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' --enable-python-plugins $@
|
||||||
|
#./configure CFLAGS='-g3 -O0' CXXFLAGS='-g3 -O0' --enable-python-plugins $@
|
||||||
|
@ -96,7 +96,6 @@ 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
|
||||||
|
@ -130,7 +130,7 @@ 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
|
#if PY_MAJOR_VERSION >= 3
|
||||||
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
||||||
#else
|
#else
|
||||||
char *c_item = PyString_AsString(item);
|
char *c_item = PyString_AsString(item);
|
||||||
@ -149,14 +149,14 @@ 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
|
#if PY_MAJOR_VERSION >= 3
|
||||||
char *c_arg = PyBytes_AS_STRING(PyUnicode_AsUTF8String(arg));
|
char *c_arg = PyBytes_AS_STRING(PyUnicode_AsUTF8String(arg));
|
||||||
#else
|
#else
|
||||||
char *c_arg = PyString_AsString(arg);
|
char *c_arg = PyString_AsString(arg);
|
||||||
#endif
|
#endif
|
||||||
PyObject *desc = PyList_GetItem(item, 1);
|
PyObject *desc = PyList_GetItem(item, 1);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
char *c_desc = PyBytes_AS_STRING(PyUnicode_AsUTF8String(desc));
|
char *c_desc = PyBytes_AS_STRING(PyUnicode_AsUTF8String(desc));
|
||||||
#else
|
#else
|
||||||
char *c_desc = PyString_AsString(desc);
|
char *c_desc = PyString_AsString(desc);
|
||||||
@ -173,7 +173,7 @@ 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
|
#if PY_MAJOR_VERSION >= 3
|
||||||
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
||||||
#else
|
#else
|
||||||
char *c_item = PyString_AsString(item);
|
char *c_item = PyString_AsString(item);
|
||||||
@ -236,7 +236,7 @@ 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
|
#if PY_MAJOR_VERSION >= 3
|
||||||
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
||||||
#else
|
#else
|
||||||
char *c_item = PyString_AsString(item);
|
char *c_item = PyString_AsString(item);
|
||||||
@ -273,7 +273,7 @@ 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
|
#if PY_MAJOR_VERSION >= 3
|
||||||
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
char *c_item = PyBytes_AS_STRING(PyUnicode_AsUTF8String(item));
|
||||||
#else
|
#else
|
||||||
char *c_item = PyString_AsString(item);
|
char *c_item = PyString_AsString(item);
|
||||||
@ -809,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", (PyCFunction)python_api_cons_show, METH_VARARGS, "Print a line to the console." },
|
{ "cons_show", 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." },
|
||||||
@ -845,22 +845,28 @@ static PyMethodDef apiMethods[] = {
|
|||||||
{ NULL, NULL, 0, NULL }
|
{ NULL, NULL, 0, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
static struct PyModuleDef profModule =
|
static struct PyModuleDef profModule =
|
||||||
{
|
{
|
||||||
PyModuleDef_HEAD_INIT,
|
PyModuleDef_HEAD_INIT,
|
||||||
"prof", /* name of module */
|
"prof",
|
||||||
"", /* module documentation, may be NULL */
|
"",
|
||||||
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
|
-1,
|
||||||
apiMethods
|
apiMethods
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void
|
PyMODINIT_FUNC
|
||||||
python_api_init(void)
|
python_api_init(void)
|
||||||
{
|
{
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
PyModule_Create(&profModule);
|
PyObject *result = PyModule_Create(&profModule);
|
||||||
|
if (!result) {
|
||||||
|
log_debug("Failed to initialise prof module");
|
||||||
|
} else {
|
||||||
|
log_debug("Initialised prof module");
|
||||||
|
}
|
||||||
|
return result;
|
||||||
#else
|
#else
|
||||||
Py_InitModule("prof", apiMethods);
|
Py_InitModule("prof", apiMethods);
|
||||||
#endif
|
#endif
|
||||||
@ -871,7 +877,7 @@ _python_plugin_name(void)
|
|||||||
{
|
{
|
||||||
PyThreadState *ts = PyThreadState_Get();
|
PyThreadState *ts = PyThreadState_Get();
|
||||||
PyFrameObject *frame = ts->frame;
|
PyFrameObject *frame = ts->frame;
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
char const *filename = PyBytes_AS_STRING(PyUnicode_AsUTF8String(frame->f_code->co_filename));
|
char const *filename = PyBytes_AS_STRING(PyUnicode_AsUTF8String(frame->f_code->co_filename));
|
||||||
#else
|
#else
|
||||||
char const* filename = PyString_AsString(frame->f_code->co_filename);
|
char const* filename = PyString_AsString(frame->f_code->co_filename);
|
||||||
|
@ -35,8 +35,10 @@
|
|||||||
#ifndef PYTHON_API_H
|
#ifndef PYTHON_API_H
|
||||||
#define PYTHON_API_H
|
#define PYTHON_API_H
|
||||||
|
|
||||||
|
#include <Python.h>
|
||||||
|
|
||||||
void python_env_init(void);
|
void python_env_init(void);
|
||||||
void python_api_init(void);
|
PyMODINIT_FUNC python_api_init(void);
|
||||||
void python_shutdown(void);
|
void python_shutdown(void);
|
||||||
|
|
||||||
void python_command_callback(PluginCommand *command, gchar **args);
|
void python_command_callback(PluginCommand *command, gchar **args);
|
||||||
|
@ -68,22 +68,34 @@ _unref_module(PyObject *module)
|
|||||||
void
|
void
|
||||||
python_env_init(void)
|
python_env_init(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
loaded_modules = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_unref_module);
|
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();
|
Py_Initialize();
|
||||||
PyEval_InitThreads();
|
PyEval_InitThreads();
|
||||||
python_api_init();
|
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");
|
GString *path = g_string_new("import sys\n");
|
||||||
g_string_append(path, "sys.path.append(\"");
|
g_string_append(path, "sys.path.append(\"");
|
||||||
gchar *plugins_dir = plugins_get_dir();
|
|
||||||
g_string_append(path, plugins_dir);
|
g_string_append(path, plugins_dir);
|
||||||
g_free(plugins_dir);
|
|
||||||
g_string_append(path, "/\")\n");
|
g_string_append(path, "/\")\n");
|
||||||
|
|
||||||
PyRun_SimpleString(path->str);
|
PyRun_SimpleString(path->str);
|
||||||
|
python_check_error();
|
||||||
|
|
||||||
g_string_free(path, TRUE);
|
g_string_free(path, TRUE);
|
||||||
|
g_free(plugins_dir);
|
||||||
|
|
||||||
allow_python_threads();
|
allow_python_threads();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +110,9 @@ python_plugin_create(const char *const filename)
|
|||||||
} else {
|
} else {
|
||||||
gchar *module_name = g_strndup(filename, strlen(filename) - 3);
|
gchar *module_name = g_strndup(filename, strlen(filename) - 3);
|
||||||
p_module = PyImport_ImportModule(module_name);
|
p_module = PyImport_ImportModule(module_name);
|
||||||
|
if (p_module) {
|
||||||
g_hash_table_insert(loaded_modules, strdup(filename), p_module);
|
g_hash_table_insert(loaded_modules, strdup(filename), p_module);
|
||||||
|
}
|
||||||
g_free(module_name);
|
g_free(module_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,7 +294,7 @@ python_pre_chat_message_display_hook(ProfPlugin *plugin, const char *const jid,
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -350,7 +364,7 @@ python_pre_chat_message_send_hook(ProfPlugin *plugin, const char * const jid, co
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -420,7 +434,7 @@ python_pre_room_message_display_hook(ProfPlugin *plugin, const char * const room
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -491,7 +505,7 @@ python_pre_room_message_send_hook(ProfPlugin *plugin, const char *const room, co
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -584,7 +598,7 @@ python_pre_priv_message_display_hook(ProfPlugin *plugin, const char *const room,
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -656,7 +670,7 @@ python_pre_priv_message_send_hook(ProfPlugin *plugin, const char *const room, co
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -727,7 +741,7 @@ python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text)
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -805,7 +819,7 @@ python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text)
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -883,7 +897,7 @@ python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text)
|
|||||||
python_check_error();
|
python_check_error();
|
||||||
Py_XDECREF(p_function);
|
Py_XDECREF(p_function);
|
||||||
|
|
||||||
#ifdef PYTHON3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
if (result != Py_None) {
|
if (result != Py_None) {
|
||||||
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
char *result_str = strdup(PyBytes_AS_STRING(PyUnicode_AsUTF8String(result)));
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
@ -1036,6 +1050,7 @@ python_check_error(void)
|
|||||||
{
|
{
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
|
PyRun_SimpleString("import sys\nsys.stdout.flush()");
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user