Skip to content

Instantly share code, notes, and snippets.

@xoyowade
Last active December 18, 2015 10:59
Show Gist options
  • Save xoyowade/5772207 to your computer and use it in GitHub Desktop.
Save xoyowade/5772207 to your computer and use it in GitHub Desktop.
[Patch] Fix short repeated format parsing error for protobuf-2.5.0-python
diff --git a/text_format.py b/text_format.py
index 24dd07f..1afd11a 100755
--- a/text_format.py
+++ b/text_format.py
@@ -235,8 +235,16 @@ def _MergeField(tokenizer, message):
raise tokenizer.ParseErrorPreviousToken('Expected "%s".' % (end_token))
_MergeField(tokenizer, sub_message)
else:
- _MergeScalarField(tokenizer, message, field)
-
+ tokenizer.Consume(':')
+ if field.label == descriptor.FieldDescriptor.LABEL_REPEATED and tokenizer.TryConsume('['):
+ # Short repeated format, e.g. 'foo: [1, 2, 3]'
+ while True:
+ _MergeScalarField(tokenizer, message, field)
+ if tokenizer.TryConsume(']'):
+ break
+ tokenizer.Consume(',')
+ else:
+ _MergeScalarField(tokenizer, message, field)
def _MergeScalarField(tokenizer, message, field):
"""Merges a single protocol message scalar field into a message.
@@ -250,7 +258,6 @@ def _MergeScalarField(tokenizer, message, field):
ParseError: In case of ASCII parsing problems.
RuntimeError: On runtime errors.
"""
- tokenizer.Consume(':')
value = None
if field.type in (descriptor.FieldDescriptor.TYPE_INT32,
diff --git a/internal/text_format_test.py b/internal/text_format_test.py
index 4b1b4f5..fee3005 100755
--- a/internal/text_format_test.py
+++ b/internal/text_format_test.py
@@ -449,6 +449,23 @@ class TextFormatTest(unittest.TestCase):
message.repeated_string[4])
self.assertEqual(SLASH + 'x20', message.repeated_string[5])
+ def testMergeShortRepeatedPrimitiveFieleds(self):
+ # protobuf-python doesn't yet support printing short repeated format,
+ # so a hard coded text is required for testing text_format.Merge
+ parsed_message = unittest_pb2.TestAllTypes()
+ text = ('repeated_int32: [123, 456, 789]\n'
+ 'repeated_nested_enum: [FOO, BAR]\n')
+ text_format.Merge(text, parsed_message)
+
+ message = unittest_pb2.TestAllTypes()
+ message.repeated_int32.append(123)
+ message.repeated_int32.append(456)
+ message.repeated_int32.append(789)
+ message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.FOO)
+ message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAR)
+
+ self.assertEquals(message, parsed_message)
+
def assertRaisesWithMessage(self, e_class, e, func, *args, **kwargs):
"""Same as assertRaises, but also compares the exception message."""
if hasattr(e_class, '__name__'):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment