
    BkfP-                     z   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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            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,-                                 d Z.d Z/d Z0 G d de          Z1d dZ2 G d d          Z3d Z4ej5        d             Z6d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)MesonBackendi  cygwinz**/*.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 N)_module_dirsyspathremove
ValueErrorshutilrmtreeOSError     U/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/numpy/f2py/tests/util.py_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   )	r   tempfilemkdtempatexitregisterr   r   r   insertr   r   r   get_module_dirr   7   sO    &((!!!ch&&HOOA{+++r   c                      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   sE      ;.D1Ks{BCCCKr   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 r   )repr	Exception
isinstance)akwkeyeretfuncmemos        r   wrapperz_memoize.<locals>.wrapperP   s    Ar7mmd?? D!NrNNS		   S	 3ic9%% 	I
s   $ 
949)__name__)r0   r2   r1   s   ` @r   _memoizer4   M   s8    D      }GNr   c           
         dt           j        d}t                      }g }g }| D ]}	t          j                            |	          st          d|	z            t          j                            |t          j                            |	                    }
t          j	        |	|
           |
                    |
           t          j                            |
          \  }}|dv r|
                    |
           |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        dk    r*t          d|dd         dt-          |                    	 t          j        |           |D ]}	t          j        |	           n2# t          j        |           |D ]}	t          j        |	           w xY wt           j        dk    r{t2                              t7          j        t          j                            |d                    |                                         t!          j        g dt2          z              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()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   s    XSXWWWDA KL 	% 	%w~~b!! 	81B6777gll1bg..r2233C   3G$$S))	c666$$$ *,,t[)G3lBI+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H /I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.

    Nr7   )suffixwrV   rW   rX   rY   )r   openwriterh   )source_coderV   rW   rX   rj   rY   r   fs           r   
build_coderq      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                    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|t          j	        t          j	                  }|j
        dk    r	 t          j        |           dS 	 t          j        |           dS # 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}      s   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  
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 >&&&??
 
 
 !## 	f ffs0   0E B	CE CE !C"=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dS )CompilerCheckerc                 >    d| _         d| _        d| _        d| _        d S )NF)compilers_checkedhas_chas_f77has_f90selfs    r   __init__zCompilerChecker.__init__   s#    !&
r   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 )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%   & 	*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)r3   
__module____qualname__r   r   r   r   r   r   r      s2          * * * * *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                      j        |i | d S r   )superr   )r   argskwargs	__class__s      r   r   zSimplifiedMesonBackend.__init__&  s%    $)&)))))r   c                 n    |                      | j                   |                     | j                   d S r   )write_meson_build	build_dir	run_mesonr   s    r   compilezSimplifiedMesonBackend.compile)  s2    t~...t~&&&&&r   )r3   r   r   r   r   __classcell__)r   s   @r   r   r   %  sG        * * * * *' ' ' ' ' ' 'r   r   c           	         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           }	 |                                 n)# t
          j        $ r t          j        d           Y nw xY wt          j
                            d| d|j                    t          |          S )z1
    Build a module via Meson and import it.
    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_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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,0( 0 0 0./////0 HOOA)??g&=??@@@%%%s   E3 3#FF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 )F2PyTestNr7   c                 z    t          |           }d|j                            dd          d          d|j         dS )N_.r    _ext_module)typer   rsplitr3   )r   clss     r   rY   zF2PyTest.module_nameh  s?    4jjO3>((Q//3OOclOOOOr   c                     t           j        dk    rt          j        d           t	                      t
          _        t                      t
          _        t                      t
          _
        d S )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  sP    <7""KCDDD#1#3#3 %5%7%7"%5%7%7"""r   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| 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 )r7   Nstrendswith.0r^   s     r   	<genexpr>z(F2PyTest.setup_method.<locals>.<genexpr>}  s6      ??2B((..??????r   c              3   Z   K   | ]&}t          |                              d           V  'dS )r6   Nr   r   s     r   r   z(F2PyTest.setup_method.<locals>.<genexpr>~  6      AARB((00AAAAAAr   c              3   Z   K   | ]&}t          |                              d           V  'dS )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   ;"F $4"9 	&LL%%%???????	AA5AAAAA	AA5AAAAA	 	<T3 	<K:;;; 	<T3 	<K:;;; 	9d4 	98N 	9K7888 9 $	YY{ ,  DK <#&YY ,  DKKK $#r   )r3   r   r   rZ   r   rV   rW   rX   rj   r   r   r   r   propertyrY   classmethodr   r   r   r   r   r   r   \  s        DGGDDFFOP P XP 8 8 [8% % % % %r   r   c                      t          t          j        j                  j                                        } |j        |  S r   )r   numpyf2py__file__parentresolvejoinpath)r+   r[   s     r   getpathr     s1    UZ !!(0022A1:q>r   c              #      K   t          j                    }t          j        |            	 d V  t          j        |           d S # t          j        |           w xY wr   )r   rc   r>   rF   )r   curpaths     r   	switchdirr     sX      hjjGHTNNN
s   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      s     				 



         				                 " " " " " " + + + + + + + + # # # # # # 4 4 4 4 4 4 <8h.5<4*//
;;<<L    	 	 	  2 
') E& E& E& 
E&P 
5 5 5 
54   <* * * * * * * *,  oG      ' ' ' ' '\ ' ' '&& && && &&\> > > > > > > >L       r   