3
ßL]ç%  ã               @   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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   ú8/tmp/pip-install-q_7g0qgj/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_dir2   s    

r   c              C   s\   t ƒ } xHtddƒD ]:}d| }tjj| |ƒ}|tjkrtjj|d ƒ r|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 Y d d }~X nX ˆ| }t|tƒrj|‚|S )N)ÚreprÚ	ExceptionÚ
isinstance)ÚaÚkwÚkeyÚeÚret)ÚfuncÚmemor   r   ÚwrapperJ   s    
z_memoize.<locals>.wrapper)Ú__name__)r0   r2   r   )r0   r1   r   Ú_memoizeG   s    r4   c             C   sž  dt tjƒ }tƒ }g }x | D ]˜}tjj|ƒs:td| ƒ‚tjj|tjj|ƒƒ}	t	j
||	ƒ |j|	ƒ tjjtjj|ƒdƒ}tjj|ƒrtjj|tjj|ƒƒ}	tjj|	ƒst	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�rltd|dd… t|ƒf ƒ‚W dtj|ƒ x|D ]}tj|ƒ �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    






rS   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)rG   rH   rI   rJ   )r   ÚopenÚwriterS   )Zsource_coderG   rH   rI   rT   rJ   r   Úfr   r   r   Ú
build_code˜   s    rY   c              C   sØ   t d k	rt S da tjdƒ} | tttjƒd� } tdd��R}t|dƒ�}|j	| ƒ W d Q R X tj
|dg}tj|tjtjd�}|jƒ \}}W d Q R 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.py)rT   rU   Úconfig)r5   r6   s   COMPILERS:(\d+),(\d+),(\d+)é   é   é   )FFF)Ú_compiler_statusÚtextwrapÚdedentÚdictr(   r   r   r   rV   rW   r>   r?   r@   rA   rB   rC   ÚreÚsearchÚboolÚintÚgroup)rK   ÚscriptrX   rO   rP   rQ   rR   Úmr   r   r   Ú_get_compiler_status®   s$    

 rj   c               C   s
   t ƒ d S )Nr   )rj   r   r   r   r   Úhas_c_compilerß   s    rk   c               C   s
   t ƒ d S )Nr\   )rj   r   r   r   r   Úhas_f77_compilerã   s    rl   c               C   s
   t ƒ d S )Nr]   )rj   r   r   r   r   Úhas_f90_compilerç   s    rm   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_coderZ   z.pyÚwbÚ	build_extz-i)r5   r6   z%Running distutils build failed: %s
%sr7   N)&Znumpy.distutils.misc_utilrn   Znumpy.distutils.corero   r   r   r   r!   r"   r   r8   r   r9   r:   r`   ra   Úreplacerb   r(   r   r'   rV   rW   r   Úcloser<   r=   r>   r?   r@   rA   rB   rC   rD   r   rE   Ú
__import__r    )rF   rq   rJ   r,   rn   ro   r#   rL   r&   rM   rK   rh   rX   rN   rO   rP   rQ   rR   r   r   r   Úbuild_module_distutilsï   sB    







rw   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)rG   rH   rI   rT   rJ   )rG   rH   rI   rJ   )r   ÚplatformÚpytestrH   Úmodulerk   ÚsourcesÚextendrK   r:   rT   Úendswithrl   rm   rY   rG   rI   rJ   rS   )ÚselfÚcodesZ	needs_f77Z	needs_f90r&   r   r   r   ro   A  s>    












zF2PyTest.setup)r3   Ú
__module__Ú__qualname__rK   r}   rG   rH   rI   rT   r|   rJ   ro   r   r   r   r   rx   7  s   rx   )(Ú__doc__Ú
__future__r   r   r   r   r   r?   r   r   r   r`   rc   r{   Znumpy.compatr   r   Znumpy.testingr   Ú	importlibr   Úhashlibr	   ÚImportErrorr
   r   r   r   r'   r4   rS   rY   r_   rj   rk   rl   rm   rw   Úobjectrx   r   r   r   r   Ú<module>   sD   
91H