o w[e8T@sdZddlmZmZmZmZmZmZmZm Z ddl m Z m Z ddl Z ddlZddlZeefZgdZe je e je GdddeZGdd d e jZGd d d eZGd d d e jZGdddZdS)z|Weak reference support for Python. This module is an implementation of PEP 205: https://www.python.org/dev/peps/pep-0205/ )getweakrefcount getweakrefsrefproxyCallableProxyType ProxyType ReferenceType_remove_dead_weakref)WeakSet_IterationGuardN) rrrrWeakKeyDictionaryrrr 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__Ncsxz|j}|j}Wntytdt|dwfdd}t|||}t|||_t||_ d|_ t||S)Nz)argument should be a bound method, not {}cs.}|jrd|_dur|dSdSdSNF)r)argselfcallbackself_wr/usr/lib/python3.10/weakref.py_cb6s zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsmethrobjfuncrrrrrr$/s"     zWeakMethod.__new__cs2t}|}|dus|durdS|||SN)super__call__rr)rr'r( __class__rrr+Es  zWeakMethod.__call__cCs:t|tr|jr |js||uSt||o|j|jkStSr)) isinstancerrr__eq__rNotImplementedrotherrrrr/L  zWeakMethod.__eq__cCs:t|tr|jr |js||uSt||p|j|jkStSr))r.rrr__ne__rr0r1rrrr4Sr3zWeakMethod.__ne__r)) __name__ __module__ __qualname____doc__ __slots__r$r+r/r4r__hash__ __classcell__rrr,rr's  rc@seZdZdZd2ddZefddZddZd d Zd d Z d dZ ddZ ddZ ddZ e ZddZd3ddZddZddZeZddZd d!Zd"d#Zd$d%Zd3d&d'Zd3d(d)Zd*d+Zd,d-Zd.d/Zd0d1ZdS)4rzMapping 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)NcSs<|}|dur|jr|j|jdS||j|jdSdSr)) _iterating_pending_removalsappendkeydata)wrselfref_atomic_removalrrrrremovejs z,WeakValueDictionary.__init__..remove)rr _remover=setr<r@update)rr2kwrDrrr__init__is  zWeakValueDictionary.__init__cCs<|jj}|j} z|}Wn tyYdSw|||qr))r=popr@ IndexError)rrCrJdr?rrr_commit_removalszs   z$WeakValueDictionary._commit_removalscCs.|jr||j|}|durt||Sr)r=rMr@KeyErrorrr?orrr __getitem__s  zWeakValueDictionary.__getitem__cCs|jr||j|=dSr))r=rMr@rr?rrr __delitem__s zWeakValueDictionary.__delitem__cCs|jr|t|jSr))r=rMlenr@rrrr__len__s zWeakValueDictionary.__len__cCs:|jr|z |j|}W|duStyYdSwrrNrPrrr __contains__s z WeakValueDictionary.__contains__cCd|jjt|fSNz <%s at %#x>r-r5idrVrrr__repr__zWeakValueDictionary.__repr__cCs&|jr|t||j||j|<dSr))r=rMKeyedRefrEr@rr?valuerrr __setitem__szWeakValueDictionary.__setitem__cCsl|jr|t}t||jD]\}}|}|dur#|||<qWd|S1s/wY|Sr))r=rMrr r@items)rnewr?rArQrrrcopys  zWeakValueDictionary.copycCsddlm}|jr ||}t|!|jD]\}}|}|dur-|||||<qWd|S1s9wY|SNr)deepcopy)rergr=rMr-r r@rc)rmemorgrdr?rArQrrr __deepcopy__s   z WeakValueDictionary.__deepcopy__NcCsJ|jr|z|j|}Wn ty|YSw|}|dur#|S|Sr)rN)rr?defaultrArQrrrgets zWeakValueDictionary.getccsj|jr|t||jD]\}}|}|dur"||fVqWddS1s.wYdSr)r=rMr r@rc)rkrAvrrrrcs  "zWeakValueDictionary.itemsccsb|jr|t||jD] \}}|dur|VqWddS1s*wYdSr)rl)rrmrArrrkeyss  "zWeakValueDictionary.keysccsN|jr|t||jEdHWddS1s wYdS)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. Nr=rMr r@valuesrVrrr itervaluerefss   "z!WeakValueDictionary.itervaluerefsccsb|jr|t||jD] }|}|dur|VqWddS1s*wYdSr)rprrAr'rrrrqs "zWeakValueDictionary.valuescCs6|jr| |j\}}|}|dur||fSqr))r=rMr@popitem)rr?rArQrrrrtszWeakValueDictionary.popitemcGsX|jr|z |j|}Wn tyd}Ynw|dur*|r&|dSt||S)Nr)r=rMr@rJrO)rr?argsrQrrrrJs zWeakValueDictionary.popcCsZz|j|}Wn tyd}Ynw|dur+|jr|t||j||j|<|S|Sr))r@rOr=rMr_rE)rr?rjrQrrr setdefaults zWeakValueDictionary.setdefaultcKsz|jr||j}|dur)t|dst|}|D] \}}t||j|||<q|D] \}}t||j|||<q-dSNrc)r=rMr@hasattrdictrcr_rE)rr2kwargsrLr?rQrrrrG"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. )r=rMlistr@rqrVrrr valuerefs.s zWeakValueDictionary.valuerefscC|||Sr)rGr1rrr__ior__< zWeakValueDictionary.__ior__cC&t|tjr|}|||StSr)r._collections_abcMappingrerGr0rr2crrr__or__@  zWeakValueDictionary.__or__cC0t|tjr|}|||||StSr)r.rrr-rGr0rrrr__ror__G   zWeakValueDictionary.__ror__)rr))r5r6r7r8rIr rMrRrTrWrXr]rbre__copy__rirkrcro__iter__rrrqrtrJrvrGr|rrrrrrrr]s6            rcs,eZdZdZdZddZfddZZS)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*rI)rrrr?r,rrrIazKeyedRef.__init__)r5r6r7r8r9r$rIr;rrr,rr_Ps  r_c@seZdZdZd1ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZd1ddZddZddZddZeZdd Zd!d"Zd#d$Zd%d&Zd1d'd(Zd1d)d*Zd+d,Zd-d.Zd/d0ZdS)2r 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. NcCsJi|_t|fdd}||_g|_t|_d|_|dur#||dSdS)NcSsL|}|dur$|jr|j|dSz|j|=WdSty#YdSwdSr))r<r=r>r@rO)rmrBrrrrrDrs z*WeakKeyDictionary.__init__..removeF)r@rrEr=rFr< _dirty_lenrG)rryrDrrrrIps zWeakKeyDictionary.__init__cCsP|jj}|j} z|}Wn tyYdSwz||=Wn ty&Ynwqr))r=rJr@rKrO)rrJrLr?rrrrMs    z"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|vr|qSrr).0rmrLrr sz5WeakKeyDictionary._scrub_removals..F)r@r=rrVrrr_scrub_removalss z!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dSNT)rr@rrSrrrrTszWeakKeyDictionary.__delitem__cCs|jt|Sr))r@rrSrrrrRszWeakKeyDictionary.__getitem__cCs(|jr |jr |t|jt|jSr))rr=rrUr@rVrrrrWs zWeakKeyDictionary.__len__cCrYrZr[rVrrrr]r^zWeakKeyDictionary.__repr__cCs||jt||j<dSr))r@rrEr`rrrrbzWeakKeyDictionary.__setitem__cCs^t}t||jD]\}}|}|dur|||<q Wd|S1s(wY|Sr))r r r@rc)rrdr?rarQrrrres  zWeakKeyDictionary.copycCsrddlm}|}t|!|jD]\}}|}|dur&|||||<qWd|S1s2wY|Srf)rergr-r r@rc)rrhrgrdr?rarQrrrris   zWeakKeyDictionary.__deepcopy__cCs|jt||Sr))r@rkrrr?rjrrrrkrzWeakKeyDictionary.getcCs,zt|}Wn tyYdSw||jvSr)rr!r@)rr?rArrrrXs    zWeakKeyDictionary.__contains__ccs\t||jD]\}}|}|dur||fVq WddS1s'wYdSr)r r@rc)rrArar?rrrrcs  "zWeakKeyDictionary.itemsccsPt||jD] }|}|dur|Vq WddS1s!wYdSr))r r@rsrrrros  "zWeakKeyDictionary.keysccsTt||jD] \}}|dur|Vq WddS1s#wYdSr)r)rrArarrrrqs  "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. )r{r@rVrrrkeyrefss zWeakKeyDictionary.keyrefscCs.d|_ |j\}}|}|dur||fSqr)rr@rt)rr?rarQrrrrtszWeakKeyDictionary.popitemcGsd|_|jjt|g|RSr)rr@rJr)rr?rurrrrJszWeakKeyDictionary.popcCs|jt||j|Sr))r@rvrrErrrrrvrzWeakKeyDictionary.setdefaultcKs`|j}|dur#t|dsti|}|D] \}}||t||j<qt|r.||dSdSrw)r@rxr#rcrrErUrG)rryrzrLr?rarrrrGs  zWeakKeyDictionary.updatecCr}r)r~r1rrrr rzWeakKeyDictionary.__ior__cCrr)rrrrrr rzWeakKeyDictionary.__or__cCrr)rrrrrrrzWeakKeyDictionary.__ror__r))r5r6r7r8rIrMrrTrRrWr]rbrerrirkrXrcrorrqrrtrJrvrGrrrrrrrr es6        r 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(rurzatexitindexN)r5r6r7r9rrrr_Info6srcOsp|jsddl}||jdt_|}t|||_||_||_ |p#d|_ d|_t |j |_ ||j|<dt_dS)NrT)_registered_with_atexitrregister _exitfuncrrrrr(rurznext _index_iterr _registry_dirty)rr'r(rurzrinforrrrI9s      zfinalize.__init__NcCs8|j|d}|r|js|j|ji|jpiSdSdS)zZIf alive then mark as dead and return func(*args, **kwargs); otherwise return NoneN)rrJ _shutdownr(rurz)r_rrrrr+Js zfinalize.__call__cCsL|j|}|o |}|dur"|j|dr$||j|j|jp ifSdSdS)z^If alive then mark as dead and return (obj, func, args, kwargs); otherwise return NoneN)rrkrrJr(rurzrrr'rrrdetachQs  zfinalize.detachcCs:|j|}|o |}|dur||j|j|jpifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rrkrr(rurzrrrrpeekYs  z finalize.peekcCs ||jvS)zWhether finalizer is alive)rrVrrraliveas zfinalize.alivecCs|j|}t|o |jS)z*Whether finalizer should be called at exitrrkboolr)rrrrrrfs zfinalize.atexitcCs"|j|}|rt||_dSdSr)r)rrarrrrrls cCsZ|j|}|o |}|durdt|jt|fSdt|jt|t|jt|fS)Nz<%s object at %#x; dead>z!<%s object at %#x; for %r at %#x>)rrkrr#r5r\rrrrr]rs  zfinalize.__repr__cCs2dd|jD}|jddddd|DS)NcSsg|] \}}|jr||fqSr)rrfirrrr~sz-finalize._select_for_exit..cSs |djS)N)r)itemrrrs z+finalize._select_for_exit..rcSsg|]\}}|qSrrrrrrrs)rrcsort)r%Lrrr_select_for_exit{szfinalize._select_for_exitcCsd}zU|jrJddl}|rd}|d} |dustjr%|}dt_|s(n"|}z|WntyAt j t Ynw||jvsIJqWdt_ |rV| dSdSdt_ |rb| ww)NFrT)rgc isenableddisablerrrrJ Exceptionsys excepthookexc_inforenable)r% reenable_gcrpendingrrrrrs>    zfinalize._exitfuncr))r5r6r7r8r9rr itertoolscountrrrrrIr+rrpropertyrrsetterr] classmethodrrrrrrrs0     r)r8_weakrefrrrrrrrr _weakrefsetr r rrrr __all__Setr MutableSetrMutableMappingrr_r rrrrrs"(    6t9