add back opening session on context push
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent daca74d · Browse files · 1 file·+21 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+21 added−9 removed
- other:+21 added−9 removed
Provenance
- Commit
a411a24- Tree
8032648- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
daca74d
1 file changed+21 added−9 removed
modified:src/flask/ctx.py+21 added-9 removed
375 unmodified lines376 raise RuntimeError("There is no request in this context.")377 378 return self._request379 380- @property381- def session(self) -> SessionMixin:382- """The session object associated with this context. Accessed through383- :data:`.session`. Only available in request contexts, otherwise raises384- :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."""382 if self._request is None:383 raise RuntimeError("There is no request in this context.")384 385 if self._session is None:2 unmodified lines388 389 if self._session is None:390 self._session = si.make_null_session(self.app)391 396- self._session.accessed = True392 return self._session393 394+ @property395+ def session(self) -> SessionMixin:396+ """The session object associated with this context. Accessed through397+ :data:`.session`. Only available in request contexts, otherwise raises398+ :exc:`RuntimeError`. Accessing this sets :attr:`.SessionMixin.accessed`.399+ """400+ session = self._get_session()401+ session.accessed = True402+ return session403+404 def match_request(self) -> None:405 """Apply routing to the current request, storing either the matched406 endpoint and args, or a routing exception.407 """23 unmodified lines431 432 self._cv_token = _cv_app.set(self)433 appcontext_pushed.send(self.app, _async_wrapper=self.app.ensure_sync)434 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 the441+ # session is available in custom URL converters.442+ if self.url_adapter is not None:443+ self.match_request()444+445 def pop(self, exc: BaseException | None = None) -> None:446 """Pop this context so that it is no longer the active context. Then447 call teardown functions and signals.448 81 unmodified lines