B
    u9ae                @   s  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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mZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ ddlZddlZddlZ yddl!Z!W n e"k
r   dd	 Z#Y nX d
d	 Z#G dd de!j$Z%e
&dp0dZ'e
&dp@dZ(de'kpRde(kZ)dd Z*G dd dZ+G dd de+ej,Z-G dd de+e j,Z.G dd de+Z/G dd de/ej,Z0G dd de/e j,Z1G d d! d!e/Z2G d"d# d#e2ej,Z3G d$d% d%e2e j,Z4G d&d' d'e/Z5G d(d) d)e5ej,Z6G d*d+ d+e5e j,Z7G d,d- d-e/Z8G d.d/ d/e8ej,Z9G d0d1 d1e8e j,Z:G d2d3 d3Z;G d4d5 d5e;ej<Z=G d6d7 d7e;e j<Z>G d8d9 d9Z?G d:d; d;e?ej<Z@G d<d= d=e?e j<ZAG d>d? d?ZBG d@dA dAeBej,ZCG dBdC dCeBe j,ZDG dDdE dEejEZFG dFdG dGeFZGG dHdI dIeFZHejIG dJdK dKejEZJG dLdM dMZKG dNdO dOZLG dPdQ dQejEeKZMG dRdS dSeMeLZNG dTdU dUeMZOG dVdW dWejEeKZPG dXdY dYePeLZQG dZd[ d[ePZRG d\d] d]ejEZSG d^d_ d_eSZTG d`da daeSZUG dbdc dceMePZVG ddde deeVeLZWG dfdg dgeVZXG dhdi diejYZZe[eZj\ G djdk dkejEZ]G dldm dmejEZ^G dndo doej<Z_dpdq Z`G drds dse^ZaG dtdu due^ZbG dvdw dwejEZcG dxdy dyecZdG dzd{ d{ecZeG d|d} d}ejEZfG d~d defZgG dd defZheiejjdkdG dd dejEZkG dd dekZlG dd dekZmdd Zneodkrep  dS )zUnit tests for the io module.    N)dequeUserList)cyclecount)support)assert_python_okrun_python_until_end)FakePathc              O   s   t  dt| |S )Nb)arraybytes)poskw r   /usr/lib/python3.7/test_io.py	byteslike2   s    r   c              O   s:   t | |}t }t|t| |t|ddd< |S )z?Create a bytes-like object having no string or sequence methodsBN)r   EmptyStructctypesZresizelen
memoryviewcast)r   r   dataobjr   r   r   r   5   s
    
c               @   s   e Zd ZdS )r   N)__name__
__module____qualname__r   r   r   r   r   <   s   r   ZCFLAGS ZCONFIG_ARGSz-fsanitize=memoryz--with-memory-sanitizerc           	   C   s    t tddd} | jS Q R X dS )z(Get the default TextIOWrapper chunk sizerzlatin-1)encodingN)open__file___CHUNK_SIZE)fr   r   r   _default_chunk_sizeF   s    r$   c               @   sd   e Zd ZdZd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dZdS )MockRawIOWithoutReadzmA RawIO implementation without read(), so as to exercise the default
    RawIO.read() which calls readinto().r   c             C   s    t || _g | _d| _d| _d S )Nr   )list_read_stack_write_stack_reads_extraneous_reads)selfZ
read_stackr   r   r   __init__P   s    
zMockRawIOWithoutRead.__init__c             C   s   | j t| t|S )N)r(   appendr   r   )r+   r
   r   r   r   writeV   s    zMockRawIOWithoutRead.writec             C   s   dS )NTr   )r+   r   r   r   writableZ   s    zMockRawIOWithoutRead.writablec             C   s   dS )N*   r   )r+   r   r   r   fileno]   s    zMockRawIOWithoutRead.filenoc             C   s   dS )NTr   )r+   r   r   r   readable`   s    zMockRawIOWithoutRead.readablec             C   s   dS )NTr   )r+   r   r   r   seekablec   s    zMockRawIOWithoutRead.seekablec             C   s   dS )Nr   r   )r+   r   whencer   r   r   seekf   s    zMockRawIOWithoutRead.seekc             C   s   dS )Nr   r   )r+   r   r   r   telli   s    zMockRawIOWithoutRead.tellc             C   s   |  j d7  _ t|}y| jd }W n" tk
rF   |  jd7  _dS X |d kr\| jd= d S t|}t||kr| jd= ||d |< |S |d | |d d < ||d  | jd< |S d S )N   r   )r)   r   r'   
IndexErrorr*   )r+   bufZmax_lenr   nr   r   r   readintol   s$    zMockRawIOWithoutRead.readintoNc             C   s   |S )Nr   )r+   r   r   r   r   truncate   s    zMockRawIOWithoutRead.truncate)r   )N)r   r   r   __doc__r,   r.   r/   r1   r2   r3   r5   r6   r;   r<   r   r   r   r   r%   L   s   
r%   c               @   s   e Zd ZdS )CMockRawIOWithoutReadN)r   r   r   r   r   r   r   r>      s   r>   c               @   s   e Zd ZdS )PyMockRawIOWithoutReadN)r   r   r   r   r   r   r   r?      s   r?   c               @   s   e Zd ZdddZdS )	MockRawIONc             C   s8   |  j d7  _ y| jdS    |  jd7  _dS d S )Nr7   r       )r)   r'   popr*   )r+   r:   r   r   r   read   s    zMockRawIO.read)N)r   r   r   rC   r   r   r   r   r@      s   r@   c               @   s   e Zd ZdS )
CMockRawION)r   r   r   r   r   r   r   rD      s   rD   c               @   s   e Zd ZdS )PyMockRawION)r   r   r   r   r   r   r   rE      s   rE   c                   sF   e Zd Z fddZd fdd	Zdd Zdd	 Z fd
dZ  ZS )MisbehavedRawIOc                s   t  |d S )N   )superr.   )r+   r
   )	__class__r   r   r.      s    zMisbehavedRawIO.writeNc                s   t  |d S )NrG   )rH   rC   )r+   r:   )rI   r   r   rC      s    zMisbehavedRawIO.readc             C   s   dS )Nir   )r+   r   r4   r   r   r   r5      s    zMisbehavedRawIO.seekc             C   s   dS )Ni8r   )r+   r   r   r   r6      s    zMisbehavedRawIO.tellc                s   t  | t|d S )N   )rH   r;   r   )r+   r9   )rI   r   r   r;      s    zMisbehavedRawIO.readinto)N)	r   r   r   r.   rC   r5   r6   r;   __classcell__r   r   )rI   r   rF      s
   rF   c               @   s   e Zd ZdS )CMisbehavedRawION)r   r   r   r   r   r   r   rL      s   rL   c               @   s   e Zd ZdS )PyMisbehavedRawION)r   r   r   r   r   r   r   rM      s   rM   c                   s$   e Zd Z fddZdd Z  ZS )SlowFlushRawIOc                s   t    t | _d S )N)rH   r,   	threadingEventin_flush)r+   )rI   r   r   r,      s    
zSlowFlushRawIO.__init__c             C   s   | j   td d S )Ng      ?)rQ   settimesleep)r+   r   r   r   flush   s    
zSlowFlushRawIO.flush)r   r   r   r,   rU   rK   r   r   )rI   r   rN      s   rN   c               @   s   e Zd ZdS )CSlowFlushRawION)r   r   r   r   r   r   r   rV      s   rV   c               @   s   e Zd ZdS )PySlowFlushRawION)r   r   r   r   r   r   r   rW      s   rW   c               @   s   e Zd ZdZdd ZdS )CloseFailureIOr   c             C   s   | j sd| _ td S )Nr7   )closedOSError)r+   r   r   r   close   s    zCloseFailureIO.closeN)r   r   r   rY   r[   r   r   r   r   rX      s   rX   c               @   s   e Zd ZdS )CCloseFailureION)r   r   r   r   r   r   r   r\      s   r\   c               @   s   e Zd ZdS )PyCloseFailureION)r   r   r   r   r   r   r   r]      s   r]   c                   s6   e Zd Z fddZd fdd	Z fddZ  ZS )	
MockFileIOc                s   g | _ t | d S )N)read_historyrH   r,   )r+   r   )rI   r   r   r,      s    zMockFileIO.__init__Nc                s,   t  |}| j|d krd nt| |S )N)rH   rC   r_   r-   r   )r+   r:   res)rI   r   r   rC      s    zMockFileIO.readc                s   t  |}| j| |S )N)rH   r;   r_   r-   )r+   r
   r`   )rI   r   r   r;      s    zMockFileIO.readinto)N)r   r   r   r,   rC   r;   rK   r   r   )rI   r   r^      s   r^   c               @   s   e Zd ZdS )CMockFileION)r   r   r   r   r   r   r   ra      s   ra   c               @   s   e Zd ZdS )PyMockFileION)r   r   r   r   r   r   r   rb      s   rb   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
MockUnseekableIOc             C   s   dS )NFr   )r+   r   r   r   r3      s    zMockUnseekableIO.seekablec             G   s   |  dd S )Nznot seekable)UnsupportedOperation)r+   argsr   r   r   r5      s    zMockUnseekableIO.seekc             G   s   |  dd S )Nznot seekable)rd   )r+   re   r   r   r   r6      s    zMockUnseekableIO.tellc             G   s   |  dd S )Nznot seekable)rd   )r+   re   r   r   r   r<      s    zMockUnseekableIO.truncateN)r   r   r   r3   r5   r6   r<   r   r   r   r   rc      s   rc   c               @   s   e Zd ZejZdS )CMockUnseekableION)r   r   r   iord   r   r   r   r   rf      s   rf   c               @   s   e Zd ZejZdS )PyMockUnseekableION)r   r   r   pyiord   r   r   r   r   rh      s   rh   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S )MockNonBlockWriterIOc             C   s   g | _ d | _d S )N)r(   _blocker_char)r+   r   r   r   r,      s    zMockNonBlockWriterIO.__init__c             C   s   d | j}g | jd d < |S )NrA   )joinr(   )r+   sr   r   r   pop_written  s    z MockNonBlockWriterIO.pop_writtenc             C   s
   || _ dS )z'Block when a given char is encountered.N)rk   )r+   charr   r   r   block_on	  s    zMockNonBlockWriterIO.block_onc             C   s   dS )NTr   )r+   r   r   r   r2     s    zMockNonBlockWriterIO.readablec             C   s   dS )NTr   )r+   r   r   r   r3     s    zMockNonBlockWriterIO.seekablec             C   s   dS )NTr   )r+   r   r   r   r/     s    zMockNonBlockWriterIO.writablec             C   sv   t |}d}| jrby|| j}W n tk
r6   Y n,X |dkrX| j|d |  |S d | _d S | j| t|S )Nr   )r   rk   index
ValueErrorr(   r-   r   )r+   r
   r:   r   r   r   r.     s    zMockNonBlockWriterIO.writeN)
