ctx.py 16 KB

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