diff --git a/src/dbus_fast/service.pxd b/src/dbus_fast/service.pxd index 384f54f4..40a2ad3c 100644 --- a/src/dbus_fast/service.pxd +++ b/src/dbus_fast/service.pxd @@ -17,6 +17,14 @@ cdef class _Method: cdef public SignatureTree in_signature_tree cdef public SignatureTree out_signature_tree + + +cdef tuple _real_fn_result_to_body( + object result, + SignatureTree signature_tree, + bint replace_fds +) + cdef class ServiceInterface: cdef public str name diff --git a/src/dbus_fast/service.py b/src/dbus_fast/service.py index 55510fe4..25fe1028 100644 --- a/src/dbus_fast/service.py +++ b/src/dbus_fast/service.py @@ -326,6 +326,35 @@ def decorator(fn): return decorator +def _real_fn_result_to_body( + result: Optional[Any], + signature_tree: SignatureTree, + replace_fds: bool, +) -> Tuple[List[Any], List[int]]: + out_len = len(signature_tree.types) + if result is None: + final_result = [] + else: + if out_len == 1: + final_result = [result] + else: + result_type = type(result) + if result_type is not list and result_type is not tuple: + raise SignatureBodyMismatchError( + "Expected signal to return a list or tuple of arguments" + ) + final_result = result + + if out_len != len(final_result): + raise SignatureBodyMismatchError( + f"Signature and function return mismatch, expected {len(signature_tree.types)} arguments but got {len(result)}" + ) + + if not replace_fds: + return final_result, [] + return replace_fds_with_idx(signature_tree, final_result) + + class ServiceInterface: """An abstract class that can be extended by the user to define DBus services. @@ -526,28 +555,7 @@ def _fn_result_to_body( signature_tree: SignatureTree, replace_fds: bool = True, ) -> Tuple[List[Any], List[int]]: - out_len = len(signature_tree.types) - if result is None: - final_result = [] - else: - if out_len == 1: - final_result = [result] - else: - result_type = type(result) - if result_type is not list and result_type is not tuple: - raise SignatureBodyMismatchError( - "Expected signal to return a list or tuple of arguments" - ) - final_result = result - - if out_len != len(final_result): - raise SignatureBodyMismatchError( - f"Signature and function return mismatch, expected {len(signature_tree.types)} arguments but got {len(result)}" - ) - - if not replace_fds: - return final_result, [] - return replace_fds_with_idx(signature_tree, final_result) + return _real_fn_result_to_body(result, signature_tree, replace_fds) @staticmethod def _c_fn_result_to_body( @@ -559,28 +567,7 @@ def _c_fn_result_to_body( wrapped in a list to be a message body. Also they may return fds directly for type 'h' which need to be put into an external list.""" # https://github.com/cython/cython/issues/3327 - out_len = len(signature_tree.types) - if result is None: - final_result = [] - else: - if out_len == 1: - final_result = [result] - else: - result_type = type(result) - if result_type is not list and result_type is not tuple: - raise SignatureBodyMismatchError( - "Expected signal to return a list or tuple of arguments" - ) - final_result = result - - if out_len != len(final_result): - raise SignatureBodyMismatchError( - f"Signature and function return mismatch, expected {len(signature_tree.types)} arguments but got {len(result)}" - ) - - if not replace_fds: - return final_result, [] - return replace_fds_with_idx(signature_tree, final_result) + return _real_fn_result_to_body(result, signature_tree, replace_fds) @staticmethod def _handle_signal(