Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/flask/globals.py: 81%

32 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:03 +0000

1import typing as t 

2from functools import partial 

3 

4from werkzeug.local import LocalProxy 

5from werkzeug.local import LocalStack 

6 

7if t.TYPE_CHECKING: 

8 from .app import Flask 

9 from .ctx import _AppCtxGlobals 

10 from .sessions import SessionMixin 

11 from .wrappers import Request 

12 

13_request_ctx_err_msg = """\ 

14Working outside of request context. 

15 

16This typically means that you attempted to use functionality that needed 

17an active HTTP request. Consult the documentation on testing for 

18information about how to avoid this problem.\ 

19""" 

20_app_ctx_err_msg = """\ 

21Working outside of application context. 

22 

23This typically means that you attempted to use functionality that needed 

24to interface with the current application object in some way. To solve 

25this, set up an application context with app.app_context(). See the 

26documentation for more information.\ 

27""" 

28 

29 

30def _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 

37def _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 

44def _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() 

54current_app: "Flask" = LocalProxy(_find_app) # type: ignore 

55request: "Request" = LocalProxy(partial(_lookup_req_object, "request")) # type: ignore 

56session: "SessionMixin" = LocalProxy( # type: ignore 

57 partial(_lookup_req_object, "session") 

58) 

59g: "_AppCtxGlobals" = LocalProxy(partial(_lookup_app_object, "g")) # type: ignore