Skip to content
Myr

Ensure that iter_bytes does not ever yield any zero-length chunks (#2068)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent 0088253 · Browse files · 3 files+29 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+29 added−3 removed

  • tests:+26 added−0 removed
  • other:+3 added−3 removed

3 files+29 added−3 removed

modified:httpx/_decoders.py+1 added-1 removed
170 unmodified lines
171 self._chunk_size = chunk_size172173 def decode(self, content: bytes) -> typing.List[bytes]:174 if self._chunk_size is None:175 return [content]175 return [content] if content else []176177 self._buffer.write(content)178 if self._buffer.tell() >= self._chunk_size:179 value = self._buffer.getvalue()
154 unmodified lines
modified:httpx/_models.py+2 added-2 removed
1583 unmodified lines
1584 for chunk in chunker.decode(decoded):1585 yield chunk1586 decoded = decoder.flush()1587 for chunk in chunker.decode(decoded):1588 yield chunk1588 yield chunk # pragma: nocover1589 for chunk in chunker.flush():1590 yield chunk15911592 def iter_text(self, chunk_size: int = None) -> typing.Iterator[str]:
89 unmodified lines
1682 for chunk in chunker.decode(decoded):1683 yield chunk1684 decoded = decoder.flush()1685 for chunk in chunker.decode(decoded):1686 yield chunk1686 yield chunk # pragma: nocover1687 for chunk in chunker.flush():1688 yield chunk16891690 async def aiter_text(self, chunk_size: int = None) -> typing.AsyncIterator[str]:
257 unmodified lines
modified:tests/models/test_responses.py+26 added
394 unmodified lines
395 parts = [part for part in response.iter_raw(chunk_size=20)]396 assert parts == [b"Hello, world!"]397398399def test_iter_raw_doesnt_return_empty_chunks():400 def streaming_body_with_empty_chunks():401 yield b"Hello, "402 yield b""403 yield b"world!"404 yield b""405406 response = httpx.Response(200, content=streaming_body_with_empty_chunks())407408 parts = [part for part in response.iter_raw()]409 assert parts == [b"Hello, ", b"world!"]410411412def test_iter_raw_on_iterable():413 response = httpx.Response(414 200,415 content=StreamingBody(),
122 unmodified lines
538 parts = [part for part in response.iter_bytes()]539 assert parts == []540541542def test_iter_bytes_doesnt_return_empty_chunks():543 def streaming_body_with_empty_chunks():544 yield b"Hello, "545 yield b""546 yield b"world!"547 yield b""548549 response = httpx.Response(200, content=streaming_body_with_empty_chunks())550551 parts = [part for part in response.iter_bytes()]552 assert parts == [b"Hello, ", b"world!"]553554555@pytest.mark.asyncio556async def test_aiter_bytes():557 response = httpx.Response(558 200,
412 unmodified lines