changeset 36008:1f2b8a644dd4

revlog: use context manager for index file life time in __init__ This is clearer, safer and more modern.
author Boris Feld <boris.feld@octobus.net>
date Mon, 05 Feb 2018 17:34:19 +0100
parents 91b3b11565e5
children 69cf2e422490
files mercurial/revlog.py
diffstat 1 files changed, 6 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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