r   r   r   r,   rn   rp   r2   r3   r/   r.   r   r   r   r   rj      s   rj   c               @   s   e Zd ZejZdS )CMockNonBlockWriterION)r   r   r   rg   BlockingIOErrorr   r   r   r   rt   *  s   rt   c               @   s   e Zd ZejZdS )PyMockNonBlockWriterION)r   r   r   ri   ru   r   r   r   r   rv   -  s   rv   c               @   s  e Zd Zdd Zdd Zdd Zdbdd	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/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^ Z1d_d` Z2daS )cIOTestc             C   s   t t j d S )N)r   unlinkTESTFN)r+   r   r   r   setUp3  s    zIOTest.setUpc             C   s   t t j d S )N)r   rx   ry   )r+   r   r   r   tearDown6  s    zIOTest.tearDownc             C   sD  |  |dd |d |  | d |d |  |dd |  |dd |  |dd |  | d |  |ddd |  | d td}|  ||d	 d
|d d < |  |dd |  |dd |  |ddd |  | d |  |dd |  | d | t|jd d S )Ns   blah.rJ   r   s   Hello.   rq   r7   s	    world


	   s	   *********   hrG         g        )assertEqualr.   r<   r6   r5   	bytearrayassertRaises	TypeError)r+   r#   bufferr   r   r   	write_ops9  s(    

zIOTest.write_opsFc             C   s  | d}| |d t|}| ||d | t|d td}| ||d | t|d | |d d d | |dd | | dd | | d	d
 | |tdd | |ddd | | dd | | dd
 | |t d | |dd	d | | dd | | d | 	t
|jd |r|d | |  d |d | |  d | |  d
 |d td}| ||d | t|d d S )NrJ   s   hellos    worlrG   s   d
r      s   hello world
r7   rA      xir|   s   world
   g        s   world
)rC   r   r   r;   r   r   r   r5   r6   r   r   	readinto1)r+   r#   Zbufferedr   r   r   r   read_opsQ  s>    



zIOTest.read_opsl        c          	   C   sR  y|  || j| j W n" ttfk
r<   | d Y nX |  | | j |  |dd |  | | jd  |  |dd| jd  |  | | jd  |  | | jd  |  |dd| jd  |  || jd | jd  |  | | jd  |  |dd| jd  |  |dd| j |  |	dd d S )	Nzno largefile supports   xxx   rq   r7   rG   r   r   )
r   r5   LARGEOverflowErrorrs   skipTestr6   r.   r<   rC   )r+   r#   r   r   r   large_file_opst  s     zIOTest.large_file_opsc          
   C   sh  | j }xBdD ]:}| tj|"}| ||j | ||j W d Q R X qW | jtjddd"}| ||j | ||j W d Q R X | jtjddd(}| ||jd | ||jdg W d Q R X | tjd(}| ||jd | ||jdg W d Q R X | tjdP}| ||jd	 | ||jd
g | ||j	d| j
 | ||j	d| j W d Q R X d S )N)wwbr   r   )	bufferingrbs   blahs   blah
r   Zblahzblah
r7   rq   )rd   r    r   ry   r   rC   readliner.   
writelinesr5   SEEK_CURSEEK_END)r+   excmodefpr   r   r   test_invalid_operations  s&    
zIOTest.test_invalid_operationsc                sz   fdd} fdd} fdd} fdd} fd	d
} fdd} fdd} fdd}|df|df|df|df|df|df|df|df j df jdff
}	x|	D ]\}
} |
 |
 }d|k} | | d|k} | | t| jrd}n$t| j j	fr,d}n
 
d d|krJ|  n t|j |rr|d |  n t|jd  t|j |r|| n t|j| tjdr|
||fkrwd|k} | | |r|  |d n t|j  t|jd |r@|r@|  |d n t|j  t|jd W d Q R X W d Q R X qW d S )Nc                 s"   t  \} }t |  | dS )Nr   )ospiper[   FileIO)r   r   )r+   r   r   pipe_reader  s    
z3IOTest.test_optional_abilities.<locals>.pipe_readerc                 sN   t  \} } t j|  tjt j| dfd}|   |j  	|dS )Nd   )targetre   r   )
r   r   
addCleanupr[   rO   ThreadrC   startrl   r   )r   r   Zthread)r+   r   r   pipe_writer  s    z3IOTest.test_optional_abilities.<locals>.pipe_writerc                  s       S )N)BufferedReaderrc   r   )r+   r   r   buffered_reader  s    z7IOTest.test_optional_abilities.<locals>.buffered_readerc                  s       S )N)BufferedWriterrc   r   )r+   r   r   buffered_writer  s    z7IOTest.test_optional_abilities.<locals>.buffered_writerc                  s       S )N)BufferedRandomBytesIOr   )r+   r   r   buffered_random  s    z7IOTest.test_optional_abilities.<locals>.buffered_randomc                  s         S )N)BufferedRWPairrc   r   )r+   r   r   buffered_rw_pair  s    
z8IOTest.test_optional_abilities.<locals>.buffered_rw_pairc                 s$   G  fddd j }  |  dS )Nc                   s   e Zd Z jjZ jjZdS )zMIOTest.test_optional_abilities.<locals>.text_reader.<locals>.UnseekableReaderN)r   r   r   BufferedIOBaser/   r.   r   )r+   r   r   UnseekableReader  s   r   ascii)rc   TextIOWrapper)r   )r+   r   r   text_reader  s    z3IOTest.test_optional_abilities.<locals>.text_readerc                 s$   G  fddd j }  |  dS )Nc                   s   e Zd Z jjZ jjZdS )zMIOTest.test_optional_abilities.<locals>.text_writer.<locals>.UnseekableWriterN)r   r   r   r   r2   rC   r   )r+   r   r   UnseekableWriter  s   r   r   )rc   r   )r   )r+   r   r   text_writer  s    z3IOTest.test_optional_abilities.<locals>.text_writerfrfwr   r   Zrwsrw3   3zUnknown base classr#   r7   winrm   r   )r   StringIOsubTestr   r2   r/   
isinstance
TextIOBaser   	RawIOBasefailr1   r   rZ   rC   r.   sysplatform
startswithr3   r6   r5   r<   )r+   r   r   r   r   r   r   r   r   teststestZ	abilitiesr   r2   r/   r   r3   r   )r+   r   test_optional_abilities  sd    	




zIOTest.test_optional_abilitiesc          	   C   sV   d}|  t| j|d t|d}t $ tdt |  t| j|d W d Q R X d S )Nzfoo barr   r   ignore)r   rs   r    r   warningscatch_warningssimplefilterDeprecationWarning)r+   Zfn_with_NULZbytes_fnr   r   r   test_open_handles_NUL_chars  s    

z"IOTest.test_open_handles_NUL_charsc          	   C   s   | j tjddd@}| | d | | d | | d | | W d Q R X | j tjddd@}| | d | | d | | d | | W d Q R X d S )Nr   r   )r   FTr   )	r    r   ry   r   r2   r/   r3   r   r   )r+   r#   r   r   r   test_raw_file_io  s    zIOTest.test_raw_file_ioc          	   C   s   |  tjd@}| | d | | d | | d | | W d Q R X |  tjdB}| | d | | d | | d | |d W d Q R X d S )Nr   FTr   )	r    r   ry   r   r2   r/   r3   r   r   )r+   r#   r   r   r   test_buffered_file_io  s    zIOTest.test_buffered_file_ioc          	   C   s   |  tjd}|d W d Q R X |  tjd~}| | d | |dd | |dd | |d	d
 | | d | |d d | t|jd W d Q R X |  tjd}| t|jd W d Q R X d S )Nr   s"   abc
def
xyzzy
foo bar
another liner   s   abc
r   s   def
rG   s   xy   s   zzy
s   foo bar
s   another lineg333333@r   )r    r   ry   r.   r   r   r   r   )r+   r#   r   r   r   test_readline(  s    zIOTest.test_readlinec             C   s*   G dd d| j }| ttft|  d S )Nc               @   s   e Zd Zdd ZdS )z+IOTest.test_readline_nonsizeable.<locals>.Rc             S   s   d S )Nr   )r+   r   r   r   r   :  s    z4IOTest.test_readline_nonsizeable.<locals>.R.readlineN)r   r   r   r   r   r   r   r   R9  s   r   )IOBaser   r   StopIterationnext)r+   r   r   r   r   test_readline_nonsizeable6  s    z IOTest.test_readline_nonsizeablec             C   s(   G dd d| j }| t| jd d S )Nc               @   s   e Zd Zdd ZdS )z'IOTest.test_next_nonsizeable.<locals>.Rc             S   s   d S )Nr   )r+   r   r   r   __next__B  s    z0IOTest.test_next_nonsizeable.<locals>.R.__next__N)r   r   r   r   r   r   r   r   r   A  s   r   r7   )r   r   r   	readlines)r+   r   r   r   r   test_next_nonsizeable>  s    zIOTest.test_next_nonsizeablec             C   s@   |   }| | | }| |d |  |}| |d d S )Ns   hello world
T)r   r   getvaluer   r   )r+   r#   r   r   r   r   test_raw_bytes_ioF  s    

zIOTest.test_raw_bytes_ioc          	   C   s|   t jd d dkst jdkr.tdd| j  | tjdd}| | W d Q R X | tjd}| | W d Q R X d S )Nr   r   darwinZ	largefilez-test requires %s bytes and a long time to runzw+br   )r   r   r   Zrequiresr   r    ry   r   )r+   r#   r   r   r   test_large_file_opsN  s    zIOTest.test_large_file_opsc          
   C   s   xdD ]}d }|  tjd|}|d W d Q R X | |jd d }y(|  tjd|}dd  W d Q R X W n" tk
r   | |jd Y qX | d qW d S )N)r   r7   r   r   s   xxxTr7   r   z1/0 didn't raise an exception)r    r   ry   r.   r   rY   ZeroDivisionErrorr   )r+   bufsizer#   r   r   r   test_with_open[  s    
zIOTest.test_with_openc          	   C   s   |  tjd}|d W d Q R X | j tjddd}| | d W d Q R X |  tjd}| | d W d Q R X |  tjd}| | d W d Q R X d S )Nr   s   xxxabr   )r   r   a)r    r   ry   r.   r   r6   assertGreater)r+   r#   r   r   r   test_append_mode_tellk  s    zIOTest.test_append_mode_tellc          
      s   g  G  fddd| j }tdtfb |tjd}|d ~t  |  dddg | tjd	}| |	 d W d Q R X W d Q R X d S )
Nc                   s:   e Zd Z fddZ fddZ fddZ  ZS )z(IOTest.test_destructor.<locals>.MyFileIOc                s6    d yt j}W n tk
r*   Y nX |  d S )Nr7   )r-   rH   __del__AttributeError)r+   r#   )rI   recordr   r   r   x  s    
z0IOTest.test_destructor.<locals>.MyFileIO.__del__c                s    d t   d S )NrG   )r-   rH   r[   )r+   )rI   r   r   r   r[     s    
z.IOTest.test_destructor.<locals>.MyFileIO.closec                s    d t   d S )Nr   )r-   rH   rU   )r+   )rI   r   r   r   rU     s    
z.IOTest.test_destructor.<locals>.MyFileIO.flush)r   r   r   r   r[   rU   rK   r   )r   )rI   r   MyFileIOw  s   r   r   r   s   xxxr7   rG   r   r   )
r   r   check_warningsResourceWarningry   r.   
gc_collectr   r    rC   )r+   r   r#   r   )r   r   test_destructoru  s    
zIOTest.test_destructorc                s>   g  G  fddd|}| }~t   |  dddg d S )Nc                   sB   e Zd Zdd Z fddZ fddZ fddZ  ZS )	z+IOTest._check_base_destructor.<locals>.MyIOc             S   s   d| _ d| _d| _d S )Nr7   rG   r   )on_delon_closeon_flush)r+   r   r   r   r,     s    z4IOTest._check_base_destructor.<locals>.MyIO.__init__c                s8    | j yt j}W n tk
r,   Y nX |  d S )N)r-   r   rH   r   r   )r+   r#   )rI   r   r   r   r     s    z3IOTest._check_base_destructor.<locals>.MyIO.__del__c                s    | j t   d S )N)r-   r   rH   r[   )r+   )rI   r   r   r   r[     s    z1IOTest._check_base_destructor.<locals>.MyIO.closec                s    | j t   d S )N)r-   r   rH   rU   )r+   )rI   r   r   r   rU     s    z1IOTest._check_base_destructor.<locals>.MyIO.flush)r   r   r   r,   r   r[   rU   rK   r   )r   )rI   r   MyIO  s   r   r7   rG   r   )r   r   r   )r+   baser   r#   r   )r   r   _check_base_destructor  s    zIOTest._check_base_destructorc             C   s   |  | j d S )N)r   r   )r+   r   r   r   test_IOBase_destructor  s    zIOTest.test_IOBase_destructorc             C   s   |  | j d S )N)r   r   )r+   r   r   r   test_RawIOBase_destructor  s    z IOTest.test_RawIOBase_destructorc             C   s   |  | j d S )N)r   r   )r+   r   r   r   test_BufferedIOBase_destructor  s    z%IOTest.test_BufferedIOBase_destructorc             C   s   |  | j d S )N)r   r   )r+   r   r   r   test_TextIOBase_destructor  s    z!IOTest.test_TextIOBase_destructorc          	   C   sR   |  tjd}|d W d Q R X |  tjd}| | d W d Q R X d S )Nr   s   xxxr   )r    r   ry   r.   r   rC   )r+   r#   r   r   r   test_close_flushes  s    zIOTest.test_close_flushesc                s   t  dtd t   fdd}|  |tjd |	  |
	  |	 	  d S )Nir   c          	      s2   | $  |   |  f W d Q R X d S )N)r   r.   r   )r#   )r   r:   r+   r   r   check  s    z'IOTest.test_array_writes.<locals>.checkr   )r   ranger   tobytesr   r   r   ry   r   r@   r   r   )r+   r   r   )r   r:   r+   r   test_array_writes  s    zIOTest.test_array_writesc             C   s   | j t| jtjddd d S )Nr   F)closefd)r   rs   r    r   ry   )r+   r   r   r   test_closefd  s    zIOTest.test_closefdc          	   C   s   |  tjd}|d W d Q R X |  tjdJ}| j | ddd}| | d |d |  | 	t
|j W d Q R X d S )Nr   zegg
r   F)r   r   )r    r   ry   r.   r1   r   rC   r5   r[   r   rs   )r+   r#   filer   r   r   test_read_closed  s    
zIOTest.test_read_closedc             C   s   | j t| jtjddd d S )Nr   F)r   )r   rs   r    r   ry   )r+   r   r   r   test_no_closefd_with_filename  s    z$IOTest.test_no_closefd_with_filenamec          	   C   sz   |  tjd}|d W d Q R X |  tjd>}| |jjjd | j | ddd}| |jjjd W d Q R X d S )Nr   s   egg
r   TF)r   )	r    r   ry   r.   r   r   rawr   r1   )r+   r#   r   r   r   r   test_closefd_attr  s    zIOTest.test_closefd_attrc          	   C   s   t dtf8 | t jd}|d ||_t|}~t 	  W d Q R X | 
| | | t jd}| | d W d Q R X d S )Nr   r   s   abcxxxr   )r   r   r   r   ry   r.   r#   weakrefrefr   assertIsNoner    r   rC   )r+   r#   wrr   r   r   test_garbage_collection  s    

zIOTest.test_garbage_collectionc          	   C   s   d}t j|s | d| tjdkr4| d tjtj	k rJ| d | j
|ddd}| t|j W d Q R X | 
|d}| t|j W d Q R X | 
|d	}| t|j W d Q R X d S )
Nz	/dev/zeroz{0} does not existiz+test can only run in a 32-bit address spacez&test requires at least 2 GiB of memoryr   r   )r   r   )r   pathexistsr   formatr   maxsizer   Zreal_max_memuseZ_2Gr    r   r   rC   )r+   Zzeror#   r   r   r   test_unbounded_file  s    


zIOTest.test_unbounded_filec                sd   | j ||g   fdd}|_| tj | j |   |  d  dd _d S )Nc                  s   j g d d < t d S )N)rY   rZ   r   )rY   r#   r   r   	bad_flush	  s    z4IOTest.check_flush_error_on_close.<locals>.bad_flushr   c               S   s   d S )Nr   r   r   r   r   <lambda>  rA   z3IOTest.check_flush_error_on_close.<locals>.<lambda>)r    rU   r   rZ   r[   
assertTruerY   assertFalse)r+   re   kwargsr  r   )rY   r#   r   check_flush_error_on_close  s    
z!IOTest.check_flush_error_on_closec             C   s.  | j tjddd ttjtjtjB }| j |ddd ttjtjtjB }| j |dddd t| |  tjd ttjtjtjB }|  |d ttjtjtjB }| j |ddd t| |  tjd ttjtjtjB }|  |d ttjtjtjB }| j |ddd t| d S )Nr   r   )r   F)r   r   )r   r   )r  r   ry   r   r    O_WRONLYO_CREATr[   )r+   fdr   r   r   test_flush_error_on_close  s$    

z IOTest.test_flush_error_on_closec             C   s<   | j tjddd}|  |  |  | t|j d S )Nr   r   )r   )r    r   ry   r[   r   rs   rU   )r+   r#   r   r   r   test_multi_close+  s
    zIOTest.test_multi_closec             C   s   |  d}| |dd | |dd | |dd | |dd  | |dd | |dd | |dd  | |dd d S )	N)s   abc   dNs   efgNrG   s   ab   cr  s   ef   grA   )r%   r   rC   )r+   rawior   r   r   test_RawIOBase_read2  s    
zIOTest.test_RawIOBase_readc             C   sD   |   |  |  |  |  f}x|D ]}| t|d q(W d S )N__dict__)r   r   r   r   r   r  hasattr)r+   r   r   r   r   r   test_types_have_dict?  s    

zIOTest.test_types_have_dictc          	      sp   |  tjd}|d W d Q R X t tjtj  fdd}| j dd|d}| | d W d Q R X d S )Nr   zegg
c                s    S )Nr   )r
  flags)r  r   r   openerN  s    z"IOTest.test_opener.<locals>.openerznon-existentr   )r#  )r    r   ry   r.   r   O_RDONLYr   rC   )r+   r#   r#  r   )r  r   test_openerJ  s    zIOTest.test_openerc          	   C   sB   dd }|  t}tdd|d W d Q R X | t|jd d S )Nc             S   s   dS )Nrq   r   )fnamer"  r   r   r   	badopenerU  s    z4IOTest.test_bad_opener_negative_1.<locals>.badopenerznon-existentr   )r#  zopener returned -1)r   rs   r    r   str	exception)r+   r'  cmr   r   r   test_bad_opener_negative_1S  s    z!IOTest.test_bad_opener_negative_1c          	   C   sB   dd }|  t}tdd|d W d Q R X | t|jd d S )Nc             S   s   dS )Nr   )r&  r"  r   r   r   r'  ]  s    z8IOTest.test_bad_opener_other_negative.<locals>.badopenerznon-existentr   )r#  zopener returned -2)r   rs   r    r   r(  r)  )r+   r'  r*  r   r   r   test_bad_opener_other_negative[  s    z%IOTest.test_bad_opener_other_negativec          
   C   sp   |  tdZ}|  tdB}| j| dd}|j| dd |  |  |  W d Q R X W d Q R X d S )Nr   F)r   )r    r!   r   r1   r,   r   r[   )r+   f1f2Zfileior   r   r   test_fileio_closefdc  s    zIOTest.test_fileio_closefdc          
   C   sB   t | . | t | jt jddd W d Q R X W d Q R X d S )Nr   r   )r   )r   check_no_resource_warningr   rs   r    ry   )r+   r   r   r   test_nonbuffered_textioo  s    zIOTest.test_nonbuffered_textioc          
   C   sB   t | . | t | jt jddd W d Q R X W d Q R X d S )Nr   Zinvalid)newline)r   r1  r   rs   r    ry   )r+   r   r   r   test_invalid_newlinet  s    zIOTest.test_invalid_newlinec          
   C   sn   G dd d| j }| }xPdD ]H}| |4 td}| t|||d | t|d W d Q R X qW d S )Nc               @   s   e Zd Zdd ZeZdS )z3IOTest.test_buffered_readinto_mixin.<locals>.Streamc             S   s   dS )Ns   12345r   )r+   sizer   r   r   rC   |  s    z8IOTest.test_buffered_readinto_mixin.<locals>.Stream.readN)r   r   r   rC   read1r   r   r   r   Stream{  s   r7  )r;   r   rJ   s   12345)r   r   r   r   getattrr   )r+   r7  streammethodr   r   r   r   test_buffered_readinto_mixiny  s    
z#IOTest.test_buffered_readinto_mixinc          
      s    fdd}|t tj |t tjd  tjd4}t | } t  |d W d Q R X W d Q R X t d } t  |d W d Q R X t t} t  |d W d Q R X  	t
d  t tjd W d Q R X d S )Nc          	      sN     | d}|d W d Q R X   | d} | d W d Q R X d S )Nr   zegg
r   )r    r.   r   rC   )r
  r#   )r+   r   r   check_path_succeeds  s    z7IOTest.test_fspath_support.<locals>.check_path_succeedszutf-8r   zread/write/append modeZrwxa)r	   r   ry   encoder    r1   r   r   FloatingPointErrorassertRaisesRegexrs   )r+   r<  r#   Zbad_pathr   )r+   r   test_fspath_support  s     zIOTest.test_fspath_supportc             C   s8   |  d}| | d |  d}| | d d S )N)s   abcr  s   efgs   abcdefg)r%   r   rC   readall)r+   r  r   r   r   test_RawIOBase_readall  s    

zIOTest.test_RawIOBase_readallc          
   C   s   G dd d| j }d}d}x|D ]}| | |\}}}}|tt|}	t|f| }
t|	|}| ||
| | t|
| | |
d | t| |f||  }| |
|d  | | t|	j	||  W d Q R X q W d S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z3IOTest.test_BufferedIOBase_readinto.<locals>.Readerc             S   s
   || _ d S )N)avail)r+   rC  r   r   r   r,     s    z<IOTest.test_BufferedIOBase_readinto.<locals>.Reader.__init__c             S   s"   | j d | }| j |d  | _ |S )N)rC  )r+   r5  resultr   r   r   rC     s    z8IOTest.test_BufferedIOBase_readinto.<locals>.Reader.readc             S   s   |  t|dS )z$Returns no more than 5 bytes at oncerJ   )rC   min)r+   r5  r   r   r   r6    s    z9IOTest.test_BufferedIOBase_readinto.<locals>.Reader.read1N)r   r   r   r,   rC   r6  r   r   r   r   Reader  s   rF  )
)r;   r   rJ   rJ   )r;   r   r|   r|   )r;   rJ   r|   rJ   )r;   r|      r|   )r;   r   r   r   )r   r   rJ   rJ   )r   r   r|   rJ   )r   rJ   r|   rJ   )r   r|   rG  rJ   )r   r   r   r      )
r   r   r   r   r   r8  r   r   ZassertSequenceEqualrC  )r+   rF  r   ZUNUSED_BYTEr   r:  rC  ZrequestrD  readerr   Zunusedr   r   r   test_BufferedIOBase_readinto  s    

z#IOTest.test_BufferedIOBase_readintoc             C   s*   G dd d| j }| }| t|j d S )Nc               @   s   e Zd Zdd Zdd ZdS )z#IOTest.test_close_assert.<locals>.Rc             S   s   d S )Nr   )r+   namevaluer   r   r   __setattr__  s    z/IOTest.test_close_assert.<locals>.R.__setattr__c             S   s
   t  d S )N)rZ   )r+   r   r   r   rU     s    z)IOTest.test_close_assert.<locals>.R.flushN)r   r   r   rM  rU   r   r   r   r   r     s   r   )r   r   rZ   r[   )r+   r   r#   r   r   r   test_close_assert  s    zIOTest.test_close_assertN)F)3r   r   r   rz   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  r	  r  r  r  r  r  r!  r%  r+  r-  r0  r2  r4  r;  r@  rB  rJ  rN  r   r   r   r   rw   1  s`   
!h	

	)rw   c               @   s   e Zd Zdd ZdS )CIOTestc             C   sL   G dd d| j }|  | }||_t|}~~t  | | | d S )Nc               @   s   e Zd Zdd ZdS )z*CIOTest.test_IOBase_finalize.<locals>.MyIOc             S   s   d S )Nr   )r+   r   r   r   r[     s    z0CIOTest.test_IOBase_finalize.<locals>.MyIO.closeN)r   r   r   r[   r   r   r   r   r     s   r   )r   r   r  r  r   r   r  )r+   r   r   r  r   r   r   test_IOBase_finalize  s    
zCIOTest.test_IOBase_finalizeN)r   r   r   rP  r   r   r   r   rO    s   rO  c               @   s   e Zd ZdS )PyIOTestN)r   r   r   r   r   r   r   rQ    s   rQ  c               @   s   e Zd Zdd Zdd ZdS )APIMismatchTestc             C   s*   t jtjtjdd}| j|t dd dS )z:Test that pyio RawIOBase class has all c RawIOBase methods)__weakref__)r   z6Python RawIOBase does not have all C RawIOBase methods)msgN)r   detect_api_mismatchri   r   rg   r   rR   )r+   mismatchr   r   r   test_RawIOBase_io_in_pyio_match  s    z/APIMismatchTest.test_RawIOBase_io_in_pyio_matchc             C   s&   t tjtj}| j|t dd dS )z:Test that c RawIOBase class has all pyio RawIOBase methodsz6C RawIOBase does not have all Python RawIOBase methods)rT  N)r   rU  rg   r   ri   r   rR   )r+   rV  r   r   r   test_RawIOBase_pyio_in_io_match  s    z/APIMismatchTest.test_RawIOBase_pyio_in_io_matchN)r   r   r   rW  rX  r   r   r   r   rR    s   rR  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S )CommonBufferedTestsc             C   s<   |   }| |}| | | | t|j t| d S )N)r@   tpassertIsdetachr   rs   repr)r+   r  r9   r   r   r   test_detach  s
    
