B
    u9a-                 @   s   d dl Z d dlZd dlZd dlZG dd dejZG dd dZG dd deZG dd	 d	ejZG d
d deZ	G dd deZ
dd Zdd ZeeZdddZdd Zedkre  dS )    Nc               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestGetProfilec             C   s   t d  d S )N)sys
setprofile)self r   )/usr/lib/python3.7/test_sys_setprofile.pysetUp   s    zTestGetProfile.setUpc             C   s   t d  d S )N)r   r   )r   r   r   r   tearDown   s    zTestGetProfile.tearDownc             C   s   |  t  d S )N)ZassertIsNoner   
getprofile)r   r   r   r   
test_empty   s    zTestGetProfile.test_emptyc             C   s&   dd }t | | t  | d S )Nc              W   s   d S )Nr   )argsr   r   r   fn   s    z&TestGetProfile.test_setget.<locals>.fn)r   r   ZassertIsr
   )r   r   r   r   r   test_setget   s    
zTestGetProfile.test_setgetN)__name__
__module____qualname__r   r	   r   r   r   r   r   r   r      s   r   c               @   s.   e Zd Zdd Zdd Zd
ddZdd	 ZdS )HookWatcherc             C   s   g | _ g | _d S )N)framesevents)r   r   r   r   __init__   s    zHookWatcher.__init__c             C   s(   |dks|dks|dkr$|  || d S )Ncallreturn	exception)	add_event)r   frameeventargr   r   r   callback   s    zHookWatcher.callbackNc             C   sh   |dkrt d}y| j|}W n* tk
rL   t| j}| j| Y nX | j||t|f dS )zAdd an event to the log.N   )	r   	_getframer   index
ValueErrorlenappendr   ident)r   r   r   Zframenor   r   r   r   #   s    

zHookWatcher.add_eventc                s.   t | jjt t g d| _ fdd| jD S )zRemove calls to add_event().Nc                s   g | ]}|d   kr|qS )   r   ).0item)
disallowedr   r   
<listcomp>5   s    z*HookWatcher.get_events.<locals>.<listcomp>)r$   r   __func__r   r   )r   r   )r(   r   
get_events0   s    zHookWatcher.get_events)N)r   r   r   r   r   r   r+   r   r   r   r   r      s   
r   c               @   sN   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZeeeeeedZ	dS )ProfileSimulatorc             C   s   || _ g | _t|  d S )N)testcasestackr   r   )r   r-   r   r   r   r   9   s    zProfileSimulator.__init__c             C   s   | j | | | d S )N)dispatch)r   r   r   r   r   r   r   r   >   s    zProfileSimulator.callbackc             C   s   |  d| | j| d S )Nr   )r   r.   r#   )r   r   r   r   r   
trace_callB   s    zProfileSimulator.trace_callc             C   s   |  d| | j  d S )Nr   )r   r.   pop)r   r   r   r   r   trace_returnF   s    zProfileSimulator.trace_returnc             C   s   | j d d S )Nz2the profiler should never receive exception events)r-   fail)r   r   r   r   r   trace_exceptionJ   s    z ProfileSimulator.trace_exceptionc             C   s   d S )Nr   )r   r   r   r   r   
trace_passN   s    zProfileSimulator.trace_pass)r   r   r   Zc_callZc_returnZc_exceptionN)
r   r   r   r   r   r0   r2   r4   r5   r/   r   r   r   r   r,   8   s   r,   c               @   s   e Zd Zdd ZdS )TestCaseBasec             C   s8   t ||  }||kr4| dt|t|f  d S )Nz'Expected events:
%s
Received events:
%s)capture_eventsnew_watcherr3   pprintZpformat)r   callableZexpectedr   r   r   r   check_events\   s    zTestCaseBase.check_eventsN)r   r   r   r;   r   r   r   r   r6   [   s   r6   c               @   s|   e Z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d ZdS )ProfileHookTestCasec             C   s   t  S )N)r   )r   r   r   r   r8   d   s    zProfileHookTestCase.new_watcherc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   d S )Nr   )pr   r   r   fh   s    z*ProfileHookTestCase.test_simple.<locals>.fr   r   r   )r$   r;   )r   r>   f_identr   r   r   test_simpleg   s    zProfileHookTestCase.test_simplec             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   dd  d S )Nr   r   r   )r=   r   r   r   r>   p   s    z-ProfileHookTestCase.test_exception.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_exceptiono   s    z"ProfileHookTestCase.test_exceptionc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   ydd  W n   Y nX d S )Nr   r   r   )r=   r   r   r   r>   x   s      z4ProfileHookTestCase.test_caught_exception.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_caught_exceptionw   s    z)ProfileHookTestCase.test_caught_exceptionc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   ydd  W n   Y nX d S )Nr   r   r   )r=   r   r   r   r>      s      z;ProfileHookTestCase.test_caught_nested_exception.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_caught_nested_exception   s    z0ProfileHookTestCase.test_caught_nested_exceptionc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   dd  d S )Nr   r   r   )r=   r   r   r   r>      s    z4ProfileHookTestCase.test_nested_exception.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_nested_exception   s    z)ProfileHookTestCase.test_nested_exceptionc                sd   dd   fdd}t  }t |}| |dd|fdd|fdd|fd	d|fd	d|fdd|fg d S )
Nc             S   s   dd  d S )Nr   r   r   )r=   r   r   r   r>      s    z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.fc          	      s8   y |  W n&   y |  W n   Y nX Y nX d S )Nr   )r=   )r>   r   r   g   s      z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.gr   r   r%   r      )r$   r;   )r   rE   r?   g_identr   )r>   r   test_exception_in_except_clause   s    z3ProfileHookTestCase.test_exception_in_except_clausec          
      s\   dd   fdd}t  }t |}| |dd|fdd|fdd|fdd	|fdd|fg d S )
Nc             S   s   dd  d S )Nr   r   r   )r=   r   r   r   r>      s    z9ProfileHookTestCase.test_exception_propagation.<locals>.fc          	      s   z |  W d |  d X d S )Nzfalling through)r   )r=   )r>   r   r   rE      s     z9ProfileHookTestCase.test_exception_propagation.<locals>.gr   r   r%   r   zfalling through)r$   r;   )r   rE   r?   rG   r   )r>   r   test_exception_propagation   s    z.ProfileHookTestCase.test_exception_propagationc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s&   ydd  W n   dd  Y nX d S )Nr   r   r   )r=   r   r   r   r>      s      z/ProfileHookTestCase.test_raise_twice.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_raise_twice   s    z$ProfileHookTestCase.test_raise_twicec             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s    ydd  W n    Y nX d S )Nr   r   r   )r=   r   r   r   r>      s      z1ProfileHookTestCase.test_raise_reraise.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_raise_reraise   s    z&ProfileHookTestCase.test_raise_reraisec             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s
   t  d S )N)	Exception)r=   r   r   r   r>      s    z)ProfileHookTestCase.test_raise.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   
test_raise   s    zProfileHookTestCase.test_raisec                s   dd   fddfddfddfd	d
}t  }t }t }t }t |}| |dd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fg
 d S )Nc               S   s   dd  d S )Nr   r   r   r   r   r   r   r>      s    z5ProfileHookTestCase.test_distant_exception.<locals>.fc                  s
      d S )Nr   r   )r>   r   r   rE      s    z5ProfileHookTestCase.test_distant_exception.<locals>.gc                  s
      d S )Nr   r   )rE   r   r   h   s    z5ProfileHookTestCase.test_distant_exception.<locals>.hc                  s
      d S )Nr   r   )rN   r   r   i   s    z5ProfileHookTestCase.test_distant_exception.<locals>.ic                s
      d S )Nr   )r=   )rO   r   r   j   s    z5ProfileHookTestCase.test_distant_exception.<locals>.jr   r   r%   rF         r   )r$   r;   )r   rP   r?   rG   h_identi_identj_identr   )r>   rE   rN   rO   r   test_distant_exception   s(    z*ProfileHookTestCase.test_distant_exceptionc                st   dd   fdd}t  }t |}| |dd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fg d S )	Nc              s   s   xt dD ]
} | V  q
W d S )Nr%   )range)rO   r   r   r   r>      s    z-ProfileHookTestCase.test_generator.<locals>.fc                s   x  D ]}qW d S )Nr   )r=   rO   )r>   r   r   rE      s    z-ProfileHookTestCase.test_generator.<locals>.gr   r   r%   r   )r$   r;   )r   rE   r?   rG   r   )r>   r   test_generator   s    z"ProfileHookTestCase.test_generatorc                st   dd   fdd}t  }t |}| |dd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fg d S )	Nc              s   s   xt dD ]
} | V  q
W d S )Nr%   )rW   )rO   r   r   r   r>     s    z2ProfileHookTestCase.test_stop_iteration.<locals>.fc                s   x  D ]}qW d S )Nr   )r=   rO   )r>   r   r   rE     s    z2ProfileHookTestCase.test_stop_iteration.<locals>.gr   r   r%   r   )r$   r;   )r   rE   r?   rG   r   )r>   r   test_stop_iteration  s    z'ProfileHookTestCase.test_stop_iterationN)r   r   r   r8   r@   rA   rB   rC   rD   rH   rI   rJ   rK   rM   rV   rX   rY   r   r   r   r   r<   c   s   				r<   c               @   sD   e Z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 )ProfileSimulatorTestCasec             C   s   t | S )N)r,   )r   r   r   r   r8     s    z$ProfileSimulatorTestCase.new_watcherc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   d S )Nr   )r=   r   r   r   r>     s    z/ProfileSimulatorTestCase.test_simple.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   r@     s    z$ProfileSimulatorTestCase.test_simplec             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   dd  d S )Nr   r   r   )r=   r   r   r   r>   %  s    z8ProfileSimulatorTestCase.test_basic_exception.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_basic_exception$  s    z-ProfileSimulatorTestCase.test_basic_exceptionc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   ydd  W n   Y nX d S )Nr   r   r   )r=   r   r   r   r>   -  s      z9ProfileSimulatorTestCase.test_caught_exception.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   rB   ,  s    z.ProfileSimulatorTestCase.test_caught_exceptionc                s   dd   fddfddfddfd	d
}t  }t }t }t }t |}| |dd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fdd|fg
 d S )Nc               S   s   dd  d S )Nr   r   r   r   r   r   r   r>   6  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.fc                  s
      d S )Nr   r   )r>   r   r   rE   8  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.gc                  s
      d S )Nr   r   )rE   r   r   rN   :  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.hc                  s
      d S )Nr   r   )rN   r   r   rO   <  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.ic                s
      d S )Nr   )r=   )rO   r   r   rP   >  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.jr   r   r%   rF   rQ   rR   r   )r$   r;   )r   rP   r?   rG   rS   rT   rU   r   )r>   rE   rN   rO   r   rV   5  s(    z/ProfileSimulatorTestCase.test_distant_exceptionc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   t   d S )N)dictget)r=   r   r   r   r>   S  s    z?ProfileSimulatorTestCase.test_unbound_method_no_args.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r   test_unbound_method_no_argsR  s    z4ProfileSimulatorTestCase.test_unbound_method_no_argsc             C   s0   dd }t |}| |dd|fdd|fg d S )Nc             S   s   t td d S )N*   )r\   r]   print)r=   r   r   r   r>   [  s    zDProfileSimulatorTestCase.test_unbound_method_invalid_args.<locals>.fr   r   r   )r$   r;   )r   r>   r?   r   r   r    test_unbound_method_invalid_argsZ  s    z9ProfileSimulatorTestCase.test_unbound_method_invalid_argsN)
r   r   r   r8   r@   r[   rB   rV   r^   ra   r   r   r   r   rZ     s   	rZ   c             C   s$   t | dr| j}n| j}|j|jfS )Nf_code)hasattrrb   __code__co_firstlinenoco_name)Zfunctioncoder   r   r   r$   b  s    
r$   c             C   s   y| | W n   Y nX d S )Nr   )r>   r=   r   r   r   protectj  s      rh   c             C   sb   |d krt  }t }t  z$t|j t| | td  W d |rPt  X |	 dd S )Nr   )
r   gcZ	isenabledZdisabler   r   r   rh   Zenabler+   )r:   r=   Zold_gcr   r   r   r7   q  s    

r7   c             C   s   dd l }| t|  d S )Nr   )r9   r7   )r:   r9   r   r   r   show_events  s    rk   __main__)N)rj   r9   r   ZunittestZTestCaser   r   r,   r6   r<   rZ   r$   rh   Zprotect_identr7   rk   r   mainr   r   r   r   <module>   s"    # 6J
