B
    u9a}                 @   s   d dl Z d dlmZ d dlZd dlZd dlZd dlZG dd dZG dd dZ	G dd dZ
G d	d
 d
e
e	e jZG dd dZG dd de
e	ee jZ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edkre   dS )    N)supportc               @   s    e Zd Zdd Zdd ZeZdS )IntLikec             C   s
   || _ d S )N)_num)selfZnum r   #/usr/lib/python3.7/test_memoryio.py__init__   s    zIntLike.__init__c             C   s   | j S )N)r   )r   r   r   r   	__index__   s    zIntLike.__index__N)__name__
__module____qualname__r   r	   __int__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 )MemorySeekTestMixinc             C   s   |  d}| |}d S )N
1234567890)buftypeioclass)r   bufbytesIor   r   r   testInit   s    
zMemorySeekTestMixin.testInitc             C   sx   |  d}| |}| |d d |d | |dd |d | |dd  |d | | j|  d S )Nr            i  )r   r   assertEqualreadEOF)r   r   r   r   r   r   testRead   s    

zMemorySeekTestMixin.testReadc             C   s:   |  d}| |}| ||  | | j|  d S )Nr   )r   r   r   r   r   )r   r   r   r   r   r   testReadNoArgs$   s    

z"MemorySeekTestMixin.testReadNoArgsc             C   sn   |  d}| |}|d |d | ||  |d | |dd  |  | t|jd d S )Nr   r   r      g        )r   r   r   seekr   assertRaises	TypeError)r   r   r   r   r   r   testSeek+   s    




zMemorySeekTestMixin.testSeekc             C   s\   |  d}| |}| d|  |d | d|  |d | d|  d S )Nr   r   r   i'  )r   r   r   tellr   )r   r   r   r   r   r   testTell7   s    



zMemorySeekTestMixin.testTellN)r
   r   r   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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 )*MemoryTestMixinc             C   s   |   }| | j|j d S )N)r   r   UnsupportedOperationdetach)r   r   r   r   r   test_detachD   s    zMemoryTestMixin.test_detachc             C   s   |  ||dd |  |dd |  ||dd |  | d |  |dd |  | d |  ||dd |  |dd |  ||dd	 |  |d
d
 |  | d	 d S )Nzblah.r   r   zHello.   z	 world


	   hr      )r   writer   r"   truncate)r   ftr   r   r   	write_opsH   s    zMemoryTestMixin.write_opsc             C   s   |  d}| |}| || j  | | | |  }| || j  | | | | t|jd  |  | t	|j|  d d S )Nzhello world
 )
r   r   r0   r   getvaluer   r    r,   close
ValueError)r   r   memior   r   r   
test_writeU   s    

zMemoryTestMixin.test_writec             C   s   |  d}|  }| ||gd d  | | |d  |g  | | |d  |  }| t|j|gdg  | | | | t|jd  |  | t|jg  d S )Nr   d   r   )	r   r   r   
writelinesr2   r   r    r3   r4   )r   r   r5   r   r   r   test_writelinesb   s    

zMemoryTestMixin.test_writelinesc                s*      } fdd} t|j|  d S )Nc               3   s     dV  td S )Nspam)r   KeyboardInterruptr   )r   r   r   	error_gens   s    z8MemoryTestMixin.test_writelines_error.<locals>.error_gen)r   r   r;   r8   )r   r5   r<   r   )r   r   test_writelines_errorq   s    z%MemoryTestMixin.test_writelines_errorc             C   sn  |  d}| |}| t|jd | t|jtd |d | |tdd | | |d d  | | d | | |d d  | |dd | | |d d  | |	 d |dd |
| | | |d d |  |	 }| |d | | |	 | | t|jd |  | t|jd | t|jtd d S )	Nr   r(      r   r      0)r   r   r   r4   r-   r   r   r   r2   r"   r,   r    r3   )r   r   r5   posr   r   r   test_truncatey   s,    



zMemoryTestMixin.test_truncatec             C   s   |  d}| |}| | | | d }| | | j ||d  | | |d  || | | | | t|jg  d S )Nr   r@   )r   r   r   r2   r   r   r   r    )r   r   r5   r   r   r   	test_init   s    



zMemoryTestMixin.test_initc             C   sz  |  d}| |}| |d| j | |d|d d  | |d|dd  | |d|dd   | | | j |d | |td| j | |td|d d  | |td|dd  | |td|dd   |d | | | | | | j | | d |d | |d| |d | |td| |d | t| t| |d	 | t| t| |d | |d | | 	t
|jd
 |t|d  | |d| j |t|d  | |td| j |t|d  | | | j |  | 	t|j d S )Nr   r   r   r   r   i  
   r>   r7   r1   )r   r   r   r   r   r   r   r"   typer   r    lenr3   r4   )r   r   r5   r   r   r   	test_read   sF    








zMemoryTestMixin.test_readc             C   s  |  d}| |d }| |d| j | |td| j | | | | | | | | | j |d | |d|d d  | |d|dd  | |d|dd  |d | |td|d d  | |td|dd  | |td|dd  |d | |d| |d | |td| |d | |d| j | |td| j |t|d d  | | | j |  d}| |d	 d d }| | | | | | | | |d d  | | | j |d | t| t| | | | | 	t
|jd
 |  | 	t|j d S )Nz1234567890
r@   r   r   rE      r>   r   r   r1   )r   r   r   readliner   r   r   rG   rF   r   r    r3   r4   )r   r   r5   r   r   r   test_readline   sH    







zMemoryTestMixin.test_readlinec             C   sZ  |  d}| |d }| | |gd  |d | | |dd  g|gd   |d | |d|gd  |d | |d|gd  |d | |d|gd  |d | t| d t| |d | |d |gd  | t|jd	 |t|d d
  | | g  |	  | t
|j d S )Nz1234567890
rE   r   r)   r   rI   r@   r>   r1   r   )r   r   r   	readlinesr   rF   r   r    rG   r3   r4   )r   r   r5   r   r   r   test_readlines   s(    

$




zMemoryTestMixin.test_readlinesc             C   s  |  d}| |d }| t|| | t|d | t|d d}x |D ]}| || |d7 }qRW | |d |d d}x |D ]}| || |d7 }qW | |d |t|d d  | t|g  | |d }|	  | 
t|j d S )Nz1234567890
rE   __iter____next__r   r   r@   )r   r   r   iterZ
assertTruehasattrr   rG   listr3   r   r4   rO   )r   r   r5   iliner   r   r   test_iterator  s*    



zMemoryTestMixin.test_iteratorc             C   s   |  d}| |}| | | |  | | | | t| t| | |d }| | dd  |  d | |}|  | t|j d S )Nr   i  Z890)	r   r   r   r2   r   rF   r3   r   r4   )r   r   r5   r   r   r   test_getvalue  s    


zMemoryTestMixin.test_getvaluec             C   s\  |  d}| |}|d | t|jd | t|jdd | t|jdd | |dd | |ddd | | | | |dd | |ddd | | |dd   | |t|t| | | | j |t|d  | | | j | |ddt| | | | j |	  | t|jd d S )Nr   r   r>   r   r   r   r@   )
r   r   r   r   r4   r   r   rG   r   r3   )r   r   r5   r   r   r   	test_seek,  s(    


zMemoryTestMixin.test_seekc             C   s   |  d}| |}| |t|d d | | | j | | d | | | |	| j | | | |	| | | ||  d |  d S )Nr   r       )
r   r   r   r   rG   r   r   r"   r2   r,   )r   r   r5   r   r   r   test_overseekC  s    


zMemoryTestMixin.test_overseekc             C   sr   |  d}| |}| | d |d | | d |d | | d |  | t|j d S )Nr   r   r   i'  )r   r   r   r"   r   r3   r   r4   )r   r   r5   r   r   r   	test_tellP  s    



zMemoryTestMixin.test_tellc             C   s(   |  d}| |}| | d  d S )Nr   )r   r   r   flush)r   r   r5   r   r   r   
test_flush\  s    

zMemoryTestMixin.test_flushc             C   s   |   }| | d | | d | | d | | d | |jd |  | t	|j | t	|j | t	|j | t	|j | |jd d S )NTF)
r   r   writablereadableseekableisattyclosedr3   r   r4   )r   r5   r   r   r   
test_flagsb  s    zMemoryTestMixin.test_flagsc                sF    d  fdd} fdd}|   |   d S )Nr   c                 s"   G dd dj } |  }| S )Nc               @   s   e Zd ZdS )z>MemoryTestMixin.test_subclassing.<locals>.test1.<locals>.MemION)r
   r   r   r   r   r   r   MemIOt  s   re   )r   r2   )re   m)r   r   r   r   test1s  s    z/MemoryTestMixin.test_subclassing.<locals>.test1c                 s(   G fdddj } |  d }| S )Nc                   s   e Zd Z fddZdS )z>MemoryTestMixin.test_subclassing.<locals>.test2.<locals>.MemIOc                s    j | | d S )N)r   r   )meab)r   r   r   r   z  s    zGMemoryTestMixin.test_subclassing.<locals>.test2.<locals>.MemIO.__init__N)r
   r   r   r   r   )r   r   r   re   y  s   re   )r   r2   )re   rf   )r   r   r   r   test2x  s    
z/MemoryTestMixin.test_subclassing.<locals>.test2)r   r   )r   rg   rk   r   )r   r   r   test_subclassingq  s
    
z MemoryTestMixin.test_subclassingc             C   s$   xt dD ]}|  }d|_q
W d S )Nr7   r   )ranger   foo)r   _r5   r   r   r   test_instance_dict_leak  s    z'MemoryTestMixin.test_instance_dict_leakc       	         s    d} |}d|_|d G  fddd j}dd l}d|_|j|_||_||d}|d xt	dt
jd	 D ]}x||fD ]z}t
t
j||d
} | |   |j|j  |j|j  | |  |   tt
j|| qW q|W |`d S )Nr   *   r@   c                   s   e Zd Z fddZdS )z6MemoryTestMixin.test_pickling.<locals>.PickleTestMemIOc                s    j | | || _d S )N)r   r   rn   )rh   Z	initvaluern   )r   r   r   r     s    z?MemoryTestMixin.test_pickling.<locals>.PickleTestMemIO.__init__N)r
   r   r   r   r   )r   r   r   PickleTestMemIO  s   rr   r   __main__P   r   )Zprotocol)r   r   rn   r   rs   r   r
   r   rr   rm   pickleZHIGHEST_PROTOCOLloadsdumpsr   r2   	__class__r"   r3   r   r4   )	r   r   r5   rr   rs   ZsubmemioprotoobjZobj2r   )r   r   test_pickling  s*    




zMemoryTestMixin.test_picklingN)r
   r   r   r'   r0   r6   r9   r=   rC   rD   rH   rK   rM   rU   rW   rX   r[   r\   r^   rd   rl   rp   r{   r   r   r   r   r$   B   s(   &)r$   c               @   s`   e Zd ZejZedd Zej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S )PyBytesIOTestc             C   s
   |  dS )Nascii)encode)sr   r   r   r     s    zPyBytesIOTest.buftype    c             C   s   |  d}| }| t|d |d | }| t|d | t|jd | t|j | t|j	 | 
|j d|dd< | t|d | | d ~t  |  |	  | t|j d S )Ns
   1234567890r   sd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs   abcr   r(   s
   123abc7890)r   	getbufferr   bytesr   r   BufferErrorr,   r-   r3   assertFalserc   r2   r   Z
gc_collectr4   )r   r5   r   r   r   r   test_getbuffer  s$    

zPyBytesIOTest.test_getbufferc             C   s<   |  d}| | | | | | |d| d S )Nr   r>   )r   r   r   read1)r   r   r   r   r   
test_read1  s    
zPyBytesIOTest.test_read1c             C   sj  |  d}| |}td}| ||d | |d | ||d | |d | ||d | |d td}|d | ||d | |d	 td
}|d | ||d | |d
 | t|jd dd l}|dd}| |}|| | |	 d	 |
  | t|j| | d}t }|d || | |d
 d S )Nr   s   hellor   s   12345s   67890r   s   hello worldrE   s   1234567890dr   r1   rj   s   123rq   )r   r   	bytearrayr   readintor   r   r    arraytobytesr3   r4   )r   r   r5   rj   r   ri   r   r   r   test_readinto  s<    








zPyBytesIOTest.test_readintoc             C   s   |  d}| |}| |ddd | |ddd | |ddd | |ddd | |ddd	 | |ddd
 |dd | | |dd   |d |dd | | |dd   d S )Nr   r>   r   r   r   r@   r)   rE   rY   rV   )r   r   r   r   r   )r   r   r5   r   r   r   test_relative_seek  s    


z PyBytesIOTest.test_relative_seekc             C   s>   |   }| t| j d | t|jd | t|jdg d S )Nr   )r   r   r    r,   r8   )r   r5   r   r   r   test_unicode  s    zPyBytesIOTest.test_unicodec             C   s\   d}dd l }| dt|}| |}| | | | ||d | | | d S )Ns
   1234567890r   rj   rE   )r   rR   r   r   r2   r,   )r   r   r   ri   r5   r   r   r   test_bytes_array  s    
zPyBytesIOTest.test_bytes_arrayc             C   s.   |  d}| j|d | jt| j|d d d S )Nr   )Zinitial_bytes)rn   )r   r   r   r    )r   r   r   r   r   test_issue5449  s    
zPyBytesIOTest.test_issue5449N)r
   r   r   pyior%   staticmethodr   BytesIOr   r   r   r   r   r   r   r   r   r   r   r   r   r|     s   !	r|   c               @   sd   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S )TextIOTestMixinc                s   | j d d  fdd}|  jd   d |  |  jd  d |  |  jd  d |  |  jd	 d S )
N)newlinec                  s     d    d S )Nr   )r   r   r   )r5   r   r   force_decode%  s    
z<TextIOTestMixin.test_newlines_property.<locals>.force_decodeza

zb
)r   z
zcd)r   z
)r   r   newlinesr,   )r   r   r   )r5   r   test_newlines_property   s    


z&TextIOTestMixin.test_newlines_propertyc             C   sx   |   }| t|jdd | t|jdd | t|jdd | t|jdd | t|jdd | t|jdd d S )Nr>   r   r   rV   r@   )r   r   OSErrorr   )r   r5   r   r   r   r   3  s    z"TextIOTestMixin.test_relative_seekc             C   s0   |   }| |j | |j | |j d S )N)r   ZassertIsNoneencodingerrorsr   line_buffering)r   r5   r   r   r   test_textio_properties=  s    z&TextIOTestMixin.test_textio_propertiesc             C   s~   |  d}| t|dddg | | d |   }| |dd |d | t|dddg | | d d S )Nza
b
cdza
zb
zcdr?   r   )r   r   rR   r2   r,   r   )r   r5   r   r   r   test_newline_defaultF  s    

z$TextIOTestMixin.test_newline_defaultc             C   s  | j dd d}| t|ddddg |d | |dd	 | |d
d | |d
d | |dd | | d | j d d}| d
|d | d|d | d|d |d | | d | | d | j dd d}| |dd d S )Nza
b
cd)r   za
zb
zc
dr   r   ri   r@   z
bz
cr   za
b
c
dr   zb
zcdza
bza
b)r   r   rR   r   r   r2   r,   )r   r5   r   r   r   test_newline_noneQ  s"    

z!TextIOTestMixin.test_newline_nonec             C   s   | j ddd}| t|ddddg |d | |d	d
 | |dd | |dd | | d | j dd}| d|d | d|d | d|d | d|d |d | t|ddddg | | d d S )Nza
b
cdr1   )r   za
zb
zcr   r   r   za
br@   z
cr   r   zbzd)r   r   rR   r   r   r2   r,   )r   r5   r   r   r   test_newline_emptyg  s    

