o
    HXhz                     @   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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#e$dkre%  dS dS )z!Test script for the gzip module.
    N)PIPEPopen)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   8/opt/python-3.10.19/usr/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   setUp2      zBaseTest.setUpc                 C   r   r   r   r   r   r   r   tearDown5   r#   zBaseTest.tearDownN)r   r   r   r   TESTFNr!   r"   r$   r   r   r   r   r   /   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osr7   closer   r2   r   r   r   
test_writeB   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(   r6   a3   )pathlibPathr!   r
   r+   r,   r8   ZassertIsInstancenamestrr/   r-   )r   r!   r2   dr   r   r   "test_write_read_with_pathlike_fileP   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 )Nr6      B)   rJ      )shape)r4   
memoryviewr8   r*   rangecast)r   mr0   r   r   r   test_write_memoryview_   s   zTestGzip.test_write_memoryviewc                 C   s   |  ttd  d S )Nr6   )r4   	bytearrayr8   r   r   r   r   test_write_bytearraye      zTestGzip.test_write_bytearrayc                 C   s   |  tdtd  d S )NI(   )r4   arrayr8   r   r   r   r   test_write_arrayh   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 )Nr5    rb)	r
   r+   r!   assertRaises	TypeErrorr,   r8   r-   r/   r>   r   r   r   test_write_incompatible_typek   s   "z%TestGzip.test_write_incompatible_typec                 C   sR   |    t| jd}| }W d    n1 sw   Y  | |td  d S )Nr)   r6   )r?   r
   r+   r!   r/   r-   r8   r   r2   rF   r   r   r   	test_readx   s
   
zTestGzip.test_readc                 C   s   |    g }d}t| jd$}	 | }|sn|| |t|7 }| | | qW d    n1 s6w   Y  | d	|t
d  d S )Nr   r)   T    r6   )r?   r
   r+   r!   read1appendr.   r-   r   joinr8   )r   Zblocksnreadr2   rF   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 )Nrf   compresslevelrZ   fileobjr1   )r
   compressr8   r+   r   BytesIOr-   r/   )r   rg   
compressedr2   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)   rf   r   r(   r`   )r?   r
   r+   r!   rk   assertFalseclosedr=   
assertTruer[   
ValueErrorr/   r   r   r,   r9   )r   r2   rk   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   rZ   r6   )	r?   r
   r+   r!   r,   data2r/   r-   r8   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    n1 s<w   Y  q"t | jd}d}	 |d
}||7 }|sYnqMW d    n1 sdw   Y  | |d d S )Nr5   	      ar      ru   rZ   r`   rf   i    s   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)r
   r+   r!   r,   rN   r/   r-   )r   r2   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xbr6   rZ   )	r
   r+   r!   r,   r8   r-   r/   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 )NrZ   c                 S   s   g | ]}|qS r   r   ).0liner   r   r   
<listcomp>   s    z1TestGzip.test_buffered_reader.<locals>.<listcomp>r6   Tkeepends)	r?   r
   r+   r!   r   BufferedReaderr-   r8   
splitlines)r   r2   r)   linesr   r   r   test_buffered_reader   s   zTestGzip.test_buffered_readerc                 C   sz   |    t| jd(}d}	 ||}|s|dkrn| t||k |d d }qW d    d S 1 s6w   Y  d S )NrZ   r   rf   r6   )r?   r
   r+   r!   readlinerr   r.   )r   r2   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r2nq(W d    d S 1 s>w   Y  d S )NrZ   rf      )r?   r
   r+   r!   	readlines)r   r2   r   r   r   r   test_readlines   s   


"zTestGzip.test_readlinesc                 C   s   |    t| j@}	 | }| }|sn+| }|| t|dkr)d}nt|}||}| 	|d | | || qW d    d S 1 sMw   Y  d S )Nrf   
   )
r?   r
   r+   r!   r   r   r   r.   r/   r-   )r   r2   ZoldposZline1Znewpos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   rf   )whence      )r?   r
   r+   r!   r/   r   r-   r8   )r   r2   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   rH      s   GZ
)r
   r+   r!   rN   r   r,   )r   r2   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)   rZ   xr   )	r?   r
   r+   r!   r-   Z	myfileobjr1   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    n1 s&w   Y  qd S )N)r5   rZ   rD   )r
   r+   r!   rr   r;   r-   rD   )r   r1   r2   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 )NrZ   rD   )	r?   r
   r+   r!   rr   r;   rk   r-   rD   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,   r8   rr   r;   ZassertIsNoner   r/   r-   )r   r   fWritefReadZdataReadr   r   r   
test_mtime2  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   rZ         rf      zLatin-1    r`   rK   z<i      rJ   s   d)r
   r+   r!   r,   r8   openr/   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_namep  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    n1 scw   Y  qd S )N))fastrf      )Zbestry   r   )Ztradeoff   r   rJ   r(   rh   rZ   rf   )
subTestr
   r+   r!   r,   r8   r   r   r/   r-   )	r   casesZ	xflOffsetrD   levelZexpectedXflByter   r   r   r   r   r   test_compresslevel_metadatat  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 )Nr5   s   xxxrZ   z4__enter__ on a closed file didn't raise an exceptionrf   r   z1/0 didn't raise an exception)r
   r+   r!   r,   r=   rs   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 )Nr5   r6   ru   s2                                                     rZ   zIncorrect data in file)r
   r+   r!   r,   r8   r   r/   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   )rr   
issubclassr
   BadGzipFileOSErrorr   r   r   r   test_gzip_BadGzipFile_exception  rT   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 )Nr5   r6   r)   )	r   r!   r,   r8   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 )Nr6   r5   rj   rZ   )r8   r   r
   r+   r,   getvaluer-   r/   )r   uncompressedbufr2   rn   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 n| |t	|| |t	|7 }q4| |d	d | |t	| W d    d S 1 sow   Y  d S )
Nr{   r5   c                  s   s    	 t dddD ]} | V  qq)NT   r6   r   )rN   )nr   r   r   sizes  s
   z!TestGzip.test_peek.<locals>.sizesrZ   !   r   r`   d   )
r8   r
   r+   r!   r,   Zmax_read_chunkpeekr-   r/   r.   )r   r   r2   r   rd   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 )Nr6   asciiTr   r)   encoding)r8   decoder   r?   r
   r+   r!   r   TextIOWrapperr-   r   )r   r   r2   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 )Nr5   r(   rj   )r<   r   r!   O_WRONLYO_CREATfdopenr
   r+   )r   fdr2   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    n1 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    n	1 sGw   Y  qd S )Nr5   r+br)   rj   r(   r@   r   z)rZ   r   rk   )r5   ru   r   )r
   r+   r!   r=   r   r-   r1   ZREADZWRITEr[   rs   r   r    ZassertWarnsFutureWarning)r   r2   r   r1   r   r   r   test_fileobj_mode  sR   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 ASCIIr5   r6   rZ   )
r!   r   r   ZskipTestr
   r+   r,   r8   r-   r/   )r   Zstr_filenameZbytes_filenamer2   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    >Ary   rh   r   rf   z)Excessive amount of data was decompressedN)r
   rl   intZ
assertLessr.   r   DEFAULT_BUFFER_SIZErm   r+   r-   r/   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    n1 s8w   Y  qqd S )Nr   )rf   )r   )ry   rZ   rj   )r8   rw   r
   rl   r-   typer*   r+   r   rm   r/   )r   r0   r   datacr2   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    n1 sXw   Y  q
qd S )Nr   r   )r0   r   r   rZ   rj   rf   )r8   rw   r   r
   rl   r-   r   r*   r+   r   rm   r/   r   )r   r   r0   r   r   r2   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 )Nr5   rj   )r8   rw   r   rm   r
   r+   r,   r-   
decompressr   rl   )r   r0   r   r2   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    n1 sw   Y  q^d S )Nr6   ir   rf   r   r   )r8   r
   rl   r+   r   rm   r[   EOFErrorr/   r-   r.   rN   )r   r0   Z	truncatedr2   r|   r   r   r   test_read_truncated5  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   rm   r-   r/   )r   Zgzdatar2   r   r   r   test_read_with_extraC  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 )Nr5   rZ   )	r
   r   r!   r,   r8   r   r   _fpZprependr>   r   r   r   test_prepend_errorK  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)rf   r      rK   r   r(   rj   )
rW   r.   itemsizer
   r+   r   rm   r-   r,   r   )r   qZLENGTHr2   r   r   r   test_issue44439R  s   "zTestGzip.test_issue44439N)r'   )2r   r   r   r4   r?   rG   rQ   rS   rX   r]   r_   re   r   r   ro   rt   rx   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&   9   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 )Nr6   r5   rZ   ru   r   r   r8   r
   r   r!   r,   r   r/   r-   r[   r   r   r    r   r   r2   	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 )Nr5   r6   ru   rA   )	rB   rC   r!   r
   r   r,   r8   r-   r/   )r   r!   r2   r   r   r   test_pathlike_filew  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 )Nr6   r(   rZ   r)   r@   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   r6   
wtr   rZ   rtZatr   r8   r   replacer<   linesepr
   r   r!   r,   r   r/   r-   r   r   Zuncompressed_rawr2   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 )Nr6   r   r)   rZ   r  r   )	r8   r   r
   rl   r   r   rm   r-   r/   )r   Zuncompressed_bytesZuncompressed_strrn   r2   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xbtrZ   zutf-8r   ignore)errorsr  )newline)r[   r\   r
   r   rs   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   r6   r  r  zutf-16r   rZ   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 )Nr5   s   foobarr  r   r  )r   r  Zfoobar)r
   r   r!   r,   r-   r/   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   r6   r  r  )r   r  r  )r8   r   r
   r   r!   r,   r-   r   )r   r   r2   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 )Nr5   rj   -mr
   -dstdinstdoutstderrr`   )r   rm   r
   r+   r,   r0   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.gzr5   )r1   r   r
   r!  testgziprZ   r   r`   )r<   pathrc   TEMPDIRrp   existsr
   r   r,   r0   r   r-   r/   rr   )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'rf   r`   )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"  r`   r   r   )r&  r'  r   r   r(  r0   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-  .gzr5   r   r
   r`   )r<   r.  rc   r/  rp   r0  r   r,   r0   r   rr   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    n1 sqw   Y  qd S )	N)--fast--best)compress_levelr-  r9  r5   r   r
   r`   )r   r<   r.  rc   r/  rp   r0  r   r,   r0   r   rr   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 --fastr`   r	   ZassertInr-   r6  r   r   r   %test_compress_fast_best_are_exclusiveA     z5TestCommandLine.test_compress_fast_best_are_exclusivec                 C   rA  )Nr   r
   r<  r!  sA   error: argument -d/--decompress: not allowed with argument --fastr`   rB  r6  r   r   r   -test_decompress_cannot_have_flags_compressionF  rD  z=TestCommandLine.test_decompress_cannot_have_flags_compressionN)r   r   r   r0   r,  r  r/  r4  r7  r8  r;  r@  rC  rE  r   r   r   r   r    s    



r  __main__)&__doc__rW   r  r   r<   rB   r   r&  Zunittest
subprocessr   r   Ztest.supportr   r   r   r   Ztest.support.script_helperr   r	   import_moduler
   r8   rw   r.  abspathr%   r/  rm   r   ZTestCaser   r&   r   r  r  r   mainr   r   r   r   <module>   s@    

    & 	\