Called after the request is dispatched and the response is returned, right before the request context is popped. This calls all functions decorated with :meth:`teardown_request`, and :meth:`Blueprint.teardown_request` if a blueprint handled the request. Finally, the
(
self,
exc: BaseException | None = _sentinel, # type: ignore[assignment]
)
| 1324 | return response |
| 1325 | |
| 1326 | def do_teardown_request( |
| 1327 | self, |
| 1328 | exc: BaseException | None = _sentinel, # type: ignore[assignment] |
| 1329 | ) -> None: |
| 1330 | """Called after the request is dispatched and the response is |
| 1331 | returned, right before the request context is popped. |
| 1332 | |
| 1333 | This calls all functions decorated with |
| 1334 | :meth:`teardown_request`, and :meth:`Blueprint.teardown_request` |
| 1335 | if a blueprint handled the request. Finally, the |
| 1336 | :data:`request_tearing_down` signal is sent. |
| 1337 | |
| 1338 | This is called by |
| 1339 | :meth:`RequestContext.pop() <flask.ctx.RequestContext.pop>`, |
| 1340 | which may be delayed during testing to maintain access to |
| 1341 | resources. |
| 1342 | |
| 1343 | :param exc: An unhandled exception raised while dispatching the |
| 1344 | request. Detected from the current exception information if |
| 1345 | not passed. Passed to each teardown function. |
| 1346 | |
| 1347 | .. versionchanged:: 0.9 |
| 1348 | Added the ``exc`` argument. |
| 1349 | """ |
| 1350 | if exc is _sentinel: |
| 1351 | exc = sys.exc_info()[1] |
| 1352 | |
| 1353 | for name in chain(request.blueprints, (None,)): |
| 1354 | if name in self.teardown_request_funcs: |
| 1355 | for func in reversed(self.teardown_request_funcs[name]): |
| 1356 | self.ensure_sync(func)(exc) |
| 1357 | |
| 1358 | request_tearing_down.send(self, _async_wrapper=self.ensure_sync, exc=exc) |
| 1359 | |
| 1360 | def do_teardown_appcontext( |
| 1361 | self, |