Commit d4a89710 authored by Guilhem Saurel's avatar Guilhem Saurel

module initialization compatible with python 2 & 3

parent 2c6f3151
......@@ -72,7 +72,9 @@ extern PyObject* realTimeLoggerDestroy(PyObject* self, PyObject* args);
extern PyObject* realTimeLoggerInstance(PyObject* self, PyObject* args);
} // namespace debug
PyObject* dgpyError;
struct module_state {
PyObject* dgpyError;
};
/**
\brief plug a signal into another one.
......@@ -140,6 +142,25 @@ PyObject* enableTrace(PyObject* /*self*/, PyObject* args) {
}
return Py_BuildValue("");
}
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct dynamicgraph::python::module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&dynamicgraph::python::_state)
static struct dynamicgraph::python::module_state _state;
#endif
static PyObject *
#if PY_MAJOR_VERSION >= 3
error_out(PyObject* m, PyObject*) {
struct module_state *st = GETSTATE(m);
#else
error_out(PyObject*, PyObject*) {
struct module_state *st = &dynamicgraph::python::_state;
#endif
PyErr_SetString(st->dgpyError, "something bad happened");
return NULL;
}
} // namespace python
} // namespace dynamicgraph
......@@ -219,18 +240,53 @@ static PyMethodDef dynamicGraphMethods[] = {
"Destroy the real time logger."},
{"real_time_logger_instance", dynamicgraph::python::debug::realTimeLoggerInstance, METH_VARARGS,
"Starts the real time logger."},
{"error_out", (PyCFunction)dynamicgraph::python::error_out, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC initwrap(void) {
PyObject* m;
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"dynamic_graph",
NULL,
sizeof(struct dynamicgraph::python::module_state),
dynamicGraphMethods,
NULL,
NULL,
NULL,
NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC
PyInit_dynamic_graph(void)
m = Py_InitModule("wrap", dynamicGraphMethods);
if (m == NULL) return;
#else
#define INITERROR return
std::string msg("dynamic_graph.error");
void
initdynamic_graph(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("dynamic_graph", dynamicGraphMethods);
#endif
if (module == NULL)
INITERROR;
struct dynamicgraph::python::module_state *st = GETSTATE(module);
st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
if (st->dgpyError == NULL) {
Py_DECREF(module);
INITERROR;
}
dynamicgraph::python::dgpyError = PyErr_NewException(const_cast<char*>(msg.c_str()), NULL, NULL);
Py_INCREF(dynamicgraph::python::dgpyError);
PyModule_AddObject(m, "error", dynamicgraph::python::dgpyError);
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment