revlog: simplify the try nesting in the `_writing` context
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 26 May 2021 21:35:51 +0200
changeset 47335 1844a2e3401c
parent 47334 53ab13d6a5db
child 47336 27e9ed1217c5
revlog: simplify the try nesting in the `_writing` context Lets use a single try, with conditional cleanup. This make is easier to add a file handle dedicated to sidedata. Differential Revision: https://phab.mercurial-scm.org/D10775
mercurial/revlog.py
--- a/mercurial/revlog.py	Thu May 20 21:54:21 2021 +0200
+++ b/mercurial/revlog.py	Wed May 26 21:35:51 2021 +0200
@@ -2232,41 +2232,43 @@
         if self._writinghandles is not None:
             yield
         else:
-            r = len(self)
-            dsize = 0
-            if r:
-                dsize = self.end(r - 1)
-            dfh = None
-            if not self._inline:
-                try:
-                    dfh = self._datafp(b"r+")
-                    if self._docket is None:
-                        dfh.seek(0, os.SEEK_END)
-                    else:
-                        dfh.seek(self._docket.data_end, os.SEEK_SET)
-                except IOError as inst:
-                    if inst.errno != errno.ENOENT:
-                        raise
-                    dfh = self._datafp(b"w+")
-                transaction.add(self._datafile, dsize)
+            ifh = dfh = None
             try:
+                r = len(self)
+                # opening the data file.
+                dsize = 0
+                if r:
+                    dsize = self.end(r - 1)
+                dfh = None
+                if not self._inline:
+                    try:
+                        dfh = self._datafp(b"r+")
+                        if self._docket is None:
+                            dfh.seek(0, os.SEEK_END)
+                        else:
+                            dfh.seek(self._docket.data_end, os.SEEK_SET)
+                    except IOError as inst:
+                        if inst.errno != errno.ENOENT:
+                            raise
+                        dfh = self._datafp(b"w+")
+                    transaction.add(self._datafile, dsize)
+
+                # opening the index file.
                 isize = r * self.index.entry_size
                 ifh = self.__index_write_fp()
                 if self._inline:
                     transaction.add(self._indexfile, dsize + isize)
                 else:
                     transaction.add(self._indexfile, isize)
-                try:
-                    self._writinghandles = (ifh, dfh)
-                    try:
-                        yield
-                        if self._docket is not None:
-                            self._write_docket(transaction)
-                    finally:
-                        self._writinghandles = None
-                finally:
+                # exposing all file handle for writing.
+                self._writinghandles = (ifh, dfh)
+                yield
+                if self._docket is not None:
+                    self._write_docket(transaction)
+            finally:
+                self._writinghandles = None
+                if ifh is not None:
                     ifh.close()
-            finally:
                 if dfh is not None:
                     dfh.close()