bt2: value.py: _IntegerValue._value_to_int(): require `numbers.Integral`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Jun 2019 04:44:04 +0000 (00:44 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 3 Jul 2019 01:31:27 +0000 (21:31 -0400)
Be more strict in _IntegerValue._value_to_int(): require that the
parameter's type is an instance of `numbers.Integral` instead of
`numbers.Real` to make the following raise a type error:

    bt2.UnsignedIntegerValue(17.5)

and

    a = bt2.UnsignedIntegerValue()
    a.value = 17.5

I believe it's better to be strict here than to arbitrarily choose to
use the parameter's value's floor.

BoolValue._value_to_bool() and RealValue._value_to_float() already
require resp. a boolean value and a real value, so this is just
analogous.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I374baf8c5d69400b687daabea873598eb2543a47
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1567
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/bindings/python/bt2/bt2/value.py
tests/bindings/python/bt2/test_value.py

index 4fbe799286683b2ab3f0c2bd4fb289b0c0802392..81a72ffa785e7d735736e839714c30db69dfd315 100644 (file)
@@ -286,8 +286,8 @@ class _IntegerValue(_IntegralValue):
         super().__init__(ptr)
 
     def _value_to_int(self, value):
         super().__init__(ptr)
 
     def _value_to_int(self, value):
-        if not isinstance(value, numbers.Real):
-            raise TypeError('expecting a number object')
+        if not isinstance(value, numbers.Integral):
+            raise TypeError('expecting an integral number object')
 
         value = int(value)
         self._check_int_range(value)
 
         value = int(value)
         self._check_int_range(value)
index 2d6e25109d7a3e2c6c71bb9d18664ed4c7084f0c..6f66930e93ca9c3e009fbd8434898fd48a79d30e 100644 (file)
@@ -736,7 +736,7 @@ class _TestIntegerValue(_TestNumericValue):
         del self._def_value
 
     def _assert_expecting_int(self):
         del self._def_value
 
     def _assert_expecting_int(self):
-        return self.assertRaisesRegex(TypeError, r'expecting a number object')
+        return self.assertRaisesRegex(TypeError, r'expecting an integral number object')
 
     def _assert_expecting_int64(self):
         return self.assertRaisesRegex(ValueError, r"expecting a signed 64-bit integral value")
 
     def _assert_expecting_int64(self):
         return self.assertRaisesRegex(ValueError, r"expecting a signed 64-bit integral value")
@@ -766,15 +766,6 @@ class _TestIntegerValue(_TestNumericValue):
         i = self._CLS(True)
         self.assertTrue(i)
 
         i = self._CLS(True)
         self.assertTrue(i)
 
-    def test_create_from_float(self):
-        i = self._CLS(99.6)
-        self.assertEqual(i, 99)
-
-    def test_create_from_vfloat(self):
-        f = bt2.create_value(17.5)
-        i = self._CLS(f)
-        self.assertEqual(i, 17)
-
     def test_create_from_unknown(self):
         class A:
             pass
     def test_create_from_unknown(self):
         class A:
             pass
@@ -806,11 +797,6 @@ class _TestIntegerValue(_TestNumericValue):
         self._def.value = bt2.create_value(raw)
         self.assertEqual(self._def, raw)
 
         self._def.value = bt2.create_value(raw)
         self.assertEqual(self._def, raw)
 
-    def test_assign_vfloat(self):
-        raw = 123.456
-        self._def.value = bt2.create_value(raw)
-        self.assertEqual(self._def, int(raw))
-
 
 class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase):
     _CLS = bt2.SignedIntegerValue
 
 class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase):
     _CLS = bt2.SignedIntegerValue
This page took 0.026749 seconds and 4 git commands to generate.