diff options
| -rw-r--r-- | CHANGES.rst | 4 | ||||
| -rw-r--r-- | src/pyramid/scripting.py | 2 | ||||
| -rw-r--r-- | tests/test_scripting.py | 24 |
3 files changed, 30 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 70ee43b96..70f5bbb64 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -78,6 +78,10 @@ Features in testing. See https://github.com/Pylons/pyramid/pull/3559 +- Finished callbacks are now executed as part of the ``closer`` that is + invoked as part of ``pyramid.scripting.prepare`` and + ``pyramid.paster.boostrap``. + Deprecations ------------ diff --git a/src/pyramid/scripting.py b/src/pyramid/scripting.py index abcdd1030..4178b6526 100644 --- a/src/pyramid/scripting.py +++ b/src/pyramid/scripting.py @@ -94,6 +94,8 @@ def prepare(request=None, registry=None): apply_request_extensions(request) def closer(): + if request.finished_callbacks: + request._process_finished_callbacks() ctx.end() root_factory = registry.queryUtility( diff --git a/tests/test_scripting.py b/tests/test_scripting.py index 8f74f35f8..b8a18f57e 100644 --- a/tests/test_scripting.py +++ b/tests/test_scripting.py @@ -1,3 +1,4 @@ +from collections import deque import unittest @@ -162,6 +163,20 @@ class Test_prepare(unittest.TestCase): self.assertEqual(request.context, root) self.assertEqual(request.registry, registry) + def test_closer_invokes_finished_callbacks(self): + finish_called = [False] + + def finished_callback(request): + finish_called[0] = True + + request = DummyRequest({}) + request.registry = self._makeRegistry() + info = self._callFUT(request=request) + request.add_finished_callback(finished_callback) + closer = info['closer'] + closer() + self.assertTrue(finish_called[0]) + class Test__make_request(unittest.TestCase): def _callFUT(self, path='/', registry=None): @@ -234,6 +249,15 @@ class DummyRequest(object): def __init__(self, environ): self.environ = environ + self.finished_callbacks = deque() + + def add_finished_callback(self, cb): + self.finished_callbacks.append(cb) + + def _process_finished_callbacks(self): + while self.finished_callbacks: + cb = self.finished_callbacks.popleft() + cb(self) class DummyExtensions: |
