MCPcopy
hub / github.com/pallets/flask / RequestContext

Class RequestContext

src/flask/ctx.py:287–459  ·  view source on GitHub ↗

The request context contains per-request information. The Flask app creates and pushes it at the beginning of the request, then pops it at the end of the request. It will create the URL adapter and request object for the WSGI environment provided. Do not attempt to use this class di

Source from the content-addressed store, hash-verified

285
286
287class RequestContext:
288 """The request context contains per-request information. The Flask
289 app creates and pushes it at the beginning of the request, then pops
290 it at the end of the request. It will create the URL adapter and
291 request object for the WSGI environment provided.
292
293 Do not attempt to use this class directly, instead use
294 :meth:`~flask.Flask.test_request_context` and
295 :meth:`~flask.Flask.request_context` to create this object.
296
297 When the request context is popped, it will evaluate all the
298 functions registered on the application for teardown execution
299 (:meth:`~flask.Flask.teardown_request`).
300
301 The request context is automatically popped at the end of the
302 request. When using the interactive debugger, the context will be
303 restored so ``request`` is still accessible. Similarly, the test
304 client can preserve the context after the request ends. However,
305 teardown functions may already have closed some resources such as
306 database connections.
307 """
308
309 def __init__(
310 self,
311 app: Flask,
312 environ: WSGIEnvironment,
313 request: Request | None = None,
314 session: SessionMixin | None = None,
315 ) -> None:
316 self.app = app
317 if request is None:
318 request = app.request_class(environ)
319 request.json_module = app.json
320 self.request: Request = request
321 self.url_adapter = None
322 try:
323 self.url_adapter = app.create_url_adapter(self.request)
324 except HTTPException as e:
325 self.request.routing_exception = e
326 self.flashes: list[tuple[str, str]] | None = None
327 self._session: SessionMixin | None = session
328 # Functions that should be executed after the request on the response
329 # object. These will be called before the regular "after_request"
330 # functions.
331 self._after_request_functions: list[ft.AfterRequestCallable[t.Any]] = []
332
333 self._cv_tokens: list[
334 tuple[contextvars.Token[RequestContext], AppContext | None]
335 ] = []
336
337 def copy(self) -> RequestContext:
338 """Creates a copy of this request context with the same request object.
339 This can be used to move a request context to a different greenlet.
340 Because the actual request object is the same this cannot be used to
341 move a request context to a different thread unless access to the
342 request object is locked.
343
344 .. versionadded:: 0.10

Callers 1

request_contextMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected