Skip to content

Commit 89737a8

Browse files
committed
Propagate exceptions during type conversion (#4911)
Release 4.2.0 generalized the use of utility functions to convert Python vector-valued objects of size 3 to the corresponding C++ script interface Vector3d or Vector3i types. Exceptions raised in these functions were not propagated to the Python interpreter, printing "Exception ignored in: 'espressomd.utils.make_Vector3d'" to the screen and leaving the call site with a fully or partially uninitialized object. This is now resolved.
1 parent 9e81648 commit 89737a8

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/python/espressomd/utils.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ cdef extern from "utils/quaternion.hpp" namespace "Utils":
105105

106106
cdef make_array_locked(Vector3d)
107107
cdef make_array_locked_vector(vector[Vector3d] v)
108-
cdef Vector3d make_Vector3d(a)
109-
cdef Vector3i make_Vector3i(a)
108+
cdef Vector3d make_Vector3d(a) except *
109+
cdef Vector3i make_Vector3i(a) except *
110110

111111
cdef extern from "utils/Factory.hpp" namespace "Utils":
112112
cdef cppclass Factory[T]:

src/python/espressomd/utils.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,17 @@ cdef make_array_locked_vector(vector[Vector3d] v):
241241
return array_locked(ret)
242242

243243

244-
cdef Vector3d make_Vector3d(a):
244+
cdef Vector3d make_Vector3d(a) except *:
245245
cdef Vector3d v
246+
assert len(a) == 3
246247
for i, ai in enumerate(a):
247248
v[i] = ai
248249
return v
249250

250251

251-
cdef Vector3i make_Vector3i(a):
252+
cdef Vector3i make_Vector3i(a) except *:
252253
cdef Vector3i v
254+
assert len(a) == 3
253255
for i, ai in enumerate(a):
254256
v[i] = ai
255257
return v

testsuite/python/lb.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ def test_properties(self):
107107
lbf[0, 0, 0].velocity = [1, 2]
108108
with self.assertRaises(Exception):
109109
lbf[0, 1].velocity = [1, 2, 3]
110+
with self.assertRaises(TypeError):
111+
lbf.ext_force_density = 0
112+
with self.assertRaises(AssertionError):
113+
lbf.ext_force_density = [1, 2]
114+
with self.assertRaises(AssertionError):
115+
lbf.ext_force_density = [1, 2, 3, 4]
110116

111117
def test_raise_if_not_active(self):
112118
class MockLBFluid(self.lb_class):

0 commit comments

Comments
 (0)