Skip to content

Commit

Permalink
Reverse order of execution of post-request handlers. This fixes palle…
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jul 12, 2010
1 parent f1a6fbe commit d12d732
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -8,6 +8,9 @@ Version 0.6

Release date to be announced, codename to be decided.

- after request functions are now called in reverse order of
registration.

Version 0.5.1
-------------

Expand Down
11 changes: 11 additions & 0 deletions docs/upgrading.rst
Expand Up @@ -19,6 +19,17 @@ installation, make sure to pass it the ``-U`` parameter::

$ easy_install -U Flask

Version 0.6
-----------

Flask 0.6 comes with a backwards incompatible change which affects the
order of after-request handlers. Previously they were called in the order
of the registration, now they are called in reverse order. This change
was made so that Flask behaves more like people expected it to work and
how other systems handle request pre- and postprocessing. If you
dependend on the order of execution of post-request functions, be sure to
change the order.

Version 0.5
-----------

Expand Down
8 changes: 6 additions & 2 deletions flask/app.py
Expand Up @@ -707,6 +707,10 @@ def process_response(self, response):
before it's sent to the WSGI server. By default this will
call all the :meth:`after_request` decorated functions.
.. versionchanged:: 0.5
As of Flask 0.5 the functions registered for after request
execution are called in reverse order of registration.
:param response: a :attr:`response_class` object.
:return: a new response object or the same, has to be an
instance of :attr:`response_class`.
Expand All @@ -717,9 +721,9 @@ def process_response(self, response):
self.save_session(ctx.session, response)
funcs = ()
if mod and mod in self.after_request_funcs:
funcs = chain(funcs, self.after_request_funcs[mod])
funcs = reversed(self.after_request_funcs[mod])
if None in self.after_request_funcs:
funcs = chain(funcs, self.after_request_funcs[None])
funcs = chain(funcs, reversed(self.after_request_funcs[None]))
for handler in funcs:
response = handler(response)
return response
Expand Down
24 changes: 24 additions & 0 deletions tests/flask_tests.py
Expand Up @@ -318,6 +318,30 @@ def fails():
assert 'Internal Server Error' in rv.data
assert len(called) == 1

def test_before_after_request_order(self):
called = []
app = flask.Flask(__name__)
@app.before_request
def before1():
called.append(1)
@app.before_request
def before2():
called.append(2)
@app.after_request
def after1(response):
called.append(4)
return response
@app.after_request
def after1(response):
called.append(3)
return response
@app.route('/')
def index():
return '42'
rv = app.test_client().get('/')
assert rv.data == '42'
assert called == [1, 2, 3, 4]

def test_error_handling(self):
app = flask.Flask(__name__)
@app.errorhandler(404)
Expand Down

0 comments on commit d12d732

Please sign in to comment.