B
    n\$                 @   sP  d Z ddlmZmZ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ZddlZddlmZmZ ddlmZ ddlmZ yddlmZ W n  ek
r   ddlmZ Y nX 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 e&Z'dS )!z
Utility functions for

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

    )divisionabsolute_importprint_functionN)asbytesasstr)temppath)import_module)md5)newc            	   C   s^   t d k	rZytjt  W n tk
r,   Y nX ytt  W n ttfk
rT   Y nX d a d S )N)	_module_dirsyspathremove
ValueErrorshutilZrmtreeIOErrorOSError r   r   4lib/python3.7/site-packages/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   tempfileZmkdtempatexitregisterr   r   r   insertr   r   r   r   get_module_dir2   s    

r   c              C   sZ   t  } xFtddD ]8}d| }tj| |}|tjkrtj|d s|S qW tdd S )Ni  i z_test_ext_module_%dz.pyz(Failed to create a temporary module name)	r   rangeosr   joinr   modulesisfileRuntimeError)djnamefnr   r   r   get_temp_module_name<   s    r%   c                s   i  fdd} j |_ |S )Nc           
      sn   t | |f}|krTy | ||< W n, tk
rR } z||<  W d d }~X Y nX | }t|trj||S )N)repr	Exception
isinstance)akwkeyeZret)funcmemor   r   wrapperJ   s    
z_memoize.<locals>.wrapper)__name__)r-   r/   r   )r-   r.   r   _memoizeG   s    r1   c          
   C   s  dt tj }t }g }x| D ]}tj|s:td| tj|tj|}	t	
||	 ||	 tjtj|d}tj|rtj|tj|}	tj|	st	
||	 qW |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rjtd|dd t|f W dt| x|D ]}t| q~W X 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()z%s is not a filez.f2py_f2cmapNz-cz-mzskip:zonly:)stdoutstderrr   zRunning f2py failed: %s
%s   )r&   r   r   r   r   r   r    r   basenamer   copyfileappenddirnamer%   getcwdchdir
executable
subprocessPopenPIPESTDOUTcommunicate
returncoder   unlinkr   )source_filesoptionsskiponlymodule_namecoder!   dst_sourcesr$   dstZ	f2py_optscwdcmdpouterrr   r   r   build_module^   sH    






rP   c          
   C   sV   |dkrd}t |d6}t|d}||  W dQ R X t|g||||dS Q R X dS )z6
    Compile and import Fortran code using f2py.

    Nz.f)suffixw)rD   rE   rF   rG   )r   openwriterP   )Zsource_coderD   rE   rF   rQ   rG   r   fr   r   r   
build_code   s    rV   c           
   C   s   t d k	rt S da d} | tttjd } tddR}t|d}||  W d Q R X tj|dg}t	j
|t	jt	jd}| \}}W d Q R X td	|}|rtt|d
tt|dtt|dfa t S )N)FFFa7  
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.py)rQ   rR   Zconfig)r2   r3   s   COMPILERS:(\d+),(\d+),(\d+)         )_compiler_statusdictr&   r   r   r   rS   rT   r;   r<   r=   r>   r?   r@   researchboolintgroup)rH   scriptrU   rL   rM   rN   rO   mr   r   r   _get_compiler_status   s"    

 rd   c               C   s
   t  d S )Nr   )rd   r   r   r   r   has_c_compiler   s    re   c               C   s
   t  d S )NrX   )rd   r   r   r   r   has_f77_compiler   s    rf   c               C   s
   t  d S )NrY   )rd   r   r   r   r   has_f90_compiler   s    rg   c          
   K   s  ddl m} ddlm} t }g }xP| D ]H}tj|sDtd| tj	|tj
|}	t||	 ||	 q(W t|dd}dt|ttjd }
tj	|t d	 }|| t|d
}|t|
 |  t }zdt| tj|ddg}tj|tjtjd}|  \}}|j!dkrDtd|dd t"|f W dt| x|D ]}t#| qXW X t$| tj%| S )z6
    Build a module via distutils and import it.

    r   )Configuration)setupz%s is not a file
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_coderW   z.pywbZ	build_extz-i)r2   r3   z%Running distutils build failed: %s
%sr4   N)&Znumpy.distutils.misc_utilrh   Znumpy.distutils.coreri   r   r   r   r   r    r   r5   r   r6   r7   textwrapdedentreplacer\   r&   r   r%   rS   rT   r   closer9   r:   r;   r<   r=   r>   r?   r@   rA   r   rB   
__import__r   )rC   rk   rG   r*   rh   ri   r!   rI   r$   rJ   rH   rb   rU   rK   rL   rM   rN   rO   r   r   r   build_module_distutils   s@    







rr   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 )F2PyTestNz.fc             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}x*|D ]"}|drd}ql|drld}qlW |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 )NZwin32z)Fails with MinGW64 Gfortran (Issue #9673)zNo C compiler availableFz.fTz.f90z No Fortran 77 compiler availablez No Fortran 90 compiler available)rD   rE   rF   rQ   rG   )rD   rE   rF   rG   )r   platformpytestrE   modulere   sourcesextendrH   r7   rQ   endswithrf   rg   rV   rD   rF   rG   rP   )selfZcodesZ	needs_f77Z	needs_f90r$   r   r   r   ri   A  s>    














zF2PyTest.setup)r0   
__module____qualname__rH   rw   rD   rE   rF   rQ   rv   rG   ri   r   r   r   r   rs   7  s   rs   )(__doc__Z
__future__r   r   r   r   r   r<   r   r   r   rm   r]   ru   Znumpy.compatr   r   Znumpy.testingr   	importlibr   Zhashlibr	   ImportErrorr
   r   r   r   r%   r1   rP   rV   r[   rd   re   rf   rg   rr   objectrs   r   r   r   r   <module>   sD   
91H