third_party/protobuf/python/google/protobuf/internal/message_test.py
def testAssignInvalidEnum(self):
    """Assigning an invalid enum number is not allowed in proto2."""
    m = unittest_pb2.TestAllTypes()

    # Proto2 can not assign unknown enum.
    with self.assertRaises(ValueError) as _:
      m.optional_nested_enum = 1234567
    self.assertRaises(ValueError, m.repeated_nested_enum.append, 1234567)
    # Assignment is a different code path than append for the C++ impl.
    m.repeated_nested_enum.append(2)
    m.repeated_nested_enum[0] = 2
    with self.assertRaises(ValueError):
      m.repeated_nested_enum[0] = 123456

    # Unknown enum value can be parsed but is ignored.
    m2 = unittest_proto3_arena_pb2.TestAllTypes()
    m2.optional_nested_enum = 1234567
    m2.repeated_nested_enum.append(7654321)
    serialized = m2.SerializeToString()

    m3 = unittest_pb2.TestAllTypes()
    m3.ParseFromString(serialized)
    self.assertFalse(m3.HasField('optional_nested_enum'))
    # 1 is the default value for optional_nested_enum.
    self.assertEqual(1, m3.optional_nested_enum)
    self.assertEqual(0, len(m3.repeated_nested_enum))
    m2.Clear()
    m2.ParseFromString(m3.SerializeToString())
    self.assertEqual(1234567, m2.optional_nested_enum)
    self.assertEqual(7654321, m2.repeated_nested_enum[0])
Assigning an invalid enum number is not allowed in proto2.
Assigning an invalid enum number is not allowed in proto2.
Assigning an invalid enum number is not allowed in proto2 .
def testAssignInvalidEnum(self):
    m = unittest_pb2.TestAllTypes()
    with self.assertRaises(ValueError) as _:
      m.optional_nested_enum = 1234567
    self.assertRaises(ValueError, m.repeated_nested_enum.append, 1234567)
    m.repeated_nested_enum.append(2)
    m.repeated_nested_enum[0] = 2
    with self.assertRaises(ValueError):
      m.repeated_nested_enum[0] = 123456
    m2 = unittest_proto3_arena_pb2.TestAllTypes()
    m2.optional_nested_enum = 1234567
    m2.repeated_nested_enum.append(7654321)
    serialized = m2.SerializeToString()
    m3 = unittest_pb2.TestAllTypes()
    m3.ParseFromString(serialized)
    self.assertFalse(m3.HasField('optional_nested_enum'))
    self.assertEqual(1, m3.optional_nested_enum)
    self.assertEqual(0, len(m3.repeated_nested_enum))
    m2.Clear()
    m2.ParseFromString(m3.SerializeToString())
    self.assertEqual(1234567, m2.optional_nested_enum)
    self.assertEqual(7654321, m2.repeated_nested_enum[0])
def testAssignInvalidEnum ( self ) : m = unittest_pb2 . TestAllTypes ( ) with self . assertRaises ( ValueError ) as _ : m . optional_nested_enum = 1234567 self . assertRaises ( ValueError , m . repeated_nested_enum . append , 1234567 ) m . repeated_nested_enum . append ( 2 ) m . repeated_nested_enum [ 0 ] = 2 with self . assertRaises ( ValueError ) : m . repeated_nested_enum [ 0 ] = 123456 m2 = unittest_proto3_arena_pb2 . TestAllTypes ( ) m2 . optional_nested_enum = 1234567 m2 . repeated_nested_enum . append ( 7654321 ) serialized = m2 . SerializeToString ( ) m3 = unittest_pb2 . TestAllTypes ( ) m3 . ParseFromString ( serialized ) self . assertFalse ( m3 . HasField ( 'optional_nested_enum' ) ) self . assertEqual ( 1 , m3 . optional_nested_enum ) self . assertEqual ( 0 , len ( m3 . repeated_nested_enum ) ) m2 . Clear ( ) m2 . ParseFromString ( m3 . SerializeToString ( ) ) self . assertEqual ( 1234567 , m2 . optional_nested_enum ) self . assertEqual ( 7654321 , m2 . repeated_nested_enum [ 0 ] )
Assigning an invalid enum number is not allowed in proto2.
Assigning an invalid enum number is not allowed in proto2 .
"""Assigning an invalid enum number is not allowed in proto2.""" # Proto2 can not assign unknown enum. # Assignment is a different code path than append for the C++ impl. # Unknown enum value can be parsed but is ignored. # 1 is the default value for optional_nested_enum.
self