From 3dc0614bb91af3ef70b5f5414318e5f2e052e582 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 28 Jun 2019 01:03:18 -0400 Subject: [PATCH] bt2: field.py: numeric classes: remove __i*__ operators Those operators do a binary operation to a field object and another operand, and then set the result as the field object's value. Now that I think about it, it is weird to do something like: my_int_field += 17.5 and that, when the value of `my_int_field` was 23, its value after the operation becomes 40. This is not a problem for a native Python object because `a` does not name the same object after the operation: a = 23 type(a) # int id(a) # 140669243488160 a += 17.5 type(a) # float id(a) # 140669043054920 but we can't do this with our field object: this is internal Python's magic. So I believe we should be more strict. But this means you can't do: my_int_field += 17.5 anymore (a `TypeError` exception would be raised). This means a lot of specific automatically injected tests must conditionally exist (depending on whether or not the test is possible with the two operands, whereas it's always possible now). Instead of doing this work, I choose to completely remove the support for "self" operators. We can add useful ones in the future when we have time, but I don't even think they are super useful: because a field object becomes conceptually frozen as soon as you emit a message which owns it indirectly, there's no point in doing `+=`, `-=`, `**=`, `<<=`, and so on. For those rare cases, you can always do: my_int_field = my_int_field + 17 Signed-off-by: Philippe Proulx Change-Id: I5410ff8fdb2c3a1910e788542376d521601fdf8f Reviewed-on: https://review.lttng.org/c/babeltrace/+/1568 Tested-by: jenkins Reviewed-by: Francis Deslauriers Reviewed-by: Simon Marchi --- src/bindings/python/bt2/bt2/field.py | 48 ------- tests/bindings/python/bt2/test_field.py | 184 ------------------------ 2 files changed, 232 deletions(-) diff --git a/src/bindings/python/bt2/bt2/field.py b/src/bindings/python/bt2/bt2/field.py index ee283157..7d68d0e0 100644 --- a/src/bindings/python/bt2/bt2/field.py +++ b/src/bindings/python/bt2/bt2/field.py @@ -167,34 +167,6 @@ class _NumericField(_Field): def __rpow__(self, base): return self._extract_value(base) ** self._value - def __iadd__(self, other): - self.value = self + other - return self - - def __isub__(self, other): - self.value = self - other - return self - - def __imul__(self, other): - self.value = self * other - return self - - def __itruediv__(self, other): - self.value = self / other - return self - - def __ifloordiv__(self, other): - self.value = self // other - return self - - def __imod__(self, other): - self.value = self % other - return self - - def __ipow__(self, other): - self.value = self ** other - return self - class _IntegralField(_NumericField, numbers.Integral): def __lshift__(self, other): @@ -230,26 +202,6 @@ class _IntegralField(_NumericField, numbers.Integral): def __invert__(self): return ~self._value - def __ilshift__(self, other): - self.value = self << other - return self - - def __irshift__(self, other): - self.value = self >> other - return self - - def __iand__(self, other): - self.value = self & other - return self - - def __ixor__(self, other): - self.value = self ^ other - return self - - def __ior__(self, other): - self.value = self | other - return self - class _IntegerField(_IntegralField, _Field): pass diff --git a/tests/bindings/python/bt2/test_field.py b/tests/bindings/python/bt2/test_field.py index f6fc4ca3..356e0532 100644 --- a/tests/bindings/python/bt2/test_field.py +++ b/tests/bindings/python/bt2/test_field.py @@ -244,54 +244,6 @@ class _TestNumericField: with self.assertRaises(TypeError): op(self._def, None) - def _test_ibinop_value(self, op, rhs): - r, rv = self._binop(op, rhs) - - if r is None: - return - - # The inplace operators are special for field objects because - # they do not return a new, immutable object like it's the case - # for Python numbers. In Python, `a += 2`, where `a` is a number - # object, assigns a new number object reference to `a`, dropping - # the old reference. Since BT's field objects are mutable, we - # modify their internal value with the inplace operators. This - # means however that we can lose data in the process, for - # example: - # - # int_value_obj += 3.3 - # - # Here, if `int_value_obj` is a Python `int` with the value 2, - # it would be a `float` object after this, holding the value - # 5.3. In our case, if `int_value_obj` is an integer field - # object, 3.3 is converted to an `int` object (3) and added to - # the current value of `int_value_obj`, so after this the value - # of the object is 5. This does not compare to 5.3, which is - # why we also use the `int()` type here. - if isinstance(self._def, bt2.field._IntegerField): - rv = int(rv) - - self.assertEqual(r, rv) - - def _test_ibinop_type(self, op, rhs): - r, rv = self._binop(op, rhs) - - if r is None: - return - - self.assertIs(r, self._def) - - def _test_ibinop_invalid_unknown(self, op): - class A: - pass - - with self.assertRaises(TypeError): - op(self._def, A()) - - def _test_ibinop_invalid_none(self, op): - with self.assertRaises(TypeError): - op(self._def, None) - def _test_binop_rhs_false(self, test_cb, op): test_cb(op, False) @@ -502,90 +454,6 @@ class _TestNumericField: def _test_binop_lhs_value_same_zero_vfloat(self, op): self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op) - def _test_ibinop_type_false(self, op): - self._test_binop_rhs_false(self._test_ibinop_type, op) - - def _test_ibinop_type_true(self, op): - self._test_binop_rhs_true(self._test_ibinop_type, op) - - def _test_ibinop_type_pos_int(self, op): - self._test_binop_rhs_pos_int(self._test_ibinop_type, op) - - def _test_ibinop_type_neg_int(self, op): - self._test_binop_rhs_neg_int(self._test_ibinop_type, op) - - def _test_ibinop_type_zero_int(self, op): - self._test_binop_rhs_zero_int(self._test_ibinop_type, op) - - def _test_ibinop_type_pos_vint(self, op): - self._test_binop_rhs_pos_vint(self._test_ibinop_type, op) - - def _test_ibinop_type_neg_vint(self, op): - self._test_binop_rhs_neg_vint(self._test_ibinop_type, op) - - def _test_ibinop_type_zero_vint(self, op): - self._test_binop_rhs_zero_vint(self._test_ibinop_type, op) - - def _test_ibinop_type_pos_float(self, op): - self._test_binop_rhs_pos_float(self._test_ibinop_type, op) - - def _test_ibinop_type_neg_float(self, op): - self._test_binop_rhs_neg_float(self._test_ibinop_type, op) - - def _test_ibinop_type_zero_float(self, op): - self._test_binop_rhs_zero_float(self._test_ibinop_type, op) - - def _test_ibinop_type_pos_vfloat(self, op): - self._test_binop_rhs_pos_vfloat(self._test_ibinop_type, op) - - def _test_ibinop_type_neg_vfloat(self, op): - self._test_binop_rhs_neg_vfloat(self._test_ibinop_type, op) - - def _test_ibinop_type_zero_vfloat(self, op): - self._test_binop_rhs_zero_vfloat(self._test_ibinop_type, op) - - def _test_ibinop_value_false(self, op): - self._test_binop_rhs_false(self._test_ibinop_value, op) - - def _test_ibinop_value_true(self, op): - self._test_binop_rhs_true(self._test_ibinop_value, op) - - def _test_ibinop_value_pos_int(self, op): - self._test_binop_rhs_pos_int(self._test_ibinop_value, op) - - def _test_ibinop_value_neg_int(self, op): - self._test_binop_rhs_neg_int(self._test_ibinop_value, op) - - def _test_ibinop_value_zero_int(self, op): - self._test_binop_rhs_zero_int(self._test_ibinop_value, op) - - def _test_ibinop_value_pos_vint(self, op): - self._test_binop_rhs_pos_vint(self._test_ibinop_value, op) - - def _test_ibinop_value_neg_vint(self, op): - self._test_binop_rhs_neg_vint(self._test_ibinop_value, op) - - def _test_ibinop_value_zero_vint(self, op): - self._test_binop_rhs_zero_vint(self._test_ibinop_value, op) - - def _test_ibinop_value_pos_float(self, op): - self._test_binop_rhs_pos_float(self._test_ibinop_value, op) - - def _test_ibinop_value_neg_float(self, op): - self._test_binop_rhs_neg_float(self._test_ibinop_value, op) - - def _test_ibinop_value_zero_float(self, op): - self._test_binop_rhs_zero_float(self._test_ibinop_value, op) - - def _test_ibinop_value_pos_vfloat(self, op): - self._test_binop_rhs_pos_vfloat(self._test_ibinop_value, op) - - def _test_ibinop_value_neg_vfloat(self, op): - self._test_binop_rhs_neg_vfloat(self._test_ibinop_value, op) - - def _test_ibinop_value_zero_vfloat(self, op): - self._test_binop_rhs_zero_vfloat(self._test_ibinop_value, op) - def test_bool_op(self): self.assertEqual(bool(self._def), bool(self._def_value)) @@ -648,22 +516,6 @@ _BINOPS = ( ) -_IBINOPS = ( - ('iadd', operator.iadd), - ('iand', operator.iand), - ('ifloordiv', operator.ifloordiv), - ('ilshift', operator.ilshift), - ('imod', operator.imod), - ('imul', operator.imul), - ('ior', operator.ior), - ('ipow', operator.ipow), - ('irshift', operator.irshift), - ('isub', operator.isub), - ('itruediv', operator.itruediv), - ('ixor', operator.ixor), -) - - _UNARYOPS = ( ('neg', operator.neg), ('pos', operator.pos), @@ -684,9 +536,6 @@ def _inject_numeric_testing_methods(cls): def test_binop_name(suffix): return 'test_binop_{}_{}'.format(name, suffix) - def test_ibinop_name(suffix): - return 'test_ibinop_{}_{}'.format(name, suffix) - def test_unaryop_name(suffix): return 'test_unaryop_{}_{}'.format(name, suffix) @@ -758,39 +607,6 @@ def _inject_numeric_testing_methods(cls): setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop)) setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop)) - # inject testing methods for each inplace binary operation - for name, ibinop in _IBINOPS: - setattr(cls, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField._test_ibinop_invalid_unknown, op=ibinop)) - setattr(cls, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField._test_ibinop_invalid_none, op=ibinop)) - setattr(cls, test_ibinop_name('type_true'), partialmethod(_TestNumericField._test_ibinop_type_true, op=ibinop)) - setattr(cls, test_ibinop_name('value_true'), partialmethod(_TestNumericField._test_ibinop_value_true, op=ibinop)) - setattr(cls, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField._test_ibinop_type_pos_int, op=ibinop)) - setattr(cls, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField._test_ibinop_type_pos_vint, op=ibinop)) - setattr(cls, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField._test_ibinop_value_pos_int, op=ibinop)) - setattr(cls, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField._test_ibinop_value_pos_vint, op=ibinop)) - setattr(cls, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField._test_ibinop_type_neg_int, op=ibinop)) - setattr(cls, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField._test_ibinop_type_neg_vint, op=ibinop)) - setattr(cls, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField._test_ibinop_value_neg_int, op=ibinop)) - setattr(cls, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField._test_ibinop_value_neg_vint, op=ibinop)) - setattr(cls, test_ibinop_name('type_false'), partialmethod(_TestNumericField._test_ibinop_type_false, op=ibinop)) - setattr(cls, test_ibinop_name('value_false'), partialmethod(_TestNumericField._test_ibinop_value_false, op=ibinop)) - setattr(cls, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField._test_ibinop_type_zero_int, op=ibinop)) - setattr(cls, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField._test_ibinop_type_zero_vint, op=ibinop)) - setattr(cls, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField._test_ibinop_value_zero_int, op=ibinop)) - setattr(cls, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField._test_ibinop_value_zero_vint, op=ibinop)) - setattr(cls, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField._test_ibinop_type_pos_float, op=ibinop)) - setattr(cls, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField._test_ibinop_type_neg_float, op=ibinop)) - setattr(cls, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_pos_vfloat, op=ibinop)) - setattr(cls, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_neg_vfloat, op=ibinop)) - setattr(cls, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField._test_ibinop_value_pos_float, op=ibinop)) - setattr(cls, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField._test_ibinop_value_neg_float, op=ibinop)) - setattr(cls, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_pos_vfloat, op=ibinop)) - setattr(cls, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_neg_vfloat, op=ibinop)) - setattr(cls, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField._test_ibinop_type_zero_float, op=ibinop)) - setattr(cls, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_zero_vfloat, op=ibinop)) - setattr(cls, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField._test_ibinop_value_zero_float, op=ibinop)) - setattr(cls, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_zero_vfloat, op=ibinop)) - class _TestIntegerFieldCommon(_TestNumericField): def test_assign_true(self): -- 2.34.1