o
    HXîhÂ-  ã                   @   sþ   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZmZ dd„ Zdd„ ZG dd	„ d	ee
jƒƒZG d
d„ dƒZG dd„ dee
jeddZG dd„ dee
jeddZG dd„ de
jƒZedkr}e
 ¡  dS dS )é    N)Úsupport)Ú	os_helperÚscript_helperc                    ó   t  ˆ ¡‡ fdd„ƒ}|S )z+Runs function with SOURCE_DATE_EPOCH unset.c                     sD   t  ¡ }| d¡ ˆ | i |¤ŽW  d   ƒ S 1 sw   Y  d S )NÚSOURCE_DATE_EPOCH)r   ÚEnvironmentVarGuardZunset©ÚargsÚkwargsÚenv©Úfxn© ú>/opt/python-3.10.19/usr/lib/python3.10/test/test_py_compile.pyÚwrapper   s   

$þz*without_source_date_epoch.<locals>.wrapper©Ú	functoolsÚwraps©r   r   r   r   r   Úwithout_source_date_epoch   ó   r   c                    r   )z)Runs function with SOURCE_DATE_EPOCH set.c                     sB   t  ¡ }d|d< ˆ | i |¤ŽW  d   ƒ S 1 sw   Y  d S )NZ	123456789r   )r   r   r   r   r   r   r      s   
$þz'with_source_date_epoch.<locals>.wrapperr   r   r   r   r   Úwith_source_date_epoch   r   r   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚSourceDateEpochTestMetac          	         sZ   t ƒ  | |||¡}t|ƒD ]}| d¡r*t||ƒ}|r t|ƒ}nt|ƒ}t|||ƒ q|S )NZtest_)ÚsuperÚ__new__ÚdirÚ
startswithÚgetattrr   r   Úsetattr)	ÚmclsÚnameÚbasesZdctÚsource_date_epochÚclsÚattrÚmethr   ©Ú	__class__r   r   r   &   s   


€zSourceDateEpochTestMeta.__new__)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r&   r   r   %   s    r   c                   @   sî   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Ze e	j
 e	j¡ p$e	j
 e	j¡d	¡d
d„ ƒZdd„ Zdd„ Zdd„ Ze ee	dƒoCe	 ¡ dkd¡e e	jdkd¡dd„ ƒƒZdd„ Zdd„ Ze ejjdkd¡dd„ ƒZd d!„ Zd"d#„ Zd$d%„ Zd&S )'ÚPyCompileTestsBasec                 C   s²   t jt ¡ d| _tj | jd¡| _| jd | _t	j
 | j¡| _tj t ¡ ¡d | _tj | j¡d }|r;t |¡ t| jdƒ}| d¡ W d   ƒ d S 1 sRw   Y  d S )N)r   ú_test.pyÚcr   Úwúx = 123
)ÚtempfileÚmkdtempÚosÚgetcwdÚ	directoryÚpathÚjoinÚsource_pathÚpyc_pathÚ	importlibÚutilÚcache_from_sourceÚ
cache_pathÚ
splitdriveÚ	cwd_driveÚchdirÚopenÚwrite)ÚselfÚdriveÚfiler   r   r   ÚsetUp7   s   
"ÿzPyCompileTestsBase.setUpc                 C   s&   t  | j¡ | jrt | j¡ d S d S ©N)ÚshutilÚrmtreer5   r?   r3   r@   ©rC   r   r   r   ÚtearDownG   s   ÿzPyCompileTestsBase.tearDownc                 C   s<   t  | j| j¡ |  tj | j¡¡ |  tj | j	¡¡ d S rG   )
Ú
py_compileÚcompiler8   r9   Ú
assertTruer3   r6   ÚexistsÚassertFalser=   rJ   r   r   r   Útest_absolute_pathL   s   z%PyCompileTestsBase.test_absolute_pathc              	   C   sŽ   zt  | jd | j¡ W n ttfy   |  d¡ Y d S w t j | j¡s'J ‚|  t	¡ t
 | j| j¡ W d   ƒ d S 1 s@w   Y  d S )Nz.actualz.need to be able to create a symlink for a file)r3   Úsymlinkr9   ÚNotImplementedErrorÚOSErrorZskipTestr6   ÚislinkÚassertRaisesÚFileExistsErrorrL   rM   r8   rJ   r   r   r   Útest_do_not_overwrite_symlinksQ   s   ÿ"ÿz1PyCompileTestsBase.test_do_not_overwrite_symlinksz7requires os.devnull and for it to be a non-regular filec                 C   s@   |   t¡ t | jtj¡ W d   ƒ d S 1 sw   Y  d S rG   )rV   rW   rL   rM   r8   r3   ÚdevnullrJ   r   r   r   Ú&test_do_not_overwrite_nonregular_files]   s   "ÿz9PyCompileTestsBase.test_do_not_overwrite_nonregular_filesc                 C   s$   t  | j¡ |  tj | j¡¡ d S rG   )rL   rM   r8   rN   r3   r6   rO   r=   rJ   r   r   r   Útest_cache_pathe   s   z"PyCompileTestsBase.test_cache_pathc                 C   sx   t  | j¡ t tj | j¡tj | j	¡¡ W d   ƒ n1 s!w   Y  |  
tj | j	¡¡ |  tj | j¡¡ d S rG   )r   Z
change_cwdr5   rL   rM   r3   r6   Úbasenamer8   r9   rN   rO   rP   r=   rJ   r   r   r   Útest_cwdi   s   ÿÿzPyCompileTestsBase.test_cwdc                 C   sL   t  tj | j¡tj | j¡¡ |  tj | j¡¡ |  	tj | j
¡¡ d S rG   )rL   rM   r3   r6   Úrelpathr8   r9   rN   rO   rP   r=   rJ   r   r   r   Útest_relative_pathp   s
   ÿz%PyCompileTestsBase.test_relative_pathÚgeteuidr   znon-root user requiredÚntz/cannot control directory permissions on Windowsc              
   C   s–   t  | j¡}t  | jtj¡ z3|  t¡ t | j	| j
¡ W d   ƒ n1 s'w   Y  W t  | j|j¡ d S W t  | j|j¡ d S t  | j|j¡ w rG   )r3   Ústatr5   ÚchmodÚS_IREADrV   ÚIOErrorrL   rM   r8   r9   Úst_mode)rC   Úmoder   r   r   Útest_exceptions_propagatev   s   ÿý&z,PyCompileTestsBase.test_exceptions_propagatec                 C   sp   t j t j t¡d¡}t ¡  |  tj	|dd¡ W d   ƒ n1 s$w   Y  |  
t j tj |¡¡¡ d S )Núbad_coding2.pyF)Údoraise)r3   r6   r7   ÚdirnameÚ__file__r   Úcaptured_stderrÚassertIsNonerL   rM   rP   rO   r:   r;   r<   )rC   Ú
bad_codingr   r   r   Útest_bad_coding†   s   
ÿ


ÿz"PyCompileTestsBase.test_bad_codingc                 C   sž   t  | j| j¡ |  tj | j¡¡ |  tj | j	¡¡ t
| jdƒ}tj | ¡ di ¡}W d   ƒ n1 s7w   Y  tj d¡rEd}nd}|  ||¡ d S )NÚrbÚtestr   é   r   )rL   rM   r8   r9   rN   r3   r6   rO   rP   r=   rA   r:   Ú_bootstrap_externalÚ_classify_pycÚreadÚenvironÚgetÚassertEqual)rC   ÚfpÚflagsZexpected_flagsr   r   r   Útest_source_date_epoch   s   
ÿÿz)PyCompileTestsBase.test_source_date_epochztest does not work with -Oc              	   C   sÐ   t j | jd¡}tj |¡}|d }t j |¡\}}t j |¡}|  	t j ||¡t j dd 
tjj¡¡¡ t|dƒ}| d¡ W d   ƒ n1 sJw   Y  t |¡ |  t j |¡¡ |  t j |¡¡ d S )Nz
foo.bar.pyr.   Ú__pycache__zfoo.bar.{}.pycr/   r0   )r3   r6   r7   r5   r:   r;   r<   Úsplitr\   ry   ÚformatÚsysÚimplementationÚ	cache_tagrA   rB   rL   rM   rN   rO   rP   )rC   Z
weird_pathr=   r9   ÚheadÚtailZpenultimate_tailrE   r   r   r   Útest_double_dot_no_clobber›   s$   þþÿ
z-PyCompileTestsBase.test_double_dot_no_clobberc                 C   s   |   dtj| jdd¡ d S )Nzopt-2é   )Úoptimize)ÚassertInrL   rM   r8   rJ   r   r   r   Útest_optimization_path¯   s   z)PyCompileTestsBase.test_optimization_pathc                 C   sÄ   t j| jt jjd t| jdƒ}tj 	| 
¡ di ¡}W d   ƒ n1 s%w   Y  |  |d¡ t j| jt jjd t| jdƒ}tj 	| 
¡ di ¡}W d   ƒ n1 sUw   Y  |  |d¡ d S )N)Úinvalidation_moderq   rr   rs   é   )rL   rM   r8   ÚPycInvalidationModeÚCHECKED_HASHrA   r=   r:   rt   ru   rv   ry   ÚUNCHECKED_HASH)rC   rz   r{   r   r   r   Útest_invalidation_mode³   s(   þ
ÿÿþ
ÿÿz)PyCompileTestsBase.test_invalidation_modec              	   C   sÌ   t j t j t¡d¡}t ¡ M}|  tj	|ddd¡ |  tj	|ddd¡ |  
| ¡ d¡ |  tj¡ tj	|ddd W d   ƒ n1 sGw   Y  W d   ƒ d S W d   ƒ d S 1 s_w   Y  d S )Nri   Fr†   )rj   ÚquietTÚ r‹   )r3   r6   r7   rk   rl   r   rm   rn   rL   rM   ry   ÚgetvaluerV   ÚPyCompileError)rC   ro   Ústderrr   r   r   Ú
test_quietÅ   s   
ÿü"üzPyCompileTestsBase.test_quietN)r(   r)   r*   rF   rK   rQ   rX   ÚunittestZskipIfr3   r6   rO   rY   ÚisfilerZ   r[   r]   r_   Úhasattrr`   r    rh   rp   r|   r€   r{   r‡   r…   r‰   r   r•   r   r   r   r   r,   5   s4     ÿ
ÿÿ
r,   c                   @   ó   e Zd ZdS )ÚPyCompileTestsWithSourceEpochN©r(   r)   r*   r   r   r   r   rš   Ï   ó    rš   T)Ú	metaclassr"   c                   @   r™   )Ú PyCompileTestsWithoutSourceEpochNr›   r   r   r   r   rž   Ö   rœ   rž   Fc                   @   s\   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚPyCompileCLITestCasec                 C   sh   t  ¡ | _tj | jd¡| _tj 	| j¡| _
t| jdƒ}| d¡ W d   ƒ d S 1 s-w   Y  d S )Nr-   r/   r0   )r1   r2   r5   r3   r6   r7   r8   r:   r;   r<   r=   rA   rB   )rC   rE   r   r   r   rF   ß   s   
"ÿzPyCompileCLITestCase.setUpc                 C   s   t  | j¡ d S rG   )r   rI   r5   rJ   r   r   r   rK   æ   s   zPyCompileCLITestCase.tearDownc                 O   sT   |r|d dkrd|v rt jtjdddg|d  ¡ ddS tjddg|¢R i |¤ŽS )Nr   ú-Úinputú-mrL   T)r¡   Úcapture_output)Ú
subprocessÚrunr€   Ú
executableÚencoder   Zassert_python_ok)rC   r	   r
   r   r   r   Úpycompilecmdé   s   
