py/objtype: Make mp_obj_type_t.flags constants public, moved to obj.h.

This commit is contained in:
Damien George 2020-01-30 00:15:26 +11:00
parent 96a4435be1
commit c3450effd4
2 changed files with 13 additions and 12 deletions

View File

@ -444,6 +444,10 @@ typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
// this arg to mp_map_lookup(). // this arg to mp_map_lookup().
typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *); typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
// Flags for type behaviour (mp_obj_type_t.flags)
#define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001)
#define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
typedef enum { typedef enum {
PRINT_STR = 0, PRINT_STR = 0,
PRINT_REPR = 1, PRINT_REPR = 1,

View File

@ -44,9 +44,6 @@
#define ENABLE_SPECIAL_ACCESSORS \ #define ENABLE_SPECIAL_ACCESSORS \
(MICROPY_PY_DESCRIPTORS || MICROPY_PY_DELATTR_SETATTR || MICROPY_PY_BUILTINS_PROPERTY) (MICROPY_PY_DESCRIPTORS || MICROPY_PY_DELATTR_SETATTR || MICROPY_PY_BUILTINS_PROPERTY)
#define TYPE_FLAG_IS_SUBCLASSED (0x0001)
#define TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
STATIC mp_obj_t static_class_method_make_new(const mp_obj_type_t *self_in, size_t n_args, size_t n_kw, const mp_obj_t *args); STATIC mp_obj_t static_class_method_make_new(const mp_obj_type_t *self_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
/******************************************************************************/ /******************************************************************************/
@ -615,7 +612,7 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
mp_obj_class_lookup(&lookup, self->base.type); mp_obj_class_lookup(&lookup, self->base.type);
mp_obj_t member = dest[0]; mp_obj_t member = dest[0];
if (member != MP_OBJ_NULL) { if (member != MP_OBJ_NULL) {
if (!(self->base.type->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) { if (!(self->base.type->flags & MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
// Class doesn't have any special accessors to check so return straightaway // Class doesn't have any special accessors to check so return straightaway
return; return;
} }
@ -680,7 +677,7 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
if (!(self->base.type->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) { if (!(self->base.type->flags & MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
// Class doesn't have any special accessors so skip their checks // Class doesn't have any special accessors so skip their checks
goto skip_special_accessors; goto skip_special_accessors;
} }
@ -1061,13 +1058,13 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
} else { } else {
#if ENABLE_SPECIAL_ACCESSORS #if ENABLE_SPECIAL_ACCESSORS
// Check if we add any special accessor methods with this store // Check if we add any special accessor methods with this store
if (!(self->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) { if (!(self->flags & MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
if (check_for_special_accessors(MP_OBJ_NEW_QSTR(attr), dest[1])) { if (check_for_special_accessors(MP_OBJ_NEW_QSTR(attr), dest[1])) {
if (self->flags & TYPE_FLAG_IS_SUBCLASSED) { if (self->flags & MP_TYPE_FLAG_IS_SUBCLASSED) {
// This class is already subclassed so can't have special accessors added // This class is already subclassed so can't have special accessors added
mp_raise_msg(&mp_type_AttributeError, "can't add special method to already-subclassed class"); mp_raise_msg(&mp_type_AttributeError, "can't add special method to already-subclassed class");
} }
self->flags |= TYPE_FLAG_HAS_SPECIAL_ACCESSORS; self->flags |= MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
} }
} }
#endif #endif
@ -1123,8 +1120,8 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
} }
#if ENABLE_SPECIAL_ACCESSORS #if ENABLE_SPECIAL_ACCESSORS
if (mp_obj_is_instance_type(t)) { if (mp_obj_is_instance_type(t)) {
t->flags |= TYPE_FLAG_IS_SUBCLASSED; t->flags |= MP_TYPE_FLAG_IS_SUBCLASSED;
base_flags |= t->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS; base_flags |= t->flags & MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
} }
#endif #endif
} }
@ -1166,12 +1163,12 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
#if ENABLE_SPECIAL_ACCESSORS #if ENABLE_SPECIAL_ACCESSORS
// Check if the class has any special accessor methods // Check if the class has any special accessor methods
if (!(o->flags & TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) { if (!(o->flags & MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS)) {
for (size_t i = 0; i < o->locals_dict->map.alloc; i++) { for (size_t i = 0; i < o->locals_dict->map.alloc; i++) {
if (mp_map_slot_is_filled(&o->locals_dict->map, i)) { if (mp_map_slot_is_filled(&o->locals_dict->map, i)) {
const mp_map_elem_t *elem = &o->locals_dict->map.table[i]; const mp_map_elem_t *elem = &o->locals_dict->map.table[i];
if (check_for_special_accessors(elem->key, elem->value)) { if (check_for_special_accessors(elem->key, elem->value)) {
o->flags |= TYPE_FLAG_HAS_SPECIAL_ACCESSORS; o->flags |= MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS;
break; break;
} }
} }