U
    adS                     @   s   d Z 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 )z{Weak reference support for Python.

This module is an implementation of PEP 205:

http://www.python.org/dev/peps/pep-0205/
    )getweakrefcountgetweakrefsrefproxyCallableProxyType	ProxyTypeReferenceType_remove_dead_weakref)WeakSet_IterationGuardNr   r   r   r   WeakKeyDictionaryr   r   r   
ProxyTypesWeakValueDictionaryr
   
WeakMethodfinalizec                       sD   e Zd 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   z
    A custom `weakref.ref` subclass which simulates a weak reference to
    a bound method, working around the lifetime problem of bound methods.
    )	_func_ref
_meth_type_alive__weakref__Nc                    s~   z|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callbackZself_wr /usr/lib/python3.8/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#   ,   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 r   )
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   r1   P   s
    
zWeakMethod.__ne__)N)__name__
__module____qualname____doc__	__slots__r#   r)   r-   r1   r   __hash____classcell__r   r   r*   r   r   $   s   
c                   @   s   e Zd 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eZdd Zd d! Zd"d# Zd$d% Zd.d&d'Zd/d(d)Zd*d+ ZdS )0r   zMapping class that references values weakly.

    Entries in the dictionary will be discarded when no strong
    reference to the value exists anymore
    r   c                K   s>   t | tfdd}|| _g | _t | _i | _| j|f| d S )Nc                 S   s6   | }|d k	r2|j r$|j| j n||j| j d S r'   )
_iterating_pending_removalsappendkeydata)wrselfrefZ_atomic_removalr   r   r   r   removeg   s
    z,WeakValueDictionary.__init__.<locals>.remove)r   r	   _remover:   setr9   r=   update)r   r/   kwr@   r   r   r   __init__f   s    	zWeakValueDictionary.__init__c                 C   s(   | j }| j}|r$| }t|| qd S r'   )r:   r=   popr	   )r   ldr<   r   r   r   _commit_removalsw   s
    z$WeakValueDictionary._commit_removalsc                 C   s4   | j r|   | j|  }|d kr,t|n|S d S r'   r:   rI   r=   KeyErrorr   r<   or   r   r   __getitem__   s    
zWeakValueDictionary.__getitem__c                 C   s   | j r|   | j|= d S r'   )r:   rI   r=   r   r<   r   r   r   __delitem__   s    zWeakValueDictionary.__delitem__c                 C   s   | j r|   t| jS r'   )r:   rI   lenr=   r   r   r   r   __len__   s    zWeakValueDictionary.__len__c                 C   s>   | j r|   z| j|  }W n tk
r4   Y dS X |d k	S r   rJ   rL   r   r   r   __contains__   s    z WeakValueDictionary.__contains__c                 C   s   d| j jt| f S Nz<%s at %#x>r+   r2   idrR   r   r   r   __repr__   s    zWeakValueDictionary.__repr__c                 C   s&   | j r|   t|| j|| j|< d S r'   )r:   rI   KeyedRefrA   r=   r   r<   valuer   r   r   __setitem__   s    zWeakValueDictionary.__setitem__c              	   C   sV   | j r|   t }t| 0 | j D ]\}}| }|d k	r(|||< q(W 5 Q R X |S r'   )r:   rI   r   r   r=   items)r   newr<   r>   rM   r   r   r   copy   s    
zWeakValueDictionary.copyc              	   C   sj   ddl m} | jr|   |  }t| 6 | j D ]$\}}| }|d k	r6|||||< q6W 5 Q R X |S Nr   )deepcopy)r_   ra   r:   rI   r+   r   r=   r]   )r   memora   r^   r<   r>   rM   r   r   r   __deepcopy__   s    
z WeakValueDictionary.__deepcopy__Nc                 C   sP   | j r|   z| j| }W n tk
r4   | Y S X | }|d krH|S |S d S r'   rJ   )r   r<   defaultr>   rM   r   r   r   get   s    
zWeakValueDictionary.getc              	   c   sR   | j r|   t| 2 | j D ] \}}| }|d k	r"||fV  q"W 5 Q R X d S r'   r:   rI   r   r=   r]   )r   kr>   vr   r   r   r]      s    
zWeakValueDictionary.itemsc              	   c   sJ   | j r|   t| * | j D ]\}}| d k	r"|V  q"W 5 Q R X d S r'   rf   )r   rg   r>   r   r   r   keys   s    

