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? ...@@ -2,6 +2,8 @@ What's new in ZODB3 3.5a0?
========================== ==========================
Release date: DD-MMM-YYYY 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? What's new in ZODB3 3.4a5?
========================== ==========================
......
...@@ -359,6 +359,8 @@ class Transaction(object): ...@@ -359,6 +359,8 @@ class Transaction(object):
ft.writelines(traceback.format_exception_only(t, v)) ft.writelines(traceback.format_exception_only(t, v))
raise t, v, tb raise t, v, tb
def getBeforeCommitHooks(self):
return iter(self._before_commit)
def beforeCommitHook(self, hook, *args, **kws): def beforeCommitHook(self, hook, *args, **kws):
self._before_commit.append((hook, args, kws)) self._before_commit.append((hook, args, kws))
......
...@@ -189,6 +189,13 @@ class ITransaction(zope.interface.Interface): ...@@ -189,6 +189,13 @@ class ITransaction(zope.interface.Interface):
instead. 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): class ITransactionDeprecated(zope.interface.Interface):
"""Deprecated parts of the transaction API.""" """Deprecated parts of the transaction API."""
......
...@@ -419,6 +419,12 @@ def test_beforeCommitHook(): ...@@ -419,6 +419,12 @@ def test_beforeCommitHook():
>>> t = transaction.begin() >>> t = transaction.begin()
>>> t.beforeCommitHook(hook, '1') >>> 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 When transaction commit starts, the hook is called, with its
arguments. arguments.
...@@ -432,6 +438,8 @@ def test_beforeCommitHook(): ...@@ -432,6 +438,8 @@ def test_beforeCommitHook():
A hook's registration is consumed whenever the hook is called. Since A hook's registration is consumed whenever the hook is called. Since
the hook above was called, it's no longer registered: the hook above was called, it's no longer registered:
>>> len(list(t.getBeforeCommitHooks()))
0
>>> transaction.commit() >>> transaction.commit()
>>> log >>> log
[] []
...@@ -483,11 +491,21 @@ def test_beforeCommitHook(): ...@@ -483,11 +491,21 @@ def test_beforeCommitHook():
["arg '2' kw1 'no_kw1' kw2 'no_kw2'"] ["arg '2' kw1 'no_kw1' kw2 'no_kw2'"]
>>> reset_log() >>> reset_log()
If several hooks are defined, they are called in order. Let's register several hooks.
>>> t = transaction.begin() >>> t = transaction.begin()
>>> t.beforeCommitHook(hook, '4', kw1='4.1') >>> t.beforeCommitHook(hook, '4', kw1='4.1')
>>> t.beforeCommitHook(hook, '5', kw2='5.2') >>> 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() >>> t.commit()
>>> len(log) >>> len(log)
2 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