def _AddHasFieldMethod(message_descriptor, cls):
is_proto3 = (message_descriptor.syntax == "<STR_LIT>")<EOL>error_msg = _Proto3HasError if is_proto3 else _Proto2HasError<EOL>hassable_fields = {}<EOL>for field in message_descriptor.fields:<EOL><INDENT>if field.label == _FieldDescriptor.LABEL_REPEATED:<EOL><INDENT>continue<EOL><DEDENT>if (is_proto3 and field.cpp_type != _FieldDescriptor.CPPTYPE_MESSAGE and<EOL>not field.containing_oneof):<EOL><INDENT>continue<EOL><DEDENT>hassable_fields[field.name] = field<EOL><DEDENT>if not is_proto3:<EOL><INDENT>for oneof in message_descriptor.oneofs:<EOL><INDENT>hassable_fields[oneof.name] = oneof<EOL><DEDENT><DEDENT>def HasField(self, field_name):<EOL><INDENT>try:<EOL><INDENT>field = hassable_fields[field_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise ValueError(error_msg % field_name)<EOL><DEDENT>if isinstance(field, descriptor_mod.OneofDescriptor):<EOL><INDENT>try:<EOL><INDENT>return HasField(self, self._oneofs[field].name)<EOL><DEDENT>except KeyError:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:<EOL><INDENT>value = self._fields.get(field)<EOL>return value is not None and value._is_present_in_parent<EOL><DEDENT>else:<EOL><INDENT>return field in self._fields<EOL><DEDENT><DEDENT><DEDENT>cls.HasField = HasField<EOL>
Helper for _AddMessageMethods().