Fixes for LineDecoding (#1075)

* 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

Co-authored-by: Sheridan C Rawlins <scr@verizonmedia.com>
This commit is contained in:
Tom Christie 2020-07-23 09:45:35 +01:00 committed by GitHub
parent bd339212de
commit 20f4911e80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 9 deletions

View File

@ -233,12 +233,18 @@ class LineDecoder:
def decode(self, text: str) -> typing.List[str]:
lines = []
if text.startswith("\n") and self.buffer and self.buffer[-1] == "\r":
# Handle the case where we have an "\r\n" split across
# our previous input, and our new chunk.
lines.append(self.buffer[:-1] + "\n")
self.buffer = ""
text = text[1:]
if text and self.buffer and self.buffer[-1] == "\r":
if text.startswith("\n"):
# Handle the case where we have an "\r\n" split across
# our previous input, and our new chunk.
lines.append(self.buffer[:-1] + "\n")
self.buffer = ""
text = text[1:]
else:
# Handle the case where we have "\r" at the end of our
# previous input.
lines.append(self.buffer[:-1] + "\n")
self.buffer = ""
while text:
num_chars = len(text)

View File

@ -247,14 +247,13 @@ def test_line_decoder_cr():
assert decoder.flush() == ["c\n"]
# Issue #1033
# TODO: This seems like another bug; fix expectations and results.
decoder = LineDecoder()
assert decoder.decode("") == []
assert decoder.decode("12345\r") == []
assert decoder.decode("foo ") == []
assert decoder.decode("foo ") == ["12345\n"]
assert decoder.decode("bar ") == []
assert decoder.decode("baz\r") == []
assert decoder.flush() == ["12345\rfoo bar baz\n"]
assert decoder.flush() == ["foo bar baz\n"]
def test_line_decoder_crnl():