z"TextIOTestMixin.test_newline_emptyc             C   s   | j ddd}| t|dddg | | d | j dd}| |dd |d | t|dddg | | d d S )	Nza
b
cdr   )r   za
zb
zcdr?   r   )r   r   rR   r2   r,   r   )r   r5   r   r   r   test_newline_lfz  s    
zTextIOTestMixin.test_newline_lfc             C   s   | j ddd}| | d |d | t|ddddd	g | | d | j dd}| |dd
 |d | t|ddddd	g |d | | ddddd	g | | d d S )Nza
b
cdr   )r   zabcdr   zazbzcr   r?   )r   r   r   r   rR   r2   r,   rL   )r   r5   r   r   r   test_newline_cr  s    


zTextIOTestMixin.test_newline_crc             C   s   | j ddd}| | d |d | t|dddg |d | | dddg | | d | j dd}| |dd	 |d | t|dddg | | d d S )
Nza
b
cdz
)r   z
a
b
cdr   za
zb
zcdr?   )r   r   r   r   rR   rL   r2   r,   )r   r5   r   r   r   test_newline_crlf  s    


z!TextIOTestMixin.test_newline_crlfc             C   s4   | j dd d}| |dd | | d d S )Nza
b
)r   r   za
b
)r   r   r   r2   )r   r5   r   r   r   test_issue5265  s    zTextIOTestMixin.test_issue5265c             C   sB   | j t| jdd | j t| jdd xdD ]}| j|d q*W d S )N   
)r   error)Nr1   r   r   z
)r   r    r   r4   )r   r   r   r   r   test_newline_argument  s    
z%TextIOTestMixin.test_newline_argumentN)r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
	r   c               @   s(   e Zd ZeZejZejZdZ	dd Z
dS )PyStringIOTestr1   c             C   s@   |  d}| | d |   }|d | | d d S )Nu   )r   r   r   r,   r2   )r   r5   r   r   r   test_lone_surrogates  s
    

z#PyStringIOTest.test_lone_surrogatesN)r
   r   r   strr   r   StringIOr   r%   r   r   r   r   r   r   r     s
   r   c               @   s,   e Zd ZeZejZdZG dd dejZ	dS )PyStringIOPickleTestr1   c               @   s   e Zd Zdd Zdd ZdS )zPyStringIOPickleTest.ioclassc             O   s   t t tj||S )N)ru   rv   rw   r   r   )clsargskwargsr   r   r   __new__  s    z$PyStringIOPickleTest.ioclass.__new__c             O   s   d S )Nr   )r   r   r   r   r   r   r     s    z%PyStringIOPickleTest.ioclass.__init__N)r
   r   r   r   r   r   r   r   r   r     s   r   N)
r
   r   r   r   r   r   r%   r   r   r   r   r   r   r   r     s   r   c               @   s|   e Zd ZejZejZdd Zdd Ze	j
Z
e	jdd Zdd Ze	jd	d
 Ze	jdd Ze	jdd Ze	jdd ZdS )CBytesIOTestc             C   sr   |   }| }| t|d t|d  | |d t |d d k	rX| |d t |  | 	t
|j d S )Nr   r   r   r@   )r   __getstate__r   rG   r   assertIsInstanceintdictr3   r   r4   )r   r5   stater   r   r   test_getstate  s    zCBytesIOTest.test_getstatec             C   s   |   }|d |tddd f |ddddif | t|jd | t|jd | t|jd | t|jd	 | t|jd
 | t|j | t|jd |  | t|jd d S )N)s   no errorr   Ns   no errorr   r:   r   )r   r>   N)Zunicoder   N)r   g        N)r   r   r   )s   len-testr   )s   closedr   N)r   __setstate__r   r   r4   r    r3   )r   r5   r   r   r   test_setstate  s    
