Skip to content

Commit ef9e787

Browse files
committed
Dont allow on_collision(None, 1). Better assert for non-inf mass.
1 parent b4a0690 commit ef9e787

File tree

7 files changed

+15
-8
lines changed

7 files changed

+15
-8
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Other improvements:
9494
GCed.
9595
- Improved documentation in many places (``Vec2d``, ``Poly``, ``Shape`` and
9696
more)
97+
- Improved assert for ``body.mass`` sanity check.
9798
- Internal cleanup of code
9899

99100

benchmarks/pymunk-collision-callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
b = pymunk.Body(1,10)
99
c = pymunk.Circle(b, 5)
1010
s.add(b, c)
11-
h = s.add_collision_handler(None, None)
11+
h = s.on_collision(None, None)
1212
def f(arb, s, data):
1313
return False
1414
h.pre_solve = f

pymunk/body.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ def mass(self) -> float:
252252

253253
@mass.setter
254254
def mass(self, mass: float) -> None:
255-
assert (
256-
self.space is None or mass > 0
255+
assert self.space is None or 0 < mass < float(
256+
"inf"
257257
), "Dynamic bodies must have mass > 0 if they are attached to a Space."
258258
lib.cpBodySetMass(self._body, mass)
259259

pymunk/space.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ def on_collision(
605605
normally.
606606
607607
Its possible to pass in None for one or both of the collision types.
608-
None matches any collision type on a Shape.
608+
None matches any collision type on a Shape. However, if
609+
collision_type_a is None, then collision_type_b must also be None.
609610
610611
If you call this multiple times with the same combination of
611612
collision_type_a and collision_type_b, then the last call will
@@ -642,8 +643,9 @@ def on_collision(
642643
# key = min(collision_type_a, collision_type_b), max(
643644
# collision_type_a, collision_type_b
644645
# )
645-
if collision_type_a == None and collision_type_b != None:
646-
collision_type_b, collision_type_a = collision_type_a, collision_type_b
646+
assert (
647+
collision_type_a != None or collision_type_b == None
648+
), "collision_type_a can not be None if collision_type_b is not None. Please swap them."
647649

648650
key = collision_type_a, collision_type_b
649651
if key not in self._handlers:

pymunk/tests/test_body.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ def test_mass(self) -> None:
181181
with self.assertRaises(AssertionError):
182182
b.mass = 0
183183

184+
with self.assertRaises(AssertionError):
185+
b.mass = float("inf")
186+
184187
s.remove(b)
185188
b.mass = 0
186189
s.add(b)

pymunk/tests/test_space.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,8 @@ def pre_solve(arb: p.Arbiter, space: p.Space, data: Any) -> None:
914914
d["space"] = space # type: ignore
915915

916916
s.on_collision(1, None, pre_solve=pre_solve)
917-
s.on_collision(None, 1, pre_solve=pre_solve)
917+
with self.assertRaises(AssertionError):
918+
s.on_collision(None, 1, pre_solve=pre_solve)
918919

919920
s.step(0.1)
920921

0 commit comments

Comments
 (0)