Method: flask.scaffold.Scaffold.teardown_request
Calls: 18, Exceptions: 0, Paths: 1Back
Path 1: 18 calls (1.0)
test_context_refcounts.
test_context_refcounts.
1@setupmethod
2 def teardown_request(self, f: T_teardown) -> T_teardown:
3 """Register a function to be called when the request context is
4 popped. Typically this happens at the end of each request, but
5 contexts may be pushed manually as well during testing.
6
7 .. code-block:: python
8
9 with app.test_request_context():
10 ...
11
12 When the ``with`` block exits (or ``ctx.pop()`` is called), the
13 teardown functions are called just before the request context is
14 made inactive.
15
16 When a teardown function was called because of an unhandled
17 exception it will be passed an error object. If an
18 :meth:`errorhandler` is registered, it will handle the exception
19 and the teardown will not receive it.
20
21 Teardown functions must avoid raising exceptions. If they
22 execute code that might fail they must surround that code with a
23 ``try``/``except`` block and log any errors.
24
25 The return values of teardown functions are ignored.
26
27 This is available on both app and blueprint objects. When used on an app, this
28 executes after every request. When used on a blueprint, this executes after
29 every request that the blueprint handles. To register with a blueprint and
30 execute after every request, use :meth:`.Blueprint.teardown_app_request`.
31 """
32 self.teardown_request_funcs.setdefault(None, []).append(f)
33 return f