[Zodb-checkins] SVN: ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/ - beforeCommitOrdered() -> addBeforeCommitHook()

Julien Anguenot ja at nuxeo.com
Thu Aug 11 07:59:07 EDT 2005


Log message for revision 37862:
   - beforeCommitOrdered() -> addBeforeCommitHook()
   - Change signature addBeforeCommitHook(self, hook, args=(), kws=None, order=0)
   - Use a global index attribute on the transaction to simplify the
  algo with bisect
  
  

Changed:
  U   ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/_transaction.py
  U   ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/tests/test_transaction.py

-=-
Modified: ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/_transaction.py
===================================================================
--- ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/_transaction.py	2005-08-11 11:52:17 UTC (rev 37861)
+++ ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/_transaction.py	2005-08-11 11:59:07 UTC (rev 37862)
@@ -239,10 +239,12 @@
         # raised, incorporating this traceback.
         self._failure_traceback = None
 
-        # Holds (hook, args, kws) triples added by beforeCommitHook.
-        # TODO:  in Python 2.4, change to collections.deque; lists can be
-        # inefficient for FIFO access of this kind.
+        # Holds (order, index, hook, args, kws) added by
+        # addbeforeCommitHook.  TODO: in Python 2.4, change to
+        # collections.deque; lists can be inefficient for FIFO access
+        # of this kind.
         self._before_commit = []
+        self._before_commit_index = 0
 
     # Raise TransactionFailedError, due to commit()/join()/register()
     # getting called when the current transaction has already suffered
@@ -412,21 +414,24 @@
         # Don't return the hook order and index values because of
         # backward compatibility. As well, those are internals
         return iter([x[2:5] for x in self._before_commit])
-
-    def beforeCommitHookOrdered(self, __hook, __order, *args, **kws):
-        if not isinstance(__order, int):
+    
+    def addBeforeCommitHook(self, hook, args=(), kws=None, order=0):
+        if not isinstance(order, int):
             raise ValueError("An integer value is required "
                              "for the order argument")
+        if kws is None:
+            kws = {}
         # `index` goes up by 1 on each append.  Then no two tuples can
         # compare equal, and indeed no more than the `order` and
         # `index` fields ever get compared when the tuples are compared
         # (because no two `index` fields are equal).
-        index = len([x[1] for x in self._before_commit if x[1] == __order])
-        bisect.insort(self._before_commit, (__order, index, __hook, args, kws))
+        bisect.insort(self._before_commit, (order, self._before_commit_index,
+                                            hook, tuple(args), kws))
+        self._before_commit_index += 1
 
-    def beforeCommitHook(self, __hook, *args, **kws):
+    def beforeCommitHook(self, hook, *args, **kws):
         # Default order is zero (0)
-        self.beforeCommitHookOrdered(__hook, 0, *args, **kws)
+        self.addBeforeCommitHook(hook, args, kws, order=0)
 
     def _callBeforeCommitHooks(self):
         # Call all hooks registered, allowing further registrations
@@ -434,6 +439,7 @@
         while self._before_commit:
             order, index, hook, args, kws = self._before_commit.pop(0)
             hook(*args, **kws)
+        self._before_commit_index = 0
 
     def _commitResources(self):
         # Execute the two-phase commit protocol.

Modified: ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/tests/test_transaction.py
===================================================================
--- ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/tests/test_transaction.py	2005-08-11 11:52:17 UTC (rev 37861)
+++ ZODB/branches/anguenot-ordering-beforecommitsubscribers/src/transaction/tests/test_transaction.py	2005-08-11 11:59:07 UTC (rev 37862)
@@ -418,22 +418,22 @@
     def test_01_beforecommithook_order_exceptions(self):
         # string
         t = transaction.Transaction()
-        self.assertRaises(ValueError, t.beforeCommitHookOrdered,
-                          hook, 'string')
+        self.assertRaises(ValueError, t.addBeforeCommitHook,
+                          hook, order='string')
 
     def test_02_beforecommithook_order_exceptions(self):
         # float
         t = transaction.Transaction()
