Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/encodings/_win_cp_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading