Skip to content
Myr

Fixes for LineDecoding (#1075)

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

* Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines. This seems like a one character fix + the test. * Update the tests. * Undo formatting/indentation. * Update long comment. * Fix trailing cr line decoding

authored by Tom Christie · committed by GitHub · parent bd33921 · Browse files · 2 files+14 added−9 removed
This commit

State

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

Trailers

  • Co-authored-by: Sheridan C Rawlins <scr@verizonmedia.com>

Diff stats

2 files+14 added−9 removed

  • tests:+2 added−3 removed
  • other:+12 added−6 removed

2 files+14 added−9 removed

modified:httpx/_decoders.py+12 added-6 removed
231 unmodified lines
232233 def decode(self, text: str) -> typing.List[str]:234 lines = []235236 if text and self.buffer and self.buffer[-1] == "\r":236 if text.startswith("\n") and self.buffer and self.buffer[-1] == "\r":237 # Handle the case where we have an "\r\n" split across238 # our previous input, and our new chunk.239 lines.append(self.buffer[:-1] + "\n")240 self.buffer = ""241 text = text[1:]237 if text.startswith("\n"):238 # Handle the case where we have an "\r\n" split across239 # our previous input, and our new chunk.240 lines.append(self.buffer[:-1] + "\n")241 self.buffer = ""242 text = text[1:]243 else:244 # Handle the case where we have "\r" at the end of our245 # previous input.246 lines.append(self.buffer[:-1] + "\n")247 self.buffer = ""248249 while text:250 num_chars = len(text)251 for idx in range(num_chars):
47 unmodified lines
modified:tests/test_decoders.py+2 added-3 removed
245 unmodified lines
246 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]247 assert decoder.flush() == ["c\n"]248249 # Issue #1033250 # TODO: This seems like another bug; fix expectations and results.250 decoder = LineDecoder()251 assert decoder.decode("") == []252 assert decoder.decode("12345\r") == []254 assert decoder.decode("foo ") == []253 assert decoder.decode("foo ") == ["12345\n"]254 assert decoder.decode("bar ") == []255 assert decoder.decode("baz\r") == []257 assert decoder.flush() == ["12345\rfoo bar baz\n"]256 assert decoder.flush() == ["foo bar baz\n"]257258259def test_line_decoder_crnl():260 decoder = LineDecoder()
31 unmodified lines