py/obj: Add functions to retrieve large integers from mp_obj_t.

This commit provides helpers to retrieve integer values from
mp_obj_t when the content does not fit in a 32 bits integer,
without risking an implicit wrap due to an int overflow.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
This commit is contained in:
Yoctopuce dev
2025-06-30 23:28:20 +02:00
committed by Damien George
parent 49159ef6b7
commit c4a88f2ce7
6 changed files with 102 additions and 0 deletions

View File

@@ -314,6 +314,36 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
return val;
}
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
mp_uint_t mp_obj_get_uint(mp_const_obj_t arg) {
if (!mp_obj_is_exact_type(arg, &mp_type_int)) {
mp_obj_t as_int = mp_unary_op(MP_UNARY_OP_INT_MAYBE, (mp_obj_t)arg);
if (as_int == MP_OBJ_NULL) {
mp_raise_TypeError_int_conversion(arg);
}
arg = as_int;
}
return mp_obj_int_get_uint_checked(arg);
}
long long mp_obj_get_ll(mp_const_obj_t arg) {
if (!mp_obj_is_exact_type(arg, &mp_type_int)) {
mp_obj_t as_int = mp_unary_op(MP_UNARY_OP_INT_MAYBE, (mp_obj_t)arg);
if (as_int == MP_OBJ_NULL) {
mp_raise_TypeError_int_conversion(arg);
}
arg = as_int;
}
if (mp_obj_is_small_int(arg)) {
return MP_OBJ_SMALL_INT_VALUE(arg);
} else {
long long res;
mp_obj_int_to_bytes_impl((mp_obj_t)arg, MP_ENDIANNESS_BIG, sizeof(res), (byte *)&res);
return res;
}
}
#endif
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
if (mp_obj_is_int(arg)) {
return mp_obj_int_get_truncated(arg);

View File

@@ -1051,6 +1051,8 @@ static inline bool mp_obj_is_integer(mp_const_obj_t o) {
}
mp_int_t mp_obj_get_int(mp_const_obj_t arg);
mp_uint_t mp_obj_get_uint(mp_const_obj_t arg);
long long mp_obj_get_ll(mp_const_obj_t arg);
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
#if MICROPY_PY_BUILTINS_FLOAT

View File

@@ -295,6 +295,22 @@ mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
return mp_obj_int_get_truncated(self_in);
}
mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in) {
if (mp_obj_is_small_int(self_in)) {
if (MP_OBJ_SMALL_INT_VALUE(self_in) >= 0) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
}
} else {
const mp_obj_int_t *self = self_in;
long long value = self->val;
mp_uint_t truncated = (mp_uint_t)value;
if (value >= 0 && (long long)truncated == value) {
return truncated;
}
}
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("overflow converting long int to machine word"));
}
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_int_as_float_impl(mp_obj_t self_in) {
assert(mp_obj_is_exact_type(self_in, &mp_type_int));