U e5dS @sdZddlmZmZmZmZmZmZmZm Z ddl m Z m Z ddl Z ddlZddlZeefZddddd d d d d ddddg ZGdddeZGddde jZGdddeZGdd 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_IterationGuardNrrrrWeakKeyDictionaryrrr 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(tk r8tdt|dYnXfdd}t|||}t|||_t||_ d|_ t||S)Nz)argument should be a bound method, not {}cs&}|jr"d|_dk r"|dSNF)r)argselfcallbackZself_wr/usr/lib64/python3.8/weakref.py_cb3s zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsZmethrobjfuncrrrrrr#,s    zWeakMethod.__new__cs2t}|}|dks"|dkr&dS|||SN)super__call__rr)rr%r& __class__rrr)Bs  zWeakMethod.__call__cCs:t|tr6|jr|js||kSt||o4|j|jkSdSr) isinstancerrr__eq__rrotherrrrr-Is   zWeakMethod.__eq__cCs:t|tr6|jr|js||k St||p4|j|jkSdSNT)r,rrr__ne__rr.rrrr1Ps   zWeakMethod.__ne__)N) __name__ __module__ __qualname____doc__ __slots__r#r)r-r1r__hash__ __classcell__rrr*rr$s  c@seZdZdZd,ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZd-ddZddZddZeZddZd d!Zd"d#Zd$d%Zd.d&d'Zd/d(d)Zd*d+ZdS)0rzMapping 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|jn||j|jdSr') _iterating_pending_removalsappendkeydata)wrselfrefZ_atomic_removalrrrrremovegs 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 )rldr<rrr_commit_removalsws z$WeakValueDictionary._commit_removalscCs4|jr||j|}|dkr,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|}Wntk r4YdSX|dk SrrJrLrrr __contains__sz 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__c CsV|jr|t}t|0|jD]\}}|}|dk r(|||<q(W5QRX|Sr')r:rIrr r=items)rnewr<r>rMrrrcopys zWeakValueDictionary.copyc Csjddlm}|jr||}t|6|jD]$\}}|}|dk r6|||||<q6W5QRX|SNr)deepcopy)r_rar:rIr+r r=r])rmemorar^r<r>rMrrr __deepcopy__s  z WeakValueDictionary.__deepcopy__NcCsP|jr|z|j|}Wntk r4|YSX|}|dkrH|S|SdSr'rJ)rr<defaultr>rMrrrgets zWeakValueDictionary.getc csR|jr|t|2|jD] \}}|}|dk r"||fVq"W5QRXdSr'r:rIr r=r])rkr>vrrrr]s zWeakValueDictionary.itemsc csJ|jr|t|*|jD]\}}|dk r"|Vq"W5QRXdSr'rf)rrgr>rrrkeyss   zWeakValueDictionary.keysc cs6|jr|t||jEdHW5QRXdS)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.itervaluerefsc csJ|jr|t|*|jD]}|}|dk r"|Vq"W5QRXdSr'rjrr>r%rrrrks zWeakValueDictionary.valuescCs8|jr||j\}}|}|dk r||fSqdSr')r:rIr=popitem)rr<r>rMrrrrns 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|ds.t|}|D]\}}t||j|||<q6|D]\}}t||j|||<qZdSNr])r:rIr=hasattrdictr]rYrA)rr/kwargsrHr<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.valuerefs)r)N)N)N)r2r3r4r5rErIrNrPrSrTrXr\r___copy__rcrer]ri__iter__rlrkrnrFrprCrvrrrrrZs.          cs,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#CszKeyedRef.__new__cst||dSr')r(rE)rrzrr<r*rrrEHszKeyedRef.__init__)r2r3r4r5r6r#rEr8rrr*rrY7s rYc@seZdZdZd+ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZd,ddZddZddZddZeZdd 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&|jfdd|jD|_d|_dS)Ncsg|]}|kr|qSrr).0rgrHrr 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|jt|jSr')r{r:rrQr=rRrrrrSs zWeakKeyDictionary.__len__cCsd|jjt|fSrUrVrRrrrrXszWeakKeyDictionary.__repr__cCs||jt||j<dSr')r=rrArZrrrr\szWeakKeyDictionary.__setitem__c CsHt}t|0|jD]\}}|}|dk r|||<qW5QRX|Sr')r r r=r])rr^r<r[rMrrrr_s zWeakKeyDictionary.copyc Cs\ddlm}|}t|6|jD]$\}}|}|dk r(|||||<q(W5QRX|Sr`)r_rar+r r=r])rrbrar^r<r[rMrrrrcs  zWeakKeyDictionary.__deepcopy__cCs|jt||Sr')r=rerrr<rdrrrreszWeakKeyDictionary.getcCs.z t|}Wntk r"YdSX||jkSr)rr r=)rr<r>rrrrTs  zWeakKeyDictionary.__contains__c csDt|2|jD] \}}|}|dk r||fVqW5QRXdSr'r r=r])rr>r[r<rrrr]s  zWeakKeyDictionary.itemsc cs8t|&|jD]}|}|dk r|VqW5QRXdSr')r r=rmrrrris   zWeakKeyDictionary.keysc cs<t|*|jD]\}}|dk r|VqW5QRXdSr'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\}}|}|dk r||fSqdSr0)r{r=rn)rr<r[rMrrrrns zWeakKeyDictionary.popitemcGsd|_|jjt|f|Sr0)r{r=rFr)rr<rorrrrFszWeakKeyDictionary.popcCs|jt||j|Sr')r=rprrArrrrrpszWeakKeyDictionary.setdefaultcKs\|j}|dk rFt|ds$ti|}|D]\}}||t||j<q,t|rX||dSrq)r=rrr"r]rrArQrC)rrsrtrHr<r[rrrrCs  zWeakKeyDictionary.update)N)N)N)N)r2r3r4r5rErIrrPrNrSrXr\r_rwrcrerTr]rirxrkrrnrFrprCrrrrr Ls.      c@seZdZdZdZiZdZeZ dZ dZ GdddZ ddZ de _dd d Zd d ZddZeddZeddZejddZddZeddZeddZd 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&rortatexitindexN)r2r3r4r6rrrr_InfosrcOs>t|dkr|^}}}}n|s(tdnd|krDtdt|d|d}t|dkr~|^}}}ddl}|jdtdd nFd |krtdt|d|d }|^}}ddl}|jd tdd t|}|jsddl}| |j d t _| }t |||_||_||_|pd|_d |_t|j|_||j|<d t _dS) Nzz!<%s object at %#x; for %r at %#x>)rrerr"r2rWrrrrrXYs  zfinalize.__repr__cCs2dd|jD}|jddddd|DS)NcSsg|]\}}|jr||fqSr)rr|firrrr~esz-finalize._select_for_exit..cSs |djS)Nr)r)itemrrrfz+finalize._select_for_exit..rycSsg|] \}}|qSrrrrrrr~gs)rr]sort)r$Lrrr_select_for_exitbszfinalize._select_for_exitcCsd}z|jrddl}|r(d}|d}|dks:tjrH|}dt_|sNq| }z |Wn"t k rt j t YnX||jks,tq,W5dt_|r|XdS)NFTr)rrZenablergcZ isenabledZdisablerrrF Exceptionsys excepthookexc_infoAssertionError)r$Z reenable_gcrZpendingrrrrris, zfinalize._exitfunc)N)r2r3r4r5r6rr itertoolscountrrrrrE__text_signature__r)rrpropertyrrsetterrX classmethodrrrrrrrs0*      )r5_weakrefrrrrrrrr Z _weakrefsetr r _collections_abcrrr __all__rMutableMappingrrYr rrrrrs0 ( 6^