o
    h(                     @   sB  d 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
Z
ddlZddlmZ ddlmZmZ ddlmZmZ ddlmZ dadadd	 Zd
d Zdd Zdd Zeg g g dfddZeg g g ddfddZdadd Zdd Z dd Z!dd Z"edd Z#G dd dZ$d d! Z%e
j&d"d# Z'dS )$z
Utility functions for

- building and importing modules on test time, using a temporary location
- detecting if compilers are present
- determining paths to tests

    N)Path)asbytesasstr)temppathIS_WASM)import_modulei  c                   C   sZ   t d ur+ztjt  W n	 ty   Y nw ztt  W n	 ty&   Y nw d a d S d S N)_module_dirsyspathremove
ValueErrorshutilrmtreeOSError r   r   P/var/www/html/scripts/venv/lib/python3.10/site-packages/numpy/f2py/tests/util.py_cleanup"   s   r   c                   C   s6   t d u rt a tt t tjvrtjdt  t S Nr   )	r	   tempfilemkdtempatexitregisterr   r
   r   insertr   r   r   r   get_module_dir0   s   

r   c                  C   s,   t   dt } td7 a| tjv rtd| S )Nz_test_ext_module_%d   z%Temporary module name already in use.)r   _module_numr
   modulesRuntimeError)namer   r   r   get_temp_module_name:   s   
r    c                    s   i  fdd} j |_ |S )Nc               
      sh   t | |f}|vr'z | i ||< W n ty& } z||<  d }~ww | }t|tr2||S r   )repr	Exception
isinstance)akwkeyeretfuncmemor   r   wrapperI   s   
z_memoize.<locals>.wrapper)__name__)r*   r,   r   r)   r   _memoizeF   s   r.   c              
   C   s  dt jd}t }g }g }| D ]5}	tj|	std|	 tj|tj|	}
t	|	|
 |
|
 tj|
\}}|dv rE|
|
 q|sJJ |du rQt }dd|g| | }|rc|dg| 7 }|rl|d	g| 7 }t }zDt| t jd|g| }tj|tjtjd
}| \}}|jdkrtd|dd t|f W t| |D ]}	t|	 qt|S t| |D ]}	t|	 qw )zH
    Compile and import a f2py module, built from the given files.

    zimport sys; sys.path = z&; import numpy.f2py; numpy.f2py.main()%s is not a file).f90.fz.c.pyfNz-cz-mzskip:zonly:stdoutstderrr   zRunning f2py failed: %s
%s   )r
   r   r   osisfiler   joinbasenamer   copyfileappendsplitextr    getcwdchdir
executable
subprocessPopenPIPESTDOUTcommunicate
returncoder   unlinkr   )source_filesoptionsskiponlymodule_namecodeddst_sourcesf2py_sourcesfndstbaseext	f2py_optscwdcmdpouterrr   r   r   build_module_   sX   





r[   c              	   C   s   |du rd}t |d,}t|d}||  W d   n1 s!w   Y  t|g||||dW  d   S 1 s:w   Y  dS )z6
    Compile and import Fortran code using f2py.

    Nr1   )suffixwrI   rJ   rK   rL   )r   openwriter[   )source_coderI   rJ   rK   r\   rL   r   fr   r   r   
build_code   s   $rc   c            	   	   C   s   t d urt S da trt S tdttj d} | tttjd } t	 }z?t
j|d}t|d}||  W d    n1 sCw   Y  tjddg}tj|tjtj|d}| \}}W t| nt| w td	|}|rtt|d
tt|dtt|dfa t S )N)FFFz8        import os
        import sys
        sys.path = a{  

        def configuration(parent_name='',top_path=None):
            global config
            from numpy.distutils.misc_util import Configuration
            config = Configuration('', parent_name, top_path)
            return config

        from numpy.distutils.core import setup
        setup(configuration=configuration)

        config_cmd = config.get_config_cmd()
        have_c = config_cmd.try_compile('void foo() {}')
        print('COMPILERS:%%d,%%d,%%d' %% (have_c,
                                          config.have_f77c(),
                                          config.have_f90c()))
        sys.exit(99)
        )syspathzsetup.pyr]   config)r4   r5   rV   s   COMPILERS:(\d+),(\d+),(\d+)r         )_compiler_statusr   textwrapdedentr!   r
   r   dictr   r   r7   r9   r_   r`   r@   rA   rB   rC   rD   rE   r   r   researchboolintgroup)	rM   tmpdirscriptrb   rW   rX   rY   rZ   mr   r   r   _get_compiler_status   s<   
rt   c                   C   
   t  d S r   rt   r   r   r   r   has_c_compiler      
rw   c                   C   ru   )Nr   rv   r   r   r   r   has_f77_compiler   rx   ry   c                   C   ru   )Nrf   rv   r   r   r   r   has_f90_compiler   rx   rz   c              
   K   s  t  }g }| D ]$}tj|std| tj|tj|}t|| |	| qt
|dd}dttj d| d}tj|t d }	|	|	 t|	d}
|
t| W d	   n1 sgw   Y  t }z@t| tj|	d
dg}tj|tjtjd}| \}}|jdkrtd|dd	 t|f W t| |D ]}t| qnt| |D ]}t| qw t| tj| S )z6
    Build a module via distutils and import it.

    r/   
z
    z!
import os
import sys
sys.path = z

def configuration(parent_name='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('', parent_name, top_path)
    z
    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(configuration=configuration)
    z.pywbN	build_extz-ir3   r   z%Running distutils build failed: %s
%sr6   ) r   r7   r   r8   r   r9   r:   r   r;   r<   ri   rj   replacer!   r
   r    r_   r`   r   r>   r?   r@   rA   rB   rC   rD   rE   rF   r   rG   
__import__r   )rH   config_coderL   r%   rN   rO   rQ   rR   rM   rr   rb   rV   rW   rX   rY   rZ   r   r   r   build_module_distutils  sV   





r   c                   @   s<   e Zd ZdZdZg Zg Zg ZdZdZ	e
dd Zdd ZdS )F2PyTestNr1   c                 C   s*   t | }d|jddd  d|j dS )N_.r   _ext_module)type
__module__rsplitr-   )selfclsr   r   r   rL   U  s   "zF2PyTest.module_namec                 C   sL  t jdkr
td | jd urd S t std g }| jr$|| j | jd ur/|	| j
 d}d}d}|D ]}t|drCd}q7t|drMd}q7t|drVd}q7|rat satd	 |rkt sktd
 |rxt sxt sxtd | jd urt| j| j| j| j| j
| jd| _| jd urt| j| j| j| j| jd| _d S d S )Nwin32z)Fails with MinGW64 Gfortran (Issue #9673)zNo C compiler availableFr1   Tr0   r2   z No Fortran 77 compiler availablez No Fortran 90 compiler availablezNo Fortran compiler available)rI   rJ   rK   r\   rL   r^   )r
   platformpytestrJ   modulerw   sourcesextendrM   r<   r\   strendswithry   rz   rc   rI   rK   rL   r[   )r   codes	needs_f77	needs_f90	needs_pyfrQ   r   r   r   setup_methodZ  s\   











	zF2PyTest.setup_method)r-   r   __qualname__rM   r   rI   rJ   rK   r\   r   propertyrL   r   r   r   r   r   r   L  s    
r   c                  G   s   t tjjj }|j|  S r   )r   numpyf2py__file__parentresolvejoinpath)r$   rN   r   r   r   getpath  s   
r   c              	   c   s8    t  }t|  zd V  W t| d S t| w r   )r   rV   r7   r?   )r   curpathr   r   r   	switchdir  s   
r   )(__doc__r7   r
   rA   r   r   r   ri   rl   r   
contextlibr   pathlibr   numpy.compatr   r   numpy.testingr   r   	importlibr   r	   r   r   r   r    r.   r[   rc   rh   rt   rw   ry   rz   r   r   r   contextmanagerr   r   r   r   r   <module>   sT    
:>	
DJ