From b3be4072888a4ce054993c2801802721466ea02d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 22 May 2019 08:18:26 -0700 Subject: [PATCH] bpo-33482: fix codecs.StreamRecoder.writelines (GH-6779) A very simple fix. I found this while writing typeshed stubs for StreamRecoder. https://bugs.python.org/issue33482 --- Lib/codecs.py | 2 +- Lib/test/test_codecs.py | 21 +++++++++++++++++++ .../2018-05-13-10-36-37.bpo-33482.jalAaQ.rst | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Documentation/2018-05-13-10-36-37.bpo-33482.jalAaQ.rst diff --git a/Lib/codecs.py b/Lib/codecs.py index 6b028adb1d2..884be0b2c02 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -838,7 +838,7 @@ class StreamRecoder: def writelines(self, list): - data = ''.join(list) + data = b''.join(list) data, bytesdecoded = self.decode(data, self.errors) return self.writer.write(data) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 8c14f5981d0..f665febfc90 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3146,6 +3146,27 @@ class Latin1Test(unittest.TestCase): self.assertEqual(data.decode('latin1'), expected) +class StreamRecoderTest(unittest.TestCase): + def test_writelines(self): + bio = io.BytesIO() + codec = codecs.lookup('ascii') + sr = codecs.StreamRecoder(bio, codec.encode, codec.decode, + encodings.ascii.StreamReader, encodings.ascii.StreamWriter) + sr.writelines([b'a', b'b']) + self.assertEqual(bio.getvalue(), b'ab') + + def test_write(self): + bio = io.BytesIO() + codec = codecs.lookup('latin1') + # Recode from Latin-1 to utf-8. + sr = codecs.StreamRecoder(bio, codec.encode, codec.decode, + encodings.utf_8.StreamReader, encodings.utf_8.StreamWriter) + + text = 'àñé' + sr.write(text.encode('latin1')) + self.assertEqual(bio.getvalue(), text.encode('utf-8')) + + @unittest.skipIf(_testcapi is None, 'need _testcapi module') class LocaleCodecTest(unittest.TestCase): """ diff --git a/Misc/NEWS.d/next/Documentation/2018-05-13-10-36-37.bpo-33482.jalAaQ.rst b/Misc/NEWS.d/next/Documentation/2018-05-13-10-36-37.bpo-33482.jalAaQ.rst new file mode 100644 index 00000000000..bda5be87723 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2018-05-13-10-36-37.bpo-33482.jalAaQ.rst @@ -0,0 +1 @@ +Make `codecs.StreamRecoder.writelines` take a list of bytes.