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>
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)
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")
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
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