Skip to content
Myr

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

1 file+33 added−42 removed

modified:src/flask/globals.py+33 added-42 removed
1import typing as t2from functools import partial2from contextvars import ContextVar34from werkzeug.local import LocalProxy4from werkzeug.local import LocalStack56if 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 Request1313_request_ctx_err_msg = """\14Working outside of request context.1516This typically means that you attempted to use functionality that needed17an active HTTP request. Consult the documentation on testing for18information about how to avoid this problem.\19"""20_app_ctx_err_msg = """\14_no_app_msg = """\15Working outside of application context.1617This typically means that you attempted to use functionality that needed24to interface with the current application object in some way. To solve25this, set up an application context with app.app_context(). See the26documentation for more information.\18the current application. To solve this, set up an application context19with 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)23app_ctx: "AppContext" = _app_ctx_stack( # type: ignore[assignment]24 unbound_message=_no_app_msg25)26current_app: "Flask" = _app_ctx_stack( # type: ignore[assignment]27 "app", unbound_message=_no_app_msg28)29g: "_AppCtxGlobals" = _app_ctx_stack( # type: ignore[assignment]30 "g", unbound_message=_no_app_msg31)3233_no_req_msg = """\34Working outside of request context.3530def _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)353637def _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)424344def _find_app():45 top = _app_ctx_stack.top46 if top is None:47 raise RuntimeError(_app_ctx_err_msg)48 return top.app495051# context locals52_request_ctx_stack = LocalStack()53_app_ctx_stack = LocalStack()54current_app: "Flask" = LocalProxy(_find_app) # type: ignore55request: "Request" = LocalProxy(partial(_lookup_req_object, "request")) # type: ignore56session: "SessionMixin" = LocalProxy( # type: ignore57 partial(_lookup_req_object, "session")36This typically means that you attempted to use functionality that needed37an active HTTP request. Consult the documentation on testing for38information 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)42request_ctx: "RequestContext" = _request_ctx_stack( # type: ignore[assignment]43 unbound_message=_no_req_msg44)59g: "_AppCtxGlobals" = LocalProxy(partial(_lookup_app_object, "g")) # type: ignore45request: "Request" = _request_ctx_stack( # type: ignore[assignment]46 "request", unbound_message=_no_req_msg47)48session: "SessionMixin" = _request_ctx_stack( # type: ignore[assignment]49 "session", unbound_message=_no_req_msg50)