From 25bb9babab83dac379726dc2de47f6023a3834e9 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 28 Jun 2019 01:08:25 -0400 Subject: [PATCH] bt2: field.py: _value_to_int(): require `numbers.Integral` Be more strict in the _value_to_int() functions: require that the parameter's type is an instance of `numbers.Integral` instead of `numbers.Real` to make the following raise a type error: my_int_field.value = 17.5 I believe it's better to be strict here than to arbitrarily choose to use the parameter's value's floor. RealValue._value_to_float() already requires a real value, so this is just analogous. Signed-off-by: Philippe Proulx Change-Id: I3b981e90f7e2c133fa4d56e79333c4fe600eca8b Reviewed-on: https://review.lttng.org/c/babeltrace/+/1569 Tested-by: jenkins Reviewed-by: Francis Deslauriers Reviewed-by: Simon Marchi --- src/bindings/python/bt2/bt2/field.py | 8 ++++---- tests/bindings/python/bt2/test_field.py | 5 ----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/bindings/python/bt2/bt2/field.py b/src/bindings/python/bt2/bt2/field.py index 7d68d0e0..9882f023 100644 --- a/src/bindings/python/bt2/bt2/field.py +++ b/src/bindings/python/bt2/bt2/field.py @@ -211,8 +211,8 @@ class _UnsignedIntegerField(_IntegerField, _Field): _NAME = 'Unsigned integer' def _value_to_int(self, value): - if not isinstance(value, numbers.Real): - raise TypeError('expecting a real number object') + if not isinstance(value, numbers.Integral): + raise TypeError('expecting an integral number object') value = int(value) utils._check_uint64(value) @@ -234,8 +234,8 @@ class _SignedIntegerField(_IntegerField, _Field): _NAME = 'Signed integer' def _value_to_int(self, value): - if not isinstance(value, numbers.Real): - raise TypeError('expecting a real number object') + if not isinstance(value, numbers.Integral): + raise TypeError('expecting an integral number object') value = int(value) utils._check_int64(value) diff --git a/tests/bindings/python/bt2/test_field.py b/tests/bindings/python/bt2/test_field.py index 356e0532..a22d3f4a 100644 --- a/tests/bindings/python/bt2/test_field.py +++ b/tests/bindings/python/bt2/test_field.py @@ -636,11 +636,6 @@ class _TestIntegerFieldCommon(_TestNumericField): self._def.value = field self.assertEqual(self._def, raw) - def test_assign_float(self): - raw = 123.456 - self._def.value = raw - self.assertEqual(self._def, int(raw)) - def test_assign_invalid_type(self): with self.assertRaises(TypeError): self._def.value = 'yes' -- 2.34.1