o
    >~#g/                     @   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/dd	Zd
ZdZG dd dZesoe Ze  dd Z dd Z!dd Z"dd 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"Z0e/g g g ddfd#d$Z1G d%d& d&eZ2d/d'd(Z3G d)d* d*Z4d+d, Z5ej6d-d. Z7d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)   z/var/www/static.ux5.de/https/Moving-Object-Detection-with-OpenCV/env/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__W   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_compilers]   s   

zCompilerChecker.check_compilersN)__name__
__module____qualname__r3   rA   r)   r)   r)   r*   r,   V   s    r,   c                   C      t jS N)checkerr.   r)   r)   r)   r*   has_c_compilerp      rH   c                   C   rE   rF   )rG   r/   r)   r)   r)   r*   has_f77_compilers   rI   rJ   c                   C   rE   rF   )rG   r0   r)   r)   r)   r*   has_f90_compilerv   rI   rK   c                   C   s   t jot jS rF   )rG   r0   r/   r)   r)   r)   r*   has_fortran_compilery   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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()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   3  s   zSimplifiedMesonBackend.__init__c                 C   s   |  | j | | j d S rF   )write_meson_build	build_dir	run_mesonr1   r)   r)   r*   compile6  s   zSimplifiedMesonBackend.compile)rB   rC   rD   r3   r   __classcell__r)   r)   r   r*   r   2  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_meson;  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   t  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_classy  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   h  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   )8__doc__r~   r   r   r   r   r!   rR   textwraprer   
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>   sl    
 

O
	-G