
    -Ph:              	          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Zd dlmZ ddl	m
Z
 ddlmZ ddlmZmZmZ ddlmZmZ ddl	mZ dd	lmZ  G d
 de          Zd Z G d dej                  Z G d de          Z e ej         ej        ddd                    j        d          Z e ej         ej        ddd                    j        d          Zd Z G d de          Z d Z!ddZ"ddZ#d Z$dS )    N)partial   )Errors)
CodeWriter)TreeFragmentstrip_common_indentStringParseContext)TreeVisitorVisitorTransform)TreePath)	PostParsec                   $     e Zd Z fdZd Z xZS )NodeTypeWriterc                 d    t                                                       d| _        g | _        d S )Nr   )super__init___indentsresultself	__class__s    P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/Cython/TestUtils.pyr   zNodeTypeWriter.__init__   s+        c                 B   | j         sd}n+| j         d         }|d         d|dd         z  }n|d         }| j                            d| j        z  |d|j        j        z              | xj        dz  c_        |                     |           | xj        dz  c_        d S )	Nz(root)   z%s[%d]r      z  z: )access_pathr   appendr   r   __name__visitchildren)r   nodenametips       r   
visit_NodezNodeTypeWriter.visit_Node   s     	DD"2&C1v!#ac(*1v4$-/'+ttT^-D-DEF 	G 	G 	G4   r   )r    
__module____qualname__r   r%   __classcell__r   s   @r   r   r      sG            
      r   r   c                     t                      }|                    |            d                    dg|j        z   dgz             S )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   	treetypesr1   )   s?    
 	AGGDMMM99bTAH_t+,,,r   c                   V    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dd	Z
d
 ZefdZd ZdS )
CythonTestc                 ,    t          j                     d S Nr   init_threadr   s    r   setUpzCythonTest.setUp5       r   c                 ,    t          j                     d S r5   r6   r8   s    r   tearDownzCythonTest.tearDown8   r:   r   c           
         t          |t                    s|                    d          }t          |t                    s|                    d          }t          t	          ||                    D ]%\  }\  }}|                     ||d|||fz             &|                     t          |          t          |          dd                    |          dd                    |                     dS )zHChecks that the given strings or lists of strings are equal line by liner+   zLine %d:
Exp: %s
Got: %sUnmatched lines. Got:

Expected:
N)
isinstancelistsplit	enumeratezipassertEquallenr.   )r   expectedr   idxexpected_lineresult_lines         r   assertLineszCythonTest.assertLines;   s   (D)) 	,~~d++H&$'' 	(\\$''F1:3x;P;P1Q1Q 	_ 	_-C--]K9S-Q\<]]_ _ _ _XFGKyyQYGZGZGZGZ\`\e\efl\m\m\mn	p 	p 	p 	p 	pr   c                 `    t                      }|                    |           |j        j        S r5   )r   writer   lines)r   treewriters      r   codeToLineszCythonTest.codeToLinesG   s(    T}""r   c                 R    d                     |                     |                    S )Nr+   )r.   rQ   )r   rO   s     r   codeToStringzCythonTest.codeToStringL   s"    yy))$//000r   c           	         |                      |          }t          |                    d                    }t          t	          ||                    D ]%\  }\  }}|                     ||d|||fz             &|                     t          |          t          |          dd                    |          d|           d S )Nr+   zLine %d:
Got: %s
Exp: %sr>   r?   )rQ   r   rB   rC   rD   rE   rF   r.   )r   rG   result_treeresult_linesexpected_linesrH   linerI   s           r   
assertCodezCythonTest.assertCodeO   s    ''44,X^^D-A-ABB*3Cn4U4U*V*V 	X 	X&C&$]D9S$<VVX X X X\**C,?,?,?GKyyQ]G^G^G^G^`h`hi	k 	k 	k 	k 	kr   c                 `    |                      t          j        ||          d d|z             d S )Nz"Path '%s' not found in result tree)assertNotEqualr   
find_first)r   pathrU   s      r   assertNodeExistszCythonTest.assertNodeExistsZ   sB    H/TBBD@4G	I 	I 	I 	I 	Ir   Nc                     |i }|g }|                                  }|                    d          r|t          d          d         }|                    dd          }t	          ||||          S )zNSimply create a tree fragment using the name of the test-case in parse errors.Nz	__main__.._pipeline)id
startswithrF   replacer   )r   codepxdsrc   r#   s        r   fragmentzCythonTest.fragment^   sz    <DHwwyy??;'' 	+K(())*D||C%%D$x@@@@r   c                      t          |          S r5   )r1   )r   r/   s     r   r1   zCythonTest.treetypesj   s    r   c                     	  |             |                      d|z             dS # |$ r/}|                     t          ||                     |cY d}~S d}~ww xY w)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
assertTruer@   )r   funcexc_typees       r   should_failzCythonTest.should_failm   sz    	DFFFII88CDDDDD 	 	 	OOJq(33444HHHHHH	s   "& A$AAAc                     	  |            S # t           $ r-}|                     t          |                     Y d}~dS d}~ww xY w)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)	Exceptionrl   str)r   rn   excs      r   should_not_failzCythonTest.should_not_failx   s[    	 466M 	  	  	 IIc#hh	 s   	 
A">A)NN)r    r&   r'   r9   r<   rK   rQ   rS   rY   r^   ri   r1   rs   rq   rv    r   r   r3   r3   3   s            
p 
p 
p# # #
1 1 1	k 	k 	kI I I
A 
A 
A 
A   *3 	 	 	 	         r   r3   c                       e Zd ZdZddZdS )TransformTesta.  
    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                 d    |i }|                      ||          j        }|D ]} ||          }|S r5   )ri   r/   )r   rc   pyxrh   rO   Ts         r   run_pipelinezTransformTest.run_pipeline   sE    <D}}S$'', 	 	A1T77DDr   r5   )r    r&   r'   __doc__r}   rw   r   r   ry   ry      s2         .     r   ry   z\s+r,   zz
        /[*] (
            (?: [^*\n] | [*][^/] )*
            [\n]
            (?: [^*] | [*][^/] )*
        ) [*]/
    z\s\s+z
    (?:
        <pre class=["'][^"']*cython\s+line[^"']*["']\s*>
        (?:[^<]|<(?!/pre))+
        </pre>
    )|(?:
        <style[^>]*>
        (?:[^<]|<(?!/style))+
        </style>
    )
    c                    d x}}|                      d          r6t          j        d| dd          d          \  }} |                                 } |                      d          rg| dd                                          } |                      d          r6t          j        d| dd          d          \  }} |                                 } ||| fS )N/z(?<!\\)/r   )maxsplit:)re   rerB   strip)patternstartends      r   _parse_patternr      s    EC# "+wqrr{QGGGw--//# &!""+##%%c"" 	&8KqIIILCmmooG#wr   c                   D     e Zd Z fdZd Zd Zd Zd Zej	        Z
 xZS )TreeAssertVisitorc                 r    t                                                       d | _        g | _        g | _        d S r5   )r   r   _module_pos_c_patterns_c_antipatternsr   s    r   r   zTreeAssertVisitor.__init__   s7    !r   c                 b      j          j        cd  fd fdfd}|S )Nc           	      J    t          j        | d|d|rdndd|           d S )Nz	Pattern 'z' waszwas notz
 found in )r   error)posr   found	file_paths       r   rl   z7TreeAssertVisitor.create_c_file_validator.<locals>.fail   sG    L-I--	     r   c                 0   |rHt          j        ||          }|r||                                d          }n j        |d|            |rHt          j        ||          }|r|d |                                         }n j        |d|            |S )NFr   r   )r   searchr   r   r   )r   contentr   r   rB   rl   r   s        r   extract_sectionzBTreeAssertVisitor.create_c_file_validator.<locals>.extract_section   s     T	%11 T%eiikkll3GGD)5SSSS R	#w// R%nu{{}}n5GGD)3eyQQQQNr   c                 D   
D ]L}t          |          \  }}} | |||          }t          j        ||          s 	j        |d|            MD ]L}t          |          \  }}} | |||          }t          j        ||          r 	j        |d|            Md S )NFr   T)r   r   r   r   )r   r   r   r   r   sectionantipatternantipatternsr   rl   patternsr   s          r   validate_file_contentzHTreeAssertVisitor.create_c_file_validator.<locals>.validate_file_content   s    # V V&4W&=&=#sG)/)WeSIIy'22 VD)7%9UUUU+ Y Y*8*E*E'sK)/)WeSII9['22 YD);diXXXXY Yr   c                     | j         }ss| S t          |d          5 }|                                }d d d            n# 1 swxY w Y   t          |          } ||           d S )Nutf8)encoding)c_fileopenread_strip_c_comments)r   r   fr   r   r   r   s       r   validate_c_filezBTreeAssertVisitor.create_c_file_validator.<locals>.validate_c_file   s    ]F  fv... #!&&((# # # # # # # # # # # # # # #'00G!!&'22222s   AAA)r   r   )r   r   r   r   rl   r   r   s   ` @@@@@r   create_c_file_validatorz)TreeAssertVisitor.create_c_file_validator   s    !%!143G,	 	 		 	 	 	 	 		Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y		3 		3 		3 		3 		3 		3 		3 r   c                    |j         }d|v r=|d         D ]4}t          j        ||          t          j        |j        d|z             5d|v r?|d         D ]6}t          j        ||          }|t          j        |j        d|z             7d|v r | j                            |d                    d|v r"| j                            |d                    d S d S )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   r\   r   r   r   r   extendr   )r   r"   r   r]   
first_nodes        r   _check_directivesz#TreeAssertVisitor._check_directives  s$   _
$
22"#<= N N&tT22:LELN N N &33"#=> L L%0t<<
)L"CdJL L L $z11##J/G$HIII$
22 ''
3L(MNNNNN 32r   c                 r    |j         | _        |                     |           |                     |           |S r5   )r   r   r   r!   r   r"   s     r   visit_ModuleNodez"TreeAssertVisitor.visit_ModuleNode!  s9    8t$$$4   r   c                 Z    |                      |           |                     |           |S r5   )r   r!   r   s     r   visit_CompilerDirectivesNodez.TreeAssertVisitor.visit_CompilerDirectivesNode'  s/    t$$$4   r   )r    r&   r'   r   r   r   r   r   r   recurse_to_childrenr%   r(   r)   s   @r   r   r      sv        " " " " "3 3 3jO O O(    
 "5JJJJJr   r   c                    t           j        gt           j        t          j                            |d          gt           j        t          j                            |d          gd}|t          j                    }g d }}t          | d          5 }	 |D ](}|d d         dk    r(|                                                    d                                          	                    d          
                    d	t          j        j                  }t          j                            ||          }	t          j                            t          j                            |	                    s1t          j        t          j                            |	                     ||d }}
|
                                 t          |	d
          }:||                    |           S|                                r|                                                    d          s|                                dvrt'          j        |	                    d                    }|s|d         |dd          }}	 |                    ||         |z              # t,          $ r |                    |           Y $w xY w*	 ||                                 n# ||                                 w w xY wd d d            n# 1 swxY w Y   ||fS )Nz	cython.pyzcythonize.py)PYTHONCYTHON	CYTHONIZErb   s   #####   #r   r   wb)s   """s   '''r   r   )sys
executableosr]   r.   tempfilemkdtempr   r   decoderf   sepexistsdirnamemakedirscloserM   lstripre   shlexrB   r   KeyError)	tree_fileworkdircython_rootprogramsheadercur_filer   rX   filenamer]   to_closecommandprogargss                 r   unpack_source_treer   /  s   >">27<<[#I#IJnbgll;&O&OP H "$$4HF	i		 !!	! 3 38x''#zz||11$77==??FFvNNVVWZ\^\c\ghhH7<<::D7>>"'//$*?*?@@ ;BGOOD$9$9:::+-5t( (((#D$//HH)NN4((((ZZ\\ 	3$++--*B*B4*H*H 	3zz||+;;;"'+dkk&.A.A"B"B&0%,QZd3"MM(4.*=>>>>' 3 3 3"MM'222223)3. #    #     $3! ! ! ! ! ! ! ! ! ! ! ! ! ! !6 F?sO   K=G#K5JKJ84K7J88K>K=K..K==LLFc                     t          |t                    rd}d}d}nd}d}d}||}|rt          j        |          }t	          | |||          5 }|                    |           ddd           dS # 1 swxY w Y   dS )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   Nr0   r+   zutf-8)moder   newline)r@   bytestextwrapdedentr   rM   )r   r   r   r   r   r   default_encodingr   s           r   
write_filer   W  s     '5!! #  "# +/'**	idXw	G	G	G 1	                 s   A33A7:A7c                 P   t          | |||           	 t          j                            |          }n# t          $ r d}Y nw xY w|#|t          j                            |           k    r:t          | |||           ||t          j                            |           k    8dS dS )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   r]   getmtimeOSError)r   
newer_thanr   r   r   
other_times         r   write_newer_filer   u  s     y'&8DDDDW%%j11

   


 

bg.>.>y.I.I I I9gfxHHHH 

bg.>.>y.I.I I I I I I Is   5 AAc                 L   t          d          }	 t          j                    5 }t          | t	          |          g          }|                                }ddd           n# 1 swxY w Y   |r|d         |S # t          j        $ r}t          |j                  d}~ww xY w)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.
    testrb   Nr   )	r	   r   local_errorsr   r   
substituteCompileErrorSyntaxErrormessage_only)rg   contexterrorsr   rp   s        r   py_parse_coder     s     !((G	* "" 	)f!$)G2D2D1EFFFF&&((F	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)  	)OM * * *!.)))*s:   A; 4A$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   r1   TestCaser3   ry   compilesubr   _strip_cython_code_from_htmlr   r   r   r   r   r   rw   r   r   <module>r      s   				 				   



               " " " " " " X X X X X X X X X X ; ; ; ; ; ; ; ;       3 3 3 3 3 3    [   .- - -L  L  L  L  L " L  L  L ^    J   L GJBJ
BF62  	 	  r    'wzrz
BF8R 
 
	 
	( ( r    
 
 
^6 ^6 ^6 ^6 ^6( ^6 ^6 ^6B% % %P   <I I I I&* * * * *r   