zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
U
Þ=®dõOã@sždZddlmZddlZddlmZddlmZddl    m
Z
ddl m Z ddl mZdd    l mZdd
l mZdd l mZdd l mZdd l mZddl mZddl mZejr:ddlZejdejdejfdZe d¡Ze d¡Zejejeefgejeej effZ!ejejeej efgejeeffZ"edƒZ#de#_dZ$Gdd„dƒZ%e%dƒZ&Gdd„de%ƒZ'Gdd„de(ƒZ)Gdd„de ƒZ*e)ƒj+Z+dS) a+Signals and events.
 
A small implementation of signals, inspired by a snippet of Django signal
API client code seen in a blog post.  Signals are first-class objects and
each manages its own receivers and message emission.
 
The :func:`signal` function provides singleton behavior for named signals.
 
é)Ú annotationsN)Ú defaultdict)Úcontextmanager)Úwarn)ÚWeakValueDictionary)Úannotatable_weakref)Úhashable_identity)Ú IdentityType)Úis_coroutine_function)Ú lazy_property)Ú    reference)Úsymbol)Ú    WeakTypesÚ
T_callable.)ÚboundÚTÚPÚANYzToken for "any sender".c@s‚eZdZdZeZeddœdd„ƒZeddœdd„ƒZdGdd    d
œd d „Zed fdddddœdd„Z    dHddddœdd„Z
e efddddœdd„ƒZ e ddœdd„ƒZ efddd dœd!d"„Zdd#œdd$dd%d&œd'd(„Zdd)œdd*dd%d+œd,d-„Zddd.œd/d0„Zddd.œd1d2„Zdd3d.œd4d5„Zefddd    dœd6d7„Zd8d8d    d9œd:d;„Zd<d    d=œd>d?„Zd<d    d@œdAdB„Zd    dœdCdD„Zd    dœdEdF„ZdS)IÚSignalzA notification emitter.©ÚreturncCs
tddS)zßEmitted after each :meth:`connect`.
 
        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.©Údoc©r©Úself©rúCd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\blinker/base.pyÚreceiver_connected2s
zSignal.receiver_connectedcCs
tddS)a Emitted after :meth:`disconnect`.
 
        The sender is the signal instance, and the :meth:`disconnect` arguments
        are passed through: *receiver* and *sender*.
 
        Note, this signal is emitted **only** when :meth:`disconnect` is
        called explicitly.
 
        The disconnect signal can not be emitted by an automatic disconnect
        (due to a weakly referenced receiver or sender going out of scope),
        as the receiver and/or sender instances are no longer available for
        use at the time this signal would be emitted.
 
        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.rrrrrrÚreceiver_disconnected>szSignal.receiver_disconnectedNú
str | NoneÚNone)rrcCs4|r
||_i|_d|_ttƒ|_ttƒ|_i|_dS)zt
        :param doc: optional.  If provided, will be assigned to the signal's
          __doc__ attribute.
 
        FN)Ú__doc__Ú    receiversÚis_mutedrÚsetÚ _by_receiverÚ
_by_senderÚ _weak_senders)rrrrrÚ__init__Vs
 
zSignal.__init__Trzt.AnyÚbool)ÚreceiverÚsenderÚweakrc    
Cs~t|ƒ}|r t||jƒ}||_n|}|tkr2t}nt|ƒ}|j ||¡|j|     |¡|j
|     |¡~|tk    r¸||j kr¸zt||j ƒ}||_ Wntk
r¦YnX|j  ||¡~d|jkr|jjrz|jj||||dWn4tk
r}z| ||¡|‚W5d}~XYnXtjrz|tk    rzztj||||dWn4tk
rx}z| ||¡|‚W5d}~XYnX|S)aaConnect *receiver* to signal events sent by *sender*.
 
        :param receiver: A callable.  Will be invoked by :meth:`send` with
          `sender=` as a single positional argument and any ``kwargs`` that
          were provided to a call to :meth:`send`.
 
        :param sender: Any object or :obj:`ANY`, defaults to ``ANY``.
          Restricts notifications delivered to *receiver* to only those
          :meth:`send` emissions sent by *sender*.  If ``ANY``, the receiver
          will always be notified.  A *receiver* may be connected to
          multiple *sender* values on the same Signal through multiple calls
          to :meth:`connect`.
 
        :param weak: If true, the Signal will hold a weakref to *receiver*
          and automatically disconnect when *receiver* goes out of scope or
          is garbage collected.  Defaults to True.
 
        r)r+r,r-N)Z receiver_argZ
