Skip to content

Commit 9dcc60d

Browse files
committed
py: Detect ZeroDivisionError properly for floats.
1 parent 212f89e commit 9dcc60d

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

py/objfloat.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,18 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
118118
// TODO: verify that C floor matches Python semantics
119119
case MP_BINARY_OP_FLOOR_DIVIDE:
120120
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
121+
if (rhs_val == 0) {
122+
zero_division_error:
123+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
124+
}
121125
lhs_val = MICROPY_FLOAT_C_FUN(floor)(lhs_val / rhs_val);
122-
goto check_zero_division;
126+
break;
123127
case MP_BINARY_OP_TRUE_DIVIDE:
124-
case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
125-
lhs_val /= rhs_val;
126-
check_zero_division:
127-
if (isinf(lhs_val)){ // check for division by zero
128-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
128+
case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
129+
if (rhs_val == 0) {
130+
goto zero_division_error;
129131
}
132+
lhs_val /= rhs_val;
130133
break;
131134
case MP_BINARY_OP_POWER:
132135
case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break;

0 commit comments

Comments
 (0)