bt2: use `_extract_value()` on comparison
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Thu, 27 Jun 2019 23:38:59 +0000 (19:38 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Jun 2019 02:47:25 +0000 (22:47 -0400)
After further discussions, we realized that there was a better way to
address the issue that commit 7bb4180 is fixing.

The real issue was that we were unconditionally converting the `other`
object to the `complex` type even when neither objects were of the
`complex` type. This was causing problem because of the lost of
precision when storing very large integer value in a variable of
`complex` type.

The better solution is to convert both objects to the type of the wider
object, and that is what Python does automatically. To enable that, we
simply need to convert the `other` object to it's corresponding standard
numeric type [1]. This commit uses the existing `_extract_value()`
method to achieve that.

[1]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: Ib658d1fdaf41a0ee592212a101fa4ad0ea952eb4
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1561
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/bindings/python/bt2/bt2/field.py
src/bindings/python/bt2/bt2/value.py

index f5b9b59645365bcfcca2079101986fa516109602..d35cb25887f4752e299167f689a9589903e159e4 100644 (file)
@@ -99,13 +99,13 @@ class _NumericField(_Field):
             raise TypeError('unorderable types: {}() < {}()'.format(self.__class__.__name__,
                                                                     other.__class__.__name__))
 
-        return self._value < float(other)
+        return self._value < self._extract_value(other)
 
     def _spec_eq(self, other):
         if not isinstance(other, numbers.Number):
             return NotImplemented
 
-        return self._value == complex(other)
+        return self._value == self._extract_value(other)
 
     def __rmod__(self, other):
         return self._extract_value(other) % self._value
@@ -250,18 +250,6 @@ class _IntegralField(_NumericField, numbers.Integral):
         self.value = self | other
         return self
 
-    def __lt__(self, other):
-        if not isinstance(other, numbers.Integral):
-            return super().__lt__(other);
-
-        return self._value < int(other)
-
-    def _spec_eq(self, other):
-        if not isinstance(other, numbers.Integral):
-            return super()._spec_eq(other);
-
-        return self._value == int(other)
-
 
 class _IntegerField(_IntegralField, _Field):
     pass
index c5c23fe6783ab9ca018da78a752b2126cc85bd70..b6fb6769fbe938fee8a2adcc29cff5227aeac08f 100644 (file)
@@ -158,7 +158,7 @@ class _NumericValue(_Value):
             raise TypeError('unorderable types: {}() < {}()'.format(self.__class__.__name__,
                                                                     other.__class__.__name__))
 
-        return self._value < float(other)
+        return self._value < self._extract_value(other)
 
     def _spec_eq(self, other):
         pass
@@ -167,7 +167,7 @@ class _NumericValue(_Value):
         if not isinstance(other, numbers.Number):
             return False
 
-        return self._value == complex(other)
+        return self._value == self._extract_value(other)
 
     def __rmod__(self, other):
         return self._extract_value(other) % self._value
@@ -312,18 +312,6 @@ class _IntegralValue(_NumericValue, numbers.Integral):
         self.value = self | other
         return self
 
-    def __lt__(self, other):
-        if not isinstance(other, numbers.Integral):
-            return super().__lt__(other)
-
-        return self._value < int(other)
-
-    def __eq__(self, other):
-        if not isinstance(other, numbers.Integral):
-            return super().__eq__(other)
-
-        return self._value == int(other)
-
 
 class _RealValue(_NumericValue, numbers.Real):
     pass
This page took 0.027857 seconds and 4 git commands to generate.