Skip to content

Commit 632cf57

Browse files
committed
Merge branch 'master' of github.com:dpgeorge/micropython
2 parents 20006db + d26b379 commit 632cf57

6 files changed

Lines changed: 31 additions & 6 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/obj.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ machine_int_t mp_obj_get_int(mp_obj_t arg) {
155155
return 1;
156156
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
157157
return MP_OBJ_SMALL_INT_VALUE(arg);
158+
} else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
159+
return mp_obj_int_get_checked(arg);
158160
#if MICROPY_ENABLE_FLOAT
159161
} else if (MP_OBJ_IS_TYPE(arg, &float_type)) {
160162
// TODO work out if this should be floor, ceil or trunc

py/obj.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
261261

262262
// int
263263
extern const mp_obj_type_t int_type;
264+
// For long int, returns value truncated to machine_int_t
265+
machine_int_t mp_obj_int_get(mp_obj_t self_in);
266+
// Will rains exception if value doesn't fit into machine_int_t
267+
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
264268

265269
// exception
266270
extern const mp_obj_type_t exception_type;

py/objint.c

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

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

@@ -68,17 +68,24 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
6868
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
6969
return MP_OBJ_NEW_SMALL_INT(value);
7070
}
71-
// TODO: Raise exception
72-
assert(0);
71+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
7372
return mp_const_none;
7473
}
7574

7675
mp_obj_t mp_obj_new_int(machine_int_t value) {
7776
if (MP_OBJ_FITS_SMALL_INT(value)) {
7877
return MP_OBJ_NEW_SMALL_INT(value);
7978
}
80-
// TODO: Raise exception
81-
assert(0);
79+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OverflowError, "small int overflow"));
8280
return mp_const_none;
8381
}
82+
83+
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
84+
return MP_OBJ_SMALL_INT_VALUE(self_in);
85+
}
86+
87+
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
88+
return MP_OBJ_SMALL_INT_VALUE(self_in);
89+
}
90+
8491
#endif

py/objint_longlong.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,17 @@ mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
120120
return o;
121121
}
122122

123-
machine_int_t mp_obj_int_get_int(mp_obj_t self_in) {
123+
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
124+
if (MP_OBJ_IS_SMALL_INT(self_in)) {
125+
return MP_OBJ_SMALL_INT_VALUE(self_in);
126+
}
124127
mp_obj_int_t *self = self_in;
125128
return self->val;
126129
}
127130

131+
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
132+
// TODO: Check overflow
133+
return mp_obj_int_get(self_in);
134+
}
135+
128136
#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)