py/obj: Add support for __float__ and __complex__ functions.

This commit is contained in:
Andrew Leech
2022-07-04 17:35:46 +10:00
committed by Damien George
parent fa15aed0f7
commit 1e87b56219
9 changed files with 130 additions and 3 deletions

View File

@@ -0,0 +1,42 @@
# test __float__ function support
class TestFloat:
def __float__(self):
return 10.0
class TestStrFloat:
def __float__(self):
return "a"
class TestNonFloat:
def __float__(self):
return 6
class Test:
pass
print("%.1f" % float(TestFloat()))
print("%.1f" % TestFloat())
try:
print(float(TestStrFloat()))
except TypeError:
print("TypeError")
try:
print(float(TestNonFloat()))
except TypeError:
print("TypeError")
try:
print(float(Test()))
except TypeError:
print("TypeError")