diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 02b5c306..12d89b5b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,6 @@ repos: - id: debug-statements - id: check-builtin-literals - id: check-case-conflict - - id: check-docstring-first - id: check-json - id: check-toml - id: check-xml diff --git a/bench/unmarshall_passive.py b/bench/unmarshall_passive.py index 243d3d91..c563bdbc 100644 --- a/bench/unmarshall_passive.py +++ b/bench/unmarshall_passive.py @@ -21,6 +21,7 @@ def unmarhsall_bluez_rssi_message(): stream.seek(0) unmarshaller.unmarshall() + unmarshaller.unmarshall() count = 3000000 diff --git a/src/dbus_fast/_private/unmarshaller.pxd b/src/dbus_fast/_private/unmarshaller.pxd index f86b1431..76610f91 100644 --- a/src/dbus_fast/_private/unmarshaller.pxd +++ b/src/dbus_fast/_private/unmarshaller.pxd @@ -112,16 +112,18 @@ cdef class Unmarshaller: cdef unsigned int _body_len cdef unsigned int _serial cdef unsigned int _header_len - cdef object _message_type - cdef object _flag + cdef unsigned int _message_type + cdef unsigned int _flag cdef unsigned int _msg_len cdef unsigned int _is_native cdef object _uint32_unpack cdef object _int16_unpack cdef object _uint16_unpack cdef object _stream_reader + cdef object _sock_reader cdef bint _negotiate_unix_fd cdef bint _read_complete + cdef unsigned int _endian cdef _next_message(self) diff --git a/src/dbus_fast/_private/unmarshaller.py b/src/dbus_fast/_private/unmarshaller.py index fcf9ba9c..48b7239c 100644 --- a/src/dbus_fast/_private/unmarshaller.py +++ b/src/dbus_fast/_private/unmarshaller.py @@ -202,8 +202,10 @@ class Unmarshaller: "_uint16_unpack", "_is_native", "_stream_reader", + "_sock_reader", "_negotiate_unix_fd", "_read_complete", + "_endian", ) def __init__( @@ -236,6 +238,11 @@ def __init__( if isinstance(stream, io.BufferedRWPair) and hasattr(stream, "reader"): self._stream_reader = stream.reader.read # type: ignore[attr-defined] self._stream_reader = stream.read + elif self._negotiate_unix_fd: + self._sock_reader = self._sock.recvmsg + else: + self._sock_reader = self._sock.recv + self._endian = 0 def _next_message(self) -> None: """Reset the unmarshaller to its initial state. @@ -280,7 +287,7 @@ def _read_sock_with_fds(self, pos: _int, missing_bytes: _int) -> None: # This will raise BlockingIOError if there is no data to read # which we store in the MARSHALL_STREAM_END_ERROR object try: - recv = self._sock.recvmsg(missing_bytes, UNIX_FDS_CMSG_LENGTH) # type: ignore[union-attr] + recv = self._sock_reader(missing_bytes, UNIX_FDS_CMSG_LENGTH) # type: ignore[union-attr] except OSError as e: errno = e.errno if errno == EAGAIN or errno == EWOULDBLOCK: @@ -311,7 +318,7 @@ def _read_sock_without_fds(self, pos: _int) -> None: # which we store in the MARSHALL_STREAM_END_ERROR object while True: try: - data = self._sock.recv(DEFAULT_BUFFER_SIZE) # type: ignore[union-attr] + data = self._sock_reader(DEFAULT_BUFFER_SIZE) # type: ignore[union-attr] except OSError as e: errno = e.errno if errno == EAGAIN or errno == EWOULDBLOCK: @@ -650,7 +657,9 @@ def _read_header(self) -> None: self._msg_len = ( self._header_len + (-self._header_len & 7) + self._body_len ) # align 8 - self._readers = self._readers_by_type[endian] + if self._endian != endian: + self._readers = self._readers_by_type[endian] + self._endian = endian def _read_body(self) -> None: """Read the body of the message.""" diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index 6eb1df8d..f852f00e 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -435,14 +435,12 @@ def __repr__(self) -> str: ) -@lru_cache(maxsize=None) -def get_signature_tree(signature: str) -> SignatureTree: - """Get a signature tree for the given signature. +get_signature_tree = lru_cache(maxsize=None)(SignatureTree) +"""Get a signature tree for the given signature. - :param signature: The signature to get a tree for. - :type signature: str +:param signature: The signature to get a tree for. +:type signature: str - :returns: The signature tree for the given signature. - :rtype: :class:`SignatureTree` - """ - return SignatureTree(signature) +:returns: The signature tree for the given signature. +:rtype: :class:`SignatureTree` +"""