Commit 84f43c6d authored by Florent Guillaume's avatar Florent Guillaume

Merge of r30182 in the 3.4 branch:

A ``getBeforeCommitHooks()`` method has been added to Transaction. It
returns an iterable producing the registered beforeCommit hooks.
parent 1f2211ca
......@@ -2,6 +2,8 @@ What's new in ZODB3 3.5a0?
==========================
Release date: DD-MMM-YYYY
A ``getBeforeCommitHooks()`` method has been added to Transaction. It
returns an iterable producing the registered beforeCommit hooks.
What's new in ZODB3 3.4a5?
==========================
......
......@@ -359,6 +359,8 @@ class Transaction(object):
ft.writelines(traceback.format_exception_only(t, v))
raise t, v, tb
def getBeforeCommitHooks(self):
return iter(self._before_commit)
def beforeCommitHook(self, hook, *args, **kws):
self._before_commit.append((hook, args, kws))
......
......@@ -189,6 +189,13 @@ class ITransaction(zope.interface.Interface):
instead.
"""
def getBeforeCommitHooks():
"""Return iterable producing the registered beforeCommit hooks.
A triple (hook, args, kws) is produced for each registered hook.
The hooks are produced in the order in which they were registered.
"""
class ITransactionDeprecated(zope.interface.Interface):
"""Deprecated parts of the transaction API."""
......
......@@ -419,6 +419,12 @@ def test_beforeCommitHook():
>>> t = transaction.begin()
>>> t.beforeCommitHook(hook, '1')
We can see that the hook is indeed registered.
>>> [(hook.func_name, args, kws)
... for hook, args, kws in t.getBeforeCommitHooks()]
[('hook', ('1',), {})]
When transaction commit starts, the hook is called, with its
arguments.
......@@ -432,6 +438,8 @@ def test_beforeCommitHook():
A hook's registration is consumed whenever the hook is called. Since
the hook above was called, it's no longer registered:
>>> len(list(t.getBeforeCommitHooks()))
0
>>> transaction.commit()
>>> log
[]
......@@ -483,11 +491,21 @@ def test_beforeCommitHook():
["arg '2' kw1 'no_kw1' kw2 'no_kw2'"]
>>> reset_log()
If several hooks are defined, they are called in order.
Let's register several hooks.
>>> t = transaction.begin()
>>> t.beforeCommitHook(hook, '4', kw1='4.1')
>>> t.beforeCommitHook(hook, '5', kw2='5.2')
They are returned in the same order by getBeforeCommitHooks.
>>> [(hook.func_name, args, kws) #doctest: +NORMALIZE_WHITESPACE
... for hook, args, kws in t.getBeforeCommitHooks()]
[('hook', ('4',), {'kw1': '4.1'}),
('hook', ('5',), {'kw2': '5.2'})]
And commit also calls them in this order.
>>> t.commit()
>>> len(log)
2
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment