]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. (GH-3201) (...
authorOren Milman <orenmn@gmail.com>
Tue, 7 Nov 2017 00:17:54 +0000 (02:17 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 7 Nov 2017 00:17:54 +0000 (16:17 -0800)
Lib/test/test_io.py

index 3dc6e9ffcac157b7c0479b524c9c4954ff5e95cc..ea5ec656f755b31b600874319bdee89ab4cba5cb 100644 (file)
@@ -2666,6 +2666,22 @@ class TextIOWrapperTest(unittest.TestCase):
         t = self.TextIOWrapper(NonbytesStream('a'))
         self.assertEqual(t.read(), u'a')
 
+    def test_illegal_encoder(self):
+        # bpo-31271: A TypeError should be raised in case the return value of
+        # encoder's encode() is invalid.
+        class BadEncoder:
+            def encode(self, dummy):
+                return u'spam'
+        def get_bad_encoder(dummy):
+            return BadEncoder()
+        rot13 = codecs.lookup("rot13")
+        with support.swap_attr(rot13, '_is_text_encoding', True), \
+             support.swap_attr(rot13, 'incrementalencoder', get_bad_encoder):
+            t = io.TextIOWrapper(io.BytesIO(b'foo'), encoding="rot13")
+        with self.assertRaises(TypeError):
+            t.write('bar')
+            t.flush()
+
     def test_illegal_decoder(self):
         # Issue #17106
         # Bypass the early encoding check added in issue 20404