Skip to content
Myr

Fixed `iter_text` adding an empty string (#2998)

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

authored by James Braza · committed by GitHub · parent 90538a3 · Browse files · 3 files+17 added−4 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+17 added−4 removed

  • tests:+11 added−0 removed
  • other:+6 added−4 removed

3 files+17 added−4 removed

modified:httpx/_decoders.py+4 added-2 removed
210 unmodified lines
211 self._chunk_size = chunk_size212213 def decode(self, content: str) -> typing.List[str]:214 if self._chunk_size is None:215 return [content]215 return [content] if content else []216217 self._buffer.write(content)218 if self._buffer.tell() >= self._chunk_size:219 value = self._buffer.getvalue()
59 unmodified lines
279 self.trailing_cr = True280 text = text[:-1]281282 if not text:283 return []283 # NOTE: the edge case input of empty text doesn't occur in practice,284 # because other httpx internals filter out this value285 return [] # pragma: no cover286287 trailing_newline = text[-1] in NEWLINE_CHARS288 lines = text.splitlines()289
38 unmodified lines
modified:httpx/_models.py+2 added-2 removed
851 unmodified lines
852 for chunk in chunker.decode(text_content):853 yield chunk854 text_content = decoder.flush()855 for chunk in chunker.decode(text_content):856 yield chunk856 yield chunk # pragma: no cover857 for chunk in chunker.flush():858 yield chunk859860 def iter_lines(self) -> typing.Iterator[str]:
95 unmodified lines
956 for chunk in chunker.decode(text_content):957 yield chunk958 text_content = decoder.flush()959 for chunk in chunker.decode(text_content):960 yield chunk960 yield chunk # pragma: no cover961 for chunk in chunker.flush():962 yield chunk963964 async def aiter_lines(self) -> typing.AsyncIterator[str]:
251 unmodified lines
modified:tests/test_decoders.py+11 added
217 unmodified lines
218 response.read()219 assert response.text == ""220221222@pytest.mark.parametrize(223 ["data", "expected"],224 [((b"Hello,", b" world!"), ["Hello,", " world!"])],225)226def test_streaming_text_decoder(227 data: typing.Iterable[bytes], expected: typing.List[str]228) -> None:229 response = httpx.Response(200, content=iter(data))230 assert list(response.iter_text()) == expected231232233def test_line_decoder_nl():234 response = httpx.Response(200, content=[b""])235 assert list(response.iter_lines()) == []236
49 unmodified lines