� S�Ljc@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 [email protected] (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|dkr�dSdS(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|dkr�dS|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.pytRepeatedFieldSize�s 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�siiicsat|��t�t�| s%t�|rD���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 tAssertionError(RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt StringSizer�s  csat|��t�t�| s%t�|rD���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 R(RRRRR((R RR sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt BytesSizer�s  csMt|�d�| st�|r6�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(RR(RRRRR((R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt GroupSizers csUt|��t�| st�|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(RRR(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    ((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@iii�l(R1R2(R3RR4R5((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeSignedVarint�s      ((R8((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.(R1t 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((R3Rt 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((R3RRDR(RFRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytEncodeRepeatedField�s  cs|���|||�S(N((R3RRD(RFRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyt EncodeField�s (RCRtWIRETYPE_LENGTH_DELIMITEDR;(RRRRIRJRK(R RFRB(RGRHsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytSpecificEncoder�s((RBRFR RM((R RFRBsD/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((R3RRDRER(R RFRGRRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRI�s   cs5x.|D]&}|���|�|�|�qWdS(N((R3RRDR(RFRRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRJ�s  cs |���|�|�|�S(N((R3RRD(RFRRH(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRK�s (RCRRLR;(RRRRIRJRK(R RFRRB(RGRHsD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRM�s((RBRFR RRM((R RFRRBsD/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 (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_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(R3R((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�(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)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(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.pyRJ�s  cs0|���||j�|�|j||�S(N(R Rd(R3RRD(RGRa(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pyRK�s N(RCRRLR;R(RRRRJRK((RGRasD/usr/lib/python2.7/site-packages/google/protobuf/internal/encoder.pytMessageEncoder�s csvdjtdtj�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; } } 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|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&(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  %