o
    gz                     @   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 ddlmZ ddlmZmZ ddlmZ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re&dd dS dS )z!Test script for the gzip module.
    N)PIPEPopen)support)import_helper)	os_helper)_4G
bigmemtest)assert_python_okassert_python_failure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   D/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_gzip.pyseekable&   s   zUnseekableIO.seekablec                 C      t jNioUnsupportedOperationr   r   r   r   tell)      zUnseekableIO.tellc                 G   r   r   r   )r   argsr   r   r   seek,   r   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      t | j d S r   r   unlinkfilenamer   r   r   r   setUp3      zBaseTest.setUpc                 C   r   r   r    r   r   r   r   tearDown6   r$   zBaseTest.tearDownN)r   r   r   r   TESTFNr"   r#   r%   r   r   r   r   r   0   s    r   c                   @   s  e 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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+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^S )`TestGzipbc                 C   s   t |}t| jd| }||}W d    n1 sw   Y  | |t| t| jd| }| | | W d    d S 1 sGw   Y  d S )Nwr)bytesr   GzipFiler"   writeassertEquallenread)r   datamodeZb_dataflr   r   r   write_and_read_back;   s   "zTestGzip.write_and_read_backc                 C   sx   t | jd'}|td  |  |  ttdr#t	|  |
  W d    n1 s1w   Y  |
  d S )Nwb2   fsync)r   r,   r"   r-   data1flushfilenohasattrosr8   closer   r3   r   r   r   
test_writeC   s   

zTestGzip.test_writec                 C   s   t | j}t|d}|td  W d    n1 sw   Y  | |jt	 t|d}|t W d    n1 s@w   Y  t|}|
 }W d    n1 sYw   Y  | |td  | |jt	 d S )Nr)   r7   a3   )pathlibPathr"   r   r,   r-   r9   ZassertIsInstancenamestrr0   r.   )r   r"   r3   dr   r   r   "test_write_read_with_pathlike_fileQ   s   
z+TestGzip.test_write_read_with_pathlike_filec                 C   sB   |  ttd  tttd}|jdg dd}|  | d S )Nr7      B)   rK      )shape)r5   
memoryviewr9   r+   rangecast)r   mr1   r   r   r   test_write_memoryview`   s   zTestGzip.test_write_memoryviewc                 C   s   |  ttd  d S )Nr7   )r5   	bytearrayr9   r   r   r   r   test_write_bytearrayf      zTestGzip.test_write_bytearrayc                 C   s   |  tdtd  d S )NI(   )r5   arrayr9   r   r   r   r   test_write_arrayi   s   zTestGzip.test_write_arrayc              	   C   s   t | jdA}| t |d W d    n1 sw   Y  | t |g  W d    n1 s7w   Y  |t W d    n1 sKw   Y  t | jd}| | t W d    d S 1 skw   Y  d S )Nr6    rb)	r   r,   r"   assertRaises	TypeErrorr-   r9   r.   r0   r?   r   r   r   test_write_incompatible_typel   s   "z%TestGzip.test_write_incompatible_typec                 C   sR   |    t| jd}| }W d    n1 sw   Y  | |td  d S )Nr*   r7   )r@   r   r,   r"   r0   r.   r9   r   r3   rG   r   r   r   	test_ready   s
   
zTestGzip.test_readc                 C   s   |    g }d}t| jd$}	 | }|sq,|| |t|7 }| | | qW d    n1 s6w   Y  | d	|t
d  d S )Nr   r*   T    r7   )r@   r   r,   r"   read1appendr/   r.   r   joinr9   )r   Zblocksnreadr3   rG   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 )Nrg   compresslevelr[   fileobjr2   )r   compressr9   r,   r   BytesIOr.   r0   )r   rh   
compressedr3   r   r   r   test_read_large   s   zTestGzip.test_read_largec                 C   sv  |    t| jd}|j}| |j |  | |j | 	t
 |d W d    n1 s3w   Y  | 	t
 |d W d    n1 sMw   Y  | 	t
 |  W d    n1 sfw   Y  t| jd}|j}| |j |  | |j | 	t
 |d W d    n1 sw   Y  | 	t
 |  W d    d S 1 sw   Y  d S )Nr*   rg   r   r)   ra   )r@   r   r,   r"   rl   assertFalseclosedr>   
assertTruer\   
ValueErrorr0   r   r   r-   r:   )r   r3   rl   r   r   r   test_io_on_closed_object   s4   

"z!TestGzip.test_io_on_closed_objectc                 C   s   |    t| jd}|td  W d    n1 sw   Y  t| jd}| }W d    n1 s8w   Y  | |td td   d S )Nab   r[   r7   )	r@   r   r,   r"   r-   data2r0   r.   r9   r_   r   r   r   test_append   s   
zTestGzip.test_appendc              	   C   s   t | jdd}|d W d    n1 sw   Y  tddD ]}t | jdd}|d W d    q"1 s<w   Y  q"t | jd}d}	 |d
}||7 }|sYqZqMW d    n1 sdw   Y  | |d d S )Nr6   	      ar      rv   r[   ra   rg   i    s   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)r   r,   r"   r-   rO   r0   r.   )r   r3   iZzgfilecontentsZztxtr   r   r   test_many_append   s"   
zTestGzip.test_many_appendc                 C   s   t | jd}|td  W d    n1 sw   Y  t | jd}| | td  W d    n1 s:w   Y  | t t | jd W d    d S 1 sWw   Y  d S )Nxbr7   r[   )	r   r,   r"   r-   r9   r.   r0   r\   FileExistsErrorr?   r   r   r   test_exclusive_write   s   "zTestGzip.test_exclusive_writec              	   C   s   |    t| jd$}t|}dd |D }W d    n1 s#w   Y  W d    n1 s2w   Y  | |dtjdd  d S )Nr[   c                 S   s   g | ]}|qS r   r   ).0liner   r   r   
<listcomp>   s    z1TestGzip.test_buffered_reader.<locals>.<listcomp>r7   Tkeepends)	r@   r   r,   r"   r   BufferedReaderr.   r9   
splitlines)r   r3   r*   linesr   r   r   test_buffered_reader   s   zTestGzip.test_buffered_readerc                 C   sz   |    t| jd(}d}	 ||}|s|dkrq+| t||k |d d }qW d    d S 1 s6w   Y  d S )Nr[   r   rg   r7   )r@   r   r,   r"   readliners   r/   )r   r3   Zline_lengthLr   r   r   test_readline   s   
"zTestGzip.test_readlinec                 C   s   |    t| jd}| }W d    n1 sw   Y  t| jd}	 |d}|g kr2q3q(W d    d S 1 s>w   Y  d S )Nr[   rg      )r@   r   r,   r"   	readlines)r   r3   r   r   r   r   test_readlines   s   


"zTestGzip.test_readlinesc                 C   s   |    t| j@}	 | }| }|sqB| }|| t|dkr)d}nt|}||}| 	|d | | || qW d    d S 1 sMw   Y  d S )Nrg   
   )
r@   r   r,   r"   r   r   r   r/   r0   r.   )r   r3   ZoldposZline1ZnewposZamountZline2r   r   r   test_seek_read   s"   


"zTestGzip.test_seek_readc                 C   sn   |    t| j}|d |jddd |d}W d    n1 s&w   Y  | |tdd  d S )Nr   rg   )whence      )r@   r   r,   r"   r0   r   r.   r9   )r   r3   yr   r   r   test_seek_whence  s   
zTestGzip.test_seek_whencec                 C   sZ   t | jd}tdddD ]}|| |d qW d    d S 1 s&w   Y  d S )Nr)   r   rI      s   GZ
)r   r,   r"   rO   r   r-   )r   r3   posr   r   r   test_seek_write  s   
"zTestGzip.test_seek_writec                 C   s   |    t| jd}| |jjd W d    n1 sw   Y  t| j t| jd}| |jjd W d    d S 1 sDw   Y  d S )Nr*   r[   xr   )	r@   r   r,   r"   r.   Z	myfileobjr2   r   r!   r?   r   r   r   	test_mode  s   "zTestGzip.test_modec              	   C   s\   dD ])}t | j|}| t|d | |j| j W d    q1 s&w   Y  qd S )N)r6   r[   rE   )r   r,   r"   rs   r<   r.   rE   )r   r2   r3   r   r   r   test_1647484'  s   zTestGzip.test_1647484c                 C   s`   |    t| jd}| t|jd | |jj| j W d    d S 1 s)w   Y  d S )Nr[   rE   )	r@   r   r,   r"   rs   r<   rl   r.   rE   r?   r   r   r   test_paddedfile_getattr-  s
   "z TestGzip.test_paddedfile_getattrc                 C   s   d}t j| jd|d}|t W d    n1 sw   Y  t | j(}| t|d | |j |	 }| 
|t | 
|j| W d    d S 1 sQw   Y  d S )N[r)   mtimer   )r   r,   r"   r-   r9   rs   r<   ZassertIsNoner   r0   r.   )r   r   fWritefReadZdataReadr   r   r   
test_mtime3  s   "zTestGzip.test_mtimec              	   C   s  d}t j| jd|d}|t W d    n1 sw   Y  t| jd}|d}| |d |d}| |d z| jd	d
 }d}W n t	yV   d}d
}Y nw |d}| || |d}	| |	t
d| |d}
| |
d |d}| |d |t|}| || |t| jjd  |d}| |d |d}| |t
dtt W d    d S 1 sw   Y  d S )Nr   r)   r   r[         rg      zLatin-1    ra   rL   z<i      rK   s   d)r   r,   r"   r-   r9   openr0   r.   encodeUnicodeEncodeErrorstructpackr/   r   r=   statst_size)r   r   r   r   ZidBytesZcmByteZexpectednameZexpectedflagsZ	flagsByteZ
mtimeBytesxflByteZosByteZ	nameBytesZ
crc32BytesZ
isizeBytesr   r   r   test_metadata>  s@   







"zTestGzip.test_metadatac                 C   s   t j| _|   d S r   )r   ZTESTFN_ASCIIr"   r   r   r   r   r   test_metadata_ascii_nameq  s   z!TestGzip.test_metadata_ascii_namec           	   
   C   s   g d}d}|D ]`\}}}|  |N tj| jd|d}| |t W d    n1 s.w   Y  t| jd}|| |d}| 	|| W d    n1 sTw   Y  W d    q1 scw   Y  qd S )N))fastrg      )Zbestrz   r   )Ztradeoff   r   rK   r)   ri   r[   rg   )
subTestr   r,   r"   r-   r9   r   r   r0   r.   )	r   casesZ	xflOffsetrE   levelZexpectedXflByter   r   r   r   r   r   test_compresslevel_metadatau  s    

z$TestGzip.test_compresslevel_metadatac                 C   s   t | jd}|d W d    n1 sw   Y  t | jd}|  z| W d    n1 s5w   Y  W n	 tyD   Y nw | d zt | jd}dd  W d    n1 saw   Y  W n
 tyq   Y d S w | d d S )Nr6   s   xxxr[   z4__enter__ on a closed file didn't raise an exceptionrg   r   z1/0 didn't raise an exception)r   r,   r"   r-   r>   rt   ZfailZeroDivisionErrorr?   r   r   r   test_with_open  s.   

zTestGzip.test_with_openc                 C   s   t | jd}|td  W d    n1 sw   Y  t| jd}|d W d    n1 s4w   Y  t | jd}| }| |td d W d    d S 1 sYw   Y  d S )Nr6   r7   rv   s2                                                     r[   zIncorrect data in file)r   r,   r"   r-   r9   r   r0   r.   r_   r   r   r   test_zero_padded_file  s   "zTestGzip.test_zero_padded_filec                 C   s   |  ttjt d S r   )rs   
issubclassr   BadGzipFileOSErrorr   r   r   r   test_gzip_BadGzipFile_exception  rU   z(TestGzip.test_gzip_BadGzipFile_exceptionc                 C   s~   t | jd}|td  W d    n1 sw   Y  t| jd}| tj|j W d    d S 1 s8w   Y  d S )Nr6   r7   r*   )	r   r"   r-   r9   r   r,   r\   r   r   )r   filer   r   r   test_bad_gzip_file  s   "zTestGzip.test_bad_gzip_filec                 C   s   t d }t }tj|dd}|| W d    n1 sw   Y  | }t|}tj|dd}| | | W d    d S 1 sFw   Y  d S )Nr7   r6   rk   r[   )r9   r   r   r,   r-   getvaluer.   r0   )r   uncompressedbufr3   ro   r   r   r   test_non_seekable_file  s   "zTestGzip.test_non_seekable_filec                 C   s   t d }t| jd}|| W d    n1 sw   Y  dd }t| jdA}d|_d}| D ]}||}|dkrA qS| |t	|| |t	|7 }q4| |d	d | |t	| W d    d S 1 sow   Y  d S )
Nr|   r6   c                  s   s    	 t dddD ]} | V  qq)NT   r7   r   )rO   )nr   r   r   sizes  s
   z!TestGzip.test_peek.<locals>.sizesr[   !   r   ra   d   )
r9   r   r,   r"   r-   Zmax_read_chunkpeekr.   r0   r/   )r   r   r3   r   re   r   sr   r   r   	test_peek  s"   

"zTestGzip.test_peekc              	   C   s   t d djdd}|   t| jd0}tj|dd}| 	|
 | W d    n1 s1w   Y  W d    d S W d    d S 1 sIw   Y  d S )Nr7   asciiTr   r*   encoding)r9   decoder   r@   r   r,   r"   r   TextIOWrapperr.   r   )r   r   r3   tr   r   r   test_textio_readlines  s   "zTestGzip.test_textio_readlinesc              	   C   s   t | jt jt jB }t |d(}tj|dd}W d    n1 s$w   Y  W d    d S W d    d S 1 s<w   Y  d S )Nr6   r)   rk   )r=   r   r"   O_WRONLYO_CREATfdopenr   r,   )r   fdr3   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    n1 s*w   Y  t j|dd}| |jt j W d    n1 sIw   Y  t j|dd}| |jt j W d    n1 shw   Y  t j|dd}| |jt j W d    n1 sw   Y  | 	t
 t j|dd W d    n1 sw   Y  W d    n1 sw   Y  d	D ]6}t| j|&}t j|d
}| |jt j W d    n1 sw   Y  W d    q1 sw   Y  qdD ]Z}d|v rt| j t| j|?}| t t j|d
}W d    n	1 sw   Y  | | |jt j W d    n	1 s7w   Y  W d    q1 sGw   Y  qd S )Nr6   r+br*   rk   r)   rA   r   z)r[   r   rl   )r6   rv   r   )r   r,   r"   r>   r   r.   r2   ZREADZWRITEr\   rt   r   r!   ZassertWarnsFutureWarning)r   r3   r   r2   r   r   r   test_fileobj_mode  sN    zTestGzip.test_fileobj_modec                 C   s   | j }z|d}W n ty   | d Y nw t|d}|td  W d    n1 s1w   Y  t|d}| |	 td  W d    n1 sQw   Y  t|d}| |	 td  W d    d S 1 srw   Y  d S )Nr   z%Temporary file name needs to be ASCIIr6   r7   r[   )
r"   r   r   ZskipTestr   r,   r-   r9   r.   r0   )r   Zstr_filenameZbytes_filenamer3   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    >Arz   ri   r   rg   z)Excessive amount of data was decompressedN)r   rm   intZ
assertLessr/   r   DEFAULT_BUFFER_SIZErn   r,   r.   r0   ZassertLessEqual_bufferrawr   )r   ZbombdecompZ
max_decompr   r   r   test_decompress_limited  s   

z TestGzip.test_decompress_limitedc              
   C   s   t tfD ]:}dD ]5}tj|g|R  }| t|t tjt	|dd}| |
 | W d    q1 s8w   Y  qqd S )Nr   )rg   )r   )rz   r[   rk   )r9   rx   r   rm   r.   typer+   r,   r   rn   r0   )r   r1   r   datacr3   r   r   r   test_compress  s   zTestGzip.test_compressc                 C   s   d}t tfD ]X}dD ]S}| j||dB tj|g|R d|i}| t|t tjt	
|dd}|d | |j| W d    n1 sIw   Y  W d    q
1 sXw   Y  q
qd S )Nr   r   )r1   r   r   r[   rk   rg   )r9   rx   r   r   rm   r.   r   r+   r,   r   rn   r0   r   )r   r   r1   r   r   r3   r   r   r   test_compress_mtime!  s   
zTestGzip.test_compress_mtimec              	   C   s   t tfD ];}t }tj|dd}|| W d    n1 s!w   Y  | t|	 | t
|}| t|| qd S )Nr6   rk   )r9   rx   r   rn   r   r,   r-   r.   
decompressr   rm   )r   r1   r   r3   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    n1 s(w   Y  tjt|d}| 	|t
|| | t|jd W d    n1 sTw   Y  tddD ]'}tjt|d | d}| t|jd W d    q^1 sw   Y  q^d S )Nr7   ir   rg   r   r   )r9   r   rm   r,   r   rn   r\   EOFErrorr0   r.   r/   rO   )r   r1   Z	truncatedr3   r}   r   r   r   test_read_truncated6  s   zTestGzip.test_read_truncatedc                 C   sL   d}t jt|d}| | d W d    d S 1 sw   Y  d S )Ns   cQ ExtraI-. 2Mx   r   s   Test)r   r,   r   rn   r.   r0   )r   Zgzdatar3   r   r   r   test_read_with_extraD  s   "zTestGzip.test_read_with_extrac                 C   sz   t | jd}|t W d    n1 sw   Y  t | jd}|jjj  W d    d S 1 s6w   Y  d S )Nr6   r[   )	r   r   r"   r-   r9   r   r   _fpZprependr?   r   r   r   test_prepend_errorL  s   "zTestGzip.test_prepend_errorc                 C   sx   t  dg d}t||j }tjt dd}| ||| | |	 | W d    d S 1 s5w   Y  d S )NQ)rg   r      rL   r   r)   rk   )
rX   r/   itemsizer   r,   r   rn   r.   r-   r   )r   qZLENGTHr3   r   r   r   test_issue44439S  s   "zTestGzip.test_issue44439N)r(   )2r   r   r   r5   r@   rH   rR   rT   rY   r^   r`   rf   r   r   rp   ru   ry   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'   :   s^    




3

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   s  t d }t| jd}|| W d    n1 sw   Y  t| jd}t| }| || W d    n1 s>w   Y  t| jd}| | | W d    n1 s]w   Y  t| jd}|| W d    n1 syw   Y  t| jd}t| }| ||d  W d    n1 sw   Y  | t	 t| jd W d    n1 sw   Y  t
| j t| jd}|| W d    n1 sw   Y  t| jd}t| }| || W d    d S 1 sw   Y  d S )Nr7   r6   r[   rv   r   r   r9   r   r   r"   r-   r   r0   r.   r\   r   r   r!   r   r   r3   	file_datar   r   r   test_binary_modes]  s:   $zTestOpen.test_binary_modesc                 C   s   t | j}t|d}|td  W d    n1 sw   Y  t|d}|t W d    n1 s9w   Y  t|}| | td  W d    d S 1 sYw   Y  d S )Nr6   r7   rv   rB   )	rC   rD   r"   r   r   r-   r9   r.   r0   )r   r"   r3   r   r   r   test_pathlike_filex  s   "zTestOpen.test_pathlike_filec                 C   s  t d }t| jd}|| W d    n1 sw   Y  t| jd}t| }| || W d    n1 s>w   Y  t| jd}| | | W d    n1 s]w   Y  t| jd}|| W d    n1 syw   Y  t| jd}t| }| ||d  W d    n1 sw   Y  | t	 t| jd W d    n1 sw   Y  t
| j t| jd}|| W d    n1 sw   Y  t| jd}t| }| || W d    d S 1 sw   Y  d S )Nr7   r)   r[   r*   rA   r   r   r   r   r   r   r   test_implicit_binary_modes  s:   $z#TestOpen.test_implicit_binary_modesc                 C   sx  t dd }|dtj}tj| jddd}|| W d    n1 s'w   Y  t| jd}t	|
 d}| || W d    n1 sMw   Y  tj| jddd}| |
 | W d    n1 snw   Y  tj| jddd}|| W d    n1 sw   Y  t| jd}t	|
 d}| ||d	  W d    d S 1 sw   Y  d S )
Nr   r7   
wtr   r[   rtZatr   r9   r   replacer=   linesepr   r   r"   r-   r   r0   r.   r   r   Zuncompressed_rawr3   r   r   r   r   test_text_modes  s&   "zTestOpen.test_text_modesc                 C   s   t d }|d}t|}tt|d}| | | W d    n1 s*w   Y  tt|d}| | | W d    n1 sKw   Y  tjt|ddd}| | | W d    d S 1 sow   Y  d S )Nr7   r   r*   r[   r  r   )	r9   r   r   rm   r   r   rn   r.   r0   )r   Zuncompressed_bytesZuncompressed_strro   r3   r   r   r   test_fileobj  s   

"zTestOpen.test_fileobjc                 C   s^  |  t td W d    n1 sw   Y  |  t t| jd W d    n1 s1w   Y  |  t t| jd W d    n1 sMw   Y  |  t tj| jddd W d    n1 skw   Y  |  t tj| jddd W d    n1 sw   Y  |  t tj| jdd	d
 W d    d S 1 sw   Y  d S )Ngw/^@ZwbtZxbtr[   zutf-8r   ignore)errorsr  )newline)r\   r]   r   r   rt   r"   r   r   r   r   test_bad_params  s$   "zTestOpen.test_bad_paramsc                 C   s   t dd }|dtj}tj| jddd}|| W d    n1 s'w   Y  t| jd}t	|
 d}| || W d    n1 sMw   Y  tj| jddd}| |
 | W d    d S 1 sow   Y  d S )	Nr   r7   r  r  zutf-16r   r[   r  r  r	  r   r   r   test_encoding  s   "zTestOpen.test_encodingc                 C   s   t | jd}|d W d    n1 sw   Y  t j| jdddd}| | d W d    d S 1 s:w   Y  d S )Nr6   s   foobarr  r   r  )r   r  Zfoobar)r   r   r"   r-   r.   r0   r?   r   r   r   test_encoding_error_handler  s   "z$TestOpen.test_encoding_error_handlerc                 C   s   t dd }tj| jdddd}|| W d    n1 s!w   Y  tj| jdddd}| | |g W d    d S 1 sEw   Y  d S )Nr   r7   r  r  )r   r  r  )r9   r   r   r   r"   r-   r.   r   )r   r   r3   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               	      s2   t   z| i |W t  S t  w r   )r=   makedirsr   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
eedd Zdd Zdd ZdS )TestCommandLines   This is a simple test with gzipc              	   C   s   t  M}tj|dd}|| j W d    n1 sw   Y  tjdddf}t|t	t	t	d}|
| \}}W d    n1 sEw   Y  W d    n1 sTw   Y  | |d | || j d S )Nr6   rk   -mr   -dstdinstdoutstderrra   )r   rn   r   r,   r-   r1   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    n1 s(w   Y  t
ddd|\}}}tt jtdd}| | | j	 W d    n1 sUw   Y  | t j| | |d	 | |d
 | |d
 d S )Nztestgzip.gzr6   )r2   r   r   r!  testgzipr[   r   ra   )r=   pathrd   TEMPDIRrq   existsr   r   r-   r1   r	   r.   r0   rs   )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 )Nr   r   r!  zthisisatest.outs.   filename doesn't end in .gz: 'thisisatest.out'rg   ra   )r
   r.   stripr   r3  r*  r+  r   r   r   $test_decompress_infile_outfile_error  s   z4TestCommandLine.test_decompress_infile_outfile_errorc                 C   sp   t jddf}t|tttd}|| j\}}W d    n1 s!w   Y  | |d | |d d d d S )Nr   r   r"  ra   r   r   )r&  r'  r   r   r(  r1   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    n1 s*w   Y  t	dd|\}}}| 
t j| | |d | |d d S )Nr-  .gzr6   r   r   ra   )r=   r.  rd   r/  rq   r0  r   r-   r1   r	   rs   r.   )r   local_testgzipr1  r2  r3  r*  r+  r   r   r   $test_compress_infile_outfile_default   s   z4TestCommandLine.test_compress_infile_outfile_defaultc              
   C   s   dD ]t}| j |dd tjtd}|d }| tj| t|d}|| j	 W d    n1 s5w   Y  t
dd||\}}}| tj| | |d | |d t| | tj| W d    q1 sqw   Y  qd S )	N)--fast--best)compress_levelr-  r9  r6   r   r   ra   )r   r=   r.  rd   r/  rq   r0  r   r-   r1   r	   rs   r.   remove)r   r>  r:  r1  r2  r3  r*  r+  r   r   r   test_compress_infile_outfile/  s    
z,TestCommandLine.test_compress_infile_outfilec                 C   0   t dddd\}}}| d| | |d d S )Nr   r   r<  r=  s8   error: argument --best: not allowed with argument --fastra   r
   ZassertInr.   r6  r   r   r   %test_compress_fast_best_are_exclusiveB     z5TestCommandLine.test_compress_fast_best_are_exclusivec                 C   rA  )Nr   r   r<  r!  sA   error: argument -d/--decompress: not allowed with argument --fastra   rB  r6  r   r   r   -test_decompress_cannot_have_flags_compressionG  rD  z=TestCommandLine.test_decompress_cannot_have_flags_compressionN)r   r   r   r1   r,  r  r/  r4  r7  r8  r;  r@  rC  rE  r   r   r   r   r    s    



r  c                 C   s   t ttt d S r   )r   Zrun_unittestr'   r   r  verboser   r   r   	test_mainM  s   rH  __main__TrF  r   )(__doc__rX   r  r   r=   rC   r   r&  Zunittest
subprocessr   r   testr   Ztest.supportr   r   r   r   Ztest.support.script_helperr	   r
   import_moduler   r9   rx   r.  abspathr&   r/  rn   r   ZTestCaser   r'   r   r  r  rH  r   r   r   r   r   <module>   sD    

    & 	
\