|
1 | 1 | import typing as t
|
2 |
| -from functools import partial |
| 2 | +from contextvars import ContextVar |
3 | 3 |
|
4 |
| -from werkzeug.local import LocalProxy |
5 | 4 | from werkzeug.local import LocalStack
|
6 | 5 |
|
7 | 6 | if t.TYPE_CHECKING: # pragma: no cover
|
8 | 7 | from .app import Flask
|
9 | 8 | from .ctx import _AppCtxGlobals
|
| 9 | + from .ctx import AppContext |
| 10 | + from .ctx import RequestContext |
10 | 11 | from .sessions import SessionMixin
|
11 | 12 | from .wrappers import Request
|
12 | 13 |
|
13 |
| -_request_ctx_err_msg = """\ |
14 |
| -Working outside of request context. |
15 |
| -
|
16 |
| -This typically means that you attempted to use functionality that needed |
17 |
| -an active HTTP request. Consult the documentation on testing for |
18 |
| -information about how to avoid this problem.\ |
19 |
| -""" |
20 |
| -_app_ctx_err_msg = """\ |
| 14 | +_no_app_msg = """\ |
21 | 15 | Working outside of application context.
|
22 | 16 |
|
23 | 17 | This typically means that you attempted to use functionality that needed
|
24 |
| -to interface with the current application object in some way. To solve |
25 |
| -this, set up an application context with app.app_context(). See the |
26 |
| -documentation for more information.\ |
| 18 | +the current application. To solve this, set up an application context |
| 19 | +with app.app_context(). See the documentation for more information.\ |
27 | 20 | """
|
| 21 | +_cv_app: ContextVar[t.List["AppContext"]] = ContextVar("flask.app_ctx") |
| 22 | +_app_ctx_stack: LocalStack["AppContext"] = LocalStack(_cv_app) |
| 23 | +app_ctx: "AppContext" = _app_ctx_stack( # type: ignore[assignment] |
| 24 | + unbound_message=_no_app_msg |
| 25 | +) |
| 26 | +current_app: "Flask" = _app_ctx_stack( # type: ignore[assignment] |
| 27 | + "app", unbound_message=_no_app_msg |
| 28 | +) |
| 29 | +g: "_AppCtxGlobals" = _app_ctx_stack( # type: ignore[assignment] |
| 30 | + "g", unbound_message=_no_app_msg |
| 31 | +) |
28 | 32 |
|
| 33 | +_no_req_msg = """\ |
| 34 | +Working outside of request context. |
29 | 35 |
|
30 |
| -def _lookup_req_object(name): |
31 |
| - top = _request_ctx_stack.top |
32 |
| - if top is None: |
33 |
| - raise RuntimeError(_request_ctx_err_msg) |
34 |
| - return getattr(top, name) |
35 |
| - |
36 |
| - |
37 |
| -def _lookup_app_object(name): |
38 |
| - top = _app_ctx_stack.top |
39 |
| - if top is None: |
40 |
| - raise RuntimeError(_app_ctx_err_msg) |
41 |
| - return getattr(top, name) |
42 |
| - |
43 |
| - |
44 |
| -def _find_app(): |
45 |
| - top = _app_ctx_stack.top |
46 |
| - if top is None: |
47 |
| - raise RuntimeError(_app_ctx_err_msg) |
48 |
| - return top.app |
49 |
| - |
50 |
| - |
51 |
| -# context locals |
52 |
| -_request_ctx_stack = LocalStack() |
53 |
| -_app_ctx_stack = LocalStack() |
54 |
| -current_app: "Flask" = LocalProxy(_find_app) # type: ignore |
55 |
| -request: "Request" = LocalProxy(partial(_lookup_req_object, "request")) # type: ignore |
56 |
| -session: "SessionMixin" = LocalProxy( # type: ignore |
57 |
| - partial(_lookup_req_object, "session") |
| 36 | +This typically means that you attempted to use functionality that needed |
| 37 | +an active HTTP request. Consult the documentation on testing for |
| 38 | +information about how to avoid this problem.\ |
| 39 | +""" |
| 40 | +_cv_req: ContextVar[t.List["RequestContext"]] = ContextVar("flask.request_ctx") |
| 41 | +_request_ctx_stack: LocalStack["RequestContext"] = LocalStack(_cv_req) |
| 42 | +request_ctx: "RequestContext" = _request_ctx_stack( # type: ignore[assignment] |
| 43 | + unbound_message=_no_req_msg |
| 44 | +) |
| 45 | +request: "Request" = _request_ctx_stack( # type: ignore[assignment] |
| 46 | + "request", unbound_message=_no_req_msg |
| 47 | +) |
| 48 | +session: "SessionMixin" = _request_ctx_stack( # type: ignore[assignment] |
| 49 | + "session", unbound_message=_no_req_msg |
58 | 50 | )
|
59 |
| -g: "_AppCtxGlobals" = LocalProxy(partial(_lookup_app_object, "g")) # type: ignore |
0 commit comments