<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
hnV                @   s(  d Z dZddlZddlZddlZddlZddlZddlZddlZddl	Z	yddl
ZW n ek
rp   ddlZY nX ye W n ek
r   dd ZY nX ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ej rXe!Z"dd Z#dd Z$dd Z%dd Z&ej'Z'ej(defdefG dd  d e'Z)G d!d" d"e'Z*G d#d$ d$e'Z+G d%d& d&e'Z,G d'd( d(e'Z-ej.ej/ d)kpej0d0k d,G d-d. d.e'Z1e2d/kr$ej3  dS )1a  Tests python protocol buffers against the golden message.

Note that the golden messages exercise every known field type, thus this
test ends up exercising and verifying nearly all of the parsing and
serialization code in the whole library.

TODO(kenton):  Merge with wire_format_test?  It doesn't make a whole lot of
sense to call this a test of the "message" module, which only declares an
abstract interface.
z!gps@google.com (Gregory P. Smith)    Nc             C   s   | |k| |k  S )N )xyr   r   "/usr/lib/python3.6/message_test.py<lambda>>   s    r   )map_proto2_unittest_pb2)map_unittest_pb2)unittest_pb2)unittest_proto3_arena_pb2)descriptor_pb2)descriptor_pool)message_factory)text_format)api_implementation)encoder)packed_field_test_pb2)	test_util)testing_refleaks)message)_parameterizedc             C   s   | | kS )Nr   )valr   r   r   isnanV   s    r   c             C   s   t |  ot | d S )Nr   )r   )r   r   r   r   isinfY   s    r   c             C   s   t | o| dkS )Nr   )r   )r   r   r   r   IsPosInf\   s    r   c             C   s   t | o| dk S )Nr   )r   )r   r   r   r   IsNegInf^   s    r   Z_proto2Z_proto3c            
   @   s8  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^d_d`dadbe1 g i e2 g
Z3dcdd Z4dedf Z5dgdh Z6didj Z7dkdl Z8dmdn Z9dodp Z:G dqdr dre;Z<dsdt Z=dudv Z>dwdx Z?dydz Z@d{d| ZAd}d~ ZBdd ZCdd ZDdd ZEdd ZFdd ZGd]S )MessageTestc             C   sX   t j dkr| jd tjd}| jt}|jj| W d Q R X | j	dt
|j d S )NpythonzSkipping testBadUtf8String, currently only the python api implementation raises UnicodeDecodeError when a string field contains bad utf-8.Zbad_utf8_stringzTestAllTypes.optional_string)r   TypeZskipTestr   GoldenFileDataassertRaisesUnicodeDecodeErrorTestAllTypes
FromStringassertInstr	exception)selfmessage_moduleZbad_utf8_datacontextr   r   r   testBadUtf8Stringj   s    

zMessageTest.testBadUtf8Stringc             C   sr   |t krtjd}n
tjd}|j }|j| |t krDtj| | | j||j  tj	|}| j||j  d S )NZ golden_message_oneof_implementedZgolden_message_proto3)
r	   r   r   r!   ParseFromStringZExpectAllFieldsSetassertEqualSerializeToStringcopydeepcopy)r&   r'   golden_datagolden_messagegolden_copyr   r   r   testGoldenMessaget   s    


zMessageTest.testGoldenMessagec             C   sh   t jd}|j }|j| |j }t j| | j|| | j||j  tj|}| j||j  d S )Ngolden_packed_fields_message)	r   r   TestPackedTypesr*   ZSetAllPackedFieldsr+   r,   r-   r.   )r&   r'   r/   r0   all_setr1   r   r   r   testGoldenPackedMessage   s    



z#MessageTest.testGoldenPackedMessagec             C   s   |j  }| jt|jd | jt|jd tjdd}tj dkrv| jt	j
}|j| W d Q R X | jdt|j nBtjdd0}tjd	 |j| | jd
t|d j	 W d Q R X d S )Nr   0      r   zUnexpected end-group tag.T)recordalwaysz4Unexpected end-group tag: Not all data was converted)r!   r   	TypeErrorr"   	Exceptionr   ZTagBytesr   r   r   DecodeErrorr+   r$   r%   warningscatch_warningssimplefilter)r&   r'   msgZend_tagr(   wr   r   r   testParseErrors   s    

zMessageTest.testParseErrorsc          
      s   d}|j  }|jjddddg | j||jd d | j||jdd | j||jdd G d	d
 d
t G  fdddt}| j  |j| d W d Q R X d S )Ns5   
One string
Two string
Red stringBlue stringz
One stringz
Two stringz
Red stringzBlue string)deterministicFTc               @   s   e Zd ZdS )z:MessageTest.testDeterminismParameters.<locals>.BadArgErrorN)__name__
__module____qualname__r   r   r   r   BadArgError   s   rJ   c                   s$   e Zd Z fddZ fddZdS )z5MessageTest.testDeterminismParameters.<locals>.BadArgc                s
     d S )Nr   )r&   )rJ   r   r   __bool__   s    z>MessageTest.testDeterminismParameters.<locals>.BadArg.__bool__c                s
     d S )Nr   )r&   )rJ   r   r   rK      s    N)rG   rH   rI   rK   r   )rJ   r   r   BadArg   s   rL   )r!   repeated_stringextendr+   r,   r>   objectr   )r&   r'   r/   r0   rL   r   )rJ   r   testDeterminismParameters   s"    z%MessageTest.testDeterminismParametersc             C   s@   t jd}|j }|j| tj|}tj|}| j|| d S )Nr0   )r   r   r!   r*   pickledumpsloadsr+   )r&   r'   r/   r0   pickled_messageunpickled_messager   r   r   testPickleSupport   s    



zMessageTest.testPickleSupportc             C   s   |t krd}nd}|j }|j| | jt|j | jt|j | jt|jd  | jt|jd  | j	||j
  d S )Ns   ]  a              s    ]  a              r   )r	   r!   r*   
assertTruer   optional_floatoptional_doublerepeated_floatrepeated_doubler+   r,   )r&   r'   r/   r0   r   r   r   testPositiveInfinity   s    
z MessageTest.testPositiveInfinityc             C   s   |t krd}nd}|j }|j| | jt|j | jt|j | jt|jd  | jt|jd  | j	||j
  d S )Ns   ]  a              s    ]  a              r   )r	   r!   r*   rW   r   rX   rY   rZ   r[   r+   r,   )r&   r'   r/   r0   r   r   r   testNegativeInfinity   s    
z MessageTest.testNegativeInfinityc             C   s   d}|j  }|j| | jt|j | jt|j | jt|jd  | jt|jd  |j }|j  }|j| | jt|j | jt|j | jt|jd  | jt|jd  d S )Ns   ]  a              r   )	r!   r*   rW   r   rX   rY   rZ   r[   r,   )r&   r'   r/   r0   
serializedr   r   r   r   testNotANumber   s    

zMessageTest.testNotANumberc             C   sR   d}|j  }|j| | jt|jd  | jt|jd  | j||j  d S )Ns           r   )r4   r*   rW   r   packed_floatpacked_doubler+   r,   )r&   r'   r/   r0   r   r   r   testPositiveInfinityPacked  s    
z&MessageTest.testPositiveInfinityPackedc             C   sR   d}|j  }|j| | jt|jd  | jt|jd  | j||j  d S )Ns           r   )r4   r*   rW   r   r`   ra   r+   r,   )r&   r'   r/   r0   r   r   r   testNegativeInfinityPacked  s    
z&MessageTest.testNegativeInfinityPackedc             C   s   d}|j  }|j| | jt|jd  | jt|jd  |j }|j  }|j| | jt|jd  | jt|jd  d S )Ns           r   )r4   r*   rW   r   r`   ra   r,   )r&   r'   r/   r0   r^   r   r   r   r   testNotANumberPacked%  s    

z MessageTest.testNotANumberPackedc             C   st  |j  }tjdd}||_|j|j  | j|j|k dtjdd }||_|j|j  | j|j|k | |_|j|j  | j|j| k | |_|j|j  | j|j| k tjdd}||_|j|j  | j|j|k dtjdd }||_|j|j  | j|j|k | |_|j|j  | j|j| k | |_|j|j  | j|j| k d S )N      g      ?ii)r!   mathpowrX   r*   r,   rW   )r&   r'   r   kMostPosExponentNoSigBitskMostPosExponentOneSigBitkMostNegExponentNoSigBitskMostNegExponentOneSigBitr   r   r   testExtremeFloatValues3  s:    z"MessageTest.testExtremeFloatValuesc             C   st  |j  }tjdd}||_|j|j  | j|j|k dtjdd }||_|j|j  | j|j|k | |_|j|j  | j|j| k | |_|j|j  | j|j| k tjdd}||_|j|j  | j|j|k dtjdd }||_|j|j  | j|j|k | |_|j|j  | j|j| k | |_|j|j  | j|j| k d S )Nre   i  g      ?ii)r!   rg   rh   rY   r*   r,   rW   )r&   r'   r   ri   rj   rk   rl   r   r   r   testExtremeDoubleValues`  s:    z#MessageTest.testExtremeDoubleValuesc             C   s"   |j  }d|_| jt|d d S )Ng       @zoptional_float: 2.0
)r!   rX   r+   r$   )r&   r'   r   r   r   r   testFloatPrinting  s    zMessageTest.testFloatPrintingc             C   s>   |j  }d|_tjdkr*| jt|d n| jt|d d S )Ng^!7ݚ?   z%optional_double: 0.12345678912345678
z optional_double: 0.123456789123
)rp   )r!   rY   sysversion_infor+   r$   )r&   r'   r   r   r   r   testHighPrecisionFloatPrinting  s
    
z*MessageTest.testHighPrecisionFloatPrintingc             C   s<   |j  }tj| |j }|j|j  | jt|d d S )N )r!   r   ZSetAllNonLazyFieldsZTestEmptyMessager*   r,   r+   r$   )r&   r'   Z	populatedemptyr   r   r   testUnknownFieldPrinting  s
    
z$MessageTest.testUnknownFieldPrintingc             C   s   |j  }|jjdd |jjdd |jjdd |jjdd | jddddgdd |jD  | jddddgdd t|jD  | jddddgd	d |jd d d
 D  d S )Nr8   )bbre   rp   r9   c             S   s   g | ]
}|j qS r   )rw   ).0mr   r   r   
<listcomp>  s    z@MessageTest.testRepeatedNestedFieldIteration.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )rw   )rx   ry   r   r   r   rz     s    c             S   s   g | ]
}|j qS r   )rw   )rx   ry   r   r   r   rz     s    r<   )r!   repeated_nested_messageaddr+   reversed)r&   r'   rC   r   r   r    testRepeatedNestedFieldIteration  s    z,MessageTest.testRepeatedNestedFieldIterationc             C   s  |j  }|jjd |jjd |jjd |jj  | j|jd d | j|jd d | j|jd d | jt|jtdddg |jjd |jjd |jjd |jj  | j|jd d | j|jd d | j|jd d |jjd |jjd	 |jjd
 |jj  | j|jd d | j|jd d
 | j|jd d	 | jt|jtdd
d	g |j	jd |j	jd |j	jd |j	j  | j|j	d d | j|j	d d | j|j	d d | jt|j	tdddg dS )z7Check some different types with the default comparator.r8   rp   re   r   g?g?g333333?acb   a   c   bN)
r!   repeated_int32appendsortr+   r$   rZ   assertAlmostEqualrM   Zrepeated_bytes)r&   r'   r   r   r   r   0testSortingRepeatedScalarFieldsDefaultComparator  s@    



z<MessageTest.testSortingRepeatedScalarFieldsDefaultComparatorc             C   s   |j  }|jjd
 |jjd |jjd |jjtd | j|jd d | j|jd d | j|jd d |jjd |jjd |jjd |jjtd | j|jd d | j|jd d | j|jd d d	S )z2Check some different types with custom comparator.rp   re   r8   )keyr   aaarw   r   Nr<   r<   r   r   )r!   r   r   r   absr+   rM   len)r&   r'   r   r   r   r   /testSortingRepeatedScalarFieldsCustomComparator  s    z;MessageTest.testSortingRepeatedScalarFieldsCustomComparatorc             C   s   |j  }d|jj _d|jj _d|jj _d|jj _d|jj _d|jj _|jjtjdd | j|jd	 jd | j|jd jd | j|jd jd | j|jd jd | j|jd jd | j|jd jd | jt|jd
 dS )zECheck passing a custom comparator to sort a repeated composite field.r8   rp   re         r9   rw   )r   r   z0[bb: 1
, bb: 2
, bb: 3
, bb: 4
, bb: 5
, bb: 6
]N)	r!   r{   r|   rw   r   operator
attrgetterr+   r$   )r&   r'   r   r   r   r   2testSortingRepeatedCompositeFieldsCustomComparator  s     z>MessageTest.testSortingRepeatedCompositeFieldsCustomComparatorc             C   s   |j  }d|jj _d|jj _d|jj _d|jj _d|jj _d|jj _d|jj _|jjdd	 d
 | jdddddddgdd |jD  |j }|j  |j| | jdddddddgdd |jD  dS )zECheck passing a custom comparator to sort a repeated composite field.         !         
   c             S   s
   | j d S )Nr   )rw   )zr   r   r   r   
  s    zFMessageTest.testSortingRepeatedCompositeFieldsStable.<locals>.<lambda>)r   c             S   s   g | ]
}|j qS r   )rw   )rx   nr   r   r   rz     s    zHMessageTest.testSortingRepeatedCompositeFieldsStable.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )rw   )rx   r   r   r   r   rz     s    N)	r!   r{   r|   rw   r   r+   r,   ClearZMergeFromString)r&   r'   r   pbr   r   r   (testSortingRepeatedCompositeFieldsStable  s$    
z4MessageTest.testSortingRepeatedCompositeFieldsStablec             C   s@  |j  }tjd}dd }d|jj _d|jj _d|jj _d|jj _d|jj _d	|jj _|jj|d
 | jdd |jD dddd	ddg |jj|dd | jdd |jD ddd	dddg tj	dkrdS |jj|d | jdd |jD dddd	ddg |jj|dd | jdd |jD ddd	dddg dS )zECheck sorting a repeated composite field using list.sort() arguments.rw   c             S   s   t | j|jS )N)cmprw   )r   r   r   r   r   r     s    zEMessageTest.testRepeatedCompositeFieldSortArguments.<locals>.<lambda>r8   rp   re   r   r   r9   )r   c             S   s   g | ]
}|j qS r   )rw   )rx   kr   r   r   rz   %  s    zGMessageTest.testRepeatedCompositeFieldSortArguments.<locals>.<listcomp>T)r   reversec             S   s   g | ]
}|j qS r   )rw   )rx   r   r   r   r   rz   (  s    N)sort_functionc             S   s   g | ]
}|j qS r   )rw   )rx   r   r   r   r   rz   ,  s    )r   r   c             S   s   g | ]
}|j qS r   )rw   )rx   r   r   r   r   rz   /  s    )rp   )
r!   r   r   r{   r|   rw   r   r+   rq   rr   )r&   r'   r   Zget_bbZcmp_bbr   r   r   'testRepeatedCompositeFieldSortArguments  s.    

 z3MessageTest.testRepeatedCompositeFieldSortArgumentsc             C   s  |j  }|jjd |jjd |jjd |jjtd | jt|jdddg |jjtdd | jt|jdddg tjdk rdd }|jj|d	 | jt|jdddg |jj|dd
 | jt|jdddg |j	jd |j	jd |j	jd |j	jt
d | jt|j	dddg |j	jt
dd | jt|j	dddg tjd k rdd }|j	j|d	 | jt|j	dddg |j	j|dd
 | jt|j	dddg dS )!z9Check sorting a scalar field using list.sort() arguments.rp   re   r8   )r   T)r   r   c             S   s   t t| t|S )N)r   r   )r   r   r   r   r   r   >  s    zBMessageTest.testRepeatedScalarFieldSortArguments.<locals>.<lambda>)r   )r   r   r   rw   r   c             S   s   t t| t|S )N)r   r   )r   r   r   r   r   r   L  s    Nr   r   r<   r<   r   r   r   r   r<   )rp   r<   r   r   r   r   r<   )rp   )r!   r   r   r   r   r+   listrq   rr   rM   r   )r&   r'   r   Zabs_cmpZlen_cmpr   r   r   $testRepeatedScalarFieldSortArguments2  s6    
z0MessageTest.testRepeatedScalarFieldSortArgumentsc             C   sJ  |j  }|j  }|jjd |jjd |jjd |jjd |jjd |jjd d|jj _d|jj _d|jj _d|jj _d|jj _d|jj _tjdkrd S ||k }|j|jk }| jt	||d | jt	|j|jd | jt	|jdddgd | jt	|j|jd | j
t t	|j|jd d   W d Q R X d S )Nr   r8   re   rp   )rp   )r!   r   r   r{   r|   rw   rq   rr   r+   r   r   r=   )r&   r'   m1m2_r   r   r   testRepeatedFieldsComparableR  s2    
 
z(MessageTest.testRepeatedFieldsComparablec             C   s,   |j  }| j|jtj | j|jtj d S )N)r!   assertIsInstancer   collectionsMutableSequencer{   )r&   r'   ry   r   r   r   testRepeatedFieldsAreSequencesu  s    z*MessageTest.testRepeatedFieldsAreSequencesc             C   sL   |j  }| jt t|j W d Q R X | jt t|j W d Q R X d S )N)r!   r   r=   hashr   r{   )r&   r'   ry   r   r   r   testRepeatedFieldsNotHashable{  s
    z)MessageTest.testRepeatedFieldsNotHashablec             C   s*   |j  }|jjjg  | j|jd d S )Npayload)NestedTestAllTypesr   r   rN   rW   HasField)r&   r'   ry   r   r   r   $testRepeatedFieldInsideNestedMessage  s    z0MessageTest.testRepeatedFieldInsideNestedMessagec             C   s   t || | j|j| dS )zMake sure that a nested message object exists.

    As soon as a nested message attribute is accessed, it will be present in the
    _fields dict, without being marked as actually being set.
    N)getattrassertFalser   )r&   rC   Z	attributer   r   r   ensureNestedMessageExists  s    
z%MessageTest.ensureNestedMessageExistsc             C   s,   |j  }| jt|jd | jt|jd d S )NZno_such_oneof_fieldr   )r!   r   
ValueError
WhichOneofr>   )r&   r'   ry   r   r   r    testOneofGetCaseNonexistingField  s    z,MessageTest.testOneofGetCaseNonexistingFieldc             C   s   |j  }| jd |jd | j|jd d|_| jd|jd | j|jd | j|jd d|_| jd|jd | j|jd | j|jd d S )Noneof_fieldoneof_uint32r   oneof_stringrt   )	r!   assertIsr   r   r   r   r+   rW   r   )r&   r'   ry   r   r   r   testOneofDefaultValues  s    z"MessageTest.testOneofDefaultValuesc             C   sb  |j  }| jd |jd d|_| jd|jd | j|jd d|_| jd|jd | j|jd | j|jd |j	 | jd|jd | j|jd | j|jd |j	j
 | jd|jd | j|jd | j|jd d|j	_
| jd|jd | j|jd | j|jd d|_| jd|jd | j|jd | j|jd d S )	Nr   r   r   foor   oneof_nested_messages   bboneof_bytes)r!   r   r   r   r+   rW   r   r   r   r   rw   r   )r&   r'   ry   r   r   r   testOneofSemantics  s2    zMessageTest.testOneofSemanticsc             C   s>   |j  }d|_| j|d | jd|jd | jd|j d S )Nr   r   r   r   )r!   r   r   r+   r   )r&   r'   ry   r   r   r   !testOneofCompositeFieldReadAccess  s
    z-MessageTest.testOneofCompositeFieldReadAccessc             C   s   |j  }| jd |jd |tkr2| j|jd d|_| jd|jd |tkrb| j|jd d|_	| jd|jd |j
d | jd |jd |tkr| j|jd d S )Nr   r   r   s   bbr   )r!   r   r   r	   r   r   r   r+   rW   r   
ClearField)r&   r'   ry   r   r   r   testOneofWhichOneof  s    
zMessageTest.testOneofWhichOneofc             C   sV   |j  }d|_|jd |tkr0| j|jd | j|jd | jd |jd d S )Nr   r   r   )r!   r   r   r	   r   r   r   r   )r&   r'   ry   r   r   r   testOneofClearField  s    
zMessageTest.testOneofClearFieldc             C   sV   |j  }d|_|jd |tkr0| j|jd | j|jd | jd |jd d S )Nr   r   r   )r!   r   r   r	   r   r   r   r   )r&   r'   ry   r   r   r   testOneofClearSetField  s    
z"MessageTest.testOneofClearSetFieldc             C   sp   |j  }d|_| j|d |jd | jd|j |tkrJ| j|jd | j|jd | jd|jd d S )Nr   r   r   r   )	r!   r   r   r   r+   r	   rW   r   r   )r&   r'   ry   r   r   r   testOneofClearUnsetField  s    
z$MessageTest.testOneofClearUnsetFieldc             C   s:   |j  }d|_|j  }|j|j  | jd|jd d S )Nr   r   r   )r!   r   r*   r,   r+   r   )r&   r'   ry   r   r   r   r   testOneofDeserialize   s
    z MessageTest.testOneofDeserializec             C   s6   |j  }d|_|j  }|j| | jd|jd d S )Nr   r   r   )r!   r   CopyFromr+   r   )r&   r'   ry   r   r   r   r   testOneofCopyFrom  s
    
zMessageTest.testOneofCopyFromc             C   sb   |j  }d|j_|j  }d|j_d|jj_|j| | jd|jjd | jd|jjjd d S )Nr   s   bbr   r   r   )r   r   r   r   child	MergeFromr+   r   )r&   r'   ry   r   r   r   r   testOneofNestedMergeFrom  s    

z$MessageTest.testOneofNestedMergeFromc             C   sf   |j  }d|jj_d|jjj_|j  }d|j_|j| | jd|jjd | jd|jjjd d S )Nr      r   r   r   )	r   r   r   rw   r   r   r   r+   r   )r&   r'   ry   r   r   r   r   testOneofMessageMergeFrom  s    

z%MessageTest.testOneofMessageMergeFromc             C   s(   |j |j j d}| jd|jd d S )N)r   r   r   )r!   NestedMessager+   r   )r&   r'   ry   r   r   r   testOneofNestedMessageInit$  s    z&MessageTest.testOneofNestedMessageInitc             C   sB   |j  }d|_|j  | j|jd d|_| jd|jd d S )Nr   r   s   bbr   )r!   r   r   assertIsNoner   r   r+   )r&   r'   ry   r   r   r   testOneofClear)  s    zMessageTest.testOneofClearc             C   s&   |j  }td|_| j|jtj dS )zmAssigning a byte string to a string field should result
    in the value being converted to a Unicode string.rt   N)r!   r$   optional_stringr   six	text_type)r&   r'   ry   r   r   r   "testAssignByteStringToUnicodeField1  s    
z.MessageTest.testAssignByteStringToUnicodeFieldc             C   s   |j  }|jjd |jtdtt|j }| jt|jt| d|jj _|jtdtt|j }| jt|jt| dS )z|It should be possible to use long-valued indicies in slices

    This didn't used to work in the v2 C++ implementation.
    r8   r   rp   N)	r!   r   r   intr   r+   r{   r|   rw   )r&   r'   ry   Zslr   r   r   testLongValuedSlice8  s    zMessageTest.testLongValuedSlicec             C   sl   |j  }| jt }|jjdd tdD  W dQ R X | jt }|jjdd tdD  W dQ R X dS )z5This didn't use to work in the v2 C++ implementation.c             s   s   | ]
}t V  qd S )N)r   )rx   ir   r   r   	<genexpr>M  s    zCMessageTest.testExtendShouldNotSwallowExceptions.<locals>.<genexpr>r   Nc             s   s   | ]
}t V  qd S )N)r   )rx   r   r   r   r   r   P  s    )r!   r   	NameErrorr   rN   rangerepeated_nested_enum)r&   r'   ry   r   r   r   r   $testExtendShouldNotSwallowExceptionsI  s    $z0MessageTest.testExtendShouldNotSwallowExceptionsNFr   g            rt   c             C   s^   |j  }| jg |j x(tjD ]}|jj| | jg |j qW |jjg  | jg |j dS )z,Test no-ops extending repeated int32 fields.N)r!   assertSequenceEqualr   r   FALSY_VALUESrN   )r&   r'   ry   falsy_valuer   r   r   testExtendInt32WithNothingT  s    z&MessageTest.testExtendInt32WithNothingc             C   s^   |j  }| jg |j x(tjD ]}|jj| | jg |j qW |jjg  | jg |j dS )z,Test no-ops extending repeated float fields.N)r!   r   rZ   r   r   rN   )r&   r'   ry   r   r   r   r   testExtendFloatWithNothinga  s    z&MessageTest.testExtendFloatWithNothingc             C   s^   |j  }| jg |j x(tjD ]}|jj| | jg |j qW |jjg  | jg |j dS )z-Test no-ops extending repeated string fields.N)r!   r   rM   r   r   rN   )r&   r'   ry   r   r   r   r   testExtendStringWithNothingn  s    z'MessageTest.testExtendStringWithNothingc             C   s   |j  }| jg |j |jjdg | jdg|j |jjddg | jdddg|j |jjddg | jdddddg|j dS )z7Test extending repeated int32 fields with python lists.r   r8   re   rp   r9   N)r!   r   r   rN   )r&   r'   ry   r   r   r   testExtendInt32WithPythonList{  s    z)MessageTest.testExtendInt32WithPythonListc             C   s   |j  }| jg |j |jjdg | jdg|j |jjddg | jdddg|j |jjddg | jdddddg|j dS )z7Test extending repeated float fields with python lists.g        g      ?g       @g      @g      @N)r!   r   rZ   rN   )r&   r'   ry   r   r   r   testExtendFloatWithPythonList  s    z)MessageTest.testExtendFloatWithPythonListc             C   s   |j  }| jg |j |jjdg | jdg|j |jjddg | jdddg|j |jjddg | jdddddg|j dS )z8Test extending repeated string fields with python lists.rt   Z11Z22Z33Z44N)r!   r   rM   rN   )r&   r'   ry   r   r   r   testExtendStringWithPythonList  s    z*MessageTest.testExtendStringWithPythonListc             C   s:   |j  }| jg |j |jjd | jdddg|j dS )zDTest extending repeated string fields with characters from a string.abcr   r   r   N)r!   r   rM   rN   )r&   r'   ry   r   r   r   testExtendStringWithString  s    z&MessageTest.testExtendStringWithStringc               @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )zMessageTest.TestIterablezThis iterable object mimics the behavior of numpy.array.

    __nonzero__ fails for length > 1, and returns bool(item[0]) for length == 1.

    Nc             C   s   |pg | _ d S )N)_list)r&   valuesr   r   r   __init__  s    z!MessageTest.TestIterable.__init__c             C   s8   t | j}|dkrdS |dkr,t| jd S tdd S )Nr   Fr8   zTruth value is ambiguous.)r   r   boolr   )r&   sizer   r   r   rK     s    
z!MessageTest.TestIterable.__bool__c             C   s
   t | jS )N)r   r   )r&   r   r   r   __len__  s    z MessageTest.TestIterable.__len__c             C   s
   | j j S )N)r   __iter__)r&   r   r   r   r     s    z!MessageTest.TestIterable.__iter__)N)rG   rH   rI   __doc__r   rK   r   r   r   r   r   r   TestIterable  s
   
r   c             C   s   |j  }| jg |j |jjtjg  | jg |j |jjtjdg | jdg|j |jjtjddg | jdddg|j |jjtjddg | jdddddg|j dS )z3Test extending repeated int32 fields with iterable.r   r8   re   rp   r9   N)r!   r   r   rN   r   r   )r&   r'   ry   r   r   r   testExtendInt32WithIterable  s    z'MessageTest.testExtendInt32WithIterablec             C   s   |j  }| jg |j |jjtjg  | jg |j |jjtjdg | jdg|j |jjtjddg | jdddg|j |jjtjddg | jdddddg|j dS )z3Test extending repeated float fields with iterable.g        g      ?g       @g      @g      @N)r!   r   rZ   rN   r   r   )r&   r'   ry   r   r   r   testExtendFloatWithIterable  s    z'MessageTest.testExtendFloatWithIterablec             C   s   |j  }| jg |j |jjtjg  | jg |j |jjtjdg | jdg|j |jjtjddg | jdddg|j |jjtjddg | jdddddg|j dS )z4Test extending repeated string fields with iterable.rt   1234N)r!   r   rM   rN   r   r   )r&   r'   ry   r   r   r   testExtendStringWithIterable  s    z(MessageTest.testExtendStringWithIterablec          
   C   sP   t j dkst j dkrd S |j }| jtj}tj|jtj	 W d Q R X d S )Ncppre   )
r   r   ZVersionr!   r   rQ   ZPickleErrorrR   r   ZHIGHEST_PROTOCOL)r&   r'   ry   r   r   r   r   !testPickleRepeatedScalarContainer  s    z-MessageTest.testPickleRepeatedScalarContainerc             C   s   |j  }|jj  dS )z?Exercise a scenario that has led to segfaults in the past.
    N)r!   r{   r   )r&   r'   ry   r   r   r   'testSortEmptyRepeatedCompositeContainer  s    z3MessageTest.testSortEmptyRepeatedCompositeContainerc          	   C   s,   |j  }| jt}|jd W dQ R X dS )zBUsing HasField on a repeated field should raise an exception.
    r   N)r!   r   r   r   )r&   r'   ry   r   r   r   r   testHasFieldOnRepeatedField  s    z'MessageTest.testHasFieldOnRepeatedFieldc             C   s   |j  }| jt}|jj  W d Q R X |jjttd | jd|jj  | jd|jjd | jd|jjd | jddg|j d S )Nr   r9   r   re   r8   rp   )	r!   r   
IndexErrorr   poprN   r   r   r+   )r&   r'   ry   r   r   r   r   testRepeatedScalarFieldPop  s    z&MessageTest.testRepeatedScalarFieldPopc             C   s   |j  }| jt}|jj  W d Q R X | jt}|jjd W d Q R X x tdD ]}|jj }||_qTW | j	d|jj j | j	d|jjdj | j	d|jjdj | j	ddgdd	 |jD  d S )
Nr7   r   r9   r   re   r8   rp   c             S   s   g | ]
}|j qS r   )rw   )rx   r   r   r   r   rz     s    z=MessageTest.testRepeatedCompositeFieldPop.<locals>.<listcomp>)
r!   r   r  r{   r  r=   r   r|   rw   r+   )r&   r'   ry   r   r   r   r   r   r   testRepeatedCompositeFieldPop  s    

z)MessageTest.testRepeatedCompositeFieldPopc             C   s\   |j  }x.tdD ]"}|jj|| |jj }||_qW | j|j|j | j|j|j d S )Nr   )	r!   r   r   insertr{   r|   rw   r   r+   )r&   r'   ry   r   r   r   r   r   testRepeatedCompareWithSelf  s    

z'MessageTest.testRepeatedCompareWithSelfc             C   s6   |j  }|jj }|j}|jj }| j|jjd dS )zwA case that lead to a segfault when a message detached from its parent
    container has itself a child container.
    r   N)r   Zrepeated_childr|   r   r+   r   optional_int32)r&   r'   ry   r   r   r   testReleasedNestedMessages  s
    

z&MessageTest.testReleasedNestedMessagesc             C   sz   |j  }| jt g |_W d Q R X |jjd tj dkrZ| jt g |_W d Q R X n| jt g |_W d Q R X d S )Nr8   r   )r!   r   AttributeErrorr   r   r   r   r=   )r&   r'   ry   r   r   r   testSetRepeatedComposite(  s    z$MessageTest.testSetRepeatedComposite)HrG   rH   rI   r)   r2   r6   rE   rP   rV   r\   r]   r_   rb   rc   rd   rm   rn   ro   rs   rv   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	bytearraysetr   r   r   r   r   r   r   r   rO   r   r   r   r   r  r  r  r  r  r	  r  r  r   r   r   r   r   e   s   
$			--( #	#		
	
	
r   c               @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )
Proto2Testc             C   s  t j }| j|jd | j|jd | j|jd | jt |jd W d Q R X | jt |jd W d Q R X | jt |jd W d Q R X | jd|j | jd|j | jd|j	j
 d|_d|_d|j	_
| j|jd | j|jd | j|jd d	|_d
|_d|j	_
| j|jd | j|jd | j|jd |jd |jd |jd | j|jd | j|jd | j|jd | jd|j | jd|j | jd|j	j
 d S )Nr
  optional_booloptional_nested_messagefield_doesnt_existr   r{   r   Fr   T   )r	   r!   r   r   r   r   r+   r
  r  r  rw   rW   r   )r&   r   r   r   r   testFieldPresence:  sD    


zProto2Test.testFieldPresencec             C   s
  t j }| jt}d|_W dQ R X | jt|jjd |jjd d|jd< | jt d|jd< W dQ R X tj }d|_|jjd |j }t j }|j	| | j
|jd | jd|j | jdt|j |j  |j	|j  | jd|j | jd|jd  dS )	z:Assigning an invalid enum number is not allowed in proto2.i Nre   r   i@ it optional_nested_enumr8   )r	   r!   r   r   r  r   r   r
   r,   r*   r   r   r+   r   r   )r&   ry   r   r   r^   Zm3r   r   r   testAssignInvalidEnumj  s*    

z Proto2Test.testAssignInvalidEnumc          
   C   s6   t j }d|jd< | jt d|jd< W d Q R X d S )Nr   {   r8   )r   ZTestEnumMapZknown_map_fieldr   r   Zunknown_map_field)r&   ry   r   r   r   testUnknownEnumMap  s    
zProto2Test.testUnknownEnumMapc             C   s   t j }| jtt|d d S )N
Extensions)r	   r!   r   r  r   )r&   rC   r   r   r   testExtensionsErrors  s    zProto2Test.testExtensionsErrorsc             C   sh   t jd}tj }|j| tj }t j| | j|| | j||j  tj	|}| j||j  d S )Nr0   )
r   r   r	   ZTestAllExtensionsr*   ZSetAllExtensionsr+   r,   r-   r.   )r&   r/   r0   r5   r1   r   r   r   testGoldenExtensions  s    



zProto2Test.testGoldenExtensionsc             C   sh   t jd}tj }|j| tj }t j| | j|| | j||j  tj	|}| j||j  d S )Nr3   )
r   r   r	   ZTestPackedExtensionsr*   ZSetAllPackedExtensionsr+   r,   r-   r.   )r&   r/   r0   r5   r1   r   r   r   testGoldenPackedExtensions  s    



z%Proto2Test.testGoldenPackedExtensionsc             C   sN   t jdd}tj|}tj|}| j|| | j|jd | jtj	|j
 d S )Nr8   )r   )r	   ZTestRequiredrQ   rR   rS   r+   r   r   r   ZEncodeErrorr,   )r&   r0   rT   rU   r   r   r   testPickleIncompleteProto  s    

z$Proto2Test.testPickleIncompleteProtoc             C   s  t j t j t j g}d|d _d|d _d|d _d|d _t j }d|_d|_d|_t jj }|jj| |j	j| |j
j| |jj| |jj| |jj jj|d  |jj jj|d  |jj jj|d  |jj jj|d  |jj jj|d  |jj jj|d  |j }t j }|j| | j|j| | j|j| | j|jj| | j|jt jj | | jt|jd | jt|jd | jt|jt jj d dS )zcCheck the merge behavior when a required or optional field appears
    multiple times in the input.r8   r   re   rp   ZhelloN)r	   r!   r
  optional_int64r   ZTestParsingMergeZRepeatedFieldsGeneratorZfield1rN   Zfield2Zfield3Zext1Zext2Zgroup1r|   r   Zgroup2r,   r*   r+   Zrequired_all_typesZoptional_all_typesoptionalgroupZoptional_group_all_typesr  Zoptional_extr   Zrepeated_all_typesrepeatedgroupZrepeated_ext)r&   ZmessagesZmerged_message	generatordataZparsing_merger   r   r   testParsingMerge  sJ    








