B
    v9a!                 @   s   d Z ddlZddlZ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	mZ ddl	mZ ddlmZ dd	 Ze ZG d
d dZdd Ze Zdd ZejejejjefZe Zdd Zdd ZdS ))	coroutineiscoroutinefunctioniscoroutine    N   )base_futures)	constants)format_helpers)loggerc               C   s"   t jjp t jj o ttjdS )NZPYTHONASYNCIODEBUG)sysflagsdev_modeignore_environmentboolosenvironget r   r    /usr/lib/python3.7/coroutines.py_is_debug_mode   s    r   c               @   s   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdddZdd Z	e
dd Ze
dd Ze
dd Zdd Ze
dd Zdd ZdS )CoroWrapperNc             C   sZ   t |st |st||| _|| _tt	d| _
t|dd | _t|dd | _d S )Nr   __name____qualname__)inspectisgeneratorr   AssertionErrorgenfuncr   extract_stackr
   	_getframe_source_tracebackgetattrr   r   )selfr   r   r   r   r   __init__&   s    zCoroWrapper.__init__c             C   sJ   t | }| jr4| jd }|d|d  d|d  7 }d| jj d| dS )	Nz, created at r   :r   < >)_format_coroutiner   	__class__r   )r!   	coro_reprframer   r   r   __repr__.   s
    
zCoroWrapper.__repr__c             C   s   | S )Nr   )r!   r   r   r   __iter__6   s    zCoroWrapper.__iter__c             C   s   | j d S )N)r   send)r!   r   r   r   __next__9   s    zCoroWrapper.__next__c             C   s   | j |S )N)r   r.   )r!   valuer   r   r   r.   <   s    zCoroWrapper.sendc             C   s   | j |||S )N)r   throw)r!   typer0   	tracebackr   r   r   r1   ?   s    zCoroWrapper.throwc             C   s
   | j  S )N)r   close)r!   r   r   r   r4   B   s    zCoroWrapper.closec             C   s   | j jS )N)r   gi_frame)r!   r   r   r   r5   E   s    zCoroWrapper.gi_framec             C   s   | j jS )N)r   
gi_running)r!   r   r   r   r6   I   s    zCoroWrapper.gi_runningc             C   s   | j jS )N)r   gi_code)r!   r   r   r   r7   M   s    zCoroWrapper.gi_codec             C   s   | S )Nr   )r!   r   r   r   	__await__Q   s    zCoroWrapper.__await__c             C   s   | j jS )N)r   gi_yieldfrom)r!   r   r   r   r9   T   s    zCoroWrapper.gi_yieldfromc             C   s   t | dd }t |dd }|d k	r||jdkr|| d}t | dd}|rrdt|}|dtj d	7 }|| 7 }t	| d S )
Nr   r5   r#   z was never yielded fromr   r    zB
Coroutine object created at (most recent call last, truncated to z last lines):
)
r    f_lastijoinr3   format_listr   ZDEBUG_STACK_DEPTHrstripr	   error)r!   r   r+   msgtbr   r   r   __del__X   s    
zCoroWrapper.__del__)N)NN)r   
__module__r   r"   r,   r-   r/   r.   r1   r4   propertyr5   r6   r7   r8   r9   rB   r   r   r   r   r   #   s   

r   c                sj   t rS t r ntfdd t  tsH }nt fdd}t|_|S )zDecorator to mark coroutines.

    If the coroutine is not yielded from before it is destroyed,
    an error message is logged.
    c              ?   sr    | |}t |s(t|s(t|tr4|E d H }n:y
|j}W n tk
rR   Y nX t|tj	j
rn| E d H }|S )N)r   Zisfuturer   r   
isinstancer   r8   AttributeErrorcollectionsabc	Awaitable)argskwZresZ
await_meth)r   r   r   corov   s    


zcoroutine.<locals>.coroc                 s@   t  | |d}|jr |jd= tdd |_tdd |_|S )N)r   r#   r   r   )r   r   r    r   r   )rJ   kwdsw)rL   r   r   r   wrapper   s    zcoroutine.<locals>.wrapper)	r   r   isgeneratorfunction	functoolswrapstypesr   _DEBUG_is_coroutine)r   rO   r   )rL   r   r   r   h   s    


r   c             C   s   t | pt| ddtkS )z6Return True if func is a decorated coroutine function.rU   N)r   r   r    rU   )r   r   r   r   r      s    
r   c             C   s@   t | tkrdS t| tr8ttdk r4tt |  dS dS dS )z)Return True if obj is a coroutine object.Td   FN)r2   _iscoroutine_typecacherE   _COROUTINE_TYPESlenadd)objr   r   r   r      s    
r   c       
         st  t | stt| t  fdd}dd }d }t| drF| jrF| j}nt| dr\| jr\| j}|| }|s~|| rz| dS |S d }t| dr| jr| j}nt| d	r| jr| j}|j	pd
}d} r0| j
d k	r0t| j
s0t| j
}|d k	r|\}}|d kr| d| d| }	n| d| d| }	n@|d k	rV|j}| d| d| }	n|j}| d| d| }	|	S )Nc                s`    rt | jdi S t| dr,| jr,| j}n*t| drD| jrD| j}ndt| j d}| dS )Nr   r   r   r%   z without __name__>z())r   Z_format_callbackr   hasattrr   r   r2   )rL   	coro_name)is_corowrapperr   r   get_name   s    z#_format_coroutine.<locals>.get_namec             S   s<   y| j S  tk
r6   y| jS  tk
r0   dS X Y nX d S )NF)
cr_runningrF   r6   )rL   r   r   r   
is_running   s    z%_format_coroutine.<locals>.is_runningcr_coder7   z runningr5   cr_framez<empty co_filename>r   z done, defined at r$   z running, defined at z running at )r   r   rE   r   r\   rb   r7   r5   rc   co_filenamer   r   rP   r   Z_get_function_sourcef_linenoco_firstlineno)
rL   r_   ra   Z	coro_coder]   Z
coro_framefilenamelinenosourcer*   r   )r^   r   r(      sH    
	



r(   )__all__Zcollections.abcrG   rQ   r   r   r
   r3   rS   r:   r   r   r   logr	   r   rT   r   r   objectrU   r   CoroutineTypeGeneratorTyperH   	CoroutinerX   setrW   r   r(   r   r   r   r   <module>   s,   E5