a
    LfP-                     @   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re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   N/var/www/ai-form-bot/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 |sJ |du rt }dd|g| | }|dd	g7 }|r|d
g| 7 }|r|dg| 7 }t }zt| 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|	 q0 t jdkrt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  }ztj|d}t|d}|d |d|  d |r||  d|  d ||  d| d |d	|  d
|  d|  d|  d	 W d    n1 s0    Y  tjg dd|tj	tj	d}|j
dkrW 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 stjdkstj `}|td|tdt|tdt	g}|d 
 | _|d 
 | _|d 
 | _W d    n1 s0    Y  d| _ d S )Nr   cfortranr   r      T)rk   r   rG   
concurrentfuturesZThreadPoolExecutorZ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	std |r| j	s| jstd | jd urt
| j| j| j| j| j| jd| _ | jd urt| 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   rw   rx   rj   r~   r{   r   r   r   r   r   r   r   contextmanagerr   r   r   r   r   <module>   sh   

H
	
.F