Skip to content

Commit 166bb40

Browse files
committed
Add OverflowError and use it for small int overflow instead of assert.
1 parent 8655065 commit 166bb40

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

py/mpqstrraw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Q(OSError)
3232
Q(SyntaxError)
3333
Q(TypeError)
3434
Q(ValueError)
35+
Q(OverflowError)
3536

3637
Q(abs)
3738
Q(all)

py/objint.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
5656

5757
// This is called only with strings whose value doesn't fit in SMALL_INT
5858
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
59-
assert(0);
59+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "long int not supported in this build"));
6060
return mp_const_none;
6161
}
6262

@@ -66,17 +66,15 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
6666
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
6767
return MP_OBJ_NEW_SMALL_INT(value);
6868
}
69-
// TODO: Raise exception
70-
assert(0);
69+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
7170
return mp_const_none;
7271
}
7372

7473
mp_obj_t mp_obj_new_int(machine_int_t value) {
7574
if (MP_OBJ_FITS_SMALL_INT(value)) {
7675
return MP_OBJ_NEW_SMALL_INT(value);
7776
}
78-
// TODO: Raise exception
79-
assert(0);
77+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
8078
return mp_const_none;
8179
}
8280
#endif

py/runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ void rt_init(void) {
8989
mp_map_add_qstr(&map_builtins, MP_QSTR_TypeError, mp_obj_new_exception(MP_QSTR_TypeError));
9090
mp_map_add_qstr(&map_builtins, MP_QSTR_SyntaxError, mp_obj_new_exception(MP_QSTR_SyntaxError));
9191
mp_map_add_qstr(&map_builtins, MP_QSTR_ValueError, mp_obj_new_exception(MP_QSTR_ValueError));
92+
// Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
93+
// TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
94+
mp_map_add_qstr(&map_builtins, MP_QSTR_OverflowError, mp_obj_new_exception(MP_QSTR_OverflowError));
9295
mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
9396
mp_map_add_qstr(&map_builtins, MP_QSTR_AssertionError, mp_obj_new_exception(MP_QSTR_AssertionError));
9497

0 commit comments

Comments
 (0)