Skip to content

nativeio, microcontroller and board modules #50

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

Merged
merged 3 commits into from
Nov 21, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
py: Add support for properties on native objects when they are in the…
… locals dict.
  • Loading branch information
tannewt committed Nov 19, 2016
commit f03a9ac505c563d9c7d06f198d1af2674328bfe3
48 changes: 47 additions & 1 deletion py/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy