
    Bwg/                     X   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Zd	Zd
Z G d d      Zes e       Zej;                          d Zd Zd Z d Z!da"da#ejH                  dk(  r= ee%      jL                  jL                  jL                  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/ G d de      Z0d!dZ1 G d d      Z2d Z3e	jh                  d         Z5y)"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)MesonBackendc                    t         j                  dk(  rt        j                  dd       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|d      }j                  dk(  t        j                   |       S # 1 sw Y   IxY w# t        j                  $ r t        j                  dd       Y \w xY w# t        j                   |       w xY 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runmesons         L/var/www/horilla/myenv/lib/python3.12/site-packages/numpy/f2py/tests/util.pycheck_languager,      sd   
||w@UYZFWW\\&-8
*c" 
	aGG23GGodV4014& @dKL4&L>?@TF*f/v 6#f$68
		h!~~*#	H ""a'f-
	 
	$ ,, 	hKKMbfg	h 	fsI    ,E% ,A5D)!E% *D5 E% )D2.E% 5*E"E% !E""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__T   s    !&
    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)Ncygwincfortranr         T)r0   r   r   
concurrentfuturesThreadPoolExecutorsubmitr,   fortran77_codefortran90_coderesultr1   r2   r3   )r5   executorr?   s      r+   check_compilerszCompilerChecker.check_compilersZ   s    &&1I##668 	3HOONC8OONI~NOONI~N %QZ..0
&qz002&qz002	3 &*D" 2J&	3 	3s   BC$$C-N)__name__
__module____qualname__r6   rF    r7   r+   r.   r.   S   s    *r7   r.   c                  "    t         j                  S N)checkerr1   rJ   r7   r+   has_c_compilerrN   m   s    ==r7   c                  "    t         j                  S rL   )rM   r2   rJ   r7   r+   has_f77_compilerrP   p       ??r7   c                  "    t         j                  S rL   )rM   r3   rJ   r7   r+   has_f90_compilerrS   s   rQ   r7   c                  F    t         j                  xr t         j                  S rL   )rM   r3   r2   rJ   r7   r+   has_fortran_compilerrU   v   s    OO/0r7   i  r9   z**/*.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rL   )_module_dirr   r   remove
ValueErrorr#   r$   OSErrorrJ   r7   r+   _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   )	rW   r   r   atexitregisterr[   r   r   insertrJ   r7   r+   get_module_dirr`      sF    &&(!chh&HHOOA{+r7   c                  x    t                dt        z  } t        dz  a| t        j                  v rt	        d      | S )Nz_test_ext_module_%dr<   z%Temporary module name already in use.)r`   _module_numr   modulesRuntimeError)names    r+   get_temp_module_namerf      s;      ;.D1Ks{{BCCKr7   c                 <     i  fd} j                   |_         |S )Nc                      t        | |f      }|vr	  | i ||<   |   }t        |t              r||S # t        $ r}||<    d }~ww xY wrL   )repr	Exception
isinstance)akwkeyeretfuncmemos        r+   wrapperz_memoize.<locals>.wrapper   sj    Ar7md? !NrNS	 3ic9%I
  S	s   8 	AAA)rG   )rq   rs   rr   s   ` @r+   _memoizert      s     D }}GNr7   c           
         dt         j                  d}t               }t               st	        j
                  d       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               }g }d|vrd|vrdg}d	d
|g|z   |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/                         \  }}|j0                  dk7  rt        d|dd dt3        |            	 t        j"                  |       |D ]  }	t        j4                  |	        	 t         j6                  dk(  rst8        j;                  t=        j<                  t        j                  j                  |dj?                  |                         t'        j@                  g dt8        z          tC        |      S # t        j"                  |       |D ]  }	t        j4                  |	        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()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:    
r9   z{:s}*)z/usr/bin/rebasez
--databasez--obliviousz	--verbose)"r   r   r`   rU   r   r   r   isfilerd   r   basenamer#   copyfileappendsplitextrf   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errs                       r+   build_moduler      s    %SXXL0VWDA!34 KL 	%ww~~b!1B677ggll1bgg..r23C 3GG$$S)	c66$	% < *,K#727V^e7e34t[)G3kALPI+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K 3L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.

    Nrx   )suffixr   r   r   r   r   )r   r   r   r   )source_coder   r   r   r   r   r   r)   s           r+   
build_coder     sv     ~		  5D$_ 	!GGK 	!TF$+!%!%(3	55 5	! 	!5 5s!   AAAA	AA'c                   $     e Zd Z fdZd Z xZS )SimplifiedMesonBackendc                 $    t        |   |i | y rL   )superr6   )r5   argskwargs	__class__s      r+   r6   zSimplifiedMesonBackend.__init__0  s    $)&)r7   c                 p    | j                  | j                         | j                  | j                         y rL   )write_meson_build	build_dir	run_mesonr4   s    r+   compilezSimplifiedMesonBackend.compile3  s$    t~~.t~~&r7   )rG   rH   rI   r6   r   __classcell__)r   s   @r+   r   r   /  s    *'r7   r   c           	         t               st        j                  d       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 )z1
    Build a module via Meson and import it.
    rv   
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   /rJ   )rU   r   r   r`   rf   r   getr   r   r   r_   meson_build_dirr   )r   r   r   r   backends        r+   build_mesonr   8  s     !34 I*, %  jj"5 	
 ZZ3 ZZ3 **["- jj"5 ZZ3 ::lB/ jj"5 J+ ::lB/ JJ}b1  $6>  **["-!G& OO HHOOA)Ag&=&=%>?@%%r7   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)F2PyTestNrx   c                 x    t        |       }d|j                  j                  dd      d    d|j                   dS )N_.r<   _ext_module)typerH   rsplitrG   )r5   clss     r+   r   zF2PyTest.module_nameq  s;    4j3>>((Q/34Acll^;OOr7   c                     t         j                  dk(  rt        j                  d       t	               t
        _        t               t
        _        t               t
        _
        t               t
        _        y )Nr
   z)Fails with MinGW64 Gfortran (Issue #9673))r   r   r   r   rN   r   _has_c_compilerrP   _has_f77_compilerrS   _has_f90_compilerrU   _has_fortran_compiler)r   s    r+   setup_classzF2PyTest.setup_classv  sG    <<7"KKCD#1#3 %5%7"%5%7")=)?&r7   c                 B   | 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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)rx   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)rw   Nr   r   s     r+   r   z(F2PyTest.setup_method.<locals>.<genexpr>       ARB((0Ar   c              3   P   K   | ]  }t        |      j                  d          yw)ry   Nr   r   s     r+   r   z(F2PyTest.setup_method.<locals>.<genexpr>  r   r   z No Fortran 77 compiler availablez No Fortran 90 compiler availablerv   )r   r   r   r   r   r   )moduler   r   r   r   anyr   r   r   r   r   r   r   r   r   r   )r5   codes	needs_f77	needs_f90	needs_pyfs        r+   setup_methodzF2PyTest.setup_method  s5   ;;" $"99LL%???	A5AA	A5AA	T33KK:;T33KK:;T77KK78 99 $		YYYY{{ ,,DK <<#&YYYY ,,DK $r7   )rG   rH   rI   r   r   r   r   r   r   r   r   r   r   propertyr   classmethodr   r   rJ   r7   r+   r   r   e  se    DGGDDFFOP P @ @%r7   r   c                      t        t        j                  j                        j                  j                         } |j                  |  S rL   )r   numpyf2py__file__parentresolvejoinpath)rl   r   s     r+   getpathr     s5    UZZ  !((002A1::q>r7   c              #      K   t        j                         }t        j                  |        	 d  t        j                  |       y # t        j                  |       w xY wwrL   )r   r   r   r   )r   curpaths     r+   	switchdirr     s<     hhjGHHTN
s   *A!A A!AA!rL   )6__doc__r   r   r   r   r   r#   r]   r   
contextlibr   concurrent.futuresr>   pathlibr   numpy._utilsr   numpy.testingr   r   	importlibr   numpy.f2py._backends._mesonr   r,   rB   rC   r.   rM   rF   rN   rP   rS   rU   rW   rb   r   r   r   NUMPY_INSTALL_ROOTlistr   r[   r`   rf   rt   r   r   r   r   r   r   contextmanagerr   rJ   r7   r+   <module>r     sc    	 
          " + # 4>* *, G1 <<8h..55<<*//
;<L	2 
') L& 
L&^ 
5 
56'\ '%&Z? ?N  r7   