-        self.assertRaises(ValueError, t.beforeCommitHookOrdered,
-                          hook, 1.2)
+        self.assertRaises(ValueError, t.addBeforeCommitHook,
+                          hook, order=1.2)
 
     def test_03_beforecommithook_order_exceptions(self):
         # object
         t = transaction.Transaction()
         class foo:
             pass
-        self.assertRaises(ValueError, t.beforeCommitHookOrdered,
-                          hook, foo())
+        self.assertRaises(ValueError, t.addBeforeCommitHook,
+                          hook, order=foo())
 
     # XXX if the type check for whatever reasons gets more complex one
     # day just add some more tests in here
@@ -578,8 +578,8 @@
       >>> reset_log()
     """
 
-def test_beforeCommitHookOrdered():
-    """Test the beforeCommitHookOrdered with order arguments.
+def test_addBeforeCommitHook():
+    """Test the addBeforeCommitHook with order arguments.
 
     Let's define a hook to call, and a way to see that it was called.
 
@@ -595,7 +595,7 @@
 
       >>> import transaction
       >>> t = transaction.begin()
-      >>> t.beforeCommitHookOrdered(hook, 0, '1')
+      >>> t.addBeforeCommitHook(hook, '1', order=0)
 
     We can see that the hook is indeed registered.
 
@@ -606,7 +606,7 @@
     Let's add another one with a smaller order. It will be registered
     to be call at first
 
-      >>> t.beforeCommitHookOrdered(hook, -999999, '2')
+      >>> t.addBeforeCommitHook(hook, '2', order=-999999)
       >>> [(hook.func_name, args, kws)
       ...  for hook, args, kws in t.getBeforeCommitHooks()]
       [('hook', ('2',), {}), ('hook', ('1',), {})]
@@ -614,7 +614,7 @@
     Let's add another one with a bigger order. It will be registered
     to be call at last
     
-      >>> t.beforeCommitHookOrdered(hook, 999999, '3')
+      >>> t.addBeforeCommitHook(hook, '3', order=999999)
       >>> for hook, args, kws in t.getBeforeCommitHooks():
       ...     print (hook.func_name, args, kws)
       ('hook', ('2',), {})
@@ -625,7 +625,7 @@
     Now, we will check that the insertion with the same order values
     respect the order of the registration.
 
-      >>> t.beforeCommitHookOrdered(hook, 0, '4')
+      >>> t.addBeforeCommitHook(hook, '4', order=0)
       >>> for hook, args, kws in t.getBeforeCommitHooks():
       ...     print (hook.func_name, args, kws)
       ('hook', ('2',), {})
@@ -633,7 +633,7 @@
       ('hook', ('4',), {})
       ('hook', ('3',), {})
 
-      >>> t.beforeCommitHookOrdered(hook, 999999, '5')
+      >>> t.addBeforeCommitHook(hook, '5', order=999999)
       >>> for hook, args, kws in t.getBeforeCommitHooks():
       ...     print (hook.func_name, args, kws)
       ('hook', ('2',), {})
@@ -642,7 +642,7 @@
       ('hook', ('3',), {})
       ('hook', ('5',), {})
 
-      >>> t.beforeCommitHookOrdered(hook, -999999, '6')
+      >>> t.addBeforeCommitHook(hook, '6', order=-999999)
       >>> for hook, args, kws in t.getBeforeCommitHooks():
       ...     print (hook.func_name, args, kws)
       ('hook', ('2',), {})
@@ -659,7 +659,7 @@
       >>> def hook2(arg='no_arg', kw1='no_kw1', kw2='no_kw2'):
       ...     log.append("arg %r kw1 %r kw2 %r" % (arg, kw1, kw2))
     
-      >>> t.beforeCommitHookOrdered(hook2, 0, '8')
+      >>> t.addBeforeCommitHook(hook2, '8', order=0)
       >>> for hook, args, kws in t.getBeforeCommitHooks():
       ...     print (hook.func_name, args, kws)
       ('hook', ('2',), {})



More information about the Zodb-checkins mailing list