Commit 0c293def authored by kenton@google.com's avatar kenton@google.com

Fix issue 207

parent 15b675ee
...@@ -1035,6 +1035,19 @@ class ReflectionTest(unittest.TestCase): ...@@ -1035,6 +1035,19 @@ class ReflectionTest(unittest.TestCase):
self.assertEqual(222, ext2[1].bb) self.assertEqual(222, ext2[1].bb)
self.assertEqual(333, ext2[2].bb) self.assertEqual(333, ext2[2].bb)
def testMergeFromBug(self):
message1 = unittest_pb2.TestAllTypes()
message2 = unittest_pb2.TestAllTypes()
# Cause optional_nested_message to be instantiated within message1, even
# though it is not considered to be "present".
message1.optional_nested_message
self.assertFalse(message1.HasField('optional_nested_message'))
# Merge into message2. This should not instantiate the field is message2.
message2.MergeFrom(message1)
self.assertFalse(message2.HasField('optional_nested_message'))
def testCopyFromSingularField(self): def testCopyFromSingularField(self):
# Test copy with just a singular field. # Test copy with just a singular field.
proto1 = unittest_pb2.TestAllTypes() proto1 = unittest_pb2.TestAllTypes()
......
...@@ -943,13 +943,21 @@ def _AddMergeFromMethod(cls): ...@@ -943,13 +943,21 @@ def _AddMergeFromMethod(cls):
fields = self._fields fields = self._fields
for field, value in msg._fields.iteritems(): for field, value in msg._fields.iteritems():
if field.label == LABEL_REPEATED or field.cpp_type == CPPTYPE_MESSAGE: if field.label == LABEL_REPEATED:
field_value = fields.get(field) field_value = fields.get(field)
if field_value is None: if field_value is None:
# Construct a new object to represent this field. # Construct a new object to represent this field.
field_value = field._default_constructor(self) field_value = field._default_constructor(self)
fields[field] = field_value fields[field] = field_value
field_value.MergeFrom(value) field_value.MergeFrom(value)
elif field.cpp_type == CPPTYPE_MESSAGE:
if value._is_present_in_parent:
field_value = fields.get(field)
if field_value is None:
# Construct a new object to represent this field.
field_value = field._default_constructor(self)
fields[field] = field_value
field_value.MergeFrom(value)
else: else:
self._fields[field] = value self._fields[field] = value
cls.MergeFrom = MergeFrom cls.MergeFrom = MergeFrom
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment