Skip to content

Commit

Permalink
Rework stream to use a conventional read loop
Browse files Browse the repository at this point in the history
  • Loading branch information
smason committed Nov 30, 2023
1 parent 4ae4b71 commit 043edc8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion changelog/3216.feature.rst
@@ -1,3 +1,3 @@
HTTPResponse.stream(amt=None) now yields data from non-chunked responses as it
is available. Previously all intermediate data was buffered and only returned
as a single bytes object at the end of the the response.
as a single, large, bytes object at the end of the the response.
8 changes: 5 additions & 3 deletions src/urllib3/response.py
Expand Up @@ -827,6 +827,8 @@ def _fp_read(
del data # to reduce peak memory usage by `max_chunk_amt`.
return buffer.getvalue()
elif read1:
# work around: https://github.com/python/cpython/issues/112064
amt = amt or self.length_remaining
return self._fp.read1(amt) if amt is not None else self._fp.read1()
else:
# StringIO doesn't like amt=None
Expand Down Expand Up @@ -1024,9 +1026,9 @@ 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
if not data:
break
yield data

# Overrides from io.IOBase
def readable(self) -> bool:
Expand Down

0 comments on commit 043edc8

Please sign in to comment.