sender_argZweak_arg)rr Ú_cleanup_receiverÚ receiver_idrÚANY_IDr#Ú
setdefaultr'Úaddr&r(Ú_cleanup_senderÚ    sender_idÚ    TypeErrorÚ__dict__rÚsendÚ
disconnect)    rr+r,r-r/Ú receiver_refr4Ú
sender_refÚerrrÚconnectjsV  
ÿ
 ÿ
 zSignal.connectFz$t.Callable[[T_callable], T_callable])r,r-rcsdddœ‡‡‡fdd„ }|S)aKConnect the decorated function as a receiver for *sender*.
 
        :param sender: Any object or :obj:`ANY`.  The decorated function
          will only receive :meth:`send` emissions sent by *sender*.  If
          ``ANY``, the receiver will always be notified.  A function may be
          decorated multiple times with differing *sender* values.
 
        :param weak: If true, the Signal will hold a weakref to the
          decorated function and automatically disconnect when *receiver*
          goes out of scope or is garbage collected.  Unlike
          :meth:`connect`, this defaults to False.
 
        The decorated function will be invoked by :meth:`send` with
          `sender=` as a single positional argument and any ``kwargs`` that
          were provided to the call to :meth:`send`.
 
 
        .. versionadded:: 1.1
 
        r)Úfnrcsˆ |ˆˆ¡|S©N)r<)r=©rr,r-rrÚ    decoratorÈsz%Signal.connect_via.<locals>.decoratorr)rr,r-r@rr?rÚ connect_via°szSignal.connect_viaz
t.Callablezt.Generator[(None, None, None)])r+r,rc
csZ|j||ddz
dVWn0tk
rJ}z| |¡|‚W5d}~XYn X| |¡dS)ahExecute a block with the signal temporarily connected to *receiver*.
 
        :param receiver: a receiver callable
        :param sender: optional, a sender to filter on
 
        This is a context manager for use in the ``with`` statement.  It can
        be useful in unit tests.  *receiver* is connected to the signal for
        the duration of the ``with`` block, and will be disconnected
        automatically when exiting the block:
 
        .. code-block:: python
 
          with on_ready.connected_to(receiver):
             # do stuff
             on_ready.send(123)
 
        .. versionadded:: 1.1
 
        F)r,r-N)r<Ú    Exceptionr8)rr+r,r;rrrÚ connected_toÎs
 
zSignal.connected_toc
csJd|_z6z
dVWn&tk
r8}z|‚W5d}~XYnXW5d|_XdS)z\Context manager for temporarily disabling signal.
        Useful for test purposes.
        TFN)r$rB)rr;rrrÚmutedîs 
z Signal.mutedzt.ContextManager[None]cCstdtƒ| ||¡S)agAn alias for :meth:`connected_to`.
 
        :param receiver: a receiver callable
        :param sender: optional, a sender to filter on
 
        .. versionadded:: 0.9
 
        .. versionchanged:: 1.1
          Renamed to :meth:`connected_to`.  ``temporarily_connected_to`` was
          deprecated in 1.2 and will be removed in a subsequent version.
 
        zAtemporarily_connected_to is deprecated; use connected_to instead.)rÚDeprecationWarningrC)rr+r,rrrÚtemporarily_connected_toûs
