Ejemplo n.º 1
0
    def validate_value(self, value):
        cls = type(self)
        if not self.type_check_fn(value):
            raise EncodingTypeError(
                "Value of type {0} cannot be encoded by {1}".format(
                    type(value),
                    cls.__name__,
                )
            )

        illegal_value = (
            self.illegal_value_fn is not None and
            self.illegal_value_fn(value)
        )
        if illegal_value:
            raise IllegalValue(
                'Value {} cannot be encoded by {}'.format(repr(value), cls.__name__)
            )

        lower_bound, upper_bound = self.bounds_fn(self.value_bit_size)
        if value < lower_bound or value > upper_bound:
            raise ValueOutOfBounds(
                "Value {0} cannot be encoded in {1} bits.  Must be bounded "
                "between [{2}, {3}]".format(
                    repr(value),
                    self.value_bit_size,
                    lower_bound,
                    upper_bound,
                )
            )
Ejemplo n.º 2
0
    def validate_value(self, value):
        super().validate_value(value)

        with decimal.localcontext(abi_decimal_context):
            residue = value % (TEN**-self.frac_places)

        if residue > 0:
            raise IllegalValue(
                '{} cannot encode value {}: '
                'residue {} outside allowed fractional precision of {}'.format(
                    type(self).__name__,
                    repr(value),
                    repr(residue),
                    self.frac_places,
                ))