3 ^Oc @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 WeakMethodfinalizecsDeZdZdZdZdddZfd d Zd d Zd dZe 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__Nc s~y|j}|j}Wn(tk r8tdjt|dYnXfdd}tj|||}t|||_t||_ d|_ t||S)Nz)argument should be a bound method, not {}cs&}|jr"d|_dk r"|dSNF)r)ZargselfcallbackZself_wr/usr/lib64/python3.6/weakref.py_cb3s zWeakMethod.__new__.._cbT) Z__self__Z__func__ZAttributeError TypeErrorZformattyper__new__rrr)clsZmethrobjfuncrrrrrr,s   zWeakMethod.__new__cs2tj}|j}|dks"|dkr&dS|j||SN)super__call__rr)rrr  __class__rrr#Bs  zWeakMethod.__call__cCs>t|tr:|j s|j r"||kStj||o8|j|jkSdSr) isinstancerrr__eq__rrZotherrrrr'I  zWeakMethod.__eq__cCs>t|tr:|j s|j r"||k Stj||p8|j|jkSdSNT)r&rrr__ne__rr(rrrr+Pr)zWeakMethod.__ne__)rrrrr!) __name__ __module__ __qualname____doc__ __slots__rr#r'r+rZ__hash__ __classcell__rrr$rr$s  c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ e Z ddZd+ddZddZddZeZddZdd Zd!d"Zd#d$Zd,d%d&Zd'd(Zd)d*ZdS)-r zMapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists anymore cst|s td|^}}t|dkr0tdt|t|tffdd }||_g|_t|_i|_|j ||dS)NzGdescriptor '__init__' of 'WeakValueDictionary' object needs an argument$expected at most 1 arguments, got %dcs4|}|dk r0|jr$|jj|jn ||jdSr!) _iterating_pending_removalsappendkey)wrselfrefZ_atomic_removalrdrrremovems z,WeakValueDictionary.__init__..remove) rlenrr_remover5setr4dataupdate)argsZkwrr<rr:r__init__fs   zWeakValueDictionary.__init__cCs,|j}|j}x|r&|j}t||qWdSr!)r5r@popr)rlr;r7rrr_commit_removals}s z$WeakValueDictionary._commit_removalscCs4|jr|j|j|}|dkr,t|n|SdSr!r5rFr@KeyErrorrr7orrr __getitem__s   zWeakValueDictionary.__getitem__cCs|jr|j|j|=dSr!)r5rFr@rr7rrr __delitem__zWeakValueDictionary.__delitem__cCs|jr|jt|jSr!)r5rFr=r@rrrr__len__rNzWeakValueDictionary.__len__c Cs<|jr|jy|j|}Wntk r2dSX|dk SrrGrIrrr __contains__sz WeakValueDictionary.__contains__cCd|jjt|fSNz <%s at %#x>r%r,idrOrrr__repr__zWeakValueDictionary.__repr__cCs&|jr|jt||j||j|<dSr!)r5rFKeyedRefr>r@rr7valuerrr __setitem__rNzWeakValueDictionary.__setitem__cCsF|jr|jt}x,|jjD]\}}|}|dk r |||<q W|Sr!)r5rFr r@items)rnewr7r8rJrrrcopys zWeakValueDictionary.copycCsZddlm}|jr|j|j}x2|jjD]$\}}|}|dk r.|||||<q.W|SNr)deepcopy)r^r`r5rFr%r@r\)rmemor`r]r7r8rJrrr __deepcopy__s z WeakValueDictionary.__deepcopy__Nc CsL|jr|jy|j|}Wntk r0|SX|}|dkrD|S|SdSr!rG)rr7defaultr8rJrrrgetszWeakValueDictionary.getc csV|jr|jt|6x.|jjD] \}}|}|dk r$||fVq$WWdQRXdSr!r5rFr r@r\)rkr8Zvrrrr\s zWeakValueDictionary.itemsc csN|jr|jt|.x&|jjD]\}}|dk r$|Vq$WWdQRXdSr!re)rrfr8rrrkeyss   zWeakValueDictionary.keysc cs6|jr|jt||jjEdHWdQRXdS)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. Nr5rFr r@valuesrOrrr itervaluerefss  z!WeakValueDictionary.itervaluerefsc csN|jr|jt|.x&|jjD]}|}|dk r$|Vq$WWdQRXdSr!rhrr8rrrrris zWeakValueDictionary.valuescCs<|jr|jx(|jj\}}|}|dk r||fSqWdSr!)r5rFr@popitem)rr7r8rJrrrrlszWeakValueDictionary.popitemc Gs`|jr|jy|jj|}Wntk r8d}YnX|dkrX|rN|dSt|n|SdS)Nr)r5rFr@rDrH)rr7rBrJrrrrDs  zWeakValueDictionary.popc Cs`y|j|}Wntk r(d}YnX|dkrX|jr@|jt||j||j|<|S|SdSr!)r@rHr5rFrXr>)rr7rcrJrrr setdefaults zWeakValueDictionary.setdefaultcOs|s td|^}}t|dkr0tdt||r<|dnd}|jrN|j|j}|dk rt|dsrti|}x&|jD]\}}t||j |||<q|Wt|r|j |dS)NzEdescriptor 'update' of 'WeakValueDictionary' object needs an argumentr2r3rr\) rr=r5rFr@hasattrrr\rXr>rA)rBkwargsrdictr;r7rJrrrrA s    zWeakValueDictionary.updatecCs|jr|jt|jjS)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. )r5rFlistr@rirOrrr valuerefs3s zWeakValueDictionary.valuerefsr!r!)r,r-r.r/rCrFrKrMrPrQrVr[r^__copy__rbrdr\rg__iter__rjrirlrDrmrArrrrrrr Zs.         cs,eZdZdZdZddZfddZZS)rXa[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. r7cCstj|||}||_|Sr!)rrr7)robrr7rrrrrNszKeyedRef.__new__cstj||dSr!)r"rC)rrurr7r$rrrCSrWzKeyedRef.__init__r7)r,r-r.r/r0rrCr1rrr$rrXBsrXc@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|j|dS)NcSs.|}|dk r*|jr"|jj|n|j|=dSr!)r4r5r6r@)rfr9rrrrr<ds z*WeakKeyDictionary.__init__..removeF)r@rr>r5r?r4 _dirty_lenrA)rrpr<rrrrCbszWeakKeyDictionary.__init__c Cs>|j}|j}x,|r8y||j=Wqtk r4YqXqWdSr!)r5r@rDrH)rrEr;rrrrFssz"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|kr|qSrr).0rfr:rr z5WeakKeyDictionary._scrub_removals..F)r@r5rwrOrr:r_scrub_removalssz!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dSr*)rwr@rrLrrrrMzWeakKeyDictionary.__delitem__cCs|jt|Sr!)r@rrLrrrrKrWzWeakKeyDictionary.__getitem__cCs(|jr|jr|jt|jt|jSr!)rwr5r{r=r@rOrrrrPs zWeakKeyDictionary.__len__cCrRrSrTrOrrrrVrWzWeakKeyDictionary.__repr__cCs||jt||j<dSr!)r@rr>rYrrrr[rWzWeakKeyDictionary.__setitem__cCs8t}x,|jjD]\}}|}|dk r|||<qW|Sr!)r r@r\)rr]r7rZrJrrrr^s  zWeakKeyDictionary.copycCsLddlm}|j}x2|jjD]$\}}|}|dk r |||||<q W|Sr_)r^r`r%r@r\)rrar`r]r7rZrJrrrrbs zWeakKeyDictionary.__deepcopy__cCs|jjt||Sr!)r@rdrrr7rcrrrrdrWzWeakKeyDictionary.getc Cs,y t|}Wntk r dSX||jkSr)rrr@)rr7r8rrrrQs  zWeakKeyDictionary.__contains__c csHt|6x.|jjD] \}}|}|dk r||fVqWWdQRXdSr!r r@r\)rr8rZr7rrrr\s  zWeakKeyDictionary.itemsc cs<t|*x"|jD]}|}|dk r|VqWWdQRXdSr!)r r@rkrrrrgs   zWeakKeyDictionary.keysc cs@t|.x&|jjD]\}}|dk r|VqWWdQRXdSr!r~)rr8rZrrrris  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. )rqr@rOrrrkeyrefss zWeakKeyDictionary.keyrefscCs4d|_x(|jj\}}|}|dk r||fSqWdSr*)rwr@rl)rr7rZrJrrrrls zWeakKeyDictionary.popitemcGsd|_|jjt|f|Sr*)rwr@rDr)rr7rBrrrrDr|zWeakKeyDictionary.popcCs|jjt||j|Sr!)r@rmrr>r}rrrrmrWzWeakKeyDictionary.setdefaultcKs`|j}|dk rJt|ds$ti|}x$|jD]\}}||t||j<q.Wt|r\|j|dS)Nr\)r@rnrr\rr>r=rA)rrpror;r7rZrrrrAs  zWeakKeyDictionary.updater!r!r!r!)r,r-r.r/rCrFr{rMrKrPrVr[r^rsrbrdrQr\rgrtrirrlrDrmrArrrrr Ws.      c@seZdZdZfZiZdZejZ dZ dZ GdddZ ddZ ddd Zd d Zd d ZeddZeddZejddZddZeddZeddZdS)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. Fc@seZdZdZdS) zfinalize._Infoweakrefr rBroatexitindexN)rr rBrorr)r,r-r.r0rrrr_Info srcOsp|js ddl}|j|jdt_|j}t|||_||_||_ |pFd|_ d|_t |j |_ ||j|<dt_dS)NrT)_registered_with_atexitrZregister _exitfuncrrrrr rBroZnext _index_iterr _registry_dirty)rrr rBrorinforrrrCs     zfinalize.__init__NcCs2|jj|d}|r.|j r.|j|j|jp*iSdS)zZIf alive then mark as dead and return func(*args, **kwargs); otherwise return NoneN)rrD _shutdownr rBro)rZ_rrrrr#s zfinalize.__call__cCsH|jj|}|o|j}|dk rD|jj|drD||j|j|jp@ifSdS)z^If alive then mark as dead and return (obj, func, args, kwargs); otherwise return NoneN)rrdrrDr rBrorrrrrrdetach&s  zfinalize.detachcCs:|jj|}|o|j}|dk r6||j|j|jp2ifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rrdrr rBrorrrrpeek.s  z finalize.peekcCs ||jkS)zWhether finalizer is alive)rrOrrralive6szfinalize.alivecCs|jj|}t|o|jS)z*Whether finalizer should be called at exitrrdZboolr)rrrrrr;s zfinalize.atexitcCs|jj|}|rt||_dSr!r)rrZrrrrrAs cCs^|jj|}|o|j}|dkr6dt|jt|fSdt|jt|t|jt|fSdS)Nz<%s object at %#x; dead>z!<%s object at %#x; for %r at %#x>)rrdrrr,rUrrrrrVGs   zfinalize.__repr__cCs2dd|jjD}|jddddd|DS)NcSsg|]\}}|jr||fqSr)rrxfZirrrrySrzz-finalize._select_for_exit..cSs |djS)Nr2)r)ZitemrrrZTsz+finalize._select_for_exit..rvcSsg|] \}}|qSrrrrrrryUrz)rr\Zsort)rZLrrr_select_for_exitPszfinalize._select_for_exitcCsd}z|jrddl}|jr(d}|jd}xj|dkss"(  6i