py: Add support for properties on native objects when they are in the locals dict.
This commit is contained in:
parent
ceeac4791e
commit
f03a9ac505
48
py/runtime.c
48
py/runtime.c
|
@ -34,6 +34,7 @@
|
|||
#include "py/compile.h"
|
||||
#include "py/objstr.h"
|
||||
#include "py/objtuple.h"
|
||||
#include "py/objtype.h"
|
||||
#include "py/objlist.h"
|
||||
#include "py/objmodule.h"
|
||||
#include "py/objgenerator.h"
|
||||
|
@ -102,7 +103,7 @@ void mp_deinit(void) {
|
|||
//mp_obj_dict_free(&dict_main);
|
||||
mp_module_deinit();
|
||||
|
||||
// call port specific deinitialization if any
|
||||
// call port specific deinitialization if any
|
||||
#ifdef MICROPY_PORT_INIT_FUNC
|
||||
MICROPY_PORT_DEINIT_FUNC;
|
||||
#endif
|
||||
|
@ -989,6 +990,22 @@ void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t
|
|||
dest[0] = member;
|
||||
dest[1] = self;
|
||||
}
|
||||
#if MICROPY_PY_BUILTINS_PROPERTY
|
||||
} else if (MP_OBJ_IS_TYPE(member, &mp_type_property) && mp_obj_is_native_type(type)) {
|
||||
// object member is a property; delegate the load to the property
|
||||
// Note: This is an optimisation for code size and execution time.
|
||||
// The proper way to do it is have the functionality just below
|
||||
// in a __get__ method of the property object, and then it would
|
||||
// be called by the descriptor code down below. But that way
|
||||
// requires overhead for the nested mp_call's and overhead for
|
||||
// the code.
|
||||
const mp_obj_t *proxy = mp_obj_property_get(member);
|
||||
if (proxy[0] == mp_const_none) {
|
||||
mp_raise_msg(&mp_type_AttributeError, "unreadable attribute");
|
||||
} else {
|
||||
dest[0] = mp_call_function_n_kw(proxy[0], 1, 0, &self);
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
// class member is a value, so just return that value
|
||||
dest[0] = member;
|
||||
|
@ -1068,6 +1085,35 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
|
|||
// success
|
||||
return;
|
||||
}
|
||||
#if MICROPY_PY_BUILTINS_PROPERTY
|
||||
} else if (type->locals_dict != NULL) {
|
||||
// generic method lookup
|
||||
// this is a lookup in the object (ie not class or type)
|
||||
assert(type->locals_dict->base.type == &mp_type_dict); // Micro Python restriction, for now
|
||||
mp_map_t *locals_map = &type->locals_dict->map;
|
||||
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
|
||||
if (elem != NULL && MP_OBJ_IS_TYPE(elem->value, &mp_type_property)) {
|
||||
// attribute exists and is a property; delegate the store/delete
|
||||
// Note: This is an optimisation for code size and execution time.
|
||||
// The proper way to do it is have the functionality just below in
|
||||
// a __set__/__delete__ method of the property object, and then it
|
||||
// would be called by the descriptor code down below. But that way
|
||||
// requires overhead for the nested mp_call's and overhead for
|
||||
// the code.
|
||||
const mp_obj_t *proxy = mp_obj_property_get(elem->value);
|
||||
mp_obj_t dest[2] = {base, value};
|
||||
if (value == MP_OBJ_NULL) {
|
||||
// delete attribute
|
||||
if (proxy[2] != mp_const_none) {
|
||||
mp_call_function_n_kw(proxy[2], 1, 0, dest);
|
||||
return;
|
||||
}
|
||||
} else if (proxy[1] != mp_const_none) {
|
||||
mp_call_function_n_kw(proxy[1], 2, 0, dest);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
mp_raise_msg(&mp_type_AttributeError, "no such attribute");
|
||||
|
|
Loading…
Reference in New Issue