ó Du—ac@sydZdZddlZddlZddlmZdZe Zd„Zd„Z d„Z d „Z d „Z d „Z e e ƒZZZe eƒZZe e ejƒZZe d ƒZZZe d ƒZZZe dƒZd„Zd„Zd„Zd„Z d„Z!d„Z"d„Z#d„Z$e#ƒZ%e$ƒZ&d„Z'd„Z(d„Z)d„Z*d„Z+d„Z,e)ej-e&e ƒZ.Z/Z0e)ej-e%eƒZ1Z2e*ej-e%eejƒZ3Z4e+ej5dƒZ6e+ej7dƒZ8e+ej5dƒZ9e+ej7d ƒZ:e,ej5d!ƒZ;e,ej7d"ƒZ<d#„Z=d$„Z>d%„Z?d&„Z@d'„ZAd(„ZBd)„ZCdS(*s­Code 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)iÿÿÿÿN(t wire_formatgðcCs”|dkrdS|dkr dS|dkr0dS|dkr@dS|d krPd S|d kr`d S|d krpdS|dkr€dS|dkrdSdS(s#Compute the size of a varint value.iiiÿ?iiÿÿiiÿÿÿilÿÿilÿÿÿilÿÿÿilÿÿÿÿilÿÿÿÿi 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|dkr€dS|dkrdS|dkr dSdS(s*Compute the size of a signed varint value.ii iiiÿ?iiÿÿiiÿÿÿilÿÿilÿÿÿilÿÿÿilÿÿÿÿilÿÿÿÿi ((R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_SignedVarintSize`s*          cCsttj|dƒƒS(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_TagSizeoscs‡fd†}|S(s…A sizer which uses the function compute_value_size to compute the size of each value. Typically compute_value_size is _VarintSize.csgt|ƒ‰|r1t‰‡‡‡fd†}|S|rM‡‡fd†}|S‡‡fd†}|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.pytPackedFieldSize†s cs5ˆt|ƒ}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 FieldSize”s(RR(Rt is_repeatedt is_packedR RR(R (R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt SpecificSizer‚s ((R R((R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _SimpleSizer~scs‡‡fd†}|S(s‘Like SimpleSizer, but modify_value is invoked on each value before it is passed to compute_value_size. modify_value is typically ZigZagEncode.cspt|ƒ‰|r4t‰‡‡‡‡fd†}|S|rS‡‡‡fd†}|S‡‡‡fd†}|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.pyRªs csˆˆˆ|ƒƒS(N((R(R RR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR±s(RR(RRRR RR(R R(R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRŸs ((R RR((R RsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_ModifiedSizer›scs‡fd†}|S(sWLike _SimpleSizer except for a fixed-size field. The input is the size of one value.csut|ƒ‰|r1t‰‡‡‡fd†}|S|rTˆˆ‰‡fd†}|Sˆˆ‰‡fd†}|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.pyRÆscsˆS(N((R(t field_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRËs(RR(RRRR RR(R(RRR R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR¼s   ((RR((RsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _FixedSizer¸siiicsTt|ƒ‰t‰t‰|r7‡‡‡fd†}|S‡‡‡fd†}|SdS(s#Returns a sizer for a string field.csNˆt|ƒ}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.pyRîs  cs'ˆ|jdƒƒ}ˆˆ|ƒ|S(Nsutf-8(R(RR(R RR (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRösN(RRR (RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt StringSizeræs csTt|ƒ‰t‰t‰|r7‡‡‡fd†}|S‡‡‡fd†}|SdS(s"Returns a sizer for a bytes field.csEˆt|ƒ}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 (RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt BytesSizerüs cs@t|ƒd‰|r)‡fd†}|S‡fd†}|SdS(s"Returns a sizer for a group field.ics5ˆt|ƒ}x|D]}||jƒ7}qW|S(N(R tByteSize(RRR(R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRs csˆ|jƒS(N(R(R(R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRsN(R(RRRRR((R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt GroupSizers csHt|ƒ‰t‰|r.‡‡fd†}|S‡‡fd†}|SdS(s$Returns a sizer for a message field.csEˆt|ƒ}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(RR(RRRRR((R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt MessageSizer$s csJtdƒdtdƒt|ƒtdƒ‰t‰‡‡fd†}|S(sìReturns 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|j‰t|jttƒ‰‡‡‡fd†}|S(s Returns a sizer for a map field.cs`d}xS|D]K}||}ˆjd|d|ƒ}|ˆ|ƒ7}ˆr |jƒq 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    ((R5((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@iii€l(R0R1(R2RR3R4((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeSignedVarint‡s      ((R7((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_SignedVarintEncoderƒs cCs&g}t|j|tƒdj|ƒS(s€Encode 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 _VarintBytes™scCstjttj||ƒƒƒS(sCEncode the given tag and return the bytes. Only called at startup.(R0t binary_typeR?RR(Rt wire_type((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytTagBytes¢scs‡‡‡fd†}|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|tjƒ‰t‰‡‡‡‡fd†}|S|ret|ˆƒ‰‡‡fd†}|St|ˆƒ‰‡‡fd†}|SdS(Ncsf|ˆƒd}x|D]}|ˆ|ƒ7}qWˆ|||ƒx|D]}ˆ|||ƒqHWdS(Ni((R2Rt deterministictsizeR(R t encode_valuetlocal_EncodeVarintt tag_bytes(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodePackedField»s   cs/x(|D] }|ˆƒˆ|||ƒqWdS(N((R2RRCR(RERG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeRepeatedFieldÆs  cs|ˆƒˆ|||ƒS(N((R2RRC(RERG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt EncodeFieldÍs (RBRtWIRETYPE_LENGTH_DELIMITEDR:(RRRRHRIRJ(R RERA(RFRGsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytSpecificEncoder·s((RARER RL((R RERAsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_SimpleEncoder¬s cs‡‡‡‡fd†}|S(s“Like SimpleEncoder but additionally invokes modify_value on every value before passing it to encode_value. Usually modify_value is ZigZagEncode.cs—|r=t|tjƒ‰t‰‡‡‡‡‡fd†}|S|rkt|ˆƒ‰‡‡‡fd†}|St|ˆƒ‰‡‡‡fd†}|SdS(Ncsr|ˆƒd}x$|D]}|ˆˆ|ƒƒ7}qWˆ|||ƒx$|D]}ˆ|ˆ|ƒ|ƒqNWdS(Ni((R2RRCRDR(R RERFRRG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRHÝs   cs5x.|D]&}|ˆƒˆ|ˆ|ƒ|ƒqWdS(N((R2RRCR(RERRG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRIès  cs |ˆƒˆ|ˆ|ƒ|ƒS(N((R2RRC(RERRG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJïs (RBRRKR:(RRRRHRIRJ(R RERRA(RFRGsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRLÙs((RARER RRL((R RERRAsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_ModifiedEncoderÕscs(tjˆƒ‰‡‡‡fd†}|S(s¿Return 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(). cs tj‰|rFt|tjƒ‰t‰‡‡‡‡‡fd†}|S|rtt|ˆƒ‰‡‡‡fd†}|St|ˆƒ‰‡‡‡fd†}|SdS(NcsL|ˆƒˆ|t|ƒˆ|ƒx!|D]}|ˆˆ|ƒƒq+WdS(N(R (R2RRCR(tformatRFtlocal_struct_packRGR(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRHs  cs2x+|D]#}|ˆƒ|ˆˆ|ƒƒqWdS(N((R2RR3R(RORPRG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRIs  cs|ˆƒ|ˆˆ|ƒƒS(N((R2RR3(RORPRG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJs (tstructtpackRBRRKR:(RRRRHRIRJ(RORRA(RFRPRGsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRLs (RQtcalcsize(RARORL((RORRAsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_StructPackEncoder÷scsktjˆƒ‰ˆdkr'd„‰n(ˆdkr?d„‰ntdˆƒ‚‡‡‡‡fd†}|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|dƒn5|tkr2|dƒn||krK|dƒn‚dS(Ns€s€ÿsÀ(t_POS_INFt_NEG_INF(R2R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeNonFiniteOrRaise+s      icSsR|tkr|dƒn5|tkr2|dƒn||krK|dƒn‚dS(Nsðsðÿsø(RURV(R2R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRW6s      sGCan't encode floating-point values that are %d bytes long (only 4 or 8)cs©tj‰|rIt|tjƒ‰t‰‡‡‡‡‡‡fd†}|S|rzt|ˆƒ‰‡‡‡‡fd†}|St|ˆƒ‰‡‡‡‡fd†}|SdS(Ncsq|ˆƒˆ|t|ƒˆ|ƒxF|D]>}y|ˆˆ|ƒƒWq+tk rhˆ||ƒq+Xq+WdS(N(R t SystemError(R2RRCR(RWRORFRPRGR(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRHHs   csWxP|D]H}|ˆƒy|ˆˆ|ƒƒWqtk rNˆ||ƒqXqWdS(N(RX(R2RR3R(RWRORPRG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRIUs    csF|ˆƒy|ˆˆ|ƒƒWntk rAˆ||ƒnXdS(N(RX(R2RR3(RWRORPRG(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJ_s   (RQRRRBRRKR:(RRRRHRIRJ(RWRORRA(RFRPRGsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRLCs  (RQRSt ValueError(RARORL((RWRORRAsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_FloatingPointEncoders     $s|D]6}|ˆƒˆ||jƒ|ƒ|j||ƒqWdS(N(RRc(R2RRCR(RFR`(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRIôs  cs0|ˆƒˆ||jƒ|ƒ|j||ƒS(N(RRc(R2RRC(RFR`(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJûs N(RBRRKR:(RRRRIRJ((RFR`sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMessageEncoderíscsvdjtdtjƒtdtjƒt|ƒtdtjƒgƒ‰tdtjƒ‰t‰‡‡‡fd†}|S(säEncoder 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; } } R9iiics:|ˆƒˆ||jƒ|ƒ|j||ƒ|ˆƒS(N(RRc(R2RRC(t end_bytesRFt start_bytes(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJs ( R=RBRRfR]R?RKRgR:(RRJ((RjRFRksD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMessageSetItemEncoders  cs4|j‰t|jttƒ‰‡‡fd†}|S(sÅEncoder 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|jƒƒn|}x:|D]2}ˆjd|d||ƒ}ˆ|||ƒq%WdS(NR$R(tsortedtkeysR%(R2RRCt value_keysR$R((tencode_messageR+(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJ5s (R+RiR,R-(R.RJ((RpR+sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt MapEncoder&s (Dt__doc__t __author__RQR0tgoogle.protobuf.internalRRURVRRRRRRt Int32Sizert Int64Sizert EnumSizert UInt32Sizert UInt64Sizert ZigZagEncodet SInt32Sizert SInt64Sizert Fixed32Sizert SFixed32Sizert FloatSizert Fixed64Sizert SFixed64Sizert DoubleSizert BoolSizerRRR R!R#R/R6R8R:t_EncodeSignedVarintR?RBRMRNRTRZR]t Int32Encodert Int64Encodert EnumEncodert UInt32Encodert UInt64Encodert SInt32Encodert SInt64EncodertWIRETYPE_FIXED32tFixed32EncodertWIRETYPE_FIXED64tFixed64EncodertSFixed32EncodertSFixed64Encodert FloatEncodert DoubleEncoderR^RaRbRhRiRlRq(((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytAsn        !            ) " & R  %