Commit 27028bcb authored by kenton@google.com's avatar kenton@google.com

Fix issue 208.

parent 0c293def
...@@ -200,6 +200,32 @@ class ReflectionTest(unittest.TestCase): ...@@ -200,6 +200,32 @@ class ReflectionTest(unittest.TestCase):
unittest_pb2.ForeignMessage(c=12)], unittest_pb2.ForeignMessage(c=12)],
list(proto.repeated_foreign_message)) 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): def testSimpleHasBits(self):
# Test a scalar. # Test a scalar.
proto = unittest_pb2.TestAllTypes() proto = unittest_pb2.TestAllTypes()
......
...@@ -384,7 +384,7 @@ def _AddInitMethod(message_descriptor, cls): ...@@ -384,7 +384,7 @@ def _AddInitMethod(message_descriptor, cls):
copy.MergeFrom(field_value) copy.MergeFrom(field_value)
self._fields[field] = copy self._fields[field] = copy
else: else:
self._fields[field] = field_value setattr(self, field_name, field_value)
init.__module__ = None init.__module__ = None
init.__doc__ = None init.__doc__ = None
...@@ -937,6 +937,10 @@ def _AddMergeFromMethod(cls): ...@@ -937,6 +937,10 @@ def _AddMergeFromMethod(cls):
CPPTYPE_MESSAGE = _FieldDescriptor.CPPTYPE_MESSAGE CPPTYPE_MESSAGE = _FieldDescriptor.CPPTYPE_MESSAGE
def MergeFrom(self, msg): 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 assert msg is not self
self._Modified() self._Modified()
......
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