From 0b2f809f9b56861dae3ae5154d73b4afaf632a0a Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 27 Jun 2022 07:57:03 -0700 Subject: [PATCH] access names as proxies directly --- src/flask/globals.py | 79 ++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/src/flask/globals.py b/src/flask/globals.py index 7824204f..f4781f6c 100644 --- a/src/flask/globals.py +++ b/src/flask/globals.py @@ -1,59 +1,50 @@ import typing as t -from functools import partial +from contextvars import ContextVar -from werkzeug.local import LocalProxy from werkzeug.local import LocalStack if t.TYPE_CHECKING: # pragma: no cover from .app import Flask from .ctx import _AppCtxGlobals + from .ctx import AppContext + from .ctx import RequestContext from .sessions import SessionMixin from .wrappers import Request -_request_ctx_err_msg = """\ -Working outside of request context. - -This typically means that you attempted to use functionality that needed -an active HTTP request. Consult the documentation on testing for -information about how to avoid this problem.\ -""" -_app_ctx_err_msg = """\ +_no_app_msg = """\ Working outside of application context. This typically means that you attempted to use functionality that needed -to interface with the current application object in some way. To solve -this, set up an application context with app.app_context(). See the -documentation for more information.\ +the current application. To solve this, set up an application context +with app.app_context(). See the documentation for more information.\ """ - - -def _lookup_req_object(name): - top = _request_ctx_stack.top - if top is None: - raise RuntimeError(_request_ctx_err_msg) - return getattr(top, name) - - -def _lookup_app_object(name): - top = _app_ctx_stack.top - if top is None: - raise RuntimeError(_app_ctx_err_msg) - return getattr(top, name) - - -def _find_app(): - top = _app_ctx_stack.top - if top is None: - raise RuntimeError(_app_ctx_err_msg) - return top.app - - -# context locals -_request_ctx_stack = LocalStack() -_app_ctx_stack = LocalStack() -current_app: "Flask" = LocalProxy(_find_app) # type: ignore -request: "Request" = LocalProxy(partial(_lookup_req_object, "request")) # type: ignore -session: "SessionMixin" = LocalProxy( # type: ignore - partial(_lookup_req_object, "session") +_cv_app: ContextVar[t.List["AppContext"]] = ContextVar("flask.app_ctx") +_app_ctx_stack: LocalStack["AppContext"] = LocalStack(_cv_app) +app_ctx: "AppContext" = _app_ctx_stack( # type: ignore[assignment] + unbound_message=_no_app_msg +) +current_app: "Flask" = _app_ctx_stack( # type: ignore[assignment] + "app", unbound_message=_no_app_msg +) +g: "_AppCtxGlobals" = _app_ctx_stack( # type: ignore[assignment] + "g", unbound_message=_no_app_msg +) + +_no_req_msg = """\ +Working outside of request context. + +This typically means that you attempted to use functionality that needed +an active HTTP request. Consult the documentation on testing for +information about how to avoid this problem.\ +""" +_cv_req: ContextVar[t.List["RequestContext"]] = ContextVar("flask.request_ctx") +_request_ctx_stack: LocalStack["RequestContext"] = LocalStack(_cv_req) +request_ctx: "RequestContext" = _request_ctx_stack( # type: ignore[assignment] + unbound_message=_no_req_msg +) +request: "Request" = _request_ctx_stack( # type: ignore[assignment] + "request", unbound_message=_no_req_msg +) +session: "SessionMixin" = _request_ctx_stack( # type: ignore[assignment] + "session", unbound_message=_no_req_msg ) -g: "_AppCtxGlobals" = LocalProxy(partial(_lookup_app_object, "g")) # type: ignore