o
    DfP-                     @   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rme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se+ 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   sZ   t d ur+ztjt  W n	 ty   Y nw ztt  W n	 ty&   Y nw d a d S d S N)_module_dirsyspathremove
ValueErrorshutilrmtreeOSError r   r   N/home/ubuntu/webapp/venv/lib/python3.10/site-packages/numpy/f2py/tests/util.py_cleanup)   s   r   c                   C   s6   t d u rt a tt t tjvrt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               
      sh   t | |f}|vr'z | i ||< W n ty& } z||<  d }~ww | }t|tr2||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 ]5}	tj|	std|	 tj|tj|	}
t	|	|
 |
|
 tj|
\}}|dv rE|
|
 q|sJJ |du rQt }dd|g| | }|dd	g7 }|ri|d
g| 7 }|rr|dg| 7 }t }zAt| t jd|g| }tj|tjtjd}| \}}|jdkrtd|dd t|f W t| |D ]}	t|	 qnt| |D ]}	t|	 qw 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ddst_sourcesf2py_sourcesfndstbaseext	f2py_optscwdcmdpouterrr   r   r   build_modulef   sn   






r`   c              	   C   s   |du rd}t |d,}t|d}||  W d   n1 s!w   Y  t|g||||dW  d   S 1 s:w   Y  dS )z6
    Compile and import Fortran code using f2py.

    Nr0   )suffixwrN   rO   rP   rQ   )r   openwriter`   )source_coderN   rO   rP   ra   rQ   r   fr   r   r   
build_code   s   $rh   c                 C   s  t  }zwtj|d}t|d@}|d |d|  d |rJ||  d|  d ||  d| d |d	|  d
|  d|  d|  d	 W d    n1 sTw   Y  tjg dd|tj	tj	d}|j
dkrtW t| dS W t| dS t| w )Nzmeson.buildrb   zproject('check_compilers')
zadd_languages('z')
z _compiler = meson.get_compiler('z_code = '''z'''
_have_z
_feature =z_compiler.compiles(z_code, name: 'z feature check')
)r2   setupbtmpF)checkr[   r3   r4   r   T)r   r   r6   r   r8   rd   re   r@   runrB   rE   r   r   )langcode_snippettmpdir
meson_filerg   runmesonr   r   r   check_language   s@   

rs   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 sJtjdksLtj 0}|td|tdt|tdt	g}|d 
 | _|d 
 | _|d 
 | _W d    n1 s@w   Y  d| _ d S d S d S )Nr   cfortranr   r      T)ru   r   rG   
concurrentfuturesThreadPoolExecutorsubmitrs   fortran77_codefortran90_coderesultrv   rw   rx   )rz   executorr   r   r   r   check_compilers  s   

zCompilerChecker.check_compilersN)r-   
__module____qualname__r{   r   r   r   r   r   rt      s    rt   c                   C      t jS r	   )checkerrv   r   r   r   r   has_c_compiler     r   c                   C   r   r	   )r   rw   r   r   r   r   has_f77_compiler  r   r   c                   C   r   r	   )r   rx   r   r   r   r   has_f90_compiler  r   r   c                       s$   e Zd Z fddZdd Z  ZS )SimplifiedMesonBackendc                    s   t  j|i | d S r	   )superr{   )rz   argskwargs	__class__r   r   r{   &  s   zSimplifiedMesonBackend.__init__c                 C   s   |  | j | | j d S r	   )write_meson_build	build_dir	run_mesonry   r   r   r   compile)  s   zSimplifiedMesonBackend.compile)r-   r   r   r{   r   __classcell__r   r   r   r   r   %  s    r   c                 K   s4  t  }|du r
t }tdi d|d| d|dg d|d|dg d|dg d|dg d	|d	g d
|d
g d|dg d|dg d|dg d|dg d|dg d|ddd|di }z|  W n tjy   td Y nw t	j
d| d|j  t|S )z1
    Build a module via Meson and import it.
    N
modulenamesourcesextra_objectsr   include_dirslibrary_dirs	librariesdefine_macrosundef_macros
f2py_flagssysinfo_flagsfc_flags
flib_flagssetup_flagsremove_build_dirF	extra_datzFailed to compile moduler   /r   )r   r    r   getr   r@   CalledProcessErrorpytestrO   r   r   r   meson_build_dirr   )rM   rQ   r   r   backendr   r   r   build_meson.  sV   	
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   _ext_module)typer   rsplitr-   )rz   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r|| j tdd |D }tdd |D }tdd |D }|r=| js=td |rG| j	sGtd |rT| j	sT| jsTtd | jd urjt
| j| j| j| j| j| jd| _ | jd urt| j| j| j| j| jd	| _ d S d S )
Nc                 s       | ]
}t |d V  qdS )r0   Nstrendswith.0rV   r   r   r   	<genexpr>}      z(F2PyTest.setup_method.<locals>.<genexpr>c                 s   r   )r/   Nr   r   r   r   r   r   ~  r   c                 s   r   )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   ra   rQ   rc   )moduler   rR   r;   ra   anyr   r   rO   r   rh   rN   rP   rQ   r`   )rz   codes	needs_f77	needs_f90	needs_pyfr   r   r   setup_methodu  s@   







	zF2PyTest.setup_method)r-   r   r   rR   r   rN   rO   rP   ra   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   numpyf2py__file__parentresolvejoinpath)r$   rS   r   r   r   getpath  s   
r   c              	   c   s8    t  }t|  zd V  W t| d S t| w r	   )r   r[   r6   r>   )r   curpathr   r   r   	switchdir  s   
r   r	   )7__doc__rJ   r6   r   r@   r   r   r   textwraprer   
contextlibr   concurrent.futuresr   pathlibr   numpy._utilsr   numpy.testingr   r   	importlibr   numpy.f2py._backends._mesonr   r
   r   rG   r   r   NUMPY_INSTALL_ROOTlistrH   r   r   r    r.   r`   rh   rs   r   r   rt   r   r   r   r   r   r   r   r   r   contextmanagerr   r   r   r   r   <module>   sj    

H

	.F