From b6e73305392c9de052e51cca6fae8fee12ae231e Mon Sep 17 00:00:00 2001 From: florentx Date: Wed, 21 Apr 2010 01:23:09 +0200 Subject: [PATCH] Use setdefault() because it sets session.modified correctly. --- flask.py | 4 ++-- tests/flask_tests.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/flask.py b/flask.py index 7eca40b9..1f0fb7e6 100644 --- a/flask.py +++ b/flask.py @@ -167,7 +167,7 @@ def flash(message): :param message: the message to be flashed. """ - session['_flashes'] = session.get('_flashes', ()) + (message,) + session.setdefault('_flashes', []).append(message) def get_flashed_messages(): @@ -177,7 +177,7 @@ def get_flashed_messages(): """ flashes = _request_ctx_stack.top.flashes if flashes is None: - _request_ctx_stack.top.flashes = flashes = session.pop('_flashes', ()) + _request_ctx_stack.top.flashes = flashes = session.pop('_flashes', []) return flashes diff --git a/tests/flask_tests.py b/tests/flask_tests.py index 6a27373b..5f07fbe8 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -117,6 +117,18 @@ class BasicFunctionalityTestCase(unittest.TestCase): expect_exception(flask.session.__setitem__, 'foo', 42) expect_exception(flask.session.pop, 'foo') + def test_flashes(self): + app = flask.Flask(__name__) + app.secret_key = 'testkey' + + with app.test_request_context(): + assert not flask.session.modified + flask.flash('Zap') + flask.session.modified = False + flask.flash('Zip') + assert flask.session.modified + assert list(flask.get_flashed_messages()) == ['Zap', 'Zip'] + def test_request_processing(self): app = flask.Flask(__name__) evts = []