mirror of
https://github.com/micropython/micropython.git
synced 2026-01-06 20:20:14 +01:00
py/objtype: Implement __delattr__ and __setattr__.
This patch implements support for class methods __delattr__ and __setattr__ for customising attribute access. It is controlled by the config option MICROPY_PY_DELATTR_SETATTR and is disabled by default.
This commit is contained in:
@@ -635,6 +635,12 @@ typedef double mp_float_t;
|
||||
#define MICROPY_PY_DESCRIPTORS (0)
|
||||
#endif
|
||||
|
||||
// Whether to support class __delattr__ and __setattr__ methods
|
||||
// This costs some code size and makes all del attrs and store attrs slow
|
||||
#ifndef MICROPY_PY_DELATTR_SETATTR
|
||||
#define MICROPY_PY_DELATTR_SETATTR (0)
|
||||
#endif
|
||||
|
||||
// Support for async/await/async for/async with
|
||||
#ifndef MICROPY_PY_ASYNC_AWAIT
|
||||
#define MICROPY_PY_ASYNC_AWAIT (1)
|
||||
|
||||
34
py/objtype.c
34
py/objtype.c
@@ -533,6 +533,15 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
|
||||
|
||||
// try __getattr__
|
||||
if (attr != MP_QSTR___getattr__) {
|
||||
#if MICROPY_PY_DELATTR_SETATTR
|
||||
// If the requested attr is __setattr__/__delattr__ then don't delegate the lookup
|
||||
// to __getattr__. If we followed CPython's behaviour then __setattr__/__delattr__
|
||||
// would have already been found in the "object" base class.
|
||||
if (attr == MP_QSTR___setattr__ || attr == MP_QSTR___delattr__) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
mp_obj_t dest2[3];
|
||||
mp_load_method_maybe(self_in, MP_QSTR___getattr__, dest2);
|
||||
if (dest2[0] != MP_OBJ_NULL) {
|
||||
@@ -626,10 +635,35 @@ STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t val
|
||||
|
||||
if (value == MP_OBJ_NULL) {
|
||||
// delete attribute
|
||||
#if MICROPY_PY_DELATTR_SETATTR
|
||||
// try __delattr__ first
|
||||
mp_obj_t attr_delattr_method[3];
|
||||
mp_load_method_maybe(self_in, MP_QSTR___delattr__, attr_delattr_method);
|
||||
if (attr_delattr_method[0] != MP_OBJ_NULL) {
|
||||
// __delattr__ exists, so call it
|
||||
attr_delattr_method[2] = MP_OBJ_NEW_QSTR(attr);
|
||||
mp_call_method_n_kw(1, 0, attr_delattr_method);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
|
||||
return elem != NULL;
|
||||
} else {
|
||||
// store attribute
|
||||
#if MICROPY_PY_DELATTR_SETATTR
|
||||
// try __setattr__ first
|
||||
mp_obj_t attr_setattr_method[4];
|
||||
mp_load_method_maybe(self_in, MP_QSTR___setattr__, attr_setattr_method);
|
||||
if (attr_setattr_method[0] != MP_OBJ_NULL) {
|
||||
// __setattr__ exists, so call it
|
||||
attr_setattr_method[2] = MP_OBJ_NEW_QSTR(attr);
|
||||
attr_setattr_method[3] = value;
|
||||
mp_call_method_n_kw(2, 0, attr_setattr_method);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user