Skip to content

Commit a411a24

Browse files
committed
add back opening session on context push
1 parent daca74d commit a411a24

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/flask/ctx.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,8 @@ def request(self) -> Request:
377377

378378
return self._request
379379

380-
@property
381-
def session(self) -> SessionMixin:
382-
"""The session object associated with this context. Accessed through
383-
:data:`.session`. Only available in request contexts, otherwise raises
384-
:exc:`RuntimeError`. Accessing this sets :attr:`.SessionMixin.accessed`.
385-
"""
380+
def _get_session(self) -> SessionMixin:
381+
"""Open the session if it is not already open for this request context."""
386382
if self._request is None:
387383
raise RuntimeError("There is no request in this context.")
388384

@@ -393,9 +389,18 @@ def session(self) -> SessionMixin:
393389
if self._session is None:
394390
self._session = si.make_null_session(self.app)
395391

396-
self._session.accessed = True
397392
return self._session
398393

394+
@property
395+
def session(self) -> SessionMixin:
396+
"""The session object associated with this context. Accessed through
397+
:data:`.session`. Only available in request contexts, otherwise raises
398+
:exc:`RuntimeError`. Accessing this sets :attr:`.SessionMixin.accessed`.
399+
"""
400+
session = self._get_session()
401+
session.accessed = True
402+
return session
403+
399404
def match_request(self) -> None:
400405
"""Apply routing to the current request, storing either the matched
401406
endpoint and args, or a routing exception.
@@ -427,8 +432,15 @@ def push(self) -> None:
427432
self._cv_token = _cv_app.set(self)
428433
appcontext_pushed.send(self.app, _async_wrapper=self.app.ensure_sync)
429434

430-
if self._request is not None and self.url_adapter is not None:
431-
self.match_request()
435+
if self._request is not None:
436+
# Open the session at the moment that the request context is available.
437+
# This allows a custom open_session method to use the request context.
438+
self._get_session()
439+
440+
# Match the request URL after loading the session, so that the
441+
# session is available in custom URL converters.
442+
if self.url_adapter is not None:
443+
self.match_request()
432444

433445
def pop(self, exc: BaseException | None = None) -> None:
434446
"""Pop this context so that it is no longer the active context. Then

0 commit comments

Comments
 (0)