Skip to content

Commit aaef185

Browse files
committed
py: Add mp_obj_is_float function (macro) and use it where appropriate.
1 parent 60401d4 commit aaef185

File tree

8 files changed

+12
-11
lines changed

8 files changed

+12
-11
lines changed

py/modbuiltins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
9393
if (0) {
9494
// dummy
9595
#if MICROPY_PY_BUILTINS_FLOAT
96-
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
96+
} else if (mp_obj_is_float(o_in)) {
9797
mp_float_t value = mp_obj_float_get(o_in);
9898
// TODO check for NaN etc
9999
if (value < 0) {

py/obj.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
268268
return MP_OBJ_SMALL_INT_VALUE(arg);
269269
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
270270
return mp_obj_int_as_float(arg);
271-
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
271+
} else if (mp_obj_is_float(arg)) {
272272
return mp_obj_float_get(arg);
273273
} else {
274274
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
@@ -295,7 +295,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
295295
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
296296
*real = mp_obj_int_as_float(arg);
297297
*imag = 0;
298-
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
298+
} else if (mp_obj_is_float(arg)) {
299299
*real = mp_obj_float_get(arg);
300300
*imag = 0;
301301
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ typedef struct _mp_obj_float_t {
568568
mp_obj_base_t base;
569569
mp_float_t value;
570570
} mp_obj_float_t;
571+
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
571572
mp_float_t mp_obj_float_get(mp_obj_t self_in);
572573
mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
573574

py/objfloat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
7272
mp_uint_t l;
7373
const char *s = mp_obj_str_get_data(args[0], &l);
7474
return mp_parse_num_decimal(s, l, false, false, NULL);
75-
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
75+
} else if (mp_obj_is_float(args[0])) {
7676
// a float, just return it
7777
return args[0];
7878
} else {
@@ -121,7 +121,7 @@ mp_obj_t mp_obj_new_float(mp_float_t value) {
121121
}
122122

123123
mp_float_t mp_obj_float_get(mp_obj_t self_in) {
124-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_float));
124+
assert(mp_obj_is_float(self_in));
125125
mp_obj_float_t *self = self_in;
126126
return self->value;
127127
}

py/objint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
6060
const char *s = mp_obj_str_get_data(args[0], &l);
6161
return mp_parse_num_integer(s, l, 0, NULL);
6262
#if MICROPY_PY_BUILTINS_FLOAT
63-
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
63+
} else if (mp_obj_is_float(args[0])) {
6464
return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
6565
#endif
6666
} else {

py/objint_mpz.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
178178
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
179179
zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
180180
#if MICROPY_PY_BUILTINS_FLOAT
181-
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
181+
} else if (mp_obj_is_float(rhs_in)) {
182182
return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
183183
#if MICROPY_PY_BUILTINS_COMPLEX
184184
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {

py/objstr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,15 +827,15 @@ STATIC bool arg_looks_integer(mp_obj_t arg) {
827827
STATIC bool arg_looks_numeric(mp_obj_t arg) {
828828
return arg_looks_integer(arg)
829829
#if MICROPY_PY_BUILTINS_FLOAT
830-
|| MP_OBJ_IS_TYPE(arg, &mp_type_float)
830+
|| mp_obj_is_float(arg)
831831
#endif
832832
;
833833
}
834834

835835
STATIC mp_obj_t arg_as_int(mp_obj_t arg) {
836836
#if MICROPY_PY_BUILTINS_FLOAT
837-
if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
838-
return mp_obj_new_int_from_float(mp_obj_get_float(arg));
837+
if (mp_obj_is_float(arg)) {
838+
return mp_obj_new_int_from_float(mp_obj_float_get(arg));
839839
}
840840
#endif
841841
return arg;

py/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
458458
return mp_obj_new_int(lhs_val);
459459
}
460460
#if MICROPY_PY_BUILTINS_FLOAT
461-
} else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
461+
} else if (mp_obj_is_float(rhs)) {
462462
mp_obj_t res = mp_obj_float_binary_op(op, lhs_val, rhs);
463463
if (res == MP_OBJ_NULL) {
464464
goto unsupported_op;

0 commit comments

Comments
 (0)