ctx.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. # -*- coding: utf-8 -*-
  2. """
  3. flask.ctx
  4. ~~~~~~~~~
  5. Implements the objects required to keep the context.
  6. :copyright: (c) 2011 by Armin Ronacher.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from __future__ import with_statement
  10. import sys
  11. from functools import update_wrapper
  12. from werkzeug.exceptions import HTTPException
  13. from .globals import _request_ctx_stack, _app_ctx_stack
  14. from .module import blueprint_is_module
  15. from .signals import appcontext_pushed, appcontext_popped
  16. class _AppCtxGlobals(object):
  17. """A plain object."""
  18. def get(self, name, default=None):
  19. return self.__dict__.get(name, default)
  20. def __contains__(self, item):
  21. return item in self.__dict__
  22. def __iter__(self):
  23. return iter(self.__dict__)
  24. def __repr__(self):
  25. top = _app_ctx_stack.top
  26. if top is not None:
  27. return '<flask.g of %r>' % top.app.name
  28. return object.__repr__(self)
  29. def after_this_request(f):
  30. """Executes a function after this request. This is useful to modify
  31. response objects. The function is passed the response object and has
  32. to return the same or a new one.
  33. Example::
  34. @app.route('/')
  35. def index():
  36. @after_this_request
  37. def add_header(response):
  38. response.headers['X-Foo'] = 'Parachute'
  39. return response
  40. return 'Hello World!'
  41. This is more useful if a function other than the view function wants to
  42. modify a response. For instance think of a decorator that wants to add
  43. some headers without converting the return value into a response object.
  44. .. versionadded:: 0.9
  45. """
  46. _request_ctx_stack.top._after_request_functions.append(f)
  47. return f
  48. def copy_current_request_context(f):
  49. """A helper function that decorates a function to retain the current
  50. request context. This is useful when working with greenlets. The moment
  51. the function is decorated a copy of the request context is created and
  52. then pushed when the function is called.
  53. Example::
  54. import gevent
  55. from flask import copy_current_request_context
  56. @app.route('/')
  57. def index():
  58. @copy_current_request_context
  59. def do_some_work():
  60. # do some work here, it can access flask.request like you
  61. # would otherwise in the view function.
  62. ...
  63. gevent.spawn(do_some_work)
  64. return 'Regular response'
  65. .. versionadded:: 0.10
  66. """
  67. top = _request_ctx_stack.top
  68. if top is None:
  69. raise RuntimeError('This decorator can only be used at local scopes '
  70. 'when a request context is on the stack. For instance within '
  71. 'view functions.')
  72. reqctx = top.copy()
  73. def wrapper(*args, **kwargs):
  74. with reqctx:
  75. return f(*args, **kwargs)
  76. return update_wrapper(wrapper, f)
  77. def has_request_context():
  78. """If you have code that wants to test if a request context is there or
  79. not this function can be used. For instance, you may want to take advantage
  80. of request information if the request object is available, but fail
  81. silently if it is unavailable.
  82. ::
  83. class User(db.Model):
  84. def __init__(self, username, remote_addr=None):
  85. self.username = username
  86. if remote_addr is None and has_request_context():
  87. remote_addr = request.remote_addr
  88. self.remote_addr = remote_addr
  89. Alternatively you can also just test any of the context bound objects
  90. (such as :class:`request` or :class:`g` for truthness)::
  91. class User(db.Model):
  92. def __init__(self, username, remote_addr=None):
  93. self.username = username
  94. if remote_addr is None and request:
  95. remote_addr = request.remote_addr
  96. self.remote_addr = remote_addr
  97. .. versionadded:: 0.7
  98. """
  99. return _request_ctx_stack.top is not None
  100. def has_app_context():
  101. """Works like :func:`has_request_context` but for the application
  102. context. You can also just do a boolean check on the
  103. :data:`current_app` object instead.
  104. .. versionadded:: 0.9
  105. """
  106. return _app_ctx_stack.top is not None
  107. class AppContext(object):
  108. """The application context binds an application object implicitly
  109. to the current thread or greenlet, similar to how the
  110. :class:`RequestContext` binds request information. The application
  111. context is also implicitly created if a request context is created
  112. but the application is not on top of the individual application
  113. context.
  114. """
  115. def __init__(self, app):
  116. self.app = app
  117. self.url_adapter = app.create_url_adapter(None)
  118. self.g = app.app_ctx_globals_class()
  119. # Like request context, app contexts can be pushed multiple times
  120. # but there a basic "refcount" is enough to track them.
  121. self._refcnt = 0
  122. def push(self):
  123. """Binds the app context to the current context."""
  124. self._refcnt += 1
  125. _app_ctx_stack.push(self)
  126. appcontext_pushed.send(self.app)
  127. def pop(self, exc=None):
  128. """Pops the app context."""
  129. self._refcnt -= 1
  130. if self._refcnt <= 0:
  131. if exc is None:
  132. exc = sys.exc_info()[1]
  133. self.app.do_teardown_appcontext(exc)
  134. rv = _app_ctx_stack.pop()
  135. assert rv is self, 'Popped wrong app context. (%r instead of %r)' \
  136. % (rv, self)
  137. appcontext_popped.send(self.app)
  138. def __enter__(self):
  139. self.push()
  140. return self
  141. def __exit__(self, exc_type, exc_value, tb):
  142. self.pop(exc_value)
  143. class RequestContext(object):
  144. """The request context contains all request relevant information. It is
  145. created at the beginning of the request and pushed to the
  146. `_request_ctx_stack` and removed at the end of it. It will create the
  147. URL adapter and request object for the WSGI environment provided.
  148. Do not attempt to use this class directly, instead use
  149. :meth:`~flask.Flask.test_request_context` and
  150. :meth:`~flask.Flask.request_context` to create this object.
  151. When the request context is popped, it will evaluate all the
  152. functions registered on the application for teardown execution
  153. (:meth:`~flask.Flask.teardown_request`).
  154. The request context is automatically popped at the end of the request
  155. for you. In debug mode the request context is kept around if
  156. exceptions happen so that interactive debuggers have a chance to
  157. introspect the data. With 0.4 this can also be forced for requests
  158. that did not fail and outside of `DEBUG` mode. By setting
  159. ``'flask._preserve_context'`` to `True` on the WSGI environment the
  160. context will not pop itself at the end of the request. This is used by
  161. the :meth:`~flask.Flask.test_client` for example to implement the
  162. deferred cleanup functionality.
  163. You might find this helpful for unittests where you need the
  164. information from the context local around for a little longer. Make
  165. sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in
  166. that situation, otherwise your unittests will leak memory.
  167. """
  168. def __init__(self, app, environ, request=None):
  169. self.app = app
  170. if request is None:
  171. request = app.request_class(environ)
  172. self.request = request
  173. self.url_adapter = app.create_url_adapter(self.request)
  174. self.flashes = None
  175. self.session = None
  176. # Request contexts can be pushed multiple times and interleaved with
  177. # other request contexts. Now only if the last level is popped we
  178. # get rid of them. Additionally if an application context is missing
  179. # one is created implicitly so for each level we add this information
  180. self._implicit_app_ctx_stack = []
  181. # indicator if the context was preserved. Next time another context
  182. # is pushed the preserved context is popped.
  183. self.preserved = False
  184. # remembers the exception for pop if there is one in case the context
  185. # preservation kicks in.
  186. self._preserved_exc = None
  187. # Functions that should be executed after the request on the response
  188. # object. These will be called before the regular "after_request"
  189. # functions.
  190. self._after_request_functions = []
  191. self.match_request()
  192. # XXX: Support for deprecated functionality. This is going away with
  193. # Flask 1.0
  194. blueprint = self.request.blueprint
  195. if blueprint is not None:
  196. # better safe than sorry, we don't want to break code that
  197. # already worked
  198. bp = app.blueprints.get(blueprint)
  199. if bp is not None and blueprint_is_module(bp):
  200. self.request._is_old_module = True
  201. def _get_g(self):
  202. return _app_ctx_stack.top.g
  203. def _set_g(self, value):
  204. _app_ctx_stack.top.g = value
  205. g = property(_get_g, _set_g)
  206. del _get_g, _set_g
  207. def copy(self):
  208. """Creates a copy of this request context with the same request object.
  209. This can be used to move a request context to a different greenlet.
  210. Because the actual request object is the same this cannot be used to
  211. move a request context to a different thread unless access to the
  212. request object is locked.
  213. .. versionadded:: 0.10
  214. """
  215. return self.__class__(self.app,
  216. environ=self.request.environ,
  217. request=self.request
  218. )
  219. def match_request(self):
  220. """Can be overridden by a subclass to hook into the matching
  221. of the request.
  222. """
  223. try:
  224. url_rule, self.request.view_args = \
  225. self.url_adapter.match(return_rule=True)
  226. self.request.url_rule = url_rule
  227. except HTTPException as e:
  228. self.request.routing_exception = e
  229. def push(self):
  230. """Binds the request context to the current context."""
  231. # If an exception occurs in debug mode or if context preservation is
  232. # activated under exception situations exactly one context stays
  233. # on the stack. The rationale is that you want to access that
  234. # information under debug situations. However if someone forgets to
  235. # pop that context again we want to make sure that on the next push
  236. # it's invalidated, otherwise we run at risk that something leaks
  237. # memory. This is usually only a problem in testsuite since this
  238. # functionality is not active in production environments.
  239. top = _request_ctx_stack.top
  240. if top is not None and top.preserved:
  241. top.pop(top._preserved_exc)
  242. # Before we push the request context we have to ensure that there
  243. # is an application context.
  244. app_ctx = _app_ctx_stack.top
  245. if app_ctx is None or app_ctx.app != self.app:
  246. app_ctx = self.app.app_context()
  247. app_ctx.push()
  248. self._implicit_app_ctx_stack.append(app_ctx)
  249. else:
  250. self._implicit_app_ctx_stack.append(None)
  251. _request_ctx_stack.push(self)
  252. # Open the session at the moment that the request context is
  253. # available. This allows a custom open_session method to use the
  254. # request context (e.g. code that access database information
  255. # stored on `g` instead of the appcontext).
  256. self.session = self.app.open_session(self.request)
  257. if self.session is None:
  258. self.session = self.app.make_null_session()
  259. def pop(self, exc=None):
  260. """Pops the request context and unbinds it by doing that. This will
  261. also trigger the execution of functions registered by the
  262. :meth:`~flask.Flask.teardown_request` decorator.
  263. .. versionchanged:: 0.9
  264. Added the `exc` argument.
  265. """
  266. app_ctx = self._implicit_app_ctx_stack.pop()
  267. clear_request = False
  268. if not self._implicit_app_ctx_stack:
  269. self.preserved = False
  270. self._preserved_exc = None
  271. if exc is None:
  272. exc = sys.exc_info()[1]
  273. self.app.do_teardown_request(exc)
  274. # If this interpreter supports clearing the exception information
  275. # we do that now. This will only go into effect on Python 2.x,
  276. # on 3.x it disappears automatically at the end of the exception
  277. # stack.
  278. if hasattr(sys, 'exc_clear'):
  279. sys.exc_clear()
  280. request_close = getattr(self.request, 'close', None)
  281. if request_close is not None:
  282. request_close()
  283. clear_request = True
  284. rv = _request_ctx_stack.pop()
  285. assert rv is self, 'Popped wrong request context. (%r instead of %r)' \
  286. % (rv, self)
  287. # get rid of circular dependencies at the end of the request
  288. # so that we don't require the GC to be active.
  289. if clear_request:
  290. rv.request.environ['werkzeug.request'] = None
  291. # Get rid of the app as well if necessary.
  292. if app_ctx is not None:
  293. app_ctx.pop(exc)
  294. def auto_pop(self, exc):
  295. if self.request.environ.get('flask._preserve_context') or \
  296. (exc is not None and self.app.preserve_context_on_exception):
  297. self.preserved = True
  298. self._preserved_exc = exc
  299. else:
  300. self.pop(exc)
  301. def __enter__(self):
  302. self.push()
  303. return self
  304. def __exit__(self, exc_type, exc_value, tb):
  305. # do not pop the request stack if we are in debug mode and an
  306. # exception happened. This will allow the debugger to still
  307. # access the request object in the interactive shell. Furthermore
  308. # the context can be force kept alive for the test client.
  309. # See flask.testing for how this works.
  310. self.auto_pop(exc_value)
  311. def __repr__(self):
  312. return '<%s \'%s\' [%s] of %s>' % (
  313. self.__class__.__name__,
  314. self.request.url,
  315. self.request.method,
  316. self.app.name,
  317. )