L iJUddlmZddlmZddlZddlZddlZddl m Z ddl m Z ddl mZddlmZddlmZdd lmZdd lmZej*d ej,d ej.f ZedZ dZGddZGddeZGddeeefZGddej@Z!eZ"de#d< e"jHZ$de#d<y)) annotationsN) defaultdict)contextmanager)cached_property)iscoroutinefunction)make_id)make_ref)SymbolF.)boundANYc<eZdZUdZeZ eZded< eddZ eddZ dddZ edfdd Z ddd Z eef dd Zed d Z ddd  d!dZ ddd d"dZd#dZ d$dZefd%dZd&dZ d'dZ d(dZd)dZd)dZy)*SignalzKA notification emitter. :param doc: The docstring for the signal. ztype[set[t.Any]] set_classctdS)zEmitted at the end of each :meth:`connect` call. The signal sender is the signal instance, and the :meth:`connect` arguments are passed through: ``receiver``, ``sender``, and ``weak``. .. versionadded:: 1.2 z"Emitted after a receiver connects.docrselfs R/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/blinker/base.pyreceiver_connectedzSignal.receiver_connected)s>??ctdS)aEmitted at the end of each :meth:`disconnect` call. The sender is the signal instance, and the :meth:`disconnect` arguments are passed through: ``receiver`` and ``sender``. This signal is emitted **only** when :meth:`disconnect` is called explicitly. This signal cannot be emitted by an automatic disconnect when a weakly referenced receiver or sender goes out of scope, as the instance is no longer be available to be used as the sender for this signal. An alternative approach is available by subscribing to :attr:`receiver_connected` and setting up a custom weakref cleanup callback on weak receivers and senders. .. versionadded:: 1.2 z%Emitted after a receiver disconnects.rrrs rreceiver_disconnectedzSignal.receiver_disconnected4s&ABBrNc|r||_i|_ d|_t|j|_t|j|_i|_y)NF)__doc__ receiversis_mutedrr _by_receiver _by_sender _weak_senders)rrs r__init__zSignal.__init__IsO DL   $ 5@5P3>t~~3N>@rTct|}|turtn t|}|r)t||j ||j |<n||j |<|j |j||j|j||tur7||jvr) t||j||j|<d|jvr8|jj r" |jj|||||S|S#t$rYSwxYw#t$r|j||wxYw)aConnect ``receiver`` to be called when the signal is sent by ``sender``. :param receiver: The callable to call when :meth:`send` is called with the given ``sender``, passing ``sender`` as a positional argument along with any extra keyword arguments. :param sender: Any object or :data:`ANY`. ``receiver`` will only be called when :meth:`send` is called with this sender. If ``ANY``, the receiver will be called for any sender. A receiver may be connected to multiple senders by calling :meth:`connect` multiple times. :param weak: Track the receiver with a :mod:`weakref`. The receiver will be automatically disconnected when it is garbage collected. When connecting a receiver defined within a function, set to ``False``, otherwise it will be disconnected when the function scope ends. r)receiversenderweak)r rANY_IDr _make_cleanup_receiverrr"addr!r#_make_cleanup_sender TypeError__dict__rsend disconnect)rr&r'r( receiver_id sender_ids rconnectzSignal.connect[sG h' $mF *2$55kB+DNN; '+3DNN; '  "&&{3 +&**95  $2D2D!D 08D55i@1""9- 4== 0T5L5L5V5V '',,8F-x   &1 s2(D"?D1" D.-D.1Ecdfd }|S)aConnect the decorated function to be called when the signal is sent by ``sender``. The decorated function will be called when :meth:`send` is called with the given ``sender``, passing ``sender`` as a positional argument along with any extra keyword arguments. :param sender: Any object or :data:`ANY`. ``receiver`` will only be called when :meth:`send` is called with this sender. If ``ANY``, the receiver will be called for any sender. A receiver may be connected to multiple senders by calling :meth:`connect` multiple times. :param weak: Track the receiver with a :mod:`weakref`. The receiver will be automatically disconnected when it is garbage collected. When connecting a receiver defined within a function, set to ``False``, otherwise it will be disconnected when the function scope ends.= .. versionadded:: 1.1 c.j||SN)r3)fnrr'r(s r decoratorz%Signal.connect_via..decorators LLVT *Ir)r7r returnr )rr'r(r8s``` r connect_viazSignal.connect_vias( rc#K|j||d d|j|y#|j|wxYww)aA context manager that temporarily connects ``receiver`` to the signal while a ``with`` block executes. When the block exits, the receiver is disconnected. Useful for tests. :param receiver: The callable to call when :meth:`send` is called with the given ``sender``, passing ``sender`` as a positional argument along with any extra keyword arguments. :param sender: Any object or :data:`ANY`. ``receiver`` will only be called when :meth:`send` is called with this sender. If ``ANY``, the receiver will be called for any sender. .. versionadded:: 1.1 F)r'r(N)r3r0)rr&r's r connected_tozSignal.connected_tos<" Xf5 9 &J OOH %DOOH %sA.AAAc#JKd|_ dd|_y#d|_wxYww)zA context manager that temporarily disables the signal. No receivers will be called if the signal is sent, until the ``with`` block exits. Useful for tests. TNF)r rs rmutedz Signal.muteds'   "J!DMEDMs## #)_async_wrapperc |jrgSg}|j|D]F}t|r| td|||fi|}n ||fi|}|j ||fH|S)a/Call all receivers that are connected to the given ``sender`` or :data:`ANY`. Each receiver is called with ``sender`` as a positional argument along with any extra keyword arguments. Return a list of ``(receiver, return value)`` tuples. The order receivers are called is undefined, but can be influenced by setting :attr:`set_class`. If a receiver raises an exception, that exception will propagate up. This makes debugging straightforward, with an assumption that correctly implemented receivers will not raise. :param sender: Call receivers connected to this sender, in addition to those connected to :data:`ANY`. :param _async_wrapper: Will be called on any receivers that are async coroutines to turn them into sync callables. For example, could run the receiver with an event loop. :param kwargs: Extra keyword arguments to pass to each receiver. .. versionchanged:: 1.7 Added the ``_async_wrapper`` argument. z$Cannot send to a coroutine function.r receivers_forr RuntimeErrorappend)rr'r@kwargsresultsr&results rr/z Signal.sendsB ==I**62 /H"8,!)&'MNN11&CFC!&3F3 NNHf- . /r) _sync_wrapperc K|jrgSg}|j|D]V}t|s%| td|||fi|d{}n||fi|d{}|j ||fX|S7-7w)aAwait all receivers that are connected to the given ``sender`` or :data:`ANY`. Each receiver is called with ``sender`` as a positional argument along with any extra keyword arguments. Return a list of ``(receiver, return value)`` tuples. The order receivers are called is undefined, but can be influenced by setting :attr:`set_class`. If a receiver raises an exception, that exception will propagate up. This makes debugging straightforward, with an assumption that correctly implemented receivers will not raise. :param sender: Call receivers connected to this sender, in addition to those connected to :data:`ANY`. :param _sync_wrapper: Will be called on any receivers that are sync callables to turn them into async coroutines. For example, could call the receiver in a thread. :param kwargs: Extra keyword arguments to pass to each receiver. .. versionadded:: 1.7 Nz(Cannot send to a non-coroutine function.rB)rr'rIrFrGr&rHs r send_asynczSignal.send_asyncs@ ==I**62 /H&x0 (&'QRR6}X6vHHH'9&99 NNHf- . / I9s$ABA?B#B$BBc|jsy|jtry|turyt ||jvS)aCheck if there is at least one receiver that will be called with the given ``sender``. A receiver connected to :data:`ANY` will always be called, regardless of sender. Does not check if weakly referenced receivers are still live. See :meth:`receivers_for` for a stronger search. :param sender: Check for receivers connected to this sender, in addition to those connected to :data:`ANY`. FT)rr"r)rr )rr's rhas_receivers_forzSignal.has_receivers_for1s:~~ ??6 " S=v$//11rc#K|jsyt|}||jvr$|jt|j|z}n!|jtj }|D]c}|jj |}|!t |tjr%|}||j|t[|`|eyw)aIYield each receiver to be called for ``sender``, in addition to those to be called for :data:`ANY`. Weakly referenced receivers that are not live will be disconnected and skipped. :param sender: Yield receivers connected to this sender, in addition to those connected to :data:`ANY`. N) rr r"r)copyget isinstanceweakrefref _disconnect)rr'r2idsr1r&strongs rrCzSignal.receivers_forFs~~ FO  '//&)DOOI,FFC//&)..0C K~~))+6H(GKK0!>$$[&9  sCCc|turt}n t|}t|}|j||d|jvr6|j j r|j j|||yyy)aDisconnect ``receiver`` from being called when the signal is sent by ``sender``. :param receiver: A connected receiver callable. :param sender: Disconnect from only this sender. By default, disconnect from all senders. r)r&r'N)rr)r rTr.rrr/)rr&r'r2r1s rr0zSignal.disconnectlsu S=IIh'  i0 $t}} 4**44  & & + +D8F + S5 5rc`|tk(ri|jj|d0|jj D]}|j ||j j|dy|j|j ||j|j |yr6)r)r!popr"valuesdiscardr)rr1r2buckets rrTzSignal._disconnects    $$[$7C"oo4460FNN;/0 NN  {D 1 OOI & . .{ ;   k * 2 29 =rcdfd }|S)ztCreate a callback function to disconnect a weakly referenced receiver when it is garbage collected. c\tjsjtyyr6)sys is_finalizingrTr))rSr1rs rcleanupz.Signal._make_cleanup_receiver..cleanups&$$&  f5'r)rSz#weakref.ref[c.Callable[..., t.Any]]r9Noner:)rr1ras`` rr*zSignal._make_cleanup_receivers 6 rc.tk7sJdfd }|S)zCreate a callback function to disconnect all receivers for a weakly referenced sender when it is garbage collected. cjjdjjdD] }j|j "y)Nr:)r#rYr"r!r[)rSr1rr2s rraz,Signal._make_cleanup_sender..cleanupsT    " "9d 3#229bA B !!+.66yA Br)rSzweakref.ref[t.Any]r9rb)r))rr2ras`` rr,zSignal._make_cleanup_senders F""" B rc|j|jfD]8}t|jD]\}}|r |j |d:y)aEPrune unused sender/receiver bookkeeping. Not threadsafe. Connecting & disconnecting leaves behind a small amount of bookkeeping data. Typical workloads using Blinker, for example in most web apps, Flask, CLI scripts, etc., are not adversely affected by this bookkeeping. With a long-running process performing dynamic signal routing with high volume, e.g. connecting to function closures, senders are all unique object instances. Doing all of this over and over may cause memory usage to grow due to extraneous bookkeeping. (An empty ``set`` for each stale sender/receiver pair.) This method will prune that bookkeeping away, with the caveat that such pruning is not threadsafe. The risk is that cleanup of a fully disconnected receiver/sender pair occurs while another thread is connecting that same pair. If you are in the highly dynamic, unique receiver/sender situation that has lead you to this method, that failure mode is perhaps not a big deal for you. N)r"r!listitemsrY)rmappingidentr\s r_cleanup_bookkeepingzSignal._cleanup_bookkeepingsQ*):):; -G!%gmmo!6 - vKKt, - -rc|jj|jj|jj|jjy)z7Disconnect all receivers and senders. Useful for tests.N)r#clearrr"r!rs r _clear_statezSignal._clear_statesF   "   !r)r9rr6)r str | Noner9rb)r&r r't.Anyr(boolr9r )F)r'ror(rpr9zc.Callable[[F], F])r&c.Callable[..., t.Any]r'ror9c.Generator[None, None, None])r9rr)r' t.Any | Noner@z^c.Callable[[c.Callable[..., c.Coroutine[t.Any, t.Any, t.Any]]], c.Callable[..., t.Any]] | NonerFror9*list[tuple[c.Callable[..., t.Any], t.Any]])r'rsrIz^c.Callable[[c.Callable[..., t.Any]], c.Callable[..., c.Coroutine[t.Any, t.Any, t.Any]]] | NonerFror9rt)r'ror9rp)r'ror9z/c.Generator[c.Callable[..., t.Any], None, None])r&rqr'ror9rb)r1 c.Hashabler2rur9rb)r1rur9z7c.Callable[[weakref.ref[c.Callable[..., t.Any]]], None])r2rur9z&c.Callable[[weakref.ref[t.Any]], None])r9rb)__name__ __module__ __qualname__rrsetr__annotations__rrrr$r3r;rr=r?r/rKrMrCr0rTr*r,rjrmr:rrrrs C>"%I%@@CC(A$47T0d4@C&.&8=& &&&. " " $111  11 41j $000  00 40d2*$$ 8$LLOT0 > %  @ # / -4"rrc2eZdZdZddfd Zdfd ZxZS) NamedSignalaA named generic notification emitter. The name is not used by the signal itself, but matches the key in the :class:`Namespace` that it belongs to. :param name: The name of the signal within the namespace. :param doc: The docstring for the signal. c2t||||_yr6)superr$name)rrr __class__s rr$zNamedSignal.__init__s  rcJt|}|ddd|jdS)Nz; >)r~__repr__r)rbasers rrzNamedSignal.__repr__s-w!s)Btyym1--rr6)rstrrrnr9rb)r9r)rvrwrxrr$r __classcell__)rs@rr|r|s ..rr|ceZdZdZdddZy) Namespacez A dict mapping names to signals.Nc2||vrt||||<||S)zReturn the :class:`NamedSignal` for the given ``name``, creating it if required. Repeated calls with the same name return the same signal. :param name: The name of the signal. :param doc: The docstring of the signal. )r|rrrs rsignalzNamespace.signals& t $T3/DJDzrr6rrrrnr9r|)rvrwrxrrr:rrrrs * rrceZdZdddZy)_PNamespaceSignalNcyr6r:rs r__call__z_PNamespaceSignal.__call__srr6r)rvrwrxrr:rrrrsMrrdefault_namespacer)% __future__rcollections.abcabccr_typingtrR collectionsr contextlibr functoolsrinspectr _utilitiesr r r TypeVarCallableAnyr rr)rr|dictrrProtocolrrrzrr:rrrs" #%%'  AIIcCJ/0 Um u"u"p .&.& S+%&  N N ){9*.444r