zProto2Test.testParsingMergec             C   s`  t jddddddiddii d	dd
iddigdt jjgddd}| j|t j | jd|j | jd|j | jd|j | jd|j | jd|j	j
 | j|jt jj | jd|jj | j|jd | j|jt j  | jt jj|j | jdt|j | jd
|jd j
 | jd|jd j
 | jdt|j | jt jj|jd  | jt jj|jd  | jd|j | jd|j | j|jd | jdt|j | jd|j t jd	d}| jt jj|j | jt t jddid W d Q R X | jt t jddid W d Q R X | jt t jdd W d Q R X | jt t jdd W d Q R X d S )Nd      g     r@   xr   i  rw   i  BAZiX  i  FOOi   r   )r
  optional_fixed32rX   optional_bytesr   r  optional_foreign_messager  r!  r   default_int32r   r,  re   r   r8   r  *   )r  ZINVALID_NESTED_FIELD   )r  ZINVALID_VALUE_TYPEZINVALID_LABEL)r   ) r	   r!   ZBARr   r+   r
  r*  rX   r+  r   r   r  r   rw   rW   r   r,  ForeignMessager(  r  r   r!  r   r)  r-  r   r   rZ   Zdefault_int64r   r   r=   )r&   r   r   r   r   testPythonicInit  sl    






zProto2Test.testPythonicInitN)rG   rH   rI   r  r  r  r  r  r  r  r$  r1  r   r   r   r   r  8  s   01r  c               @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAS )B
Proto3Testc             C   s@   t |}x&|D ]\}}| j|||  ||= qW | ji | d S )N)dictr+   )r&   Zmap_iterZ
dict_valuer   vr   r   r   assertMapIterEquals+  s
    
zProto3Test.assertMapIterEqualsc          4   C   s  t j }| jt |jd W d Q R X | jt |jd W d Q R X | jt |jd W d Q R X | jt |jd W d Q R X | j|jd | jt |jd W d Q R X | jt |jd W d Q R X | jt |jd W d Q R X | jd	|j | jd	|j | jd
|j	 | jd|j
 | jd	|jj d	|j_| j|jd d|_d|_d|_	d|_
d|j_|jd |jd |jd |jd |jd | jd	|j | jd	|j | jd
|j	 | jd|j
 | jd	|jj d S )Nr
  rX   r   r  r  r  r   r{   r   rt   Fr   g?r   Tr  )r
   r!   r   r   r   r   r+   r
  rX   r   r  r  rw   rW   r   )r&   r   r   r   r   r  5  sL    




zProto3Test.testFieldPresencec             C   s   t j }d|_| jd|j |jjd | jd|jd  d|jd< | jd|jd  |j }t j }|j| | jd|j | jd|jd  dS )zCAssigning an unknown enum value is allowed and preserves the value.i iTr   it N)r
   r!   r  r+   r   r   r,   r*   )r&   ry   r^   r   r   r   r   testAssignUnknownEnumn  s    

z Proto3Test.testAssignUnknownEnumc             C   sN  t j }| jd|jk | jd|jk | jd|jk | jd|jk | jd|jk | jd|jk | jd|j	k | jd|j
k | jd|jk | jd|jd  | jd|jd  | jd|jd  | jd|jd  | jd	|jd  | jt|jd t | jd|jd  | jt|jd t | jd
|j	d  | jd|j
d  | jd|jd  | jd|jk | jd|jk | jd|jk | jd|jk | jd|jk | jd|jk | jd|j	k | jd|j
k | jd|jk | j|j	d tj | jt |j	d  W d Q R X | jt d|j	k W d Q R X d S )Nr  re   r   Fr   o   ix  r   g        rt   r   il        l     l        il        l     l        il        l     l        )r   TestMapr   map_int32_int32map_int64_int64map_uint32_uint32map_uint64_uint64map_int32_doublemap_bool_boolmap_string_stringZmap_int32_bytesmap_int32_enumr+   rW   
isinstancefloatr   r   r   r   r   r=   )r&   rC   r   r   r   testScalarMapDefaults  sF    z Proto3Test.testScalarMapDefaultsc             C   s&  t j }| j|jjd | jd|jjdd | j|jjd d|jd< | jd|jjd | jd|jjd | jt |jjd W d Q R X | j|jjd | jd|jjdd |jd }| j	||jjd t
j dkr| jt |jjd W d Q R X n| jd |jjd d S )Nr   r   r  rt   r   )r   r8  r   r9  getr+   r   r=   map_int32_foreign_messager   r   r   )r&   rC   submsgr   r   r   
testMapGet  s"    

zProto3Test.testMapGetc          !   C   s4  t j }| jdt|j | jd|jk d|jd< d|jd< d|jd< d|jd< d|j	d< d	|j
d
< d|jd< d|jd< d|jd< d|jd< | jg |j  | jd
t|j | jt d|jd< W d Q R X | jd
t|j | jt d|jd< W d Q R X |j }t j }|j| | jt d|jd< W d Q R X | jt d|jd< W d Q R X | 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  tj dkr0| jdt|j d S )!Nr   r   i  r  re   "   r   g333333?gffffff
@r8   123r   Tix  r   z{-123: -456}i8il        l     l        l     l        l        i8il        l     l        l     l        l        )r   r8  r+   r   r9  r   r:  r;  r<  Zmap_int32_floatr=  r?  r>  r@  FindInitializationErrorsr   r=   r,   r*   r   r   r   r$   )r&   rC   r^   msg2r   r   r   testScalarMap  sP    










zProto3Test.testScalarMapc             C   s@   t j }d|jd< d|jd< | j|j d | jd|j  d S )Nr   rt   r   s   
  r
  )r   r8  r9  r?  r+   ByteSizer,   )r&   rC   r   r   r   testMapEntryAlwaysSerialized  s    

z'Proto3Test.testMapEntryAlwaysSerializedc             C   sn   t j }d}|jd}||j|< t|jj d \}}| j|| | j|| | j|tj	 | j|tj	 d S )Nu   ሴutf8r   )
r   r8  encoder?  r   itemsr+   r   r   r   )r&   rC   Zunicode_objZ	bytes_objr   valuer   r   r    testStringUnicodeConversionInMap  s    

z+Proto3Test.testStringUnicodeConversionInMapc             C   sZ  t j }| jdt|j | jd|jk |jd  |jjd
 | jdt|j | jd|j | jd|j | jdt|j | jt	 |jd  W d Q R X | jt
 |jd |jd< W d Q R X | jdt|j |j }t j }|j| | jdt|j | jd|j | jd|j | jdt|j tj dkrV| jd	tt|j d S )Nr   r   r  i  re   rI  i  r   r  i8i8i8)r   r8  r+   r   rE  r   Zget_or_creater#   r   r=   r   r,   r*   r   r   r$   )r&   rC   r^   rK  r   r   r   testMessageMap&  s0    

zProto3Test.testMessageMapc             C   s   t j }d|jd j_|jd= d|jd j_| jdt|j d|jd j_| jdt|j |j }t j }|j| ddg}x|D ]}|j|= qW d S )Nr8   re   )	r   r8  map_int32_all_typesr  rw   r+   r   r,   r*   )r&   rC   r^   rK  keysr   r   r   r   testNestedMessageMapItemDeleteO  s    

z)Proto3Test.testNestedMessageMapItemDeletec             C   sp   t j }d|jd< |j }d|jd< | j|j |d  d|jd _|j }d|jd _| j|j |d  d S )Nr8         )r   r8  r9  rM  r+   rE  r   )r&   rC   r   r   r   r   testMapByteSize`  s    

