Skip to content

Commit 84f5208

Browse files
committed
gh-158980: Fix StreamReader.decode() of the Windows cpXXX codecs
StreamReader.read() calls decode() with two arguments, but the codecs created by create_win32_code_page_codec() required a third one, final. Make it optional, as in encodings.mbcs.
1 parent 9d22a53 commit 84f5208

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎Lib/encodings/_win_cp_codecs.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def encode(self, input, errors='strict'):
2222
return code_page_encode(cp, input, errors)
2323

2424
class StreamReader(codecs.StreamReader):
25-
def decode(self, input, errors, final):
25+
def decode(self, input, errors, final=False):
2626
return code_page_decode(cp, input, errors, final)
2727

2828
return codecs.CodecInfo(

‎Lib/test/test_codecs.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,6 +3654,33 @@ def test_incremental(self):
36543654
False)
36553655
self.assertEqual(decoded, ('abc', 3))
36563656

3657+
def test_stream_reader(self):
3658+
# cp20866 and cp20932 are implemented by encodings._win_cp_codecs.
3659+
for cp, text in ((20866, '\u043f\u0440\u0438\n\u043c\u0438\u0440\n'),
3660+
(20932, 'a\u3042\u65e5b\n\u672c\n')):
3661+
with self.subTest(cp=cp):
3662+
if not is_code_page_present(cp):
3663+
self.skipTest(f'cp{cp} is not present')
3664+
info = codecs.lookup(f'cp{cp}')
3665+
data = text.encode(f'cp{cp}')
3666+
3667+
reader = info.streamreader(io.BytesIO(data))
3668+
self.assertEqual(reader.read(), text)
3669+
3670+
reader = info.streamreader(io.BytesIO(data))
3671+
self.assertEqual(list(iter(reader.readline, '')),
3672+
text.splitlines(keepends=True))
3673+
3674+
# A partial multibyte sequence is kept until the next read.
3675+
reader = info.streamreader(io.BytesIO(data))
3676+
self.assertEqual(list(iter(lambda: reader.read(1), '')),
3677+
list(text))
3678+
3679+
stream = codecs.StreamReaderWriter(io.BytesIO(data),
3680+
info.streamreader,
3681+
info.streamwriter)
3682+
self.assertEqual(stream.read(), text)
3683+
36573684
def test_mbcs_code_page(self):
36583685
# Check that codec for the current Windows (ANSII) code page is
36593686
# always available.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :exc:`TypeError` when reading from the stream reader (for example, one
2+
returned by :func:`codecs.getreader`) of a "cpXXX" codec that is implemented
3+
using the Windows code page API.

0 commit comments

Comments
 (0)