a
    cDa%                     @   s  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
mZmZ ddl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S )z
Utility functions for

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

    N)asbytesasstr)temppath)import_modulei  c                   C   sV   t d urRztjt  W n ty*   Y n0 ztt  W n tyL   Y n0 d a d S N)_module_dirsyspathremove
ValueErrorshutilrmtreeOSError r   r   e/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/numpy/f2py/tests/util.py_cleanup   s    r   c                   C   s6   t d u r2t a tt t tjvr2tjdt  t S Nr   )	r   tempfilemkdtempatexitregisterr   r   r	   insertr   r   r   r   get_module_dir,   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)dnamer   r   r   get_temp_module_name6   s    
r   c                    s   i  fdd} j |_ |S )Nc               
      st   t | |f}|vrZz | i ||< W n. tyX } z||<  W Y d }~n
d }~0 0 | }t|trp||S r   )repr	Exception
isinstance)akwkeyeretfuncmemor   r   wrapperE   s    
z_memoize.<locals>.wrapper)__name__)r)   r+   r   r(   r   _memoizeB   s    r-   c              
   C   s  dt tj }t }g }g }| D ]j}	tj|	s<td|	 tj|tj|	}
t	
|	|
 ||
 tj|
\}}|dv r ||
 q |du rt }dd|g| | }|r|dg| 7 }|r|dg| 7 }t }zt| tjd|g| }tj|tjtjd	}| \}}|jd
kr<td|dd t|f W t| |D ]}	t|	 qLn"t| |D ]}	t|	 qn0 t|S )zH
    Compile and import a f2py module, built from the given files.

    zEimport sys; sys.path = %s; import numpy.f2py as f2py2e; f2py2e.main()%s is not a file).f90.fz.cz.pyfNz-cz-mzskip:zonly:stdoutstderrr   zRunning f2py failed: %s
%s   )r    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coder   dst_sourcesZf2py_sourcesfndstbaseextZ	f2py_optscwdcmdpouterrr   r   r   build_moduleY   sR    




rV   c              	   C   s   |du rd}t |dX}t|d}||  W d   n1 sB0    Y  t|g||||dW  d   S 1 st0    Y  dS )z6
    Compile and import Fortran code using f2py.

    Nr0   )suffixwrG   rH   rI   rJ   )r   openwriterV   )Zsource_coderG   rH   rI   rW   rJ   r	   fr   r   r   
build_code   s    (r]   c            	   	   C   s  t d urt S da td} | tttjd } t }z~t	j
|d}t|d}||  W d    n1 sp0    Y  tjddg}tj|tjtj|d}| \}}W t| nt| 0 td|}|rtt|d	tt|d
tt|dfa t S )N)FFFa          import os
        import sys
        sys.path = %(syspath)s

        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.pyrX   config)r2   r3   rQ   s   COMPILERS:(\d+),(\d+),(\d+)r         )_compiler_statustextwrapdedentdictr    r   r	   r   r   r5   r7   rZ   r[   r>   r?   r@   rA   rB   rC   r   r   researchboolintgroup)	rK   Ztmpdirscriptr\   rR   rS   rT   rU   mr   r   r   _get_compiler_status   s.    
(
 rm   c                   C   s
   t  d S r   rm   r   r   r   r   has_c_compiler   s    ro   c                   C   s
   t  d S )Nr   rn   r   r   r   r   has_f77_compiler   s    rp   c                   C   s
   t  d S )Nr`   rn   r   r   r   r   has_f90_compiler   s    rq   c              
   K   s  t  }g }| D ]H}tj|s*td| tj|tj|}t|| |	| qt
|dd}t
dt|ttjd }tj|t d }	|	|	 t|	d}
|
t| W d   n1 s0    Y  t }zt| tj|	d	d
g}tj|tjtjd}| \}}|jdkrFtd|dd t|f W t| |D ]}t| qVn"t| |D ]}t| qx0 t| tj | S )z6
    Build a module via distutils and import it.

    r.   
z
    a          import os
        import sys
        sys.path = %(syspath)s

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

        if __name__ == "__main__":
            from numpy.distutils.core import setup
            setup(configuration=configuration)
        )config_coder^   z.pywbN	build_extz-ir1   r   z%Running distutils build failed: %s
%sr4   )!r   r5   r	   r6   r   r7   r8   r   r9   r:   rc   rd   replacere   r    r   r   rZ   r[   r   r<   r=   r>   r?   r@   rA   rB   rC   rD   r   rE   
__import__r   )rF   rs   rJ   r$   r   rL   rM   rN   rK   rk   r\   rQ   rR   rS   rT   rU   r   r   r   build_module_distutils   sH    
,



rx   c                   @   s4   e Zd ZdZdZg Zg Zg ZdZdZ	dZ
dd ZdS )F2PyTestNr0   c                 C   s  t jdkrtd | jd ur"d S t s2td g }| jrH|| j | jd ur^|	| j
 d}d}|D ]"}|dr~d}qj|drjd}qj|rt std |rt std	 | jd urt| j| j| j| j| j
| jd
| _| jd urt| j| j| j| j| jd| _d S )Nwin32z)Fails with MinGW64 Gfortran (Issue #9673)zNo C compiler availableFr0   Tr/   z No Fortran 77 compiler availablez No Fortran 90 compiler available)rG   rH   rI   rW   rJ   rY   )r   platformpytestrH   modulero   sourcesextendrK   r:   rW   endswithrp   rq   r]   rG   rI   rJ   rV   )selfcodesZ	needs_f77Z	needs_f90rM   r   r   r   setup=  sB    













zF2PyTest.setup)r,   
__module____qualname__rK   r~   rG   rH   rI   rW   r}   rJ   r   r   r   r   r   ry   3  s   ry   ) __doc__r5   r   r?   r   r   r   rc   rf   r|   Znumpy.compatr   r   Znumpy.testingr   	importlibr   r   r   r   r   r   r-   rV   r]   rb   rm   ro   rp   rq   rx   ry   r   r   r   r   <module>   s@   
87
C