From 408ff0389a0ab62df29b715cb51fa3c2ed413f0a Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 17 Nov 2022 19:34:49 +1000 Subject: [PATCH] Use enum's constructor to resolve enum values 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] [, , ] Resolving the enum through the constructor fixes this. MESSAGE_TYPE_MAP and HEADER_NAME_MAP are changed in solidarity. Fixes #142 --- dbus_next/_private/constants.py | 3 --- dbus_next/_private/unmarshaller.py | 9 ++++----- dbus_next/constants.py | 6 ------ 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/dbus_next/_private/constants.py b/dbus_next/_private/constants.py index d9ee0c2..22a6b80 100644 --- a/dbus_next/_private/constants.py +++ b/dbus_next/_private/constants.py @@ -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} diff --git a/dbus_next/_private/unmarshaller.py b/dbus_next/_private/unmarshaller.py index 55b9884..146b874 100644 --- a/dbus_next/_private/unmarshaller.py +++ b/dbus_next/_private/unmarshaller.py @@ -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 @@ -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): @@ -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: diff --git a/dbus_next/constants.py b/dbus_next/constants.py index b9494f8..6afc9b8 100644 --- a/dbus_next/constants.py +++ b/dbus_next/constants.py @@ -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 """ @@ -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. """