3
Kd%                 @   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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_module)md5i  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   I/var/www/html/virt/lib64/python3.6/site-packages/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_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_name8   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   wrapperG   s    
z_memoize.<locals>.wrapper)__name__)r(   r*   r   )r(   r)   r   _memoizeD   s    r,   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rt }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   )r-   r.   r/   r0   )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    






rU   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)rF   rG   rH   rI   )r   openwriterU   )Zsource_coderF   rG   rH   rV   rI   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|}|rt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.pyrW   config)r1   r2   rP   s   COMPILERS:(\d+),(\d+),(\d+)r         )FFF)_compiler_statustextwrapdedentdictr    r   r	   r   r   r4   r6   rX   rY   r=   r>   r?   r@   rA   rB   r   r   researchboolintgroup)	rJ   ZtmpdirscriptrZ   rQ   rR   rS   rT   mr   r   r   _get_compiler_status   s,    
 rk   c               C   s
   t  d S )Nr   )rk   r   r   r   r   has_c_compiler   s    rl   c               C   s
   t  d S )Nr   )rk   r   r   r   r   has_f77_compiler   s    rm   c               C   s
   t  d S )Nr^   )rk   r   r   r   r   has_f90_compiler   s    rn   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|
 W dQ R X tj }zdtj| tj|ddg}tj|tjtjd}|j \}}|j dkrNtd|dd t!|f W dtj| x|D ]}tj"| qbW 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wbN	build_extz-i)r1   r2   z%Running distutils build failed: %s
%sr3   )%Znumpy.distutils.misc_utilro   Znumpy.distutils.corerp   r   r4   r	   r5   r   r6   r7   r   r8   r9   ra   rb   replacerc   r    r   r   rX   rY   r   r;   r<   r=   r>   r?   r@   rA   rB   rC   r   rD   
__import__r   )rE   rr   rI   r$   ro   rp   r   rK   rL   rM   rJ   ri   rZ   rP   rQ   rR   rS   rT   r   r   r   build_module_distutils   s@    






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rd}ql|jdrld}qlW |rt  rtjd |rt  rtj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 availableFz.fTz.f90z No Fortran 77 compiler availablez No Fortran 90 compiler available)rF   rG   rH   rV   rI   )rF   rG   rH   rI   )r   platformpytestrG   modulerl   sourcesextendrJ   r9   rV   endswithrm   rn   r[   rF   rH   rI   rU   )selfcodesZ	needs_f77Z	needs_f90rL   r   r   r   rp   B  s>    












zF2PyTest.setup)r+   
__module____qualname__rJ   r}   rF   rG   rH   rV   r|   rI   rp   r   r   r   r   rx   8  s   rx   )"__doc__r4   r   r>   r   r   r   ra   rd   r{   Znumpy.compatr   r   Znumpy.testingr   	importlibr   hashlibr   r   r   r   r   r   r,   rU   r[   r`   rk   rl   rm   rn   rw   rx   r   r   r   r   <module>   s>   
87G