我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用google.protobuf.message.ClearField()。
def testOneofWhichOneof(self, message_module): m = message_module.TestAllTypes() self.assertIs(None, m.WhichOneof('oneof_field')) if message_module is unittest_pb2: self.assertFalse(m.HasField('oneof_field')) m.oneof_uint32 = 11 self.assertEqual('oneof_uint32', m.WhichOneof('oneof_field')) if message_module is unittest_pb2: self.assertTrue(m.HasField('oneof_field')) m.oneof_bytes = b'bb' self.assertEqual('oneof_bytes', m.WhichOneof('oneof_field')) m.ClearField('oneof_bytes') self.assertIs(None, m.WhichOneof('oneof_field')) if message_module is unittest_pb2: self.assertFalse(m.HasField('oneof_field'))
def testMapIterInvalidatedByClearField(self): # Map iterator is invalidated when field is cleared. # But this case does need to not crash the interpreter. # For the C++ implementation this tests the correctness of # ScalarMapContainer::Release() msg = map_unittest_pb2.TestMap() it = iter(msg.map_int32_int32) msg.ClearField('map_int32_int32') with self.assertRaises(RuntimeError): for _ in it: pass it = iter(msg.map_int32_foreign_message) msg.ClearField('map_int32_foreign_message') with self.assertRaises(RuntimeError): for _ in it: pass
def testOneofClearField(self, message_module): m = message_module.TestAllTypes() m.oneof_uint32 = 11 m.ClearField('oneof_field') if message_module is unittest_pb2: self.assertFalse(m.HasField('oneof_field')) self.assertFalse(m.HasField('oneof_uint32')) self.assertIs(None, m.WhichOneof('oneof_field'))
def testOneofClearSetField(self, message_module): m = message_module.TestAllTypes() m.oneof_uint32 = 11 m.ClearField('oneof_uint32') if message_module is unittest_pb2: self.assertFalse(m.HasField('oneof_field')) self.assertFalse(m.HasField('oneof_uint32')) self.assertIs(None, m.WhichOneof('oneof_field'))
def testOneofClearUnsetField(self, message_module): m = message_module.TestAllTypes() m.oneof_uint32 = 11 self.ensureNestedMessageExists(m, 'oneof_nested_message') m.ClearField('oneof_nested_message') self.assertEqual(11, m.oneof_uint32) if message_module is unittest_pb2: self.assertTrue(m.HasField('oneof_field')) self.assertTrue(m.HasField('oneof_uint32')) self.assertEqual('oneof_uint32', m.WhichOneof('oneof_field'))
def testMapValidAfterFieldCleared(self): # Map needs to work even if field is cleared. # For the C++ implementation this tests the correctness of # ScalarMapContainer::Release() msg = map_unittest_pb2.TestMap() int32_map = msg.map_int32_int32 int32_map[2] = 4 int32_map[3] = 6 int32_map[4] = 8 msg.ClearField('map_int32_int32') self.assertEqual(b'', msg.SerializeToString()) matching_dict = {2: 4, 3: 6, 4: 8} self.assertMapIterEquals(int32_map.items(), matching_dict)
def testMessageMapValidAfterFieldCleared(self): # Map needs to work even if field is cleared. # For the C++ implementation this tests the correctness of # ScalarMapContainer::Release() msg = map_unittest_pb2.TestMap() int32_foreign_message = msg.map_int32_foreign_message int32_foreign_message[2].c = 5 msg.ClearField('map_int32_foreign_message') self.assertEqual(b'', msg.SerializeToString()) self.assertTrue(2 in int32_foreign_message.keys())
def testMapValidAfterFieldCleared(self): # Map needs to work even if field is cleared. # For the C++ implementation this tests the correctness of # ScalarMapContainer::Release() msg = map_unittest_pb2.TestMap() map = msg.map_int32_int32 map[2] = 4 map[3] = 6 map[4] = 8 msg.ClearField('map_int32_int32') matching_dict = {2: 4, 3: 6, 4: 8} self.assertMapIterEquals(map.items(), matching_dict)
def testMapIterValidAfterFieldCleared(self): # Map iterator needs to work even if field is cleared. # For the C++ implementation this tests the correctness of # ScalarMapContainer::Release() msg = map_unittest_pb2.TestMap() msg.map_int32_int32[2] = 4 msg.map_int32_int32[3] = 6 msg.map_int32_int32[4] = 8 it = msg.map_int32_int32.items() msg.ClearField('map_int32_int32') matching_dict = {2: 4, 3: 6, 4: 8} self.assertMapIterEquals(it, matching_dict)
def testFieldPresence(self): message = unittest_pb2.TestAllTypes() self.assertFalse(message.HasField("optional_int32")) self.assertFalse(message.HasField("optional_bool")) self.assertFalse(message.HasField("optional_nested_message")) with self.assertRaises(ValueError): message.HasField("field_doesnt_exist") with self.assertRaises(ValueError): message.HasField("repeated_int32") with self.assertRaises(ValueError): message.HasField("repeated_nested_message") self.assertEqual(0, message.optional_int32) self.assertEqual(False, message.optional_bool) self.assertEqual(0, message.optional_nested_message.bb) # Fields are set even when setting the values to default values. message.optional_int32 = 0 message.optional_bool = False message.optional_nested_message.bb = 0 self.assertTrue(message.HasField("optional_int32")) self.assertTrue(message.HasField("optional_bool")) self.assertTrue(message.HasField("optional_nested_message")) # Set the fields to non-default values. message.optional_int32 = 5 message.optional_bool = True message.optional_nested_message.bb = 15 self.assertTrue(message.HasField("optional_int32")) self.assertTrue(message.HasField("optional_bool")) self.assertTrue(message.HasField("optional_nested_message")) # Clearing the fields unsets them and resets their value to default. message.ClearField("optional_int32") message.ClearField("optional_bool") message.ClearField("optional_nested_message") self.assertFalse(message.HasField("optional_int32")) self.assertFalse(message.HasField("optional_bool")) self.assertFalse(message.HasField("optional_nested_message")) self.assertEqual(0, message.optional_int32) self.assertEqual(False, message.optional_bool) self.assertEqual(0, message.optional_nested_message.bb) # TODO(tibell): The C++ implementations actually allows assignment # of unknown enum values to *scalar* fields (but not repeated # fields). Once checked enum fields becomes the default in the # Python implementation, the C++ implementation should follow suit.