U kfQ@sdZddlmZddlZddlmZddlZddlmZddl Z ddl m Z ddl m Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddlZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#dd l$m%Z%dd!l&m'Z'ed"e d#Z(e j)rdd$l*m+Z+dd%l*m,Z,dd&l*m-Z-Gd'd(d(e#j.eeZ/Gd)d*d*e e(Z0ee e(e0e(fZ1Gd+d,d,e/eZ2Gd-d.d.e/ee e!Z3Gd/d0d0e3eZ4Gd1d2d2e'Z5Gd3d4d4e3eZ6Gd5d6d6e6eZ7Gd7d8d8e6eZ8dS)9aAttribute implementation for _Dispatch classes. The various listener targets for a particular event class are represented as attributes, which refer to collections of listeners to be fired off. These collections can exist at the class level as well as at the instance level. An event is fired off using code like this:: some_object.dispatch.first_connect(arg1, arg2) Above, ``some_object.dispatch`` would be an instance of ``_Dispatch`` and ``first_connect`` is typically an instance of ``_ListenerCollection`` if event listeners are present, or ``_EmptyListener`` if none are present. The attribute mechanics here spend effort trying to ensure listener functions are available with a minimum of function call overhead, that unnecessary objects aren't created (i.e. many empty per-instance listener collections), as well as that everything is garbage collectable when owning references are lost. Other features such as "propagation" of listener functions across many ``_Dispatch`` instances, "joining" of multiple ``_Dispatch`` instances, as well as support for subclass propagation (e.g. events assigned to ``Pool`` vs. ``QueuePool``) are all implemented here. ) annotationsN)chain) TracebackType)Any)cast) Collection)Deque) FrozenSet)Generic)Iterator)MutableMapping)MutableSequence)NoReturn)Optional)Sequence)Set)Tuple)Type)TypeVar)Union)legacy)registry)_ET) _EventKey)_ListenerFnType)exc)util)AsyncAdaptedLock)Protocol_T)bound) _Dispatch)_DispatchCommon)_HasEventsDispatchc@s(eZdZUdZded<ddddZdS) RefCollection)refzweakref.ref[RefCollection[_ET]]r'returncCst|tjSN)weakrefr'rZ_collection_gcedselfr.E/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/event/attr.py_memoized_attr_refPsz RefCollection._memoized_attr_refN)__name__ __module__ __qualname__ __slots____annotations__r0r.r.r.r/r&Ks r&c@seZdZdddddZdddddZddd d d Zdddd d ZdddddZddddZddddZ ddddZ dS)_empty_collectionr!None)elementr)cCsdSr*r.r-r8r.r.r/appendUsz_empty_collection.appendcCsdSr*r.r9r.r.r/ appendleftXsz_empty_collection.appendleftz Sequence[_T])otherr)cCsdSr*r.)r-r<r.r.r/extend[sz_empty_collection.extendcCsdSr*r.r9r.r.r/remove^sz_empty_collection.removerboolcCsdSNFr.r9r.r.r/ __contains__asz_empty_collection.__contains__z Iterator[_T]r(cCstgSr*)iterr,r.r.r/__iter__dsz_empty_collection.__iter__cCsdSr*r.r,r.r.r/cleargsz_empty_collection.clearintcCsdSNrr.r,r.r.r/__len__jsz_empty_collection.__len__N) r1r2r3r:r;r=r>rArCrDrGr.r.r.r/r6Tsr6c@seZdZUdZdZded<ded<ded<ded <d ed <d ed <dddddZddddddZdddddZddddddZ dddddd Z ddddd!d"Z d#dd$d%d&Z ddd'd(d)Z dd*d+d,Zd-d.d/d0d1Zd2S)3_ClsLevelDispatchz2Class-level events on :class:`._Dispatch` classes.)clsnamename arg_nameshas_kwlegacy_signatures _clslevel __weakref__strrIrJz Sequence[str]rKr?rLz,MutableSequence[legacy._LegacySignatureType]rMzCMutableMapping[Type[_ET], _ListenerFnSequenceType[_ListenerFnType]]rNzType[_HasEventsDispatch[_ET]]r)parent_dispatch_clsfncCsv|j|_|j|_t|}|jdd|_t|j|_ t t t t |dgddd|_t||||_t|_dS)NrZ_legacy_signaturescSs|dSrFr.)sr.r.r/z,_ClsLevelDispatch.__init__..)key)r1rJrIrZinspect_getfullargspecargsrKr?varkwrLlistreversedsortedgetattrrMrZ_augment_fn_docs__doc__r+WeakKeyDictionaryrN)r-rQrRargspecr.r.r/__init__s   z_ClsLevelDispatch.__init__rRnamedr)cCsN|r||}|jrJztj|dd}Wntk r:YnXt|||}|S)NT)Zno_self)_wrap_fn_for_kwrMrZget_callable_argspec TypeErrorrZ_wrap_fn_for_legacy)r-rRrbr_r.r.r/_adjust_fn_specs z!_ClsLevelDispatch._adjust_fn_spec)rRr)csddddfdd }|S)NrrWkwr)cs$ttj|}||f|Sr*)dictziprKupdate)rWrgZargdictrRr-r.r/wrap_kws z2_ClsLevelDispatch._wrap_fn_for_kw..wrap_kwr.)r-rRrlr.rkr/rcsz!_ClsLevelDispatch._wrap_fn_for_kw_EventKey[_ET]r7) event_key is_appendr)cCs|j}t|tstdt|dds6td|dt|D]`}||k rb||j krb| |q@||j krv| ||r|j | |j q@|j | |j q@t||dS)Nz*Class-level Event targets must be classes._sa_propagate_class_eventsTz&Can't assign an event directly to the z class)dispatch_target isinstancetypeAssertionErrorr\rZInvalidRequestErrorrwalk_subclassesrNupdate_subclassr: _listen_fnr;rZ_stored_in_collection)r-rnrotargetclsr.r.r/_do_insert_or_appends(     z&_ClsLevelDispatch._do_insert_or_appendrn propagater)cCs|j|dddS)NFrorzr-rnr|r.r.r/insertsz_ClsLevelDispatch.insertcCs|j|dddS)NTr}r~rr.r.r/r:sz_ClsLevelDispatch.append Type[_ET])rxr)cs|||jkr2t|ddr&t|j|<n t|j|<|j||jddD],}||jkrJfdd|j|DqJdS)NrpTrcsg|]}|kr|qSr.r.).0rRZclslevelr.r/ sz5_ClsLevelDispatch.update_subclass..)rNr\ collectionsdequer6__mro__r=)r-rxryr.rr/rvs     z!_ClsLevelDispatch.update_subclassrnr)cCsB|j}t|D] }||jkr|j||jqt||dSr*)rqrrurNr>rwr_removed_from_collection)r-rnrxryr.r.r/r>s  z_ClsLevelDispatch.remover(cCs8t}|jD]}|||qt||dS)zClear all class level listenersN)setrNvaluesrjrDr_clear)r-Zto_clear dispatcherr.r.r/rDs   z_ClsLevelDispatch.clear_Dispatch[_ET]_ClsLevelDispatch[_ET]objr)cCs|SzReturn an event collection which can be modified. For _ClsLevelDispatch at the class level of a dispatcher, this returns self. r.r-rr.r.r/ for_modifysz_ClsLevelDispatch.for_modifyN)r1r2r3r]r4r5r`rercrzrr:rvr>rDrr.r.r.r/rHqs$   rHc@seZdZUdZded<ddddddZd dd d d Zd dddZddddZddddZ d d ddddZ d d ddddZ d d ddddZ d d ddddZ d ddd!d"d#Zd ddd!d$d%Zd dd&d'd(Zd)d*d+d,d-Zd.S)/_InstanceLevelDispatchr.rparentrr?racCs|j||Sr*)rrer-rRrbr.r.r/resz&_InstanceLevelDispatch._adjust_fn_specritemr)cCs tdSr*NotImplementedErrorr-rr.r.r/rAsz#_InstanceLevelDispatch.__contains__rEr(cCs tdSr*rr,r.r.r/rG sz_InstanceLevelDispatch.__len__Iterator[_ListenerFnType]cCs tdSr*rr,r.r.r/rC sz_InstanceLevelDispatch.__iter__cCs tdSr*rr,r.r.r/__bool__sz_InstanceLevelDispatch.__bool__r7rfcOs tdSr*rr-rWrgr.r.r/ exec_oncesz _InstanceLevelDispatch.exec_oncecOs tdSr*rrr.r.r/exec_once_unless_exceptionsz1_InstanceLevelDispatch.exec_once_unless_exceptioncOs tdSr*rrr.r.r/_exec_w_sync_on_first_runsz0_InstanceLevelDispatch._exec_w_sync_on_first_runcOs tdSr*rrr.r.r/__call__sz_InstanceLevelDispatch.__call__rmr{cCs tdSr*rrr.r.r/rsz_InstanceLevelDispatch.insertcCs tdSr*rrr.r.r/r:!sz_InstanceLevelDispatch.appendrcCs tdSr*rr-rnr.r.r/r>$sz_InstanceLevelDispatch.remove_DispatchCommon[_ET]_InstanceLevelDispatch[_ET]rcCs|Srr.rr.r.r/r's z!_InstanceLevelDispatch.for_modifyN)r1r2r3r4r5rerArGrCrrrrrrr:r>rr.r.r.r/rs rc@s2eZdZUdZdZeZded<dZded<ded <d ed <d ed <dddddZ dddddZ ddddddZ ddddddZ ddddddZ ddddd d!Zddddd"d#Zddddd$d%Zddddd&d'Zddd(dd)d*Zdd+d,d-d.Zd/d0d1d2Zd3d0d4d5Zd+d0d6d7Zd8S)9_EmptyListenerzServes as a proxy interface to the events served by a _ClsLevelDispatch, when there are no instance-level events present. Is replaced by _ListenerCollection when instance-level events are added. )rparent_listenersrJzFrozenSet[_ListenerFnType]r|r.z Tuple[()] listenersrrz(_ListenerFnSequenceType[_ListenerFnType]rrPrJrr target_clscCs2||jkr||||_|j||_|j|_dSr*)rNrvrrrJr-rrr.r.r/r`Es    z_EmptyListener.__init__r_ListenerCollection[_ET]rcCs`td|}|jdk stt|j|j}t||j|krFt||j|ntt||jt s\t|S)zReturn an event collection which can be modified. For _EmptyListener at the instance level of a dispatcher, this generates a new _ListenerCollection, applies it to the instance, and returns it. rN) rZ _instance_clsrt_ListenerCollectionrr\rJsetattrrr_JoinedListener)r-rresultr.r.r/rLs z_EmptyListener.for_modifyrrrfcOs tddS)Nzneed to call for_modify()rrr.r.r/ _needs_modifyasz_EmptyListener._needs_modifycOs|j||dSr*rrr.r.r/rdsz_EmptyListener.exec_oncecOs|j||dSr*rrr.r.r/rgsz)_EmptyListener.exec_once_unless_exceptioncOs|j||dSr*rrr.r.r/rjsz_EmptyListener.insertcOs|j||dSr*rrr.r.r/r:msz_EmptyListener.appendcOs|j||dSr*rrr.r.r/r>psz_EmptyListener.removecOs|j||dSr*rrr.r.r/rDssz_EmptyListener.clearr7cOs|jD]}|||qdSzExecute this event.Nrr-rWrgrRr.r.r/rvs z_EmptyListener.__call__r?rcCs ||jkSr*rrr.r.r/rA|sz_EmptyListener.__contains__rEr(cCs t|jSr*)lenrr,r.r.r/rGsz_EmptyListener.__len__rcCs t|jSr*)rBrr,r.r.r/rCsz_EmptyListener.__iter__cCs t|jSr*)r?rr,r.r.r/rsz_EmptyListener.__bool__N)r1r2r3r]r4 frozensetr|r5rr`rrrrrr:r>rDrrArGrCrr.r.r.r/r3s*   rc@s.eZdZddddZddddd d d Zd S) _MutexProtocolr?r(cCsdSr*r.r,r.r.r/ __enter__rUz_MutexProtocol.__enter__zOptional[Type[BaseException]]zOptional[BaseException]zOptional[TracebackType]zOptional[bool])exc_typeexc_valexc_tbr)cCsdSr*r.)r-rrrr.r.r/__exit__sz_MutexProtocol.__exit__N)r1r2r3rrr.r.r.r/rsrcseZdZUdZded<ded<ded<ded<ded <d d d fd d ZddddZddddZdd d ddddZd d ddddZ d d ddddZ d d ddddZ d d ddddZ d dd d!d"Z d#dd$d%Zd&dd'd(Zddd)d*ZZS)+_CompoundListener)_exec_once_mutex _exec_once_exec_w_sync_once _is_asynciorrCollection[_ListenerFnType]rrr?rrr)argrgcstj||d|_dSr@)superr`r)r-rrg __class__r.r/r`sz_CompoundListener.__init__r7r(cCs d|_dS)NT)rr,r.r.r/ _set_asynciosz_CompoundListener._set_asynciocCs|jr tStSdSr*)rr threadingLockr,r.r.r/_memoized_attr__exec_once_mutexsz1_CompoundListener._memoized_attr__exec_once_mutex)retry_on_exceptionrWrgr)c OsX|jH|jsJz*z|||d}Wnd}YnXW5|rB|sHd|_XW5QRXdS)NTF)rr)r-rrWrg exceptionr.r.r/_exec_once_impls  z!_CompoundListener._exec_once_implrfcOs|js|jd||dS)z]Execute this event, but only if it has not been executed already for this collection.FN)Frrrr.r.r/rsz_CompoundListener.exec_oncecOs|js|jd||dS)atExecute this event, but only if it has not been executed already for this collection, or was called by a previous exec_once_unless_exception call and raised an exception. If exec_once was already called, then this method will never run the callable regardless of whether it raised or not. .. versionadded:: 1.3.8 TN)Trrr.r.r/rs z,_CompoundListener.exec_once_unless_exceptionc OsL|js>|j*z|||WnYnXd|_W5QRXn |||dS)anExecute this event, and use a mutex if it has not been executed already for this collection, or was called by a previous _exec_w_sync_on_first_run call and raised an exception. If _exec_w_sync_on_first_run was already called and didn't raise an exception, then a mutex is not used. .. versionadded:: 1.4.11 TN)rrrr.r.r/rs z+_CompoundListener._exec_w_sync_on_first_runcOs0|jD]}|||q|jD]}|||qdSrrrrr.r.r/rs   z_CompoundListener.__call__rcCs||jkp||jkSr*rrr.r.r/rAsz_CompoundListener.__contains__rEcCst|jt|jSr*)rrrr,r.r.r/rGsz_CompoundListener.__len__rcCst|j|jSr*)rrrr,r.r.r/rCsz_CompoundListener.__iter__cCst|jp |jSr*)r?rrr,r.r.r/rsz_CompoundListener.__bool__)r1r2r3r4r5r`rrrrrrrrArGrCr __classcell__r.r.rr/rs$ rcseZdZUdZdZded<ded<ded<d ed <d ed <dd dfdd ZdddddZd(ddddddZddddddZ ddddd d!Z ddd"d#d$Z dd%d&d'Z Z S))rzInstance-level attributes on instances of :class:`._Dispatch`. Represents a collection of listeners. As of 0.7.9, _ListenerCollection is only first created via the _EmptyListener.for_modify() method. )rrrJrr|rOrrrrrPrJzDeque[_ListenerFnType]rzSet[_ListenerFnType]r|rrcsZt||jkr||d|_d|_|j||_||_|j|_t |_ t |_ dSr@)rr`rNrvrrrrrJrrrrr|rrr.r/r`s     z_ListenerCollection.__init__rrrcCs|S)zReturn an event collection which can be modified. For _ListenerCollection at the instance level of a dispatcher, this returns self. r.rr.r.r/r%s z_ListenerCollection.for_modifyTr?r7)r<only_propagater)csjj}t|j|jfdd|jD}|||jrL|j|}t ||dS)zIPopulate from the listeners in another :class:`_Dispatch` object.cs&g|]}|krr|jkr|qSr.)r|)rlZexisting_listener_setrr-r.r/r8s  z/_ListenerCollection._update..N) rrr|rjr=rrunionrZ_stored_in_collection_multi)r-r<rZexisting_listenersZother_listenersZ to_associater.rr/_update0s  z_ListenerCollection._updatermr{cCs$|||jr |r |j|jdSr*)Zprepend_to_listrr|addrwrr.r.r/rHsz_ListenerCollection.insertcCs$|||jr |r |j|jdSr*)Zappend_to_listrr|rrwrr.r.r/r:Msz_ListenerCollection.appendrcCs,|j|j|j|jt||dSr*)rr>rwr|discardrrrr.r.r/r>Rsz_ListenerCollection.remover(cCs&t||j|j|jdSr*)rrrr|rDr,r.r.r/rDWs z_ListenerCollection.clear)T)r1r2r3r]r4r5r`rrrr:r>rDrr.r.rr/rs     rc@seZdZUdZded<ded<ded<ded <ddd d d d ZejsXeddddZ ddddddZ dddddZ ddddddZ dddddd Z ddd!d"d#Zddd$d%Zd&S)'r)parent_dispatchrJlocalrrrrPrJrrrrz_EmptyListener[_ET])rrJrcCs$d|_||_||_||_|j|_dSr@)rrrJrr)r-rrJrr.r.r/r`es z_JoinedListener.__init__r(cCst|j|jSr*)r\rrJr,r.r.r/rxsz_JoinedListener.listenersrr?racCs|j||Sr*)rrerr.r.r/re|sz_JoinedListener._adjust_fn_specz_JoinedListener[_ET]rcCs|j||_|_|Sr*)rrrrr.r.r/rsz_JoinedListener.for_modifyrmr7r{cCs|j||dSr*)rrrr.r.r/rsz_JoinedListener.insertcCs|j||dSr*)rr:rr.r.r/r:sz_JoinedListener.appendrcCs|j|dSr*)rr>rr.r.r/r>sz_JoinedListener.removecCs tdSr*rr,r.r.r/rDsz_JoinedListener.clearN)r1r2r3r4r5r`typing TYPE_CHECKINGpropertyrrerrr:r>rDr.r.r.r/r]s  r)9r] __future__rr itertoolsrrtypesrrrrrrr r r r r rrrrrrrrr+rrrrrrrZutil.concurrencyrZ util.typingr r!rbaser#r$r%Z MemoizedSlotsr&r6Z_ListenerFnSequenceTyperHrrrrrrr.r.r.r/sb                                   7V l]