New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
py: Support object __getattr__, __delattr__ and __setattr__ methods #2687
Changes from 1 commit
58f509b
978fbdb
b7bb31d
cfe2663
600425c
e60fe93
4c2766b
6471d7e
a19cfea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,13 +59,48 @@ STATIC mp_obj_t object___new__(mp_obj_t cls) { | |
STATIC MP_DEFINE_CONST_FUN_OBJ_1(object___new___fun_obj, object___new__); | ||
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(object___new___obj, MP_ROM_PTR(&object___new___fun_obj)); | ||
|
||
#if MICROPY_PY_ATTRS_METHODS | ||
STATIC mp_obj_t object___setattr__(mp_obj_t self_in, mp_obj_t attr_in, mp_obj_t value) { | ||
mp_map_lookup(mp_obj_dict_get_map(self_in), MP_OBJ_NEW_QSTR(mp_obj_str_get_qstr(attr_in)), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value; | ||
return value; | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_3(object___setattr___obj, object___setattr__); | ||
|
||
STATIC mp_obj_t object___getattr__(mp_obj_t self_in, mp_obj_t attr_in) { | ||
qstr attr = mp_obj_str_get_qstr(attr_in); | ||
mp_map_elem_t *elem = mp_map_lookup(mp_obj_dict_get_map(self_in), MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); | ||
if (elem == NULL) { | ||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, | ||
"'%s' object has no attribute '%q'", | ||
mp_obj_get_type_str(self_in), attr)); | ||
} | ||
return elem->value; | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_2(object___getattr___obj, object___getattr__); | ||
|
||
STATIC mp_obj_t object___delattr__(mp_obj_t self_in, mp_obj_t attr_in) { | ||
mp_map_lookup(mp_obj_dict_get_map(self_in), MP_OBJ_NEW_QSTR(mp_obj_str_get_qstr(attr_in)), MP_MAP_LOOKUP_REMOVE_IF_FOUND); | ||
return mp_const_none; | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_2(object___delattr___obj, object___delattr__); | ||
#endif | ||
|
||
STATIC const mp_rom_map_elem_t object_locals_dict_table[] = { | ||
#if MICROPY_CPYTHON_COMPAT | ||
{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&object___init___obj) }, | ||
#endif | ||
#if MICROPY_CPYTHON_COMPAT | ||
{ MP_ROM_QSTR(MP_QSTR___new__), MP_ROM_PTR(&object___new___obj) }, | ||
#endif | ||
#if (MICROPY_CPYTHON_COMPAT && MICROPY_PY_ATTRS_METHODS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just #if MICROPY_PY_ATTRS_METHODS ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. becouse i have placed it into: #if MICROPY_CPYTHON_COMPAT There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah didin't see that in the github diff. Still it's a bit confusing as now one might use |
||
{ MP_ROM_QSTR(MP_QSTR___delattr__), MP_ROM_PTR(&object___delattr___obj) }, | ||
#endif | ||
#if (MICROPY_CPYTHON_COMPAT && MICROPY_PY_ATTRS_METHODS) | ||
{ MP_ROM_QSTR(MP_QSTR___getattr__), MP_ROM_PTR(&object___getattr___obj) }, | ||
#endif | ||
#if (MICROPY_CPYTHON_COMPAT && MICROPY_PY_ATTRS_METHODS) | ||
{ MP_ROM_QSTR(MP_QSTR___setattr__), MP_ROM_PTR(&object___setattr___obj) }, | ||
#endif | ||
}; | ||
|
||
STATIC MP_DEFINE_CONST_DICT(object_locals_dict, object_locals_dict_table); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'slower' would be better wording imo, without measuring the actual impact it's hard to make absolute statements about it.