U
    ¸mœdpQ  ã                   @   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   úU/home/sam/Atlas/atlas_env/lib/python3.8/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 )ú'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   r1   r2   r3   r   r4   r5   r6   r7   r8   ©r:   r'   r;   r<   r=   r>   r   r   r   Útest_gen_pyf_stdoutm   s    rB   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   r1   r2   r3   r   r4   r5   r"   ÚpytestÚraisesÚ
SystemExitr6   r7   r8   )r:   r'   r;   r<   r>   Úerrr   r   r   Útest_gen_pyf_no_overwritez   s    rJ   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s‚t
‚q‚W 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   r1   r2   r3   r   r4   r5   r6   r7   r8   )r:   r*   r;   Úfoutlr<   ÚtoskipÚ	remainingr=   rI   ÚskeyÚrkeyr   r   r   Útest_f2py_skip‰   s&     þ
ÿÿrY   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s‚t
‚q‚W 5 Q R X dS )z>Test that functions can be kept by only:
    CLI :: only:
    rK   rL   rM   rN   r-   rO   z -m test only: ú<buildmodule: Could not find the body of interfaced routine "rP   rQ   rR   NrS   )r:   r*   r;   rT   r<   rU   Útokeepr=   rI   rW   rX   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sšt‚qšW 5 Q R X dS )zfTests that it is possible to return to file processing mode
    CLI :: :
    BUG: numpy-gh #20520
    rK   rL   rM   ztd s0 hiÚblahr-   rO   ú -m z only: z : rZ   rP   rQ   rR   N)r   r   r   r1   r2   r3   r   r4   r5   r6   r7   r8   )r:   r'   r*   r;   rT   r<   rU   Zipath2r[   r   r=   rI   rW   rX   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
    ÚhirL   r-   rO   r^   N)r   r   r1   r2   r3   r   r4   r5   r6   r   r9   r   r8   r   )r:   r'   r;   ZMNAMErT   r<   r   r   r   Útest_mod_gen_f77Ö   s    ra   c           	   	   C   sú   t |dd�}|j}t d¡}t d¡}| tdd|› d� ¡ ¡ t |j	¡�< t
ƒ  |  ¡ \}}| |¡dk	srt‚| |¡dks„t‚W 5 Q R X | tdd|› d	� ¡ ¡ t |j	¡�< t
ƒ  |  ¡ \}}| |¡dksÚt‚| |¡dk	sìt‚W 5 Q R X dS )
zHLowers cases by flag or when -h is present

    CLI :: --[no-]lower
    rK   rL   zHI\(\)zhi\(\)r-   rO   z -m test --lowerNz -m test --no-lower)r   r   ÚreÚcompiler1   r2   r3   r   r4   r5   r6   r7   Úsearchr8   ©	r:   r)   r;   rT   r<   ZcapshiZcapslor=   r>   r   r   r   Útest_lower_cmodç   s$    

ÿrf   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sŒt‚W 5 Q R X | tdd|› d|j› d
� ¡ ¡ t 	|j
¡�< tƒ  |  ¡ \}}| |¡d	ksêt‚| |¡d	k	süt‚W 5 Q R X d	S )z^Lowers cases in signature files by flag or when -h is present

    CLI :: --[no-]lower -h
    rK   rL   z	Block: HIz	Block: hir-   rO   z -h z -m test --overwrite-signatureNz) -m test --overwrite-signature --no-lower)r   r   rb   rc   r1   r2   r   r3   r   r4   r5   r6   r7   rd   r8   re   r   r   r   Útest_lower_sig  s0    

ýýrg   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 "rR   Nr@   )r:   r'   r;   r<   r   Zodirr=   r>   r   r   r   Útest_build_dir(  s    ÿri   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-   rC   z --overwrite-signaturerD   rE   r   r   r/   N)r   r1   r2   r3   r   r4   r5   r"   r6   r7   r8   rA   r   r   r   Útest_overwrite9  s     þrj   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-   rh   r.   z --latex-docúDocumentation is saved to fileú
module.texú\documentclassN©r   r1   r2   r3   r   r4   r5   r6   r7   r8   ÚopenÚread©r:   r'   r;   r<   r   r=   r>   Zotexr   r   r   Útest_latexdocJ  s    ÿrr   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-   rh   r.   z --no-latex-docrk   Nr@   ©r:   r'   r;   r<   r   r=   r>   r   r   r   Útest_nolatexdoc\  s    ÿrt   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 )zžEnsures that truncated documentation is written out

    TODO: Test to ensure this has no effect without --latex-doc
    CLI :: --latex-doc --short-latex
    r]   r-   rh   r.   z --latex-doc --short-latexrk   ú./rl   rm   Nrn   rq   r   r   r   Útest_shortlatexl  s    ýrv   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-   rh   r.   z --rest-docú#ReST Documentation is saved to fileru   zmodule.restz.. -*- rest -*-Nrn   )r:   r'   r;   r<   r   r=   r>   Zorstr   r   r   Útest_restdoc‚  s    ÿrx   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-   rh   r.   z --no-rest-docrw   Nr@   rs   r   r   r   Útest_norestexdoc”  s    ÿry   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-   rh   r.   z --debug-capiru   r
   z#define DEBUGCFUNCSN©r   r1   r2   r3   r   r4   r5   r6   ro   rp   r8   )r:   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s„t‚W 5 Q R X dS )zEEnsures that debugging wrappers work

    CLI :: --debug-capi -c
    r]   r-   rh   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   r1   r2   r3   r   r4   r5   r6   ÚshlexÚ
subprocessÚrunÚtextwrapÚdedentÚstdoutr8   Ú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s¬t	‚W 5 Q R X dS )zoEnsures that fortran subroutine wrappers for F77 are included by default

    CLI :: --[no]-wrap-functions
    r]   r-   rh   r.   ú Fortran 77 wrappers are saved toz --wrap-functionsNr@   rs   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-   rh   r.   z --no-wrap-functionsr�   Nr@   rs   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-   rh   r.   z' -include<stdbool.h> -include<stdio.h> ru   r
   z#include <stdbool.h>z#include <stdio.h>Nrz   )r:   r'   r;   r<   r   r{   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@   )r:   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   r1   r2   r3   r   r4   r5   r6   r7   Úlenr8   rA   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@   rA   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)r1   r2   r3   rF   rG   rH   r6   r7   ÚnumpyÚ__version__Ústripr8   )r:   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   r1   r2   r3   r   r4   r5   r6   r‚   rƒ   r„   r‡   r8   )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…   rb   r2   rƒ   r‚   Úpathlibr   Úcollectionsr   rF   Ú r   Znumpy.f2py.f2py2er   r6   r   r   Zfixturer'   r)   r*   r,   r?   rB   rJ   ÚmarkZxfailrY   r\   r_   ra   rf   rg   ri   rj   rr   rt   rv   rx   ry   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€   (

'








'
		
