summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Petrello <lists@ryanpetrello.com>2011-07-22 12:04:42 -0400
committerRyan Petrello <lists@ryanpetrello.com>2011-07-22 12:04:42 -0400
commit0311e0b5a22181cba56db2b7520a5ba6df831f27 (patch)
tree48142fe539903704a95c3ec4ec25e0fbb49d3089
parent27ff1e4492d3d8613e9bdce1d760e7184238abe5 (diff)
downloadpecan-0311e0b5a22181cba56db2b7520a5ba6df831f27.tar.gz
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.
-rw-r--r--pecan/hooks.py14
-rw-r--r--tests/test_hooks.py9
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 = []