Skip to main content
https://github.com/lexsca/rollback/actions/workflows/checks.yml/badge.svg https://img.shields.io/pypi/v/rollback.svg https://img.shields.io/pypi/pyversions/rollback.svg https://img.shields.io/github/license/lexsca/rollback.svg https://img.shields.io/badge/code%20style-black-000000.svg

This is a simple Pythonic mechanism for rolling back multiple operations in a predictable way, usable as a context manager or a standalone instance (see Example usage). By default, errors are re-raised, but an explicit mode or call must be supplied to trigger a rollback. Valid modes are:

  • onError Boolean when True will roll back if an error is raised

  • onSuccess Boolean when True will roll back if an error is not raised

Both modes can be set to True to always rollback. A rollback can also be triggered manually by calling doRollback. Note that multiple calls to doRollback will only call the rollback steps once.

Errors can be supressed by setting raiseError to False. Note that errors from rollback steps will not be surpressed, regardless of the raiseError setting.

If a rollback is triggered, each step is called in a last in, first out order (LIFO). That is, the most recently added step is called first, the first step is called last.

Compatibility

Rollback has no external dependencies and is regularly tested with all currently supported versions of cPython.

Installation

Install from source:

python setup.py install

Install from PyPI:

pip install rollback

Example usage

from __future__ import print_function

from rollback import Rollback

# *always* rollback after exiting block, letting any error be re-raised
with Rollback(onError=True, onSuccess=True) as rollback:
  print('do a1')
  rollback.addStep(print, 'undo a1')
  print('do a2')
  rollback.addStep(print, 'undo a2')

# rollback *only* if *no* error is raised, letting any error be re-raised
with Rollback(onSuccess=True) as rollback:
  print('do b1')
  rollback.addStep(print, 'undo b1')
  print('do b2')
  rollback.addStep(print, 'undo b2')

# rollback manually
with Rollback() as rollback:
  print('do c1')
  rollback.addStep(print, 'undo c1')
  print('do c2')
  rollback.addStep(print, 'undo c2')
  rollback.doRollback()

# rollback *only* if an error is raised, suppressing the error
with Rollback(onError=True, raiseError=False) as rollback:
  print('do d1')
  rollback.addStep(print, 'undo d1')
  print('do d2')
  rollback.addStep(print, 'undo d2')
  raise RuntimeError('this is not re-raised')

# rollback *only* if an error is raised, letting the error be re-raised
with Rollback(onError=True) as rollback:
  print('do e1')
  rollback.addStep(print, 'undo e1')
  print('do e2')
  rollback.addStep(print, 'undo e2')
  raise RuntimeError('this is re-raised')

Produces output:

do a1
do a2
undo a2
undo a1
do b1
do b2
undo b2
undo b1
do c1
do c2
undo c2
undo c1
do d1
do d2
undo d2
undo d1
do e1
do e2
undo e2
undo e1
Traceback (most recent call last):
  File "example.py", line 41, in <module>
    raise RuntimeError('this is re-raised')
RuntimeError: this is re-raised

Release files for rollback 1.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rollback 1.0.11
File Size Uploaded
rollback-1.0.11.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rollback 1.0.11
File Interpreter ABI Platform
rollback-1.0.11-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / rollback-1.0.11.tar.gz

Download URL rollback-1.0.11.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
da6598c25cab0d62ec7c06ba6f80f06ee12ac091434637f34674d0661bca950b
BLAKE2b-256 checksum
How to use checksums
04e9e8ed08a7f0d9d47a406ff00b820f56cd6988aa4763d18451d8616c5df63f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / rollback-1.0.11-py3-none-any.whl

Download URL rollback-1.0.11-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b3e178dd35be82e526d7032e2bd904c04be9219a116376603ff032405e11b3d
BLAKE2b-256 checksum
How to use checksums
6042cd7437a00d2b3835df9e1e27dd84bea8a550c9e2747d23dc032c2f125cc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

1.0.11 This release

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page