From 09a926c19afa6e137319da5f9fd60b0852946ea4 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Thu, 27 Jun 2019 19:38:59 -0400 Subject: [PATCH] bt2: use `_extract_value()` on comparison 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 Change-Id: Ib658d1fdaf41a0ee592212a101fa4ad0ea952eb4 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1561 Tested-by: jenkins Reviewed-by: Philippe Proulx --- src/bindings/python/bt2/bt2/field.py | 16 ++-------------- src/bindings/python/bt2/bt2/value.py | 16 ++-------------- 2 files changed, 4 insertions(+), 28 deletions(-) diff --git a/src/bindings/python/bt2/bt2/field.py b/src/bindings/python/bt2/bt2/field.py index f5b9b596..d35cb258 100644 --- a/src/bindings/python/bt2/bt2/field.py +++ b/src/bindings/python/bt2/bt2/field.py @@ -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 diff --git a/src/bindings/python/bt2/bt2/value.py b/src/bindings/python/bt2/bt2/value.py index c5c23fe6..b6fb6769 100644 --- a/src/bindings/python/bt2/bt2/value.py +++ b/src/bindings/python/bt2/bt2/value.py @@ -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 -- 2.34.1