Skip to content

Commit

Permalink
Use enum's constructor to resolve enum values
Browse files Browse the repository at this point in the history
Rather than creating a map of {value: enum}, let's just use Foo(value)
to resolve to the respective enum value.

This fixes a regression with introspection introduced in
commit 3282eed "Improve unmarshall performance":

  File "dbus_next/_private/unmarshaller.py", line 247, in _read_header
    self.flag = MESSAGE_FLAG_MAP[buffer[2]]
                ~~~~~~~~~~~~~~~~^^^^^^^^^^^
  KeyError: 0

MESSAGE_FLAG_MAP is built like this:
    MESSAGE_FLAG_MAP = {field.value: field for field in MessageFlag}

But MessageFlag is a IntFlag, so the zero value (NONE) is missing from
the iterator:

  >>> [f for f in dbus_next.constants.MessageFlag]
  [<MessageFlag.NO_REPLY_EXPECTED: 1>, <MessageFlag.NO_AUTOSTART: 2>,
   <MessageFlag.ALLOW_INTERACTIVE_AUTHORIZATION: 4>]

Resolving the enum through the constructor fixes this. MESSAGE_TYPE_MAP
and HEADER_NAME_MAP are changed in solidarity.

Fixes altdesktop#142
  • Loading branch information
whot authored and garyvdm committed May 10, 2024
1 parent ab566e1 commit 2ed3100
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
3 changes: 0 additions & 3 deletions dbus_next/_private/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ class HeaderField(Enum):
SENDER = 7
SIGNATURE = 8
UNIX_FDS = 9


HEADER_NAME_MAP = {field.value: field.name for field in HeaderField}
9 changes: 4 additions & 5 deletions dbus_next/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
LITTLE_ENDIAN,
BIG_ENDIAN,
PROTOCOL_VERSION,
HEADER_NAME_MAP,
)
from ..constants import MessageType, MessageFlag, MESSAGE_FLAG_MAP, MESSAGE_TYPE_MAP
from ..constants import MessageType, MessageFlag
from ..signature import SignatureTree, SignatureType, Variant
from ..errors import InvalidMessageError

Expand Down Expand Up @@ -233,7 +232,7 @@ def header_fields(self, header_length):
o = self.offset + 1
self.offset += signature_len + 2 # one for the byte, one for the '\0'
tree = SignatureTree._get(self.buf[o:o + signature_len].decode())
headers[HEADER_NAME_MAP[field_0]] = self.read_argument(tree.types[0])
headers[HeaderField(field_0).name] = self.read_argument(tree.types[0])
return headers

def _read_header(self):
Expand All @@ -243,8 +242,8 @@ def _read_header(self):
self.read_to_offset(HEADER_SIGNATURE_SIZE)
buffer = self.buf
endian = buffer[0]
self.message_type = MESSAGE_TYPE_MAP[buffer[1]]
self.flag = MESSAGE_FLAG_MAP[buffer[2]]
self.message_type = MessageType(buffer[1])
self.flag = MessageFlag(buffer[2])
protocol_version = buffer[3]

if endian != LITTLE_ENDIAN and endian != BIG_ENDIAN:
Expand Down
6 changes: 0 additions & 6 deletions dbus_next/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class MessageType(Enum):
SIGNAL = 4 #: A broadcast signal to subscribed connections


MESSAGE_TYPE_MAP = {field.value: field for field in MessageType}


class MessageFlag(IntFlag):
"""Flags that affect the behavior of sent and received messages
"""
Expand All @@ -29,9 +26,6 @@ class MessageFlag(IntFlag):
ALLOW_INTERACTIVE_AUTHORIZATION = 4


MESSAGE_FLAG_MAP = {field.value: field for field in MessageFlag}


class NameFlag(IntFlag):
"""A flag that affects the behavior of a name request.
"""
Expand Down

0 comments on commit 2ed3100

Please sign in to comment.