Skip to content

Commit 0b2f809

Browse files
committed
access names as proxies directly
1 parent 89463cb commit 0b2f809

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

src/flask/globals.py

+33-42
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,50 @@
11
import typing as t
2-
from functools import partial
2+
from contextvars import ContextVar
33

4-
from werkzeug.local import LocalProxy
54
from werkzeug.local import LocalStack
65

76
if t.TYPE_CHECKING: # pragma: no cover
87
from .app import Flask
98
from .ctx import _AppCtxGlobals
9+
from .ctx import AppContext
10+
from .ctx import RequestContext
1011
from .sessions import SessionMixin
1112
from .wrappers import Request
1213

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 = """\
2115
Working outside of application context.
2216
2317
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.\
2720
"""
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+
)
2832

33+
_no_req_msg = """\
34+
Working outside of request context.
2935
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
5850
)
59-
g: "_AppCtxGlobals" = LocalProxy(partial(_lookup_app_object, "g")) # type: ignore

0 commit comments

Comments
 (0)