B
    u9a}N                 @   s  d dl m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
rh   dZ	Y nX G dd dZG dd dejeZG dd dejeZG d	d
 d
ejeZG dd dejZG dd dejZG dd dejeZG dd dejeZG dd dejZdZejdkr&dndZG dd dZeeejkdG dd dejZdd ZG d d! d!ejeZG d"d# d#ejZG d$d% d%ejeZeje e	d&G d'd( d(ejeZ!e"d)kre#  dS )*    )supportNc               @   s   e Zd Zdd ZdS )HelperMixinc          	   G   s   t t j|f| }| || z\ttjd}t j||f|  W d Q R X ttjd}t |}W d Q R X | || W d t	tj X d S )Nwbrb)
marshalloadsdumpsassertEqualopenr   TESTFNdumploadunlink)selfsampleZextranewf r   "/usr/lib/python3.7/test_marshal.pyhelper   s    zHelperMixin.helperN)__name__
__module____qualname__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 )IntTestCasec             C   s>   t jd }x.|r8x| |fD ]}| | qW |d? }qW d S )N      )sysmaxsizer   )r   nexpectedr   r   r   	test_ints   s
    
zIntTestCase.test_intsc             C   s   d}| d }xh||| |d?  fD ]P}xJ|rrdt j|dddd }t|}| || |dkrhd	}q*|dL }q*W q$W td
}| |d td}| |d td}| |d td}| |d d S )Nl    r      I   littleT)signedr   s	   IܺvT2l   \u1Q s	   I#Egl\u1Q s	   I*;L]nl   Tv0ur{ s	   Iĳl	Tv0ur{ )intto_bytesr   r   r	   )r   Zmaxint64Zminint64basesgotr   r   r   
test_int64%   s$    





zIntTestCase.test_int64c             C   s   xdD ]}|  | qW d S )N)TF)r   )r   br   r   r   	test_bool<   s    
zIntTestCase.test_boolN)r   r   r   r    r+   r-   r   r   r   r   r      s   r   c               @   s   e Zd Zdd ZdS )FloatTestCasec             C   s   d}t jd }x6||krDx | |fD ]}| t| q$W |d }qW d}t|d}t|}| || t|d}t|}| || t jd }xF||k rx0| |fD ]"}t|}| | | |d qW |d9 }qW d S )Ng}:g6ҫsgS:^@g        r   r   gZ1%)r   r   r   floatr   r   r   r	   )r   Zsmallr   r   r   r)   r*   r   r   r   test_floatsA   s(    






zFloatTestCase.test_floatsN)r   r   r   r0   r   r   r   r   r.   @   s   r.   c               @   s$   e Zd Zdd Zdd Zdd ZdS )StringTestCasec             C   s4   x.ddddd gD ]}|  tt| qW d S )N u   Andrè Previnabc i'  )r   r   r   r   )r   r)   r   r   r   test_unicode\   s    zStringTestCase.test_unicodec             C   s(   x"ddddd gD ]}|  | qW d S )Nr2   u   Andrè Previnr3   r4   i'  )r   )r   r)   r   r   r   test_string`   s    zStringTestCase.test_stringc             C   s(   x"ddddd gD ]}|  | qW d S )N    s   Andr Previns   abc    i'  )r   )r   r)   r   r   r   
test_bytesd   s    zStringTestCase.test_bytesN)r   r   r   r5   r6   r9   r   r   r   r   r1   [   s   r1   c               @   s   e Zd Zdd ZdS )ExceptionTestCasec             C   s    t t t}| t| d S )N)r   r   r   StopIterationr	   )r   r   r   r   r   test_exceptionsi   s    z!ExceptionTestCase.test_exceptionsN)r   r   r   r<   r   r   r   r   r:   h   s   r:   c               @   s2   e Zd Zdd Zdd Zdd Zejdd Zd	S )
CodeTestCasec             C   s(   t jj}tt|}| || d S )N)r:   r<   __code__r   r   r   r	   )r   cor   r   r   r   	test_coden   s    zCodeTestCase.test_codec             C   s&   d}t jjf| }tt| d S )Ni  )r:   r<   r>   r   r   r   )r   countZcodesr   r   r   test_many_codeobjectss   s    z"CodeTestCase.test_many_codeobjectsc             C   sP   t ddd}t ddd}tt||f\}}| |jd | |jd d S )Nxf1execyf2)compiler   r   r   r	   co_filename)r   Zco1Zco2r   r   r   test_different_filenamesy   s
    z%CodeTestCase.test_different_filenamesc             C   sP   d}t |dd}tt|}x*|jD ] }t|tjr(| |j	|j	 q(W d S )Nzdef f(): pass
def g(): passZmyfilerE   )
rH   r   r   r   	co_consts
isinstancetypesCodeTypeassertIsrI   )r   r)   r?   objr   r   r   test_same_filename_used   s    z$CodeTestCase.test_same_filename_usedN)	r   r   r   r@   rB   rJ   r   cpython_onlyrQ   r   r   r   r   r=   m   s   r=   c            	   @   sR   e Zd Zdddddgdddd	Zd
d Zdd Zdd Zdd Zej	dd Z
dS )ContainerTestCasezfoo@bar.baz.spamgHzns@i   r   z.zyx.41)
.zyx.41rT   rT   rT   rT   rT   rT   rT   rT   rT   Fu   Andrè Previn)ZastringZafloatZanintZ
ashortlongZalistZatupleZabooleanZaunicodec             C   s   |  | j d S )N)r   d)r   r   r   r   	test_dict   s    zContainerTestCase.test_dictc             C   s   |  t| j  d S )N)r   listrU   items)r   r   r   r   	test_list   s    zContainerTestCase.test_listc             C   s   |  t| j  d S )N)r   tuplerU   keys)r   r   r   r   
test_tuple   s    zContainerTestCase.test_tuplec             C   s*   x$t tfD ]}| || j  q
W d S )N)set	frozensetr   rU   r[   )r   constructorr   r   r   	test_sets   s    zContainerTestCase.test_setsc             C   s&   t  }tt|}| || d S )N)r^   r   r   r   rO   )r   rP   Zobj2r   r   r   test_empty_frozenset_singleton   s    z0ContainerTestCase.test_empty_frozenset_singletonN)r   r   r   rU   rV   rY   r\   r`   r   rR   ra   r   r   r   r   rS      s   rS   c               @   s$   e Zd Zdd Zdd Zdd ZdS )BufferTestCasec             C   s6   t d}| | tt|}| t|t d S )Ns   abc)	bytearrayr   r   r   r   r	   typebytes)r   r,   r   r   r   r   test_bytearray   s    
zBufferTestCase.test_bytearrayc             C   s6   t d}| | tt|}| t|t d S )Ns   abc)
memoryviewr   r   r   r   r	   rd   re   )r   r,   r   r   r   r   test_memoryview   s    
zBufferTestCase.test_memoryviewc             C   s,   t  dd}tt|}| |d d S )NBs   abc)arrayr   r   r   r	   )r   ar   r   r   r   
test_array   s    zBufferTestCase.test_arrayN)r   r   r   rf   rh   rl   r   r   r   r   rb      s   rb   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d Zdd Zdd Zdd ZdS )BugsTestCasec             C   s   t dgd  d S )N   i  )r   r   )r   r   r   r   test_bug_5888452   s    zBugsTestCase.test_bug_5888452c             C   sF   |  ttjd |  ttjd |  ttjtdd d d  d S )N   0   fr   A   r%   )assertRaises	Exceptionr   r   r   )r   r   r   r   test_patch_873224   s    zBugsTestCase.test_patch_873224c             C   s8   |  ttddd |  ttddd d S )N   r   r   )r	   r   r   r   )r   r   r   r   test_version_argument   s    z"BugsTestCase.test_version_argumentc          	   C   sD   x>t dD ]2}t|g}yt| W q
 tk
r:   Y q
X q
W d S )N   )rangere   r   r   rt   )r   icr   r   r   	test_fuzz   s    
zBugsTestCase.test_fuzzc                s.   dd }|dt j  fdd}|d| d S )Nc             S   s\   |d|  d  |d|  d  |d|  d  |d|  d d|    |d|  d  d S )Ns   )   Ns   (   s   [   s   {Nrp   s   >   r   )Ncheckr   r   r   	run_tests   s
    z4BugsTestCase.test_loads_recursion.<locals>.run_testsd   c                s     ttj|  d S )N)rs   
ValueErrorr   r   )r)   )r   r   r   r      s    z0BugsTestCase.test_loads_recursion.<locals>.checki   )r   r   )r   r   r   r   )r   r   test_loads_recursion   s    z!BugsTestCase.test_loads_recursionc             C   s   g  }}t jdkrd}nd}x(t|d D ]}|dg |d }q*W t|}t|}| t|t| | t|d t|d  | t|d t|d  |dg | 	t
tj| d S )Nnti  i  r   r   r%   )osnamery   appendr   r   r   r	   lenrs   r   )r   headZlastZMAX_MARSHAL_STACK_DEPTHrz   dataZnew_headr   r   r   test_recursion_limit   s    


z!BugsTestCase.test_recursion_limitc             C   sB   x<t tttttttfD ]$}td|fi }| 	t
tj|  qW d S )Nsubtyp)r&   r/   complexrZ   rW   dictr]   r^   rd   rs   r   r   r   )r   typr   r   r   r   test_exact_type_match  s    z"BugsTestCase.test_exact_type_matchc             C   s   t d}d| }t| d S )Ng    .Ar3   )r&   r   r   )r   sizeZ
testStringr   r   r   test_large_marshal  s    zBugsTestCase.test_large_marshalc             C   s   d}|  ttj| d S )Ns	   l       )rs   r   r   r   )r   Zinvalid_stringr   r   r   test_invalid_longs  s    zBugsTestCase.test_invalid_longsc          
   C   s   ddddddddgff}xd	D ]}t |}g }zttjd
<}x4|D ],}t|| |rb|| ||  qDW W d Q R X ttjdP}xHt	|D ]<\}}| 
|t| |r|| | 
|| |  qW W d Q R X W d ttj X qW d S )Nr   r3   s   defg      ?r   rk   r,      c)r7   s   0123r   r   )r   r
   r   r   r   r   writer   tell	enumerater	   r   readr   )r   r   ZinterleavedZilenZ	positionsr   rU   rz   r   r   r   test_multiple_dumps_and_loads  s$    



&z*BugsTestCase.test_multiple_dumps_and_loadsc             C   s   d}|  ttj| d S )NT)rs   	TypeErrorr   r   )r   Zunicode_stringr   r   r   !test_loads_reject_unicode_strings2  s    z.BugsTestCase.test_loads_reject_unicode_stringsc          	   C   s>   G dd dt j}x&dD ]}| ttj|t| qW d S )Nc                   s   e Zd Z fddZ  ZS )z/BugsTestCase.test_bad_reader.<locals>.BadReaderc                s(   t  |}|d k	r$|dkr$|d7 }|S )N   i@B )superreadinto)r   bufr   )	__class__r   r   r   9  s    z8BugsTestCase.test_bad_reader.<locals>.BadReader.readinto)r   r   r   r   __classcell__r   r   )r   r   	BadReader8  s   r   )g      ?y              ?s
   0123456789
0123456789)ioBytesIOrs   r   r   r   r   )r   r   valuer   r   r   test_bad_reader7  s    

zBugsTestCase.test_bad_readerc             C   s<   t d}x,tt|D ]}| tt j|d|  qW d S )N)helloZdollyNr   )r   r   ry   r   rs   EOFErrorr   )r   r   rz   r   r   r   test_eofB  s    
zBugsTestCase.test_eofN)r   r   r   ro   ru   rw   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   rm      s   
rm   l        l    r"   r   c               @   s   e Zd Zdd ZdS )
NullWriterc             C   s   d S )Nr   )r   r)   r   r   r   r   K  s    zNullWriter.writeN)r   r   r   r   r   r   r   r   r   J  s   r   z!test cannot run on 32-bit systemsc               @   s   e Zd Zdd Zejeddddd Zejeddddd	 Zejee	d
 dddd Z
