o
    8ήc<                     @   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 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   D/tmp/pip-target-vg8gfxp4/lib/python/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   >   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   r0   )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.   r/   appendformat)r   eventr&   r   r   r   markO   s   
zRefRecorder.markc                 C   s   | j |}| j| d S r   )r0   popr/   r7   )r   r6   r   r   r   r   r4   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   ).0r6   r   r   r   
<listcomp>a   s    z%RefRecorder.alive.<locals>.<listcomp>)r0   r   r   r   r   alive\   s   zRefRecorder.alivec                 C   s   | j S )z,
        A list of recorded events.
        )r/   r   r   r   r   recordedc   s   zRefRecorder.recordedN)r!   r"   r#   __doc__r   r1   r   __call__r:   r4   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:   )recrC   rD   rE   dr   r   r   simple_usecase1k   s   

rK   c                 C   s*   | d}| d}|  d |}|}d }|S )NrC   rD   rF   rH   )rI   rC   rD   xyr   r   r   simple_usecase2u   s   
rN   c                 C   sN   | d}| d}| d}|}|D ]}|| }|  d q|  d || }|S )NrC   rD   rE   --loop bottom----loop exit--rH   )rI   rC   rD   rE   rL   rM   r   r   r   looping_usecase1~   s   
rQ   c                 C   s   | d}| d}| d}|D ]:}|  d || }|| }|  d |D ]}|  d || }|  d q$|  d |rA|| }n n|  d	 q|  d
 |  d |S )NrC   rD   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--rH   )rI   rC   rD   rR   rL   zrM   r   r   r   looping_usecase2   s(   






rW   c                 c   s"    | d}| d}|V  |V  d S NrC   rD   r   rI   rC   rD   r   r   r   generator_usecase1   s
   
rZ   c                 c   s,    | d}| d}|D ]}|V  q|V  d S rX   r   )rI   rC   rD   rL   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 | r   r\   )rL   r   r   r   do_raise   s   r^   c                 C   s@   | d}| d}| d}|rt d | d}||  ||  d S )NrC   rD   rJ   foorE   r^   )rI   rC   rD   rJ   rE   r   r   r   raising_usecase1   s   ra   c                 C   s0   | d}| d}|r| d}t | ||  d S )NrC   rD   rE   r`   )rI   rC   rD   rE   r   r   r   raising_usecase2   s   rb   c                 C   s    | d}| d}|rt |d S rX   r]   rY   r   r   r   raising_usecase3   s
   rc   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      FAILEDOK)ranger:   str)rI   nijr   r   r   del_before_definition   s    


rn   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brarH   )rI   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   pyobjectforceobj_flagsentry_point)r   pyfunccrr   r   r   compile   s   zTestObjLifetime.compileNc                 C   s\   t  }| |}|d ur(| | || W d    |S 1 s!w   Y  |S || |S r   )r*   rz   assertRaises)r   rx   raisesrI   cfuncr   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?   listr7   removeassertEqual)r   rI   expectedactualr?   	remainingrJ   r   r   r   assertRecordOrder  s   

z!TestObjLifetime.assertRecordOrderc                 C   sJ   |  t}| |j | |g d | |g d | |g d d S )N)rC   rD   rF   )rC   rE   rF   )rF   zb + crG   )r~   rK   assertFalser>   r   r   rI   r   r   r   test_simple1  s
   
zTestObjLifetime.test_simple1c                 C   s*   |  t}| |j | |g d d S )N)rD   rF   rC   )r~   rN   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)rC   rD   rP   rE   )	iter(a)#1rO   	iter(a)#2rO   	iter(a)#3rO   iter(a)rP   )r~   rQ   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 )NrC   rS   )r   rT   rU   )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--)r~   rW   r   r>   r   r   r   r   r   test_looping21  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   )
rz   r*   assertRefCountr)   
assertTruer>   r   r   gccollect)r   genfuncr}   rI   genr   r   r   exercise_generatorG  s2   
"z"TestObjLifetime.exercise_generatorc                 C      |  t d S r   )r   rZ   r   r   r   r   test_generator1b     zTestObjLifetime.test_generator1c                 C   r   r   )r   r[   r   r   r   r   test_generator2e  r   zTestObjLifetime.test_generator2c                 C   s    |  t}| |jg d 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   J   |  t | jttd}| |j W d    d S 1 sw   Y  d S N)r|   )r   r^   r~   ra   r\   r   r>   r   r   r   r   test_raising1l     "zTestObjLifetime.test_raising1c                 C   r   r   )r   r^   r~   rb   r\   r   r>   r   r   r   r   test_raising2q  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   r\   r~   rc   r   r>   r   r   r   r   test_raising3v  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)ro   rp   rq   ro   rp   rq   ro   )rz   rr   r*   iterr)   r   r>   r?   )r   r}   rI   iteratorr   r   r    test_inf_loop_multiple_back_edge{  s   
z0TestObjLifetime.test_inf_loop_multiple_back_edger   )r!   r"   r#   r@   rz   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}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
   define_nopython_pipelinestate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   rC   rD   rE   rJ   r   r   r   r_     
   zOTestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_ir.<locals>.fooEXTEND_VARIABLE_LIFETIMESr   preserved_ir)r	   r   r   	overloads
signaturesmetadata)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%   )	r   lenblocksr)   r   valueszipbodyassertIsInstance)r   expectblkexpect_classgot_stmtr   r   r   check  s
   zATestExtendingVariableLifetimes.test_lifetime_basic.<locals>.checkFr$   T   )r   AssignDelReturn)r   r   r   del_after_use_irr   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   _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<   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)firr   inst_is_delnot_delsbegin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   ir_debugir_debug_ext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+   sysr2   r   r   	itertoolsr   unittestnumbar   numba.core.controlflowr   r   numba.core.compilerr   r   r   r	   r
   numba.core.untyped_passesr   numba.core.typed_passesr   
numba.corer   r   numba.tests.supportr   r   r   enable_pyobj_flagsenable_pyobjectrv   force_pyobjectno_pyobj_flagsobjectr   r   r*   rK   rN   rQ   rW   rZ   r[   RuntimeErrorr\   r^   ra   rb   rc   rn   rr   rs   r   r!   mainr   r   r   r   <module>   sR    4
	
 m