Skip to content
Myr

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

3 files+78 added−3 removed

modified:flask/app.py+16 added-1 removed
26 unmodified lines
27 locked_cached_property, _tojson_filter, _endpoint_from_view_func, \28 find_package29from .wrappers import Request, Response30from .config import ConfigAttribute, Config31from .ctx import RequestContext31from .ctx import RequestContext, AppContext32from .globals import _request_ctx_stack, request33from .sessions import SecureCookieSessionInterface34from .module import blueprint_is_module35from .templating import DispatchingJinjaLoader, Environment, \
1421 unmodified lines
1457 if rv is not None:1458 return rv1459 request_tearing_down.send(self)14601461 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.14661467 Example usage::14681469 with app.app_context():1470 ...14711472 .. versionadded:: 0.91473 """1474 return AppContext(self)14751476 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 bound
80 unmodified lines
modified:flask/ctx.py+53 added-1 removed
9 unmodified lines
10"""1112from werkzeug.exceptions import HTTPException1314from .globals import _request_ctx_stack14from .globals import _request_ctx_stack, _app_ctx_stack15from .module import blueprint_is_module161718class _RequestGlobals(object):19 pass202122def _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 ctx282930def 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 fail
24 unmodified lines
58 """59 return _request_ctx_stack.top is not None606162class 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 """7071 def __init__(self, app):72 self.app = app7374 def push(self):75 """Binds the app context to the current context."""76 _app_ctx_stack.push(self)7778 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)8384 def __enter__(self):85 self.push()86 return self8788 def __exit__(self, exc_type, exc_value, tb):89 self.pop()909192class 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 the
34 unmodified lines
130 # indicator if the context was preserved. Next time another context131 # is pushed the preserved context is popped.132 self.preserved = False133134 # 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 = None138139 self.match_request()140141 # XXX: Support for deprecated functionality. This is going away with142 # Flask 1.0
29 unmodified lines
172 top = _request_ctx_stack.top173 if top is not None and top.preserved:174 top.pop()175176 # 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)179180 _request_ctx_stack.push(self)181182 # Open the session at the moment that the request context is183 # available. This allows a custom open_session method to use the
16 unmodified lines
200 # 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'] = None203204 # 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 = None208209 def __enter__(self):210 self.push()211 return self212
19 unmodified lines
modified:flask/globals.py+9 added-1 removed
18 unmodified lines
19 raise RuntimeError('working outside of request context')20 return getattr(top, name)212223def _find_app():24 top = _app_ctx_stack.top25 if top is None:26 raise RuntimeError('working outside of application context')27 return top.app282930# context locals31_request_ctx_stack = LocalStack()25current_app = LocalProxy(partial(_lookup_object, 'app'))32_app_ctx_stack = LocalStack()33current_app = LocalProxy(_find_app)34request = LocalProxy(partial(_lookup_object, 'request'))35session = LocalProxy(partial(_lookup_object, 'session'))36g = LocalProxy(partial(_lookup_object, 'g'))