Verified Commit bb8ac263 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Change type of Message.ident to int

Store the ident value as an integer instead of a byte string with
a length of one, which would inevitably cause trouble somewhere.

Also incidentally fixes an issue packing/unpacking messages in the
unlikely event that a message is greater than 2^31 bytes due to using
a signed integer type for the message size.
parent 7e01d89a
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -212,9 +212,9 @@ class FilterProtocol:

	def __init__(self, *, abort_on_unknown: bool = False) -> None:
		self.abort_on_unknown = abort_on_unknown
		self.nr = set[bytes]()
		self.actions = set[bytes]([messages.Progress.ident])
		self.state: tuple[messages.Message, set[bytes]]|None = None
		self.nr = set[int]()
		self.actions = set[int]([messages.Progress.ident])
		self.state: tuple[messages.Message, set[int]]|None = None
		self._optflags = ProtocolFlags(0)
		self._actflags = ActionFlags(0)

+6 −6
Original line number Diff line number Diff line
@@ -238,18 +238,18 @@ class Message(metaclass=ABCMeta):
	>>> del buf[:10]
	"""

	ident: ClassVar[bytes]
	ident: ClassVar[int]

	_message_classes = dict[bytes, "type[Message]"]()
	_hdr_struct = Struct("!lc")
	_message_classes = dict[int, "type[Message]"]()
	_hdr_struct = Struct("!LB")

	@classmethod
	def __init_subclass__(cls, /, ident: bytes = b""):
		super().__init_subclass__()
		if ident:  # pragma: no-branch
			assert len(ident) == 1
			Message._message_classes[ident] = cls
			cls.ident = ident
			cls.ident = ident[0]
			Message._message_classes[cls.ident] = cls

	@classmethod
	def unpack(cls, buf: FixedSizeBuffer) -> tuple[Message, int]:
@@ -261,7 +261,7 @@ class Message(metaclass=ABCMeta):
			raise NeedsMore
		size, ident = cls._hdr_struct.unpack_from(buf[:])
		assert isinstance(size, int)
		assert isinstance(ident, bytes)
		assert isinstance(ident, int)
		end = hdr_size + size - 1
		if buf.filled < end:
			raise NeedsMore