zCommonBufferedTests.test_detachc             C   s&   |   }| |}| d|  d S )Nr0   )r@   rZ  r   r1   )r+   r  bufior   r   r   test_fileno  s    
zCommonBufferedTests.test_filenoc             C   s:   |   }| |}| t|jdd | t|jdd d S )Nr   rq   r}   )r@   rZ  r   rs   r5   )r+   r  r_  r   r   r   test_invalid_args  s    
z%CommonBufferedTests.test_invalid_argsc                sN   | j }g  G  fddd|}|  }||}~t  |  dddg d S )Nc                   s:   e Zd Z fddZ fddZ fddZ  ZS )zBCommonBufferedTests.test_override_destructor.<locals>.MyBufferedIOc                s6    d yt j}W n tk
r*   Y nX |  d S )Nr7   )r-   rH   r   r   )r+   r#   )rI   r   r   r   r   !  s    
zJCommonBufferedTests.test_override_destructor.<locals>.MyBufferedIO.__del__c                s    d t   d S )NrG   )r-   rH   r[   )r+   )rI   r   r   r   r[   )  s    
zHCommonBufferedTests.test_override_destructor.<locals>.MyBufferedIO.closec                s    d t   d S )Nr   )r-   rH   rU   )r+   )rI   r   r   r   rU   ,  s    
zHCommonBufferedTests.test_override_destructor.<locals>.MyBufferedIO.flush)r   r   r   r   r[   rU   rK   r   )r   )rI   r   MyBufferedIO   s   rb  r7   rG   r   )rZ  r@   r   r   r   )r+   rZ  rb  r  r_  r   )r   r   test_override_destructor  s    z,CommonBufferedTests.test_override_destructorc                s4   |   }| |  fdd}|  | t| d S )Nc            	      s     W d Q R X d S )Nr   r   )r_  r   r   _with9  s    z7CommonBufferedTests.test_context_manager.<locals>._with)r@   rZ  r   rs   )r+   r  rd  r   )r_  r   test_context_manager5  s
    
z(CommonBufferedTests.test_context_managerc          	      s       fdd}td}t| W d Q R X |  }|rt|	 d 
|d| 
|d| d S )Nc                  s     j d S )N)rZ  xyzzyr   )r  r+   r   r   r#   E  s    z<CommonBufferedTests.test_error_through_destructor.<locals>.fstderrr7   zException OSError: z ignored)rX   r   captured_outputr   r   r   stripr   r   
splitlinesr  r   endswith)r+   r#   rm   r   )r  r+   r   test_error_through_destructorA  s    z1CommonBufferedTests.test_error_through_destructorc             C   sr   |   }| |}d| jj| jjf }| t|d|  d|_| t|d|  d|_| t|d|  d S )Nz	(%s\.)?%sz<%s>dummyz<%s name='dummy'>s   dummyz<%s name=b'dummy'>)r@   rZ  r   r   assertRegexr]  rK  )r+   r  r
   Zclsnamer   r   r   	test_reprP  s    
zCommonBufferedTests.test_reprc          	   C   sR   |   }| |}t|d|( yt| W n tk
rB   Y nX W d Q R X d S )NrK  )r@   rZ  r   	swap_attrr]  RuntimeError)r+   r  r
   r   r   r   test_recursive_reprZ  s    
z'CommonBufferedTests.test_recursive_reprc                s   |   g  fdd}|_|  | t j |  j | j |  | d  | d  dd _d S )Nc                  s    j j gd d < t d S )N)rY   rZ   r   )r
   rY   r  r   r   r  i  s    z@CommonBufferedTests.test_flush_error_on_close.<locals>.bad_flushr   r7   c               S   s   d S )Nr   r   r   r   r   r  t  rA   z?CommonBufferedTests.test_flush_error_on_close.<locals>.<lambda>)	r@   rU   rZ  r   rZ   r[   r  rY   r  )r+   r  r   )r
   rY   r  r   r  d  s    

z-CommonBufferedTests.test_flush_error_on_closec          	   C   s   |   }dd }dd }||_| |}||_| t}|  W d Q R X | |jjd | 	|jj
t | |jj
jd | |j d S )Nc               S   s   t dd S )NrU   )rZ   r   r   r   r   r  x  s    z@CommonBufferedTests.test_close_error_on_close.<locals>.bad_flushc               S   s   t dd S )Nr[   )rZ   r   r   r   r   	bad_closez  s    z@CommonBufferedTests.test_close_error_on_close.<locals>.bad_close)r[   )rU   )r@   r[   rZ  rU   r   rZ   r   r)  re   assertIsInstance__context__r  rY   )r+   r  r  rs  r
   errr   r   r   test_close_error_on_closev  s    
z-CommonBufferedTests.test_close_error_on_closec          	   C   s   |   }dd }dd }||_| |}||_| t}|  W d Q R X | dt|j | 	|jj
t | dt|jj
 | |j d S )Nc               S   s   t d S )N)non_existing_flushr   r   r   r   r    s    zNCommonBufferedTests.test_nonnormalized_close_error_on_close.<locals>.bad_flushc               S   s   t d S )N)non_existing_closer   r   r   r   rs    s    zNCommonBufferedTests.test_nonnormalized_close_error_on_close.<locals>.bad_closery  rx  )r@   r[   rZ  rU   r   	NameErrorassertInr(  r)  rt  ru  r  rY   )r+   r  r  rs  r
   rv  r   r   r   'test_nonnormalized_close_error_on_close  s    
z;CommonBufferedTests.test_nonnormalized_close_error_on_closec             C   s<   |   }| |}|  |  |  | t|j d S )N)r@   rZ  r[   r   rs   rU   )r+   r  r
   r   r   r   r    s    
z$CommonBufferedTests.test_multi_closec             C   s6   |  | d}| | j|j | | j|jd d S )Ns
   AAAAAAAAAAr   )rZ  rc   r   rd   r6   r5   )r+   r_  r   r   r   test_unseekable  s    z#CommonBufferedTests.test_unseekablec          	   C   s:   |   }| |}|   }| t ||_W d Q R X d S )N)r@   rZ  r   r   r  )r+   r  r9   xr   r   r   test_readonly_attributes  s
    
