Skip to content

Commit

Permalink
Allow HTTPResponse.stream to use read1 when amt=None
Browse files Browse the repository at this point in the history
  • Loading branch information
smason committed Dec 4, 2023
1 parent 4ae4b71 commit 24fb044
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/urllib3/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,9 +1024,14 @@ def stream(
data = self.read1(amt=amt, decode_content=decode_content)
else:
data = self.read(amt=amt, decode_content=decode_content)

if data:
yield data
else:
if self._fp:
self._fp.close()
break
if self.length_remaining:
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)

# Overrides from io.IOBase
def readable(self) -> bool:
Expand Down
5 changes: 3 additions & 2 deletions test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,10 +1036,11 @@ def read1(self, amt: int | None = None) -> bytes:
try:
return next(self.chunkiter)
except StopIteration:
# http.client automatically closes at stream end
self.closed = True
return b""

def close(self) -> None:
self.closed = True

chunks = b"foo longer bar".split()
fp = MockHTTPRequest(iter(chunks))
resp = HTTPResponse(fp, preload_content=False) # type: ignore[arg-type]
Expand Down

0 comments on commit 24fb044

Please sign in to comment.