diff --git a/Lib/encodings/_win_cp_codecs.py b/Lib/encodings/_win_cp_codecs.py index 4f8eb886794404..8040fdaaa3eb8a 100644 --- a/Lib/encodings/_win_cp_codecs.py +++ b/Lib/encodings/_win_cp_codecs.py @@ -22,7 +22,7 @@ def encode(self, input, errors='strict'): return code_page_encode(cp, input, errors) class StreamReader(codecs.StreamReader): - def decode(self, input, errors, final): + def decode(self, input, errors, final=False): return code_page_decode(cp, input, errors, final) return codecs.CodecInfo( diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 1916ee5507e972..9b9b88afbc0aeb 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3654,6 +3654,33 @@ def test_incremental(self): False) self.assertEqual(decoded, ('abc', 3)) + def test_stream_reader(self): + # cp20866 and cp20932 are implemented by encodings._win_cp_codecs. + for cp, text in ((20866, '\u043f\u0440\u0438\n\u043c\u0438\u0440\n'), + (20932, 'a\u3042\u65e5b\n\u672c\n')): + with self.subTest(cp=cp): + if not is_code_page_present(cp): + self.skipTest(f'cp{cp} is not present') + info = codecs.lookup(f'cp{cp}') + data = text.encode(f'cp{cp}') + + reader = info.streamreader(io.BytesIO(data)) + self.assertEqual(reader.read(), text) + + reader = info.streamreader(io.BytesIO(data)) + self.assertEqual(list(iter(reader.readline, '')), + text.splitlines(keepends=True)) + + # A partial multibyte sequence is kept until the next read. + reader = info.streamreader(io.BytesIO(data)) + self.assertEqual(list(iter(lambda: reader.read(1), '')), + list(text)) + + stream = codecs.StreamReaderWriter(io.BytesIO(data), + info.streamreader, + info.streamwriter) + self.assertEqual(stream.read(), text) + def test_mbcs_code_page(self): # Check that codec for the current Windows (ANSII) code page is # always available. diff --git a/Misc/NEWS.d/next/Windows/2026-10-07-12-21-36.gh-issue-158980.TkelJq.rst b/Misc/NEWS.d/next/Windows/2026-10-07-12-21-36.gh-issue-158980.TkelJq.rst new file mode 100644 index 00000000000000..6f657c7e2c2640 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-10-07-12-21-36.gh-issue-158980.TkelJq.rst @@ -0,0 +1,3 @@ +Fix :exc:`TypeError` when reading from the stream reader (for example, one +returned by :func:`codecs.getreader`) of a "cpXXX" codec that is implemented +using the Windows code page API.