From a8d78cc39839aaf4a77bef05377c457c7ba75ead Mon Sep 17 00:00:00 2001 From: Yonatan Goldschmidt Date: Wed, 22 Jan 2020 22:21:20 +0100 Subject: [PATCH] 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 --- py/obj.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/py/obj.h b/py/obj.h index ea3d2db706..c94d09afab 100644 --- a/py/obj.h +++ b/py/obj.h @@ -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