Skip to content

Commit 300c8bd

Browse files
committed
Added ZeroDivisionError to float division.
1 parent a2f2f73 commit 300c8bd

5 files changed

Lines changed: 11 additions & 2 deletions

File tree

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ extern const mp_obj_type_t mp_type_OverflowError;
201201
extern const mp_obj_type_t mp_type_OSError;
202202
extern const mp_obj_type_t mp_type_NotImplementedError;
203203
extern const mp_obj_type_t mp_type_StopIteration;
204+
extern const mp_obj_type_t mp_type_ZeroDivisionError;
204205

205206
// Constant objects, globally accessible
206207

py/objexcept.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ MP_DEFINE_EXCEPTION(OverflowError, BaseException)
9393
MP_DEFINE_EXCEPTION(OSError, BaseException)
9494
MP_DEFINE_EXCEPTION(NotImplementedError, BaseException)
9595
MP_DEFINE_EXCEPTION(StopIteration, BaseException)
96+
MP_DEFINE_EXCEPTION(ZeroDivisionError, BaseException)
9697

9798
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {
9899
return mp_obj_new_exception_msg_varg(exc_type, NULL);

py/objfloat.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdlib.h>
22
#include <assert.h>
3+
#include <math.h>
34

45
#include "nlr.h"
56
#include "misc.h"
@@ -105,8 +106,12 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
105106
case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
106107
*/
107108
case RT_BINARY_OP_TRUE_DIVIDE:
108-
case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: lhs_val /= rhs_val; break;
109-
109+
case RT_BINARY_OP_INPLACE_TRUE_DIVIDE:
110+
lhs_val /= rhs_val;
111+
if (isinf(lhs_val)){ // check for division by zero
112+
nlr_jump(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
113+
}
114+
break;
110115
case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
111116
case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
112117
case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Q(SyntaxError)
4747
Q(TypeError)
4848
Q(ValueError)
4949
Q(OverflowError)
50+
Q(ZeroDivisionError)
5051

5152
Q(NoneType)
5253

py/runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
158158
{ MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
159159
{ MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
160160
{ MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
161+
{ MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
161162

162163
// Extra builtins as defined by a port
163164
MICROPY_EXTRA_BUILTINS

0 commit comments

Comments
 (0)