From d86d22e1e7db19c4b50d92862f8eb0a430d64444 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Thu, 27 Feb 2014 22:48:25 +0200
Subject: [PATCH] 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.
---
 py/obj.h     |  2 ++
 py/objtype.c | 24 ++++++++++++++----------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/py/obj.h b/py/obj.h
index c7ffb4619..855b15150 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 46b96c731..c814e9c75 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));
 }
-- 
GitLab