Why mp_type_framebuf is declared as STATIC?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
boochow
Posts: 30
Joined: Sat Dec 16, 2017 9:36 am

Why mp_type_framebuf is declared as STATIC?

Post by boochow » Mon Apr 19, 2021 3:40 am

Hi guys, I have a question.
I am going to make a subclass of framebuf class in my C user module.
I think it is possible by pointing `mp_type_framebuf` from `parent` of my class, but it fails to build because `mp_type_framebuf` is declared as `STATIC.`

Code: Select all

include "py/runtime.h"

extern mp_obj_type_t mp_type_framebuf;

STATIC const mp_rom_map_elem_t rp2x_vgafb_locals_table[] = {
};
STATIC MP_DEFINE_CONST_DICT(rp2x_vgafb_locals, rp2x_vgafb_locals_table);

const mp_obj_type_t rp2x_vgafb_type = {
    { &mp_type_type },
    .name = MP_QSTR_VGAfb,
    .locals_dict = (mp_obj_t)&rp2x_vgafb_locals,
    .parent = &mp_type_framebuf,
};

Code: Select all

STATIC const mp_obj_type_t mp_type_framebuf = {
    { &mp_type_type },
    .name = MP_QSTR_FrameBuffer,
    .make_new = framebuf_make_new,
    .buffer_p = { .get_buffer = framebuf_get_buffer },
    .locals_dict = (mp_obj_dict_t *)&framebuf_locals_dict,
};
So I removed the `STATIC` declaration from mp_type_framebuf and successfully built my project.
However, I wonder why `mp_type_framebuf` is declared as STATIC.
Any thoughts?

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

Re: Why mp_type_framebuf is declared as STATIC?

Post by stijn » Mon Apr 19, 2021 6:00 am

By default anything which isn't part of the public API, i.e. doesn't get exposed and remains local to a source file, is declared static. This is cleaner, plus as fas as I'm aware it allows certain optimizations both with respect to performance and code size. You're not the first one with this question when it comes to framebuf though, so arguably its pubilc API should be larger (which doesn't necessarily mean the type should not be static, not sure if inheritance is the best way to extend the functionality)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why mp_type_framebuf is declared as STATIC?

Post by pythoncoder » Mon Apr 19, 2021 9:02 am

One thing to note is that (unlike other built-in classes) the FrameBuffer class is designed to be subclassed in Python. I don't know what this involves at the C level, but there may be implications in any attempt to change it.
Peter Hinch
Index to my micropython libraries.

User avatar
boochow
Posts: 30
Joined: Sat Dec 16, 2017 9:36 am

Re: Why mp_type_framebuf is declared as STATIC?

Post by boochow » Tue Apr 20, 2021 11:05 pm

@stijn @pythoncoder Thanks, that's very clear. I think I'll change the design of my code. As a user, I feel it a bit inconsistent that some things that can be done in Python cannot be done in the C module, but it makes sense as a development policy for MicroPython.

Post Reply