þzSignal.temporarily_connected_to)Ú_async_wrapperzAsyncWrapperType | Nonezlist[tuple[t.Callable, t.Any]])r,rGÚkwargsrcOsf|jr
gS| |¡}g}| |¡D]>}t|ƒrF|dkr>tdƒ‚||ƒ}||f|Ž}| ||f¡q"|S)aEmit this signal on behalf of *sender*, passing on ``kwargs``.
 
        Returns a list of 2-tuples, pairing receivers with their return
        value. The ordering of receiver notification is undefined.
 
        :param sender: Any object or ``None``.  If omitted, synonymous
          with ``None``.  Only accepts one positional argument.
        :param _async_wrapper: A callable that should wrap a coroutine
          receiver and run it when called synchronously.
 
        :param kwargs: Data to be sent to receivers.
        Nz#Cannot send to a coroutine function©r$Ú_extract_senderÚ receivers_forr
Ú RuntimeErrorÚappend)rrGr,rHÚresultsr+Úresultrrrr7s
 z Signal.send)Ú _sync_wrapperzSyncWrapperType | None)r,rPrHrcÏsl|jr
gS| |¡}g}| |¡D]D}t|ƒsF|dkr>tdƒ‚||ƒ}||f|ŽIdH}| ||f¡q"|S)aEmit this signal on behalf of *sender*, passing on ``kwargs``.
 
        Returns a list of 2-tuples, pairing receivers with their return
        value. The ordering of receiver notification is undefined.
 
        :param sender: Any object or ``None``.  If omitted, synonymous
          with ``None``. Only accepts one positional argument.
        :param _sync_wrapper: A callable that should wrap a synchronous
          receiver and run it when awaited.
 
        :param kwargs: Data to be sent to receivers.
        Nz'Cannot send to a non-coroutine functionrI)rrPr,rHrNr+rOrrrÚ
send_async0s
zSignal.send_async)r,rcCsn|js.|r*t|ƒdkr*tdt|ƒ›dƒ‚gSt|ƒdkr@d}n*t|ƒdkrbtdt|ƒ›dƒ‚n|d}|S)NTéz-send() accepts only one positional argument, z givenr)r#Úlenr5©rr,rrrrJPsÿ  ÿzSignal._extract_sendercCs2|js
dS|jtrdS|tkr$dSt|ƒ|jkS)zôTrue if there is probably a receiver for *sender*.
 
        Performs an optimistic check only.  Does not guarantee that all
        weakly referenced receivers are still alive.  See
        :meth:`receivers_for` for a stronger search.
 
        FT)r#r'r0rrrTrrrÚhas_receivers_forgs
zSignal.has_receivers_forz9t.Generator[t.Callable | annotatable_weakref, None, None]ccs|jrŒt|ƒ}||jkr.|jt|j|B}n|jt ¡}|D]J}|j |¡}|dkrZq@t|tƒr„|ƒ}|dkr€| |t¡q@|}|Vq@dS)z2Iterate all live receivers listening for *sender*.N)    r#rr'r0ÚcopyÚgetÚ
isinstancerÚ _disconnect)rr,r4Úidsr/r+ÚstrongrrrrKws 
 
 zSignal.receivers_forcCsR|tkrt}nt|ƒ}t|ƒ}| ||¡d|jkrN|jjrN|jj|||ddS)aDisconnect *receiver* from this signal's events.
 
        :param receiver: a previously :meth:`connected<connect>` callable
 
        :param sender: a specific sender to disconnect from, or :obj:`ANY`
          to disconnect from all senders.  Defaults to ``ANY``.
 
        r)r+r,N)rr0rrYr6rr#r7)rr+r,r4r/rrrr8Žs
 ÿþzSignal.disconnectr    )r/r4rcCsd|tkr@|j |d¡r0|j ¡D]}| |¡q |j |d¡n |j| |¡|j| |¡dS)NF)r0r&Úpopr'ÚvaluesÚdiscardr#)rr/r4ÚbucketrrrrY¥s zSignal._disconnectr)r9rcCs| t t|j¡t¡dS)z'Disconnect a receiver from all senders.N)rYÚtÚcastr    r/r0)rr9rrrr.¯szSignal._cleanup_receiver)r:rcCsPt t|j¡}|tkst‚|j |d¡|j |d¡D]}|j    | 
