B
    u9a T                 @   s   d dl mZmZmZmZmZmZmZmZ d dl	m
Z
mZ d dlZd dlZd dlZeefZdddddd	d
ddddddg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jZG dd dZdS )    )getweakrefcountgetweakrefsrefproxyCallableProxyType	ProxyTypeReferenceType_remove_dead_weakref)WeakSet_IterationGuardNr   r   r   r   WeakKeyDictionaryr   r   r   
ProxyTypesWeakValueDictionaryr
   
WeakMethodfinalizec                   s@   e Zd ZdZdddZ fddZdd Zd	d
 Zej	Z	  Z
S )r   )	_func_ref
_meth_type_alive__weakref__Nc                s~   y|j }|j}W n( tk
r8   tdt|d Y nX  fdd}t| ||}t|||_t||_	d|_
t||S )Nz)argument should be a bound method, not {}c                s&    }|j r"d|_  d k	r" | d S )NF)r   )argself)callbackself_wr /usr/lib/python3.7/weakref.py_cb3   s
    zWeakMethod.__new__.<locals>._cbT)__self____func__AttributeError	TypeErrorformattyper   __new__r   r   r   )clsZmethr   objfuncr   r   r   )r   r   r   r"   ,   s    

zWeakMethod.__new__c                s2   t   }|  }|d ks"|d kr&d S | ||S )N)super__call__r   r   )r   r$   r%   )	__class__r   r   r'   B   s
    
zWeakMethod.__call__c             C   s:   t |tr6| jr|js| |kS t| |o4| j|jkS dS )NF)
isinstancer   r   r   __eq__r   )r   otherr   r   r   r*   I   s
    
zWeakMethod.__eq__c             C   s:   t |tr6| jr|js| |k	S t| |p4| j|jkS dS )NT)r)   r   r   r   __ne__r   )r   r+   r   r   r   r,   P   s
    
zWeakMethod.__ne__)N)__name__
__module____qualname__	__slots__r"   r'   r*   r,   r   __hash____classcell__r   r   )r(   r   r   $   s   
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eZdd Zd*ddZdd Zdd ZeZdd Zdd Zd d! Zd"d# Zd+d$d%Zd&d' Zd(d) ZdS ),r   c                 st   | st d| ^}} t| dkr0t dt|  t|tf fdd	}||_g |_t |_i  |_ |j	| | d S )NzGdescriptor '__init__' of 'WeakValueDictionary' object needs an argument   z$expected at most 1 arguments, got %dc                s4   | }|d k	r0|j r$|j| j n| | j d S )N)
_iterating_pending_removalsappendkey)wrselfrefZ_atomic_removalr   )dr   r   removem   s
    z,WeakValueDictionary.__init__.<locals>.remove)
r   lenr   r	   _remover5   setr4   dataupdate)argskwr   r;   r   )r:   r   __init__f   s    	
zWeakValueDictionary.__init__c             C   s,   | j }| j}x|r&| }t|| qW d S )N)r5   r?   popr	   )r   lr:   r7   r   r   r   _commit_removals}   s
    z$WeakValueDictionary._commit_removalsc             C   s4   | j r|   | j|  }|d kr,t|n|S d S )N)r5   rF   r?   KeyError)r   r7   or   r   r   __getitem__   s    
zWeakValueDictionary.__getitem__c             C   s   | j r|   | j|= d S )N)r5   rF   r?   )r   r7   r   r   r   __delitem__   s    zWeakValueDictionary.__delitem__c             C   s   | j r|   t| jS )N)r5   rF   r<   r?   )r   r   r   r   __len__   s    zWeakValueDictionary.__len__c             C   s<   | j r|   y| j|  }W n tk
r2   dS X |d k	S )NF)r5   rF   r?   rG   )r   r7   rH   r   r   r   __contains__   s    z WeakValueDictionary.__contains__c             C   s   d| j jt| f S )Nz<%s at %#x>)r(   r-   id)r   r   r   r   __repr__   s    zWeakValueDictionary.__repr__c             C   s&   | j r|   t|| j|| j|< d S )N)r5   rF   KeyedRefr=   r?   )r   r7   valuer   r   r   __setitem__   s    zWeakValueDictionary.__setitem__c          	   C   sZ   | j r|   t }t| 4 x,| j D ]\}}| }|d k	r*|||< q*W W d Q R X |S )N)r5   rF   r   r   r?   items)r   newr7   r8   rH   r   r   r   copy   s    
zWeakValueDictionary.copyc          	   C   sn   ddl m} | jr|   |  }t| : x2| j D ]$\}}| }|d k	r8|||||< q8W W d Q R X |S )Nr   )deepcopy)rT   rU   r5   rF   r(   r   r?   rR   )r   memorU   rS   r7   r8   rH   r   r   r   __deepcopy__   s    
z WeakValueDictionary.__deepcopy__Nc             C   sL   | j r|   y| j| }W n tk
r0   |S X | }|d krD|S |S d S )N)r5   rF   r?   rG   )r   r7   defaultr8   rH   r   r   r   get   s    zWeakValueDictionary.getc          	   c   sV   | j r|   t| 6 x.| j D ] \}}| }|d k	r$||fV  q$W W d Q R X d S )N)r5   rF   r   r?   rR   )r   kr8   vr   r   r   rR      s    
zWeakValueDictionary.itemsc          	   c   sN   | j r|   t| . x&| j D ]\}}| d k	r$|V  q$W W d Q R X d S )N)r5   rF   r   r?   rR   )r   rZ   r8   r   r   r   keys   s    

zWeakValueDictionary.keysc          	   c   s6   | j r|   t|  | j E d H  W d Q R X d S )N)r5   rF   r   r?   values)r   r   r   r   itervaluerefs   s    

z!WeakValueDictionary.itervaluerefsc          	   c   sN   | j r|   t| . x&| j D ]}| }|d k	r$|V  q$W W d Q R X d S )N)r5   rF   r   r?   r]   )r   r8   r$   r   r   r   r]      s    
zWeakValueDictionary.valuesc             C   s<   | j r|   x(| j \}}| }|d k	r||fS qW d S )N)r5   rF   r?   popitem)r   r7   r8   rH   r   r   r   r_      s    zWeakValueDictionary.popitemc             G   s`   | j r|   y| j| }W n tk
r8   d }Y nX |d krX|rN|d S t|n|S d S )Nr   )r5   rF   r?   rD   rG   )r   r7   rA   rH   r   r   r   rD     s    

zWeakValueDictionary.popc             C   s`   y| j |  }W n tk
r(   d }Y nX |d krX| jr@|   t|| j|| j |< |S |S d S )N)r?   rG   r5   rF   rO   r=   )r   r7   rX   rH   r   r   r   
setdefault  s    
zWeakValueDictionary.setdefaultc              O   s   | st d| ^}} t| dkr0t dt|  | r<| d nd }|jrN|  |j}|d k	rt|dsrti |}x&| D ]\}}t||j	|||< q|W t|r|
| d S )NzEdescriptor 'update' of 'WeakValueDictionary' object needs an argumentr3   z$expected at most 1 arguments, got %dr   rR   )r   r<   r5   rF   r?   hasattrr!   rR   rO   r=   r@   )rA   kwargsr   dictr:   r7   rH   r   r   r   r@   "  s     
zWeakValueDictionary.updatec             C   s   | j r|   t| j S )N)r5   rF   listr?   r]   )r   r   r   r   	valuerefs5  s    
zWeakValueDictionary.valuerefs)N)N)r-   r.   r/   rC   rF   rI   rJ   rK   rL   rN   rQ   rT   __copy__rW   rY   rR   r\   __iter__r^   r]   r_   rD   r`   r@   re   r   r   r   r   r   Z   s,   			
			
c                   s(   e Zd ZdZdd Z fddZ  ZS )rO   )r7   c             C   s   t | ||}||_|S )N)r   r"   r7   )r!   obr   r7   r   r   r   r   r"   P  s    zKeyedRef.__new__c                s   t  || d S )N)r&   rC   )r   rh   r   r7   )r(   r   r   rC   U  s    zKeyedRef.__init__)r-   r.   r/   r0   r"   rC   r2   r   r   )r(   r   rO   D  s   
rO   c               @   s   e Z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eZdd Zd+ddZdd Zdd Zdd ZeZdd Zd d! Zd"d# Zd$d% Zd,d&d'Zd-d(d)ZdS ).r   Nc             C   sF   i | _ t| fdd}|| _g | _t | _d| _|d k	rB| | d S )Nc             S   s.   | }|d k	r*|j r"|j|  n|j| = d S )N)r4   r5   r6   r?   )rZ   r9   r   r   r   r   r;   f  s
    z*WeakKeyDictionary.__init__.<locals>.removeF)r?   r   r=   r5   r>   r4   
_dirty_lenr@   )r   rc   r;   r   r   r   rC   d  s    zWeakKeyDictionary.__init__c             C   s>   | j }| j}x,|r8y|| = W q tk
r4   Y qX qW d S )N)r5   r?   rD   rG   )r   rE   r:   r   r   r   rF   u  s    z"WeakKeyDictionary._commit_removalsc                s&   | j   fdd| jD | _d| _d S )Nc                s   g | ]}| kr|qS r   r   ).0rZ   )r:   r   r   
<listcomp>  s    z5WeakKeyDictionary._scrub_removals.<locals>.<listcomp>F)r?   r5   ri   )r   r   )r:   r   _scrub_removals  s    z!WeakKeyDictionary._scrub_removalsc             C   s   d| _ | jt|= d S )NT)ri   r?   r   )r   r7   r   r   r   rJ     s    zWeakKeyDictionary.__delitem__c             C   s   | j t| S )N)r?   r   )r   r7   r   r   r   rI     s    zWeakKeyDictionary.__getitem__c             C   s(   | j r| jr|   t| jt| j S )N)ri   r5   rl   r<   r?   )r   r   r   r   rK     s    zWeakKeyDictionary.__len__c             C   s   d| j jt| f S )Nz<%s at %#x>)r(   r-   rM   )r   r   r   r   rN     s    zWeakKeyDictionary.__repr__c             C   s   || j t|| j< d S )N)r?   r   r=   )r   r7   rP   r   r   r   rQ     s    zWeakKeyDictionary.__setitem__c          	   C   sL   t  }t| 4 x,| j D ]\}}| }|d k	r|||< qW W d Q R X |S )N)r   r   r?   rR   )r   rS   r7   rP   rH   r   r   r   rT     s    
zWeakKeyDictionary.copyc          	   C   s`   ddl m} |  }t| : x2| j D ]$\}}| }|d k	r*|||||< q*W W d Q R X |S )Nr   )rU   )rT   rU   r(   r   r?   rR   )r   rV   rU   rS   r7   rP   rH   r   r   r   rW     s    
zWeakKeyDictionary.__deepcopy__c             C   s   | j t||S )N)r?   rY   r   )r   r7   rX   r   r   r   rY     s    zWeakKeyDictionary.getc             C   s,   yt |}W n tk
r    dS X || jkS )NF)r   r   r?   )r   r7   r8   r   r   r   rL     s
    zWeakKeyDictionary.__contains__c          	   c   sH   t | 6 x.| j D ] \}}| }|d k	r||fV  qW W d Q R X d S )N)r   r?   rR   )r   r8   rP   r7   r   r   r   rR     s
    
zWeakKeyDictionary.itemsc          	   c   s<   t | * x"| jD ]}| }|d k	r|V  qW W d Q R X d S )N)r   r?   )r   r8   r$   r   r   r   r\     s
    
zWeakKeyDictionary.keysc          	   c   s@   t | . x&| j D ]\}}| d k	r|V  qW W d Q R X d S )N)r   r?   rR   )r   r8   rP   r   r   r   r]     s    

zWeakKeyDictionary.valuesc             C   s
   t | jS )N)rd   r?   )r   r   r   r   keyrefs  s    
zWeakKeyDictionary.keyrefsc             C   s4   d| _ x(| j \}}| }|d k	r||fS qW d S )NT)ri   r?   r_   )r   r7   rP   rH   r   r   r   r_     s    zWeakKeyDictionary.popitemc             G   s   d| _ | jjt|f| S )NT)ri   r?   rD   r   )r   r7   rA   r   r   r   rD     s    zWeakKeyDictionary.popc             C   s   | j t|| j|S )N)r?   r`   r   r=   )r   r7   rX   r   r   r   r`     s    zWeakKeyDictionary.setdefaultc             K   s`   | j }|d k	rJt|ds$ti |}x$| D ]\}}||t|| j< q.W t|r\| | d S )NrR   )r?   ra   r!   rR   r   r=   r<   r@   )r   rc   rb   r:   r7   rP   r   r   r   r@     s    
zWeakKeyDictionary.update)N)N)N)N)r-   r.   r/   rC   rF   rl   rJ   rI   rK   rN   rQ   rT   rf   rW   rY   rL   rR   r\   rg   r]   rm   r_   rD   r`   r@   r   r   r   r   r   Y  s,   
	


c               @   s   e Zd ZdZi ZdZe ZdZ	dZ
G dd dZdd Zddd	Zd
d Zdd Zedd Zedd Zejdd Zdd Zedd Zedd ZdS )r   r   Fc               @   s   e Zd ZdZdS )zfinalize._Info)weakrefr%   rA   rb   atexitindexN)r-   r.   r/   r0   r   r   r   r   _Info  s   rq   c              O   s  t | dkr| ^}}}} nz| s(tdnld|krDtdt | d  |d}t | dkrf| ^}}} n.d|krtdt | d  |d}| ^}} t| } |jsdd l}||j d	t_|	 }t
|||_||_| |_|pd |_d	|_t|j|_||j|< d	t_d S )
N   z<descriptor '__init__' of 'finalize' object needs an argumentr%   z9finalize expected at least 2 positional arguments, got %dr3      r$   r   T)r<   r   rD   tuple_registered_with_atexitro   register	_exitfuncr   rq   r   rn   r%   rA   rb   next_index_iterrp   	_registry_dirty)rA   rb   r   r$   r%   ro   infor   r   r   rC     s:    




zfinalize.__init__Nc             C   s0   | j | d }|r,| js,|j|j|jp(i S d S )N)rz   rD   	_shutdownr%   rA   rb   )r   _r|   r   r   r   r'   7  s    
zfinalize.__call__c             C   sH   | j | }|o| }|d k	rD| j | d rD||j|j|jp@i fS d S )N)rz   rY   rn   rD   r%   rA   rb   )r   r|   r$   r   r   r   detach>  s    zfinalize.detachc             C   s:   | j | }|o| }|d k	r6||j|j|jp2i fS d S )N)rz   rY   rn   r%   rA   rb   )r   r|   r$   r   r   r   peekF  s    zfinalize.peekc             C   s
   | | j kS )N)rz   )r   r   r   r   aliveN  s    zfinalize.alivec             C   s   | j | }t|o|jS )N)rz   rY   boolro   )r   r|   r   r   r   ro   S  s    zfinalize.atexitc             C   s   | j | }|rt||_d S )N)rz   rY   r   ro   )r   rP   r|   r   r   r   ro   Y  s    c             C   s^   | j | }|o| }|d kr6dt| jt| f S dt| jt| t|jt|f S d S )Nz<%s object at %#x; dead>z!<%s object at %#x; for %r at %#x>)rz   rY   rn   r!   r-   rM   )r   r|   r$   r   r   r   rN   _  s    zfinalize.__repr__c             C   s2   dd | j  D }|jdd d dd |D S )Nc             S   s   g | ]\}}|j r||fqS r   )ro   )rj   fir   r   r   rk   k  s    z-finalize._select_for_exit.<locals>.<listcomp>c             S   s
   | d j S )Nr3   )rp   )itemr   r   r   <lambda>l      z+finalize._select_for_exit.<locals>.<lambda>)r7   c             S   s   g | ]\}}|qS r   r   )rj   r   r   r   r   r   rk   m  s    )rz   rR   sort)r#   Lr   r   r   _select_for_exith  s    zfinalize._select_for_exitc             C   s   d}z| j rdd l}| r(d}|  d }x\|d ks<tjrJ|  }dt_|sPP | }y
|  W q. tk
r   t	j
t	   Y q.X q.W W d dt_|r|  X d S )NFr   T)rz   gcZ	isenabledZdisabler   r{   r   rD   	Exceptionsys
excepthookexc_infor}   Zenable)r#   Zreenable_gcr   Zpendingr   r   r   r   rw   o  s.    
zfinalize._exitfunc)N)r-   r.   r/   r0   rz   r}   	itertoolscountry   r{   ru   rq   rC   r'   r   r   propertyr   ro   setterrN   classmethodr   rw   r   r   r   r   r     s"   %
	)_weakrefr   r   r   r   r   r   r   r	   Z_weakrefsetr
   r   _collections_abcr   r   r   __all__r   MutableMappingr   rO   r   r   r   r   r   r   <module>   s    (

6 k 