@@ -67,10 +67,10 @@ const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_
67
67
} else if (mp_obj_is_obj (o_in )) {
68
68
const mp_obj_base_t * o = MP_OBJ_TO_PTR (o_in );
69
69
return o -> type ;
70
- #if MICROPY_PY_BUILTINS_FLOAT
70
+ #if MICROPY_PY_BUILTINS_FLOAT
71
71
} else if ((((mp_uint_t )(o_in )) & 0xff800007 ) != 0x00000006 ) {
72
72
return & mp_type_float ;
73
- #endif
73
+ #endif
74
74
} else {
75
75
static const mp_obj_type_t * const types [] = {
76
76
& mp_type_str , & mp_type_NoneType , & mp_type_str , & mp_type_bool ,
@@ -89,11 +89,11 @@ const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_
89
89
} else if (mp_obj_is_float (o_in )) {
90
90
return & mp_type_float ;
91
91
#endif
92
- #if MICROPY_OBJ_IMMEDIATE_OBJS
92
+ #if MICROPY_OBJ_IMMEDIATE_OBJS
93
93
} else if (mp_obj_is_immediate_obj (o_in )) {
94
94
static const mp_obj_type_t * const types [2 ] = {& mp_type_NoneType , & mp_type_bool };
95
95
return types [MP_OBJ_IMMEDIATE_OBJ_VALUE (o_in ) & 1 ];
96
- #endif
96
+ #endif
97
97
} else {
98
98
const mp_obj_base_t * o = MP_OBJ_TO_PTR (o_in );
99
99
return o -> type ;
@@ -225,20 +225,20 @@ mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2) {
225
225
if (mp_obj_is_str (o2 )) {
226
226
// both strings, use special function
227
227
return mp_obj_str_equal (o1 , o2 ) ? local_true : local_false ;
228
- #if MICROPY_PY_STR_BYTES_CMP_WARN
228
+ #if MICROPY_PY_STR_BYTES_CMP_WARN
229
229
} else if (mp_obj_is_type (o2 , & mp_type_bytes )) {
230
230
str_bytes_cmp :
231
231
mp_warning (MP_WARN_CAT (BytesWarning ), "Comparison between bytes and str" );
232
232
return local_false ;
233
- #endif
233
+ #endif
234
234
} else {
235
235
goto skip_one_pass ;
236
236
}
237
- #if MICROPY_PY_STR_BYTES_CMP_WARN
237
+ #if MICROPY_PY_STR_BYTES_CMP_WARN
238
238
} else if (mp_obj_is_str (o2 ) && mp_obj_is_type (o1 , & mp_type_bytes )) {
239
239
// o1 is not a string (else caught above), so the objects are not equal
240
240
goto str_bytes_cmp ;
241
- #endif
241
+ #endif
242
242
}
243
243
244
244
// fast path for small ints
@@ -348,14 +348,21 @@ bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
348
348
val = 1 ;
349
349
} else if (mp_obj_is_small_int (arg )) {
350
350
val = (mp_float_t )MP_OBJ_SMALL_INT_VALUE (arg );
351
- #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
351
+ #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
352
352
} else if (mp_obj_is_type (arg , & mp_type_int )) {
353
353
val = mp_obj_int_as_float_impl (arg );
354
- #endif
354
+ #endif
355
355
} else if (mp_obj_is_float (arg )) {
356
356
val = mp_obj_float_get (arg );
357
357
} else {
358
- return false;
358
+ // Call __float__() function if it exists.
359
+ mp_obj_t dest [2 ];
360
+ mp_load_method_maybe (arg , MP_QSTR___float__ , dest );
361
+ if (dest [0 ] == MP_OBJ_NULL ) {
362
+ // No conversion to float available
363
+ return false;
364
+ }
365
+ val = mp_obj_float_get (mp_call_method_n_kw (0 , 0 , dest ));
359
366
}
360
367
361
368
* value = val ;
@@ -388,11 +395,11 @@ bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag)
388
395
} else if (mp_obj_is_small_int (arg )) {
389
396
* real = (mp_float_t )MP_OBJ_SMALL_INT_VALUE (arg );
390
397
* imag = 0 ;
391
- #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
398
+ #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
392
399
} else if (mp_obj_is_type (arg , & mp_type_int )) {
393
400
* real = mp_obj_int_as_float_impl (arg );
394
401
* imag = 0 ;
395
- #endif
402
+ #endif
396
403
} else if (mp_obj_is_float (arg )) {
397
404
* real = mp_obj_float_get (arg );
398
405
* imag = 0 ;
0 commit comments