Patchwork D10775: revlog: simplify the try nesting in the `_writing` context

login
register
mail settings
Submitter phabricator
Date May 27, 2021, 7:06 a.m.
Message ID <differential-rev-PHID-DREV-5tlg5cwwolymqhsgsi3p-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49094/
State Superseded
Headers show

Comments

phabricator - May 27, 2021, 7:06 a.m.
marmoute created this revision.
Herald added a reviewer: indygreg.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Lets use a single try, with conditional cleanup. This make is easier to add a
  file handle dedicated to sidedata.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10775

AFFECTED FILES
  mercurial/revlog.py

CHANGE DETAILS




To: marmoute, indygreg, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -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()