U
    O8cpQ                     @   s\  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dtd
dZe	jddd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$d0d1 Z%d2d3 Z&d4d5 Z'd6d7 Z(e	jjd8d9d:d; Z)d<d= Z*d>d? Z+d@dA Z,dBdC Z-dDdE Z.dFdG Z/dHdI Z0dJdK Z1dLdM Z2e	jjd8d9dNdO 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 ZDdrds ZEdS )u    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   @/tmp/pip-unpacked-wheel-fd_gsd75/numpy/f2py/tests/test_f2py2e.pyget_io_paths   s    r   session)Zscopec                 C   s2   t dddd }|  d }|j|dd |S )'Generates a single f90 file for testingtestssrcclizhiworld.f90z	hello.f90asciiencodingr   Zgetpath	read_textZgetbasetemp
write_texttmpdir_factory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 dddd }t dddd }|  d }|  d }|j|dd	 |j|dd	 |S )
r   r   r   f2cmapzisoFortranEnvMap.f90z.f2py_f2cmapz
f2cmap.f90Zmapfiler   r   r   )r#   r$   r*   r%   Zfmapr   r   r   
f2cmap_f90R   s    r+   c              	   C   s   t |}t |jd }|tdd| d|   t|j6 t  | 	 \}}d|ks`t
t |  srt
W 5 Q R X 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AssertionErrorexists)capfdr&   monkeypatchipathZopathout_r   r   r   test_gen_pyf^   s    r>   c              	   C   sf   t |}|tdd|   t|j0 t  |  \}}d|ksLt	d|ksXt	W 5 Q R X dS )zHEnsures that a signature file can be dumped to stdout
    CLI :: -h
    r,   zf2py -h stdout r.   zfunction hi() ! in N
r   r0   r1   r2   r   r3   r4   r5   r6   r7   r9   r&   r:   r;   r<   r=   r   r   r   test_gen_pyf_stdoutm   s    rA   c              
   C   s   t |}|tdd|   t|jL t djddd t	t
$ t  |  \}}d|ksjtW 5 Q R X W 5 Q R X 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   r0   r1   r2   r   r3   r4   r!   pytestraises
SystemExitr5   r6   r7   )r9   r&   r:   r;   r=   errr   r   r   test_gen_pyf_no_overwritez   s    rI   c              	   C   s   t |dd}|j}d}d}|tdd| d|   t|j\ t  | 	 \}}| D ]}	d|	 d	|ks`t
q`| D ]}
d
|
 d|kst
qW 5 Q R X 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   r0   r1   r2   r   r3   r4   r5   r6   r7   )r9   r)   r:   foutlr;   toskip	remainingr<   rH   skeyrkeyr   r   r   test_f2py_skip   s&     
rX   c              	   C   s   t |dd}|j}d}d}|tdd| d|   t|j\ t  | 	 \}}| D ]}	d|	 d	|ks`t
q`| D ]}
d
|
 d|kst
qW 5 Q R X dS )z>Test that functions can be kept by only:
    CLI :: only:
    rJ   rK   rL   rM   r,   rN   z -m test only: <buildmodule: Could not find the body of interfaced routine "rO   rP   rQ   NrR   )r9   r)   r:   rS   r;   rT   tokeepr<   rH   rV   rW   r   r   r   test_f2py_only   s&     
r[   c                 C   s   t |dd}|j}d}t|}d}d}	|tdd| d|	 d	| d
|   t|j\ t	  | 
 \}
}| D ]}d| d|ksxtqx| D ]}d| d|
kstqW 5 Q R X dS )zfTests that it is possible to return to file processing mode
    CLI :: :
    BUG: numpy-gh #20520
    rJ   rK   rL   ztd s0 hiblahr,   rN    -m z only: z : rY   rO   rP   rQ   N)r   r   r   r0   r1   r2   r   r3   r4   r5   r6   r7   )r9   r&   r)   r:   rS   r;   rT   Zipath2rZ   r   r<   rH   rV   rW   r   r   r   test_file_processing_switch   s*    
r^   c              	   C   sv   d}t ||d}|j}|tdd| d|   t|j t  W 5 Q R X t	
|jsbtt	
|jsrtdS )zHChecks the generation of files based on a module name
    CLI :: -m
    hirK   r,   rN   r]   N)r   r   r0   r1   r2   r   r3   r4   r5   r   r8   r   r7   r   )r9   r&   r:   ZMNAMErS   r;   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	< t
  |  \}}||dk	srt||dkstW 5 Q R X |tdd| d	  t|j	< t
  |  \}}||dkst||dk	stW 5 Q R X dS )
zHLowers cases by flag or when -h is present

    CLI :: --[no-]lower
    rJ   rK   zHI\(\)zhi\(\)r,   rN   z -m test --lowerNz -m test --no-lower)r   r   recompiler0   r1   r2   r   r3   r4   r5   r6   searchr7   	r9   r(   r:   rS   r;   ZcapshiZcapslor<   r=   r   r   r   test_lower_cmod   s$    

re   c           	   	   C   s
  t |dd}|j}td}td}|tdd| d|j d  t	|j
< t  |  \}}||d	k	szt||d	kstW 5 Q R X |tdd| d|j d
  t	|j
< t  |  \}}||d	kst||d	k	stW 5 Q R X d	S )z^Lowers cases in signature files by flag or when -h is present

    CLI :: --[no-]lower -h
    rJ   rK   z	Block: HIz	Block: hir,   rN   z -h z -m test --overwrite-signatureNz) -m test --overwrite-signature --no-lower)r   r   ra   rb   r0   r1   r   r2   r   r3   r4   r5   r6   rc   r7   rd   r   r   r   test_lower_sig  s0    

rf   c              
   C   sv   t |}d}d}|tdd| d| d|   t|j, t  |  \}}d| d|ksht	W 5 Q R X d	S )
zNEnsures that the build directory can be specified

    CLI :: --build-dir
    r\   Ztttmpr,   f2py -m r-   z --build-dir zWrote C/API module "rQ   Nr?   )r9   r&   r:   r;   r   Zodirr<   r=   r   r   r   test_build_dir(  s    rh   c              	   C   sn   t |}|tdd| d  t|j6 t djddd t  | 	 \}}d|ks`t
W 5 Q R X d	S )
zXEnsures that the build directory can be specified

    CLI :: --overwrite-signature
    r,   rB   z --overwrite-signaturerC   rD   r   r   r.   N)r   r0   r1   r2   r   r3   r4   r!   r5   r6   r7   r@   r   r   r   test_overwrite9  s     ri   c              
   C   s   t |}d}|tdd| d| d  t|jR t  |  \}}d|ksXt	t | d
 }d| ks|t	W 5 Q R X W 5 Q R X d	S )
zJEnsures that TeX documentation is written out

    CLI :: --latex-doc
    r\   r,   rg   r-   z --latex-docDocumentation is saved to file
module.tex\documentclassNr   r0   r1   r2   r   r3   r4   r5   r6   r7   openreadr9   r&   r:   r;   r   r<   r=   Zotexr   r   r   test_latexdocJ  s    rq   c              	   C   sf   t |}d}|tdd| d| d  t|j$ t  |  \}}d|ksXt	W 5 Q R X dS )zMEnsures that TeX documentation is written out

    CLI :: --no-latex-doc
    r\   r,   rg   r-   z --no-latex-docrj   Nr?   r9   r&   r:   r;   r   r<   r=   r   r   r   test_nolatexdoc\  s    rs   c              
   C   s   t |}d}|tdd| d| d  t|jT t  |  \}}d|ksXt	t d| d
 }d	| ks~t	W 5 Q R X W 5 Q R X 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
    r\   r,   rg   r-   z --latex-doc --short-latexrj   ./rk   rl   Nrm   rp   r   r   r   test_shortlatexl  s    ru   c              
   C   s   t |}d}|tdd| d| d  t|jT t  |  \}}d|ksXt	t d| d
 }d	| ks~t	W 5 Q R X W 5 Q R X d
S )zIEnsures that RsT documentation is written out

    CLI :: --rest-doc
    r\   r,   rg   r-   z --rest-doc#ReST Documentation is saved to filert   zmodule.restz.. -*- rest -*-Nrm   )r9   r&   r:   r;   r   r<   r=   Zorstr   r   r   test_restdoc  s    rw   c              	   C   sf   t |}d}|tdd| d| d  t|j$ t  |  \}}d|ksXt	W 5 Q R X dS )zLEnsures that TeX documentation is written out

    CLI :: --no-rest-doc
    r\   r,   rg   r-   z --no-rest-docrv   Nr?   rr   r   r   r   test_norestexdoc  s    rx   c              
   C   s~   t |}d}|tdd| d| d  t|j< t  t d| d }d|	 ksft
W 5 Q R X W 5 Q R X d	S )
zIEnsures that debugging wrappers are written

    CLI :: --debug-capi
    r\   r,   rg   r-   z --debug-capirt   r
   z#define DEBUGCFUNCSNr   r0   r1   r2   r   r3   r4   r5   rn   ro   r7   )r9   r&   r:   r;   r   ocmodr   r   r   test_debugcapi  s    r{   zConsistently fails on CI.)reasonc              	   C   s   t | }d}|tdd| d| d  t|jP t  td}t	j
|ddd	}d
}td}|j|ksvt|j|kstW 5 Q R X dS )zEEnsures that debugging wrappers work

    CLI :: --debug-capi -c
    r\   r,   rg   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   r0   r1   r2   r   r3   r4   r5   shlex
subprocessruntextwrapdedentstdoutr7   stderr)r&   r:   r;   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 5 Q R X |  \}}d|ks`t	|tdd| d| d  t|j$ t  |  \}}d|kst	W 5 Q R X dS )zoEnsures that fortran subroutine wrappers for F77 are included by default

    CLI :: --[no]-wrap-functions
    r\   r,   rg   r-    Fortran 77 wrappers are saved toz --wrap-functionsNr?   rr   r   r   r   test_wrapfunc_def  s    r   c              	   C   sf   t |}d}|tdd| d| d  t|j$ t  |  \}}d|ksXt	W 5 Q R X dS )zeEnsures that fortran subroutine wrappers for F77 can be disabled

    CLI :: --no-wrap-functions
    r\   r,   rg   r-   z --no-wrap-functionsr   Nr?   rr   r   r   r   test_nowrapfunc  s    r   c              
   C   s   t |}d}|tdd| d| d  t|jL t  t d| d &}|	 }d|ksjt
d	|ksvt
W 5 Q R X W 5 Q R X d
S )zcAdd to the include directories

    CLI :: -include
    TODO: Document this in the help string
    r\   r,   rg   r-   z' -include<stdbool.h> -include<stdio.h> rt   r
   z#include <stdbool.h>z#include <stdio.h>Nry   )r9   r&   r:   r;   r   rz   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   t |}|tdd| d  t|jT t  |  \}}d|ksNt	d|ksZt	d|ksft	d|ksrt	d|ks~t	W 5 Q R X d	S )
zOCheck that Fortran-to-Python KIND specs can be passed

    CLI :: --f2cmap
    r,   f2py -m blah z --f2cmap mapfilez!Reading f2cmap from 'mapfile' ...z&Mapping "real(kind=real32)" to "float"z'Mapping "real(kind=real64)" to "double"z,Mapping "integer(kind=int64)" to "long_long"z0Successfully applied user defined f2cmap changesNr?   )r9   r+   r:   r;   r<   r=   r   r   r   test_f2cmap$  s    r   c              	   C   s`   t |}|tdd| d  t|j( t  |  \}}t	|dksRt
W 5 Q R X dS )z)Reduce verbosity

    CLI :: --quiet
    r,   r   z --quietr   N)r   r0   r1   r2   r   r3   r4   r5   r6   lenr7   r@   r   r   r   
test_quiet6  s    r   c              	   C   s\   t |}|tdd| d  t|j$ t  |  \}}d|ksNt	W 5 Q R X dS )z-Increase verbosity

    CLI :: --verbose
    r,   r   z
 --verboseZanalyzelineNr?   r@   r   r   r   test_verboseD  s    r   c              	   C   sX   | tdd  tt2 t  |  \}}ddl}|j	|
 ksJtW 5 Q R X dS )z"Ensure version

    CLI :: -v
    r,   zf2py -vr   N)r0   r1   r2   rE   rF   rG   r5   r6   Znumpy__version__stripr7   )r9   r:   r<   r=   npr   r   r   test_versionR  s    r   c              	   C   sp   t | }|tdd| d  t|j8 t  td}t	j
|ddd}d}|j|ksbtW 5 Q R X d	S )
z
    CLI :: -c
    r,   r   z -cz"python -c "import blah; blah.hi()"Tr}   r~   r   N)r   r0   r1   r2   r   r3   r4   r5   r   r   r   r   r7   )r&   r:   r;   r   r   r   r   r   r   test_npdistop`  s    
r   c                   C   s   dS )z
    CLI :: -c --fcompiler
    Nr   r   r   r   r   test_npd_fcompilert  s    r   c                   C   s   dS )z
    CLI :: -c --compiler
    Nr   r   r   r   r   test_npd_compiler|  s    r   c                   C   s   dS )z$
    CLI :: -c --help-fcompiler
    Nr   r   r   r   r   test_npd_help_fcompiler  s    r   c                   C   s   dS )z
    CLI :: -c --f77exec
    Nr   r   r   r   r   test_npd_f77exec  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   )Fr   ra   r1   r   r   pathlibr   collectionsr   rE    r   Znumpy.f2py.f2py2er   r5   r   r   Zfixturer&   r(   r)   r+   r>   rA   rI   markZxfailrX   r[   r^   r`   re   rf   rh   ri   rq   rs   ru   rw   rx   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   r   <module>   s   (

'








'
		
