diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 83cfbbcc5..b8f68bf8c 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -571,7 +571,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof /// captured by reference (and thus memory pointed by bytearray may change /// or become invalid at later time). Use bytes_at() to capture by value. mp_obj_t uctypes_struct_bytearray_at(mp_obj_t ptr, mp_obj_t size) { - return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr)); + return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)mp_obj_int_get_truncated(ptr)); } MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytearray_at); @@ -580,7 +580,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytear /// captured by value, i.e. copied. Use bytearray_at() to capture by reference /// ("zero copy"). mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) { - return mp_obj_new_bytes((void*)mp_obj_int_get(ptr), mp_obj_int_get(size)); + return mp_obj_new_bytes((void*)mp_obj_int_get_truncated(ptr), mp_obj_int_get_truncated(size)); } MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at); diff --git a/extmod/modure.c b/extmod/modure.c index eb010faf9..910166249 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -62,7 +62,7 @@ STATIC void match_print(void (*print)(void *env, const char *fmt, ...), void *en STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) { mp_obj_match_t *self = self_in; - mp_int_t no = mp_obj_int_get(no_in); + mp_int_t no = mp_obj_int_get_truncated(no_in); if (no < 0 || no >= self->num_matches / 2) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in)); } @@ -130,7 +130,7 @@ STATIC mp_obj_t re_split(uint n_args, const mp_obj_t *args) { int maxsplit = 0; if (n_args > 2) { - maxsplit = mp_obj_int_get(args[2]); + maxsplit = mp_obj_int_get_truncated(args[2]); } mp_obj_t retval = mp_obj_new_list(0, NULL); diff --git a/py/binary.c b/py/binary.c index 46a4eb694..e4dac31e9 100644 --- a/py/binary.c +++ b/py/binary.c @@ -252,7 +252,12 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte ** val = (mp_uint_t)val_in; break; default: - val = mp_obj_get_int(val_in); + // we handle large ints here by calling the truncated accessor + if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) { + val = mp_obj_int_get_truncated(val_in); + } else { + val = mp_obj_get_int(val_in); + } } mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, val); diff --git a/py/obj.c b/py/obj.c index e58149588..c869a6546 100644 --- a/py/obj.c +++ b/py/obj.c @@ -179,7 +179,7 @@ mp_int_t mp_obj_hash(mp_obj_t o_in) { if (hash_method[0] != MP_OBJ_NULL) { mp_obj_t hash_val = mp_call_method_n_kw(0, 0, hash_method); if (MP_OBJ_IS_INT(hash_val)) { - return mp_obj_int_get(hash_val); + return mp_obj_int_get_truncated(hash_val); } } } diff --git a/py/obj.h b/py/obj.h index febff67c2..3721bea92 100644 --- a/py/obj.h +++ b/py/obj.h @@ -460,12 +460,12 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj); // int // For long int, returns value truncated to mp_int_t -mp_int_t mp_obj_int_get(mp_const_obj_t self_in); +mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in); +// Will raise exception if value doesn't fit into mp_int_t +mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in); #if MICROPY_PY_BUILTINS_FLOAT mp_float_t mp_obj_int_as_float(mp_obj_t self_in); #endif -// Will raise exception if value doesn't fit into mp_int_t -mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in); // exception #define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new) diff --git a/py/objint.c b/py/objint.c index 8e048ddec..7f0e2b250 100644 --- a/py/objint.c +++ b/py/objint.c @@ -270,7 +270,7 @@ mp_obj_t mp_obj_new_int(mp_int_t value) { return mp_const_none; } -mp_int_t mp_obj_int_get(mp_const_obj_t self_in) { +mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) { return MP_OBJ_SMALL_INT_VALUE(self_in); } diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 8d47308b0..ec55c7784 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -198,7 +198,7 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, return o; } -mp_int_t mp_obj_int_get(mp_const_obj_t self_in) { +mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) { if (MP_OBJ_IS_SMALL_INT(self_in)) { return MP_OBJ_SMALL_INT_VALUE(self_in); } else { @@ -209,7 +209,7 @@ mp_int_t mp_obj_int_get(mp_const_obj_t self_in) { mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) { // TODO: Check overflow - return mp_obj_int_get(self_in); + return mp_obj_int_get_truncated(self_in); } #if MICROPY_PY_BUILTINS_FLOAT diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 7eff54c53..5480bd385 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -305,12 +305,12 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, return o; } -mp_int_t mp_obj_int_get(mp_const_obj_t self_in) { +mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) { if (MP_OBJ_IS_SMALL_INT(self_in)) { return MP_OBJ_SMALL_INT_VALUE(self_in); } else { const mp_obj_int_t *self = self_in; - // TODO this is a hack until we remove mp_obj_int_get function entirely + // hash returns actual int value if it fits in mp_int_t return mpz_hash(&self->mpz); } } diff --git a/py/stream.c b/py/stream.c index 810e9b349..db7e5657f 100644 --- a/py/stream.c +++ b/py/stream.c @@ -229,7 +229,7 @@ STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) { // https://docs.python.org/3/library/socket.html#socket.socket.recv_into mp_uint_t len = bufinfo.len; if (n_args > 2) { - len = mp_obj_int_get(args[2]); + len = mp_obj_int_get_truncated(args[2]); if (len > bufinfo.len) { len = bufinfo.len; } diff --git a/stmhal/modusocket.c b/stmhal/modusocket.c index 1c0b036d3..d705636c1 100644 --- a/stmhal/modusocket.c +++ b/stmhal/modusocket.c @@ -288,7 +288,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) { const void *optval; mp_uint_t optlen; if (mp_obj_is_integer(args[3])) { - int val = mp_obj_int_get(args[3]); + mp_int_t val = mp_obj_int_get_truncated(args[3]); optval = &val; optlen = sizeof(val); } else { diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py index c3049c55d..cebffc142 100644 --- a/tests/basics/struct1.py +++ b/tests/basics/struct1.py @@ -25,3 +25,15 @@ print(struct.pack("<6sH", b"foo", 10000)) s = struct.pack("BHBI", 10, 100, 200, 300) v = struct.unpack("BHBI", s) print(v == (10, 100, 200, 300)) + +# check maximum pack on 32-bit machine +print(struct.pack("nargs, 0, pyargs); - *(ffi_arg*)ret = mp_obj_int_get(res); + *(ffi_arg*)ret = mp_obj_int_get_truncated(res); } STATIC mp_obj_t mod_ffi_callback(mp_obj_t rettype_in, mp_obj_t func_in, mp_obj_t paramtypes_in) { @@ -313,7 +313,7 @@ mp_obj_t ffifunc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const if (a == mp_const_none) { values[i] = 0; } else if (MP_OBJ_IS_INT(a)) { - values[i] = mp_obj_int_get(a); + values[i] = mp_obj_int_get_truncated(a); } else if (MP_OBJ_IS_STR(a)) { const char *s = mp_obj_str_get_str(a); values[i] = (ffi_arg)s; @@ -422,7 +422,7 @@ mp_obj_t mod_ffi_open(mp_uint_t n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_ffi_open_obj, 1, 2, mod_ffi_open); mp_obj_t mod_ffi_as_bytearray(mp_obj_t ptr, mp_obj_t size) { - return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr)); + return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)mp_obj_int_get_truncated(ptr)); } MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray); diff --git a/unix/modsocket.c b/unix/modsocket.c index 749e58328..f844d3b35 100644 --- a/unix/modsocket.c +++ b/unix/modsocket.c @@ -218,7 +218,7 @@ STATIC mp_obj_t socket_setsockopt(mp_uint_t n_args, const mp_obj_t *args) { const void *optval; socklen_t optlen; if (MP_OBJ_IS_INT(args[3])) { - int val = mp_obj_int_get(args[3]); + int val = mp_obj_int_get_truncated(args[3]); optval = &val; optlen = sizeof(val); } else {