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.
This commit is contained in:
Paul Sokolovsky 2014-02-27 22:48:25 +02:00
parent d08fd68664
commit d86d22e1e7
2 changed files with 16 additions and 10 deletions

View File

@ -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);

View File

@ -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));
}