
    /je&                        d dl m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Z G d dej                  Zed	k    r ej                     dS dS )    )annotationsN)patchreturnstrc                    t           j        dk    } | rt          j        S t           j                            t          j                  r5t          j        t          j        t           j                  rt          j        S t           j                            t           j        	                    t          j        d          dd                    }t           j                            |          rt           j        	                    |dd          }t           j                            |          r|S t           j        	                    |dd          }t           j                            |          r|S t          j        S )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      i/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/onnx/test/test_env_python_executable.pyget_python_executer      s    goG ~ 
w~~cn%% ")CNBG*L*L ~ 
Y'	22D$?? J 
w}}Z   W\\*eY??
7>>*%% 	W\\*eX>>
7>>*%% 	>    c                  V   e Zd ZdZddZddZ ej        ej	        dk    o
ej
        dk     d          dd	            Zdd
ZddZ ej        ej	        dk    o
ej
        dk     d          dd            ZddZddZ ej         e	j                    dk    d          dd            ZdS )TestGetPythonExecutablez;Test suite for get_python_execute() function from setup.py.r   Nonec                    t          dd          5  t                      }|                     |t          j                   ddd           dS # 1 swxY w Y   dS )zEOn Windows, get_python_execute() should always return sys.executable.os.namer   Nr   r   assertEqualr   r   selfresults     r   #test_windows_returns_sys_executablez;TestGetPythonExecutable.test_windows_returns_sys_executable3   s    9d## 	5 	5'))FVS^444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5   /AAAc                    t          dd          5  t                      }|                     |t          j                   ddd           dS # 1 swxY w Y   dS )zBWhen sys.executable is valid, it should be returned (non-Windows).r$   posixNr%   r'   s     r   &test_valid_sys_executable_is_preferredz>TestGetPythonExecutable.test_valid_sys_executable_is_preferred9   s    9g&& 	5 	5'))FVS^444		5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5r+   win32)      ziOn Windows this test requires Python >= 3.11 due to sysconfig/sys.abiflags behavior on older interpretersc           
        t          dd          5  t          dd          5  t          d          5 }t          d          5 }t          d          5 }d|_        d|_        d|_        t                      }|                     |d           d	d	d	           n# 1 swxY w Y   d	d	d	           n# 1 swxY w Y   d	d	d	           n# 1 swxY w Y   d	d	d	           n# 1 swxY w Y   d	d	d	           d	S # 1 swxY w Y   d	S )
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_backz>TestGetPythonExecutable.test_invalid_sys_executable_falls_backA   sG    )W%%	< 	<"$9::	< 	< "##	< (3+		< #./""	<
 '1 (-K$',K$&+J#'))FV%:;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   C5CCB/	:BB/	BB/	B B/	#C/B33C6B37C:CC

CC
CC5C!	!C5$C!	%C55C9<C9c           
        t          j                    5 }t          j        |          dz  }|dz  }|dz  dz  }|                    d           |                    d           |dz  }|                    d	           t          d
d          5  t          dd          5  t          d          5 }t          |          |_        t                      }| 
                    |t          |                     ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )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   r   r7   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_binz:TestGetPythonExecutable.test_fallback_finds_python3_in_binW   sd    (** 	<f%l6225EEO*U2L  /:\It,,,""4"000')3LE*** i))	< 	<&(9::	< 	< *++	< 0= .11A-B-B*+--  \):):;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<   A<E	D1!D1AD	7DDD
DDD1DD1!D"D1%E	1D5	5E	8D5	9E		EEc           
        t          j                    5 }t          j        |          dz  }|dz  }|dz  dz  }|                    d           |                    d           |dz  }|                    d	           t          d
d          5  t          dd          5  t          d          5 }t          |          |_        t                      }| 
                    |t          |                     ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zDTest fallback finds python (not python3) when python3 doesn't exist.r>   r
   r	   r?   Tr@   r   rB   rC   r$   r-   r3   rE   rF   NrG   )r(   rN   rO   rP   rQ   mock_pythonrS   r)   s           r   !test_fallback_finds_python_in_binz9TestGetPythonExecutable.test_fallback_finds_python_in_binq   sd    (** 	;f%l6225EEO*U2L  /:\It,,,""4"000&1K5))) i))	; 	;&(9::	; 	; *++	; 0= .11A-B-B*+--  [)9)9:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;rU   c           
        t          dd          5  t          dd          5  t          d          5 }t          d          5 }t          d          5 }d|_        d	|_        d	|_        t                      }|                     |t          j                   d
d
d
           n# 1 swxY w Y   d
d
d
           n# 1 swxY w Y   d
d
d
           n# 1 swxY w Y   d
d
d
           n# 1 swxY w Y   d
d
d
           d
S # 1 swxY w Y   d
S )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_checkz8TestGetPythonExecutable.test_executable_permission_check   sH    )W%%	5 	5"$566	5 	5 "##	5 (3+		5 #./""	5
 '1 (,K$',K$&+J#'))FVS^444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5s   C?C'CB9	AB"B9	"B&&B9	)B&*B9	-C9B==C B=CC'CC'CC'C?'C+	+C?.C+	/C??DDc                   t                      }|                     |t                     |                     t	          |          d           t          j                    dk    r"|                     |t          j	                   dS t          j                            t          j	                  rKt          j        t          j	        t          j                  r$|                     |t          j	                   dS dS dS )z:Test with real environment to ensure it works in practice.r   WindowsN)r   assertIsInstancer   assertGreaterlenplatformsystemr&   r   r   r   r   r   r   r   r'   s     r   test_real_environmentz-TestGetPythonExecutable.test_real_environment   s    #%% 	fc***3v;;*** ?	))VS^44444 W^^CN++ 	5	#."'0R0R 	5VS^44444	5 	5 	5 	5r   c                   t          dd          5  t                      }t          t          d          r{t          t          d          rft          j        t          j        k    }|rJt          j                            t          j	                  r!| 
                    |t          j	        d           ddd           dS # 1 swxY w Y   dS )z;Test that virtual environment Python is correctly detected.r$   r-   prefixbase_prefixz-Virtual environment Python should be detectedN)r   r   hasattrr   rd   re   r   r   r   r   r&   )r(   r)   in_venvs      r   "test_virtual_environment_detectionz:TestGetPythonExecutable.test_virtual_environment_detection   s    9g&& 	 	'))F sH%% '#}*E*E *7 rw~~cn== $$G  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   BB==CCr\   z0Fallback mechanism only applies to POSIX systemsc                   t          dd          5  t          d          5 }t          d          5 }t          d          5 }t          d          5 }t          d          5 }t          dd	 
          5  d|_        d|_        d|_        d|_        d|_        dd}||_        t                      }|                     |d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )a  Test 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   rF   r6   zos.path.abspathzos.path.joinc                 ,    d                     |           S )N/)r   )argss    r   <lambda>zKTestGetPythonExecutable.test_cpython_issue_84399_fallback.<locals>.<lambda>   s    CHHTNN r   )side_effectFz/usr/include/python3.12Tz/usrr   r   r   boolc                    | dk    S )N/usr/bin/python3 )r   s    r   isfile_checkzOTestGetPythonExecutable.test_cpython_issue_84399_fallback.<locals>.isfile_check   s    111r   rq   N)r   r   r   ro   )r   r7   rn   r   r&   )r(   r9   r:   rS   r;   mock_abspathrs   r)   s           r   !test_cpython_issue_84399_fallbackz9TestGetPythonExecutable.test_cpython_issue_84399_fallback   s@    "$=>>	9 	9"##	9'2+	9 #.&''		9 ,9/""	9
 '1#$$	9 )5..J.JKKK	9 	9 (-K$',K$)BM&&*J#(.L%2 2 2 2 '3K#'))FV%7888/	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9s   EED/D	D!C*4ACC*CC*CC*D*C..D1C.2D5D	DD	D	D	D/DD/D D/#E/D33E6D37E:EE
	
EE
	EE"%E"N)r   r"   )__name__
__module____qualname____doc__r*   r.   unittestskipIfr   r`   version_infor<   rT   rX   rZ   rb   rh   ra   ru   rr   r   r   r!   r!   0   sn       EE5 5 5 55 5 5 5 X_>C$4w$>s < < <	 <$< < < <4; ; ; ;4 X_>C$4w$>s 5 5 5	 5$5 5 5 5      X_Y&:  9  9  9	  9  9  9r   r!   __main__)r   r   )
__future__r   r   rJ   r`   r   r   rH   rz   unittest.mockr   r   TestCaser!   rv   mainrr   r   r   <module>r      s    # " " " " " 				   



               >u9 u9 u9 u9 u9h/ u9 u9 u9p zHMOOOOO r   