Repository navigation
gh-160309: Fix StreamReader.read() silently dropping an incomplete final character - #160642
saiyam-jain-dev wants to merge 3 commits into
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
BHUVANSH855
left a comment
There was a problem hiding this comment.
CLA check is failing, sign it we can't review without that.
29f7951 to
db97211
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
The commit author email has been updated to match the signed CLA, and the CLA check is now passing (green ✅). I have ensured @pganssle @BHUVANSH855 Whenever you have a chance, could you take another look at this PR? Thanks! |
… final character When a byte stream ends with an incomplete multibyte sequence (e.g. a truncated UTF-8 character), StreamReader.read() would silently discard those bytes: the read loop exited on empty newdata while bytebuffer still held the fragment, and bytebuffer was never decoded. Fix by detecting leftover bytes in bytebuffer when the underlying stream is exhausted and decoding them immediately. The codec's stateless decode function (e.g. utf_8_decode with final=True) then applies the error handler: 'strict' raises UnicodeDecodeError, 'replace' emits the replacement character, and 'ignore' drops the bytes - matching the behaviour of bytes.decode() and io.TextIOWrapper.
db97211 to
e6fb522
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Hi @pganssle, @BHUVANSH855, @sobolevn — just a friendly ping. The CLA is signed ✅ and the main CI checks (lint, change-detection, all platform builds) are green. The failing checks (WASI, Hypothesis, ASan/UBSan/MSan) appear to be pre-existing flaky/infrastructure issues unrelated to this PR — the same failures are present across other open PRs on the same day. The core fix (passing \inal=True\ during EOF in StreamReader) and 4 regression tests in \ est_codecs.py\ are ready for review whenever you have bandwidth. Happy to adjust anything! |
Summary
Fixes #160309
codecs.StreamReader.read()silently drops bytes when the stream ends with an incomplete multibyte sequence. The read loop exits on emptynewdatawhileself.bytebufferstill holds the trailing bytes, and those bytes were never decoded.Bug demonstration (before fix)
Every other decoding path raises or applies the error handler:
Fix
When the underlying stream is exhausted and
self.bytebufferis non-empty, decode the leftover bytes withfinal=True(falling back safely for 2-argument customdecodeimplementations) immediately before breaking out of the loop:errors='strict'→ raisesUnicodeDecodeErrorerrors='replace'→ emits the replacement character\ufffderrors='ignore'→ drops the bytesAlso updated
StreamReader.decodeinutf_16.py,utf_32.py,utf_8_sig.py,unicode_escape.py, andraw_unicode_escape.pyto forwardfinal=False, matchingutf_8.pyandIncrementalDecoder.Added comprehensive test cases in
Lib/test/test_codecs.py(StreamReaderTest):test_incomplete_final_character_stricttest_incomplete_final_character_replacetest_incomplete_final_character_ignoretest_custom_two_arg_streamreader_subclassThis matches the behavior of
bytes.decode()andio.TextIOWrapper.codecs.StreamReader.read()silently drops an incomplete character at the end of the stream #160309