From 32e734b4f53595bc0d4fdea4206e852d0848c399 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Thu, 23 Mar 2023 12:35:46 +0100 Subject: Wrappers Python: Separate BoolError data type to get rid of decode_func --- wrappers/python/eduvpn_common/loader.py | 4 ++-- wrappers/python/eduvpn_common/main.py | 9 ++------ wrappers/python/eduvpn_common/types.py | 38 +++++++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 15 deletions(-) (limited to 'wrappers') diff --git a/wrappers/python/eduvpn_common/loader.py b/wrappers/python/eduvpn_common/loader.py index 1cf7ab5..36381b4 100644 --- a/wrappers/python/eduvpn_common/loader.py +++ b/wrappers/python/eduvpn_common/loader.py @@ -4,7 +4,7 @@ from collections import defaultdict from ctypes import CDLL, c_char_p, c_int, c_void_p, cdll from eduvpn_common import __version__ -from eduvpn_common.types import DataError, ReadRxBytes, VPNStateChange +from eduvpn_common.types import BoolError, DataError, ReadRxBytes, VPNStateChange def load_lib() -> CDLL: @@ -102,5 +102,5 @@ def initialize_functions(lib: CDLL) -> None: c_char_p, c_int, ReadRxBytes, - ], DataError + ], BoolError lib.CancelFailover.argtypes, lib.CancelFailover.restype = [], c_void_p diff --git a/wrappers/python/eduvpn_common/main.py b/wrappers/python/eduvpn_common/main.py index 2c5c71e..d8e35f9 100644 --- a/wrappers/python/eduvpn_common/main.py +++ b/wrappers/python/eduvpn_common/main.py @@ -44,24 +44,20 @@ class EduVPN(object): initialize_functions(self.lib) def go_function( - self, func: Any, *args: Iterator, decode_func: Optional[Callable] = None + self, func: Any, *args: Iterator ) -> Any: """Call an internal go function and properly forward the arguments. Also handles decoding the result :param func: Any: The Go function to call from the shared library :param \*args: Iterator: The arguments to call the function with - :param decode_func: Optional[Callable]: (Default value = None): The function to decode the result into a Python type :meta private: """ # The functions all have at least one arg type which is the name of the client args_gen = encode_args(list(args), func.argtypes) res = func(*(args_gen)) - if decode_func is None: - return decode_res(func.restype)(self.lib, res) - else: - return decode_func(self.lib, res) + return decode_res(func.restype)(self.lib, res) def cancel_oauth(self) -> None: """Cancel the OAuth process""" @@ -272,7 +268,6 @@ class EduVPN(object): gateway, wg_mtu, readrxbytes, - decode_func=lambda lib, x: get_data_error(lib, x, get_bool), ) if dropped_err: forwardError(dropped_err) diff --git a/wrappers/python/eduvpn_common/types.py b/wrappers/python/eduvpn_common/types.py index 4e54ad3..b01455d 100644 --- a/wrappers/python/eduvpn_common/types.py +++ b/wrappers/python/eduvpn_common/types.py @@ -4,7 +4,7 @@ from typing import Any, Iterator, List, Tuple class DataError(Structure): - """The C type that represents a tuple of data and error as returned by the Go library + """The C type that represents a tuple of data and error (both strings) as returned by the Go library :meta private: """ @@ -12,6 +12,15 @@ class DataError(Structure): _fields_ = [("data", c_void_p), ("error", c_void_p)] +class BoolError(Structure): + """The C type that represents a tuple of boolean and error as returned by the Go library + + :meta private: + """ + + _fields_ = [("boolean", c_int), ("error", c_void_p)] + + # The type for a Go state change callback VPNStateChange = CFUNCTYPE(c_int, c_int, c_int, c_char_p) ReadRxBytes = CFUNCTYPE(c_ulonglong) @@ -50,9 +59,9 @@ def decode_res(res: Any) -> Any: :rtype: Any """ decode_map = { - c_int: get_bool, c_void_p: get_ptr_string, DataError: get_data_error, + BoolError: get_bool_error, } return decode_map.get(res, lambda lib, x: x) @@ -78,24 +87,41 @@ def get_ptr_string(lib: CDLL, ptr: c_void_p) -> str: def get_data_error( - lib: CDLL, data_error: DataError, data_conv: Any = get_ptr_string + lib: CDLL, data_error: DataError ) -> Tuple[str, str]: """Convert a C data+error structure to a Python usable data+error structure :param lib: CDLL: The Go shared library :param data_error: DataError: The data error C structure - :param data_conv: Any: The function to convert the data :meta private: :return: The data and error :rtype: Tuple[str, str] """ - data = data_conv(lib, data_error.data) + data = get_ptr_string(lib, data_error.data) error = get_ptr_string(lib, data_error.error) return data, error +def get_bool_error( + lib: CDLL, bool_error: BoolError +) -> Tuple[bool, str]: + """Convert a C boolean (c int)+error structure to a Python usable boolean+error structure + + :param lib: CDLL: The Go shared library + :param bool_error: BoolError: The boolean and error C structure + + :meta private: + + :return: The bool and error + :rtype: Tuple[bool, str] + """ + boolean = get_bool(lib, bool_error.boolean) + error = get_ptr_string(lib, bool_error.error) + return boolean, error + + def get_bool(lib: CDLL, boolInt: c_int) -> bool: """Get a bool from the Go shared library. Essentially just checking if an int represents 'True' @@ -107,4 +133,4 @@ def get_bool(lib: CDLL, boolInt: c_int) -> bool: :return: The boolean converted to Python :rtype: bool """ - return boolInt != 0 + return int(boolInt) != 0 -- cgit v1.2.3