ctx.py 16 KB

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