Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/flask/globals.py: 64%
44 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1import typing as t
2from contextvars import ContextVar
4from werkzeug.local import LocalProxy
6if t.TYPE_CHECKING: # pragma: no cover
7 from .app import Flask
8 from .ctx import _AppCtxGlobals
9 from .ctx import AppContext
10 from .ctx import RequestContext
11 from .sessions import SessionMixin
12 from .wrappers import Request
15class _FakeStack:
16 def __init__(self, name: str, cv: ContextVar[t.Any]) -> None:
17 self.name = name
18 self.cv = cv
20 def _warn(self):
21 import warnings
23 warnings.warn(
24 f"'_{self.name}_ctx_stack' is deprecated and will be"
25 " removed in Flask 2.3. Use 'g' to store data, or"
26 f" '{self.name}_ctx' to access the current context.",
27 DeprecationWarning,
28 stacklevel=3,
29 )
31 def push(self, obj: t.Any) -> None:
32 self._warn()
33 self.cv.set(obj)
35 def pop(self) -> t.Any:
36 self._warn()
37 ctx = self.cv.get(None)
38 self.cv.set(None)
39 return ctx
41 @property
42 def top(self) -> t.Optional[t.Any]:
43 self._warn()
44 return self.cv.get(None)
47_no_app_msg = """\
48Working outside of application context.
50This typically means that you attempted to use functionality that needed
51the current application. To solve this, set up an application context
52with app.app_context(). See the documentation for more information.\
53"""
54_cv_app: ContextVar["AppContext"] = ContextVar("flask.app_ctx")
55__app_ctx_stack = _FakeStack("app", _cv_app)
56app_ctx: "AppContext" = LocalProxy( # type: ignore[assignment]
57 _cv_app, unbound_message=_no_app_msg
58)
59current_app: "Flask" = LocalProxy( # type: ignore[assignment]
60 _cv_app, "app", unbound_message=_no_app_msg
61)
62g: "_AppCtxGlobals" = LocalProxy( # type: ignore[assignment]
63 _cv_app, "g", unbound_message=_no_app_msg
64)
66_no_req_msg = """\
67Working outside of request context.
69This typically means that you attempted to use functionality that needed
70an active HTTP request. Consult the documentation on testing for
71information about how to avoid this problem.\
72"""
73_cv_request: ContextVar["RequestContext"] = ContextVar("flask.request_ctx")
74__request_ctx_stack = _FakeStack("request", _cv_request)
75request_ctx: "RequestContext" = LocalProxy( # type: ignore[assignment]
76 _cv_request, unbound_message=_no_req_msg
77)
78request: "Request" = LocalProxy( # type: ignore[assignment]
79 _cv_request, "request", unbound_message=_no_req_msg
80)
81session: "SessionMixin" = LocalProxy( # type: ignore[assignment]
82 _cv_request, "session", unbound_message=_no_req_msg
83)
86def __getattr__(name: str) -> t.Any:
87 if name == "_app_ctx_stack":
88 import warnings
90 warnings.warn(
91 "'_app_ctx_stack' is deprecated and will be removed in Flask 2.3.",
92 DeprecationWarning,
93 stacklevel=2,
94 )
95 return __app_ctx_stack
97 if name == "_request_ctx_stack":
98 import warnings
100 warnings.warn(
101 "'_request_ctx_stack' is deprecated and will be removed in Flask 2.3.",
102 DeprecationWarning,
103 stacklevel=2,
104 )
105 return __request_ctx_stack
107 raise AttributeError(name)