a
    p=ic6M                     @   sH  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	Z	ddl
mZ d dlmZ eddZdrd
dZe	jdddd Ze	jdddd Ze	jdddd Zdd Zdd Zdd Ze	jjdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Z d(d) Z!d*d+ Z"d,d- Z#d.d/ Z$d0d1 Z%d2d3 Z&d4d5 Z'e	jjd6d7d8d9 Z(d:d; Z)d<d= Z*d>d? Z+d@dA Z,dBdC Z-dDdE Z.dFdG Z/dHdI Z0dJdK Z1e	jjd6d7dLdM Z2dNdO Z3dPdQ Z4dRdS Z5dTdU Z6dVdW Z7dXdY Z8dZd[ Z9d\d] Z:d^d_ Z;d`da Z<dbdc Z=ddde Z>dfdg Z?dhdi Z@djdk ZAdldm ZBdndo ZCdpdq ZDdS )s    N)Path)
namedtuple   )util)mainPPathsz(finp, f90inp, pyf, wrap77, wrap90, cmodfuntitledc              
   C   sR   t | }t|d|d|d|| d|| d|| ddS )ax  Takes in a temporary file for testing and returns the expected output and input paths

    Here expected output is essentially one of any of the possible generated
    files.

    ..note::

         Since this does not actually run f2py, none of these are guaranteed to
         exist, and module names are typically incorrect

    Parameters
    ----------
    fname_inp : str
                The input filename
    mname : str, optional
                The name of the module, untitled by default

    Returns
    -------
    genp : NamedTuple PPaths
            The possible paths which are generated, not all of which exist
    z.fz.f90.pyfz-f2pywrappers.fz-f2pywrappers2.f90module.c)finpf90inppyfwrap77Zwrap90cmodf)r   r   with_suffix	with_name)Z	fname_inpmnamebpath r   m/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/numpy/f2py/tests/test_f2py2e.pyget_io_paths   s    r   session)scopec                 C   s2   t dddd }|  d }|j|dd |S )z'Generates a single f90 file for testingtestssrcclizhiworld.f90z	hello.f90asciiencodingr   Zgetpath	read_textZgetbasetemp
write_textZtmpdir_factoryZfdatfnr   r   r   hello_world_f908   s    r$   c                 C   s2   t dddd }|  d }|j|dd |S )'Generates a single f77 file for testingr   r   r   zhi77.fzhello.fr   r   r   r"   r   r   r   hello_world_f77A   s    r&   c                 C   s2   t dddd }|  d }|j|dd |S )r%   r   r   Zreturn_realzfoo77.fzfoo.fr   r   r   r"   r   r   r   retreal_f77J   s    r'   c                 C   s   t |}t |jd }|tdd| d|   t|j@ t  | 	 \}}d|v s`J t | 
 srJ W d   n1 s0    Y  dS )zIEnsures that a signature file is generated via the CLI
    CLI :: -h
    r	   argvzf2py -h  Saving signatures to fileN)r   stemsetattrsyssplitr   	switchdirparentf2pycli
readouterrexists)capfdr$   monkeypatchipathZopathout_r   r   r   test_gen_pyfS   s    r9   c                 C   sn   t |}|tdd|   t|j. t  |  \}}d|v sLJ W d   n1 s`0    Y  dS )zHEnsures that a signature file can be dumped to stdout
    CLI :: -h
    r(   zf2py -h stdout r*   N	r   r,   r-   r.   r   r/   r0   r1   r2   r4   r$   r5   r6   r7   r8   r   r   r   test_gen_pyf_stdoutb   s    r<   c              	   C   s   t |}|tdd|   t|jj t djddd t	t
. t  |  \}}d|v sjJ W d   n1 s~0    Y  W d   n1 s0    Y  dS )	zjEnsures that the CLI refuses to overwrite signature files
    CLI :: -h without --overwrite-signature
    r(   f2py -h faker.pyf 	faker.pyf	Fake newsr   r   z&Use --overwrite-signature to overwriteN)r   r,   r-   r.   r   r/   r0   r!   pytestraises
SystemExitr1   r2   )r4   r$   r5   r6   r8   errr   r   r   test_gen_pyf_no_overwriten   s    rD   c                 C   s   t |dd}|j}d}d}|tdd| d|   t|jf t  | 	 \}}| D ]}	d|	 d	|v s`J q`| D ]}
d
|
 d|v sJ qW d   n1 s0    Y  dS )z9Tests that functions can be skipped
    CLI :: skip:
    testr   t0 t4 t8 sd s8 s4td s0r(   f2py z -m test skip: z=buildmodule: Could not found the body of interfaced routine "". Skipping.Constructing wrapper function ""N
r   r   r,   r-   r.   r   r/   r0   r1   r2   )r4   r'   r5   foutlr6   toskip	remainingr7   rC   skeyrkeyr   r   r   test_f2py_skip}   s$    
rS   c                 C   s   t |dd}|j}d}d}|tdd| d|   t|jf t  | 	 \}}| D ]}	d|	 d	|v s`J q`| D ]}
d
|
 d|v sJ qW d   n1 s0    Y  dS )z>Test that functions can be kept by only:
    CLI :: only:
    rE   rF   rG   rH   r(   rI   z -m test only: <buildmodule: Could not find the body of interfaced routine "rJ   rK   rL   NrM   )r4   r'   r5   rN   r6   rO   tokeepr7   rC   rQ   rR   r   r   r   test_f2py_only   s$    
rV   c                 C   s   t |dd}|j}d}t|}d}d}	|tdd| d|	 d	| d
|   t|jf t	  | 
 \}
}| D ]}d| d|v sxJ qx| D ]}d| d|
v sJ qW d   n1 s0    Y  dS )zfTests that it is possible to return to file processing mode
    CLI :: :
    BUG: numpy-gh #20520
    rE   rF   rG   ztd s0 hiblahr(   rI    -m z only: z : rT   rJ   rK   rL   N)r   r   r   r,   r-   r.   r   r/   r0   r1   r2   )r4   r$   r'   r5   rN   r6   rO   Zipath2rU   r   r7   rC   rQ   rR   r   r   r   test_file_processing_switch   s*    
rY   c                 C   s   d}t ||d}|j}|tdd| d|   t|j t  W d   n1 s\0    Y  t	
|jsvJ t	
|jsJ dS )zHChecks the generation of files based on a module name
    CLI :: -m
    hirF   r(   rI   rX   N)r   r   r,   r-   r.   r   r/   r0   r1   r   r3   r   r   )r4   r$   r5   ZMNAMErN   r6   r   r   r   test_mod_gen_f77   s    $r[   c           	      C   s&  t |dd}|j}td}td}|tdd| d  t|j	F t
  |  \}}||dusrJ ||du sJ W d   n1 s0    Y  |tdd| d	  t|j	H t
  |  \}}||du sJ ||dusJ W d   n1 s0    Y  dS )
zHLowers cases by flag or when -h is present

    CLI :: --[no-]lower
    rE   rF   zHI\(\)zhi\(\)r(   rI   z -m test --lowerNz -m test --no-lower)r   r   recompiler,   r-   r.   r   r/   r0   r1   r2   search	r4   r&   r5   rN   r6   ZcapshiZcapslor7   r8   r   r   r   test_lower_cmod   s$    

0r`   c           	   	   C   s6  t |dd}|j}td}td}|tdd| d|j d  t	|j
F t  |  \}}||d	uszJ ||d	u sJ W d	   n1 s0    Y  |tdd| d|j d
  t	|j
H t  |  \}}||d	u sJ ||d	usJ W d	   n1 s(0    Y  d	S )z^Lowers cases in signature files by flag or when -h is present

    CLI :: --[no-]lower -h
    rE   rF   z	Block: HIz	Block: hir(   rI   z -h z -m test --overwrite-signatureNz) -m test --overwrite-signature --no-lower)r   r   r\   r]   r,   r-   r   r.   r   r/   r0   r1   r2   r^   r_   r   r   r   test_lower_sig   s0    

0ra   c              
   C   s   t |}d}d}|tdd| d| d|   t|j6 t  |  \}}d| d|v shJ W d	   n1 s|0    Y  d	S )
zNEnsures that the build directory can be specified

    CLI :: --build-dir
    rW   Ztttmpr(   f2py -m r)   z --build-dir zWrote C/API module "rL   Nr:   )r4   r$   r5   r6   r   Zodirr7   r8   r   r   r   test_build_dir  s    rc   c                 C   s   t |}|tdd| d  t|j@ t djddd t  | 	 \}}d|v s`J W d	   n1 st0    Y  d	S )
zXEnsures that the build directory can be specified

    CLI :: --overwrite-signature
    r(   r=   z --overwrite-signaturer>   r?   r   r   r*   N)
r   r,   r-   r.   r   r/   r0   r!   r1   r2   r;   r   r   r   test_overwrite-  s    rd   c              	   C   s   t |}d}|tdd| d| d  t|jp t  |  \}}d|v sXJ t | d	  }d|
 v s|J W d	   n1 s0    Y  W d	   n1 s0    Y  d	S )
zJEnsures that TeX documentation is written out

    CLI :: --latex-doc
    rW   r(   rb   r)   z --latex-docDocumentation is saved to file
module.tex\documentclassNr   r,   r-   r.   r   r/   r0   r1   r2   openreadr4   r$   r5   r6   r   r7   r8   Zotexr   r   r   test_latexdoc>  s    rl   c              	   C   sz   t |}d}|tdd| d| d  t|j. t  |  \}}d|vsXJ W d   n1 sl0    Y  dS )zMEnsures that TeX documentation is written out

    CLI :: --no-latex-doc
    rW   r(   rb   r)   z --no-latex-docre   Nr:   r4   r$   r5   r6   r   r7   r8   r   r   r   test_nolatexdocP  s    rn   c              	   C   s   t |}d}|tdd| d| d  t|jr t  |  \}}d|v sXJ t d| d	  }d	|
 vs~J W d
   n1 s0    Y  W d
   n1 s0    Y  d
S )zEnsures that truncated documentation is written out

    TODO: Test to ensure this has no effect without --latex-doc
    CLI :: --latex-doc --short-latex
    rW   r(   rb   r)   z --latex-doc --short-latexre   ./rf   rg   Nrh   rk   r   r   r   test_shortlatex`  s    rp   c              	   C   s   t |}d}|tdd| d| d  t|jr t  |  \}}d|v sXJ t d| d	  }d	|
 v s~J W d
   n1 s0    Y  W d
   n1 s0    Y  d
S )zIEnsures that RsT documentation is written out

    CLI :: --rest-doc
    rW   r(   rb   r)   z --rest-doc#ReST Documentation is saved to filero   zmodule.restz.. -*- rest -*-Nrh   )r4   r$   r5   r6   r   r7   r8   Zorstr   r   r   test_restdocv  s    rr   c              	   C   sz   t |}d}|tdd| d| d  t|j. t  |  \}}d|vsXJ W d   n1 sl0    Y  dS )zLEnsures that TeX documentation is written out

    CLI :: --no-rest-doc
    rW   r(   rb   r)   z --no-rest-docrq   Nr:   rm   r   r   r   test_norestexdoc  s    rs   c              	   C   s   t |}d}|tdd| d| d  t|jZ t  t d| d  }d|	 v sfJ W d	   n1 sz0    Y  W d	   n1 s0    Y  d	S )
zIEnsures that debugging wrappers are written

    CLI :: --debug-capi
    rW   r(   rb   r)   z --debug-capiro   r
   z#define DEBUGCFUNCSN
r   r,   r-   r.   r   r/   r0   r1   ri   rj   )r4   r$   r5   r6   r   ocmodr   r   r   test_debugcapi  s    rv   zConsistently fails on CI.)reasonc              	   C   s   t | }d}|tdd| d| d  t|jZ t  td}t	j
|ddd	}d
}td}|j|ksvJ |j|ksJ W d   n1 s0    Y  dS )zEEnsures that debugging wrappers work

    CLI :: --debug-capi -c
    rW   r(   rb   r)   z -c --debug-capiz#python3 -c "import blah; blah.hi()"TUTF-8capture_outputr    Hello World
a   debug-capi:Python C/API function blah.hi()
debug-capi:float hi=:output,hidden,scalar
debug-capi:hi=0
debug-capi:Fortran subroutine `f2pywraphi(&hi)'
debug-capi:hi=0
debug-capi:Building return value.
debug-capi:Python C/API function blah.hi: successful.
debug-capi:Freeing memory.
        N)r   r,   r-   r.   r   r/   r0   r1   shlex
subprocessruntextwrapdedentstdoutstderr)r$   r5   r6   r   cmd_runrouteoutZeerrr   r   r   test_debugcapi_bld  s    


r   c              	   C   s   t |}d}|tdd| d|   t|j t  W d   n1 sR0    Y  |  \}}d|v stJ |tdd| d| d  t|j. t  |  \}}d|v sJ W d   n1 s0    Y  dS )zoEnsures that fortran subroutine wrappers for F77 are included by default

    CLI :: --[no]-wrap-functions
    rW   r(   rb   r)   N Fortran 77 wrappers are saved toz --wrap-functionsr:   rm   r   r   r   test_wrapfunc_def  s    $r   c              	   C   sz   t |}d}|tdd| d| d  t|j. t  |  \}}d|vsXJ W d   n1 sl0    Y  dS )zeEnsures that fortran subroutine wrappers for F77 can be disabled

    CLI :: --no-wrap-functions
    rW   r(   rb   r)   z --no-wrap-functionsr   Nr:   rm   r   r   r   test_nowrapfunc  s    r   c              	   C   s   t |}d}|tdd| d| d  t|jj t  t d| d 0}|	 }d|v sjJ d	|v svJ W d
   n1 s0    Y  W d
   n1 s0    Y  d
S )zcAdd to the include directories

    CLI :: -include
    TODO: Document this in the help string
    rW   r(   rb   r)   z' -include<stdbool.h> -include<stdio.h> ro   r
   z#include <stdbool.h>z#include <stdio.h>Nrt   )r4   r$   r5   r6   r   ru   Zocmrr   r   r   test_inclheader  s    r   c                   C   s   dS )z?Add to the include directories

    CLI :: --include-paths
    Nr   r   r   r   r   test_inclpath  s    r   c                   C   s   dS )z;Add to the include directories

    CLI :: --help-link
    Nr   r   r   r   r   
test_hlink  s    r   c                   C   s   dS )zOCheck that Fortran-to-Python KIND specs can be passed

    CLI :: --f2cmap
    Nr   r   r   r   r   test_f2cmap  s    r   c                 C   st   t |}|tdd| d  t|j2 t  |  \}}t	|dksRJ W d   n1 sf0    Y  dS )z)Reduce verbosity

    CLI :: --quiet
    r(   f2py -m blah z --quietr   N)
r   r,   r-   r.   r   r/   r0   r1   r2   lenr;   r   r   r   
test_quiet!  s    r   c                 C   sp   t |}|tdd| d  t|j. t  |  \}}d|v sNJ W d   n1 sb0    Y  dS )z-Increase verbosity

    CLI :: --verbose
    r(   r   z
 --verboseZanalyzelineNr:   r;   r   r   r   test_verbose/  s    r   c                 C   sl   | tdd  tt< t  |  \}}ddl}|j	|
 ksJJ W d   n1 s^0    Y  dS )z"Ensure version

    CLI :: -v
    r(   zf2py -vr   N)r,   r-   r.   r@   rA   rB   r1   r2   numpy__version__strip)r4   r5   r7   r8   npr   r   r   test_version=  s    r   c                 C   s   t | }|tdd| d  t|jB t  td}t	j
|ddd}d}|j|ksbJ W d	   n1 sv0    Y  d	S )
z
    CLI :: -c
    r(   r   z -cz"python -c "import blah; blah.hi()"Trx   ry   r{   N)r   r,   r-   r.   r   r/   r0   r1   r|   r}   r~   r   )r$   r5   r6   r   r   r   r   r   r   test_npdistopK  s    
r   c                   C   s   dS )z
    CLI :: -c --fcompiler
    Nr   r   r   r   r   test_npd_fcompiler_  s    r   c                   C   s   dS )z
    CLI :: -c --compiler
    Nr   r   r   r   r   test_npd_compilerg  s    r   c                   C   s   dS )z$
    CLI :: -c --help-fcompiler
    Nr   r   r   r   r   test_npd_help_fcompilero  s    r   c                   C   s   dS )z
    CLI :: -c --f77exec
    Nr   r   r   r   r   test_npd_f77execw  s    r   c                   C   s   dS )z
    CLI :: -c --f90exec
    Nr   r   r   r   r   test_npd_f90exec  s    r   c                   C   s   dS )z
    CLI :: -c --f77flags
    Nr   r   r   r   r   test_npd_f77flags  s    r   c                   C   s   dS )z
    CLI :: -c --f90flags
    Nr   r   r   r   r   test_npd_f90flags  s    r   c                   C   s   dS )z
    CLI :: -c --opt
    Nr   r   r   r   r   test_npd_opt  s    r   c                   C   s   dS )z
    CLI :: -c --arch
    Nr   r   r   r   r   test_npd_arch  s    r   c                   C   s   dS )z
    CLI :: -c --noopt
    Nr   r   r   r   r   test_npd_noopt  s    r   c                   C   s   dS )z
    CLI :: -c --noarch
    Nr   r   r   r   r   test_npd_noarch  s    r   c                   C   s   dS )z
    CLI :: -c --debug
    Nr   r   r   r   r   test_npd_debug  s    r   c                   C   s   dS )z%
    CLI :: -c --link-<resource>
    Nr   r   r   r   r   test_npd_link_auto  s    r   c                   C   s   dS )z/
    CLI :: -c -L/path/to/lib/ -l<libname>
    Nr   r   r   r   r   test_npd_lib  s    r   c                   C   s   dS )z
    CLI :: -D<define>
    Nr   r   r   r   r   test_npd_define  s    r   c                   C   s   dS )z
    CLI :: -U<name>
    Nr   r   r   r   r   test_npd_undefine  s    r   c                   C   s   dS )z$
    CLI :: -I/path/to/include/
    Nr   r   r   r   r   test_npd_incl  s    r   c                   C   s   dS )z8
    CLI :: <filename>.o <filename>.so <filename>.a
    Nr   r   r   r   r   test_npd_linker  s    r   )r   )Er   r\   r-   r}   r|   pathlibr   collectionsr   r@    r   Znumpy.f2py.f2py2er   r1   r   r   Zfixturer$   r&   r'   r9   r<   rD   markZxfailrS   rV   rY   r[   r`   ra   rc   rd   rl   rn   rp   rr   rs   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s|   (

'






'
			
