Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unmarshaller cython typing for SignatureType and SignatureTree #168

Merged
merged 3 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 39 additions & 38 deletions src/dbus_fast/_private/unmarshaller.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cython

from ..message cimport Message
from ..signature cimport Variant
from ..signature cimport SignatureTree, SignatureType, Variant


cdef object MAX_UNIX_FDS_SIZE
Expand Down Expand Up @@ -41,37 +41,37 @@ cdef object INT16_UNPACK_BIG_ENDIAN
cdef object UINT16_UNPACK_LITTLE_ENDIAN
cdef object UINT16_UNPACK_BIG_ENDIAN

cdef object MESSAGE_TYPE_MAP
cdef object MESSAGE_FLAG_MAP
cdef cython.dict MESSAGE_TYPE_MAP
cdef cython.dict MESSAGE_FLAG_MAP
cdef object HEADER_MESSAGE_ARG_NAME

cdef object SIGNATURE_TREE_EMPTY
cdef object SIGNATURE_TREE_B
cdef object SIGNATURE_TREE_N
cdef object SIGNATURE_TREE_O
cdef object SIGNATURE_TREE_S
cdef object SIGNATURE_TREE_U
cdef object SIGNATURE_TREE_Y

cdef object SIGNATURE_TREE_AS
cdef object SIGNATURE_TREE_AS_TYPES_0
cdef object SIGNATURE_TREE_AO
cdef object SIGNATURE_TREE_AO_TYPES_0
cdef object SIGNATURE_TREE_A_SV
cdef object SIGNATURE_TREE_A_SV_TYPES_0
cdef object SIGNATURE_TREE_SA_SV_AS
cdef object SIGNATURE_TREE_SA_SV_AS_TYPES_1
cdef object SIGNATURE_TREE_SA_SV_AS_TYPES_2
cdef object SIGNATURE_TREE_OAS
cdef object SIGNATURE_TREE_OAS_TYPES_1
cdef object SIGNATURE_TREE_OA_SA_SV
cdef object SIGNATURE_TREE_OA_SA_SV_TYPES_1
cdef object SIGNATURE_TREE_AY
cdef object SIGNATURE_TREE_AY_TYPES_0
cdef object SIGNATURE_TREE_A_QV
cdef object SIGNATURE_TREE_A_QV_TYPES_0
cdef object SIGNATURE_TREE_A_OA_SA_SV
cdef object SIGNATURE_TREE_A_OA_SA_SV_TYPES_0
cdef SignatureTree SIGNATURE_TREE_EMPTY
cdef SignatureTree SIGNATURE_TREE_B
cdef SignatureTree SIGNATURE_TREE_N
cdef SignatureTree SIGNATURE_TREE_O
cdef SignatureTree SIGNATURE_TREE_S
cdef SignatureTree SIGNATURE_TREE_U
cdef SignatureTree SIGNATURE_TREE_Y

cdef SignatureTree SIGNATURE_TREE_AS
cdef SignatureType SIGNATURE_TREE_AS_TYPES_0
cdef SignatureTree SIGNATURE_TREE_AO
cdef SignatureType SIGNATURE_TREE_AO_TYPES_0
cdef SignatureTree SIGNATURE_TREE_A_SV
cdef SignatureType SIGNATURE_TREE_A_SV_TYPES_0
cdef SignatureTree SIGNATURE_TREE_SA_SV_AS
cdef SignatureType SIGNATURE_TREE_SA_SV_AS_TYPES_1
cdef SignatureType SIGNATURE_TREE_SA_SV_AS_TYPES_2
cdef SignatureTree SIGNATURE_TREE_OAS
cdef SignatureType SIGNATURE_TREE_OAS_TYPES_1
cdef SignatureTree SIGNATURE_TREE_OA_SA_SV
cdef SignatureType SIGNATURE_TREE_OA_SA_SV_TYPES_1
cdef SignatureTree SIGNATURE_TREE_AY
cdef SignatureType SIGNATURE_TREE_AY_TYPES_0
cdef SignatureTree SIGNATURE_TREE_A_QV
cdef SignatureType SIGNATURE_TREE_A_QV_TYPES_0
cdef SignatureTree SIGNATURE_TREE_A_OA_SA_SV
cdef SignatureType SIGNATURE_TREE_A_OA_SA_SV_TYPES_0

cdef unsigned int TOKEN_O_AS_INT
cdef unsigned int TOKEN_S_AS_INT
Expand Down Expand Up @@ -133,23 +133,23 @@ cdef class Unmarshaller:
)
cdef _read_to_pos(self, unsigned long pos)

cpdef read_boolean(self, object type_)
cpdef read_boolean(self, SignatureType type_)

cdef _read_boolean(self)

cpdef read_uint32_unpack(self, object type_)
cpdef read_uint32_unpack(self, SignatureType type_)

