Merge pull request #2354 from davidism/stream-session
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Only open session if request does not have open session
authored by David Lord · committed by GitHub · parents 49f3e96, 6637e20 · Browse files · 3 files·+29 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−8 removed
- tests:+14 added−0 removed
- other:+15 added−8 removed
Provenance
- Commit
3b678b7- Tree
2938955- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
49f3e96- Parent 2
6637e20
3 files changed+29 added−8 removed
modified:flask/ctx.py+11 added-8 removed
323 unmodified lines324 sys.exc_clear()325 326 _request_ctx_stack.push(self)327 328- # Open the session at the moment that the request context is329- # available. This allows a custom open_session method to use the330- # request context (e.g. code that access database information331- # stored on `g` instead of the appcontext).332- session_interface = self.app.session_interface333- self.session = session_interface.open_session(self.app, self.request)334-328+ # Open the session at the moment that the request context is available.329+ # This allows a custom open_session method to use the request context.330+ # Only open a new session if this is the first time the request was331+ # pushed, otherwise stream_with_context loses the session.332 if self.session is None:336- self.session = session_interface.make_null_session(self.app)333+ session_interface = self.app.session_interface334+ self.session = session_interface.open_session(335+ self.app, self.request336+ )337 338+ if self.session is None:339+ self.session = session_interface.make_null_session(self.app)340+341 def pop(self, exc=_sentinel):342 """Pops the request context and unbinds it by doing that. This will343 also trigger the execution of functions registered by the344 :meth:`~flask.Flask.teardown_request` decorator.71 unmodified linesmodified:tests/test_helpers.py+14 added
859 unmodified lines860 rv = client.get('/?name=World')861 assert rv.data == b'Hello World!'862 assert called == [42]863 864+ def test_stream_keeps_session(self, app, client):865+ @app.route('/')866+ def index():867+ flask.session['test'] = 'flask'868 869+ @flask.stream_with_context870+ def gen():871+ yield flask.session['test']872+873+ return flask.Response(gen())874+875+ rv = client.get('/')876+ assert rv.data == b'flask'877+878+879 class TestSafeJoin(object):880 def test_safe_join(self):881 # Valid combinations of *args and expected joined paths.882 passing = (64 unmodified linesmodified:CHANGES+4 added
65 unmodified lines66 ``BadRequestKeyError`` has a message with the bad key in debug mode instead67 of the generic bad request message. (`#2348`_)68 - Allow registering new tags with ``TaggedJSONSerializer`` to support69 storing other types in the session cookie. (`#2352`_)70+- Only open the session if the request has not been pushed onto the context71+ stack yet. This allows ``stream_with_context`` generators to access the same72+ session that the containing view uses. (`#2354`_)73 74 .. _#1489: https://github.com/pallets/flask/pull/148975 .. _#1621: https://github.com/pallets/flask/pull/162176 .. _#1898: https://github.com/pallets/flask/pull/189812 unmodified lines89 .. _#2319: https://github.com/pallets/flask/pull/231990 .. _#2326: https://github.com/pallets/flask/pull/232691 .. _#2348: https://github.com/pallets/flask/pull/234892 .. _#2352: https://github.com/pallets/flask/pull/235293+.. _#2354: https://github.com/pallets/flask/pull/235494 95 Version 0.12.296 --------------97 617 unmodified lines