mpy-cross: Assertion failed when compiling tests/basics/exception_chain.py.
Port, board and/or hardware
Unix on Linux/amd64
MicroPython version
MicroPython v1.26.0-preview.261.gbcc8d4ee1.dirty on 2025-06-22; linux [GCC 15.1.1] version
Reproduction
- Build mpy-cross with no special flags (
make -C mpy-cross clean && make -C mpy-cross) - Compile
tests/basics/exception_chain.pyinto native code (arch does not matter) withmpy-cross -X emit=native -march=debug tests/basics/exception_chain.py mpy-crosswill crash.
Expected behaviour
Compiling tests/basics/exception_chain.py should not crash mpy-cross.
Observed behaviour
mpy-cross crashes with:
mpy-cross: ../py/emitnative.c:2947: emit_native_raise_varargs: Assertion `n_args == 1' failed.`
Additional Information
If having a multi-arg exception is not supported for native code compilation, then an error message should be printed rather than crashing the compiler.
Code of Conduct
Yes, I agree
I think i can find how to fix this error! In this project:
...
class Circle(PhysicalObject):
def __init__(self,
pos: Pos2D,
radius: float,
mass: float,
fixed: bool = False
):
super().__init__(pos, mass, fixed)
self.radius = radius
#@micropython.native
def collision(self, other: PhysicalObject):
if isinstance(other, Circle):
# 碰撞位置:两圆心的加权中点
pos = self.pos + (other.pos - self.pos) * (self.radius / (self.radius + other.radius))
try:
dis = abs(self.pos - other.pos)
except OverflowError:
print("OverflowError: self.pos = {}, other.pos = {}, self.pos-other.pos = {}".format(self.pos, other.pos, (self.pos - other.pos)))
raise
# 若两圆心距离小于两圆半径之和,则碰撞。为优化逻辑,这里检查是否不碰撞
if dis >= (self.radius + other.radius):
return None
# 两圆心单位法向量,任意方向
normal = (self.pos - other.pos).normalize() # 注意:该向量指向self_pos
# 优化:
# 如果两圆均不固定,则按质量比,按碰撞法向移动两圆心,使两圆恰好接触
# 如果一圆固定,则移动另一圆心,使两圆恰好接触
# 穿透深度
penetration = self.radius + other.radius - dis
# 按质量分配移动量
if not self.fixed and not other.fixed: # 实验性
# 仅在阈值范围内进行碰撞处理
# 方案1: 质量大的移动少
"""
total_mass = self.mass + other.mass
self.pos += normal * (penetration * other.mass / total_mass)
other.pos -= normal * (penetration * self.mass / total_mass)
"""
# 方案2: 按半径平均分配移动量
self.pos += normal * (penetration * (other.radius / (self.radius + other.radius)))
other.pos -= normal * (penetration * (self.radius / (self.radius + other.radius)))
elif self.fixed:
other.pos = pos - normal * other.radius * 2
elif other.fixed:
self.pos = pos + normal * self.radius * 2
return CollisionPoint(pos, normal)
else:
return other.collision(self)
class Line(PhysicalObject):
def __init__(self,
pos: Pos2D,
direction: Vector2D,
mass: float,
fixed: bool = False
):
super().__init__(pos, mass, fixed)
self.direction = direction.normalize()
if self.direction == Vector2D(0, 0):
raise ValueError("Direction vector cannot be zero")
@micropython.native
def collision(self, other: PhysicalObject):
if isinstance(other, Line):
raise NotImplementedError
elif isinstance(other, Circle):
# 圆心到直线上一点p的向量
c = other.pos
p = self.pos
d = self.direction
# 投影参数t
t = d * (c - p)
# 垂足坐标
foot = p + d * t
# 从垂足指向圆心的向量
vec = c - foot
dist = abs(vec)
if dist >= other.radius:
return None
# 碰撞点:圆与直线的接触点(在圆的边界上,沿法向从圆心后退半径距离)
# 注意:直线无限长,碰撞点应该是圆心沿法向向直线移动半径距离的点
if dist == 0:
# 圆心正好在直线上,法向可以取垂直方向
normal = d.rotate90() # 垂直向量
else:
normal = vec.normalize()
# 优化:如果线是固定的,将圆的坐标设置为恰好让线接触圆的边界而不进入圆内
if self.fixed:
other.pos = foot + normal * other.radius
return CollisionPoint(foot, normal)
else:
return other.collision(self)
...
if i am not comment the Circle's @micropython.native, it will be a error:
PS H:\pgs\SimplePhysics> mpy-cross-v5 physics2d.py -O3 -march=xtensawin
Assertion failed: n_args == 1, file ../py/emitnative.c, line 2840
When i give a clear parameters like this:
...
pos = self.pos + (other.pos - self.pos) * (self.radius / (self.radius + other.radius))
try:
dis = abs(self.pos - other.pos)
except OverflowError as e:
print("OverflowError: self.pos = {}, other.pos = {}, self.pos-other.pos = {}".format(self.pos, other.pos, (self.pos - other.pos)))
raise e # this
...
There a compile successful.
This is fixed by 59180805d0370859d4d94357234895f30e39ef79. Closing as completed.