U e5d‹Sã @sÐdZddlmZmZmZmZmZmZmZm Z ddl m Z m Z ddl Z ddlZddlZeefZddddd d d d d ddddg ZGdd„deƒZGdd„de jƒZGdd„deƒZGdd „d e jƒZGdd„dƒZdS)z{Weak reference support for Python. This module is an implementation of PEP 205: http://www.python.org/dev/peps/pep-0205/ é)ÚgetweakrefcountÚ getweakrefsÚrefÚproxyÚCallableProxyTypeÚ ProxyTypeÚ ReferenceTypeÚ_remove_dead_weakref)ÚWeakSetÚ_IterationGuardNrrrrÚWeakKeyDictionaryrrrÚ ProxyTypesÚWeakValueDictionaryr Ú WeakMethodÚfinalizecsDeZdZdZdZd dd„Z‡fdd„Zdd „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(tk r8td t|ƒ¡ƒd‚YnX‡‡fdd„}t |||¡}t||ƒ|_t|ƒ|_ d|_ t|ƒ‰|S)Nz)argument should be a bound method, not {}cs&ˆƒ}|jr"d|_ˆdk r"ˆ|ƒdS©NF)r)ÚargÚself©ÚcallbackZself_wr©ú/usr/lib64/python3.8/weakref.pyÚ_cb3s zWeakMethod.__new__.._cbT) Ú__self__Ú__func__ÚAttributeErrorÚ TypeErrorÚformatÚtyperÚ__new__rrr)ÚclsZmethrÚobjÚfuncrrrrrr#,s  ÿÿ  zWeakMethod.__new__cs2tƒ ¡}| ¡}|dks"|dkr&dS| ||¡S©N)ÚsuperÚ__call__rr)rr%r&©Ú __class__rrr)Bs  zWeakMethod.__call__cCs:t|tƒr6|jr|js||kSt ||¡o4|j|jkSdSr)Ú isinstancerrrÚ__eq__r©rÚotherrrrr-Is   zWeakMethod.__eq__cCs:t|tƒr6|jr|js||k St ||¡p4|j|jkSdS©NT)r,rrrÚ__ne__rr.rrrr1Ps   zWeakMethod.__ne__)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú __slots__r#r)r-r1rÚ__hash__Ú __classcell__rrr*rr$s  c@sÀeZdZdZd,dd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z e Z dd„Zd-dd„Zdd„Zdd„ZeZdd„Zd d!„Zd"d#„Zd$d%„Zd.d&d'„Zd/d(d)„Zd*d+„ZdS)0rz›Mapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists anymore rcKs>t|ƒtfdd„}||_g|_tƒ|_i|_|j|f|ŽdS)NcSs6|ƒ}|dk r2|jr$|j |j¡n||j|jƒdSr')Ú _iteratingÚ_pending_removalsÚappendÚkeyÚdata)ÚwrÚselfrefZ_atomic_removalrrrrÚremovegs z,WeakValueDictionary.__init__..remove)rr Ú_remover:Úsetr9r=Úupdate)rr/Úkwr@rrrÚ__init__fs  zWeakValueDictionary.__init__cCs(|j}|j}|r$| ¡}t||ƒq dSr')r:r=Úpopr )rÚlÚdr<rrrÚ_commit_removalsws z$WeakValueDictionary._commit_removalscCs4|jr| ¡|j|ƒ}|dkr,t|ƒ‚n|SdSr'©r:rIr=ÚKeyError©rr<ÚorrrÚ __getitem__€s   zWeakValueDictionary.__getitem__cCs|jr| ¡|j|=dSr')r:rIr=©rr<rrrÚ __delitem__‰szWeakValueDictionary.__delitem__cCs|jr| ¡t|jƒSr')r:rIÚlenr=©rrrrÚ__len__ŽszWeakValueDictionary.__len__cCs>|jr| ¡z|j|ƒ}Wntk r4YdSX|dk SrrJrLrrrÚ __contains__“sz WeakValueDictionary.__contains__cCsd|jjt|ƒfS©Nz <%s at %#x>©r+r2ÚidrRrrrÚ__repr__œszWeakValueDictionary.__repr__cCs&|jr| ¡t||j|ƒ|j|<dSr')r:rIÚKeyedRefrAr=©rr<ÚvaluerrrÚ __setitem__ŸszWeakValueDictionary.__setitem__c CsV|jr| ¡tƒ}t|ƒ�0|j ¡D]\}}|ƒ}|dk r(|||<q(W5QRX|Sr')r:rIrr r=Úitems)rÚnewr<r>rMrrrÚcopy¤s zWeakValueDictionary.copyc Csjddlm}|jr| ¡| ¡}t|ƒ�6|j ¡D]$\}}|ƒ}|dk r6|||||ƒ<q6W5QRX|S©Nr)Údeepcopy)r_rar:rIr+r r=r])rÚmemorar^r<r>rMrrrÚ __deepcopy__±s  z WeakValueDictionary.__deepcopy__NcCsP|jr| ¡z|j|}Wntk r4|YSX|ƒ}|dkrH|S|SdSr'rJ)rr<Údefaultr>rMrrrÚget½s zWeakValueDictionary.getc csR|jr| ¡t|ƒ�2|j ¡D] \}}|ƒ}|dk r"||fVq"W5QRXdSr'©r:rIr r=r])rÚkr>Úvrrrr]Ìs zWeakValueDictionary.itemsc csJ|jr| ¡t|ƒ�*|j ¡D]\}}|ƒdk r"|Vq"W5QRXdSr'rf)rrgr>rrrÚkeysÕs   zWeakValueDictionary.keysc cs6|jr| ¡t|ƒ�|j ¡EdHW5QRXdS)a�Return 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. N©r:rIr r=ÚvaluesrRrrrÚ itervaluerefsßs  z!WeakValueDictionary.itervaluerefsc csJ|jr| ¡t|ƒ�*|j ¡D]}|ƒ}|dk r"|Vq"W5QRXdSr'rj©rr>r%rrrrkîs zWeakValueDictionary.valuescCs8|jr| ¡|j ¡\}}|ƒ}|dk r||fSqdSr')r:rIr=Úpopitem)rr<r>rMrrrrn÷s zWeakValueDictionary.popitemcGs`|jr| ¡z|j |¡ƒ}Wntk r8d}YnX|dkrX|rN|dSt|ƒ‚n|SdS)Nr)r:rIr=rFrK)rr<ÚargsrMrrrrFs  zWeakValueDictionary.popcCs`z|j|ƒ}Wntk r(d}YnX|dkrX|jr@| ¡t||j|ƒ|j|<|S|SdSr')r=rKr:rIrYrA)rr<rdrMrrrÚ setdefaults zWeakValueDictionary.setdefaultcKsz|jr| ¡|j}|dk rRt|dƒs.t|ƒ}| ¡D]\}}t||j|ƒ||<q6| ¡D]\}}t||j|ƒ||<qZdS©Nr])r:rIr=ÚhasattrÚdictr]rYrA)rr/ÚkwargsrHr<rMrrrrCs zWeakValueDictionary.updatecCs|jr| ¡t|j ¡ƒS)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:rIÚlistr=rkrRrrrÚ valuerefs(s zWeakValueDictionary.valuerefs)r)N)N)N)r2r3r4r5rErIrNrPrSrTrXr\r_Ú__copy__rcrer]riÚ__iter__rlrkrnrFrprCrvrrrrrZs.          cs,eZdZdZdZdd„Z‡fdd„Z‡ZS)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#CszKeyedRef.__new__cstƒ ||¡dSr')r(rE)rrzrr<r*rrrEHszKeyedRef.__init__)r2r3r4r5r6r#rEr8rrr*rrY7s rYc@sÀeZdZdZd+dd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z e Z dd„Zd,dd„Zdd„Zdd„Zdd„ZeZdd „Zd!d"„Zd#d$„Zd%d&„Zd-d'd(„Zd.d)d*„ZdS)/r 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|_|dk rB| |¡dS)NcSs.|ƒ}|dk r*|jr"|j |¡n|j|=dSr')r9r:r;r=)rgr?rrrrr@Ys z*WeakKeyDictionary.__init__..removeF)r=rrAr:rBr9Ú _dirty_lenrC)rrsr@rrrrEWszWeakKeyDictionary.__init__cCs:|j}|j}|r6z|| ¡=Wq tk r2Yq Xq dSr')r:r=rFrK)rrGrHrrrrIhsz"WeakKeyDictionary._commit_removalscs&|j‰‡fdd„|jDƒ|_d|_dS)Ncsg|]}|ˆkr|‘qSrr)Ú.0rg©rHrrÚ wsz5WeakKeyDictionary._scrub_removals..F)r=r:r{rRrr}rÚ_scrub_removalsusz!WeakKeyDictionary._scrub_removalscCsd|_|jt|ƒ=dSr0)r{r=rrOrrrrPzszWeakKeyDictionary.__delitem__cCs|jt|ƒSr')r=rrOrrrrN~szWeakKeyDictionary.__getitem__cCs(|jr|jr| ¡t|jƒt|jƒSr')r{r:rrQr=rRrrrrS�s zWeakKeyDictionary.__len__cCsd|jjt|ƒfSrUrVrRrrrrXˆszWeakKeyDictionary.__repr__cCs||jt||jƒ<dSr')r=rrArZrrrr\‹szWeakKeyDictionary.__setitem__c CsHtƒ}t|ƒ�0|j ¡D]\}}|ƒ}|dk r|||<qW5QRX|Sr')r r r=r])rr^r<r[rMrrrr_Žs zWeakKeyDictionary.copyc Cs\ddlm}| ¡}t|ƒ�6|j ¡D]$\}}|ƒ}|dk r(|||ƒ||<q(W5QRX|Sr`)r_rar+r r=r])rrbrar^r<r[rMrrrrc™s  zWeakKeyDictionary.__deepcopy__cCs|j t|ƒ|¡Sr')r=rer©rr<rdrrrre£szWeakKeyDictionary.getcCs.z t|ƒ}Wntk r"YdSX||jkSr)rr r=)rr<r>rrrrT¦s  zWeakKeyDictionary.__contains__c csDt|ƒ�2|j ¡D] \}}|ƒ}|dk r||fVqW5QRXdSr'©r r=r])rr>r[r<rrrr]­s  zWeakKeyDictionary.itemsc cs8t|ƒ�&|jD]}|ƒ}|dk r|VqW5QRXdSr')r r=rmrrrri´s   zWeakKeyDictionary.keysc cs<t|ƒ�*|j ¡D]\}}|ƒdk r|VqW5QRXdSr'r�)rr>r[rrrrk½s  zWeakKeyDictionary.valuescCs t|jƒS)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=rRrrrÚkeyrefsÃs zWeakKeyDictionary.keyrefscCs0d|_|j ¡\}}|ƒ}|dk r||fSqdSr0)r{r=rn)rr<r[rMrrrrnÏs zWeakKeyDictionary.popitemcGsd|_|jjt|ƒf|žŽSr0)r{r=rFr)rr<rorrrrF×szWeakKeyDictionary.popcCs|j t||jƒ|¡Sr')r=rprrAr€rrrrpÛszWeakKeyDictionary.setdefaultcKs\|j}|dk rFt|dƒs$tiƒ|ƒ}| ¡D]\}}||t||jƒ<q,t|ƒrX| |¡dSrq)r=rrr"r]rrArQrC)rrsrtrHr<r[rrrrCÞs  zWeakKeyDictionary.update)N)N)N)N)r2r3r4r5rErIrrPrNrSrXr\r_rwrcrerTr]rirxrkr‚rnrFrprCrrrrr Ls.      c@s¨eZdZdZdZiZdZe ¡Z dZ dZ Gdd„dƒZ dd„Z de _dd d „Zd d „Zdd„Zedd„ƒZedd„ƒZejdd„ƒZdd„Zedd„ƒZedd„ƒZd S)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&rortÚatexitÚindexN)r2r3r4r6rrrrÚ_Infosr†cOs>t|ƒdkr|^}}}}nª|s(tdƒ‚nœd|krDtdt|ƒdƒ‚| d¡}t|ƒdkr~|^}}}ddl}|jdtdd �nFd |krštdt|ƒdƒ‚| d ¡}|^}}ddl}|jd tdd �t|ƒ}|jsìddl}|  |j ¡d t _|  ¡}t ||ƒ|_||_||_|�pd|_d |_t|jƒ|_||j|<d t _dS) Nézz!<%s object at %#x; for %r at %#x>)r”rerƒr"r2rWr™rrrrXYs  ÿzfinalize.__repr__cCs2dd„|j ¡Dƒ}|jdd„d�dd„|DƒS)NcSsg|]\}}|jr||f‘qSr)r„©r|ÚfÚirrrr~esz-finalize._select_for_exit..cSs |djS)Nrˆ)r…)ÚitemrrrÚfóz+finalize._select_for_exit..rycSsg|] \}}|‘qSrrrŸrrrr~gs)r”r]Úsort)r$ÚLrrrÚ_select_for_exitbszfinalize._select_for_exitcCs°d}z’|jr”ddl}| ¡r(d}| ¡d}|dks:tjrH| ¡}dt_|sNq”|  ¡}z |ƒWn"t k r‚t j t   ¡ŽYnX||jks,t‚q,W5dt_|rª| ¡XdS)NFTr)rr—Zenabler”ÚgcZ isenabledZdisabler•r§rFÚ ExceptionÚsysÚ excepthookÚexc_infoÚAssertionError)r$Z reenable_gcr¨Zpendingr rrrr‘is, zfinalize._exitfunc)N)r2r3r4r5r6r”r—Ú itertoolsÚcountr“r•r�r†rEÚ__text_signature__r)ršr›Úpropertyrœr„ÚsetterrXÚ classmethodr§r‘rrrrrés0*      )r5Ú_weakrefrrrrrrrr Z _weakrefsetr r Ú_collections_abcrªr®r Ú__all__rÚMutableMappingrrYr rrrrrÚs0 ( ý6^