o ?Og8T@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)argselfcallbackZself_wr./opt/alt/python310/lib64/python3.10/weakref.py_cb6s zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsZmethrobjfuncrrrrrr#/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__rr.r/rrrr2Sr1zWeakMethod.__ne__r') __name__ __module__ __qualname____doc__ __slots__r#r)r-r2r__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)rr0kwrBrrr__init__is  zWeakValueDictionary.__init__cCs<|jj}|j} z|}Wn tyYdSw|||qr')r;popr> IndexError)rrArHdr=rrr_commit_removalszs   z$WeakValueDictionary._commit_removalscCs.|jr||j|}|durt||Sr'r;rKr>KeyErrorrr=orrr __getitem__s  zWeakValueDictionary.__getitem__cCs|jr||j|=dSr')r;rKr>rr=rrr __delitem__s zWeakValueDictionary.__delitem__cCs|jr|t|jSr')r;rKlenr>rrrr__len__s zWeakValueDictionary.__len__cCs:|jr|z |j|}W|duStyYdSwrrLrNrrr __contains__s z WeakValueDictionary.__contains__cCd|jjt|fSNz <%s at %#x>r+r3idrTrrr__repr__zWeakValueDictionary.__repr__cCs&|jr|t||j||j|<dSr')r;rKKeyedRefrCr>rr=valuerrr __setitem__szWeakValueDictionary.__setitem__cCsl|jr|t}t||jD]\}}|}|dur#|||<qWd|S1s/wY|Sr')r;rKrr r>items)rnewr=r?rOrrrcopys  zWeakValueDictionary.copycCsddlm}|jr ||}t|!|jD]\}}|}|dur-|||||<qWd|S1s9wY|SNr)deepcopy)rcrer;rKr+r r>ra)rmemorerbr=r?rOrrr __deepcopy__s   z WeakValueDictionary.__deepcopy__NcCsJ|jr|z|j|}Wn ty|YSw|}|dur#|S|Sr'rL)rr=defaultr?rOrrrgets zWeakValueDictionary.getccsj|jr|t||jD]\}}|}|dur"||fVqWddS1s.wYdSr'r;rKr r>ra)rkr?vrrrras  "zWeakValueDictionary.itemsccsb|jr|t||jD] \}}|dur|VqWddS1s*wYdSr'rj)rrkr?rrrkeyss  "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;rKr r>valuesrTrrr itervaluerefss   "z!WeakValueDictionary.itervaluerefsccsb|jr|t||jD] }|}|dur|VqWddS1s*wYdSr'rnrr?r%rrrros "zWeakValueDictionary.valuescCs6|jr| |j\}}|}|dur||fSqr')r;rKr>popitem)rr=r?rOrrrrrszWeakValueDictionary.popitemcGsX|jr|z |j|}Wn tyd}Ynw|dur*|r&|dSt||S)Nr)r;rKr>rHrM)rr=argsrOrrrrHs zWeakValueDictionary.popcCsZz|j|}Wn tyd}Ynw|dur+|jr|t||j||j|<|S|Sr')r>rMr;rKr]rC)rr=rhrOrrr setdefaults zWeakValueDictionary.setdefaultcKsz|jr||j}|dur)t|dst|}|D] \}}t||j|||<q|D] \}}t||j|||<q-dSNra)r;rKr>hasattrdictrar]rC)rr0kwargsrJr=rOrrrrE"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;rKlistr>rorTrrr valuerefs.s zWeakValueDictionary.valuerefscC|||Sr'rEr/rrr__ior__< zWeakValueDictionary.__ior__cC&t|tjr|}|||StSr'r,_collections_abcMappingrcrEr.rr0crrr__or__@  zWeakValueDictionary.__or__cC0t|tjr|}|||||StSr'r,rrr+rEr.rrrr__ror__G   zWeakValueDictionary.__ror__)rr')r3r4r5r6rGr rKrPrRrUrVr[r`rc__copy__rgrirarm__iter__rprorrrHrtrErzr}rrrrrrr]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(rG)rrrr=r*rrrGazKeyedRef.__init__)r3r4r5r6r7r#rGr9rrr*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>rM)rkr@rrrrrBrs z*WeakKeyDictionary.__init__..removeF)r>rrCr;rDr: _dirty_lenrE)rrwrBrrrrGps zWeakKeyDictionary.__init__cCsP|jj}|j} z|}Wn tyYdSwz||=Wn ty&Ynwqr')r;rHr>rIrM)rrHrJr=rrrrKs    z"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|vr|qSrr).0rkrJrr sz5WeakKeyDictionary._scrub_removals..F)r>r;rrTrrr_scrub_removalss z!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dSNT)rr>rrQrrrrRszWeakKeyDictionary.__delitem__cCs|jt|Sr')r>rrQrrrrPszWeakKeyDictionary.__getitem__cCs(|jr |jr |t|jt|jSr')rr;rrSr>rTrrrrUs zWeakKeyDictionary.__len__cCrWrXrYrTrrrr[r\zWeakKeyDictionary.__repr__cCs||jt||j<dSr')r>rrCr^rrrr`zWeakKeyDictionary.__setitem__cCs^t}t||jD]\}}|}|dur|||<q Wd|S1s(wY|Sr')r r r>ra)rrbr=r_rOrrrrcs  zWeakKeyDictionary.copycCsrddlm}|}t|!|jD]\}}|}|dur&|||||<qWd|S1s2wY|Srd)rcrer+r r>ra)rrfrerbr=r_rOrrrrgs   zWeakKeyDictionary.__deepcopy__cCs|jt||Sr')r>rirrr=rhrrrrirzWeakKeyDictionary.getcCs,zt|}Wn tyYdSw||jvSr)rr r>)rr=r?rrrrVs    zWeakKeyDictionary.__contains__ccs\t||jD]\}}|}|dur||fVq WddS1s'wYdSr'r r>ra)rr?r_r=rrrras  "zWeakKeyDictionary.itemsccsPt||jD] }|}|dur|Vq WddS1s!wYdSr')r r>rqrrrrms  "zWeakKeyDictionary.keysccsTt||jD] \}}|dur|Vq WddS1s#wYdSr'r)rr?r_rrrros  "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. )ryr>rTrrrkeyrefss zWeakKeyDictionary.keyrefscCs.d|_ |j\}}|}|dur||fSqr)rr>rr)rr=r_rOrrrrrszWeakKeyDictionary.popitemcGsd|_|jjt|g|RSr)rr>rHr)rr=rsrrrrHszWeakKeyDictionary.popcCs|jt||j|Sr')r>rtrrCrrrrrtrzWeakKeyDictionary.setdefaultcKs`|j}|dur#t|dsti|}|D] \}}||t||j<qt|r.||dSdSru)r>rvr"rarrCrSrE)rrwrxrJr=r_rrrrEs  zWeakKeyDictionary.updatecCr{r'r|r/rrrr} r~zWeakKeyDictionary.__ior__cCrr'rrrrrr rzWeakKeyDictionary.__or__cCrr'rrrrrrrzWeakKeyDictionary.__ror__r')r3r4r5r6rGrKrrRrPrUr[r`rcrrgrirVrarmrrorrrrHrtrEr}rrrrrrr 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&rsrxatexitindexN)r3r4r5r7rrrr_Info6srcOsp|jsddl}||jdt_|}t|||_||_||_ |p#d|_ d|_t |j |_ ||j|<dt_dS)NrT)_registered_with_atexitrregister _exitfuncrrrrr&rsrxnext _index_iterr _registry_dirty)rr%r&rsrxrinforrrrG9s      zfinalize.__init__NcCs8|j|d}|r|js|j|ji|jpiSdSdS)zZIf alive then mark as dead and return func(*args, **kwargs); otherwise return NoneN)rrH _shutdownr&rsrx)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)rrirrHr&rsrxrrr%rrrdetachQs  zfinalize.detachcCs:|j|}|o |}|dur||j|j|jpifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rrirr&rsrxrrrrpeekYs  z finalize.peekcCs ||jvS)zWhether finalizer is alive)rrTrrraliveas zfinalize.alivecCs|j|}t|o |jS)z*Whether finalizer should be called at exitrriboolr)rrrrrrfs zfinalize.atexitcCs"|j|}|rt||_dSdSr'r)rr_rrrrrls 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>)rrirr"r3rZrrrrr[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)rrasort)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)rgcZ isenabledZdisablerrrrH Exceptionsys excepthookexc_inforZenable)r$Z reenable_gcrZpendingrrrrrs>    zfinalize._exitfuncr')r3r4r5r6r7rr itertoolscountrrrrrGr)rrpropertyrrsetterr[ classmethodrrrrrrrs0     r)r6_weakrefrrrrrrrr Z _weakrefsetr r rrrr __all__Setr MutableSetrMutableMappingrr]r rrrrrs"(    6t9