예제 #1
0
    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
예제 #2
0
    def decode(self, stream: IO) -> int:
        value = decode_varint(stream)

        if value > 2**63 - 1:
            value -= 2**64

        return value
예제 #3
0
    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
예제 #4
0
    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
예제 #5
0
    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
예제 #6
0
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)
예제 #7
0
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)
예제 #8
0
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
예제 #9
0
 def decode(self, stream: IO):
     length = decode_varint(stream)
     message_stream = io.BytesIO(stream.read(length))
     return self.of_type.from_stream(message_stream)
예제 #10
0
 def decode(self, stream: IO) -> bool:
     value = decode_varint(stream)
     return bool(value)
예제 #11
0
 def decode(self, stream: IO) -> int:
     zig_zag_value = decode_varint(stream)
     return decode_zig_zag(zig_zag_value)
예제 #12
0
 def decode(self, stream: IO) -> int:
     return decode_varint(stream)