The actual WSGI application. This is not implemented in :meth:`__call__` so that middlewares can be applied without losing a reference to the app object. Instead of doing this:: app = MyMiddleware(app) It's a better idea to do this instead:: app.wsg
(
self, environ: WSGIEnvironment, start_response: StartResponse
)
| 1477 | builder.close() |
| 1478 | |
| 1479 | def wsgi_app( |
| 1480 | self, environ: WSGIEnvironment, start_response: StartResponse |
| 1481 | ) -> cabc.Iterable[bytes]: |
| 1482 | """The actual WSGI application. This is not implemented in |
| 1483 | :meth:`__call__` so that middlewares can be applied without |
| 1484 | losing a reference to the app object. Instead of doing this:: |
| 1485 | |
| 1486 | app = MyMiddleware(app) |
| 1487 | |
| 1488 | It's a better idea to do this instead:: |
| 1489 | |
| 1490 | app.wsgi_app = MyMiddleware(app.wsgi_app) |
| 1491 | |
| 1492 | Then you still have the original application object around and |
| 1493 | can continue to call methods on it. |
| 1494 | |
| 1495 | .. versionchanged:: 0.7 |
| 1496 | Teardown events for the request and app contexts are called |
| 1497 | even if an unhandled error occurs. Other events may not be |
| 1498 | called depending on when an error occurs during dispatch. |
| 1499 | See :ref:`callbacks-and-errors`. |
| 1500 | |
| 1501 | :param environ: A WSGI environment. |
| 1502 | :param start_response: A callable accepting a status code, |
| 1503 | a list of headers, and an optional exception context to |
| 1504 | start the response. |
| 1505 | """ |
| 1506 | ctx = self.request_context(environ) |
| 1507 | error: BaseException | None = None |
| 1508 | try: |
| 1509 | try: |
| 1510 | ctx.push() |
| 1511 | response = self.full_dispatch_request() |
| 1512 | except Exception as e: |
| 1513 | error = e |
| 1514 | response = self.handle_exception(e) |
| 1515 | except: |
| 1516 | error = sys.exc_info()[1] |
| 1517 | raise |
| 1518 | return response(environ, start_response) |
| 1519 | finally: |
| 1520 | if "werkzeug.debug.preserve_context" in environ: |
| 1521 | environ["werkzeug.debug.preserve_context"](_cv_app.get()) |
| 1522 | environ["werkzeug.debug.preserve_context"](_cv_request.get()) |
| 1523 | |
| 1524 | if error is not None and self.should_ignore_error(error): |
| 1525 | error = None |
| 1526 | |
| 1527 | ctx.pop(error) |
| 1528 | |
| 1529 | def __call__( |
| 1530 | self, environ: WSGIEnvironment, start_response: StartResponse |
no test coverage detected