"""Save any changed session data."""
- """Response.body wrapper which saves session data."""
- if isinstance(body, types.GeneratorType):
- # If the body is a generator, we have to save the data
- # *after* the generator has been consumed
- # If the body is not a generator, we save the data
- # before the body is returned (so we can release the lock).
- cherrypy.response.body = wrap_body(cherrypy.response.body)
+ # Guard against running twice
+ if hasattr(cherrypy.request, "_sessionsaved"):
+ cherrypy.request._sessionsaved = True
+ if cherrypy.response.stream:
+ # If the body is being streamed, we have to save the data
+ # *after* the response has been written out
+ cherrypy.request.hooks.attach('on_end_request', cherrypy.session.save)
+ # If the body is not being streamed, we save the data now
+ # (so we can release the lock).
+ if isinstance(cherrypy.response.body, types.GeneratorType):