@@ -122,7 +122,8 @@ def copy_current_request_context(f):
122
122
"""A helper function that decorates a function to retain the current
123
123
request context. This is useful when working with greenlets. The moment
124
124
the function is decorated a copy of the request context is created and
125
- then pushed when the function is called.
125
+ then pushed when the function is called. The current session is also
126
+ included in the copied request context.
126
127
127
128
Example::
128
129
@@ -133,8 +134,8 @@ def copy_current_request_context(f):
133
134
def index():
134
135
@copy_current_request_context
135
136
def do_some_work():
136
- # do some work here, it can access flask.request like you
137
- # would otherwise in the view function.
137
+ # do some work here, it can access flask.request or
138
+ # flask.session like you would otherwise in the view function.
138
139
...
139
140
gevent.spawn(do_some_work)
140
141
return 'Regular response'
@@ -276,14 +277,14 @@ class RequestContext(object):
276
277
that situation, otherwise your unittests will leak memory.
277
278
"""
278
279
279
- def __init__ (self , app , environ , request = None ):
280
+ def __init__ (self , app , environ , request = None , session = None ):
280
281
self .app = app
281
282
if request is None :
282
283
request = app .request_class (environ )
283
284
self .request = request
284
285
self .url_adapter = app .create_url_adapter (self .request )
285
286
self .flashes = None
286
- self .session = None
287
+ self .session = session
287
288
288
289
# Request contexts can be pushed multiple times and interleaved with
289
290
# other request contexts. Now only if the last level is popped we
@@ -321,10 +322,15 @@ def copy(self):
321
322
request object is locked.
322
323
323
324
.. versionadded:: 0.10
325
+
326
+ .. versionchanged:: 1.1
327
+ The current session object is used instead of reloading the original
328
+ data. This prevents `flask.session` pointing to an out-of-date object.
324
329
"""
325
330
return self .__class__ (self .app ,
326
331
environ = self .request .environ ,
327
- request = self .request
332
+ request = self .request ,
333
+ session = self .session
328
334
)
329
335
330
336
def match_request (self ):
0 commit comments