
    qKgP-                     b   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j8                  d	k(  r= ee      j<                  j<                  j<                  Z e ej                  d
            Z!d Z"d Z#d Z$d Z%e%g g g dfd       Z&e%g g g ddfd       Z'd dZ(dZ)dZ* G d d      Z+es e+       Z,e,j[                          d Z.d Z/d Z0 G d de      Z1d dZ2 G d d      Z3d Z4ejj                  d        Z6y)!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                      t         A	 t        j                  j                  t                	 t        j                  t                d a y y # t        $ r Y )w xY w# t        $ r Y w xY wN)_module_dirsyspathremove
ValueErrorshutilrmtreeOSError     Y/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numpy/f2py/tests/util.py_cleanupr   )   sa    	HHOOK(	MM+&    		  		s"   #A	 A 		AA	A$#A$c                      t         gt        j                         a t        j                  t
               t         t        j                  vr$t        j                  j                  dt                t         S )Nr   )	r   tempfilemkdtempatexitregisterr   r   r   insertr   r   r   get_module_dirr   7   sF    &&(!chh&HHOOA{+r   c                  x    t                dt        z  } t        dz  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)names    r   get_temp_module_namer%   A   s;      ;.D1Ks{{BCCKr   c                 <     i  fd} j                   |_         |S )Nc                      t        | |f      }|vr	  | i ||<   |   }t        |t              r||S # t        $ r}||<    d }~ww xY wr   )repr	Exception
