
    ih+                         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mZ ddlmZ ddlmZ ddlmZmZ ddlmZ dadaej4                  d	k(  r= ee      j8                  j8                  j8                  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a&d Z'd Z(d Z)d Z*e#d        Z+ G d d      Z,d Z-ej\                  d        Z/y)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)asstr)	asunicode)temppathIS_WASM)import_modulei  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     S/var/www/html/dev/engine/venv/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   6   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&   @   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>.wrapperO   sj    Ar7md? !NrNS	 3ic9%I
  S	s   8 	AAA)__name__)r1   r3   r2   s   ` @r   _memoizer5   L   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   }|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()%s is not a file).f90.fz.c.pyfNz-cz-mz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_modulerj   e   sv    %SXXL0VWDA KL 	%ww~~b!1B677ggll1bgg..r23C 3GG$$S)	c..$	% < *,t[)G3lBI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   9B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.

    Nr9   )suffixwrX   rY   rZ   r[   )r   openwriterj   )source_coderX   rY   rZ   rl   r[   r   fs           r   
build_coders      sv     ~		  5D$_ 	!GGK 	!TF$+!%!%(3	55 5	! 	!5 5s!   AAAA	AA'c            	         t         t         S da t        rt         S t        j                  dt	        t
        j                         d      } | t        t	        t
        j                              z  } t        j                         }	 t        j                  j                  |d      }t        |d      5 }|j                  |        d d d        t
        j                  ddg}t        j                   |t        j"                  t        j$                  |      }|j'                         \  }}t)        j*                  |       t-        j.                  d	|      }|rht1        t3        |j5                  d
                  t1        t3        |j5                  d                  t1        t3        |j5                  d                  fa t         S # 1 sw Y   xY w# t)        j*                  |       w xY w)N)FFFz8        import os
        import sys
        sys.path = a{  

        def configuration(parent_name='',top_path=None):
            global config
            from numpy.distutils.misc_util import Configuration
            config = Configuration('', parent_name, top_path)
            return config

        from numpy.distutils.core import setup
        setup(configuration=configuration)

        config_cmd = config.get_config_cmd()
        have_c = config_cmd.try_compile('void foo() {}')
        print('COMPILERS:%%d,%%d,%%d' %% (have_c,
                                          config.have_f77c(),
                                          config.have_f90c()))
        sys.exit(99)
        )syspathzsetup.pyrm   config)r<   r=   re   s   COMPILERS:(\d+),(\d+),(\d+)r!         )_compiler_statusr   textwrapdedentr)   r   r   dictr   r   r@   rB   ro   rp   rI   rJ   rK   rL   rM   rN   r   r   researchboolintgroup)	r\   tmpdirscriptrr   rf   rg   rh   ri   ms	            r   _get_compiler_statusr      s{   #, ??   N# $	 D* $tCHH~..DFfj1&# 	!GGDM	 ~~z84S$.OO$.$5$5!') ==?Sf
		137AQWWQZ!QWWQZ!QWWQZ!
 )	 	 	fs%   ,G 2GA#G GG G%c                      t               d   S r   r   r   r   r   has_c_compilerr         !!$$r   c                      t               d   S )Nr!   r   r   r   r   has_f77_compilerr     r   r   c                      t               d   S )Nrw   r   r   r   r   has_f90_compilerr     r   r   c                 @   t               }g }| D ]  }t        j                  j                  |      st	        d|z        t        j                  j                  |t        j                  j                  |            }t        j                  ||       |j                  |        t        j                  |      j                  dd      }dt        t        j                         d| d}t        j                  j                  |t               dz         }	|j                  |	       t!        |	d      5 }
|
j#                  |j%                  d	             d
d
d
       t        j&                         }	 t        j(                  |       t        j*                  |	ddg}t-        j.                  |t,        j0                  t,        j2                        }|j5                         \  }}|j6                  dk7  rt	        d|dd
 dt9        |            	 t        j(                  |       |D ]  }t        j:                  |        	 t=        |       t        j>                  |   S # 1 sw Y   xY w# t        j(                  |       |D ]  }t        j:                  |        w xY w)z6
    Build a module via distutils and import it.

    r7   r?   z
    z!
import os
import sys
sys.path = z

def configuration(parent_name='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('', parent_name, top_path)
    z
    return config

if __name__ == "__main__":
    from numpy.distutils.core import setup
    setup(configuration=configuration)
    z.pywblatin1N	build_extz-ir;   r   z Running distutils build failed: r>   ) r   r@   r   rA   r$   rB   rC   r   rD   rE   rz   r{   replacer)   r   r&   ro   rp   encoderG   rH   rI   rJ   rK   rL   rM   rN   rO   r   rP   
__import__r#   )rW   config_coder[   r-   r]   r^   r`   ra   r\   r   rr   re   rf   rg   rh   ri   s                   r   build_module_distutilsr     s/    	A K  ww~~b!1B677ggll1bgg..r23C 3  //+.66tXFK N 
 M D WW\\!13e;<Fv	fd	 'q	H%&' ))+C
~~v{D9S$.OO$.$5$57 ==?S<<1 #ABs 5 6 6  	  	BIIbM	 {;;{##1' '  	  	BIIbM	s   2!I0BI* I'*3Jc                   >    e Zd ZdZdZg Zg Zg ZdZdZ	e
d        Zd Zy)F2PyTestNr9   c                 x    t        |       }d|j                  j                  dd      d    d|j                   dS )N_.r!   _ext_module)type
__module__rsplitr4   )selfclss     r   r[   zF2PyTest.module_namej  s;    4j3>>((Q/34Acll^;OOr   c                 R   t         j                  dk(  rt        j                  d       | j                  y t               st        j                  d       g }| j                  r|j                  | j                         | j                  |j                  | j                         d}d}d}|D ]Y  }t        |      j                  d      rd} t        |      j                  d      rd}=t        |      j                  d      sXd}[ |rt               st        j                  d	       |rt               st        j                  d
       |r)t               st               st        j                  d       | j                  Rt        | j                  | j                   | j                  | j"                  | j                  | j$                        | _        | j                  Ht'        | j                  | j                   | j                  | j"                  | j$                        | _        y y )Nwin32z)Fails with MinGW64 Gfortran (Issue #9673)zNo C compiler availableFr9   Tr8   r:   z No Fortran 77 compiler availablez No Fortran 90 compiler availablezNo Fortran compiler available)rX   rY   rZ   rl   r[   rn   )r   rQ   pytestrY   moduler   sourcesrS   r\   rE   rl   strendswithr   r   rs   rX   rZ   r[   rj   )r   codes	needs_f77	needs_f90	needs_pyfr`   s         r   setup_methodzF2PyTest.setup_methodo  s   <<7"KKCD;;" KK12<<LL&99 LL%			 	!B2w% 	R!!&) 	R!!&) 		! -/KK:;-/KK:;.04D4FKK78 99 $		YYYY{{ ,,DK <<#&YYYY ,,DK $r   )r4   r   __qualname__r\   r   rX   rY   rZ   rl   r   propertyr[   r   r   r   r   r   r   a  s>    DGGDDFFP P4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   re   r@   rH   )r   curpaths     r   	switchdirr     s<     hhjGHHTN
s   *A!A A!AA!)0__doc__rT   r@   r   rJ   r   r   r   rz   r}   r   
contextlibr   pathlibr   numpy.compatr   numpy._utilsr   numpy.testingr   r   	importlibr   r   r"   rQ   r   r   NUMPY_INSTALL_ROOTlistrR   r   r   r&   r5   rj   rs   ry   r   r   r   r   r   r   r   contextmanagerr   r   r   r   <module>r      s>    	 
      	      " + # <<8h..55<<*//
;<L	2 
') F& 
F&R 
5 
54  ;|%%% 
<$ 
<$HB BT  r   