summaryrefslogtreecommitdiff
path: root/tests/float/complex_dunder.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-07-25 15:23:48 +1000
committerDamien George <damien@micropython.org>2022-07-25 16:11:26 +1000
commit4fe3e493b1a62381db15b724f77d565ff2666120 (patch)
tree93497990188f39e0c747d1bdebce8fb170c527b2 /tests/float/complex_dunder.py
parent1e87b56219c69306d77a887cac3d29146180f113 (diff)
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 'tests/float/complex_dunder.py')
-rw-r--r--tests/float/complex_dunder.py6
1 files changed, 6 insertions, 0 deletions
diff --git a/tests/float/complex_dunder.py b/tests/float/complex_dunder.py
index 128dc6929..975d829b4 100644
--- a/tests/float/complex_dunder.py
+++ b/tests/float/complex_dunder.py
@@ -1,6 +1,11 @@
# test __complex__ function support
+class TestFloat:
+ def __float__(self):
+ return 1.0
+
+
class TestComplex:
def __complex__(self):
return 1j + 10
@@ -20,6 +25,7 @@ class Test:
pass
+print(complex(TestFloat()))
print(complex(TestComplex()))
try: