o
    r::je&                     @  s   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dZG dd	 d	ejZed
kr@e  dS dS )    )annotationsN)patchreturnstrc                  C  s   t jdk} | r
tjS t jtjrt tjt jrtjS t jt j	t
ddd}t j|rQt j	|dd}t j|rA|S t j	|dd}t j|rQ|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_bin r   k/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnx/test/test_env_python_executable.pyget_python_execute   s    
r   c                   @  s   e Zd ZdZdddZdddZeej	dkoej
d	k d
dddZdddZdddZeej	dko8ej
d	k d
dddZdddZdddZee	 dkddddZdS )TestGetPythonExecutablez;Test suite for get_python_execute() function from setup.py.r   Nonec                 C  D   t dd t }| |tj W d   dS 1 sw   Y  dS )zEOn Windows, get_python_execute() should always return sys.executable.os.namer   Nr   r   assertEqualr   r   selfresultr   r   r   #test_windows_returns_sys_executable3   s   "z;TestGetPythonExecutable.test_windows_returns_sys_executablec                 C  r    )zBWhen sys.executable is valid, it should be returned (non-Windows).r!   posixNr"   r$   r   r   r   &test_valid_sys_executable_is_preferred9   s   "z>TestGetPythonExecutable.test_valid_sys_executable_is_preferredwin32)      ziOn Windows this test requires Python >= 3.11 due to sysconfig/sys.abiflags behavior on older interpretersc                 C  s  t ddt t ddV t dB}t d.}t d}d|_d|_d|_t }| |d W d	   n1 s7w   Y  W d	   n1 sFw   Y  W d	   n1 sUw   Y  W d	   n1 sdw   Y  W d	   d	S W d	   d	S 1 s|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&   r   r   r   &test_invalid_sys_executable_falls_backA   s2     P z>TestGetPythonExecutable.test_invalid_sys_executable_falls_backc                 C  (  t  }t|d }|d }|d d }|jdd |jdd |d }|jdd	 td
dA tdd, td}t||_t	 }| 
|t| W d   n1 sWw   Y  W d   n1 sfw   Y  W d   n1 suw   Y  W d   dS W d   dS 1 sw   Y  dS )zLTest fallback finds python3 in bin directory when sys.executable is invalid.python_installr   r   
python3.12Tparentsr	     moder!   r(   r-   /invalid/pythonsysconfig.get_pathNtempfileTemporaryDirectorypathlibPathmkdirtouchr   r   r1   r   r#   )r%   tmpdirmock_python_dirmock_bin_dirmock_include_dirmock_python3mock_get_pathr&   r   r   r   "test_fallback_finds_python3_in_binW   4   

  "z:TestGetPythonExecutable.test_fallback_finds_python3_in_binc                 C  r7   )zDTest fallback finds python (not python3) when python3 doesn't exist.r8   r   r   r9   Tr:   r
   r<   r=   r!   r(   r-   r?   r@   NrA   )r%   rH   rI   rJ   rK   mock_pythonrM   r&   r   r   r   !test_fallback_finds_python_in_binq   rO   z9TestGetPythonExecutable.test_fallback_finds_python_in_binc                 C  s  t ddu t ddW t dC}t d/}t d}d|_d	|_d	|_t }| |tj W d
   n1 s8w   Y  W d
   n1 sGw   Y  W d
   n1 sVw   Y  W d
   n1 sew   Y  W d
   d
S W d
   d
S 1 s}w   Y  d
S )z?Test that executable permission is verified for sys.executable.r!   r(   r-   z/path/to/pythonr.   r/   r0   TFN)r   r1   r   r#   r   r   r2   r   r   r    test_executable_permission_check   s2     P z8TestGetPythonExecutable.test_executable_permission_checkc                 C  sx   t  }| |t | t|d t dkr | |tj	 dS t
jtj	r8t
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$   r   r   r   test_real_environment   s   z-TestGetPythonExecutable.test_real_environmentc                 C  s   t ddM t }ttdr2ttdr:tjtjk}|rBtjtj	rJ| 
|tj	d W d   dS W d   dS W d   dS W d   dS W d   dS 1 sUw   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   rZ   r[   r   r   r   r   r#   )r%   r&   in_venvr   r   r   "test_virtual_environment_detection   s&   "z:TestGetPythonExecutable.test_virtual_environment_detectionrS   z0Fallback mechanism only applies to POSIX systemsc                 C  sx  t dd t d}t d|}t dh}t dT}t d@}t dd	d
 d( d|_d|_d|_d|_d|_ddd}||_t }| |d W d   n1 sRw   Y  W d   n1 saw   Y  W d   n1 spw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   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.
        r-   z/usr/bin/python-invalidr.   r/   r@   r0   zos.path.abspathzos.path.joinc                  W  s
   d | S )N/)r   )argsr   r   r   <lambda>   s   
 zKTestGetPythonExecutable.test_cpython_issue_84399_fallback.<locals>.<lambda>)side_effectFz/usr/include/python3.12Tz/usrr   r   r   boolc                 S  s   | dkS )N/usr/bin/python3r   )r   r   r   r   isfile_check   s   zOTestGetPythonExecutable.test_cpython_issue_84399_fallback.<locals>.isfile_checkrd   N)r   r   r   rc   )r   r1   rb   r   r#   )r%   r3   r4   rM   r5   mock_abspathre   r&   r   r   r   !test_cpython_issue_84399_fallback   sN   

    P z9TestGetPythonExecutable.test_cpython_issue_84399_fallbackN)r   r   )__name__
__module____qualname____doc__r'   r)   unittestskipIfr   rW   version_infor6   rN   rQ   rR   rY   r^   rX   rg   r   r   r   r   r   0   s.    






r   __main__)r   r   )
__future__r   r   rD   rW   r   r   rB   rl   unittest.mockr   r   TestCaser   rh   mainr   r   r   r   <module>   s   
 9