o
    >~#g>                     @   s  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m	Z	 ddl
mZ d dlmZ d dlZd dlZd dlZG dd dejZG dd	 d	ZG d
d dZG dd dejZG dd dejZG dd dZG dd dejZG dd dZG dd dejZG dd dejZejjG dd dejZG dd dZG dd dejZ G d d! d!ejZ!G d"d# d#Z"G d$d% d%Z#dS )&    N)markinnerspacesnameargspattern   )util)crackfortranc                   @   &   e Zd ZeddddgZdd ZdS )TestNoSpacetestssrcr   z	gh15035.fc                 C   s   t jg dt jd}t jg dt jd}| j| t ||d s$J | j||g t ||d s6J | jddks@J d S )Nr         )dtyper   23   2)nparrayfloat64modulesubballclosesubct0)selfkw r   /var/www/static.ux5.de/https/Moving-Object-Detection-with-OpenCV/env/lib/python3.10/site-packages/numpy/f2py/tests/test_crackfortran.pytest_module   s   zTestNoSpace.test_moduleN)__name__
__module____qualname__r   getpathsourcesr   r   r   r   r   r      s    r   c                   @   ,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestPublicPrivatec                 C   s   t dddd}tt|g}t|dksJ |d }d|d d	 d
 v s(J d|d d	 d
 vs4J d|d d d
 v s@J d|d d d
 vsLJ d|d d d
 vsXJ d|d d d
 v sdJ d S )Nr	   r
   r   zprivatemod.f90r   r   privatevarsaattrspecpublicbsetar   r"   r   strlen)r   fpathmodr   r   r   test_defaultPrivate   s   z%TestPublicPrivate.test_defaultPrivatec                 C   s   t dddd}tt|g}t|dksJ |d }d|d d	 d
 v s(J d|d d	 d
 vs4J d|d d d
 vs@J d|d d d
 v sLJ d S )Nr	   r
   r   zpublicmod.f90r   r   r&   r'   r(   r)   r*   r,   r-   r   tmp_pathr0   r1   r   r   r   test_defaultPublic+   s   z$TestPublicPrivate.test_defaultPublicc                 C   s   t dddd}tt|g}t|dksJ |d d }t|d d	 d
dhks,J t|d d	 ddhks:J t|d d	 dhksGJ d S )Nr	   r
   r   zaccesstype.f90r   r   r'   r(   r)   r&   zbind(c)b_r*   c)r   r"   r   r.   r/   set)r   r4   r0   r1   ttr   r   r   test_access_type5   s   z"TestPublicPrivate.test_access_typec                 C   sJ   t dddd}tt|g}t|dksJ t|}d|vs#J d S )Nr	   r
   r   zgh23879.f90r   bar)r   r"   r   r.   r/   crack2fortran)r   r4   r0   r1   pyfr   r   r   test_nowrap_private_proceedures>   s
   
z1TestPublicPrivate.test_nowrap_private_proceeduresN)r   r    r!   r2   r5   r:   r>   r   r   r   r   r%      s
    
	r%   c                   @   s   e Zd Zdd Zdd ZdS )TestModuleProcedurec                 C   s.  t dddd}tt|g}t|dksJ |d }d|v r(t|d dks*J |d d d	 d
ks6J d|d d v s@J |d d d ddgksNJ |d d d	 dksZJ d|d d v sdJ |d d d dgksqJ |d d d	 dks}J d|d d v sJ |d d d ddgksJ d S )Nr	   r
   r   zoperators.f90r   r   body	   namezoperator(.item.)implementedbyitem_int	item_realr   zoperator(==)items_are_equalr   zassignment(=)get_intget_realr-   r3   r   r   r   test_moduleOperatorsF   s$   z(TestModuleProcedure.test_moduleOperatorsc                 C   s   t dddd}tt|g}t|dksJ |d }|d d d	 d
gks)J |d d d	 dgks6J |d d d	 dgksCJ d S )Nr	   r
   r   zpubprivmod.f90r   r   r'   r(   r)   r&   r+   r*   r,   r-   r3   r   r   r   test_notPublicPrivateX   s   z)TestModuleProcedure.test_notPublicPrivateN)r   r    r!   rI   rJ   r   r   r   r   r?   E   s    r?   c                   @   s.   e Zd ZeddddgZdd Zdd Zd	S )
TestExternalr	   r
   r   z	gh17859.fc                 C   $   dd }| j |}|dksJ d S )Nc                 S      | d S N{   r   xr   r   r   incrg      z5TestExternal.test_external_as_statement.<locals>.incrrO   )r   external_as_statementr   rR   rr   r   r   test_external_as_statementf      z'TestExternal.test_external_as_statementc                 C   rL   )Nc                 S   rM   rN   r   rP   r   r   r   rR   n   rS   z5TestExternal.test_external_as_attribute.<locals>.incrrO   )r   external_as_attributerU   r   r   r   test_external_as_attributem   rX   z'TestExternal.test_external_as_attributeN)r   r    r!   r   r"   r#   rW   rZ   r   r   r   r   rK   b   s    rK   c                   @   r   )TestCrackFortranr	   r
   r   z
gh2848.f90c                 C   s   | j dd}|dksJ d S )Nr   r   r   r   )r   gh2848)r   rV   r   r   r   test_gh2848y   s   zTestCrackFortran.test_gh2848N)r   r    r!   r   r"   r#   r^   r   r   r   r   r[   u   s    r[   c                   @   r$   )
TestMarkinnerspacesc                 C   s&   g d}|D ]
}t ||ksJ qd S )N)za z aza b cz'abcdefghij'r   )r   	test_listir   r   r   test_do_not_touch_normal_spaces   s   z3TestMarkinnerspaces.test_do_not_touch_normal_spacesc                 C   $   t ddksJ t ddksJ d S )Nza 'b c' \' \'za 'b@_@c' \' \'za "b c" \" \"za "b@_@c" \" \"r`   r   r   r   r   test_one_relevant_space      z+TestMarkinnerspaces.test_one_relevant_spacec                 C   rd   )Nza 'b c" " d' eza 'b@_@c"@_@"@_@d' eza "b c' ' d" eza "b@_@c'@_@'@_@d" er`   re   r   r   r   test_ignore_inner_quotes   rg   z,TestMarkinnerspaces.test_ignore_inner_quotesc                 C   rd   )Nza 'b c' 'd e'za 'b@_@c' 'd@_@e'za "b c" "d e"za "b@_@c" "d@_@e"r`   re   r   r   r   test_multiple_relevant_spaces   rg   z1TestMarkinnerspaces.test_multiple_relevant_spacesN)r   r    r!   rc   rf   rh   ri   r   r   r   r   r_   ~   s
    r_   c                   @   s   e Zd ZdZdZedZg dZdgZ	ee	 Z
dZee
D ]\ZZdd ed	D Zeejeed
ed7 Zqejde
ejjdd Zejde
dd ZdS )TestDimSpeca  This test suite tests various expressions that are used as dimension
    specifications.

    There exists two usage cases where analyzing dimensions
    specifications are important.

    In the first case, the size of output arrays must be defined based
    on the inputs to a Fortran function. Because Fortran supports
    arbitrary bases for indexing, for instance, `arr(lower:upper)`,
    f2py has to evaluate an expression `upper - lower + 1` where
    `lower` and `upper` are arbitrary expressions of input parameters.
    The evaluation is performed in C, so f2py has to translate Fortran
    expressions to valid C expressions (an alternative approach is
    that a developer specifies the corresponding C expressions in a
    .pyf file).

    In the second case, when user provides an input array with a given
    size but some hidden parameters used in dimensions specifications
    need to be determined based on the input array size. This is a
    harder problem because f2py has to solve the inverse problem: find
    a parameter `p` such that `upper(p) - lower(p) + 1` equals to the
    size of input array. In the case when this equation cannot be
    solved (e.g. because the input array size is wrong), raise an
    error before calling the Fortran function (that otherwise would
    likely crash Python process when the size of input arrays is
    wrong). f2py currently supports this case only when the equation
    is linear with respect to unknown parameter.

    z.f90a
  
      function get_arr_size_{count}(a, n) result (length)
        integer, intent(in) :: n
        integer, dimension({dimspec}), intent(out) :: a
        integer length
        length = size(a)
      end function

      subroutine get_inv_arr_size_{count}(a, n)
        integer :: n
        ! the value of n is computed in f2py wrapper
        !f2py intent(out) n
        integer, dimension({dimspec}), intent(in) :: a
        if (a({first}).gt.0) then
          ! print*, "a=", a
        endif
      end subroutine
    )nz2*nz2:nzn/2z5 - n/2z3*n:20zn*(n+1):n*(n+5)z2*n, nz2*n:3*n*n+2*n c                 C   s&   g | ]}d |v r| d d ndqS ):r   1)split).0dr   r   r   
<listcomp>   s   & zTestDimSpec.<listcomp>,z, )countdimspecfirstru   c                 C   sF   | j |}t| jd| }dD ]}||\}}|j|ks J qd S )Nget_arr_size_r   r   r         )all_dimspecsindexgetattrr   size)r   ru   rt   get_arr_sizerk   szr(   r   r   r   test_array_size   s   zTestDimSpec.test_array_sizec                 C   s   | j |}t| jd| }t| jd| }dD ])}||\}}|| jv r-|||}n||}||\}	}
||	ksCJ ||||	fqd S )Nrw   get_inv_arr_size_rx   )r{   r|   r}   r   nonlinear_dimspecs)r   ru   rt   r   get_inv_arr_sizerk   r   r(   n1sz1_r   r   r   test_inv_array_size   s   
zTestDimSpec.test_inv_array_sizeN)r   r    r!   __doc__suffixtextwrapdedentcode_templatelinear_dimspecsr   r{   code	enumeratert   ru   ro   lstformatjoinpytestmarkparametrizeslowr   r   r   r   r   r   rj      s(    

	rj   c                   @      e Zd Zdd ZdS )TestModuleDeclarationc                 C   sP   t dddd}tt|g}t|dksJ |d d d d	 d
ks&J d S )Nr	   r
   r   zfoo_deps.f90r   r   r'   abar=zbar('abar')r-   r3   r   r   r   test_dependencies   s    z'TestModuleDeclaration.test_dependenciesN)r   r    r!   r   r   r   r   r   r          r   c                   @   r   )TestEvalc                 C   sZ   t j}|di dksJ |di dksJ |dtddddks"J |d	i d
ks+J d S )N123z12 + 315za + br   r   )r(   r+   3z"123"z'123')r   _eval_scalardict)r   eval_scalarr   r   r   test_eval_scalar  s
   zTestEval.test_eval_scalarN)r   r    r!   r   r   r   r   r   r     r   r   c                   @   s&   e Zd Zejdg ddd ZdS )TestFortranReaderencoding)asciizutf-8zutf-16zutf-32c                 C   sp   |d| d }|j d|d}|d W d    n1 sw   Y  tt|g}|d d dks6J d S )	Ninput_with_z_encoding.f90r   )r   zd
                     subroutine foo()
                     end subroutine foo
                     r   rB   foo)openwriter   r.   )r   r4   r   f_pathffr1   r   r   r   test_input_encoding  s   z%TestFortranReader.test_input_encodingN)r   r    r!   r   r   r   r   r   r   r   r   r     s
    r   c                   @   sB   e Zd ZeddddgZejje	j
ddu ddd	d
 ZdS )TestUnicodeCommentr	   r
   r   zunicode_comment.f90charset_normalizerNz7test requires charset_normalizer which is not installed)reasonc                 C   s   | j d d S )Nr   )r   r   re   r   r   r   test_encoding_comment  s   z(TestUnicodeComment.test_encoding_comment)r   r    r!   r   r"   r#   r   r   skipif	importlib	find_specr   r   r   r   r   r     s    r   c                   @   s(   e Zd Zejdgg ddd ZdS )TestNameArgsPatternBacktracking	adversary))z
@)@bind@(@)z#@)@bind                         @(@)z@)@bind foo bar baz@(@c                 C   s   d}d}d\}}t ||D ]@}|| }t |D ]&}g }	t |D ]}t }
t|}|	t |
  qt|	dk s=J q|rBJ |d }t|sMJ qdS )zPaddress ReDOS vulnerability:
        https://github.com/numpy/numpy/issues/23338   ry   )      g?z@)@N)rangetimeperf_counterr   searchappendr   median)r   r   trials_per_batchbatches_per_regex
start_repsend_repsiirepeated_adversaryr   timesr   mtchgood_version_of_adversaryr   r   r   !test_nameargspattern_backtracking(  s    
zATestNameArgsPatternBacktracking.test_nameargspattern_backtrackingN)r   r    r!   r   r   r   r   r   r   r   r   r   '  s    r   c                   @   s.   e Zd ZeddddgZejjdd Z	dS )TestFunctionReturnr	   r
   r   zgh23598.f90c                 C   s   | j dddksJ d S )Nr   ry   r   )r   
intproductre   r   r   r   test_function_rettypeN  s   z(TestFunctionReturn.test_function_rettypeN)
r   r    r!   r   r"   r#   r   r   r   r   r   r   r   r   r   K  s    r   c                   @   r   )TestFortranGroupCountersc              
   C   sN   t dddd}ztt|g W d S  ty& } zJ d| d }~ww )Nr	   r
   r   z	gh23533.fFz0'crackfortran.crackfortran' raised an exception )r   r"   r   r.   	Exception)r   r0   excr   r   r   test_end_if_commentU  s   z,TestFortranGroupCounters.test_end_if_commentN)r   r    r!   r   r   r   r   r   r   T  r   r   c                   @   r   )TestF77CommonBlockReaderc                 C   sb   t dddd}tt }tt|g}W d    n1 s"w   Y  d| vs/J d S )Nr	   r
   r   zgh22648.pyfMismatch)	r   r"   
contextlibredirect_stdoutioStringIOr   r.   getvalue)r   r4   r0   stdout_f2pyr1   r   r   r   test_gh22648_  s
   z%TestF77CommonBlockReader.test_gh22648N)r   r    r!   r   r   r   r   r   r   ^  r   r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestParamEvalc                 C   s^   d}t tjtjtjd}dddddddd	d
}d}tj||||d}|dddks-J d S )Nz(/3.14, 4./)kindselected_int_kindselected_real_kind   r   rz   r\   r   r   r   )dpintparamarraynestedz(2)ru   gQ	@g      @r   r   
_kind_func_selected_int_kind_func_selected_real_kind_func
param_evalr   vg_paramsparamsru   retr   r   r   test_param_eval_nestedg  s   

z$TestParamEval.test_param_eval_nestedc                 C   sJ   d}t tjtjtjd}i }d}tj||||d}|ddddks#J d S )	Nz(/ 6, 3, 1 /)r   z(-1:1)r      r   r   )r   r   r   r   r   r   r   !test_param_eval_nonstandard_ranger  s   z/TestParamEval.test_param_eval_nonstandard_rangec                 C   <   d}t tjtjtjd}i }d}tjttj||||d d S )N6r   rl   r   	r   r   r   r   r   r   raises
ValueErrorr   r   r   r   r   ru   r   r   r   test_param_eval_empty_range|     
z)TestParamEval.test_param_eval_empty_rangec                 C   s>   d}t tjtjtjd}i }tj|||d d}|dksJ d S )Nz3.14_dpr   r   r   )r   r   r   r   r   r   r   r   test_param_eval_non_array_param  s   z-TestParamEval.test_param_eval_non_array_paramc                 C   r   )Nz*reshape((/ (i, i=1, 250) /), (/5, 10, 5/))r   z(0:4, 3:12, 5)r   r   r   r   r   r   test_param_eval_too_many_dims  r   z+TestParamEval.test_param_eval_too_many_dimsN)r   r    r!   r   r   r   r   r   r   r   r   r   r   e  s    

	r   )$r   codecsr   unicodedatar   numpyr   numpy.f2py.crackfortranr   r   rl   r   
numpy.f2pyr   r   r   r   F2PyTestr   r%   r?   rK   r[   r_   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s:    '	g
$	