cdef unsigned int _read_uint32_unpack(self)

cpdef read_int16_unpack(self, object type_)
cpdef read_int16_unpack(self, SignatureType type_)

cdef int _read_int16_unpack(self)

cpdef read_uint16_unpack(self, object type_)
cpdef read_uint16_unpack(self, SignatureType type_)

cdef unsigned int _read_uint16_unpack(self)

cpdef read_string_unpack(self, object type_)
cpdef read_string_unpack(self, SignatureType type_)

@cython.locals(
str_start=cython.uint,
Expand All @@ -158,15 +158,16 @@ cdef class Unmarshaller:

cdef _read_variant(self)

cpdef read_array(self, object type_)
cpdef read_array(self, SignatureType type_)

@cython.locals(
beginning_pos=cython.ulong,
array_length=cython.uint,
child_type=SignatureType,
)
cdef _read_array(self, object type_)
cdef _read_array(self, SignatureType type_)

cpdef read_signature(self, object type_)
cpdef read_signature(self, SignatureType type_)

@cython.locals(
o=cython.ulong,
Expand Down
22 changes: 11 additions & 11 deletions src/dbus_fast/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def _read_to_pos(self, pos: int) -> None:
if len(data) + start_len != pos:
raise MARSHALL_STREAM_END_ERROR

def read_uint32_unpack(self, type_: SignatureType) -> int:
def read_uint32_unpack(self, type_) -> int:
return self._read_uint32_unpack()

def _read_uint32_unpack(self) -> int:
Expand All @@ -297,7 +297,7 @@ def _read_uint32_unpack(self) -> int:
)
return self._uint32_unpack(self._buf, self._pos - UINT32_SIZE)[0]

def read_uint16_unpack(self, type_: SignatureType) -> int:
def read_uint16_unpack(self, type_) -> int:
return self._read_uint16_unpack()

def _read_uint16_unpack(self) -> int:
Expand All @@ -308,7 +308,7 @@ def _read_uint16_unpack(self) -> int:
)
return self._uint16_unpack(self._buf, self._pos - UINT16_SIZE)[0]

def read_int16_unpack(self, type_: SignatureType) -> int:
def read_int16_unpack(self, type_) -> int:
return self._read_int16_unpack()

def _read_int16_unpack(self) -> int:
Expand All @@ -319,13 +319,13 @@ def _read_int16_unpack(self) -> int:
)
return self._int16_unpack(self._buf, self._pos - INT16_SIZE)[0]

def read_boolean(self, type_: SignatureType) -> bool:
def read_boolean(self, type_) -> bool:
return self._read_boolean()

def _read_boolean(self) -> bool:
return bool(self._read_uint32_unpack())

def read_string_unpack(self, type_: SignatureType) -> str:
def read_string_unpack(self, type_) -> str:
return self._read_string_unpack()

def _read_string_unpack(self) -> str:
Expand All @@ -341,7 +341,7 @@ def _read_string_unpack(self) -> str:
self._pos += self._uint32_unpack(self._buf, str_start - UINT32_SIZE)[0] + 1
return self._buf[str_start : self._pos - 1].decode()

def read_signature(self, type_: SignatureType) -> str:
def read_signature(self, type_) -> str:
return self._read_signature()

def _read_signature(self) -> str:
Expand All @@ -351,7 +351,7 @@ def _read_signature(self) -> str:
self._pos = o + signature_len + 1
return self._buf[o : o + signature_len].decode()

def read_variant(self, type_: SignatureType) -> Variant:
def read_variant(self, type_) -> Variant:
return self._read_variant()

def _read_variant(self) -> Variant:
Expand Down Expand Up @@ -402,23 +402,23 @@ def _read_variant(self) -> Variant:
False,
)

def read_struct(self, type_: SignatureType) -> List[Any]:
def read_struct(self, type_) -> List[Any]:
self._pos += -self._pos & 7 # align 8
readers = self._readers
return [
readers[child_type.token](self, child_type) for child_type in type_.children
]

def read_dict_entry(self, type_: SignatureType) -> Tuple[Any, Any]:
def read_dict_entry(self, type_) -> Tuple[Any, Any]:
self._pos += -self._pos & 7 # align 8
return self._readers[type_.children[0].token](
self, type_.children[0]
), self._readers[type_.children[1].token](self, type_.children[1])

def read_array(self, type_: SignatureType) -> Iterable[Any]:
def read_array(self, type_) -> Iterable[Any]:
return self._read_array(type_)

def _read_array(self, type_: SignatureType) -> Iterable[Any]:
def _read_array(self, type_) -> Iterable[Any]:
self._pos += -self._pos & 3 # align 4 for the array
self._pos += (
-self._pos & (UINT32_SIZE - 1)
Expand Down