B
    u9aßk  ã               @   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	m
Z
mZ ddlmZ ddlmZmZ ddlmZ e d¡ZdZd	Zej ej¡d
 ZG dd„ dejƒZG dd„ dejƒZG dd„ deƒZG dd„ deƒZ dd„ Z!G dd„ dejƒZ"ddd„Z#e$dkre#dd dS )z!Test script for the gzip module.
é    N)ÚPIPEÚPopen)Úsupport)Ú_4GÚ
bigmemtest)Úassert_python_okÚgzipsy     int length=DEFAULTALLOC, err = Z_OK;
  PyObject *RetVal;
  int flushmode = Z_FINISH;
  unsigned long start_total_out;

sŽ   /* zlibmodule.c -- gzip-compatible data compression */
/* See http://www.gzip.org/zlib/
/* See http://www.winimage.com/zLibDll for Windows */
z-gzdirc               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚUnseekableIOc             C   s   dS )NF© )Úselfr
   r
   ú/usr/lib/python3.7/test_gzip.pyÚseekable$   s    zUnseekableIO.seekablec             C   s
   t j‚d S )N)ÚioÚUnsupportedOperation)r   r
   r
   r   Útell'   s    zUnseekableIO.tellc             G   s
   t j‚d S )N)r   r   )r   Úargsr
   r
   r   Úseek*   s    zUnseekableIO.seekN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r
   r
   r
   r   r	   #   s   r	   c               @   s"   e Zd ZejZdd„ Zdd„ ZdS )ÚBaseTestc             C   s   t  | j¡ d S )N)r   ÚunlinkÚfilename)r   r
   r
   r   ÚsetUp1   s    zBaseTest.setUpc             C   s   t  | j¡ d S )N)r   r   r   )r   r
   r
   r   ÚtearDown4   s    zBaseTest.tearDownN)r   r   r   r   ÚTESTFNr   r   r   r
   r
   r
   r   r   .   s   r   c               @   sR  e Zd ZdSd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d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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„ Z*dPdQ„ Z+dRS )TÚTestGzipÚbc          	   C   sr   t |ƒ}t | jd| ¡}| |¡}W d Q R X |  |t|ƒ¡ t | jd| ¡}|  | ¡ |¡ W d Q R X d S )NÚwÚr)Úbytesr   ÚGzipFiler   ÚwriteÚassertEqualÚlenÚread)r   ÚdataÚmodeZb_dataÚfÚlr
   r
   r   Úwrite_and_read_back9   s    zTestGzip.write_and_read_backc          	   C   sd   t  | jd¡D}| td ¡ | ¡  | ¡  ttdƒrFt 	| ¡ ¡ | 
¡  W d Q R X | 
¡  d S )NÚwbé2   Úfsync)r   r!   r   r"   Údata1ÚflushÚfilenoÚhasattrÚosr-   Úclose)r   r(   r
   r
   r   Ú
test_writeA   s    
zTestGzip.test_writec          	   C   s¢   t  | j¡}t |d¡}| td ¡ W d Q R X |  |jt	¡ t |d¡}| t¡ W d Q R X t |¡}| 
¡ }W d Q R X |  |td ¡ |  |jt	¡ d S )Nr   r,   Úaé3   )ÚpathlibÚPathr   r   r!   r"   r.   ZassertIsInstanceÚnameÚstrr%   r#   )r   r   r(   Údr
   r
   r   Ú"test_write_read_with_pathlike_fileO   s    z+TestGzip.test_write_read_with_pathlike_filec             C   sD   |   ttd ƒ¡ tttdƒƒƒ}|jddddgd}|   |¡ d S )Nr,   é   ÚBé   é   )Úshape)r*   Ú
