changeset 42338:ec5bd3ab26bf

bookmarks: use context manager when writing files Differential Revision: https://phab.mercurial-scm.org/D6382
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 15 May 2019 10:13:29 -0700
parents 21eda240be07
children 2b77183ac477
files mercurial/bookmarks.py
diffstat 1 files changed, 5 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bookmarks.py	Wed May 15 10:54:36 2019 -0400
+++ b/mercurial/bookmarks.py	Wed May 15 10:13:29 2019 -0700
@@ -204,27 +204,18 @@
             rbm._writeactive()
 
         with repo.wlock():
-            file_ = repo.vfs('bookmarks', 'w', atomictemp=True,
-                             checkambig=True)
-            try:
-                self._write(file_)
-            except: # re-raises
-                file_.discard()
-                raise
-            finally:
-                file_.close()
+            with repo.vfs('bookmarks', 'w', atomictemp=True,
+                          checkambig=True) as f:
+                self._write(f)
 
     def _writeactive(self):
         if self._aclean:
             return
         with self._repo.wlock():
             if self._active is not None:
-                f = self._repo.vfs('bookmarks.current', 'w', atomictemp=True,
-                                   checkambig=True)
-                try:
+                with self._repo.vfs('bookmarks.current', 'w', atomictemp=True,
+                                   checkambig=True) as f:
                     f.write(encoding.fromlocal(self._active))
-                finally:
-                    f.close()
             else:
                 self._repo.vfs.tryunlink('bookmarks.current')
         self._aclean = True