a ;`R@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: http://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/usr/lib/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@seZdZdZd2ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZd3ddZddZddZeZddZd 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)wrselfrefZ_atomic_removalrrrrremovejs z,WeakValueDictionary.__init__..remove)rr _remover:setr9r=update)rr0kwr@rrr__init__is  zWeakValueDictionary.__init__cCs(|j}|j}|r$|}t||q dSr')r:r=popr )rldr<rrr_commit_removalszs z$WeakValueDictionary._commit_removalscCs4|jr||j|}|dur,t|n|SdSr'r:rIr=KeyErrorrr<orrr __getitem__s   zWeakValueDictionary.__getitem__cCs|jr||j|=dSr')r:rIr=rr<rrr __delitem__szWeakValueDictionary.__delitem__cCs|jr|t|jSr')r:rIlenr=rrrr__len__szWeakValueDictionary.__len__cCs<|jr|z|j|}Wnty2YdS0|duSrrJrLrrr __contains__s z WeakValueDictionary.__contains__cCsd|jjt|fSNz <%s at %#x>r+r2idrRrrr__repr__szWeakValueDictionary.__repr__cCs&|jr|t||j||j|<dSr')r:rIKeyedRefrAr=rr<valuerrr __setitem__szWeakValueDictionary.__setitem__cCsj|jr|t}t|:|jD]\}}|}|dur(|||<q(Wdn1s\0Y|Sr')r:rIrr r=items)rnewr<r>rMrrrcopys (zWeakValueDictionary.copycCs~ddlm}|jr||}t|@|jD]$\}}|}|dur6|||||<q6Wdn1sp0Y|SNr)deepcopy)r_rar:rIr+r r=r])rmemorar^r<r>rMrrr __deepcopy__s  .z WeakValueDictionary.__deepcopy__NcCsN|jr|z|j|}Wnty2|YS0|}|durF|S|SdSr'rJ)rr<defaultr>rMrrrgets  zWeakValueDictionary.getccsf|jr|t|<|jD] \}}|}|dur"||fVq"Wdn1sX0YdSr'r:rIr r=r])rkr>vrrrr]s zWeakValueDictionary.itemsccs^|jr|t|4|jD]\}}|dur"|Vq"Wdn1sP0YdSr'rf)rrgr>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:rIr r=valuesrRrrr itervaluerefss  z!WeakValueDictionary.itervaluerefsccs^|jr|t|4|jD]}|}|dur"|Vq"Wdn1sP0YdSr'rjrr>r%rrrrks zWeakValueDictionary.valuescCs8|jr||j\}}|}|dur||fSqdSr')r:rIr=popitem)rr<r>rMrrrrns zWeakValueDictionary.popitemcGs^|jr|z|j|}Wnty6d}Yn0|durV|rL|dSt|n|SdS)Nr)r:rIr=rFrK)rr<argsrMrrrrFs   zWeakValueDictionary.popcCs^z|j|}Wnty&d}Yn0|durV|jr>|t||j||j|<|S|SdSr')r=rKr:rIrYrA)rr<rdrMrrr setdefaults  zWeakValueDictionary.setdefaultcKsz|jr||j}|durRt|ds.t|}|D]\}}t||j|||<q6|D]\}}t||j|||<qZdSNr])r:rIr=hasattrdictr]rYrA)rr0kwargsrHr<rMrrrrCs 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:rIlistr=rkrRrrr valuerefs+s zWeakValueDictionary.valuerefscCs|||Sr'rCr/rrr__ior__9s zWeakValueDictionary.__ior__cCs&t|tjr"|}|||StSr'r,_collections_abcMappingr_rCr.rr0crrr__or__=s   zWeakValueDictionary.__or__cCs0t|tjr,|}|||||StSr'r,rzr{r+rCr.r|rrr__ror__Ds    zWeakValueDictionary.__ror__)r)N)N)N)r2r3r4r5rErIrNrPrSrTrXr\r___copy__rcrer]ri__iter__rlrkrnrFrprCrvrxr~rrrrrr]s4          rcs,eZdZdZdZddZfddZZS)rYa[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#YszKeyedRef.__new__cst||dSr')r(rE)rrrr<r*rrrE^szKeyedRef.__init__)r2r3r4r5r6r#rEr8rrr*rrYMs rYc@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)NcSs.|}|dur*|jr"|j|n|j|=dSr')r9r:r;r=)rgr?rrrrr@os z*WeakKeyDictionary.__init__..removeF)r=rrAr:rBr9 _dirty_lenrC)rrsr@rrrrEmszWeakKeyDictionary.__init__cCs8|j}|j}|r4z||=Wq ty0Yq 0q dSr')r:r=rFrK)rrGrHrrrrI~s z"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|vr|qSrr).0rgrHrr z5WeakKeyDictionary._scrub_removals..F)r=r:rrRrrr_scrub_removalssz!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dSNT)rr=rrOrrrrPszWeakKeyDictionary.__delitem__cCs|jt|Sr')r=rrOrrrrNszWeakKeyDictionary.__getitem__cCs(|jr|jr|t|jt|jSr')rr:rrQr=rRrrrrSs zWeakKeyDictionary.__len__cCsd|jjt|fSrUrVrRrrrrXszWeakKeyDictionary.__repr__cCs||jt||j<dSr')r=rrArZrrrr\szWeakKeyDictionary.__setitem__cCs\t}t|:|jD]\}}|}|dur|||<qWdn1sN0Y|Sr')r r r=r])rr^r<r[rMrrrr_s (zWeakKeyDictionary.copycCspddlm}|}t|@|jD]$\}}|}|dur(|||||<q(Wdn1sb0Y|Sr`)r_rar+r r=r])rrbrar^r<r[rMrrrrcs  .zWeakKeyDictionary.__deepcopy__cCs|jt||Sr')r=rerrr<rdrrrreszWeakKeyDictionary.getcCs,z t|}Wnty YdS0||jvSr)rr r=)rr<r>rrrrTs   zWeakKeyDictionary.__contains__ccsXt|<|jD] \}}|}|dur||fVqWdn1sJ0YdSr'r r=r])rr>r[r<rrrr]s  zWeakKeyDictionary.itemsccsLt|0|jD]}|}|dur|VqWdn1s>0YdSr')r r=rmrrrris   zWeakKeyDictionary.keysccsPt|4|jD]\}}|dur|VqWdn1sB0YdSr'r)rr>r[rrrrks  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. )rur=rRrrrkeyrefss zWeakKeyDictionary.keyrefscCs0d|_|j\}}|}|dur||fSqdSr)rr=rn)rr<r[rMrrrrns zWeakKeyDictionary.popitemcGsd|_|jjt|g|RSr)rr=rFr)rr<rorrrrFszWeakKeyDictionary.popcCs|jt||j|Sr')r=rprrArrrrrpszWeakKeyDictionary.setdefaultcKs\|j}|durFt|ds$ti|}|D]\}}||t||j<q,t|rX||dSrq)r=rrr"r]rrArQrC)rrsrtrHr<r[rrrrCs  zWeakKeyDictionary.updatecCs|||Sr'rwr/rrrrxs zWeakKeyDictionary.__ior__cCs&t|tjr"|}|||StSr'ryr|rrrr~s   zWeakKeyDictionary.__or__cCs0t|tjr,|}|||||StSr'rr|rrrr s    zWeakKeyDictionary.__ror__)N)N)N)N)r2r3r4r5rErIrrPrNrSrXr\r_rrcrerTr]rirrkrrnrFrprCrxr~rrrrrr bs4       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&rortatexitindexN)r2r3r4r6rrrr_Info+srcOsp|js ddl}||jdt_|}t|||_||_||_ |pFd|_ d|_t |j |_ ||j|<dt_dS)NrT)_registered_with_atexitrregister _exitfuncrrrrr&rortnext _index_iterr _registry_dirty)rr%r&rortrinforrrrE.s     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)rrF _shutdownr&rort)r_rrrrr)?s 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)rrerrFr&rortrrr%rrrdetachFs  zfinalize.detachcCs:|j|}|o|}|dur6||j|j|jp2ifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rrerr&rortrrrrpeekNs  z finalize.peekcCs ||jvS)zWhether finalizer is alive)rrRrrraliveVszfinalize.alivecCs|j|}t|o|jS)z*Whether finalizer should be called at exitrreboolr)rrrrrr[s zfinalize.atexitcCs|j|}|rt||_dSr'r)rr[rrrrras 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>)rrerr"r2rWrrrrrXgs  zfinalize.__repr__cCs2dd|jD}|jddddd|DS)NcSsg|]\}}|jr||fqSr)rrfirrrrsrz-finalize._select_for_exit..cSs |djS)N)r)itemrrrtrz+finalize._select_for_exit..rcSsg|] \}}|qSrrrrrrrur)rr]sort)r$Lrrr_select_for_exitpszfinalize._select_for_exitcCsd}z|jrddl}|r(d}|d}|dus:tjrH|}dt_|sNq|}z |Wn tyt j t Yn0||jvs,Jq,Wdt_ |r| ndt_ |r| 0dS)NFrT)rgcZ isenabledZdisablerrrrF Exceptionsys excepthookexc_inforZenable)r$Z reenable_gcrZpendingrrrrrws2   zfinalize._exitfunc)N)r2r3r4r5r6rr itertoolscountrrrrrEr)rrpropertyrrsetterrX classmethodrrrrrrrs.      r)r5_weakrefrrrrrrrr Z _weakrefsetr r rzrrr __all__Setr MutableSetrMutableMappingrrYr rrrrrs  (   6q1