memoryviewr.   r    ÚrangeÚcast)r   Úmr&   r
   r
   r   Útest_write_memoryview^   s    zTestGzip.test_write_memoryviewc             C   s   |   ttd ƒ¡ d S )Nr,   )r*   Ú	bytearrayr.   )r   r
   r
   r   Útest_write_bytearrayd   s    zTestGzip.test_write_bytearrayc             C   s   |   t dtd ¡¡ d S )NÚIé(   )r*   Úarrayr.   )r   r
   r
   r   Útest_write_arrayg   s    zTestGzip.test_write_arrayc          
   C   s’   t  | jd¡P}|  t¡ | d¡ W d Q R X |  t¡ | g ¡ W d Q R X | t¡ W d Q R X t  | jd¡}|  | ¡ t¡ W d Q R X d S )Nr+   Ú Úrb)	r   r!   r   ÚassertRaisesÚ	TypeErrorr"   r.   r#   r%   )r   r(   r
   r
   r   Útest_write_incompatible_typej   s    z%TestGzip.test_write_incompatible_typec          	   C   s>   |   ¡  t | jd¡}| ¡ }W d Q R X |  |td ¡ d S )Nr   r,   )r4   r   r!   r   r%   r#   r.   )r   r(   r;   r
   r
   r   Ú	test_readw   s    zTestGzip.test_readc          	   C   s~   |   ¡  g }d}t | jd¡@}x8| ¡ }|s0P | |¡ |t|ƒ7 }|  | ¡ |¡ q"W W d Q R X |  d 	|¡t
d ¡ d S )Nr   r   ó    r,   )r4   r   r!   r   Úread1Úappendr$   r#   r   Újoinr.   )r   ZblocksÚnreadr(   r;   r
   r
   r   Ú
test_read1~   s    
zTestGzip.test_read1é   )ÚsizeZmemusec             C   s8   t jtdd}t jt |¡dd}|  | |¡t¡ d S )NrY   )ÚcompresslevelrN   )Úfileobjr'   )r   Úcompressr.   r!   r   ÚBytesIOr#   r%   )r   rZ   Ú
compressedr(   r
   r
   r   Útest_read_large   s    zTestGzip.test_read_largec          	   C   s  |   ¡  t | jd¡}|j}|  |j¡ | ¡  |  |j¡ |  	t
¡ | d¡ W d Q R X |  	t
¡ | d¡ W d Q R X |  	t
¡ | ¡  W d Q R X t | jd¡}|j}|  |j¡ | ¡  |  |j¡ |  	t
¡ | d¡ W d Q R X |  	t
¡ | ¡  W d Q R X d S )Nr   rY   r   r   rS   )r4   r   r!   r   r\   ÚassertFalseÚclosedr3   Ú
assertTruerO   Ú
ValueErrorr%   r   r   r"   r/   )r   r(   r\   r
   r
   r   Útest_io_on_closed_object•   s*    z!TestGzip.test_io_on_closed_objectc          	   C   sn   |   ¡  t | jd¡}| td ¡ W d Q R X t | jd¡}| ¡ }W d Q R X |  |td td  ¡ d S )NÚabé   rN   r,   )	r4   r   r!   r   r"   Údata2r%   r#   r.   )r   r(   r;   r
   r
   r   Útest_append±   s    zTestGzip.test_appendc          
   C   s¬   t  | jdd¡}| d¡ W d Q R X x8tddƒD ]*}t  | jdd¡}| d¡ W d Q R X q2W t  | jd¡(}d}x| d	¡}||7 }|svP qvW W d Q R X |  |d
¡ d S )Nr+   é	   ó   ar   éÈ   rf   rN   rS   i    sÉ   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)r   r!   r   r"   rC   r%   r#   )r   r(   ÚiZzgfileÚcontentsZztxtr
   r
   r   Útest_many_append»   s    
 zTestGzip.test_many_appendc          	   C   s~   t  | jd¡}| td ¡ W d Q R X t  | jd¡}|  | ¡ td ¡ W d Q R X |  t¡ t  | jd¡ W d Q R X d S )NÚxbr,   rN   )	r   r!   r   r"   r.   r#   r%   rO   ÚFileExistsError)r   r(   r
   r
   r   Útest_exclusive_writeÎ   s    zTestGzip.test_exclusive_writec          
   C   sb   |   ¡  t | jd¡*}t |¡}dd„ |D ƒ}W d Q R X W d Q R X |  |dtjdd ¡ d S )NrN   c             S   s   g | ]}|‘qS r
   r
   )Ú.0Úliner
   r
   r   ú
<listcomp>Ý   s    z1TestGzip.test_buffered_reader.<locals>.<listcomp>r,   T)Úkeepends)	r4   r   r!   r   r   ÚBufferedReaderr#   r.   Ú
splitlines)r   r(   r   Úlinesr
   r
   r   Útest_buffered_readerÖ   s
    "zTestGzip.test_buffered_readerc          	   C   sf   |   ¡  t | jd¡F}d}x:| |¡}|s6|dkr6P |  t|ƒ|k¡ |d d }qW W d Q R X d S )NrN   r   rY   r,   )r4   r   r!   r   Úreadlinerc   r$   )r   r(   Zline_lengthÚLr
   r
   r   Útest_readlineá   s    
 zTestGzip.test_readlinec          	   C   sb   |   ¡  t | jd¡}| ¡ }W d Q R X t | jd¡ }x| d¡}|g kr<P q<W W d Q R X d S )NrN   é–   )r4   r   r!   r   Ú	readlines)r   r(   r|   r
   r
   r   Útest_readlinesí   s    
 zTestGzip.test_readlinesc          	   C   s”   |   ¡  t | j¡v}xn| ¡ }| ¡ }|s.P | ¡ }| |¡ t|ƒdkrRd}nt|ƒ}| |¡}|  	|d |… |¡ | |¡ qW W d Q R X d S )Né
   )
r4   r   r!   r   r   r{   r   r$   r%   r#   )r   r(   ZoldposZline1ZnewposÚamountZline2r
   r
   r   Útest_seek_readù   s     

zTestGzip.test_seek_readc          	   C   sZ   |   ¡  t | j¡(}| d¡ |jddd | d¡}W d Q R X |  |tdd… ¡ d S )Nr   rY   )Úwhenceé   é   )r4   r   r!   r   r%   r   r#   r.   )r   r(   Úyr
   r
   r   Útest_seek_whence  s    
zTestGzip.test_seek_whencec          	   C   sH   t  | jd¡0}x(tdddƒD ]}| |¡ | d¡ qW W d Q R X d S )Nr   r   r=   é   s   GZ
)r   r!   r   rC   r   r"   )r   r(   Úposr
   r
   r   Útest_seek_write  s    
zTestGzip.test_seek_writec          	   C   sl   |   ¡  t | jd¡}|  |jjd¡ W d Q R X t | j¡ t | jd¡}|  |jjd¡ W d Q R X d S )Nr   rN   Úxrp   )	r4   r   r!   r   r#   Z	myfileobjr'   r   r   )r   r(   r
   r
   r   Ú	test_mode  s    zTestGzip.test_modec          
   C   sL   xFdD ]>}t  | j|¡&}|  t|dƒ¡ |  |j| j¡ W d Q R X qW d S )N)r+   rN   r9   )r   r!   r   rc   r1   r#   r9   )r   r'   r(   r
   r
   r   Útest_1647484%  s    
zTestGzip.test_1647484c          	   C   sJ   |   ¡  t | jd¡*}|  t|jdƒ¡ |  |jj| j¡ W d Q R X d S )NrN   r9   )	r4   r   r!   r   rc   r1   r\   r#   r9   )r   r(   r
   r
   r   Útest_paddedfile_getattr+  s    z TestGzip.test_paddedfile_getattrc          	   C   s†   d}t j| jd|d}| t¡ W d Q R X t  | j¡D}|  t|dƒ¡ |  |j¡ | 	¡ }|  
|t¡ |  
|j|¡ W d Q R X d S )NiÍ[r   )Úmtimer   )r   r!   r   r"   r.   rc   r1   ZassertIsNoner   r%   r#   )r   r   ÚfWriteÚfReadZdataReadr
   r
   r   Ú
test_mtime1  s    zTestGzip.test_mtimec          	   C   sP  d}t j| jd|d}| t¡ W d Q R X t| jdƒ}| d¡}|  |d¡ | d¡}|  |d¡ | d¡}|  |d¡ | d	¡}|  |t 	d
|¡¡ | d¡}|  |d¡ | d¡}	|  |	d¡ | j 
d¡d }
| t|
ƒ¡}|  ||
¡ | t | j¡jd ¡ | d	¡}|  |d¡ | d	¡}|  |t 	d
ttƒ¡¡ W d Q R X d S )NiÍ[r   )r   rN   é   s   ‹rY   ó   r@   z<ió   ó   ÿzLatin-1ó    r?   s   ¯×dƒ)r   r!   r   r"   r.   Úopenr%   r#   ÚstructÚpackÚencoder$   r   r2   ÚstatÚst_size)r   r   r‘   r’   ZidBytesZcmByteZ	flagsByteZ
mtimeBytesZxflByteZosByteZexpectedZ	nameBytesZ
crc32BytesZ
isizeBytesr
   r
   r   Útest_metadata<  s0    







zTestGzip.test_metadatac          	   C   s¸   t  | jd¡}| d¡ W d Q R X t  | jd¡}| ¡  y| W d Q R X W n tk
rb   Y nX |  d¡ y&t  | jd¡}dd  W d Q R X W n tk
r¨   Y nX |  d¡ d S )Nr+   s   xxxrN   z4__enter__ on a closed file didn't raise an exceptionrY   r   z1/0 didn't raise an exception)r   r!   r   r"   r3   rd   ZfailÚZeroDivisionError)r   r(   r
   r
   r   Útest_with_openi  s     
zTestGzip.test_with_openc          	   C   s‚   t  | jd¡}| td ¡ W d Q R X t| jdƒ}| d¡ W d Q R X t  | jd¡ }| ¡ }|  |td d¡ W d Q R X d S )Nr+   r,   rf   s2                                                     rN   zIncorrect data in file)r   r!   r   r"   r.   r™   r%   r#   )r   r(   r;   r
   r
   r   Útest_zero_padded_file~  s    zTestGzip.test_zero_padded_filec          	   C   sp   t d }tƒ }tj|dd}| |¡ W d Q R X | ¡ }t|ƒ}tj|dd}|  | ¡ |¡ W d Q R X d S )Nr,   r+   )r\   r'   rN   )r.   r	   r   r!   r"   Úgetvaluer#   r%   )r   ÚuncompressedÚbufr(   r_   r
   r
   r   Útest_non_seekable_fileŠ  s    zTestGzip.test_non_seekable_filec          	   C   sÄ   t d }t | jd¡}| |¡ W d Q R X dd„ }t | jd¡x}d|_d}xD|ƒ D ]:}| |¡}|dkrnP |  | t	|ƒ¡|¡ |t	|ƒ7 }qVW |  | d	¡d¡ |  |t	|ƒ¡ W d Q R X d S )
Nrl   r+   c              s   s&   x xt dddƒD ]
} | V  qW qW d S )Né   r,   r   )rC   )Únr
   r
   r   Úsizes™  s    z!TestGzip.test_peek.<locals>.sizesrN   é!   r   rS   éd   )
r.   r   r!   r   r"   Zmax_read_chunkÚpeekr#   r%   r$   )r   r¤   r(   r©   rW   r¨   Úsr
   r
   r   Ú	test_peek”  s    
zTestGzip.test_peekc          
   C   sf   t d  d¡jdd}|  ¡  t | jd¡0}tj|dd}|  	| 
¡ |¡ W d Q R X W d Q R X d S )Nr,   ÚasciiT)rv   r   )Úencoding)r.   Údecoderx   r4   r   r!   r   r   ÚTextIOWrapperr#   r   )r   ry   r(   Útr
   r
   r   Útest_textio_readlinesª  s
    zTestGzip.test_textio_readlinesc          
   C   sL   t  | jt jt jB ¡}t  |d¡ }tj|dd}W d Q R X W d Q R X d S )Nr+   r   )r\   r'   )r2   r™   r   ÚO_WRONLYÚO_CREATÚfdopenr   r!   )r   Úfdr(   Úgr
   r
   r   Útest_fileobj_from_fdopen²  s    z!TestGzip.test_fileobj_from_fdopenc             C   s®  t  | jd¡ ¡  t| jdƒÒ}t j|dd}|  |jt j¡ W d Q R X t j|dd}|  |jt j¡ W d Q R X t j|dd}|  |jt j¡ W d Q R X t j|dd}|  |jt j¡ W d Q R X |  	t
¡ t j|dd W d Q R X W d Q R X xLd	D ]D}t| j|ƒ.}t j|d
}|  |jt j¡ W d Q R X W d Q R X qüW xddD ]\}d|krdt | j¡ t| j|ƒ.}t j|d
}|  |jt j¡ W d Q R X W d Q R X qJW d S )Nr+   zr+br   )r\   r'   r   r5   rŒ   Úz)rN   zr+b)r\   )r+   rf   rp   )r   r!   r   r3   r™   r#   r'   ZREADZWRITErO   rd   r   r   )r   r(   r¹   r'   r
   r
   r   Útest_fileobj_modeº  s,    "
(

zTestGzip.test_fileobj_modec          	   C   s¶   | j }y| d¡}W n tk
r2   |  d¡ Y nX t |d¡}| td ¡ W d Q R X t |d¡}|  | 	¡ td ¡ W d Q R X t |d¡}|  | 	¡ td ¡ W d Q R X d S )Nr¯   z%Temporary file name needs to be ASCIIr+   r,   rN   )
r   rœ   ÚUnicodeEncodeErrorZskipTestr   r!   r"   r.   r#   r%   )r   Zstr_filenameZbytes_filenamer(   r
   r
   r   Útest_bytes_filenameÒ  s    zTestGzip.test_bytes_filenamec             C   st   t jdtdƒ dd}|  t|ƒtj¡ t |¡}t j|d}|  	| 
d¡d¡ dtj }|  |jj ¡ |d¡ dS )	z-Decompressed data buffering should be limitedr˜   g    €„>Arj   )r[   )r\   rY   z)Excessive amount of data was decompressedN)r   r]   ÚintZ
assertLessr$   r   ÚDEFAULT_BUFFER_SIZEr^   r!   r#   r%   ZassertLessEqualÚ_bufferÚrawr   )r   ZbombZdecompZ
max_decompr
   r
   r   Útest_decompress_limitedà  s    

z TestGzip.test_decompress_limitedc             C   st   xnt tgD ]b}x\dD ]T}tj|f|žŽ }|  t|ƒt¡ tjt 	|¡dd}|  | 
¡ |¡ W d Q R X qW q
W d S )N)r
   )rY   )é   )rj   rN   )r\   r'   )r.   rh   r   r]   r#   Útyper    r!   r   r^   r%   )r   r&   r   Údatacr(   r
   r
   r   Útest_compressî  s    
zTestGzip.test_compressc          
   C   st   xnt tfD ]b}t ¡ }tj|dd}| |¡ W d Q R X |  t | 	¡ ¡|¡ t 
|¡}|  t |¡|¡ q
W d S )Nr+   )r\   r'   )r.   rh   r   r^   r   r!   r"   r#   Ú
decompressr£   r]   )r   r&   r¥   r(   rÆ   r
   r
   r   Útest_decompressö  s    
zTestGzip.test_decompressc          
   C   sØ   t d }t |¡d d… }tjt |¡d}|  t|j¡ W d Q R X tjt |¡d,}|  	| t
|ƒ¡|¡ |  t|jd¡ W d Q R X xHtddƒD ]:}tjt |d |… ¡d}|  t|jd¡ W d Q R X q–W d S )Nr,   iøÿÿÿ)r\   rY   r”   r   )r.   r   r]   r!   r   r^   rO   ÚEOFErrorr%   r#   r$   rC   )r   r&   Z	truncatedr(   rm   r
   r
   r   Útest_read_truncated   s    zTestGzip.test_read_truncatedc          	   C   s6   d}t jt |¡d}|  | ¡ d¡ W d Q R X d S )Ns   ‹²cQÿ ExtraI-. 2ÑMx   )r\   s   Test)r   r!   r   r^   r#   r%   )r   Zgzdatar(   r
   r
   r   Útest_read_with_extra  s    zTestGzip.test_read_with_extrac          	   C   sP   t  | jd¡}| t¡ W d Q R X t  | jd¡}|jjj ¡  W d Q R X d S )Nr+   rN   )	r   r™   r   r"   r.   rÁ   rÂ   Z_fpZprepend)r   r(   r
   r
   r   Útest_prepend_error  s    zTestGzip.test_prepend_errorN)r   ),r   r   r   r*   r4   r<   rF   rH   rL   rQ   rR   rX   r   r   r`   re   ri   ro   rr   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   8   sN   


-

r   c               @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚTestOpenc          	   C   sl  t d }t | jd¡}| |¡ W d Q R X t| jdƒ }t | ¡ ¡}|  ||¡ W d Q R X t | jd¡}|  | ¡ |¡ W d Q R X t | jd¡}| |¡ W d Q R X t| jdƒ$}t | ¡ ¡}|  ||d ¡ W d Q R X |  t	¡ t | jd¡ W d Q R X t
 | j¡ t | jd¡}| |¡ W d Q R X t| jdƒ }t | ¡ ¡}|  ||¡ W d Q R X d S )Nr,   r+   rN   rf   r”   rp   )r.   r   r™   r   r"   rÈ   r%   r#   rO   rq   r   r   )r   r¤   r(   Ú	file_datar
   r
   r   Útest_binary_modes  s*    zTestOpen.test_binary_modesc          	   C   s‚   t  | j¡}t |d¡}| td ¡ W d Q R X t |d¡}| t¡ W d Q R X t |¡}|  | ¡ td ¡ W d Q R X d S )Nr+   r,   rf   r6   )	r7   r8   r   r   r™   r"   r.   r#   r%   )r   r   r(   r
   r
   r   Útest_pathlike_file9  s    zTestOpen.test_pathlike_filec          	   C   sl  t d }t | jd¡}| |¡ W d Q R X t| jdƒ }t | ¡ ¡}|  ||¡ W d Q R X t | jd¡}|  | ¡ |¡ W d Q R X t | jd¡}| |¡ W d Q R X t| jdƒ$}t | ¡ ¡}|  ||d ¡ W d Q R X |  t	¡ t | jd¡ W d Q R X t
 | j¡ t | jd¡}| |¡ W d Q R X t| jdƒ }t | ¡ ¡}|  ||¡ W d Q R X d S )Nr,   r   rN   r   r5   r”   rŒ   )r.   r   r™   r   r"   rÈ   r%   r#   rO   rq   r   r   )r   r¤   r(   rÏ   r
   r
   r   Útest_implicit_binary_modesB  s*    z#TestOpen.test_implicit_binary_modesc          	   C   s  t  d¡d }| dtj¡}t | jd¡}| |¡ W d Q R X t| jdƒ&}t 	| 
¡ ¡ d¡}|  ||¡ W d Q R X t | jd¡}|  | 
¡ |¡ W d Q R X t | jd¡}| |¡ W d Q R X t| jdƒ*}t 	| 
¡ ¡ d¡}|  ||d ¡ W d Q R X d S )	Nr¯   r,   Ú
ÚwtrN   ÚrtZatr”   )r.   r±   Úreplacer2   Úlinesepr   r™   r   r"   rÈ   r%   r#   )r   r¤   Úuncompressed_rawr(   rÏ   r
   r
   r   Útest_text_modes^  s    zTestOpen.test_text_modesc          	   C   sª   t d }| d¡}t |¡}t t |¡d¡}|  | ¡ |¡ W d Q R X t t |¡d¡}|  | ¡ |¡ W d Q R X t t |¡d¡}|  | ¡ |¡ W d Q R X d S )Nr,   r¯   r   rN   rÕ   )	r.   r±   r   r]   r™   r   r^   r#   r%   )r   Zuncompressed_bytesZuncompressed_strr_   r(   r
   r
   r   Útest_fileobjn  s    

zTestOpen.test_fileobjc          	   C   sä   |   t¡ t d¡ W d Q R X |   t¡ t | jd¡ W d Q R X |   t¡ t | jd¡ W d Q R X |   t¡ tj| jddd W d Q R X |   t¡ tj| jddd W d Q R X |   t¡ tj| jdd	d
 W d Q R X d S )Ngw¾Ÿ/Ý^@ZwbtZxbtrN   zutf-8)r°   Úignore)ÚerrorsrÓ   )Únewline)rO   rP   r   r™   rd   r   )r   r
   r
   r   Útest_bad_paramsy  s    zTestOpen.test_bad_paramsc          	   C   s®   t  d¡d }| dtj¡}tj| jddd}| |¡ W d Q R X t| jdƒ&}t 	| 
¡ ¡ d¡}|  ||¡ W d Q R X tj| jddd}|  | 
¡ |¡ W d Q R X d S )	Nr¯   r,   rÓ   rÔ   zutf-16)r°   rN   rÕ   )r.   r±   rÖ   r2   r×   r   r™   r   r"   rÈ   r%   r#   )r   r¤   rØ   r(   rÏ   r
   r
   r   Útest_encodingˆ  s    zTestOpen.test_encodingc          	   C   sX   t  | jd¡}| d¡ W d Q R X t j| jdddd}|  | ¡ d¡ W d Q R X d S )Nr+   s   fooÿbarrÕ   r¯   rÛ   )r°   rÜ   Zfoobar)r   r™   r   r"   r#   r%   )r   r(   r
   r
   r   Útest_encoding_error_handler”  s
    z$TestOpen.test_encoding_error_handlerc          	   C   sj   t  d¡d }tj| jddd}| |¡ W d Q R X tj| jddd}|  | ¡ |g¡ W d Q R X d S )Nr¯   r,   rÔ   rÓ   )rÝ   rÕ   ú)r.   r±   r   r™   r   r"   r#   r   )r   r¤   r(   r
   r
   r   Útest_newlineœ  s
    zTestOpen.test_newlineN)r   r   r   rÐ   rÑ   rÒ   rÙ   rÚ   rÞ   rß   rà   râ   r
   r
   r
   r   rÎ     s   	rÎ   c                s   ‡ fdd„}|S )Nc                s   t  ˆ ¡‡‡ fdd„ƒ}|S )Nc           	      s&   t  ˆ ¡ z
ˆ| |ŽS t ˆ ¡ X d S )N)r2   Úmakedirsr   Zrmtree)r   Úkwargs)Ú	directoryÚfunctionr
   r   Úwrapper§  s    

z?create_and_remove_directory.<locals>.decorator.<locals>.wrapper)Ú	functoolsÚwraps)ræ   rç   )rå   )ræ   r   Ú	decorator¦  s    z.create_and_remove_directory.<locals>.decoratorr
   )rå   rê   r
   )rå   r   Úcreate_and_remove_directory¥  s    	rë   c               @   sP   e Zd ZdZdd„ Zeeƒdd„ ƒZdd„ Zeeƒdd	„ ƒZ	eeƒd
d„ ƒZ
dS )ÚTestCommandLines   This is a simple test with gzipc          
   C   s”   t  ¡ h}tj|dd}| | j¡ W d Q R X tjdddf}t|t	t	t	d}| 
| ¡ ¡\}}W d Q R X W d Q R X |  |d¡ |  || j¡ d S )Nr+   )r\   r'   z-mr   z-d)ÚstdinÚstdoutÚstderrrS   )r   r^   r   r!   r"   r&   ÚsysÚ
executabler   r   Úcommunicater£   r#   )r   Zbytes_ioZ	gzip_filer   ÚprocÚoutÚerrr
   r
   r   Útest_decompress_stdin_stdoutµ  s    
&z,TestCommandLine.test_decompress_stdin_stdoutc          	   C   sÆ   t j td¡}|  t j |¡¡ tj|dd}| | j	¡ W d Q R X t
ddd|ƒ\}}}tt j td¡dƒ}|  | ¡ | j	¡ W d Q R X |  t j |¡¡ |  |d	¡ |  |d
¡ |  |d
¡ d S )Nztestgzip.gzr+   )r'   z-mr   z-dÚtestgziprN   r   rS   )r2   ÚpathrV   ÚTEMPDIRra   Úexistsr   r™   r"   r&   r   r#   r%   rc   )r   ÚgzipnameÚfpÚrcrô   rõ   Zgunzipedr
   r
   r   Útest_decompress_infile_outfileÁ  s    z.TestCommandLine.test_decompress_infile_outfilec             C   s<   t ddddƒ\}}}|  d|¡ |  |d¡ |  |d¡ d S )Nz-mr   z-dzthisisatest.outs   filename doesn't end in .gz:r   rS   )r   ZassertInr#   )r   rý   rô   rõ   r
   r
   r   Ú$test_decompress_infile_outfile_errorÒ  s    z4TestCommandLine.test_decompress_infile_outfile_errorc          	   C   s\   t jddf}t|tttd}| | j¡\}}W d Q R X |  |d¡ |  |d d… d¡ d S )Nz-mr   )rí   rî   rï   rS   r”   s   ‹)rð   rñ   r   r   rò   r&   r#   )r   r   ró   rô   rõ   r
   r
   r   Útest_compress_stdin_outfileØ  s
    z+TestCommandLine.test_compress_stdin_outfilec          	   C   s–   t j td¡}|d }|  t j |¡¡ t|dƒ}| | j¡ W d Q R X t	dd|ƒ\}}}|  
t j |¡¡ |  |d¡ |  |d¡ |  |d¡ d S )Nr÷   z.gzr+   z-mr   r   rS   )r2   rø   rV   rù   ra   rú   r™   r"   r&   r   rc   r#   )r   Zlocal_testgziprû   rü   rý   rô   rõ   r
   r
   r   Ú$test_compress_infile_outfile_defaultá  s    z4TestCommandLine.test_compress_infile_outfile_defaultN)r   r   r   r&   rö   rë   rù   rþ   rÿ   r   r  r
   r
   r
   r   rì   ²  s   	rì   c             C   s   t  ttt¡ d S )N)r   Zrun_unittestr   rÎ   rì   )Úverboser
   r
   r   Ú	test_mainò  s    r  Ú__main__T)r  )N)%Ú__doc__rK   rè   r   r2   r7   rš   rð   ZunittestÚ
subprocessr   r   Ztestr   Ztest.supportr   r   Ztest.support.script_helperr   Úimport_moduler   r.   rh   rø   Úabspathr   rù   r^   r	   ZTestCaser   r   rÎ   rë   rì   r  r   r
   r
   r
   r   Ú<module>   s:   

   h 	@

