py/obj: Add debug-only runtime checks to mp_obj_is_type().

Zero effect on non debug builds, and also usually optimized out even in
debug builds as mp_obj_is_type() is called with a compile-time known type.
I'm not sure we even have dynamic uses of mp_obj_is_type() at the moment,
but if we ever will they will be protected from now on.

Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
This commit is contained in:
Yonatan Goldschmidt 2020-01-22 22:21:20 +01:00 committed by Damien George
parent 2a6ba47110
commit a8d78cc398
1 changed files with 6 additions and 6 deletions

View File

@ -750,12 +750,12 @@ void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
// Type checks are split to a separate, constant result macro. This is so it doesn't hinder the compilers's
// optimizations (other tricks like using ({ expr; exper; }) or (exp, expr, expr) in mp_obj_is_type() result
// in missed optimizations)
#define mp_type_assert_not_bool_int_str_nonetype(t) ( \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_bool), \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_int), \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_str), \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_NoneType), \
1)
#define mp_type_assert_not_bool_int_str_nonetype(t) ( \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_bool), assert((t) != &mp_type_bool), \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_int), assert((t) != &mp_type_int), \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_str), assert((t) != &mp_type_str), \
MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_NoneType), assert((t) != &mp_type_NoneType), \
1)
#define mp_obj_is_type(o, t) (mp_type_assert_not_bool_int_str_nonetype(t) && mp_obj_is_exact_type(o, t))
#if MICROPY_OBJ_IMMEDIATE_OBJS