@@ -250,8 +250,7 @@ def __hash__(self):
250
250
251
251
252
252
class CallbackRegistry (object ):
253
- """
254
- Handle registering and disconnecting for a set of signals and
253
+ """Handle registering and disconnecting for a set of signals and
255
254
callbacks:
256
255
257
256
>>> def oneat(x):
@@ -286,8 +285,24 @@ class CallbackRegistry(object):
286
285
functions). This technique was shared by Peter Parente on his
287
286
`"Mindtrove" blog
288
287
<http://mindtrove.info/python-weak-references/>`_.
288
+
289
+
290
+ Parameters
291
+ ----------
292
+ exception_handler : callable, optional
293
+ If provided must have signature ::
294
+
295
+ def handler(exception: Exception) -> None:
296
+
297
+ If not None this function will be called with any `Exception`
298
+ subclass raised by the callbacks in `CallbackRegistry.process`.
299
+ The handler may either consume the exception or re-raise.
300
+
301
+ The callable must be pickle-able.
302
+
289
303
"""
290
- def __init__ (self ):
304
+ def __init__ (self , exception_handler = None ):
305
+ self .exception_handler = exception_handler
291
306
self .callbacks = dict ()
292
307
self ._cid = 0
293
308
self ._func_cid_map = {}
@@ -301,10 +316,10 @@ def __init__(self):
301
316
# http://bugs.python.org/issue12290).
302
317
303
318
def __getstate__ (self ):
304
- return True
319
+ return { 'exception_handler' : self . exception_handler }
305
320
306
321
def __setstate__ (self , state ):
307
- self .__init__ ()
322
+ self .__init__ (** state )
308
323
309
324
def connect (self , s , func ):
310
325
"""
@@ -365,6 +380,17 @@ def process(self, s, *args, **kwargs):
365
380
proxy (* args , ** kwargs )
366
381
except ReferenceError :
367
382
self ._remove_proxy (proxy )
383
+ # this does not capture KeyboardInterrupt, SystemExit,
384
+ # and GeneratorExit
385
+ except Exception as e :
386
+ if self .exception_handler is not None :
387
+ self .exception_handler (e )
388
+ else :
389
+ raise
390
+
391
+
392
+ def exception_printer (exception ):
393
+ traceback .print_exc ()
368
394
369
395
370
396
class silent_list (list ):
0 commit comments