U
    4Jeo                     @   s  d Z dZddlZddlZddlZddl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 ddlmZ ddlmZ ddlmZ ddlmZ ede G dd deZejG dd deejZejG dd deejZ ejG dd deej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%G d'd( d(eZ&G d)d* d*eZ'G d+d, d,eZ(ejG d-d. d.ejZ)e"d/d0e$e#d1d2ge$e#d3d4ge$d5e#d6d7gid8e%d9d5d:fd;e'd<d=fgd>d8e%d9d5d?fd;e'd<d@fdAe&dBdCfgdDd8e%d9d5dEfdFe&d<dGfgdHdIe$e#dJdKge$e#dLdMge$d5e#dNdOgid8e%d9d5dPfd;e'd<dQfgd>d8e%d9d5dRfd;e'd<dSfdAe&dBdCfgdDd8e%d9d5dTfdFe&d<dGfgdUZ*e"dVd0dWe$e#dXdYge$e#dZd[ge$d5e#d\d]gid8e%d9d5d^fd;e'd<d_fgd>d8e%d9d5d`fd;e'd<dafdAe&dBdCfgdDd8e%d9d5dbfdFe&d<dGfgdce(dddefgdfid/dggdggdhZ+e,dikre-  dS )jz*Tests for google.protobuf.descriptor_pool.z"matthewtoia@google.com (Matt Toia)    N)unittest_import_pb2)unittest_import_public_pb2)unittest_pb2)descriptor_pb2)api_implementation)descriptor_pool_test1_pb2)descriptor_pool_test2_pb2)factory_test1_pb2)factory_test2_pb2)file_options_test_pb2)more_messages_pb2)no_package_pb2)testing_refleaks)
descriptor)descriptor_database)descriptor_pool)message_factory)symbol_databaseerrorc                   @   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/S )0DescriptorPoolTestBasec                 C   s   d}| j |}| |tj | ||j | d|j | d|j	 d}| j |}| |tj | ||j | d|j | d|j	 d S )N,google/protobuf/internal/factory_test1.protogoogle.protobuf.python.internalFactory1Message,google/protobuf/internal/factory_test2.protoFactory2Message)
poolFindFileByNameassertIsInstancer   FileDescriptorassertEqualnamepackageassertInmessage_types_by_name)selfZname1
file_desc1Zname2
file_desc2 r'   Q/tmp/pip-unpacked-wheel-0oacnh65/google/protobuf/internal/descriptor_pool_test.pytestFindFileByNameB   s    z)DescriptorPoolTestBase.testFindFileByNamec              	   C   s&   |  t | jd W 5 Q R X d S NDoes not exist)assertRaisesKeyErrorr   r   r$   r'   r'   r(   testFindFileByNameFailureQ   s    z0DescriptorPoolTestBase.testFindFileByNameFailurec           	      C   s  | j d}| |tj | d|j | d|j | d|j	 | j d}| |tj | d|j | d|j | d|j	 | j d}| |tj | d|j | j d	}| |tj | d|j | j d
}| |tj | d|j t
 d	stt
 ds(tt
 d
s<t| j d}| |tj | d|j | j d}| |tj | d|j | j d}| |tj | d|j | t| j jd d S )N/google.protobuf.python.internal.Factory1Messager   r   r   /google.protobuf.python.internal.Factory2Messager   r   -google.protobuf.python.internal.another_field>google.protobuf.python.internal.Factory2Message.one_more_fieldprotobuf_unittest.TestServicegoogle/protobuf/unittest.proto:google.protobuf.python.internal.Factory1Message.list_valuez1google.protobuf.python.internal.FACTORY_1_VALUE_0z"protobuf_unittest.TestAllTypes.FOOz:google.protobuf.python.internal.Factory1Message.none_field)r   FindFileContainingSymbolr   r   r   r   r    r!   r"   r#   r   DefaultAssertionErrorr,   r-   )	r$   r%   r&   Z
file_desc3Z
file_desc4Z
file_desc5Z
file_desc6Z
file_desc7Z
file_desc8r'   r'   r(   testFindFileContainingSymbolU   s    


z3DescriptorPoolTestBase.testFindFileContainingSymbolc              	   C   s&   |  t | jd W 5 Q R X d S r*   )r,   r-   r   r7   r.   r'   r'   r(   #testFindFileContainingSymbolFailure   s    z:DescriptorPoolTestBase.testFindFileContainingSymbolFailurec           	      C   s  | j d}| |tj | d|j | d|j | d |j | 	|j
 |jd }| d|j | ||j |jd }| d|j | ||j | ||jd j | ||jd j | j d}| |tj | d	|j | d|j | |j |jd }| d
|j | ||j |jd }| d|j | ||j | ||jd j | ||jd j | |jd j | d|jd j | |jd j | d|jd j | |jd j | d|jd j | |jd j | 	|jd j | |jd j | d|jd j | j d}| || | |jd j | d|jd j | dt|j | dt|j | dt|jd j dD ]:}| |jd |j| j | |j| |jd j qd S )Nr0   r   r   ZNestedFactory1MessageZNestedFactory1EnumZnested_factory_1_messageZnested_factory_1_enumr1   r   ZNestedFactory2MessageZNestedFactory2EnumZnested_factory_2_messageZnested_factory_2_enumZint_with_defaulti  Zdouble_with_defaultg{G#@Zstring_with_defaultzhello worldZbool_with_defaultZenum_with_default   zEgoogle.protobuf.python.internal.Factory2Message.NestedFactory2MessageZbytes_with_defaults   a c   )Z	oneof_intZoneof_string)r   FindMessageTypeByNamer   r   Z
Descriptorr   r    	full_namecontaining_typeassertFalsehas_optionsZnested_types
enum_typesfields_by_namemessage_type	enum_typeZassertIsNone
assertTruehas_default_valuedefault_valuelenZoneofsoneofs_by_namefieldsZcontaining_oneofr"   )	r$   Zmsg1Znested_msg1nested_enum1Zmsg2Znested_msg2nested_enum2Zmsg3r    r'   r'   r(   testFindMessageTypeByName   s    







 

 

 
 


z0DescriptorPoolTestBase.testFindMessageTypeByNamec                 C   s   |  t| jjd |  t| jjd t dkr6t}nt}|  || jj	d |  || jj
d |  || jjd |  || jjd |  || jjd |  || jjd |  || jjd |  || jjd t dkrt}|  || jjd d S )N pythonr   )r,   	TypeErrorr   FindExtensionByNumberr-   FindMethodByNamer   TypeAttributeErrorr>   FindFieldByNameFindExtensionByNameFindEnumTypeByNameFindOneofByNameFindServiceByNamer7   r   )r$   Z
error_typer'   r'   r(   testFindTypeErrors   s     z)DescriptorPoolTestBase.testFindTypeErrorsc              	   C   s&   |  t | jd W 5 Q R X d S r*   )r,   r-   r   r>   r.   r'   r'   r(    testFindMessageTypeByNameFailure  s    z7DescriptorPoolTestBase.testFindMessageTypeByNameFailurec                 C   s  | j d}| |tj | d|jd j | d|jd j | |j	 | j d}| |tj | d|jd j | d|jd j | j d	}| |tj | d|jd
 j | d|jd j | j d}| |tj | d|jd j | d|jd j d S )Nz,google.protobuf.python.internal.Factory1Enumr   ZFACTORY_1_VALUE_0r<   ZFACTORY_1_VALUE_1zBgoogle.protobuf.python.internal.Factory1Message.NestedFactory1EnumZNESTED_FACTORY_1_VALUE_0ZNESTED_FACTORY_1_VALUE_1z,google.protobuf.python.internal.Factory2EnumZFACTORY_2_VALUE_0ZFACTORY_2_VALUE_1zBgoogle.protobuf.python.internal.Factory2Message.NestedFactory2EnumZNESTED_FACTORY_2_VALUE_0ZNESTED_FACTORY_2_VALUE_1)
