diff --git a/src/dynamic_graph/dynamic-graph-py.cc b/src/dynamic_graph/dynamic-graph-py.cc index f4762ba008f72b4ed3454ced56c9252064862b26..fdc9319d9d43ce13a95138ee29a1980bdba80258 100644 --- a/src/dynamic_graph/dynamic-graph-py.cc +++ b/src/dynamic_graph/dynamic-graph-py.cc @@ -17,7 +17,13 @@ namespace python { /** \brief plug a signal into another one. */ -PyObject* plug(PyObject* /*self*/, PyObject* args) { +PyObject* plug( +#if PY_MAJOR_VERSION >= 3 + PyObject* m, PyObject* args +#else + PyObject*, PyObject* args +#endif + ) { PyObject* objOut = NULL; PyObject* objIn = NULL; void* pObjOut; @@ -40,10 +46,22 @@ PyObject* plug(PyObject* /*self*/, PyObject* args) { pObjIn = PyCapsule_GetPointer(objIn, "dynamic_graph.Signal"); SignalBase<int>* signalIn = (SignalBase<int>*)pObjIn; - if (signalIn == NULL) return NULL; + if (signalIn == NULL) { + struct module_state* st = GETSTATE(m); + std::ostringstream oss; + oss << "dgpy.plug in argument must be a dynamic_graph.Signal, not a " << PyCapsule_GetName(objIn); + PyErr_SetString(st->dgpyError, oss.str().c_str()); + return NULL; + } pObjOut = PyCapsule_GetPointer(objOut, "dynamic_graph.Signal"); SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut; - if (signalOut == NULL) return NULL; + if (signalOut == NULL) { + struct module_state* st = GETSTATE(m); + std::ostringstream oss; + oss << "dgpy.plug out argument must be a dynamic_graph.Signal, not a " << PyCapsule_GetName(objOut); + PyErr_SetString(st->dgpyError, oss.str().c_str()); + return NULL; + } std::ostringstream os; try { @@ -123,6 +141,14 @@ void initwrap(void) INITERROR; } + Py_XINCREF(st->dgpyError); + if (PyModule_AddObject(module, "dgpyError", st->dgpyError) < 0) { + Py_XDECREF(st->dgpyError); + Py_CLEAR(st->dgpyError); + Py_DECREF(module); + return NULL; + } + #if PY_MAJOR_VERSION >= 3 return module; #endif diff --git a/unitTesting/test_bindings.py b/unitTesting/test_bindings.py index 62c86ab0630c20c528625a643eae00748c73df6e..02b8acbb111aff4b51c4392139005291fce56add 100644 --- a/unitTesting/test_bindings.py +++ b/unitTesting/test_bindings.py @@ -1,6 +1,7 @@ import unittest import dynamic_graph as dg + from custom_entity import CustomEntity @@ -15,10 +16,18 @@ class BindingsTests(unittest.TestCase): second = CustomEntity('second_entity') # Check that we can connect first.out to second.in dg.plug(first.signal('out_double'), second.signal('in_double')) + # Check that we can't connect first.out to second - with self.assertRaises(ValueError) as cm: + with self.assertRaises(dg.dgpyError) as cm_in: dg.plug(first.signal('out_double'), second) - self.assertEqual(str(cm.exception), "PyCapsule_GetPointer called with incorrect name") + self.assertEqual(str(cm_in.exception), + "dgpy.plug in argument must be a dynamic_graph.Signal, not a dynamic_graph.Entity") + + # Check that we can't connect first to second.in + with self.assertRaises(dg.dgpyError) as cm_out: + dg.plug(first, second.signal('in_double')) + self.assertEqual(str(cm_out.exception), + "dgpy.plug out argument must be a dynamic_graph.Signal, not a dynamic_graph.Entity") if __name__ == '__main__':