access names as proxies directly
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 89463cb · Browse files · 1 file·+33 added−42 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+33 added−42 removed
- other:+33 added−42 removed
Provenance
- Commit
0b2f809- Tree
f37e89e- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
89463cb
1 file changed+33 added−42 removed
modified:src/flask/globals.py+33 added-42 removed
1 import typing as t2-from functools import partial2+from contextvars import ContextVar3 4-from werkzeug.local import LocalProxy4 from werkzeug.local import LocalStack5 6 if t.TYPE_CHECKING: # pragma: no cover7 from .app import Flask8 from .ctx import _AppCtxGlobals9+ from .ctx import AppContext10+ from .ctx import RequestContext11 from .sessions import SessionMixin12 from .wrappers import Request13 13-_request_ctx_err_msg = """\14-Working outside of request context.15-16-This typically means that you attempted to use functionality that needed17-an active HTTP request. Consult the documentation on testing for18-information about how to avoid this problem.\19-"""20-_app_ctx_err_msg = """\14+_no_app_msg = """\15 Working outside of application context.16 17 This typically means that you attempted to use functionality that needed24-to interface with the current application object in some way. To solve25-this, set up an application context with app.app_context(). See the26-documentation for more information.\18+the current application. To solve this, set up an application context19+with app.app_context(). See the documentation for more information.\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_msg25+)26+current_app: "Flask" = _app_ctx_stack( # type: ignore[assignment]27+ "app", unbound_message=_no_app_msg28+)29+g: "_AppCtxGlobals" = _app_ctx_stack( # type: ignore[assignment]30+ "g", unbound_message=_no_app_msg31+)32 33+_no_req_msg = """\34+Working outside of request context.35 30-def _lookup_req_object(name):31- top = _request_ctx_stack.top32- 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.top39- 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.top46- if top is None:47- raise RuntimeError(_app_ctx_err_msg)48- return top.app49-50-51-# context locals52-_request_ctx_stack = LocalStack()53-_app_ctx_stack = LocalStack()54-current_app: "Flask" = LocalProxy(_find_app) # type: ignore55-request: "Request" = LocalProxy(partial(_lookup_req_object, "request")) # type: ignore56-session: "SessionMixin" = LocalProxy( # type: ignore57- partial(_lookup_req_object, "session")36+This typically means that you attempted to use functionality that needed37+an active HTTP request. Consult the documentation on testing for38+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_msg44 )59-g: "_AppCtxGlobals" = LocalProxy(partial(_lookup_app_object, "g")) # type: ignore45+request: "Request" = _request_ctx_stack( # type: ignore[assignment]46+ "request", unbound_message=_no_req_msg47+)48+session: "SessionMixin" = _request_ctx_stack( # type: ignore[assignment]49+ "session", unbound_message=_no_req_msg50+)