o
    ðT·jŽ/  ã                   @   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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/dd	„Zd
ZdZG dd„ dƒZesgeƒ Ze ¡  dd„ Zdd„ Zdd„ Z dd„ 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/G d%d&„ d&eƒZ0d/d'd(„Z1G d)d*„ d*ƒZ2d+d,„ Z3e	j4d-d.„ ƒZ5dS )0z¥
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)ÚMesonBackendc                 C   s*  t jdkrtjddd� t ¡ }z~tj |d¡}t	|dƒ�@}| 
d¡ | 
d| › d	�¡ |rV| 
| › d
| › d	�¡ | 
| › d|› d�¡ | 
d| › d| › d| › d| › d�	¡ W d   ƒ n1 s`w   Y  ztjg d¢d|dd�}W n tjyƒ   tjddd� Y nw |jdkW t |¡ S t |¡ w )NÚwin32z*No Fortran tests on Windows (Issue #25134)T)Úallow_module_levelzmeson.buildÚwz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')
)ÚmesonÚsetupÚbtmpF)ÚcheckÚcwdÚcapture_outputz3meson not present, skipping compiler dependent testr   )ÚsysÚplatformÚpytestÚskipÚtempfileÚmkdtempÚosÚpathÚjoinÚopenÚwriteÚ
subprocessÚrunÚCalledProcessErrorÚ
returncodeÚshutilÚrmtree)ÚlangÚcode_snippetÚtmpdirÚ
meson_fileÚfÚrunmeson© r)   úX/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/numpy/f2py/tests/util.pyÚcheck_language   sD   

ÿÿþÿ€ú
üÿ
r+   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__T   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 )NÚcygwinÚcÚfortranr   é   é   T)r-   r   r   Ú
concurrentÚfuturesÚThreadPoolExecutorÚsubmitr+   Úfortran77_codeÚfortran90_codeÚresultr.   r/   r0   )r2   Úexecutorr:   r)   r)   r*   Úcheck_compilersZ   s   
ý÷
ôzCompilerChecker.check_compilersN)Ú__name__Ú
__module__Ú__qualname__r3   rA   r)   r)   r)   r*   r,   S   s    r,   c                   C   ó   t jS ©N)Úcheckerr.   r)   r)   r)   r*   Úhas_c_compilerm   ó   rH   c                   C   rE   rF   )rG   r/   r)   r)   r)   r*   Úhas_f77_compilerp   rI   rJ   c                   C   rE   rF   )rG   r0   r)   r)   r)   r*   Úhas_f90_compilers   rI   rK   c                   C   s   t jot jS rF   )rG   r0   r/   r)   r)   r)   r*   Úhas_fortran_compilerv   s   rL   i  r4   z**/*.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 rF   )Ú_module_dirr   r   ÚremoveÚ
ValueErrorr!   r"   ÚOSErrorr)   r)   r)   r*   Ú_cleanup†   s   ÿÿ÷rQ   c                   C   s6   t d u rt ¡ a t t¡ t tjvrtj dt ¡ t S )Nr   )	rM   r   r   ÚatexitÚregisterrQ   r   r   Úinsertr)   r)   r)   r*   Úget_module_dir”   s   

rU   c                  C   s,   t ƒ  dt } td7 a| tjv rtdƒ‚| S )Nz_test_ext_module_%dr7   z%Temporary module name already in use.)rU   Ú_module_numr   ÚmodulesÚRuntimeError)Únamer)   r)   r*   Úget_temp_module_namež   s   
rZ   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 rF   )ÚreprÚ	ExceptionÚ
isinstance)ÚaÚkwÚkeyÚeÚret©ÚfuncÚmemor)   r*   Úwrapper­   s   €þ
z_memoize.<locals>.wrapper)rB   )rd   rf   r)   rc   r*   Ú_memoizeª   s   rg   c              
   C   s  dt j›d�}tƒ }tƒ st d¡ g }g }| D ]5}	tj |	¡s&td|	 ƒ‚tj 	|tj 
|	¡¡}
t |	|
¡ | |
¡ tj |
¡\}}|dv rM| |
¡ q|sRJ ‚|du rYtƒ }g }d|vrfd|vrfdg}d	d
|g| | | }|ddg7 }|r€|dg| 7 }|r‰|dg| 7 }t ¡ }zAt |¡ 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 |	¡ qÆnt |¡ |D ]}	t |	¡ qÖw 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()úNo Fortran compiler availablez%s is not a file)ú.f90z.f95ú.fz.cú.pyfNz--freethreading-compatiblez--no-freethreading-compatiblez-cz-mz	--backendr   zskip:zonly:)ÚstdoutÚstderrr   zRunning f2py failed: %s
%sé   r4   z{:s}*)z/usr/bin/rebasez
--databasez--obliviousz	--verbose)"r   r   rU   rL   r   r   r   ÚisfilerX   r   Úbasenamer!   ÚcopyfileÚappendÚsplitextrZ   ÚgetcwdÚchdirÚ
executabler   ÚPopenÚPIPEÚSTDOUTÚcommunicater    r   Úunlinkr   Ú_module_listÚextendÚglobÚformatÚ
check_callr   )Úsource_filesÚoptionsr   ÚonlyÚmodule_nameÚcodeÚdÚdst_sourcesÚf2py_sourcesÚfnÚdstÚbaseÚextÚgil_optionsÚ	f2py_optsr   ÚcmdÚpÚoutÚerrr)   r)   r*   Úbuild_moduleÃ   sx   


€
þ
ÿÿ
ÿ
ýÿ
ÿÿÿ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.

    Nrj   )Úsuffixr
   ©r‚   r   rƒ   r„   )r   r   r   r“   )Úsource_coder‚   r   rƒ   r”   r„   r   r'   r)   r)   r*   Ú
build_code  s   ÿü$ýr—   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚSimplifiedMesonBackendc                    s   t ƒ j|i |¤Ž d S rF   )Úsuperr3   )r2   ÚargsÚkwargs©Ú	__class__r)   r*   r3   0  s   zSimplifiedMesonBackend.__init__c                 C   s   |   | j¡ |  | j¡ d S rF   )Úwrite_meson_buildÚ	build_dirÚ	run_mesonr1   r)   r)   r*   Úcompile3  s   zSimplifiedMesonBackend.compile)rB   rC   rD   r3   r¡   Ú__classcell__r)   r)   rœ   r*   r˜   /  s    r˜   c                 K   s   t ƒ st d¡ 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 ¡“Ž}| ¡  tj	 
d|› d|j› �¡ t|ƒS )z1
    Build a module via Meson and import it.
    rh   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_datr   ú/r)   )rL   r   r   rU   rZ   r˜   Úgetr¡   r   r   rT   Úmeson_build_dirr   )r�   r„   r›   rŸ   Úbackendr)   r)   r*   Úbuild_meson8  sR   
ÿþýüûúùø	÷
öõôóòñð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 )	ÚF2PyTestNrj   c                 C   s*   t | ƒ}d|j dd¡d › d|j› d�S )NÚ_Ú.r7   éÿÿÿÿÚ_ext_module)ÚtyperC   ÚrsplitrB   )r2   Úclsr)   r)   r*   r„   q  s   "zF2PyTest.module_namec                 C   s8   t jdkr
t d¡ tƒ t_tƒ t_t	ƒ t_
tƒ t_d S )Nr   z)Fails with MinGW64 Gfortran (Issue #9673))r   r   r   r   rH   r·   Ú_has_c_compilerrJ   Ú_has_f77_compilerrK   Ú_has_f90_compilerrL   Ú_has_fortran_compiler)r¾   r)   r)   r*   Úsetup_classv  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¡ |rQ| j
sQt d¡ | jd urgt| j| j| j| j| j| jd�| _ | jd ur}t| j| j| j| j| jd	�| _ d S d S )
Nc                 s   ó   � | ]
}t |ƒ d ¡V  qdS )rj   N©ÚstrÚendswith©Ú.0r‰   r)   r)   r*   Ú	<genexpr>‡  ó   € z(F2PyTest.setup_method.<locals>.<genexpr>c                 s   rÄ   )ri   NrÅ   rÈ   r)   r)   r*   rÊ   ˆ  rË   c                 s   rÄ   )rk   NrÅ   rÈ   r)   r)   r*   rÊ   ‰  rË   z No Fortran 77 compiler availablez No Fortran 90 compiler availablerh   )r‚   r   rƒ   r”   r„   r•   )Úmoduler¤   r…   rr   r”   ÚanyrÀ   r   r   rÁ   rÂ   r—   r‚   rƒ   r„   r“   )r2   ÚcodesÚ	needs_f77Ú	needs_f90Ú	needs_pyfr)   r)   r*   Úsetup_method  s@   







ú
	ûÿzF2PyTest.setup_method)rB   rC   rD   r…   r¤   r‚   r   rƒ   r”   rÌ   r¿   rÀ   rÁ   Úpropertyr„   ÚclassmethodrÃ   rÒ   r)   r)   r)   r*   r·   e  s     

r·   c                  G   s   t tjjƒj ¡ }|j| Ž S rF   )r   ÚnumpyÚf2pyÚ__file__ÚparentÚresolveÚjoinpath)r^   r†   r)   r)   r*   Úgetpath¬  s   
rÛ   c              	   c   s8   � t  ¡ }t | ¡ zd V  W t |¡ d S t |¡ w rF   )r   r   r   ru   )r   Úcurpathr)   r)   r*   Ú	switchdir²  s   €
rÝ   rF   )6Ú__doc__r~   r   r   r   r   r!   rR   r   Ú
contextlibrÕ   Úconcurrent.futuresr9   Úpathlibr   Únumpy._utilsr   Únumpy.testingr   r   Ú	importlibr   Únumpy.f2py._backends._mesonr   r+   r=   r>   r,   rG   rA   rH   rJ   rK   rL   rM   rV   r   r×   rØ   ÚNUMPY_INSTALL_ROOTÚlistr|   rQ   rU   rZ   rg   r“   r—   r˜   r¶   r·   rÛ   ÚcontextmanagerrÝ   r)   r)   r)   r*   Ú<module>   sh    


Oû
	-G