
    J/Phd                     r   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZ d dlmZ i ZdddZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z& G d d e          Z' G d! d"e          Z( G d# d$e          Z)e*d%k    r ej+                     dS dS )&    N)jit)CFGraphControlFlowAnalysis)types)FunctionIdentityByteCode_fix_LOAD_GLOBAL_arg)TestCaseT)_nrtnopythonc                 8    d}t          |           D ]}||z  }|S Nr   rangexyresultis       ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/tests/test_flow_control.pyfor_loop_usecase1r      s,    F1XX  !M    c                 b    d}t          t          | |d                    D ]\  }}|||z  z  }|S )Nr   )	enumerater   r   r   r   r   js        r   for_loop_usecase2r      s@    F%1b//**  1!a%Mr   c                 &    d}| |g}|D ]}||z  }|S r    )r   r   r   lstr   s        r   for_loop_usecase3r"      s/    Fa&C  !Mr   c                 \    d}t          d          D ]}t          d          D ]}|dz  }|S )Nr   
      r   r   s        r   for_loop_usecase4r&   %   sF    F2YY  r 	 	AaKFF	Mr   c                 H    d}t          |           D ]}|dz  }||k    r n|S Nr   r%   r   r   s       r   for_loop_usecase5r)   -   s=    F1XX  !A::E Mr   c                 F    d}t          |           D ]}||k    r	|dz  }|S r(   r   r   s       r   for_loop_usecase6r+   6   s8    F1XX  q55!Mr   c                 X    t          |           D ]}d} t          |           D ]}  dS dS r(   r   )r   r   r   r   s       r   for_loop_usecase7r-   ?   sB    1XX  q 	 	A1111r   c                 H    d}t          | ||| z
  dz             D ]}|dz  }|S r(   r   r   s       r   for_loop_usecase8r/   I   s:    F1aQ##  !Mr   c                     d}t          |           D ]+}d} t          |           D ]}|| dz  k    r||z  } n||z  },|S )Nr      r   )r   r   zr   r   s        r   for_loop_usecase9r3   P   sg    	A1XX  q 	 	AAEzzQ  FAHr   c                 H    t          |           D ]}||k    r|} n|dz  }|S )Nr1   r   )r   r   r   r2   s       r   for_loop_usecase10r5   ^   s?    1XX  66AE  EHr   c                 :    d}d}|| k     r||z  }|dz  }|| k     |S r(   r    r   s       r   while_loop_usecase1r7   h   s9    F	A
a%%!	Q a%% Mr   c                 ,    d}|| k    r|dz  }|| k    |S r(   r    r   r   r   s      r   while_loop_usecase2r:   q   s)    F
A++! A++Mr   c                 f    d}d}d}|| k     r$||k     r|||z   z  }|dz  }|dz  }||k     || k     $|S r(   r    r   s        r   while_loop_usecase3r<   x   s_    F	A	A
a%%!eea!eOFFAFA !ee a%%
 Mr   c                 &    d}	 |dz  }|| k    rn|S )Nr   Tr%   r    r9   s      r   while_loop_usecase4r>      s-    F!A:: Mr   c                 D    d}|| k     r||k    r|dz  }|dz  }|| k     |S )Nr   r1   r%   r    r9   s      r   while_loop_usecase5r@      s@    F
1**A::aKF!	 1**
 Mr   c                 $    | dk    rn|dk    rn	 dS )Nr   Tr    r   r   s     r   ifelse_usecase1rC      s"    1uu	
Q4r   c                 2    | |k    rdS | dk    s|dk    rdS dS )Nr%   r   r1      r    rB   s     r   ifelse_usecase2rF      s+    1uuq	
a166qqr   c                 F    | dk    r|dk    rdS |dk     rdS dS | dk     rdS dS r(   r    rB   s     r   ifelse_usecase3rH      s?    1uuq551UU11	
Qqqr   c                     | |k    rdS d S )Nr%   r    rB   s     r   ifelse_usecase4rJ      s    Avvq vr   c                     | |k    rdndS )NTFr    rB   s     r   ternary_ifelse_usecase1rL      s    q5544e#r   c                 P    | }|}	 	 ||dz
  k    rn|dz  }|dz  }||k    rn||fS )NTr%   r    )r   r   Lr   s       r   double_infinite_looprO      s[    	A	A	AEzzFA	 	
Q66 a4Kr   c                  *    	 d S # t           $ r Y d S w xY wN)	Exceptionr    r   r   try_except_usecaserS      s.       s   
c                   h   e Zd ZefdZefdZd ZefdZd ZefdZ	d Z
efdZd	 Zefd
Zd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd ZefdZd Z efdZ!d Z"efd Z#d! Z$efd"Z%d# Z&efd$Z'd% Z(efd&Z)d' Z*efd(Z+d) Z,efd*Z-d+ Z.d,S )-TestFlowControlc                     t          t          j        t          j        ffi ||          }t          j        ||          D ]\  }}d }d }		  |||          }
n# t
          $ r}|}Y d }~nd }~ww xY w	  |||          }||                     d|z              |                     |
|           n# t
          $ r?}| |}	|                     t          |          t          |	                     Y d }~d }~ww xY wd S )Nz(Invalid for pure-python but numba works
)	r   r   intp	itertoolsproductrR   failassertEqualtype)selfpyfunc
x_operands
y_operandsflagscfuncr   r   pyerrcerrpyresecress                r   run_testzTestFlowControl.run_test   sW   6UZ,6666v>>%j*== 	. 	.DAqEDq!   .uQ{{ $III#$ % % %  ----  : : :=  ed4jj99999999	:	. 	.s0   A
A/#A**A/3B00
C9:5C44C9c                 H    |                      t          g ddg|           d S )Nir   r$   r   ra   )rh   r   r]   ra   s     r   test_for_loop1zTestFlowControl.test_for_loop1   s)    's%HHHHHr   c                 <    |                      t                     d S Nrk   )rm   no_pyobj_flagsr]   s    r   test_for_loop1_npmz"TestFlowControl.test_for_loop1_npm       .11111r   c                 J    |                      t          g dg d|           d S )Nrj   rk   )rh   r   rl   s     r   test_for_loop2zTestFlowControl.test_for_loop2   s9    '|||! 	 	# 	# 	# 	# 	#r   c                 <    |                      t                     d S ro   )ru   rp   rq   s    r   test_for_loop2_npmz"TestFlowControl.test_for_loop2_npm   rs   r   c                 F    |                      t          dgdg|           dS )z(
        List requires pyobject
        r%   r1   rk   N)rh   r"   rl   s     r   test_for_loop3zTestFlowControl.test_for_loop3   s7     	'!qc! 	 	# 	# 	# 	# 	#r   c                 <    |                      t                     d S ro   )ry   rp   rq   s    r   test_for_loop3_npmz"TestFlowControl.test_for_loop3_npm  rs   r   c                 F    |                      t          dgdg|           d S Nr$   rk   )rh   r&   rl   s     r   test_for_loop4zTestFlowControl.test_for_loop4	  s'    '"t5AAAAAr   c                 <    |                      t                     d S ro   )r~   rp   rq   s    r   test_for_loop4_npmz"TestFlowControl.test_for_loop4_npm  rs   r   c                 F    |                      t          dgdg|           d S Nd   2   rk   )rh   r)   rl   s     r   test_for_loop5zTestFlowControl.test_for_loop5  '    '#EBBBBBr   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_for_loop5_npmz"TestFlowControl.test_for_loop5_npm  rs   r   c                 F    |                      t          dgdg|           d S r   )rh   r+   rl   s     r   test_for_loop6zTestFlowControl.test_for_loop6  r   r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_for_loop6_npmz"TestFlowControl.test_for_loop6_npm  rs   r   c                 F    |                      t          dgdg|           d S )N   r   rk   )rh   r-   rl   s     r   test_for_loop7zTestFlowControl.test_for_loop7  s'    '!qc?????r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_for_loop7_npmz"TestFlowControl.test_for_loop7_npm  rs   r   c                 J    |                      t          ddgg d|           d S Nr   r%   )r   r1   r$   rk   )rh   r/   rl   s     r   test_for_loop8zTestFlowControl.test_for_loop8!  +    '!Q5IIIIIr   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_for_loop8_npmz"TestFlowControl.test_for_loop8_npm$  rs   r   c                 J    |                      t          ddgg d|           d S r   )rh   r3   rl   s     r   test_for_loop9zTestFlowControl.test_for_loop9'  r   r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_for_loop9_npmz"TestFlowControl.test_for_loop9_npm*  rs   r   c                 H    |                      t          dgddg|           d S )Nr   r1      rk   )rh   r5   rl   s     r   test_for_loop10zTestFlowControl.test_for_loop10-  s)    (1#1vUCCCCCr   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_for_loop10_npmz#TestFlowControl.test_for_loop10_npm0  s    >22222r   c                 F    |                      t          dgdg|           d S Nr$   r   rk   )rh   r7   rl   s     r   test_while_loop1z TestFlowControl.test_while_loop13  '    )B4!EBBBBBr   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_while_loop1_npmz$TestFlowControl.test_while_loop1_npm6      N33333r   c                 F    |                      t          dgdg|           d S r   )rh   r:   rl   s     r   test_while_loop2z TestFlowControl.test_while_loop29  r   r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_while_loop2_npmz$TestFlowControl.test_while_loop2_npm<  r   r   c                 F    |                      t          dgdg|           d S r}   )rh   r<   rl   s     r   test_while_loop3z TestFlowControl.test_while_loop3?  s'    )B4"UCCCCCr   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_while_loop3_npmz$TestFlowControl.test_while_loop3_npmB  r   r   c                 F    |                      t          dgdg|           d S r   )rh   r>   rl   s     r   test_while_loop4z TestFlowControl.test_while_loop4E  r   r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_while_loop4_npmz$TestFlowControl.test_while_loop4_npmH  r   r   c                 J    |                      t          g dg d|           d S )N)r   r   r$   rk   )rh   r@   rl   s     r   test_while_loop5z TestFlowControl.test_while_loop5K  s+    ):::zzzOOOOOr   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_while_loop5_npmz$TestFlowControl.test_while_loop5_npmN  r   r   c                 J    |                      t          g dg d|           d S N)r   r   r%   rk   )rh   rC   rl   s     r   test_ifelse1zTestFlowControl.test_ifelse1Q  *    ozzz:::UKKKKKr   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_ifelse1_npmz TestFlowControl.test_ifelse1_npmT      /////r   c                 J    |                      t          g dg d|           d S r   )rh   rF   rl   s     r   test_ifelse2zTestFlowControl.test_ifelse2W  r   r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_ifelse2_npmz TestFlowControl.test_ifelse2_npmZ  r   r   c                 J    |                      t          g dg d|           d S r   )rh   rH   rl   s     r   test_ifelse3zTestFlowControl.test_ifelse3]  r   r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_ifelse3_npmz TestFlowControl.test_ifelse3_npm`  r   r   c                 J    |                      t          g dg d|           d S r   )rh   rJ   rl   s     r   test_ifelse4zTestFlowControl.test_ifelse4c  r   r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_ifelse4_npmz TestFlowControl.test_ifelse4_npmf  r   r   c                 J    |                      t          g dg d|           d S r   )rh   rL   rl   s     r   test_ternary_ifelse1z$TestFlowControl.test_ternary_ifelse1i  s9    -zzz:::! 	 	# 	# 	# 	# 	#r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_ternary_ifelse1_npmz(TestFlowControl.test_ternary_ifelse1_npmm  s    !!!77777r   c                 F    |                      t          dgdg|           d S r   )rh   rO   rl   s     r   test_double_infinite_loopz)TestFlowControl.test_double_infinite_loopp  s5    *RD1#! 	 	# 	# 	# 	# 	#r   c                 <    |                      t                     d S ro   )r   rp   rq   s    r   test_double_infinite_loop_npmz-TestFlowControl.test_double_infinite_loop_npmt  s    &&^&<<<<<r   N)/__name__
