From 9d340ad7477774a19a7bdffc83d6cb8cb3e05edc Mon Sep 17 00:00:00 2001 From: florentx Date: Tue, 20 Apr 2010 22:46:36 +0200 Subject: [PATCH 1/3] Fix a doc oversight, and revert 5876a8fd. --- docs/testing.rst | 4 ++-- flask.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/testing.rst b/docs/testing.rst index 219825ea..0901792b 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -68,7 +68,7 @@ to create a temporary database and initialize it. The :func:`~tempfile.mkstemp` function does two things for us: it returns a low-level file handle and a random file name, the latter we use as database name. We just have to keep the `db_fd` around so that we can use -the :func:`os.close` function to close the function. +the :func:`os.close` function to close the file. If we now run that testsuite, we should see the following output:: @@ -76,7 +76,7 @@ If we now run that testsuite, we should see the following output:: ---------------------------------------------------------------------- Ran 0 tests in 0.000s - + OK Even though it did not run any tests, we already know that our flaskr diff --git a/flask.py b/flask.py index 3fed49d0..01f24eb9 100644 --- a/flask.py +++ b/flask.py @@ -270,6 +270,7 @@ def _get_package_path(name): # figure out if simplejson escapes slashes. This behaviour was changed # from one version to another without reason. if not json_available or '\\/' not in json.dumps('/'): + def _tojson_filter(*args, **kwargs): if __debug__: _assert_have_json() From 7cf5a9bf6e34fc57f82e560f01c408fbe603e9d4 Mon Sep 17 00:00:00 2001 From: florentx Date: Wed, 21 Apr 2010 00:40:43 +0200 Subject: [PATCH 2/3] Use a tuple to store _flashes, and simplify the flask.Request class. --- flask.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/flask.py b/flask.py index 01f24eb9..7eca40b9 100644 --- a/flask.py +++ b/flask.py @@ -56,10 +56,7 @@ class Request(RequestBase): :attr:`~flask.Flask.request_class` to your subclass. """ - def __init__(self, environ): - RequestBase.__init__(self, environ) - self.endpoint = None - self.view_args = None + endpoint = view_args = None @cached_property def json(self): @@ -170,7 +167,7 @@ def flash(message): :param message: the message to be flashed. """ - session['_flashes'] = (session.get('_flashes', [])) + [message] + session['_flashes'] = session.get('_flashes', ()) + (message,) def get_flashed_messages(): @@ -180,8 +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 From b6e73305392c9de052e51cca6fae8fee12ae231e Mon Sep 17 00:00:00 2001 From: florentx Date: Wed, 21 Apr 2010 01:23:09 +0200 Subject: [PATCH 3/3] 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 = []