changeset 50254:df76808d5f21

dirstate: use a context manager to handle the file used for writing the branch This is more modern.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 02 Mar 2023 14:50:17 +0100
parents 444fa55f5dd2
children fa04407bda7a
files mercurial/dirstate.py
diffstat 1 files changed, 5 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dirstate.py	Thu Mar 02 11:54:21 2023 +0100
+++ b/mercurial/dirstate.py	Thu Mar 02 14:50:17 2023 +0100
@@ -613,19 +613,17 @@
 
     def setbranch(self, branch):
         self.__class__._branch.set(self, encoding.fromlocal(branch))
-        f = self._opener(b'branch', b'w', atomictemp=True, checkambig=True)
-        try:
+        vfs = self._opener
+        with vfs(b'branch', b'w', atomictemp=True, checkambig=True) as f:
             f.write(self._branch + b'\n')
-            f.close()
-
             # make sure filecache has the correct stat info for _branch after
             # replacing the underlying file
+            #
+            # XXX do we actually need this,
+            # refreshing the attribute is quite cheap
             ce = self._filecache[b'_branch']
             if ce:
                 ce.refresh()
-        except:  # re-raises
-            f.discard()
-            raise
 
     def invalidate(self):
         """Causes the next access to reread the dirstate.