# HG changeset patch # User Bryan O'Sullivan # Date 1452892487 28800 # Node ID b2145c195f24a545a2f7051e5830c7ebb40734da # Parent 3315a9c2019c6eaef0b010b276e932d4ef74d7ec transaction: turn a transaction into a Python context manager This lets us greatly simply acquire/release cycles. If the block completes without raising an exception, the transaction is closed. Code pattern before: try: tr = repo.transaction('x') # zillions of lines of code tr.close() finally: tr.release() And after: with tr.transaction('x'): # ... diff --git a/mercurial/transaction.py b/mercurial/transaction.py --- a/mercurial/transaction.py +++ b/mercurial/transaction.py @@ -333,6 +333,14 @@ if self.count > 0 and self.usages == 0: self._abort() + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + if exc_type is None: + self.close() + self.release() + def running(self): return self.count > 0