Patchwork [08,of,13] revlog: use context manager for index file life time in __init__

login
register
mail settings
Submitter Boris Feld
Date Feb. 6, 2018, 1:21 p.m.
Message ID <da0b895c385747d82029.1517923289@FB>
Download mbox | patch
Permalink /patch/27363/
State New
Headers show

Comments

Boris Feld - Feb. 6, 2018, 1:21 p.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1517848459 -3600
#      Mon Feb 05 17:34:19 2018 +0100
# Node ID da0b895c385747d820294509d102d633419f8036
# Parent  d238ec45ba25f7c1ea9ec22aa11a4ec699c72740
# EXP-Topic revlog-fp
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r da0b895c3857
revlog: use context manager for index file life time in __init__

This is clearer, safer and more modern.

Patch

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -621,13 +621,12 @@  class revlog(object):
         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