Skip to content

Commit 1e87b56

Browse files
pi-anldpgeorge
authored andcommitted
py/obj: Add support for __float__ and __complex__ functions.
1 parent fa15aed commit 1e87b56

9 files changed

Lines changed: 130 additions & 3 deletions

File tree

py/obj.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,13 @@ bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
355355
} else if (mp_obj_is_float(arg)) {
356356
val = mp_obj_float_get(arg);
357357
} else {
358-
return false;
358+
arg = mp_unary_op(MP_UNARY_OP_FLOAT_MAYBE, (mp_obj_t)arg);
359+
if (arg != MP_OBJ_NULL && mp_obj_is_float(arg)) {
360+
val = mp_obj_float_get(arg);
361+
} else {
362+
return false;
363+
}
359364
}
360-
361365
*value = val;
362366
return true;
363367
}
@@ -399,7 +403,12 @@ bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag)
399403
} else if (mp_obj_is_type(arg, &mp_type_complex)) {
400404
mp_obj_complex_get(arg, real, imag);
401405
} else {
402-
return false;
406+
arg = mp_unary_op(MP_UNARY_OP_COMPLEX_MAYBE, (mp_obj_t)arg);
407+
if (arg != MP_OBJ_NULL && mp_obj_is_type(arg, &mp_type_complex)) {
408+
mp_obj_complex_get(arg, real, imag);
409+
} else {
410+
return false;
411+
}
403412
}
404413
return true;
405414
}

py/objcomplex.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si
8888
// a complex, just return it
8989
return args[0];
9090
} else {
91+
mp_float_t real, imag;
92+
if (mp_obj_get_complex_maybe(args[0], &real, &imag)) {
93+
return mp_obj_new_complex(real, imag);
94+
}
9195
// something else, try to cast it to a complex
9296
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
9397
}

py/objtype.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
378378
[MP_UNARY_OP_INVERT] = MP_QSTR___invert__,
379379
[MP_UNARY_OP_ABS] = MP_QSTR___abs__,
380380
#endif
381+
#if MICROPY_PY_BUILTINS_FLOAT
382+
[MP_UNARY_OP_FLOAT_MAYBE] = MP_QSTR___float__,
383+
#if MICROPY_PY_BUILTINS_COMPLEX
384+
[MP_UNARY_OP_COMPLEX_MAYBE] = MP_QSTR___complex__,
385+
#endif
386+
#endif
381387
#if MICROPY_PY_SYS_GETSIZEOF
382388
[MP_UNARY_OP_SIZEOF] = MP_QSTR___sizeof__,
383389
#endif

py/runtime.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,15 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
319319
// if arg==mp_const_none.
320320
return mp_const_true;
321321
}
322+
#if MICROPY_PY_BUILTINS_FLOAT
323+
if (op == MP_UNARY_OP_FLOAT_MAYBE
324+
#if MICROPY_PY_BUILTINS_COMPLEX
325+
|| op == MP_UNARY_OP_COMPLEX_MAYBE
326+
#endif
327+
) {
328+
return MP_OBJ_NULL;
329+
}
330+
#endif
322331
// With MP_UNARY_OP_INT, mp_unary_op() becomes a fallback for mp_obj_get_int().
323332
// In this case provide a more focused error message to not confuse, e.g. chr(1.0)
324333
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE

py/runtime0.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ typedef enum {
7676
MP_UNARY_OP_HASH, // __hash__; must return a small int
7777
MP_UNARY_OP_ABS, // __abs__
7878
MP_UNARY_OP_INT, // __int__
79+
MP_UNARY_OP_FLOAT_MAYBE, // __float__
80+
MP_UNARY_OP_COMPLEX_MAYBE, // __complex__
7981
MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
8082
} mp_unary_op_t;
8183

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
categories: Types,float
3+
description: uPy allows implicit conversion of objects in maths operations while CPython does not.
4+
cause: Unknown
5+
workaround: Objects should be wrapped in `float(obj)` for compatibility with CPython.
6+
"""
7+
8+
9+
class Test:
10+
def __float__(self):
11+
return 0.5
12+
13+
14+
print(2.0 * Test())

tests/float/complex_dunder.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# test __complex__ function support
2+
3+
4+
class TestComplex:
5+
def __complex__(self):
6+
return 1j + 10
7+
8+
9+
class TestStrComplex:
10+
def __complex__(self):
11+
return "a"
12+
13+
14+
class TestNonComplex:
15+
def __complex__(self):
16+
return 6
17+
18+
19+
class Test:
20+
pass
21+
22+
23+
print(complex(TestComplex()))
24+
25+
try:
26+
print(complex(TestStrComplex()))
27+
except TypeError:
28+
print("TypeError")
29+
30+
31+
try:
32+
print(complex(TestNonComplex()))
33+
except TypeError:
34+
print("TypeError")
35+
36+
37+
try:
38+
print(complex(Test()))
39+
except TypeError:
40+
print("TypeError")

tests/float/float_dunder.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# test __float__ function support
2+
3+
4+
class TestFloat:
5+
def __float__(self):
6+
return 10.0
7+
8+
9+
class TestStrFloat:
10+
def __float__(self):
11+
return "a"
12+
13+
14+
class TestNonFloat:
15+
def __float__(self):
16+
return 6
17+
18+
19+
class Test:
20+
pass
21+
22+
23+
print("%.1f" % float(TestFloat()))
24+
print("%.1f" % TestFloat())
25+
26+
27+
try:
28+
print(float(TestStrFloat()))
29+
except TypeError:
30+
print("TypeError")
31+
32+
33+
try:
34+
print(float(TestNonFloat()))
35+
except TypeError:
36+
print("TypeError")
37+
38+
39+
try:
40+
print(float(Test()))
41+
except TypeError:
42+
print("TypeError")

tests/run-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ def run_tests(pyb, tests, args, result_dir, num_threads=1):
525525
skip_tests.add("float/int_big_float.py")
526526
skip_tests.add("float/true_value.py")
527527
skip_tests.add("float/types.py")
528+
skip_tests.add("float/complex_dunder.py")
528529

529530
if not has_coverage:
530531
skip_tests.add("cmdline/cmd_parsetree.py")

0 commit comments

Comments
 (0)