summaryrefslogtreecommitdiff
path: root/ports/unix/coverage.c
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 /ports/unix/coverage.c
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>
Diffstat (limited to 'ports/unix/coverage.c')
-rw-r--r--ports/unix/coverage.c36
1 files changed, 36 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));
}