r   rY   r   r   ZEnumDescriptorr   values_by_namenumberrA   rB   )r$   Zenum1rM   Zenum2rN   r'   r'   r(   testFindEnumTypeByName
  sJ     
 
 
 
z-DescriptorPoolTestBase.testFindEnumTypeByNamec              	   C   s&   |  t | jd W 5 Q R X d S r*   )r,   r-   r   rY   r.   r'   r'   r(   testFindEnumTypeByNameFailure(  s    z4DescriptorPoolTestBase.testFindEnumTypeByNameFailurec              	   C   s\   | j d}| |jd | |j|j | |j | t	 | j d W 5 Q R X d S )Nr6   Z
list_valuer+   )
r   rW   r   r    labelZLABEL_REPEATEDrA   rB   r,   r-   )r$   fieldr'   r'   r(   testFindFieldByName,  s    z*DescriptorPoolTestBase.testFindFieldByNamec              	   C   s@   | j d}| |jd | t | j d W 5 Q R X d S )Nz;google.protobuf.python.internal.Factory2Message.oneof_fieldoneof_fieldr+   )r   rZ   r   r    r,   r-   )r$   oneofr'   r'   r(   testFindOneofByName6  s    z*DescriptorPoolTestBase.testFindOneofByNamec              	   C   sh   | j d}| |jd | j d}| |jd | |jd | t | j d W 5 Q R X d S )Nr3   one_more_fieldr2   another_field  r+   )r   rX   r   r    r_   r,   r-   rW   )r$   	extensionr'   r'   r(   testFindExtensionByName=  s    z.DescriptorPoolTestBase.testFindExtensionByNamec                 C   s   | j d}| j d}|jd }| j d}|jd }| j |}t||g}| |t| |d | j |}| |t| d S )Nr0   r1   rh   r   ri   Zunexpected_element)r   r>   extensions_by_namer   ZFindAllExtensionssetr   append)r$   factory1_messageZfactory2_messagerh   Zfactory_test2ri   
extensionsZexpected_extension_numbersr'   r'   r(   testFindAllExtensionsJ  s"    


z,DescriptorPoolTestBase.testFindAllExtensionsc              	   C   sx   | j d}| j d | j |d}| |jd | j |d}| |jd | t | j |d}W 5 Q R X d S )Nr0   r     rh   rj   ri   i )r   r>   r   rS   r   r    r,   r-   )r$   rp   rk   r'   r'   r(   testFindExtensionByNumber`  s    z0DescriptorPoolTestBase.testFindExtensionByNumberc              	   C   sj   |  t | jd W 5 Q R X |  t | jd W 5 Q R X |  t | jd W 5 Q R X d S )Nr2   r3   r6   )r,   r-   r   rW   rX   r.   r'   r'   r(   testExtensionsAreNotFieldsp  s    z1DescriptorPoolTestBase.testExtensionsAreNotFieldsc              	   C   s|   | j d}| |jd | t | j d W 5 Q R X | j d}| |j| | t | j d W 5 Q R X d S )Nr4   r+   z!protobuf_unittest.TestService.Fooz*protobuf_unittest.TestService.Doesnotexist)	r   r[   r   r?   r,   r-   rT   assertIsZcontaining_service)r$   servicemethodr'   r'   r(   testFindServicez  s    z&DescriptorPoolTestBase.testFindServicec                 C   s8   t  }t|| _|| j || j |   d S N)	r   DescriptorDatabaser   DescriptorPoolr   Addfactory_test1_fdfactory_test2_fdrO   )r$   dbr'   r'   r(   testUserDefinedDB  s
    z(DescriptorPoolTestBase.testUserDefinedDBc                 C   s   t | trt dkrd S t | _| j| j	 }| j| j
	 }| |jd | |jd |   | jtjj}|jd jd }| |jd d S )NrQ   r   r   classZ	int_fieldZjson_int)
isinstance#SecondaryDescriptorFromDescriptorDBr   rU   r   r|   r   AddSerializedFiler~   SerializeToStringr   r   r    rO   r   
DESCRIPTORserialized_pbr#   rD   Z	json_name)r$   file1file2Z	file_jsonrc   r'   r'   r(   testAddSerializedFile  s,    

z,DescriptorPoolTestBase.testAddSerializedFilec                    s    fdd}|t j t jd} |t j || t trVt	
 dkrVdS tjt jj} j|  jd}|| dS )z:Test the default value of enums which don't start at zero.c                    s&   | j d jd j} |tjj d S )NDescriptorPoolTest1nested_enum)r#   rD   rI   r   r   r   BETA)file_descriptorrI   r.   r'   r(   _CheckDefaultValue  s    zGDescriptorPoolTestBase.testEnumDefaultValue.<locals>._CheckDefaultValue4google/protobuf/internal/descriptor_pool_test1.protorQ   N)r   r   r   r8   r   r   rv   r   r   r   rU   r   FileDescriptorProto
FromStringr   r}   )r$   r   r   Zdescriptor_protor'   r.   r(   testEnumDefaultValue  s$    


z+DescriptorPoolTestBase.testEnumDefaultValuec                    s   fdd  fdd}|t   t }|tjtj	j
 |tjtj	j
 |tjt j	j
 t||t jj	j}||  dS )z0Check the value returned by non-existent fields.c                    s     | |  | | d S rz   )r   r   )valueZexpected_valueZexpected_typer.   r'   r(   _CheckValueAndType  s    zTDescriptorPoolTestBase.testDefaultValueForCustomMessages.<locals>._CheckValueAndTypec                    s   zt }W n tk
r    t}Y nX zt}W n tk
rB   t}Y nX  | jdt  | jd|tf  | jdttf  | j	dttf  | j
dt  | jd|  | jdt  | j| jt d S )Nr   FrP       )long	NameErrorintunicodestroptional_int32Zoptional_uint64Zoptional_floatfloatZoptional_doubleZoptional_boolboolZoptional_stringZoptional_bytesbytesZoptional_nested_enumZFOO)msgZint64Zunicode_type)r   r'   r(   _CheckDefaultValues  s     

zUDescriptorPoolTestBase.testDefaultValueForCustomMessages.<locals>._CheckDefaultValuesN)r   TestAllTypesr   r|   r}   r   r   r   r   r   r   r   r   ZMessageFactoryZGetPrototyper>   r?   )r$   r   r   Zmessage_classr'   )r   r$   r(   !testDefaultValueForCustomMessages  s&    



z8DescriptorPoolTestBase.testDefaultValueForCustomMessagesc                 C   sF   t | trt dkrd S tjdd}| j| | j|	  d S )NrQ   some/file.protor    )
r   r   r   rU   r   r   r   r}   r   r   )r$   	file_descr'   r'   r(   testAddFileDescriptor  s    
z,DescriptorPoolTestBase.testAddFileDescriptorc                 C   s   t | trt dkrd S tjtjj	}tjt
jj	}tjtjj	}| j| | j| | j| t| | j t| | j d S )NrQ   )r   r   r   rU   r   r   r   r   r   r   r   r   r   r}   
TEST1_FILE	CheckFile
TEST2_FILE)r$   Zmore_messages_descZ
test1_descZ
test2_descr'   r'   r(   testComplexNesting  s"    
z)DescriptorPoolTestBase.testComplexNestingc              	   C   s8  t | trt dkrd S tjtjj	}t
|}d|_t dkrHnt
| j}tj}||jd  ||jd  ||jd  ||jd  || tjddP}td	 || | t| | |d
 jt | dt |d
 j! W 5 Q R X |"|j | #t$ |"|j W 5 Q R X d S )NrQ   Z
other_filer   ForeignEnumTestServiceoptional_int32_extensionT)recordalwaysr   z)Conflict register for file "other_file": )%r   r   r   rU   r   r   r   r   r   r   copydeepcopyr    r   _AddDescriptorr#   _AddEnumDescriptorenum_types_by_name_AddServiceDescriptorservices_by_name_AddExtensionDescriptorrm   r}   warningscatch_warningssimplefilterrG   rJ   rv   categoryRuntimeWarningr"   r   messager   r,   rR   )r$   Zunittest_fdZconflict_fdr   r   wr'   r'   r(   testConflictRegister  sH    




z+DescriptorPoolTestBase.testConflictRegisterN)__name__
__module____qualname__r)   r/   r:   r;   rO   r\   r]   r`   ra   rd   rg   rl   rr   rt   ru   ry   r   r   r   r   r   r   r   r'   r'   r'   r(   r   @   s.   FR

 &r   c                   @   s   e Zd Zdd Zdd ZdS )DefaultDescriptorPoolTestc                 C   s2   t  | _tjtjj| _	tjt