zCBytesIOTest.test_setstatec             C   s^   t d}| j}| tt | |t | d}|td| |t	d|   d S )NZP2n2Pni     a)
r   Zcalcobjsizecheck_sizeofr   object
__sizeof__ior   sys	getsizeof)r   ZbasesizeZchecknr   r   r   test_sizeof  s    
zCBytesIOTest.test_sizeofc             C   sL   d}t |}| |}| t ||d  || | t || d S )Ns                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   r   )r   getrefcountr   r   )r   mutationZimmold_rcr5   r   r   r   _test_cow_mutation  s    

zCBytesIOTest._test_cow_mutationc             C   s   dd }|  | d S )Nc             S   s   |  d d S )Nr   )r-   )r5   r   r   r   r     s    z0CBytesIOTest.test_cow_truncate.<locals>.mutation)r   )r   r   r   r   r   test_cow_truncate  s    zCBytesIOTest.test_cow_truncatec             C   s   dd }|  | d S )Nc             S   s   |  d | d d S )Nr   s   foo)r   r,   )r5   r   r   r   r     s    
z-CBytesIOTest.test_cow_write.<locals>.mutation)r   )r   r   r   r   r   test_cow_write  s    zCBytesIOTest.test_cow_writec                s,   |  d}|   fdd}| | d S )Ns   fooooooc                s   |    d S )N)r   )r5   )r   r   r   r     s    z0CBytesIOTest.test_cow_setstate.<locals>.mutation)r   r   r   )r   r5   r   r   )r   r   test_cow_setstate  s    
zCBytesIOTest.test_cow_setstatec             C   s2   t d}t|}| |}| t|| d S )Ni   )r   r   r   r   r   )r   Zbar   r5   r   r   r   test_cow_mutable   s    

zCBytesIOTest.test_cow_mutableN)r
   r   r   r   r   r   r%   r   r   r   r   Zcpython_onlyr   r   r   r   r   r   r   r   r   r   r     s   		r   c               @   s0   e Zd ZejZejZdd Zdd Zdd Z	dS )CStringIOTestc             C   s   |  d}| |}| | | | ||t| | | t| | | | | ||t| | | t|d  | | ||  d S )Nu   𠌊𠍇r@   )r   r   r   r2   r,   rG   r"   )r   r   r5   r   r   r   test_widechar0  s    

zCStringIOTest.test_widecharc             C   s   |   }| }| t|d | |d t | |d t | |d t |d d k	rl| |d t |  | 	t
|j d S )Nr   r   r   r@   r   )r   r   r   rG   r   r   r   r   r3   r   r4   )r   r5   r   r   r   r   r   <  s    zCStringIOTest.test_getstatec             C   s   |   }|d |dddddif | t|jd | t|jd | t|jd	 | t|jd
 | t|jd | t|jd | t|jd | t|j | t|jd |  | t|jd d S )N)zno errorr   r   Nzno errorr1   r   r:   r   )r1   r.   r   N)r1   r1   r>   N)r   r1   r   N)r1   r   r   N)r1   r1   g        N)r1   r1   r   r   )zlen-testr   )rc   r1   r   N)r   r   r   r4   r    r3   )r   r5   r   r   r   r   H  s    
zCStringIOTest.test_setstateN)
r
   r   r   r   r   r   r%   r   r   r   r   r   r   r   r   *  s
   r   c               @   s$   e Zd ZejZG dd dejZdS )CStringIOPickleTestc               @   s   e Zd Zdd Zdd ZdS )zCStringIOPickleTest.ioclassc             O   s   t t tj||S )N)ru   rv   rw   r   r   )r   r   r   r   r   r   r   ^  s    z#CStringIOPickleTest.ioclass.__new__c             O   s   d S )Nr   )r   r   r   r   r   r   r   `  s    z$CStringIOPickleTest.ioclass.__init__N)r
   r   r   r   r   r   r   r   r   r   ]  s   r   N)r
   r   r   r   r%   r   r   r   r   r   r   r   Z  s   r   rs   )ZunittestZtestr   r   Z_pyior   ru   r   r   r   r$   ZTestCaser|   r   r   r   r   r   r   r
   mainr   r   r   r   <module>   s*   -  np 
W0
