o
    j<                     @   sJ  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 deZdd ZG dd dejZG dd deZeeedddjdZeeedddjdZdd ZG dd deZ dd Z!d$ddZ"d$d d!Z#d"d# Z$dS )%    N)partial   )Errors)
CodeWriter)TreeFragmentstrip_common_indentStringParseContext)TreeVisitorVisitorTransform)TreePath)	PostParsec                       s$   e Zd Z fddZdd Z  ZS )NodeTypeWriterc                    s   t    d| _g | _d S )Nr   )super__init___indentsresultself	__class__ Q/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/Cython/TestUtils.pyr      s   

zNodeTypeWriter.__init__c                 C   s   | j sd}n| j d }|d d urd|dd  }n|d }| jd| j d||jjf   |  jd7  _| | |  jd8  _d S )	Nz(root)   z%s[%d]r      z  z%s: %s)Zaccess_pathr   appendr   r   __name__visitchildren)r   nodenameZtipr   r   r   
visit_Node   s   

zNodeTypeWriter.visit_Node)r   
__module____qualname__r   r    __classcell__r   r   r   r   r      s    r   c                 C   s(   t  }||  ddg|j dg 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wr   r   r   	treetypes)   s   
r*   c                   @   sj   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dd ZefddZdd ZdS )
CythonTestc                 C      t   d S Nr   Zinit_threadr   r   r   r   setUp5      zCythonTest.setUpc                 C   r,   r-   r.   r   r   r   r   tearDown8   r0   zCythonTest.tearDownc              	   C   s   t |ts
|d}t |ts|d}tt||D ]\}\}}| ||d|||f  q| t|t|dd|d|f  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:
%s
Expected:
%sN)
isinstancelistsplit	enumeratezipassertEquallenr'   )r   expectedr   idxexpected_lineZresult_liner   r   r   assertLines;   s   



zCythonTest.assertLinesc                 C   s   t  }|| |jjS r-   )r   writer   lines)r   treewriterr   r   r   codeToLinesG   s   
zCythonTest.codeToLinesc                 C   s   d | |S )Nr$   )r'   rB   )r   r@   r   r   r   codeToStringL   s   zCythonTest.codeToStringc              	   C   st   |  |}t|d}tt||D ]\}\}}| ||d|||f  q| t|t|dd||f  d S )Nr$   zLine %d:
Got: %s
Exp: %sr2   )rB   r   r5   r6   r7   r8   r9   r'   )r   r:   result_treeZresult_linesZexpected_linesr;   liner<   r   r   r   
assertCodeO   s   
zCythonTest.assertCodec                 C   s   |  t||d d|  d S )Nz"Path '%s' not found in result tree)ZassertNotEqualr   
find_first)r   pathrD   r   r   r   assertNodeExistsZ   s   zCythonTest.assertNodeExistsNc                 C   sV   |du ri }|du rg }|   }|dr|tdd }|dd}t||||dS )zNSimply create a tree fragment using the name of the test-case in parse errors.Nz	__main__.._pipeline)id
startswithr9   replacer   )r   codepxdsrM   r   r   r   r   fragment^   s   
zCythonTest.fragmentc                 C   s   t |S r-   )r*   )r   r(   r   r   r   r*   j   s   zCythonTest.treetypesc              
   C   sT   z|  |  d|  W dS  |y) } z| t|| |W  Y d}~S d}~w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)failZ
assertTruer3   )r   funcexc_typeer   r   r   should_failm   s   zCythonTest.should_failc              
   C   s>   z| W S  t y } z| t| W Y d}~dS d}~w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)	ExceptionrT   str)r   rU   excr   r   r   should_not_failx   s   zCythonTest.should_not_fail)NN)r   r!   r"   r/   r1   r=   rB   rC   rF   rI   rS   r*   rY   rX   r\   r   r   r   r   r+   3   s    
r+   c                   @   s   e Zd ZdZd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                 C   s0   |d u ri }|  ||j}|D ]}||}q|S r-   )rS   r(   )r   rM   pyxrR   r@   Tr   r   r   run_pipeline   s   
zTransformTest.run_pipeliner-   )r   r!   r"   __doc__r`   r   r   r   r   r]      s    r]   z\s+r%   zz
        /[*] (
            (?: [^*\n] | [*][^/] )*
            [\n]
            (?: [^*] | [*][^/] )*
        ) [*]/
    z\s\s+z
    (?:
        <pre class=["'][^"']*cython\s+line[^"']*["']\s*>
        (?:[^<]|<(?!/pre))+
        </pre>
    )|(?:
        <style[^>]*>
        (?:[^<]|<(?!/style))+
        </style>
    )
    c                 C   s   d  }}|  drtjd| dd  dd\}} |  } |  dr?| dd   } |  dr?tjd| dd  dd\}} |  } ||| fS )N/z(?<!\\)/r   )maxsplit:)rO   rer5   strip)patternstartendr   r   r   _parse_pattern   s   



rj   c                       sB   e Zd Z fddZdd Zdd Zdd Zd	d
 Zej	Z
  ZS )TreeAssertVisitorc                    s    t    d | _g | _g | _d S r-   )r   r   _module_pos_c_patterns_c_antipatternsr   r   r   r   r      s   

zTreeAssertVisitor.__init__c                    sL   j j dd fdd fdd fdd}|S )	Nc                 S   s"   t | d||r	dnd|f  d S )NzPattern '%s' %s found in %swaszwas not)r   error)posrg   found	file_pathr   r   r   rT      s
   
z7TreeAssertVisitor.create_c_file_validator.<locals>.failc                    sv   |rt ||}|r|| d  }n	 j|d| d |r9t ||}|r0|d |  }|S  j|d| d |S )NFrr   rs   )re   searchri   rl   rh   )rs   contentrh   ri   r5   )rT   r   r   r   extract_section   s   zBTreeAssertVisitor.create_c_file_validator.<locals>.extract_sectionc                    s   D ]}t |\}}}| |||}t||s!j|d| d q D ]}t |\}}}| |||}t||rCj|d| d q$d S )NFrt   T)rj   re   ru   rl   )rs   rv   rg   rh   ri   sectionZantipattern)antipatternsrw   rT   patternsr   r   r   validate_file_content   s   zHTreeAssertVisitor.create_c_file_validator.<locals>.validate_file_contentc                    s\   | j }s	 s	| S t|dd}| }W d    n1 sw   Y  t|}|| d S )Nutf8)encoding)c_fileopenread_strip_c_comments)r   r~   frv   )ry   rz   r{   r   r   validate_c_file   s   
zBTreeAssertVisitor.create_c_file_validator.<locals>.validate_c_file)rm   rn   )r   r   r   )ry   rw   rT   rz   r   r{   r   create_c_file_validator   s   z)TreeAssertVisitor.create_c_file_validatorc           	      C   s  |j }d|v r|d D ]}t||d u rt|jd|  qd|v r=|d D ]}t||}|d ur<t|jd|  q'd|v rI| j|d  d|v rU| j|d  d|v r|d }|d urdd	l	m
} | }||j }||kr||j t|jd
 d S d S d S d S )NZtest_assert_path_existsz+Expected path '%s' not found in result treeZtest_fail_if_path_existsz)Unexpected path '%s' found in result treeZtest_assert_c_code_hasZtest_fail_if_c_code_hasZ"test_body_needs_exception_handlingr   )HasNoExceptionHandlingVisitorz,Node had unexpected exception handling value)
directivesr   rG   r   rp   rq   rm   extendrn   Compiler.ParseTreeTransformsr   body)	r   r   r   rH   Z
first_nodevaluer   visitorr   r   r   r   _check_directives  sJ   
z#TreeAssertVisitor._check_directivesc                 C   s    |j | _| | | | |S r-   )rq   rl   r   r   r   r   r   r   r   visit_ModuleNode-  s   

z"TreeAssertVisitor.visit_ModuleNodec                 C   s   |  | | | |S r-   )r   r   r   r   r   r   visit_CompilerDirectivesNode3  s   

z.TreeAssertVisitor.visit_CompilerDirectivesNode)r   r!   r"   r   r   r   r   r   r
   Zrecurse_to_childrenr    r#   r   r   r   r   rk      s    5 rk   c              
   C   s  t jgt jtj|dgt jtj|dgd}|d u r t }g d }}t| d}z|D ]}|d d dkrw| d 	d
d	tjj}tj||}	tjtj|	sdttj|	 |d urq|d }
}|
  t|	d
}q.|d ur|| q.| r| ds| dvrt|	d}|sq.|d |dd  }}z||| |  W q. ty   || Y q.w q.W |d ur|  n
|d ur|  w w W d    ||fS 1 sw   Y  ||fS )Nz	cython.pyzcythonize.py)PYTHONZCYTHONZ	CYTHONIZErb   s   #####   #r|   rb   wb)s   """s   '''r   r   )sys
executableosrH   r'   tempfilemkdtempr   rf   decoderP   sepexistsdirnamemakedirscloser>   lstriprO   shlexr5   r   KeyError)Z	tree_fileZworkdirZcython_rootZprogramsheaderZcur_filer   rE   filenamerH   to_closecommandprogargsr   r   r   unpack_source_tree;  sX   
$


r   Fc                 C   s~   t |trd}d}d}nd}d}d}|du r|}|rt|}t| |||d}|| W d   dS 1 s8w   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   Nr)   r$   zutf-8)moder}   newline)r3   bytestextwrapdedentr   r>   )rs   rv   r   r}   r   r   Zdefault_encodingr   r   r   r   
write_filec  s   

"r   c                 C   s   t | |||d ztj|}W n ty   d}Y nw |du s(|tj| kr>t | |||d |du s(|tj| ks(d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   rH   getmtimeOSError)rs   Z
newer_thanrv   r   r}   Z
other_timer   r   r   write_newer_file  s    r   c              
   C   s   t d}z*t }t| t|gd}| }W d   n1 s!w   Y  |r,|d |W S  tjy@ } zt|jd}~w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.
    testrL   Nr   )	r   r   Zlocal_errorsr   r   
substituteZCompileErrorSyntaxErrorZmessage_only)rQ   contexterrorsr   rW   r   r   r   py_parse_code  s   


r   )FN)%r   re   Zunittestr   r   r   r   	functoolsr   ZCompilerr   r   ZCompiler.TreeFragmentr   r   r   ZCompiler.Visitorr	   r
   r   r   r   r   r*   ZTestCaser+   r]   compilesubr   Z_strip_cython_code_from_htmlrj   rk   r   r   r   r   r   r   r   r   <module>   sD    
O&
m
(
