Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 63d0a6e

Browse files
authored
Merge pull request #38 from nim65s/devel
explicit error in case of misuse of dg.plug, fix #36
2 parents f874f72 + 6ff7168 commit 63d0a6e

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/dynamic_graph/dynamic-graph-py.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,22 @@ PyObject* plug(PyObject* /*self*/, PyObject* args) {
4040

4141
pObjIn = PyCapsule_GetPointer(objIn, "dynamic_graph.Signal");
4242
SignalBase<int>* signalIn = (SignalBase<int>*)pObjIn;
43-
if (signalIn == NULL) return NULL;
43+
if (signalIn == NULL) {
44+
std::ostringstream oss;
45+
oss << "dynamic_graph.plug(a, b): Argument 'b' must be of type 'dynamic_graph.Signal', but got "
46+
<< PyCapsule_GetName(objIn);
47+
PyErr_SetString(PyExc_TypeError, oss.str().c_str());
48+
return NULL;
49+
}
4450
pObjOut = PyCapsule_GetPointer(objOut, "dynamic_graph.Signal");
4551
SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut;
46-
if (signalOut == NULL) return NULL;
52+
if (signalOut == NULL) {
53+
std::ostringstream oss;
54+
oss << "dynamic_graph.plug(a, b): Argument 'a' must be of type 'dynamic_graph.Signal', but got "
55+
<< PyCapsule_GetName(objOut);
56+
PyErr_SetString(PyExc_TypeError, oss.str().c_str());
57+
return NULL;
58+
}
4759
std::ostringstream os;
4860

4961
try {
@@ -123,6 +135,14 @@ void initwrap(void)
123135
INITERROR;
124136
}
125137

138+
Py_XINCREF(st->dgpyError);
139+
if (PyModule_AddObject(module, "dgpyError", st->dgpyError) < 0) {
140+
Py_XDECREF(st->dgpyError);
141+
Py_CLEAR(st->dgpyError);
142+
Py_DECREF(module);
143+
return NULL;
144+
}
145+
126146
#if PY_MAJOR_VERSION >= 3
127147
return module;
128148
#endif

unitTesting/interpreter-test-runfile.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ int main(int argc, char** argv) {
6262
res = testFile("test_python-syntax_error.py",
6363
std::string(" File \"test_python-syntax_error.py\", line 2\n"
6464
" hello world\n"
65+
#if PY_MINOR_VERSION >= 8
66+
" ^\n"
67+
#else
6568
" ^\n"
69+
#endif
6670
"SyntaxError: invalid syntax\n"),
6771
numTest) &&
6872
res;

unitTesting/test_bindings.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import unittest
22

33
import dynamic_graph as dg
4+
45
from custom_entity import CustomEntity
56

7+
ERR = "dynamic_graph.plug(a, b): Argument '%s' must be of type 'dynamic_graph.Signal', but got dynamic_graph.Entity"
8+
69

710
class BindingsTests(unittest.TestCase):
811
def test_bindings(self):
@@ -15,10 +18,16 @@ def test_type_check(self):
1518
second = CustomEntity('second_entity')
1619
# Check that we can connect first.out to second.in
1720
dg.plug(first.signal('out_double'), second.signal('in_double'))
21+
1822
# Check that we can't connect first.out to second
19-
with self.assertRaises(ValueError) as cm:
23+
with self.assertRaises(TypeError) as cm_in:
2024
dg.plug(first.signal('out_double'), second)
21-
self.assertEqual(str(cm.exception), "PyCapsule_GetPointer called with incorrect name")
25+
self.assertEqual(str(cm_in.exception), ERR % 'b')
26+
27+
# Check that we can't connect first to second.in
28+
with self.assertRaises(TypeError) as cm_out:
29+
dg.plug(first, second.signal('in_double'))
30+
self.assertEqual(str(cm_out.exception), ERR % 'a')
2231

2332

2433
if __name__ == '__main__':

0 commit comments

Comments
 (0)