From d41946f9ceea1afaece68700d072a4ad9211a62a Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Fri, 19 Jun 2020 09:23:52 -0700 Subject: [PATCH] Make _cmp generic for all EDNS Options. It now compares the wire formats of two objects of the same class. Previously we did type-specific stuff for ECSOption, but we only checked the padded address info, not the srclen or scopelen, so different options could compare the same. Also, it's not clear that there's any better semantic ordering of options than the wire format, so we will just go for simplicity and use the generic implementation. --- dns/edns.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/dns/edns.py b/dns/edns.py index 7d781378..82e7abcb 100644 --- a/dns/edns.py +++ b/dns/edns.py @@ -69,7 +69,7 @@ class Option: Returns a ``bytes`` or ``None``. """ - raise NotImplementedError + raise NotImplementedError # pragma: no cover @classmethod def from_wire(cls, otype, wire, current, olen): @@ -87,14 +87,20 @@ class Option: Returns a ``dns.edns.Option``. """ - raise NotImplementedError + raise NotImplementedError # pragma: no cover def _cmp(self, other): """Compare an EDNS option with another option of the same type. Returns < 0 if < *other*, 0 if == *other*, and > 0 if > *other*. """ - raise NotImplementedError + wire = self.to_wire() + owire = other.to_wire() + if wire == owire: + return 0 + if wire > owire: + return 1 + return -1 def __eq__(self, other): if not isinstance(other, Option): @@ -105,9 +111,9 @@ class Option: def __ne__(self, other): if not isinstance(other, Option): - return False + return True if self.otype != other.otype: - return False + return True return self._cmp(other) != 0 def __lt__(self, other): @@ -160,13 +166,6 @@ class GenericOption(Option): def from_wire(cls, otype, wire, current, olen): return cls(otype, wire[current: current + olen]) - def _cmp(self, other): - if self.data == other.data: - return 0 - if self.data > other.data: - return 1 - return -1 - def __str__(self): return self.to_text() @@ -299,13 +298,6 @@ class ECSOption(Option): return cls(addr, src, scope) - def _cmp(self, other): - if self.addrdata == other.addrdata: - return 0 - if self.addrdata > other.addrdata: - return 1 - return -1 - def __str__(self): return self.to_text() -- 2.47.3