1
1
from __future__ import annotations
2
2
3
3
import collections .abc as c
4
+ import sys
4
5
import typing as t
5
- import warnings
6
6
import weakref
7
7
from collections import defaultdict
8
- from contextlib import AbstractContextManager
9
8
from contextlib import contextmanager
10
9
from functools import cached_property
11
10
from inspect import iscoroutinefunction
12
- from weakref import WeakValueDictionary
13
11
14
12
from ._utilities import make_id
15
13
from ._utilities import make_ref
16
14
from ._utilities import Symbol
17
15
18
- if t .TYPE_CHECKING :
19
- F = t .TypeVar ("F" , bound = c .Callable [..., t .Any ])
16
+ F = t .TypeVar ("F" , bound = c .Callable [..., t .Any ])
20
17
21
18
ANY = Symbol ("ANY" )
22
19
"""Symbol for "any sender"."""
@@ -139,15 +136,6 @@ def connect(self, receiver: F, sender: t.Any = ANY, weak: bool = True) -> F:
139
136
self .disconnect (receiver , sender )
140
137
raise
141
138
142
- if _receiver_connected .receivers and self is not _receiver_connected :
143
- try :
144
- _receiver_connected .send (
145
- self , receiver_arg = receiver , sender_arg = sender , weak_arg = weak
146
- )
147
- except TypeError :
148
- self .disconnect (receiver , sender )
149
- raise
150
-
151
139
return receiver
152
140
153
141
def connect_via (self , sender : t .Any , weak : bool = False ) -> c .Callable [[F ], F ]:
@@ -213,24 +201,6 @@ def muted(self) -> c.Generator[None, None, None]:
213
201
finally :
214
202
self .is_muted = False
215
203
216
- def temporarily_connected_to (
217
- self , receiver : c .Callable [..., t .Any ], sender : t .Any = ANY
218
- ) -> AbstractContextManager [None ]:
219
- """Deprecated alias for :meth:`connected_to`.
220
-
221
- .. deprecated:: 1.1
222
- Renamed to ``connected_to``. Will be removed in Blinker 1.9.
223
-
224
- .. versionadded:: 0.9
225
- """
226
- warnings .warn (
227
- "'temporarily_connected_to' is renamed to 'connected_to'. The old name is"
228
- " deprecated and will be removed in Blinker 1.9." ,
229
- DeprecationWarning ,
230
- stacklevel = 2 ,
231
- )
232
- return self .connected_to (receiver , sender )
233
-
234
204
def send (
235
205
self ,
236
206
sender : t .Any | None = None ,
@@ -434,7 +404,10 @@ def _make_cleanup_receiver(
434
404
"""
435
405
436
406
def cleanup (ref : weakref .ref [c .Callable [..., t .Any ]]) -> None :
437
- self ._disconnect (receiver_id , ANY_ID )
407
+ # If the interpreter is shutting down, disconnecting can result in a
408
+ # weird ignored exception. Don't call it in that case.
409
+ if not sys .is_finalizing ():
410
+ self ._disconnect (receiver_id , ANY_ID )
438
411
439
412
return cleanup
440
413
@@ -488,23 +461,6 @@ def _clear_state(self) -> None:
488
461
self ._by_receiver .clear ()
489
462
490
463
491
- _receiver_connected = Signal (
492
- """\
493
- Sent by a :class:`Signal` after a receiver connects.
494
-
495
- :argument: the Signal that was connected to
496
- :keyword receiver_arg: the connected receiver
497
- :keyword sender_arg: the sender to connect to
498
- :keyword weak_arg: true if the connection to receiver_arg is a weak reference
499
-
500
- .. deprecated:: 1.2
501
- Individual signals have their own :attr:`~Signal.receiver_connected` and
502
- :attr:`~Signal.receiver_disconnected` signals with a slightly simplified
503
- call signature. This global signal will be removed in Blinker 1.9.
504
- """
505
- )
506
-
507
-
508
464
class NamedSignal (Signal ):
509
465
"""A named generic notification emitter. The name is not used by the signal
510
466
itself, but matches the key in the :class:`Namespace` that it belongs to.
@@ -524,18 +480,7 @@ def __repr__(self) -> str:
524
480
return f"{ base [:- 1 ]} ; { self .name !r} >" # noqa: E702
525
481
526
482
527
- if t .TYPE_CHECKING :
528
-
529
- class PNamespaceSignal (t .Protocol ):
530
- def __call__ (self , name : str , doc : str | None = None ) -> NamedSignal : ...
531
-
532
- # Python < 3.9
533
- _NamespaceBase = dict [str , NamedSignal ] # type: ignore[misc]
534
- else :
535
- _NamespaceBase = dict
536
-
537
-
538
- class Namespace (_NamespaceBase ):
483
+ class Namespace (dict [str , NamedSignal ]):
539
484
"""A dict mapping names to signals."""
540
485
541
486
def signal (self , name : str , doc : str | None = None ) -> NamedSignal :
@@ -551,71 +496,17 @@ def signal(self, name: str, doc: str | None = None) -> NamedSignal:
551
496
return self [name ]
552
497
553
498
554
- class _WeakNamespace (WeakValueDictionary ): # type: ignore[type-arg]
555
- """A weak mapping of names to signals.
556
-
557
- Automatically cleans up unused signals when the last reference goes out
558
- of scope. This namespace implementation provides similar behavior to Blinker
559
- <= 1.2.
560
-
561
- .. deprecated:: 1.3
562
- Will be removed in Blinker 1.9.
563
-
564
- .. versionadded:: 1.3
565
- """
566
-
567
- def __init__ (self ) -> None :
568
- warnings .warn (
569
- "'WeakNamespace' is deprecated and will be removed in Blinker 1.9."
570
- " Use 'Namespace' instead." ,
571
- DeprecationWarning ,
572
- stacklevel = 2 ,
573
- )
574
- super ().__init__ ()
575
-
576
- def signal (self , name : str , doc : str | None = None ) -> NamedSignal :
577
- """Return the :class:`NamedSignal` for the given ``name``, creating it
578
- if required. Repeated calls with the same name return the same signal.
579
-
580
- :param name: The name of the signal.
581
- :param doc: The docstring of the signal.
582
- """
583
- if name not in self :
584
- self [name ] = NamedSignal (name , doc )
585
-
586
- return self [name ] # type: ignore[no-any-return]
499
+ class _PNamespaceSignal (t .Protocol ):
500
+ def __call__ (self , name : str , doc : str | None = None ) -> NamedSignal : ...
587
501
588
502
589
503
default_namespace : Namespace = Namespace ()
590
504
"""A default :class:`Namespace` for creating named signals. :func:`signal`
591
505
creates a :class:`NamedSignal` in this namespace.
592
506
"""
593
507
594
- signal : PNamespaceSignal = default_namespace .signal
508
+ signal : _PNamespaceSignal = default_namespace .signal
595
509
"""Return a :class:`NamedSignal` in :data:`default_namespace` with the given
596
510
``name``, creating it if required. Repeated calls with the same name return the
597
511
same signal.
598
512
"""
599
-
600
-
601
- def __getattr__ (name : str ) -> t .Any :
602
- if name == "receiver_connected" :
603
- warnings .warn (
604
- "The global 'receiver_connected' signal is deprecated and will be"
605
- " removed in Blinker 1.9. Use 'Signal.receiver_connected' and"
606
- " 'Signal.receiver_disconnected' instead." ,
607
- DeprecationWarning ,
608
- stacklevel = 2 ,
609
- )
610
- return _receiver_connected
611
-
612
- if name == "WeakNamespace" :
613
- warnings .warn (
614
- "'WeakNamespace' is deprecated and will be removed in Blinker 1.9."
615
- " Use 'Namespace' instead." ,
616
- DeprecationWarning ,
617
- stacklevel = 2 ,
618
- )
619
- return _WeakNamespace
620
-
621
- raise AttributeError (name )
0 commit comments