a
    ç´«jP-  ã                   @   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
Z
ddlZddlZddlZddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZ dadaejd	krÚeeƒjjjZe e d
¡ƒZ!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-dd„Z(dZ)dZ*G dd„ dƒZ+e�s\e+ƒ Z,e, -¡  dd„ Z.dd „ Z/d!d"„ Z0G d#d$„ d$eƒZ1d.d%d&„Z2G d'd(„ d(ƒZ3d)d*„ Z4ej5d+d,„ ƒZ6dS )/z¥
Utility functions for

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

é    N)ÚPath)Ú	asunicode)ÚtemppathÚIS_WASM)Úimport_module)ÚMesonBackendi  Úcygwinz**/*.dllc                   C   sV   t d urRztj t ¡ W n ty*   Y n0 zt t ¡ W n tyL   Y n0 d a d S ©N)Ú_module_dirÚsysÚpathÚremoveÚ
ValueErrorÚshutilÚrmtreeÚOSError© r   r   úT/var/www/html/laksyashrimp/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_dir7   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)Únamer   r   r   Úget_temp_module_nameA   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   ÚwrapperP   s    
z_memoize.<locals>.wrapper)Ú__name__)r*   r,   r   r)   r   Ú_memoizeM   s    r.   c              
   C   sÜ  dt j›d�}tƒ }g }g }| D ]j}	tj |	¡s<td|	 ƒ‚tj |tj |	¡¡}
t 	|	|
¡ | 
|
¡ tj |
¡\}}|dv r | 
|
¡ q |s”J ‚|du r¢tƒ }dd|g| | }|dd	g7 }|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�rPtd|dd… t|ƒf ƒ‚W t |¡ |D ]}	t |	¡ �q`n"t |¡ |D ]}	t |	¡ �q‚0 t jdk�rÔt t tj |d |¡¡¡¡ t g d¢t ¡ t|ƒS )zH
    Compile and import a f2py module, built from the given files.

    zimport sys; sys.path = z&; import numpy.f2py; numpy.f2py.main()z%s is not a file)ú.f90z.f95ú.fz.cú.pyfNz-cz-mz	--backendÚmesonzskip:zonly:)ÚstdoutÚstderrr   zRunning f2py failed: %s
%sé   r   z{:s}*)z/usr/bin/rebasez
--databasez--obliviousz	--verbose)r   r   r   ÚosÚisfiler   ÚjoinÚbasenamer   ÚcopyfileÚappendÚsplitextr    ÚgetcwdÚchdirÚ
executableÚ
subprocessÚPopenÚPIPEÚSTDOUTÚcommunicateÚ
returncoder   ÚunlinkÚplatformÚ_module_listÚextendÚglobÚformatÚ
check_callr   )Úsource_filesÚoptionsÚskipÚonlyÚmodule_nameÚcodeÚdZdst_sourcesZf2py_sourcesÚfnÚdstÚbaseÚextZ	f2py_optsÚcwdÚcmdÚpÚoutÚerrr   r   r   Úbuild_modulef   sf    

þÿ
ü
ÿÿÿr]   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.

    Nr0   )ÚsuffixÚw©rN   rO   rP   rQ   )r   ÚopenÚwriter]   )Zsource_coderN   rO   rP   r^   rQ   r   Úfr   r   r   Ú
build_code¯   s    (ürd   c                 C   s  t  ¡ }zütj |d¡}t|dƒ�€}| d¡ | d| › d�¡ |r”| | › d| › d�¡ | | › d|› d�¡ | d	| › d
| › d| › d| › d�	¡ W d   ƒ n1 s¨0    Y  tjg d¢d|tj	tj	d�}|j
dkrèW t |¡ dS W t |¡ dS W t |¡ nt |¡ 0 dS )Nzmeson.buildr_   zproject('check_compilers')
zadd_languages('z')
z _compiler = meson.get_compiler('z_code = '''z'''
Z_have_z
_feature =z_compiler.compiles(z_code, name: 'z feature check')
)r2   ÚsetupZbtmpF)ÚcheckrX   r3   r4   r   T)r   r   r6   r   r8   ra   rb   r@   ÚrunrB   rE   r   r   )ÚlangZcode_snippetZtmpdirZ
meson_filerc   Zrunmesonr   r   r   Úcheck_languageÊ   sD    
ÿÿþÿ"û

ü
þri   z]
C Example Fortran 77 code
      PROGRAM HELLO
      PRINT *, 'Hello, Fortran 77!'
      END
zÙ
! Example Fortran 90 code
program hello90
  type :: greeting
    character(len=20) :: text
  end type greeting

  type(greeting) :: greet
  greet%text = 'hello, fortran 90!'
  print *, greet%text
end program hello90
c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚCompilerCheckerc                 C   s   d| _ d| _d| _d| _d S )NF)Úcompilers_checkedÚhas_cÚhas_f77Úhas_f90©Úselfr   r   r   Ú__init__þ   s    zCompilerChecker.__init__c                 C   s”   | j s�tjdks�tj ¡ �`}| td¡| tdt¡| tdt	¡g}|d  
¡ | _|d  
¡ | _|d  
¡ | _W d   ƒ n1 s€0    Y  d| _ d S )Nr   ÚcÚfortranr   r   é   T)rk   r   rG   Ú
concurrentÚfuturesÚThreadPoolExecutorÚsubmitri   Úfortran77_codeÚfortran90_codeÚresultrl   rm   rn   )rp   Úexecutorrv   r   r   r   Úcheck_compilers  s    
ý,zCompilerChecker.check_compilersN)r-   Ú
__module__Ú__qualname__rq   r}   r   r   r   r   rj   ý   s   rj   c                   C   s   t jS r	   )Úcheckerrl   r   r   r   r   Úhas_c_compiler  s    r�   c                   C   s   t jS r	   )r€   rm   r   r   r   r   Úhas_f77_compiler  s    r‚   c                   C   s   t jS r	   )r€   rn   r   r   r   r   Úhas_f90_compiler  s    rƒ   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚSimplifiedMesonBackendc                    s   t ƒ j|i |¤Ž d S r	   )Úsuperrq   )rp   ÚargsÚkwargs©Ú	__class__r   r   rq   &  s    zSimplifiedMesonBackend.__init__c                 C   s   |   | j¡ |  | j¡ d S r	   )Zwrite_meson_buildÚ	build_dirZ	run_mesonro   r   r   r   Úcompile)  s    zSimplifiedMesonBackend.compile)r-   r~   r   rq   r‹   Ú__classcell__r   r   rˆ   r   r„   %  s   r„   c                 K   sò   t ƒ }|du rtƒ }t|| | dg ¡|| dg ¡| dg ¡| dg ¡| dg ¡| dg ¡| dg ¡| d	g ¡| d
g ¡| dg ¡| dg ¡| dd¡| di ¡d�}z| ¡  W n tjyÎ   t d¡ Y n0 t	j
 d|› d|j› �¡ t|ƒS )z1
    Build a module via Meson and import it.
    NÚextra_objectsÚinclude_dirsÚlibrary_dirsÚ	librariesÚdefine_macrosÚundef_macrosÚ
f2py_flagsÚsysinfo_flagsÚfc_flagsÚ
flib_flagsÚsetup_flagsÚremove_build_dirFÚ	extra_dat)Ú
modulenameÚsourcesr�   rŠ   rŽ   r�   r�   r‘   r’   r“   r”   r•   r–   r—   r˜   r™   zFailed to compile moduler   ú/)r   r    r„   Úgetr‹   r@   ÚCalledProcessErrorÚpytestrO   r   r   r   Zmeson_build_dirr   )rM   rQ   r‡   rŠ   Úbackendr   r   r   Úbuild_meson.  s6    












ðr¡   c                   @   sT   e Zd ZdZdZg Zg Zg ZdZdZ	dZ
dZdZedd„ ƒZedd„ ƒZdd„ ZdS )	ÚF2PyTestNr0   c                 C   s*   t | ƒ}d|j dd¡d › d|j› d�S )NÚ_Ú.r   éÿÿÿÿZ_ext_module)Útyper~   Úrsplitr-   )rp   Úclsr   r   r   rQ   h  s    zF2PyTest.module_namec                 C   s0   t jdkrt d¡ tƒ t_tƒ t_t	ƒ t_
d S )NÚwin32z)Fails with MinGW64 Gfortran (Issue #9673))r   rG   rŸ   rO   r�   r¢   Ú_has_c_compilerr‚   Ú_has_f77_compilerrƒ   Ú_has_f90_compiler)r¨   r   r   r   Úsetup_classm  s
    

zF2PyTest.setup_classc                 C   s   | j d urd S | jr| jng }| jr0| | j¡ tdd„ |D ƒƒ}tdd„ |D ƒƒ}tdd„ |D ƒƒ}|rz| jszt d¡ |rŽ| j	sŽt d¡ |r¨| j	s¨| j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 )
Nc                 s   s   | ]}t |ƒ d ¡V  qdS )r0   N©ÚstrÚendswith©Ú.0rT   r   r   r   Ú	<genexpr>}  ó    z(F2PyTest.setup_method.<locals>.<genexpr>c                 s   s   | ]}t |ƒ d ¡V  qdS )r/   Nr®   r±   r   r   r   r³   ~  r´   c                 s   s   | ]}t |ƒ d ¡V  qdS )r1   Nr®   r±   r   r   r   r³     r´   z No Fortran 77 compiler availablez No Fortran 90 compiler availablezNo Fortran compiler available)rN   rO   rP   r^   rQ   r`   )Úmoduler›   rR   r;   r^   Úanyr«   rŸ   rO   r¬   rd   rN   rP   rQ   r]   )rp   ÚcodesZ	needs_f77Z	needs_f90Z	needs_pyfr   r   r   Úsetup_methodu  s>    






ú	
ûzF2PyTest.setup_method)r-   r~   r   rR   r›   rN   rO   rP   r^   rµ   rª   r«   r¬   ÚpropertyrQ   Úclassmethodr­   r¸   r   r   r   r   r¢   \  s   

r¢   c                  G   s   t tjjƒj ¡ }|j| Ž S r	   )r   ÚnumpyZf2pyÚ__file__ÚparentÚresolveÚjoinpath)r$   rS   r   r   r   Úgetpath¢  s    rÀ   c              	   c   s8   t  ¡ }t | ¡ zd V  W t |¡ nt |¡ 0 d S r	   )r   rX   r6   r>   )r   Zcurpathr   r   r   Ú	switchdir¨  s
    
rÁ   )N)N)7Ú__doc__rJ   r6   r   r@   r   r   r   ÚtextwrapÚrerŸ   Ú
contextlibr»   Úconcurrent.futuresru   Úpathlibr   Znumpy._utilsr   Znumpy.testingr   r   Ú	importlibr   Znumpy.f2py._backends._mesonr   r
   r   rG   r¼   r½   ZNUMPY_INSTALL_ROOTÚlistrH   r   r   r    r.   r]   rd   ri   ry   rz   rj   r€   r}   r�   r‚   rƒ   r„   r¡   r¢   rÀ   ÚcontextmanagerrÁ   r   r   r   r   Ú<module>   sh   

Hû
	
.F