a �DOg8T�@s�dZddlmZmZmZmZmZmZmZm Z ddl m Z m Z ddl Z ddlZddlZeefZgd�Ze j�e �e j�e �Gdd�de�ZGdd �d e j�ZGd d �d e�ZGd d �d e j�ZGdd�d�ZdS)z|Weak reference support for Python. This module is an implementation of PEP 205: https://www.python.org/dev/peps/pep-0205/ �)�getweakrefcount� getweakrefs�ref�proxy�CallableProxyType� ProxyType� ReferenceType�_remove_dead_weakref)�WeakSet�_IterationGuardN) rrrr�WeakKeyDictionaryrrr� ProxyTypes�WeakValueDictionaryr � WeakMethod�finalizecsDeZdZdZdZd dd�Z�fdd�Zdd �Zd d �Ze j Z �Z S) rz� 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__Ncs|z|j}|j}Wn&ty6td�t|���d�Yn0��fdd�}t�|||�}t||�|_t|�|_ d|_ t|��|S)Nz)argument should be a bound method, not {}cs&��}|jr"d|_�dur"�|�dS�NF)r)�arg�self��callback�self_wr��/usr/lib64/python3.9/weakref.py�_cb6s zWeakMethod.__new__.._cbT) �__self__�__func__�AttributeError� TypeError�format�typer�__new__rrr)�cls�methr�obj�funcrrrrrr$/s   ��  zWeakMethod.__new__cs2t���}|��}|dus"|dur&dS|�||�S�N)�super�__call__rr)rr'r(�� __class__rrr+Es  zWeakMethod.__call__cCs:t|t�r6|jr|js||uSt�||�o4|j|jkStSr))� isinstancerrr�__eq__r�NotImplemented�r�otherrrrr/Ls   zWeakMethod.__eq__cCs:t|t�r6|jr|js||uSt�||�p4|j|jkStSr))r.rrr�__ne__rr0r1rrrr3Ss   zWeakMethod.__ne__)N) �__name__� __module__� __qualname__�__doc__� __slots__r$r+r/r3r�__hash__� __classcell__rrr,rr's  rc@s�eZdZdZd2dd�Zefdd�Zdd�Zd d �Zd d �Z d d�Z dd�Z dd�Z dd�Z e Zdd�Zd3dd�Zdd�Zdd�ZeZdd�Zd d!�Zd"d#�Zd$d%�Zd4d&d'�Zd5d(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�ZdS)6rz�Mapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists anymore rcKsBt|�tfdd�}||_g|_t�|_i|_|j|fi|��dS)NcSs6|�}|dur2|jr$|j�|j�n||j|j�dSr))� _iterating�_pending_removals�append�key�data)�wr�selfref�_atomic_removalrrrr�removejs z,WeakValueDictionary.__init__..remove)rr �_remover<�setr;r?�update)rr2�kwrCrrr�__init__is  zWeakValueDictionary.__init__cCs>|jj}|j}z |�}Wnty,YdS0|||�qdSr))r<�popr?� IndexError)rrBrI�dr>rrr�_commit_removalszs  z$WeakValueDictionary._commit_removalscCs4|jr|��|j|�}|dur,t|��n|SdSr)�r<rLr?�KeyError�rr>�orrr� __getitem__�s   zWeakValueDictionary.__getitem__cCs|jr|��|j|=dSr))r<rLr?�rr>rrr� __delitem__�szWeakValueDictionary.__delitem__cCs|jr|��t|j�Sr))r<rL�lenr?�rrrr�__len__�szWeakValueDictionary.__len__cCs<|jr|��z|j|�}Wnty2YdS0|duSrrMrOrrr� __contains__�s z WeakValueDictionary.__contains__cCsd|jjt|�fS�Nz <%s at %#x>�r-r4�idrUrrr�__repr__�szWeakValueDictionary.__repr__cCs&|jr|��t||j|�|j|<dSr))r<rL�KeyedRefrDr?�rr>�valuerrr� __setitem__�szWeakValueDictionary.__setitem__cCsj|jr|��t�}t|��:|j��D]\}}|�}|dur(|||<q(Wd�n1s\0Y|Sr))r<rLrr r?�items)r�newr>r@rPrrr�copy�s (zWeakValueDictionary.copycCs~ddlm}|jr|��|��}t|��@|j��D]$\}}|�}|dur6|||||�<q6Wd�n1sp0Y|S�Nr)�deepcopy)rbrdr<rLr-r r?r`)r�memordrar>r@rPrrr� __deepcopy__�s  .z WeakValueDictionary.__deepcopy__NcCsN|jr|��z|j|}Wnty2|YS0|�}|durF|S|SdSr)rM)rr>�defaultr@rPrrr�get�s  zWeakValueDictionary.getccsf|jr|��t|��<|j��D] \}}|�}|dur"||fVq"Wd�n1sX0YdSr)�r<rLr r?r`)r�kr@�vrrrr`�s zWeakValueDictionary.itemsccs^|jr|��t|��4|j��D]\}}|�dur"|Vq"Wd�n1sP0YdSr)ri)rrjr@rrr�keys�s   zWeakValueDictionary.keysccsJ|jr|��t|�� |j��EdHWd�n1s<0YdS)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<rLr r?�valuesrUrrr� itervaluerefs�s  z!WeakValueDictionary.itervaluerefsccs^|jr|��t|��4|j��D]}|�}|dur"|Vq"Wd�n1sP0YdSr)rm�rr@r'rrrrn�s zWeakValueDictionary.valuescCs8|jr|��|j��\}}|�}|dur||fSqdSr))r<rLr?�popitem)rr>r@rPrrrrq�s zWeakValueDictionary.popitemcGs^|jr|��z|j�|��}Wnty6d}Yn0|durV|rL|dSt|��n|SdS)Nr)r<rLr?rIrN)rr>�argsrPrrrrIs   zWeakValueDictionary.popcCs^z|j|�}Wnty&d}Yn0|durV|jr>|��t||j|�|j|<|S|SdSr))r?rNr<rLr\rD)rr>rgrPrrr� setdefaults  zWeakValueDictionary.setdefaultcKsz|jr|��|j}|durRt|d�s.t|�}|��D]\}}t||j|�||<q6|��D]\}}t||j|�||<qZdS�Nr`)r<rLr?�hasattr�dictr`r\rD)rr2�kwargsrKr>rPrrrrF"s zWeakValueDictionary.updatecCs|jr|��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<rL�listr?rnrUrrr� valuerefs.s zWeakValueDictionary.valuerefscCs|�|�|Sr)�rFr1rrr�__ior__<s zWeakValueDictionary.__ior__cCs&t|tj�r"|��}|�|�|StSr)�r.�_collections_abc�MappingrbrFr0�rr2�crrr�__or__@s   zWeakValueDictionary.__or__cCs0t|tj�r,|��}|�|�|�|�|StSr)�r.r}r~r-rFr0rrrr�__ror__Gs    zWeakValueDictionary.__ror__)r)N)N)N)r4r5r6r7rHr rLrQrSrVrWr[r_rb�__copy__rfrhr`rl�__iter__rornrqrIrsrFryr{r�r�rrrrr]s4          rcs,eZdZdZdZdd�Z�fdd�Z�ZS)r\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>cCst�|||�}||_|Sr))rr$r>)r#�obrr>rrrrr$\szKeyedRef.__new__cst��||�dSr))r*rH)rr�rr>r,rrrHaszKeyedRef.__init__)r4r5r6r7r8r$rHr:rrr,rr\Ps r\c@s�eZdZdZd1dd�Zdd�Zdd�Zd d �Zd d �Zd d�Z dd�Z dd�Z dd�Z e Z dd�Zd2dd�Zdd�Zdd�Zdd�ZeZdd �Zd!d"�Zd#d$�Zd%d&�Zd3d'd(�Zd4d)d*�Zd+d,�Zd-d.�Zd/d0�ZdS)5r 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. NcCsFi|_t|�fdd�}||_g|_t�|_d|_|durB|�|�dS)NcSsF|�}|durB|jr"|j�|�n z |j|=Wnty@Yn0dSr))r;r<r=r?rN)rjrArrrrrCrs  z*WeakKeyDictionary.__init__..removeF)r?rrDr<rEr;� _dirty_lenrF)rrvrCrrrrHps zWeakKeyDictionary.__init__cCsR|jj}|j}z |�}Wnty,YdS0z ||=WqtyJYq0qdSr))r<rIr?rJrN)rrIrKr>rrrrL�s    z"WeakKeyDictionary._commit_removalscs&|j��fdd�|jD�|_d|_dS)Ncsg|]}|�vr|�qSrr)�.0rj�rKrr� ��z5WeakKeyDictionary._scrub_removals..F)r?r<r�rUrr�r�_scrub_removals�sz!WeakKeyDictionary._scrub_removalscCsd|_|jt|�=dS�NT)r�r?rrRrrrrS�szWeakKeyDictionary.__delitem__cCs|jt|�Sr))r?rrRrrrrQ�szWeakKeyDictionary.__getitem__cCs(|jr|jr|��t|j�t|j�Sr))r�r<r�rTr?rUrrrrV�s zWeakKeyDictionary.__len__cCsd|jjt|�fSrXrYrUrrrr[�szWeakKeyDictionary.__repr__cCs||jt||j�<dSr))r?rrDr]rrrr_�szWeakKeyDictionary.__setitem__cCs\t�}t|��:|j��D]\}}|�}|dur|||<qWd�n1sN0Y|Sr))r r r?r`)rrar>r^rPrrrrb�s (zWeakKeyDictionary.copycCspddlm}|��}t|��@|j��D]$\}}|�}|dur(|||�||<q(Wd�n1sb0Y|Src)rbrdr-r r?r`)rrerdrar>r^rPrrrrf�s  .zWeakKeyDictionary.__deepcopy__cCs|j�t|�|�Sr))r?rhr�rr>rgrrrrh�szWeakKeyDictionary.getcCs,z t|�}Wnty YdS0||jvSr)rr!r?)rr>r@rrrrW�s   zWeakKeyDictionary.__contains__ccsXt|��<|j��D] \}}|�}|dur||fVqWd�n1sJ0YdSr)�r r?r`)rr@r^r>rrrr`�s  zWeakKeyDictionary.itemsccsLt|��0|jD]}|�}|dur|VqWd�n1s>0YdSr))r r?rprrrrl�s   zWeakKeyDictionary.keysccsPt|��4|j��D]\}}|�dur|VqWd�n1sB0YdSr)r�)rr@r^rrrrn�s  zWeakKeyDictionary.valuescCs t|j�S)azReturn 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. )rxr?rUrrr�keyrefs�s zWeakKeyDictionary.keyrefscCs0d|_|j��\}}|�}|dur||fSqdSr�)r�r?rq)rr>r^rPrrrrq�s zWeakKeyDictionary.popitemcGsd|_|jjt|�g|�R�Sr�)r�r?rIr)rr>rrrrrrI�szWeakKeyDictionary.popcCs|j�t||j�|�Sr))r?rsrrDr�rrrrs�szWeakKeyDictionary.setdefaultcKs\|j}|durFt|d�s$ti�|�}|��D]\}}||t||j�<q,t|�rX|�|�dSrt)r?rur#r`rrDrTrF)rrvrwrKr>r^rrrrF�s  zWeakKeyDictionary.updatecCs|�|�|Sr)rzr1rrrr{ s zWeakKeyDictionary.__ior__cCs&t|tj�r"|��}|�|�|StSr)r|rrrrr� s   zWeakKeyDictionary.__or__cCs0t|tj�r,|��}|�|�|�|�|StSr)r�rrrrr�s    zWeakKeyDictionary.__ror__)N)N)N)N)r4r5r6r7rHrLr�rSrQrVr[r_rbr�rfrhrWr`rlr�rnr�rqrIrsrFr{r�r�rrrrr es4      r c@s�eZdZdZdZiZdZe��Z dZ dZ Gdd�d�Z dd�Z dd d �Zd d �Zd d�Zedd��Zedd��Zejdd��Zdd�Zedd��Zedd��ZdS)raClass 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. rFc@seZdZdZdS)zfinalize._Info)�weakrefr(rrrw�atexit�indexN)r4r5r6r8rrrr�_Info6sr�cOsp|js ddl}|�|j�dt_|��}t||�|_||_||_ |pFd|_ d|_t |j �|_ ||j|<dt_dS)NrT)�_registered_with_atexitr��register� _exitfuncrr�rr�r(rrrw�next� _index_iterr�� _registry�_dirty)rr'r(rrrwr��inforrrrH9s     zfinalize.__init__NcCs4|j�|d�}|r0|js0|j|ji|jp*i��SdS)zZIf alive then mark as dead and return func(*args, **kwargs); otherwise return NoneN)r�rI� _shutdownr(rrrw)r�_r�rrrr+Js zfinalize.__call__cCsH|j�|�}|o|��}|durD|j�|d�rD||j|j|jp@ifSdS)z^If alive then mark as dead and return (obj, func, args, kwargs); otherwise return NoneN)r�rhr�rIr(rrrw�rr�r'rrr�detachQs  zfinalize.detachcCs:|j�|�}|o|��}|dur6||j|j|jp2ifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)r�rhr�r(rrrwr�rrr�peekYs  z finalize.peekcCs ||jvS)zWhether finalizer is alive)r�rUrrr�aliveaszfinalize.alivecCs|j�|�}t|�o|jS)z*Whether finalizer should be called at exit�r�rh�boolr�)rr�rrrr�fs zfinalize.atexitcCs|j�|�}|rt|�|_dSr)r�)rr^r�rrrr�ls cCs^|j�|�}|o|��}|dur6dt|�jt|�fSdt|�jt|�t|�jt|�fSdS)Nz<%s object at %#x; dead>z!<%s object at %#x; for %r at %#x>)r�rhr�r#r4rZr�rrrr[rs  �zfinalize.__repr__cCs2dd�|j��D�}|jdd�d�dd�|D�S)NcSsg|]\}}|jr||f�qSr)r��r��f�irrrr�~r�z-finalize._select_for_exit..cSs |djS)N�)r�)�itemrrr�r�z+finalize._select_for_exit..r�cSsg|] \}}|�qSrrr�rrrr��r�)r�r`�sort)r%�Lrrr�_select_for_exit{szfinalize._select_for_exitcCs�d}z�|jr�ddl}|��r(d}|��d}|dus:tjrH|��}dt_|sNq�|��}z |�Wn ty�t j t � ��Yn0||jvs,J�q,Wdt_ |r�|� �ndt_ |r�|� �0dS)NFrT)r��gc� isenabled�disablerr�r�rI� Exception�sys� excepthook�exc_infor��enable)r%� reenable_gcr��pendingr�rrrr��s2   �zfinalize._exitfunc)N)r4r5r6r7r8r�r�� itertools�countr�r�r�r�rHr+r�r��propertyr�r��setterr[� classmethodr�r�rrrrrs.      r)r7�_weakrefrrrrrrrr � _weakrefsetr r r}r�r�r �__all__�Setr�� MutableSetr�MutableMappingrr\r rrrrr�s  (   6t9