jj| _d S rz   )r   r8   r   r   r   r   r	   r   r   r~   r
   r   r.   r'   r'   r(   setUp/  s    
zDefaultDescriptorPoolTest.setUpc                 C   s   |  | jdtj |  | jdtjj |  | jdtjjjd  |  | j	dtj
j |  | jdtjjd  |  | jdtjjjd	  |  | jd
tjjd  d S )Nr5   protobuf_unittest.TestAllTypesz-protobuf_unittest.TestAllTypes.optional_int32r   protobuf_unittest.ForeignEnumz*protobuf_unittest.optional_int32_extensionr   z*protobuf_unittest.TestAllTypes.oneof_fieldre   r4   r   )rv   r   r   r   r   r>   r   rW   rD   rY   r   rX   rm   rZ   rK   r[   r   r.   r'   r'   r(   testFindMethods6  s@    






z)DefaultDescriptorPoolTest.testFindMethodsN)r   r   r   r   r   r'   r'   r'   r(   r   ,  s   r   c                   @   s   e Zd Zdd ZdS )CreateDescriptorPoolTestc                 C   s   t  | _tjtjj| _	tjt
jj| _| j| j	 | j| j | jtjtjj | jtjtjj | jtjtjj | jtjtjj d S rz   )r   r|   r   r   r   r   r	   r   r   r~   r
   r   r}   r   r   r   r   r.   r'   r'   r(   r   S  s*    
zCreateDescriptorPoolTest.setUpN)r   r   r   r   r'   r'   r'   r(   r   P  s   r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   t jtjj| _t jtjj| _t	
 | _| j| j | j| j | jt jtjj | jt jtjj | jt jtjj | jt jtjj tj| jd| _d S )N)Zdescriptor_db)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   j  s,    
