[Python-checkins] [3.7] bpo-33482: fix codecs.StreamRecoder.writelines (GH-6779) (GH-13502)

Miss Islington (bot) webhook-mailer at python.org
Wed May 22 12:28:43 EDT 2019


https://github.com/python/cpython/commit/81c5ec9e417aebfe92945a05771006e4241f4e08
commit: 81c5ec9e417aebfe92945a05771006e4241f4e08
branch: 3.7
author: Jelle Zijlstra <jelle.zijlstra at gmail.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-05-22T09:28:38-07:00
summary:

[3.7] bpo-33482: fix codecs.StreamRecoder.writelines (GH-6779) (GH-13502)



A very simple fix. I found this while writing typeshed stubs for StreamRecoder.

https://bugs.python.org/issue33482.
(cherry picked from commit b3be4072888a4ce054993c2801802721466ea02d)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>





https://bugs.python.org/issue33482

files:
A Misc/NEWS.d/next/Documentation/2018-05-13-10-36-37.bpo-33482.jalAaQ.rst
M Lib/codecs.py
M Lib/test/test_codecs.py

diff --git a/Lib/codecs.py b/Lib/codecs.py
index a70ed20f2bc7..3cd78fc9f197 100644
--- a/Lib/codecs.py
+++ b/Lib/codecs.py
@@ -838,7 +838,7 @@ def write(self, data):
 
     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 5ba2c7bdc5f8..a3a2f6563a1f 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -3298,5 +3298,26 @@ def test_decode(self):
                 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'))
+
+
 if __name__ == "__main__":
     unittest.main()
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 000000000000..bda5be87723d
--- /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.



More information about the Python-checkins mailing list