[SOLVED]mp_obj_bool_t is private which means you cannot read value of a bool

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

[SOLVED]mp_obj_bool_t is private which means you cannot read value of a bool

Post by jickster » Sun Feb 25, 2018 10:37 pm

How do you read the value of a bool?

MP_OBJ_IS_TYPE(o, &mp_type_bool)

But then how do you get the value of a bool?

You cannot cast it to mp_obj_bool_t because that is not in a header.
Last edited by jickster on Mon Feb 26, 2018 6:50 pm, edited 1 time in total.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: mp_obj_bool_t is private which means you cannot read value of a bool

Post by jickster » Mon Feb 26, 2018 6:50 pm

jickster wrote:
Sun Feb 25, 2018 10:37 pm
How do you read the value of a bool?

MP_OBJ_IS_TYPE(o, &mp_type_bool)

But then how do you get the value of a bool?

You cannot cast it to mp_obj_bool_t because that is not in a header.
https://github.com/micropython/micropython/issues/3641

In general the way to decide truthness is to use mp_obj_is_true(o). This will cover all cases in Python where you are allowed to use an object in place of a bool (eg if you pass 0 or 1).

There's also mp_obj_is_integer(o) which can be used to tell if an object is an integer or bool.

If it's really important to distinguish False and True from 0 and 1 then use the expression: (MP_OBJ_TO_PTR(o) == mp_const_true).

Post Reply