o
    1&i=                     @   sj  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m	Z	 d dl
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 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  Z%d!d" Z&d#d$ Z'd%d& Z(d'd( Z)G d)d* d*eZ*G d+d, d,eeZ+e,d-kre-  dS dS ).    N)	takewhile)njitjit)CompilerBaseDefaultPassBuilder)
PreserveIR)IRLegalization)typesir)TestCaseoverride_configSerialMixinc                   @   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   HC:\wamp64\www\opt\env\Lib\site-packages\numba/tests/test_obj_lifetime.py__init__   s   z_Dummy.__init__c                 C   s&   t |tsJ t| jd| j|jf S )Nz%s + %s)
isinstancer   r   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   3   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_dummy8   s   
zRefRecorder.make_dummyc                 C   s   t || j}|j| j|< d S r   )weakrefref_on_disposalr   r,   )r   dummywrr   r   r   r   >   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*   r+   appendformat)r   eventr"   r   r   r   markD   s   
zRefRecorder.markc                 C   s   | j |}| j| d S r   )r,   popr+   r3   )r   r2   r   r   r   r   r0   M   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   ).0r2   r   r   r   
<listcomp>V   s    z%RefRecorder.alive.<locals>.<listcomp>)r,   r   r   r   r   aliveQ   s   zRefRecorder.alivec                 C   s   | j S )z,
        A list of recorded events.
        )r+   r   r   r   r   recordedX   s   zRefRecorder.recordedN)r   r   r   __doc__r   r-   r   __call__r6   r0   propertyr:   r;   r   r   r   r   r&   ,   s    	
r&   c                 C   s@   | d}| d}| d}|| }|  d || }|  d |S )Nabc--1----2--r6   )recr?   r@   rA   dr   r   r   simple_usecase1`   s   

rG   c                 C   s*   | d}| d}|  d |}|}d }|S )Nr?   r@   rB   rD   )rE   r?   r@   xyr   r   r   simple_usecase2j   s   
rJ   c                 C   sN   | d}| d}| d}|}|D ]}|| }|  d q|  d || }|S )Nr?   r@   rA   --loop bottom----loop exit--rD   )rE   r?   r@   rA   rH   rI   r   r   r   looping_usecase1s   s   
rM   c                 C   s   | d}| d}| d}|D ]:}|  d || }|| }|  d |D ]}|  d || }|  d q$|  d |rA|| }n n|  d	 q|  d
 |  d |S )Nr?   r@   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--rD   )rE   r?   r@   rN   rH   zrI   r   r   r   looping_usecase2   s(   






rS   c                 c   s"    | d}| d}|V  |V  d S Nr?   r@   r   rE   r?   r@   r   r   r   generator_usecase1   s
   
rV   c                 c   s,    | d}| d}|D ]}|V  q|V  d S rT   r   )rE   r?   r@   rH   r   r   r   generator_usecase2   s   
rW   c                   @   s   e Zd ZdS )MyErrorN)r   r   r   r   r   r   r   rX      s    rX   c                 C   s   t | r   rX   )rH   r   r   r   do_raise   s   rZ   c                 C   s@   | d}| d}| d}|rt d | d}||  ||  d S )Nr?   r@   rF   foorA   rZ   )rE   r?   r@   rF   rA   r   r   r   raising_usecase1   s   r]   c                 C   s0   | d}| d}|r| d}t | ||  d S )Nr?   r@   rA   r\   )rE   r?   r@   rA   r   r   r   raising_usecase2   s   r^   c                 C   s    | d}| d}|rt |d S rT   rY   rU   r   r   r   raising_usecase3   s
   r_   c                 C   s   d}t |D ]9}| t| d}t |D ]}  dS |dk r q|dkr5t |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)ranger6   str)rE   nijr   r   r   del_before_definition   s    


ri   c                 c   s.    	 |  d dV  | d}|r|  d 	 q)z^
    test to reveal bug of invalid liveness when infinite loop has multiple
    backedge.
    TyieldNpbrarD   )rE   rk   r   r   r   inf_loop_multiple_back_edge   s   

rm   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ddd|}|S )NTF)ZforceobjZlooplift)r   r	   Zpyobject)r   pyfunccfuncr   r   r   compile   s   zTestObjLifetime.compileNc                 C   s\   t  }| |}|d ur(| | || W d    |S 1 s!w   Y  |S || |S r   )r&   rq   assertRaises)r   ro   raisesrE   rp   r   r   r   compile_and_record   s   


z"TestObjLifetime.compile_and_recordc                 C   sP   g }|j }t|}|D ]}||v 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;   listr3   removeassertEqual)r   rE   expectedactualr;   	remainingrF   r   r   r   assertRecordOrder   s   

z!TestObjLifetime.assertRecordOrderc                 C   sJ   |  t}| |j | |g d | |g d | |g d d S )N)r?   r@   rB   )r?   rA   rB   )rB   zb + crC   )rt   rG   assertFalser:   r{   r   rE   r   r   r   test_simple1  s
   
zTestObjLifetime.test_simple1c                 C   s*   |  t}| |j | |g d d S )N)r@   rB   r?   )rt   rJ   r|   r:   r{   r}   r   r   r   test_simple2  s   
zTestObjLifetime.test_simple2c                 C   s:   |  t}| |j | |g d | |g d d S )N)r?   r@   rL   rA   )	iter(a)#1rK   	iter(a)#2rK   	iter(a)#3rK   iter(a)rL   )rt   rM   r|   r:   r{   r}   r   r   r   test_looping1  s   
zTestObjLifetime.test_looping1c                 C   sj   |  t}| |j | |ddg | |g d | |g d | |g d | |ddg d S )Nr?   rO   )r   rP   rQ   )iter(b)z--inner loop exit #1--r   z--inner loop exit #2--r   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--)rt   rS   r|   r:   r{   r}   r   r   r   test_looping2(  s   

zTestObjLifetime.test_looping2c                 C   s   |  |}t }| |  ||}t| | |j t| | |j W d    n1 s0w   Y  t }| | ||}~t	  | |j W d    n1 sWw   Y  t }| |" ||}t| | |j ~t	  | |j W d    d S 1 sw   Y  d S r   )
rq   r&   assertRefCountr%   
assertTruer:   ru   r|   gcZcollect)r   Zgenfuncrp   rE   genr   r   r   exercise_generator>  s2   
"z"TestObjLifetime.exercise_generatorc                 C      |  t d S r   )r   rV   r   r   r   r   test_generator1Y     zTestObjLifetime.test_generator1c                 C   r   r   )r   rW   r   r   r   r   test_generator2\  r   zTestObjLifetime.test_generator2c                 C   s    |  t}| |jg d d S )N)012)rt   ri   rw   r;   r}   r   r   r   test_del_before_definition_  s   
z*TestObjLifetime.test_del_before_definitionc                 C   J   |  t | jttd}| |j W d    d S 1 sw   Y  d S N)rs   )r   rZ   rt   r]   rX   r|   r:   r}   r   r   r   test_raising1c     "zTestObjLifetime.test_raising1c                 C   r   r   )r   rZ   rt   r^   rX   r|   r:   r}   r   r   r   test_raising2h  r   zTestObjLifetime.test_raising2c                 C   sJ   |  t | jttd}| |j W d    d S 1 sw   Y  d S r   )r   rX   rt   r_   r|   r:   r}   r   r   r   test_raising3m  r   zTestObjLifetime.test_raising3c                 C   st   |  t}t }t||}t| | |jg  t| | |jg  t| | |jg  | |jg d d S )N)rj   rk   rl   rj   rk   rl   rj   )rq   rm   r&   iterr%   rw   r:   r;   )r   rp   rE   iteratorr   r   r    test_inf_loop_multiple_back_edger  s   
z0TestObjLifetime.test_inf_loop_multiple_back_edger   )r   r   r   r<   rq   rt   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rn      s"    

rn   c                   @   s   e Zd Zdd Zdd ZdS )TestExtendingVariableLifetimesc                    s   dd } fdd}|d}g t jfd t jt jt jt jt jt jt j}||| |d}g t jfd t jt jfd t j}||| d S )	Nc                 S   sr   G dd dt }t|ddd }td|  |  |j|jd  }|jd }W d    |S 1 s2w   Y  |S )	Nc                   @      e Zd Zdd ZdS )z`TestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_ir.<locals>.IRPreservingCompilerc                 S   &   t | j}|tt |  |gS r   r   Zdefine_nopython_pipelinestateZadd_pass_afterr   r   finalizer   pmr   r   r   define_pipelines     zqTestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_ir.<locals>.IRPreservingCompiler.define_pipelinesNr   r   r   r   r   r   r   r   IRPreservingCompiler      r   )pipeline_classc                  S      d} d}| | }|| }|S N
      r   r?   r@   rA   rF   r   r   r   r[     
   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 ]
\}} || qd S )Nr!   )	rw   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
   AssignDel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dd|i| dd }|  |j|jd  }|jd }|S )	Nc                   @   r   )zfTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_ir.<locals>.IRPreservingCompilerc                 S   r   r   r   r   r   r   r   r     r   zwTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_ir.<locals>.IRPreservingCompiler.define_pipelinesNr   r   r   r   r   r     r   r   r   c                  S   r   r   r   r   r   r   r   r[     r   zUTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_ir.<locals>.foor   r   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   )r8   stmtr   r   r   r9     s    zoTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.is_del_grouped_at_the_end.<locals>.<listcomp>r!   )	r   r   r   ru   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__).r'   r.   r   r   	itertoolsr   ZunittestZnumbar   r   Znumba.core.compilerr   r   Znumba.core.untyped_passesr   Znumba.core.typed_passesr   Z
numba.corer	   r
   Znumba.tests.supportr   r   r   objectr   r   r&   rG   rJ   rM   rS   rV   rW   RuntimeErrorrX   rZ   r]   r^   r_   ri   rm   rn   r   r   mainr   r   r   r   <module>   sD    4
	
 m