Example #1
0
def infer_from_python(value: Any):
    """
    Convert pythonic value to ctypes buffer, type hint-aware.

    :param value: pythonic value or (value, type_hint) tuple,
    :return: bytes.
    """
    if is_hinted(value):
        value, data_type = value
    else:
        data_type = AnyDataObject
    return data_type.from_python(value)
Example #2
0
def infer_from_python(value: Any):
    """
    Convert pythonic value to ctypes buffer, type hint-aware.

    :param value: pythonic value or (value, type_hint) tuple,
    :return: bytes.
    """
    if is_hinted(value):
        value, data_type = value
    else:
        data_type = AnyDataObject
    return data_type.from_python(value)
Example #3
0
    def from_python(cls, value, type_id=None):
        header_class = cls.build_header()
        header = header_class()
        length = len(value)
        header.length = length
        if hasattr(header, 'type_code'):
            header.type_code = int.from_bytes(cls.type_code,
                                              byteorder=PROTOCOL_BYTE_ORDER)
        if hasattr(header, 'type'):
            header.type = type_id
        buffer = bytes(header)

        for k, v in value.items():
            if is_hinted(k):
                buffer += k[1].from_python(k[0])
            else:
                buffer += AnyDataObject.from_python(k)
            if is_hinted(v):
                buffer += v[1].from_python(v[0])
            else:
                buffer += AnyDataObject.from_python(v)
        return buffer
Example #4
0
    def from_python(self, value):
        header_class = self.build_header()
        header = header_class()

        try:
            length = len(value)
        except TypeError:
            value = [value]
            length = 1
        header.length = length
        buffer = bytes(header)

        for x in value:
            if is_hinted(x):
                buffer += x[1].from_python(x[0])
            else:
                buffer += super().from_python(x)
        return buffer
def __unpack_hinted(value):
    if is_hinted(value):
        return value
    return value, AnyDataObject