z)SecondaryDescriptorFromDescriptorDB.setUpc           	   	   C   s   t  }d|_d|_|j }d|_|j }d|_d|_tj	j
|_tj	j|_d|_|j }d|_|j }d|_|j }d	|_d
|_| j| | td| jjd | td| jjd | t}| jd W 5 Q R X | t|jd t dkrd}| t}| jd W 5 Q R X | t|jd|  | t}| j d W 5 Q R X | t|jd|  | t}| j!d W 5 Q R X | t|jd|  | t}| j"d W 5 Q R X | t|jd|  | t}| j#d W 5 Q R X | t|jd|  d S )N	collectorZ
error_fileZErrorMessager<   Znested_message_fieldZ
SubMessageZMyOneofZMyEnumZMyEnumValuer   zcollector.ErrorMessageZ	none_file)z'none_file'z"Couldn't find file none_file"rQ   zInvalid proto descriptor for file "error_file":\n  collector.ErrorMessage.nested_message_field: "SubMessage" is not defined.\n  collector.ErrorMessage.MyOneof: Oneof must have at least one field.\n'z:'Couldn\'t build file for message collector.ErrorMessage\nz+collector.ErrorMessage.nested_message_fieldzM'Couldn\'t build file for field collector.ErrorMessage.nested_message_field\nzcollector.MyEnumz1'Couldn\'t build file for enum collector.MyEnum\nzcollector.MyEnumValuez8'Couldn\'t build file for symbol collector.MyEnumValue\nzcollector.ErrorMessage.MyOneofz@'Couldn\'t build file for oneof collector.ErrorMessage.MyOneof\n)$r   r   r!   r    rE   addrc   r_   r   FieldDescriptorLABEL_OPTIONALrb   TYPE_MESSAGEtype	type_nameZ
oneof_declrF   r   r   r}   assertRaisesRegexr-   r   r>   r   r,   r"   r   	exceptionr   rU   r   rW   rY   r7   rZ   )	r$   
file_protorE   rc   rf   rF   Z
enum_valueexc	error_msgr'   r'   r(   testErrorCollector|  sp    






	z6SecondaryDescriptorFromDescriptorDB.testErrorCollectorN)r   r   r   r   r   r'   r'   r'   r(   r   f  s   r   c                   @   s   e Zd ZdddZdd ZdS )	ProtoFileNc                 C   s*   || _ || _|| _|pg | _|p"g | _d S rz   )r    r!   messagesdependenciespublic_dependencies)r$   r    r!   r   r   r   r'   r'   r(   __init__  s
    