zWeakValueDictionary.keysc              	   c   s6   | j r|   t|  | j E dH  W 5 Q R X dS )a  Return an iterator that yields the weak references to the values.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the values around longer than needed.

        Nr:   rI   r   r=   valuesrR   r   r   r   itervaluerefs   s    

z!WeakValueDictionary.itervaluerefsc              	   c   sJ   | j r|   t| * | j D ]}| }|d k	r"|V  q"W 5 Q R X d S r'   rj   r   r>   r%   r   r   r   rk      s    
zWeakValueDictionary.valuesc                 C   s8   | j r|   | j \}}| }|d k	r||fS qd S r'   )r:   rI   r=   popitem)r   r<   r>   rM   r   r   r   rn      s    zWeakValueDictionary.popitemc                 G   s`   | j r|   z| j| }W n tk
r8   d }Y nX |d krX|rN|d S t|n|S d S )Nr   )r:   rI   r=   rF   rK   )r   r<   argsrM   r   r   r   rF      s    

zWeakValueDictionary.popc                 C   s`   z| j |  }W n tk
r(   d }Y nX |d krX| jr@|   t|| j|| j |< |S |S d S r'   )r=   rK   r:   rI   rY   rA   )r   r<   rd   rM   r   r   r   
setdefault  s    
zWeakValueDictionary.setdefaultc                K   sz   | j r|   | j}|d k	rRt|ds.t|}| D ]\}}t|| j|||< q6| D ]\}}t|| j|||< qZd S Nr]   )r:   rI   r=   hasattrdictr]   rY   rA   )r   r/   kwargsrH   r<   rM   r   r   r   rC     s    
zWeakValueDictionary.updatec                 C   s   | j r|   t| j S )a~  Return a list of weak references to the values.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the values around longer than needed.

        )r:   rI   listr=   rk   rR   r   r   r   	valuerefs(  s    
zWeakValueDictionary.valuerefs)r   )N)N)N)r2   r3   r4   r5   rE   rI   rN   rP   rS   rT   rX   r\   r_   __copy__rc   re   r]   ri   __iter__rl   rk   rn   rF   rp   rC   rv   r   r   r   r   r   Z   s.   
			
			

c                       s,   e Zd ZdZdZdd Z fddZ  ZS )rY   a[  Specialized reference that includes a key corresponding to the value.

    This is used in the WeakValueDictionary to avoid having to create
    a function object for each key stored in the mapping.  A shared
    callback object can use the 'key' attribute of a KeyedRef instead
    of getting a reference to the key from an enclosing scope.

    r<   c                 C   s   t | ||}||_|S r'   )r   r#   r<   )r"   obr   r<   r   r   r   r   r#   C  s    zKeyedRef.__new__c                    s   t  || d S r'   )r(   rE   )r   rz   r   r<   r*   r   r   rE   H  s    zKeyedRef.__init__)r2   r3   r4   r5   r6   r#   rE   r8   r   r   r*   r   rY   7  s   	rY   c                   @   s   e Zd 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   a   Mapping class that references keys weakly.

    Entries in the dictionary will be discarded when there is no
    longer a strong reference to the key. This can be used to
    associate additional data with an object owned by other parts of
    an application without adding attributes to those objects. This
    can be especially useful with objects that override attribute
    accesses.
    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 r'   )r9   r:   r;   r=   )rg   r?   r   r   r   r   r@   Y  s
    z*WeakKeyDictionary.__init__.<locals>.removeF)r=   r   rA   r:   rB   r9   
_dirty_lenrC   )r   rs   r@   r   r   r   rE   W  s    zWeakKeyDictionary.__init__c                 C   s:   | j }| j}|r6z|| = W q tk
r2   Y qX qd S r'   )r:   r=   rF   rK   )r   rG   rH   r   r   r   rI   h  s    z"WeakKeyDictionary._commit_removalsc                    s&   | j   fdd| jD | _d| _d S )Nc                    s   g | ]}| kr|qS r   r   ).0rg   rH   r   r   
<listcomp>w  s      z5WeakKeyDictionary._scrub_removals.<locals>.<listcomp>F)r=   r:   r{   rR   r   r}   r   _scrub_removalsu  s    z!WeakKeyDictionary._scrub_removalsc                 C   s   d| _ | jt|= d S r0   )r{   r=   r   rO   r   r   r   rP   z  s    zWeakKeyDictionary.__delitem__c                 C   s   | j t| S r'   )r=   r   rO   r   r   r   rN   ~  s    zWeakKeyDictionary.__getitem__c                 C   s(   | j r| jr|   t| jt| j S r'   )r{   r:   r   rQ   r=   rR   r   r   r   rS     s    zWeakKeyDictionary.__len__c                 C   s   d| j jt| f S rU   rV   rR   r   r   r   rX     s    zWeakKeyDictionary.__repr__c                 C   s   || j t|| j< d S r'   )r=   r   rA   rZ   r   r   r   r\     s    zWeakKeyDictionary.__setitem__c              	   C   sH   t  }t| 0 | j D ]\}}| }|d k	r|||< qW 5 Q R X |S r'   )r   r   r=   r]   )r   r^   r<   r[   rM   r   r   r   r_     s    
zWeakKeyDictionary.copyc              	   C   s\   ddl m} |  }t| 6 | j D ]$\}}| }|d k	r(|||||< q(W 5 Q R X |S r`   )r_   ra   r+   r   r=   r]   )r   rb   ra   r^   r<   r[   rM   r   r   r   rc     s    
zWeakKeyDictionary.__deepcopy__c                 C   s   | j t||S r'   )r=   re   r   r   r<   rd   r   r   r   re     s    zWeakKeyDictionary.getc                 C   s.   zt |}W n tk
r"   Y dS X || jkS r   )r   r    r=   )r   r<   r>   r   r   r   rT     s
    zWeakKeyDictionary.__contains__c              	   c   sD   t | 2 | j D ] \}}| }|d k	r||fV  qW 5 Q R X d S r'   r   r=   r]   )r   r>   r[   r<   r   r   r   r]     s
    
zWeakKeyDictionary.itemsc              	   c   s8   t | & | jD ]}| }|d k	r|V  qW 5 Q R X d S r'   )r   r=   rm   r   r   r   ri     s
    

zWeakKeyDictionary.keysc              	   c   s<   t | * | j D ]\}}| d k	r|V  qW 5 Q R X d S r'   r   )r   r>   r[   r   r   r   rk     s    

zWeakKeyDictionary.valuesc                 C   s
   t | jS )az  Return a list of weak references to the keys.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the keys around longer than needed.

        )ru   r=   rR   r   r   r   keyrefs  s    
zWeakKeyDictionary.keyrefsc                 C   s0   d| _ | j \}}| }|d k	r||fS qd S r0   )r{   r=   rn   )r   r<   r[   rM   r   r   r   rn     s
    zWeakKeyDictionary.popitemc                 G   s   d| _ | jjt|f| S r0   )r{   r=   rF   r   )r   r<   ro   r   r   r   rF     s    zWeakKeyDictionary.popc                 C   s   | j t|| j|S r'   )r=   rp   r   rA   r   r   r   r   rp     s    zWeakKeyDictionary.setdefaultc                K   s\   | j }|d k	rFt|ds$ti |}| D ]\}}||t|| j< q,t|rX| | d S rq   )r=   rr   r"   r]   r   rA   rQ   rC   )r   rs   rt   rH   r<   r[   r   r   r   rC     s    
zWeakKeyDictionary.update)N)N)N)N)r2   r3   r4   r5   rE   rI   r   rP   rN   rS   rX   r\   r_   rw   rc   re   rT   r]   ri   rx   rk   r   rn   rF   rp   rC   r   r   r   r   r   L  s.   

	


c                   @   s   e Zd ZdZdZi ZdZe Z	dZ
dZG dd dZdd Zde_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   a  Class for finalization of weakrefable objects

    finalize(obj, func, *args, **kwargs) returns a callable finalizer
    object which will be called when obj is garbage collected. The
    first time the finalizer is called it evaluates func(*arg, **kwargs)
    and returns the result. After this the finalizer is dead, and
    calling it just returns None.

    When the program exits any remaining finalizers for which the
    atexit attribute is true will be run in reverse order of creation.
    By default atexit is true.
    r   Fc                   @   s   e Zd ZdZdS )zfinalize._Info)weakrefr&   ro   rt   atexitindexN)r2   r3   r4   r6   r   r   r   r   _Info  s   r   c                  O   s>  t | dkr| ^}}}} n| s(tdnd|krDtdt | d  |d}t | dkr~| ^}}} dd l}|jdtdd	 nFd
|krtdt | d  |d
}| ^}} dd l}|jdtdd	 t| } |jsdd l}|	|j
 dt_| }t|||_||_| |_|pd |_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 %d      r   z0Passing 'func' as keyword argument is deprecated)
stacklevelr%   z/Passing 'obj' as keyword argument is deprecatedT)rQ   r    rF   warningswarnDeprecationWarningtuple_registered_with_atexitr   register	_exitfuncr   r   r   r   r&   ro   rt   next_index_iterr   	_registry_dirty)ro   rt   r   r%   r&   r   r   infor   r   r   rE     sR    



 

 
zfinalize.__init__z&($self, obj, func, /, *args, **kwargs)Nc                 C   s0   | j | d}|r,| js,|j|j|jp(i S dS )zZIf alive then mark as dead and return func(*args, **kwargs);
        otherwise return NoneN)r   rF   	_shutdownr&   ro   rt   )r   _r   r   r   r   r)   1  s    
zfinalize.__call__c                 C   sH   | j | }|o| }|dk	rD| j | drD||j|j|jp@i fS dS )z^If alive then mark as dead and return (obj, func, args, kwargs);
        otherwise return NoneN)r   re   r   rF   r&   ro   rt   r   r   r%   r   r   r   detach8  s    zfinalize.detachc                 C   s:   | j | }|o| }|dk	r6||j|j|jp2i fS dS )zMIf alive then return (obj, func, args, kwargs);
        otherwise return NoneN)r   re   r   r&   ro   rt   r   r   r   r   peek@  s    zfinalize.peekc                 C   s
   | | j kS )zWhether finalizer is alive)r   rR   r   r   r   aliveH  s    zfinalize.alivec                 C   s   | j | }t|o|jS )z*Whether finalizer should be called at exitr   re   boolr   )r   r   r   r   r   r   M  s    zfinalize.atexitc                 C   s   | j | }|rt||_d S r'   r   )r   r[   r   r   r   r   r   S  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>)r   re   r   r"   r2   rW   r   r   r   r   rX   Y  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   )r   r|   fir   r   r   r~   e  s      z-finalize._select_for_exit.<locals>.<listcomp>c                 S   s
   | d j S )Nr   )r   )itemr   r   r   <lambda>f      z+finalize._select_for_exit.<locals>.<lambda>ry   c                 S   s   g | ]\}}|qS r   r   r   r   r   r   r~   g  s     )r   r]   sort)r$   Lr   r   r   _select_for_exitb  s    zfinalize._select_for_exitc                 C   s   d}z| jrdd l}| r(d}|  d }|d ks:t jrH|  }dt _|sNq|	 }z
|  W n" t
k
r   tjt   Y nX || jks,tq,W 5 dt _|r|  X d S )NFTr   )r   r   Zenabler   gcZ	isenabledZdisabler   r   rF   	Exceptionsys
excepthookexc_infoAssertionError)r$   Zreenable_gcr   Zpendingr   r   r   r   r   i  s,    
zfinalize._exitfunc)N)r2   r3   r4   r5   r6   r   r   	itertoolscountr   r   r   r   rE   __text_signature__r)   r   r   propertyr   r   setterrX   classmethodr   r   r   r   r   r   r     s0   *



	
)r5   _weakrefr   r   r   r   r   r   r   r	   Z_weakrefsetr
   r   _collections_abcr   r   r   __all__r   MutableMappingr   rY   r   r   r   r   r   r   <module>   s0   (
      6 ^ 