summaryrefslogtreecommitdiff
path: root/py/obj.c
diff options
context:
space:
mode:
authorAndrew Leech <andrew.leech@planetinnovation.com.au>2022-07-04 17:35:46 +1000
committerDamien George <damien@micropython.org>2022-07-25 14:23:34 +1000
commit1e87b56219c69306d77a887cac3d29146180f113 (patch)
treec2e0dd58749e4ec4644407660a73d7db3201d940 /py/obj.c
parentfa15aed0f718562871288aa174e91507a134db28 (diff)
py/obj: Add support for __float__ and __complex__ functions.
Diffstat (limited to 'py/obj.c')
-rw-r--r--py/obj.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/py/obj.c b/py/obj.c
index 51f6d85de..5a05ea58c 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -355,9 +355,13 @@ bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
} else if (mp_obj_is_float(arg)) {
val = mp_obj_float_get(arg);
} else {
- return false;
+ arg = mp_unary_op(MP_UNARY_OP_FLOAT_MAYBE, (mp_obj_t)arg);
+ if (arg != MP_OBJ_NULL && mp_obj_is_float(arg)) {
+ val = mp_obj_float_get(arg);
+ } else {
+ return false;
+ }
}
-
*value = val;
return true;
}
@@ -399,7 +403,12 @@ bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag)
} else if (mp_obj_is_type(arg, &mp_type_complex)) {
mp_obj_complex_get(arg, real, imag);
} else {
- return false;
+ arg = mp_unary_op(MP_UNARY_OP_COMPLEX_MAYBE, (mp_obj_t)arg);
+ if (arg != MP_OBJ_NULL && mp_obj_is_type(arg, &mp_type_complex)) {
+ mp_obj_complex_get(arg, real, imag);
+ } else {
+ return false;
+ }
}
return true;
}