__module____qualname__enable_pyobj_flagsrh   rm   rr   ru   rw   ry   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   rU   rU      s        *. . . .0 $6 I I I I2 2 2 $6 # # # #2 2 2 $6 # # # #2 2 2 $6 B B B B2 2 2 $6 C C C C2 2 2 $6 C C C C2 2 2 $6 @ @ @ @2 2 2 $6 J J J J2 2 2 $6 J J J J2 2 2 %7 D D D D3 3 3 &8 C C C C4 4 4 &8 C C C C4 4 4 &8 D D D D4 4 4 &8 C C C C4 4 4 &8 P P P P4 4 4 "4 L L L L0 0 0 "4 L L L L0 0 0 "4 L L L L0 0 0 "4 L L L L0 0 0 *< # # # #8 8 8 /A # # # #= = = = =r   rU   c                       e Zd ZdZd dZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )!TestCFGraphz3
    Test the numba.controlflow.CFGraph class.
    r   c                     t                      }|D ]}|                    |           |                                D ] \  }}|D ]}|                    ||           !|S )zG
        Build a CFGraph class from a dict of adjacency lists.
        )r   add_nodeitemsadd_edge)r]   dentry_pointgnodedestsdests          r   from_adj_listzTestCFGraph.from_adj_list}  s|     II 	 	DJJt7799 	' 	'KD% ' '

4&&&&'r   c                     |                      ddgdgdgg d          }|                    d           |                                 |S )z
        A simple CFG corresponding to the following code structure:

            c = (... if ... else ...) + ...
            return b + c
                 r   r   r   r   r   r   set_entry_pointprocessr]   r   s     r   	loopless1zTestCFGraph.loopless1  sQ     B8"B4RHHII	!			r   c                     |                      ddgdgdgg ddgddgdgg d          }|                    d           |                                 |S )	zc
        Same as loopless1(), but with added dead blocks (some of them
        in a loop).
        r   r   r   r   [   ]   \   )r   r   r   r   r   r   r   ^   r   r   s     r   loopless1_dead_nodesz TestCFGraph.loopless1_dead_nodes  su    
 Rab 	 	 	
!			r   c           	          |                      ddgdgdgddgg g d          }|                    d           |                                 |S )aA  
        A loopless CFG corresponding to the following code structure:

            c = (... if ... else ...) + ...
            if c:
                return ...
            else:
                return ...

        Note there are two exit points, and the entry point has been
        changed to a non-zero value.
        r   r   r   *   "   c   r   r   r   r   r   r   r   r   s     r   	loopless2zTestCFGraph.loopless2  s`     bt"B8LLN N	"			r   c                    |                      i ddgdddgddgddgdddgdd	d
gd	dgd
dgddgddgddgddgdddgdddgdg ddgdg           }|                    d           |                                 |S )a^  
        A CFG with multiple nested loops:

            for y in b:
                for x in a:
                    # This loop has two back edges
                    if b:
                        continue
                    else:
                        continue
            for z in c:
                if z:
                    return ...
        r   r   r$   <         8          ,   9   =   D   W   G   P   X   r   r   s     r   multiple_loopszTestCFGraph.multiple_loops  s3      (A3  ( !B8 ( "RD ( !#RD ( !#RH	 (
 !#RH ( !#RD ( !#RD ( !#RD ( !#QC ( !#RD ( !#RD ( !#RH ( !#RH ( !#B ( !#RD (  !#B! ( ! !$ 	
!			r   c                     |                      dgddgddgg ddgdgdgg d          }|                    d	           |                                 |S )
a  
        A CFG with three loop exits, one of which is also a function
        exit point, and another function exit point:

            for x in a:
                if a:
                    return b
                elif b:
                    break
            return c
        r   r$   $      r      %   r   r   r$   r  r   r  r  r  r   r   r   s     r   multiple_exitszTestCFGraph.multiple_exits  su     Rba 	 	 	
!			r   c           	          |                      ddgg dgddgdgdgd          }|                    d           |                                 |S )z
        A CFG with a infinite loop and an alternate exit point:

            if c:
                return
            while True:
                if a:
                    ...
                else:
                    ...
        r$      r      r  r   r  r$   r   r  r  r   r   r   s     r   infinite_loop1zTestCFGraph.infinite_loop1  sb     QBRDr2hRDrdKKM M	!			r   c                     |                      dgddgdgdgd          }|                    d           |                                 |S )z
        A CFG with no exit point at all:

            while True:
                if a:
                    ...
                else:
                    ...
        rE      	   r   rE   r  r  r   r   r   s     r   infinite_loop2zTestCFGraph.infinite_loop2  sS     A3B7sDDEE	!			r   c                    |                                  }|                     t          |                    d                    ddg           |                     t          |                    d                    g            |                     t          |                    d                    g            |                     t          |                    d                    ddg           d S )Nr   )r   N)r   Nr   )r   r[   sorted
successorspredecessorsr   s     r   test_simple_propertiesz"TestCFGraph.test_simple_properties  s    NNQ00:z2JKKKR 0 0112666q 1 122B777r 2 233j*5MNNNNNr   c                    |                                  }|                     t          |                                          dg           |                                 }|                     t          |                                          dg           |                                 }|                     t          |                                          ddg           |                                 }|                     t          |                                          ddg           |                                 }|                     t          |                                          dg           |                                 }|                     t          |                                          g            | 	                                }|                     t          |                                          ddg           d S )	Nr   r   r   r  r  r  r  r  )
r   r[   r  exit_pointsr   r   r	  r  r  r  r   s     r   test_exit_pointszTestCFGraph.test_exit_points#  s   NN002$777%%''002$777NN002r(;;;!!002r(;;;!!001#666!!00"555!!002r(;;;;;r   c                    |                                  }|                     t          |                                          d           |                     t	          |                                          g d           |                                 }|                     t          |                                          d           |                     t	          |                                          g d           |                                 }|                     t          |                                          d           |                                 }|                     t          |                                          d           | 	                                }|                     t          |                                          d           | 
                                }|                     t	          |                                          g d           |                     t	          |                                          g d           d S )Nr   r   r   r   r   r   r   r   )r   r   r   r   )r   r[   len
dead_nodesr  nodesr   r	  r  r  r   r   s     r   test_dead_nodeszTestCFGraph.test_dead_nodes3  s   NNQ\\^^,,a000		**(	* 	* 	*NNQ\\^^,,a000		**111	3 	3 	3!!Q\\^^,,a000!!Q\\^^,,a000!!Q\\^^,,a000%%''//)))	+ 	+ 	+		**(	* 	* 	* 	* 	*r   c                    |                                  }|                    d          }|                     t          |          g            |                    d          }|                     t          |          g            |                    d          }|                     t          |          ddg           |                    d          }|                     t          |          g d           |                                 }|                    d          }|                     t          |          g            |                    d          }|                     t          |          g            |                    d          }|                     t          |          ddg           |                    d	          }|                     t          |          g d
           |                    d          }|                     t          |          g            |                    d          }|                     t          |          g d           d S )Nr   r   r   r   )r   r   r   r   r   r  r  r   r$   r   r  r  r  r   )r  r$   r   r  r  )r   descendentsr[   r  r  )r]   r   r   s      r   test_descendentszTestCFGraph.test_descendentsI  s   NNMM"B'''MM"B'''MM"RH---MM"$8$8$8999!!MM"B'''MM"B'''MM"RH---MM"LLL111MM!B'''MM!$7$7$788888r   c                 "   |                                  }|                     |                                g dg df           |                                 }|                     |                                g dg df           |                                 }|                     |                                g dg df           |                                 }|                     |                                g dg dg d	g d
f           d S )Nr   )r   r   r   r   )r   r   r   r   r   r   r   r  )r   rE   r  r  r  )r   r  r$   r   r  r  )r   r$   r   r  r  r  )r   r$   r   r  r  r  )r   assertIn
topo_orderr   r  r  r   s     r   test_topo_orderzTestCFGraph.test_topo_ordera  s3   NNallnn&8	: 	: 	:NNallnn///1I1I1IJ	L 	L 	L!!allnn$}}mmm4	6 	6 	6!!allnn---/E/E/E---/E/E/EG	H 	H 	H 	H 	Hr   c                       fd}                                   |g dg df            |g dg dg df                                             |g dg dg d	g d
f           d S )Nc           
      *                        t                              |                     |                                t                              | d d d                             |                                t                              | d                    d d d         |                                t                              | d d d         d                    d d d         |           j                            |                                 t                              |                     |                                t                              | d                    d d d         |           d S )Nr   T)reverse)r.  list	topo_sortrandomshuffle)r'  expectedr   r]   s     r   check_topo_sortz3TestCFGraph.test_topo_sort.<locals>.check_topo_sortq  si   MM$q{{51122H===MM$q{{52;7788(CCCMM$q{{5${??@@2F"$ $ $MM$q{{52;{EEFFtttL"$ $ $K&&&MM$q{{51122H===MM$q{{5${??@@2F"$ $ $ $ $r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r  r$   r   r  )r   r$   r  r  )r   r$   r  r  )r   r  r$   r  )r   r  )r]   r9  r   s   ` @r   test_topo_sortzTestCFGraph.test_topo_sortp  s    
	$ 
	$ 
	$ 
	$ 
	$ 
	$ NN(((+;+;+;*=>>>((()))+;+;+;<	> 	> 	>!!(///???K	M 	M 	M 	M 	Mr   c                 
   |                      t          |          t          |                     t          |          D ]B}|                      t          ||                   t          ||                   d|           Cd S )Nzmismatch for r[   r  )r]   gotr8  r   s       r   check_dominatorszTestCFGraph.check_dominators  s    fX&6&67773KK 	: 	:DVCI..x~0F0F0F26$8: : : :	: 	:r   c                      fd}                                                                   fD ]M}|                                 |ddg            |dddg            |dddg            |dddg           N                                 }|                                 |ddg            |dddg            |dddg            |dddg            |dg d            |d	g d
           d S )Nc                 Z                         t          |                    |           d S rQ   r<  r   ldomsr]   s     r   eq_z1TestCFGraph.test_dominators_loopless.<locals>.eq_  )    VDG__a00000r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   )r   r   
dominatorsr   r]   rD  r   rC  s   `  @r   test_dominators_looplessz$TestCFGraph.test_dominators_loopless  sP   	1 	1 	1 	1 	1 	1..""D$=$=$?$?@ 	 	A<<>>DCA3KKKCQGCQGCQGNN||~~BBRBRBRBBr   c                 v   |                                  }|                                }|                     |dgddgg dg dg dg dg dg dd	           |                                 }|                                }|                     |i ddgdddgd
g ddg ddg ddg ddg ddg ddg ddg ddg ddg ddg dd g d!d"g d#d$g d%d&g d'           |                                 }|                                }|                     |dgdd(gdd
gg d)g d*g d+d,           d S )-Nr   r   )r   r   r$   )r   r   r$   r  )r   r   r$   r   )r   r   r$   r   r  )r   r   r  )r   r   r  r  r$   )r   r$   r   r   )r   r$   r   r   r   )r   r$   r   r   r   r   )r   r   r   r   r$   r   r   )r   r   r   r   r   r$   r   r  )r   r   r   r   r$   r  r   r   )r   r   r   r   r$   r   r  )r   r   r   r   r  r$   r   r   )r   r   r   r  )r   r   r  r   r  )r   r  r   r  r   r  )r   r  r  r   r   r  r  )r  r   r  r  r   r   r  r  )r   r  r  r   r   r  r  )r   r  r  r  r   r   r  r  )r   r$   r   )r   r$   r  r   )r   r$   r   r  r  )r  rF  r>  r	  r  r]   r   rC  s      r   test_dominators_loopsz!TestCFGraph.test_dominators_loops  sY   !!||~~d#$##$a&#-::#1>>#1>>#5#5#5#-::#-::! !		" 		" 		" !!||~~d!q1# ! 1a&!!:::!  ">>>!  "#5#5#5	!
  "#9#9#9!  "#=#=#=!  "#=#=#=!  "#9#9#9!  "#=#=#=!  ":::!  ">>>!  "#5#5#5!  "#9#9#9!  "#=#=#=!  "#9#9#9!   "#=#=#=!!	" 	" 	"& !!||~~d#$##$a&$%r7#.;;#2??#2??! !	" 	" 	" 	" 	"r   c                      fd}                                                                   fD ]M}|                                 |dddg            |dddg            |dddg            |ddg           N                                 }|                                 |ddg            |ddg            |ddg            |dddg            |dddg            |dddg           d S )	Nc                 Z                         t          |                    |           d S rQ   r<  rA  s     r   rD  z6TestCFGraph.test_post_dominators_loopless.<locals>.eq_  rE  r   r   r   r   r   r   r   r   )r   r   post_dominatorsr   rG  s   `  @r   test_post_dominators_looplessz)TestCFGraph.test_post_dominators_loopless  sJ   	1 	1 	1 	1 	1 	1..""D$=$=$?$?@ 	 	A$$&&DCAr7OOOCRHCRHCRDMMMMNN  ""BBBBRBRBRr   c                    |                                  }|                                }|                     |ddgdgdgdgdgddgddgdgd	           |                                 }|                                }|                     |i dg d
dg ddg ddg ddg ddg ddg ddg ddg ddg ddg ddddgddgddgddgd d!d gd!d!g           d S )"Nr   r   r$   r  r   r  r  r  r  )r   r   r  r  r   )r   r  r  r   )	r  r   r$   r   r   r   r  r   r  r   )r  r   r   r   r   r  r   r  r   )r   r  r   r   r  r   r  )r  r   r   r   r   r  r   r  r   )r   r  r   r   r   r  r   r  r  )r  r   r  r   r   r  r   r  r   )r  r   r   r  r   r  r  )r  r   r  r  r   r   )r   r  r  r  r  r  r  r  r  )r  rN  r>  r	  rJ  s      r   test_post_dominators_loopsz&TestCFGraph.test_post_dominators_loops  s   !!  ""d#$a&#$#$&4$&4$&4$&8$&8$&4! !		" 		" 		" !!  ""d!q"4"4"4 ! ///!!#F#F#F!  "#B#B#B!  "#>#>#>	!
  "#B#B#B!  "#B#B#B!  "#B#B#B!  "#:#:#:!  "#6#6#6!  "<<<!  "B8!  "B4!  "B4!  "B4!  "B8!   "B4!!	" 	" 	" 	" 	"r   c           
      .   |                                  }|                                }|                     |dgdgddgdgdgdgd           |                                 }|                                }|                     |ddgdgd	gd
gd           d S )Nr   r  r$   r   r  r  r  rE   r  r  r  )r  rN  r>  r  rJ  s      r   #test_post_dominators_infinite_loopsz/TestCFGraph.test_post_dominators_infinite_loops   s     !!  ""d#$##$#$&8$&4$&4$&4! !	" 	" 	" !!  ""d#$a&#$##$#$&4! !	" 	" 	" 	" 	"r   c                      fd} |                                  h dt                      t                      t                      d            |                                 t                      t                      ddht                      t                      h dd            |                                 h dt                      t                      t                      d            |                                 i ddhdd	d
hd
dhddhdddhddhdt                      ddhdt                      d	dhddhdddhdddhdt                      dt                      ddhdt                                  |                                 dhh dt                      ddhdht                      t                      t                      d            |                                 d	dht                      dhddht                      t                      d            |                                 dhd d!ht                      t                      d"           d S )#Nc                 \    |                                  }                    ||           d S rQ   )dominator_treer[   )graphr8  domtreer]   s      r   checkz.TestCFGraph.test_dominator_tree.<locals>.check  s0    **,,GWh/////r   >   r   r   r   r   r   r   r$  r   r   r$   r   r  r  r  r  r  r  r   r   r   r   r   r  r  >   r$   r  r  r  r  )r   r   r  r$   r   r  r  r  r  r  r  rE   r  r  r  r   setr   r   r	  r  r  r  r]   rY  s   ` r   test_dominator_treezTestCFGraph.test_dominator_tree  s   	0 	0 	0 	0 	0 	dnn,,CEEsuu#%%@@	B 	B 	Bdnn55cee"bsuu#%%<<! !	" 	" 	" 	d'')),,CEEsuu#%%@@	B 	B 	Bd!!##/q1# /q2r( /B /b2$ /RH /B4/SUU/$&/.0#%%/9;bT/B4/b"X/')B8/57/ 355/ rd/ %'/	0 	0 	0
 	d!!###,,,CEEBx4SUU355: :	; 	; 	; 	d!!##q'cee"B855 	 	 	 	d!!###2q'cee77	9 	9 	9 	9 	9r   c                 h     fd} |                                  ddddd            |                                 ddddddd            |                                 ddddd            |                                 i ddddddd	dd
d	dd
dddddd
dddddddddddddddd            |                                 ddddddddd            |                                 ddddd	d	d            |                                 ddddd           d S )Nc                 \    |                                  }                    ||           d S rQ   )immediate_dominatorsr[   )rW  r8  idomsr]   s      r   rY  z4TestCFGraph.test_immediate_dominators.<locals>.check1  s0    ..00EUH-----r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r   r   r  r   r  r   r  r  r  r  r  r  r  r  rE   r  )r   r   r   r	  r  r  r  r\  s   ` r   test_immediate_dominatorsz%TestCFGraph.test_immediate_dominators0  s-   	. 	. 	. 	. 	. 	dnnq))	+ 	+ 	+dnn22222>>	@ 	@ 	@d''))q))	+ 	+ 	+d!!##7q! 7Q 72q 7"b 7"b 7"b 72727!27')27/11768"72727!27')27/127	8 	8 	8 	d!!##qaRRRQAFF	H 	H 	Hd!!##qbbb99	; 	; 	;d!!##aQ''	) 	) 	) 	) 	)r   c                      fd} |                                  t                      dhdht                      d            |                                 dhdht                      t                      t                      t                      d            |                                 t                      dhdht                      d            |                                 i dt                      ddhddhddhd	d	dhd
d	hdd	hdd	hddhddhdt                      dt                      ddhddhdt                      dt                      dt                                  |                                 t                      dhddht                      ddhdhdht                      d            |                                 t                      t                      t                      dhdhdhd            |                                 t                      dhdhdhd           d S )Nc                 \    |                                  }                    ||           d S rQ   )dominance_frontierr[   )rW  r8  dfr]   s      r   rY  z2TestCFGraph.test_dominance_frontier.<locals>.checkG  s0    ))++BR*****r   r   r   rb  r   r   r$   r   r   r   r   r  r   r  r   r  r  r  r  r  r  r  r  r  rE   r  rZ  r\  s   ` r   test_dominance_frontierz#TestCFGraph.test_dominance_frontierF  s   	+ 	+ 	+ 	+ 	+ 	dnn%%bTt77	9 	9 	9dnn4bTsuu#%%SUUNN	P 	P 	Pd''))%%bTt77	9 	9 	9d!!##Dq#%% DQC DaS D"qc D2Aw DRD DB4DbTD#%sD,.D57D@BCEEDB4DbTD#%suuD.0#%%D9;SUUD	E 	E 	E 	d!!##%%QCb!W#%%72$CEE; ;	< 	< 	< 	d!!##%%CEEsuu2$RDrdKK	M 	M 	Md!!##%%QCQCaS11	3 	3 	3 	3 	3r   c                 `   |                                  |                                 fD ]9}|                     t          |                                          ddg           :|                                 }|                     t          |                                          ddg           d S )Nr   r   r   )r   r   r[   r  backboner   r   s     r   test_backbone_looplessz"TestCFGraph.test_backbone_loopless]  s    ..""D$=$=$?$?@ 	< 	<AVAJJLL11Ar7;;;;NN

--Bx88888r   c                    |                                  }|                     t          |                                          g d           |                                 }|                     t          |                                          dg           |                                 }|                     t          |                                          ddg           d S )N)r   r   r   r  r  r   rE   )r	  r[   r  rj  r  r  r   s     r   test_backbone_loopszTestCFGraph.test_backbone_loopsc  s    !!

--/A/A/ABBB!!

--s333!!

--1v66666r   c                 0   |                                  |                                 |                                 fD ]7}|                     t	          |                                          d           8|                                 }|                     t          |                                          g d           |                                d         }|                                d         }|                                d         }|                     |j        d           |                     t          |j	                  dg           |                     t          |j
                  dg           |                     t          |j                  g d           |                     |j        d           |                     t          |j	                  dg           |                     t          |j
                  d	g           |                     t          |j                  g d
           |                     |j        d           |                     t          |j	                  dg           |                     t          |j
                  ddg           |                     t          |j                  ddg           dD ]+}|                     |                    |          g            ,dD ],}|                     |                    |          |g           -d
D ]-}|                     |                    |          ||g           .dD ],}|                     |                    |          |g           -|                                 }|                     t          |                                          dg           |                                d         }|                     |j        d           |                     t          |j	                  dg           |                     t          |j
                  g            |                     t          |j                  g d           dD ]+}|                     |                    |          g            ,dD ],}|                     |                    |          |g           -|                                 }|                     t          |                                          dg           |                                d         }|                     |j        d           |                     t          |j	                  dg           |                     t          |j
                  g            |                     t          |j                  g d           dD ]+}|                     |                    |          g            ,dD ],}|                     |                    |          |g           -|                                 }|                     t          |                                          dg           |                                d         }|                     |j        d           |                     t          |j	                  dg           |                     t          |j
                  g d           |                     t          |j                  g d           dD ]+}|                     |                    |          g            ,dD ],}|                     |                    |          |g           -d S )Nr   )r   r   r  r   r   r  r   )	r   r$   r   r   r   r   r  r   r  r   r   )r   r   r   r  r  r  r  r  )r   r   r  r  r  r  )r   r$   r   r   r  )r  r  r$   r*  )r   r  r$   rE   )rE   r  r  r   )r  r  r  )r   r$   r   )r   r  r  r  )r   r   r   r[   r%  loopsr	  r  headerentriesexitsbodyin_loopsr  r  r  )r]   r   outer1inner1outer2r   loops          r   
test_loopszTestCFGraph.test_loopsk  s   ..""D$=$=$?$?..""$ 	0 	0AS^^Q////!!		**KKK888122***//!555--t444,,<<<	> 	> 	>+++//"666--t444,,.>.>.>???+++//"666--Bx888,,r2h777+ 	3 	3DQZZ--r2222' 	9 	9DQZZ--x8888$ 	A 	ADQZZ--/?@@@@ 	9 	9DQZZ--x8888!!		**RD111wwyy}b)))--t444
++R000	**LLL999 	3 	3DQZZ--r2222  	7 	7DQZZ--v6666!!		**QC000wwyy|a(((--s333
++R000	**JJJ777 	3 	3DQZZ--r2222 	7 	7DQZZ--v6666!!		**QC000wwyy|a(((--s333
++\\\:::	**KKK888# 	3 	3DQZZ--r2222 	7 	7DQZZ--v6666	7 	7r   c                 Z   |                      i dddhdddhdddhdddhddhdddhddd	hd	d
hdddhdddhdd
hdddhdddhdd
hdd
dhdd
dhdd
hi d
ddhddhdddhddhdddhddhdddhddhdddhddhddhdddhdd hdd hd d!d"hd!d"hd"d#d$hi d$d%d&hd%d&hd&d'd(hd'd)d(hd)t                      d(d*d+hd+d*hd*d,d-hd-d.hd.d/hd/d0d1hd1d2d3hd3d4d5hd4d2d5hd5d2hd2d/hd0d6hd#hd7hd8hd9d:hd;d<hd=d>hd;d>hd;hd8hd#ht                      d?          }|                    d           |                                 i }|                    |@          }|                     |dAdBh           |                     |dC         dD           d S )ENr   &            r   @   r   :      H   V   r  l   r   f   t   |                     i  i  i(  iF  iJ  ir  iT  iv  i|  i~  2  i  i  i  i  i6  i$  iJ  i<    iT  i`  b  i  il    iv  i|  i        0    
        )r  r  r  r  r  r  r  r  r  r  r  )stats)r  r  )r  r  iteration_count   )r   r[  r   r   _find_back_edgesr[   )r]   r   r  
back_edgess       r   test_loop_dfs_pathologicalz&TestCFGraph.test_loop_dfs_pathological  s*    ? 
Bx? 
R? 
 R? 
 R	? 

 ? 
 R? 
 R? 
 ? 
 R? 
 R? 
 ? 
 b	? 
 c
? 
 #? 
 #s? 
  #s!? 
" ##? 
 ? 
$ #s%? 
& #'? 
( #s)? 
* #+? 
, #s-? 
. #/? 
0 #s1? 
2 #3? 
4 #s5? 
6 #7? 
8 #9? 
: #s;? 
< #=? 
> #?? 
@ #sA? 
B #C? 
D #sE? 
 ? 
 ? 
F #sG? 
H #I? 
J #sK? 
L #sM? 
N O? 
P #sQ? 
R #S? 
T #sU? 
V #W? 
X #Y? 
Z #s[? 
\ #s]? 
^ #s_? 
` #sa? 
b #c? 
d #e? 
f #g? 
 ? 
h ssss}? 
 ? 
 ? 
 ? ?@ 	
!			''e'44
j*%=>>>01377777r   c                      fd} |            } |            }                      ||                                ddgdgg dgd          }|                    d           |                                                       ||            |            }|                    d           |                                                      ||                                ddgdgdgdgg d          }|                    d           |                                                      ||                                ddgdgg d	          }|                    d           |                                                      ddgdgg d	          }|                    d           |                                                      ||           d S )
Nc                                           ddgdgdgg d          } |                     d           |                                  | S )Nr   r   r   r   r   r   )r   r]   s    r   get_newz(TestCFGraph.test_equals.<locals>.get_new  sP    ""BxbTt#L#LMMAa   IIKKKHr   r   r   r   )r   r   r   r   r   r~  )r   r   r   r   r~  )r   r   r   )r[   r   r   r   assertNotEqual)r]   r  r   r   r   r2   as   `      r   test_equalszTestCFGraph.test_equals  s   	 	 	 	 	 GIIGII 	A B8"2B4HHII	!			A GII	"			Aq!!! B8"B4bT$& (  ( ) )	!			Aq!!! B8!"==>>	!			B8"2>>??	!			Aq!!!!!r   Nro  ) r   r   r   __doc__r   r   r   r   r	  r  r  r  r  r"  r(  r,  r0  r:  r>  rH  rK  rO  rQ  rS  r]  rc  rh  rk  rm  rz  r  r  r    r   r   r   r   x  s           
 
 
  &  &# # #J  4  $  O O O< < < * * *,9 9 90H H HM M M,: : :  $+" +" +"Z  $!" !" !"F" " ",9 9 94) ) ),3 3 3.9 9 97 7 7F7 F7 F7PH8 H8 H8T(" (" (" (" ("r   r   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )TestRealCodeDomFronta)  Test IDOM and DOMFRONT computation on real python bytecode.
    Note: there will be less testing on IDOM (esp in loop) because of
    the extra blocks inserted by the interpreter.  But, testing on DOMFRONT
    (which depends on IDOM) is easier.

    Testing is done by associating names to basicblock by using globals of
    the pattern "SET_BLOCK_<name>", which are scanned by
    `.get_cfa_and_namedblocks` into *namedblocks* dictionary.  That way, we
    can check that a block of a certain name is a IDOM or DOMFRONT of another
    named block.
    c                 L    t          |          }|                                 |S rQ   )r   run)r]   bccfas      r   r  zTestRealCodeDomFront.cfa4  s!    !"%%			
r   c                     t          j        |          }t          |          }|                     |          }|                     ||          }||fS )N)func_id)r   from_functionr   r  _scan_namedblocks)r]   fnfidr  r  namedblockss         r   get_cfa_and_namedblocksz,TestRealCodeDomFront.get_cfa_and_namedblocks9  sS    ,R00c"""hhrll,,R55 Kr   c                    i }t          d |                                D                       }d}|D ]}|j        dk    r|j        t	          |j                           }|                    |          ro|t          |          d         }t          ||dd         |d         dz   gz             D ]\  }	}
|	|j	        cxk    r|
k     rn  nt          d          |	}|||<   |S )zScan namedblocks as denoted by a LOAD_GLOBAL bytecode referring
        to global variables with the pattern "SET_BLOCK_<name>", where "<name>"
        would be the name for the current block.
        c                     g | ]	}|j         
S r    )offset).0r   s     r   
<listcomp>z:TestRealCodeDomFront._scan_namedblocks.<locals>.<listcomp>R  s    <<<a<<<r   
SET_BLOCK_LOAD_GLOBALNr%   r   zunreachable loop)r  
iterblocksopnameco_namesr	   arg
startswithr%  zipr  AssertionError)r]   r  r  r  blocksprefixinstgvnamesrf   blknos               r   r  z&TestRealCodeDomFront._scan_namedblocksL  s   
 <<3>>+;+;<<<== 	. 	.D{m++[!5dh!?!?@==(( 	.c&kkll+D #FF122J&*q.9I,I J J A A1////a/////!E 0 --?@@@E(-K%r   c                     d }|                      |          \  }}|j                                        }|                     ||d                             |                     ||d                             d S )Nc                 h    d}t            d}t          rt           |dz  }|dz  }t          t           |S r(   )SET_BLOCK_ASET_BLOCK_B0SET_BLOCK_B1SET_BLOCK_C)ncr   s      r   fooz+TestRealCodeDomFront.test_loop.<locals>.foof  sF    AKA QQ   KHr   AC)r  rW  rf  assertFalser]   r  r  blkptsdomfronts        r   	test_loopzTestRealCodeDomFront.test_loope  s    		 		 		 22377V
 9//11&+.///&+./////r   c                    d }|                      |          \  }}|                     |d         |d                    |j                                        }|                     ||d                             |                     ||d                             |                     |d         h||d                             |                     |d         h||d	                             d S )
Nc                     t            t          rIt           t          r-t           t
          rt           n| rt           t           t          -t           t          It           d S rQ   )r  r  r  SET_BLOCK_C0SET_BLOCK_C1SET_BLOCK_D0SET_BLOCK_D1SET_BLOCK_D2SET_BLOCK_ESET_BLOCK_FSET_BLOCK_G)r  s    r   r  z<TestRealCodeDomFront.test_loop_nested_and_break.<locals>.foo{  si    K 
"   L# %$ %$K #     
 KKKr   D0C1r  GFD1ED2)r  r[   rW  rf  r  r  s        r   test_loop_nested_and_breakz/TestRealCodeDomFront.test_loop_nested_and_breakz  s    	 	 	 22377Vvd|444 9//11&+.///&+./// 	&+(>???&+(>?????r   c                 0   d }|                      |          \  }}|j                                        }|                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d	                             |j                                        }|                     ||d                             |                     ||d                             |                     ||d	                             |                     |d         h||d                             |                     |d         h||d                             |                     |d         h||d                             |                     |d         h||d                             |                     |d	         h||d                             d S )
Nc                     d}t            | |k     r
t           d}nt          r
t           d}n	t           d}t
           | |z  dk    rt           |dz  }t           |S )Nr   r%   r1   rE   )r  SET_BLOCK_Br  r  SET_BLOCK_Dr  r  r  )r  br  s      r   r  z.TestRealCodeDomFront.test_if_else.<locals>.foo  si    AK1uu K1uzzQKHr   r  BC0r  Dr  r  r  r  rW  r`  r[   rf  r  r]   r  r  r  ra  r  s         r   test_if_elsez!TestRealCodeDomFront.test_if_else  s[   	 	 	( 22377V	..00eF3K&8999eF4L&9:::uVD\':;;;uVC['9:::eF3K&8999eF3K&8999eF3K&89999//11&+.///&+.///&+.///&+(=>>>&+(>???&+(>???&+(=>>>&+(=>>>>>r   c                 L   d }|                      |          \  }}|j                                        }|                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |                     |d         ||d                             |j                                        }|                     ||d                             |                     ||d                             |                     |d         h||d                             |                     |d         h||d	                             |                     |d         h||d                             |                     |d         h||d                             d S )
Nc                      t           r@t           t          r
t           d} n!t          r
t
           d} n	t           d} t           t           t           | S )Nr   r%   r1   )
SET_BLOCK_A0SET_BLOCK_A1r  r  r  r  SET_BLOCK_C2r  r  r  )r  s    r   r  z5TestRealCodeDomFront.test_if_else_nested.<locals>.foo  sU      
  LAA# $$KKHr   A0A1B1r  r  r  r  r  r  r  s         r   test_if_else_nestedz(TestRealCodeDomFront.test_if_else_nested  s   	 	 	$ 22377V	..00uVD\':;;;uVD\':;;;uVD\':;;;uVC['9:::uVC['9:::uVC['9:::9//11&,/000&+.///&+(>???&+(>???&+(=>>>&+(=>>>>>r   c                    d }|                      |          \  }}|j                                        }|                     d|           |                     |d         ||d                             |                     |d         ||d                             |j                                        }|                     ||d                             |                     ||d                             |                     |d         h||d                             |                     |d         h||d                             d S )Nc                  B    t            	 t          r	t           d S t           rQ   )r  r  r  r  r  r    r   r   r  z4TestRealCodeDomFront.test_infinite_loop.<locals>.foo  s(    K KF	r   r  r  r  r  r  )r  rW  r`  assertNotInr[   rf  r  r  s         r   test_infinite_loopz'TestRealCodeDomFront.test_infinite_loop  s8   	 	 	 22377V	..00 	f%%%eF3K&8999eF3K&89999//11&+.///&+.///&+(=>>>&+(=>>>>>r   N)r   r   r   r  r  r  r  r  r  r  r  r  r    r   r   r  r  (  s        
 
  
     &  20 0 0*@ @ @8(? (? (?T#? #? #?J? ? ? ? ?r   r  __main__),rX   unittestnumbar   numba.core.controlflowr   r   
numba.corer   numba.core.bytecoder   r   r	   numba.tests.supportr
   r   rp   r   r   r"   r&   r)   r+   r-   r/   r3   r5   r7   r:   r<   r>   r@   rC   rF   rH   rJ   rL   rO   rS   rU   r   r  r   mainr    r   r   <module>r     s              ? ? ? ? ? ? ? ?       P P P P P P P P P P ( ( ( ( ( ( D11                        	 	 	            
$ $ $     ]= ]= ]= ]= ]=h ]= ]= ]=@m
" m
" m
" m
" m
"( m
" m
" m
"`T? T? T? T? T?8 T? T? T?n zHMOOOOO r   