ejee	d
 dddd Zejee	d eed
  dddd Zejee	d eed
  dddd Zejeddddd ZdS )LargeValuesTestCasec             C   s   |  ttj|t  d S )N)rs   r   r   r   r   )r   r   r   r   r   check_unmarshallableP  s    z(LargeValuesTestCase.check_unmarshallabler   F)r   ZmemuseZdry_runc             C   s   |  d|  d S )N   x)r   )r   r   r   r   r   r9   S  s    zLargeValuesTestCase.test_bytesc             C   s   |  d|  d S )NrC   )r   )r   r   r   r   r   test_strW  s    zLargeValuesTestCase.test_strr   c             C   s   |  d|  d S )N)N)r   )r   r   r   r   r   r\   [  s    zLargeValuesTestCase.test_tuplec             C   s   |  d g|  d S )N)r   )r   r   r   r   r   rY   _  s    zLargeValuesTestCase.test_list   c             C   s   |  tt| d S )N)r   r]   ry   )r   r   r   r   r   test_setc  s    zLargeValuesTestCase.test_setc             C   s   |  tt| d S )N)r   r^   ry   )r   r   r   r   r   test_frozenseti  s    z"LargeValuesTestCase.test_frozensetc             C   s   |  t| d S )N)r   rc   )r   r   r   r   r   rf   o  s    z"LargeValuesTestCase.test_bytearrayN)r   r   r   r   r   Z
bigmemtest
LARGE_SIZEr9   r   pointer_sizer\   rY   r   	getsizeofr   r   rf   r   r   r   r   r   N  s   r   c             C   s   t || krdS | t | t|ttttfrJxL|D ]}t| | q6W n4t|tr~x(|	 D ]\}}t| | t| | q^W t
| S )z&Collect object ids seen in a structureN)idaddrL   rW   rZ   r]   r^   CollectObjectIDsr   rX   r   )ZidsrP   ekvr   r   r   r   s  s    


r   c               @   sb   e Zd ZdZdZdZdedeediZdddZd	d
 Zdd Z	dd Z
dd Zdd Zdd ZdS )InstancingTestCasei gn?Zabcdeabcdeabcder   ZgoodbyeFc       
      C   s   ||f}t t |}t|d}t t t|}| || |st|d}t t t|}	| |	| |s| t|t| n| t|t| d S )N   r   )	r   r]   r   r   r   r	   ZassertGreaterr   ZassertGreaterEqual)
r   Zrsample	recursivesimpler   Zn0Zs3Zn3s2Zn2r   r   r   helper3  s    zInstancingTestCase.helper3c             C   s    |  | j | j| jdd d S )NT)r   )r   intobjr   )r   r   r   r   testInt  s    zInstancingTestCase.testIntc             C   s   |  | j | | j d S )N)r   floatobjr   )r   r   r   r   	testFloat  s    zInstancingTestCase.testFloatc             C   s   |  | j | | j d S )N)r   strobjr   )r   r   r   r   testStr  s    zInstancingTestCase.testStrc             C   s   |  | j | | j d S )N)r   dictobjr   )r   r   r   r   testDict  s    zInstancingTestCase.testDictc          	   C   sL   t td}| }W d Q R X tdr4t|td}| | | | d S )Nr   z.pyrE   )r
   __file__r   endswithrH   r   r   )r   r   coder   r   r   
testModule  s    

zInstancingTestCase.testModulec             C   sD   t | j}||d< | j|dd | jg}|| | j|dd d S )Nr   T)r   )r   r   r   r   )r   rU   lr   r   r   testRecursion  s    

z InstancingTestCase.testRecursionN)FF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
r   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )CompatibilityTestCasec          	   C   sN   t td}| }W d Q R X tdr4t|td}t||}t| d S )Nr   z.pyrE   )r
   r   r   r   rH   r   r   r   )r   versionr   r   r   r   r   r   _test  s    