þz!PyCompileCLITestCase.pycompilecmdc                 G   s   t jddg|¢R Ž S )Nr¢   rL   )r   Zassert_python_failure)rC   r	   r   r   r   Úpycompilecmd_failureó   s   z)PyCompileCLITestCase.pycompilecmd_failurec                 C   sR   | j d| jd}|  |jd¡ |  |jd¡ |  |jd¡ |  tj 	| j
¡¡ d S )Nr    )r¡   r   ó    )r¨   r8   ry   Ú
returncodeÚstdoutr”   rN   r3   r6   rO   r=   )rC   Úresultr   r   r   Ú
test_stdinö   s
   zPyCompileCLITestCase.test_stdinc                 C   sR   |   | j| j¡\}}}|  |d¡ |  |d¡ |  |d¡ |  tj | j¡¡ d S )Nr   rª   )r¨   r8   ry   rN   r3   r6   rO   r=   )rC   Úrcr¬   r”   r   r   r   Útest_with_filesý   s
   z$PyCompileCLITestCase.test_with_filesc                 C   sN   t j t j t¡d¡}|  |¡\}}}|  |d¡ |  |d¡ |  d|¡ d S )Núbadsyntax_3131.pyr‹   rª   s   SyntaxError)r3   r6   r7   rk   rl   r©   ry   rˆ   ©rC   Z
bad_syntaxr¯   r¬   r”   r   r   r   Útest_bad_syntax  s
   z$PyCompileCLITestCase.test_bad_syntaxc                 C   sP   t j t j t¡d¡}|  d|¡\}}}|  |d¡ |  |d¡ |  |d¡ d S )Nr±   ú-qr‹   rª   )r3   r6   r7   rk   rl   r©   ry   r²   r   r   r   Útest_bad_syntax_with_quiet  s
   z/PyCompileCLITestCase.test_bad_syntax_with_quietc                 C   sV   t j t j t¡d¡}|  | j|¡\}}}|  |d¡ |  |d¡ |  d| 	¡ ¡ d S )Núshould_not_exists.pyr‹   rª   s   no such file or directory)
r3   r6   r7   rk   rl   r©   r8   ry   rˆ   Úlower©rC   Zshould_not_existsr¯   r¬   r”   r   r   r   Útest_file_not_exists  s
   z)PyCompileCLITestCase.test_file_not_existsc                 C   sT   t j t j t¡d¡}|  d| j|¡\}}}|  |d¡ |  |d¡ |  |d¡ d S )Nr¶   r´   r‹   rª   )r3   r6   r7   rk   rl   r©   r8   ry   r¸   r   r   r   Útest_file_not_exists_with_quiet  s
   z4PyCompileCLITestCase.test_file_not_exists_with_quietN)r(   r)   r*   rF   rK   r¨   r©   r®   r°   r³   rµ   r¹   rº   r   r   r   r   rŸ   Ý   s    
rŸ   Ú__main__)r   Úimportlib.utilr:   r3   rL   rH   rb   r¤   r€   r1   r–   rr   r   Ztest.supportr   r   r   r   ÚtypeZTestCaser   r,   rš   rž   rŸ   r(   Úmainr   r   r   r   Ú<module>   s>    
 ýýDÿ