3
eåU^$&  ã               @   sT  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	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)Únewi  c               C   s^   t d k	rZytjjt ƒ W n tk
r,   Y nX ytj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   ú6/tmp/pip-build-fou5mlat/numpy/numpy/f2py/tests/util.pyÚ_cleanup%   s    r   c               C   s6   t d kr2tjƒ a tjtƒ t tjkr2tjjdt ƒ t S )Nr   )	r   ÚtempfileÚmkdtempÚatexitÚregisterr   r   r   Úinsertr   r   r   r   Úget_module_dir3   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_name=   s    
r#   c                s   i ‰‡ ‡fdd„}ˆ j |_ |S )Nc                 sn   t | |fƒ}|ˆkrTyˆ | |Žˆ|< W n, tk
rR } z|ˆ|< ‚ W Y d d }~X nX ˆ| }t|tƒrj|‚|S )N)ÚreprÚ	ExceptionÚ
isinstance)ÚaÚkwÚkeyÚeÚret)ÚfuncÚmemor   r   ÚwrapperL   s    
z_memoize.<locals>.wrapper)Ú__name__)r,   r.   r   )r,   r-   r   Ú_memoizeI   s    r0   c             C   sr  dt tjƒ }tƒ }g }g }xr| D ]j}	tjj|	ƒs>td|	 ƒ‚tjj|tjj|	ƒƒ}
t	j
|	|
ƒ |j|
ƒ tjj|
ƒ\}}|dkr"|j|
ƒ q"W |dkržtƒ }dd	|g| | }|rÂ|d
g| 7 }|rÔ|dg| 7 }tjƒ }zftj|ƒ tjd|g| }tj|tjtjd�}|jƒ \}}|jdk�r@td|dd… t|ƒf ƒ‚W dtj|ƒ x|D ]}	tj|	ƒ �qTW 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 fileú.f90ú.fú.cú.pyfNz-cz-mzskip:zonly:)ÚstdoutÚstderrr   zRunning f2py failed: %s
%sé   )r1   r2   r3   r4   )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_module`   sF    






rY   c             C   sV   |dkrd}t |d��6}t|dƒ�}|j| ƒ 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)rJ   rK   rL   rM   )r   ÚopenÚwriterY   )Zsource_coderJ   rK   rL   rZ   rM   r   Úfr   r   r   Ú
build_code™   s    r_   c        	      C   sì   t d k	rt S da tjdƒ} | tttjƒd� } tjƒ }z`t	jj
|dƒ}t|dƒ�}|j| ƒ W d Q R X tjddg}tj|tjtj|d�}|jƒ \}}W d tj|ƒ X tjd|ƒ}|rètt|jd	ƒƒƒtt|jd
ƒƒƒtt|jdƒƒƒfa t S )NFa¾          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.pyr[   Úconfig)r5   r6   rT   s   COMPILERS:(\d+),(\d+),(\d+)r   é   é   )FFF)Ú_compiler_statusÚtextwrapÚdedentÚdictr$   r   r   r   r   r8   r:   r\   r]   rA   rB   rC   rD   rE   rF   r   r   ÚreÚsearchÚboolÚintÚgroup)	rN   ZtmpdirÚscriptr^   rU   rV   rW   rX   Úmr   r   r   Ú_get_compiler_status¯   s,    
 ro   c               C   s
   t ƒ d S )Nr   )ro   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 )Nrb   )ro   r   r   r   r   Úhas_f90_compilerî   s    rr   c             K   s†  ddl m} ddlm} tƒ }g }xP| D ]H}tjj|ƒsDtd| ƒ‚tjj	|tjj
|ƒƒ}	tj||	ƒ |j|	ƒ q(W tj|ƒjddƒ}tjdƒt|ttjƒd� }
tjj	|tƒ d	 ƒ}|j|ƒ t|d
ƒ}|jt|
ƒƒ |jƒ  tjƒ }zdtj|ƒ tj|ddg}tj|tjtjd�}|j ƒ \}}|j!dk�rJtd|dd… t"|ƒf ƒ‚W dtj|ƒ x|D ]}tj#|ƒ �q^W 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_coder`   z.pyÚwbÚ	build_extz-i)r5   r6   z%Running distutils build failed: %s
%sr7   N)&Znumpy.distutils.misc_utilrs   Znumpy.distutils.corert   r   r8   r   r9   r    r:   r;   r   r<   r=   re   rf   Úreplacerg   r$   r   r#   r\   r]   r   Úcloser?   r@   rA   rB   rC   rD   rE   rF   rG   r   rH   Ú
__import__r   )rI   rv   rM   r(   rs   rt   r!   rO   rP   rQ   rN   rm   r^   rT   rU   rV   rW   rX   r   r   r   Úbuild_module_distutilsö   sB    







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 )ÚF2PyTestNz.fc             C   s  t jdkrtjdƒ | jd k	r"d S tƒ s2tjdƒ g }| jrH|j| jƒ | jd k	r^|j	| j
ƒ d}d}x*|D ]"}|jdƒr€d}ql|jdƒrld}qlW |r¨tƒ  r¨tjdƒ |r¾tƒ  r¾tjd	ƒ | jd k	rêt| 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 availableFz.fTz.f90z No Fortran 77 compiler availablez No Fortran 90 compiler available)rJ   rK   rL   rZ   rM   )rJ   rK   rL   rM   )r   ÚplatformÚpytestrK   Úmodulerp   ÚsourcesÚextendrN   r=   rZ   Úendswithrq   rr   r_   rJ   rL   rM   rY   )ÚselfÚcodesZ	needs_f77Z	needs_f90rP   r   r   r   rt   H  s>    












zF2PyTest.setup)r/   Ú
__module__Ú__qualname__rN   r‚   rJ   rK   rL   rZ   r�   rM   rt   r   r   r   r   r}   >  s   r}   ))Ú__doc__Ú
__future__r   r   r   r8   r   rB   r   r   r   re   rh   r€   Znumpy.compatr   r   Znumpy.testingr   Ú	importlibr   Úhashlibr	   ÚImportErrorr
   r   r   r   r   r#   r0   rY   r_   rd   ro   rp   rq   rr   r|   Úobjectr}   r   r   r   r   Ú<module>   sF   
87H