diff mercurial/util.py @ 29394:6d96658a22b0

atomictempfile: add context manager support Close the file (moving it in place) on clean context exit, discard when there has been an exception.
author Martijn Pieters <mjpieters@fb.com>
date Thu, 23 Jun 2016 18:21:25 +0100
parents 50269a4dce61
children 80880ad3fccd
line wrap: on
line diff
--- a/mercurial/util.py	Thu Jun 23 18:20:58 2016 +0100
+++ b/mercurial/util.py	Thu Jun 23 18:21:25 2016 +0100
@@ -1516,6 +1516,15 @@
         if safehasattr(self, '_fp'): # constructor actually did something
             self.discard()
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exctype, excvalue, traceback):
+        if exctype is not None:
+            self.discard()
+        else:
+            self.close()
+
 def makedirs(name, mode=None, notindexed=False):
     """recursive directory creation with parent mode inheritance