]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Close WSGI iterable when WSGIByteStream is closed (#1830)
authorStanis Trendelenburg <stanis.trendelenburg@gmail.com>
Wed, 1 Sep 2021 15:21:01 +0000 (17:21 +0200)
committerGitHub <noreply@github.com>
Wed, 1 Sep 2021 15:21:01 +0000 (16:21 +0100)
* Make test fail when WSGI iterable is not closed

* Close WSGI iterable when WSGIByteStream is closed

httpx/_transports/wsgi.py
tests/test_wsgi.py

index 58e8309d8a8a3b668141214faa51810b2cf37452..e71f1604d8c747171cbc45c1630c846f3920220b 100644 (file)
@@ -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):
     """
index b130e53c40db3c428aa1cd0d383c55b7c32b26c0..164899b57f0fa8b80d09635ea512a8877a0f2461 100644 (file)
@@ -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):