Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved open_session call from RequestContext.push to wsgi_app #1538

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions flask/app.py
Expand Up @@ -1924,6 +1924,15 @@ def test_request_context(self, *args, **kwargs):
finally:
builder.close()

def ctx_open_session(self):
"""Opens session for current request context using session_interface.
"""
ctx = _request_ctx_stack.top
session = self.open_session(ctx.request)
if session is None:
session = self.make_null_session()
ctx.session = session

def wsgi_app(self, environ, start_response):
"""The actual WSGI application. This is not implemented in
`__call__` so that middlewares can be applied without losing a
Expand Down Expand Up @@ -1954,6 +1963,7 @@ def wsgi_app(self, environ, start_response):
error = None
try:
try:
self.ctx_open_session()
response = self.full_dispatch_request()
except Exception as e:
error = e
Expand Down
8 changes: 0 additions & 8 deletions flask/ctx.py
Expand Up @@ -325,14 +325,6 @@ def push(self):

_request_ctx_stack.push(self)

# Open the session at the moment that the request context is
# available. This allows a custom open_session method to use the
# request context (e.g. code that access database information
# stored on `g` instead of the appcontext).
self.session = self.app.open_session(self.request)
if self.session is None:
self.session = self.app.make_null_session()

def pop(self, exc=_sentinel):
"""Pops the request context and unbinds it by doing that. This will
also trigger the execution of functions registered by the
Expand Down
28 changes: 28 additions & 0 deletions tests/test_appctx.py
Expand Up @@ -43,6 +43,34 @@ def test_request_context_means_app_context():
assert flask.current_app._get_current_object() == app
assert flask._app_ctx_stack.top is None

def test_request_context_reset_correctly():
app = flask.Flask(__name__)

class BadSessionInterface(object):

"""Fails once."""
fail = True

def open_session(self, *args, **kwargs):
if self.fail:
setattr(flask.g, "test_g_attr", 1)

This comment was marked as off-topic.

self.fail = False
raise Exception()
def make_null_session(self, *args, **kwargs):
return None

app.session_interface = BadSessionInterface()

try:
with app.test_request_context():
app.ctx_open_session()
except:

This comment was marked as off-topic.

This comment was marked as off-topic.

pass

with app.test_request_context():
app.ctx_open_session()
assert getattr(flask.g, "test_g_attr", None) is None

This comment was marked as off-topic.


def test_app_context_provides_current_app():
app = flask.Flask(__name__)
with app.app_context():
Expand Down
2 changes: 2 additions & 0 deletions tests/test_basic.py
Expand Up @@ -314,6 +314,7 @@ def expect_exception(f, *args, **kwargs):
else:
assert False, 'expected exception'
with app.test_request_context():
app.ctx_open_session()
assert flask.session.get('missing_key') is None
expect_exception(flask.session.__setitem__, 'foo', 42)
expect_exception(flask.session.pop, 'foo')
Expand Down Expand Up @@ -454,6 +455,7 @@ def test_flashes():
app.secret_key = 'testkey'

with app.test_request_context():
app.ctx_open_session()
assert not flask.session.modified
flask.flash('Zap')
flask.session.modified = False
Expand Down