@@ -175,13 +175,20 @@ class CallbackRegistry:
175175 The default handler prints the exception (with `traceback.print_exc`) if
176176 an interactive event loop is running; it re-raises the exception if no
177177 interactive event loop is running.
178+
179+ signals : list, optional
180+ If not None, *signals* is a list of signals that this registry handles:
181+ attempting to `process` or to `connect` to a signal not in the list
182+ throws a `ValueError`. The default, None, does not restrict the
183+ handled signals.
178184 """
179185
180186 # We maintain two mappings:
181187 # callbacks: signal -> {cid -> weakref-to-callback}
182188 # _func_cid_map: signal -> {weakref-to-callback -> cid}
183189
184- def __init__ (self , exception_handler = _exception_printer ):
190+ def __init__ (self , exception_handler = _exception_printer , * , signals = None ):
191+ self ._signals = None if signals is None else list (signals ) # Copy it.
185192 self .exception_handler = exception_handler
186193 self .callbacks = {}
187194 self ._cid_gen = itertools .count ()
@@ -217,6 +224,8 @@ def connect(self, signal, func):
217224 if signal == "units finalize" :
218225 _api .warn_deprecated (
219226 "3.5" , name = signal , obj_type = "signal" , alternative = "units" )
227+ if self ._signals is not None :
228+ _api .check_in_list (self ._signals , signal = signal )
220229 self ._func_cid_map .setdefault (signal , {})
221230 proxy = _weak_or_strong_ref (func , self ._remove_proxy )
222231 if proxy in self ._func_cid_map [signal ]:
@@ -280,6 +289,8 @@ def process(self, s, *args, **kwargs):
280289 All of the functions registered to receive callbacks on *s* will be
281290 called with ``*args`` and ``**kwargs``.
282291 """
292+ if self ._signals is not None :
293+ _api .check_in_list (self ._signals , signal = s )
283294 for cid , ref in list (self .callbacks .get (s , {}).items ()):
284295 func = ref ()
285296 if func is not None :
0 commit comments