From 15ac05fd45afb0d85f1806a77fc9ec47f4949f01 Mon Sep 17 00:00:00 2001 From: Mathieu Bridon Date: Thu, 9 Aug 2018 10:27:18 +0200 Subject: python: Fix inequality comparisons 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 Reviewed-by: Dylan Baker --- src/mesa/main/format_parser.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/mesa') diff --git a/src/mesa/main/format_parser.py b/src/mesa/main/format_parser.py index 3321ad33ffa..c0d73c9d22e 100644 --- a/src/mesa/main/format_parser.py +++ b/src/mesa/main/format_parser.py @@ -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: -- cgit v1.2.3