revlog: use context manager for index file lifetime in checkinlinesize
authorBoris Feld <boris.feld@octobus.net>
Mon, 05 Feb 2018 17:34:57 +0100
changeset 35971 0f2c51afafb2
parent 35970 69cf2e422490
child 35972 82afb1a5ed94
revlog: use context manager for index file lifetime in checkinlinesize This is clearer, safer and more modern.
mercurial/revlog.py
--- a/mercurial/revlog.py	Mon Feb 05 17:34:47 2018 +0100
+++ b/mercurial/revlog.py	Mon Feb 05 17:34:57 2018 +0100
@@ -1863,16 +1863,16 @@
             for r in self:
                 df.write(self._getsegmentforrevs(r, r)[1])
 
-        fp = self._indexfp('w')
-        self.version &= ~FLAG_INLINE_DATA
-        self._inline = False
-        for i in self:
-            e = self._io.packentry(self.index[i], self.node, self.version, i)
-            fp.write(e)
+        with self._indexfp('w') as fp:
+            self.version &= ~FLAG_INLINE_DATA
+            self._inline = False
+            io = self._io
+            for i in self:
+                e = io.packentry(self.index[i], self.node, self.version, i)
+                fp.write(e)
 
-        # if we don't call close, the temp file will never replace the
-        # real index
-        fp.close()
+            # the temp file replace the real index when we exit the context
+            # manager
 
         tr.replace(self.indexfile, trindex * self._io.size)
         self._chunkclear()