summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/karamba_python.cpp
diff options
context:
space:
mode:
authorDavid C. Rankin <trin@3111skyline.com>2014-09-27 11:36:51 -0500
committerTimothy Pearson <kb9vqf@pearsoncomputing.net>2014-09-27 11:36:51 -0500
commit36230c0b5d872388449b20d46d39a9e66a017714 (patch)
treeb6f776f011ca59208d33d314ccb8ffdea65a91a8 /superkaramba/src/karamba_python.cpp
parenta594faa6526f7b3259319bc706d88aacd0fd0286 (diff)
downloadtdeutils-36230c0b5d872388449b20d46d39a9e66a017714.tar.gz
tdeutils-36230c0b5d872388449b20d46d39a9e66a017714.zip
Add Python >= v3 support to SuperKaramba
Diffstat (limited to 'superkaramba/src/karamba_python.cpp')
-rw-r--r--superkaramba/src/karamba_python.cpp57
1 files changed, 55 insertions, 2 deletions
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);