diff options
author | Damien George <damien@micropython.org> | 2022-07-25 15:23:48 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2022-07-25 16:11:26 +1000 |
commit | 4fe3e493b1a62381db15b724f77d565ff2666120 (patch) | |
tree | 93497990188f39e0c747d1bdebce8fb170c527b2 /py/objcomplex.c | |
parent | 1e87b56219c69306d77a887cac3d29146180f113 (diff) | |
download | micropython-4fe3e493b1a62381db15b724f77d565ff2666120.tar.gz micropython-4fe3e493b1a62381db15b724f77d565ff2666120.zip |
py/obj: Make mp_obj_get_complex_maybe call mp_obj_get_float_maybe first.
This commit simplifies mp_obj_get_complex_maybe() by first calling
mp_obj_get_float_maybe() to handle the cases corresponding to floats.
Only if that fails does it attempt to extra a full complex number.
This reduces code size and also means that mp_obj_get_complex_maybe() now
supports user-defined classes defining __float__; in particular this allows
user-defined classes to be used as arguments to cmath-module function.
Furthermore, complex_make_new() can now be simplified to directly call
mp_obj_get_complex(), instead of mp_obj_get_complex_maybe() followed by
mp_obj_get_float(). This also improves error messages from complex with
an invalid argument, it now raises "can't convert <type> to complex" rather
than "can't convert <type> to float".
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/objcomplex.c')
-rw-r--r-- | py/objcomplex.c | 7 |
1 files changed, 2 insertions, 5 deletions
diff --git a/py/objcomplex.c b/py/objcomplex.c index 3c4cb66140..4aa598a0bc 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -89,11 +89,8 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si return args[0]; } else { mp_float_t real, imag; - if (mp_obj_get_complex_maybe(args[0], &real, &imag)) { - return mp_obj_new_complex(real, imag); - } - // something else, try to cast it to a complex - return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); + mp_obj_get_complex(args[0], &real, &imag); + return mp_obj_new_complex(real, imag); } case 2: |