def test_decode(self, value, expected_value): field = Repeated(Int32, number=1, packed=False) stream = io.BytesIO(value) data = [] for _ in expected_value: decode_varint(stream) data.append(field.decode(stream)) assert data == expected_value
def decode(self, stream: IO) -> int: value = decode_varint(stream) if value > 2**63 - 1: value -= 2**64 return value
def decode(self, stream: IO) -> Optional[int]: value = decode_varint(stream) # Specification: omit value that's not in the enum's variants try: return self.py_enum(value) except ValueError: return None
def test_encode_decode(self, data): encoded = Bytes(number=1).encode_value(data) stream = io.BytesIO(encoded) length = decode_varint(stream) assert length == len(data) stream.seek(0) decoded_data = Bytes(number=1).decode(stream) assert decoded_data == data
def decode(self, stream: IO) -> list: length = decode_varint(stream) items = [] data = stream.read(length) if len(data) < length: raise MessageDecodeError( f'Expected to read {length:_} bytes, read {len(data):_} bytes instead' ) stream = io.BytesIO(data) while stream.seek(0, io.SEEK_CUR) < length: items.append(self.field.decode(stream)) return items
def test_decode_varint_raises_when_max_data_length_exceeded(): bad_value = bytes([255] * 10 + [1]) stream = io.BytesIO(bad_value) with pytest.raises(MessageDecodeError): decode_varint(stream)
def test_decode_varint_raises_on_eof(bad_input): encoded_value = bytes(bad_input) stream = io.BytesIO(encoded_value) with pytest.raises(MessageDecodeError): decode_varint(stream)
def test_encode_decode_varint(value): encoded_value = encode_varint(value) stream = io.BytesIO(encoded_value) decoded_value = decode_varint(stream) assert decoded_value == value
def decode(self, stream: IO): length = decode_varint(stream) message_stream = io.BytesIO(stream.read(length)) return self.of_type.from_stream(message_stream)
def decode(self, stream: IO) -> bool: value = decode_varint(stream) return bool(value)
def decode(self, stream: IO) -> int: zig_zag_value = decode_varint(stream) return decode_zig_zag(zig_zag_value)
def decode(self, stream: IO) -> int: return decode_varint(stream)