feat: add additional cython types to the unmarshaller (#45)

This commit is contained in:
J. Nick Koston 2022-10-01 23:04:45 -10:00 committed by GitHub
parent d2ce4a1846
commit 0f279a5ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -12,7 +12,7 @@ cdef unsigned int HEADER_SIGNATURE_SIZE
cdef class Unmarshaller:
cdef object unix_fds
cdef object buf
cdef bytearray buf
cdef object view
cdef unsigned int pos
cdef object stream
@ -30,10 +30,15 @@ cdef class Unmarshaller:
@cython.locals(
start_len=cython.ulong,
missing_bytes=cython.ulong,
missing_bytes=cython.ulong
)
cpdef read_to_pos(self, unsigned long pos)
cpdef read_uint32_cast(self, object type_)
@cython.locals(
buf_bytes=cython.bytearray,
)
cpdef read_string_cast(self, type_ = *)
@cython.locals(
@ -54,6 +59,10 @@ cdef class Unmarshaller:
)
cpdef _read_header(self)
cpdef _read_body(self)
cpdef unmarshall(self)
@cython.locals(
beginning_pos=cython.ulong,
o=cython.ulong,

View File

@ -218,6 +218,10 @@ class Unmarshaller:
if len(data) + start_len != pos:
raise MarshallerStreamEndError()
def read_uint32_cast(self, signature: SignatureType) -> Any:
self.pos += UINT32_SIZE + (-self.pos & (UINT32_SIZE - 1)) # align
return self.view[self.pos - UINT32_SIZE : self.pos].cast(UINT32_CAST)[0]
def read_boolean(self, type_=None) -> bool:
return bool(self.readers[UINT32_SIGNATURE.token](self, UINT32_SIGNATURE))
@ -228,8 +232,7 @@ class Unmarshaller:
# read terminating '\0' byte as well (str_length + 1)
start_pos = self.pos - UINT32_SIZE
self.pos += self.view[start_pos : self.pos].cast(UINT32_CAST)[0] + 1
end_pos = self.pos - 1
return self.buf[str_start:end_pos].decode()
return self.buf[str_start : self.pos - 1].decode()
def read_string_unpack(self, type_=None) -> str:
"""Read a string using unpack."""
@ -397,12 +400,12 @@ class Unmarshaller:
to be resumed when more data comes in over the wire.
"""
try:
if not self.message_type:
if not self.msg_len:
self._read_header()
self._read_body()
except MarshallerStreamEndError:
return None
return self.message
return self._message
_complex_parsers_unpack: Dict[
str, Callable[["Unmarshaller", SignatureType], Any]
@ -426,6 +429,8 @@ class Unmarshaller:
"(": read_struct,
"{": read_dict_entry,
"v": read_variant,
"h": read_uint32_cast,
UINT32_DBUS_TYPE: read_uint32_cast,
}
_ctype_by_endian: Dict[