]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Make _cmp generic for all EDNS Options. It now compares the wire formats
authorBob Halley <halley@dnspython.org>
Fri, 19 Jun 2020 16:23:52 +0000 (09:23 -0700)
committerBob Halley <halley@dnspython.org>
Fri, 19 Jun 2020 16:23:52 +0000 (09:23 -0700)
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

index 7d7813788ed1b2406821660a4fe1367a37bbd042..82e7abcb861514b7b0caecf752c1b829a76e07bd 100644 (file)
@@ -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()