zProtoFile.__init__c                 C   s   | | j}|| j|j || j|j dd |jD }|| j| dd |jD }|| j| | j D ]\}}||d || qrd S )Nc                 S   s   g | ]
}|j qS r'   r   .0fr'   r'   r(   
<listcomp>  s     z'ProtoFile.CheckFile.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r'   r   r   r'   r'   r(   r     s     )	r   r    r   r!   r   r   r   items	CheckType)r$   testr   r   Zdependencies_namesZpublic_dependencies_namesr    msg_typer'   r'   r(   r     s    zProtoFile.CheckFile)NN)r   r   r   r   r   r'   r'   r'   r(   r     s     
r   c                   @   s   e Zd Zdd Zdd ZdS )EnumTypec                 C   s
   || _ d S rz   )values)r$   r   r'   r'   r(   r     s    zEnumType.__init__c                 C   s   |j | }|||j d|j|g}|||j |||j |||j t| jD ]\\}\}}	|j	| }
|||
j |||
j
 ||	|
j |||
j |||j q\d S N.)r   r   r    joinr?   r@   file	enumerater   r^   indexr_   r   r"   Zenum_values_by_name)r$   r   msg_descr    r   	enum_descZexpected_enum_full_namer   r   r_   Z
value_descr'   r'   r(   r     s    

zEnumType.CheckTypeNr   r   r   r   r   r'   r'   r'   r(   r     s   r   c                   @   s   e Zd ZdddZdd ZdS )MessageTypeFNc                 C   s    || _ || _|| _|pg | _d S rz   )	type_dict
field_listis_extendablerq   )r$   r   r   r   rq   r'   r'   r(   r     s    zMessageType.__init__c           
      C   s  |d kr$|j | }d|j|g}n|j| }d|j|g}|||j |||j |||j ||j| || j	|j	 | j
 D ]\}}||||| qt| jD ]\}\}}	|	||||| qt| jD ]\}\}}	|	||||| qd S r   )r#   r   r!   nested_types_by_namer?   r   r    r@   r   r   r   r   r   r   r   
