
    h:              	          S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKJr  SSK	J
r
  SSKJr  SSKJrJrJr  SSKJrJr  SSK	Jr  SS	KJr   " S
 S\5      rS r " S S\R0                  5      r " S S\5      r\" \R6                  " \R8                  " SSS5      5      R8                  S5      r\" \R6                  " \R8                  " SSS5      5      R8                  S5      rS r " S S\5      r S r!SS jr"SS jr#S r$g)    N)partial   )Errors)
CodeWriter)TreeFragmentstrip_common_indentStringParseContext)TreeVisitorVisitorTransform)TreePath)	PostParsec                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )NodeTypeWriter   c                 >   > [         TU ]  5         SU l        / U l        g )Nr   )super__init___indentsresultself	__class__s    J/var/www/fran/franai/venv/lib/python3.13/site-packages/Cython/TestUtils.pyr   NodeTypeWriter.__init__   s        c                 t   U R                   (       d  SnO#U R                   S   nUS   b	  SUSS -  nOUS   nU R                  R                  SU R                  -  U< SUR                  R
                  < 3-   5        U =R                  S-  sl        U R                  U5        U =R                  S-  sl        g )	Nz(root)   z%s[%d]r      z  z: )access_pathr   appendr   r   __name__visitchildren)r   nodenametips       r   
visit_NodeNodeTypeWriter.visit_Node   s    D""2&C1v!#a(*1v4$--/'+T^^-D-DEF 	G4 r   )r   r   )r"   
__module____qualname____firstlineno__r   r'   __static_attributes____classcell__r   s   @r   r   r      s    
 r   r   c                 ~    [        5       nUR                  U 5        SR                  S/UR                  -   S/-   5      $ )zReturns a string representing the tree by class names.
There's a leading and trailing whitespace so that it can be
compared by simple string comparison while still making test
cases look ok.
 )r   visitjoinr   )rootws     r   	treetypesr6   )   s7    
 	AGGDM99bTAHH_t+,,r   c                   `    \ rS rSrS rS rS rS rS rS r	S r
SS
 jrS r\4S jrS rSrg	)
CythonTest3   c                 .    [         R                  " 5         g Nr   init_threadr   s    r   setUpCythonTest.setUp5       r   c                 .    [         R                  " 5         g r;   r<   r>   s    r   tearDownCythonTest.tearDown8   rA   r   c           
         [        U[        5      (       d  UR                  S5      n[        U[        5      (       d  UR                  S5      n[        [	        X5      5       H  u  nu  pEU R                  XESX4U4-  5        M!     U R                  [        U5      [        U5      SSR                  U5      < SSR                  U5      < 35        g)zHChecks that the given strings or lists of strings are equal line by liner0   zLine %d:
Exp: %s
Got: %sUnmatched lines. Got:

Expected:
N)
isinstancelistsplit	enumeratezipassertEquallenr3   )r   expectedr   idxexpected_lineresult_lines         r   assertLinesCythonTest.assertLines;   s    (D))~~d+H&$''\\$'F1:3x;P1Q-C--]9SQ\<]]_ 2R 	XFGKyyQYGZ\`\e\efl\mn	pr   c                 d    [        5       nUR                  U5        UR                  R                  $ r;   )r   writer   lines)r   treewriters      r   codeToLinesCythonTest.codeToLinesG   s%    T}}"""r   c                 B    SR                  U R                  U5      5      $ )Nr0   )r3   rZ   )r   rX   s     r   codeToStringCythonTest.codeToStringL   s    yy))$/00r   c           	      @   U R                  U5      n[        UR                  S5      5      n[        [	        X45      5       H  u  nu  pgU R                  XvSXVU4-  5        M!     U R                  [        U5      [        U5      SSR                  U5      < SU< 35        g )Nr0   zLine %d:
Got: %s
Exp: %srF   rG   )rZ   r   rJ   rK   rL   rM   rN   r3   )r   rO   result_treeresult_linesexpected_linesrP   linerQ   s           r   
assertCodeCythonTest.assertCodeO   s    ''4,X^^D-AB*3C4U*V&C&$]9S<VVX +W 	\*C,?GKyyQ]G^`hi	kr   c                 X    U R                  [        R                  " X!5      S SU-  5        g )Nz"Path '%s' not found in result tree)assertNotEqualr   
find_first)r   pathr`   s      r   assertNodeExistsCythonTest.assertNodeExistsZ   s(    H//BD@4G	Ir   Nc                     Uc  0 nUc  / nU R                  5       nUR                  S5      (       a  U[        S5      S nUR                  SS5      n[	        XX#S9$ )zNSimply create a tree fragment using the name of the test-case in parse errors.Nz	__main__.._pipeline)id
startswithrN   replacer   )r   codepxdsrp   r%   s        r   fragmentCythonTest.fragment^   s`    <DHwwy??;''K()*D||C%D@@r   c                     [        U5      $ r;   )r6   )r   r4   s     r   r6   CythonTest.treetypesj   s    r   c                      U" 5         U R                  SU-  5        g! U a&  nU R                  [        X25      5        Us SnA$ SnAff = f)zCalls "func" and fails if it doesn't raise the right exception
(any exception by default). Also returns the exception in question.
z Expected an exception of type %rN)fail
assertTruerH   )r   funcexc_typees       r   should_failCythonTest.should_failm   sD    	FII88CD 	OOJq34H	s    A
AA
A
c                 t     U" 5       $ ! [          a$  nU R                  [        U5      5         SnAgSnAff = f)zCalls func and succeeds if and only if no exception is raised
(i.e. converts exception raising into a failed testcase). Returns
the return value of func.N)	Exceptionr{   str)r   r}   excs      r   should_not_failCythonTest.should_not_failx   s1    	 6M 	 IIc#h	 s   	 
727 )NN)r"   r)   r*   r+   r?   rC   rS   rZ   r]   rd   rj   rv   r6   r   r   r   r,   r   r   r   r8   r8   3   sB    
p#
1	kI
A *3 	 r   r8   c                   "    \ rS rSrSrSS jrSrg)TransformTest   a  
Utility base class for transform unit tests. It is based around constructing
test trees (either explicitly or by parsing a Cython code string); running
the transform, serialize it using a customized Cython serializer (with
special markup for nodes that cannot be represented in Cython),
and do a string-comparison line-by-line of the result.

To create a test case:
 - Call run_pipeline. The pipeline should at least contain the transform you
   are testing; pyx should be either a string (passed to the parser to
   create a post-parse tree) or a node representing input to pipeline.
   The result will be a transformed result.

 - Check that the tree is correct. If wanted, assertCode can be used, which
   takes a code string as expected, and a ModuleNode in result_tree
   (it serializes the ModuleNode to a string and compares line-by-line).

All code strings are first stripped for whitespace lines and then common
indentation.

Plans: One could have a pxd dictionary parameter to run_pipeline.
Nc                 h    Uc  0 nU R                  X#5      R                  nU H  nU" U5      nM     U$ r;   )rv   r4   )r   rp   pyxru   rX   Ts         r   run_pipelineTransformTest.run_pipeline   s8    <D}}S',,AT7D r   r   r;   )r"   r)   r*   r+   __doc__r   r,   r   r   r   r   r      s    .r   r   z\s+r1   zz
        /[*] (
            (?: [^*\n] | [*][^/] )*
            [\n]
            (?: [^*] | [*][^/] )*
        ) [*]/
    z\s\s+z
    (?:
        <pre class=["'][^"']*cython\s+line[^"']*["']\s*>
        (?:[^<]|<(?!/pre))+
        </pre>
    )|(?:
        <style[^>]*>
        (?:[^<]|<(?!/style))+
        </style>
    )
    c                 f   S =pU R                  S5      (       a+  [        R                  " SU SS  SS9u  pU R                  5       n U R                  S5      (       aT  U SS  R                  5       n U R                  S5      (       a+  [        R                  " SU SS  SS9u  p U R                  5       n XU 4$ )N/z(?<!\\)/r   )maxsplit:)rr   rerJ   strip)patternstartends      r   _parse_patternr      s    E#+wqr{QG--/#!"+##%c""88KqILCmmoGwr   c                   X   ^  \ rS rSrU 4S jrS rS rS rS r\	R                  rSrU =r$ )TreeAssertVisitor   c                 L   > [         TU ]  5         S U l        / U l        / U l        g r;   )r   r   _module_pos_c_patterns_c_antipatternsr   s    r   r   TreeAssertVisitor.__init__   s&    !r   c                 |   ^ ^^^^^ T R                   T R                  smmS mUU 4S jmUUUUU 4S jmUUU4S jnU$ )Nc           	      \    [         R                  " U SU< SU(       a  SOS< SU< 35        g )Nz	Pattern 'z' waszwas notz
 found in )r   error)posr   found	file_paths       r   r{   7TreeAssertVisitor.create_c_file_validator.<locals>.fail   s%    LLI- r   c                 4  > U(       aC  [         R                  " X!5      nU(       a  XR                  5       S  nOT" TR                  USU S9  U(       aE  [         R                  " X15      nU(       a  US UR	                  5        nU$ T" TR                  USU S9  U$ )NFr   r   )r   searchr   r   r   )r   contentr   r   rJ   r{   r   s        r   extract_sectionBTreeAssertVisitor.create_c_file_validator.<locals>.extract_section   s    		%1%iikl3G))5S		#/%nu{{}5G N ))3eyQNr   c                 F  > T
 HJ  n[        U5      u  p4nT" XX45      n[        R                  " X%5      (       a  M7  T	" TR                  USU S9  ML     T HJ  n[        U5      u  p4nT" XX45      n[        R                  " Xe5      (       d  M7  T	" TR                  USU S9  ML     g )NFr   T)r   r   r   r   )r   r   r   r   r   sectionantipatternantipatternsr   r{   patternsr   s          r   validate_file_contentHTreeAssertVisitor.create_c_file_validator.<locals>.validate_file_content   s    #&4W&=#G))eIyy22))7%9U $  ,*8*E'K))eI99[22));diX  ,r   c                    > U R                   nT(       d	  T(       d  U $ [        USS9 nUR                  5       nS S S 5        [        W5      nT" X5        g ! , (       d  f       N"= f)Nutf8)encoding)c_fileopenread_strip_c_comments)r   r   fr   r   r   r   s       r   validate_c_fileBTreeAssertVisitor.create_c_file_validator.<locals>.validate_c_file   sM    ]]Ffv.!&&( /'0G!&2 /.s   A
A#)r   r   )r   r   r   r   r{   r   r   s   ` @@@@@r   create_c_file_validator)TreeAssertVisitor.create_c_file_validator   s>    !%!1!143G3G,			Y 	Y		3 r   c                    UR                   nSU;   aI  US    H@  n[        R                  " X5      b  M  [        R                  " UR
                  SU-  5        MB     SU;   aK  US    HB  n[        R                  " X5      nUc  M  [        R                  " UR
                  SU-  5        MD     SU;   a  U R                  R                  US   5        SU;   a  U R                  R                  US   5        g g )Ntest_assert_path_existsz+Expected path '%s' not found in result treetest_fail_if_path_existsz)Unexpected path '%s' found in result treetest_assert_c_code_hastest_fail_if_c_code_has)	
directivesr   rh   r   r   r   r   extendr   )r   r$   r   ri   
first_nodes        r   _check_directives#TreeAssertVisitor._check_directives  s    __
$
2"#<=&&t2:LLELN >
 &3"#=>%00<
)LL"CdJL ? $z1##J/G$HI$
2  ''
3L(MN 3r   c                 l    UR                   U l        U R                  U5        U R                  U5        U$ r;   )r   r   r   r#   r   r$   s     r   visit_ModuleNode"TreeAssertVisitor.visit_ModuleNode!  s/    88t$4 r   c                 J    U R                  U5        U R                  U5        U$ r;   )r   r#   r   s     r   visit_CompilerDirectivesNode.TreeAssertVisitor.visit_CompilerDirectivesNode'  s#    t$4 r   )r   r   r   )r"   r)   r*   r+   r   r   r   r   r   r   recurse_to_childrenr'   r,   r-   r.   s   @r   r   r      s+    "3jO(
 "55Jr   r   c                    [         R                  /[         R                  [        R                  R	                  US5      /[         R                  [        R                  R	                  US5      /S.nUc  [
        R                  " 5       n/ S pT[        U S5       n U GH  nUS S S:X  Ga  UR                  5       R                  S5      R                  5       R                  S5      R                  S	[        R                  R                  5      n[        R                  R	                  X5      n	[        R                  R                  [        R                  R                  U	5      5      (       d3  [        R                  " [        R                  R                  U	5      5        Ub  US pZU
R                  5         [        U	S
5      nGM*  Ub  UR!                  U5        GMA  UR                  5       (       d  GMY  UR#                  5       R%                  S5      (       a  GM  UR                  5       S;  d  GM  [&        R(                  " UR                  S5      5      nU(       d  GM  US   USS  p UR+                  X<   U-   5        GM     Ub  UR                  5         S S S 5        X4$ ! [,         a    UR+                  U5         GM(  f = f! Ub  UR                  5         f f = f! , (       d  f       X4$ = f)Nz	cython.pyzcythonize.py)PYTHONCYTHON	CYTHONIZErb   s   #####   #r   r   wb)s   """s   '''r   r   )sys
executableosri   r3   tempfilemkdtempr   r   decoders   sepexistsdirnamemakedirscloserV   lstriprr   shlexrJ   r!   KeyError)	tree_fileworkdircython_rootprogramsheadercur_filer   rc   filenameri   to_closecommandprogargss                 r   unpack_source_treer   /  s4   >>">>277<<[#IJnnbggll;&OPH ""$4H	i	!	!8x'#zz|11$7==?FFvNVVWZ\^\c\c\g\ghH77<<:D77>>"''//$*?@@BGGOOD$9:+-5t( (#D$/H)NN4(ZZ\\$++-*B*B4*H*Hzz|+;;"'++dkk&.A"B&%,QZd3"MM(.*=>' . # 5 
6 ?  ( 3"MM'223 #  $3 
	6 ?s[   K"EK	6"K	K	48K	-J&K	K"&KK	KK		KK""
K2c                     [        U[        5      (       a  SnSnSnOSnSnSnUc  UnU(       a  [        R                  " U5      n[	        XX5S9 nUR                  U5        SSS5        g! , (       d  f       g= f)zWrite some content (text or bytes) to the file
at `file_path` without translating `'\n'` into `os.linesep`.

The default encoding is `'utf-8'`.
r   Nr5   r0   zutf-8)moder   newline)rH   bytestextwrapdedentr   rV   )r   r   r   r   r   r   default_encodingr   s           r   
write_filer   W  sv     '5!!  "#//'*	iX	G1	 
H	G	Gs   A**
A8c                 2   [        XX4S9   [        R                  R                  U5      nUb#  U[        R                  R                  U 5      :  a5  [        XX4S9  Uc  M  U[        R                  R                  U 5      :  a  M4  gg! [         a    Sn Njf = f)z
Write `content` to the file `file_path` without translating `'\n'`
into `os.linesep` and make sure it is newer than the file `newer_than`.

The default encoding is `'utf-8'` (same as for `write_file`).
)r   r   N)r   r   ri   getmtimeOSError)r   
newer_thanr   r   r   
other_times         r   write_newer_filer  u  s     y&DWW%%j1

 

bgg.>.>y.I I9fH 

bgg.>.>y.I I	  
s   B BBc                 >   [        S5      n [        R                  " 5        n[        U [	        U5      /S9nUR                  5       nSSS5        W(       a  US   eW$ ! , (       d  f       N= f! [        R                   a  n[        UR                  5      eSnAff = f)z
Compiles code far enough to get errors from the parser and post-parse stage.

Is useful for checking for syntax errors, however it doesn't generate runable
code.
testro   Nr   )	r	   r   local_errorsr   r   
substituteCompileErrorSyntaxErrormessage_only)rt   contexterrorsr   r   s        r   py_parse_coder    s     !(G	*  "f!$)G2D1EFF&&(F # )OM #"  *!..))*s.   A. %AA. 
A+'A. .BBB)FN)%r   r   unittestr   r   r   r   	functoolsr   Compilerr   r   Compiler.TreeFragmentr   r   r	   Compiler.Visitorr
   r   r   Compiler.ParseTreeTransformsr   r   r6   TestCaser8   r   compilesubr   _strip_cython_code_from_htmlr   r   r   r   r  r  r   r   r   <module>r     s    	 	   
     " X X ;  3[ .-L "" L ^J L BJJFF62  	 #r   'rzzFF8R 
 
	( #r  
^6( ^6B%P<I&*r   