U
    9%e<                     @   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 )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   \/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/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   r2   )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)r0   r1   appendformat)r   eventr(   r   r   r   markO   s    
zRefRecorder.markc                 C   s   | j |}| j| d S r   )r2   popr1   r9   )r   r8   r   r   r   r   r6   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   ).0r8   r   r   r   
<listcomp>a   s     z%RefRecorder.alive.<locals>.<listcomp>)r2   r!   r   r   r   alive\   s    zRefRecorder.alivec                 C   s   | j S )z,
        A list of recorded events.
        )r1   r!   r   r   r   recordedc   s    zRefRecorder.recordedN)r#   r$   r%   __doc__r   r3   r   __call__r<   r6   propertyr@   rA   r   r   r   r   r,   7   s   	
r,   c                 C   s@   | d}| d}| d}|| }|  d || }|  d |S )Nabc--1----2--r<   )recrE   rF   rG   dr   r   r   simple_usecase1k   s    

rM   c                 C   s*   | d}| d}|  d |}|}d }|S )NrE   rF   rH   rJ   )rK   rE   rF   xyr   r   r   simple_usecase2u   s    
rP   c                 C   sN   | d}| d}| d}|}|D ]}|| }|  d q |  d || }|S )NrE   rF   rG   --loop bottom----loop exit--rJ   )rK   rE   rF   rG   rN   rO   r   r   r   looping_usecase1~   s    
rS   c                 C   s   | d}| d}| d}|D ]t}|  d || }|| }|  d |D ] }|  d || }|  d qH|  d |r|| }n q|  d	 q|  d
 |  d |S )NrE   rF   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--rJ   )rK   rE   rF   rT   rN   zrO   r   r   r   looping_usecase2   s(    






rY   c                 c   s    | d}| d}|V  |V  d S NrE   rF   r   rK   rE   rF   r   r   r   generator_usecase1   s    r\   c                 c   s*   | d}| d}|D ]
}|V  q|V  d S rZ   r   )rK   rE   rF   rN   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^   )rN   r   r   r   do_raise   s    r`   c                 C   s@   | d}| d}| d}|r4t d | d}||  ||  d S )NrE   rF   rL   foorG   r`   )rK   rE   rF   rL   rG   r   r   r   raising_usecase1   s    rc   c                 C   s0   | d}| d}|r$| d}t | ||  d S )NrE   rF   rG   rb   )rK   rE   rF   rG   r   r   r   raising_usecase2   s    rd   c                 C   s    | d}| d}|rt |d S rZ   r_   r[   r   r   r   raising_usecase3   s    re   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)rK   nijr   r   r   del_before_definition   s     


ro   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brarJ   )rK   rq   r   r   r   inf_loop_multiple_back_edge   s    

rs   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   sD   t  }| |}|d k	r8| | || W 5 Q R X n|| |S r   )r,   rw   assertRaises)r   ru   raisesrK   rv   r   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)rA   listr9   removeassertEqual)r   rK   expectedactualrA   	remainingrL   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 )NrE   rF   rH   rG   zb + crI   )rz   rM   assertFalser@   r   r   rK   r   r   r   test_simple1  s
    
zTestObjLifetime.test_simple1c                 C   s,   |  t}| |j | |dddg d S )NrF   rH   rE   )rz   rP   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 )
NrE   rF   rR   rG   	iter(a)#1rQ   	iter(a)#2	iter(a)#3iter(a))rz   rS   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 )NrE   rU   r   rV   rW   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--)rz   rY   r   r@   r   r   r   r   r   test_looping23  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genfuncrv   rK   genr   r   r   exercise_generatorI  s,    
z"TestObjLifetime.exercise_generatorc                 C   s   |  t d S r   )r   r\   r!   r   r   r   test_generator1d  s    zTestObjLifetime.test_generator1c                 C   s   |  t d S r   )r   r]   r!   r   r   r   test_generator2g  s    zTestObjLifetime.test_generator2c                 C   s"   |  t}| |jdddg d S )N012)rz   ro   r}   rA   r   r   r   r   test_del_before_definitionj  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`   rz   rc   r^   r   r@   r   r   r   r   test_raising1n  s    zTestObjLifetime.test_raising1c              	   C   s4   |  t  | jttd}| |j W 5 Q R X d S r   )r   r`   rz   rd   r^   r   r@   r   r   r   r   test_raising2s  s    zTestObjLifetime.test_raising2c              	   C   s4   |  t  | jttd}| |j W 5 Q R X d S r   )r   r^   rz   re   r   r@   r   r   r   r   test_raising3x  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 )Nrp   rq   rr   )rw   rs   r,   iterr+   r}   r@   rA   )r   rv   rK   iteratorr   r   r    test_inf_loop_multiple_back_edge}  s    
z0TestObjLifetime.test_inf_loop_multiple_back_edge)N)r#   r$   r%   rB   rw   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt      s    

rt   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   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   rE   rF   rG   rL   r   r   r   ra     s
    zOTestExtendingVariableLifetimes.test_lifetime_basic.<locals>.get_ir.<locals>.fooZEXTEND_VARIABLE_LIFETIMESr   preserved_ir)r
   r   r   	overloads
signaturesmetadata)Zextend_lifetimesr   ra   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   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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   ra     s
    zUTestExtendingVariableLifetimes.test_dbg_extend_lifetimes.<locals>.get_ir.<locals>.foor   r   )r
   r   r   r   r   )optionsr   ra   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   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__):r-   sysr4   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,   rM   rP   rS   rY   r\   r]   RuntimeErrorr^   r`   rc   rd   re   ro   rs   rt   r   r#   mainr   r   r   r   <module>   sN   4
	
 m