isinstance)akwkeyeretfuncmemos        r   wrapperz_memoize.<locals>.wrapperP   sj    Ar7md? !NrNS	 3ic9%I
  S	s   8 	AAA)__name__)r0   r2   r1   s   ` @r   _memoizer4   M   s     D }}GNr   c           
         dt         j                  d}t               }g }g }| D ]  }	t        j                  j	                  |	      st        d|	z        t        j                  j                  |t        j                  j                  |	            }
t        j                  |	|
       |j                  |
       t        j                  j                  |
      \  }}|dv s|j                  |
        |sJ |
t               }dd|g|z   |z   }|dd	gz  }|r	|d
g|z   z  }|r	|dg|z   z  }t        j                         }	 t        j                  |       t         j                  d|g|z   }t!        j"                  |t         j$                  t         j&                        }|j)                         \  }}|j*                  dk7  rt        d|dd dt-        |            	 t        j                  |       |D ]  }	t        j.                  |	        	 t         j0                  dk(  rst2        j5                  t7        j6                  t        j                  j                  |dj9                  |                         t!        j:                  g dt2        z          t=        |      S # t        j                  |       |D ]  }	t        j.                  |	        w xY w)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:    
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errs                      r   build_modulerh   f   sv    %SXXL0VWDA KLww~~b!1B677ggll1bgg..r23C 3GG$$S)	c66$  < *,t[)G3lBI+w''IgY%%	gY%%	 ))+C
~~tT*Y6S$.OO$.$5$57 ==?S<<1 #AB3 9 : :  	 BIIbM  ||x 	IIbggll1gnn[&ABC	
 	I	
 %%) 	 BIIbM s    BJ" "3Kc                     |d}t        |      5 }t        |d      5 }|j                  |        ddd       t        |g||||      cddd       S # 1 sw Y   #xY w# 1 sw Y   yxY w)z6
    Compile and import Fortran code using f2py.

    Nr7   )suffixwrV   rW   rX   rY   )r   openwriterh   )source_coderV   rW   rX   rj   rY   r   fs           r   
build_coderq      sc     ~		 D$_GGK  TF$+!%!%(3	5 
!	 _ 
!	 s!   AAAA	AA'c                    t        j                         }	 t        j                  j	                  |d      }t        |d      5 }|j                  d       |j                  d|  d       |rL|j                  |  d|  d       |j                  |  d| d       |j                  d	|  d
|  d|  d|  d	       d d d        t        j                  g dd|t        j                  t        j                        }|j                  dk(  r	 t        j                  |       y	 t        j                  |       y# 1 sw Y   ~xY w# t        j                  |       w xY w)Nzmeson.buildrk   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')
)r9   setupbtmpF)checkrc   r:   r;   r   T)r   r   r>   r   r@   rm   rn   rH   runrJ   rM   r   r   )langcode_snippettmpdir
meson_filerp   runmesons         r   check_languager}      s6   FWW\\&-8
*c"aGG23GGodV4014& @dKL4&L>?@TF*f/v 6#f$68 # >>&????
 !# 	f f/ #". 	fs%   ,E  A5D47AE  4D=9E   E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                       e Zd Zd Zd Zy)CompilerCheckerc                 <    d| _         d| _        d| _        d| _        y )NF)compilers_checkedhas_chas_f77has_f90selfs    r   __init__zCompilerChecker.__init__   s    !&
r   c                    | j                   st        j                  dk(  st        j                  j                         5 }|j                  t        d      |j                  t        dt              |j                  t        dt              g}|d   j                         | _        |d   j                         | _        |d   j                         | _        d d d        d| _         y y y # 1 sw Y   xY w)Nr	   cfortranr   r       T)r   r   rO   
concurrentfuturesThreadPoolExecutorsubmitr}   fortran77_codefortran90_coderesultr   r   r   )r   executorr   s      r   check_compilerszCompilerChecker.check_compilers  s    &&1I##668HOONC8OONI~NOONI~N %QZ..0
&qz002&qz002 9 &*D" 2J&88s   BC$$C-N)r3   
__module____qualname__r   r   r   r   r   r   r      s    *r   r   c                  "    t         j                  S r   )checkerr   r   r   r   has_c_compilerr     s    ==r   c                  "    t         j                  S r   )r   r   r   r   r   has_f77_compilerr         ??r   c                  "    t         j                  S r   )r   r   r   r   r   has_f90_compilerr     r   r   c                   $     e Zd Z fdZd Z xZS )SimplifiedMesonBackendc                 $    t        |   |i | y r   )superr   )r   argskwargs	__class__s      r   r   zSimplifiedMesonBackend.__init__&  s    $)&)r   c                 p    | j                  | j                         | j                  | j                         y r   )write_meson_build	build_dir	run_mesonr   s    r   compilezSimplifiedMesonBackend.compile)  s$    t~~.t~~&r   )r3   r   r   r   r   __classcell__)r   s   @r   r   r   %  s    *'r   r   c           	      2   t               }|
t               }t        di d|d| d|j                  dg       d|d|j                  dg       d|j                  dg       d|j                  dg       d|j                  dg       d	|j                  d	g       d
|j                  d
g       d|j                  dg       d|j                  dg       d|j                  dg       d|j                  dg       d|j                  dd      d|j                  di       }	 |j	                          t        j                  j                  d| d|j                          t        |      S # t
        j                  $ r t        j                  d       Y dw xY w)z1
    Build a module via Meson and import it.
    
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   rH   CalledProcessErrorpytestrW   r   r   r   meson_build_dirr   )rU   rY   r   r   backends        r   build_mesonr   .  s     I*, %  jj"5 	
 ZZ3 ZZ3 **["- jj"5 ZZ3 ::lB/ jj"5 J+ ::lB/ JJ}b1  $6>  **["-!G,0
 HHOOA)Ag&=&=%>?@%% (( 0./0s   !E+ +(FFc                   Z    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        Zed        Zd Zy)F2PyTestNr7   c                 x    t        |       }d|j                  j                  dd      d    d|j                   dS )N_.r    _ext_module)typer   rsplitr3   )r   clss     r   rY   zF2PyTest.module_nameh  s;    4j3>>((Q/34Acll^;OOr   c                     t         j                  dk(  rt        j                  d       t	               t
        _        t               t
        _        t               t
        _
        y )Nwin32z)Fails with MinGW64 Gfortran (Issue #9673))r   rO   r   rW   r   r   _has_c_compilerr   _has_f77_compilerr   _has_f90_compiler)r   s    r   setup_classzF2PyTest.setup_classm  s<    <<7"KKCD#1#3 %5%7"%5%7"r   c                 Z   | j                   y | j                  r| j                  ng }| j                  r|j                  | j                         t        d |D              }t        d |D              }t        d |D              }|r!| j                  st        j                  d       |r!| j                  st        j                  d       |r-| j                  s!| j                  st        j                  d       | j                  Rt        | j                  | j                  | j                  | j                  | j                  | j                        | _         | j                  Ht        | j                  | j                  | j                  | j                  | j                        | _         y y )	Nc              3   P   K   | ]  }t        |      j                  d          yw)r7   Nstrendswith.0r^   s     r   	<genexpr>z(F2PyTest.setup_method.<locals>.<genexpr>}  s      ?2B((.   $&c              3   P   K   | ]  }t        |      j                  d          yw)r6   Nr   r   s     r   r   z(F2PyTest.setup_method.<locals>.<genexpr>~        A5RB((05r   c              3   P   K   | ]  }t        |      j                  d          yw)r8   Nr   r   s     r   r   z(F2PyTest.setup_method.<locals>.<genexpr>  r   r   z No Fortran 77 compiler availablez No Fortran 90 compiler availablezNo Fortran compiler available)rV   rW   rX   rj   rY   rl   )moduler   rZ   rC   rj   anyr   r   rW   r   rq   rV   rX   rY   rh   )r   codes	needs_f77	needs_f90	needs_pyfs        r   setup_methodzF2PyTest.setup_methodu  s=   ;;" $"99LL%???	A5AA	A5AA	T33KK:;T33KK:;d448N8NKK78 99 $		YYYY{{ ,,DK <<#&YYYY ,,DK $r   )r3   r   r   rZ   r   rV   rW   rX   rj   r   r   r   r   propertyrY   classmethodr   r   r   r   r   r   r   \  sc    DGGDDFFOP P 8 8%r   r   c                      t        t        j                  j                        j                  j                         } |j                  |  S r   )r   numpyf2py__file__parentresolvejoinpath)r+   r[   s     r   getpathr     s5    UZZ  !((002A1::q>r   c              #      K   t        j                         }t        j                  |        	 d  t        j                  |       y # t        j                  |       w xY wwr   )r   rc   r>   rF   )r   curpaths     r   	switchdirr     s<     hhjGHHTN
s   *A!A A!AA!r   )7__doc__rR   r>   r   rH   r   r   r   textwraprer   
contextlibr   concurrent.futuresr   pathlibr   numpy._utilsr   numpy.testingr   r   	importlibr   numpy.f2py._backends._mesonr   r   r!   rO   r   r   NUMPY_INSTALL_ROOTlistrP   r   r   r%   r4   rh   rq   r}   r   r   r   r   r   r   r   r   r   r   r   r   contextmanagerr   r   r   r   <module>r      sd    	 
      	      " + # 4 <<8h..55<<*//
;<L	2 
') E& 
E&P 
5 
54<* *, G'\ '&&\> >L  r   