a DOg8T@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__Ncs|z|j}|j}Wn&ty6tdt|dYn0fdd}t|||}t|||_t||_ d|_ t||S)Nz)argument should be a bound method, not {}cs&}|jr"d|_dur"|dSNF)r)argselfcallbackZself_wr,/opt/alt/python39/lib64/python3.9/weakref.py_cb6s zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsZmethrobjfuncrrrrrr#/s     zWeakMethod.__new__cs2t}|}|dus"|dur&dS|||SN)super__call__rr)rr%r& __class__rrr)Es  zWeakMethod.__call__cCs:t|tr6|jr|js||uSt||o4|j|jkStSr') isinstancerrr__eq__rNotImplementedrotherrrrr-Ls   zWeakMethod.__eq__cCs:t|tr6|jr|js||uSt||p4|j|jkStSr')r,rrr__ne__rr.r/rrrr1Ss   zWeakMethod.__ne__)N) __name__ __module__ __qualname____doc__ __slots__r#r)r-r1r__hash__ __classcell__rrr*rr's  rc@seZdZdZd2ddZefddZddZd d Zd d Z d dZ ddZ ddZ ddZ e ZddZd3ddZddZddZeZddZd d!Zd"d#Zd$d%Zd4d&d'Zd5d(d)Zd*d+Zd,d-Zd.d/Zd0d1ZdS)6rzMapping 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|jn||j|jdSr') _iterating_pending_removalsappendkeydata)wrselfref_atomic_removalrrrrremovejs z,WeakValueDictionary.__init__..remove)rr _remover:setr9r=update)rr0kwrArrr__init__is  zWeakValueDictionary.__init__cCs>|jj}|j}z |}Wnty,YdS0|||qdSr')r:popr= IndexError)rr@rGdr<rrr_commit_removalszs  z$WeakValueDictionary._commit_removalscCs4|jr||j|}|dur,t|n|SdSr'r:rJr=KeyErrorrr<orrr __getitem__s   zWeakValueDictionary.__getitem__cCs|jr||j|=dSr')r:rJr=rr<rrr __delitem__szWeakValueDictionary.__delitem__cCs|jr|t|jSr')r:rJlenr=rrrr__len__szWeakValueDictionary.__len__cCs<|jr|z|j|}Wnty2YdS0|duSrrKrMrrr __contains__s z WeakValueDictionary.__contains__cCsd|jjt|fSNz <%s at %#x>r+r2idrSrrr__repr__szWeakValueDictionary.__repr__cCs&|jr|t||j||j|<dSr')r:rJKeyedRefrBr=rr<valuerrr __setitem__szWeakValueDictionary.__setitem__cCsj|jr|t}t|:|jD]\}}|}|dur(|||<q(Wdn1s\0Y|Sr')r:rJrr r=items)rnewr<r>rNrrrcopys (zWeakValueDictionary.copycCs~ddlm}|jr||}t|@|jD]$\}}|}|dur6|||||<q6Wdn1sp0Y|SNr)deepcopy)r`rbr:rJr+r r=r^)rmemorbr_r<r>rNrrr __deepcopy__s  .z WeakValueDictionary.__deepcopy__NcCsN|jr|z|j|}Wnty2|YS0|}|durF|S|SdSr'rK)rr<defaultr>rNrrrgets  zWeakValueDictionary.getccsf|jr|t|<|jD] \}}|}|dur"||fVq"Wdn1sX0YdSr'r:rJr r=r^)rkr>vrrrr^s zWeakValueDictionary.itemsccs^|jr|t|4|jD]\}}|dur"|Vq"Wdn1sP0YdSr'rg)rrhr>rrrkeyss   zWeakValueDictionary.keysccsJ|jr|t| |jEdHWdn1s<0YdS)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:rJr r=valuesrSrrr itervaluerefss  z!WeakValueDictionary.itervaluerefsccs^|jr|t|4|jD]}|}|dur"|Vq"Wdn1sP0YdSr'rkrr>r%rrrrls zWeakValueDictionary.valuescCs8|jr||j\}}|}|dur||fSqdSr')r:rJr=popitem)rr<r>rNrrrros zWeakValueDictionary.popitemcGs^|jr|z|j|}Wnty6d}Yn0|durV|rL|dSt|n|SdS)Nr)r:rJr=rGrL)rr<argsrNrrrrGs   zWeakValueDictionary.popcCs^z|j|}Wnty&d}Yn0|durV|jr>|t||j||j|<|S|SdSr')r=rLr:rJrZrB)rr<rerNrrr setdefaults  zWeakValueDictionary.setdefaultcKsz|jr||j}|durRt|ds.t|}|D]\}}t||j|||<q6|D]\}}t||j|||<qZdSNr^)r:rJr=hasattrdictr^rZrB)rr0kwargsrIr<rNrrrrD"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:rJlistr=rlrSrrr valuerefs.s zWeakValueDictionary.valuerefscCs|||Sr'rDr/rrr__ior__<s zWeakValueDictionary.__ior__cCs&t|tjr"|}|||StSr'r,_collections_abcMappingr`rDr.rr0crrr__or__@s   zWeakValueDictionary.__or__cCs0t|tjr,|}|||||StSr'r,r{r|r+rDr.r}rrr__ror__Gs    zWeakValueDictionary.__ror__)r)N)N)N)r2r3r4r5rFr rJrOrQrTrUrYr]r`__copy__rdrfr^rj__iter__rmrlrorGrqrDrwryrrrrrrr]s4          rcs,eZdZdZdZddZfddZZS)rZa[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(rF)rrrr<r*rrrFaszKeyedRef.__init__)r2r3r4r5r6r#rFr8rrr*rrZPs rZc@seZdZdZd1ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZd2ddZddZddZddZeZdd Zd!d"Zd#d$Zd%d&Zd3d'd(Zd4d)d*Zd+d,Zd-d.Zd/d0ZdS)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')r9r:r;r=rL)rhr?rrrrrArs  z*WeakKeyDictionary.__init__..removeF)r=rrBr:rCr9 _dirty_lenrD)rrtrArrrrFps zWeakKeyDictionary.__init__cCsR|jj}|j}z |}Wnty,YdS0z ||=WqtyJYq0qdSr')r:rGr=rHrL)rrGrIr<rrrrJs    z"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|vr|qSrr).0rhrIrr z5WeakKeyDictionary._scrub_removals..F)r=r:rrSrrr_scrub_removalssz!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dSNT)rr=rrPrrrrQszWeakKeyDictionary.__delitem__cCs|jt|Sr')r=rrPrrrrOszWeakKeyDictionary.__getitem__cCs(|jr|jr|t|jt|jSr')rr:rrRr=rSrrrrTs zWeakKeyDictionary.__len__cCsd|jjt|fSrVrWrSrrrrYszWeakKeyDictionary.__repr__cCs||jt||j<dSr')r=rrBr[rrrr]szWeakKeyDictionary.__setitem__cCs\t}t|:|jD]\}}|}|dur|||<qWdn1sN0Y|Sr')r r r=r^)rr_r<r\rNrrrr`s (zWeakKeyDictionary.copycCspddlm}|}t|@|jD]$\}}|}|dur(|||||<q(Wdn1sb0Y|Sra)r`rbr+r r=r^)rrcrbr_r<r\rNrrrrds  .zWeakKeyDictionary.__deepcopy__cCs|jt||Sr')r=rfrrr<rerrrrfszWeakKeyDictionary.getcCs,z t|}Wnty YdS0||jvSr)rr r=)rr<r>rrrrUs   zWeakKeyDictionary.__contains__ccsXt|<|jD] \}}|}|dur||fVqWdn1sJ0YdSr'r r=r^)rr>r\r<rrrr^s  zWeakKeyDictionary.itemsccsLt|0|jD]}|}|dur|VqWdn1s>0YdSr')r r=rnrrrrjs   zWeakKeyDictionary.keysccsPt|4|jD]\}}|dur|VqWdn1sB0YdSr'r)rr>r\rrrrls  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. )rvr=rSrrrkeyrefss zWeakKeyDictionary.keyrefscCs0d|_|j\}}|}|dur||fSqdSr)rr=ro)rr<r\rNrrrros zWeakKeyDictionary.popitemcGsd|_|jjt|g|RSr)rr=rGr)rr<rprrrrGszWeakKeyDictionary.popcCs|jt||j|Sr')r=rqrrBrrrrrqszWeakKeyDictionary.setdefaultcKs\|j}|durFt|ds$ti|}|D]\}}||t||j<q,t|rX||dSrr)r=rsr"r^rrBrRrD)rrtrurIr<r\rrrrDs  zWeakKeyDictionary.updatecCs|||Sr'rxr/rrrry s zWeakKeyDictionary.__ior__cCs&t|tjr"|}|||StSr'rzr}rrrr s   zWeakKeyDictionary.__or__cCs0t|tjr,|}|||||StSr'rr}rrrrs    zWeakKeyDictionary.__ror__)N)N)N)N)r2r3r4r5rFrJrrQrOrTrYr]r`rrdrfrUr^rjrrlrrorGrqrDryrrrrrrr es4      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&rpruatexitindexN)r2r3r4r6rrrr_Info6srcOsp|js ddl}||jdt_|}t|||_||_||_ |pFd|_ d|_t |j |_ ||j|<dt_dS)NrT)_registered_with_atexitrregister _exitfuncrrrrr&rprunext _index_iterr _registry_dirty)rr%r&rprurinforrrrF9s     zfinalize.__init__NcCs4|j|d}|r0|js0|j|ji|jp*iSdS)zZIf alive then mark as dead and return func(*args, **kwargs); otherwise return NoneN)rrG _shutdownr&rpru)r_rrrrr)Js zfinalize.__call__cCsH|j|}|o|}|durD|j|drD||j|j|jp@ifSdS)z^If alive then mark as dead and return (obj, func, args, kwargs); otherwise return NoneN)rrfrrGr&rprurrr%rrrdetachQs  zfinalize.detachcCs:|j|}|o|}|dur6||j|j|jp2ifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rrfrr&rprurrrrpeekYs  z finalize.peekcCs ||jvS)zWhether finalizer is alive)rrSrrraliveaszfinalize.alivecCs|j|}t|o|jS)z*Whether finalizer should be called at exitrrfboolr)rrrrrrfs zfinalize.atexitcCs|j|}|rt||_dSr'r)rr\rrrrrls 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>)rrfrr"r2rXrrrrrYrs  zfinalize.__repr__cCs2dd|jD}|jddddd|DS)NcSsg|]\}}|jr||fqSr)rrfirrrr~rz-finalize._select_for_exit..cSs |djS)N)r)itemrrrrz+finalize._select_for_exit..rcSsg|] \}}|qSrrrrrrrr)rr^sort)r$Lrrr_select_for_exit{szfinalize._select_for_exitcCsd}z|jrddl}|r(d}|d}|dus:tjrH|}dt_|sNq|}z |Wq,tyt j t Yq,0q,Wdt_ |r| ndt_ |r| 0dS)NFrT)rgcZ isenabledZdisablerrrrG Exceptionsys excepthookexc_inforZenable)r$Z reenable_gcrZpendingrrrrrs2   zfinalize._exitfunc)N)r2r3r4r5r6rr itertoolscountrrrrrFr)rrpropertyrrsetterrY classmethodrrrrrrrs.      r)r5_weakrefrrrrrrrr Z _weakrefsetr r r{rrr __all__Setr MutableSetrMutableMappingrrZr rrrrrs  (   6t9