
    ~Od(                     n   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mZ ddlmZmZ ddlmZ dada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)asbytesasstr)temppathIS_WASM)import_modulei  c                      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     5lib/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   0   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>.wrapperI   s    Ar7mmd?? D!NrNNS		   S	 3ic9%% 	I
s   $ 
949)__name__)r0   r2   r1   s   ` @r   _memoizer4   F   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t1          |          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   r   r   osisfiler#   joinbasenamer   copyfileappendsplitextr%   getcwdchdir
executable
subprocessPopenPIPESTDOUTcommunicate
returncoder   unlinkr   )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_modulerc   _   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s 5 6 6 6  	  	 	BIbMMMM	 	  	 	BIbMMMM	 %%%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.

    Nr8   )suffixwrQ   rR   rS   rT   )r   openwriterc   )source_coderQ   rR   rS   re   rT   r   fs           r   
build_coderl      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.pyrf   config)r;   r<   r^   s   COMPILERS:(\d+),(\d+),(\d+)r          )_compiler_statusr   textwrapdedentr(   r   r   dictr   r   r?   rA   rh   ri   rH   rI   rJ   rK   rL   rM   r   r   researchboolintgroup)	rU   tmpdirscriptrk   r_   r`   ra   rb   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 )Nrp   r   r   r   r   has_f90_compilerr      r   r   c                 j   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 }
|
                    t%          |                     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.

    r6   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wbN	build_extz-ir:   r   z Running distutils build failed: r=   ) r   r?   r   r@   r#   rA   rB   r   rC   rD   rs   rt   replacer(   r   r%   rh   ri   r   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r   rO   
__import__r"   )rP   config_coderT   r,   rV   rW   rY   rZ   rU   r|   rk   r^   r_   r`   ra   rb   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	               )++C
~v{D9S$.O$.$57 7 7 ==??S<1, #ABBs 5 6 6 6  	  	 	BIbMMMM	 	  	 	BIbMMMM	 {;{##s   
#E99E= E=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 )F2PyTestNr8   c                 z    t          |           }d|j                            dd          d          d|j         dS )N_.r    _ext_module)type
__module__rsplitr3   )selfclss     r   rT   zF2PyTest.module_nameU  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 availableFr8   Tr7   r9   z No Fortran 77 compiler availablez No Fortran 90 compiler availablezNo Fortran compiler available)rQ   rR   rS   re   rT   rg   )r   platformpytestrR   moduler   sourcesextendrU   rD   re   strendswithr   r   rl   rQ   rS   rT   rc   )r   codes	needs_f77	needs_f90	needs_pyfrY   s         r   setup_methodzF2PyTest.setup_methodZ  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   )r3   r   __qualname__rU   r   rQ   rR   rS   re   r   propertyrT   r   r   r   r   r   r   L  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+   rV   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   r^   r?   rG   )r   curpaths     r   	switchdirr     sX      hjjGHTNNN
s   A A)(__doc__r?   r   rI   r   r   r   rs   rv   r   
contextlibr   pathlibr   numpy.compatr   r   numpy.testingr   r   	importlibr   r   r!   r   r   r%   r4   rc   rl   rr   r~   r   r   r   r   r   r   contextmanagerr   r   r   r   <module>r      s9    
			 



         				             ' ' ' ' ' ' ' ' + + + + + + + + # # # # # #     	 	 	  2 
') 7& 7& 7& 
7&t 
5 5 5 
54  ; ; ;|% % %% % %% % % 
<$ <$ 
<$HB B B B B B B BT       r   