3 SLj @sdZdZddlZddlZddlZddlmZddlmZye Wne k rXe Z YnXddZ dd Z d d Zd d ZddZddZddZddZddZddZddZddZGdddejZdS) zpUtilities for Python proto2 tests. This is intentionally modeled on C++ code in //google/protobuf/test_util.*. z#robinson@google.com (Will Robinson)N)unittest_import_pb2) unittest_pb2cCs |jjdkS)NZproto2)Z DESCRIPTORZsyntax)messager/usr/lib/python3.6/test_util.pyIsProto27srcCsd|_d|_d|_d|_d|_d|_d|_d|_d |_d |_ d |_ d |_ d |_ d|_ d|_t|rjd|j_d|j_d|j_d|j_d|j_tjj|_tj|_t|rt j!|_"d|_#d|_$|j%j&d|j'j&d|j(j&d|j)j&d|j*j&d|j+j&d|j,j&d|j-j&d|j.j&d|j/j&d |j0j&d!|j1j&d"|j2j&d |j3j&d#|j4j&d$t|rd%|j5j6_d&|j7j6_d'|j8j6_d(|j9j6_d)|j:j6_|j;j&tjj<|j=j&tj>t|r|j?j&t j@|jAj&d*|jBj&d+|j%j&d,|j'j&d,|j(j&d,|j)j&d,|j*j&d,|j+j&d,|j,j&d,|j-j&d,|j.j&d,|j/j&d,|j0j&d,|j1j&d,|j2j&d |j3j&d-|j4j&d.d/|j%d0<d1|j'd0<d2|j(d0<d3|j)d0<d4|j*d0<d5|j+d0<d6|j,d0<d7|j-d0<d8|j.d0<d9|j/d0<d:|j0d0<d;|j1d0<d<|j2d0<d=|j3d0<d>|j4d0<t|r`d?|j5j6_d@|j7j6_dA|j8j6_dB|j9j6_dC|j:j6_|j;j&tjj<tjj|j;d0<|j=j&tjt|r|j?j&t j!|jAj&dD|jBj&dEt|rvdF|_CdG|_DdH|_EdI|_FdJ|_GdK|_HdL|_IdM|_JdN|_KdO|_LdP|_MdQ|_Nd<|_OdR|_PdS|_QtjjR|_StjT|_Ut jV|_WdT|_XdU|_YdV|_ZdW|j[_dX|_\dY|_]dZS)[zmSets every non-lazy field in the message to a unique value. Args: message: A TestAllTypes instance. efghijklmnopT115s116uvwx~124125215s216224225r00i-i.i/i0i1i2i3i4i5i6i7i8F315s316i=i>i?i@iG324325iiiiiiiiiiii415s416424425iYiZ603s604N)^optional_int32optional_int64optional_uint32optional_uint64optional_sint32optional_sint64optional_fixed32optional_fixed64optional_sfixed32optional_sfixed64optional_floatoptional_double optional_booloptional_stringoptional_bytesr optionalgroupaoptional_nested_messagebboptional_foreign_messagecoptional_import_messagedoptional_public_import_messageer TestAllTypesBAZoptional_nested_enum FOREIGN_BAZoptional_foreign_enumr IMPORT_BAZoptional_import_enumoptional_string_piece optional_cordrepeated_int32appendrepeated_int64repeated_uint32repeated_uint64repeated_sint32repeated_sint64repeated_fixed32repeated_fixed64repeated_sfixed32repeated_sfixed64repeated_floatrepeated_double repeated_boolrepeated_stringrepeated_bytes repeatedgroupaddrepeated_nested_messagerepeated_foreign_messagerepeated_import_messagerepeated_lazy_messagerepeated_nested_enumBARrepeated_foreign_enum FOREIGN_BARrepeated_import_enum IMPORT_BARrepeated_string_piece repeated_cord default_int32 default_int64default_uint32default_uint64default_sint32default_sint64default_fixed32default_fixed64default_sfixed32default_sfixed64 default_floatdefault_double default_booldefault_string default_bytesFOOdefault_nested_enum FOREIGN_FOOdefault_foreign_enum IMPORT_FOOdefault_import_enumZdefault_string_pieceZ default_cordZ oneof_uint32Zoneof_nested_messageZ oneof_stringZ oneof_bytes)rrrrSetAllNonLazyFields;s                                                                   rcCst|d|j_dS)N)roptional_lazy_messagerL)rrrr SetAllFieldssrcCsb|j}t}t}d||j<d||j<d||j<d||j<d||j<d||j<d||j <d||j <d ||j <d ||j <d ||j <d ||j<d ||j<d||j<d||j<d||j_d||j_d||j_d||j_d||j_d||j_|jj||j<|jj||j<|j ||j!<|j"||j#<d||j$<d||j%<||j&j'd||j(j'd||j)j'd||j*j'd||j+j'd||j,j'd||j-j'd||j.j'd||j/j'd ||j0j'd!||j1j'd"||j2j'd#||j3j'd ||j4j'd$||j5j'd%d&||j6j7_d'||j8j7_d(||j9j7_d)||j:j7_d*||j;j7_||j<j'|jj=||j>j'|j?||j@j'|jA||jBj'd+||jCj'd,||j&j'd-||j(j'd.||j)j'd/||j*j'd0||j+j'd1||j,j'd2||j-j'd3||j.j'd4||j/j'd5||j0j'd6||j1j'd7||j2j'd8||j3j'd9||j4j'd:||j5j'd;d<||j6j7_d=||j8j7_d>||j9j7_d?||j:j7_d@||j;j7_||j<j'|jj||j>j'|j ||j@j'|j"||jBj'dA||jCj'dBdC||jD<dD||jE<dE||jF<dF||jG<dG||jH<dH||jI<dI||jJ<dJ||jK<dK||jL<dL||jM<dM||jN<dN||jO<d9||jP<dO||jQ<dP||jR<|jjS||jT<|jU||jV<|jW||jX<dQ||jY<dR||jZ<dS||j[<dT||j\_dU||j]<dV||j^<dWS)XzzSets every extension in the message to a unique value. Args: message: A unittest_pb2.TestAllExtensions instance. rr r r r r rrrrrrTrs116rrrrrrrrrrrrr r!r"r#r$r%r&r'r(s216r)r*r+r,r-r.r/i-i.i/i0i1i2i3i4i5i6i7i8Fr3s316i=i>i?i@iGr4r5iiiiiiiiiiiir6s416r7r8iYiZr9s604N)_ ExtensionsrrZoptional_int32_extensionZoptional_int64_extensionZoptional_uint32_extensionZoptional_uint64_extensionZoptional_sint32_extensionZoptional_sint64_extensionZoptional_fixed32_extensionZoptional_fixed64_extensionZoptional_sfixed32_extensionZoptional_sfixed64_extensionZoptional_float_extensionZoptional_double_extensionZoptional_bool_extensionZoptional_string_extensionZoptional_bytes_extensionZoptionalgroup_extensionrJZ!optional_nested_message_extensionrLZ"optional_foreign_message_extensionrNZ!optional_import_message_extensionrPZ(optional_public_import_message_extensionrRZoptional_lazy_message_extensionrSrTZoptional_nested_enum_extensionrVZoptional_foreign_enum_extensionrXZoptional_import_enum_extensionZoptional_string_piece_extensionZoptional_cord_extensionZrepeated_int32_extensionr]Zrepeated_int64_extensionZrepeated_uint32_extensionZrepeated_uint64_extensionZrepeated_sint32_extensionZrepeated_sint64_extensionZrepeated_fixed32_extensionZrepeated_fixed64_extensionZrepeated_sfixed32_extensionZrepeated_sfixed64_extensionZrepeated_float_extensionZrepeated_double_extensionZrepeated_bool_extensionZrepeated_string_extensionZrepeated_bytes_extensionZrepeatedgroup_extensionrmZ!repeated_nested_message_extensionZ"repeated_foreign_message_extensionZ!repeated_import_message_extensionZrepeated_lazy_message_extensionZrepeated_nested_enum_extensionrsZrepeated_foreign_enum_extensionruZrepeated_import_enum_extensionrwZrepeated_string_piece_extensionZrepeated_cord_extensionZdefault_int32_extensionZdefault_int64_extensionZdefault_uint32_extensionZdefault_uint64_extensionZdefault_sint32_extensionZdefault_sint64_extensionZdefault_fixed32_extensionZdefault_fixed64_extensionZdefault_sfixed32_extensionZdefault_sfixed64_extensionZdefault_float_extensionZdefault_double_extensionZdefault_bool_extensionZdefault_string_extensionZdefault_bytes_extensionrZdefault_nested_enum_extensionrZdefault_foreign_enum_extensionrZdefault_import_enum_extensionZdefault_string_piece_extensionZdefault_cord_extensionZoneof_uint32_extensionZoneof_nested_message_extensionZoneof_string_extensionZoneof_bytes_extension)r extensionspb2Z import_pb2rrrSetAllExtensionss                                               rcCs.d|_d|_d|_d|jtj<d|jtj<dS)zSets every field and extension in the message to a unique value. Args: message: A unittest_pb2.TestAllExtensions message. r2foog?barN)my_int my_stringmy_floatrrmy_extension_intmy_extension_string)rrrrSetAllFieldsAndExtensionsms  rcCstj}tj}g}tj}d|_|j|j|jd|j|<|j|j|jd|_ |j|j|jd|j|<|j|j|jd|_ |j|j|jdj |}||krt d||fdS) zEnsures that serialized is the serialization we expect for a message filled with SetAllFieldsAndExtensions(). (Specifically, ensures that the serialization is in canonical, tag-number order). r2rrrg?zExpected %r, found %rN) rrrZTestFieldOrderingsrr]ZSerializeToStringZClearrrrjoin ValueError)Z serializedrrZexpected_stringsrZexpectedrrr#ExpectAllFieldsAndExtensionsInOrderzs,   rcCs |j|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jd |j|jd |j|jd |j|jd |j|jd |j|jd|j|jdt|r |j|jd|j|jd|j|jd|j|jd|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Rtjjtjj|drBtjj|d|}t|dStjj|d}qWd}tjj|d|}tjj|r~t|dStddS) zFFinds the given golden file and returns a file object representing it..zsrc/google/protobufzsrc/google/protobuf/testdatarbz..z'third_party/py/google/protobuf/testdatazCould not find golden files. This test must be run from within the protobuf source package so that it can read test data files from the C++ source tree.N)ospathexistsropen RuntimeError)filenamer full_pathrrr GoldenFileis    rc Cst| }|jSQRXdS)z5Finds the given golden file and returns its contents.N)rread)rfrrrGoldenFileDatas rcCs|jjddg|jjddg|jjddg|jjddg|jjd d g|jjd d g|jjd dg|jjddg|j jddg|j jddg|j jddg|j jddg|j jddg|jjtjtjgdS)zgSets every field in the message to a unique value. Args: message: A TestPackedTypes instance. iYiiZii[ii\ii]ii^ii_ii`iiaiibig@g8@g @g@@TFN)Z packed_int32extendZ packed_int64Z packed_uint32Z packed_uint64Z packed_sint32Z packed_sint64Zpacked_fixed32Zpacked_fixed64Zpacked_sfixed32Zpacked_sfixed64Z packed_floatZ packed_doubleZ packed_boolZ packed_enumrrurV)rrrrSetAllPackedFieldss rcCs*|j}t}||jjddg||jjddg||jjddg||jjddg||jjd d g||jjd d g||j jd dg||j jddg||j jddg||j jddg||j jddg||jjddg||jjddg||jjtjtjgdS)z}Sets every extension in the message to a unique value. Args: message: A unittest_pb2.TestPackedExtensions instance. iYiiZii[ii\ii]ii^ii_ii`iiaiibig@g8@g @g@@TFN)rrZpacked_int32_extensionrZpacked_int64_extensionZpacked_uint32_extensionZpacked_uint64_extensionZpacked_sint32_extensionZpacked_sint64_extensionZpacked_fixed32_extensionZpacked_fixed64_extensionZpacked_sfixed32_extensionZpacked_sfixed64_extensionZpacked_float_extensionZpacked_double_extensionZpacked_bool_extensionZpacked_enum_extensionrurV)rrrrrrSetAllPackedExtensionss"rcCs|jjddg|jjddg|jjddg|jjddg|jjd d g|jjd d g|jjd dg|jjddg|j jddg|j jddg|j jddg|j jddg|j jddg|jjtjtjgdS)zvSets every field in the message to a unique value. Args: message: A unittest_pb2.TestUnpackedTypes instance. iYiiZii[ii\ii]ii^ii_ii`iiaiibig@g8@g @g@@TFN)Zunpacked_int32rZunpacked_int64Zunpacked_uint32Zunpacked_uint64Zunpacked_sint32Zunpacked_sint64Zunpacked_fixed32Zunpacked_fixed64Zunpacked_sfixed32Zunpacked_sfixed64Zunpacked_floatZunpacked_doubleZ unpacked_boolZ unpacked_enumrrurV)rrrrSetAllUnpackedFieldss rc@sZeZdZdZdUddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Z d=d>Z!d?d@Z"dAdBZ#dCdDZ$dEdFZ%dGdHZ&dIdJZ'dKdLZ(dMdNZ)dOdPZ*dQdRZ+dSdTZ,dS)VNonStandardIntegeraAn integer object that does not subclass int. This is used to verify that both C++ and regular proto systems can handle integer others than int and long and that they handle them in predictable ways. NonStandardInteger is the minimal legal specification for a custom Integral. As such, it does not support 0 < x < 5 and it is not hashable. Note: This is added here instead of relying on numpy or a similar library with custom integers to limit dependencies. NcCs t|tr|j}||_||_dS)N) isinstancervalerror_string_on_conversion)selfrrrrr__init__s zNonStandardInteger.__init__cCs|jrt|jt|jS)N)rrlongr)rrrr__long__s zNonStandardInteger.__long__cCsttj|jS)N)roperatorabsr)rrrr__abs__szNonStandardInteger.__abs__cCsttj|j|S)N)rrrmr)ryrrr__add__szNonStandardInteger.__add__cCsttj|j|S)N)rrdivr)rrrrr__div__szNonStandardInteger.__div__cCstj|j|S)N)reqr)rrrrr__eq__szNonStandardInteger.__eq__cCsttj|j|S)N)rrfloordivr)rrrrr __floordiv__szNonStandardInteger.__floordiv__cCsttj|j|S)N)rrtruedivr)rrrrr __truediv__szNonStandardInteger.__truediv__cCsttj|jS)N)rrinvertr)rrrr __invert__szNonStandardInteger.__invert__cCsttj|j|S)N)rrmodr)rrrrr__mod__szNonStandardInteger.__mod__cCsttj|j|S)N)rrmulr)rrrrr__mul__szNonStandardInteger.__mul__cCsttj|jS)N)rrnegr)rrrr__neg__szNonStandardInteger.__neg__cCsttj|jS)N)rrposr)rrrr__pos__ szNonStandardInteger.__pos__cCsttj|j|S)N)rrpowr)rrrrr__pow__ szNonStandardInteger.__pow__cCs t|jS)N)intr)rrrr __trunc__szNonStandardInteger.__trunc__cCsttj||jS)N)rrrmr)rrrrr__radd__szNonStandardInteger.__radd__cCsttj||jS)N)rrrr)rrrrr__rdiv__szNonStandardInteger.__rdiv__cCsttj||jS)N)rrrr)rrrrr__rmod__szNonStandardInteger.__rmod__cCsttj||jS)N)rrrr)rrrrr__rmul__szNonStandardInteger.__rmul__cCsttj||jS)N)rrrr)rrrrr__rpow__szNonStandardInteger.__rpow__cCsttj||jS)N)rrrr)rrrrr __rfloordiv__!sz NonStandardInteger.__rfloordiv__cCsttj||jS)N)rrrr)rrrrr __rtruediv__$szNonStandardInteger.__rtruediv__cCsttj|j|S)N)rrlshiftr)rrrrr __lshift__'szNonStandardInteger.__lshift__cCsttj|j|S)N)rrrshiftr)rrrrr __rshift__*szNonStandardInteger.__rshift__cCsttj||jS)N)rrrr)rrrrr __rlshift__-szNonStandardInteger.__rlshift__cCsttj||jS)N)rrrr)rrrrr __rrshift__0szNonStandardInteger.__rrshift__cCst|tr|j}tj|j|S)N)rrrrle)rrrrr__le__3s zNonStandardInteger.__le__cCst|tr|j}tj|j|S)N)rrrrlt)rrrrr__lt__8s zNonStandardInteger.__lt__cCsttj|j|S)N)rrand_r)rrrrr__and__=szNonStandardInteger.__and__cCsttj|j|S)N)rror_r)rrrrr__or__@szNonStandardInteger.__or__cCsttj|j|S)N)rrxorr)rrrrr__xor__CszNonStandardInteger.__xor__cCsttj||jS)N)rrrr)rrrrr__rand__FszNonStandardInteger.__rand__cCsttj||jS)N)rrrr)rrrrr__ror__IszNonStandardInteger.__ror__cCsttj||jS)N)rrrr)rrrrr__rxor__LszNonStandardInteger.__rxor__cCs|jS)N)r)rrrr__bool__OszNonStandardInteger.__bool__cCs|jS)N)r)rrrr __nonzero__RszNonStandardInteger.__nonzero__cCs|S)Nr)rrrr__ceil__UszNonStandardInteger.__ceil__cCs|S)Nr)rrrr __floor__XszNonStandardInteger.__floor__cCs|jrt|jt|jS)N)rrrr)rrrr__int__[s zNonStandardInteger.__int__cCs|S)Nr)rrrr __round__`szNonStandardInteger.__round__cCs d|jS)NzNonStandardInteger(%s))r)rrrr__repr__cszNonStandardInteger.__repr__)N)-__name__ __module__ __qualname____doc__rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrsT  r)r __author__ZnumbersrZos.pathrZgoogle.protobufrrr NameErrorrrrrrrrrrrrrrZIntegralrrrrr#s4     R