Skip to content
Myr

Fix #2935: Copy current session object in copy_current_request_context (#2936)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add session to RequestContext.copy()

authored by Dave Chevell · committed by David Lord · parent 7e714bd · Browse files · 3 files+22 added−6 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+22 added−6 removed

  • tests:+4 added−0 removed
  • other:+18 added−6 removed

3 files+22 added−6 removed

modified:flask/ctx.py+12 added-6 removed
120 unmodified lines
121def copy_current_request_context(f):122 """A helper function that decorates a function to retain the current123 request context. This is useful when working with greenlets. The moment124 the function is decorated a copy of the request context is created and125 then pushed when the function is called.125 then pushed when the function is called. The current session is also126 included in the copied request context.127128 Example::129130 import gevent
2 unmodified lines
133 @app.route('/')134 def index():135 @copy_current_request_context136 def do_some_work():136 # do some work here, it can access flask.request like you137 # would otherwise in the view function.137 # do some work here, it can access flask.request or138 # flask.session like you would otherwise in the view function.139 ...140 gevent.spawn(do_some_work)141 return 'Regular response'142
133 unmodified lines
276 sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in277 that situation, otherwise your unittests will leak memory.278 """279279 def __init__(self, app, environ, request=None):280 def __init__(self, app, environ, request=None, session=None):281 self.app = app282 if request is None:283 request = app.request_class(environ)284 self.request = request285 self.url_adapter = app.create_url_adapter(self.request)286 self.flashes = None286 self.session = None287 self.session = session288289 # Request contexts can be pushed multiple times and interleaved with290 # other request contexts. Now only if the last level is popped we291 # get rid of them. Additionally if an application context is missing
29 unmodified lines
321 move a request context to a different thread unless access to the322 request object is locked.323324 .. versionadded:: 0.10325326 .. versionchanged:: 1.1327 The current session object is used instead of reloading the original328 data. This prevents `flask.session` pointing to an out-of-date object.329 """330 return self.__class__(self.app,331 environ=self.request.environ,327 request=self.request332 request=self.request,333 session=self.session334 )335336 def match_request(self):337 """Can be overridden by a subclass to hook into the matching
126 unmodified lines
modified:tests/test_reqctx.py+4 added
154 unmodified lines
155 greenlets = []156157 @app.route('/')158 def index():159 flask.session['fizz'] = 'buzz'160 reqctx = flask._request_ctx_stack.top.copy()161162 def g():163 assert not flask.request
2 unmodified lines
166 assert flask.request167 assert flask.current_app == app168 assert flask.request.path == '/'169 assert flask.request.args['foo'] == 'bar'170 assert flask.session.get('fizz') == 'buzz'171 assert not flask.request172 return 42173174 greenlets.append(greenlet(g))
9 unmodified lines
184 greenlets = []185186 @app.route('/')187 def index():188 flask.session['fizz'] = 'buzz'189 reqctx = flask._request_ctx_stack.top.copy()190191 @flask.copy_current_request_context192 def g():193 assert flask.request194 assert flask.current_app == app195 assert flask.request.path == '/'196 assert flask.request.args['foo'] == 'bar'197 assert flask.session.get('fizz') == 'buzz'198 return 42199200 greenlets.append(greenlet(g))201 return 'Hello World!'
30 unmodified lines
modified:CHANGES.rst+6 added
7 unmodified lines
8-----------910Unreleased1112- :meth:`flask.RequestContext.copy` includes the current session13 object in the request context copy. This prevents ``flask.session`` 14 pointing to an out-of-date object. (`#2935`)1516.. _#2935: https://github.com/pallets/flask/issues/2935171819Version 1.0.320-------------2122Unreleased
924 unmodified lines