Skip to content

CPython binds struct methods that take the struct by value - #49

Merged
bdbarnett merged 1 commit into
mainfrom
fix/byvalue-methods
Oct 27, 2026
Merged

bdbarnett merged 1 commit into
mainfrom
fix/byvalue-methods

Conversation

@bdbarnett

@bdbarnett bdbarnett commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #46. #48 has merged, so this is based on main.

On CPython, a struct method is now bound when its first argument is the struct by value, as on MicroPython. Before, it was bound only when that argument was a pointer to the struct. So lv_color_brightness(lv_color_t c) and the rest were generated but never reachable, as methods or as module functions.

>>> c = lv.color_hex(0x336699)
>>> c.brightness(), hex(c.to_u32() & 0xFFFFFF), c.to_32(255).alpha
(108, '0x336699', 255)

All 21 methods listed in #46 are now bound on CPython. A by-value method gets a copy of its struct, so calling it never changes the struct.

area_t.diff was the other case: its first parameter is an array (lv_area_t res_p[]). It's now bound the way MicroPython binds it, as a static method that takes all three arguments.

The MicroPython and CircuitPython outputs are unchanged.

The new smoke test test_struct_methods_by_value calls to_u32, eq, brightness, lighten and to_32, and checks that the other methods exist. Before this change, CPython failed it with 'lvgl.lv_color_t' object has no attribute 'to_u32', and MicroPython passed it. With this change it passes on both locally, along with the rest of the smoke test and lvgl-python's unittests. pytest (109) passes and regenerate_all.sh --check matches.

A struct method was bound only when its first argument was a pointer to the struct. So lv_color_brightness(lv_color_t c) and the other by-value methods were generated but never added to a method table, and weren't module functions either. They're now methods, as on MicroPython.
Base automatically changed from fix/struct-dir to main October 27, 2026 16:16
@bdbarnett
bdbarnett merged commit fa290d7 into main Oct 27, 2026
4 checks passed
@bdbarnett
bdbarnett deleted the fix/byvalue-methods branch October 27, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

CPython: struct methods that take the struct by value are missing (color_t.brightness and others)

1 participant