From 0311e0b5a22181cba56db2b7520a5ba6df831f27 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Fri, 22 Jul 2011 12:04:42 -0400 Subject: Fixing a bug in the `after_commit` hook implementation. Previously, when requests resulted in an HTTP 404 (or any request that didn't end up routing to a controller), the `after_commit` would fail because the code assumed a state.controller existed. --- pecan/hooks.py | 14 +++++++++++--- tests/test_hooks.py | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pecan/hooks.py b/pecan/hooks.py index 2795f95..e8848b7 100644 --- a/pecan/hooks.py +++ b/pecan/hooks.py @@ -160,10 +160,18 @@ class TransactionHook(PecanHook): self.rollback() else: self.commit() + + # + # If a controller was routed to, find any + # after_commit actions it may have registered, and perform + # them. + # controller = getattr(state, 'controller', None) - actions = _cfg(controller).get('after_commit', []) - for action in actions: - action() + if controller is not None: + actions = _cfg(controller).get('after_commit', []) + for action in actions: + action() + self.clear() class RequestViewerHook(PecanHook): diff --git a/tests/test_hooks.py b/tests/test_hooks.py index 5a0d069..e50d5ec 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -609,6 +609,15 @@ class TestTransactionHook(object): assert run_hook[5] == 'action-two' assert run_hook[6] == 'clear' + run_hook = [] + + response = app.get('/fourohfour', status=404) + assert response.status_int == 404 + + assert len(run_hook) == 2 + assert run_hook[0] == 'start_ro' + assert run_hook[1] == 'clear' + def test_transaction_hook_with_transactional_decorator(self): run_hook = [] -- cgit v1.2.1