a
    �¥»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   sZ   t d urVztj t ¡ W n ty*   Y n0 zt t ¡ W n ttfyP   Y n0 d a d S ©N)	Ú_module_dirÚsysÚpathÚremoveÚ
ValueErrorÚshutilÚrmtreeÚIOErrorÚOSError© r   r   úO/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numpy/f2py/tests/util.pyÚ_cleanup   s    r   c                   C   s6   t d u r2t ¡ a t t¡ t tjv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v 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               
      st   t | |fƒ}|ˆvrZzˆ | i |¤Žˆ|< W n. tyX } z|ˆ|< ‚ W Y d }~n
d }~0 0 ˆ| }t|tƒrp|‚|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   sŠ  dt tjƒ }tƒ }g }g }| D ]j}	tj |	¡s<td|	 ƒ‚tj |tj |	¡¡}
t	 
|	|
¡ | |
¡ tj |
¡\}}|dv r | |
¡ q |du rštƒ }dd|g| | }|r¾|dg| 7 }|rÐ|dg| 7 }t ¡ }z†t |¡ tjd|g| }tj|tjtjd	�}| ¡ \}}|jd
k�r<td|dd… t|ƒf ƒ‚W t |¡ |D ]}	t |	¡ �qLn"t |¡ |D ]}	t |	¡ �qn0 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Ú
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_moduleY   sR    ÿ


ÿÿ
ü
rW   c              	   C   s‚   |du rd}t |d��X}t|dƒ�}| | ¡ W d  ƒ n1 sB0    Y  t|g||||d�W  d  ƒ S 1 st0    Y  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 urt S da t d¡} | tttjƒd� } t ¡ }z~t	j 
|d¡}t|dƒ�}| | ¡ W d   ƒ n1 sp0    Y  tjddg}tj|tjtj|d�}| ¡ \}}W t |¡ nt |¡ 0 t d|¡}|�rt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   r6   r8   r[   r\   r?   r@   rA   rB   rC   rD   r   r   Ú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 d  ƒ n1 sê0    Y  t ¡ }z„t |¡ tj|ddg}tj|tjtjd�}| ¡ \}}|j dk�r^td|dd… t!|ƒf ƒ‚W t |¡ |D ]}t "|¡ �qnn"t |¡ |D ]}t "|¡ �q�0 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ÚwbNÚ	build_extz-ir2   z%Running distutils build failed: %s
%sr5   )%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   r   rF   Ú
__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ï   sL    ò
,

ÿÿ
ü
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 ur"d S tƒ s2t d¡ g }| jrH| | j¡ | jd ur^| 	| j
¡ d}d}|D ]"}| d¡r~d}qj| d¡rjd}qj|r¢tƒ s¢t d¡ |r¶tƒ s¶t d	¡ | jd urât| j| j| j| j| j
| jd
�| _| jd u�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   @  sB    












ý
þ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   r@   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