Implemented a separate application context.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent a130597 · Browse files · 3 files·+78 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+78 added−3 removed
- other:+78 added−3 removed
Provenance
- Commit
4728823- Tree
c351b7e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a130597
3 files changed+78 added−3 removed
modified:flask/app.py+16 added-1 removed
26 unmodified lines27 locked_cached_property, _tojson_filter, _endpoint_from_view_func, \28 find_package29 from .wrappers import Request, Response30 from .config import ConfigAttribute, Config31-from .ctx import RequestContext31+from .ctx import RequestContext, AppContext32 from .globals import _request_ctx_stack, request33 from .sessions import SecureCookieSessionInterface34 from .module import blueprint_is_module35 from .templating import DispatchingJinjaLoader, Environment, \1421 unmodified lines1457 if rv is not None:1458 return rv1459 request_tearing_down.send(self)1460 1461+ def app_context(self):1462+ """Binds the application only. For as long as the application is bound1463+ to the current context the :data:`flask.current_app` points to that1464+ application. An application context is automatically created when a1465+ request context is pushed if necessary.1466+1467+ Example usage::1468+1469+ with app.app_context():1470+ ...1471+1472+ .. versionadded:: 0.91473+ """1474+ return AppContext(self)1475+1476 def request_context(self, environ):1477 """Creates a :class:`~flask.ctx.RequestContext` from the given1478 environment and binds it to the current context. This must be used in1479 combination with the `with` statement because the request is only bound80 unmodified linesmodified:flask/ctx.py+53 added-1 removed
9 unmodified lines10 """11 12 from werkzeug.exceptions import HTTPException13 14-from .globals import _request_ctx_stack14+from .globals import _request_ctx_stack, _app_ctx_stack15 from .module import blueprint_is_module16 17 18 class _RequestGlobals(object):19 pass20 21 22+def _push_app_if_necessary(app):23+ top = _app_ctx_stack.top24+ if top is None or top.app != app:25+ ctx = app.app_context()26+ ctx.push()27+ return ctx28+29+30 def has_request_context():31 """If you have code that wants to test if a request context is there or32 not this function can be used. For instance, you may want to take advantage33 of request information if the request object is available, but fail24 unmodified lines58 """59 return _request_ctx_stack.top is not None60 61 62+class AppContext(object):63+ """The application context binds an application object implicitly64+ to the current thread or greenlet, similar to how the65+ :class:`RequestContext` binds request information. The application66+ context is also implicitly created if a request context is created67+ but the application is not on top of the individual application68+ context.69+ """70+71+ def __init__(self, app):72+ self.app = app73+74+ def push(self):75+ """Binds the app context to the current context."""76+ _app_ctx_stack.push(self)77+78+ def pop(self):79+ """Pops the app context."""80+ rv = _app_ctx_stack.pop()81+ assert rv is self, 'Popped wrong app context. (%r instead of %r)' \82+ % (rv, self)83+84+ def __enter__(self):85+ self.push()86+ return self87+88+ def __exit__(self, exc_type, exc_value, tb):89+ self.pop()90+91+92 class RequestContext(object):93 """The request context contains all request relevant information. It is94 created at the beginning of the request and pushed to the95 `_request_ctx_stack` and removed at the end of it. It will create the34 unmodified lines130 # indicator if the context was preserved. Next time another context131 # is pushed the preserved context is popped.132 self.preserved = False133 134+ # Indicates if pushing this request context also triggered the pushing135+ # of an application context. If it implicitly pushed an application136+ # context, it will be stored there137+ self._pushed_application_context = None138+139 self.match_request()140 141 # XXX: Support for deprecated functionality. This is going away with142 # Flask 1.029 unmodified lines172 top = _request_ctx_stack.top173 if top is not None and top.preserved:174 top.pop()175 176+ # Before we push the request context we have to ensure that there177+ # is an application context.178+ self._pushed_application_context = _push_app_if_necessary(self.app)179+180 _request_ctx_stack.push(self)181 182 # Open the session at the moment that the request context is183 # available. This allows a custom open_session method to use the16 unmodified lines200 # get rid of circular dependencies at the end of the request201 # so that we don't require the GC to be active.202 rv.request.environ['werkzeug.request'] = None203 204+ # Get rid of the app as well if necessary.205+ if self._pushed_application_context:206+ self._pushed_application_context.pop()207+ self._pushed_application_context = None208+209 def __enter__(self):210 self.push()211 return self212 19 unmodified linesmodified:flask/globals.py+9 added-1 removed
18 unmodified lines19 raise RuntimeError('working outside of request context')20 return getattr(top, name)21 22 23+def _find_app():24+ top = _app_ctx_stack.top25+ if top is None:26+ raise RuntimeError('working outside of application context')27+ return top.app28+29+30 # context locals31 _request_ctx_stack = LocalStack()25-current_app = LocalProxy(partial(_lookup_object, 'app'))32+_app_ctx_stack = LocalStack()33+current_app = LocalProxy(_find_app)34 request = LocalProxy(partial(_lookup_object, 'request'))35 session = LocalProxy(partial(_lookup_object, 'session'))36 g = LocalProxy(partial(_lookup_object, 'g'))