Patchwork D12122: dirstate: use a context manager when writing the dirstate

login
register
mail settings
Submitter phabricator
Date Jan. 31, 2022, 3:30 p.m.
Message ID <differential-rev-PHID-DREV-ioermiaubwbhr2lm2mlw-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50448/
State New
Headers show

Comments

phabricator - Jan. 31, 2022, 3:30 p.m.
marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This make sure the file is closed in a timely manner.
  
  We define a lambda for the file opening. It might seems a bit overkill here but
  a future changeset will need to do more of those.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D12122

AFFECTED FILES
  mercurial/dirstate.py

CHANGE DETAILS




To: marmoute, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -713,8 +713,9 @@ 
             )
             return
 
-        st = self._opener(filename, b"w", atomictemp=True, checkambig=True)
-        self._writedirstate(tr, st)
+        file = lambda f: self._opener(f, b"w", atomictemp=True, checkambig=True)
+        with file(self._filename) as f:
+            self._writedirstate(tr, f)
 
     def addparentchangecallback(self, category, callback):
         """add a callback to be called when the wd parents are changed