From 657aa7bd21f9408437c7d11aecdbf858994081db Mon Sep 17 00:00:00 2001 From: Samuel Felton Date: Wed, 19 Jun 2024 14:14:00 +0200 Subject: [PATCH] Debug function pointer tripping up python bindings compilation --- .../python/generator/visp_python_bindgen/methods.py | 11 ++++++++++- modules/python/generator/visp_python_bindgen/utils.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/python/generator/visp_python_bindgen/methods.py b/modules/python/generator/visp_python_bindgen/methods.py index 913f7e9b28..92164afcba 100644 --- a/modules/python/generator/visp_python_bindgen/methods.py +++ b/modules/python/generator/visp_python_bindgen/methods.py @@ -276,6 +276,7 @@ def define_method(method: types.Method, method_config: Dict, is_class_method, sp method_name = get_name(method.name) py_method_name = method_config.get('custom_name') or method_name return_type = get_type(method.return_type, specs, header_env.mapping) + param_is_function_ptr = [is_function_pointer(param.type) for param in method.parameters] method_signature = get_method_signature(method_name, get_type(method.return_type, {}, header_env.mapping), @@ -353,7 +354,15 @@ def make_keep_alive_str(values) -> str: # Arguments that are inputs to the lambda function that wraps the ViSP function input_param_types = [params_strs[i] for i in range(len(param_is_input)) if param_is_input[i]] - params_with_names = [t + ' ' + name for t, name in zip(input_param_types, input_param_names)] + + def to_argument_name(type: str, name: str) -> str: + if '(*)' in type: + return type.replace('(*)', f'(*{name})', 1) + else: + return type + ' ' + name + + + params_with_names = [to_argument_name(t, name) for t, name in zip(input_param_types, input_param_names)] # Params that are only outputs: they should be declared in function. Assume that they are default constructible param_is_only_output = [not is_input and is_output for is_input, is_output in zip(param_is_input, param_is_output)] diff --git a/modules/python/generator/visp_python_bindgen/utils.py b/modules/python/generator/visp_python_bindgen/utils.py index cc4a720d3b..0bbd4efeb8 100644 --- a/modules/python/generator/visp_python_bindgen/utils.py +++ b/modules/python/generator/visp_python_bindgen/utils.py @@ -225,6 +225,8 @@ def segment_repr(segment: types.PQNameSegment) -> str: return '::'.join(final_segment_reprs) + + def get_type(param: Union[types.FunctionType, types.DecoratedType, types.Value], owner_specs: Dict[str, str], header_env_mapping: Dict[str, str]) -> Optional[str]: ''' Get the type of a parameter. Compared to get_typename, this function resolves the parameter's constness, whether it is a ref, moveref or pointer. @@ -350,6 +352,14 @@ def is_pointer_to_const_cstr(param: types.Pointer) -> bool: return False +def is_function_pointer(param: types.DecoratedType) -> bool: + ''' + Whether the typed is a function pointer + ''' + if not isinstance(param, types.Pointer): + return False + return isinstance(param.ptr_to, types.FunctionType) + def is_non_const_ref_to_immutable_type(param: types.DecoratedType) -> bool: ''' Returns true if the parameter is a mutable reference to an immutable type in Python.