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)argselfcallbackself_wr/usr/lib64/python3.9/weakref.py_cb6s zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsmethrobjfuncrrrrrr$/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__rr0r1rrrr3Ss   zWeakMethod.__ne__)N) __name__ __module__ __qualname____doc__ __slots__r$r+r/r3r__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<setr;r?update)rr2kwrCrrr__init__is  zWeakValueDictionary.__init__cCs>|jj}|j}z |}Wnty,YdS0|||qdSr))r<popr? IndexError)rrBrIdr>rrr_commit_removalszs  z$WeakValueDictionary._commit_removalscCs4|jr||j|}|dur,t|n|SdSr)r<rLr?KeyErrorrr>orrr __getitem__s   zWeakValueDictionary.__getitem__cCs|jr||j|=dSr))r<rLr?rr>rrr __delitem__szWeakValueDictionary.__delitem__cCs|jr|t|jSr))r<rLlenr?rrrr__len__szWeakValueDictionary.__len__cCs<|jr|z|j|}Wnty2YdS0|duSrrMrOrrr __contains__s z WeakValueDictionary.__contains__cCsd|jjt|fSNz <%s at %#x>r-r4idrUrrr__repr__szWeakValueDictionary.__repr__cCs&|jr|t||j||j|<dSr))r<rLKeyedRefrDr?rr>valuerrr __setitem__szWeakValueDictionary.__setitem__cCsj|jr|t}t|:|jD]\}}|}|dur(|||<q(Wdn1s\0Y|Sr))r<rLrr r?items)rnewr>r@rPrrrcopys (zWeakValueDictionary.copycCs~ddlm}|jr||}t|@|jD]$\}}|}|dur6|||||<q6Wdn1sp0Y|SNr)deepcopy)rbrdr<rLr-r r?r`)rmemordrar>r@rPrrr __deepcopy__s  .z WeakValueDictionary.__deepcopy__NcCsN|jr|z|j|}Wnty2|YS0|}|durF|S|SdSr)rM)rr>defaultr@rPrrrgets  zWeakValueDictionary.getccsf|jr|t|<|jD] \}}|}|dur"||fVq"Wdn1sX0YdSr)r<rLr r?r`)rkr@vrrrr`s zWeakValueDictionary.itemsccs^|jr|t|4|jD]\}}|dur"|Vq"Wdn1sP0YdSr)ri)rrjr@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<rLr r?valuesrUrrr itervaluerefss  z!WeakValueDictionary.itervaluerefsccs^|jr|t|4|jD]}|}|dur"|Vq"Wdn1sP0YdSr)rmrr@r'rrrrns zWeakValueDictionary.valuescCs8|jr||j\}}|}|dur||fSqdSr))r<rLr?popitem)rr>r@rPrrrrqs zWeakValueDictionary.popitemcGs^|jr|z|j|}Wnty6d}Yn0|durV|rL|dSt|n|SdS)Nr)r<rLr?rIrN)rr>argsrPrrrrIs   zWeakValueDictionary.popcCs^z|j|}Wnty&d}Yn0|durV|jr>|t||j||j|<|S|SdSr))r?rNr<rLr\rD)rr>rgrPrrr setdefaults  zWeakValueDictionary.setdefaultcKsz|jr||j}|durRt|ds.t|}|D]\}}t||j|||<q6|D]\}}t||j|||<qZdSNr`)r<rLr?hasattrdictr`r\rD)rr2kwargsrKr>rPrrrrF"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<rLlistr?rnrUrrr valuerefs.s zWeakValueDictionary.valuerefscCs|||Sr)rFr1rrr__ior__<s zWeakValueDictionary.__ior__cCs&t|tjr"|}|||StSr)r._collections_abcMappingrbrFr0rr2crrr__or__@s   zWeakValueDictionary.__or__cCs0t|tjr,|}|||||StSr)r.r}r~r-rFr0rrrr__ror__Gs    zWeakValueDictionary.__ror__)r)N)N)N)r4r5r6r7rHr rLrQrSrVrWr[r_rb__copy__rfrhr`rl__iter__rornrqrIrsrFryr{rrrrrrr]s4          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*rH)rrrr>r,rrrHaszKeyedRef.__init__)r4r5r6r7r8r$rHr:rrr,rr\Ps r\c@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))r;r<r=r?rN)rjrArrrrrCrs  z*WeakKeyDictionary.__init__..removeF)r?rrDr<rEr; _dirty_lenrF)rrvrCrrrrHps zWeakKeyDictionary.__init__cCsR|jj}|j}z |}Wnty,YdS0z ||=WqtyJYq0qdSr))r<rIr?rJrN)rrIrKr>rrrrLs    z"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|vr|qSrr).0rjrKrr z5WeakKeyDictionary._scrub_removals..F)r?r<rrUrrr_scrub_removalssz!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dSNT)rr?rrRrrrrSszWeakKeyDictionary.__delitem__cCs|jt|Sr))r?rrRrrrrQszWeakKeyDictionary.__getitem__cCs(|jr|jr|t|jt|jSr))rr<rrTr?rUrrrrVs zWeakKeyDictionary.__len__cCsd|jjt|fSrXrYrUrrrr[szWeakKeyDictionary.__repr__cCs||jt||j<dSr))r?rrDr]rrrr_szWeakKeyDictionary.__setitem__cCs\t}t|:|jD]\}}|}|dur|||<qWdn1sN0Y|Sr))r r r?r`)rrar>r^rPrrrrbs (zWeakKeyDictionary.copycCspddlm}|}t|@|jD]$\}}|}|dur(|||||<q(Wdn1sb0Y|Src)rbrdr-r r?r`)rrerdrar>r^rPrrrrfs  .zWeakKeyDictionary.__deepcopy__cCs|jt||Sr))r?rhrrr>rgrrrrhszWeakKeyDictionary.getcCs,z t|}Wnty YdS0||jvSr)rr!r?)rr>r@rrrrWs   zWeakKeyDictionary.__contains__ccsXt|<|jD] \}}|}|dur||fVqWdn1sJ0YdSr)r r?r`)rr@r^r>rrrr`s  zWeakKeyDictionary.itemsccsLt|0|jD]}|}|dur|VqWdn1s>0YdSr))r r?rprrrrls   zWeakKeyDictionary.keysccsPt|4|jD]\}}|dur|VqWdn1sB0YdSr)r)rr@r^rrrrns  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. )rxr?rUrrrkeyrefss zWeakKeyDictionary.keyrefscCs0d|_|j\}}|}|dur||fSqdSr)rr?rq)rr>r^rPrrrrqs zWeakKeyDictionary.popitemcGsd|_|jjt|g|RSr)rr?rIr)rr>rrrrrrIszWeakKeyDictionary.popcCs|jt||j|Sr))r?rsrrDrrrrrsszWeakKeyDictionary.setdefaultcKs\|j}|durFt|ds$ti|}|D]\}}||t||j<q,t|rX||dSrt)r?rur#r`rrDrTrF)rrvrwrKr>r^rrrrFs  zWeakKeyDictionary.updatecCs|||Sr)rzr1rrrr{ s zWeakKeyDictionary.__ior__cCs&t|tjr"|}|||StSr)r|rrrrr s   zWeakKeyDictionary.__or__cCs0t|tjr,|}|||||StSr)rrrrrrs    zWeakKeyDictionary.__ror__)N)N)N)N)r4r5r6r7rHrLrrSrQrVr[r_rbrrfrhrWr`rlrrnrrqrIrsrFr{rrrrrrr 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(rrrwatexitindexN)r4r5r6r8rrrr_Info6srcOsp|js ddl}||jdt_|}t|||_||_||_ |pFd|_ d|_t |j |_ ||j|<dt_dS)NrT)_registered_with_atexitrregister _exitfuncrrrrr(rrrwnext _index_iterr _registry_dirty)rr'r(rrrwrinforrrrH9s     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)rrI _shutdownr(rrrw)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)rrhrrIr(rrrwrrr'rrrdetachQs  zfinalize.detachcCs:|j|}|o|}|dur6||j|j|jp2ifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rrhrr(rrrwrrrrpeekYs  z finalize.peekcCs ||jvS)zWhether finalizer is alive)rrUrrraliveaszfinalize.alivecCs|j|}t|o|jS)z*Whether finalizer should be called at exitrrhboolr)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>)rrhrr#r4rZrrrrr[rs  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 |Wn tyt j t Yn0||jvs,Jq,Wdt_ |r| ndt_ |r| 0dS)NFrT)rgc isenableddisablerrrrI Exceptionsys excepthookexc_inforenable)r% reenable_gcrpendingrrrrrs2   zfinalize._exitfunc)N)r4r5r6r7r8rr itertoolscountrrrrrHr+rrpropertyrrsetterr[ classmethodrrrrrrrs.      r)r7_weakrefrrrrrrrr _weakrefsetr r r}rrr __all__Setr MutableSetrMutableMappingrr\r rrrrrs  (   6t9