eaiovnaovbqoebvqoeavibavo  Duac@sydZdZddlZddlZddlmZdZe ZdZdZ dZ d Z d Z d Z e e ZZZe eZZe e ejZZe d ZZZe d ZZZe dZdZdZdZdZ dZ!dZ"dZ#dZ$e#Z%e$Z&dZ'dZ(dZ)dZ*dZ+dZ,e)ej-e&e Z.Z/Z0e)ej-e%eZ1Z2e*ej-e%eejZ3Z4e+ej5dZ6e+ej7dZ8e+ej5dZ9e+ej7d Z:e,ej5d!Z;e,ej7d"Z<d#Z=d$Z>d%Z?d&Z@d'ZAd(ZBd)ZCdS(*sCode for encoding protocol message primitives. Contains the logic for encoding every logical protocol field type into one of the 5 physical wire types. This code is designed to push the Python interpreter's performance to the limits. The basic idea is that at startup time, for every field (i.e. every FieldDescriptor) we construct two functions: a "sizer" and an "encoder". The sizer takes a value of this field's type and computes its byte size. The encoder takes a writer function and a value. It encodes the value into byte strings and invokes the writer function to write those strings. Typically the writer function is the write() method of a BytesIO. We try to do as much work as possible when constructing the writer and the sizer rather than when calling them. In particular: * We copy any needed global functions to local variables, so that we do not need to do costly global table lookups at runtime. * Similarly, we try to do any attribute lookups at startup time if possible. * Every field's tag is encoded to bytes at startup, since it can't change at runtime. * Whatever component of the field size we can compute at startup, we do. * We *avoid* sharing code if doing so would make the code slower and not sharing does not burden us too much. For example, encoders for repeated fields do not just call the encoders for singular fields in a loop because this would add an extra function call overhead for every loop iteration; instead, we manually inline the single-value encoder into the loop. * If a Python function lacks a return statement, Python actually generates instructions to pop the result of the last statement off the stack, push None onto the stack, and then return that. If we really don't care what value is returned, then we can save two instructions by returning the result of the last statement. It looks funny but it helps. * We assume that type and bounds checking has happened at a higher level. s kenton@google.com (Kenton Varda)iN(t wire_formatgcCs|dkrdS|dkr dS|dkr0dS|dkr@dS|d krPd S|d kr`d S|d krpdS|dkrdS|dkrdSdS(s#Compute the size of a varint value.iii?iiiiililililili i ((tvalue((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _VarintSizeRs&         cCs|dkrdS|dkr dS|dkr0dS|dkr@dS|d krPd S|d kr`d S|d krpdS|dkrdS|dkrdS|dkrdSdS(s*Compute the size of a signed varint value.ii iii?iiiiililililili ((R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_SignedVarintSize`s*          cCsttj|dS(sQReturns the number of bytes required to serialize a tag with this field number.i(RRtPackTag(t field_number((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_TagSizeoscsfd}|S(sA sizer which uses the function compute_value_size to compute the size of each value. Typically compute_value_size is _VarintSize.csgt||r1tfd}|S|rMfd}|Sfd}|SdS(Ncs9d}x|D]}||7}q W||S(Ni((Rtresulttelement(tcompute_value_sizetlocal_VarintSizettag_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytPackedFieldSizes cs5t|}x|D]}||7}qW|S(N(tlen(RRR(R R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytRepeatedFieldSizes cs|S(N((R(R R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt FieldSizes(RR(Rt is_repeatedt is_packedR RR(R (R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt SpecificSizers ((R R((R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _SimpleSizer~scsfd}|S(sLike SimpleSizer, but modify_value is invoked on each value before it is passed to compute_value_size. modify_value is typically ZigZagEncode.cspt||r4tfd}|S|rSfd}|Sfd}|SdS(Ncs?d}x$|D]}||7}q W||S(Ni((RRR(R R t modify_valueR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR s cs;t|}x$|D]}||7}qW|S(N(R (RRR(R RR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs cs|S(N((R(R RR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs(RR(RRRR RR(R R(R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs ((R RR((R RsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_ModifiedSizerscsfd}|S(sWLike _SimpleSizer except for a fixed-size field. The input is the size of one value.csut||r1tfd}|S|rTfd}|Sfd}|SdS(Ncs"t|}||S(N(R (RR(R R t value_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR scst|S(N(R (R(t element_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRscsS(N((R(t field_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs(RR(RRRR RR(R(RRR R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs   ((RR((RsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _FixedSizersiiicsat|tt| s%t|rDfd}|Sfd}|SdS(s#Returns a sizer for a string field.csNt|}x7|D]/}|jd}|||7}qW|S(Nsutf-8(R tencode(RRRtl(R t local_lenR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs  cs'|jd}||S(Nsutf-8(R(RR(R RR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRsN(RRR tAssertionError(RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt StringSizers  csat|tt| s%t|rDfd}|Sfd}|SdS(s"Returns a sizer for a bytes field.csEt|}x.|D]&}|}|||7}qW|S(N(R (RRRR(R RR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs   cs|}||S(N((RR(R RR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR s N(RRR R(RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt BytesSizers  csMt|d| st|r6fd}|Sfd}|SdS(s"Returns a sizer for a group field.ics5t|}x|D]}||j7}qW|S(N(R tByteSize(RRR(R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs cs|jS(N(R (R(R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRsN(RR(RRRRR((R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt GroupSizers csUt|t| st|r;fd}|Sfd}|SdS(s$Returns a sizer for a message field.csEt|}x.|D]&}|j}|||7}qW|S(N(R R (RRRR(R R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR+s   cs|j}||S(N(R (RR(R R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR3s N(RRR(RRRRR((R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt MessageSizer$s  csJtddtdt|tdtfd}|S(sReturns a sizer for extensions of MessageSet. The message set message looks like this: message MessageSet { repeated group Item = 1 { required int32 type_id = 2; required string message = 3; } } iiics|j}||S(N(R (RR(R t static_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRLs (RR(RR((R R#sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMessageSetItemSizer=s ! cs7|jt|jttfd}|S(s Returns a sizer for a map field.cs`d}xS|D]K}||}jd|d|}||7}r |jq q W|S(NitkeyR(t_concrete_classR (t map_valuettotalR%Rt entry_msg(tis_message_mapt message_sizert message_type(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR_s  (R,R"tnumbertFalse(tfield_descriptorR*R((R*R+R,sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMapSizerWs cCs d}|S(sBReturn an encoder for a basic varint value (does not include tag).cSs_|d@}|dL}x5|rK|tjd|B|d@}|dL}qW|tj|S(Niii(tsixtint2byte(twriteRtunused_deterministictbits((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt EncodeVarintws    ((R6((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_VarintEncoderts cCs d}|S(sKReturn an encoder for a basic signed varint value (does not include tag).cSsx|dkr|d7}n|d@}|dL}x5|rd|tjd|B|d@}|dL}q0W|tj|S(Niii@iiil(R1R2(R3RR4R5((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeSignedVarints      ((R8((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_SignedVarintEncoders cCs&g}t|j|tdj|S(sEncode the given integer as a varint and return the bytes. This is only called at startup time so it doesn't need to be fast.t(t _EncodeVarinttappendtTruetjoin(Rtpieces((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _VarintBytesscCstjttj||S(sCEncode the given tag and return the bytes. Only called at startup.(R1t binary_typeR@RR(Rt wire_type((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytTagBytesscsfd}|S(s_Return a constructor for an encoder for fields of a particular type. Args: wire_type: The field's wire type, for encoding tags. encode_value: A function which encodes an individual value, e.g. _EncodeVarint(). compute_value_size: A function which computes the size of an individual value, e.g. _VarintSize(). cs|r:t|tjtfd}|S|ret|fd}|St|fd}|SdS(Ncsf|d}x|D]}||7}qW|||x|D]}|||qHWdS(Ni((R3Rt deterministictsizeR(R t encode_valuetlocal_EncodeVarintt tag_bytes(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodePackedFields   cs/x(|D] }||||qWdS(N((R3RRDR(RFRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeRepeatedFields  cs||||S(N((R3RRD(RFRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt EncodeFields (RCRtWIRETYPE_LENGTH_DELIMITEDR;(RRRRIRJRK(R RFRB(RGRHsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytSpecificEncoders((RBRFR RM((R RFRBsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_SimpleEncoders csfd}|S(sLike SimpleEncoder but additionally invokes modify_value on every value before passing it to encode_value. Usually modify_value is ZigZagEncode.cs|r=t|tjtfd}|S|rkt|fd}|St|fd}|SdS(Ncsr|d}x$|D]}||7}qW|||x$|D]}|||qNWdS(Ni((R3RRDRER(R RFRGRRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRIs   cs5x.|D]&}||||qWdS(N((R3RRDR(RFRRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJs  cs ||||S(N((R3RRD(RFRRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRKs (RCRRLR;(RRRRIRJRK(R RFRRB(RGRHsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRMs((RBRFR RRM((R RFRRBsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_ModifiedEncoderscs(tjfd}|S(sReturn a constructor for an encoder for a fixed-width field. Args: wire_type: The field's wire type, for encoding tags. format: The format string to pass to struct.pack(). cstj|rFt|tjtfd}|S|rtt|fd}|St|fd}|SdS(NcsL||t||x!|D]}||q+WdS(N(R (R3RRDR(tformatRGtlocal_struct_packRHR(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRIs  cs2x+|D]#}|||qWdS(N((R3RR4R(RPRQRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJs  cs|||S(N((R3RR4(RPRQRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRKs (tstructtpackRCRRLR;(RRRRIRJRK(RPRRB(RGRQRHsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRMs (RRtcalcsize(RBRPRM((RPRRBsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_StructPackEncoderscsktjdkr'dn(dkr?dntdfd}|S(sqReturn a constructor for an encoder for float fields. This is like StructPackEncoder, but catches errors that may be due to passing non-finite floating-point values to struct.pack, and makes a second attempt to encode those values. Args: wire_type: The field's wire type, for encoding tags. format: The format string to pass to struct.pack(). icSsR|tkr|dn5|tkr2|dn||krK|dndS(Nsss(t_POS_INFt_NEG_INF(R3R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeNonFiniteOrRaise+s      icSsR|tkr|dn5|tkr2|dn||krK|dndS(Nsss(RVRW(R3R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRX6s      sGCan't encode floating-point values that are %d bytes long (only 4 or 8)cstj|rIt|tjtfd}|S|rzt|fd}|St|fd}|SdS(Ncsq||t||xF|D]>}y||Wq+tk rh||q+Xq+WdS(N(R t SystemError(R3RRDR(RXRPRGRQRHR(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRIHs   csWxP|D]H}|y||Wqtk rN||qXqWdS(N(RY(R3RR4R(RXRPRQRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJUs    csF|y||Wntk rA||nXdS(N(RY(R3RR4(RXRPRQRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRK_s   (RRRSRCRRLR;(RRRRIRJRK(RXRPRRB(RGRQRHsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRMCs  (RRRTt ValueError(RBRPRM((RXRPRRBsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_FloatingPointEncoders     $s|D]6}|||j||j||qWdS(N(R Rd(R3RRDR(RGRa(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJs  cs0|||j||j||S(N(R Rd(R3RRD(RGRa(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRKs N(RCRRLR;R(RRRRJRK((RGRasD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMessageEncoders csvdjtdtjtdtjt|tdtjgtdtjtfd}|S(sEncoder for extensions of MessageSet. The message set message looks like this: message MessageSet { repeated group Item = 1 { required int32 type_id = 2; required string message = 3; } } R:iiics:|||j||j|||S(N(R Rd(R3RRD(t end_bytesRGt start_bytes(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRKs ( R>RCRRgR^R@RLRhR;(RRK((RkRGRlsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMessageSetItemEncoders  cs4|jt|jttfd}|S(sEncoder for extensions of MessageSet. Maps always have a wire format like this: message MapEntry { key_type key = 1; value_type value = 2; } repeated MapEntry map = N; cs_|rt|jn|}x:|D]2}jd|d||}|||q%WdS(NR%R(tsortedtkeysR&(R3RRDt value_keysR%R)(tencode_messageR,(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRK5s (R,RjR-R.(R/RK((RqR,sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt MapEncoder&s (Dt__doc__t __author__RRR1tgoogle.protobuf.internalRRVRWRRRRRRt Int32Sizert Int64Sizert EnumSizert UInt32Sizert UInt64Sizert ZigZagEncodet SInt32Sizert SInt64Sizert Fixed32Sizert SFixed32Sizert FloatSizert Fixed64Sizert SFixed64Sizert DoubleSizert BoolSizerRRR!R"R$R0R7R9R;t_EncodeSignedVarintR@RCRNRORUR[R^t Int32Encodert Int64Encodert EnumEncodert UInt32Encodert UInt64Encodert SInt32Encodert SInt64EncodertWIRETYPE_FIXED32tFixed32EncodertWIRETYPE_FIXED64tFixed64EncodertSFixed32EncodertSFixed64Encodert FloatEncodert DoubleEncoderR_RbRcRiRjRmRr(((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytAsn        !            ) " & R  %