1. Nick Coghlan
  2. contextlib2
  3. Issues
Issue #9 wontfix

Accept a "callback_error" keyword argument in ExitStack

Nick Coghlan
repo owner created an issue

One common feature with cleanup operations is the desire to ignore, log, or otherwise handle exceptions that they trigger without replacing the original exception that triggered the need for cleanup.

A "callback_error" argument in the ExitStack on constructor could allow this to be customised.

Comments (7)

  1. Nick Coghlan reporter
    • changed status to open

    And since I keep forgetting: adding an exit callback to the stack that suppresses errors doesn't work, because that will *also* suppress errors that occur in the body of the with statement.

  2. Nick Coghlan reporter

    Alternative approaches to the same problem would be to accept a "wrap_callback" callable in init, or to provide a documented "wrap_callback" method that can be overridden by subclasses.

  3. Nick Coghlan reporter

    OTOH, it may be better to just ignore this completely and rely on context to preserve the details of the original exception if the cleanup goes horribly wrong...

  4. Nick Coghlan reporter

    Exception chaining in Python 3 was added to deal with this, and ExitStack is careful to preserve those semantics. If a context manager or callback wants to suppress errors internally, it can still do that.

  5. Log in to comment