Skip to content

py/obj: Add support for __float__ converter function. #8857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions py/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,22 @@ bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
} else if (mp_obj_is_float(arg)) {
val = mp_obj_float_get(arg);
} else {
return false;
// Call __float__() function if it exists.
mp_obj_t dest[2];
mp_load_method_maybe(arg, MP_QSTR___float__, dest);
if (dest[0] != MP_OBJ_NULL) {
arg = mp_call_method_n_kw(0, 0, dest);
if (mp_obj_is_float(arg)) {
val = mp_obj_float_get(arg);
} else {
// __float__ returned non-float value
return false;
}
} else {
// No conversion to float available
return false;
}
}

*value = val;
return true;
}
Expand Down Expand Up @@ -399,7 +412,20 @@ bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag)
} else if (mp_obj_is_type(arg, &mp_type_complex)) {
mp_obj_complex_get(arg, real, imag);
} else {
return false;
// Call __complex__() function if it exists.
mp_obj_t dest[2];
mp_load_method_maybe(arg, MP_QSTR___complex__, dest);
if (dest[0] == MP_OBJ_NULL) {
// No conversion to complex available
return false;
}
arg = mp_call_method_n_kw(0, 0, dest);
if (mp_obj_is_type(arg, &mp_type_complex)) {
mp_obj_complex_get(arg, real, imag);
} else {
// __complex__ returned non-complex value
return false;
}
}
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions py/objcomplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si
// a complex, just return it
return args[0];
} else {
mp_float_t real, imag;
if (mp_obj_get_complex_maybe(args[0], &real, &imag)) {
return mp_obj_new_complex(real, imag);
}
// something else, try to cast it to a complex
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/float/complex_dunder.py
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")
40 changes: 40 additions & 0 deletions tests/float/float_dunder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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()))

try:
print(float(TestStrFloat()))
except TypeError:
print("TypeError")


try:
print(float(TestNonFloat()))
except TypeError:
print("TypeError")


try:
print(float(Test()))
except TypeError:
print("TypeError")