Patchwork [5,of,7,main-line-of-work] transaction: pass the transaction to 'finalize' callback

login
register
mail settings
Submitter Pierre-Yves David
Date Nov. 11, 2014, 5:35 p.m.
Message ID <76544974a3959aeeff65.1415727312@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/6679/
State Accepted
Commit f60ed8cf4afc742e569e0e9d8c21a643485a4247
Headers show

Comments

Pierre-Yves David - Nov. 11, 2014, 5:35 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1415464298 0
#      Sat Nov 08 16:31:38 2014 +0000
# Node ID 76544974a3959aeeff65b0c21888b75e7a1e2db0
# Parent  0655518980f77b00f74ee2a13fecd0ce5273dfd4
transaction: pass the transaction to 'finalize' callback

The callback will likely needs to perform some operation related to the
transaction (eg: registering file update). So we better pass the current
transaction as the callback argument. Otherwise callback that needs it has to
rely on horrible weak reference trick.

This allow already allow us to slay a wild weak reference usage.

Patch

diff --git a/mercurial/changelog.py b/mercurial/changelog.py
--- a/mercurial/changelog.py
+++ b/mercurial/changelog.py
@@ -3,11 +3,10 @@ 
 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
 #
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-import weakref
 from node import bin, hex, nullid
 from i18n import _
 import util, error, revlog, encoding
 
 _defaultextra = {'branch': 'default'}
@@ -238,12 +237,11 @@  class changelog(revlog.revlog):
                 self._delaybuf = []
                 self.opener = _delayopener(self._realopener, self.indexfile,
                                            self._delaybuf)
         self._delayed = True
         tr.addpending('cl-%i' % id(self), self._writepending)
-        trp = weakref.proxy(tr)
-        tr.addfinalize('cl-%i' % id(self), lambda: self._finalize(trp))
+        tr.addfinalize('cl-%i' % id(self), self._finalize)
 
     def _finalize(self, tr):
         "finalize index updates"
         self._delayed = False
         self.opener = self._realopener
diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -302,10 +302,12 @@  class transaction(object):
 
     @active
     def addfinalize(self, category, callback):
         """add a callback to be called when the transaction is closed
 
+        The transaction will be given as callback first argument.
+
         Category is a unique identifier to allow overwriting old callbacks with
         newer callbacks.
         """
         self._finalizecallback[category] = callback
 
@@ -323,11 +325,11 @@  class transaction(object):
         '''commit the transaction'''
         if self.count == 1 and self.onclose is not None:
             self._generatefiles()
             categories = sorted(self._finalizecallback)
             for cat in categories:
-                self._finalizecallback[cat]()
+                self._finalizecallback[cat](self)
             self.onclose()
 
         self.count -= 1
         if self.count != 0:
             return