
    Ngo9              	          d dl m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Zd dl	m
Z
 d dlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ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S )    )absolute_importN)open)partial   )Errors)
CodeWriter)TreeFragmentstrip_common_indent)TreeVisitorVisitorTransform)TreePathc                   $     e Zd Z fdZd Z xZS )NodeTypeWriterc                 r    t          t          |                                            d| _        g | _        d S )Nr   )superr   __init___indentsresultself	__class__s    L/var/www/html/ai-engine/env/lib/python3.11/site-packages/Cython/TestUtils.pyr   zNodeTypeWriter.__init__   s1    nd##,,...    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! 3qs8+1v54=0(,dn.E.EFG 	H 	H 	H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::seah&#.///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.setUp7       r   c                 ,    t          j                     d S r5   r6   r8   s    r   tearDownzCythonTest.tearDown:   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)) 	-~~e,,H&$'' 	)\\%((F1:3x;P;P1Q1Q 	_ 	_-C--]K9S-Q\<]]_ _ _ _XFGKyyQYGZGZGZGZ\a\f\fgm\n\n\no	q 	q 	q 	q 	qr   c                 `    t                      }|                    |           |j        j        S r5   )r   writer   lines)r   treewriters      r   codeToLineszCythonTest.codeToLinesI   s(    T}""r   c                 R    d                     |                     |                    S )Nr+   )r.   rQ   )r   rO   s     r   codeToStringzCythonTest.codeToStringN   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.assertCodeQ   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.assertNodeExists\   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pxdsrb   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.treetypesl   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_failo   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)	Exceptionrk   str)r   rm   excs      r   should_not_failzCythonTest.should_not_failz   s[    	 466M 	  	  	 IIc#hh	 s   	 
A">A)NN)r    r&   r'   r9   r<   rK   rQ   rS   rY   r^   rh   r1   rr   rp   ru    r   r   r3   r3   5   s            
q 
q 
q# # #
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   )rh   r/   )r   rb   pyxrg   rO   Ts         r   run_pipelinezTransformTest.run_pipeline   sE    <D}}S$'', 	 	A1T77DDr   r5   )r    r&   r'   __doc__r|   rv   r   r   rx   rx      s2         .     r   rx   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:)rd   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                     t          t          |                                            d | _        g | _        g | _        d S r5   )r   r   r   _module_pos_c_patterns_c_antipatternsr   s    r   r   zTreeAssertVisitor.__init__   s>    &&//111!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   rk   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   rk   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   rk   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          |          } ||           t          j                            |          d         dz   }t          j                            |          rt          j                            |          t          j                            |          k    r\t          |d          5 }|                                }d d d            n# 1 swxY w Y   t          |          } ||           d S d S d S )Nutf8)encodingr   z.html)
c_filer   read_strip_c_commentsosr]   splitextexistsgetmtime_strip_cython_code_from_html)r   r   fr   	html_filer   r   r   s        r   validate_c_filezBTreeAssertVisitor.create_c_file_validator.<locals>.validate_c_file  s   ]F  fv... #!&&((# # # # # # # # # # # # # # #'00G!!&'222((003g=Iw~~i(( :RW-=-=f-E-EIYIYZcIdId-d-d)f555 'ffhhG' ' ' ' ' ' ' ' ' ' ' ' ' ' '6w??%%i99999	: :-d-ds#   AAA?D  D$'D$)r   r   )r   r   r   r   rk   r   r   s   ` @@@@@r   create_c_file_validatorz)TreeAssertVisitor.create_c_file_validator   s    !%!143G,	 	 		 	 	 	 	 		Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y	: 	: 	: 	: 	: 	: 	:$ 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_CompilerDirectivesNode0  s/    t$$$4   r   )r    r&   r'   r   r   r   r   r   r   recurse_to_childrenr%   r(   r)   s   @r   r   r      sv        " " " " ": : :x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
executabler   r]   r.   tempfilemkdtempr   r   decodere   sepr   dirnamemakedirscloserM   lstriprd   shlexrB   r   KeyError)	tree_fileworkdircython_rootprogramsheadercur_filer   rX   filenamer]   to_closecommandprogargss                 r   unpack_source_treer   8  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   `  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]   r   OSError)r   
newer_thanr   r   r   
other_times         r   write_newer_filer   ~  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)FN)%
__future__r   r   r   unittestr   r   r   r   ior   	functoolsr   Compilerr   r   Compiler.TreeFragmentr	   r
   Compiler.Visitorr   r   r   r   r1   TestCaser3   rx   compilesubr   r   r   r   r   r   r   rv   r   r   <module>r      s   & & & & & & 				 				   



                     " " " " " " D D D D D D D D ; ; ; ; ; ; ; ;          [   .0 0 0L  L  L  L  L " L  L  L ^    J   L GJBJ
BF62  	 	  r    'wzrz
BF8R 
 
	 
	( ( r    
 
 
e6 e6 e6 e6 e6( e6 e6 e6P% % %P   <I I I I I Ir   