py/objcomplex: Add mp_obj_get_complex_maybe for use in complex bin-op.

This allows complex binary operations to fail gracefully with unsupported
operation rather than raising an exception, so that special methods work
correctly.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2020-06-22 10:21:02 +10:00
parent 41fa8b5482
commit 9f911d822e
6 changed files with 35 additions and 2 deletions

View File

@@ -57,3 +57,9 @@ for f_name, f, test_vals in functions:
if abs(real) < 1e-6:
real = 0.0
print("complex(%.5g, %.5g)" % (real, ret.imag))
# test invalid type passed to cmath function
try:
log([])
except TypeError:
print("TypeError")

View File

@@ -0,0 +1,15 @@
# test complex interacting with special methods
class A:
def __add__(self, x):
print("__add__")
return 1
def __radd__(self, x):
print("__radd__")
return 2
print(A() + 1j)
print(1j + A())