zCompatibilityTestCase._testc             C   s   |  d d S )Nr   )r   )r   r   r   r   test0To3  s    zCompatibilityTestCase.test0To3c             C   s   |  d d S )Nr   )r   )r   r   r   r   test1To3  s    zCompatibilityTestCase.test1To3c             C   s   |  d d S )Nr   )r   )r   r   r   r   test2To3  s    zCompatibilityTestCase.test2To3c             C   s   |  d d S )Nr   )r   )r   r   r   r   test3To3  s    zCompatibilityTestCase.test3To3N)r   r   r   r   r   r   r   r   r   r   r   r   r     s
   r   c               @   s*   e Zd ZdZeeZdd Zdd ZdS )InterningTestCasezthis is an interned stringc             C   sX   t t | j}| || j | t|t| j t|}| t|t| d S )N)r   r   r   r   r	   r   r   intern)r   r)   r   r   r   r   
testIntern  s
    
zInterningTestCase.testInternc             C   sZ   t t | jd}| || j | t|t| j t|}| t|t| d S )Nr   )	r   r   r   r   r	   ZassertNotEqualr   r   r   )r   r)   r   r   r   r   testNoIntern  s
    
zInterningTestCase.testNoInternN)r   r   r   r   r   r   r   r   r   r   r   r   r     s   
r   zrequires _testcapic               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )CAPI_TestCasec          
   C   sd   x^t tjd D ]L}tdtj| ttjd}| }W d Q R X t	tj | 
|d qW d S )Nr   ixV4r   s   xV4)ry   r   r   	_testcapiZpymarshal_write_long_to_filer   r   r
   r   r   r	   )r   r   r   r   r   r   r   test_write_long_to_file  s    z%CAPI_TestCase.test_write_long_to_filec          
   C   s~   ddddddd f}xdt tjd D ]R}t|tj| ttjd	}| }W d Q R X t	tj | 
t|| q$W d S )
Nu   €s   abc{   gF@y      @       @z
long line i  r   r   )ry   r   r   r   Zpymarshal_write_object_to_filer   r   r
   r   r   r	   r   )r   rP   r   r   r   r   r   r   test_write_object_to_file  s    z'CAPI_TestCase.test_write_object_to_filec          	   C   s   t tjd}|d W d Q R X ttj\}}ttj | |d | |d t tjd}|d W d Q R X | t	 ttj W d Q R X ttj d S )Nr   s   4xxxxi4  r      )
r
   r   r   r   r   Zpymarshal_read_short_from_filer   r	   rs   r   )r   r   rpr   r   r   test_read_short_from_file  s    z'CAPI_TestCase.test_read_short_from_filec          	   C   s   t tjd}|d W d Q R X ttj\}}ttj | |d | |d t tjd}|d W d Q R X | t	 ttj W d Q R X ttj d S )Nr   s   xV4xxxxixV4r   s   V4)
r
   r   r   r   r   Zpymarshal_read_long_from_filer   r	   rs   r   )r   r   r   r   r   r   r   test_read_long_from_file
  s    z&CAPI_TestCase.test_read_long_from_filec          
   C   s   d}xt tjd D ]}t||}ttjd}||d  W d Q R X t	tj\}}t
tj | || ttjd}||d d  W d Q R X | t t	tj W d Q R X t
tj qW d S )N)u   €s   abcr   gF@y      @       @r   r   s   xxxx)ry   r   r   r   r
   r   r   r   r   Z$pymarshal_read_last_object_from_filer   r	   rs   r   )r   rP   r   r   r   r   r   r   r   r   test_read_last_object_from_file  s    z-CAPI_TestCase.test_read_last_object_from_filec          
   C   s   d}xt tjd D ]}t||}ttjd}||d  W d Q R X t	tj\}}t
tj | || | |t| ttjd}||d d  W d Q R X | t t	tj W d Q R X t
tj qW d S )N)u   €s   abcr   gF@y      @       @r   r   s   xxxx)ry   r   r   r   r
   r   r   r   r   Zpymarshal_read_object_from_filer   r	   r   rs   r   )r   rP   r   r   r   r   r   r   r   r   test_read_object_from_file(  s    z(CAPI_TestCase.test_read_object_from_fileN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   	r   __main__)$Ztestr   rj   r   r   r   Zunittestr   rM   r   ImportErrorr   ZTestCaser   r.   r1   r:   r=   rS   rb   rm   r   r   r   r   ZskipIfr   r   r   r   r   rR   Z
skipUnlessr   r   mainr   r   r   r   <module>   sF   
$  
$?Q
