3 \O @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"|dS)NF)r)argself)callbackself_wr/usr/lib64/python3.6/weakref.py_cb3s zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsmethrobjfuncrrr)rrrr",s   zWeakMethod.__new__cs2tj}|j}|dks"|dkr&dS|j||S)N)super__call__rr)rr%r&) __class__rrr(Bs  zWeakMethod.__call__cCs>t|tr:|j s|j r"||kStj||o8|j|jkSdS)NF) isinstancerrr__eq__r)rotherrrrr+Is  zWeakMethod.__eq__cCs>t|tr:|j s|j r"||k Stj||p8|j|jkSdS)NT)r*rrr__ne__r)rr,rrrr-Ps  zWeakMethod.__ne__)rrrr)N) __name__ __module__ __qualname____doc__ __slots__r"r(r+r-r__hash__ __classcell__rr)r)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)-rzMapping 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 argumentz$expected at most 1 arguments, got %dcs4|}|dk r0|jr$|jj|jn ||jdS)N) _iterating_pending_removalsappendkey)wrselfref_atomic_removalr)drrremovems z,WeakValueDictionary.__init__..remove) rlenrr _remover7setr6dataupdate)argskwrr>r)r=r__init__fs   zWeakValueDictionary.__init__cCs,|j}|j}x|r&|j}t||qWdS)N)r7rBpopr )rlr=r9rrr_commit_removals}s z$WeakValueDictionary._commit_removalscCs4|jr|j|j|}|dkr,t|n|SdS)N)r7rIrBKeyError)rr9orrr __getitem__s   zWeakValueDictionary.__getitem__cCs|jr|j|j|=dS)N)r7rIrB)rr9rrr __delitem__szWeakValueDictionary.__delitem__cCs|jr|jt|jS)N)r7rIr?rB)rrrr__len__szWeakValueDictionary.__len__c Cs<|jr|jy|j|}Wntk r2dSX|dk S)NF)r7rIrBrJ)rr9rKrrr __contains__sz WeakValueDictionary.__contains__cCsd|jjt|fS)Nz <%s at %#x>)r)r.id)rrrr__repr__szWeakValueDictionary.__repr__cCs&|jr|jt||j||j|<dS)N)r7rIKeyedRefr@rB)rr9valuerrr __setitem__szWeakValueDictionary.__setitem__cCsF|jr|jt}x,|jjD]\}}|}|dk r |||<q W|S)N)r7rIrrBitems)rnewr9r:rKrrrcopys zWeakValueDictionary.copycCsZddlm}|jr|j|j}x2|jjD]$\}}|}|dk r.|||||<q.W|S)Nr)deepcopy)rWrXr7rIr)rBrU)rmemorXrVr9r:rKrrr __deepcopy__s z WeakValueDictionary.__deepcopy__Nc CsL|jr|jy|j|}Wntk r0|SX|}|dkrD|S|SdS)N)r7rIrBrJ)rr9defaultr:rKrrrgetszWeakValueDictionary.getc csV|jr|jt|6x.|jjD] \}}|}|dk r$||fVq$WWdQRXdS)N)r7rIr rBrU)rkr:vrrrrUs zWeakValueDictionary.itemsc csN|jr|jt|.x&|jjD]\}}|dk r$|Vq$WWdQRXdS)N)r7rIr rBrU)rr]r:rrrkeyss   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. N)r7rIr rBvalues)rrrr itervaluerefss  z!WeakValueDictionary.itervaluerefsc csN|jr|jt|.x&|jjD]}|}|dk r$|Vq$WWdQRXdS)N)r7rIr rBr`)rr:r%rrrr`s zWeakValueDictionary.valuescCs<|jr|jx(|jj\}}|}|dk r||fSqWdS)N)r7rIrBpopitem)rr9r:rKrrrrbszWeakValueDictionary.popitemc Gs`|jr|jy|jj|}Wntk r8d}YnX|dkrX|rN|dSt|n|SdS)Nr)r7rIrBrGrJ)rr9rDrKrrrrGs  zWeakValueDictionary.popc Cs`y|j|}Wntk r(d}YnX|dkrX|jr@|jt||j||j|<|S|SdS)N)rBrJr7rIrRr@)rr9r[rKrrr 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 argumentr5z$expected at most 1 arguments, got %drrU) rr?r7rIrBhasattrr!rUrRr@rC)rDkwargsrdictr=r9rKrrrrC 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. )r7rIlistrBr`)rrrr valuerefs3s zWeakValueDictionary.valuerefs)N)N)r.r/r0r1rFrIrLrMrNrOrQrTrW__copy__rZr\rUr___iter__rar`rbrGrcrCrhrrrrrZs.         cs,eZdZdZdZddZfddZZS)rRa[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. r9cCstj|||}||_|S)N)rr"r9)r!obrr9rrrrr"NszKeyedRef.__new__cstj||dS)N)r'rF)rrkrr9)r)rrrFSszKeyedRef.__init__)r9)r.r/r0r1r2r"rFr4rr)r)rrRBsrRc@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|=dS)N)r6r7r8rB)r]r;rrrrr>ds z*WeakKeyDictionary.__init__..removeF)rBrr@r7rAr6 _dirty_lenrC)rrfr>rrrrFbszWeakKeyDictionary.__init__c Cs>|j}|j}x,|r8y||j=Wqtk r4YqXqWdS)N)r7rBrGrJ)rrHr=rrrrIssz"WeakKeyDictionary._commit_removalscs&|jfdd|jD|_d|_dS)Ncsg|]}|kr|qSrr).0r])r=rr sz5WeakKeyDictionary._scrub_removals..F)rBr7rl)rr)r=r_scrub_removalssz!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dS)NT)rlrBr)rr9rrrrMszWeakKeyDictionary.__delitem__cCs|jt|S)N)rBr)rr9rrrrLszWeakKeyDictionary.__getitem__cCs(|jr|jr|jt|jt|jS)N)rlr7ror?rB)rrrrrNs zWeakKeyDictionary.__len__cCsd|jjt|fS)Nz <%s at %#x>)r)r.rP)rrrrrQszWeakKeyDictionary.__repr__cCs||jt||j<dS)N)rBrr@)rr9rSrrrrTszWeakKeyDictionary.__setitem__cCs8t}x,|jjD]\}}|}|dk r|||<qW|S)N)r rBrU)rrVr9rSrKrrrrWs  zWeakKeyDictionary.copycCsLddlm}|j}x2|jjD]$\}}|}|dk r |||||<q W|S)Nr)rX)rWrXr)rBrU)rrYrXrVr9rSrKrrrrZs zWeakKeyDictionary.__deepcopy__cCs|jjt||S)N)rBr\r)rr9r[rrrr\szWeakKeyDictionary.getc Cs,y t|}Wntk r dSX||jkS)NF)rrrB)rr9r:rrrrOs  zWeakKeyDictionary.__contains__c csHt|6x.|jjD] \}}|}|dk r||fVqWWdQRXdS)N)r rBrU)rr:rSr9rrrrUs  zWeakKeyDictionary.itemsc cs<t|*x"|jD]}|}|dk r|VqWWdQRXdS)N)r rB)rr:r%rrrr_s   zWeakKeyDictionary.keysc cs@t|.x&|jjD]\}}|dk r|VqWWdQRXdS)N)r rBrU)rr:rSrrrr`s  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. )rgrB)rrrrkeyrefss zWeakKeyDictionary.keyrefscCs4d|_x(|jj\}}|}|dk r||fSqWdS)NT)rlrBrb)rr9rSrKrrrrbs zWeakKeyDictionary.popitemcGsd|_|jjt|f|S)NT)rlrBrGr)rr9rDrrrrGszWeakKeyDictionary.popcCs|jjt||j|S)N)rBrcrr@)rr9r[rrrrcszWeakKeyDictionary.setdefaultcKs`|j}|dk rJt|ds$ti|}x$|jD]\}}||t||j<q.Wt|r\|j|dS)NrU)rBrdr!rUrr@r?rC)rrfrer=r9rSrrrrCs  zWeakKeyDictionary.update)N)N)N)N)r.r/r0r1rFrIrorMrLrNrQrTrWrirZr\rOrUr_rjr`rprbrGrcrCrrrrr 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&rDreatexitindexN)rqr&rDrerrrs)r.r/r0r2rrrr_Info srtcOsp|js ddl}|j|jdt_|j}t|||_||_||_ |pFd|_ d|_t |j |_ ||j|<dt_dS)NrT)_registered_with_atexitrrregister _exitfuncrrtrrqr&rDrenext _index_iterrs _registry_dirty)rr%r&rDrerrinforrrrFs     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)rzrG _shutdownr&rDre)r_r|rrrr(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)rzr\rqrGr&rDre)rr|r%rrrdetach&s  zfinalize.detachcCs:|jj|}|o|j}|dk r6||j|j|jp2ifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rzr\rqr&rDre)rr|r%rrrpeek.s  z finalize.peekcCs ||jkS)zWhether finalizer is alive)rz)rrrralive6szfinalize.alivecCs|jj|}t|o|jS)z*Whether finalizer should be called at exit)rzr\boolrr)rr|rrrrr;s zfinalize.atexitcCs|jj|}|rt||_dS)N)rzr\rrr)rrSr|rrrrrAs 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>)rzr\rqr!r.rP)rr|r%rrrrQGs   zfinalize.__repr__cCs2dd|jjD}|jddddd|DS)NcSsg|]\}}|jr||fqSr)rr)rmfirrrrnSsz-finalize._select_for_exit..cSs |djS)Nr5)rs)itemrrrTsz+finalize._select_for_exit..)r9cSsg|] \}}|qSrr)rmrrrrrrnUs)rzrUsort)r#Lrrr_select_for_exitPszfinalize._select_for_exitcCsd}z|jrddl}|jr(d}|jd}x\|dkss"(  6i