[PATCH 1 of 3 v2] cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails

Mads Kiilerich mads at kiilerich.com
Thu Oct 13 00:39:29 UTC 2016


# HG changeset patch
# User Mads Kiilerich <madski at unity3d.com>
# Date 1476317981 -7200
#      Thu Oct 13 02:19:41 2016 +0200
# Node ID 8db96226f748ff80003c0964bc4ece3b6c3e6d12
# Parent  b85fa6bf298be07804a74d8fdec0d19fdbc6d740
cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails

Python "delstructors" are terrible - this one because it assumed that __init__
had completed before it was called. That would not necessarily be the case if
the repository was read only or broken and saving the dirstate thus failed in
unexpected ways. That could give confusing warnings about missing '_active'
after failures.

To fix that, make sure all member variables are "declared" before doing
anything that possibly could fail. [Famous last words.]

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3502,10 +3502,11 @@ class dirstateguard(object):
 
     def __init__(self, repo, name):
         self._repo = repo
+        self._active = False
+        self._closed = False
         self._suffix = '.backup.%s.%d' % (name, id(self))
         repo.dirstate.savebackup(repo.currenttransaction(), self._suffix)
         self._active = True
-        self._closed = False
 
     def __del__(self):
         if self._active: # still active


More information about the Mercurial-devel mailing list