YfO @s(dZddlmZmZmZmZmZmZmZm 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 WeakMethodfinalizecs^eZdZdZdZdddZfd d Zd d Zd dZe j 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 sy|j}|j}Wn0tk rHtdjt|dYnXfdd}tj|||}t|||_t||_ d|_ t||S)Nz)argument should be a bound method, not {}cs5}|jr1d|_dk r1|dS)NF)r)argself)callbackself_wr,/opt/alt/python35/lib64/python3.5/weakref.py_cb3s     zWeakMethod.__new__.._cbT) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsmethrobjfuncrrr)rrrr",s      zWeakMethod.__new__csGtj}|j}|dks3|dkr7dS|j||S)N)super__call__rr)rr%r&) __class__rrr(Bs  zWeakMethod.__call__cCsSt|trO|j s#|j r-||kStj||oN|j|jkSdS)NF) isinstancerrr__eq__r)rotherrrrr+Is  "zWeakMethod.__eq__cCsSt|trO|j s#|j r-||k Stj||pN|j|jkSdS)NT)r*rrr__ne__r)rr,rrrr-Ps  "zWeakMethod.__ne__)rrrr) __name__ __module__ __qualname____doc__ __slots__r"r(r+r-r__hash__rr)r)rr$s   c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ e Z ddZdddZddZddZeZddZdd Zd!d"Zd#d$Zdd%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 cs|std|^}}t|dkrFtdt|t|tfdd}||_g|_t|_i|_|j ||dS)NzGdescriptor '__init__' of 'WeakValueDictionary' object needs an argumentz$expected at most 1 arguments, got %dcsH|}|dk rD|jr4|jj|jn||jdS)N) _iterating_pending_removalsappendkey)wrselfref_atomic_removalr)drrremovems    z,WeakValueDictionary.__init__..remove) rlenrr _remover6setr5dataupdate)argskwrr=r)r<r__init__fs      zWeakValueDictionary.__init__cCs<|j}|j}x#|r7|j}t||qWdS)N)r6rApopr )rlr<r8rrr_commit_removals}s     z$WeakValueDictionary._commit_removalscCsF|jr|j|j|}|dkr>t|n|SdS)N)r6rHrAKeyError)rr8orrr __getitem__s    zWeakValueDictionary.__getitem__cCs!|jr|j|j|=dS)N)r6rHrA)rr8rrr __delitem__s  zWeakValueDictionary.__delitem__cCs |jr|jt|jS)N)r6rHr>rA)rrrr__len__s  zWeakValueDictionary.__len__c CsJ|jr|jy|j|}Wntk r?dSYnX|dk S)NF)r6rHrArI)rr8rJrrr __contains__s    z WeakValueDictionary.__contains__cCsd|jjt|fS)Nz <%s at %#x>)r)r.id)rrrr__repr__szWeakValueDictionary.__repr__cCs3|jr|jt||j||j|r6rHrAhasattrr!rTrQr?rB)rCkwargsrdictr<r8rJrrrrB 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. )r6rHlistrAr_)rrrr valuerefs3s  zWeakValueDictionary.valuerefs)r.r/r0r1rErHrKrLrMrNrPrSrV__copy__rYr[rTr^__iter__r`r_rarFrbrBrgrrrrrZs.            cs:eZdZdZdZddZfddZS)rQa[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. r8cCs"tj|||}||_|S)N)rr"r8)r!obrr8rrrrr"Ns zKeyedRef.__new__cstj||dS)N)r'rE)rrjrr8)r)rrrESszKeyedRef.__init__)r8)r.r/r0r1r2r"rErr)r)rrQBs  rQc@seZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ e Z ddZdddZddZddZddZeZdd Zd!d"Zd#d$Zd%d&Zdd'd(Zdd)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. NcCsbi|_t|dd}||_g|_t|_d|_|dk r^|j|dS)NcSs?|}|dk r;|jr1|jj|n |j|=dS)N)r5r6r7rA)r\r:rrrrr=ds    z*WeakKeyDictionary.__init__..removeF)rArr?r6r@r5 _dirty_lenrB)rrer=rrrrEbs      zWeakKeyDictionary.__init__c CsI|j}|j}x0|rDy||j=Wqtk r@YqXqWdS)N)r6rArFrI)rrGr<rrrrHss    z"WeakKeyDictionary._commit_removalscs5|jfdd|jD|_d|_dS)Ncs"g|]}|kr|qSrr).0r\)r<rr s z5WeakKeyDictionary._scrub_removals..F)rAr6rk)rr)r<r_scrub_removalss z!WeakKeyDictionary._scrub_removalscCsd|_|jt|=dS)NT)rkrAr)rr8rrrrLs zWeakKeyDictionary.__delitem__cCs|jt|S)N)rAr)rr8rrrrKszWeakKeyDictionary.__getitem__cCs6|jr|jr|jt|jt|jS)N)rkr6rnr>rA)rrrrrMs zWeakKeyDictionary.__len__cCsd|jjt|fS)Nz <%s at %#x>)r)r.rO)rrrrrPszWeakKeyDictionary.__repr__cCs||jt||jrB)rrerdr<r8rRrrrrBs   zWeakKeyDictionary.update)r.r/r0r1rErHrnrLrKrMrPrSrVrhrYr[rNrTr^rir_rorarFrbrBrrrrr 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&rCrdatexitindexN)rpr&rCrdrqrr)r.r/r0r2rrrr_Info s rscOs|js.ddl}|j|jdt_|j}t|||_||_||_ |pgd|_ d|_t |j |_ ||j|z!<%s object at %#x; for %r at %#x>)ryr[rpr!r.rO)rr{r%rrrrPGs  zfinalize.__repr__cCsCdd|jjD}|jddddd|DS)NcSs+g|]!\}}|jr||fqSr)rq)rlfirrrrmSs z-finalize._select_for_exit..r8cSs |djS)Nr4)rr)itemrrrTsz+finalize._select_for_exit..cSsg|]\}}|qSrr)rlrrrrrrmUs )ryrTsort)r#Lrrr_select_for_exitPszfinalize._select_for_exitcCsd}z|jrddl}|jr:d}|jd}x|dksXtjrm|j}dt_|stP|j}y |Wn%tk rt j t j YnX||jksCt qCWWddt_ |r|jXdS)NFrT)rygc isenableddisablerrzrrF Exceptionsys excepthookexc_infoAssertionErrorr|enable)r# reenable_gcrpendingrrrrrvWs.          zfinalize._exitfunc)r.r/r0r1r2ryr| itertoolscountrxrzrtrsrEr(r~rpropertyrrqsetterrP classmethodrrvrrrrrs$      )r1_weakrefrrrrrrrr _weakrefsetr r collectionsrrr __all__rMutableMappingrrQr rrrrrs:        6