From e233a55a296a981bce5fe3a7c20049bea46e3a16 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 11 Jan 2015 21:07:15 +0000 Subject: [PATCH] py: Remove unnecessary BINARY_OP_EQUAL code that just checks pointers. Previous patch c38dc3ccc76d1a9bf867704f43ea5d15da3fea7b allowed any object to be compared with any other, using pointer comparison for a fallback. As such, existing code which checked for this case is no longer needed. --- py/obj.h | 3 +-- py/objfun.c | 19 +++---------------- py/objstr.c | 6 +----- py/objtype.c | 13 ------------- 4 files changed, 5 insertions(+), 36 deletions(-) diff --git a/py/obj.h b/py/obj.h index 79b61402b7..6caf50d842 100644 --- a/py/obj.h +++ b/py/obj.h @@ -81,7 +81,7 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int)) #define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str)) #define MP_OBJ_IS_STR_OR_BYTES(o) (MP_OBJ_IS_QSTR(o) || (MP_OBJ_IS_OBJ(o) && ((mp_obj_base_t*)(o))->type->binary_op == mp_obj_str_binary_op)) -#define MP_OBJ_IS_FUN(o) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type->binary_op == mp_obj_fun_binary_op)) +#define MP_OBJ_IS_FUN(o) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type->name == MP_QSTR_function)) #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1) #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_int_t)(small_int)) << 1) | 1)) @@ -568,7 +568,6 @@ typedef struct _mp_obj_fun_builtin_t { // use this to make const objects that go void *fun; // must be a pointer to a callable function in ROM } mp_obj_fun_builtin_t; -mp_obj_t mp_obj_fun_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); const char *mp_obj_fun_get_name(mp_const_obj_t fun); const char *mp_obj_code_get_name(const byte *code_info); diff --git a/py/objfun.c b/py/objfun.c index b86d6f8dc6..3f26915223 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -43,17 +43,9 @@ #define DEBUG_printf(...) (void)0 #endif -// This binary_op method is used for all function types, and is also -// used to determine if an object is of generic function type. -mp_obj_t mp_obj_fun_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { - switch (op) { - case MP_BINARY_OP_EQUAL: - // These objects can be equal only if it's the same underlying structure, - // we don't even need to check for 2nd arg type. - return MP_BOOL(lhs_in == rhs_in); - } - return MP_OBJ_NULL; // op not supported -} +// Note: the "name" entry in mp_obj_type_t for a function type must be +// MP_QSTR_function because it is used to determine if an object is of generic +// function type. /******************************************************************************/ /* builtin functions */ @@ -109,7 +101,6 @@ const mp_obj_type_t mp_type_fun_builtin = { { &mp_type_type }, .name = MP_QSTR_function, .call = fun_builtin_call, - .binary_op = mp_obj_fun_binary_op, }; /******************************************************************************/ @@ -264,7 +255,6 @@ const mp_obj_type_t mp_type_fun_bc = { .print = fun_bc_print, #endif .call = fun_bc_call, - .binary_op = mp_obj_fun_binary_op, }; mp_obj_t mp_obj_new_fun_bc(mp_uint_t scope_flags, mp_uint_t n_pos_args, mp_uint_t n_kwonly_args, mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code) { @@ -345,7 +335,6 @@ STATIC const mp_obj_type_t mp_type_fun_native = { { &mp_type_type }, .name = MP_QSTR_function, .call = fun_native_call, - .binary_op = mp_obj_fun_binary_op, }; mp_obj_t mp_obj_new_fun_native(mp_uint_t n_args, void *fun_data) { @@ -404,7 +393,6 @@ STATIC const mp_obj_type_t mp_type_fun_viper = { { &mp_type_type }, .name = MP_QSTR_function, .call = fun_viper_call, - .binary_op = mp_obj_fun_binary_op, }; mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig) { @@ -515,7 +503,6 @@ STATIC const mp_obj_type_t mp_type_fun_asm = { { &mp_type_type }, .name = MP_QSTR_function, .call = fun_asm_call, - .binary_op = mp_obj_fun_binary_op, }; mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) { diff --git a/py/objstr.c b/py/objstr.c index 688988c019..01ed3a2d3b 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -348,16 +348,12 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { } else if (lhs_type == &mp_type_bytes) { mp_buffer_info_t bufinfo; if (!mp_get_buffer(rhs_in, &bufinfo, MP_BUFFER_READ)) { - goto incompatible; + return MP_OBJ_NULL; // op not supported } rhs_data = bufinfo.buf; rhs_len = bufinfo.len; } else { // incompatible types - incompatible: - if (op == MP_BINARY_OP_EQUAL) { - return mp_const_false; // can check for equality against every type - } return MP_OBJ_NULL; // op not supported } diff --git a/py/objtype.c b/py/objtype.c index ec616e355e..0db98508c5 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -762,18 +762,6 @@ STATIC bool type_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { return false; } -STATIC mp_obj_t type_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { - switch (op) { - case MP_BINARY_OP_EQUAL: - // Types can be equal only if it's the same type structure, - // we don't even need to check for 2nd arg type. - return MP_BOOL(lhs_in == rhs_in); - - default: - return MP_OBJ_NULL; // op not supported - } -} - const mp_obj_type_t mp_type_type = { { &mp_type_type }, .name = MP_QSTR_type, @@ -782,7 +770,6 @@ const mp_obj_type_t mp_type_type = { .call = type_call, .load_attr = type_load_attr, .store_attr = type_store_attr, - .binary_op = type_binary_op, }; mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) {