CheckFieldrq   )
r$   r   Zcontaining_type_descr    r   descZexpected_full_namesubtyper   rc   r'   r'   r(   r     s     

zMessageType.CheckType)FNr   r'   r'   r'   r(   r     s     
r   c                   @   s   e Zd Zdd Zdd ZdS )	EnumFieldc                 C   s   || _ || _|| _d S rz   )r_   r   rI   )r$   r_   r   rI   r'   r'   r(   r     s    zEnumField.__init__c           	      C   s   |j | }|j| j }|||j d|j|g}|||j |||j || j|j |t	j
j|j |t	j
j|j ||j ||j| j j|j ||j| j j |||j |||j |||j d S r   )rD   r   r   r   r    r   r?   r   r_   r   r   Z	TYPE_ENUMr   ZCPPTYPE_ENUMcpp_typerG   rH   r^   rI   rA   rB   r@   rF   r   )	r$   r   r   r    r   r   
field_descr   expected_field_full_namer'   r'   r(   r     s&    

zEnumField.CheckFieldNr   r   r   r   r   r'   r'   r'   r(   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )MessageFieldc                 C   s   || _ || _d S rz   )r_   r   )r$   r_   r   r'   r'   r(   r   )  s    zMessageField.__init__c           	      C   s   |j | }|j| j }|||j d|j|g}|||j |||j || j|j |t	j
j|j |t	j
j|j ||j |||j |||j |||j ||jd  d S r   )rD   r   r   r   r    r   r?   r   r_   r   r   r   r   CPPTYPE_MESSAGEr   rA   rH   r@   rE   r   rI   )	r$   r   r   r    r   r   r   Zfield_type_descr   r'   r'   r(   r   -  s     

zMessageField.CheckFieldNr   r'   r'   r'   r(   r   '  s   r   c                   @   s   e Zd Zdd Zdd ZdS )StringFieldc                 C   s   || _ || _d S rz   )r_   rI   )r$   r_   rI   r'   r'   r(   r   A  s    zStringField.__init__c                 C   s   |j | }|||j d|j|g}|||j |||j || j|j |tjj	|j
 |tjj|j ||j || j|j |||j d S r   )rD   r   r    r   r?   r   r_   r   r   ZTYPE_STRINGr   ZCPPTYPE_STRINGr   rG   rH   rI   r   r$   r   r   r    r   r   r   r   r'   r'   r(   r   E  s    

zStringField.CheckFieldNr   r'   r'   r'   r(   r   ?  s   r   c                   @   s   e Zd Zdd Zdd ZdS )ExtensionFieldc                 C   s   || _ || _d S rz   )r_   extended_type)r$   r_   r  r'   r'   r(   r   V  s    zExtensionField.__init__c                 C   s   |j | }|||j d|j|g}|||j || j|j |||j |tjj	|j
 |tjj|j ||j ||j |||j |||j || j|jj |||j d S r   )rm   r   r    r   r?   r_   r   r   r   r   r   r   r   rA   rH   rG   Zis_extensionZextension_scoperE   r  r@   r   r   r'   r'   r(   r   Z  s     

zExtensionField.CheckFieldNr   r'   r'   r'   r(   r  T  s   r  c                   @   s   e Zd Zdd Zee dkddd Zdd Z	ee dkdd	d
 Z
ee dkddd Zee dkddd Zdd Zdd Zdd Zdd ZdS )AddDescriptorTestc              	   C   s   t  }|tjj | d||d j | 	t
 ||d  W 5 Q R X |tjjj | d||d j | d|dj | d||d j d S )Nr   z,protobuf_unittest.TestAllTypes.NestedMessager5   )r   r|   r   r   r   r   r   r>   r?   r,   r-   NestedMessager   r    r7   r$   prefixr   r'   r'   r(   _TestMessageo  s>    zAddDescriptorTest._TestMessagerQ   zOnly pure python allows _Add*()c                 C   s   |  d |  d d S NrP   r   )r  r.   r'   r'   r(   testMessage  s    
zAddDescriptorTest.testMessagec              	   C   s   t  }t dkr$|tjj n|tjj | 	d|
|d j | t |
|d  W 5 Q R X t dkr|tjjj n|tjjj | 	d|
|d j | 	d|dj | 	d||d j d S )Ncppr   z(protobuf_unittest.ForeignEnum.NestedEnumz)protobuf_unittest.TestAllTypes.NestedEnumr5   )r   r|   r   rU   AddEnumDescriptorr   r   r   r   r   rY   r?   r,   r-   r   
NestedEnumr   r    r7   r  r'   r'   r(   	_TestEnum  sF    zAddDescriptorTest._TestEnumc                 C   s   |  d |  d d S r  )r  r.   r'   r'   r(   testEnum  s    
zAddDescriptorTest.testEnumc              	   C   sL   t  }| t |d W 5 Q R X |tj | d|dj	 d S )Nr4   )
r   r|   r,   r-   r[   r   r   Z_TESTSERVICEr   r?   r$   r   r'   r'   r(   testService  s    
zAddDescriptorTest.testServicec              	   C   sL   t  }|tj | d|dj | t	 |
d W 5 Q R X d S )Nr5   r   )r   r|   _AddFileDescriptorr   r   r   r   r    r,   r-   r7   r  r'   r'   r(   testFile  s    zAddDescriptorTest.testFilec                 C   sR   t  }tjj}| t|j| t }tj	| |
| | || d S rz   )r   r|   r   r   r    r,   r-   r   r   ZCopyToProtor}   rG   )r$   r   Zproto_file_namer   r'   r'   r(   testEmptyDescriptorPool  s    
z)AddDescriptorTest.testEmptyDescriptorPoolc                 C   s
  t  }tjddd}|jjdd || | |dj	d | |
