From 36230c0b5d872388449b20d46d39a9e66a017714 Mon Sep 17 00:00:00 2001 From: "David C. Rankin" Date: Sat, 27 Sep 2014 11:36:51 -0500 Subject: Add Python >= v3 support to SuperKaramba --- superkaramba/src/karamba_python.cpp | 57 +++++++++++++++++++++++++++++-- superkaramba/src/meter_python.cpp | 6 ++-- superkaramba/src/misc_python.cpp | 2 +- superkaramba/src/svcgrp_python.cpp | 68 ++++++++++++++++++------------------- superkaramba/src/task_python.cpp | 44 ++++++++++++------------ 5 files changed, 115 insertions(+), 62 deletions(-) (limited to 'superkaramba') diff --git a/superkaramba/src/karamba_python.cpp b/superkaramba/src/karamba_python.cpp index 5e64ed7..73212a0 100644 --- a/superkaramba/src/karamba_python.cpp +++ b/superkaramba/src/karamba_python.cpp @@ -47,6 +47,24 @@ #include "misc_python.h" #include "input_python.h" +struct module_state { + PyObject *error; +}; + +#if PY_MAJOR_VERSION >= 3 +#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) +#else +#define GETSTATE(m) (&_state) +static struct module_state _state; +#endif + +static PyObject * +error_out(PyObject *m) { + struct module_state *st = GETSTATE(m); + PyErr_SetString(st->error, "something bad happened in karamba_python.cpp"); + return NULL; +} + /******************************************* * Python methods are defined here. * Each method accessible from python should have: @@ -338,6 +356,38 @@ static PyMethodDef karamba_methods[] = { {NULL, NULL, 0 ,NULL} }; +#if PY_MAJOR_VERSION >= 3 + +static int karamba_traverse(PyObject *m, visitproc visit, void *arg) { + Py_VISIT(GETSTATE(m)->error); + return 0; +} + +static int karamba_clear(PyObject *m) { + Py_CLEAR(GETSTATE(m)->error); + return 0; +} + +static struct PyModuleDef karambadef = { + PyModuleDef_HEAD_INIT, + "karamba", + NULL, + sizeof(struct module_state), + karamba_methods, + NULL, + karamba_traverse, + karamba_clear, + NULL +}; + +#define INITERROR return NULL + +#else + +#define INITERROR return + +#endif + PyThreadState* KarambaPython::mainThreadState = 0; KarambaPython::KarambaPython(const ThemeFile& theme, bool reloading): @@ -356,9 +406,12 @@ KarambaPython::KarambaPython(const ThemeFile& theme, bool reloading): PyRun_SimpleString((char*)"sys.path.insert(0, '')"); PyImport_AddModule((char*)"karamba"); +#if PY_MAJOR_VERSION >= 3 + PyModule_Create(&karambadef); +#else Py_InitModule((char*)"karamba", karamba_methods); - - pName = PyString_FromString(theme.pythonModule().ascii()); +#endif + pName = PyBytes_FromString(theme.pythonModule().ascii()); pModule = PyImport_Import(pName); fprintf(stderr, "%s\n", pypath); diff --git a/superkaramba/src/meter_python.cpp b/superkaramba/src/meter_python.cpp index 12c3a7e..c5d745b 100644 --- a/superkaramba/src/meter_python.cpp +++ b/superkaramba/src/meter_python.cpp @@ -93,9 +93,9 @@ TQString fromUcs4(TQ_UINT32* ucs4) TQString PyString2TQString(PyObject* text) { TQString qtext; - if (PyString_CheckExact(text)) + if (PyBytes_CheckExact(text)) { - char* t = PyString_AsString(text); + char* t = PyBytes_AsString(text); qtext.setAscii(t); } else if (PyUnicode_CheckExact(text)) @@ -148,7 +148,7 @@ PyObject* TQString2PyString(TQString string) } else - pyString = PyString_FromString(""); + pyString = PyBytes_FromString(""); return pyString; } diff --git a/superkaramba/src/misc_python.cpp b/superkaramba/src/misc_python.cpp index 06b679d..271ec51 100644 --- a/superkaramba/src/misc_python.cpp +++ b/superkaramba/src/misc_python.cpp @@ -343,7 +343,7 @@ PyObject* py_read_theme_file(PyObject *, PyObject *args) return NULL; karamba* k = (karamba*)widget; TQByteArray ba = k->theme().readThemeFile(file); - return PyString_FromStringAndSize(ba.data(), ba.size()); + return PyBytes_FromStringAndSize(ba.data(), ba.size()); } /* now a method we need to expose to Python */ diff --git a/superkaramba/src/svcgrp_python.cpp b/superkaramba/src/svcgrp_python.cpp index 8b827ec..236d920 100644 --- a/superkaramba/src/svcgrp_python.cpp +++ b/superkaramba/src/svcgrp_python.cpp @@ -37,16 +37,16 @@ static PyObject *get_svc_grp(KServiceGroup::Ptr const& g) PyObject *tuple = PyTuple_New(2); PyObject *dict = PyDict_New(); - PyDict_SetItem(dict, PyString_FromString("caption"), - PyString_FromString(g->caption().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("caption"), + PyBytes_FromString(g->caption().ascii())); if (g->comment() != NULL) - PyDict_SetItem(dict, PyString_FromString("comment"), - PyString_FromString(g->comment().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("comment"), + PyBytes_FromString(g->comment().ascii())); if (g->icon() != NULL) - PyDict_SetItem(dict, PyString_FromString("icon"), - PyString_FromString(g->icon().ascii())); - PyDict_SetItem(dict, PyString_FromString("relpath"), - PyString_FromString(g->relPath().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("icon"), + PyBytes_FromString(g->icon().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("relpath"), + PyBytes_FromString(g->relPath().ascii())); PyTuple_SET_ITEM(tuple, 0, Py_BuildValue((char*)"l", 0)); PyTuple_SET_ITEM(tuple, 1, dict); @@ -61,43 +61,43 @@ static PyObject *get_svc(KService::Ptr const& g) PyObject *dict = PyDict_New(); if (g->exec() != NULL) - PyDict_SetItem(dict, PyString_FromString("exec"), - PyString_FromString(g->exec().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("exec"), + PyBytes_FromString(g->exec().ascii())); if (g->menuId() != NULL) - PyDict_SetItem(dict, PyString_FromString("menuid"), - PyString_FromString(g->menuId().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("menuid"), + PyBytes_FromString(g->menuId().ascii())); if (g->name() != NULL) - PyDict_SetItem(dict, PyString_FromString("name"), - PyString_FromString(g->name().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("name"), + PyBytes_FromString(g->name().ascii())); if (g->path() != NULL) - PyDict_SetItem(dict, PyString_FromString("path"), - PyString_FromString(g->path().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("path"), + PyBytes_FromString(g->path().ascii())); if (g->icon() != NULL) - PyDict_SetItem(dict, PyString_FromString("icon"), - PyString_FromString(g->icon().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("icon"), + PyBytes_FromString(g->icon().ascii())); if (g->library() != NULL) - PyDict_SetItem(dict, PyString_FromString("library"), - PyString_FromString(g->library().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("library"), + PyBytes_FromString(g->library().ascii())); if (g->comment() != NULL) - PyDict_SetItem(dict, PyString_FromString("comment"), - PyString_FromString(g->comment().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("comment"), + PyBytes_FromString(g->comment().ascii())); if (g->type() != NULL) - PyDict_SetItem(dict, PyString_FromString("type"), - PyString_FromString(g->type().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("type"), + PyBytes_FromString(g->type().ascii())); if (g->genericName() != NULL) - PyDict_SetItem(dict, PyString_FromString("genericname"), - PyString_FromString(g->genericName().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("genericname"), + PyBytes_FromString(g->genericName().ascii())); /* - PyDict_SetItem(dict, PyString_FromString("terminal"), + PyDict_SetItem(dict, PyBytes_FromString("terminal"), Py_BuildValue("l", g->terminal())); - PyDict_SetItem(dict, PyString_FromString("type"), - PyString_FromString(g->type().ascii())); - PyDict_SetItem(dict, PyString_FromString("username"), - PyString_FromString(g->username().ascii())); - PyDict_SetItem(dict, PyString_FromString("substuid"), + PyDict_SetItem(dict, PyBytes_FromString("type"), + PyBytes_FromString(g->type().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("username"), + PyBytes_FromString(g->username().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("substuid"), Py_BuildValue("l", g->substituteUid())); - PyDict_SetItem(dict, PyString_FromString("path"), - PyString_FromString(g->path().ascii())); + PyDict_SetItem(dict, PyBytes_FromString("path"), + PyBytes_FromString(g->path().ascii())); */ PyTuple_SET_ITEM(tuple, 0, Py_BuildValue((char*)"l", 1)); diff --git a/superkaramba/src/task_python.cpp b/superkaramba/src/task_python.cpp index 4b81f26..e8d1d3e 100644 --- a/superkaramba/src/task_python.cpp +++ b/superkaramba/src/task_python.cpp @@ -143,50 +143,50 @@ PyObject* getTaskInfo(long widget, long ctask) //Task Name if (currTask->name() != NULL) { - PyList_Append(pList, PyString_FromString(currTask->name().latin1())); + PyList_Append(pList, PyBytes_FromString(currTask->name().latin1())); } else { - PyList_Append(pList, PyString_FromString("")); + PyList_Append(pList, PyBytes_FromString("")); } //Icon Name if (currTask->iconName() != NULL) { - PyList_Append(pList, PyString_FromString(currTask->iconName().latin1())); + PyList_Append(pList, PyBytes_FromString(currTask->iconName().latin1())); } else { - PyList_Append(pList, PyString_FromString("")); + PyList_Append(pList, PyBytes_FromString("")); } //Class Name if (currTask->className() != NULL) { - PyList_Append(pList, PyString_FromString(currTask->className().latin1())); + PyList_Append(pList, PyBytes_FromString(currTask->className().latin1())); } else { - PyList_Append(pList, PyString_FromString("")); + PyList_Append(pList, PyBytes_FromString("")); } // Desktop this task is on - PyList_Append(pList, PyInt_FromLong(currTask->desktop())); + PyList_Append(pList, PyLong_FromLong(currTask->desktop())); // is it maximized? - PyList_Append(pList, PyInt_FromLong(currTask->isMaximized())); + PyList_Append(pList, PyLong_FromLong(currTask->isMaximized())); // is it iconified? - PyList_Append(pList, PyInt_FromLong(currTask->isIconified())); + PyList_Append(pList, PyLong_FromLong(currTask->isIconified())); // is it shaded? - PyList_Append(pList, PyInt_FromLong(currTask->isShaded())); + PyList_Append(pList, PyLong_FromLong(currTask->isShaded())); // is it focused? - PyList_Append(pList, PyInt_FromLong(currTask->isActive())); + PyList_Append(pList, PyLong_FromLong(currTask->isActive())); // a reference back to itself - PyList_Append(pList, PyInt_FromLong((long)currTask)); + PyList_Append(pList, PyLong_FromLong((long)currTask)); return pList; @@ -235,35 +235,35 @@ PyObject* getStartupInfo(long widget, long cstartup) //Startup Name if (startup -> text() != NULL) { - PyList_Append(pList, PyString_FromString(startup -> text().latin1())); + PyList_Append(pList, PyBytes_FromString(startup -> text().latin1())); } else { - PyList_Append(pList, PyString_FromString("")); + PyList_Append(pList, PyBytes_FromString("")); } //Icon Name if (startup -> icon() != NULL) { - PyList_Append(pList, PyString_FromString(startup -> icon().latin1())); + PyList_Append(pList, PyBytes_FromString(startup -> icon().latin1())); } else { - PyList_Append(pList, PyString_FromString("")); + PyList_Append(pList, PyBytes_FromString("")); } //Executable Name if (startup -> bin() != NULL) { - PyList_Append(pList, PyString_FromString(startup -> bin().latin1())); + PyList_Append(pList, PyBytes_FromString(startup -> bin().latin1())); } else { - PyList_Append(pList, PyString_FromString("")); + PyList_Append(pList, PyBytes_FromString("")); } // a reference back to itself - PyList_Append(pList, PyInt_FromLong((long) startup)); + PyList_Append(pList, PyLong_FromLong((long) startup)); return pList; @@ -300,7 +300,7 @@ PyObject* getTaskNames(long widget) const char* tmp = task->name().latin1(); if(tmp == 0) continue; - pString = PyString_FromString(tmp); + pString = PyBytes_FromString(tmp); if(pString) PyList_Append(pList, pString); } @@ -329,7 +329,7 @@ PyObject* getTaskList(long widget) Task* task; for (task = taskList.first(); task; task = taskList.next()) { - pString = PyInt_FromLong((long)task); + pString = PyLong_FromLong((long)task); PyList_Append(pList, pString); } return pList; @@ -358,7 +358,7 @@ PyObject* getStartupList(long widget) for (startup = startupList.first(); startup; startup = startupList.next()) { - pString = PyInt_FromLong((long) startup); + pString = PyLong_FromLong((long) startup); PyList_Append(pList, pString); } return pList; -- cgit v1.2.1