From 4d8e038a033a12ea75bd11274fc01d7158792368 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 17 Nov 2022 14:27:35 -0600 Subject: [PATCH] feat: add typing for SignatureType and SignatureTree --- src/dbus_fast/_private/unmarshaller.pxd | 73 +++++++++++++------------ src/dbus_fast/_private/unmarshaller.py | 22 ++++---- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/dbus_fast/_private/unmarshaller.pxd b/src/dbus_fast/_private/unmarshaller.pxd index a0045fab..1ae1014c 100644 --- a/src/dbus_fast/_private/unmarshaller.pxd +++ b/src/dbus_fast/_private/unmarshaller.pxd @@ -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 @@ -45,33 +45,33 @@ cdef object MESSAGE_TYPE_MAP cdef object 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 @@ -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, @@ -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, diff --git a/src/dbus_fast/_private/unmarshaller.py b/src/dbus_fast/_private/unmarshaller.py index 985198cb..d93a525c 100644 --- a/src/dbus_fast/_private/unmarshaller.py +++ b/src/dbus_fast/_private/unmarshaller.py @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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)