dj	d tjdd}|jjdd}|jjd	d
tjjtjjd |jjdd}|jjddd |jjdd t  }|| | d|
dj	 | d|dj	 | d|dj	 d S )Nr   r!   )r    r!   Messager   zpackage.Messagezsome/filename/container.protoZ
TopMessageZbbr<   )r    r_   r   rb   ZTopEnumZFOREIGN_FOO   )r    r_   Z
TopService)r   r|   r   r   rE   r   r}   r   r   r    r>   rc   ZFieldDescriptorProtoZ
TYPE_INT32r   rF   r   rw   rY   r[   )r$   r   r   r   Zmessage_protoZ
enum_protor'   r'   r(   testCustomDescriptorPool  sF     


z*AddDescriptorTest.testCustomDescriptorPoolc                 C   sp   t  }d}tj|d}tj}d|jj| _|	| |
|}| }| d|j| j | ||  d S )Nz9file_descriptor_options_with_custom_descriptor_pool.protor   Zfoo)r   r|   r   r   r   Zfoo_optionsoptionsZ
ExtensionsZfoo_namer}   r   Z
GetOptionsr   rv   )r$   r   	file_nameZfile_descriptor_protoZextension_idr   r  r'   r'   r(   1testFileDescriptorOptionsWithCustomDescriptorPool  s    

zCAddDescriptorTest.testFileDescriptorOptionsWithCustomDescriptorPoolc              	   C   sZ  t  }t dkr| t |d W 5 Q R X | t |d W 5 Q R X | t |d W 5 Q R X | t |	d W 5 Q R X | t |
d W 5 Q R X n| t |d W 5 Q R X | t |d W 5 Q R X | t |d W 5 Q R X | t |d W 5 Q R X | t |d W 5 Q R X d S )NrQ   r   )r   r|   r   rU   r,   rR   ZAddDescriptorr  ZAddServiceDescriptorZAddExtensionDescriptorZAddFileDescriptorr   r   r   r   r  r  r'   r'   r(   testAddTypeError  s,    z"AddDescriptorTest.testAddTypeErrorN)r   r   r   r  unittestZskipIfr   rU   r	  r  r  r  r  r  r  r  r  r'   r'   r'   r(   r  l  s,   
$

	
r  r   r   )ZALPHAr<   )r   r=   )ZEPSILON   )ZETA   r  )ETA   )ZTHETA   r   r<   r  Znested_fieldr=   theta)r  DeepNestedMessager  betaZdeep_nested_message   r#  )r  r  r   Znested_messager  T)r   )GAMMAr%  )ZDELTAr  )IOTA	   )ZKAPPA
   )ZLAMBDA   )MU   r+  lambdar'  deltar&  )r   ZDescriptorPoolTest2z4google/protobuf/internal/descriptor_pool_test2.protoZDescriptorPoolTest3)ZNU   )XI   )ZOMICRON   )PI   )RHO   )ZSIGMA   r5  sigmar3  nur0  Zdescriptor_pool_testrs   r   )rq   z,google/protobuf/internal/more_messages.proto)r   r   __main__).__doc__
__author__r   osr  r   Zgoogle.protobufr   r   r   r   Zgoogle.protobuf.internalr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   DeprecationWarningobjectr   ZTestCaser   r   r   r   r   r   r   r   r   r  r  r   r   r   mainr'   r'   r'   r(   <module>   s     o#Z  B

 
	

 
	. 

 
	 
