C module class with properties and methods

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
robdobson
Posts: 6
Joined: Mon Sep 28, 2020 9:23 am

C module class with properties and methods

Post by robdobson » Mon Sep 28, 2020 9:44 am

I've been following this document: https://readthedocs.org/projects/microp ... df/latest/

In the Classes section under Properties (around page 40) there is an example which adds a .attr = to the type definition of a class
(propertyclass_type in the example).

I have tried to extend this example to have both a property and a method but when I try to run it the method is not found - I get the error message: AttributeError: 'propertyclass' object has no attribute 'name'

Looking at the source code in runtime.c around line 1091 there is the code

Code: Select all

    } else if (type->attr != NULL) {
        // this type can do its own load, so call it
        type->attr(obj, attr, dest);

    } 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); // MicroPython 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_convert_member_lookup(obj, type, elem->value, dest);
        }
    }
I assume when .attr is not present the "generic method lookup" is used but when .attr is present a different approach is used. I also assume this has to define both the methods and the properties.

Is there an example for what the ..._attr method should look like to define both properties and methods?

The example in the referenced document has the code:

Code: Select all

STATIC void propertyclass_attr(mp_obj_t self, qstr attribute, mp_obj_t *destination) {
    if(attribute == MP_QSTR_x) {
        destination[0] = propertyclass_x(self);
    } else if(attribute == MP_QSTR_y) {
        destination[0] = propertyclass_y(self);
    }
}
How would I change this to include methods too?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: C module class with properties and methods

Post by stijn » Mon Sep 28, 2020 11:21 am

If the attribute to lookup is your method name, set destination[0] to your method object and destination[1] to self. Also note that your function should deal with setting attributes as well, right now that gets ignored. For examples, search the source code for ".attr = " to see how others do it. E.g. one of the first matches leads to thet task_attr() function in modysyncio.c where you can get a pretty good idea of how things need to be done.

robdobson
Posts: 6
Joined: Mon Sep 28, 2020 9:23 am

Re: C module class with properties and methods

Post by robdobson » Mon Sep 28, 2020 2:03 pm

Cool, I'll take a look, thanks

Post Reply