10000 py/obj.h: Add and use mp_obj_is_bool() helper. · andrewleech/micropython@d9433d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9433d3

Browse files
Jongydpgeorge
authored andcommitted
py/obj.h: Add and use mp_obj_is_bool() helper.
Commit d96cfd1 introduced a regression in testing for bool objects, that such objects were in some cases no longer recognised and bools, eg when using mp_obj_is_type(o, &mp_type_bool), or mp_obj_is_integer(o). This commit fixes that problem by adding mp_obj_is_bool(o). Builds with MICROPY_OBJ_IMMEDIATE_OBJS enabled check if the object is any of the const True or False objects. Builds without it use the old method of ->type checking, which compiles to smaller code (compared with the former mentioned method). Fixes micropython#5538.
1 parent 27f41e6 commit d9433d3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

py/obj.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,12 @@ extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
665665
// Note: these are kept as macros because inline functions sometimes use much
666666
// more code space than the equivalent macros, depending on the compiler.
667667
#define mp_obj_is_type(o, t) (mp_obj_is_obj(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type == (t))) // this does not work for checking int, str or fun; use below macros for that
668+
#if MICROPY_OBJ_IMMEDIATE_OBJS
669+
// bool's are immediates, not real objects, so test for the 2 possible values.
670+
#define mp_obj_is_bool(o) ((o) == mp_const_false || (o) == mp_const_true)
671+
#else
672+
#define mp_obj_is_bool(o) mp_obj_is_type(o, &mp_type_bool)
673+
#endif
668674
#define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_type(o, &mp_type_int))
669675
#define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_type(o, &mp_type_str))
670676
#define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && ((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op))
@@ -721,7 +727,7 @@ bool mp_obj_is_true(mp_obj_t arg);
721727
bool mp_obj_is_callable(mp_obj_t o_in);
722728
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
723729

724-
static inline bool mp_obj_is_integer(mp_const_obj_t o) { return mp_obj_is_int(o) || mp_obj_is_type(o, &mp_type_bool); } // returns true if o is bool, small int or long int
730+
static inline bool mp_obj_is_integer(mp_const_obj_t o) { return mp_obj_is_int(o) || mp_obj_is_bool(o); } // returns true if o is bool, small int or long int
725731
mp_int_t mp_obj_get_int(mp_const_obj_t arg);
726732
mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
727733
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);

py/objstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ STATIC bool istype(char ch) {
910910
}
911911

912912
STATIC bool arg_looks_integer(mp_obj_t arg) {
913-
return mp_obj_is_type(arg, &mp_type_bool) || mp_obj_is_int(arg);
913+
return mp_obj_is_bool(arg) || mp_obj_is_int(arg);
914914
}
915915

916916
STATIC bool arg_looks_numeric(mp_obj_t arg) {

0 commit comments

Comments
 (0)
0