U
    {h!                     @   s   d dl Zd dlZd dlmZ d dlT d dlmZ d dlT d dlm	Z	m
Z
mZ G dd deZG dd	 d	ZG d
d deZeZG dd deZG dd dejZedkrd dlZe  dS )    N)TransformTest)*)_calculate_pickle_checksums)MainSymtabOptionsc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestNormalizeTreec                 C   s"   |  dj}| d| | d S )Nif x: yz
(root): StatListNode
  stats[0]: IfStatNode
    if_clauses[0]: IfClauseNode
      condition: NameNode
      body: ExprStatNode
        expr: NameNode
)fragmentrootassertLines	treetypesselft r   Q/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Compiler/Tests/TestParseTreeTransforms.py)test_parserbehaviour_is_what_we_coded_for   s    z;TestNormalizeTree.test_parserbehaviour_is_what_we_coded_forc                 C   s(   |  td gd}| d| | d S )Nr	   z
(root): StatListNode
  stats[0]: IfStatNode
    if_clauses[0]: IfClauseNode
      condition: NameNode
      body: StatListNode
        stats[0]: ExprStatNode
          expr: NameNode
run_pipelineZNormalizeTreer   r   r   r   r   r   test_wrap_singlestat   s    z&TestNormalizeTree.test_wrap_singlestatc                 C   s(   |  td gd}| d| | d S )Nz?
            if z:
                x
                y
        z
(root): StatListNode
  stats[0]: IfStatNode
    if_clauses[0]: IfClauseNode
      condition: NameNode
      body: StatListNode
        stats[0]: ExprStatNode
          expr: NameNode
        stats[1]: ExprStatNode
          expr: NameNode
r   r   r   r   r   test_wrap_multistat#   s    
z%TestNormalizeTree.test_wrap_multistatc                 C   s(   |  td gd}| d| | d S )Nz!
            a, b = x, y
        z
(root): StatListNode
  stats[0]: SingleAssignmentNode
    lhs: TupleNode
      args[0]: NameNode
      args[1]: NameNode
    rhs: TupleNode
      args[0]: NameNode
      args[1]: NameNode
r   r   r   r   r   test_statinexpr5   s    	z!TestNormalizeTree.test_statinexprc                 C   s(   |  td gd}| d| | d S )NzI
            x
            y
            if z:
                x
        a  
(root): StatListNode
  stats[0]: ExprStatNode
    expr: NameNode
  stats[1]: ExprStatNode
    expr: NameNode
  stats[2]: IfStatNode
    if_clauses[0]: IfClauseNode
      condition: NameNode
      body: StatListNode
        stats[0]: ExprStatNode
          expr: NameNode
r   r   r   r   r   test_wrap_offagainD   s    z$TestNormalizeTree.test_wrap_offagainN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r      s
   r   c                   @   s   e Zd Zdd Zdd ZdS )TestWithTransformc                 C   s"   |  td gd}| d| d S )Nz0
        with x:
            y = z ** 3
        ax  

        $0_0 = x
        $0_2 = $0_0.__exit__
        $0_0.__enter__()
        $0_1 = True
        try:
            try:
                $1_0 = None
                y = z ** 3
            except:
                $0_1 = False
                if (not $0_2($1_0)):
                    raise
        finally:
            if $0_1:
                $0_2(None, None, None)

        r   ZWithTransformZ
assertCoder   r   r   r   test_simplified\   s    z!TestWithTransform.test_simplifiedc                 C   s"   |  td gd}| d| d S )Nz5
        with x as y:
            y = z ** 3
        a  

        $0_0 = x
        $0_2 = $0_0.__exit__
        $0_3 = $0_0.__enter__()
        $0_1 = True
        try:
            try:
                $1_0 = None
                y = $0_3
                y = z ** 3
            except:
                $0_1 = False
                if (not $0_2($1_0)):
                    raise
        finally:
            if $0_1:
                $0_2(None, None, None)

        r   r   r   r   r   
test_basicv   s    zTestWithTransform.test_basicN)r   r   r   r   r    r   r   r   r   r   Z   s   r   c                       sP   e Zd ZdZdZddddddddZ fdd	Zd
d Zdd Zdd Z	  Z
S )TestInterpretCompilerDirectiveszO
    This class tests the parallel directives AST-rewriting and importing.
    a?  
        cimport cython.parallel
        cimport cython.parallel as par
        from cython cimport parallel as par2
        from cython cimport parallel

        from cython.parallel cimport threadid as tid
        from cython.parallel cimport threadavailable as tavail
        from cython.parallel cimport prange
    cython.parallelzcython.parallel.threadidzcython.parallel.threadavailablezcython.parallel.prange)r"   parZpar2paralleltidZtavailZprangec                    sR   t    ttj}tj|}t||j	}t
dd ||_|g| _tj| _d S )N__main__)supersetUpr   ZCompilationOptionsZdefault_optionsr   ContextZfrom_optionsZInterpretCompilerDirectivesZcompiler_directivesr   ZModuleScopeZmodule_scopepipeline
DebugFlagsdebug_exception_on_error)r   Zcompilation_optionsctxZ	transform	__class__r   r   r(      s    
z%TestInterpretCompilerDirectives.setUpc                 C   s   | j t_ d S N)r,   r+   )r   r   r   r   tearDown   s    z(TestInterpretCompilerDirectives.tearDownc                 C   s.   |  | j| j | jd j}| || j d S )Nr   )r   r*   import_codeparallel_directivesassertEqualexpected_directives_dictr   r3   r   r   r   !test_parallel_directives_cimports   s    zATestInterpretCompilerDirectives.test_parallel_directives_cimportsc                 C   s6   |  | j| jdd | jd j}| || j d S )NZcimportimportr   )r   r*   r2   replacer3   r4   r5   r6   r   r   r    test_parallel_directives_imports   s
    z@TestInterpretCompilerDirectives.test_parallel_directives_imports)r   r   r   __doc__r2   r5   r(   r1   r7   r:   __classcell__r   r   r.   r   r!      s   r!   c                   @   s   e Zd Zdd Zdd ZdS )TestDebugTransformc                    s   t  fdd|D S )Nc                 3   s   | ]}| j kV  qd S r0   attrib).0attrelemr   r   	<genexpr>   s     z3TestDebugTransform.elem_hasattrs.<locals>.<genexpr>)all)r   rC   attrsr   rB   r   elem_hasattrs   s    z TestDebugTransform.elem_hasattrsc                    s:  zt j| jsttj| j}t|	d}|s:tdd |D }| 
t|t| t|	d}|srtdd |D  | 
t|t  | 
d|d | 
d|d	 d
}d}t fdd|D st fdd|D \}}}| 
|jd d | d|jd  | ||s(tt|	d}	|	s@t|	jdd d dd |	D }
| 
td|
 | |	d |stt|	d}|st| 
dtt| |	d}dd |D }|st| 
dt| d|kstd |kstW n4   t| j}zt|  W 5 |  X  Y nX d S )!Nz/Module/Globalsc                 S   s   i | ]}|j d  |j d qS )nametyper>   r@   er   r   r   
<dictcomp>   s      z6TestDebugTransform.test_debug_info.<locals>.<dictcomp>z/Module/Functionsc                 S   s   i | ]}|j d  |qS )qualified_namer>   rJ   r   r   r   rL      s      ZCObjectZc_varZPythonObjectZ
python_var)zcodefile.spamzcodefile.hamzcodefile.eggszcodefile.closurezcodefile.inner)rH   cnamerM   c                 3   s   | ]}| kV  qd S r0   r   )r@   fZ	xml_funcsr   r   rD      s     z5TestDebugTransform.test_debug_info.<locals>.<genexpr>c                    s   g | ]} | qS r   r   )r@   funcnamerP   r   r   
<listcomp>   s     z6TestDebugTransform.test_debug_info.<locals>.<listcomp>rH   spamrN   ZLocalsc                 S   s
   | j d S )NrH   r>   )rK   r   r   r   <lambda>       z4TestDebugTransform.test_debug_info.<locals>.<lambda>)keyc                 S   s   g | ]}|j d  qS rH   r>   rJ   r   r   r   rR      s     Zabcdr   	Arguments   ZStepIntoFunctionsc                 S   s   g | ]}|j d  qS rW   r>   )r@   xr   r   r   rR     s        ZputsZsome_c_function)ospathexistsZ
debug_destAssertionErrorDebugWriteretreeparselistfindr4   lengetrE   r?   ZassertNotEqualrG   sortopencloseprintread)r   r   LZxml_globalsZ	funcnamesZrequired_xml_attrsrS   ZhameggsZspam_localsnamesZspam_argumentsZ	step_intoZspam_stepintorO   r   rP   r   test_debug_info   sR    





z"TestDebugTransform.test_debug_infoN)r   r   r   rG   ro   r   r   r   r   r=      s   r=   c                   @   s   e Zd Zdd ZdS ) TestAnalyseDeclarationsTransformc                 C   s4   t dddg}dt|  kr&dks0n t|d S )NZmember1Zmember2Zmember3r[      )r   re   r_   )r   Z	checksumsr   r   r   test_calculate_pickle_checksums  s    z@TestAnalyseDeclarationsTransform.test_calculate_pickle_checksumsN)r   r   r   rr   r   r   r   r   rp     s   rp   r&   )os.pathr\   ZunittestZCython.TestUtilsr   Z#Cython.Compiler.ParseTreeTransformsr   ZCython.Compiler.NodesZCython.Compilerr   r   r   r   r   r!   r`   ZDebuggerTestCaseobjectr=   ZTestCaserp   r   mainr   r   r   r   <module>   s   O7?B