3 SLj@sdZdZddlmZddlZddlZddlZddlZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z dd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZejZdZGdddeZddZddZddZ ddZ!ddZ"ddZ#d d!Z$d"d#Z%d$d%Z&d&d'Z'd(d)Z(d*d+Z)d,d-Z*d.d/Z+d0d1Z,d2d3Z-d4d5Z.d6d7Z/d8d9Z0d:d;Z1dd?Z3d@dAZ4dBZ5dCZ6dDdEZ7dFdGZ8dHdIZ9dJdKZ:dLdMZ;dNdOZdTdUZ?dVdWZ@dXdYZAdZd[ZBd\d]ZCd^d_ZDd`daZEdbdcZFdddeZGdfdgZHdhdiZIdjdkZJdldmZKdndoZLdpdqZMGdrdsdseNZOGdtdudueOZPGdvdwdweNZQdS)xa Contains a metaclass and helper functions used to create protocol message classes from Descriptor objects at runtime. Recall that a metaclass is the "type" of a class. (A class is to a metaclass what an instance is to a class.) In this case, we use the GeneratedProtocolMessageType metaclass to inject all the useful functionality into the classes output by the protocol compiler at compile-time. The upshot of all this is that the real implementation details for ALL pure-Python protocol buffers are *here in this file*. z#robinson@google.com (Will Robinson))BytesION)api_implementation) containers)decoder)encoder)enum_type_wrapper)message_listener) type_checkers)well_known_types) wire_format) descriptor)message) text_formatzgoogle.protobuf.Anycs0eZdZdZdZfddZfddZZS)GeneratedProtocolMessageTypeaQMetaclass for protocol message classes created at runtime from Descriptors. We add implementations for all methods described in the Message class. We also create properties to allow getting/setting all fields in the protocol message. Finally, we create slots to prevent users from accidentally "setting" nonexistent fields in the protocol message, which then wouldn't get serialized / deserialized properly. The protocol compiler currently uses this metaclass to create protocol message classes at runtime. Clients can also manually create their own classes at runtime, as in this example: mydescriptor = Descriptor(.....) factory = symbol_database.Default() factory.pool.AddDescriptor(mydescriptor) MyProtoClass = factory.GetPrototype(mydescriptor) myproto_instance = MyProtoClass() myproto.foo_field = 23 ... DESCRIPTORcsZ|tj}|jtjkr(|tj|jf7}t||t||tt|}|j||||}|S)aCustom allocation for runtime-generated class types. We override __new__ because this is apparently the only place where we can meaningfully set __slots__ on the class we're creating(?). (The interplay between metaclasses and slots is not very well-documented). Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. Returns: Newly-allocated class. ) r_DESCRIPTOR_KEY full_namer ZWKTBASES&_AddClassAttributesForNestedExtensions _AddSlotssuper__new__)clsnamebases dictionaryr superclass new_class) __class__$/usr/lib/python3.6/python_message.pyris     z$GeneratedProtocolMessageType.__new__cs|tj}i|_|jr6|jjr6tj|df|jtj<x|j D]}t ||q>W||_ t ||t ||t||t||t|t||t||tt|}|j|||dS)aHere we perform the majority of our work on the class. We add enum getters, an __init__ method, implementations of all Message methods, and properties for all fields in the protocol type. Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. N)rr_decoders_by_tag has_options GetOptionsmessage_set_wire_formatrZMessageSetItemDecoderZMESSAGE_SET_ITEM_TAGfields_AttachFieldHelpers_concrete_class_AddEnumValues_AddInitMethod_AddPropertiesForFields_AddPropertiesForExtensions_AddStaticMethods_AddMessageMethods_AddPrivateHelperMethodsr__init__)rrrrr fieldr)rrrr.s"          z%GeneratedProtocolMessageType.__init__)__name__ __module__ __qualname____doc__rrr. __classcell__rr)rrrNs rcCs|S)aReturns the name of the public property attribute which clients can use to get and (in some cases) set the value of a protocol message field. Args: proto_field_name: The protocol message field name, exactly as it appears (or would appear) in a .proto file. r)proto_field_namerrr _PropertyNamesr6cCsjt|tstd||js*td|j|js>td|j|j|jk rftd|j|jj|jjfdS)z0Verify that the given extension handle is valid.z3HasExtension() expects an extension handle, got: %sz"%s" is not an extension.z""%s" is missing a containing_type.zKExtension "%s" extends message type "%s", but this message is of type "%s".N) isinstance_FieldDescriptorKeyError is_extensionrcontaining_typer)r extension_handlerrr_VerifyExtensionHandles   r=c Csddddddddd g |d <d S) a Adds a __slots__ entry to dictionary, containing the names of all valid attributes for this message type. Args: message_descriptor: A Descriptor instance describing this message type. dictionary: Class dictionary to which we'll add a '__slots__' entry. _cached_byte_size_cached_byte_size_dirty_fields_unknown_fields_is_present_in_parent _listener_listener_for_children __weakref___oneofs __slots__Nr)message_descriptorrrrrrsrcCs2|jo0|jjo0|jjjo0|jtjko0|jtj kS)N) r:r;r!r"r#typer8 TYPE_MESSAGElabelZLABEL_OPTIONAL)r/rrr_IsMessageSetExtensions   rLcCs |jtjko|jjo|jjjS)N)rIr8rJ message_typer!r" map_entry)r/rrr _IsMapFields rOcCs|jjd}|jtjkS)Nvalue)rMfields_by_namecpp_typer8CPPTYPE_MESSAGE)r/Z value_typerrr_IsMessageMapField s rTcs@jtjkotjj}|s&d}nFjjdkrDjo@j j }n(jodj j dodj j dk}| }t rt j}t jt}nNtrt jj}t jj}n,tjjj|}tjjj|}|_|_t_fdd}|tjjdr.AddDecoderT)rKr8LABEL_REPEATEDr ZIsTypePackablerIr;syntaxr!r"rUHasFieldrOrZ MapEncoderZMapSizerrTrLZMessageSetItemEncoderrVZMessageSetItemSizerr ZTYPE_TO_ENCODERZ TYPE_TO_SIZER_encoder_sizer _DefaultValueConstructorForFieldrYZFIELD_TYPE_TO_WIRE_TYPEZWIRETYPE_LENGTH_DELIMITED)rr]Z is_packablerZZhas_packed_falseZ field_encoderZsizerr`r)rr]r^r_rr%sB            r%cCs(|j}x|jD]\}}|||<qWdS)N)extensions_by_nameitems)r rextension_dictextension_nameextension_fieldrrrrUsrcCsHxB|jD]8}t||jtj|x|jD]}t||j|jq(WqWdS)aSets class-level attributes for all enum fields defined in this message. Also exporting a class-level object that can name enum values. Args: descriptor: Descriptor object for this message type. cls: Class we're constructing for this message type. N)Z enum_typessetattrrrZEnumTypeWrappervaluesrV)r r enum_typeZ enum_valuerrrr'\s  r'csvjtjkrtdjjj}tj|d|dt rTfdd}|Stjfdd}|SdS)Nz&map_entry set on non-repeated field %skeyrPcstj|jjjS)N)rZ MessageMaprDrM)r )r/ key_checker value_fieldrrMakeMessageMapDefaultts z:_GetInitializeDefaultForMap..MakeMessageMapDefaultcstj|jjS)N)rZ ScalarMaprDrM)r )r/rp value_checkerrrMakePrimitiveMapDefault{sz<_GetInitializeDefaultForMap..MakePrimitiveMapDefault) rKr8ra ValueErrorrrMrQr GetTypeCheckerrT)r/rQrrrtr)r/rprsrqrrXks   rXcstrtSjtjkrvjr:jgkr:tdjjtj kr\j fdd}|St j fdd}|Sjtj krj fdd}|Sfdd }|S) aReturns a function which returns a default value for a field. Args: field: FieldDescriptor object for this field. The returned function has one argument: message: Message instance containing this field, or a weakref proxy of same. That function in turn returns a default value for this field. The default value may refer back to |message| via a weak reference. z/Repeated field default value not empty list: %scstj|jjS)N)rZRepeatedCompositeFieldContainerrDrM)r )r/rrMakeRepeatedMessageDefaultszD_DefaultValueConstructorForField..MakeRepeatedMessageDefaultcstj|jS)N)rZRepeatedScalarFieldContainerrD)r ) type_checkerrrMakeRepeatedScalarDefaultszC_DefaultValueConstructorForField..MakeRepeatedScalarDefaultcs,j}|jjdk r t|n|j|S)N)r& _SetListenerrW_OneofListenerrD)r result)r/rMrrMakeSubMessageDefaults z?_DefaultValueConstructorForField..MakeSubMessageDefaultcsjS)N) default_value)r )r/rrMakeScalarDefaultsz;_DefaultValueConstructorForField..MakeScalarDefault) rOrXrKr8raZhas_default_valuer~rurRrSrMr rv)r/rwryr}rr)r/rMrxrrfs&        rfcCsZtjd}t|jdkrtd|j|fYnX|S)aConvert a string or integer enum value to an integer. If the value is a string, it is converted to the enum value in enum_type with the same name. If the value is not a string, it's returned as-is. (No conversion or bounds-checking is done.) z Enum type %s: unknown label "%s")r7rZ string_typesZvalues_by_namerVr9rur)rnrPrrr_GetIntegerEnumValues  z,_AddInitMethod.._GetIntegerEnumValuec s"d|_t|dk|_i|_i|_f|_d|_tj|_ t ||_ x|j D]\}}t |dkrztdj|f|dkrqLjtjkrXj|}jtjkr trtrx*|D]}||j||qWn |j|n6xb|D],}t|tr |jf|q|jj|qWn,jtjkrBfdd|D}|j|||j<qLjtjkr̈j|}|}t|trjj f|}y|j|Wn"tk rt!j|YnX||j<qLjtjkrj"|}yt#|||WqLtk rt!j|YqLXqLWdS)NrFz,%s() got an unexpected keyword argument '%s'csg|]}j|qSr)rn).0val)rr/rr sz0_AddInitMethod..init..)$r>rr?r@rFrArBmessage_listener_modNullMessageListenerrC _ListenerrDrh_GetFieldByNamerrrKr8rarYrRrSrOrT MergeFromupdater7dictaddZ CPPTYPE_ENUMextendrMr&rrnrl)selfkwargsr field_valuecopyrorZnew_val)rrH)r/rinits`               z_AddInitMethod..initN)r1r3r.)rHrrr)rrHrr(s =r(c Cs6y |j|Stk r0td|j|fYnXdS)zReturns a field descriptor by field name. Args: message_descriptor: A Descriptor describing all fields in message. field_name: The name of the field to retrieve. Returns: The field descriptor associated with the field name. z&Protocol message %s has no "%s" field.N)rQr9rur)rHrrrrrs  rcCs2x|jD]}t||qW|jr.tdd|_dS)z=Adds properties for all fields in this protocol message type.cSst|S)N)_ExtensionDict)rrrr/sz)_AddPropertiesForFields..N)r$_AddPropertiesForField is_extendablepropertyZ Extensions)r rr/rrrr)'s r)cCsZ|jjd}t|||j|jtjkr4t||n"|jtj krLt ||n t ||dS)a#Adds a public property for a protocol message field. Clients can use this property to get and (in the case of non-repeated scalar fields) directly set the value of a protocol message field. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. _FIELD_NUMBERN) rupperrlrVrKr8ra_AddPropertiesForRepeatedFieldrRrS*_AddPropertiesForNonRepeatedCompositeField'_AddPropertiesForNonRepeatedScalarField)r/r constant_namerrrr2s    rcsXjt}fdd}d|_d|_fdd}d}t||t|||ddS) aAdds a public property for a "repeated" protocol message field. Clients can use this property to get the value of the field, which will be either a _RepeatedScalarFieldContainer or _RepeatedCompositeFieldContainer (see below). Note that when clients add values to these containers, we perform type-checking in the case of repeated scalar fields, and we also set any necessary "has" bits as a side-effect. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. cs0|jj}|dkr,j|}|jj|}|S)N)r@getrY setdefault)rr)r/rrgetter\s   z._AddPropertiesForRepeatedField..getterNzGetter for %s.cstddS)NzIAssignment not allowed to repeated field "%s" in protocol message object.)AttributeError)r new_value)r5rrsetterosz._AddPropertiesForRepeatedField..setterz/Magic attribute generated for "%s" proto field.)doc)rr6r1r3rlr)r/r property_namerrrr)r/r5rrKs   rc sj}t|}tjjt}jjdk}fdd}d|_d||_ |oXj fddj rfdd }n}d|_d ||_ d |}t ||t |||d dS) aAdds a public property for a nonrepeated, scalar protocol message field. Clients can use this property to get and directly set the value of the field. Note that when the client sets the value of a field by using this property, all necessary "has" bits are set as a side-effect, and we also perform type-checking. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. proto3cs|jjS)N)r@r)r)r~r/rrrsz7_AddPropertiesForNonRepeatedScalarField..getterNzGetter for %s.cs@j|}r$| r$|jjdn ||j<|js<|jdS)N) CheckValuer@popr? _Modified)rr)clear_when_set_to_defaultr/rxrr field_setters    z=_AddPropertiesForNonRepeatedScalarField..field_settercs|||jdS)N)_UpdateOneofState)rr)r/rrrrs z7_AddPropertiesForNonRepeatedScalarField..setterzSetter for %s.z/Magic attribute generated for "%s" proto field.)r) rr6r rvr~setr;rbr1r3rWrlr) r/rr5rZ valid_values is_proto3rrrr)rr~r/rrxrrws$      rcsXjt}fdd}d|_d|_fdd}d}t||t|||ddS) aNAdds a public property for a nonrepeated, composite protocol message field. A composite field is a "group" or "message" field. Clients can use this property to get the value of the field, but cannot assign to the property directly. Args: field: A FieldDescriptor for this field. cls: The class we're constructing. cs0|jj}|dkr,j|}|jj|}|S)N)r@rrYr)rr)r/rrrs   z:_AddPropertiesForNonRepeatedCompositeField..getterNzGetter for %s.cstddS)NzJAssignment not allowed to composite field "%s" in protocol message object.)r)rr)r5rrrsz:_AddPropertiesForNonRepeatedCompositeField..setterz/Magic attribute generated for "%s" proto field.)r)rr6r1r3rlr)r/rrrrrr)r/r5rrs    rcCsd|j}x.|jD]"\}}|jd}t|||jqW|jdk r`|jj}|j||_|j||_dS)z=Adds properties for all fields in this protocol message type.rN) rgrhrrlrVfilepool_extensions_by_number_extensions_by_name)r rrirjrkrrrrrr*s   r*cs0fdd}t|_fdd}t|_dS)Ncs&j|_jjjj|t|dS)N)rr;rrZAddExtensionDescriptorr%)r<)rrrRegisterExtensionsz,_AddStaticMethods..RegisterExtensioncs}|j||S)N)MergeFromString)sr )rrr FromStrings z%_AddStaticMethods..FromString) staticmethodrr)rrrr)rrr+s   r+cCs>|djtjkrt|dS|djtjkr6|djSdSdS)zGiven a (FieldDescriptor, value) tuple from _fields, return true if the value should be included in the list returned by ListFields().rrTN)rKr8raboolrRrSrB)itemrrr _IsPresents   rcCsdd}||_dS)z Helper for _AddMessageMethods().cSs(dd|jjD}|jddd|S)NcSsg|]}t|r|qSr)r)rrrrrr sz<_AddListFieldsMethod..ListFields..cSs |djS)Nr)rV)rrrrr sz:_AddListFieldsMethod..ListFields..)ro)r@rhsort)rZ all_fieldsrrr ListFields sz(_AddListFieldsMethod..ListFieldsN)r)rHrrrrr_AddListFieldsMethodsrz:Protocol message has no non-repeated submessage field "%s"z/Protocol message has no non-repeated field "%s"cs|jdk}|rtntix@|jD]6}|jtjkr4q"|rN|jtjkrN|j rNq"||j <q"W|szx|j D]}||j <qhWfdd|_ dS)z Helper for _AddMessageMethods().rcsy |}Wn tk r,t|YnXt|tjrdy||j|jStk r`dSXn0|jtj kr|j j |}|dk o|j S||j kSdS)NF) r9rur7descriptor_modZOneofDescriptorrFrrRr8rSr@rrB)rrr/rP)rc error_msghassable_fieldsrrrc(s    z$_AddHasFieldMethod..HasFieldN) rb_Proto3HasError_Proto2HasErrorr$rKr8rarRrSrWrZoneofsrc)rHrrr/Zoneofr)rcrrr_AddHasFieldMethods     rcsfdd}||_dS)z Helper for _AddMessageMethods().csyj|}Wndtk rry(j|}||jkr@|j|}ndSWn&tk rltdj|fYnXYnX||jkrt|j|dr|j|j|j|=|jj |j d|kr|j|j =|j dS)Nz(Protocol message %s() has no "%s" field.InvalidateIterators) rQr9oneofs_by_namerFrurr@hasattrrrrWr)rrr/)rHrr ClearField?s$     z(_AddClearFieldMethod..ClearFieldN)r)rHrrr)rHr_AddClearFieldMethod=s !rcCsdd}||_dS)z Helper for _AddMessageMethods().cSs(t||||jkr|j|=|jdS)N)r=r@r)rr<rrrClearExtensiones  z0_AddClearExtensionMethod..ClearExtensionN)r)rrrrr_AddClearExtensionMethodcsrcCsdd}||_dS)z Helper for _AddMessageMethods().cSsXt|||jtjkr$td|j|jtjkrJ|jj |}|dk oH|j S||jkSdS)Nz"%s" is repeated.) r=rKr8rar9rrRrSr@rrB)rr<rPrrr HasExtensionqs    z,_AddHasExtensionMethod..HasExtensionN)r)rrrrr_AddHasExtensionMethodos rcCshddlm}|j}|j}|s"dS|jdd}|jj|}|dkrHdS|j|}|}|j|j |S)atUnpacks Any message and returns the unpacked message. This internal method is different from public Any Unpack method which takes the target message as argument. _InternalUnpackAny method does not have target message type and need to find the message type in descriptor pool. Args: msg: An Any message to be unpacked. Returns: The unpacked message. r)symbol_databaseN/r) google.protobufrZDefaulttype_urlsplitrZFindMessageTypeByNameZ GetPrototypeZParseFromStringrP)msgrfactoryrZ type_namer Z message_classr rrr_InternalUnpackAny}s    rcCsdd}||_dS)z Helper for _AddMessageMethods().cSst|tj s|j|jkrdS||kr*dS|jjtkrVt|}t|}|rV|rV||kS|j|jksjdSt|j }|j t|j }|j ||kS)NFT) r7 message_modZMessagerr_AnyFullTypeNamerrlistrAr)rotherZany_aZany_bZunknown_fieldsZother_unknown_fieldsrrr__eq__s"    z _AddEqualsMethod..__eq__N)r)rHrrrrr_AddEqualsMethodsrcCsdd}||_dS)z Helper for _AddMessageMethods().cSs tj|S)N)rMessageToString)rrrr__str__sz_AddStrMethod..__str__N)r)rHrrrrr _AddStrMethodsrcCsdd}||_dS)z Helper for _AddMessageMethods().cSs tj|S)N)rr)rrrr__repr__sz _AddReprMethod..__repr__N)r)rHrrrrr_AddReprMethodsrcCsdd}||_dS)z Helper for _AddMessageMethods().cSstj|ddjdS)NT)Zas_utf8zutf-8)rrdecode)rrrr __unicode__sz&_AddUnicodeMethod..__unicode__N)r)Zunused_message_descriptorrrrrr_AddUnicodeMethodsrc Cs<ytj|}|||Stk r6tjd|YnXdS)a Returns the number of bytes needed to serialize a non-repeated element. The returned byte count includes space for tag information and any other additional space associated with serializing value. Args: value: Value we're serializing. field_number: Field number of this value. (Since the field number is stored as part of a varint-encoded tag, this has an impact on the total bytes required to serialize the value). field_type: The type of the field. One of the TYPE_* constants within FieldDescriptor. zUnrecognized field type: %dN)r ZTYPE_TO_BYTE_SIZE_FNr9r EncodeError)rPZ field_numberZ field_typefnrrr_BytesForNonRepeatedElements   rcCsdd}||_dS)z Helper for _AddMessageMethods().cSs|js |jSd}|j}|jjrJ|jdj|j}||jdj|j7}nLx"|j D]\}}||j|7}qTWx&|j D]\}}|t |t |7}qvW||_d|_d|j _ |S)NrrorPF)r?r>rr"rNrQrerorPrrArrDdirty)rsizer r]rr[ value_bytesrrrByteSizes z$_AddByteSizeMethod..ByteSizeN)r)rHrrrrr_AddByteSizeMethodsrcCsdd}||_dS)z Helper for _AddMessageMethods().c[s8g}|js,tjd|jjdj|jf|jf|S)Nz)Message %s is missing required fields: %s,) IsInitializedrrrrjoinFindInitializationErrorsSerializePartialToString)rrerrorsrrrSerializeToString s z6_AddSerializeToStringMethod..SerializeToStringN)r)rHrrrrr_AddSerializeToStringMethodsrcCs"dd}||_ddd}||_dS)z Helper for _AddMessageMethods().c[st}|j|jf||jS)N)r_InternalSerializewritegetvalue)rroutrrrrszD_AddSerializePartialToStringMethod..SerializePartialToStringNcSs|dkrtj}nt|}|j}|jjrX|jdj||j||jdj||j |nHx"|j D]\}}|j|||qbWx"|j D]\}}||||qWdS)NrorP) rZ)IsPythonDefaultSerializationDeterministicrrr"rNrQrdrorPrrA)rZ write_bytesZ deterministicr r]rr[rrrrInternalSerializes     z=_AddSerializePartialToStringMethod..InternalSerialize)N)rr)rHrrrrrr"_AddSerializePartialToStringMethods rcsFdd}||_tjtj|j|jdkfdd}||_dS)z Helper for _AddMessageMethods().cSs|t|}y |j|d||kr&tjdWnNttfk rJtjdYn.tjk rv}ztj|WYdd}~XnX|S)NrzUnexpected end-group tag.zTruncated message.)r_InternalParserZ DecodeError IndexErrorrstructerror)rZ serializedZlengtherrrr8sz2_AddMergeFromStringMethod..MergeFromStringrc s|j|j}|j}x||kr||\}}j|d\}} |dkr|} ||||}|dkrb|S sptjr|s~g}|_|j||| |f|}q||||||}| r|j| qW|S)Nr)NNr)rr@rArrZ&GetPythonProto3PreserveUnknownsDefaultappendr) rbufferposendZ field_dictZunknown_field_listr[Znew_posr\Z field_descZvalue_start_pos)decoders_by_tagr local_ReadTaglocal_SkipFieldrr InternalParseLs,  z0_AddMergeFromStringMethod..InternalParseN)rrZReadTagZ SkipFieldr rbr)rHrrrr)rrrrr_AddMergeFromStringMethod6s  rcs:dd|jDdfdd }||_fdd}||_dS) z[Adds the IsInitialized and FindInitializationError methods to the protocol message class.cSsg|]}|jtjkr|qSr)rKr8ZLABEL_REQUIRED)rr/rrrrksz+_AddIsInitializedMethod..NcsxJD]B}||jks.|jtjkr|j|j r|dk rD|j|jdSqWxt|jjD]\}}|jtjkr\|j tj kr|j j r|j j jrq\xZ|D]&}|js|dk r|j|jdSqWq\|jr\|j r\|dk r|j|jdSq\WdS)aChecks if all required fields of a message are set. Args: errors: A list which, if provided, will be populated with the field paths of all missing required fields. Returns: True iff the specified message has all required fields set. NFT)r@rRr8rSrBrrrrhrKrarMr!r"rNr)rrr/rPelement)required_fieldsrrrns.        z._AddIsInitializedMethod..IsInitializedc s>g}x$D]}|j|js |j|jq Wx |jD]\}}|jtjkr6|jr^d|j}n|j}t |rt |rx@|D]6}||}d||f|j }|fdd|D7}qzWnq6|j tj krxntt|D]6}||}d||f|j }|fdd|D7}qWq6|d|j }|fdd|D7}q6W|S) zFinds required fields which are not initialized. Returns: A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz". z(%s)z%s[%s].csg|] }|qSrr)rr)prefixrrrszM_AddIsInitializedMethod..FindInitializationErrors..z%s[%d].csg|] }|qSrr)rr)rrrrs.csg|] }|qSrr)rr)rrrrs)rcrr rrRr8rSr:rrOrTrrKraranger) rrr/rPrrorZ sub_errorsi)r)rrrs6       z9_AddIsInitializedMethod..FindInitializationErrors)N)r$rr)rHrrrr)rr_AddIsInitializedMethodgs ' ,rcs&tjtjfdd}|_dS)Ncst|s tdj|jjf|j|j}x|jjD]\}}|jkr||j|}|dkrp|j |}|||<|j |q:|j kr|j r|j|}|dkr|j |}|||<|j |q:||j|<|j r:|j|q:W|jr|jsg|_|jj|jdS)NzLParameter to MergeFrom() must be instance of same class: expected %s got %s.)r7rr0rrr@rhrKrrYrrRrBrWrrAr)rrr$r/rPr)rSrarrrrs6          z&_AddMergeFromMethod..MergeFrom)r8rarSr)rrr)rSrarr_AddMergeFromMethods%rcsfdd}||_dS)Nc s`yj|}Wn tk r.td|YnX|jj|d}|dk rX|j|jrX|jSdSdS)zDReturns the name of the currently set field inside a oneof, or None.z)Protocol message has no oneof "%s" field.N)rr9rurFrrcr)rZ oneof_namer/Z nested_field)rHrr WhichOneofsz(_AddWhichOneofMethod..WhichOneof)r)rHrrr)rHr_AddWhichOneofMethods rcCsdd}||_dS)NcSst|f|jfS)N)rI __getstate__)rrrr __reduce__sz$_AddReduceMethod..__reduce__)r)rrrrr_AddReduceMethodsrcCsi|_f|_i|_|jdS)N)r@rArFr)rrrr_Clear sr cCsXg|_xL|jD]@\}}|jtjkr|jtjkrHx|D] }|jq6Wq|jqWdS)N)rArrRr8rSrKraDiscardUnknownFields)rr/rPZ sub_messagerrr_DiscardUnknownFieldss   r"cCs|dkrtj|_n||_dS)N)rrrC)rZlistenerrrrrzs rzcCst||t||t|||jr4t|t|t||t||t||t ||t ||t ||t ||t ||t||t|t||t|t|_t|_t|_dS)z3Adds implementations of all Message methods to cls.N)rrrrrrrrrrrrrrrrrrr ZClearr"r!rz)rHrrrrr,$s*             r,cCs&dd}dd}||_||_||_dS)z5Adds implementation of private helper methods to cls.cSs(|js$d|_d|j_d|_|jjdS)zwSets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change. TN)r?rDrrBrCModified)rrrrr#As z*_AddPrivateHelperMethods..ModifiedcSs0|jj|j|}||k r,|j|=||j|j<dS)zSets field as the active field in its containing oneof. Will also delete currently active field in the oneof, if it is different from the argument. Does not mark the message as modified. N)rFrrWr@)rr/Z other_fieldrrrrPsz3_AddPrivateHelperMethods.._UpdateOneofStateN)rZ SetInParentr)rHrr#rrrrr->s  r-c@s eZdZdZddZddZdS)ra0MessageListener implementation that a parent message registers with its child message. In order to support semantics like: foo.bar.baz.qux = 23 assert foo.HasField('bar') ...child objects must have back references to their parents. This helper class is at the heart of this support. cCs*t|tjr||_n tj||_d|_dS)zArgs: parent_message: The message whose _Modified() method we should call when we receive Modified() messages. FN)r7weakref ProxyType_parent_message_weakrefproxyr)rparent_messagerrrr.ns  z_Listener.__init__c Cs2|jr dSy|jjWntk r,YnXdS)N)rr&rReferenceError)rrrrr#s z_Listener.ModifiedN)r0r1r2r3r.r#rrrrr`s rcs,eZdZdZfddZfddZZS)r{zCSpecial listener implementation for setting composite oneof fields.cstt|j|||_dS)zArgs: parent_message: The message whose _Modified() method we should call when we receive Modified() messages. field: The descriptor of the field being set in the parent message. N)rr{r._field)rr(r/)rrrr.sz_OneofListener.__init__c s:y |jj|jtt|jWntk r4YnXdS)zEAlso updates the state of the containing oneof in the parent message.N)r&rr*rr{r#r))r)rrrr#s z_OneofListener.Modified)r0r1r2r3r.r#r4rr)rrr{s r{c@sPeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dS)rzDict-like container for supporting an indexable "Extensions" field on proto instances. Note that in all cases we expect extension handles to be FieldDescriptors. cCs ||_dS)zMextended_message: Message instance for which we are the Extensions dict. N)_extended_message)rZextended_messagerrrr.sz_ExtensionDict.__init__c Cst|j||jjj|}|dk r&|S|jtjkr@|j|j}nF|jtj kr|j j }y|j |jj Wqtk r|YqXn|jS|jjj||}|S)z8Returns the current value of the given extension handle.N)r=r+r@rrKr8rarYrRrSrMr&rzrDr)r~r)rr<r|rrr __getitem__s     z_ExtensionDict.__getitem__cCsHt||jsdS|jj}|jj}dd|D}dd|D}||kS)NFcSsg|]}|jr|qSr)r:)rr/rrrrsz)_ExtensionDict.__eq__..cSsg|]}|jr|qSr)r:)rr/rrrrs)r7rr+r)rrZ my_fieldsZ other_fieldsrrrrs   z_ExtensionDict.__eq__cCs ||k S)Nr)rrrrr__ne__sz_ExtensionDict.__ne__cCs tddS)Nzunhashable object)r)rrrr__hash__sz_ExtensionDict.__hash__cCs\t|j||jtjks$|jtjkr2td|jt j |}|j ||jj |<|jj dS)zlIf extension_handle specifies a non-repeated, scalar extension field, sets the value of that field. zKCannot assign to extension "%s" because it is a repeated or composite type.N)r=r+rKr8rarRrSrrr rvrr@r)rr<rPrxrrr __setitem__s     z_ExtensionDict.__setitem__cCs|jjj|dS)zTries to find a known extension with the specified name. Args: name: Extension full name. Returns: Extension field descriptor. N)r+rr)rrrrr_FindExtensionByNames z#_ExtensionDict._FindExtensionByNamecCs|jjj|dS)zTries to find a known extension with the field number. Args: number: Extension field number. Returns: Extension field descriptor. N)r+rr)rrVrrr_FindExtensionByNumbers z%_ExtensionDict._FindExtensionByNumberN) r0r1r2r3r.r,rr-r.r/r0r1rrrrrs!  r)Rr3 __author__iorrrr$rZgoogle.protobuf.internalrrrrrrrr r r rr rr rrZFieldDescriptorr8rrIrr6r=rrLrOrTr%rr'rXrfrr(rr)rrrrr*r+rrrrrrrrrrrrrrrrrrrrrrr r"rzr,r-objectrr{rrrrr1s             kC7 T ,8,  *& ( 1_, ".