z,CommonBufferedTests.test_readonly_attributesN)r   r   r   r^  r`  ra  rc  re  rl  ro  rr  r  rw  r|  r  r}  r  r   r   r   r   rY    s   

rY  c               @   s(   e Zd Zejdd Zejdd ZdS )
SizeofTestc             C   s\   d}d}|   }| j||d}t|| }|   }| j||d}| t|||  d S )Ni   i    )buffer_size)r@   rZ  r   	getsizeofr   )r+   Zbufsize1Zbufsize2r  r_  r5  r   r   r   test_sizeof  s    zSizeofTest.test_sizeofc             C   sF   d}|   }| j||d}t|| }|  | t|| d S )Ni   )r  )r@   rZ  r   r  r[   r   )r+   r   r  r_  r5  r   r   r   test_buffer_freeing  s    zSizeofTest.test_buffer_freeingN)r   r   r   r   cpython_onlyr  r  r   r   r   r   r    s   r  c               @   s   e Zd 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eddd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*BufferedReaderTestr   c             C   s   |  dg}| |}|| |j|dd |j|dd | d|  | jt|j|dd | jt|j|dd | jt|j|dd |  dg}|| | d|  d S )Ns   abci   )r     r   irq   )r@   rZ  r,   r   rC   r   rs   )r+   r  r_  r   r   r   test_constructor  s    


z#BufferedReaderTest.test_constructorc             C   sX   | j | j }~| j | j }| ttfd|jd ||   | |dd d S )Nzuninitialized|has no attributer   rA   )	rZ  __new__r?  rs   r   rC   r,   r@   r   )r+   r_  r   r   r   test_uninitialized  s    

z%BufferedReaderTest.test_uninitializedc             C   sH   x2dD ]*}|  d}| |}| d|| qW | t|jd d S )N)NrG  )s   abcr  s   efgs   abcdefgr,  )r@   rZ  r   rC   r   rs   )r+   argr  r_  r   r   r   	test_read  s
    


zBufferedReaderTest.test_readc             C   s   |  d}| |}| d|d | d|d | |jd | d|d | d|d | |jd | d	|d | |jd
 | d|d | |jd | d|d | |jd d S )N)s   abcr  s   efg   ar7      brA   r   r  r   r  rG   s   efgr   r   )r@   rZ  r   rC   r6  r)   )r+   r  r_  r   r   r   
test_read1  s    

zBufferedReaderTest.test_read1c             C   s   |  d}| |}| d|d | d|  | d|  | d|d | |jd | d	|  | |jd
 d S )N)s   abcr  s   efgr  r7   s   bcr  s   efgrq   r   rA   r   )r@   rZ  r   rC   r6  r)   )r+   r  r_  r   r   r   test_read1_arbitrary  s    

z'BufferedReaderTest.test_read1_arbitraryc             C   s  |  d}| |}td}| ||d | |d | ||d | |d | ||d | |d | ||d | |d | ||d | |d |  d	}| |}| ||d | |d | ||d | |d
 d S )N)s   abcr  s   efgrG   s   abs   cds   efr7   s   gfr   )s   abcNs   cb)r@   rZ  r   r   r;   )r+   r  r_  r
   r   r   r   test_readinto   s&    



z BufferedReaderTest.test_readintoc             C   s4  d}|  d}| j||d}td}| |dd | |jd | ||d | |d | |jd | ||d | |d d d	 | |jd | ||d | |d
 | |jd td| }| |dd | |jd | ||d | |d d d | |jd d S )Nr   )s   abcs   des   fghs   jkl)r  rG   r   s   abcr7   s   abr  s   des   fghr|   s   fghjklr   )r@   rZ  r   r   peekr)   r   )r+   r  r  r_  r
   r   r   r   test_readinto1  s*    
z!BufferedReaderTest.test_readinto1c             C   s   d}d}|  |f}| j||d}tdd}||}| |t| t|d}| |t| | 	|d | |d |  | 	||d  dt||d    d S )N<   s   aaaaaaaaaaaaaaaaaaaaaaaaaa)r  r   s    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxr   r   )
r@   rZ  r   r;   r   r   r   r   
assertLessr   )r+   r  r   r  r_  r
   r:   bmr   r   r   test_readinto_array,  s    
z&BufferedReaderTest.test_readinto_arrayc             C   s   d}d}|  |f}| j||d}tdd}||}| |t| t|d}| |t| | 	|d | |d |  | 	||d  dt||d    d S )Nr  s   aaaaaaaaaaaaaaaaaaaaaaaaaa)r  r   s    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxr   r   )
r@   rZ  r   r   r   r   r   r   r  r   )r+   r  r   r  r_  r
   r:   r  r   r   r   test_readinto1_arrayA  s    
z'BufferedReaderTest.test_readinto1_arrayc                sZ    fdd}  |  dddg   | dddg   | d dddg d S )Nc                 s     d}  | S )N)s   abc
s   d
s   ef)r@   rZ  )r  )r+   r   r   r_  W  s    
z0BufferedReaderTest.test_readlines.<locals>.bufios   abc
s   d
s   efrJ   )r   r   )r+   r_  r   )r+   r   test_readlinesV  s    z!BufferedReaderTest.test_readlinesc          	   C   s   d}t |}dddddg|dggddddg|ggdddddgdddggg}xp|D ]h\}}}| |}| j||d	}d}	x2|D ]*}
| ||
||	|	|
   |	|
7 }	q|W | |j| qPW d S )
Ns	   abcdefghir   r   r7   r      r   rG   )r  )r   r^   rZ  r   rC   r_   )r+   r   Zdlenr   r   Zbuf_read_sizesZraw_read_sizesr  r_  r   nbytesr   r   r   test_buffering^  s    

z!BufferedReaderTest.test_bufferingc             C   s   |  d}| |}| d|d | d|d | d|  | d|d | |  | d|  |  d}| d	|  | |  d S )
N)s   abcr  Ns   efgNNNs   abcdr|      er7   s   fgrA   )r  NNr  )r@   rZ  r   rC   r  r  rA  )r+   r  r_  r   r   r   test_read_non_blockingr  s    


z)BufferedReaderTest.test_read_non_blockingc             C   s*   |  d}| |}| d|d d S )N)s   abcr  s   efgs   abcdefgi(#  )r@   rZ  r   rC   )r+   r  r_  r   r   r   test_read_past_eof  s    

z%BufferedReaderTest.test_read_past_eofc             C   s(   |  d}| |}| d|  d S )N)s   abcr  s   efgs   abcdefg)r@   rZ  r   rC   )r+   r  r_  r   r   r   test_read_all  s    

z BufferedReaderTest.test_read_allcpuc          
      s.  zd}t td| }t| tt|}| tjd	| W d Q R X | jtj| j
dd}| |d g g  fddfd	d
tdD }t| td W d Q R X | d  d}x0tdD ]$}tt|g}| ||| qW W d Q R X W d ttj X d S )Ni     r   r   )r   r  c           
      sh   y4x.t ddgD ]}  | }|s$P | qW W n. tk
rb } z|  W d d }~X Y nX d S )Nr7      )r   rC   r-   	Exception)r:   rm   e)r_  errorsresultsr   r   r#     s    

z*BufferedReaderTest.test_threads.<locals>.fc                s   g | ]}t j d qS ))r   )rO   r   ).0r~  )r#   r   r   
<listcomp>  s    z3BufferedReaderTest.test_threads.<locals>.<listcomp>r   g{Gz?z(the following exceptions were caught: %rrA   )r&   r   randomZshuffler   r   r    r   ry   r.   	read_moderZ  start_threadsrS   rT   r  rl   r   r   rx   )r+   Nlrm   r  threadsr   cr   )r_  r  r#   r  r   test_threads  s,    


$zBufferedReaderTest.test_threadsc             C   sb   |  | d}| | j|j | | j|jd |d | | j|jd | | j|j d S )Ns
   AAAAAAAAAAr   r7   )rZ  rc   r   rd   r6   r5   rC   )r+   r_  r   r   r   r}    s    
z"BufferedReaderTest.test_unseekablec             C   s6   |  d}| |}| t|jd | t|j d S )N)s   abcr  s   efgr   )rF   rZ  r   rZ   r5   r6   )r+   r  r_  r   r   r   test_misbehaved_io  s    

z%BufferedReaderTest.test_misbehaved_ioc          	   C   s   d}xd|d ||d |d fD ]}|  d| g}| ||}| ||d|  | |jdd||j |  d|d  dg}| ||}| ||d|  | |jdd||j q W d S )Nr  rG   r7   r   r   zfailed for {}: {} != 0)r@   rZ  r   rC   r*   r  )r+   r   r:   r  r_  r   r   r   test_no_extraneous_read  s     

z*BufferedReaderTest.test_no_extraneous_readc             C   sD   t t d}|d |  | t|j | t|jd d S )Ns   12r7   )	rg   r   r   rC   r[   r   rs   r  r6  )r+   r
   r   r   r   test_read_on_closed  s
    
z&BufferedReaderTest.test_read_on_closedN)r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   requires_resourcer  r}  r  r  r  r   r   r   r   r    s(   
(r  c               @   sF   e Zd ZejZeeddd Z	dd Z
dd Zdd	 Zd
d ZdS )CBufferedReaderTestzGMSan defaults to crashing instead of returning NULL for malloc failure.c             C   sD   t |  tjdkr@|  }| |}| ttt	f|j
|tj d S )Ni)r  r  r   r  r@   rZ  r   r   MemoryErrorrs   r,   )r+   r  r_  r   r   r   r    s    


z$CBufferedReaderTest.test_constructorc             C   s   |  dg}| |}| jt|j|dd | t|j | jt|j|dd | t|j | jt|j|dd | t|j d S )Ns   abcr   )r  irq   )r@   rZ  r   rs   r,   rC   )r+   r  r_  r   r   r   test_initialization  s    
z'CBufferedReaderTest.test_initializationc             C   s(   |  d}| |}| t|jd d S )N)s   abcr  s   efgr   )rF   rZ  r   rZ   rC   )r+   r  r_  r   r   r   test_misbehaved_io_read  s    

z+CBufferedReaderTest.test_misbehaved_io_readc          	   C   sn   |  tjtj tdtf8 | tjd}| |}||_t	
|}~t  W d Q R X | | | d S )Nr   zw+b)r   r   rx   ry   r   r   r   rZ  r#   r  r  r   r  )r+   r  r#   r  r   r   r   r	     s    

z+CBufferedReaderTest.test_garbage_collectionc          	   C   s0   |  td | t ddd W d Q R X d S )Nr   i   )r?  r   rZ  rg   r   )r+   r   r   r   test_args_error  s    z#CBufferedReaderTest.test_args_errorN)r   r   r   rg   r   rZ  unittestskipIfMEMORY_SANITIZERr  r  r  r	  r  r   r   r   r   r    s   
r  c               @   s   e Zd ZejZdS )PyBufferedReaderTestN)r   r   r   ri   r   rZ  r   r   r   r   r    s   r  c               @   s   e Zd 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ed(d)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3S )4BufferedWriterTestr   c             C   s   |   }| |}|| |j|dd |j|dd | d|d |  | jt|j|dd | jt|j|dd | jt|j|dd || | d|d	 |  | d
|j	d d S )Ni   )r  r  r   s   abcr   irq   s   ghirA   s   abcghi)
r@   rZ  r,   r   r.   rU   r   rs   rl   r(   )r+   r  r_  r   r   r   r    s    


z#BufferedWriterTest.test_constructorc             C   sX   | j | j }~| j | j }| ttfd|jd ||   | |dd d S )Nzuninitialized|has no attributerA   r   )	rZ  r  r?  rs   r   r.   r,   r@   r   )r+   r_  r   r   r   r  *  s    

z%BufferedWriterTest.test_uninitializedc             C   sD   |   }| |}|d | |j |  | |jdg d S )Ns   howdy!)r@   rZ  r.   r  r(   r\  r   )r+   r  r9   r   r   r   test_detach_flush4  s    

z$BufferedWriterTest.test_detach_flushc             C   sh   |   }| |d}|d | |j td}|| d|d d < |  | d|jd d S )Nr  s   abcs   defs   ***rA   s   abcdef)	r@   rZ  r.   r  r(   r   rU   r   rl   )r+   writerr_  r   r   r   r   
test_write<  s    

zBufferedWriterTest.test_writec             C   sr   |   }| |d}d}x.tdt|dD ]}||||d   q*W d|j}| ||d d | d S )Nr  s   abcdefghijklmnopr   r   rA   i)	r@   rZ  r   r   r.   rl   r(   r  r   )r+   r  r_  contentsr:   Zflushedr   r   r   test_write_overflowH  s    z&BufferedWriterTest.test_write_overflowc       	      C   s   t tdd }d}|  }| |d}dd }| }xT|t|k rtt|t|| }| |||||  | || ||7 }q8W |	  | |d
|j d S )Nr  i  r   r   c              s   s.   x(t dD ]} xtdD ]
}| V  qW q
W d S )Nr7      )r   r   )r5  r   r   r   r   	gen_sizesZ  s    z2BufferedWriterTest.check_writes.<locals>.gen_sizesrA   )r   r   r@   rZ  r   rE  r   r   r.   rU   rl   r(   )	r+   Zintermediate_funcr  r:   r  r_  r  sizesr5  r   r   r   check_writesS  s    zBufferedWriterTest.check_writesc             C   s   |  dd  d S )Nc             S   s   d S )Nr   )r_  r   r   r   r  h  rA   z0BufferedWriterTest.test_writes.<locals>.<lambda>)r  )r+   r   r   r   test_writesg  s    zBufferedWriterTest.test_writesc             C   s   |  dd  d S )Nc             S   s   |   S )N)rU   )r_  r   r   r   r  k  rA   z<BufferedWriterTest.test_writes_and_flushes.<locals>.<lambda>)r  )r+   r   r   r   test_writes_and_flushesj  s    z*BufferedWriterTest.test_writes_and_flushesc             C   s(   dd }|  | dd }|  | d S )Nc             S   s8   |   }| |d d | |d d | |d d S )Nr7   r   )r6   r5   )r_  r   r   r   r   _seekabsn  s    z:BufferedWriterTest.test_writes_and_seeks.<locals>._seekabsc             S   s4   |  dd}|  dd |  dd |  |d d S )Nr   r7   rq   )r5   )r_  r   r   r   r   _seekrelt  s    z:BufferedWriterTest.test_writes_and_seeks.<locals>._seekrel)r  )r+   r  r  r   r   r   test_writes_and_seeksm  s    
z(BufferedWriterTest.test_writes_and_seeksc             C   s   |  dd  d S )Nc             S   s   |  |  S )N)r<   r6   )r_  r   r   r   r  |  rA   z>BufferedWriterTest.test_writes_and_truncates.<locals>.<lambda>)r  )r+   r   r   r   test_writes_and_truncates{  s    z,BufferedWriterTest.test_writes_and_truncatesc          
   C   s   |   }| |d}| |dd | |dd |d | |dd |d y|d	 W n* | jk
r } z
|j}W d d }~X Y nX | d
 | |d | | d | |dd | }| 	|
d| d S )Nr  s   abcdr   s   efghirJ      ks   jklmn   0s   opqrwxyz0123456789z'BlockingIOError should have been raisedr  s   abcdefghijklmnopqrwxyzs	   ABCDEFGHIr}   s	   01234567A)rj   rZ  r   r.   rp   ru   characters_writtenr   rn   r  r   )r+   r  r_  r  writtenrm   r   r   r   test_write_non_blocking~  s$    



z*BufferedWriterTest.test_write_non_blockingc             C   s   t  }| |d}| |dd | | d |dd | |dd |dd | | d | |dd |  | | d	 d S )
Nr   s   abcdefr|   r   s   XYrG   s   XYcdefs   123456s   XYcdef123456)	rg   r   rZ  r   r.   r6   r5   r   rU   )r+   r  r_  r   r   r   test_write_and_rewind  s    z(BufferedWriterTest.test_write_and_rewindc             C   s<   |   }| |d}|d |  | d|jd  d S )Nr  s   abcr   )r@   rZ  r.   rU   r   r(   )r+   r  r_  r   r   r   
test_flush  s
    
zBufferedWriterTest.test_flushc             C   sH   dddg}|   }| |d}|| |  | d|jd d S )Ns   abs   cds   efr  rA   s   abcdef)r@   rZ  r   rU   r   rl   r(   )r+   r  r  r_  r   r   r   test_writelines  s    

z"BufferedWriterTest.test_writelinesc             C   sL   t dddg}|  }| |d}|| |  | d|jd d S )Ns   abs   cds   efr  rA   s   abcdef)r   r@   rZ  r   rU   r   rl   r(   )r+   r  r  r_  r   r   r   test_writelines_userlist  s    
z+BufferedWriterTest.test_writelines_userlistc             C   sN   |   }| |d}| t|jdddg | t|jd  | t|jd d S )Nr  r7   rG   r   abc)r@   rZ  r   r   r   )r+   r  r_  r   r   r   test_writelines_error  s
    z(BufferedWriterTest.test_writelines_errorc             C   s>   |   }| |d}|d ~t  | d|jd  d S )Nr  s   abcr   )r@   rZ  r.   r   r   r   r(   )r+   r  r_  r   r   r   r     s    
z"BufferedWriterTest.test_destructorc          	   C   s   |  tjtj | jtj| jdd>}| |d}|d | |	dd | |
 d W d Q R X | jtjddd}| | d W d Q R X d S )	Nr   )r   r  s   abcdefr   r|   r   s   abc)r   r   rx   ry   r    
write_moderZ  r.   r   r<   r6   rC   )r+   r  r_  r#   r   r   r   test_truncate  s    
z BufferedWriterTest.test_truncatec          
   C   s   |  tjtj | tjd}|dd  W d Q R X dddg}xb|D ]Z}| jtjd|d>}|d|d	   |d	 |  | |	 |d
  W d Q R X qHW d S )Nr       i'  i    i      zr+b)r   r7   rG   )
r   r   rx   ry   r    r.   rC   r<   r   r6   )r+   r#   Zbuffer_sizesr  r   r   r   test_truncate_after_write  s    


z,BufferedWriterTest.test_truncate_after_writer  c       
   
      sZ  zDd}t td| }tddg}d}t x6|t|k rdt|}||||   ||7 }q0W ~| jtj	| j
ddt}| |d g  fdd	fd
dtdD }t| td W d Q R X | d     W d Q R X | tj	d }W d Q R X x*tdD ]}	| |t |	g| q"W W d ttj	 X d S )Ni  r  r7   r  r   )r   r  c           
      sj   y6x0y  } W n tk
r$   d S X  |  qW W n. tk
rd } z|  W d d }~X Y nX d S )N)popleftr8   r.   r  r-   )rm   r  )r_  r  queuer   r   r#      s    
z*BufferedWriterTest.test_threads.<locals>.fc                s   g | ]}t j d qS ))r   )rO   r   )r  r~  )r#   r   r   r    s    z3BufferedWriterTest.test_threads.<locals>.<listcomp>r   g{Gz?z(the following exceptions were caught: %rr   )r   r   r   r   r   r   r-   r    r   ry   r  rZ  r  rS   rT   r  r[   rC   r   r   rx   )
r+   r  r  r  r:   r5  r  r  rm   r   r   )r_  r  r#   r  r   r    s4    
"zBufferedWriterTest.test_threadsc             C   sF   |   }| |d}| t|jd | t|j | t|jd d S )NrJ   r   s   abcdef)rF   rZ  r   rZ   r5   r6   r.   )r+   r  r_  r   r   r   r    s
    z%BufferedWriterTest.test_misbehaved_ioc          	   C   s,   |  t | |  dd W d Q R X d S )Nr  r   )r   r   rZ  r@   )r+   r   r   r   test_max_buffer_size_removal  s    z/BufferedWriterTest.test_max_buffer_size_removalc             C   sH   |   }dd }||_| |}|d | t|j | |j d S )Nc             S   s
   t  d S )N)rZ   )r
   r   r   r   	bad_write%  s    z?BufferedWriterTest.test_write_error_on_close.<locals>.bad_writes   spam)r@   r.   rZ  r   rZ   r[   r  rY   )r+   r  r  r
   r   r   r   test_write_error_on_close#  s    

z,BufferedWriterTest.test_write_error_on_closec             C   s\   |   }| |d}tj|jd}|  |j  | t	|j
d | |j |  d S )Nr  )r   s   spam)rN   rZ  rO   r   r[   r   rQ   waitr   rs   r.   r  rY   rl   )r+   r  r_  tr   r   r   test_slow_close_from_thread-  s    
z.BufferedWriterTest.test_slow_close_from_threadN)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  r  r   r   r   r   r    s2   
/
r  c               @   s>   e Zd ZejZeeddd Z	dd Z
dd Zdd	 Zd
S )CBufferedWriterTestzGMSan defaults to crashing instead of returning NULL for malloc failure.c             C   sD   t |  tjdkr@|  }| |}| ttt	f|j
|tj d S )Ni)r  r  r   r  r@   rZ  r   r   r  rs   r,   )r+   r  r_  r   r   r   r  =  s    


z$CBufferedWriterTest.test_constructorc             C   s   |   }| |}| jt|j|dd | t|jd | jt|j|dd | t|jd | jt|j|dd | t|jd d S )Nr   )r  s   defirq   )r@   rZ  r   rs   r,   r.   )r+   r  r_  r   r   r   r  I  s    
z'CBufferedWriterTest.test_initializationc          	   C   s   |  tjtj tdtfB | tjd}| |}|d ||_	t
|}~t  W d Q R X | | | | tjd}| | d W d Q R X d S )Nr   zw+bs   123xxxr   )r   r   rx   ry   r   r   r   rZ  r.   r~  r  r  r   r  r    r   rC   )r+   r  r#   r  r   r   r   r	  S  s    


z+CBufferedWriterTest.test_garbage_collectionc          	   C   s0   |  td | t ddd W d Q R X d S )Nr   i   )r?  r   rZ  rg   r   )r+   r   r   r   r  d  s    z#CBufferedWriterTest.test_args_errorN)r   r   r   rg   r   rZ  r  r  r  r  r  r	  r  r   r   r   r   r  :  s
   
r  c               @   s   e Zd ZejZdS )PyBufferedWriterTestN)r   r   r   ri   r   rZ  r   r   r   r   r  j  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)d* Zd+S ),BufferedRWPairTestc             C   s$   |  |  |  }| |j d S )N)rZ  r@   r  rY   )r+   pairr   r   r   r  o  s    z#BufferedRWPairTest.test_constructorc             C   s   | j | j }~| j | j }| ttfd|jd | ttfd|jd ||  |   | 	|dd | 	|dd d S )Nzuninitialized|has no attributer   rA   )
rZ  r  r?  rs   r   rC   r.   r,   r@   r   )r+   r  r   r   r   r  s  s    



z%BufferedRWPairTest.test_uninitializedc             C   s(   |  |  |  }| | j|j d S )N)rZ  r@   r   rd   r\  )r+   r  r   r   r   r^    s    zBufferedRWPairTest.test_detachc          	   C   s2   |  t | |  |  dd W d Q R X d S )Nr  r   )r   r   rZ  r@   )r+   r   r   r   (test_constructor_max_buffer_size_removal  s    z;BufferedRWPairTest.test_constructor_max_buffer_size_removalc             C   s,   G dd dt }| t| j| |    d S )Nc               @   s   e Zd Zdd ZdS )zJBufferedRWPairTest.test_constructor_with_not_readable.<locals>.NotReadablec             S   s   dS )NFr   )r+   r   r   r   r2     s    zSBufferedRWPairTest.test_constructor_with_not_readable.<locals>.NotReadable.readableN)r   r   r   r2   r   r   r   r   NotReadable  s   r  )r@   r   rZ   rZ  )r+   r  r   r   r   "test_constructor_with_not_readable  s    z5BufferedRWPairTest.test_constructor_with_not_readablec             C   s,   G dd dt }| t| j|   |  d S )Nc               @   s   e Zd Zdd ZdS )zLBufferedRWPairTest.test_constructor_with_not_writeable.<locals>.NotWriteablec             S   s   dS )NFr   )r+   r   r   r   r/     s    zUBufferedRWPairTest.test_constructor_with_not_writeable.<locals>.NotWriteable.writableN)r   r   r   r/   r   r   r   r   NotWriteable  s   r  )r@   r   rZ   rZ  )r+   r  r   r   r   #test_constructor_with_not_writeable  s    z6BufferedRWPairTest.test_constructor_with_not_writeablec             C   sv   |  | d|  }| |dd | |dd | | d |  | d|  }| |d d d S )Ns   abcdefr   s   abcr7   r  s   ef)rZ  r   r@   r   rC   )r+   r  r   r   r   r    s    zBufferedRWPairTest.test_readc                sX    fdd}  |  dddg   |  dddg   | dddg d S )Nc                  s      d  S )Ns	   abc
def
h)rZ  r   r@   r   )r+   r   r   r    rA   z3BufferedRWPairTest.test_readlines.<locals>.<lambda>s   abc
s   def
r~   rJ   )r   r   )r+   r  r   )r+   r   r    s    z!BufferedRWPairTest.test_readlinesc             C   s<   |  | d|  }| |dd | | d d S )Ns   abcdefr   s   abcs   def)rZ  r   r@   r   r6  )r+   r  r   r   r   r    s    zBufferedRWPairTest.test_read1c          
   C   sl   xfdD ]^}|  |J | | d|  }td}| t|||d | t|d W d Q R X qW d S )N)r;   r   s   abcdefs        rJ   s   abcde)r   rZ  r   r@   r   r   r8  r   )r+   r:  r  r   r   r   r   r    s    
z BufferedRWPairTest.test_readintoc             C   sf   |   }| |   |}|d |  td}|| d|d d < |  | |jddg d S )Ns   abcs   defs   ***)r@   rZ  r.   rU   r   r   r(   )r+   r   r  r   r   r   r   r    s    

zBufferedRWPairTest.test_writec             C   sB   |  | d|  }| |dd | |dd d S )Ns   abcdefr   s   abc)rZ  r   r@   r  r  r   r   rC   )r+   r  r   r   r   	test_peek  s    zBufferedRWPairTest.test_peekc             C   s&   |  |  |  }| |  d S )N)rZ  r@   r  r2   )r+   r  r   r   r   test_readable  s    z BufferedRWPairTest.test_readablec             C   s&   |  |  |  }| |  d S )N)rZ  r@   r  r/   )r+   r  r   r   r   test_writeable  s    z!BufferedRWPairTest.test_writeablec             C   s&   |  |  |  }| |  d S )N)rZ  r@   r  r3   )r+   r  r   r   r   test_seekable  s    z BufferedRWPairTest.test_seekablec             C   s8   |  |  |  }| |j |  | |j d S )N)rZ  r@   r  rY   r[   r  )r+   r  r   r   r   test_close_and_closed  s    z(BufferedRWPairTest.test_close_and_closedc          	   C   s   dd }|   }||_|   }| ||}| t}|  W d Q R X | dt|j | |j	 | 
|j	 | |j	 d S )Nc               S   s   t  d S )N)reader_non_existingr   r   r   r   reader_close  s    zIBufferedRWPairTest.test_reader_close_error_on_close.<locals>.reader_closer  )r@   r[   rZ  r   rz  r{  r(  r)  r  rY   r  )r+   r  rI  r  r  rv  r   r   r    test_reader_close_error_on_close  s    z3BufferedRWPairTest.test_reader_close_error_on_closec          	   C   s   dd }|   }|   }||_| ||}| t}|  W d Q R X | dt|j | |j	 | 
|j	 | |j	 d S )Nc               S   s   t  d S )N)writer_non_existingr   r   r   r   writer_close  s    zIBufferedRWPairTest.test_writer_close_error_on_close.<locals>.writer_closer  )r@   r[   rZ  r   rz  r{  r(  r)  r  rY   r  )r+   r  rI  r  r  rv  r   r   r    test_writer_close_error_on_close  s    z3BufferedRWPairTest.test_writer_close_error_on_closec          	   C   s   dd }dd }|   }||_|   }||_| ||}| t}|  W d Q R X | dt|j | |jj	t | dt|jj	 | 
|j | 
|j | 
|j d S )Nc               S   s   t  d S )N)r  r   r   r   r   r    s    zPBufferedRWPairTest.test_reader_writer_close_error_on_close.<locals>.reader_closec               S   s   t  d S )N)r  r   r   r   r   r    s    zPBufferedRWPairTest.test_reader_writer_close_error_on_close.<locals>.writer_closer  r  )r@   r[   rZ  r   rz  r{  r(  r)  rt  ru  r  rY   )r+   r  r  rI  r  r  rv  r   r   r   'test_reader_writer_close_error_on_close  s    z:BufferedRWPairTest.test_reader_writer_close_error_on_closec             C   s   G dd dt }| |d|d}| |  | |d|d}| |  | |d|d}| |  | |d|d}| |  d S )Nc               @   s   e Zd Zdd Zdd ZdS )z8BufferedRWPairTest.test_isatty.<locals>.SelectableIsAttyc             S   s   t |  || _d S )N)r@   r,   _isatty)r+   isattyr   r   r   r,     s    
zABufferedRWPairTest.test_isatty.<locals>.SelectableIsAtty.__init__c             S   s   | j S )N)r  )r+   r   r   r   r    s    z?BufferedRWPairTest.test_isatty.<locals>.SelectableIsAtty.isattyN)r   r   r   r,   r  r   r   r   r   SelectableIsAtty  s   r  FT)r@   rZ  r  r  r  )r+   r  r  r   r   r   test_isatty  s    zBufferedRWPairTest.test_isattyc             C   s*   |  |  |  }t|}d }d }d S )N)rZ  r@   r  r  )r+   Zbrwr  r   r   r   test_weakref_clearing$  s    
z(BufferedRWPairTest.test_weakref_clearingN)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  m  s*   			r  c               @   s   e Zd ZejZdS )CBufferedRWPairTestN)r   r   r   rg   r   rZ  r   r   r   r   r  *  s   r  c               @   s   e Zd ZejZdS )PyBufferedRWPairTestN)r   r   r   ri   r   rZ  r   r   r   r   r  -  s   r  c               @   s   e Zd Zd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+Zd+S ),BufferedRandomTestzrb+zwb+c             C   s   t |  t|  d S )N)r  r  r  )r+   r   r   r   r  5  s    
z#BufferedRandomTest.test_constructorc             C   s   t |  t|  d S )N)r  r  r  )r+   r   r   r   r  9  s    
z%BufferedRandomTest.test_uninitializedc             C   sn   |  d}| |d}| d|d |d |d | |j | d|  | d|jd	  d S )
N)s   asdfs   ghjkr  s   asrG   s   ddds   eees   ghjks   dddeeer   )r@   rZ  r   rC   r.   r  r(   )r+   r  r   r   r   r   test_read_and_write=  s    


z&BufferedRandomTest.test_read_and_writec             C   s   |  d}| |}| d|d | d|  |dd | d|d |d |dd | d|  | d	|  |d
d | d|  |dd | d|  | d|d |  | d|  | 	t
|jd d S )Ns	   asdfghjkls   asrG   r   s   asdfr   s   123fs	   asdf123flr}   rJ   r7   rG  s   fl   g        )r   rZ  r   rC   r6   r5   r.   rU   r   r   r   )r+   r  r   r   r   r   test_seek_and_tellH  s$    


z%BufferedRandomTest.test_seek_and_tellc             C   s   |  d}| |}| d||d |d | d||d | d|  |  | d|  | d|| |dd |d	 |  |dd | d	||d
 d S )Ns	   abcdefghis   abrG   s   12s   efr|   s   ghir   s   XYZr   )r   rZ  r   r.   r6   rU   r5   )r+   Z	read_funcr  r_  r   r   r   check_flush_and_read_  s    



z'BufferedRandomTest.check_flush_and_readc             C   s   |  dd  d S )Nc             W   s
   | j | S )N)rC   )r_  re   r   r   r   r  r  rA   z8BufferedRandomTest.test_flush_and_read.<locals>.<lambda>)r  )r+   r   r   r   test_flush_and_readq  s    z&BufferedRandomTest.test_flush_and_readc             C   s   ddd}|  | d S )Nrq   c             S   s.   t |dkr|nd}| |}t|d | S )Nr   i'  )r   r;   r   )r_  r:   r
   r   r   r   	_readintou  s    
z=BufferedRandomTest.test_flush_and_readinto.<locals>._readinto)rq   )r  )r+   r
  r   r   r   test_flush_and_readintot  s    
z*BufferedRandomTest.test_flush_and_readintoc             C   s   ddd}|  | d S )Nrq   c             S   s2   |  |}|dkr|d | }| t|d |S )Nrq   r7   )r  r5   r   )r_  r:   r
   r   r   r   _peek|  s
    
z5BufferedRandomTest.test_flush_and_peek.<locals>._peek)rq   )r  )r+   r  r   r   r   test_flush_and_peek{  s    
z&BufferedRandomTest.test_flush_and_peekc             C   sh   |  d}| |}|d |  |d |  |dd | d|  | d|  d S )Ns	   abcdefghis   123s   45r   s	   12345fghi)r   rZ  r.   rU   r5   r   r   rC   )r+   r  r_  r   r   r   test_flush_and_write  s    



z'BufferedRandomTest.test_flush_and_writec             C   s   t |  t|  d S )N)r  r  r  )r+   r   r   r   r    s    
zBufferedRandomTest.test_threadsc             C   s(   dd }|  | dd }|  | d S )Nc             S   s   |  d d S )Nr7   )r  )r_  r   r   r   r    s    z6BufferedRandomTest.test_writes_and_peek.<locals>._peekc             S   s.   |   }| dd | d | |d d S )Nrq   r7   r   )r6   r5   r  )r_  r   r   r   r   r    s    
)r  )r+   r  r   r   r   test_writes_and_peek  s    
z'BufferedRandomTest.test_writes_and_peekc             C   s   dd }|  | d S )Nc             S   s   |  dd | d d S )Nrq   r7   )r5   rC   )r_  r   r   r   _read  s    z7BufferedRandomTest.test_writes_and_reads.<locals>._read)r  )r+   r  r   r   r   test_writes_and_reads  s    z(BufferedRandomTest.test_writes_and_readsc             C   s   dd }|  | d S )Nc             S   s   |  dd | d d S )Nrq   r7   )r5   r6  )r_  r   r   r   _read1  s    z9BufferedRandomTest.test_writes_and_read1s.<locals>._read1)r  )r+   r  r   r   r   test_writes_and_read1s  s    z)BufferedRandomTest.test_writes_and_read1sc             C   s   dd }|  | d S )Nc             S   s   |  dd | td d S )Nrq   r7   )r5   r;   r   )r_  r   r   r   r    s    z;BufferedRandomTest.test_writes_and_readintos.<locals>._read)r  )r+   r  r   r   r   test_writes_and_readintos  s    z,BufferedRandomTest.test_writes_and_readintosc             C   s   xdD ]}|  d}| |d}| |dd | | d |d|  | | |d  |  | | |d  | }| |dd|  dd|    qW d S )N)r7   rJ   s
   AAAAAAAAAAr   r7      A   Br}   )r   rZ  r   rC   r6   r.   rU   r   )r+   Zoverwrite_sizer  r_  rm   r   r   r   test_write_after_readahead  s    

z-BufferedRandomTest.test_write_after_readaheadc          	   C   s   dd }d}xt dt|D ]x}xrt |t|D ]`}| |}| |d}|||| |  t|}d||< d||< | | |d||f  q0W qW d S )	Nc             S   s:   |  | | ||  | d |  | | d d S )N      )r5   rC   r.   )r_  Zpos1Zpos2r   r   r   mutate  s
    


z:BufferedRandomTest.test_write_rewind_write.<locals>.mutates   r   r   rG   r7   zfailed result for i=%d, j=%d)r   r   r   rZ  rU   r   r   r   )r+   r  r
   r   jr  r_  expectedr   r   r   test_write_rewind_write  s    
z*BufferedRandomTest.test_write_rewind_writec             C   s^   |  d}| |d}| |dd | | d | |dd | | d d S )Ns
   AAAAAAAAAAr   rG   s   AAs   BBr   )r   rZ  r   rC   r<   r.   )r+   r  r_  r   r   r   !test_truncate_after_read_or_write  s    
z4BufferedRandomTest.test_truncate_after_read_or_writec             C   s   t |  t|  d S )N)r  r  r  )r+   r   r   r   r    s    
z%BufferedRandomTest.test_misbehaved_ioc          
   C   s:  |  d}| |d}|d | |dd |d | |dd |d td}|| | |d	 |d
 | |dd |	  | |
 d W d Q R X W d Q R X |  dd}| |dL}| |dd |d | |dd |	  | |
 d W d Q R X W d Q R X d S )Ns   abcdefghr      1r7   r     2r  r      f   4r~   s   1b2d3f4hs   abcr  r  s   a2c)r   rZ  r.   r   rC   r6  r   r;   r  rU   r   )r+   r  r#   r9   r   r   r   test_interleaved_read_write  s*    




$
z.BufferedRandomTest.test_interleaved_read_writec          
   C   s   |  d}| |l}|d | | d |d | | d |d | | d |  | | d W d Q R X W d Q R X d S )	Ns
   ab
cdef
g
r  s   b
r   s   def
r      
s
   1b
2def
3
)r   rZ  r.   r   r   rU   r   )r+   r  r#   r   r   r   test_interleaved_readline_write	  s    


z2BufferedRandomTest.test_interleaved_readline_writeN)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   r   r  1  s.   r  c               @   s6   e Zd ZejZeeddd Z	dd Z
dd ZdS )	CBufferedRandomTestzGMSan defaults to crashing instead of returning NULL for malloc failure.c             C   sD   t |  tjdkr@|  }| |}| ttt	f|j
|tj d S )Ni)r  r  r   r  r@   rZ  r   r   r  rs   r,   )r+   r  r_  r   r   r   r  	  s    


z$CBufferedRandomTest.test_constructorc             C   s   t |  t|  d S )N)r  r	  r  )r+   r   r   r   r	  %	  s    
z+CBufferedRandomTest.test_garbage_collectionc          	   C   s0   |  td | t ddd W d Q R X d S )Nr   i   )r?  r   rZ  rg   r   )r+   r   r   r   r  )	  s    z#CBufferedRandomTest.test_args_errorN)r   r   r   rg   r   rZ  r  r  r  r  r	  r  r   r   r   r   r&  	  s   r&  c               @   s   e Zd ZejZdS )PyBufferedRandomTestN)r   r   r   ri   r   rZ  r   r   r   r   r'  /	  s   r'  c               @   s\   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdddZ	dd Z
dZedd ZdS )StatefulIncrementalDecodera  
    For testing seek/tell behavior with a stateful, buffering decoder.

    Input is a sequence of words.  Words may be fixed-length (length set
    by input) or variable-length (period-terminated).  In variable-length
    mode, extra periods are ignored.  Possible words are:
      - 'i' followed by a number sets the input length, I (maximum 99).
        When I is set to 0, words are space-terminated.
      - 'o' followed by a number sets the output length, O (maximum 99).
      - Any other word is converted into a word followed by a period on
        the output.  The output word consists of the input word truncated
        or padded out with hyphens to make its length equal to O.  If O
        is 0, the word is output verbatim without truncating or padding.
    I and O are initially set to 1.  When I changes, any buffered input is
    re-scanned according to the new I.  EOF also terminates the last word.
    strictc             C   s   t j| | |   d S )N)codecsIncrementalDecoderr,   reset)r+   r  r   r   r   r,   P	  s    z#StatefulIncrementalDecoder.__init__c             C   s   dt |  S )Nz<SID %x>)id)r+   r   r   r   __repr__T	  s    z#StatefulIncrementalDecoder.__repr__c             C   s   d| _ d| _t | _d S )Nr7   )r   or   r   )r+   r   r   r   r,  W	  s    z StatefulIncrementalDecoder.resetc             C   s,   | j dA | jdA  }}t| j|d | fS )Nr7   r   )r   r/  r   r   )r+   r   r/  r   r   r   getstate\	  s    z#StatefulIncrementalDecoder.getstatec             C   s:   |\}}t || _t|d\}}|dA |dA  | _| _d S )Nr   r7   )r   r   divmodr   r/  )r+   stater   rg   r   r/  r   r   r   setstate`	  s    
z#StatefulIncrementalDecoder.setstateFc             C   s   d}xl|D ]d}| j dkrF|tdkr8| jrD||  7 }qn| j| q
| j| t| j| j kr
||  7 }q
W |r| jr||  7 }|S )Nr   r   .)r   ordr   process_wordr-   r   )r+   inputfinaloutputr
   r   r   r   decodef	  s    


z!StatefulIncrementalDecoder.decodec             C   s   d}| j d tdkr6tdt| j dd  p,d| _nv| j d tdkrhtdt| j dd  p^d| _nD| j d}t|| jk r|d| j 7 }| jr|d | j }|d	7 }t | _ |S )
Nr   r   r   c   r7   r/  r   -r4  )	r   r5  rE  intr   r/  r:  r   r   )r+   r9  r   r   r   r6  w	  s      z'StatefulIncrementalDecoder.process_wordc          	   C   s6   | j r2|dkr2td}tjd|jd d d d | dS d S )Ntest_decoderzlatin-1)rK  r=  r:  incrementalencoderstreamreaderstreamwriterincrementaldecoder)codecEnabledr*  lookup	CodecInfor=  )clsrK  latin1r   r   r   lookupTestDecoder	  s    
z,StatefulIncrementalDecoder.lookupTestDecoderN)r)  )F)r   r   r   r=   r,   r.  r,  r0  r3  r:  r6  rC  classmethodrH  r   r   r   r   r(  >	  s   

r(  c               @   s*   e Zd ZdZdddddddgZd	d
 ZdS )StatefulIncrementalDecoderTestzB
    Make sure the StatefulIncrementalDecoder actually works.
    )s   abcdFza.b.c.d.)s   oiabcdTzabcd.)s   oi...abcd...Tzabcd.)s   i.o6.x.xyz.toolongtofit.Fzx-----.xyz---.toolon.)s
   i.i2.o6xyzTzxy----.z-----.)s   i.o3.i6.abcdefghijklmnopTzabc.ghi.mno.)s;   i.o29.a.b.cde.o15.abcdefghijabcdefghij.i3.a.b.c.d.ei00k.l.mTza----------------------------.b----------------------------.cde--------------------------.abcdefghijabcde.a.b------------..c.------------.d.e------------.k--------------.l--------------.m--------------.c             C   s`   x.| j D ]$\}}}t }| |||| qW t }| |dd | |ddd d S )Ns   oiabcdr   rA   r7   zabcd.)
test_casesr(  r   r:  )r+   r7  eofr9  dr   r   r   r>  	  s    z+StatefulIncrementalDecoderTest.test_decoderN)r   r   r   r=   rK  r>  r   r   r   r   rJ  	  s   rJ  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eejjddd Zejeejjd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 d0d1 Z!d2d3 Z"d4d5 Z#d6d7 Z$d8d9 Z%d:d; Z&d<d= Z'd>d? Z(d@dA Z)dBdC Z*dDdE Z+dFdG Z,dHdI Z-dJdK Z.dLdM Z/dNdO Z0dPdQ Z1dRdS Z2dTdU Z3dVdW Z4dXdY Z5dZd[ Z6ej7d\d] Z8d^d_ Z9d`da Z:dbdc Z;ddde Z<dfdg Z=dhdi Z>djdk Z?dldm Z@dndo ZAdpdq ZBdrds ZCdtdu ZDdvdw ZEdxdy ZFejGdzd{ ZHejGd|d} ZId~d ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdS )TextIOWrapperTestc             C   s"   d| _ dd| _ttj d S )Ns   AAA
BBBCCC
DDD
EEE
s   AAA
BBB
CCC
DDD
EEE
r   )testdatar:  
normalizedr   rx   ry   )r+   r   r   r   rz   	  s    zTextIOWrapperTest.setUpc             C   s   t t j d S )N)r   rx   ry   )r+   r   r   r   r{   	  s    zTextIOWrapperTest.tearDownc             C   s   |  d}| |d}| |}|j|ddd | |jd | |jd |j|ddd	 | |jd | |jd | d
|  | jt	|j|dd | jt
|j|dd d S )Ns   é

i  zlatin-1z
)r   r3  Fzutf-8T)r   line_bufferingu   é
r0   )r3  rf  )r   r   r   r,   r   r   rQ  r   r   r   rs   )r+   r   r
   r  r   r   r   r  	  s    

z"TextIOWrapperTest.test_constructorc             C   sf   | j | j }~| j | j }| tt| | ttfd|jd |	| 
  | |dd d S )Nzuninitialized|has no attributer   r   )r   r  r   r  r]  r?  rs   r   rC   r,   r@   r   )r+   r  r   r   r   r  	  s    

z$TextIOWrapperTest.test_uninitializedc          	   C   s<   |   }| |}| td | j|dd W d Q R X d S )Nzis not a text encodinghex)r   )r   r   r?  LookupErrorr   )r+   r   r
   r   r   r   *test_non_text_encoding_codecs_are_rejected	  s    
z<TextIOWrapperTest.test_non_text_encoding_codecs_are_rejectedc             C   s   |   }| |}| |}| | | | j|dd}|d | |  |  | | d | 	t
|j t| | |jd | |jd | |j | |j d S )Nr   )r   Zhowdys   howdyr)  )r   r   r   r[  r\  r.   r  r   r   r   rs   r]  r   r  rQ  write_through)r+   r   r
   r  r   r   r   r^  	  s    


zTextIOWrapperTest.test_detachc             C   s   |  dd}| |}| j|dd}| jj}| t|d|  d|_| t|d|  d|_| t|d|  d	|_| t|d
|  |j	
  t| d S )NZhellozutf-8)r   z'<(%s\.)?TextIOWrapper encoding='utf-8'>rm  z4<(%s\.)?TextIOWrapper name='dummy' encoding='utf-8'>r   z=<(%s\.)?TextIOWrapper name='dummy' mode='r' encoding='utf-8'>s   dummyz><(%s\.)?TextIOWrapper name=b'dummy' mode='r' encoding='utf-8'>)r   r=  r   r   r   rn  r]  rK  r   r   r\  )r+   r  r
   r  modnamer   r   r   ro  
  s"    









zTextIOWrapperTest.test_reprc          	   C   sR   |   }| |}t|d|( yt| W n tk
rB   Y nX W d Q R X d S )NrK  )r   r   r   rp  r]  rq  )r+   r  r  r   r   r   rr  
  s    
z%TextIOWrapperTest.test_recursive_reprc             C   sv   |   }| |d}| j|ddd}|d | | d |d | | d |d	 | | d
 d S )Ni  
T)r3  rQ  XrA   zY
Zs   XY
ZzABs   XY
ZAB)r   r   r   r.   r   r   )r+   r   r
   r  r   r   r   test_line_buffering!
  s    


z%TextIOWrapperTest.test_line_bufferingc             C   s  |   }| |d}| j|ddd}|d | | d |jdd | | d	 |d
 | | d |d | | d |jdd | | d |d | | d |  |jd d | |jd |jdd |  |jd d | |jd d S )Ni  rW  F)r3  rQ  zAB
CrA   T)rQ  s   AB
CzDEF
Gs	   AB
CDEF
GHs
   AB
CDEF
GHZIJ)r   r   r   r.   r   r   reconfigurerQ  )r+   r   r
   r  r   r   r   test_reconfigure_line_buffering,
  s,    



z1TextIOWrapperTest.test_reconfigure_line_bufferingzutf-8 mode is enabledc          	   C   sv   t tj}zNxdD ]}|tjkrtj|= qW td}|  }| |}| |j| W d tj	  tj
| X d S )N)LC_ALLLANGLC_CTYPEF)dictr   environlocalegetpreferredencodingr   r   r   r   clearupdate)r+   Zold_environkeyZcurrent_locale_encodingr
   r  r   r   r   test_default_encodingG
  s    





z'TextIOWrapperTest.test_default_encodingc                sP   dd l  |  } fdd|_| t| j|  fdd|_| t| j| d S )Nr   c                  s
    j d S )Nr7   )ZINT_MAXr   )	_testcapir   r   r  `
  rA   z8TextIOWrapperTest.test_device_encoding.<locals>.<lambda>c                  s
    j d S )Nr7   )ZUINT_MAXr   )rh  r   r   r  b
  rA   )rh  r   r1   r   r   r   )r+   r
   r   )rh  r   test_device_encodingZ
  s    z&TextIOWrapperTest.test_device_encodingc             C   sJ   |   }| j|dd}| |jd | |}| |j t|j d S )Nzutf-8)r   )r   r   r   r   assertIsNotNoner*  rD  )r+   r
   r  r   r   r   test_encodinge
  s    
zTextIOWrapperTest.test_encodingc             C   s   |  d}| j|dd}| t|j |  d}| j|ddd}| t|j |  d}| j|ddd}| | d |  d}| j|ddd}| | d	 d S )
Ns   abc

r   )r   r)  )r   r  r   zabc

replaceu   abc
�
)r   r   r   UnicodeErrorrC   r   )r+   r
   r  r   r   r   test_encoding_errors_readingn
  s    



z.TextIOWrapperTest.test_encoding_errors_readingc             C   s   |   }| j|dd}| t|jd |   }| j|ddd}| t|jd |   }| j|dddd}|d	 |  | | d
 |   }| j|dddd}|d	 |  | | d d S )Nr   )r      ÿr)  )r   r  r   rW  )r   r  r3  u	   abcÿdef
s   abcdef
rl  s   abc?def
)r   r   r   rm  r.   rU   r   r   )r+   r
   r  r   r   r   test_encoding_errors_writing
  s$    



z.TextIOWrapperTest.test_encoding_errors_writingc          	   C   sT  dddddg}d dddddggd|gd	ddd
dggdddggddddggg}d}x|D ]}t d||}xdD ]}xtddD ]}x|D ]\}}	| | ||}
| j|
||d}|rg }xD|d}|dkrP | t	|d |
||   qW nt|}x$t||	D ]\}}| || qW | t	|t	|	 qW qW qtW qVW d S )Nzunix
z	windows
zos9zlast
Znonlzwindows
zos9
r   rW  z	os9last
z
zunix
windows
zos9last
nonlzunix
windowsz
os9z	last
nonl)zutf-8zlatin-1zutf-16z	utf-16-lez	utf-16-bezutf-32z	utf-32-lez	utf-32-be)FTr7   r   )r3  r   rG   )r   rl   r=  r   r   r   r   rC   r   r   r-   r   r&   zip)r+   Zinput_linesr   	encodingsr   r   Zdo_readsr   r3  Z	exp_linesr_  textioZ	got_linesZc2Zgot_lineZexp_liner   r   r   test_newlines
  s6    



zTextIOWrapperTest.test_newlinesc          
   C   s   d}| dd dd}xd |djddfd|djddfd	d
ddddgfddddgfddddddgfgD ]R\}}| |}| j|d|d}| | | |d | | d	| qrW d S )Ns   AAA
BB B
CCCDDDEEE
FFF
GGGs   
r$     r   T)keependsr   rW  zAAA
zBB B
zCCCDDDEEE
zFFF
ZGGGz
zAAA
BB B
CCCDDDEEE
rq  zAAA
BB B
CCCzDDDzEEEz
FFFz
GGG)r   r3  r   )
rl  r:  rj  r   r   r   r   r5   rC   rl   )r+   rO  rP  r3  r  r9   txtr   r   r   test_newlines_input
  s    

z%TextIOWrapperTest.test_newlines_inputc             C   s   ddddd}d |t j fgt|  }xl|D ]d\}}|  }| j|d|d}|d |d |d	 |  | |j	d
 | |
 | q0W d S )Ns   AAA
BBB
CCC
XY
Zs   AAABBBCCCXYZs   AAA
BBB
CCC
XY
Z)r   rW  rq  z
r   )r   r3  zAAA
BzBB
CCC
zXY
ZF)r   linesepsorteditemsr   r   r.   rU   r   rY   r   )r+   Ztestdictr   r3  r  r9   rx  r   r   r   test_newlines_output
  s    


z&TextIOWrapperTest.test_newlines_outputc                sZ   g | j  G  fddd }| }| j|dd}|d ~t  | dg d S )Nc                   s   e Zd Z fddZdS )z4TextIOWrapperTest.test_destructor.<locals>.MyBytesIOc                s    |    |  d S )N)r-   r   r[   )r+   )r   r  r   r   r[   
  s    z:TextIOWrapperTest.test_destructor.<locals>.MyBytesIO.closeN)r   r   r   r[   r   )r   r  r   r   	MyBytesIO
  s   r~  r   )r   r  s   abc)r   r   r.   r   r   r   )r+   r~  r
   r  r   )r   r  r   r   
  s    
z!TextIOWrapperTest.test_destructorc                sN   g  G  fddd| j }|  }||dd}~t  |  dddg d S )Nc                   s:   e Zd Z fddZ fddZ fddZ  ZS )z<TextIOWrapperTest.test_override_destructor.<locals>.MyTextIOc                s6    d yt j}W n tk
r*   Y nX |  d S )Nr7   )r-   rH   r   r   )r+   r#   )rI   r   r   r   r   
  s    
zDTextIOWrapperTest.test_override_destructor.<locals>.MyTextIO.__del__c                s    d t   d S )NrG   )r-   rH   r[   )r+   )rI   r   r   r   r[   
  s    
zBTextIOWrapperTest.test_override_destructor.<locals>.MyTextIO.closec                s    d t   d S )Nr   )r-   rH   rU   )r+   )rI   r   r   r   rU      s    
zBTextIOWrapperTest.test_override_destructor.<locals>.MyTextIO.flush)r   r   r   r   r[   rU   rK   r   )r   )rI   r   MyTextIO
  s   r  r   )r   r7   rG   r   )r   r   r   r   r   )r+   r  r
   r  r   )r   r   rc  
  s    z*TextIOWrapperTest.test_override_destructorc          	      s       fdd}td}t| W d Q R X |  }|rt|	 d 
|d| 
|d| d S )Nc                  s     j d S )N)r   rf  r   )r  r+   r   r   r#     s    z:TextIOWrapperTest.test_error_through_destructor.<locals>.frg  r7   zException OSError: z ignored)rX   r   rh  r   r   r   ri  r   r   rj  r  r   rk  )r+   r#   rm   r   )r  r+   r   rl  	  s    z/TextIOWrapperTest.test_error_through_destructorc             C   s  xdD ]}x~dD ]t}| j tjd|d}||_| |dd |  | j tjd|d}||_| | d | | d | }| |	dd | |d d |	d | |d	d
 | |dd | |dd | | d | | | | |	dd | |	dd	| | |dd | |	|| | | d |
dr| || |  qW qW d S )N)r7   rG   r   r   rJ   r  r            !   ?   @   A   )r   zlatin-1zutf-8zw+)r   r  r   zr+r   rG   r   r7   r  r   defutf)r    r   ry   r"   r   r.   r[   r6   rC   r5   r   multi_line_test)r+   Z	chunksizeencr#   cookier   r   r   test_basic_io  s6    
zTextIOWrapperTest.test_basic_ioc             C   s   | d |  d}g }xbdD ]Z}g }x&t|D ]}|||t|   q2W d|d }|| |f || q W | d g }	x(| }
| }|sP |	|
|f qW | 	|	| d S )Nr   u	   sÿ࿿￿)r   r7   rG   r   r   rJ      r  r  r  >   r  r  r  i  r   rW  )
r5   r<   r   r-   r   rl   r6   r.   r   r   )r+   r#   r  ZsampleZwlinesr5  charsr   lineZrlinesr   r   r   r   r  7  s(    


z!TextIOWrapperTest.multi_line_testc             C   s   | j tjddd}| }|d | }|d | }|d | | | | | d | | | | | d | | | |d x&|D ]}| |d | t	|j qW | | | |
  d S )Nzw+zutf-8)r   u   ÿ
r   )r    r   ry   r6   r.   r5   r   r   r   rZ   r[   )r+   r#   Zp0Zp1Zp2r  r   r   r   test_tellingM  s$    




zTextIOWrapperTest.test_tellingc       
   	   C   s   t  }|d }d| }t|d}| t|t| d}t|d}|| }| tjd}||d  W d Q R X | jtjdddB}|	|}	| |	t
|d | | | | | | W d Q R X d S )	NrG   r   zutf-8u   袈
r   r   )r   r   )r$   r   r=  r   r   r    r   ry   r.   rC   r(  r6   r   )
r+   Z
chunk_sizeZprefix_sizeZu_prefixprefixZu_suffixsuffixr  r#   rm   r   r   r   test_seekinga  s    
zTextIOWrapperTest.test_seekingc          	   C   sf   d}|  tjd}|| W d Q R X | j tjddd"}|j d|_|  |  W d Q R X d S )Ns   ࿿
r   r   zutf-8)r   rG   )r    r   ry   r.   r"   r   r6   )r+   r   r#   r   r   r   test_seeking_toor  s    z"TextIOWrapperTest.test_seeking_tooc                s   d d fdd	}dt _zfxtjD ]\}}}|| q$W xBtjD ]8\}}} t|d  }d| }|d }||| | qBW W d dt _X d S )	N   r   c          	      s    tjd}||  |  j tjdd} |_| }|  xt|t|d D ]}xddt|| gD ]~}j tjdd}	|||d|  |
 }	||||||   || 	| ||d  |  qrW qZW dS )zTell/seek to various points within a data stream and ensure
            that the decoded data returned by read() is consistent.r   r>  )r   r7   rJ   N)r    r   ry   r.   r[   r"   rC   r   r   r   r6   r5   )r   min_posr#   Zdecodedr   r  r  )
CHUNK_SIZEr+   r   r   test_seek_and_tell_with_data  s     

zJTextIOWrapperTest.test_seek_and_tell.<locals>.test_seek_and_tell_with_datar7   rG      .)r   )r(  rC  rJ  rK  r   )r+   r  r7  _offsetr  r  r   )r  r+   r   r  }  s    z$TextIOWrapperTest.test_seek_and_tellc             C   s   d}d}x|D ]}|   }| j||d}|| || |d | | |d  |d | | |d  | | |d | qW d S )NZ
1234567890)zutf-16z	utf-16-lez	utf-16-bezutf-32z	utf-32-lez	utf-32-be)r   r   rG   )r   r   r.   r5   r   rC   r   r=  )r+   r   r   r   r9   r#   r   r   r   test_encoded_writes  s    




z%TextIOWrapperTest.test_encoded_writesc             C   s0   G dd d| j }| | }| t|j d S )Nc               @   s   e Zd Zdd ZdS )z5TextIOWrapperTest.test_unreadable.<locals>.UnReadablec             S   s   dS )NFr   )r+   r   r   r   r2     s    z>TextIOWrapperTest.test_unreadable.<locals>.UnReadable.readableN)r   r   r   r2   r   r   r   r   
UnReadable  s   r  )r   r   r   rZ   rC   )r+   r  rx  r   r   r   test_unreadable  s    z!TextIOWrapperTest.test_unreadablec             C   sB   |  | d}d}x|d}|s&P ||7 }qW | |d d S )Ns   AA
BBr   r7   zAA
BB)r   r   rC   r   )r+   rx  readsr  r   r   r   test_read_one_by_one  s    
z&TextIOWrapperTest.test_read_one_by_onec             C   sl   |  | d}| | dddg |d | |d dddg |d | |dddg d S )Ns   AA
BB
CCzAA
zBB
ZCCr   rJ   )r   r   r   r   r5   )r+   rx  r   r   r   r    s    

z TextIOWrapperTest.test_readlinesc             C   sB   |  | d}d}x|d}|s&P ||7 }qW | |d d S )Ns   AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Br   r  zAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
B)r   r   rC   r   )r+   rx  r  r  r   r   r   test_read_by_chunk  s    
z$TextIOWrapperTest.test_read_by_chunkc             C   sB   dddg}|   }| |}|| |  | | d d S )Nr   cdefs   abcdef)r   r   r   rU   r   r   )r+   r  r9   rx  r   r   r   r    s    


z!TextIOWrapperTest.test_writelinesc             C   sF   t dddg}|  }| |}|| |  | | d d S )Nr   r  r  s   abcdef)r   r   r   r   rU   r   r   )r+   r  r9   rx  r   r   r   r    s    

z*TextIOWrapperTest.test_writelines_userlistc             C   sH   |  |  }| t|jdddg | t|jd  | t|jd d S )Nr7   rG   r   s   abc)r   r   r   r   r   )r+   rx  r   r   r   r    s    z'TextIOWrapperTest.test_writelines_errorc             C   sJ   | j | | jdd}d}x|d}|s,P ||7 }qW | || j d S )Nr   )r   r   r7   )r   r   rO  rC   r   rP  )r+   rx  r  r  r   r   r   test_issue1395_1  s    
z"TextIOWrapperTest.test_issue1395_1c             C   sP   | j | | jdd}d|_d}x|d}|s2P ||7 }q"W | || j d S )Nr   )r   r   r   )r   r   rO  r"   rC   r   rP  )r+   rx  r  r  r   r   r   test_issue1395_2  s    
z"TextIOWrapperTest.test_issue1395_2c             C   sj   | j | | jdd}d|_|d}||d7 }|| 7 }|| 7 }|| 7 }| || j d S )Nr   )r   r   )r   r   rO  r"   rC   r   r   rP  )r+   rx  r  r   r   r   test_issue1395_3  s    
z"TextIOWrapperTest.test_issue1395_3c             C   sD   | j | | jdd}d|_|d}|| 7 }| || j d S )Nr   )r   r   )r   r   rO  r"   rC   r   rP  )r+   rx  r  r   r   r   test_issue1395_4  s
    
z"TextIOWrapperTest.test_issue1395_4c             C   sX   | j | | jdd}d|_|d}| }|d || | |dd d S )Nr   )r   r   r   zBBB
)r   r   rO  r"   rC   r6   r5   r   )r+   rx  r  r   r   r   r   test_issue1395_5&  s    


z"TextIOWrapperTest.test_issue1395_5c             C   s2   |  | j}| j|dd}| | |  d S )Nr   )r   )r   rO  r   r   r3   )r+   r   rx  r   r   r   test_issue22820  s    z TextIOWrapperTest.test_issue2282c          
   C   s   t j}xdD ]}| j|d|d}|d | }W d Q R X | |d}| | d| W d Q R X | j|d|d}|d W d Q R X | |d}| | d| W d Q R X qW d S )	N)z	utf-8-sigzutf-16zutf-32r   )r   aaar   r   xxxaaaxxx)r   ry   r    r.   r6   r   rC   r=  )r+   filenamecharsetr#   r   r   r   r   test_append_bom6  s    

 z!TextIOWrapperTest.test_append_bomc          
   C   s   t j}xdD ]}| j|d|d}|d | }W d Q R X | j|d|d.}|| |d |d |d W d Q R X | |d	}| | d
| W d Q R X qW d S )N)z	utf-8-sigzutf-16zutf-32r   )r   r  zr+Zzzzr   Zbbbr   Zbbbzzz)	r   ry   r    r.   r6   r5   r   rC   r=  )r+   r  r  r#   r   r   r   r   test_seek_bomE  s    




zTextIOWrapperTest.test_seek_bomc          
   C   s   t j}xdD ]}| j|d|d}|d W d Q R X | j|d|d(}|d |d| j |d W d Q R X | |d}| | d	| W d Q R X qW d S )
N)z	utf-8-sigzutf-16zutf-32r   )r   r  r   r   r  r   r  )	r   ry   r    r.   r5   r   r   rC   r=  )r+   r  r  r#   r   r   r   test_seek_append_bomT  s    

z&TextIOWrapperTest.test_seek_append_bomc          	   C   sX   |  tjd}| |jd W d Q R X | j tjddd}| |jd W d Q R X d S )Nr   r)  rl  )r  )r    r   ry   r   r  )r+   r#   r   r   r   test_errors_propertya  s    z&TextIOWrapperTest.test_errors_propertyc          
      s   t   | jtjdddN fddfddtdD }t| j t	d	 W d Q R X W d Q R X | tj6
 }x&tdD ]}| |d
| d qW W d Q R X d S )Nr   r7   )r   c                s   d|  }    | d S )NzThread%03d
)r  r.   )r:   text)eventr#   r   r   runl  s    z1TextIOWrapperTest.test_threads_write.<locals>.runc                s   g | ]}t j |fd qS ))r   re   )rO   r   )r  r~  )r  r   r   r  p  s   z8TextIOWrapperTest.test_threads_write.<locals>.<listcomp>r   g{Gz?zThread%03d
)rO   rP   r    r   ry   r   r  rR   rS   rT   rC   r   r   )r+   r  Zcontentr:   r   )r  r#   r  r   test_threads_writeg  s    
z$TextIOWrapperTest.test_threads_writec                s   | j | | jddg   fdd}|_| tj | j | j	j |   | 
 d  | 
 d  dd _d S )	Nr   )r   c                  s    j jj g d d < t d S )N)rY   r   rZ   r   )rY   rx  r   r   r  ~  s    z>TextIOWrapperTest.test_flush_error_on_close.<locals>.bad_flushr   r7   c               S   s   d S )Nr   r   r   r   r   r    rA   z=TextIOWrapperTest.test_flush_error_on_close.<locals>.<lambda>)r   r   rO  rU   r   rZ   r[   r  rY   r   r  )r+   r  r   )rY   rx  r   r  y  s    
z+TextIOWrapperTest.test_flush_error_on_closec          	   C   s   |  | j}dd }dd }||_| j|dd}||_| t}|  W d Q R X | |jj	d | 
|jjt | |jjj	d | |j d S )	Nc               S   s   t dd S )NrU   )rZ   r   r   r   r   r    s    z>TextIOWrapperTest.test_close_error_on_close.<locals>.bad_flushc               S   s   t dd S )Nr[   )rZ   r   r   r   r   rs    s    z>TextIOWrapperTest.test_close_error_on_close.<locals>.bad_closer   )r   )r[   )rU   )r   rO  r[   r   rU   r   rZ   r   r)  re   rt  ru  r  rY   )r+   r   r  rs  rx  rv  r   r   r   rw    s    z+TextIOWrapperTest.test_close_error_on_closec          	   C   s   |  | j}dd }dd }||_| j|dd}||_| t}|  W d Q R X | dt|j	 | 
|j	jt | dt|j	j | |j d S )	Nc               S   s   t d S )N)rx  r   r   r   r   r    s    zLTextIOWrapperTest.test_nonnormalized_close_error_on_close.<locals>.bad_flushc               S   s   t d S )N)ry  r   r   r   r   rs    s    zLTextIOWrapperTest.test_nonnormalized_close_error_on_close.<locals>.bad_closer   )r   ry  rx  )r   rO  r[   r   rU   r   rz  r{  r(  r)  rt  ru  r  rY   )r+   r   r  rs  rx  rv  r   r   r   r|    s    z9TextIOWrapperTest.test_nonnormalized_close_error_on_closec             C   s@   | j | | jdd}|  |  |  | t|j d S )Nr   )r   )r   r   rO  r[   r   rs   rU   )r+   rx  r   r   r   r    s
    z"TextIOWrapperTest.test_multi_closec             C   s8   |  | | j}| | j|j | | j|jd d S )Nr   )r   rc   rO  r   rd   r6   r5   )r+   rx  r   r   r   r}    s    z!TextIOWrapperTest.test_unseekablec          	   C   sB   | j | | jdd}| | j}| t ||_W d Q R X d S )Nr   )r   )r   r   rO  r   r   r   )r+   rx  r9   r   r   r   r    s    z*TextIOWrapperTest.test_readonly_attributesc             C   sZ   |  dddg}| j|ddd}| |dd | | d	 | t|d
dg d S )Ns   abcs   defs   ghi
jkl
opq
r   rW  )r   r3  r   Zabcdzefghi
zjkl
zopq
)r@   r   r   rC   r   r&   )r+   r  rx  r   r   r   
test_rawio  s
    zTextIOWrapperTest.test_rawioc             C   sX   |  dddg}| j|dddd}|d |d	 |d
 | d|jd d S )Ns   abcs   defs   ghi
jkl
opq
r   rW  T)r   r3  rU  1z23
45rA   s   123
45)r@   r   r.   r   rl   r(   )r+   r  rx  r   r   r   test_rawio_write_through  s    



z*TextIOWrapperTest.test_rawio_write_throughc                s   g g   G  fddd| j }|  }d}||t|d }| j|ddd}|d}|| |   |  | |	 d g ||d	  |  | |	 |d
  d S )Nc                   s,   e Zd Z fddZ fddZ  ZS )zBTextIOWrapperTest.test_bufio_write_through.<locals>.BufferedWriterc                s    d t j||S )NT)r-   rH   rU   )r+   re   r  )rI   flush_calledr   r   rU     s    
zHTextIOWrapperTest.test_bufio_write_through.<locals>.BufferedWriter.flushc                s    d t j||S )NT)r-   rH   r.   )r+   re   r  )rI   write_calledr   r   r.     s    
zHTextIOWrapperTest.test_bufio_write_through.<locals>.BufferedWriter.write)r   r   r   rU   r.   rK   r   )r  r  )rI   r   r     s   r   r  rG   r   T)r   rU  rA   r   r  )
r   r   r   r   r:  r.   r  r  r   r   )r+   r   r  r   r_  rt  r  r   )r  r  r   test_bufio_write_through  s     





z*TextIOWrapperTest.test_bufio_write_throughc             C   s  |  g }| j|ddd}|d |jdd | |jd | d|jd |d	 | d|jd
 |jdd | |jd |d |  | d|jd |  |jd d | |jd |jdd |  |jd d | |jd d S )Nr   rW  )r   r3  r  T)rU  rA   r  Z23s   123FZ45s   12345)	r@   r   r.   r[  r   rU  rl   r(   rU   )r+   r  r  r   r   r   test_reconfigure_write_through  s(    



z0TextIOWrapperTest.test_reconfigure_write_throughc             C   s`   |  | d}| t|jd |  | d}| t|j |  | d}| t|j d S )Nr   r7   )r   r   r   r   rC   r   )r+   r  r   r   r   test_read_nonbytes	  s    z$TextIOWrapperTest.test_read_nonbytesc          	   C   sL   t d}t|dd tjtddd}W d Q R X | t|j	d d S )Nrot13_is_text_encodingTs   foo)r   Zbar)
r*  rD  r   rp  rg   r   r   r   r   r.   )r+   r  r  r   r   r   test_illegal_encoder  s    
z&TextIOWrapperTest.test_illegal_encoderc                s   fdd   } t|jd   } t|j   } t|j  fdd}|d} t|jd |d} t|jd |d} t|jd d S )	Nc                 s8   t d} d| _z j dddd}W d d| _X |S )NquopriTs   aaaaaarW  )r3  r   F)r*  rD  r  r   r   )r  r  )r+   r   r   _make_illegal_wrapper  s    
zETextIOWrapperTest.test_illegal_decoder.<locals>._make_illegal_wrapperr7   c          	      sH   G fddd  fdd}t d}t|d|  S Q R X d S )Nc                   s   e Zd Z fddZdS )z^TextIOWrapperTest.test_illegal_decoder.<locals>._make_very_illegal_wrapper.<locals>.BadDecoderc                s    S )Nr   )r+   )getstate_ret_valr   r   r0  4  s    zgTextIOWrapperTest.test_illegal_decoder.<locals>._make_very_illegal_wrapper.<locals>.BadDecoder.getstateN)r   r   r   r0  r   )r  r   r   
BadDecoder3  s   r  c                s     S )Nr   )rm  )r  r   r   _get_bad_decoder6  s    zdTextIOWrapperTest.test_illegal_decoder.<locals>._make_very_illegal_wrapper.<locals>._get_bad_decoderr  rB  )r*  rD  r   rp  )r  r  r  )r  )r  r  r   _make_very_illegal_wrapper2  s    
zJTextIOWrapperTest.test_illegal_decoder.<locals>._make_very_illegal_wrapperr0   r   )r7   rG   )r   r   rC   r   )r+   r  r  r   )r  r+   r   test_illegal_decoder  s    

z&TextIOWrapperTest.test_illegal_decoderc             K   s    | j j}dj||d}td|S )Na  if 1:
            import codecs
            import {iomod} as io

            # Avoid looking up codecs at shutdown
            codecs.lookup('utf-8')

            class C:
                def __init__(self):
                    self.buf = io.BytesIO()
                def __del__(self):
                    io.TextIOWrapper(self.buf, **{kwargs})
                    print("ok")
            c = C()
            )iomodr  z-c)rg   r   r  r   )r+   r  r  coder   r   r   _check_create_at_shutdownC  s    z+TextIOWrapperTest._check_create_at_shutdownc             C   s>   |   \}}}|r&| | j|  n| d|   d S )Nok)r  r{  shutdown_errorr:  r   ri  )r+   rcoutrv  r   r   r   (test_create_at_shutdown_without_encodingX  s    z:TextIOWrapperTest.test_create_at_shutdown_without_encodingc             C   s6   | j ddd\}}}| | | d|   d S )Nzutf-8r)  )r   r  r  )r  r  r   r:  ri  )r+   r  r  rv  r   r   r   %test_create_at_shutdown_with_encodingb  s    
z7TextIOWrapperTest.test_create_at_shutdown_with_encodingc             C   s@   t d}| |d}t|  }| |d|d d S )Ns   Just some random string
zutf-8r  )MemviewBytesIOr   _to_memoryviewr   r   r   rC   r:  )r+   r   r  Z	bytes_valr   r   r   test_read_byteslikei  s    z%TextIOWrapperTest.test_read_byteslikec          	   C   sj   G dd dt }x:tdD ].}y| j| dd W q tk
rF   Y qX qW dd |_| j| dd}d S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z,TextIOWrapperTest.test_issue22849.<locals>.Fc             S   s   dS )NTr   )r+   r   r   r   r2   v  rA   z5TextIOWrapperTest.test_issue22849.<locals>.F.readablec             S   s   dS )NTr   )r+   r   r   r   r/   w  rA   z5TextIOWrapperTest.test_issue22849.<locals>.F.writablec             S   s   dS )NTr   )r+   r   r   r   r3   x  rA   z5TextIOWrapperTest.test_issue22849.<locals>.F.seekableN)r   r   r   r2   r/   r3   r   r   r   r   Fu  s   r  r   zutf-8)r   c             S   s   dS )Nr   r   )r~  r   r   r   r    rA   z3TextIOWrapperTest.test_issue22849.<locals>.<lambda>)objectr   r   r  r6   )r+   r  r   r  r   r   r   test_issue22849t  s    

z!TextIOWrapperTest.test_issue22849c          	   C   s   d dd d }| |}| j|ddd}| | d | | j |jdd W d Q R X | | j |jd d	 W d Q R X d S )
Nu   abcé
rG  u   déf
utf8rW  )r   r3  zutf-8)r   )r3  )r=  r   r   r   r   r   rd   r[  )r+   r   r  rx  r   r   r   test_reconfigure_encoding_read  s    
z0TextIOWrapperTest.test_reconfigure_encoding_readc             C   sT   |   }| j|ddd}|d |jdd |d |  | | d d S )	Nr   rW  )r   r3  zfoo
z	utf-8-sig)r   u   é
s   foo
é
)r   r   r.   r[  rU   r   r   )r+   r  rx  r   r   r    test_reconfigure_write_fromascii  s    

z2TextIOWrapperTest.test_reconfigure_write_fromasciic             C   s   |   }| j|ddd}|d |jdd | | d |d |  | | d	 |   }| j|d
dd}|d |jdd |d |  | | d d S )NrG  rW  )r   r3  u   abcé
zutf-8)r   s   abc
u   déf
s
   abc
déf
r   zabc
z	utf-8-sigs	   abc
déf
)r   r   r.   r[  r   r   rU   )r+   r  rx  r   r   r   test_reconfigure_write  s    



z(TextIOWrapperTest.test_reconfigure_writec             C   sd   |   }dd |_d |_| j|ddd}|d |jdd |d	 |  | | d
 d S )Nc               S   s   dS )NFr   r   r   r   r   r    rA   zGTextIOWrapperTest.test_reconfigure_write_non_seekable.<locals>.<lambda>r   rW  )r   r3  zabc
z	utf-8-sig)r   u   déf
s   abc
﻿déf
)	r   r3   r5   r   r.   r[  rU   r   r   )r+   r  rx  r   r   r   #test_reconfigure_write_non_seekable  s    


z5TextIOWrapperTest.test_reconfigure_write_non_seekablec             C   s   |  |  ddd}|jd d | |jd | |jd |d |jdd | |jd | |jd |jdd	 | |jd | |jd |d
 |jdd d | |jd |d | | d | |	 
 d d S )Nr   rl  rW  )r   zLF
z
)r3  r   )r  zCRLF
zutf-8)r   r3  r)  r   zLF
CRLF
s	   LF
CRLF
)r   r   r[  r   r   r  r.   r5   rC   r\  r   )r+   rx  r   r   r   test_reconfigure_defaults  s"    


z+TextIOWrapperTest.test_reconfigure_defaultsc             C   s  |  d}| j|ddd}|jd d | | d |  d}| j|ddd}|jdd | | d |  d}| j|dd	d}|jdd | | d
 |  d}| j|ddd}|jd	d | | d |  d}| j|dd	d}|jdd | | d | j|   dd	d}|jd d |d |jdd |d |jdd |d |jd	d |d |jdd |d dtj d }| | 	 
d| d S )Ns   CREOFr   rW  )r3  zCR
r   zCRs	   CRLF
EOFrq  zCRLF
s	   LF
CREOFzLF
CRs   CRCRLF
EOFz
z	CRCRLF
zlinesep
zLF
zCRLF
rz  zLF
LF
CRCRLF
)r   r   r[  r   r   r.   r   rz  r\  r   r:  )r+   r  rx  r  r   r   r   test_reconfigure_newline  sB    









z*TextIOWrapperTest.test_reconfigure_newlinec             C   sb   | j | ddd}|d |  |  | j | ddd}|d |d |  d S )Ns   testr   )r   r7   r~  )r   r   rC   r6   r.   )r+   r  r   r   r   test_issue25862  s    


z!TextIOWrapperTest.test_issue25862N)Sr   r   r   rz   r{   r  r  rT  r^  ro  rr  rY  r\  r  r  r   r"  	utf8_moderg  r   r  ri  rk  rn  rp  ru  ry  r}  r   rc  rl  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  
no_tracingr  r  rw  r|  r  r}  r  r  r  r  r  r  r  r  r  Zrequires_type_collectingr  r  r  r  r  r  r  r  r  r  r  r   r   r   r   rN  	  s   	

	*/
	

 
(
$rN  c                   s,   e Zd ZdZ fddZ fddZ  ZS )r  zOA BytesIO object whose read method returns memoryviews
       rather than bytesc                s   t t |S )N)r  rH   r6  )r+   len_)rI   r   r   r6    s    zMemviewBytesIO.read1c                s   t t |S )N)r  rH   rC   )r+   r  )rI   r   r   rC     s    zMemviewBytesIO.read)r   r   r   r=   r6  rC   rK   r   r   )rI   r   r    s   r  c             C   s:   t  d}t| t| |j  }|| d|  t|S )z6Convert bytes-object *buf* to a non-trivial memoryviewr   N)r   r   itemsizeZ	frombytesr   )r9   Zarridxr   r   r   r    s    
r  c               @   s4   e Zd ZeZdZdd Zdd Zdd Zdd	 Zd
S )CTextIOWrapperTestz,RuntimeError: could not find io module statec             C   sb   |  d}| |d}| |}| jt|j|dd | t|j | j| j}| tt	| d S )Ns   é

i  rf  )r3  )
r   r   r   r   rs   r,   rC   r  r  r]  )r+   r   r
   r  r   r   r   r    s    

z&CTextIOWrapperTest.test_initializationc          	   C   s   t dtfP tt jd}| |}| j|dd}|d ||_	t
|}~t   W d Q R X | | | | t jd}| | d W d Q R X d S )Nr   r   r   )r   Z456defr   s   456def)r   r   r   rg   r   ry   r   r   r.   r~  r  r  r   r  r    r   rC   )r+   r  r
   r  r  r#   r   r   r   r	  %  s    


z*CTextIOWrapperTest.test_garbage_collectionc             C   sn   x`t dD ]T}| |  |  }| j|dd}| |  |  }| j|dd}||_||_q
W t  d S )Ni  r   )r   )r   r   r@   r   Zbuddyr   r   )r+   r   Zb1Zt1Zb2Zt2r   r   r   !test_rwpair_cleared_before_textio6  s    
z4CTextIOWrapperTest.test_rwpair_cleared_before_textioc          	   C   s0   | j |  dd}| t
 |`W d Q R X d S )Nr   )r   )r   r   r   r   r"   )r+   r  r   r   r    test_del__CHUNK_SIZE_SystemErrorD  s    z3CTextIOWrapperTest.test_del__CHUNK_SIZE_SystemErrorN)	r   r   r   rg   r  r  r	  r  r  r   r   r   r   r    s   
r  c               @   s   e Zd ZeZdZdS )PyTextIOWrapperTestz$LookupError: unknown encoding: asciiN)r   r   r   ri   rg   r  r   r   r   r   r  J  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 )IncrementalNewlineDecoderTestc                s   fdd}|dd |dd |dd |dd |dd |dd |dd |dd j t j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 |dd |dd |dd |dd d S )Nc                sB      } j| f||  |  j| f|| d S )N)r0  r   r:  r3  )r
   rm   r  r2  )decoderr+   r   r   _check_decodeS  s    
zPIncrementalNewlineDecoderTest.check_newline_decoding_utf8.<locals>._check_decodes   袈u   袈   r         rA   T)r8  r$  rW  rv  r  z
as   
z

s   
as   袈
u   袈
s   袈)r   UnicodeDecodeErrorr:  r,  )r+   r  r  r   )r  r+   r   check_newline_decoding_utf8Q  s4    



















z9IncrementalNewlineDecoderTest.check_newline_decoding_utf8c                s  g |d k	r*t |  fdd}nd  fdd}|  jd  |d |  jd |d |  jd |d |  jd |d	 |  jd
 |d | dd    d	}d k	r  |}|  |d	 |  jd  d S )Nc                s.   x( | D ]} t|g qW d S )N)r=  r-   r:  r   )rm   r
   )r  encoderrD  r   r   _decode_bytewise  s    zNIncrementalNewlineDecoderTest.check_newline_decoding.<locals>._decode_bytewisec                s"   x| D ]}  | qW d S )N)r-   r:  )rm   r  )r  rD  r   r   r    s    
zabc
rW  z
abc)rW  z
zabcr  )rq  rW  z
r   zabc

abcabc
abcabc)r*  getincrementalencoderr   newlinesrl   r,  r=  r:  )r+   r  r   r  r7  r   )r  r  rD  r   check_newline_decoding{  s0    
z4IncrementalNewlineDecoderTest.check_newline_decodingc             C   st   d}x6|D ].}|ot | }| j|dd}| || q
W t d }| j|dd}| | | t|jd d S )N)	Nzutf-8zlatin-1zutf-16z	utf-16-lez	utf-16-bezutf-32z	utf-32-lez	utf-32-beT)	translatezutf-8r0   )r*  getincrementaldecoderIncrementalNewlineDecoderr  r  r   r   r3  )r+   rs  r  r  r   r   r   test_newline_decoder  s    

