Q,Qc@s[dZdZddlZddlmZdZe ZdZdZdZ d Z d Z d Z e eZ ZZe eZZe eejZZe d ZZZe d ZZZe dZdZdZdZdZdZ dZ!dZ"e!Z#e"Z$dZ%dZ&dZ'dZ(dZ)dZ*e'ej+e$eZ,Z-Z.e'ej+e#eZ/Z0e(ej+e#eejZ1Z2e)ej3dZ4e)ej5dZ6e)ej3dZ7e)ej5dZ8e*ej3d Z9e*ej5d!Z:d"Z;d#Z<d$Z=d%Z>d&Z?d'Z@dS((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 cStringIO. 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?iiiiiIiIiIiIiIi i ((tvalue((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _VarintSizeOs&         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?iiiiiIiIiIiIiIi ((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_TagSizelscsfd}|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 _FixedSizersiiicsTt|tt|r7fd}|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 (RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt StringSizers csTt|tt|r7fd}|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 (RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt BytesSizers cs@t|d|r)fd}|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(R(RRRRR((R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt GroupSizers csHt|t|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.pyR0s N(RR(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.pyRIs (RR(RR((R R"sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMessageSetItemSizer:s ! cstfd}|S(sBReturn an encoder for a basic varint value (does not include tag).csY|d@}|dL}x2|rH|d|B|d@}|dL}qW||S(Niii((twriteRtbits(t local_chr(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt EncodeVarintXs    (tchr(R'((R&sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_VarintEncoderTs cstfd}|S(sKReturn an encoder for a basic signed varint value (does not include tag).csr|dkr|d7}n|d@}|dL}x2|ra|d|B|d@}|dL}q0W||S(Niii@iiil((R$RR%(R&(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeSignedVarintis      (R((R*((R&sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_SignedVarintEncoderds cCs#g}t|j|dj|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 _EncodeVarinttappendtjoin(Rtpieces((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt _VarintBytes{scCsttj||S(sCEncode the given tag and return the bytes. Only called at startup.(R1RR(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(Ncs`|d}x|D]}||7}qW||x|D]}||qEWdS(Ni((R$RtsizeR(R t encode_valuetlocal_EncodeVarintt tag_bytes(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodePackedFields    cs,x%|D]}|||qWdS(N((R$RR(R5R7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeRepeatedFields  cs|||S(N((R$R(R5R7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt EncodeFields (R3RtWIRETYPE_LENGTH_DELIMITEDR-(RRRR8R9R:(R R5R2(R6R7sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytSpecificEncoders((R2R5R R<((R R5R2sD/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(Ncsl|d}x$|D]}||7}qW||x!|D]}||qKWdS(Ni((R$RR4R(R R5R6RR7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR8s    cs2x+|D]#}|||qWdS(N((R$RR(R5RR7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR9s  cs|||S(N((R$R(R5RR7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR:s (R3RR;R-(RRRR8R9R:(R R5RR2(R6R7sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR<s((R2R5R RR<((R R5RR2sD/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(NcsI||t|x!|D]}||q(WdS(N(R (R$RR(tformatR6tlocal_struct_packR7R(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR8s  cs2x+|D]#}|||qWdS(N((R$RR(R?R@R7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR9s  cs|||S(N((R$R(R?R@R7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR:s (tstructtpackR3RR;R-(RRRR8R9R:(R?RR2(R6R@R7sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR<s (RAtcalcsize(R2R?R<((R?RR2sD/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(Nttt(t_POS_INFt_NEG_INF(R$R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeNonFiniteOrRaise s      icSsR|tkr|dn5|tkr2|dn||krK|dndS(Nttt(RHRI(R$R((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJs      sGCan't encode floating-point values that are %d bytes long (only 4 or 8)cstj|rIt|tjtfd}|S|rzt|fd}|St|fd}|SdS(Ncsn||t|xF|D]>}y||Wq(tk re||q(Xq(WdS(N(R t SystemError(R$RR(RJR?R6R@R7R(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR8*s   csWxP|D]H}|y||Wqtk rN||qXqWdS(N(RN(R$RR(RJR?R@R7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR97s    csF|y||Wntk rA||nXdS(N(RN(R$R(RJR?R@R7(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR:As   (RARBR3RR;R-(RRRR8R9R:(RJR?RR2(R6R@R7sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyR<%s  (RARCt ValueError(R2R?R<((RJR?RR2sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt_FloatingPointEncoders     $sRDRPRSt Int32Encodert Int64Encodert EnumEncodert UInt32Encodert UInt64Encodert SInt32Encodert SInt64EncodertWIRETYPE_FIXED32tFixed32EncodertWIRETYPE_FIXED64tFixed64EncodertSFixed32EncodertSFixed64Encodert FloatEncodert DoubleEncoderRTRWRXR^R_Rb(((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytAsh       !           ) " & R  %