From: Stanis Trendelenburg Date: Wed, 1 Sep 2021 15:21:01 +0000 (+0200) Subject: Close WSGI iterable when WSGIByteStream is closed (#1830) X-Git-Tag: 1.0.0.beta0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d3fcc74a56b32663ad97888eaea1315e28f78f4;p=thirdparty%2Fhttpx.git Close WSGI iterable when WSGIByteStream is closed (#1830) * Make test fail when WSGI iterable is not closed * Close WSGI iterable when WSGIByteStream is closed --- diff --git a/httpx/_transports/wsgi.py b/httpx/_transports/wsgi.py index 58e8309d..e71f1604 100644 --- a/httpx/_transports/wsgi.py +++ b/httpx/_transports/wsgi.py @@ -16,12 +16,17 @@ def _skip_leading_empty_chunks(body: typing.Iterable) -> typing.Iterable: class WSGIByteStream(SyncByteStream): def __init__(self, result: typing.Iterable[bytes]) -> None: + self._close = getattr(result, "close", None) self._result = _skip_leading_empty_chunks(result) def __iter__(self) -> typing.Iterator[bytes]: for part in self._result: yield part + def close(self) -> None: + if self._close is not None: + self._close() + class WSGITransport(BaseTransport): """ diff --git a/tests/test_wsgi.py b/tests/test_wsgi.py index b130e53c..164899b5 100644 --- a/tests/test_wsgi.py +++ b/tests/test_wsgi.py @@ -1,4 +1,5 @@ import sys +import wsgiref.validate from functools import partial import pytest @@ -19,7 +20,7 @@ def application_factory(output): for item in output: yield item - return application + return wsgiref.validate.validator(application) def echo_body(environ, start_response):