diff --git a/py/obj.h b/py/obj.h index c7ffb4619c..855b151506 100644 --- a/py/obj.h +++ b/py/obj.h @@ -256,6 +256,8 @@ mp_obj_t mp_obj_new_module(qstr module_name); mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in); const char *mp_obj_get_type_str(mp_obj_t o_in); bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo); +// Without supefluous arg checking +bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo); void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind); void mp_obj_print(mp_obj_t o, mp_print_kind_t kind); diff --git a/py/objtype.c b/py/objtype.c index 46b96c7314..c814e9c75e 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -454,16 +454,7 @@ mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj) { /******************************************************************************/ // subclassing and built-ins specific to types -bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) { - if (!MP_OBJ_IS_TYPE(object, &mp_type_type)) { - nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 1 must be a class")); - } - - // TODO support a tuple of classes for second argument - if (!MP_OBJ_IS_TYPE(classinfo, &mp_type_type)) { - nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 2 must be a class")); - } - +bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo) { for (;;) { if (object == classinfo) { return true; @@ -496,6 +487,19 @@ bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) { } } +bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) { + if (!MP_OBJ_IS_TYPE(object, &mp_type_type)) { + nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 1 must be a class")); + } + + // TODO support a tuple of classes for second argument + if (!MP_OBJ_IS_TYPE(classinfo, &mp_type_type)) { + nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 2 must be a class")); + } + + return mp_obj_is_subclass_fast(object, classinfo); +} + STATIC mp_obj_t mp_builtin_issubclass(mp_obj_t object, mp_obj_t classinfo) { return MP_BOOL(mp_obj_is_subclass(object, classinfo)); }