The context relating to the app bound to the current task. Do not use directly, prefer the :func:`~quart.Quart.app_context` instead. Attributes: app: The app itself. url_adapter: An adapter bound to the server, but not a specific task, useful for route build
| 235 | |
| 236 | |
| 237 | class AppContext: |
| 238 | """The context relating to the app bound to the current task. |
| 239 | |
| 240 | Do not use directly, prefer the |
| 241 | :func:`~quart.Quart.app_context` instead. |
| 242 | |
| 243 | Attributes: |
| 244 | app: The app itself. |
| 245 | url_adapter: An adapter bound to the server, but not a |
| 246 | specific task, useful for route building. |
| 247 | g: An instance of the ctx globals class. |
| 248 | """ |
| 249 | |
| 250 | def __init__(self, app: Quart) -> None: |
| 251 | self.app = app |
| 252 | self.url_adapter = app.create_url_adapter(None) |
| 253 | self.g = app.app_ctx_globals_class() |
| 254 | self._cv_tokens: list[Token] = [] |
| 255 | |
| 256 | def copy(self) -> AppContext: |
| 257 | app_context = self.__class__(self.app) |
| 258 | app_context.g = self.g |
| 259 | return app_context |
| 260 | |
| 261 | async def push(self) -> None: |
| 262 | self._cv_tokens.append(_cv_app.set(self)) |
| 263 | await appcontext_pushed.send_async( |
| 264 | self.app, |
| 265 | _sync_wrapper=self.app.ensure_async, # type: ignore[arg-type] |
| 266 | ) |
| 267 | |
| 268 | async def pop(self, exc: BaseException | None = _sentinel) -> None: # type: ignore |
| 269 | try: |
| 270 | if len(self._cv_tokens) == 1: |
| 271 | if exc is _sentinel: |
| 272 | exc = sys.exc_info()[1] |
| 273 | await self.app.do_teardown_appcontext(exc) |
| 274 | finally: |
| 275 | ctx = _cv_app.get() |
| 276 | _cv_app.reset(self._cv_tokens.pop()) |
| 277 | |
| 278 | if ctx is not self: |
| 279 | raise AssertionError( |
| 280 | f"Popped wrong app context. ({ctx!r} instead of {self!r})" |
| 281 | ) |
| 282 | |
| 283 | await appcontext_popped.send_async( |
| 284 | self.app, |
| 285 | _sync_wrapper=self.app.ensure_async, # type: ignore[arg-type] |
| 286 | ) |
| 287 | |
| 288 | async def __aenter__(self) -> AppContext: |
| 289 | await self.push() |
| 290 | return self |
| 291 | |
| 292 | async def __aexit__( |
| 293 | self, exc_type: type, exc_value: BaseException, tb: TracebackType |
| 294 | ) -> None: |
no outgoing calls
searching dependent graphs…