Skip to content

Commit

Permalink
Merge #3080
Browse files Browse the repository at this point in the history
3080: testsuite: add test for is_valid_type. r=jngrad a=KaiSzuttor

Currently it is possible to give ```nan``` as values to the particle properties because in python a ```nan``` is of type ```float```. This PR adds a check for ```None```, ```inf``` and ```nan``` to the ```is_valid_type``` and tests for the correct behavior.

Co-authored-by: Kai Szuttor <[email protected]>
Co-authored-by: Kai Szuttor <[email protected]>
  • Loading branch information
3 people authored Aug 22, 2019
2 parents d231cd3 + d85e112 commit 3aa57c4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/python/espressomd/particle_data.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ cdef class ParticleHandle:

def __set__(self, _pos):
cdef double mypos[3]
if np.isnan(_pos).any() or np.isinf(_pos).any():
raise ValueError("invalid particle position")
check_type_or_throw_except(
_pos, 3, float, "Position must be 3 floats")
for i in range(3):
Expand Down
3 changes: 2 additions & 1 deletion src/python/espressomd/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ def is_valid_type(value, t):
Extended checks for numpy int and float types.
"""

if value is None:
return False
if t == int:
return isinstance(value, (int, np.integer, np.long))
elif t == float:
Expand Down

0 comments on commit 3aa57c4

Please sign in to comment.