|¡q6dS)z'Disconnect all receivers from a sender.Nr) r`rar    r4r0ÚAssertionErrorr(r\r'r&r^)rr:r4r/rrrr3³s
 zSignal._cleanup_sendercCs<|j|jfD]*}t| ¡ƒD]\}}|s| |d¡qq dS)arPrune unused sender/receiver bookkeeping. Not threadsafe.
 
        Connecting & disconnecting leave behind a small amount of bookkeeping
        for the receiver and sender values. 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 Python process performing dynamic signal routing
        with high volume- e.g. connecting to function closures, "senders" are
        all unique object instances, and doing all of this over and over- you
        may see memory usage will 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&ÚlistÚitemsr\)rÚmappingZ_idr_rrrÚ_cleanup_bookkeeping»szSignal._cleanup_bookkeepingcCs,|j ¡|j ¡|j ¡|j ¡dS)z4Throw away all signal state.  Useful for unit tests.N)r(Úclearr#r'r&rrrrÚ _clear_stateÕs
 
 
zSignal._clear_state)N)F)Ú__name__Ú
__module__Ú __qualname__r"rr rrr)r<rArrCrDrFr7rQrJrUrKr8rYr.r3rfrhrrrrr+s@ ÿGÿÿ ÿý#ý 
ra Sent by a :class:`Signal` after a receiver connects.
 
:argument: the Signal that was connected to
:keyword receiver_arg: the connected receiver
:keyword sender_arg: the sender to connect to
:keyword weak_arg: true if the connection to receiver_arg is a weak reference
 
.. deprecated:: 1.2
 
As of 1.2, individual signals have their own private
:attr:`~Signal.receiver_connected` and
:attr:`~Signal.receiver_disconnected` signals with a slightly simplified
call signature.  This global signal is planned to be removed in 1.6.
 
c@s2eZdZdZd ddddœdd„Zdd    œd
d „ZdS) Ú NamedSignalz%A named generic notification emitter.NÚstrr r!©ÚnamerrcCst ||¡||_dSr>)rr)ro)rrorrrrr)ôs zNamedSignal.__init__rcCs$t |¡}|dd…›d|j›dS)Néÿÿÿÿz; ú>)rÚ__repr__ro)rÚbaserrrrrús
zNamedSignal.__repr__)N)rirjrkr"r)rrrrrrrlñsrlc@s$eZdZdZd    ddddœdd„ZdS)
Ú    Namespacez%A mapping of signal names to signals.Nrmr rlrnc    Cs:z
||WStk
r4| |t||ƒ¡}|YSXdS©z—Return the :class:`NamedSignal` *name*, creating it if required.
 
        Repeated calls to this function will return the same signal object.
 
        N©ÚKeyErrorr1rl©rrorrOrrrÚsignals
 
zNamespace.signal)N©rirjrkr"ryrrrrrtÿsrtc@s$eZdZdZd    ddddœdd„ZdS)
Ú WeakNamespacea-A weak mapping of signal names to signals.
 
    Automatically cleans up unused Signals when the last reference goes out
    of scope.  This namespace implementation exists for a measure of legacy
    compatibility with Blinker <= 1.2, and may be dropped in the future.
 
    .. versionadded:: 1.3
 
    Nrmr rlrnc    Cs:z
||WStk
r4| |t||ƒ¡}|YSXdSrurvrxrrrrys
 
zWeakNamespace.signal)Nrzrrrrr{s
r{),r"Ú
__future__rÚtypingr`Ú collectionsrÚ
contextlibrÚwarningsrÚweakrefrZblinker._utilitiesrrr    r
r r r rÚ TYPE_CHECKINGZtyping_extensionsÚteÚTypeVarÚCallableÚAnyrrZ    ParamSpecrÚ    AwaitableZAsyncWrapperTypeZSyncWrapperTyperr0rrrlÚdictrtr{ryrrrrÚ<module>sF                 
 
**5ÿ