atomictempfile: add context manager support
authorMartijn Pieters <mjpieters@fb.com>
Thu, 23 Jun 2016 18:21:25 +0100
changeset 29394 6d96658a22b0
parent 29393 50269a4dce61
child 29395 4c8026babe8c
atomictempfile: add context manager support Close the file (moving it in place) on clean context exit, discard when there has been an exception.
mercurial/util.py
tests/test-atomictempfile.py
--- 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
 
--- a/tests/test-atomictempfile.py	Thu Jun 23 18:20:58 2016 +0100
+++ b/tests/test-atomictempfile.py	Thu Jun 23 18:21:25 2016 +0100
@@ -96,6 +96,24 @@
         self.assertTrue(file.read(), b'foobar\n')
         file.discard()
 
+    def testcontextmanagersuccess(self):
+        """When the context closes, the file is closed"""
+        with atomictempfile('foo') as f:
+            self.assertFalse(os.path.isfile('foo'))
+            f.write(b'argh\n')
+        self.assertTrue(os.path.isfile('foo'))
+
+    def testcontextmanagerfailure(self):
+        """On exception, the file is discarded"""
+        try:
+            with atomictempfile('foo') as f:
+                self.assertFalse(os.path.isfile('foo'))
+                f.write(b'argh\n')
+                raise ValueError
+        except ValueError:
+            pass
+        self.assertFalse(os.path.isfile('foo'))
+
 if __name__ == '__main__':
     import silenttestrunner
     silenttestrunner.main(__name__)