-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nextgen Proto Pythonic API: “Add-on” proto for length prefixed serial…
…ize/parse Added the following methods: serialize_length_prefixed(message: Message, output: io.BytesIO) -> None parse_length_prefixed(message_class: Type[Message], input_bytes: io.BytesIO) -> Message The output of serialize_length_prefixed should be BytesIO or custom buffered IO that data should be written to. The output stream must be buffered, e.g. using https://docs.python.org/3/library/io.html#buffered-streams. PiperOrigin-RevId: 638375900
- Loading branch information
1 parent
fdc7f65
commit 3a9f074
Showing
5 changed files
with
243 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# Protocol Buffers - Google's data interchange format | ||
# Copyright 2008 Google Inc. All rights reserved. | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
"""Test decoder.""" | ||
|
||
import io | ||
import unittest | ||
|
||
from google.protobuf.internal import decoder | ||
from google.protobuf.internal import testing_refleaks | ||
|
||
|
||
_INPUT_BYTES = b'\x84r\x12' | ||
_EXPECTED = (14596, 18) | ||
|
||
|
||
@testing_refleaks.TestCase | ||
class DecoderTest(unittest.TestCase): | ||
|
||
def test_decode_varint_bytes(self): | ||
(size, pos) = decoder._DecodeVarint(_INPUT_BYTES, 0) | ||
self.assertEqual(size, _EXPECTED[0]) | ||
self.assertEqual(pos, 2) | ||
|
||
(size, pos) = decoder._DecodeVarint(_INPUT_BYTES, 2) | ||
self.assertEqual(size, _EXPECTED[1]) | ||
self.assertEqual(pos, 3) | ||
|
||
def test_decode_varint_bytes_empty(self): | ||
with self.assertRaises(IndexError) as context: | ||
(size, pos) = decoder._DecodeVarint(b'', 0) | ||
self.assertIn('index out of range', str(context.exception)) | ||
|
||
def test_decode_varint_bytesio(self): | ||
index = 0 | ||
input_io = io.BytesIO(_INPUT_BYTES) | ||
while True: | ||
size = decoder._DecodeVarint(input_io) | ||
if size is None: | ||
break | ||
self.assertEqual(size, _EXPECTED[index]) | ||
index += 1 | ||
self.assertEqual(index, len(_EXPECTED)) | ||
|
||
def test_decode_varint_bytesio_empty(self): | ||
input_io = io.BytesIO(b'') | ||
size = decoder._DecodeVarint(input_io) | ||
self.assertEqual(size, None) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters