
    Bwg>?                        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e	j                        Z G d d	      Z G d
 d      Z G d de	j                        Z G d de	j                        Z G d d      Z G d de	j                        Z G d d      Z G d de	j                        Z G d de	j                        Zej4                  j6                   G d de	j                               Z G d d      Z G d de	j                        Z G d  d!e	j                        Z G d" d#      Z  G d$ d%      Z!ej4                  j6                   G d& d'e	j                               Z"y)(    N)markinnerspacesnameargspattern   )util)crackfortranc                   >    e Zd Z ej                  dddd      gZd Zy)TestNoSpacetestssrcr   z	gh15035.fc                    t        j                  g dt         j                        }t        j                  g dt         j                        }| j                  j	                  |       t        j
                  ||dz         sJ | j                  j                  ||g       t        j
                  ||dz         sJ | j                  j                  d      dk(  sJ y )Nr         )dtyper   23   2)nparrayfloat64modulesubballclosesubct0)selfkws      Y/var/www/horilla/myenv/lib/python3.12/site-packages/numpy/f2py/tests/test_crackfortran.pytest_modulezTestNoSpace.test_module   s    HHYbjj1HHYbjj1{{1a!e$$$!Q {{1a!e$$${{~~d#t+++    N)__name__
__module____qualname__r   getpathsourcesr    r    r   r	   r	      s"     t||GUNKHIG,r    r	   c                   $    e Zd Zd Zd Zd Zd Zy)TestPublicPrivatec                 P   t        j                  dddd      }t        j                  t        |      g      }t	        |      dk(  sJ |d   }d|d   d	   d
   v sJ d|d   d	   d
   vsJ d|d   d   d
   v sJ d|d   d   d
   vsJ d|d   d   d
   vsJ d|d   d   d
   v sJ y )Nr
   r   r   zprivatemod.f90r   r   privatevarsaattrspecpublicbsetar   r$   r   strlen)r   fpathmods      r   test_defaultPrivatez%TestPublicPrivate.test_defaultPrivate   s    We^=MN''U53x1}}!fCK,Z8888s6{3/
;;;;CK,Z8888s6{3/
;;;;FF 3J ????3v;v.z::::r    c                    t        j                  dddd      }t        j                  t        |      g      }t	        |      dk(  sJ |d   }d|d   d	   d
   v sJ d|d   d	   d
   vsJ d|d   d   d
   vsJ d|d   d   d
   v sJ y )Nr
   r   r   zpublicmod.f90r   r   r*   r+   r,   r-   r.   r0   r1   r   tmp_pathr4   r5   s       r   test_defaultPublicz$TestPublicPrivate.test_defaultPublic)   s    We^_M''U53x1}}!fCK,Z8888s6{3/
;;;;FF 3J ????3v;v.z::::r    c                 0   t        j                  dddd      }t        j                  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(  sJ y )Nr
   r   r   zaccesstype.f90r   r   r+   r,   r-   r*   zbind(c)b_r.   c)r   r$   r   r2   r3   set)r   r9   r4   r5   tts        r   test_access_typez"TestPublicPrivate.test_access_type3   s    We^=MN''U53x1}}VF^2c7:&'Iy+AAAA2d8J'(Xy,AAAA2c7:&'H:555r    c                     t        j                  dddd      }t        j                  t        |      g      }t	        |      dk(  sJ t        j
                  |      }d|vsJ y )Nr
   r   r   zgh23879.f90r   bar)r   r$   r   r2   r3   crack2fortran)r   r9   r4   r5   pyfs        r   test_nowrap_private_proceeduresz1TestPublicPrivate.test_nowrap_private_proceedures<   sZ    We^]K''U53x1}}((-Cr    N)r!   r"   r#   r6   r:   r@   rE   r&   r    r   r(   r(      s    
;;6 r    r(   c                       e Zd Zd Zd Zy)TestModuleProcedurec                    t        j                  dddd      }t        j                  t        |      g      }t	        |      dk(  sJ |d   }d|v rt	        |d         dk(  sJ |d   d   d	   d
k(  sJ d|d   d   v sJ |d   d   d   ddgk(  sJ |d   d   d	   dk(  sJ d|d   d   v sJ |d   d   d   dgk(  sJ |d   d   d	   dk(  sJ d|d   d   v sJ |d   d   d   ddgk(  sJ y )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_realr1   r8   s       r   test_moduleOperatorsz(TestModuleProcedure.test_moduleOperatorsD   sc   We^_M''U53x1}}!f}S[!1Q!6666{1~f%);;;;#f+a.0006{1~o.%& 	& &6{1~f%777#f+a.0006{1~o.3D2EEEE6{1~f%888#f+a.0006{1~o.
#$ 	$ $r    c                    t        j                  dddd      }t        j                  t        |      g      }t	        |      dk(  sJ |d   }|d   d   d	   d
gk(  sJ |d   d   d	   dgk(  sJ |d   d   d	   dgk(  sJ y )Nr
   r   r   zpubprivmod.f90r   r   r+   r,   r-   r*   r/   r.   r0   r1   r8   s       r   test_notPublicPrivatez)TestModuleProcedure.test_notPublicPrivateV   s    We^=MN''U53x1}}!f6{3
+	}<<<6{3
+|;;;6{6":.8,>>>r    N)r!   r"   r#   rR   rT   r&   r    r   rG   rG   C   s    $$?r    rG   c                   D    e Zd Z ej                  dddd      gZd Zd Zy)TestExternalr
   r   r   z	gh17859.fc                 N    d }| j                   j                  |      }|dk(  sJ y )Nc                     | dz   S N{   r&   xs    r   incrz5TestExternal.test_external_as_statement.<locals>.incre       s7Nr    rZ   )r   external_as_statementr   r]   rs      r   test_external_as_statementz'TestExternal.test_external_as_statementd   (    	 KK--d3Cxxr    c                 N    d }| j                   j                  |      }|dk(  sJ y )Nc                     | dz   S rY   r&   r[   s    r   r]   z5TestExternal.test_external_as_attribute.<locals>.incrl   r^   r    rZ   )r   external_as_attributer`   s      r   test_external_as_attributez'TestExternal.test_external_as_attributek   rc   r    N)r!   r"   r#   r   r$   r%   rb   rg   r&   r    r   rV   rV   `   s%    t||GUNKHIGr    rV   c                   >    e Zd Z ej                  dddd      gZd Zy)TestCrackFortranr
   r   r   z
gh2848.f90c                 J    | j                   j                  dd      }|dk(  sJ y )Nr   r   r   r   )r   gh2848)r   ra   s     r   test_gh2848zTestCrackFortran.test_gh2848w   s$    KKq!$F{{r    N)r!   r"   r#   r   r$   r%   rm   r&   r    r   ri   ri   s   s     t||GUNLIJGr    ri   c                   $    e Zd Zd Zd Zd Zd Zy)TestMarkinnerspacesc                 :    g d}|D ]  }t        |      |k(  rJ  y )N)za z aza b cz'abcdefghij'r   )r   	test_listis      r   test_do_not_touch_normal_spacesz3TestMarkinnerspaces.test_do_not_touch_normal_spaces   s)    9	 	+A"1%***	+r    c                 D    t        d      dk(  sJ t        d      dk(  sJ y )Nza 'b c' \' \'za 'b@_@c' \' \'za "b c" \" \"za "b@_@c" \" \"rq   r   s    r   test_one_relevant_spacez+TestMarkinnerspaces.test_one_relevant_space   s,    015HHHH/04FFFFr    c                 D    t        d      dk(  sJ t        d      dk(  sJ y )Nza 'b c" " d' eza 'b@_@c"@_@"@_@d' eza "b c' ' d" eza "b@_@c'@_@'@_@d" erq   rv   s    r   test_ignore_inner_quotesz,TestMarkinnerspaces.test_ignore_inner_quotes   s,    126NNNN126NNNNr    c                 D    t        d      dk(  sJ t        d      dk(  sJ y )Nza 'b c' 'd e'za 'b@_@c' 'd@_@e'za "b c" "d e"za "b@_@c" "d@_@e"rq   rv   s    r   test_multiple_relevant_spacesz1TestMarkinnerspaces.test_multiple_relevant_spaces   s+    /3FFFF/04HHHHr    N)r!   r"   r#   rt   rw   ry   r{   r&   r    r   ro   ro   |   s    +
GOIr    ro   c            	          e Zd ZdZdZ ej                  d      Zg dZdgZ	ee	z   Z
dZ ee
      D ]d  \  ZZej                  d      D  cg c]  }d|v r|j                  d      d	   nd
 c}} Zeej#                  eedj%                  e            z  Zf ej(                  j+                  de
      ej(                  j,                  d               Zej(                  j+                  de
      d        Zyc c}} w )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 ,:r   1z, )countdimspecfirstr   c                     | j                   j                  |      }t        | j                  d|       }dD ]  } ||      \  }}|j                  |k(  rJ  y )Nget_arr_size_r   r   r         )all_dimspecsindexgetattrr   size)r   r   r   get_arr_sizer~   szr,   s          r   test_array_sizezTestDimSpec.test_array_size   s]     !!''0t{{mE7,CD  	 A OEB66R<<	 r    c                 8   | j                   j                  |      }t        | j                  d|       }t        | j                  d|       }dD ]H  } ||      \  }}|| j                  v r
 |||      }n ||      } ||      \  }	}
||	k(  r?J ||||	f        y )Nr   get_inv_arr_size_r   )r   r   r   r   nonlinear_dimspecs)r   r   r   r   get_inv_arr_sizer~   r   r,   n1sz1_s              r   test_inv_array_sizezTestDimSpec.test_inv_array_size   s     !!''0t{{mE7,CD"4;;2CE70KL  	/A OEB$111 &a+ &a( ""%FC9.q"b#..9	/r    N)r!   r"   r#   __doc__suffixtextwrapdedentcode_templatelinear_dimspecsr   r   code	enumerater   r   splitlstformatjoinpytestmarkparametrizeslowr   r   ).0ds   00r   r}   r}      s   < F#HOO % 	M&O **"%77LD#L1 
w?F}}S?QR!3!8Q4R$$))C. % 
 	

 [[Y5[[   6  [[Y5/ 6/' Ss   !C6r}   c                       e Zd Zd Zy)TestModuleDeclarationc                     t        j                  dddd      }t        j                  t        |      g      }t	        |      dk(  sJ |d   d   d   d	   d
k(  sJ y )Nr
   r   r   zfoo_deps.f90r   r   r+   abar=zbar('abar')r1   r8   s       r   test_dependenciesz'TestModuleDeclaration.test_dependencies   s\    We^^L''U53x1}}1vf~f%c*m;;;r    N)r!   r"   r#   r   r&   r    r   r   r      s    <r    r   c                       e Zd Zd Zy)TestEvalc                     t         j                  } |di       dk(  sJ  |di       dk(  sJ  |dt        dd            dk(  sJ  |d	i       d
k(  sJ y )N123z12 + 315za + br   r   )r,   r/   3z"123"z'123')r   _eval_scalardict)r   eval_scalars     r   test_eval_scalarzTestEval.test_eval_scalar  sg    "//5"%...8R(D0007D1N3s:::7B'7222r    N)r!   r"   r#   r   r&   r    r   r   r      s    3r    r   c                   T    e Zd Zej                  j                  dg d      d        Zy)TestFortranReaderencoding)asciizutf-8zutf-16zutf-32c                     |d| dz  }|j                  d|      5 }|j                  d       d d d        t        j                  t        |      g      }|d   d   dk(  sJ y # 1 sw Y   6xY w)	Ninput_with_z_encoding.f90r   )r   zd
                     subroutine foo()
                     end subroutine foo
                     r   rK   foo)openwriter   r2   )r   r9   r   f_pathffr5   s         r   test_input_encodingz%TestFortranReader.test_input_encoding  s}     k(=AA[[x[0 	BHH  	
 ''V61vf~&&&	 	s   A$$A-N)r!   r"   r#   r   r   r   r   r&   r    r   r   r   
  s*    [[ZCE	'E	'r    r   c                       e Zd Z ej                  dddd      gZej                  j                  e	j                  j                  d      du d      d	        Zy)
TestUnicodeCommentr
   r   r   zunicode_comment.f90charset_normalizerNz7test requires charset_normalizer which is not installed)reasonc                 :    | j                   j                  d       y )Nr   )r   r   rv   s    r   test_encoding_commentz(TestUnicodeComment.test_encoding_comment  s    
 	r    )r!   r"   r#   r   r$   r%   r   r   skipif	importlib	find_specr   r&   r    r   r   r     s]    t||GUN<QRSG[[		!	!"6	74	?H  	r    r   c                   V    e Zd Zej                  j                  dgg d      d        Zy)TestNameArgsPatternBacktracking	adversary))z
@)@bind@(@)z#@)@bind                         @(@)z@)@bind foo bar baz@(@c                    d}d}d\  }}t        ||      D ]  }||z  }t        |      D ]}  }g }	t        |      D ]Q  }t        j                         }
t        j                  |      }|	j                  t        j                         |
z
         S t        j                  |	      dk  r}J  rJ |dz   }t        j                  |      rJ  y)zPaddress ReDOS vulnerability:
        https://github.com/numpy/numpy/issues/23338   r   )      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_adversarys                r   !test_nameargspattern_backtrackingzATestNameArgsPatternBacktracking.test_nameargspattern_backtracking&  s     %
H
H- 	EB!*R ,- ./0 ;A**,B*112DEDLL!2!2!4r!9:; yy'#---. O8 );U(B%"))*CDDD'	Er    N)r!   r"   r#   r   r   r   r   r&   r    r   r   r   %  s/    [[		
EEr    r   c                   p    e Zd Z ej                  dddd      gZej                  j                  d        Z	y)TestFunctionReturnr
   r   r   zgh23598.f90c                 F    | j                   j                  dd      dk(  sJ y )Nr   r   r   )r   
intproductrv   s    r   test_function_rettypez(TestFunctionReturn.test_function_rettypeL  s#     {{%%a+r111r    N)
r!   r"   r#   r   r$   r%   r   r   r   r   r&   r    r   r   r   I  s7    t||GUNMJKG[[2 2r    r   c                       e Zd Zd Zy)TestFortranGroupCountersc                     t        j                  dddd      }	 t        j                  t        |      g       y # t        $ r}J d|        d }~ww xY w)Nr
   r   r   z	gh23533.fz0'crackfortran.crackfortran' raised an exception )r   r$   r   r2   	Exception)r   r4   excs      r   test_end_if_commentz,TestFortranGroupCounters.test_end_if_commentS  sT    We^[I	S%%s5zl3 	SRLSERR5	Ss   : 	A
AAN)r!   r"   r#   r   r&   r    r   r   r   R  s    Sr    r   c                       e Zd Zd Zy)TestF77CommonBlockReaderc                    t        j                  dddd      }t        j                  t	        j
                               5 }t        j                  t        |      g      }d d d        dj                         vsJ y # 1 sw Y   xY w)Nr
   r   r   zgh22648.pyfMismatch)	r   r$   
contextlibredirect_stdoutioStringIOr   r2   getvalue)r   r9   r4   stdout_f2pyr5   s        r   test_gh22648z%TestF77CommonBlockReader.test_gh22648]  sn    We^]K''6 	:+++SZL9C	:!5!5!7777	: 	:s     A==BN)r!   r"   r#   r   r&   r    r   r   r   \  s    8r    r   c                   *    e Zd Zd Zd Zd Zd Zd Zy)TestParamEvalc                     d}t        t        j                  t        j                  t        j                        }dddddddd	d
}d}t        j
                  ||||      }|dddk(  sJ y )Nz(/3.14, 4./)kindselected_int_kindselected_real_kind   r   r   rk   r   r   r   )dpintparamarraynestedz(2)r   gQ	@g      @r   r   
_kind_func_selected_int_kind_func_selected_real_kind_func
param_evalr   vg_paramsparamsr   rets         r   test_param_eval_nestedz$TestParamEval.test_param_eval_nestede  sv    \44"."F"F#/#H#HJ aL !aA.0%%a67K$3''''r    c                     d}t        t        j                  t        j                  t        j                        }i }d}t        j
                  ||||      }|ddddk(  sJ y )	Nz(/ 6, 3, 1 /)r  z(-1:1)r	     r   r   )r   r   r
  r  s         r   !test_param_eval_nonstandard_rangez/TestParamEval.test_param_eval_nonstandard_rangep  sc    \44"."F"F#/#H#HJ %%a67K1q))))r    c                     d}t        t        j                  t        j                  t        j                        }i }d}t        j                  t        t        j                  ||||       y )N6r  r   r	  	r   r   r  r  r  r   raises
ValueErrorr  r   r  r  r  r   s        r   test_param_eval_empty_rangez)TestParamEval.test_param_eval_empty_rangez  sW    \44"."F"F#/#H#HJ j,"9"91h%	'r    c                     d}t        t        j                  t        j                  t        j                        }i }t        j
                  |||d       }|dk(  sJ y )Nz3.14_dpr  r	  r
  )r   r  r  r  r  s        r   test_param_eval_non_array_paramz-TestParamEval.test_param_eval_non_array_param  sW    \44"."F"F#/#H#HJ %%a64Hir    c                     d}t        t        j                  t        j                  t        j                        }i }d}t        j                  t        t        j                  ||||       y )Nz*reshape((/ (i, i=1, 250) /), (/5, 10, 5/))r  z(0:4, 3:12, 5)r	  r  r  s        r   test_param_eval_too_many_dimsz+TestParamEval.test_param_eval_too_many_dims  sW    8\44"."F"F#/#H#HJ "j,"9"91h%	'r    N)r!   r"   r#   r  r  r  r!  r#  r&   r    r   r   r   c  s    	(*' 'r    r   c                   D    e Zd Z ej                  dddd      gZdgZd Zy)TestLowerF2PYDirectiver
   r   r   zgh27697.f90z--lowerc                     t        j                  t        d      5 }| j                  j                  j                  d       d d d        y # 1 sw Y   y xY w)Nzaborting directly)match)r   r  r  r   utilsmy_abort)r   r   s     r   test_no_lower_failz)TestLowerF2PYDirective.test_no_lower_fail  sA    ]]:-@A 	<SKK&&':;	< 	< 	<s   &AAN)r!   r"   r#   r   r$   r%   optionsr*  r&   r    r   r%  r%    s'    t||GUNMJKGkG<r    r%  )#r   r   r   numpyr   numpy.f2py.crackfortranr   r   r   r   
numpy.f2pyr   r   r   r   F2PyTestr	   r(   rG   rV   ri   ro   r}   r   r   r   r   r   r   r   r   r   r   r   r%  r&   r    r   <module>r0     sG       D  #   	,$-- ,%  % N? ?:4== &t}} I I*d/$-- d/N< <3t}} 3' '   "E "EH2 2St}} S8 82' 2'h <T]] < <r    