def ensure_bytes()

in src/dubbo/serialization/_interfaces.py [0:0]


def ensure_bytes(obj: Union[bytes, bytearray, memoryview]) -> bytes:
    """
    Ensure that the input object is bytes or can be converted to bytes.
    :param obj: The object to ensure.
    :type obj: Union[bytes, bytearray, memoryview]
    :return: The bytes object.
    :rtype: bytes
    """

    if isinstance(obj, bytes):
        return obj
    elif isinstance(obj, (bytearray, memoryview)):
        return bytes(obj)
    else:
        raise SerializationError(
            f"SerializationError: The incoming object is of type '{type(obj).__name__}', "
            f"which is not supported. Expected types are 'bytes', 'bytearray', or 'memoryview'.\n"
            f"Current object type: '{type(obj).__name__}'.\n"
            f"Please provide data of the correct type or configure the serializer to handle the current input type."
        )