Commit dab6fee6 authored by Guilhem Saurel's avatar Guilhem Saurel

[Exception] module not available in its definition

As PyState_AddModule is not yet called in the module definition,
PyState_FindModule will not work, so for Python 3 we need to get the
module from the methods definition to retrieve a pointer to dgpyError.

Fix #39
parent 7dc6728c
......@@ -178,12 +178,12 @@ __attribute__((unused)) static struct PyModuleDef dynamicGraphModuleDef = {
NULL,
NULL};
#define GETSTATE(m) ((struct dynamicgraph::python::module_state*)PyModule_GetState(m))
#define DGPYERROR GETSTATE(PyState_FindModule(&dynamicGraphModuleDef))->dgpyError
#define DGPYERROR(m) GETSTATE(m)->dgpyError
#define INITERROR return NULL
#else
__attribute__((unused)) static struct module_state _state;
#define GETSTATE(m) (&dynamicgraph::python::_state)
#define DGPYERROR dynamicgraph::python::_state.dgpyError
#define DGPYERROR(m) dynamicgraph::python::_state.dgpyError
#define INITERROR return
#endif
......
......@@ -7,19 +7,19 @@
/// \brief Catch all exceptions which may be sent when C++ code is
/// called.
#define CATCH_ALL_EXCEPTIONS() \
catch (const std::exception& exc) { \
PyErr_SetString(DGPYERROR, exc.what()); \
return NULL; \
} \
catch (const char* s) { \
PyErr_SetString(DGPYERROR, s); \
return NULL; \
} \
catch (...) { \
PyErr_SetString(DGPYERROR, "Unknown exception"); \
return NULL; \
} \
#define CATCH_ALL_EXCEPTIONS(m) \
catch (const std::exception& exc) { \
PyErr_SetString(DGPYERROR(m), exc.what()); \
return NULL; \
} \
catch (const char* s) { \
PyErr_SetString(DGPYERROR(m), s); \
return NULL; \
} \
catch (...) { \
PyErr_SetString(DGPYERROR(m), "Unknown exception"); \
return NULL; \
} \
struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION
......@@ -25,7 +25,7 @@ namespace debug {
std::map<std::string, ofstreamShrPtr> mapOfFiles_;
PyObject* addLoggerFileOutputStream(PyObject* /*self*/, PyObject* args) {
PyObject* addLoggerFileOutputStream(PyObject* m, PyObject* args) {
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
std::string sfilename(filename);
......@@ -38,49 +38,49 @@ PyObject* addLoggerFileOutputStream(PyObject* /*self*/, PyObject* args) {
dgRTLOG() << "Added " << filename << " as an output stream \n";
mapOfFiles_[sfilename] = ofs_shrptr;
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* closeLoggerFileOutputStream(PyObject* /*self*/, PyObject* /*args */) {
PyObject* closeLoggerFileOutputStream(PyObject* m, PyObject* /*args */) {
try {
for (std::map<std::string, ofstreamShrPtr>::iterator it = mapOfFiles_.begin(); it != mapOfFiles_.end(); ++it) {
it->second->close();
}
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* addLoggerCoutOutputStream(PyObject* /*self*/, PyObject* /*args*/) {
PyObject* addLoggerCoutOutputStream(PyObject* m, PyObject* /*args*/) {
try {
dgADD_OSTREAM_TO_RTLOG(std::cout);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* realTimeLoggerDestroy(PyObject* /*self*/, PyObject* /*args*/) {
PyObject* realTimeLoggerDestroy(PyObject* m, PyObject* /*args*/) {
try {
RealTimeLogger::destroy();
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* realTimeLoggerSpinOnce(PyObject* /*self*/, PyObject* /*args*/) {
PyObject* realTimeLoggerSpinOnce(PyObject* m, PyObject* /*args*/) {
try {
RealTimeLogger::instance().spinOnce();
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* realTimeLoggerInstance(PyObject* /*self*/, PyObject* /*args*/) {
PyObject* realTimeLoggerInstance(PyObject* m, PyObject* /*args*/) {
try {
RealTimeLogger::instance();
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
......
......@@ -17,7 +17,7 @@ namespace python {
/**
\brief plug a signal into another one.
*/
PyObject* plug(PyObject* /*self*/, PyObject* args) {
PyObject* plug(PyObject* m, PyObject* args) {
PyObject* objOut = NULL;
PyObject* objIn = NULL;
void* pObjOut;
......@@ -61,11 +61,11 @@ PyObject* plug(PyObject* /*self*/, PyObject* args) {
try {
signalIn->plug(signalOut);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* enableTrace(PyObject* /*self*/, PyObject* args) {
PyObject* enableTrace(PyObject* m, PyObject* args) {
PyObject* boolean;
char* filename = NULL;
......@@ -82,12 +82,12 @@ PyObject* enableTrace(PyObject* /*self*/, PyObject* args) {
try {
DebugTrace::openFile(filename);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
} else {
try {
DebugTrace::closeFile(filename);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
}
} else {
return NULL;
......
This diff is collapsed.
......@@ -12,20 +12,20 @@ namespace python {
namespace pool {
PyObject* writeGraph(PyObject* /*self*/, PyObject* args) {
PyObject* writeGraph(PyObject* m, PyObject* args) {
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
try {
PoolStorage::getInstance()->writeGraph(filename);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
/**
\brief Get list of entities
*/
PyObject* getEntityList(PyObject* /*self*/, PyObject* args) {
PyObject* getEntityList(PyObject* m, PyObject* args) {
if (!PyArg_ParseTuple(args, "")) return NULL;
std::vector<std::string> entityNames;
......@@ -47,7 +47,7 @@ PyObject* getEntityList(PyObject* /*self*/, PyObject* args) {
}
return Py_BuildValue("O", classTuple);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return NULL;
}
......
......@@ -57,20 +57,21 @@ PythonSignalContainer* getPythonSignalContainer() {
const std::string instanceName = "python_signals";
const std::string className = "PythonSignalContainer";
Entity* obj;
PyObject* m = PyState_FindModule(&dynamicgraph::python::dynamicGraphModuleDef);
if (PoolStorage::getInstance()->existEntity(instanceName, obj)) {
if (obj->getClassName() != className) {
std::string msg("Found an object named " + std::string(instanceName) +
",\n"
"but this object is of type " +
std::string(obj->getClassName()) + " and not " + std::string(className));
PyErr_SetString(DGPYERROR, msg.c_str());
PyErr_SetString(DGPYERROR(m), msg.c_str());
return NULL;
}
} else {
try {
obj = FactoryStorage::getInstance()->newEntity(std::string(className), std::string(instanceName));
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
}
return dynamic_cast<PythonSignalContainer*>(obj);
}
......@@ -83,7 +84,7 @@ PythonSignalContainer* getPythonSignalContainer() {
/**
\brief Create an instance of SignalWrapper
*/
PyObject* createSignalWrapper(PyObject* /*self*/, PyObject* args) {
PyObject* createSignalWrapper(PyObject* m, PyObject* args) {
PythonSignalContainer* psc = getPythonSignalContainer();
if (psc == NULL) return NULL;
......@@ -109,7 +110,7 @@ PyObject* createSignalWrapper(PyObject* /*self*/, PyObject* args) {
}
if (obj == NULL) {
PyErr_SetString(DGPYERROR, error.c_str());
PyErr_SetString(DGPYERROR(m), error.c_str());
return NULL;
}
// Register signal into the python signal container
......@@ -140,13 +141,13 @@ PyObject* getTime(PyObject* /*self*/, PyObject* args) {
return Py_BuildValue("i", time);
}
PyObject* setTime(PyObject* /*self*/, PyObject* args) {
PyObject* setTime(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
int time;
if (!PyArg_ParseTuple(args, "Oi", &object, &time)) return NULL;
if (!PyCapsule_CheckExact(object)) {
PyErr_SetString(DGPYERROR, "object should be a C object");
PyErr_SetString(DGPYERROR(m), "object should be a C object");
return NULL;
}
......@@ -157,7 +158,7 @@ PyObject* setTime(PyObject* /*self*/, PyObject* args) {
return Py_BuildValue("");
}
PyObject* display(PyObject* /*self*/, PyObject* args) {
PyObject* display(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
......@@ -170,12 +171,12 @@ PyObject* display(PyObject* /*self*/, PyObject* args) {
try {
obj->display(oss);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("s", oss.str().c_str());
}
PyObject* displayDependencies(PyObject* /*self*/, PyObject* args) {
PyObject* displayDependencies(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
int time;
......@@ -189,11 +190,11 @@ PyObject* displayDependencies(PyObject* /*self*/, PyObject* args) {
try {
obj->displayDependencies(oss, time);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("s", oss.str().c_str());
}
PyObject* getValue(PyObject* /*self*/, PyObject* args) {
PyObject* getValue(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
......@@ -274,20 +275,20 @@ PyObject* getValue(PyObject* /*self*/, PyObject* args) {
return Py_BuildValue("d", sigdouble->accessCopy());
}
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
/* Non specific signal: use a generic way. */
std::ostringstream value;
try {
signal->get(value);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
std::string valueString = value.str();
return Py_BuildValue("s", valueString.c_str());
}
PyObject* getName(PyObject* /*self*/, PyObject* args) {
PyObject* getName(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
......@@ -300,12 +301,12 @@ PyObject* getName(PyObject* /*self*/, PyObject* args) {
try {
name = signal->getName();
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("s", name.c_str());
}
PyObject* getClassName(PyObject* /*self*/, PyObject* args) {
PyObject* getClassName(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
......@@ -318,12 +319,12 @@ PyObject* getClassName(PyObject* /*self*/, PyObject* args) {
try {
signal->getClassName(name);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("s", name.c_str());
}
PyObject* setValue(PyObject* /*self*/, PyObject* args) {
PyObject* setValue(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
char* valueString = NULL;
......@@ -340,11 +341,11 @@ PyObject* setValue(PyObject* /*self*/, PyObject* args) {
try {
signal->set(value);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* recompute(PyObject* /*self*/, PyObject* args) {
PyObject* recompute(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
unsigned int time;
......@@ -356,11 +357,11 @@ PyObject* recompute(PyObject* /*self*/, PyObject* args) {
try {
signal->recompute(time);
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* unplug(PyObject* /*self*/, PyObject* args) {
PyObject* unplug(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
......@@ -371,11 +372,11 @@ PyObject* unplug(PyObject* /*self*/, PyObject* args) {
try {
signal->unplug();
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
return Py_BuildValue("");
}
PyObject* isPlugged(PyObject*, PyObject* args) {
PyObject* isPlugged(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
......@@ -387,14 +388,14 @@ PyObject* isPlugged(PyObject*, PyObject* args) {
try {
plugged = signal->isPlugged();
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
if (plugged)
return PyBool_FromLong(1);
else
return PyBool_FromLong(0);
}
PyObject* getPlugged(PyObject*, PyObject* args) {
PyObject* getPlugged(PyObject* m, PyObject* args) {
void* pointer = NULL;
PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) return NULL;
......@@ -411,7 +412,7 @@ PyObject* getPlugged(PyObject*, PyObject* args) {
throw std::runtime_error(msg);
}
}
CATCH_ALL_EXCEPTIONS();
CATCH_ALL_EXCEPTIONS(m);
// Return the pointer to the signal without destructor since the signal
// is not owned by the calling object.
return PyCapsule_New((void*)otherSignal, "dynamic_graph.Signal", NULL);
......
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