changeset 27862:b2145c195f24

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'): # ...
author Bryan O'Sullivan <bryano@fb.com>
date Fri, 15 Jan 2016 13:14:47 -0800
parents 3315a9c2019c
children ed59ae8b025e
files mercurial/transaction.py
diffstat 1 files changed, 8 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/transaction.py	Fri Jan 15 13:14:49 2016 -0800
+++ b/mercurial/transaction.py	Fri Jan 15 13:14:47 2016 -0800
@@ -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