Commit 834ff2f5 authored by Guilhem Saurel's avatar Guilhem Saurel

explicit error in case of misuse of dg.plug, fix #36

parent d4a210d2
......@@ -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
......
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__':
......
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