
    Ng+                        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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a&d Z'd Z(d Z)d Z*e#d             Z+ G d d          Z,d Z-ej.        d             Z/d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)asstr)	asunicode)temppathIS_WASM)import_modulei  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     Q/var/www/html/ai-engine/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   6   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&   @   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>.wrapperO   s    Ar7mmd?? D!NrNNS		   S	 3ic9%% 	I
s   $ 
949)__name__)r1   r3   r2   s   ` @r   _memoizer5   L   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   }|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()%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   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...$$$ *,,t[)G3lBI &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 /H?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.

    Nr9   )suffixwrX   rY   rZ   r[   )r   openwriterj   )source_coderX   rY   rZ   rl   r[   r   fs           r   
build_coders      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           t           S da t          rt           S t          j        dt	          t
          j                   d          } | t          t	          t
          j                            z  } t          j	                    }	 t          j                            |d          }t          |d          5 }|                    |            d d d            n# 1 swxY w Y   t
          j        ddg}t          j        |t          j        t          j        |          }|                                \  }}t)          j        |           n# t)          j        |           w xY wt-          j        d	|          }|rt1          t3          |                    d
                              t1          t3          |                    d                              t1          t3          |                    d                              fa t           S )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   #,   ?  NN    D* $tCH~~....DFfj11&# 	!GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ~z84S$.O$.$5!') ) ) ==??Sff
	1377A 
QWWQZZ!!QWWQZZ!!QWWQZZ!!
 s1   0E <CE C""E %C"&AE E&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                 v   t                      }g }| D ]}t          j                            |          st	          d|z            t          j                            |t          j                            |                    }t          j        ||           |	                    |           t          j        |                              dd          }dt          t          j                   d| d}t          j                            |t                      dz             }	|	                    |	           t!          |	d          5 }
|
                    |                    d	                     d
d
d
           n# 1 swxY w Y   t          j                    }	 t          j        |           t          j        |	ddg}t-          j        |t,          j        t,          j                  }|                                \  }}|j        dk    r*t	          d|dd
         dt9          |                    	 t          j        |           |D ]}t          j        |           n2# t          j        |           |D ]}t          j        |           w xY wt=          |           t          j        |         S )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    w~~b!! 	81B6777gll1bg..r2233C   3 /+..66tXFFK NN    D W\\!133e;<<Fv	fd		 'q	H%%&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' )++C
~v{D9S$.O$.$57 7 7 ==??S<1, #ABBs 5 6 6 6  	  	 	BIbMMMM	 	  	 	BIbMMMM	 {;{##s   
)E??FFBI( (/Jc                   F    e Zd ZdZdZg Zg Zg ZdZdZ	e
d             Zd ZdS )F2PyTestNr9   c                 z    t          |           }d|j                            dd          d          d|j         dS )N_.r!   _ext_module)type
__module__rsplitr4   )selfclss     r   r[   zF2PyTest.module_namej  s?    4jjO3>((Q//3OOclOOOOr   c                    t           j        dk    rt          j        d           | j        d S t                      st          j        d           g }| j        r|                    | j                   | j        |	                    | j
                   d}d}d}|D ]p}t          |                              d          rd}'t          |                              d          rd}Lt          |                              d          rd}q|r"t                      st          j        d	           |r"t                      st          j        d
           |r0t                      s"t                      st          j        d           | j        8t          | j        | j        | j        | j        | j
        | j                  | _        | j        4t'          | j        | j        | j        | j        | j                  | _        d S d S )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""KCDDD;"F  	3K1222< 	'LL&&&9 LL%%%			 	! 	!B2ww%% ! 		R!!&)) ! 		R!!&)) ! 	 	<-// 	<K:;;; 	<-// 	<K:;;; 	9.00 	94D4F4F 	9K7888 9 $	YY{ ,  DK <#&YY ,  DKKK $#r   )r4   r   __qualname__r\   r   rX   rY   rZ   rl   r   propertyr[   r   r   r   r   r   r   a  sb        DGGDDFFP P XP4 4 4 4 4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   re   r@   rH   )r   curpaths     r   	switchdirr     sX      hjjGHTNNN
s   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<4*//
;;<<L    	 	 	  2 
') F& F& F& 
F&R 
5 5 5 
54  ; ; ;|% % %% % %% % % 
<$ <$ 
<$HB B B B B B B BT       r   