eaiovnaovbqoebvqoeavibavo B  fT @sdZddlmZmZmZmZmZmZmZm Z ddl m Z m Z ddl Z ddlZddlZeefZddddd d d d d ddddg ZGdddeZGddde jZGdddeZGdd d e jZGdddZdS)z{Weak reference support for Python. This module is an implementation of PEP 205: http://www.python.org/dev/peps/pep-0205/ )getweakrefcount getweakrefsrefproxyCallableProxyType ProxyType ReferenceType_remove_dead_weakref)WeakSet_IterationGuardNrrrrWeakKeyDictionaryrrr ProxyTypesWeakValueDictionaryr WeakMethodfinalizecsDeZdZdZdZd ddZfddZdd 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~y|j}|j}Wn(tk r8tdt|dYnXfdd}t|||}t|||_t||_ d|_ t||S)Nz)argument should be a bound method, not {}cs&}|jr"d|_dk r"|dS)NF)r)argself)callbackself_wr,/opt/alt/python37/lib64/python3.7/weakref.py_cb3s zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsZmethrobjfuncrrr)rrrr",s   zWeakMethod.__new__cs2t}|}|dks"|dkr&dS|||S)N)super__call__rr)rr$r%) __class__rrr'Bs  zWeakMethod.__call__cCs:t|tr6|jr|js||kSt||o4|j|jkSdS)NF) isinstancerrr__eq__r)rotherrrrr*Is   zWeakMethod.__eq__cCs:t|tr6|jr|js||k St||p4|j|jkSdS)NT)r)rrr__ne__r)rr+rrrr,Ps   zWeakMethod.__ne__)N) __name__ __module__ __qualname____doc__ __slots__r"r'r*r,r__hash__ __classcell__rr)r(rr$s  c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ e Z ddZd+ddZddZddZeZddZdd Zd!d"Zd#d$Zd,d%d&Zd'd(Zd)d*ZdS)-rzMapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists anymore cOsl|s td|^}}t|dkr0tdt|t|tfdd}||_g|_t|_i|_|j ||dS)NzGdescriptor '__init__' of 'WeakValueDictionary' object needs an argumentz$expected at most 1 arguments, got %dcSs6|}|dk r2|jr$|j|jn||j|jdS)N) _iterating_pending_removalsappendkeydata)wrselfrefZ_atomic_removalrrrrremovems z,WeakValueDictionary.__init__..remove) rlenrr _remover6setr5r9update)argskwrr<rrr__init__fs  zWeakValueDictionary.__init__cCs,|j}|j}x|r&|}t||qWdS)N)r6r9popr )rldr8rrr_commit_removals}s z$WeakValueDictionary._commit_removalscCs4|jr||j|}|dkr,t|n|SdS)N)r6rGr9KeyError)rr8orrr __getitem__s   zWeakValueDictionary.__getitem__cCs|jr||j|=dS)N)r6rGr9)rr8rrr __delitem__szWeakValueDictionary.__delitem__cCs|jr|t|jS)N)r6rGr=r9)rrrr__len__szWeakValueDictionary.__len__cCs<|jr|y|j|}Wntk r2dSX|dk S)NF)r6rGr9rH)rr8rIrrr __contains__sz WeakValueDictionary.__contains__cCsd|jjt|fS)Nz <%s at %#x>)r(r-id)rrrr__repr__szWeakValueDictionary.__repr__cCs&|jr|t||j||j|<dS)N)r6rGKeyedRefr>r9)rr8valuerrr __setitem__szWeakValueDictionary.__setitem__c CsZ|jr|t}t|4x,|jD]\}}|}|dk r*|||<q*WWdQRX|S)N)r6rGrr r9items)rnewr8r:rIrrrcopys zWeakValueDictionary.copyc Csnddlm}|jr||}t|:x2|jD]$\}}|}|dk r8|||||<q8WWdQRX|S)Nr)deepcopy)rUrVr6rGr(r r9rS)rmemorVrTr8r:rIrrr __deepcopy__s  z WeakValueDictionary.__deepcopy__NcCsL|jr|y|j|}Wntk r0|SX|}|dkrD|S|SdS)N)r6rGr9rH)rr8defaultr:rIrrrgetszWeakValueDictionary.getc csV|jr|t|6x.|jD] \}}|}|dk r$||fVq$WWdQRXdS)N)r6rGr r9rS)rkr:vrrrrSs zWeakValueDictionary.itemsc csN|jr|t|.x&|jD]\}}|dk r$|Vq$WWdQRXdS)N)r6rGr r9rS)rr[r:rrrkeyss   zWeakValueDictionary.keysc cs6|jr|t||jEdHWdQRXdS)aReturn 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)r6rGr r9values)rrrr itervaluerefss  z!WeakValueDictionary.itervaluerefsc csN|jr|t|.x&|jD]}|}|dk r$|Vq$WWdQRXdS)N)r6rGr r9r^)rr:r$rrrr^s zWeakValueDictionary.valuescCs<|jr|x(|j\}}|}|dk r||fSqWdS)N)r6rGr9popitem)rr8r:rIrrrr`szWeakValueDictionary.popitemcGs`|jr|y|j|}Wntk r8d}YnX|dkrX|rN|dSt|n|SdS)Nr)r6rGr9rDrH)rr8rArIrrrrDs  zWeakValueDictionary.popcCs`y|j|}Wntk r(d}YnX|dkrX|jr@|t||j||j|<|S|SdS)N)r9rHr6rGrPr>)rr8rYrIrrr setdefaults zWeakValueDictionary.setdefaultcOs|s td|^}}t|dkr0tdt||r<|dnd}|jrN||j}|dk rt|dsrti|}x&|D]\}}t||j |||<q|Wt|r| |dS)NzEdescriptor 'update' of 'WeakValueDictionary' object needs an argumentr4z$expected at most 1 arguments, got %drrS) rr=r6rGr9hasattrr!rSrPr>r@)rAkwargsrdictrFr8rIrrrr@"s    zWeakValueDictionary.updatecCs|jr|t|jS)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. )r6rGlistr9r^)rrrr valuerefs5s zWeakValueDictionary.valuerefs)N)N)r-r.r/r0rCrGrJrKrLrMrOrRrU__copy__rXrZrSr]__iter__r_r^r`rDrar@rfrrrrrZs.         cs,eZdZdZdZddZfddZZS)rPa[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. )r8cCst|||}||_|S)N)rr"r8)r!obrr8rrrrr"PszKeyedRef.__new__cst||dS)N)r&rC)rrirr8)r(rrrCUszKeyedRef.__init__)r-r.r/r0r1r"rCr3rr)r(rrPDsrPc@seZdZdZd+ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZd,ddZddZddZddZeZdd Zd!d"Zd#d$Zd%d&Zd-d'd(Zd.d)d*ZdS)/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. NcCsFi|_t|fdd}||_g|_t|_d|_|dk rB||dS)NcSs.|}|dk r*|jr"|j|n|j|=dS)N)r5r6r7r9)r[r;rrrrr<fs z*WeakKeyDictionary.__init__..removeF)r9rr>r6r?r5 _dirty_lenr@)rrdr<rrrrCdszWeakKeyDictionary.__init__cCs>|j}|j}x,|r8y||=Wqtk r4YqXqWdS)N)r6r9rDrH)rrErFrrrrGusz"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|kr|qSrr).0r[)rFrr sz5WeakKeyDictionary._scrub_removals..F)r9r6rj)rr)rFr_scrub_removalssz!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dS)NT)rjr9r)rr8rrrrKszWeakKeyDictionary.__delitem__cCs|jt|S)N)r9r)rr8rrrrJszWeakKeyDictionary.__getitem__cCs(|jr|jr|t|jt|jS)N)rjr6rmr=r9)rrrrrLs zWeakKeyDictionary.__len__cCsd|jjt|fS)Nz <%s at %#x>)r(r-rN)rrrrrOszWeakKeyDictionary.__repr__cCs||jt||j<dS)N)r9rr>)rr8rQrrrrRszWeakKeyDictionary.__setitem__c CsLt}t|4x,|jD]\}}|}|dk r|||<qWWdQRX|S)N)r r r9rS)rrTr8rQrIrrrrUs zWeakKeyDictionary.copyc Cs`ddlm}|}t|:x2|jD]$\}}|}|dk r*|||||<q*WWdQRX|S)Nr)rV)rUrVr(r r9rS)rrWrVrTr8rQrIrrrrXs  zWeakKeyDictionary.__deepcopy__cCs|jt||S)N)r9rZr)rr8rYrrrrZszWeakKeyDictionary.getcCs,y t|}Wntk r dSX||jkS)NF)rrr9)rr8r:rrrrMs  zWeakKeyDictionary.__contains__c csHt|6x.|jD] \}}|}|dk r||fVqWWdQRXdS)N)r r9rS)rr:rQr8rrrrSs  zWeakKeyDictionary.itemsc cs<t|*x"|jD]}|}|dk r|VqWWdQRXdS)N)r r9)rr:r$rrrr]s   zWeakKeyDictionary.keysc cs@t|.x&|jD]\}}|dk r|VqWWdQRXdS)N)r r9rS)rr:rQrrrr^s  zWeakKeyDictionary.valuescCs t|jS)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. )rer9)rrrrkeyrefss zWeakKeyDictionary.keyrefscCs4d|_x(|j\}}|}|dk r||fSqWdS)NT)rjr9r`)rr8rQrIrrrr`s zWeakKeyDictionary.popitemcGsd|_|jjt|f|S)NT)rjr9rDr)rr8rArrrrDszWeakKeyDictionary.popcCs|jt||j|S)N)r9rarr>)rr8rYrrrraszWeakKeyDictionary.setdefaultcKs`|j}|dk rJt|ds$ti|}x$|D]\}}||t||j<q.Wt|r\||dS)NrS)r9rbr!rSrr>r=r@)rrdrcrFr8rQrrrr@s  zWeakKeyDictionary.update)N)N)N)N)r-r.r/r0rCrGrmrKrJrLrOrRrUrgrXrZrMrSr]rhr^rnr`rDrar@rrrrr Ys.       c@seZdZdZdZiZdZeZ dZ dZ GdddZ ddZ dd d Zd d Zd dZeddZeddZejddZddZeddZeddZdS)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%rArcatexitindexN)r-r.r/r1rrrr_InfosrrcOs t|dkr|^}}}}nz|s(tdnld|krDtdt|d|d}t|dkrf|^}}}n.d|krtdt|d|d}|^}}t|}|jsddl}||jd t_| }t |||_ ||_ ||_ |pd|_d |_t|j|_||j|<d t_dS) Nzs  zfinalize.detachcCs:|j|}|o|}|dk r6||j|j|jp2ifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)r{rZror%rArc)rr}r$rrrpeekFs  z finalize.peekcCs ||jkS)zWhether finalizer is alive)r{)rrrraliveNszfinalize.alivecCs|j|}t|o|jS)z*Whether finalizer should be called at exit)r{rZboolrp)rr}rrrrpSs zfinalize.atexitcCs|j|}|rt||_dS)N)r{rZrrp)rrQr}rrrrpYs cCs^|j|}|o|}|dkr6dt|jt|fSdt|jt|t|jt|fSdS)Nz<%s object at %#x; dead>z!<%s object at %#x; for %r at %#x>)r{rZror!r-rN)rr}r$rrrrO_s   zfinalize.__repr__cCs2dd|jD}|jddddd|DS)NcSsg|]\}}|jr||fqSr)rp)rkfirrrrlksz-finalize._select_for_exit..cSs |djS)Nr4)rq)itemrrrlz+finalize._select_for_exit..)r8cSsg|] \}}|qSrr)rkrrrrrrlms)r{rSsort)r#Lrrr_select_for_exithszfinalize._select_for_exitcCsd}z|jrddl}|r(d}|d}xj|dkss"(  6k