U
    Y	at%                     @   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   s^   t d k	rZztjt  W n tk
r,   Y nX ztt  W n ttfk
rT   Y nX d a d S N)	_module_dirsyspathremove
ValueErrorshutilrmtreeIOErrorOSError r   r   9/tmp/pip-unpacked-wheel-zbzmve17/numpy/f2py/tests/util.py_cleanup   s    r   c                   C   s6   t d kr2t a tt t tjk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k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               
      sn   t | |f}|krTz | ||< W n, tk
rR } z||<  W 5 d }~X Y nX | }t|trj||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   sj  dt tj }t }g }g }| D ]j}	tj|	s<td|	 tj|tj|	}
t	
|	|
 ||
 tj|
\}}|dkr ||
 q |dkrt }dd|g| | }|r|dg| 7 }|r|dg| 7 }t }zft| tjd|g| }tj|tjtjd	}| \}}|jd
kr<td|dd t|f W 5 t| |D ]}	t|	 qNX 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unlink
executable
subprocessPopenPIPESTDOUTcommunicate
returncoder   r   )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   sL    




rW   c              
   C   sb   |dkrd}t |dB}t|d}||  W 5 Q R X t|g||||dW  5 Q R  S Q R X dS )z6
    Compile and import Fortran code using f2py.

    Nr1   )suffixwrH   rI   rJ   rK   )r   openwriterW   )Zsource_coderH   rI   rJ   rX   rK   r	   fr   r   r   
build_code   s    r^   c            	   	   C   s   t d k	rt S da td} | tttjd } t }z`tj|d}t|d}||  W 5 Q R X tjddg}tj|tjtj|d}| \}}W 5 t	
| X td|}|rt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.pyrY   config)r3   r4   rR   s   COMPILERS:(\d+),(\d+),(\d+)r         )_compiler_statustextwrapdedentdictr!   r   r	   r   r   r   r   r6   r8   r[   r\   r@   rA   rB   rC   rD   rE   researchboolintgroup)	rL   Ztmpdirscriptr]   rS   rT   rU   rV   mr   r   r   _get_compiler_status   s.    

 rn   c                   C   s
   t  d S r   rn   r   r   r   r   has_c_compiler   s    rp   c                   C   s
   t  d S )Nr   ro   r   r   r   r   has_f77_compiler   s    rq   c                   C   s
   t  d S )Nra   ro   r   r   r   r   has_f90_compiler   s    rr   c              
   K   s  ddl m} ddlm} t }g }| D ]H}tj|sBt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 5 Q R X t }zdt| tj|ddg}tj|tjtjd}|  \}}|j!dkrJtd|dd t"|f W 5 t| |D ]}t| q\X t#| tj$| S )z6
    Build a module via distutils and import it.

    r   )Configuration)setupr/   
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wb	build_extz-ir2   z%Running distutils build failed: %s
%sr5   N)%Znumpy.distutils.misc_utilrs   Znumpy.distutils.corert   r   r6   r	   r7   r   r8   r9   r   r:   r;   rd   re   replacerf   r!   r   r    r[   r\   r   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   r   
__import__r   )rG   rv   rK   r%   rs   rt   r   rM   rN   rO   rL   rl   r]   rR   rS   rT   rU   rV   r   r   r   build_module_distutils   sF    




r{   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 )F2PyTestNr1   c                 C   s  t jdkrtd | jd k	r"d S t s2td g }| jrH|| j | jd k	r^|	| j
 d}d}|D ]"}|dr~d}qj|drjd}qj|rt std |rt std	 | jd k	rt| j| j| j| j| j
| jd
| _| jd k	rt| j| j| j| j| jd| _d S )Nwin32z)Fails with MinGW64 Gfortran (Issue #9673)zNo C compiler availableFr1   Tr0   z No Fortran 77 compiler availablez No Fortran 90 compiler available)rH   rI   rJ   rX   rK   rZ   )r   platformpytestrI   modulerp   sourcesextendrL   r;   rX   endswithrq   rr   r^   rH   rJ   rK   rW   )selfcodesZ	needs_f77Z	needs_f90rN   r   r   r   rt   @  sF    












 
 zF2PyTest.setup)r-   
__module____qualname__rL   r   rH   rI   rJ   rX   r   rK   rt   r   r   r   r   r|   6  s   r|   ) __doc__r6   r   rA   r   r   r   rd   rg   r   Znumpy.compatr   r   Znumpy.testingr   	importlibr   r   r   r   r   r    r.   rW   r^   rc   rn   rp   rq   rr   r{   r|   r   r   r   r   <module>   s@   
87
F