python: Fix inequality comparisons
authorMathieu Bridon <bochecha@daitauha.fr>
Thu, 9 Aug 2018 08:27:18 +0000 (10:27 +0200)
committerDylan Baker <dylan@pnwbakers.com>
Fri, 10 Aug 2018 15:45:59 +0000 (08:45 -0700)
On Python 3, executing `foo != bar` will first try to call
foo.__ne__(bar), and fallback on the opposite result of foo.__eq__(bar).

Python 2 does not do that.

As a result, those __eq__ methods were never called, when we were
testing for inequality.

Expliclty adding the __ne__ methods fixes this issue, in a way that is
compatible with both Python 2 and 3.

However, this means the __eq__ methods are now called when testing for
`foo != None`, so they need to be guarded correctly.

Signed-off-by: Mathieu Bridon <bochecha@daitauha.fr>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
src/amd/vulkan/vk_format_parse.py
src/gallium/auxiliary/util/u_format_parse.py
src/mesa/main/format_parser.py

index 00cf1adf5a7b6f0b83710498348717f6c0b04496..ba730388a7b302fd3f7f34aa3b1b572b9a24ad7a 100644 (file)
@@ -73,8 +73,14 @@ class Channel:
         return s
 
     def __eq__(self, other):
+        if other is None:
+            return False
+
         return self.type == other.type and self.norm == other.norm and self.pure == other.pure and self.size == other.size and self.scaled == other.scaled
 
+    def __ne__(self, other):
+        return not self == other
+
     def max(self):
         '''Maximum representable number.'''
         if self.type == FLOAT:
index d3874cd895b8d3e55ca73be2c59bea74cee2be46..48cc012cd2a76e175ece815a37acde2e713c543b 100644 (file)
@@ -72,8 +72,14 @@ class Channel:
         return s
 
     def __eq__(self, other):
+        if other is None:
+            return False
+
         return self.type == other.type and self.norm == other.norm and self.pure == other.pure and self.size == other.size
 
+    def __ne__(self, other):
+        return not self == other
+
     def max(self):
         '''Maximum representable number.'''
         if self.type == FLOAT:
index 3321ad33ffa19772a2d0b95606d22ee5edefeeba..c0d73c9d22e537cfb3207c6e48126b334a7348c3 100644 (file)
@@ -61,8 +61,14 @@ class Channel:
       return s
 
    def __eq__(self, other):
+      if other is None:
+         return False
+
       return self.type == other.type and self.norm == other.norm and self.size == other.size
 
+   def __ne__(self, other):
+      return not self.__eq__(other)
+
    def max(self):
       """Returns the maximum representable number."""
       if self.type == FLOAT: