B
    18\-                 @   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   \/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/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)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   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   r0   r%   )r   r   r   r   r   
trace_callB   s    zProfileSimulator.trace_callc             C   s   |  d| | j  d S )Nr   )r   r0   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   r2   r4   r6   r7   r1   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_watcherr5   pprintpformat)r   callableexpectedr   r   r   r   check_events\   s    zTestCaseBase.check_eventsN)r   r   r   r?   r   r   r   r   r8   [   s   r8   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   r:   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   rB   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   )rA   r   r   r   rB   p   s    z-ProfileHookTestCase.test_exception.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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   )rA   r   r   r   rB   x   s      z4ProfileHookTestCase.test_caught_exception.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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   )rA   r   r   r   rB      s      z;ProfileHookTestCase.test_caught_nested_exception.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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   )rA   r   r   r   rB      s    z4ProfileHookTestCase.test_nested_exception.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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   )rA   r   r   r   rB      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   )rA   )rB   r   r   g   s      z>ProfileHookTestCase.test_exception_in_except_clause.<locals>.gr    r   r'   r      )r&   r?   )r   rI   rC   g_identr   )rB   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   )rA   r   r   r   rB      s    z9ProfileHookTestCase.test_exception_propagation.<locals>.fc          	      s   z |  W d |  d X d S )Nzfalling through)r   )rA   )rB   r   r   rI      s     z9ProfileHookTestCase.test_exception_propagation.<locals>.gr    r   r'   r   zfalling through)r&   r?   )r   rI   rC   rK   r   )rB   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   )rA   r   r   r   rB      s      z/ProfileHookTestCase.test_raise_twice.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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   )rA   r   r   r   rB      s      z1ProfileHookTestCase.test_raise_reraise.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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)rA   r   r   r   rB      s    z)ProfileHookTestCase.test_raise.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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   rB      s    z5ProfileHookTestCase.test_distant_exception.<locals>.fc                  s
      d S )Nr   r   )rB   r   r   rI      s    z5ProfileHookTestCase.test_distant_exception.<locals>.gc                  s
      d S )Nr   r   )rI   r   r   h   s    z5ProfileHookTestCase.test_distant_exception.<locals>.hc                  s
      d S )Nr   r   )rR   r   r   i   s    z5ProfileHookTestCase.test_distant_exception.<locals>.ic                s
      d S )Nr   )rA   )rS   r   r   j   s    z5ProfileHookTestCase.test_distant_exception.<locals>.jr    r   r'   rJ         r   )r&   r?   )r   rT   rC   rK   h_identi_identj_identr   )rB   rI   rR   rS   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)rS   r   r   r   rB      s    z-ProfileHookTestCase.test_generator.<locals>.fc                s   x  D ]}qW d S )Nr   )rA   rS   )rB   r   r   rI      s    z-ProfileHookTestCase.test_generator.<locals>.gr    r   r'   r   )r&   r?   )r   rI   rC   rK   r   )rB   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'   )r[   )rS   r   r   r   rB     s    z2ProfileHookTestCase.test_stop_iteration.<locals>.fc                s   x  D ]}qW d S )Nr   )rA   rS   )rB   r   r   rI     s    z2ProfileHookTestCase.test_stop_iteration.<locals>.gr    r   r'   r   )r&   r?   )r   rI   rC   rK   r   )rB   r   test_stop_iteration  s    z'ProfileHookTestCase.test_stop_iterationN)r   r   r   r:   rD   rE   rF   rG   rH   rL   rM   rN   rO   rQ   rZ   r\   r]   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   r:     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   )rA   r   r   r   rB     s    z/ProfileSimulatorTestCase.test_simple.<locals>.fr    r   r   )r&   r?   )r   rB   rC   r   r   r   rD     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   )rA   r   r   r   rB   %  s    z8ProfileSimulatorTestCase.test_basic_exception.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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   )rA   r   r   r   rB   -  s      z9ProfileSimulatorTestCase.test_caught_exception.<locals>.fr    r   r   )r&   r?   )r   rB   rC   r   r   r   rF   ,  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   rB   6  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.fc                  s
      d S )Nr   r   )rB   r   r   rI   8  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.gc                  s
      d S )Nr   r   )rI   r   r   rR   :  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.hc                  s
      d S )Nr   r   )rR   r   r   rS   <  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.ic                s
      d S )Nr   )rA   )rS   r   r   rT   >  s    z:ProfileSimulatorTestCase.test_distant_exception.<locals>.jr    r   r'   rJ   rU   rV   r   )r&   r?   )r   rT   rC   rK   rW   rX   rY   r   )rB   rI   rR   rS   r   rZ   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)rA   r   r   r   rB   S  s    z?ProfileSimulatorTestCase.test_unbound_method_no_args.<locals>.fr    r   r   )r&   r?   )r   rB   rC   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`   ra   print)rA   r   r   r   rB   [  s    zDProfileSimulatorTestCase.test_unbound_method_invalid_args.<locals>.fr    r   r   )r&   r?   )r   rB   rC   r   r   r    test_unbound_method_invalid_argsZ  s    z9ProfileSimulatorTestCase.test_unbound_method_invalid_argsN)
r   r   r   r:   rD   r_   rF   rZ   rb   re   r   r   r   r   r^     s   	r^   c             C   s$   t | dr| j}n| j}|j|jfS )Nf_code)hasattrrf   __code__co_firstlinenoco_name)functioncoder   r   r   r&   b  s    
r&   c             C   s   y| | W n   Y nX d S )Nr   )rB   rA   r   r   r   protectj  s      rm   c             C   sb   |d krt  }t }t  z$t|j t| | td  W d |rPt  X |	 dd S )Nr    )
r   gc	isenableddisabler   r   r   rm   enabler-   )r=   rA   Zold_gcr   r   r   r9   q  s    

r9   c             C   s   dd l }| t|  d S )Nr   )r;   r9   )r=   r;   r   r   r   show_events  s    rs   __main__)N)ro   r;   r   unittestTestCaser   r   r.   r8   r@   r^   r&   rm   Zprotect_identr9   rs   r   mainr   r   r   r   <module>   s"    # 6J
