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

mail settings
Submitter Mads Kiilerich
Date Oct. 14, 2016, 1:17 a.m.
Message ID <426994acbf9c9b78aa69.1476407848@localhost.localdomain>
Download mbox | patch
Permalink /patch/17076/
State Accepted
Delegated to: Pierre-Yves David
Headers show


Mads Kiilerich - Oct. 14, 2016, 1:17 a.m.
# HG changeset patch
# User Mads Kiilerich <>
# Date 1476402795 -7200
#      Fri Oct 14 01:53:15 2016 +0200
# Node ID 426994acbf9c9b78aa6922a279dca4091b6508dc
# Parent  c0410814002f467c24ef07ce73850ba15b306f8e
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/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -3508,10 +3508,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