Feature request: An exception-safe undo API #1701
Labels
Effort: Medium
Issue should take < 1 month
Impact: Low
Issue is a papercut or has a good, supported workaround
Type: Enhancement
Issue is a small enhancement to existing functionality
Milestone
It feels like the
begin_undo_actions/commit_undo_actions
API is very difficult to use correctly, which is troubling because messing with Undo history is honestly already pretty scary business! As a typical example of how things can go wrong: Suppose a function callsbegin_undo_actions()
, then raises an Exception before it callscommit_undo_actions()
. Then anything done after this could end up recorded as part of the same Undo action.I think the ideal API for something like this would be a context manager. Ideally, it would also have a way to automatically cancel changes made if the
with
block is exited via an uncaught exception. Something like:(Bikeshed: if
auto_cancel=True
sounds like a reasonable default, maybe the option should instead bekeep_on_error
which defaults toFalse
?)I tried to write one myself as a wrapper around the existing API, and it was pretty tricky. I had to simulate "canceling" by committing and then undoing, but this is not safe to do if no changes have been made yet (it would commit nothing and undo the previous change instead!). It ended up looking like this:
The text was updated successfully, but these errors were encountered: