revlog: use context manager for index file life time in __init__
authorBoris Feld <boris.feld@octobus.net>
Mon, 05 Feb 2018 17:34:19 +0100
changeset 35969 1f2b8a644dd4
parent 35968 91b3b11565e5
child 35970 69cf2e422490
revlog: use context manager for index file life time in __init__ This is clearer, safer and more modern.
mercurial/revlog.py
--- a/mercurial/revlog.py	Mon Feb 05 17:22:13 2018 +0100
+++ b/mercurial/revlog.py	Mon Feb 05 17:34:19 2018 +0100
@@ -621,13 +621,12 @@
         indexdata = ''
         self._initempty = True
         try:
-            f = self._indexfp()
-            if (mmapindexthreshold is not None and
-                    self.opener.fstat(f).st_size >= mmapindexthreshold):
-                indexdata = util.buffer(util.mmapread(f))
-            else:
-                indexdata = f.read()
-            f.close()
+            with self._indexfp() as f:
+                if (mmapindexthreshold is not None and
+                        self.opener.fstat(f).st_size >= mmapindexthreshold):
+                    indexdata = util.buffer(util.mmapread(f))
+                else:
+                    indexdata = f.read()
             if len(indexdata) > 0:
                 v = versionformat_unpack(indexdata[:4])[0]
                 self._initempty = False