add back opening session on context push

This commit is contained in:
David Lord 2026-02-19 08:35:48 -08:00
parent daca74d93a
commit a411a2434b
No known key found for this signature in database
GPG key ID: 43368A7AA8CC5926

View file

@ -377,12 +377,8 @@ class AppContext:
return self._request return self._request
@property def _get_session(self) -> SessionMixin:
def session(self) -> SessionMixin: """Open the session if it is not already open for this request context."""
"""The session object associated with this context. Accessed through
:data:`.session`. Only available in request contexts, otherwise raises
:exc:`RuntimeError`. Accessing this sets :attr:`.SessionMixin.accessed`.
"""
if self._request is None: if self._request is None:
raise RuntimeError("There is no request in this context.") raise RuntimeError("There is no request in this context.")
@ -393,9 +389,18 @@ class AppContext:
if self._session is None: if self._session is None:
self._session = si.make_null_session(self.app) self._session = si.make_null_session(self.app)
self._session.accessed = True
return self._session return self._session
@property
def session(self) -> SessionMixin:
"""The session object associated with this context. Accessed through
:data:`.session`. Only available in request contexts, otherwise raises
:exc:`RuntimeError`. Accessing this sets :attr:`.SessionMixin.accessed`.
"""
session = self._get_session()
session.accessed = True
return session
def match_request(self) -> None: def match_request(self) -> None:
"""Apply routing to the current request, storing either the matched """Apply routing to the current request, storing either the matched
endpoint and args, or a routing exception. endpoint and args, or a routing exception.
@ -427,8 +432,15 @@ class AppContext:
self._cv_token = _cv_app.set(self) self._cv_token = _cv_app.set(self)
appcontext_pushed.send(self.app, _async_wrapper=self.app.ensure_sync) appcontext_pushed.send(self.app, _async_wrapper=self.app.ensure_sync)
if self._request is not None and self.url_adapter is not None: if self._request is not None:
self.match_request() # Open the session at the moment that the request context is available.
# This allows a custom open_session method to use the request context.
self._get_session()
# Match the request URL after loading the session, so that the
# session is available in custom URL converters.
if self.url_adapter is not None:
self.match_request()
def pop(self, exc: BaseException | None = None) -> None: def pop(self, exc: BaseException | None = None) -> None:
"""Pop this context so that it is no longer the active context. Then """Pop this context so that it is no longer the active context. Then