Skip to content

Commit ec19858

Browse files
Adjust binding code generator to fix UnicodeError
This change addresses a UnicodeError in the MicroPython-LVGL binding generator by explicitly handling `char *` arguments in `build_callback_func_arg`. Previously, the function relied on the `lv_to_mp` dictionary for all type conversions, which could lead to incorrect or missing converters for `char *`, causing encoding issues. Now, `char *` arguments are assigned the `ptr_to_mp` converter directly, ensuring proper conversion to MicroPython string objects. Additionally, the initial type extraction preserves qualifiers (`remove_quals=False`) to improve type handling accuracy, while maintaining compatibility for non-`char *` types by stripping qualifiers in the fallback path.
1 parent eda82dd commit ec19858

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

gen/python_api_gen_mpy.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,12 +2865,18 @@ def create_helper_struct(struct_str):
28652865

28662866

28672867
def build_callback_func_arg(arg, index, func, func_name = None):
2868-
arg_type = get_type(arg.type, remove_quals = True)
2868+
arg_type = get_type(arg.type, remove_quals = False)
28692869
cast = '(void*)' if isinstance(arg.type, c_ast.PtrDecl) else '' # needed when field is const. casting to void overrides it
2870-
if arg_type not in lv_to_mp or not lv_to_mp[arg_type]:
2871-
try_generate_type(arg.type)
2870+
2871+
if arg_type == 'char *':
2872+
converter = 'ptr_to_mp'
2873+
else:
2874+
arg_type = get_type(arg.type, remove_quals = True)
28722875
if arg_type not in lv_to_mp or not lv_to_mp[arg_type]:
2873-
raise MissingConversionException("Callback: Missing conversion to %s" % arg_type)
2876+
try_generate_type(arg.type)
2877+
if arg_type not in lv_to_mp or not lv_to_mp[arg_type]:
2878+
raise MissingConversionException("Callback: Missing conversion to %s" % arg_type)
2879+
converter = lv_to_mp[arg_type]
28742880

28752881
arg_metadata = {'c_type': arg_type, 'py_type': get_py_type(arg_type)}
28762882

@@ -2880,8 +2886,9 @@ def build_callback_func_arg(arg, index, func, func_name = None):
28802886
arg_metadata['name'] = None
28812887

28822888
callback_metadata[func_name]['args'].append(arg_metadata)
2889+
28832890
return 'mp_args[{i}] = {convertor}({cast}arg{i});'.format(
2884-
convertor = lv_to_mp[arg_type],
2891+
convertor = converter,
28852892
i = index, cast = cast)
28862893

28872894

0 commit comments

Comments
 (0)