diff options
Diffstat (limited to 'src/plugins/python_api.c')
-rw-r--r-- | src/plugins/python_api.c | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c index 6f478893..e0d54c1c 100644 --- a/src/plugins/python_api.c +++ b/src/plugins/python_api.c @@ -32,6 +32,8 @@ * */ +#include "config.h" + #include <Python.h> #include <frameobject.h> @@ -128,7 +130,11 @@ 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 c_synopsis[i] = c_item; } c_synopsis[len] = NULL; @@ -143,10 +149,18 @@ 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); - char *c_desc = PyString_AsString(desc); +#if PY_MAJOR_VERSION >= 3 + 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][1] = c_desc; } @@ -159,7 +173,11 @@ 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 c_examples[i] = c_item; } c_examples[len] = NULL; @@ -218,7 +236,11 @@ 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 c_items[i] = c_item; } c_items[len] = NULL; @@ -251,7 +273,11 @@ 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 c_items[i] = c_item; } c_items[len] = NULL; @@ -819,10 +845,31 @@ static PyMethodDef apiMethods[] = { { NULL, NULL, 0, NULL } }; -void +#if PY_MAJOR_VERSION >= 3 +static struct PyModuleDef profModule = +{ + PyModuleDef_HEAD_INIT, + "prof", + "", + -1, + apiMethods +}; +#endif + +PyMODINIT_FUNC python_api_init(void) { +#if PY_MAJOR_VERSION >= 3 + PyObject *result = PyModule_Create(&profModule); + if (!result) { + log_debug("Failed to initialise prof module"); + } else { + log_debug("Initialised prof module"); + } + return result; +#else Py_InitModule("prof", apiMethods); +#endif } static char* @@ -830,7 +877,11 @@ _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 gchar **split = g_strsplit(filename, "/", 0); char *plugin_name = strdup(split[g_strv_length(split)-1]); g_strfreev(split); |