diff options
Diffstat (limited to 'superkaramba/src/karamba_python.cpp')
-rw-r--r-- | superkaramba/src/karamba_python.cpp | 57 |
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); |