77 """Returns a context manager that (un)register the provided callback.
79 The callback is immediately called if the interrupter has already been
80 triggered or if it is triggered during the registration. This is typically
81 useful for a solver implementation so that it does not have to test
82 `interrupted` to do the same thing it does in the callback. Simply
83 registering the callback is enough.
85 The callback function can't make calls to interruption_callback(), and
86 interrupt(). This would result is a deadlock. Reading `interrupted` is fine
89 Exceptions raised in the callback are raised on exit from the context
90 manager if no other error happens within the context. Else the exception is
94 callback: The callback.
100 CallbackError: When exiting the context manager if an exception was raised
103 callback_error: Optional[Exception] =
None
105 def protetected_callback():
106 """Calls callback() storing any exception in callback_error."""
107 nonlocal callback_error
110 except Exception
as e:
119 no_exception_in_context =
False
122 no_exception_in_context =
True
128 if callback_error
is not None:
129 if no_exception_in_context:
134 "An exception occurred in callback but is masked by another"
136 repr(callback_error),