forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
py/obj: Add support for __float__ and __complex__ functions.
- Loading branch information
Showing
9 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
categories: Types,float | ||
description: uPy allows implicit conversion of objects in maths operations while CPython does not. | ||
cause: Unknown | ||
workaround: Objects should be wrapped in `float(obj)` for compatibility with CPython. | ||
""" | ||
|
||
|
||
class Test: | ||
def __float__(self): | ||
return 0.5 | ||
|
||
|
||
print(2.0 * Test()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# test __complex__ function support | ||
|
||
|
||
class TestComplex: | ||
def __complex__(self): | ||
return 1j + 10 | ||
|
||
|
||
class TestStrComplex: | ||
def __complex__(self): | ||
return "a" | ||
|
||
|
||
class TestNonComplex: | ||
def __complex__(self): | ||
return 6 | ||
|
||
|
||
class Test: | ||
pass | ||
|
||
|
||
print(complex(TestComplex())) | ||
|
||
try: | ||
print(complex(TestStrComplex())) | ||
except TypeError: | ||
print("TypeError") | ||
|
||
|
||
try: | ||
print(complex(TestNonComplex())) | ||
except TypeError: | ||
print("TypeError") | ||
|
||
|
||
try: | ||
print(complex(Test())) | ||
except TypeError: | ||
print("TypeError") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters