Commit 63d0a6ef authored by Guilhem Saurel's avatar Guilhem Saurel Committed by GitHub

Merge pull request #38 from nim65s/devel

explicit error in case of misuse of dg.plug, fix #36
parents f874f72c 6ff7168c
......@@ -40,10 +40,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) {
std::ostringstream oss;
oss << "dynamic_graph.plug(a, b): Argument 'b' must be of type 'dynamic_graph.Signal', but got "
<< PyCapsule_GetName(objIn);
PyErr_SetString(PyExc_TypeError, 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) {
std::ostringstream oss;
oss << "dynamic_graph.plug(a, b): Argument 'a' must be of type 'dynamic_graph.Signal', but got "
<< PyCapsule_GetName(objOut);
PyErr_SetString(PyExc_TypeError, oss.str().c_str());
return NULL;
}
std::ostringstream os;
try {
......@@ -123,6 +135,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
......
......@@ -62,7 +62,11 @@ int main(int argc, char** argv) {
res = testFile("test_python-syntax_error.py",
std::string(" File \"test_python-syntax_error.py\", line 2\n"
" hello world\n"
#if PY_MINOR_VERSION >= 8
" ^\n"
#else
" ^\n"
#endif
"SyntaxError: invalid syntax\n"),
numTest) &&
res;
......
import unittest
import dynamic_graph as dg
from custom_entity import CustomEntity
ERR = "dynamic_graph.plug(a, b): Argument '%s' must be of type 'dynamic_graph.Signal', but got dynamic_graph.Entity"
class BindingsTests(unittest.TestCase):
def test_bindings(self):
......@@ -15,10 +18,16 @@ 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(TypeError) 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), ERR % 'b')
# Check that we can't connect first to second.in
with self.assertRaises(TypeError) as cm_out:
dg.plug(first, second.signal('in_double'))
self.assertEqual(str(cm_out.exception), ERR % 'a')
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