-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic.py
More file actions
37 lines (31 loc) · 878 Bytes
/
atomic.py
File metadata and controls
37 lines (31 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from contextlib import contextmanager
@contextmanager
def transaction(*objs):
"""Revert all changes to objs if an exception occurs.
Example usage:
original_balance = bank.balance
with transaction(bank):
bank.transfer(stocks)
time.sleep(10000)
stocks.transfer(bank)
if bank.balance < original_balance:
raise DontInvestAfterAll
"""
originals = [obj.__dict__.copy() for obj in objs]
try:
yield
except Exception:
for obj, original in zip(objs, originals):
obj.__dict__ = original
raise
if __name__ == '__main__':
def obj(): pass
obj.attr = 1
try:
with transaction(obj):
obj.attr += 1
assert obj.attr == 2
raise TabError
except Exception:
pass
assert obj.attr == 1