aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Flask/py3/flask/globals.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2025-01-16 19:09:30 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2025-01-16 19:38:51 +0300
commit7a23ad1fa2a5561a3575177d7240d8a1aa499718 (patch)
treeb4932bad31f595149e7a42e88cf729919995d735 /contrib/python/Flask/py3/flask/globals.py
parentfbb15f5ab8a61fc7c50500e2757af0b47174d825 (diff)
downloadydb-7a23ad1fa2a5561a3575177d7240d8a1aa499718.tar.gz
Intermediate changes
commit_hash:ae9e37c897fc6d514389f7089184df33bf781005
Diffstat (limited to 'contrib/python/Flask/py3/flask/globals.py')
-rw-r--r--contrib/python/Flask/py3/flask/globals.py122
1 files changed, 85 insertions, 37 deletions
diff --git a/contrib/python/Flask/py3/flask/globals.py b/contrib/python/Flask/py3/flask/globals.py
index 6d91c75edd..254da42b98 100644
--- a/contrib/python/Flask/py3/flask/globals.py
+++ b/contrib/python/Flask/py3/flask/globals.py
@@ -1,59 +1,107 @@
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:
+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 = """\
+class _FakeStack:
+ def __init__(self, name: str, cv: ContextVar[t.Any]) -> None:
+ self.name = name
+ self.cv = cv
+
+ def _warn(self):
+ import warnings
+
+ warnings.warn(
+ f"'_{self.name}_ctx_stack' is deprecated and will be"
+ " removed in Flask 2.3. Use 'g' to store data, or"
+ f" '{self.name}_ctx' to access the current context.",
+ DeprecationWarning,
+ stacklevel=3,
+ )
+
+ def push(self, obj: t.Any) -> None:
+ self._warn()
+ self.cv.set(obj)
+
+ def pop(self) -> t.Any:
+ self._warn()
+ ctx = self.cv.get(None)
+ self.cv.set(None)
+ return ctx
+
+ @property
+ def top(self) -> t.Optional[t.Any]:
+ self._warn()
+ return self.cv.get(None)
+
+
+_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.\
"""
+_cv_app: ContextVar["AppContext"] = ContextVar("flask.app_ctx")
+__app_ctx_stack = _FakeStack("app", _cv_app)
+app_ctx: "AppContext" = LocalProxy( # type: ignore[assignment]
+ _cv_app, unbound_message=_no_app_msg
+)
+current_app: "Flask" = LocalProxy( # type: ignore[assignment]
+ _cv_app, "app", unbound_message=_no_app_msg
+)
+g: "_AppCtxGlobals" = LocalProxy( # type: ignore[assignment]
+ _cv_app, "g", unbound_message=_no_app_msg
+)
+_no_req_msg = """\
+Working outside of request context.
-def _lookup_req_object(name):
- top = _request_ctx_stack.top
- if top is None:
- raise RuntimeError(_request_ctx_err_msg)
- return getattr(top, name)
+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_request: ContextVar["RequestContext"] = ContextVar("flask.request_ctx")
+__request_ctx_stack = _FakeStack("request", _cv_request)
+request_ctx: "RequestContext" = LocalProxy( # type: ignore[assignment]
+ _cv_request, unbound_message=_no_req_msg
+)
+request: "Request" = LocalProxy( # type: ignore[assignment]
+ _cv_request, "request", unbound_message=_no_req_msg
+)
+session: "SessionMixin" = LocalProxy( # type: ignore[assignment]
+ _cv_request, "session", unbound_message=_no_req_msg
+)
-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 __getattr__(name: str) -> t.Any:
+ if name == "_app_ctx_stack":
+ import warnings
+ warnings.warn(
+ "'_app_ctx_stack' is deprecated and will be removed in Flask 2.3.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ return __app_ctx_stack
-def _find_app():
- top = _app_ctx_stack.top
- if top is None:
- raise RuntimeError(_app_ctx_err_msg)
- return top.app
+ if name == "_request_ctx_stack":
+ import warnings
+ warnings.warn(
+ "'_request_ctx_stack' is deprecated and will be removed in Flask 2.3.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ return __request_ctx_stack
-# 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")
-)
-g: "_AppCtxGlobals" = LocalProxy(partial(_lookup_app_object, "g")) # type: ignore
+ raise AttributeError(name)