zProto3Test.testMapByteSizec          	   C   s  t j }d|jd< d|jd< d|jd< d|jd _d	|jd
 _t j }d|jd< d|jd< d|jd
 _d|jd
 _|jd
 }|j| | jd|jd  | jd|jd  | jd|jd  | jd|jd  | jd|jd j | jd	|jd
 j | j	|jd
 j
d tj dkr(| jd|j i }x.|jD ]$}| j	||k |j| j||< q4W | jdd	d| |jd= | j	d|jk |jd
= | j	d
|jk tj dkr| jt |jd= W d Q R X n| jt |jd= W d Q R X d S )NrH  r   N   8   r      r   r7  r      7   c   X   r  r   dr   )r7  r^  rt   )r   r8  r9  r:  rE  r   rb  r   r+   r   r   r   r   r   r=   KeyError)r&   rC   rK  Zold_map_valueZas_dictr   r   r   r   testMergeFroml  sH    






zProto3Test.testMergeFromc          	   C   s.   t j }| jtd |jd W d Q R X d S )NzWParameter to MergeFrom\(\) must be instance of same class: expected .*TestMap got int\.r8   )r   r8  assertRaisesRegexr=   r   )r&   rC   r   r   r   testMergeFromBadType  s
    zProto3Test.testMergeFromBadTypec          	   C   s.   t j }| jtd |jd W d Q R X d S )Nz[Parameter to [A-Za-z]*From\(\) must be instance of same class: expected .*TestMap got int\.r8   )r   r8  re  r=   r   )r&   rC   r   r   r   testCopyFromBadType  s
    zProto3Test.testCopyFromBadTypec             C   s   t j }td|jtd< td	|jtd< td|jtd< td|jtd< |j }t j }|j| | j	d|jd  | j	d|jd  | j	d|jd  | j	d|jd  d S )Ni  r  re   rH  r   i8il        l     l        l     l        l        i8il        l     l        l     l        l        )
r   r8  r   r9  r:  r;  r<  r,   r*   r+   )r&   rC   r^   rK  r   r   r   testIntegerMapWithLongs  s    
z"Proto3Test.testIntegerMapWithLongsc             C   s   t j }d|jjd< |j }t j }|j| | j|| d|jjd< |j }|j| | j|| |jjj  |j }|j| | j|| d S )Ni  r  i  ix  )r   TestMapSubmessagetest_mapr9  r,   r*   r+   clear)r&   rC   r^   rK  r   r   r   testMapAssignmentCausesPresence  s    


z*Proto3Test.testMapAssignmentCausesPresencec             C   s   t j }d|jjd _|j }t j }|j| | j|| d|jjd _|j }|j| | j|| |jjd j|jjd  |j }|j| | j|| |jjj	  |j }|j| | j|| d S )Nr   r     ix  )
r   ri  rj  rE  r   r,   r*   r+   r   rk  )r&   rC   r^   rK  r   r   r   -testMapAssignmentCausesPresenceForSubmessages  s&    



z8Proto3Test.testMapAssignmentCausesPresenceForSubmessagesc             C   s~   t j }t|j}t|j}d|jd< d|jd _| jt x|D ]}qDW W d Q R X | jt x|D ]}qhW W d Q R X d S )NrI  r   r   )r   r8  iterr?  rE  r   r   RuntimeError)r&   rC   Zstring_string_iterZint32_foreign_iterr   r   r   r   testModifyMapWhileIterating  s    




z&Proto3Test.testModifyMapWhileIteratingc          
   C   s   t j }|jd }| j||jd  | j|tj d|_|j }t j }|j	| | j
d|jd j | jt tj |jd< W d Q R X d S )Nr7  r   ra  )r   r8  rE  r   r   r	   r0  r   r,   r*   r+   r   r   )r&   rC   rF  r^   rK  r   r   r   testSubmessageMap  s    

zProto3Test.testSubmessageMapc             C   s   t j }x$t|jj D ]\}}| jd qW d|jd< d|jd< d|jd< | jdt|j dddd}| jt|jj | d S )NFr9   re   r   rp      )re   rp   r9   )	r   r8  r   r9  rQ  rW   r+   r   r5  )r&   rC   r   r4  matching_dictr   r   r   testMapIteration  s    


zProto3Test.testMapIterationc                s&  t jdk r"tj }d|jd< d|jd< d|jd< d|jd< |j} jdt| tj }|j|j   fdd	}|t	|j
 t|j
  |t	|j t|j  |t	|j t|j   jd|jd  jd |jd
  jd|jd  jd|jd  jdt| |j \}} jd| |  jdt| |j   jdt|  jt |j  W d Q R X  jd|jd  jdt| |j|j  jdt|  jt |j|j|j W d Q R X  jt |jd W d Q R X  jt |jdd W d Q R X d S )Nrp   r9   re   r   rs  r   r   c                s0    j t|| d   j t|| dd   d S )Nr   r8   )r+   nextr   )seqiterator)r&   r   r   
CheckItems9  s    z-Proto3Test.testPython2Map.<locals>.CheckItemsi  r   r8   r   )rR  )rp   )rq   rr   r   r8  r9  r+   r   r*   r,   r   rQ  ro  rV  r   rD  r  popitemrk  r   rc  
setdefaultupdater=   )r&   rC   Z	map_int32rK  ry  r   rR  r   )r&   r   testPython2Map-  sJ    



zProto3Test.testPython2Mapc             C   sf   t j }d|jd< d|jd< d|jd< d|jd< d|jd< t|jj }t|jj }| j|| d S )Nrt   local_init_optrainable_variables	variablesinit_op	summaries)r   r8  r?  r   rQ  r+   )r&   rC   Zitems1Zitems2r   r   r   testMapItems]  s    




zProto3Test.testMapItemsc             C   s~   d}t j }d|jd< d|jd< d|jd< d|jd	< d
|jd< d
|jd< d|jd< d|jd< d|jd< | j||jdd d S )Ns   r
init_opdr

item1er

item2fr

item3gr
item4QQr
local_init_opar
	summarieser
trainable_variablesbr
	variablescr   r~  r   r  r   r  rb  r  er  Zitem1fZitem2gZitem3ZQQZitem4T)rF   )r   r8  r?  r+   r,   )r&   r/   rC   r   r   r   !testMapDeterministicSerializationl  s    	








z,Proto3Test.testMapDeterministicSerializationc             C   sR   t j }d|jd< d|jd< d|jd< t|jj }~dddd}| j|| d S )Nr9   re   r   rp   rs  )re   rp   r9   )r   r8  r9  r   rQ  r5  )r&   rC   itrt  r   r   r   testMapIterationClearMessage  s    