z2IncrementalNewlineDecoderTest.test_newline_decoderc                s<    fdd} j d dd}||  j d dd}|| d S )Nc                sR     | jd    | dd   | jd    | dd   | jd  d S )Nu   ഀu   ਀)r   r  r:  )dec)r+   r   r   _check  s
    z@IncrementalNewlineDecoderTest.test_newline_bytes.<locals>._checkF)r  T)r  )r+   r  r  r   )r+   r   test_newline_bytes  s
    z0IncrementalNewlineDecoderTest.test_newline_bytesc             C   s`   x.dD ]&}t d }| ||}| | qW t d }| j|dd}| |dd d S )N)r,  rq   r7   rG   zutf-8r   )r  s   
z
)r*  r  r  r  r   r:  )r+   r  r  r   r   r   test_translate  s    
z,IncrementalNewlineDecoderTest.test_translateN)r   r   r   r  r  r  r  r  r   r   r   r   r  O  s
   *!r  c               @   s   e Zd ZdS )CIncrementalNewlineDecoderTestN)r   r   r   r   r   r   r   r     s   r   c               @   s   e Zd ZdS )PyIncrementalNewlineDecoderTestN)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eeed!d"d#d$ Zd%d& Zd'd( Zd)d* Zd+S ),
MiscIOTestc             C   s   t t j d S )N)r   rx   ry   )r+   r   r   r   r{     s    zMiscIOTest.tearDownc             C   s   xz| j jD ]n}t| j |d }| || |dkr4q
q
d| ksH|dkr\| t|t| q
|ds
| t|| j	 q
W d S )Nr    errorrd   ZSEEK_)
rg   __all__r8  rj  lowerr  
issubclassr  r   r   )r+   rK  r   r   r   r   test___all__  s    
zMiscIOTest.test___all__c          	   C   sh  | j tjddd}| |jd |  tdtf |  tjd}W d Q R X | |jtj | |j	jtj | |j	j
jtj | |jd | |j	jd | |j	j
jd |  |  tjd}| |jd | |j	jd | |j	j
jd | j | dd	d
}| |jd | |j
jd | |j|  | |j
j|  |  |  d S )Nr   r   )r   r   Ur   zw+zrb+F)r   )r    r   ry   r   r   r[   r   r   rK  r   r  r1   )r+   r#   gr   r   r   test_attributes  s.    zMiscIOTest.test_attributesc             C   s  xddiddidddddddddddidd	iddddddd	dddd
iddid
ddd
dddddgD ]}| j tjf|}|  | t|j | t|j | t|j | t|j	 t
|dr| t|jd | t|j t
|dr| t|jd | t|j t
|dr6| t|j t
|drV| t|jtd t
|drv| t|jtd | t|j | t|j | t|jd | t|jd | t|j | t|j | t|jd|d krdnd | t|jg  | tt| qtW d S )Nr   r   r   r7   )r   r   rG   r   r   r   zw+zw+br  r6  i   rA  r;   r   r
   rA   r   )r    r   ry   r[   r   rs   rU   r1   r  __iter__r   r  rC   r6  rA  r;   r   r   r   r   r5   r6   r<   r.   r   r   )r+   r  r#   r   r   r   test_io_after_close  sX    

zMiscIOTest.test_io_after_closec             C   sX   G dd dt }|d}| d|}||_||_t|}~~t  | | | d S )Nc               @   s   e Zd ZdS )z*MiscIOTest.test_blockingioerror.<locals>.CN)r   r   r   r   r   r   r   C*  s   r  r   r7   )	r(  ru   r
   r  r  r  r   r   r  )r+   r  r  r
   r  r   r   r   test_blockingioerror(  s    
zMiscIOTest.test_blockingioerrorc             C   sD   |  | jtj |  | jtj |  | jtj |  | jtj d S )N)rt  r   r  ABCMetar   r   r   )r+   r   r   r   	test_abcs5  s    zMiscIOTest.test_abcsc          	   C   s   | j tjddd>}| ||j | ||j | ||j | ||j W d Q R X |  tjd>}| ||j | ||j | ||j | ||j W d Q R X |  tjd>}| ||j | ||j | ||j | ||j W d Q R X d S )Nr   r   )r   r   )	r    r   ry   rt  r   r   ZassertNotIsInstancer   r   )r+   Z	abcmoduler#   r   r   r   _check_abc_inheritance<  s    z!MiscIOTest._check_abc_inheritancec             C   s   |  |  d S )N)r  )r+   r   r   r   test_abc_inheritanceM  s    zMiscIOTest.test_abc_inheritancec             C   s   |  t d S )N)r  rg   )r+   r   r   r   test_abc_inheritance_officialQ  s    z(MiscIOTest.test_abc_inheritance_officialc          	   O   sP   t ||}t|}| t}d }t  W d Q R X | |t|jj	d  d S )Nr   )
r    r]  ZassertWarnsr   r   r   r{  r(  Zwarningre   )r+   re   r  r#   r   r*  r   r   r   _check_warn_on_deallocV  s    
z!MiscIOTest._check_warn_on_deallocc             C   s2   | j tjddd |  tjd |  tjd d S )Nr   r   )r   r   )r  r   ry   )r+   r   r   r   test_warn_on_dealloc^  s    zMiscIOTest.test_warn_on_deallocc          	      s   g   fdd}|  | t \}} ||f7  | j|f|| t \}} ||f7  t|  t|f|ddi| W d Q R X d S )Nc                 sR   xL D ]D} yt |  W q tk
rH } z|jtjkr8 W d d }~X Y qX qW d S )N)r   r[   rZ   errnoEBADF)r  r  )fdsr   r   cleanup_fdse  s    
z9MiscIOTest._check_warn_on_dealloc_fd.<locals>.cleanup_fdsr   F)r   r   r   r  r   r1  r    )r+   re   r  r  r   r   r   )r  r   _check_warn_on_dealloc_fdc  s    
z$MiscIOTest._check_warn_on_dealloc_fdc             C   s&   | j ddd |  d |  d d S )Nr   r   )r   r   )r  )r+   r   r   r   test_warn_on_dealloc_fdv  s    
z"MiscIOTest.test_warn_on_dealloc_fdc             C   s   xddiddidddddiddidddddidd	id	ddg	D ]J}xDt tjd
 D ]2}| jtjf|}| ttj|| W d Q R X qVW qBW d S )Nr   r   r   r   )r   r   r   r   zw+zw+br7   )	r   pickleZHIGHEST_PROTOCOLr    r   ry   r   r   dumps)r+   r  Zprotocolr#   r   r   r   test_pickling|  s    zMiscIOTest.test_picklingc             C   s   |  d d S )Ni @  )_test_nonblock_pipe_write)r+   r   r   r   test_nonblock_pipe_write_bigbuf  s    z*MiscIOTest.test_nonblock_pipe_write_bigbufc             C   s   |  d d S )Ni   )r  )r+   r   r   r   !test_nonblock_pipe_write_smallbuf  s    z,MiscIOTest.test_nonblock_pipe_write_smallbufset_blockingz(os.set_blocking() required for this testc             C   s  g }g }t  \}}t |d t |d | j|dd|d}| j|dd|d}| |r xdD ]}y@d}	x6t|	d d	 g| }
||
 ||
 |	d
7 }	qrW W qf | jk
r4 } zh| |j	d t
j | |j	d |j |d d |j |d< ||  d}
||
 ||
 W d d }~X Y qfX qfW xy|  P W nj | jk
r } zH| |j	d t
j | |j	d |j | |jd ||  W d d }~X Y nX q<W |t|jd 7 }W d Q R X W d Q R X d|d| }}| || | |j | |j d S )NFr   T)r   r   r   r   )i'  I   i  r      a   r7   rG   rq   s   BLOCKEDrA   )r   r   r"  r    r   r-   r.   ru   r   re   r  ZEAGAINr  rC   rU   iterrl   r  rY   )r+   r   ZsentZreceivedr   r   ZrfZwfr  r   rT  r  r   r   r   r    sL    



 &$z$MiscIOTest._test_nonblock_pipe_writec          	   C   s2   |  tjd W d Q R X | t| j tjd d S )Nr   r~  )r    r   ry   r   FileExistsError)r+   r   r   r   test_create_fail  s    
zMiscIOTest.test_create_failc          	   C   sR   |  tjd}|d W d Q R X |  tjd}| d|  W d Q R X d S )NZxbs   spamr   )r    r   ry   r.   r   rC   )r+   r#   r   r   r   test_create_writes  s    zMiscIOTest.test_create_writesc             C   s   |  t| jtjd d S )Nzrwax+)r   rs   r    r   ry   )r+   r   r   r   test_open_allargs  s    zMiscIOTest.test_open_allargsN)r   r   r   r{   r  r
  r  r  r  r  r  r  r  r  r  r  r  r   r!  r  Z
skipUnlessr   r   r  r(  r)  r*  r   r   r   r   r    s*   /3r  c               @   s0   e Zd ZeZdd Zdd Zdd Zdd Zd	S )
CMiscIOTestc             C   s6   G dd d| j j}| }td}| t|j| d S )Nc               @   s   e Zd ZdddZdS )z<CMiscIOTest.test_readinto_buffer_overflow.<locals>.BadReaderrq   c             S   s   dd S )Nr   i@B r   )r+   r:   r   r   r   rC     s    zACMiscIOTest.test_readinto_buffer_overflow.<locals>.BadReader.readN)rq   )r   r   r   rC   r   r   r   r   	BadReader  s   r,  rG   )rg   r   r   r   rs   r;   )r+   r,  r_  r
   r   r   r   test_readinto_buffer_overflow  s    z)CMiscIOTest.test_readinto_buffer_overflowc             C   s\   d t }td|\}}|j }|jdkrHd t }| || n| |d d S )Na  if 1:
            import sys
            import time
            import threading
            from test.support import SuppressCrashReport

            file = sys.{stream_name}

            def run():
                while True:
                    file.write('.')
                    file.flush()

            crash = SuppressCrashReport()
            crash.__enter__()
            # don't call __exit__(): the crash occurs at Python shutdown

            thread = threading.Thread(target=run)
            thread.daemon = True
            thread.start()

            time.sleep(0.5)
            file.write('!')
            file.flush()
            z-cr   zFatal Python error: could not acquire lock for <(_io\.)?BufferedWriter name='<{stream_name}>'> at interpreter shutdown, possibly due to daemon threadsz.!)	
format_maplocalsr   rv  r:  r  rn  r  ri  )r+   Zstream_namer  r`   r  rv  patternr   r   r   &check_daemon_threads_shutdown_deadlock  s    

z2CMiscIOTest.check_daemon_threads_shutdown_deadlockc             C   s   |  d d S )Nstdout)r1  )r+   r   r   r   ,test_daemon_threads_shutdown_stdout_deadlock  s    z8CMiscIOTest.test_daemon_threads_shutdown_stdout_deadlockc             C   s   |  d d S )Nrg  )r1  )r+   r   r   r   ,test_daemon_threads_shutdown_stderr_deadlock  s    z8CMiscIOTest.test_daemon_threads_shutdown_stderr_deadlockN)r   r   r   rg   r-  r1  r3  r4  r   r   r   r   r+    s
   	(r+  c               @   s   e Zd ZeZdS )PyMiscIOTestN)r   r   r   ri   rg   r   r   r   r   r5    s   r5  ntz%POSIX signals required for this test.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	e
j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 )"SignalsTestc             C   s   t  t j| j| _d S )N)signalSIGALRMalarm_interruptoldalrm)r+   r   r   r   rz     s    zSignalsTest.setUpc             C   s   t  t j| j d S )N)r8  r9  r;  )r+   r   r   r   r{     s    zSignalsTest.tearDownc             C   s   dd  d S )Nr7   r   r   )r+   sigframer   r   r   r:  !  s    zSignalsTest.alarm_interruptc       
         sf  g  fdd}t j|d}d|_t \ }d|d< |tjt| d  }z| jj	|f|}t
tdrttjtjg |  ttjtjg n|  td z| t|j| W d	td
 |  X t d | |d
d |dd g W d	t| t  y|  W n4 tk
r^ }	 z|	jtjkrN W d	d	}	~	X Y nX X d	S )zCheck that a partial write, when it gets interrupted, properly
        invokes the signal handler, and bubbles up the exception raised
        in the latter.c                 s   t  d} |  d S )Nr7   )r   rC   r-   )rm   )r   read_resultsr   r   r  )  s    z2SignalsTest.check_interrupted_write.<locals>._read)r   TFr   r7   pthread_sigmaskNr   rG   )rO   r   daemonr   r   r   PIPE_MAX_SIZEr   rg   r    r   r8  r?  	SIG_BLOCKr9  r   SIG_UNBLOCKalarmr   r   r.   rl   r-   rC   r   r[   rZ   r  r  )
r+   itemr   fdopen_kwargsr  r  r   
large_datawior  r   )r   r>  r   check_interrupted_write$  s8    



$

z#SignalsTest.check_interrupted_writec             C   s   | j ddddd d S )Ns   xyr   r   )r   r   )rI  )r+   r   r   r   !test_interrupted_write_unbufferedX  s    z-SignalsTest.test_interrupted_write_unbufferedc             C   s   | j dddd d S )Ns   xyr   )r   )rI  )r+   r   r   r   test_interrupted_write_buffered[  s    z+SignalsTest.test_interrupted_write_bufferedc             C   s   | j ddddd d S )Nxys   xyr   r   )r   r   )rI  )r+   r   r   r   test_interrupted_write_text^  s    z'SignalsTest.test_interrupted_write_textc       	   	      s    fdd}t  t j| t \}}| jj|f|zt d | tt	fD}x<x"t
dD ]}    q`W t|t d  qVW W d Q R X |j}t|t	r| t|dt| W d t d   t| X d S )Nc                 s      dd  d S )Nr7   r   )r.   )re   )r   rH  r   r   on_alarmc  s    
z3SignalsTest.check_reentrant_write.<locals>.on_alarmr7   r   zreentrant callr   )r8  r9  r   r   rg   r    rD  r   r   rq  r   r.   rU   rC   r   r)  r   r  r(  r   r[   )	r+   r   rF  rN  r   r   r*  r   r   r   )r   rH  r   check_reentrant_writea  s$    

"

z!SignalsTest.check_reentrant_writec             C   s   | j ddd d S )Ns   xyr   )r   )rO  )r+   r   r   r   test_reentrant_write_buffered}  s    z)SignalsTest.test_reentrant_write_bufferedc             C   s   | j dddd d S )NrL  r   r   )r   r   )rO  )r+   r   r   r   test_reentrant_write_text  s    z%SignalsTest.test_reentrant_write_textc          	      s   t  \} d|d<  fdd}ttj| z@| jj|f|}t  d td | ||	dd W d	td
 |
  t 
  t 
| X d	S )zCheck that a buffered read, when it gets interrupted (either
        returning a partial result or EINTR), properly invokes the signal
        handler and retries if the latter returned successfully.Fr   c                s   t  d d S )Ns   bar)r   r.   )r<  r=  )r   r   r   alarm_handler  s    z?SignalsTest.check_interrupted_read_retry.<locals>.alarm_handlers   foor7   r|   ZfoobarNr   )r   r   r8  r9  rg   r    r.   rD  r   rC   r[   )r+   r:  rF  r   rR  Zrior   )r   r   check_interrupted_read_retry  s    


z(SignalsTest.check_interrupted_read_retryc             C   s   | j dd dd d S )Nc             S   s
   |  dS )NrG  )r:  )r~  r   r   r   r    rA   zBSignalsTest.test_interrupted_read_retry_buffered.<locals>.<lambda>r   )r   )rS  )r+   r   r   r   $test_interrupted_read_retry_buffered  s    
z0SignalsTest.test_interrupted_read_retry_bufferedc             C   s   | j dd dd d S )Nc             S   s   | S )Nr   )r~  r   r   r   r    rA   z>SignalsTest.test_interrupted_read_retry_text.<locals>.<lambda>r   )r   )rS  )r+   r   r   r    test_interrupted_read_retry_text  s    
z,SignalsTest.test_interrupted_read_retry_textc                s\  t dt j}t \}d|d< g ddfdd}tj|dd_ fd	d
}fdd || }ttj	| zl| j
j|f|}td ||}	| ||	 |  d  |  | |tdd D  W dtd dt| t y|  W n4 tk
rT }
 z|
jtjkrD W dd}
~
X Y nX X dS )zCheck that a buffered write, when it gets interrupted (either
        returning a partial result or EINTR), properly invokes the signal
        handler and retries if the latter returned successfully.selectFr   Nc           
      sp   yDx>s@x4 gg g dd kr<td} |  q
W qW W n& tk
rj } z| W d d }~X Y nX d S )Ng      ?r   i   )rV  r   rC   r-   BaseException)rm   r   )r  r   r>  rV  write_finishedr   r   r    s    z8SignalsTest.check_interrupted_write_retry.<locals>._read)r   Tc                s   t  t j  t d d S )Nr7   )r8  r9  rD  )r<  r=  )alarm2r   r   alarm1  s    z9SignalsTest.check_interrupted_write_retry.<locals>.alarm1c                s       d S )N)r   )r<  r=  )r  r   r   rY    s    z9SignalsTest.check_interrupted_write_retry.<locals>.alarm2r7   c             s   s   | ]}t |V  qd S )N)r   )r  r~  r   r   r   	<genexpr>  s    z<SignalsTest.check_interrupted_write_retry.<locals>.<genexpr>r   )r   import_modulerA  r   r   rO   r   r@  r8  r9  rg   r    rD  r.   r   rU   rl   r  sumr[   rZ   r  r  )r+   rE  rF  r  r   r  rZ  rG  rH  r  r  r   )rY  r  r   r>  rV  r  rX  r   check_interrupted_write_retry  sB    
	





z)SignalsTest.check_interrupted_write_retryc             C   s   | j ddd d S )Nr   r   )r   )r^  )r+   r   r   r   %test_interrupted_write_retry_buffered  s    z1SignalsTest.test_interrupted_write_retry_bufferedc             C   s   | j dddd d S )Nr~  r   rG  )r   r   )r^  )r+   r   r   r   !test_interrupted_write_retry_text  s    z-SignalsTest.test_interrupted_write_retry_textN)r   r   r   rz   r{   r:  rI  rJ  rK  rM  r   r  rO  rP  rQ  rS  rT  rU  r^  r_  r`  r   r   r   r   r7    s    4Dr7  c               @   s   e Zd ZeZdS )CSignalsTestN)r   r   r   rg   r   r   r   r   ra    s   ra  c               @   s   e Zd ZeZdZdZdS )PySignalsTestN)r   r   r   ri   rg   rP  rQ  r   r   r   r   rb    s   rb  c        
         s,  t ttttttttt	t
tttttttttf}ttttttttf}tjdg }dd |D }dd |D }t  | fdd|D  | fdd|D  t j!|d< xj|D ]b}|j"#d	rxP|$ D ]\}}t%||| qW q|j"#d
rx |$ D ]\}}t%||| qW qW t&'dd |D }	|	S )Nr  c             S   s   i | ]}t t||qS r   )r8  rg   )r  rK  r   r   r   
<dictcomp>  s    zload_tests.<locals>.<dictcomp>c             S   s   i | ]}t t||qS r   )r8  ri   )r  rK  r   r   r   rc    s    c             3   s"   | ]}|j  d |j   fV  qdS )r  N)r   )r  r~  )globsr   r   r[    s    zload_tests.<locals>.<genexpr>c             3   s"   | ]}|j  d |j   fV  qdS )PyN)r   )r  r~  )rd  r   r   r[    s    r    r  re  c             S   s   g | ]}t |qS r   )r  Z	makeSuite)r  r   r   r   r   r    s    zload_tests.<locals>.<listcomp>)(rO  rQ  rR  r  r  r  r  r  r  r&  r'  rJ  r   r  r  r  r+  r5  ra  rb  r@   rF   r^   rX   rj   rc   r%   rN   rg   r  globalsre  ri   OpenWrapperr   r   r|  setattrr  Z	TestSuite)
re   r   ZmocksZall_membersZc_io_nsZpy_io_nsr   rK  r   Zsuiter   )rd  r   
load_tests  s:    

ri  __main__)qr=   r  r   r  rb  r   r  r  r8  r   Z	sysconfigrO   rS   r  r   r  collectionsr   r   	itertoolsr   r   r   r   Ztest.support.script_helperr   r   Ztest.supportr	   r*  rg   Z_pyiori   r   ImportErrorr   Z	Structurer   Zget_config_varZ_cflagsZ_config_argsr  r$   r%   r   r>   r?   r@   rD   rE   rF   rL   rM   rN   rV   rW   rX   r\   r]   r^   r   ra   rb   rc   rf   rh   rj   rt   rv   ZTestCaserw   rO  rQ  r  rR  rY  r  r  r  r  r  r  r  r  r  r  r  r&  r'  r+  r(  registerrH  rJ  rN  r  r  r  r  r  r   r  r  r+  r5  r  rK  r7  ra  rb  ri  r   mainr   r   r   r   <module>   s   
8
	,     2
 (   3  %0 > fW+        I
	3u  ;
 V	%
