Your IP : 216.73.216.196


Current Path : /opt/alt/python310/lib64/python3.10/test/__pycache__/
Upload File :
Current File : //opt/alt/python310/lib64/python3.10/test/__pycache__/test_os.cpython-310.pyc

o

0�i���	@s$	ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddl$m%Z%e�&��e�'d	e(�ddl)Z)ddl*Z*Wd�n1s�wYzddl+Z+Wne,y�dZ+Ynwzddl-Z-Wne,y�dZ-Ynwzddl.Z.Wne,y�dZ.Ynwzddl/Z/d
d�e/�0�D�Z1Wne,e2f�ygZ1Ynwz
ddl3m4Z4m5Z5Wne,�y)ej6Z4Z5Ynwdd
l7m8Z8ddlm9Z9ddl:m;Z;dZ<e=e	d��rJe	�>�dkZ<e=ed��r]ej?j@�r]ej?j@�Ad�ZBndZBej$�Ad��oke	�C�dkZDdd�ZEd�dd�ZFe�Gej$�Ad�d�ZHGdd�dejI�ZJGdd�dejI�ZKGd d!�d!ejI�ZLGd"d#�d#ejI�ZMdd$lmNZNGd%d&�d&eNjO�ZPGd'd(�d(ejI�ZQe�Re=e	d)�d*�Gd+d,�d,eQ��ZSGd-d.�d.eQ�ZTe�Re=e	d)�d*�Gd/d0�d0eS��ZUGd1d2�d2ejI�ZVe�Re=e	d3�d4�Gd5d6�d6ejI��ZWGd7d8�d8ejI�ZXGd9d:�d:ejI�ZYGd;d<�d<ejI�ZZe�Re=e	d=�d>�Gd?d@�d@ejI��Z[e�\dA�dBk�pCe�\dC�dBk�pCe�\dD�dBkZ]e�Ge]dE�e�Gej$dFkdG�GdHdI�dIejI���Z^ej_d�dJdK��Z`e�Re=e	dL�dM�GdNdO�dOejI��Zae�Rej$dPkdQ�GdRdS�dSejI��ZbGdTdU�dUejI�ZcGdVdW�dWejI�Zde�Gej$dPkdX�GdYdZ�dZejI��Zee�Gej$dPkdX�Gd[d\�d\ejI��Zfe�Rej$dPkdQ�Gd]d^�d^ejI��Zge�Rej$dPkdQ�Gd_d`�d`ejI��Zhe�Re=e	da�db�Gdcdd�ddejI��Zie�Rej$dPkdQ�e jjGdedf�dfejI���Zke�Rej$dPkdQ�Gdgdh�dhejI��Zle�Rej$dPkdQ�Gdidj�djejI��Zme jjGdkdl�dlejI��ZnGdmdn�dnejI�ZoGdodp�dpejI�ZpGdqdr�drejI�ZqGdsdt�dtejI�Zre�sdu�e�Re=e	dv�dw�Gdxdy�dyejI���Zte�Re=e	dz��o�e=e	d{�d|�Gd}d~�d~ejI��ZuGdd��d�e*jvejw�Zxe�Re=e	d��d��Gd�d��d�ejI��Zyd�d��Zze�Rez�d��e�{d�d�d��Gd�d��d�ejI���Z|e�Re=e	d��d��Gd�d��d�ejI��Z}e�Re=e	d��d��e�{d�d��Gd�d��d�ejI���Z~e�Re=e	d��d��e�{d�d�d��Gd�d��d�ejI���ZGd�d��d�ejI�Z�Gd�d��d�ejI�Z�Gd�d��d�ejI�Z�Gd�d��d�ejI�Z�e�Re=e	d��d��Gd�d��d�ejI��Z�Gd�d��d�ejI�Z�Gd�d��d�ejI�Z�Gd�d��d�ejI�Z�Gd�d��d�ejI�Z�Gd�d��d�ejI�Z�eEd��Gd�d��d�ejI��Z�e=e	d���r�Gd�d��d�e��Z�e�d�k�r�e���dSdS)��N)�support)�
import_helper)�	os_helper)�
socket_helper)�threading_helper)�warnings_helper)�win32_is_iot�ignorecC�g|]}|j�qS�)Zpw_uid)�.0�urr�3/opt/alt/python310/lib64/python3.10/test/test_os.py�
<listcomp><�r)�INT_MAX�PY_SSIZE_T_MAX��assert_python_ok)�
unix_shell)�FakePathF�geteuid�thread_infoZlinuxthreadsZfreebsdcCst�tt|�d|�S)Nzrequires os.%s)�unittest�
skipUnless�hasattr�os��namerrr�requires_os_func[�r�contentcCs<t|dd��}|�|�Wd�dS1swYdS)N�xbr)�open�write)�filenameZcontent�fprrr�create_file_s"�r'Zaixz%on AIX, splice() only accepts socketsc@�$eZdZdd�Zdd�Zdd�ZdS)�	MiscTestscCst��}|�|t�dS�N)r�getcwd�assertIsInstance�str��self�cwdrrr�test_getcwdj�zMiscTests.test_getcwdc
Cszd}tjdkr	d}d}d}|d|t|�}t����}t�|��z}|}	t��}|�	||�|t|�ttj
j�}|dkr@nHt|�|krP|dkrP|d|�}tj
�||�}zt�
|�t�|�Wn!tylYnty�}	z
|	jtjkrWYd}	~	n	�d}	~	ww|}q%tjr�td	t|����Wd�n1s�wYWd�dSWd�dS1s�wYdS)
Ni��vxworks����Zpython_test_dir_�aTrz!Tested current directory length: )�sys�platform�len�tempfileZTemporaryDirectoryrZ
change_cwdrr+�assertEqual�path�sep�join�mkdir�chdir�FileNotFoundError�OSError�errnoZENAMETOOLONGr�verbose�print)
r/Zmin_lenZdirlen�dirnameZtmpdirr<�expectedr0Zneed�excrrr�test_getcwd_long_pathnsL


������"�zMiscTests.test_getcwd_long_pathcCs.t��}|�|t�|�t�|�t���dSr*)r�getcwdbr,�bytesr;�fsdecoder+r.rrr�test_getcwdb�szMiscTests.test_getcwdbN)�__name__�
__module__�__qualname__r1rIrMrrrrr)is9r)c@s�eZdZdd�ZeZdd�Zdd�Zejdd��Z	d	d
�Z
eje�e
ekd�eje
dd
dd�dd����Zdd�Zdd�Ze�ejdkd�dd��Zdd�Zdd�Zdd�Zd d!�Zd"d#�Ze�eed$�d%�d&d'��Ze�eed$�d%�d(d)��Ze�eed$�d%�d*d+��Ze�eed,�d-�d.d/��Z e�eed,�d-�e!d0d1���Z"e�eed,�d-�e!d2d3���Z#e�eed,�d-�e!d4d5���Z$d6S)7�	FileTestscCs"tj�tj�rt�tj�dSdSr*)rr<�lexistsr�TESTFN�unlink�r/rrr�setUp���zFileTests.setUpcCs:t�tjtjtjB�}t�|�|�t�tjtj	��dSr*)
rr#rrS�O_CREAT�O_RDWR�close�
assertTrue�access�W_OK�r/�frrr�test_access�s
zFileTests.test_accessc	Cs�t�tjtjtjB�}t�|�}z/d}||dkr9t�|�|d7}|dkr+|�d�|t�|�}}||dksWt�|�nt�|�wt�	||d�|�
ttj|d�dS)Nr��
z%couldn't allocate two consecutive fds��a)
rr#rrSrXrY�duprZ�skipTest�
closerange�assertRaisesrBr$)r/�first�secondZretriesrrr�test_closerange�s


��zFileTests.test_closerangecCs<tj}t�|�}|�ttj|d�t�|�}|�||�dS�Nr)	rrSr7�getrefcountrh�	TypeErrorr�renamer;)r/r<�old�newrrr�test_rename�s


zFileTests.test_renamecCs�ttjd��1}|�d�|��|��}t�|dd�t�|d�}|�	t
|�t�|�	|d�Wd�dS1s:wYdS)N�w+b�spamr�)r#rrSr$�flush�filenor�lseek�readr;�typerK)r/�fobj�fd�srrr�	test_read�s
"�zFileTests.test_readzneeds INT_MAX < PY_SSIZE_T_MAXrbraF)�sizeZmemuseZdry_runcCsh|�tjtj�ttjd�ttjd��}t�|��|�}Wd�n1s'wY|�	|d�dS)Nstest�rb)
�
addCleanuprrTrSr'r#rryrwr;)r/rr&�datarrr�test_large_read�s�zFileTests.test_large_readcCs�t�tjtjtjB�}|�ttj|d�t�|d�t�|t	d��t�|t
d��t�|�ttjd��}|�|�
���gd��Wd�dS1sMwYdS)N�beanssbacon
seggs
sspam
r�)sbaconseggsrt)rr#rrSrX�O_WRONLYrhrnr$�	bytearray�
memoryviewrZr;ry�
splitlines)r/r|r{rrr�
test_write�s
�"�zFileTests.test_writecGs"tj|tjdd�}|�|d�dS)NT)�
creationflags�shellr)�
subprocessZcallZCREATE_NEW_CONSOLEr;)r/�argsZretcoderrr�write_windows_console�s
�zFileTests.write_windows_console�win32z$test specific to the Windows consolecCs*d}|�tjd|�|�tjdd|�dS)Nzprint('x' * 100000)�-cz-u)r�r7�
executable�r/�coderrr�test_write_windows_consolesz$FileTests.test_write_windows_consolecGs4t�tjtj�}tj|g|�Rddi�}|��dS)N�encoding�utf-8)rr#rrS�O_RDONLY�fdopenrZ)r/r�r|r_rrr�
fdopen_helperszFileTests.fdopen_helpercCsBt�tjtjtjB�}t�|�|��|�d�|�dd�dS)N�r�d)rr#rrSrXrYrZr��r/r|rrr�test_fdopens


zFileTests.test_fdopencCs�tjd}|�tjtj�|�tj|�ttjd�t|d�t�tj|�|�ttj	tj�t
|ddd��}|�|��d�Wd�dS1sJwYdS)Nz.2�1�2r�r��r��1)
rrSr�rTr'r�replacerhrA�statr#r;ry)r/�TESTFN2r_rrr�test_replaces

"�zFileTests.test_replacecCs"tjttjddd�}t�|�dS)N�)r<�flags�mode�dir_fd)rr#�__file__r�rZr^rrr�test_open_keywords,s�zFileTests.test_open_keywordsc	Cs>t�td�}z|dtjddd�WdSttfyYdSw)N�symlink�targetF)�src�dst�target_is_directoryr�)rZ
get_attributerrrS�NotImplementedErrorrB)r/r�rrr�test_symlink_keywords1s��zFileTests.test_symlink_keywords�copy_file_rangeztest needs os.copy_file_range()cC�>|�t��t�ddd�Wd�dS1swYdS�Nrrai����)rh�
ValueErrorrr�rUrrr�#test_copy_file_range_invalid_values9�"�z-FileTests.test_copy_file_range_invalid_valuesc	
Cs"tjd}d}ttj|�|�tjtj�ttjd�}|�|j�|��}t|d�}|�tj|�|�|j�|��}z	t�	||d�}Wnt
yc}z|jtjkrS�|�
|�WYd}~dSd}~ww|�|tdd��t|d��}|�|��|d|��Wd�dS1s�wYdS)N�.3�
0123456789r�rs�r�)rrSr'r�rTr#rZrwrr�rBrC�ENOSYSrf�assertIn�ranger;ry)	r/r�r��in_file�in_fd�out_file�out_fd�i�errr�test_copy_file_range>s.

��
"�zFileTests.test_copy_file_rangec

Cs^tjd}d}d}d}d}ttj|�|�tjtj�ttjd�}|�|j�|��}t|d�}|�tj|�|�|j�|��}	ztj	||	|||d�}
Wnt
yl}z|jtjkr\�|�
|�WYd}~dSd}~ww|�|
td	|d
��t|d��}|��}Wd�n1s�wY|�|d|�d|�|�||d�||||
��dS)N�.4r�r��r�r�rs)�
offset_src�
offset_dstrra�)rrSr'r�rTr#rZrwrr�rBrCr�rfr�r�ryr;)
r/�TESTFN4r��
bytes_to_copy�in_skip�out_seekr�r�r�r�r�r�ryrrr�test_copy_file_range_offset`sB



���

��z%FileTests.test_copy_file_range_offset�spliceztest needs os.splice()cCr�r�)rhr�rr�rUrrr�test_splice_invalid_values�r�z$FileTests.test_splice_invalid_valuesc
s�tjd}d}ttj|�|�tjtj�ttjd�}|�|j�|��}t�	�\��|��fdd��|��fdd��z	t�
|�d�}Wntye}z|jtj
krU�|�|�WYd}~dSd}~ww|�|tdd	��|�t��d
�|d|��dS)Nr�r�r�c�
t���Sr*�rrZr��read_fdrr�<lambda>��
z'FileTests.test_splice.<locals>.<lambda>cr�r*r�r��write_fdrrr��r�r�rr�r�)rrSr'r�rTr#rZrwr�piper�rBrCr�rfr�r�r;ry)r/r�r�r�r�r�r�r�r�r�r�test_splice�s(
��
 zFileTests.test_splicec

stjd}d}d}d}ttj|�|�tjtj�ttjd�}|�|j�|��}t�	�\��|��fdd��|��fdd��ztj
|�||d	�}Wntyk}z|jtj
kr[�|�|�WYd}~dSd}~ww|�|td
|d��t��d�}	|�|	||||��dS)
Nr�r�r�r�r�cr�r*r�rr�rrr��r�z1FileTests.test_splice_offset_in.<locals>.<lambda>cr�r*r�rr�rrr��r�)r�rrar�)rrSr'r�rTr#rZrwrr�r�rBrCr�rfr�r�ryr;)
r/r�r�r�r�r�r�r�r�ryrr�r�test_splice_offset_in�s.
��
zFileTests.test_splice_offset_inc
sptjd}d}d}d}ttj|�|�tjtj�t��\��|��fdd��|��fdd��t��|�t|d�}|�tj|�|�|j	�|�
�}ztj�|||d	�}Wntyw}z|j
t
jkrg�|�|�WYd}~dSd}~ww|�|td
|d��t|d��}	|	��}
Wd�n1s�wY|�|
d|�d
|�|�|
|d�|d|��dS)Nr�r�r�r�cr�r*r�rr�rrr��r�z2FileTests.test_splice_offset_out.<locals>.<lambda>cr�r*r�rr�rrr��r�rs)r�rrar�r�)rrSr'r�rTrr�r$r#rZrwr�rBrCr�rfr�r�ryr;)r/r�r�r�r�r�r�r�r�r�ryrr�r�test_splice_offset_out�s8

��

� z FileTests.test_splice_offset_outN)%rNrOrPrV�tearDownr`rkr�cpython_onlyrrr~rrrrZ
bigmemtestr�r�r�r7r8r�r�r�r�r�r�rrr�r�r�r��requires_splice_piper�r�r�rrrrrQ�sT


�
�
	


!
,
$rQc@seZdZdd�Zdd�Zdd�Zdd�Zd	d
�Ze�	e
ed�d�d
d��Ze�	e
ed�d�dd��Z
e�	ejdkd�dd��Ze�	ejdkd�e�	e
ed�d�dd���Zdd�Ze�	ejdkd�dd��Ze�	ejdkd�dd ��Ze�	ejdkd�d!d"��Zd#S)$�StatAttributeTestscCs(tj|_|�tj|j�t|jd�dS)N�ABC)rrS�fnamer�rTr'rUrrrrVszStatAttributeTests.setUpc
	Cs�t�|�}|�|tjd�|�|jd�t|�}tt�D]2}|dd�dkrO|��}|�d�r5dd�}ndd�}|�|t||��|tt|��|�	||�qd�
�D]}tt||�d�}t||d	�d
}|j||dd�qTz|d
|�
d�Wn	ty�Ynwz
d|_|�
d�Wn	ty�Ynwz
d|_|�
d�Wnttfy�Ynwz
d|_|�
d�Wn	ty�Ynwzt�d�}	|�
d�Wn	ty�Ynwzt�d�}	WdSty�YdSw)Nr�ZST_ZTIMEcSst|�Sr*)�int��xrrr�truncsz7StatAttributeTests.check_stat_attributes.<locals>.trunccSs|Sr*rr�rrrr�szst_atime st_mtime st_ctimei��Z_nsi'rc��deltar5�No exception raisedra�rb�rrarcr�rur�r����	rb���
�)rr�r;�ST_SIZE�st_size�dir�lower�endswith�getattrr��splitr��assertAlmostEqual�fail�
IndexError�st_mode�AttributeError�st_rdevrn�parrot�stat_result)
r/r��result�membersr�attrr�ZfloatyZnanosecondy�result2rrr�check_stat_attributessl


������
��z(StatAttributeTests.check_stat_attributescCs|�|j�dSr*)r	r�rUrrr�test_stat_attributesM�z'StatAttributeTests.test_stat_attributescCsFz
|j�t���}Wnty|�d|j�Ynw|�|�dS)Nz#cannot encode %a for the filesystem)r��encoder7�getfilesystemencoding�UnicodeEncodeErrorrfr	)r/r�rrr�test_stat_attributes_bytesPs�z-StatAttributeTests.test_stat_attributes_bytescC�ft�|j�}ttjd�D]#}t�||�}|�d|�|dkr%|�d|�t�|�}|�	||�q
dS)Nrasstat_resultruscos
stat_result
)
rr�r�r��pickle�HIGHEST_PROTOCOL�dumpsr��loadsr;�r/r�proto�pZ	unpickledrrr�test_stat_result_pickleWs
�z*StatAttributeTests.test_stat_result_pickle�statvfsztest needs os.statvfs()cCst�|j�}|�|j|d�d}t|�D]\}}|�t|d|�||�q|�t|j	t
��|�t|�d�z
d|_|�d�Wn	t
yKYnwz
d|_|�d�Wn	t
y_Ynwzt�d�}|�d�Wn	tyuYnwzt�d�}WdSty�YdSw)	Nr�)
ZbsizeZfrsizeZblocksZbfreeZbavail�filesZffreeZfavail�flagZnamemaxZf_rbrar�r�r�)rrr�r;�f_bfree�	enumerater�r[�
isinstance�f_fsidr�r9r�rr�statvfs_resultrn)r/rr�value�memberrrrr�test_statvfs_attributesas<��
��z*StatAttributeTests.test_statvfs_attributes�need os.statvfs()cCr)Nrasstatvfs_resultruscos
statvfs_result
)
rrr�r�rrrr�rr;rrrr�test_statvfs_result_pickle�s
�z-StatAttributeTests.test_statvfs_result_pickler��Win32 specific testsc
Cs^zt�d�WdSty|�d�YdSty.}z|�d�WYd}~dSd}~ww)Nzc:\pagefile.syszc:\pagefile.sys does not existzCould not stat pagefile.sys)rr�rArfrBr��r/r�rrr�test_1686475�s��zStatAttributeTests.test_1686475r�zrequires os.pipe()c	Cs�t��\}}zt�|�Wt�|�t�|�nt�|�t�|�w|�t��
}t�|�Wd�n1s8wY|�|jjtj	�dSr*)
rr�r�rZrhrBr;�	exceptionrC�EBADF)r/r��w�ctxrrr�
test_15261�s

��zStatAttributeTests.test_15261cCsL|�t|d��|�t|jt��|�d|jko dk�dS�dS)N�st_file_attributesr���)r[rrr.r�)r/rrrr�check_file_attributes�s*z(StatAttributeTests.check_file_attributesz$st_file_attributes is Win32 specificcCszt�|j�}|�|�|�|jtj@d�tjd}t�	|�|�
tj|�t�|�}|�|�|�|jtj@tj�dS)Nrr�)rr�r�r0r;r.�FILE_ATTRIBUTE_DIRECTORYrrSr?r��rmdir)r/rrFrrr�test_file_attributes�s

�




�z'StatAttributeTests.test_file_attributescCsdtj�tjd|j�}|�tj|�t|d�d}t	j
d|ddg|d�t�|�}|�|j
d�dS)	NZTEMPr�r�z
icacls.exez/denyz*S-1-5-32-545:(S)�r�r)rr<r>�environr�r�rrTr'r��
check_callr��assertNotEqualr�)r/r�ZDETACHED_PROCESSrrrr�test_access_denied�s

�
z%StatAttributeTests.test_access_deniedcCs6dtj�t���d}t�|�}|�|jtj�dS)Nz//./r)rr<�
splitdriver+r�r;r�S_IFBLK)r/r�rrrr�test_stat_block_device�s
z)StatAttributeTests.test_stat_block_deviceN)rNrOrPrVr	r
rrrrrrr#r%r7r8r(r-r0r3r8r;rrrrr�s4A

+�

	�

r�c@s�eZdZdd�Zdd�Zd)dd�Zdd	�Zed
d��Zdd
�Z	dd�Z
e�e
je
jvd�dd��Ze�e
je
jvd�dd��Ze�e
je
jvd�dd��Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zejd'd(��ZdS)*�
UtimeTestscCsDtj|_tj�|jd�|_|�tj|j�t�	|j�t
|j�dS)N�f1)rrSrFrr<r>r�r��rmtreer?r'rUrrrrV�s
zUtimeTests.setUpcCs4t�|�}|j|dkp|j|dkp|j|dkS)Nr�r�r�)rr��st_atime�st_mtime�st_ctime)r/r%�strrr�support_subsecond�s
��zUtimeTests.support_subsecondNcCs�|s|j}|�|�}|rd}d}nd}d}||||f�t�|�}|r:|j|j|ddd�|j|j|ddd�n|�|j|d�|�|j|d�|�|j|�|�|j	|�dS)Ni8Z�;l�
o]�rTlP�9��&�.>g���ư>r�)
r�rCrr�r�r?r@r;�st_atime_ns�st_mtime_ns)r/�set_timer%rC�atime_ns�mtime_nsrBrrr�_test_utime�s"

zUtimeTests._test_utimecC�dd�}|�|�dS)NcS�tj||d�dS�N��ns�r�utime�r%rPrrrrH�z'UtimeTests.test_utime.<locals>.set_time�rK�r/rHrrr�
test_utimeszUtimeTests.test_utimecCs|ddS)NrEg��&�.>rrOrrr�	ns_to_sec szUtimeTests.ns_to_secc��fdd�}��|�dS)Ncs0|\}}��|�}��|�}t�|||f�dSr*�rXrrR�r%rPrIrJZatime�mtimerUrrrH)s

z2UtimeTests.test_utime_by_indexed.<locals>.set_timerUrVrrUr�test_utime_by_indexed'sz UtimeTests.test_utime_by_indexedcrY)Ncs2|\}}��|�}��|�}tj|||fd�dS)N)�timesrZr[rUrrrH3s

z0UtimeTests.test_utime_by_times.<locals>.set_timerUrVrrUr�test_utime_by_times2szUtimeTests.test_utime_by_timesz9follow_symlinks support for utime required for this test.cCrL)NcSstj||dd�dS)NF)rP�follow_symlinksrQrSrrrrH?sz9UtimeTests.test_utime_nofollow_symlinks.<locals>.set_timerUrVrrr�test_utime_nofollow_symlinks;sz'UtimeTests.test_utime_nofollow_symlinksz,fd support for utime required for this test.cCrL)NcSsDt|dd��}tj|��|d�Wd�dS1swYdS)N�wbrrO)r#rrRrw)r%rPr&rrrrHHs"�z*UtimeTests.test_utime_fd.<locals>.set_timerUrVrrr�
test_utime_fdE�zUtimeTests.test_utime_fdz0dir_fd support for utime required for this test.cCrL)NcSsPtj�|�\}}t�|��}tj|||d�Wd�dS1s!wYdS)N)r�rP)rr<r�r�open_dir_fdrR)r%rPrFr�dirfdrrrrHRs"�z.UtimeTests.test_utime_dir_fd.<locals>.set_timerUrVrrr�test_utime_dir_fdOrdzUtimeTests.test_utime_dir_fdcCsdd�}|j||jd�dS)NcSrMrNrQrSrrrrHZrTz1UtimeTests.test_utime_directory.<locals>.set_time�r%)rKrFrVrrr�test_utime_directoryYszUtimeTests.test_utime_directorycCsbt��}||j�|�|j�sd}nd}t�|j�}d|j||j|f}|j|j|||d�dS)Ng�?g�������?zst_time=%r, current=%r, dt=%r)r��msg)�timer�rCrr�r@r�)r/rHZcurrentr�rBrjrrr�_test_utime_current_s
�

�zUtimeTests._test_utime_currentcrY)Ncst��j�dSr*�rrRr�rhrUrrrHv�z/UtimeTests.test_utime_current.<locals>.set_time�rlrVrrUr�test_utime_currentu�zUtimeTests.test_utime_currentcrY)Ncst��jd�dSr*rmrhrUrrrH|rTz3UtimeTests.test_utime_current_old.<locals>.set_timerorVrrUr�test_utime_current_old{rqz!UtimeTests.test_utime_current_oldcCsptjdkr4tj�tj�|��dd}ddl}|jj}|�	dd�}|�
|ddddd|t|��}|r6|jSdSdS)Nr�r�\�r�)
r7r8rr<r9�abspath�ctypes�windll�kernel32Zcreate_unicode_bufferZGetVolumeInformationWr9r!)r/r<�rootrvrx�buf�okrrr�get_file_system�s

��zUtimeTests.get_file_systemcCsJ|�|j�dkr
|�d�d}t�|j||f�|�t�|j�j|�dS)NZNTFSz
requires NTFSrD)	r|rFrfrrRr�r;r�r@)r/Zlargerrr�test_large_time�s

zUtimeTests.test_large_timec	Cs�|�t��tj|jddd�Wd�n1swY|�t��t�|jddg�Wd�n1s7wY|�t��t�|jd�Wd�n1sSwY|�t��t�|jd�Wd�n1sowY|�t��tj|jddgd�Wd�n1s�wY|�t��tj|jdd�Wd�n1s�wY|�t��tj|jdd�Wd�n1s�wYtjtjvr�|�t��tj|jddd�Wd�n1s�wYtjtjv�r.t	|jdd	��&}|�t��t�|�
�d�Wd�n	1�swYWd�n	1�s)wYtjtjv�rW|�t��tj|jdd	d
�Wd�dS1�sPwYdSdS)N)r�r�rOr�)r�)r�r�r�F�r`rbr�r�)rhr�rrRr�rn�supports_follow_symlinksr��supports_fdr#rw�supports_dir_fd)r/r&rrr�test_utime_invalid_arguments�sJ�����������$��z'UtimeTests.test_utime_invalid_argumentscCs�dd�}|�t��tj|j|d�dfd�Wd�n1s wY|�t��tj|j|d�dfd�Wd�n1sAwY|�t��tj|j|d�dfd�Wd�dS1scwYdS)NcsG�fdd�d�}|�S)NcseZdZ�fdd�ZdS)z?UtimeTests.test_issue31577.<locals>.get_bad_int.<locals>.BadIntcs�Sr*r)r���divmod_ret_valrr�
__divmod__��zJUtimeTests.test_issue31577.<locals>.get_bad_int.<locals>.BadInt.__divmod__N)rNrOrPr�rr�rr�BadInt�sr�r)r�r�rr�r�get_bad_int�sz/UtimeTests.test_issue31577.<locals>.get_bad_int�*rarOr)rarcr�)rhrnrrRr�)r/r�rrr�test_issue31577�s��"�zUtimeTests.test_issue31577r*)rNrOrPrVrCrKrW�staticmethodrXr]r_rrrrRr�rar�rcr�rgrirlrprrr|r}r�rr�r�rrrrr<�s<

	�
�
�


r<)�
mapping_testsc@sBeZdZdZdZdd�Zdd�Zdd�Zd	d
�Ze	�
eo!ej
�e�d�e	�
eed�d
�dd���Ze	�
eo:ej
�e�d�e	�
eed�d
�dd���Zdd�Zdd�Zdd�Zdd�Ze	�
ejd�dd��Zdd�Ze�dd �d!d"��Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Z d-d.�Z!d/d0�Z"d1d2�Z#d3d4�Z$d5d6�Z%d7d8�Z&dS)9�EnvironTestsz8check that os.environ object conform to mapping protocolNcCsBttj�|_tjrttj�|_|����D]	\}}|tj|<qdSr*)	�dictrr5�_EnvironTests__save�supports_bytes_environ�environb�_EnvironTests__saveb�
_reference�items�r/�keyr!rrrrV�s�zEnvironTests.setUpcCs>tj��tj�|j�tjrtj��tj�|j�dSdSr*)rr5�clear�updater�r�r�r�rUrrrr��s

�zEnvironTests.tearDowncCsdddd�S)NZVALUE1ZVALUE2ZVALUE3)ZKEY1ZKEY2ZKEY3rrUrrrr���zEnvironTests._referencecCstj��tjSr*)rr5r�rUrrr�_empty_mapping�s
zEnvironTests._empty_mappingzrequires a shell�popenzneeds os.popen()cCsdtj��tjjdd�t�dt��}|����}|�|d�Wd�dS1s+wYdS)NZWorld)ZHELLOz%s -c 'echo $HELLO')	rr5r�r�r�rry�stripr;)r/r�r!rrr�test_update2�s
"�zEnvironTests.test_update2cCszt�dt��,}t|�}|�t|�d�|�t|�d�|�t|�d�|�tt|�Wd�dS1s6wYdS)Nz %s -c 'echo "line1
line2
line3"'zline1
zline2
zline3
)rr�r�iterr;�nextrh�
StopIteration)r/r��itrrr�test_os_popen_iter�s�"�zEnvironTests.test_os_popen_itercCs8tj��D]\}}|�t|�t�|�t|�t�qdSr*)rr5r�r;rzr-)r/r��valrrr�test_keyvalue_types�s�z EnvironTests.test_keyvalue_typescCs.|����D]\}}|�tj�|�|�qdSr*)r�r�r;rr5�getr�rrr�
test_items�s�zEnvironTests.test_itemsc
Cs4tj}|�t|�d�d�dd�|��D����dS)z>Check that the repr() of os.environ looks like environ({...}).zenviron({{{}}})z, css �|]\}}d�||�VqdS)z
{!r}: {!r}N��format)rr�r!rrr�	<genexpr>s
�
�
�z-EnvironTests.test___repr__.<locals>.<genexpr>N)rr5r;�reprr�r>r�)r/�envrrr�
test___repr__s�zEnvironTests.test___repr__cCs\tj�tj�}gd�}dtj�|�i}tj}zt|�t_|�|t���|�|tjdd��W|t_n|t_w|�|t�i��|�dt�ddi��|�|t�|��tj	r�zt
jdd��
dd	d
�}Wd�n1snwYWn	ty}Yn	w|�
ttj|�|�t�ddi�d
g�|�t�dd
i�d
g�|�t�ddi�d
g�dSdS)N)z/montyz/pythonrtz/flying/circus�PATH�r��rtrtT)�recordr�r�)r��PATHr��abc�abc)r�defpathr��pathsepr>r5r��assertSequenceEqual�
get_exec_pathr��warnings�catch_warnings�BytesWarningrhr�)r/Zdefpath_listZ	test_pathZtest_envZ
saved_environZ	mixed_envrrr�test_get_exec_path
s@
�������zEnvironTests.test_get_exec_pathz#os.environb required for this test.cCs�d}z
|�t��d�}Wnty!dt��f}|�|�Ynw|tjd<|�tjd|�|�tjd|�d}|tjd<|�tjd|�|�	t��d�}|�tjd|�dS)	Nueuro€�surrogateescapez'U+20AC character is not encodable to %sZunicodesunicode��sbytesrK)
rr7r
rrfrr5r;r��decode)r/r!Zvalue_bytesrjZ	value_strrrr�
test_environb3s&
���

zEnvironTests.test_environbcCs�d}d}d|�d�}t���L}|�|d�t�||�tjtjd|gdtj	dd�}|�
|j��t
|��t�|�tjtjd|gdtj	dd�}|�
|j��t
d��Wd�dS1s]wYdS)NZ
PYTHONTESTVARZ	testvaluez%import os; print(repr(os.environ.get(z)))r�T)�check�stdout�text)rZEnvironmentVarGuard�popr�putenvr��runr7r��PIPEr;r��rstripr��unsetenv)r/rr!r�r��procrrr�test_putenv_unsetenvJs 
�
�"�z!EnvironTests.test_putenv_unsetenvrbr�cCs�dD]}|�ttftj|d�|�ttftj|�qtjdkr?dd}|�ttj|d�|�ttjd|�|�ttj|�dSdS)N)rtz=namezna=mezname=znameznamer!r�r���r��X)rhrBr�rr�r�r7r8)r/rZlongstrrrr�test_putenv_unsetenv_error]s
�z'EnvironTests.test_putenv_unsetenv_errorcCs�d}|�|tj�|�t��
}tj|Wd�n1swY|�|jjd|�|�|jj	�|�t��}tj|=Wd�n1sHwY|�|jjd|�|�|jj	�dS)NZ
missingkeyr)
�assertNotInrr5rh�KeyError�assertIsr)r�r[�__suppress_context__)r/Zmissing�cmrrr�
test_key_typems�
�zEnvironTests.test_key_typecCsRt|�}d}t|�dtj|<zt|�|�tj|d�Wtj|=dStj|=w)NZ__new_key__Ztest_environ_iteration)r�r�rr5r;)r/Z
collection�iteratorZnew_keyrrr�_test_environ_iteration{s
z$EnvironTests._test_environ_iterationcC�|�tj�dSr*)r�rr5rUrrr�(test_iter_error_when_changing_os_environ�rz5EnvironTests.test_iter_error_when_changing_os_environcC�|�tj���dSr*)r�rr5r�rUrrr�.test_iter_error_when_changing_os_environ_items��z;EnvironTests.test_iter_error_when_changing_os_environ_itemscCr�r*)r�rr5�valuesrUrrr�/test_iter_error_when_changing_os_environ_values�r�z<EnvironTests.test_iter_error_when_changing_os_environ_valuescCsftrtj�t�s
dSt�t�d|�d���}|����}Wd�n1s&wY|�||�dS)Nz -c 'echo $�')rrr<�existsr�ryr�r;)r/�varrGr�r!rrr�_test_underlying_process_env�s�z)EnvironTests._test_underlying_process_envcCs�d}d}|tj|<dddd|di}ttj�}|�|�tj|B}|�||�|�d||�|��}|�ttj�	|��|�
dd�|�
||�dS�	N�
_TEST_VAR_�original_value�_A_r��_B_�2�3rt)rr5r�r��assertDictEqualr;r�r��NotImplemented�__or__r��r/�overridden_keyr��
new_vars_dictrG�actual�new_vars_itemsrrr�test_or_operator�s



zEnvironTests.test_or_operatorcCs|d}dtj|<dddd|di}ttj�}|�|�tj|O_|�|tj�|�dtj|�|�dd�|�|d�dS)Nr�r�r�r�r�r�r��rr5r�r�r;r�)r/r�r�rGrrr�test_ior_operator�s


zEnvironTests.test_ior_operatorcCs�tj��}|�t��ddi}tj|O_Wd�n1s wY|�t��ddi}tj|O_Wd�n1s@wY|�|tj�dS)Nrar�)rr5�copyrhrnr;)r/Zos_environ_copyZdict_with_bad_keyZdict_with_bad_valrrr�test_ior_operator_invalid_dicts�s
��z,EnvironTests.test_ior_operator_invalid_dictscCszd}dtj|<dd|dff}ttj�}|�|�tj|O_|�|tj�|�dtj|�|�dd�|�|d�dS)Nr�r�)r�r�)r�r�r�r�r�r�)r/r�r�rGrrr�$test_ior_operator_key_value_iterable�s


z1EnvironTests.test_ior_operator_key_value_iterablecCs�d}d}|tj|<dddd|di}t|�}|�tj�|tjB}|�||�|�|||�|��}|�ttj�	|��|�
dd�|�
||�dSr�)rr5r�r�r�r;r�r�r��__ror__r�r�rrr�test_ror_operator�s

zEnvironTests.test_ror_operator)'rNrOrP�__doc__Z	type2testrVr�r�r�rrrrr<r�rr�r�r�r�r�r�r�r�r�rZrequires_mac_verr�r�r�r�r�r�r�r�r�r�r�r�rrrrr��sN��)�


	
r�c@sZeZdZdZdd�Zdd�Zdd�Zdd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�ZdS)�	WalkTestszTests for os.walk().cKs(d|vr|�d�|d<tj|fi|��S�Nr`�followlinks)r�r�walk)r/�top�kwargsrrrr��szWalkTests.walkc	Cs:tjj}|�tjtj�|tjd�|_||jd�|_||jd�|_	||jd�}||d�}||jd�}||jd�}||d�}||d�}||d	�|_
|tjd
�}|tjd
d�}	||d�}
||d
�}||d�}t�|j	�t�|�t�|�t�|�||||	|fD]"}
t|
ddd��}|�
d|
d�Wd�n1s�wYq�t��r�t�tj�|�|j
�t�d|
d�t�|dd�|d�t�|dd�|d�|dd	ggd�f|_n|dgdgf|_t�|d�zt�|�Wnt�y|�tj|tj�YdSwt�|tj�t�|�t�|�|jddd�=dS)NZTEST1�SUB1�SUB11�SUB2ZSUB21�tmp1�tmp2�tmp3�linkZTEST2�tmp4�broken_link�broken_link2�broken_link3r�r�r�zI'm z" and proud of it.  Blame test_os.
ZbrokenTZtmp5)rr	r
rrra)rr<r>r�rr>rS�	walk_path�	sub1_path�
sub11_path�	link_path�makedirsr#r$�can_symlinkr�ru�	sub2_tree�chmod�listdir�PermissionErrorr��S_IRWXUrTr2)r/r>Z	sub2_pathZ
sub21_pathZ	tmp1_pathZ	tmp2_pathZ	tmp3_pathZ	tmp5_pathZt2_pathZ	tmp4_pathZbroken_link_pathZbroken_link2_pathZbroken_link3_pathr<r_rrrrV�sZ








����

zWalkTests.setUpcCs�t|�|j��}|�t|�d�|ddddk}|dd��|dd|d��|dd|d��|�|d|jddgd	gf�|�|d||jd
gdgf�|�|d||jggf�|�|dd||j�dS)Nrurrarr�rc���rrrr)	�listr�rr;r9�sortrr
r�r/�allZflippedrrr�test_walk_topdown=s zWalkTests.test_walk_topdownNcCs�|dur|j}g}|�|�D]\}}}|�|||f�d|vr$|�d�q|�t|�d�|�|d|jdgdgf�|dd��|dd��|�|d|j�dS)Nrrcrrrrar)rr��append�remover;r9rr)r/rrry�dirsrrrr�test_walk_pruneNs
�zWalkTests.test_walk_prunecCs|�t|j��dSr*)rrrrUrrr�test_file_like_pathar�zWalkTests.test_file_like_pathcCs�t|j|jdd��}|�t|�d|�|ddddk}|dd��|dd|d	��|dd|d��|�|d|jdd
gdgf�|�|||jggf�|�||d|jdgd
gf�|�|dd||j�dS)NF��topdownrur�rarrrcrrrrr)	rr�rr;r9rr
rrrrrr�test_walk_bottom_upds$
�

���zWalkTests.test_walk_bottom_upcCsjt��s	|�d�|j|jdd�}|D]\}}}||jkr-|�|g�|�|dg�dSq|�d�dS)Nzneed symlink supportTr~rz+Didn't follow symlink with followlinks=True)rrrfr�rrr;r�)r/�walk_itryrrrrr�test_walk_symlinkys

�zWalkTests.test_walk_symlinkc
Cs�g}|j|j|jd�}t|�\}}}|�|g�d}tj�||�}tj�||d�}t�||�z5dd�|D�}	|�	|�|�
||	�|�
||	�|D]}
|
|kr\|�tj�||
�|	�qKWt�||�dSt�||�w)N)�onerrorrz.newcSsg|]\}}}|�qSrr)rr��dr_rrrr��z/WalkTests.test_walk_bad_dir.<locals>.<listcomp>)r�rrr�r;rr<r>ror[r�r�)r/�errorsr$ryrr�dir1Zpath1Zpath1new�roots�dir2rrr�test_walk_bad_dir�s&
��zWalkTests.test_walk_bad_dircsd}tj�tjd��tjj�gdg|�R�}t�|���fdd�td�D�}t|d�D]}||r6dgnggf}|D]
}��t|�|�q<tj�	|�}q.��fdd�td�D�}�}t|d�D]"}|||krldgnggf}|D]
}��t|�|�qrtj�|d�}qbdS)	N�Zdeepr'c�g|]	}�j�dd��qS)Fr!�r��r�j��baser/rrr��z7WalkTests.test_walk_many_open_files.<locals>.<listcomp>r�racr/)Tr!r0r1r3rrr�r5)
rr<r>rrSrr�r;r�rF)r/ZdepthrZitersr�rGr�rr3r�test_walk_many_open_files�s$
�z#WalkTests.test_walk_many_open_filesr*)
rNrOrPr�r�rVrrr r#r%r-r6rrrrr��sE
r��fwalkzTest needs os.fwalk()c@sLeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dZdS)�
FwalkTestszTests for os.fwalk().cks2�|j|fi|��D]\}}}}|||fVq
dSr*)r7)r/r�r�ryrrZroot_fdrrrr��s��zFwalkTests.walkcOstj|i|��Sr*)rr7�r/r�r�rrrr7�rzFwalkTests.fwalkc
Cs�|��}|��}tjddd�D]O\}}|j||d�|j||d�i}tjdi|��D]\}}}t|�t|�f||<q+|jdi|��D]\}}}}	|�||�|�	||t|�t|�f�qCqdS)z.
        compare with walk() results.
        �TFrc��repeat)r"r�)r"r`Nr)
r��	itertools�productr�rr��setr7r�r;)
r/�walk_kwargs�fwalk_kwargsr"r`rGryrr�rootfdrrr�_compare_to_walk�s��zFwalkTests._compare_to_walkcCsdtji}|�||�dS)Nr�)rrSrC)r/r�rrr�test_compare_to_walk��
zFwalkTests.test_compare_to_walkc	CsRz"t�dtj�}dtji}|��}||d<|�||�Wt�|�dSt�|�w)N�.r�r�)rr#r�rrSr�rCrZ)r/r|r@rArrr�test_dir_fd�s
zFwalkTests.test_dir_fdcCs|tjddd�D]4\}}tj|df}|j|d|i�D]!\}}}}t�|�t�|�|�t	t�
|��t	|�t	|�B�qqdS)Nr:rcr;r`)r=r>rrSr7r�fstatr�r;r?r)r/r"r`r�ryrrrBrrr�test_yields_correct_dir_fd�s

$��z%FwalkTests.test_yields_correct_dir_fdcCs\t�d�}t�|�td�D]}|�tj�D]}qqt�d�}|�tj|�|�||�dS)Nra�)	rrerZr�r7rrSr�r;)r/Zminfdr�r�Znewfdrrr�test_fd_leak�s

�
zFwalkTests.test_fd_leakN)rNrOrPr�r�r7rCrDrGrIrKr6rrrrr8�s
r8c@�eZdZdZdd�ZdS)�BytesWalkTests�Tests for os.walk() with bytes.c	ks��d|vr|�d�|d<tjt�|�fi|��D]8\}}}t�|�}tttj|��}tttj|��}|||fVtttj|��|dd�<tttj|��|dd�<qdSr�)r�rr��fsencoderLr�map)	r/r�r��broot�bdirs�bfilesryrrrrrr��s�"
�zBytesWalkTests.walkN)rNrOrPr�r�rrrrrM�srMc@seZdZdZddd�ZdS)�BytesFwalkTestsrNrFcos��tjt�|�g|�Ri|��D]:\}}}}t�|�}tttj|��}	tttj|��}
||	|
|fVtttj|	��|dd�<tttj|
��|dd�<qdSr*)rr7rOrLrrP)r/r�r�r�rQrRrS�topfdryrrrrrr7
s�*
�zBytesFwalkTests.fwalkN)rF)rNrOrPr�r7rrrrrTsrTc@s^eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Ze	�
ejd
kd�dd��Z
dd�ZdS)�MakedirTestscC�t�tj�dSr*�rr?rrSrUrrrrVrzMakedirTests.setUpc
Cs�tj}tj�|ddd�}t�|�tj�|dddd�}t�|�|�ttjtj�tj�|dddddtj�}t�|�tj�|dtjddddd�}t�|�dS�Nr*r,Zdir3Zdir4Zdir5Zdir6)	rrSrr<r>rrhrB�curdir)r/r4r<rrr�test_makedirs


�zMakedirTests.test_makedircCs�t�d��Wtj}tj�|d�}tj�|d�}t�|d�|�tj�|��|�tj�	|��tj
dkrT|�t�|�j
d@d�|�t�|�j
d@d�Wd�dSWd�dS1s_wYdS)Nrcr*r,im�ntr�i�)rZ
temp_umaskrSrr<r>rr[r��isdirrr;r�r)r/r4�parentr<rrr�	test_mode's
�"�zMakedirTests.test_modecCs�tj�tjd�}d}t�d�}t�||�|�ttj||�|jttj||dd�tj|ddd�tj||dd�t�|�tjtj�	d	�dd�dS)
Nr*r��F��exist_oki�T)r�rb�/)
rr<r>rrS�umaskrrhrBru)r/r<r��old_maskrrr� test_exist_ok_existing_directory3s

z-MakedirTests.test_exist_ok_existing_directoryc	Cs�tj�tjd�}tj}d}t�d�}zdt�t�	tj�j
�}zt�tj||B�Wnty4t
�d��wt�	tj�j
|@|krEt
�d��t�|||B�tj||dd�t�|t�t�	|�j
�|@�tj|||Bdd�Wt�|�dSt�|�w)Nr*r�r`zCannot set S_ISGID for dir.z No support for S_ISGID dir mode.Tra)rr<r>rrSr��S_ISGIDrd�S_IMODE�lstatrrrr�SkipTestr)r/r<rgr�reZexisting_testfn_moderrr�test_exist_ok_s_isgid_directoryAs(
�
�
 z,MakedirTests.test_exist_ok_s_isgid_directorycCs�tj}tj�tjd�}t|ddd��
}|�d�Wd�n1s"wY|�ttj	|�|jttj	|dd�|jttj	|dd�t�
|�dS)	Nr*r+r�r�r�FraT)rrSrr<r>r#r$rhrBrr)r/r4r<r_rrr�#test_exist_ok_existing_regular_file\s�z0MakedirTests.test_exist_ok_existing_regular_filer\zrequires WindowscCsdtj}tj�tj�tjd��}tj|dd�tjd|dgdd�}t�	|�|�
|��|�d��dS)	Nr�i�)r�z	cacls.exez/sZoemr�z6 "D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)")rrSrr<rur>r?r��check_outputr2r;r�)r/r4r<�outrrr�test_win32_mkdir_700fs
�z!MakedirTests.test_win32_mkdir_700c	Cs`tj�tjdddddd�}tj�|�s)|tjkr)tj�|�}tj�|�s)|tjkst�|�dSrY)rr<r>rrSr�rF�
removedirs�r/r<rrrr�rs��zMakedirTests.tearDownN)rNrOrPrVr[r_rfrkrlrrrrror�rrrrrVs

rV�chownzTest needs chownc@s�eZdZedd��Zdd�Ze�ee	d�d�dd��Z
e�eo$ee
�d	kd
�dd��Ze�eo5ee
�d	kd
�dd��Zedd��ZdS)�ChownFileTestscCrWr*rX��clsrrr�
setUpClass�rnzChownFileTests.setUpClasscCs�t�tj�}|j}|j}ddt�d�t�	dd�fD]}|�
ttjtj||�|�
ttjtj||�q|�
t�tj||��|�
t�tjdd��dS)Ng�y��r���rc)rr�rrS�st_uid�st_gid�decimalZDecimal�	fractionsZFractionrhrnrr�assertIsNone)r/r��uid�gidr!rrr�*test_chown_uid_gid_arguments_must_be_index�sz9ChownFileTests.test_chown_uid_gid_arguments_must_be_index�	getgroupszneed os.getgroupscCs�t��}t|�dkr|�d�|dd�\}}t�tj�j}t�tj||�t�tj�j	}|�
||�t�tj||�t�tj�j	}|�
||�dS)Nrcztest needs at least 2 groups)rr�r9rfr�rrSrxrrryr;)r/�groupsZgid_1Zgid_2r}r~rrr�test_chown_gid�s
zChownFileTests.test_chown_gidraz0test needs root privilege and more than one usercCsvtdd�\}}t�tj�j}t�tj||�t�tj�j}|�||�t�tj||�t�tj�j}|�||�dS�Nrc)	�	all_usersrr�rrSryrrrxr;)r/�uid_1�uid_2r~r}rrr�test_chown_with_root�sz#ChownFileTests.test_chown_with_rootz2test needs non-root account and more than one usercCsntdd�\}}t�tj�j}|�t��t�tj||�t�tj||�Wd�dS1s0wYdSr�)	r�rr�rrSryrhrrr)r/r�r�r~rrr�test_chown_without_permission�s"�z,ChownFileTests.test_chown_without_permissioncCrWr*)rr2rrSrtrrr�
tearDownClass�rnzChownFileTests.tearDownClassN)rNrOrP�classmethodrvrrrrrr��
root_in_posixr9r�r�r�r�rrrrrs~s 


�

�
rsc@�4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�RemoveDirsTestscCrWr*)rrrrSrUrrrrV�rzRemoveDirsTests.setUpcCst�tj�dSr*)rr>rSrUrrrr��rzRemoveDirsTests.tearDowncCsxtj�tjd�}t�|�tj�|d�}t�|�t�|�|�tj�|��|�tj�|��|�tj�tj��dS)N�dira�dirb)	rr<r>rrSr?rp�assertFalser��r/r�r�rrr�test_remove_all�s


zRemoveDirsTests.test_remove_allcCs�tj�tjd�}t�|�tj�|d�}t�|�ttj�|d��t�|�|�tj�	|��|�
tj�	|��|�
tj�	tj��dS�Nr�r��file.txt)rr<r>rrSr?r'rpr�r�r[r�rrr�test_remove_partial�s


z#RemoveDirsTests.test_remove_partialcCs�tj�tjd�}t�|�tj�|d�}t�|�ttj�|d��|�t��
t�	|�Wd�n1s7wY|�
tj�|��|�
tj�|��|�
tj�tj��dSr�)rr<r>rrSr?r'rhrBrpr[r�r�rrr�test_remove_nothing�s

�z#RemoveDirsTests.test_remove_nothingN)rNrOrPrVr�r�r�r�rrrrr��s
r�c@�eZdZdd�ZdS)�DevNullTestscCs�ttjdd��}|�d�|��Wd�n1swYttjd��}|�|��d�Wd�dS1s:wYdS)Nrbr�hellor��)r#r�devnullr$rZr;ryr^rrr�test_devnull�s

�"�zDevNullTests.test_devnullN)rNrOrPr�rrrrr���r�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�URandomTestscCsr|�tt�d��d�|�tt�d��d�|�tt�d��d�|�tt�d��d�|�tt�d��d�dS)Nrrarbr�r4)r;r9r�urandomrUrrr�test_urandom_length�s
z URandomTests.test_urandom_lengthcCs0t�d�}|�|t�t�d�}|�||�dS�N�)rr�r,rKr7�r/Zdata1Zdata2rrr�test_urandom_value�s

zURandomTests.test_urandom_valuecCs<d�dd|ddf�}td|�}|d}|�t|�|�|S)N�
zimport os, syszdata = os.urandom(%s)zsys.stdout.buffer.write(data)zsys.stdout.buffer.flush()r�ra)r>rr;r9)r/�countr�rnr�rrr�get_urandom_subprocess�s�
z#URandomTests.get_urandom_subprocesscCs$|�d�}|�d�}|�||�dSr�)r�r7r�rrr�test_urandom_subprocess�

z$URandomTests.test_urandom_subprocessN)rNrOrPr�r�r�r�rrrrr��s
r��	getrandomzneed os.getrandom()c@s@eZdZedd��Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
S)�GetRandomTestsc
CsBzt�d�WdSty }z|jtjkrt�d���d}~ww)Nraz%getrandom() syscall fails with ENOSYS)rr�rBrCr�rrj)rurHrrrrv
s
��zGetRandomTests.setUpClasscCs*t�d�}|�|t�|�t|�d�dSr�)rr�r,rKr;r9�r/r�rrr�test_getrandom_types
z"GetRandomTests.test_getrandom_typecCst�d�}|�|d�dS)Nrr�)rr�r;)r/�emptyrrr�test_getrandom0rEzGetRandomTests.test_getrandom0cCs|�ttd��dS)N�GRND_RANDOM)r[rrrUrrr�test_getrandom_random"r�z$GetRandomTests.test_getrandom_randomcCs*z
t�dtj�WdStyYdSw�Nra)rr��
GRND_NONBLOCK�BlockingIOErrorrUrrr�test_getrandom_nonblock(s
�z&GetRandomTests.test_getrandom_nonblockcCs$t�d�}t�d�}|�||�dSr�)rr�r7r�rrr�test_getrandom_value0r�z#GetRandomTests.test_getrandom_valueN)
rNrOrPr�rvr�r�r�r�r�rrrrr�s
r�ZHAVE_GETENTROPYraZHAVE_GETRANDOMZHAVE_GETRANDOM_SYSCALLz*os.random() does not use a file descriptorr3z$VxWorks can't set RLIMIT_NOFILE to 1c@s0eZdZe�ed�dd��Zdd�Zdd�ZdS)	�URandomFDTestsz!test requires the resource modulecCsd}td|�dS)Na�if 1:
            import errno
            import os
            import resource

            soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
            resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
            try:
                os.urandom(16)
            except OSError as e:
                assert e.errno == errno.EMFILE, e.errno
            else:
                raise AssertionError("OSError not raised")
            r�rr�rrr�test_urandom_failureBsz#URandomFDTests.test_urandom_failurecCsd}td|�\}}}dS)Naif 1:
            import os
            import sys
            import test.support
            os.urandom(4)
            with test.support.SuppressCrashReport():
                os.closerange(3, 256)
            sys.stdout.buffer.write(os.urandom(4))
            �-Scr)r/r��rcrn�errrrr�test_urandom_fd_closedYs	z%URandomFDTests.test_urandom_fd_closedcCs�|�tjtj�ttjd�djtjd�}td|�\}}}|�t|�d�|�	|dd�|dd��td|�\}}}|�t|�d�|�	||�dS)Nsxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa�if 1:
            import os
            import sys
            import test.support
            os.urandom(4)
            with test.support.SuppressCrashReport():
                for fd in range(3, 256):
                    try:
                        os.close(fd)
                    except OSError:
                        pass
                    else:
                        # Found the urandom fd (XXX hopefully)
                        break
                os.closerange(3, 256)
            with open({TESTFN!r}, 'rb') as f:
                new_fd = f.fileno()
                # Issue #26935: posix allows new_fd and fd to be equal but
                # some libc implementations have dup2 return an error in this
                # case.
                if new_fd != fd:
                    os.dup2(new_fd, fd)
                sys.stdout.buffer.write(os.urandom(4))
                sys.stdout.buffer.write(os.urandom(4))
            )rSr�r�rru)
r�rrTrSr'r�rr;r9r7)r/r�r�rnr�Zout2Zerr2rrr�test_urandom_fd_reopenedgs�z'URandomFDTests.test_urandom_fd_reopenedN)	rNrOrPrr�resourcer�r�r�rrrrr�=s


r�c#s~�g��fdd�}�fdd�}z%tj}tj}tj}|t_|t_|dur&|t_�VW|t_|t_|t_dS|t_|t_|t_w)z�
    Stubs out execv and execve functions when used as context manager.
    Records exec calls. The mock execv and execve functions always raise an
    exception as they would normally never return.
    cs��d||f�td��)N�execvzexecv called)r�RuntimeError�rr���callsrr�
mock_execv�sz#_execvpe_mockup.<locals>.mock_execvcs��d||f�ttjd��)N�execvez
execve called)rrBrCZENOTDIRr�r�rr�mock_execve�sz$_execvpe_mockup.<locals>.mock_execveN)rr�r�r�)r�r�r�Z
orig_execvZorig_execveZorig_defpathrr�r�_execvpe_mockup�s&�	
�r�r�zneed os.execv()c@steZdZe�ed�dd��Zdd�Zdd�Ze�	e
ed�d	�d
d��Zdd
�Z
dd�Ze�	ejdkd�dd��ZdS)�	ExecTestsz4avoid triggering a linuxthreads bug: see issue #4970cCs|�ttjddgd�dS)Nzno such app-)rhrBr�execvperUrrr�test_execvpe_with_bad_program�s�z'ExecTests.test_execvpe_with_bad_programcCsN|�ttjdd�|�ttjdg�|�ttjdd�|�ttjddg�dS)N�notepadrr�rt)rhr�rr�rUrrr�test_execv_with_bad_arglist�sz%ExecTests.test_execv_with_bad_arglistcCsB|�ttjdgd�|�ttjdgi�|�ttjddgi�dS)Nr�rt)rhr�rr�rUrrr�test_execvpe_with_bad_arglist�sz'ExecTests.test_execvpe_with_bad_arglist�_execvpez)No internal os._execvpe function to test.c
Cs�tjd}|turd}tj�t�|�|�}|}gd�}nd}gd�}tj�||�}tjdkr4t�|�}n|}ddi}t��%}|�t	tj
||�|�t|�d	�|�|d
d||ff�Wd�n1sewYt|d��(}|jt
tj
|||d
�|�t|�d	�|�|d
d|||ff�Wd�n1s�wYt��:}|��}	|tur�||	d<n||	d<|jt
tj
|||	d
�|�t|�d	�|�|d
d|||	ff�Wd�dS1s�wYdS)NZabsolutepaths
executable)sprogname�arg1�arg2r�)Zprognamer�r�r\Zspamr�rarr�)r�r�r�r�r�)rr=rKr<r>rOrr�rhr�r�r;r9rBr�r�)
r/Z	test_typeZprogram_pathZprogram�fullpathZnative_fullpathZ	argumentsr�r�Zenv_pathrrr�_test_internal_execvpe�sT


��
�
��	

�
�"�z ExecTests._test_internal_execvpecCs&|�t�tjdkr|�t�dSdS)Nr\)r�r-rrrKrUrrr�test_internal_execvpe_str�s

�z#ExecTests.test_internal_execvpe_strcCs�tjddg}tj��}d|d<|�t��t�|d||�Wd�n1s(wYtj��}d|d<|�t��t�|d||�Wd�n1sOwYtj��}d|d	<|�t��t�|d||�Wd�dS1swwYdS)
Nr��pass�cabbage�FRUITVEGETABLEr�orangeVEGETABLE=cabbage�FRUIT�lemon�FRUIT=ORANGE)r7r�rr5r�rhr�r�)r/r��newenvrrr�test_execve_invalid_env�s 
�
�
"�z!ExecTests.test_execve_invalid_envr�zWin32-specific testc
Csdz
t�ddgi�Wn ty*}z|�|jdup|jdk�WYd}~dSd}~ww|�d�dS)Nrt�argrzNo OSError raised)rr�rBr[�winerrorr�r'rrr�test_execve_with_empty_paths(��z%ExecTests.test_execve_with_empty_pathN)rNrOrPr�skipIf�USING_LINUXTHREADSr�r�r�rrrr�r�r�r7r8r�rrrrr��s�
�
/r�r�r&c@sDeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�Win32ErrorTestsc
Cszzt�tj�Wn*tyd}YdSty2}zd}|�dtj|f�WYd}~dSd}~ww|�dtj�dS)NFTz.file %s must not exist; os.stat failed with %szfile %s must not exist)rr�rrSrArBr�)r/r�rHrrrrVs
���zWin32ErrorTests.setUpcCs|�ttjtjtjd�dS)Nz.bak)rhrBrrorrSrUrrrrr'szWin32ErrorTests.test_renamecC�|�ttjtj�dSr*)rhrBrrrrSrUrrr�test_remove*r zWin32ErrorTests.test_removecCr�r*)rhrBrr@rrSrUrrr�
test_chdir-r zWin32ErrorTests.test_chdircCsT|�tjtj�ttjd��}|�ttjtj�Wd�dS1s#wYdS)Nr�)	r�rrTrSr#rhrBrr?r^rrr�
test_mkdir0s"�zWin32ErrorTests.test_mkdircCs|�ttjtjd�dSr*)rhrBrrRrrSrUrrrrW6�zWin32ErrorTests.test_utimecCs|�ttjtjd�dSrl)rhrBrrrrSrUrrr�
test_chmod9r�zWin32ErrorTests.test_chmodN)
rNrOrPrVrrr�r�r�rWr�rrrrr�sr�c@s�eZdZgd�Zdd�ZeD]Zee�e�de<qdd�Zdd�Ze	�
eed	�d
�dd��Z
e	�
eed
�d�dd��Ze	�
eed�d�dd��Ze	�
eed�d�dd��Ze	�
eed�d�dd��Ze	�
eed�d�dd��Ze	�
eed�d �d!d"��Ze	�
eed#�d$�d%d&��Ze	�
eed'�d(�d)d*��Ze	�
eed+�d,�d-d.��Ze	�
eed/�d0�d1d2��Ze	�
eed3�d4�d5d6��Ze	�
eed7�d8�d9d:��Ze	�
eed;�d<�d=d>��Zd?d@�Ze	�
eedA�dB�dCdD��ZdES)F�
TestInvalidFD)�fchdirre�	fdatasyncrH�fstatvfs�fsync�	tcgetpgrp�ttynamecs�fdd�}|S)Ncs"tt��r|�tt���dSdSr*)rrr�r�rU�r_rr�helperCs
�z(TestInvalidFD.get_single.<locals>.helperr)r_r�rr�r�
get_singleBszTestInvalidFD.get_singleZtest_c
Oshz|t��g|�Ri|��Wnty*}z|�|jtj�WYd}~dSd}~ww|�d|�dS)Nz5%r didn't raise an OSError with a bad file descriptor)r�make_bad_fdrBr;rCr*r�)r/r_r�r�r�rrrr�Js��
�zTestInvalidFD.checkcCs|jtjdd�dS)Nr�r�)r�rr�rUrrrr�Sr�zTestInvalidFD.test_fdopen�isattyztest needs os.isatty()cCs|�t�t���d�dS�NF)r;rr�rr�rUrrr�test_isattyVszTestInvalidFD.test_isattyrgztest needs os.closerange()c	Csnt��}td�D]}z	t�||�Wn	tyYqw|dkr't�d��|�t�	|||d�d�dS)Nrbrcz5Unable to acquire a range of invalid file descriptorsra)
rr�r�rrHrBrrjr;rg)r/r|r�rrrrkZs�� zTestInvalidFD.test_closerange�dup2ztest needs os.dup2()cC�|�tjd�dS)N�)r�rr�rUrrr�	test_dup2jrTzTestInvalidFD.test_dup2c
Cs�t�ttj�}|�tj|�|ddg}tj|dd�D]C\}}||kr]|j||d��,|�	t
��}t�||�Wd�n1s@wY|�|j
jtj�Wd�n1sXwYqdS)Nri�rcr;)r|�fd2)rr#r�r�r�rZr=r>�subTestrhrBr�r;r)rCr*)r/Zvalid_fdZfdsr|r�r,rrr�test_dup2_negative_fdns �����z#TestInvalidFD.test_dup2_negative_fd�fchmodztest needs os.fchmod()cCr�rl)r�rr�rUrrr�test_fchmod~rTzTestInvalidFD.test_fchmod�fchownztest needs os.fchown()cC�|�tjdd�dS)Nr)r�rrrUrrr�test_fchown��zTestInvalidFD.test_fchown�	fpathconfztest needs os.fpathconf()cC� |�tjd�|�tjd�dS)N�PC_NAME_MAX)r�r�pathconfrrUrrr�test_fpathconf��zTestInvalidFD.test_fpathconf�	ftruncateztest needs os.ftruncate()cCrrl)r�r�truncater
rUrrr�test_ftruncate�r	zTestInvalidFD.test_ftruncaterxztest needs os.lseek()cCrrl)r�rrxrUrrr�
test_lseek�rzTestInvalidFD.test_lseekryztest needs os.read()cCr�r�)r�rryrUrrrr~�rTzTestInvalidFD.test_read�readvztest needs os.readv()cCstd�}|�tj|g�dS)Nrb)r�r�rr)r/rzrrr�
test_readv�szTestInvalidFD.test_readv�	tcsetpgrpztest needs os.tcsetpgrp()cCr�rl)r�rrrUrrr�test_tcsetpgrpt�rTzTestInvalidFD.test_tcsetpgrptr$ztest needs os.write()cCr�)N� )r�rr$rUrrrr��rTzTestInvalidFD.test_write�writevztest needs os.writev()cCs|�tjdg�dS)Nr�)r�rrrUrrr�test_writev�rzTestInvalidFD.test_writevcC�|�tj�|�tjd�dS�NT)r�r�get_inheritable�set_inheritablerUrrr�test_inheritable��zTestInvalidFD.test_inheritable�get_blocking�-needs os.get_blocking() and os.set_blocking()cCrr)r�rr�set_blockingrUrrr�
test_blocking�szTestInvalidFD.test_blockingN)rNrOrPZsinglesr�r_�localsr�r�rrrrr�rkr�r�r�rrrr
r~rrr�rrrrrrrr�=sP	













�r�c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�	LinkTestscCs tj|_tj�tjd�|_dS)Nr�)rrS�file1rr<r>�file2rUrrrrV��zLinkTests.setUpcCs,|j|jfD]
}tj�|�rt�|�qdSr*)r!r"rr<r�rT)r/�filerrrr��s

��zLinkTests.tearDownc
Cs�t|�zt�||�Wnty%}z
|�d|�WYd}~nd}~wwt|d��4}t|d��}|�tj�|�	�|�	���Wd�n1sJwYWd�dSWd�dS1sbwYdS)N�
os.link(): %sr�)
r'rrrrfr#r[r<�sameopenfilerw)r/r!r"r�r=�f2rrr�
_test_link�s��P�zLinkTests._test_linkcCs|�|j|j�dSr*)r(r!r"rUrrr�	test_link�r�zLinkTests.test_linkcCs(|�t|jt���t|jt����dSr*)r(rKr!r7r
r"rUrrr�test_link_bytes�s�zLinkTests.test_link_bytescCsVzt�d�Wntyt�d��w|jd7_|jd|_|�|j|j�dS)N�ñz#Unable to encode for this platform.r�)rrO�UnicodeErrorrrjr!r"r(rUrrr�test_unicode_name�s
�zLinkTests.test_unicode_nameN)	rNrOrPrVr�r(r)r*r-rrrrr �s
r zPosix specific testsc@s�eZdZdZdZe�eed�d�dd��Z	e�eed�d�dd	��Z
e�eed
�d�dd
��Ze�eed�d�dd��Ze�eed�d�dd��Z
e�eed�d�dd��Ze�eed�d�dd��Ze�eed�d�dd��ZdS)�PosixUidGidTests��setuidztest needs os.setuid()cCsBt��dkr|�ttjd�|�ttjd�|�ttj|j�dS�Nrz
not an int)r�getuidrhrBr0rn�
OverflowError�UID_OVERFLOWrUrrr�test_setuid��zPosixUidGidTests.test_setuid�setgidztest needs os.setgid()cC�Ft��dkrts|�ttjd�|�ttjd�|�ttj|j�dSr1)	rr2�HAVE_WHEEL_GROUPrhrBr7rnr3�GID_OVERFLOWrUrrr�test_setgid��zPosixUidGidTests.test_setgid�seteuidztest needs os.seteuid()cCsBt��dkr|�ttjd�|�ttjd�|�ttj|j�dSr1)	rr2rhrBr=rn�setegidr3r4rUrrr�test_seteuid�r6zPosixUidGidTests.test_seteuidr>ztest needs os.setegid()cCr8r1)	rr2r9rhrBr>rnr3r:rUrrr�test_setegid�r<zPosixUidGidTests.test_setegid�setreuidztest needs os.setreuid()cCsnt��dkr|�ttjdd�|�ttjdd�|�ttjdd�|�ttj|jd�|�ttjd|j�dSr1)rr2rhrBrArnr3r4rUrrr�
test_setreuid�szPosixUidGidTests.test_setreuidcC�t�tjddg�dS)Nr�z,import os,sys;os.setreuid(-1,-1);sys.exit(0)�r�r6r7r�rUrrr�test_setreuid_neg1	�
�z#PosixUidGidTests.test_setreuid_neg1�setregidztest needs os.setregid()cCsrt��dkrts|�ttjdd�|�ttjdd�|�ttjdd�|�ttj|jd�|�ttjd|j�dSr1)	rr2r9rhrBrGrnr3r:rUrrr�
test_setregid	szPosixUidGidTests.test_setregidcCrC)Nr�z,import os,sys;os.setregid(-1,-1);sys.exit(0)rDrUrrr�test_setregid_neg1	rFz#PosixUidGidTests.test_setregid_neg1N)rNrOrPr4r:rrrrr5r;r?r@rBrErHrIrrrrr.�s&






r.c@sNeZdZdd�Zdd�Zdd�Zdd�Ze�e	e
d	�d
�dd��Zd
d�ZdS)�Pep383Testscs�tjrtj|_ntjrtj|_ntj|_t�|j�|_g��fdd�}|tj�tjr0|tj�tjr8|tj��s?|�	d�t
�|_t�|j�z'�D]!}t�
tj�|j|��t�|�}||jvrgtd��|j�|�qLWdSt�|j��)Ncs2zt�|�}Wn
tyYdSw��|�dSr*)rrOrr)�fn��bytesfnrr�add_filename)	s�z'Pep383Tests.setUp.<locals>.add_filenamez&couldn't create any non-ascii filenamezduplicate filename)r�TESTFN_UNENCODABLEr�ZTESTFN_NONASCIIrSrrOZbdirZTESTFN_UNICODErfr?�	unicodefnr?Zcreate_empty_filer<r>rLr��add�shutilr>)r/rNrKrrLrrV	s8







�zPep383Tests.setUpcCst�|j�dSr*)rRr>r�rUrrrr�D	rzPep383Tests.tearDownc	Cst|j}tt�|j��}|�||�t��}zt�tj�|�tt���tt�tj���Wt�|�dSt�|�wr*)	rPr?rrr�r;r+r@r=)r/rG�foundZcurrent_directoryrrr�test_listdirG	s"zPep383Tests.test_listdircCs.|jD]}ttj�|j|�d�}|��qdS�Nr�)rPr#rr<r>r�rZ)r/rKr_rrr�	test_openS	s

�zPep383Tests.test_openrr$cCs*|jD]}tj�|j|�}t�|�qdSr*)rPrr<r>r�r)r/rK�fullnamerrr�test_statvfsX	s
�zPep383Tests.test_statvfscCs&|jD]
}t�tj�|j|��qdSr*)rPrr�r<r>r�)r/rKrrr�	test_stata	s
�zPep383Tests.test_statN)
rNrOrPrVr�rTrVrrrrrXrYrrrrrJ	s%�
rJc@sFeZdZdd�Zdd�Zdd�Zdd�Ze�d	�d
d��Z	dd
�Z
dS)�Win32KillTestscCs�ddl}ddlm}ddl}|jjj}|j|_|j|�	|j
�|j|�	|j�|�	|j�|�	|j�f|_d}t
jtjdd�|�gt
jt
jt
jd�}|�|jj�|�|jj�|�|jj�d\}}	||	kr�|��dur�|�t|��}
||�|j���|
|�|
�ddd�}|�|dd�|
jr�|� ||
j�!��nt"�#d	�|d
7}||	kr�|��dusj|�$d�t%�&|j'|�|� |�(�|�dS)Nr��wintypes�runningr�z<import sys;sys.stdout.write('{}');sys.stdout.flush();input())r��stderr�stdin�rr�zPeekNamedPipe failed皙�����?raz1Did not receive communication from the subprocess))rvr\�msvcrtrwrx�
PeekNamedPipe�BOOL�restype�HANDLE�POINTERZc_char�DWORD�argtypesr��Popenr7r�r�r�r�r�rZr^r_�pollZcreate_string_bufferr9Z
get_osfhandlerwZsizeofr7r!r;r�rk�sleepr�r�kill�pid�wait)r/Zsigrvr\rbrcrjr�r��maxrzZrsltrrr�_killg	sP




�
���
�
zWin32KillTests._killcCr�r*)rq�signal�SIGTERMrUrrr�test_kill_sigterm�	rnz Win32KillTests.test_kill_sigtermcCs|�d�dS)Nr�)rqrUrrr�
test_kill_int�	szWin32KillTests.test_kill_intcCs�dt��}t�dd|�}d|d<tjtjtj�	tj�
t�d�|gtjd�}d\}}||krN|�
�durN|ddkr;n t�d�|d7}||krN|�
�dus4t�|jtj�|�d	�t�|j|�t�d
�|�
�s}t�|jtj�|�d�|��dSdS)Nz
test_os_%srrarzwin_console_handler.pyr4r`raz'Subprocess didn't finish initializationg�?zsubprocess did not stop on {})�uuidZuuid1�mmapr�rjr7r�rr<r>rFr�ZCREATE_NEW_PROCESS_GROUPrkrkrlrmrnrr�SIGINTr�r�)r/ZeventrZtagname�mr�r�rprrr�_kill_with_event�	s4���
�

�zWin32KillTests._kill_with_eventz.subprocesses aren't inheriting Ctrl+C propertycCsdddlm}ddl}|�|j��}|jjj}|�|j�|jf|_|j|_	||d�|�
tjd�dS)Nrr[�CTRL_C_EVENT)
rvr\rgZc_intrwrx�SetConsoleCtrlHandlerrdrirerzrrr{)r/r\rvZNULLr|rrr�test_CTRL_C_EVENT�	s

�
z Win32KillTests.test_CTRL_C_EVENTcCr�)N�CTRL_BREAK_EVENT)rzrrr~rUrrr�test_CTRL_BREAK_EVENT�	�z$Win32KillTests.test_CTRL_BREAK_EVENTN)rNrOrPrqrtrurzr�skipr}rrrrrrZe	s6
rZc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�Win32ListdirTestszTest listdir on Windows.c	Cs�g|_td�D]E}d|}tj�tj|�}d|}tj�tj|�}t�|�t|ddd��}|�	d|�Wd�n1s?wY|j�
||g�q|j��dS)NrczSUB%dzFILE%dr+r�r�z'I'm %s and proud of it. Blame test_os.
)�
created_pathsr�rr<r>rrSrr#r$�extendr)r/r�Zdir_nameZdir_path�	file_nameZ	file_pathr_rrrrV�	s
�zWin32ListdirTests.setUpcCrWr*)rRr>rrSrUrrrr��	rzWin32ListdirTests.tearDowncCsH|�tt�tj��|j�|�tt�t�tj���dd�|jD��dS)z-Test when the path is not an "extended" path.cS�g|]}t�|��qSr�rrO�rr<rrrr�	r(zCWin32ListdirTests.test_listdir_no_extended_path.<locals>.<listcomp>N)r;�sortedrrrrSr�rOrUrrr�test_listdir_no_extended_path�	s��z/Win32ListdirTests.test_listdir_no_extended_pathcCshdtj�tj�}|�tt�|��|j�dt�	tj�tj��}|�tt�|��dd�|jD��dS)z&Test when the path starts with '\\?\'.�\\?\�\\?\cSr�rr�r�rrrr
r(z@Win32ListdirTests.test_listdir_extended_path.<locals>.<listcomp>N)
rr<rurrSr;r�rr�rOrqrrr�test_listdir_extended_path�	s��z,Win32ListdirTests.test_listdir_extended_pathN)rNrOrPr�rVr�r�r�rrrrr��	s
r��readlinkzneeds os.readlink()c@szeZdZdZej�e�Ze�	e�Z
e�	e�Zdd�Zdd�Z
dd�Zdd	�Zejd
d��Zejdd
��Zejdd��ZdS)�
ReadlinkTestsZreadlinktestcCsbtj�|�}tj�|�}tjdkr)dd�}||�r|dd�}||�r)|dd�}|�||�dS)Nr�cSs|�t|t�r
d�Sd�S)Nr�r�)�
startswithrrK)rrrrr�
s
��z/ReadlinkTests.assertPathEqual.<locals>.<lambda>ru)rr<�normcaser7r8r;)r/�left�rightZ
has_prefixrrr�assertPathEqual
s
zReadlinkTests.assertPathEqualcCsT|�tj�|j��|�tj�|j��|�tj�|j��|�tj�|j��dSr*)	r[rr<r��filelink_target�filelinkb_targetr��filelink�	filelinkbrUrrrrV
szReadlinkTests.setUpcCs0t|j�}|�ttj|j�|�ttj|�dSr*)rr�rhrBrr�)r/r�rrr�test_not_symlink$
s
zReadlinkTests.test_not_symlinkcCs(|�ttjd�|�ttjtd��dS)Nzmissing-link)rhrArr�rrUrrr�test_missing_link)
s
�zReadlinkTests.test_missing_linkcCsBt�|j|j�|�tj|j�t|j�}|�t�	|�|j�dSr*)
rr�r�r�r�rrTrr�r�)r/r�rrr�
test_pathlike.
s
zReadlinkTests.test_pathlikecCsNt�|j|j�|�tj|j�t�t|j��}|�	||j�|�
|t�dSr*)rr�r�r�r�rrTr�rr�r,rKrqrrr�test_pathlike_bytes5
s
z!ReadlinkTests.test_pathlike_bytescCsJt�|j|j�|�tj|j�t�|j�}|�||j�|�	|t
�dSr*)rr�r�r�r�rrTr�r�r,rKrqrrr�
test_bytes=
s
zReadlinkTests.test_bytesN)rNrOrPr�rr<rur�r�rOr�r�r�rVr�r�r�skip_unless_symlinkr�r�r�rrrrr�	
s



r�c@s�eZdZdZej�e�ZdZ	ej�
e�ZdZdd�Z
dd�Zdd	�Zd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Ze�ej�d�oKej�d�d�dd��Zdd�Zdd �Zd!S)"�Win32SymlinkTestsZfilelinktestZdirlinktestzmissing linkcCs^tj�|j�s	J�tj�|j�sJ�tj�|j�rJ�tj�|j�r$J�tj�|j�r-J�dSr*)rr<r��dirlink_targetr��dirlinkr��missing_linkrUrrrrVO
s
zWin32SymlinkTests.setUpcCsVtj�|j�r
t�|j�tj�|j�rt�|j�tj�|j�r)t�|j�dSdSr*)	rr<r�r�rr�r2rRr�rUrrrr�V
s�zWin32SymlinkTests.tearDowncC�`t�|j|j�|�tj�|j��|�tj�|j��|�tj�|j��|�	|j|j�dSr*)
rr�r�r�r[r<r�r]�islink�
check_statrUrrr�test_directory_link^
�
z%Win32SymlinkTests.test_directory_linkcCr�r*)
rr�r�r�r[r<r��isfiler�r�rUrrr�test_file_linke
r�z Win32SymlinkTests.test_file_linkcCsF|j}tj�|�rt�|�d}tj�|�rJ�d}t�|||�dS)z2Create a "directory" link to a non-existent targetz"c:\\target does not exist.29r3c740TN)r�rr<rRrr�r�)r/Zlinknamer�Z
target_is_dirrrr�_create_missing_dir_linkl
s
z*Win32SymlinkTests._create_missing_dir_linkcC�|��t�|j�dSr*)r�rrr�rUrrr�,test_remove_directory_link_to_missing_targetv
sz>Win32SymlinkTests.test_remove_directory_link_to_missing_targetcCs |��|�tj�|j��dSr*)r�r�rr<r]r�rUrrr�.test_isdir_on_directory_link_to_missing_target}
r#z@Win32SymlinkTests.test_isdir_on_directory_link_to_missing_targetcCr�r*)r�rr2r�rUrrr�.test_rmdir_on_directory_link_to_missing_target�
r2z@Win32SymlinkTests.test_rmdir_on_directory_link_to_missing_targetcCsn|�t�|�t�|��|�t�|�t�|��t�|�}|�t�|�t�|��|�t�|�t�|��dSr*)r;rr�r7rirO)r/rr�Z
bytes_linkrrrr��
s

zWin32SymlinkTests.check_statc	CsHtj�tj�}tj�|d�}tj�|d�}|�tj|�t�|�t�|�t�|�tj�tj�|d��}t	|�t�
�}z_t�|�tj�|d�}t�tj�
|�d�|�dt�t�
���|�t�|�t�d��t�|�|�t�|�t�tj�
|���t�|�|�t�|�t�tj�
|���Wt�|�dSt�|�w)N�level2�level3r!r)rr<rurrSr>r�r>r?r'r+r@r��relpathr�rr;r�)r/Zlevel1r�r�r!Zorig_dirrrrr�
test_12084�
s2




�
�zWin32SymlinkTests.test_12084�C:\Users\All Users�C:\ProgramDatazTest directories not foundcCs"t�d�}|�tj�|d��dS)Nr�r�)rr�r[r<�samefile)r/r�rrr�
test_29248�
s
zWin32SymlinkTests.test_29248c	Cs�d}tjj|gd�}d||f||f|dd�|dd�fg}|D]O\}}zt�||�Wn	ty7Ynwzt�|�Wn	tyHYnwzt�t�|�t�|��Wn	ty`Yq"wzt�|�Wq"tyqYq"wdS)NZXXXXXXXXXXXXXXXXXXXXXXXXXXXrbrs�)rr<r>r�rArrBrO)r/Zsegmentr<Z
test_casesr��destrrr�test_buffer_overflow�
s8
������z&Win32SymlinkTests.test_buffer_overflowcs�tj�d��tj���s|�d��fdd�t�t���d�D�}|D].}tj	r0t
�t
d|�t�|�}|�|t�
|��|�t
�|j��|�|jt
j�dS|�d�dS)Nz$%LOCALAPPDATA%\Microsoft\WindowsAppsz%test requires a WindowsApps directorycsg|]	}tj��|��qSr)rr<r>�rr6�ryrrr�
s�z6Win32SymlinkTests.test_appexeclink.<locals>.<listcomp>z*.exezTesting withz$test requires an app execution alias)rr<�
expandvarsr]rf�fnmatch�filterrrrDrErir;r�r��S_ISLNKrZst_reparse_tagZIO_REPARSE_TAG_APPEXECLINK)r/�aliases�aliasrBrr�r�test_appexeclink�
s 

�

z"Win32SymlinkTests.test_appexeclinkN)rNrOrPr�rr<rur�r�r�rFr�r�rVr�r�r�r�r�r�r�r�r�rrrRr�r�r�r�rrrrr�F
s.
#
�
#r�c@sDeZdZdZej�ej�e��Z	dd�Z
dd�Zdd�Zdd	�Z
d
S)�Win32JunctionTestsZjunctiontestcCs(tj�|j�s	J�tj�|j�rJ�dSr*)rr<r��junction_targetrR�junctionrUrrrrV�
�zWin32JunctionTests.setUpcCs"tj�|j�rt�|j�dSdSr*)rr<rRr�rTrUrrrr�rWzWin32JunctionTests.tearDowncCs�t�|j|j�|�tj�|j��|�tj�|j��|�tj�	|j��|�
t�|j�t�|j��|�
t�|j�t�|j��|�tj�|j��|�
tj�d|j�tj�t�|j���dS)Nr�)�_winapi�CreateJunctionr�r�r[rr<rRr�r]r7r�rir;r�r�r�r�rUrrr�test_create_junctions�z'Win32JunctionTests.test_create_junctioncCs\t�|j|j�|�tj�|j��|�tj�|j��t�	|j�|�
tj�|j��dSr*)r�r�r�r�r[rr<r�rRrTr�rUrrr�test_unlink_removes_junctions
z/Win32JunctionTests.test_unlink_removes_junctionN)rNrOrPr�rr<rFrur�r�rVr�r�r�rrrrr��
s
r�c@�eZdZdd�Zdd�ZdS)�Win32NtTestsc
Cs*t�d�}t�d�}ddl}|jddd�}|jj|j_|jj|j	_|jj|jj
f|j	_|��}|j��}|�	||�
|��}|�d|�|j}dd	d
tg}td�D](}	|D]#}
z|�|
�Wn	tyfYnwzt�|
�WqTtywYqTwqP|�	||�
|��}|�d|�|j|}|�d|�dS)Nr\rvrzKernel32.dllT)Zuse_last_errorraz\\?\C:z\\?\NULz	\\?\CONINrb)r�
import_moduleZctypes.wintypesZWinDLLr\rfZGetCurrentProcessrerdZGetProcessHandleCountZLPDWORDrirhZbyrefr;r!r�r�Z_getfinalpathname�	Exceptionrr�)r/r\rvZkernelZhprocZhandle_countr{Zbefore_count�	filenames�_rZhandle_deltarrr�test_getfinalpathname_handlessH

�
����
z*Win32NtTests.test_getfinalpathname_handlescCstj}|�tj|�t��d}t�d�}t�t	j
d||t|�g��T}t��|krPzt�
|�WntyI}z
|jdks?J�WYd}~nd}~wwt��|ks)z|�d�Wntjye|��Yn	wWd�dSWd�dS1sywYdS)Nr�a�            import os
            import sys
            import time

            filename = sys.argv[1]
            deadline = float(sys.argv[2])

            while time.time() < deadline:
                try:
                    with open(filename, "w") as f:
                        pass
                except OSError:
                    pass
                try:
                    os.remove(filename)
                except OSError:
                    pass
            r�rcra)rrSr�rTrk�textwrap�dedentr�rjr7r�r-rr�rAr�roZTimeoutExpiredZ	terminate)r/r%ZdeadlineZcommandr�r�rrr�test_stat_unlink_raceKs,
������	"�z"Win32NtTests.test_stat_unlink_raceN)rNrOrPr�r�rrrrr�s0r�c@r()�NonLocalSymlinkTestscCst�d�dS)zM
        Create this structure:

        base
         \___ some_dir
        z
base/some_dirN)rrrUrrrrVxszNonLocalSymlinkTests.setUpcCst�d�dS)Nr4)rRr>rUrrrr��szNonLocalSymlinkTests.tearDowncCs.tj�dd�}t�d|�tj�|�sJ�dS)a�
        The symlink target should resolve relative to the link, not relative
        to the current directory.

        Then, link base/some_link -> base/some_dir and ensure that some_link
        is resolved as a directory.

        In issue13772, it was discovered that directory detection failed if
        the symlink target was not specified relative to the current
        directory, which was a defect in the implementation.
        r4Z	some_linkZsome_dirN)rr<r>r�r])r/r�rrr�test_directory_link_nonlocal�sz1NonLocalSymlinkTests.test_directory_link_nonlocalN)rNrOrPrVr�r�rrrrr�us	r�c@r�)�FSEncodingTestscCs(|�t�d�d�|�t�d�d�dS)Nsabc�uabcŁ)r;rrOrLrUrrr�test_nop�r�zFSEncodingTests.test_nopc	CsBdD]}zt�|�}Wn	tyYqw|�t�|�|�qdS)N)u	unicodeŁulatiné�ascii)rrOrr;rL)r/rKrMrrr�
test_identity�s��zFSEncodingTests.test_identityN)rNrOrPr�r�rrrrr��sr�c@sVeZdZdd�Ze�e�d�o"e�o"e	j
�d�p"ee
d�o"ee
d�d�dd	��Zd
S)�DeviceEncodingTestscCs|�t�d��dS)Ni@�)r|r�device_encodingrUrrr�test_bad_fd�rzDeviceEncodingTests.test_bad_fdr�win�nl_langinfo�CODESETz>test requires a tty and either Windows or nl_langinfo(CODESET)cCs(t�d�}|�|�|�t�|��dSrl)rr�ZassertIsNotNoner[�codecs�lookup)r/r�rrr�test_device_encoding�s

z(DeviceEncodingTests.test_device_encodingN)rNrOrPr�rrrr�rr7r8r�r�localer�rrrrr��s"�r�c@s�eZdZe�eed�d�dd��Zddd�Zdd	�Z	d
d�Z
e�ejdkd
�dd��Z
e�ejdkd
�dd��Ze�eed�d�dd��ZdS)�PidTests�getppidztest needs os.getppidcCs<tjtjddgtjd�}|��\}}|�t|�t�	��dS)Nr�zimport os; print(os.getppid()))r�)
r�rjr7r�r�Zcommunicater;r�r�getpid)r/rr�r�rrr�test_getppid�s
��zPidTests.test_getppidNcCs�tjdkrdtj�d�dd|�d�g}ntjd|g}t�tjtj|�}|dur+||�t�|d�\}}|�t�|�|�|�||�dS)Nr��"r�r)	r7r8r�r�spawnv�P_NOWAIT�waitpidr;�waitstatus_to_exitcode)r/r��exitcode�callbackr�rnZpid2Zstatusrrr�
check_waitpid�s
zPidTests.check_waitpidcCs|jddd�dS)Nr�r)r�r��r�rUrrr�test_waitpid�r�zPidTests.test_waitpidcCsXd}d|�d�}|j||d�|�t��t�d�Wd�dS1s%wYdS)N�zimport sys; sys.exit(�)�r�g)r�rhrnrr�)r/r�r�rrr�test_waitstatus_to_exitcode�s"�z$PidTests.test_waitstatus_to_exitcoder��win32-specific testcCs"d}d|�d�}|j||d�dS)Nl:z$import _winapi; _winapi.ExitProcess(r�r�r�)r/ZSTATUS_CONTROL_C_EXITr�rrr�test_waitpid_windows�szPidTests.test_waitpid_windowscCs�d}ddd|fD]
}|�t�|d>�|�q|�t��t�|dd>�Wd�n1s/wY|�t��t�d�Wd�dS1sJwYdS)Nr/rrar�r�r)r;rr�rhr�r3)r/Zmax_exitcoder�rrr�#test_waitstatus_to_exitcode_windows�s��"�z,PidTests.test_waitstatus_to_exitcode_windows�SIGKILLzneed signal.SIGKILLcs6dtj�d�}tj��fdd�}|j|�|d�dS)Nzimport time; time.sleep(r�cst�|��dSr*)rrm)rn�Zsignumrr�kill_process�rz?PidTests.test_waitstatus_to_exitcode_kill.<locals>.kill_process)r�r�)rZLONG_TIMEOUTrrr�r�)r/r�r�rr�r� test_waitstatus_to_exitcode_kill�sz)PidTests.test_waitstatus_to_exitcode_killr*)rNrOrPrrrrr�r�r�r�r7r8r�r�rrr�rrrrr��s




r�c@s$eZdZddd�dd�Zed�dd��Zed�d	d
��Zed�dd
��Zed�dd��Zed�dd��Z	ed�dd��Z
ed�dd��Zed�dd��Zed�dd��Z
ed�dd ��Zed�d!d"��Zed�d#d$��Zed�d%d&��Zed�d'd(��Zd)d*�Zed�d+d,��Zed�d-d.��Zd/S)0�
SpawnTestsF��with_env�	use_bytescCs�d|_tj}|�tj|�|sd|j}nttj�|_t	t
���|_|j|j|j<d|j|jf}t
|ddd��
}|�|�Wd�n1sHwYtj|g}|rfdd�|D�}d	d
�|j��D�|_|S)N�zimport sys; sys.exit(%s)z4import sys, os; magic = os.environ[%r]; sys.exit(%s)r+r�r�cSr�rr�r�rrrrr(z*SpawnTests.create_args.<locals>.<listcomp>cSs"i|]
\}}t�|�t�|��qSrr�)r�k�vrrr�
<dictcomp>s�z*SpawnTests.create_args.<locals>.<dictcomp>)r�rrSr�rTr�rr5r�r-rvZuuid4r�r#r$r7r�r�)r/rrr%r�r&r�rrr�create_args�s*
��
�zSpawnTests.create_args�spawnlcC�4|��}tjtj|dg|�R�}|�||j�dSrl)rrr�P_WAITr;r��r/r�r�rrr�test_spawnl�zSpawnTests.test_spawnl�spawnlecC�>|jdd�}tjtj|dg|�|j�R�}|�||j�dS�NT)rr)rrr
r	r�r;r�r
rrr�test_spawnle� zSpawnTests.test_spawnle�spawnlpcCrrl)rrrr	r;r�r
rrr�test_spawnlp%rzSpawnTests.test_spawnlp�spawnlpecCrr)rrrr	r�r;r�r
rrr�
test_spawnlpe+rzSpawnTests.test_spawnlper�cCsT|��}t�tj|d|�}|�||j�t�tjt|d�|�}|�||j�dSrl)rrr�r	r;r�rr
rrr�test_spawnv1s
zSpawnTests.test_spawnv�spawnvecC�6|jdd�}t�tj|d||j�}|�||j�dSr�rrrr	r�r;r�r
rrr�test_spawnve;�zSpawnTests.test_spawnve�spawnvpcCs.|��}t�tj|d|�}|�||j�dSrl)rrrr	r;r�r
rrr�test_spawnvpAszSpawnTests.test_spawnvp�spawnvpecCrr)rrrr	r�r;r�r
rrr�
test_spawnvpeGrzSpawnTests.test_spawnvpecCs0|��}t�tj|d|�}tj||jd�dS)Nrr�)rrr�r�rZwait_processr�)r/r�rnrrr�test_nowaitMszSpawnTests.test_nowaitcCs8|jddd�}t�tj|d||j�}|�||j�dS)NTr�rrr
rrr�test_spawnve_bytesSszSpawnTests.test_spawnve_bytescCs>|��}|�ttjtj|d�|�ttjtj|dd�dS�Nrrt)rrhr�rrr��r/r�rrr�test_spawnl_noargsZszSpawnTests.test_spawnl_noargscCsB|��}|�ttjtj|di�|�ttjtj|ddi�dSr")rrhr�rr
r�r#rrr�test_spawnle_noargs`s zSpawnTests.test_spawnle_noargscCsv|��}|�ttjtj|dd�|�ttjtj|dg�|�ttjtj|dd�|�ttjtj|ddg�dS�Nrrr�rt)rrhr�rr�r�r#rrr�test_spawnv_noargsfs
 zSpawnTests.test_spawnv_noargscCs~|��}|�ttjtj|ddi�|�ttjtj|dgi�|�ttjtj|ddi�|�ttjtj|ddgi�dSr&)rrhr�rrr�r#rrr�test_spawnve_noargsns
"zSpawnTests.test_spawnve_noargscCsvtjddg}tj��}d|d<z|tj|d||�}Wn	ty$Ynw|�|d�tj��}d|d<z|tj|d||�}Wn	tyIYnw|�|d�tj��}d	|d
<z|tj|d||�}Wn	tynYnw|�|d�tj	}|�
tj|�t|ddd
��
}|�
d�Wd�n1s�wYtj|g}tj��}d|d<|tj|d||�}|�|d�dS)Nr�r�r�r�r�r�r�r�r�r+r�r�zPimport sys, os
if os.getenv("FRUIT") != "orange=lemon":
    raise AssertionErrorzorange=lemon)r7r�rr5r�r	r�r;rrSr�rTr#r$)r/Zspawnr�r�r�r%r&rrr�_test_invalid_envvsF
�
�
��

zSpawnTests._test_invalid_envcCr�r*)r*rrrUrrr�test_spawnve_invalid_env�rnz#SpawnTests.test_spawnve_invalid_envcCr�r*)r*rrrUrrr�test_spawnvpe_invalid_env�rnz$SpawnTests.test_spawnvpe_invalid_envN)rNrOrPrrrrrrrrrrr r!r$r%r'r(r*r+r,rrrrr��sF




	








.
r�z>Skip due to platform/environment differences on *NIX buildbots�getloginztest needs os.getloginc@r�)�
LoginTestscCst��}|�t|�d�dSrl)rr-r7r9)r/Z	user_namerrr�
test_getlogin��zLoginTests.test_getloginN)rNrOrPr/rrrrr.��r.�getpriority�setpriorityz'needs os.getpriority and os.setpriorityc@rL)�ProgramPriorityTestsz0Tests for os.getpriority() and os.setpriority().cCs
t�tjt���}t�tjt��|d�zIt�tjt���}|dkr.|dkr.t�d|��|�||d�Wz
t�tjt��|�WdSty^}z|j	t	j
krS�WYd}~dSd}~wwzt�tjt��|�Wwty�}z
|j	t	j
krz�WYd}~wd}~ww)Nra�z?unable to reliably test setpriority at current nice level of %s)rr2�PRIO_PROCESSr�r3rrjr;rBrCZEACCES)r/r4Znew_prior�rrr�test_set_get_priority�s2��������z*ProgramPriorityTests.test_set_get_priorityN)rNrOrPr�r7rrrrr4�sr4c@sveZdZGdd�dej�Zdd�Zedd��Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�ZeZdd�Zdd�ZdS)�SendfileTestServerc@r�)zSendfileTestServer.HandlercCs.tj�||�g|_d|_d|_|�d�dS)NTFs220 ready
)�asynchat�
async_chat�__init__�	in_buffer�
accumulate�closed�push)r/�connrrrr;�s
z#SendfileTestServer.Handler.__init__cCs$|�d�}|jr|j�|�dSdS)N�)�recvr=r<rr�rrr�handle_read�s
�z&SendfileTestServer.Handler.handle_readcCsd�|j�S)Nr�)r>r<rUrrr�get_data�r�z#SendfileTestServer.Handler.get_datacCs|��d|_dSr)rZr>rUrrr�handle_close�s
z'SendfileTestServer.Handler.handle_closecC��r*rrUrrr�handle_error��z'SendfileTestServer.Handler.handle_errorN)rNrOrPr;rCrDrErGrrrr�Handler�srIcCsptj�|�tj�|�|�tjtj�|�	|�|�
d�|j��dd�\|_|_
d|_d|_t��|_dS)Nr�rcF)�	threading�Threadr;�asyncore�
dispatcherZ
create_socket�socketZAF_INETZSOCK_STREAMZbindZlistenZgetsockname�host�port�handler_instance�_activeZLock�_active_lock)r/Zaddressrrrr;�s

zSendfileTestServer.__init__cCs|jSr*)rRrUrrrr]�szSendfileTestServer.runningcCs.|jrJ�t��|_tj�|�|j��dSr*)r]rJZEvent�_SendfileTestServer__flagrK�startrorUrrrrU�s

zSendfileTestServer.startcCs|jsJ�d|_|��dSr�)r]rRr>rUrrr�stop
s
zSendfileTestServer.stopcCs2t|jdd�st�d�t|jdd�r|��dS)Nr>F���MbP?)r�rQrkrlrVrUrrrro
s
�zSendfileTestServer.waitcCsVd|_|j��|jr%tjr%|j��tjddd�|j��|jr%tjst�	�dS)NTrWra)Ztimeoutr�)
rRrTr?rLZ
socket_maprS�acquireZloop�releaseZ	close_allrUrrrr�

s


�zSendfileTestServer.runcCs|��\}}|�|�|_dSr*)ZacceptrIrQ)r/r@Zaddrrrr�
handle_accept
sz SendfileTestServer.handle_acceptcCs|��dSr*)rZrUrrr�handle_connect
r�z!SendfileTestServer.handle_connectcCsdSrlrrUrrr�writable
r�zSendfileTestServer.writablecCrFr*rrUrrrrG!
rHzSendfileTestServer.handle_errorN)rNrOrPr9r:rIr;�propertyr]rUrVror�rZr[rCr\rGrrrrr8�s
	r8�sendfileztest needs os.sendfile()c@s
eZdZddZej�d�oej�d�oej�d�Ze�	ed�Z
e�	ejdkd�Ze
d	d
��Ze
dd��Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Ze
dd��Ze
dd ��Ze
ed!d"���Ze
ed#d$���Ze
e�	eed%�d&�d'd(���Zd)S)*�TestSendfiles�12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde12345abcde�ZlinuxZsolarisZsunosz%requires headers and trailers supportr/z(test is only meaningful on 32-bit buildscCst��|_ttj|j�dSr*)rZthreading_setupr�r'rrS�DATArtrrrrv1
s
zTestSendfile.setUpClasscCstj|j�t�tj�dSr*)rZthreading_cleanupr�rrTrSrtrrrr�6
szTestSendfile.tearDownClasscCs~ttjdf�|_|j��t��|_|j�|jj|jj	f�|j�
d�|j�d�|j��|_
ttjd�|_|j��|_dS)Nrrar`r�)r8rZHOST�serverrUrN�clientZconnectrOrPZ
settimeoutrBrw�socknor#rrSr$rUrrrrV;
s

zTestSendfile.setUpcCs0|j��|j��|jjr|j��d|_dSr*)r$rZrcrbr]rVrUrrrr�G
s




zTestSendfile.tearDownc
Os\	z	tj|i|��WSty-}z|jtjkr�|jtjtjfvr(WYd}~q�d}~ww)zfA higher level wrapper representing how an application is
        supposed to use sendfile().
        TN)rr^rBrCZ
ECONNRESET�EAGAIN�EBUSY)r/r�r�r�rrr�sendfile_wrapperN
s��zTestSendfile.sendfile_wrappercCs�d}d}d}|t|j�kr8|�|j|j||�}|dkrn||7}||7}|�||k�|�||�|t|j�ks
|�|t|j��|j�t	j
�|j��|j�
�|jj��}|�t|�t|j��|�||j�dS)NrrA)r9rargrdrwr[r;rc�shutdownrN�	SHUT_RDWRrZrbrorQrD)r/�
total_sent�offset�nbytes�sentr�rrr�test_send_whole_file_
s&�	

z!TestSendfile.test_send_whole_filecCs�d}t|j�d}t|j�|}d}||kr8|�|j|j||�}|dkr%n||7}||7}|�||k�||ks|j�tj	�|j�
�|j��|jj
��}|jt|j�dd�}|�|t|��|�t|�t|��|�||�dS)NrrcrA)r9rargrdrwr[rcrhrNrirZrbrorQrDr;)r/rjrkZ	must_sendrlrmr�rGrrr�test_send_at_certain_offsetu
s(�

z(TestSendfile.test_send_at_certain_offsetc
Cs�t|j�d}zt�|j|j|d�}Wnty,}z
|jtjkr"�WYd}~nd}~ww|�	|d�|j
�tj
�|j
��|j��|jj��}|�	|d�dS)NrArr�)r9rarr^rdrwrBrC�EINVALr;rcrhrNrirZrbrorQrD)r/rkrmr�r�rrr�test_offset_overflow�
s���

z!TestSendfile.test_offset_overflowcCsT|�t��}t�|j|jdd�Wd�n1swY|�|jjtj	�dS)NrrA)
rhrBrr^rdrwr;r)rCrp�r/r�rrr�test_invalid_offset�
s�z TestSendfile.test_invalid_offsetc	Cs@tj|j|jddd�|jrtj|j|jdddddd�dSdS)NrrA)r�r�rkr�r)r�r�rkr��headers�trailersr�)rr^rdrw�SUPPORT_HEADERS_TRAILERSrUrrr�
test_keywords�
s�
��zTestSendfile.test_keywordscCs�d}d|jdd�}tj|j|jddddgd�}|�|d�||7}d}|t|�krWtt|�|d�}|�|j|j||�}|dkrCn|�||�||7}||7}|t|�ks+|�	|t|��|j
��|j�
�|jj��}|�	t|�t|��dS)	NrsxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyrrAsxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy�rti)rarr^rdrwZassertLessEqualr9�minrgr;rcrZrbrorQrD�hash)r/rjZ
expected_datarmrkrlr�rrr�test_headers�
s0���


zTestSendfile.test_headerscCs�tjd}d}|�tj|�t||�t|d��.}tj|j|�	�ddddgd�|j
��|j�
�|jj��}|�|d	�Wd�dS1sIwYdS)
Nr�sabcdefr�rr�s123456s789�rusabcde123456789)rrSr�rTr'r#rr^rdrwrcrZrbrorQrDr;)r/r�Z	file_datar_r�rrr�
test_trailers�
s

�

"�zTestSendfile.test_trailerscC�ld|jj_|�t��}tj|j|jddddgdd�Wd�n1s&wY|�	|j
jtj�dS)NFr�x�r�rx�
rbrQr=rhrBrr^rdrwr;r)rCrprrrrr�test_headers_overflow_32bits�
�
��z)TestSendfile.test_headers_overflow_32bitscCr~)NFrrr�r�r|r�rrrrr�test_trailers_overflow_32bits�
r�z*TestSendfile.test_trailers_overflow_32bits�SF_NODISKIOztest needs os.SF_NODISKIOc
Cs\ztj|j|jddtjd�WdSty-}z|jtjtjfvr"�WYd}~dSd}~ww)NrrA)r�)	rr^rdrwr�rBrCrfre)r/r�rrr�
test_flags�
s����zTestSendfile.test_flagsN)rNrOrPrar7r8r�rvrrZrequires_headers_trailers�maxsizeZrequires_32br�rvr�rVr�rgrnrorqrsrwr{r}r�r�rrr�rrrrr_%
sP����



�r_c	Cs�ttd�sdSzLttjdd��+}zt�|��dd�Wnty2YWd�Wt�tj�dSwWd�n1s=wYWt�tj�dSWt�tj�dSt�tj�w)N�setxattrFr"r�	user.testr�T)	rrr#rrSr�rwrBrT)r&rrr�supports_extended_attributes�
s&
������r�z(no non-broken extended attribute supportrcr��'c@r�)�ExtendedAttributeTestsc
KsFtj}|�tj|�t|�|�t��}|||d�fi|��Wd�n1s)wY|�|jj	t	j
�||�}	|�|	t�|||d�dfi|��t
|	�}
|
�d�|�t
||��|
�|�||dfi|��d�|||d�dtjfi|��|�||dfi|��d�|�t��}|||d�dtjfi|��Wd�n1s�wY|�|jj	t	j�|�t��}|||d�dtjfi|��Wd�n1s�wY|�|jj	t	j
�|||d�dtjfi|��|
�d�|�t
||��|
�|||d�fi|��|�t��}|||d�fi|��Wd�n	1�s$wY|�|jj	t	j
�|
�d�|�t
||��|
�|�|||d�fi|��d�|||d�dfi|��|�|||d�fi|��d�|||d�fi|��td	d
�td�D��}|D]
}|||dfi|���q�|�t
||��t
|	�t
|�B�dS)
N�	user.testr�r�r�sbyez
user.test2sfoosaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaacss�|]}d�|�VqdS)zuser.test{}Nr�)rr�rrrr�5��z;ExtendedAttributeTests._check_xattrs_str.<locals>.<genexpr>r�r)rrSr�rTr'rhrBr;r)rCZENODATAr,rr?rQr�
XATTR_REPLACE�XATTR_CREATEZEEXISTrr�r�)
r/r}�getxattrr��removexattr�	listxattrr�rKr�Z
init_xattrZxattr�many�thingrrr�_check_xattrs_str
sT�
��
�
$z(ExtendedAttributeTests._check_xattrs_strcOsN|jtg|�Ri|��t�tj�|jtjg|�Ri|��t�tj�dSr*)r�r-rrTrSrrOr9rrr�
_check_xattrs:sz$ExtendedAttributeTests._check_xattrscCs|�tjtjtjtj�dSr*�r�rr�r�r�r�rUrrr�test_simpleAs�z"ExtendedAttributeTests.test_simplecCs |jtjtjtjtjdd�dS)NFr~r�rUrrr�
test_lpathEs
�z!ExtendedAttributeTests.test_lpathcCs4dd�}dd�}dd�}dd�}|�||||�dS)	NcW�Dt|d��}tj|��g|�R�Wd�S1swYdSrU)r#rr�rw�r<r�r&rrrr�J�$�z1ExtendedAttributeTests.test_fds.<locals>.getxattrcW�Ht|dd��}tj|��g|�R�Wd�dS1swYdS�Nrbr)r#rr�rwr�rrrr�M�"�z1ExtendedAttributeTests.test_fds.<locals>.setxattrcWr�r�)r#rr�rwr�rrrr�Pr�z4ExtendedAttributeTests.test_fds.<locals>.removexattrcWr�rU)r#rr�rwr�rrrr�Sr�z2ExtendedAttributeTests.test_fds.<locals>.listxattr)r�)r/r�r�r�r�rrr�test_fdsIs
zExtendedAttributeTests.test_fdsN)rNrOrPr�r�r�r�r�rrrrr�s0r��get_terminal_sizezrequires os.get_terminal_sizec@r�)�
TermsizeTestsc
Csnzt��}Wn ty&}ztjdks|jtjtjfvr!|�d��d}~ww|�	|j
d�|�	|jd�dS)z�Check if get_terminal_size() returns a meaningful value.

        There's no easy portable way to actually check the size of the
        terminal, so let's check if it returns something sensible instead.
        r��failed to query terminal sizeNr)rr�rBr7r8rCrp�ENOTTYrfZassertGreaterEqual�columns�lines)r/rr�rrr�test_does_not_crash[s
��z!TermsizeTests.test_does_not_crashc
Cs�ztjddgtjdd���}Wnttjtfy!|�d�Ynwt|d�t|d�f}z
t	�
tj�
��}Wn tyX}ztjdksN|jtjtjfvrS|�d	��d
}~ww|�||�d
S)z�Check if stty returns the same results

        stty actually tests stdin, so get_terminal_size is invoked on
        stdin explicitly. If stty succeeded, then get_terminal_size()
        should work too.
        ZsttyrT)r^r�zstty invocation failedrarr�r�N)r�rmZDEVNULLr�rAZCalledProcessErrorrrfr�rr�r7�	__stdin__rwrBr8rCrpr�r;)r/rrGr�r�rrr�test_stty_matchms*��
��
��zTermsizeTests.test_stty_matchN)rNrOrPr�r�rrrrr�Ysr��memfd_createzrequires os.memfd_creater�rc@r�)�MemfdCreateTestscCs�t�dtj�}|�|d�|�tj|�|�t�|��t|ddd��}|�	d�|�
|��d�Wd�n1s;wYt�d�}|�tj|�|�t�|��dS)NZHirrbF)�closefdsmemfd_creater�)rr��MFD_CLOEXECr7r�rZr�rr#r$r;�tell)r/r|r_r�rrr�test_memfd_create�s
�
z"MemfdCreateTests.test_memfd_createN)rNrOrPr�rrrrr��r1r��eventfdzrequires os.eventfdr.c@r()�EventfdTestscCs�dd�}d}d}t�|�}|�|d�|�tj|�|�t�|��t�||�}|�|||��t�	||d��t�||�}|�||d��t�	||d��t�	||d��t�||�}|�||d��t�
|d	�t�
|d
�t�|�}|�|d�dS)NcSst�d|�S)z$Pack as native uint64_t
            z@Q)�struct�pack)r!rrrr��sz/EventfdTests.test_eventfd_initval.<locals>.packr�r�rr��(rcr�r�)rr�r7r�rZr�rryr;r$�
eventfd_write�eventfd_read)r/r�r�initvalr|�resrrr�test_eventfd_initval�s(

z!EventfdTests.test_eventfd_initvalcCs.d}tjtjBtjB}t�||�}|�|d�|�tj|�t�|�}|�	|d�t�|�}|�	|d�|�
t��
t�|�Wd�n1sIwY|�
t��t�|d�Wd�n1sdwYt�
|d�t�|�}|�	|d�|�
t��t�|�Wd�dS1s�wYdS)Nrcrrar�)r�EFD_CLOEXEC�
EFD_SEMAPHORE�EFD_NONBLOCKr�r7r�rZr�r;rhr�ryr�)r/r�r�r|r�rrr�test_eventfd_semaphore�s*

��
"�z#EventfdTests.test_eventfd_semaphorecCstjtjB}t�d|�}|�|d�|�tj|�t�|g|g|gd�\}}}|�|||fg|ggf�t�	|d�t�|g|g|gd�\}}}|�|||f|g|ggf�|�t�
|�d�t�	|d�t�|g|g|gd�\}}}|�|||f|gggf�t�
|�dS)Nrrr�l����)rr�r�r�r7r�rZ�selectr;r�r�)r/r�r|�rfd�wfdZxfdrrr�test_eventfd_select�sz EventfdTests.test_eventfd_selectN)rNrOrPr�r�r�rrrrr��sr�c@r�)�OSErrorTestscCs�Gdd�dt�}g|_g|_tjdurtj}ntj}|j�|�|j�||��tjdur1tj}nt�	tj�}|j�|�|j�t
|��|j�t|��|j|j|_dS)Nc@�eZdZdS)zOSErrorTests.setUp.<locals>.StrN�rNrOrPrrrr�Str��r�)
r-�bytes_filenames�unicode_filenamesrrOrSrZTESTFN_UNDECODABLErrOr�r�r�)r/r�ZdecodedZencodedrrrrV�s

zOSErrorTests.setUpcCs@|jtjf|jtjdf|jtjf|jtjtjf|jtjf|jtjf|jtj	fg}t
jdkrP|�|j
tjdf|j
tjdf|jtjdf|jtjdf|jtjff�n|�|jtjf|jtjdf|jtjdff�ttd�rv|�|jtjddf�ttd�r�|�|jtjddf�ttd�r�|�|jtjdf�ttd	�r�|�|jtjdf�ttd
�r�|�|jtjdf�ttd�r�|�|jtjf�ttd�r�t
jdkr�|�|j
tjdf�|�|jtjdf�n
|�|jtjdf�ttd
��r
|�|jtjf|jtjdf|jtjddf|jtjdff�ttd��r|�|jtjdf�ttd��r,|�|jtj f�|D]o^}}}|D]f}z2t!|t"t#f��rI||g|�R�n|�$t%d��||g|�R�Wd�n	1�scwYWn(t&�y�}z|�'|j(|t"|��WYd}~�q5d}~wt)�y�Y�q5w|�*d�+|���q5�q.dS)Nr�r�sdstr�rrr�lchownr�chflags�lchflags�chrootrr�r�suser�lchmodr�z	should bezNo exception thrown by {}),r�rr@rrir#r�r2r�rTr7r8r�r�ror�r�rrrrrr�rr�r�r�rr�r�r�r�r�r�rr-rKZassertWarnsRegex�DeprecationWarningrBr�r%�UnicodeDecodeErrorr�r�)r/Zfuncsr��funcZ	func_argsrr�rrr�test_oserror_filenames�




�
	
�
�








���"����z"OSErrorTests.test_oserror_filenameN)rNrOrPrVr�rrrrr��sr�c@r�)�
CPUCountTestscCs:t��}|dur|�|t�|�|d�dS|�d�dS)Nrz&Could not determine the number of CPUs)r�	cpu_countr,r��
assertGreaterrf)r/Zcpusrrr�test_cpu_countLs
zCPUCountTests.test_cpu_countN)rNrOrPr�rrrrr�Kr�r�c@s�eZdZdd�Ze�edud�dd��Ze�edud�dd��Ze�	e
ed	�d
�dd��Zd
d�Z
dd�Ze�	e
ed�d�dd��Zdd�Zdd�Ze�	ejdkd�dd��Ze�	e
ed�d�dd ��Ze�	e
ed!�d"�d#d$��ZdS)%�FDInheritanceTestscCsPt�ttj�}|�tj|�|�t�|�d�t�|d�|�t�|�d�dS�NFT)	rr#r�r�r�rZr;rrr�rrr�test_get_set_inheritableVs
z+FDInheritanceTests.test_get_set_inheritableNz
need fcntlcCsnt�ttj�}|�tj|�|�t�|�d�t�|tj	�}|tj
M}t�|tj|�|�t�|�d�dSr�)rr#r�r�r�rZr;r�fcntl�F_GETFD�
FD_CLOEXECZF_SETFD)r/r|r�rrr�test_get_inheritable_cloexec^sz/FDInheritanceTests.test_get_inheritable_cloexeccCsft�ttj�}|�tj|�|�t�|tj�tj	@tj	�t�
|d�|�t�|tj�tj	@d�dS)NTr)rr#r�r�r�rZr;r�r�r�rr�rrr�test_set_inheritable_cloexecks��z/FDInheritanceTests.test_set_inheritable_cloexec�O_PATHzneed os.O_PATHcC�nt�ttj�}|�tj|�|�t�|�d�t�|d�|�t�|�d�t�|d�|�t�|�d�dSr�)	rr#r�r�r�rZr;rrr�rrr�test_get_set_inheritable_o_pathvsz2FDInheritanceTests.test_get_set_inheritable_o_pathcCs�t��}|�t��
}t�|�Wd�n1swY|�|jjtj	�|�t��}t�
|d�Wd�n1s=wY|�|jjtj	�|�t��}t�
|d�Wd�n1sawY|�|jjtj	�dS�NTF)rr�rhrBrrr;r)rCr*r)r/r|r,rrr�test_get_set_inheritable_badf�s���z0FDInheritanceTests.test_get_set_inheritable_badfcCs2t�ttj�}|�tj|�|�t�|�d�dSr�)rr#r�r�r�rZr;rr�rrrrV�szFDInheritanceTests.test_openr�zneed os.pipe()cC�Pt��\}}|�tj|�|�tj|�|�t�|�d�|�t�|�d�dSr�)rr�r�rZr;r)r/r�r�rrr�	test_pipe��
zFDInheritanceTests.test_pipecCsJt�ttj�}|�tj|�t�|�}|�tj|�|�t�|�d�dSr�)	rr#r�r�r�rZrer;r�r/Zfd1r�rrr�test_dup�s

zFDInheritanceTests.test_dupcCs(t�d�}|�tj|�|�|d�dS�Nrar)rrer�rZr�r�rrr�test_dup_standard_stream�s
z+FDInheritanceTests.test_dup_standard_streamr�r�cCsHt�dtj�}|�tj|�t�|�}|�tj|�|�t�|��dS)NZNUL)rr#r�r�rZrer�rr�rrr�test_dup_nul�s

zFDInheritanceTests.test_dup_nulr�zneed os.dup2()cCs�t�ttj�}|�tj|�t�ttj�}|�tj|�|�t�||�|�|�t�	|��t�ttj�}|�tj|�|�tj||dd�|�|�
t�	|��dS)NF)Zinheritable)rr#r�r�r�rZr;r�r[rr�)r/r|r�Zfd3rrrr��szFDInheritanceTests.test_dup2�openptyzneed os.openpty()cCr�r�)rr�r�rZr;r)r/Z	master_fdZslave_fdrrr�test_openpty�r�zFDInheritanceTests.test_openpty)rNrOrPr�rr�r�r�r�rrrr�r�rVr�r�r�r7r8r�r�r�rrrrr�Us(






r�c@sHeZdZddddejfdfddddd	eed
d�fgZdd�Zd
d�ZdS)�PathTConverterTests)r�TrN)riFrNr\FN)r�F�rN)r�Fr�Nr#r�rZcCs�tj}tjdkr
d}}n
t�tj�}t|�}t�t|�tjtjB�}|�	tj
tj�|�	tj|�t|�}t|�}|jD]�\}}}	}
|j
|d���ztt|�}Wnty`YWd�q=w||||fD].}|durnqg|j
||d��||g|	�R�}
|
dur�|
|
�Wd�n1s�wYqg|�td��||g|	�R�Wd�n1s�wY|r�||g|	�R�}
|
dur�|
|
�n|�td��||g|	�R�Wd�n1s�wYWd�n1s�wYq=dS)Nr\r)rr<zto return str or byteszos.PathLike)rrSrrrOrr#r�rXr�rTrZ�	functionsr�r�r�assertRaisesRegexrn)r/Zstr_filenameZbytes_fspath�bytes_filenamer|Z
int_fspathZ
str_fspathrZallow_fdZ
extra_argsZ
cleanup_fnrKr<rrrr�test_path_t_converter�sd

��������������z)PathTConverterTests.test_path_t_convertercCs�d}|�t|d��t�td��Wd�n1swY|�t|d��t�td��Wd�n1s;wY|�t|d��t�tt���Wd�dS1s\wYdS)Nz-__fspath__\(\) to return str or bytes, not %sr�rc�floatg���Q�@�object)r�rnrr�rr�)r/rjrrr�&test_path_t_converter_and_custom_classs��"�z:PathTConverterTests.test_path_t_converter_and_custom_class)	rNrOrPr�F_OKr�r�r�r�rrrrr��s�	,r�rrc@r�)�
BlockingTestscCr�r�)	rr#r�r�r�rZr;rrr�rrrrszBlockingTests.test_blockingN)rNrOrPrrrrrr�r1r�c@r�)�ExportsTestscCs |�dtj�|�dtj�dS)Nr#r�)r�r�__all__rUrrr�test_os_all!szExportsTests.test_os_allN)rNrOrPr�rrrrr� r�r�c@r()�TestDirEntrycCs0tj�tj�|_|�tj|j�t�|j�dSr*)rr<�realpathrrSr�r>r?rUrrrrV'szTestDirEntry.setUpcCs|�ttj�dSr*)rhrnr�DirEntryrUrrr�test_uninstantiable,r�z TestDirEntry.test_uninstantiablecCsjttj�|jd�d�}dd�t�|j�D���}|�|tj�|�|j	d�ddl
}|�t|j
||�dS)Nr��pythoncSsg|]}|�qSrr�r�entryrrrr1sz0TestDirEntry.test_unpickable.<locals>.<listcomp>r)r'rr<r>�scandirr�r,r�r;rrrhrnr)r/r%rrrrr�test_unpickable/szTestDirEntry.test_unpickableN)rNrOrPrVr�rrrrrr�&sr�c@s
eZdZejZdd�Zd<dd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
d<dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Ze�ejejvd(�d)d*��Zd+d,�Zd-d.�Zd/d0�Z d1d2�Z!d3d4�Z"d5d6�Z#d7d8�Z$d9d:�Z%d;S)=�TestScandircCs>tj�tj�|_t�|j�|_|�tj|j�t�	|j�dSr*)
rr<r�rrSrO�
bytes_pathr�r>r?rUrrrrV;szTestScandir.setUpr�cCs2t|t�r|jn|j}tj�||�}t|d�|S)Nr)rrKrr<rr>r')r/rr<r%rrrr'As
zTestScandir.create_filecCs2tdd�t�|j�D��}|�t|���|�|S)Ncs��|]}|j|fVqdSr*rrrrrr�Hs��z*TestScandir.get_entries.<locals>.<genexpr>)r�rrr<r;r��keys)r/�names�entriesrrr�get_entriesGs

�zTestScandir.get_entriescCs\|r&t|�D]}|�d�sq|dvrq|�t||�t||�|||f�qdS|�||�dS)NZst_)�st_dev�st_ino�st_nlink)r�r�r;r�)r/Zstat1Zstat2Zskip_fieldsrrrr�assert_stat_equalMs
��	zTestScandir.assert_stat_equalcCs(t�|j�}|�tt|��|��dSr*)rrr<rhrnrzrZ)r/�scandir_iterrrrr�ZszTestScandir.test_uninstantiablecCs<|�d�}t�|j�}ddl}|�t|j||�|��dS)Nr�r)	r'rrr<rrhrnrrZ)r/r%rrrrrr_s

zTestScandir.test_unpickablecCs0|�|tj�|�|j|�|�|jtj�|j|��|�|��tj|jdd�j	�t�|j�}|�|�
�t�|j��|�|�
�t�|j��|�|��tj�|j��tj|jdd�}|�|j
dd�t�|j��|�|j
dd�t�|j��|�|��|tjdko�|�|�|jdd�|tjdk�dS)NFr~r\)r,rr�r;rr<r>�inoder�r
�is_dir�S_ISDIRr�is_file�S_ISREG�
is_symlinkr�r)r/rrrrrZ
entry_statZentry_lstatrrr�check_entryfs>
�

�

�
�
�
�
��zTestScandir.check_entryc	
Cs�ttd�}t��}tj�|jd�}t�|�|�d�}|rEzt�|tj�|jd��Wnt	yD}z
|�
d|�WYd}~nd}~ww|ratj|tj�|jd�dd�t�|tj�|jd	��ddg}|rl|�d�|rs|�
d
�|�|�}|d}|�|dddd�|d}|�|dddd�|r�|d}|�|dddd�|r�|d}|�|dddd�|d	}|�|d	ddd�dSdS)Nrr�r�z
link_file.txtr%�symlink_dirT)r��symlink_file.txt)rrF)rrrrr<r>r?r'rrrfr�rr�rr)	r/rr�rFr%r�r	r
rrrr�test_attributes�sH


���


�zTestScandir.test_attributescCsNt|t�r|jn|j}tt�|��}|�t|�d�|d}|�|j	|�|Sr�)
rrKrr<rrrr;r9r)r/rr<r
rrrr�	get_entry�szTestScandir.get_entrycCs|j|d�}|�tj�|��S)Nr)r'rrr<�basename)r/rr%rrr�create_file_entry�rzTestScandir.create_file_entryc	Csn|��}t��}z(t�|j�tdd�t��D��}|�t|�	��tj�
|�g�Wt�|�dSt�|�w)Ncsrr*rrrrrr��r�z5TestScandir.test_current_directory.<locals>.<genexpr>)r'rr+r@r<r�rr;r�rr)r/r%Zold_dirr
rrr�test_current_directory�s�z"TestScandir.test_current_directorycCs|��}|�t|�d�dS)Nz<DirEntry 'file.txt'>)rr;r��r/rrrr�	test_repr�r0zTestScandir.test_reprcCs*|��}|�t�|�tj�|jd��dS)Nr�)rr;r�fspathr<r>rrrr�test_fspath_protocol�s"z TestScandir.test_fspath_protocolcCsNt�d�}|j|d�}t�|�}|�|t�|�|tj�t�|j�|��dS)Nz
bytesfile.txtr)	rrOrr!r,rKr;r<r>)r/r�Zbytes_entryr!rrr�test_fspath_protocol_bytes�s

�z&TestScandir.test_fspath_protocol_bytescCs�tj�|jd�}t�|�|�d�}t�|�tjdkr#|�|���|�	|�
��|�	|���tjdkrI|�t
|j�|��|jdd�dS|�|��d�|�t
|j�|jt
|jdd�dS)Nr�r\Fr~r)rr<r>r?rr2rr[rr�rrrhrArr�r�)r/r<rrrr�test_removed_dir�s




zTestScandir.test_removed_dircCs�|��}t�|j�|�|���tjdkr|�|���|�|�	��tjdkr<|�
t|j�|�
�|j
dd�dS|�|��d�|�
t|j
�|j
t|j
dd�dS)Nr\Fr~r)rrrTr<r�rrr[rrrhrArr�r�rrrr�test_removed_file�s

zTestScandir.test_removed_filecCs�t��s	|�d�S|�d�}t�|tj�|jd��|�ddg�}|d}t�	|�|�
|��d�|�|�
��|�|���|�|j
dd��|�|jdd��|�|���|�t|j�|jdd�dS)Nzcannot create symbolic linkr�zsymlink.txtrFr~)rrrfr'rr�r<r>rrTr�rr�rrr[rrhrAr�)r/r%r
rrrr�test_broken_symlink�s"

�
zTestScandir.test_broken_symlinkc	Csp|�d�t�|j�}tt�|��}|�t|�d|�|d}|�|jd�|�|jt�tj�	|jd���dS�Nr�rarsfile.txt)
r'rrOr<rrr;r9rr>)r/�
path_bytesr
rrrrr�s
�zTestScandir.test_bytesc
Cs�|�d�ttfD]\}|t�|j��}|�t��tt�	|��}Wd�n1s*wY|�
t|�d|�|d}|�
|jd�|�
|jt�tj�
|jd���|�t|j�t�|�t|j�t�q	dSr')r'r�r�rrOr<�assertWarnsr�rrr;r9rr>r�rzrK)r/rur(r
rrrr�test_bytes_like s
���zTestScandir.test_bytes_likez.fd support for listdir required for this test.c	Cst|�tjtj�|�d�dg}t��r%t�dtj�	|jd��|�
d�t�|j���}t�|��}t|�}Wd�n1s@wYdd�|D�}|�
t|�|�|�
|t�|��|D]H}|�
|j|j�|�
t�|�|j�|�
|��|jdk�tjtjvr�tj|j|d�}|�
|��|�tj|j|dd�}|�
|jdd�|�q_Wd�dS1s�wYdS)	Nr�rcSr
rrrrrrr=rz'TestScandir.test_fd.<locals>.<listcomp>rF)r�r`r~)r�rrr�r'rrr�r<r>rrerr;r�rrr!rr�r�)r/Zexpected_namesr|r�r
r	rrBrrr�test_fd0s2


���"�zTestScandir.test_fdcCs|�ttjd�dS)Nrt)rhrArrrUrrr�test_empty_pathJr�zTestScandir.test_empty_pathcCsN|�d�t�|j�}t|�}|�t|�d|�t|�}|�t|�d|�dS)Nr�rar)r'rrr<rr;r9)r/r�r
Zentries2rrr�test_consume_iterator_twiceMs
z'TestScandir.test_consume_iterator_twicecCs$digfD]
}|�ttj|�qdS)NgX9��v��?)rhrnrr)r/�objrrr�test_bad_path_typeXs�zTestScandir.test_bad_path_typecCsh|�d�|�d�t�|j�}t|�|��|��|���
~Wd�dS1s-wYdS�Nr��	file2.txt)r'rrr<r�rZ�check_no_resource_warning�r/r�rrr�
test_close\s


"�zTestScandir.test_closecCsx|�d�|�d�t�|j��}t|�Wd�n1swY|���
~Wd�dS1s5wYdSr0)r'rrr<r�r2r3rrr�test_context_managergs


�
"�z TestScandir.test_context_managercCsV|�d�|�d�t�|j��}t|�|��Wd�dS1s$wYdSr0)r'rrr<r�rZr3rrr�test_context_manager_closeos


"�z&TestScandir.test_context_manager_closec	Cs�|�d�|�d�|�t��&t�|j��}t|�ddWd�n1s)wYWd�n1s8wY|���
~Wd�dS1sNwYdS)Nr�r1rar)r'rh�ZeroDivisionErrorrrr<r�r2r3rrr�test_context_manager_exceptionvs


���
"�z*TestScandir.test_context_manager_exceptioncCs�|�d�|�d�t�|j�}t|�|�t��
~t��Wd�n1s)wYt�|j�}t	|�|�
��
~Wd�dS1sIwYdSr0)r'rrr<r�r)�ResourceWarningrZ
gc_collectrr2r3rrr�test_resource_warning�s


�
"�z!TestScandir.test_resource_warningN)r�)&rNrOrPrr2rVr'rrr�rrrrrrr r"r#r$r%r&r�r*rrrrr�r+r,r-r/r4r5r6r8r:rrrrr8sB

)
	�

rc@s^eZdZeej�Zdd�Zdd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�ZdS)�
TestPEP519cC� dD]}|�||�|��qdS)N)r�sgoodbyessome/path/and/file�r;r!)r/�brrr�test_return_bytes���zTestPEP519.test_return_bytescCr<)N)ZhelloZgoodbyezsome/path/and/filer=)r/r}rrr�test_return_string�r@zTestPEP519.test_return_stringcCsLdD]!}t|�}|�||�|��|�dt�|��|�dt�|��qdS)N)�path/like/object�path/like/objectrCrB)rr;r!rrOrL)r/rZpathlikerrr�test_fsencode_fsdecode�s�z!TestPEP519.test_fsencode_fsdecodecCsB|�d|�td���|�tttj��|�ttd�tj��dS)Nz#feelthegilr�)r;r!rr[�
issubclassr�PathLikerrUrrrr��szTestPEP519.test_pathlikecCs4tddi�}ttt|�fD]
}|�t|j|�q
dS)NZblahr)rzr�rrhrnr!)r/Zvapor�orrr�test_garbage_in_exception_out�s�z(TestPEP519.test_garbage_in_exception_outcCs|�t|j�dSr*)rhrnr!rUrrr�test_argument_required�r�z!TestPEP519.test_argument_requiredcCsR|�t|jtd��tddi�}d|_|�t|j|��|�t|jtt���dS)Nr�Zfoorra)rhrnr!rrz�
__fspath__r7)r/�crrr�test_bad_pathlike�s
�zTestPEP519.test_bad_pathlikecCs8Gdd�dtj�}|�tt|��|�tttj��dS)Nc@r�)z0TestPEP519.test_pathlike_subclasshook.<locals>.ANr�rrrr�A�r�rM)rrFr�rErr[)r/rMrrr�test_pathlike_subclasshook�sz%TestPEP519.test_pathlike_subclasshookcCs|�tjttj�dSr*)r,rrFrK�types�GenericAliasrUrrr�test_pathlike_class_getitem�r�z&TestPEP519.test_pathlike_class_getitemN)rNrOrPr�rr!r?rArDr�rHrIrLrNrQrrrrr;�s
r;c@r�)�
TimesTestscCsrt��}|�|tj�dD]
}t||�}|�|t�q
tjdkr7|�|jd�|�|j	d�|�|j
d�dSdS)N)�user�system�
children_user�children_system�elapsedr\r)rr^r,�times_resultr�r�rr;rUrVrW)r/r^Zfieldr!rrr�
test_times�s

�zTimesTests.test_timesN)rNrOrPrYrrrrrR�r�rR�forkc@r�)�	ForkTestscCs d}td|�td|dd�dS)Nz�if 1:
            import os
            from test import support
            pid = os.fork()
            if pid != 0:
                support.wait_process(pid, exitcode=0)
        r�Zmalloc_debug)ZPYTHONMALLOCrr�rrr�	test_fork�s
zForkTests.test_forkN)rNrOrPr\rrrrr[�sr[�_fspathc@seZdZdZeej�ZdS)�TestPEP519PurePythonz>Explicitly test the pure Python implementation of os.fspath().N)rNrOrPr�r�rr]r!rrrrr^�sr^�__main__)r!r*)�r��
contextlibrzrCr�r{r=r�rwrrr�rRrrrNr�r�r�r7Z	sysconfigr:r�rJrkrOrrvr��testrZtest.supportrrrrrr8rr��simplefilterr�r9rLr��ImportErrorr�r��pwdZgetpwallr�rZ	_testcapirrr�Ztest.support.script_helperrrZtest.support.os_helperrr�rrr�versionr�r��getgidr9rr'r�r�ZTestCaser)rQr�r<r�ZBasicTestMappingProtocolr�r�rr8rMrTrVrsr�r�r�r�Zget_config_varZOS_URANDOM_DONT_USE_FDr��contextmanagerr�r�r�r�r r.rJrZr�r�r�r�r�r�r�r�r�r�r�r�r.r4rMrKr8r_r�Zrequires_linux_versionr�r�r�r�r�r�r�r�r�r�r�rr;rRr[r^rN�mainrrrr�<module>s�

������
�EZe\)EG
j;)	-����M �h#w%CGs/<0![H4�VO�Q0
Q]
yB�Y:
