U
    ,d<                     @   s  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Zd dlm	Z	 d dl
mZmZ d dlmZmZmZmZ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mZ e Zd
e_e Z d
e _!e Z"G dd de#Z$G dd de$Z%G dd de#Z&dd Z'dd Z(dd Z)dd Z*dd Z+dd Z,G dd de-Z.dd  Z/d!d" Z0d#d$ Z1d%d& Z2d'd( Z3d)d* Z4G d+d, d,eZ5G d-d. d.eeZ6e7d/kre8  dS )0    N)	takewhile)njit)CFGraphLoop)compile_extracompile_isolatedFlagsCompilerBaseDefaultPassBuilder)
PreserveIR)IRLegalization)typesir)TestCaseoverride_configSerialMixinTc                   @   s$   e Zd Zdd Zdd Zdd ZdS )_Dummyc                 C   s   || _ || _||  d S N)recordername
_add_dummy)selfr   r    r   A/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_obj_lifetime.py__init__   s    z_Dummy.__init__c                 C   s&   t |tstt| jd| j|jf S )Nz%s + %s)
isinstancer   AssertionErrorr   r   )r   otherr   r   r   __add__"   s    z_Dummy.__add__c                 C   s   t | jd| j S )Nziter(%s))_DummyIteratorr   r   r   r   r   r   __iter__&   s    z_Dummy.__iter__N)__name__
__module____qualname__r   r   r!   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdd ZeZdS )r   r   c                 C   s4   | j dkrt|  j d7  _ t| jd| j| j f S )N      z%s#%s)countStopIterationr   r   r   r    r   r   r   __next__.   s    
z_DummyIterator.__next__N)r"   r#   r$   r'   r)   nextr   r   r   r   r   *   s   r   c                   @   sT   e Zd ZdZdd Zdd Zdd ZeZdd	 Zd
d Z	e
dd Ze
dd ZdS )RefRecorderz
    An object which records events when instances created through it
    are deleted.  Custom events can also be recorded to aid in
    diagnosis.
    c                 C   s   t t| _g | _i | _d S r   )collectionsdefaultdictint_counts_events_wrsr    r   r   r   r   >   s    zRefRecorder.__init__c                 C   s
   t | |S )zK
        Make an object whose deletion will be recorded as *name*.
        )r   )r   r   r   r   r   
make_dummyC   s    zRefRecorder.make_dummyc                 C   s   t || j}|j| j|< d S r   )weakrefref_on_disposalr   r1   )r   dummywrr   r   r   r   I   s    zRefRecorder._add_dummyc                 C   s0   | j | d }|| j |< | j|j|d dS )zr
        Manually append *event* to the recorded events.
        *event* can be formatted using format().
        r&   )r'   N)r/   r0   appendformat)r   eventr'   r   r   r   markO   s    
zRefRecorder.markc                 C   s   | j |}| j| d S r   )r1   popr0   r8   )r   r7   r   r   r   r   r5   X   s    zRefRecorder._on_disposalc                 C   s   dd | j D S )zC
        A list of objects which haven't been deleted yet.
        c                 S   s   g | ]
}| qS r   r   ).0r7   r   r   r   
<listcomp>a   s     z%RefRecorder.alive.<locals>.<listcomp>)r1   r    r   r   r   alive\   s    zRefRecorder.alivec                 C   s   | j S )z,
        A list of recorded events.
        )r0   r    r   r   r   recordedc   s    zRefRecorder.recordedN)r"   r#   r$   __doc__r   r2   r   __call__r;   r5   propertyr?   r@   r   r   r   r   r+   7   s   	
r+   c                 C   s@   | d}| d}| d}|| }|  d || }|  d |S )Nabc--1----2--r;   )recrD   rE   rF   dr   r   r   simple_usecase1k   s    

rL   c                 C   s*   | d}| d}|  d |}|}d }|S )NrD   rE   rG   rI   )rJ   rD   rE   xyr   r   r   simple_usecase2u   s    
rO   c                 C   sN   | d}| d}| d}|}|D ]}|| }|  d q |  d || }|S )NrD   rE   rF   --loop bottom----loop exit--rI   )rJ   rD   rE   rF   rM   rN   r   r   r   looping_usecase1~   s    
rR   c                 C   s   | d}| d}| d}|D ]t}|  d || }|| }|  d |D ] }|  d || }|  d qH|  d |r|| }n q|  d	 q|  d
 |  d |S )NrD   rE   cum--outer loop top--z--inner loop entry #{count}--z--inner loop top #{count}--z--inner loop bottom #{count}--z--inner loop exit #{count}--z--outer loop bottom #{count}----outer loop else----outer loop exit--rI   )rJ   rD   rE   rS   rM   zrN   r   r   r   looping_usecase2   s(    






rX   c                 c   s    | d}| d}|V  |V  d S NrD   rE   r   rJ   rD   rE   r   r   r   generator_usecase1   s    r[   c                 c   s*   | d}| d}|D ]
}|V  q|V  d S rY   r   )rJ   rD   rE   rM   r   r   r   generator_usecase2   s
    r\   c                   @   s   e Zd ZdS )MyErrorN)r"   r#   r$   r   r   r   r   r]      s   r]   c                 C   s   t | d S r   r]   )rM   r   r   r   do_raise   s    r_   c                 C   s@   | d}| d}| d}|r4t d | d}||  ||  d S )NrD   rE   rK   foorF   r_   )rJ   rD   rE   rK   rF   r   r   r   raising_usecase1   s    rb   c                 C   s0   | d}| d}|r$| d}t | ||  d S )NrD   rE   rF   ra   )rJ   rD   rE   rF   r   r   r   raising_usecase2   s    rc   c                 C   s    | d}| d}|rt |d S rY   r^   rZ   r   r   r   raising_usecase3   s    rd   c                 C   s   d}t |D ]t}| t| d}t |D ]
}  dS |dk rBqn*|dkrlt |D ]}|    S | d | d | d q| d dS )zN
    This test reveal a bug that there is a del on uninitialized variable
       r      ZFAILEDOK)ranger;   str)rJ   nijr   r   r   del_before_definition   s     


rn   c                 c   s,   |  d dV  | d}|r |  d q dS )z^
    test to reveal bug of invalid liveness when infinite loop has multiple
    backedge.
    yieldNpbrarI   )rJ   rp   r   r   r   inf_loop_multiple_back_edge   s    

rr   c                   @   s   e Zd ZdZdd Zd!d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 Zdd Zdd Zdd Zdd Zdd  ZdS )"TestObjLifetimezH
    Test lifetime of Python objects inside jit-compiled functions.
    c                 C   s   t |tjftd}|jS )N)flags)r   r   Zpyobjectforceobj_flagsZentry_point)r   pyfuncZcrr   r   r   compile   s    zTestObjLifetime.compileNc              	   C   sD   t  }| |}|d k	r8| | || W 5 Q R X n|| |S r   )r+   rw   assertRaises)r   rv   raisesrJ   cfuncr   r   r   compile_and_record   s    
z"TestObjLifetime.compile_and_recordc                 C   sP   g }|j }t|}|D ] }||kr|| || q| ||d|f  dS )zk
        Check that the *expected* markers occur in that order in *rec*'s
        recorded events.
        z'the full list of recorded events is: %rN)r@   listr8   removeassertEqual)r   rJ   expectedactualr@   	remainingrK   r   r   r   assertRecordOrder  s    
z!TestObjLifetime.assertRecordOrderc                 C   sP   |  t}| |j | |dddg | |dddg | |dddg d S )NrD   rE   rG   rF   zb + crH   )r{   rL   assertFalser?   r   r   rJ   r   r   r   test_simple1  s
    
zTestObjLifetime.test_simple1c                 C   s,   |  t}| |j | |dddg d S )NrE   rG   rD   )r{   rO   r   r?   r   r   r   r   r   test_simple2   s    
zTestObjLifetime.test_simple2c                 C   sJ   |  t}| |j | |ddddg | |ddddddd	dg d S )
NrD   rE   rQ   rF   	iter(a)#1rP   	iter(a)#2	iter(a)#3iter(a))r{   rR   r   r?   r   r   r   r   r   test_looping1%  s    

   zTestObjLifetime.test_looping1c              	   C   s|   |  t}| |j | |ddg | |dddg | |dddddd	g | |d
dddddg | |ddg d S )NrD   rT   r   rU   rV   ziter(b)z--inner loop exit #1--z--inner loop exit #2--z--inner loop exit #3--r   z--inner loop entry #1--r   z--inner loop entry #2--r   z--inner loop entry #3--ziter(a)#1 + iter(a)#1z--outer loop bottom #1--)r{   rX   r   r?   r   r   r   r   r   test_looping21  s*    


  
  zTestObjLifetime.test_looping2c              	   C   s   |  |}t }| |6 ||}t| | |j t| | |j W 5 Q R X t }| |$ ||}~t	  | |j W 5 Q R X t }| |8 ||}t| | |j ~t	  | |j W 5 Q R X d S r   )
rw   r+   assertRefCountr*   
assertTruer?   r|   r   gcZcollect)r   Zgenfuncrz   rJ   genr   r   r   exercise_generatorG  s,    
z"TestObjLifetime.exercise_generatorc                 C   s   |  t d S r   )r   r[   r    r   r   r   test_generator1b  s    zTestObjLifetime.test_generator1c                 C   s   |  t d S r   )r   r\   r    r   r   r   test_generator2e  s    zTestObjLifetime.test_generator2c                 C   s"   |  t}| |jdddg d S )N012)r{   rn   r~   r@   r   r   r   r   test_del_before_definitionh  s    
z*TestObjLifetime.test_del_before_definitionc              	   C   s4   |  t  | jttd}| |j W 5 Q R X d S N)ry   )r   r_   r{   rb   r]   r   r?   r   r   r   r   test_raising1l  s    zTestObjLifetime.test_raising1c              	   C   s4   |  t  | jttd}| |j W 5 Q R X d S r   )r   r_   r{   rc   r]   r   r?   r   r   r   r   test_raising2q  s    zTestObjLifetime.test_raising2c              	   C   s4   |  t  | jttd}| |j W 5 Q R X d S r   )r   r]   r{   rd   r   r?   r   r   r   r   test_raising3v  s    zTestObjLifetime.test_raising3c              
   C   s~   |  t}t }t||}t| | |jg  t| | |jg  t| | |jg  | |jdddddddg d S )Nro   rp   rq   )rw   rr   r+   iterr*   r~   r?   r@   )r   rz   rJ   iteratorr   r   r    test_inf_loop_multiple_back_edge{  s    
z0TestObjLifetime.test_inf_loop_multiple_back_edge)N)r"   r#   r$   rA   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rs      s    

rs   c                   @   s   e Zd Zdd Zdd ZdS )TestExtendingVariableLifetimesc                    s   dd } fdd}|d}t jfd t jt jt jt jt jt jt jf}||| |d}t jfd t jft jfd t jf}||| d S )	Nc              	   S   s\   G dd dt }t|ddd }td| & |  |j|jd  }|jd }W 5 Q R X |S )	Nc                   @   s   e Zd Zdd ZdS )z`TestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_ir.<locals>.IRPreservingCompilerc                 S   s&   t | j}|tt |  |gS r   r
   Zdefine_nopython_pipelinestateZadd_pass_afterr   r   finalizer   Zpmr   r   r   define_pipelines  s    zqTestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_ir.<locals>.IRPreservingCompiler.define_pipelinesNr"   r#   r$   r   r   r   r   r   IRPreservingCompiler  s   r   )pipeline_classc                  S   s   d} d}| | }|| }|S N
      r   rD   rE   rF   rK   r   r   r   r`     s
    zOTestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_ir.<locals>.fooZEXTEND_VARIABLE_LIFETIMESr   preserved_ir)r	   r   r   	overloads
signaturesmetadata)Zextend_lifetimesr   r`   cresfunc_irr   r   r   get_ir  s    
zBTestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_irc                    sJ     t| jd tt| j }t||jD ]\}} || q0d S )Nr&   )	r~   lenblocksr*   r   valueszipbodyZassertIsInstance)r   expectblkZexpect_classZgot_stmtr    r   r   check  s    zATestExtendingVariableLifetimes.test_lifetime_basic.<locals>.checkFr%   T   )r   ZAssignDelZReturn)r   r   r   Zdel_after_use_irr   Zdel_at_block_end_irr   r    r   test_lifetime_basic  s    	  
$z2TestExtendingVariableLifetimes.test_lifetime_basicc                 C   s`   dd }|dd}|ddd}|ddd}dd }|  || |  || | || d S )	Nc                  [   sN   G dd dt }tf d|i| dd }|  |j|jd  }|jd }|S )Nc                   @   s   e Zd Zdd ZdS )zfTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_ir.<locals>.IRPreservingCompilerc                 S   s&   t | j}|tt |  |gS r   r   r   r   r   r   r     s    zwTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_ir.<locals>.IRPreservingCompiler.define_pipelinesNr   r   r   r   r   r     s   r   r   c                  S   s   d} d}| | }|| }|S r   r   r   r   r   r   r`     s
    zUTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_ir.<locals>.foor   r   )r	   r   r   r   r   )optionsr   r`   r   r   r   r   r   r     s    

zHTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_irT)debug)r   Z_dbg_extend_lifetimesFc                 S   sh   | j  \}dd |jD }tttj|}t|}tttj||d  }|t| }|t|d kS )Nc                 S   s   g | ]}t |tjqS r   )r   r   r   )r=   Zstmtr   r   r   r>     s     zoTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.is_del_grouped_at_the_end.<locals>.<listcomp>r&   )	r   r   r   r|   r   operatornot_r   truth)Zfirr   Zinst_is_delZnot_delsbeginZall_delsendr   r   r   is_del_grouped_at_the_end  s    z[TestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.is_del_grouped_at_the_end)r   r   )r   r   Zir_debugZir_debug_extZir_debug_no_extr   r   r   r   test_dbg_extend_lifetimes  s    
z8TestExtendingVariableLifetimes.test_dbg_extend_lifetimesN)r"   r#   r$   r   r   r   r   r   r   r     s   2r   __main__)9r,   sysr3   r   r   	itertoolsr   ZunittestZnumbar   Znumba.core.controlflowr   r   Znumba.core.compilerr   r   r   r	   r
   Znumba.core.untyped_passesr   Znumba.core.typed_passesr   Z
numba.corer   r   Znumba.tests.supportr   r   r   Zenable_pyobj_flagsZenable_pyobjectru   Zforce_pyobjectZno_pyobj_flagsobjectr   r   r+   rL   rO   rR   rX   r[   r\   RuntimeErrorr]   r_   rb   rc   rd   rn   rr   rs   r   r"   mainr   r   r   r   <module>   sN   4
	
 m
