
    G/Ph/                        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Zd	Zd
Z G d d          Zes e            Ze                                 d Z d Z!d Z"d Z#da$da%ej&        dk    r8 ee'          j(        j(        j(        Z) e*e)                    d                    Z+d Z,d Z-d Z.d Z/e/g g g dfd            Z0e/g g g ddfd            Z1 G d de          Z2d!dZ3 G d d          Z4d Z5ej6        d              Z7d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)MesonBackendc                    t           j        dk    rt          j        dd           t	          j                    }	 t          j                            |d          }t          |d          5 }|
                    d           |
                    d|  d	           |rX|
                    |  d
|  d	           |
                    |  d| d           |
                    d|  d|  d|  d|  d	           d d d            n# 1 swxY w Y   	 t          j        g dd|d          }n+# t          j        $ r t          j        dd           Y nw xY w|j        dk    t          j        |           S # 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         U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numpy/f2py/tests/util.pycheck_languager,   !   s'   
|w@UYZZZZFW\\&-88
*c"" 
	aGG2333GG0d000111 4KKKKKLLL4??L???@@@8T 8 88 8048 8#8 8 8  
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
		h!~***#	  HH , 	h 	h 	hKMbfgggggg	h"a'ffsT   0E1 +B	D 4E1  DE1 DE1 D' &E1 '%EE1 EE1 1F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dS )CompilerCheckerc                 >    d| _         d| _        d| _        d| _        d S )NF)compilers_checkedhas_chas_f77has_f90selfs    r+   __init__zCompilerChecker.__init__W   s#    !&
    c                 *   | j         st          j        dk    st          j                                        5 }|                    t          d          |                    t          dt                    |                    t          dt                    g}|d         
                                | _        |d         
                                | _        |d         
                                | _        d d d            n# 1 swxY w Y   d| _         d S d S d S )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_compilers]   s%   & 	*1I1I#6688 	3HOONC88OONI~NNOONI~NN %QZ..00
&qz0022&qz0022	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 &*D"""	* 	*1I1Is   B:C==DDN)__name__
__module____qualname__r6   rF    r7   r+   r.   r.   V   s2          * * * * *r7   r.   c                      t           j        S N)checkerr1   rJ   r7   r+   has_c_compilerrN   p   s
    =r7   c                      t           j        S rL   )rM   r2   rJ   r7   r+   has_f77_compilerrP   s   
    ?r7   c                      t           j        S rL   )rM   r3   rJ   r7   r+   has_f90_compilerrS   v   rQ   r7   c                  2    t           j        ot           j        S rL   )rM   r3   r2   rJ   r7   r+   has_fortran_compilerrU   y   s    O/0r7   i  r9   z**/*.dllc                      t           e	 t          j                            t                      n# t          $ r Y nw xY w	 t          j        t                      n# t          $ r Y nw xY wd a d S d S rL   )_module_dirr   r   remove
ValueErrorr#   r$   OSErrorrJ   r7   r+   _cleanupr[      s    	HOOK(((( 	 	 	D		M+&&&& 	 	 	D	 s   $. 
;;A 
A&%A&c                      t           dt          j                    a t          j        t
                     t           t          j        vr%t          j                            dt                      t           S )Nr   )	rW   r   r   atexitregisterr[   r   r   insertrJ   r7   r+   get_module_dirr`      sO    &((!!!ch&&HOOA{+++r7   c                      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      sE      ;.D1Ks{BCCCKr7   c                 2     i  fd} j         |_         |S )Nc                      t          | |f          }|vr%	  | i ||<   n# t          $ r}||<    d }~ww xY w|         }t          |t                    r||S rL   )repr	Exception
isinstance)akwkeyeretfuncmemos        r+   wrapperz_memoize.<locals>.wrapper   s    Ar7mmd?? D!NrNNS		   S	 3ic9%% 	I
s   $ 
949)rG   )rq   rs   rr   s   ` @r+   _memoizert      s8    D      }GNr7   c           
      *   dt           j        d}t                      }t                      st	          j        d           g }g }| D ]}	t          j                            |	          st          d|	z            t          j        	                    |t          j        
                    |	                    }
t          j        |	|
           |                    |
           t          j                            |
          \  }}|dv r|                    |
           |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        dk    r*t          d|dd         dt3          |                    	 t          j        |           |D ]}	t          j        |	           n2# t          j        |           |D ]}	t          j        |	           w xY wt           j        dk    r{t8                              t=          j        t          j        	                    |d                    |                                         t'          j         g dt8          z              tC          |          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:    
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      sM    XSXWWWDA!! 53444 KL 	% 	%w~~b!! 	81B6777gll1bg..r2233C   3G$$S))	c666$$$ *,,K#7227V^e7e7e34t[)G3kALPI+w''I &gY%%	 &gY%%	 )++C
~tT*Y6S$.O$.$57 7 7 ==??S<1, #ABB3 9 : : :  	  	 	BIbMMMM	 	  	 	BIbMMMM	 |x 	Ibgll1gnn[&A&ABBCC	
 	
 	
 	III	
 	
 	
 %%%s   =BI	 	/I8c                    |d}t          |          5 }t          |d          5 }|                    |            ddd           n# 1 swxY w Y   t          |g||||          cddd           S # 1 swxY w Y   dS )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     s    ~		 	 	  5D$__ 	!GGK   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!TF$+!%!%(3	5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s3   A4AA4A	A4A	A44A8;A8c                   $     e Zd Z fdZd Z xZS )SimplifiedMesonBackendc                 :     t                      j        |i | d S rL   )superr6   )r5   argskwargs	__class__s      r+   r6   zSimplifiedMesonBackend.__init__3  s%    $)&)))))r7   c                 n    |                      | j                   |                     | j                   d S rL   )write_meson_build	build_dir	run_mesonr4   s    r+   compilezSimplifiedMesonBackend.compile6  s2    t~...t~&&&&&r7   )rG   rH   rI   r6   r   __classcell__)r   s   @r+   r   r   2  sG        * * * * *' ' ' ' ' ' 'r7   r   c           	         t                      st          j        d           t                      }|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.
    rv   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   /rJ   )rU   r   r   r`   rf   r   getr   r   r   r_   meson_build_dirr   )r   r   r   r   backends        r+   build_mesonr   ;  s     !! 53444  I*,, %   ; jj"555 )	
 ZZ333 ZZ333 **["--- jj"555 ZZ333 ::lB/// jj"555 J+++ ::lB/// JJ}b111  $6>>>  **["---!G& OO HOOA)??g&=??@@@%%%r7   c                   h    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dS )F2PyTestNrx   c                 z    t          |           }d|j                            dd          d          d|j         dS )N_.r<   _ext_module)typerH   rsplitrG   )r5   clss     r+   r   zF2PyTest.module_namet  s?    4jjO3>((Q//3OOclOOOOr7   c                    t           j        dk    rt          j        d           t	                      t
          _        t                      t
          _        t                      t
          _
        t                      t
          _        d S )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_classy  s^    <7""KCDDD#1#3#3 %5%7%7"%5%7%7")=)?)?&&&r7   c                    | j         d S | j        r| j        ng }| j        r|                    | 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        8t          | j        | j        | j        | j        | j        | j                  | _         | j        4t          | j        | j        | j        | j        | j                  | _         d S d S )	Nc              3   Z   K   | ]&}t          |                              d           V  'dS )rx   Nstrendswith.0r   s     r+   	<genexpr>z(F2PyTest.setup_method.<locals>.<genexpr>  s6      ??2B((..??????r7   c              3   Z   K   | ]&}t          |                              d           V  'dS )rw   Nr   r   s     r+   r   z(F2PyTest.setup_method.<locals>.<genexpr>  6      AARB((00AAAAAAr7   c              3   Z   K   | ]&}t          |                              d           V  'dS )ry   Nr   r   s     r+   r   z(F2PyTest.setup_method.<locals>.<genexpr>  r   r7   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  s   ;"F $4"9 	&LL%%%???????	AA5AAAAA	AA5AAAAA	 	<T3 	<K:;;; 	<T3 	<K:;;; 	9T7 	9K7888 9 $	YY{ ,  DK <#&YY ,  DKKK $#r7   )rG   rH   rI   r   r   r   r   r   r   r   r   r   r   propertyr   classmethodr   r   rJ   r7   r+   r   r   h  s        DGGDDFFOP P XP @ @ [@% % % % %r7   r   c                      t          t          j        j                  j                                        } |j        |  S rL   )r   numpyf2py__file__parentresolvejoinpath)rl   r   s     r+   getpathr     s1    UZ !!(0022A1:q>r7   c              #      K   t          j                    }t          j        |            	 d V  t          j        |           d S # t          j        |           w xY wrL   )r   r   r   r   )r   curpaths     r+   	switchdirr     sX      hjjGHTNNN
s   A ArL   )8__doc__r   r   r   r   r   r#   r]   textwraprer   
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     s&     				 



         				                 " " " " " " + + + + + + + + # # # # # # 4 4 4 4 4 4   @* * * * * * * *,  oG      1 1 1 <8h.5<4*//
;;<<L    	 	 	  2 
') L& L& L& 
L&^ 
5 5 5 
56' ' ' ' '\ ' ' '%& %& %& %&Z? ? ? ? ? ? ? ?N       r7   