z'Proto3Test.testMapIterationClearMessagec             C   sf   t jdddd}| jd|jd  | jd|jd  t jdtjddid	}| jd|jd j d S )
Nre   r9   )r8   rp   )r9  r8   rp   r   )r   )rE  )r   r8  r+   r9  r	   r0  rE  r   )r&   rC   r   r   r   testMapConstruction  s    zProto3Test.testMapConstructionc             C   sd   t j }|j}d|d< d|d< d|d< |jd | jd|j  dddd}| jt|j | d S )	Nr9   re   r   rp   rs  r9  r   )re   rp   r9   )	r   r8  r9  r   r+   r,   r5  r   rQ  )r&   rC   Z	int32_maprt  r   r   r   testMapValidAfterFieldCleared  s    
z(Proto3Test.testMapValidAfterFieldClearedc             C   sL   t j }|j}d|d _|jd | jd|j  | jdt|j	 k d S )Nr   re   rE  r   )
r   r8  rE  r   r   r+   r,   rW   r   rV  )r&   rC   Zint32_foreign_messager   r   r   $testMessageMapValidAfterFieldCleared  s    

z/Proto3Test.testMessageMapValidAfterFieldClearedc             C   s|   t j }t|j}|jd | jt x|D ]}q.W W d Q R X t|j}|jd | jt x|D ]}qfW W d Q R X d S )Nr9  rE  )r   r8  ro  r9  r   r   rp  rE  )r&   rC   r  r   r   r   r   "testMapIterInvalidatedByClearField  s    





z-Proto3Test.testMapIterInvalidatedByClearFieldc             C   s   t j }| jdt|j d|jd< | jdt|j | jt |jd= W d Q R X |jd= | jdt|j | jt |jd= W d Q R X d S )Nr   r   r9   r8   ra      )r   r8  r+   r   r9  r   rc  rU  )r&   rC   r   r   r   testMapDelete  s    
zProto3Test.testMapDeletec             C   sL   t j }| j|jtj | j|jtj | j|jtj | j|jtj d S )N)r   r8  r   r9  r   MappingMutableMappingrE  )r&   rC   r   r   r   testMapsAreMapping  s    zProto3Test.testMapsAreMappingc             C   sD   t j }d|jd< | j|j|j | j|j|j | j|jd d S )Ni  r  r   i8i)r   r8  r9  r+   rE  ZassertNotEqual)r&   rC   r   r   r   testMapsCompare  s    
zProto3Test.testMapsComparec             C   s@   t j }d|jd< d|jd< d|jd _| jdt|j  d S )NrI  r   @   #   r   r   r   )	r   r8  r?  r9  Zmap_string_foreign_messager   r+   r   rJ  )r&   rC   r   r   r   (testMapFindInitializationErrorsSmokeTest  s
    

z3Proto3Test.testMapFindInitializationErrorsSmokeTestN)#rG   rH   rI   r5  r  r6  rC  rG  rL  rN  rS  rT  rW  rZ  rd  rf  rg  rh  rl  rn  rq  rr  ru  r}  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r2  (  s@   
90=)<0	r2  c               @   s   e Zd Zdd Zdd ZdS )ValidTypeNamesTestc                s   t t|jdd  d| d| f}| jt fdd|D d |f   jd}|d
 }dj|d d }t||gd	 d S )N'r8   zRepeated%sContainerzRepeated%sFieldContainerc             3   s   | ]} j |V  qd S )N)endswith)rx   r4  )tp_namer   r   r     s    z:ValidTypeNamesTest.assertImportFromName.<locals>.<genexpr>z%r does end with any of %r.)fromlistr<   r<   )r$   typesplitrW   anyjoin
__import__)r&   rC   Z	base_nameZvalid_namesparts
class_nameZmodule_namer   )r  r   assertImportFromName  s    

z'ValidTypeNamesTest.assertImportFromNamec             C   s(   t j }| j|jd | j|jd d S )NZScalarZ	Composite)r	   r!   r  r   r{   )r&   r   r   r   r   testTypeNamesCanBeImported
  s    z-ValidTypeNamesTest.testTypeNamesCanBeImportedN)rG   rH   rI   r  r  r   r   r   r   r    s   r  c               @   s$   e Zd Zdd Zdd Zdd ZdS )PackedFieldTestc             C   s   |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 |jjd |jjd |jjd |jjd d S )Nr8   g      ?T)r   r   Zrepeated_int64Zrepeated_uint32Zrepeated_uint64Zrepeated_sint32Zrepeated_sint64Zrepeated_fixed32Zrepeated_fixed64Zrepeated_sfixed32Zrepeated_sfixed64rZ   r[   Zrepeated_boolr   )r&   r   r   r   r   
setMessage  s    zPackedFieldTest.setMessagec             C   s*   t j }| j| d}| j||j  d S )NsH   
"*2:   B       J   R       Z  ?b      ?jr)r   r4   r  r+   r,   )r&   r   r/   r   r   r   testPackedFields"  s    
z PackedFieldTest.testPackedFieldsc             C   s*   t j }| j| d}| j||j  d S )Ns:    (0=   A       M   Q       ]  ?a      ?hp)r   ZTestUnpackedTypesr  r+   r,   )r&   r   r/   r   r   r   testUnpackedFields5  s    
z"PackedFieldTest.testUnpackedFieldsN)rG   rH   rI   r  r  r  r   r   r   r   r    s   r  r   re   rm  z;explicit tests of the C++ implementation for PY27 and abovec               @   s0   e Zd Zedd Zdd Zdd Zdd Zd	S )
OversizeProtosTestc             C   sF   d}t j }tj }tj|| |j| tj|j	|j
d| _d S )Na  
      name: "f/f.msg2"
      package: "f"
      message_type {
        name: "msg1"
        field {
          name: "payload"
          number: 1
          label: LABEL_OPTIONAL
          type: TYPE_STRING
        }
      }
      message_type {
        name: "msg2"
        field {
          name: "field"
          number: 1
          label: LABEL_OPTIONAL
          type: TYPE_MESSAGE
          type_name: "msg1"
        }
      }
    zf.msg2)r   ZDescriptorPoolr   ZFileDescriptorProtor   ZParseZAddr   ZMessageFactoryZGetPrototypeZFindMessageTypeByName	proto_cls)clsZ	file_descZpoolZdescr   r   r   
setUpClassN  s    

zOversizeProtosTest.setUpClassc             C   s(   | j  | _dd | jj_| jj | _d S )Nr   i   r  r8   i   i   i  )r  pfieldr   r,   p_serialized)r&   r   r   r   setUpq  s    
zOversizeProtosTest.setUpc             C   sf   ddl m} |d | j }y|j| j W n4 tjk
r` } z| jt|d W Y d d }~X nX d S )Nr   )SetAllowOversizeProtosFzError parsing message)	google.protobuf.pyext._messager  r  r*   r  r   r?   r+   r$   )r&   r  qr  r   r   r   testAssertOversizeProtov  s    z*OversizeProtosTest.testAssertOversizeProtoc             C   sB   ddl m} |d | j }|j| j | j| jjj|jj d S )Nr   )r  T)	r  r  r  r*   r  r+   r  r  r   )r&   r  r  r   r   r   testSucceedOversizeProto  s
    z+OversizeProtosTest.testSucceedOversizeProtoN)rG   rH   rI   classmethodr  r  r  r  r   r   r   r   r  I  s   #	r  __main__)re   rm  )4r   
__author__r   r-   rg   r   rQ   r   rq   r@   Z	unittest2ZunittestImportErrorr   r   Zgoogle.protobufr   r   r	   r
   r   r   r   r   Zgoogle.protobuf.internalr   r   r   r   r   r   r   ZPY3r   Zlongr   r   r   r   ZBaseTestCaseZNamedParametersr   r  r2  r  r  ZskipIfr   rr   r  rG   mainr   r   r   r   <module>)   s          W q     X9:
