U
    cc                     @   sx   d dl Z d dlZd dlZG dd deZG dd deZG dd dejZG dd	 d	eZ	d
d Z
G dd dejZdS )    Nc                   @   s   e Zd ZdS )	SomeErrorN)__name__
__module____qualname__ r   r   ?/tmp/pip-unpacked-wheel-z1hn3a16/greenlet/tests/test_tracing.pyr      s   r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )GreenletTracerNFc                 C   s   g | _ || _d S N)actionserror_on_trace)selfr   r   r   r   __init__   s    zGreenletTracer.__init__c                 G   s   | j | | jrtd S r	   )r
   appendr   r   r   argsr   r   r   __call__   s    zGreenletTracer.__call__c                 C   s   t | | _| jS r	   )greenletsettraceoldtracer
   r   r   r   r   	__enter__   s    zGreenletTracer.__enter__c                 G   s   t | j d S r	   )r   r   r   r   r   r   r   __exit__   s    zGreenletTracer.__exit__)Fr   r   r   r   r   r   r   r   r   r   r   r   r      s
   
r   c                   @   s    e Zd ZdZdd Zdd ZdS )TestGreenletTracingz*
    Tests of ``greenlet.settrace()``
    c              	   C   s   t  }dd }dd }t 0}t  |}|  t  |}| t|j W 5 Q R X | |d||ffd||ffd||ffd||ffg d S )Nc                   S   s   d S r	   r   r   r   r   r   dummy#   s    z8TestGreenletTracing.test_greenlet_tracing.<locals>.dummyc                   S   s
   t  d S r	   )r   r   r   r   r   dummyexc%   s    z;TestGreenletTracing.test_greenlet_tracing.<locals>.dummyexcswitchthrow)r   
getcurrentr   r   assertRaisesr   assertEqual)r   mainr   r   r
   g1g2r   r   r   test_greenlet_tracing!   s    





z)TestGreenletTracing.test_greenlet_tracingc              	      st   t    fdd}t  |}|  tdd$}| t|j | t  d  W 5 Q R X | |d |ffg d S )Nc                      s       d S r	   )r   r   r!   r   r   r   7   s    zBTestGreenletTracing.test_exception_disables_tracing.<locals>.dummyT)r   r   )r   r   r   r   r   r   r    gettrace)r   r   gr
   r   r%   r   test_exception_disables_tracing5   s    

z3TestGreenletTracing.test_exception_disables_tracingN)r   r   r   __doc__r$   r(   r   r   r   r   r      s   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 ZdS )
PythonTracerNc                 C   s
   g | _ d S r	   )r
   r   r   r   r   r   G   s    zPythonTracer.__init__c                 C   s   | j ||jjf d S r	   )r
   r   f_codeco_name)r   frameeventargr   r   r   r   J   s    zPythonTracer.__call__c                 C   s   t | | _| jS r	   )sys
setprofiler   r
   r   r   r   r   r   N   s    zPythonTracer.__enter__c                 G   s   t | j d S r	   )r0   r1   r   r   r   r   r   r   R   s    zPythonTracer.__exit__r   r   r   r   r   r*   D   s
   r*   c                   C   s   dS N*   r   r   r   r   r   tpt_callbackU   s    r4   c                   @   sd   e Zd Zd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S )TestPythonTracingz
    Tests of the interaction of ``sys.settrace()``
    with greenlet facilities.

    NOTE: Most of this is probably CPython specific.
    Nc              	   C   s2   t  }t  W 5 Q R X | |dddddg d S )Nreturnr   callr4   r7   r4   r9   r   c_callr   )r*   r4   r    )r   r
   r   r   r   test_trace_events_trivialb   s    	z+TestPythonTracing.test_trace_events_trivialc              	   C   s   t  }|  W 5 Q R X |S r	   )r*   r   r   Zgletr
   r   r   r   _trace_switchu   s    zTestPythonTracing._trace_switchc                 C   s,   |  |}| |ddddddddd	g	 d S )
Nr6   )r=   r@   )r9   runr8   r:   r7   rA   )Zc_returnr@   r;   r<   )r@   r    r?   r   r   r   $_check_trace_events_func_already_setz   s    
z6TestPythonTracing._check_trace_events_func_already_setc                 C   s   dd }|  t| d S )Nc                   S   s   t  S r	   r4   r   r   r   r   rA      s    zOTestPythonTracing.test_trace_events_into_greenlet_func_already_set.<locals>.run)rC   r   r   rA   r   r   r   0test_trace_events_into_greenlet_func_already_set   s    zBTestPythonTracing.test_trace_events_into_greenlet_func_already_setc                 C   s"   G dd dt j }| |  d S )Nc                   @   s   e Zd Zdd ZdS )zQTestPythonTracing.test_trace_events_into_greenlet_subclass_already_set.<locals>.Xc                 S   s   t  S r	   rD   r   r   r   r   rA      s    zUTestPythonTracing.test_trace_events_into_greenlet_subclass_already_set.<locals>.X.runNr   r   r   rA   r   r   r   r   X   s   rH   )r   rC   r   rH   r   r   r   4test_trace_events_into_greenlet_subclass_already_set   s    zFTestPythonTracing.test_trace_events_into_greenlet_subclass_already_setc                 C   s8   |   t  |  | |jddddddddg d S )Nr6   r8   r:   rB   r;   r<   )r   r4   r   r    r
   )r   r'   tracerr   r   r   /_check_trace_events_from_greenlet_sets_profiler   s    zATestPythonTracing._check_trace_events_from_greenlet_sets_profilerc                    s(   t    fdd}| t|  d S )Nc                      s       t S r	   r   r4   r   rK   r   r   rA      s    zQTestPythonTracing.test_trace_events_from_greenlet_func_sets_profiler.<locals>.run)r*   rL   r   rE   r   rN   r   2test_trace_events_from_greenlet_func_sets_profiler   s
    zDTestPythonTracing.test_trace_events_from_greenlet_func_sets_profilerc                    s.   t   G  fdddtj}| |   d S )Nc                       s   e Zd Z fddZdS )zSTestPythonTracing.test_trace_events_from_greenlet_subclass_sets_profiler.<locals>.Xc                    s       t S r	   rM   r   rN   r   r   rA      s    zWTestPythonTracing.test_trace_events_from_greenlet_subclass_sets_profiler.<locals>.X.runNrG   r   rN   r   r   rH      s   rH   )r*   r   rL   rI   r   rN   r   6test_trace_events_from_greenlet_subclass_sets_profiler   s    zHTestPythonTracing.test_trace_events_from_greenlet_subclass_sets_profilerc                    s|   t  d  d fdd} fdd}t| t|  }| |d t  | jdddd	d
ddddg	 d S )Nc                      s       t     t  dS r2   )r   r4   r   r   r#   rK   r   r   g1_run   s
    zPTestPythonTracing.test_trace_events_multiple_greenlets_switching.<locals>.g1_runc                      s    t     t      d S r	   )r4   r   r   r   r"   rK   r   r   g2_run   s    zPTestPythonTracing.test_trace_events_multiple_greenlets_switching.<locals>.g2_runr3   r6   r8   r:   r=   rR   )r9   rT   r;   r<   r*   r   r   r    r4   r
   r   rR   rT   xr   r"   r#   rK   r   .test_trace_events_multiple_greenlets_switching   s*    

z@TestPythonTracing.test_trace_events_multiple_greenlets_switchingc                    s   t  d  d fdd} fdd}t| t|       }| |d t  | jdddd	ddd
dg d S )Nc                      s.   t  j    t     t  dS r2   )r   r   parentr   r   r4   r   rQ   r   r   rR      s    zYTestPythonTracing.test_trace_events_multiple_greenlets_switching_siblings.<locals>.g1_runc                      s.   t  j  t    t     d S r	   )r   r   r[   r   r4   r   r   rS   r   r   rT      s
    zYTestPythonTracing.test_trace_events_multiple_greenlets_switching_siblings.<locals>.g2_runr3   r6   r8   r:   rU   r;   r<   rV   rW   r   rY   r   7test_trace_events_multiple_greenlets_switching_siblings   s,    

zITestPythonTracing.test_trace_events_multiple_greenlets_switching_siblings)r   r   r   r)   ZmaxDiffr>   r@   rC   rF   rJ   rL   rO   rP   rZ   r\   r   r   r   r   r5   X   s   	
%r5   )r0   Zunittestr   	Exceptionr   objectr   ZTestCaser   r*   r4   r5   r   r   r   r   <module>   s   (