def test_encode_decode_invalid_utf8(): data = b"\xc3\x00" # invalid utf-8 result = types.encode(types.decode(data)) assert isinstance(result, str) assert result == data.decode(errors="surrogateescape")
def deserialize(self, value): if value is None: return None value = types.decode(value).strip() try: event, active, bouncetime = value.split(",") except ValueError: return None if event not in self.valid_events: raise ValueError( f"invalid event for pin config {event} (Must be {self.valid_events})" ) if active not in self.valid_modes: raise ValueError( f"invalid event for pin config {active} (Must be one of {self.valid_modes})" ) try: bouncetime = int(bouncetime) except ValueError: raise ValueError( f"invalid bouncetime value for pin config {bouncetime}") return self.tuple_pinconfig(event, active, bouncetime)
def deserialize(self, value): value = types.decode(value) validators.validate_required(value, self._required) if not value: return None value = float(value) validators.validate_choice(value, self._choices) validators.validate_minimum(value, self._minimum) validators.validate_maximum(value, self._maximum) return value
def deserialize(self, value): if value is None: return None value = types.decode(value).strip() value = value.split(",") # At least Event, Active and Bouncetime settings required if len(value) < 3: return None event, active, bouncetime = value[0:3] if event not in self.valid_events: raise ValueError( f"invalid event for pin config {event} (Must be {self.valid_events})" ) if active not in self.valid_modes: raise ValueError( f"invalid event for pin config {active} (Must be one of {self.valid_modes})" ) try: bouncetime = int(bouncetime) except ValueError: raise ValueError( f"invalid bouncetime value for pin config {bouncetime}") options = {} for option in value[3:]: key, value = option.split("=") options[key] = value return self.tuple_pinconfig(event, active, bouncetime, options)
def test_decode(value, expected): assert types.decode(value) == expected