Skip to content

Commit

Permalink
feat: add additional cython types to the unmarshaller (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 2, 2022
1 parent d2ce4a1 commit 0f279a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/dbus_fast/_private/unmarshaller.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions src/dbus_fast/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ def read_to_pos(self, pos) -> None:
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))

Expand All @@ -228,8 +232,7 @@ def read_string_cast(self, type_=None) -> str:
# 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."""
Expand Down Expand Up @@ -397,12 +400,12 @@ def unmarshall(self):
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]
Expand All @@ -426,6 +429,8 @@ def unmarshall(self):
"(": read_struct,
"{": read_dict_entry,
"v": read_variant,
"h": read_uint32_cast,
UINT32_DBUS_TYPE: read_uint32_cast,
}

_ctype_by_endian: Dict[
Expand Down

0 comments on commit 0f279a5

Please sign in to comment.