o
    i<                     @   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	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  Z0d!d" Z1d#d$ Z2d%d& Z3d'd( Z4d)d* Z5G d+d, d,eZ6G d-d. d.eeZ7e8d/kre9  dS dS )0    N)	takewhile)njitjit)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   l/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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   >   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 ]:}|  d || }|| }|  d |D ]}|  d || }|  d q$|  d |rA|| }n n|  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 | r   r]   )rM   r   r   r   do_raise   s   r_   c                 C   s@   | d}| d}| d}|rt 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 ]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)ranger;   str)rJ   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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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+   rv   assertRaises)r   rt   raisesrJ   ru   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@   listr8   removeassertEqual)r   rJ   expectedactualr@   	remainingrK   r   r   r   assertRecordOrder
  s   

z!TestObjLifetime.assertRecordOrderc                 C   sJ   |  t}| |j | |g d | |g d | |g d d S )N)rD   rE   rG   )rD   rF   rG   )rG   zb + crH   )ry   rL   assertFalser?   r   r   rJ   r   r   r   test_simple1  s
   
zTestObjLifetime.test_simple1c                 C   s*   |  t}| |j | |g d d S )N)rE   rG   rD   )ry   rO   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)rD   rE   rQ   rF   )	iter(a)#1rP   	iter(a)#2rP   	iter(a)#3rP   iter(a)rQ   )ry   rR   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 )NrD   rT   )r   rU   rV   )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--)ry   rX   r   r?   r   r   r   r   r   test_looping23  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   )
rv   r+   assertRefCountr*   
assertTruer?   rz   r   gcZcollect)r   Zgenfuncru   rJ   genr   r   r   exercise_generatorI  s2   
"z"TestObjLifetime.exercise_generatorc                 C      |  t d S r   )r   r[   r    r   r   r   test_generator1d     zTestObjLifetime.test_generator1c                 C   r   r   )r   r\   r    r   r   r   test_generator2g  r   zTestObjLifetime.test_generator2c                 C   s    |  t}| |jg d d S )N)012)ry   rn   r|   r@   r   r   r   r   test_del_before_definitionj  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)rx   )r   r_   ry   rb   r]   r   r?   r   r   r   r   test_raising1n     "zTestObjLifetime.test_raising1c                 C   r   r   )r   r_   ry   rc   r]   r   r?   r   r   r   r   test_raising2s  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]   ry   rd   r   r?   r   r   r   r   test_raising3x  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   )rv   rr   r+   iterr*   r|   r?   r@   )r   ru   rJ   iteratorr   r   r    test_inf_loop_multiple_back_edge}  s   
z0TestObjLifetime.test_inf_loop_multiple_back_edger   )r"   r#   r$   rA   rv   ry   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   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   rD   rE   rF   rK   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&   )	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   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   )r=   stmtr   r   r   r>     s    zoTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.is_del_grouped_at_the_end.<locals>.<listcomp>r&   )	r   r   r   rz   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,   sysr3   r   r   	itertoolsr   ZunittestZnumbar   r   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_pyobjectZforceobj_flagsZ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>   sR    4
	
 m