Anonymous avatar Anonymous committed 76115a5

Fix issue 208.

Comments (0)

Files changed (2)

python/google/protobuf/internal/reflection_test.py

          unittest_pb2.ForeignMessage(c=12)],
         list(proto.repeated_foreign_message))
 
+  def testConstructorTypeError(self):
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, optional_int32="foo")
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, optional_string=1234)
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, optional_nested_message=1234)
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, repeated_int32=1234)
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, repeated_int32=["foo"])
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, repeated_string=1234)
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, repeated_string=[1234])
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, repeated_nested_message=1234)
+    self.assertRaises(TypeError, unittest_pb2.TestAllTypes, repeated_nested_message=[1234])
+
+  def testConstructorInvalidatesCachedByteSize(self):
+    message = unittest_pb2.TestAllTypes(optional_int32 = 12)
+    self.assertEquals(2, message.ByteSize())
+
+    message = unittest_pb2.TestAllTypes(
+        optional_nested_message = unittest_pb2.TestAllTypes.NestedMessage())
+    self.assertEquals(3, message.ByteSize())
+
+    message = unittest_pb2.TestAllTypes(repeated_int32 = [12])
+    self.assertEquals(3, message.ByteSize())
+
+    message = unittest_pb2.TestAllTypes(
+        repeated_nested_message = [unittest_pb2.TestAllTypes.NestedMessage()])
+    self.assertEquals(3, message.ByteSize())
+
   def testSimpleHasBits(self):
     # Test a scalar.
     proto = unittest_pb2.TestAllTypes()
   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'))

python/google/protobuf/reflection.py

         copy.MergeFrom(field_value)
         self._fields[field] = copy
       else:
-        self._fields[field] = field_value
+        setattr(self, field_name, field_value)
 
   init.__module__ = None
   init.__doc__ = None
   CPPTYPE_MESSAGE = _FieldDescriptor.CPPTYPE_MESSAGE
 
   def MergeFrom(self, msg):
+    if not isinstance(msg, cls):
+      raise TypeError(
+          "Parameter to MergeFrom() must be instance of same class.")
+
     assert msg is not self
     self._Modified()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.