Skip to content

Commit d86d22e

Browse files
committed
Add mp_obj_is_subclass_fast() - intended for fast argument checking.
I.e. as replacement of MP_OBJ_IS_TYPE(), which takes into account subclassing.
1 parent d08fd68 commit d86d22e

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

py/obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ mp_obj_t mp_obj_new_module(qstr module_name);
256256
mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in);
257257
const char *mp_obj_get_type_str(mp_obj_t o_in);
258258
bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo);
259+
// Without supefluous arg checking
260+
bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo);
259261

260262
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
261263
void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);

py/objtype.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,7 @@ mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj) {
454454
/******************************************************************************/
455455
// subclassing and built-ins specific to types
456456

457-
bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) {
458-
if (!MP_OBJ_IS_TYPE(object, &mp_type_type)) {
459-
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 1 must be a class"));
460-
}
461-
462-
// TODO support a tuple of classes for second argument
463-
if (!MP_OBJ_IS_TYPE(classinfo, &mp_type_type)) {
464-
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 2 must be a class"));
465-
}
466-
457+
bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo) {
467458
for (;;) {
468459
if (object == classinfo) {
469460
return true;
@@ -496,6 +487,19 @@ bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) {
496487
}
497488
}
498489

490+
bool mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) {
491+
if (!MP_OBJ_IS_TYPE(object, &mp_type_type)) {
492+
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 1 must be a class"));
493+
}
494+
495+
// TODO support a tuple of classes for second argument
496+
if (!MP_OBJ_IS_TYPE(classinfo, &mp_type_type)) {
497+
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 2 must be a class"));
498+
}
499+
500+
return mp_obj_is_subclass_fast(object, classinfo);
501+
}
502+
499503
STATIC mp_obj_t mp_builtin_issubclass(mp_obj_t object, mp_obj_t classinfo) {
500504
return MP_BOOL(mp_obj_is_subclass(object, classinfo));
501505
}

0 commit comments

Comments
 (0)