summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoctopuce dev <dev@yoctopuce.com>2025-06-30 23:28:20 +0200
committerDamien George <damien@micropython.org>2025-07-09 11:54:21 +1000
commitc4a88f2ce7da87d5f635ec25edba481917020fd8 (patch)
tree6d8c704092f58c09136d5bd41563a7c73c57e6f4
parent49159ef6b7e283681cb1c2dfe44cdf14bd397467 (diff)
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>
-rw-r--r--ports/unix/coverage.c36
-rw-r--r--py/obj.c30
-rw-r--r--py/obj.h2
-rw-r--r--py/objint_longlong.c16
-rw-r--r--tests/ports/unix/extra_coverage.py10
-rw-r--r--tests/ports/unix/extra_coverage.py.exp8
6 files changed, 102 insertions, 0 deletions
diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c
index cdab17cac..0df6bf279 100644
--- a/ports/unix/coverage.c
+++ b/ports/unix/coverage.c
@@ -488,6 +488,26 @@ static mp_obj_t extra_coverage(void) {
// mpz_set_from_float with 0 as argument
mpz_set_from_float(&mpz, 0);
mp_printf(&mp_plat_print, "%f\n", mpz_as_float(&mpz));
+
+ // convert a large integer value (stored in a mpz) to mp_uint_t and to ll;
+ mp_obj_t obj_bigint = mp_obj_new_int_from_uint((mp_uint_t)0xdeadbeef);
+ mp_printf(&mp_plat_print, "%x\n", mp_obj_get_uint(obj_bigint));
+ obj_bigint = mp_obj_new_int_from_ll(0xc0ffee777c0ffeell);
+ long long value_ll = mp_obj_get_ll(obj_bigint);
+ mp_printf(&mp_plat_print, "%x%08x\n", (uint32_t)(value_ll >> 32), (uint32_t)value_ll);
+
+ // convert a large integer value (stored via a struct object) to uint and to ll
+ // `deadbeef` global is an uctypes.struct defined by extra_coverage.py
+ obj_bigint = mp_load_global(MP_QSTR_deadbeef);
+ mp_printf(&mp_plat_print, "%x\n", mp_obj_get_uint(obj_bigint));
+ value_ll = mp_obj_get_ll(obj_bigint);
+ mp_printf(&mp_plat_print, "%x%08x\n", (uint32_t)(value_ll >> 32), (uint32_t)value_ll);
+
+ // convert a smaller integer value to mp_uint_t and to ll
+ obj_bigint = mp_obj_new_int_from_uint(0xc0ffee);
+ mp_printf(&mp_plat_print, "%x\n", mp_obj_get_uint(obj_bigint));
+ value_ll = mp_obj_get_ll(obj_bigint);
+ mp_printf(&mp_plat_print, "%x%08x\n", (uint32_t)(value_ll >> 32), (uint32_t)value_ll);
}
// runtime utils
@@ -530,6 +550,22 @@ static mp_obj_t extra_coverage(void) {
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
}
+ // mp_obj_get_uint from a non-int object (should raise exception)
+ if (nlr_push(&nlr) == 0) {
+ mp_obj_get_uint(mp_const_none);
+ nlr_pop();
+ } else {
+ mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
+ }
+
+ // mp_obj_int_get_ll from a non-int object (should raise exception)
+ if (nlr_push(&nlr) == 0) {
+ mp_obj_get_ll(mp_const_none);
+ nlr_pop();
+ } else {
+ mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
+ }
+
// call mp_obj_new_exception_args (it's a part of the public C API and not used in the core)
mp_obj_print_exception(&mp_plat_print, mp_obj_new_exception_args(&mp_type_ValueError, 0, NULL));
}
diff --git a/py/obj.c b/py/obj.c
index 1606ad520..586759460 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -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);
diff --git a/py/obj.h b/py/obj.h
index 0f87282a9..a1df661ff 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -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
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index 1940b8153..5b60eb65a 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -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));
diff --git a/tests/ports/unix/extra_coverage.py b/tests/ports/unix/extra_coverage.py
index ec68a5550..72f5fe56b 100644
--- a/tests/ports/unix/extra_coverage.py
+++ b/tests/ports/unix/extra_coverage.py
@@ -6,6 +6,16 @@ except NameError:
import errno
import io
+import uctypes
+
+# create an int-like variable used for coverage of `mp_obj_get_ll`
+buf = bytearray(b"\xde\xad\xbe\xef")
+struct = uctypes.struct(
+ uctypes.addressof(buf),
+ {"f32": uctypes.UINT32 | 0},
+ uctypes.BIG_ENDIAN,
+)
+deadbeef = struct.f32
data = extra_coverage()
diff --git a/tests/ports/unix/extra_coverage.py.exp b/tests/ports/unix/extra_coverage.py.exp
index ed21ced24..00658ab3a 100644
--- a/tests/ports/unix/extra_coverage.py.exp
+++ b/tests/ports/unix/extra_coverage.py.exp
@@ -94,6 +94,12 @@ data
1
0
0.000000
+deadbeef
+c0ffee777c0ffee
+deadbeef
+0deadbeef
+c0ffee
+000c0ffee
# runtime utils
TypeError: unsupported type for __abs__: 'str'
TypeError: unsupported types for __divmod__: 'str', 'str'
@@ -102,6 +108,8 @@ TypeError: unsupported types for __divmod__: 'str', 'str'
2
OverflowError: overflow converting long int to machine word
OverflowError: overflow converting long int to machine word
+TypeError: can't convert NoneType to int
+TypeError: can't convert NoneType to int
ValueError:
Warning: test
# format float