py/objset: Use mp_check_self() to check args of set/frozenset methods.

Following how other objects work, set/frozenset methods should use the
mp_check_self() macro to check the type of the self argument, because in
most cases this check can be a null operation.

Saves about 100-180 bytes of code for builds with set and frozenset
enabled.
This commit is contained in:
Damien George 2016-09-28 10:55:23 +10:00
parent 0c595fa094
commit dd4135aeaf

View File

@ -57,27 +57,21 @@ STATIC bool is_set_or_frozenset(mp_obj_t o) {
;
}
#if MICROPY_PY_BUILTINS_FROZENSET
STATIC void check_set_or_frozenset(mp_obj_t o) {
if (!is_set_or_frozenset(o)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
}
}
#else
#define check_set_or_frozenset(o) check_set(o)
#endif
// This macro is shorthand for mp_check_self to verify the argument is a
// set or frozenset for methods that operate on both of these types.
#define check_set_or_frozenset(o) mp_check_self(is_set_or_frozenset(o))
// This function is used to verify the argument for methods that modify
// the set object, and raises an exception if the arg is a frozenset.
STATIC void check_set(mp_obj_t o) {
if (!MP_OBJ_IS_TYPE(o, &mp_type_set)) {
// Emulate CPython behavior
#if MICROPY_PY_BUILTINS_FROZENSET
if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) {
// Mutable method called on frozenset; emulate CPython behavior, eg:
// AttributeError: 'frozenset' object has no attribute 'add'
#if MICROPY_PY_BUILTINS_FROZENSET
if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "'frozenset' has no such attribute"));
}
#endif
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "'frozenset' has no such attribute"));
}
#endif
mp_check_self(MP_OBJ_IS_TYPE(o, &mp_type_set));
}
STATIC void set_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {