\d T<dZddlmZmZmZmZmZmZmZm Z ddl m Z m Z ddl Z ddlZddlZeefZgdZe je GddeZGdd e jZGd d eZGd d e jZGddZdS)ztWeak reference support for Python. This module is an implementation of PEP 205: https://peps.python.org/pep-0205/ )getweakrefcount getweakrefsrefproxyCallableProxyType ProxyType ReferenceType_remove_dead_weakref)WeakSet_IterationGuardN) rrrrWeakKeyDictionaryr rr ProxyTypesWeakValueDictionaryr WeakMethodfinalizecHeZdZdZdZddZfdZdZdZe j Z xZ 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\ |j}|j}n>#t$r1tdt |dwxYwfd}t j|||}t |||_t ||_ d|_ t ||S)Nz)argument should be a bound method, not {}cZ}|jrd|_|dSdSdSNF)r)argselfcallbackself_wrs ..\python\lib\weakref.py_cbzWeakMethod.__new__.._cb5sH799D{ ## 'HTNNNNN # #''T) __self____func__AttributeError TypeErrorformattyper__new__rrr)clsmethrobjfuncrrrs ` @rr'zWeakMethod.__new__.s ;-C=DD ; ; ;G#VDJJ//116: ; ; # # # # # #{3S))T3t** d)) s ;Act}|}||dS|||SN)super__call__rr)rr*r+ __class__s rr/zWeakMethod.__call__DsIgg  ~~ ;$,4tS)))r ct|tr7|jr|js||uStj||o|j|jkSt Sr-) isinstancerrr__eq__rNotImplementedrothers rr3zWeakMethod.__eq__KsX eZ ( ( Q; %el %u}$:dE**Pt~/P Pr ct|tr7|jr|js||uStj||p|j|jkSt Sr-)r2rrr__ne__rr4r5s rr8zWeakMethod.__ne__RsY eZ ( ( P; )el )5((:dE**Odn.O Or r-) __name__ __module__ __qualname____doc__ __slots__r'r/r3r8r__hash__ __classcell__r0s@rrr&sy CI,*****|HHHHHr rceZdZdZddZefdZdZdZdZ dZ d Z d Z d Z e Zd ZddZdZdZeZdZdZdZdZddZddZdZdZdZdZd S)rzMapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists anymore c t|tfd}||_g|_t |_i|_|j|fi|dS)Nc|}|@|jr!|j|jdS||j|jdSdSr-) _iterating_pending_removalsappendkeydata)wrselfref_atomic_removalrs rremovez,WeakValueDictionary.__init__..removeisb799D?7*11"&99999$ODIrv66666  r )rr _removerFsetrErIupdate)rr6kwrMs r__init__zWeakValueDictionary.__init__hse"4yy:N 7 7 7 7 !#%%  E  R     r c~|jj}|j} |}n#t$rYdSwxYw|||*r-)rFpoprI IndexError)rrLrTdrHs r_commit_removalsz$WeakValueDictionary._commit_removalsysd$( I $ cee     OAs # # #  $s ! //c|jr||j|}|t||Sr-rFrWrIKeyErrorrrHos r __getitem__zWeakValueDictionary.__getitem__sH  ! $  ! ! # # # DIcN   93-- Hr cL|jr||j|=dSr-)rFrWrIrrHs r __delitem__zWeakValueDictionary.__delitem__s,  ! $  ! ! # # # IcNNNr c`|jr|t|jSr-)rFrWlenrIrs r__len__zWeakValueDictionary.__len__s-  ! $  ! ! # # #49~~r c|jr| |j|}n#t$rYdSwxYw|duSrrYr[s r __contains__z WeakValueDictionary.__contains__se  ! $  ! ! # # #  #  AA   55 }s3 AAc>d|jjt|fzSNz <%s at %#x>r0r9idrcs r__repr__zWeakValueDictionary.__repr__ 7DBBBr cx|jr|t||j||j|<dSr-)rFrWKeyedRefrNrIrrHvalues r __setitem__zWeakValueDictionary.__setitem__s<  ! $  ! ! # # #!%s;; #r c|jr|t}t|5|jD]\}}|}||||< dddn #1swxYwY|Sr-)rFrWrr rIitems)rnewrHrJr\s rcopyzWeakValueDictionary.copys  ! $  ! ! # # #!## T " " ! !9??,, ! !RBDD= CH ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !  s1A77A;>A;c4ddlm}|jr||}t |5|jD] \}}|}||||||<! dddn #1swxYwY|SNr)deepcopy)rurxrFrWr0r rIrs)rmemorxrtrHrJr\s r __deepcopy__z WeakValueDictionary.__deepcopy__s!!!!!!  ! $  ! ! # # #nn T " " 1 19??,, 1 1RBDD=/0Cd++, 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  s;B  BBNc|jr| |j|}|}||S|S#t$r|cYSwxYwr-rY)rrHdefaultrJr\s rgetzWeakValueDictionary.getsq  ! $  ! ! # # # 3BAy   NNN s : A A c#K|jr|t|5|jD]\}}|}|||fV ddddS#1swxYwYdSr-rFrWr rIrs)rkrJvs rrszWeakValueDictionary.itemss  ! $  ! ! # # # T " "  **  2BDD=Q$JJJ                   s2A--A14A1c#K|jr|t|5|jD]\}}||V ddddS#1swxYwYdSr-r)rrrJs rkeyszWeakValueDictionary.keyss  ! $  ! ! # # # T " "  **  2244#GGG                   s.A))A-0A-c#K|jr|t|5|jEd{VddddS#1swxYwYdS)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. NrFrWr rIvaluesrcs r itervaluerefsz!WeakValueDictionary.itervaluerefss  ! $  ! ! # # # T " " * *y'')) ) ) ) ) ) ) ) * * * * * * * * * * * * * * * * * *s AA!Ac#K|jr|t|5|jD]}|}||V ddddS#1swxYwYdSr-rrrJr*s rrzWeakValueDictionary.valuess  ! $  ! ! # # # T " "  i&&((  bdd?III                   s-A((A,/A,c|jr| |j\}}|}|||fS-r-)rFrWrIpopitem)rrHrJr\s rrzWeakValueDictionary.popitemsY  ! $  ! ! # # # i''))GCA}Av  r c|jr| |j|}n#t$rd}YnwxYw||r|dSt ||S)Nr)rFrWrIrTrZ)rrHargsr\s rrTzWeakValueDictionary.pops  ! $  ! ! # # # " c""$$AA   AAA  9 $Awsmm#Hs"A AAc |j|}n#t$rd}YnwxYw|;|jr|t ||j||j|<|S|Sr-)rIrZrFrWrnrN)rrHr|r\s r setdefaultzWeakValueDictionary.setdefaults  #  AA   AAA  9% (%%'''%gt|SAADIcNNHs  ''c X|jr||j}|Rt|dst |}|D]\}}t ||j|||<|D]\}}t ||j|||<dSNrs)rFrWrIhasattrdictrsrnrN)rr6kwargsrVrHr\s rrPzWeakValueDictionary.update!s  ! $  ! ! # # # I  5'** $U ++-- 8 8Q!!T\377#llnn 4 4FCas33AcFF 4 4r c|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. )rFrWlistrIrrcs r valuerefszWeakValueDictionary.valuerefs-s=  ! $  ! ! # # #DI$$&&'''r c0|||Sr-rPr5s r__ior__zWeakValueDictionary.__ior__; E r ct|tjr+|}|||St Sr-r2_collections_abcMappingrurPr4rr6cs r__or__zWeakValueDictionary.__or__?? e-5 6 6  A HHUOOOHr ct|tjr@|}|||||St Sr-r2rrr0rPr4rs r__ror__zWeakValueDictionary.__ror__FP e-5 6 6   A HHUOOO HHTNNNHr )rBr-)r9r:r;r<rRr rWr]r`rdrfrkrqru__copy__rzr}rsr__iter__rrrrTrrPrrrrrBr rrr\s!!!!"0D $ $ $ $  CCC<<<   H       H * * *        4 4 4 4 ( ( (r rc,eZdZdZdZdZfdZxZS)rna[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. rHc@tj|||}||_|Sr-)rr'rH)r&obrrHrs rr'zKeyedRef.__new__[s!{4X.. r cLt||dSr-)r.rR)rrrrHr0s rrRzKeyedRef.__init__`s# X&&&&&r )r9r:r;r<r=r'rRr?r@s@rrnrnOsXI '''''''''r rnceZdZdZddZdZdZdZdZdZ d Z d Z d Z e Z d Zdd ZdZdZdZeZdZdZdZdZddZddZdZdZdZdS)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. Nci|_t|fd}||_g|_t |_d|_|||dSdS)Nc|}|?|jr|j|dS |j|=dS#t$rYdSwxYwdSr-)rErFrGrIrZ)rrKrs rrMz*WeakKeyDictionary.__init__..removeqsv799D?*11!44444 IaLLL#  s; A A F)rIrrNrFrOrE _dirty_lenrP)rrrMs rrRzWeakKeyDictionary.__init__oso !$ii     !#%%   KK       r c|jj}|j} |}n#t$rYdSwxYw ||=n#t$rYnwxYw3r-)rFrTrIrUrZ)rrTrVrHs rrWz"WeakKeyDictionary._commit_removalss $( I  cee     cFF     s ! //7 AAcT|jfd|jD|_d|_dS)Ncg|]}|v| SrBrB).0rrVs r z5WeakKeyDictionary._scrub_removals..s!N!N!NqAvv!vvvr F)rIrFr)rrVs @r_scrub_removalsz!WeakKeyDictionary._scrub_removalss4 I!N!N!N!NT-C!N!N!Nr c>d|_|jt|=dSNT)rrIrr_s rr`zWeakKeyDictionary.__delitem__s Ic#hh   r c6|jt|Sr-)rIrr_s rr]zWeakKeyDictionary.__getitem__syS""r c|jr|jr|t|jt|jz Sr-)rrFrrbrIrcs rrdzWeakKeyDictionary.__len__sH ? #t5 #  " " "49~~D$: ; ;;;r c>d|jjt|fzSrhrircs rrkzWeakKeyDictionary.__repr__rlr c@||jt||j<dSr-)rIrrNros rrqzWeakKeyDictionary.__setitem__s ,1 #c4<(()))r ct}t|5|jD]\}}|}||||< dddn #1swxYwY|Sr-)r r rIrs)rrtrHrpr\s rruzWeakKeyDictionary.copys!! T " " # #"ioo// # # UCEE="CF # # # # # # # # # # # # # # # #  s1AA #A cddlm}|}t|5|jD] \}}|}||||||<! dddn #1swxYwY|Srw)rurxr0r rIrs)rryrxrtrHrpr\s rrzzWeakKeyDictionary.__deepcopy__s!!!!!!nn T " " 3 3"ioo// 3 3 UCEE=%XeT22CF 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3  s;A22A69A6cR|jt||Sr-)rIr}rrrHr|s rr}zWeakKeyDictionary.getsy}}SXXg...r cX t|}n#t$rYdSwxYw||jvSr)rr$rI)rrHrJs rrfzWeakKeyDictionary.__contains__sB SBB   55 TYs   c#Kt|5|jD]\}}|}|||fV ddddS#1swxYwYdSr-r rIrs)rrJrprHs rrszWeakKeyDictionary.itemss T " " % %!Y__.. % % Ebdd?u*$$$ % % % % % % % % % % % % % % % % % % %s2AAAc#Kt|5|jD]}|}||V ddddS#1swxYwYdSr-)r rIrs rrzWeakKeyDictionary.keyss T " "  i  bdd?III                   s ;??c#Kt|5|jD]\}}||V ddddS#1swxYwYdSr-r)rrJrps rrzWeakKeyDictionary.valuess T " "  !Y__..  E244#KKK                   s.AAAc*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. )rrIrcs rkeyrefszWeakKeyDictionary.keyrefssDIr cld|_ |j\}}|}|||fS-r)rrIr)rrHrpr\s rrzWeakKeyDictionary.popitemsC **,,JCA}%x  r cPd|_|jjt|g|RSr)rrIrTr)rrHrs rrTzWeakKeyDictionary.pops+ty}SXX-----r c^|jt||j|Sr-)rIrrrNrs rrzWeakKeyDictionary.setdefaults&y##CT\$:$:7CCCr c |j}|Zt|dsti|}|D]\}}||t ||j<t |r||dSdSr)rIrr&rsrrNrbrP)rrrrVrHrps rrPzWeakKeyDictionary.updates I  4)) &tBxx~~"jjll 2 2 U,1#c4<(()) v;; KK       r c0|||Sr-rr5s rrzWeakKeyDictionary.__ior__rr ct|tjr+|}|||St Sr-rrs rrzWeakKeyDictionary.__or__ rr ct|tjr@|}|||||St Sr-rrs rrzWeakKeyDictionary.__ror__rr r-)r9r:r;r<rRrWrr`r]rdrkrqrurrzr}rfrsrrrrrrTrrPrrrrBr rr r ds($      ###<<<CCC222H////%%%H        ...DDDD    r r ceZdZdZdZiZdZejZ dZ dZ GddZ dZ ddZd Zd Zed Zed Zejd ZdZedZedZdS)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. rBFceZdZdZdS)finalize._Info)weakrefr+rratexitindexN)r9r:r;r=rBr r_Infor5sL r rcT|js*ddl}||jdt_|}t |||_||_||_ |pd|_ d|_t|j |_ ||j|<dt_dS)NrT)_registered_with_atexitrregister _exitfuncrrrrr+rrnext _index_iterr _registry_dirty)rr*r+rrrinfos rrRzfinalize.__init__8s+ 4 MMM OODN + + +/3H ,zz||3~~   n  $*++ #tr Nc|j|d}|r |js|j|ji|jpiSdSdS)zZIf alive then mark as dead and return func(*args, **kwargs); otherwise return NoneN)rrT _shutdownr+rr)r_rs rr/zfinalize.__call__Isf~!!$--  @ @49di?DK,=2?? ? @ @ @ @r c|j|}|o|}|2|j|dr||j|j|jpifSdSdS)z^If alive then mark as dead and return (obj, func, args, kwargs); otherwise return NoneN)rr}rrTr+rrrrr*s rdetachzfinalize.detachPsk~!!$''%t||~~ ?t~11$==?DIt{/@bA A ???r c|j|}|o|}|||j|j|jpifSdS)zMIf alive then return (obj, func, args, kwargs); otherwise return NoneN)rr}rr+rrrs rpeekz finalize.peekXsQ~!!$''%t||~~ ?DIt{/@bA A ?r c||jvS)zWhether finalizer is alive)rrcs ralivezfinalize.alive`st~%%r cb|j|}t|o|jS)z*Whether finalizer should be called at exitrr}boolr)rrs rrzfinalize.atexites,~!!$''Dzz)dk)r cj|j|}|rt||_dSdSr-r)rrprs rrzfinalize.atexitks:~!!$''  &u++DKKK & &r c@|j|}|o|}|&dt|jt |fzSdt|jt |t|jt |fzS)Nz<%s object at %#x; dead>z!<%s object at %#x; for %r at %#x>)rr}rr&r9rjrs rrkzfinalize.__repr__qs~!!$''%t||~~ ;-d1Dbhh0OO O6d$bhhS 0BBsGGLM Mr cd|jD}|dd|DS)Nc*g|]\}}|j ||fSrB)rrfis rrz-finalize._select_for_exit..}s' B B Bu! BaU B B Br c|djS)N)r)items rz+finalize._select_for_exit..~s tAw}r rcg|]\}}|SrBrBrs rrz-finalize._select_for_exit..s!!!eq!!!r )rrssort)r(Ls r_select_for_exitzfinalize._select_for_exitzsR C B 3 3 5 5 B B B ,,---!!q!!!!r c,d} |jrddl}|rd}|d} | tjr |}dt_|sn[|} |n/#t$r"tj tj YnwxYw||jvsJdt_ |r| dSdS#dt_ |r| wwxYw)NFrT)rgc isenableddisablerrrrT Exceptionsys excepthookexc_inforenable)r( reenable_gcrpendingrs rrzfinalize._exitfuncs?   } 2 <<>>!"&KJJLLL2(/"%"6"6"8"8*/" A8 $888 77778CM11112$"&H     "&H    s0A=C. B C. )B96C.8B99C..%Dr-)r9r:r;r<r=rr itertoolscountrrrrrRr/rrpropertyrrsetterrk classmethodrrrBr rrrs_  "III!)/##K F#MMMMMMMM"@@@@BBBBBB&&X&**X*  ]&&]& MMM""["   [   r r)r<_weakrefrrrrrrr r _weakrefsetr r rrrr__all__ MutableSetrrMutableMappingrrnr rrBr rrs10000000 * +  0 0 0 $$W---33333333lppppp*9pppf'''''s'''*uuuuu(7uuupFFFFFFFFFFr