STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) { if (mp_obj_is_callable(o_in)) { return mp_const_true; } else { return mp_const_false; } }
void mp_obj_instance_load_method(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // logic: look in obj members then class locals (TODO check this against CPython) mp_obj_instance_t *self = self_in; mp_map_elem_t *elem = mp_qstr_map_lookup(self->members, attr, false); if (elem != NULL) { // object member, always treated as a value dest[1] = elem->value; dest[0] = NULL; return; } elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, false); if (elem != NULL) { if (mp_obj_is_callable(elem->value)) { // class member is callable so build a bound method dest[1] = elem->value; dest[0] = self_in; return; } else { // class member is a value, so just return that value dest[1] = elem->value; dest[0] = NULL; return; } } // no such method, so fall back to load attr dest[1] = rt_load_attr(self_in, attr); dest[0] = NULL; }
/// \method callback(fun) /// Set the function to be called in timer interrupt. /// `fun` is passed 1 argument, the timer object. /// If `fun` is `None` then the callback will be disabled. STATIC mp_obj_t pyb_rtc_callback(mp_obj_t self_in, mp_obj_t callback) { pyb_rtc_obj_t *self = self_in; if (callback == mp_const_none) { // stop interrupt self->callback = mp_const_none; mp_hal_setCallbackRTC(NULL,NULL); } else if (mp_obj_is_callable(callback)) { self->callback = callback; mp_hal_setCallbackRTC(( void(*)(void*) )pyb_rtc_exec_callback,self); } else { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "callback must be None or a callable object")); } return mp_const_none; }
mp_obj_t mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr) { // logic: look in obj members then class locals (TODO check this against CPython) mp_obj_instance_t *self = self_in; mp_map_elem_t *elem = mp_qstr_map_lookup(self->members, attr, false); if (elem != NULL) { // object member, always treated as a value return elem->value; } elem = mp_qstr_map_lookup(mp_obj_class_get_locals(self->class), attr, false); if (elem != NULL) { if (mp_obj_is_callable(elem->value)) { // class member is callable so build a bound method return mp_obj_new_bound_meth(self_in, elem->value); } else { // class member is a value, so just return that value return elem->value; } } nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(self_in), qstr_str(attr))); }