
    Sie&                        S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	J
r
  SS jr " S S\R                  5      r\S:X  a  \R                  " 5         gg)	    )annotationsN)patchc                 r   [         R                  S:H  n U (       a  [        R                  $ [         R                  R                  [        R                  5      (       aH  [         R                  " [        R                  [         R                  5      (       a  [        R                  $ [         R                  R                  [         R                  R                  [        R                  " S5      SS5      5      n[         R                  R                  U5      (       a  [         R                  R                  USS5      n[         R                  R                  U5      (       a  U$ [         R                  R                  USS5      n[         R                  R                  U5      (       a  U$ [        R                  $ )zGet the Python executable path for CMake configuration.

Prefer sys.executable as it represents the currently running Python.
Only fall back to directory traversal if sys.executable is invalid.
ntincludez..binpython3python)osnamesys
executablepathisfileaccessX_OKabspathjoin	sysconfigget_pathisdir)WINDOWS
python_dir
python_bins      c/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx/test/test_env_python_executable.pyget_python_executer      s	    ggoG~~ 
ww~~cnn%%"))CNNBGG*L*L~~ 
Y''	2D$?J 
ww}}Z  WW\\*eY?
77>>*%%WW\\*eX>
77>>*%%>>    c                     \ rS rSrSrSS jrSS jr\R                  " \	R                  S:H  =(       a    \	R                  S:  S5      SS j5       rSS	 jrSS
 jr\R                  " \	R                  S:H  =(       a    \	R                  S:  S5      SS j5       rSS jrSS jr\R                  " \
R$                  " 5       S:H  S5      SS j5       rSrg)TestGetPythonExecutable0   z;Test suite for get_python_execute() function from setup.py.c                    [        SS5         [        5       nU R                  U[        R                  5        SSS5        g! , (       d  f       g= f)zEOn Windows, get_python_execute() should always return sys.executable.os.namer   Nr   r   assertEqualr   r   selfresults     r   #test_windows_returns_sys_executable;TestGetPythonExecutable.test_windows_returns_sys_executable3   s4    9d#')FVS^^4 $##   +A
Ac                    [        SS5         [        5       nU R                  U[        R                  5        SSS5        g! , (       d  f       g= f)zBWhen sys.executable is valid, it should be returned (non-Windows).r"   posixNr#   r%   s     r   &test_valid_sys_executable_is_preferred>TestGetPythonExecutable.test_valid_sys_executable_is_preferred9   s4    9g&')FVS^^4	 '&&r*   win32)      ziOn Windows this test requires Python >= 3.11 due to sysconfig/sys.abiflags behavior on older interpretersc           
        [        SS5         [        SS5         [        S5       n[        S5       n[        S5       nSUl        SUl        SUl        [        5       nU R                  US5        S	S	S	5        S	S	S	5        S	S	S	5        S	S	S	5        S	S	S	5        g	! , (       d  f       N/= f! , (       d  f       N8= f! , (       d  f       NA= f! , (       d  f       NJ= f! , (       d  f       g	= f)
zEWhen sys.executable is invalid, should fall back to directory search.r"   r,   sys.executablez/nonexistent/pythonos.path.isfile	os.accessos.path.isdirFN)r   return_valuer   r$   r&   mock_isfilemock_access
mock_isdirr'   s        r   &test_invalid_sys_executable_falls_back>TestGetPythonExecutable.test_invalid_sys_executable_falls_backA   s     )W%"$9:"#{++/"j (-K$',K$&+J#')FV%:; #  $ ; &% #"  $# ;: &%sg   CCB;B*	2B0B*	8B; CC
B'#B*	*
B84B;;
C	C
C	C
C+c           
     ^   [         R                  " 5        n[        R                  " U5      S-  nUS-  nUS-  S-  nUR	                  SS9  UR	                  SS9  US-  nUR                  SS	9  [        S
S5         [        SS5         [        S5       n[        U5      Ul        [        5       nU R                  U[        U5      5        SSS5        SSS5        SSS5        SSS5        g! , (       d  f       N'= f! , (       d  f       N0= f! , (       d  f       N9= f! , (       d  f       g= f)zLTest fallback finds python3 in bin directory when sys.executable is invalid.python_installr   r   
python3.12Tparentsr	     moder"   r,   r3   /invalid/pythonsysconfig.get_pathNtempfileTemporaryDirectorypathlibPathmkdirtouchr   strr7   r   r$   )r&   tmpdirmock_python_dirmock_bin_dirmock_include_dirmock_python3mock_get_pathr'   s           r   "test_fallback_finds_python3_in_bin:TestGetPythonExecutable.test_fallback_finds_python3_in_binW   s
    ((*f%ll625EEO*U2L  /:\It,""4"0')3LE* i)&(9:*+} .11A-B*+-  \):; , ; * +* ,+ ;: *) +*T   A%D;DC<6C+	
C<DD+
C95C<<
D
D
D	D
D,c           
     ^   [         R                  " 5        n[        R                  " U5      S-  nUS-  nUS-  S-  nUR	                  SS9  UR	                  SS9  US-  nUR                  SS	9  [        S
S5         [        SS5         [        S5       n[        U5      Ul        [        5       nU R                  U[        U5      5        SSS5        SSS5        SSS5        SSS5        g! , (       d  f       N'= f! , (       d  f       N0= f! , (       d  f       N9= f! , (       d  f       g= f)zDTest fallback finds python (not python3) when python3 doesn't exist.r?   r   r   r@   TrA   r
   rC   rD   r"   r,   r3   rF   rG   NrH   )r&   rP   rQ   rR   rS   mock_pythonrU   r'   s           r   !test_fallback_finds_python_in_bin9TestGetPythonExecutable.test_fallback_finds_python_in_binq   s
    ((*f%ll625EEO*U2L  /:\It,""4"0&1K5) i)&(9:*+} .11A-B*+-  [)9: , ; * +* ,+ ;: *) +*rX   c           
        [        SS5         [        SS5         [        S5       n[        S5       n[        S5       nSUl        S	Ul        S	Ul        [        5       nU R                  U[        R
                  5        S
S
S
5        S
S
S
5        S
S
S
5        S
S
S
5        S
S
S
5        g
! , (       d  f       N/= f! , (       d  f       N8= f! , (       d  f       NA= f! , (       d  f       NJ= f! , (       d  f       g
= f)z?Test that executable permission is verified for sys.executable.r"   r,   r3   z/path/to/pythonr4   r5   r6   TFN)r   r7   r   r$   r   r   r8   s        r    test_executable_permission_check8TestGetPythonExecutable.test_executable_permission_check   s     )W%"$56"#{++/"j (,K$',K$&+J#')FVS^^4 #  $ 7 &% #"  $# 76 &%sh   C+CC	B8	A B'>B8	C	CC+'
B51B8	8
CC		
CC
C(	$C++
C9c                   [        5       nU R                  U[        5        U R                  [	        U5      S5        [
        R                  " 5       S:X  a!  U R                  U[        R                  5        g[        R                  R                  [        R                  5      (       aZ  [        R                  " [        R                  [        R                  5      (       a!  U R                  U[        R                  5        ggg)z:Test with real environment to ensure it works in practice.r   WindowsN)r   assertIsInstancerO   assertGreaterlenplatformsystemr$   r   r   r   r   r   r   r   r%   s     r   test_real_environment-TestGetPythonExecutable.test_real_environment   s    #% 	fc*3v;* ??	)VS^^4 WW^^CNN++		#.."''0R0RVS^^4 1S+r   c                   [        SS5         [        5       n[        [        S5      (       a  [        [        S5      (       a{  [        R                  [        R
                  :g  nU(       aS  [        R                  R                  [        R                  5      (       a!  U R                  U[        R                  S5        SSS5        g! , (       d  f       g= f)z;Test that virtual environment Python is correctly detected.r"   r,   prefixbase_prefixz-Virtual environment Python should be detectedN)r   r   hasattrr   rj   rk   r   r   r   r   r$   )r&   r'   in_venvs      r   "test_virtual_environment_detection:TestGetPythonExecutable.test_virtual_environment_detection   s    9g&')F sH%%'#}*E*E**7rww~~cnn==$$G '&&s   B0C
Cra   z0Fallback mechanism only applies to POSIX systemsc                   [        SS5         [        S5       n[        S5       n[        S5       n[        S5       n[        S5       n[        SS	 S
9   SUl        SUl        SUl        SUl        SUl        SS jnXal        [        5       nU R	                  US5        SSS5        SSS5        SSS5        SSS5        SSS5        SSS5        SSS5        g! , (       d  f       N?= f! , (       d  f       NH= f! , (       d  f       NQ= f! , (       d  f       NZ= f! , (       d  f       Nc= f! , (       d  f       Nl= f! , (       d  f       g= f)zTest that the fallback handles cpython issue #84399 edge case.

This test is skipped on Windows because the Windows implementation
always returns sys.executable without any fallback logic.
The fallback mechanism is only relevant for POSIX systems.
r3   z/usr/bin/python-invalidr4   r5   rG   r6   zos.path.abspathzos.path.joinc                 $    SR                  U 5      $ )N/)r   )argss    r   <lambda>KTestGetPythonExecutable.test_cpython_issue_84399_fallback.<locals>.<lambda>   s    CHHTNr   )side_effectFz/usr/include/python3.12Tz/usrc                    U S:H  $ )N/usr/bin/python3 )r   s    r   isfile_checkOTestGetPythonExecutable.test_cpython_issue_84399_fallback.<locals>.isfile_check   s    111r   rx   N)r   rO   returnbool)r   r7   rv   r   r$   )r&   r9   r:   rU   r;   mock_abspathrz   r'   s           r   !test_cpython_issue_84399_fallback9TestGetPythonExecutable.test_cpython_issue_84399_fallback   s    "$=>"#{++&'=/"j#$..JK (-K$',K$)BM&&*J#(.L%2 '3#')FV%78! L % # (  $ ?> LK %$ #" ('  $# ?>s   D?D.DD	C;	C*AC C*(C;0D	8D D.D?
C'#C**
C84C;;
D	D	
DD
D+'D..
D<	8D??
Ery   N)r|   None)__name__
__module____qualname____firstlineno____doc__r(   r-   unittestskipIfr   re   version_infor<   rV   r[   r^   rg   rn   rf   r   __static_attributes__ry   r   r   r   r   0   s    E55 __>C$4$4w$>s<	<$<4;4 __>C$4$4w$>s5	5$5   __Y&: 9	 9r   r   __main__)r|   rO   )
__future__r   r   rK   re   r   r   rI   r   unittest.mockr   r   TestCaser   r   mainry   r   r   <module>r      sT    # 	   
    >u9h// u9p zMMO r   