8000 [2.7] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. … · python/cpython@3053769 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3053769

Browse files
orenmnvstinner
authored andcommitted
[2.7] bpo-31271: Fix an assertion failure in io.TextIOWrapper.write. (GH-3201) (#3951)
1 parent 4376a22 commit 3053769

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Lib/test/test_io.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,22 @@ class NonbytesStream(self.StringIO):
26662666
t = self.TextIOWrapper(NonbytesStream('a'))
26672667
self.assertEqual(t.read(), u'a')
26682668

2669+
def test_illegal_encoder(self):
2670+
# bpo-31271: A TypeError should be raised in case the return value of
2671+
# encoder's encode() is invalid.
2672+
class BadEncoder:
2673+
def encode(self, dummy):
2674+
return u'spam'
2675+
def get_bad_encoder(dummy):
2676+
return BadEncoder()
2677+
rot13 = codecs.lookup("rot13")
2678+
with support.swap_attr(rot13, '_is_text_encoding', True), \
2679+
support.swap_attr(rot13, 'incrementalencoder', get_bad_encoder):
2680+
t = io.TextIOWrapper(io.BytesIO(b'foo'), encoding="rot13")
2681+
with self.assertRaises(TypeError):
2682+
t.write('bar')
2683+
t.flush()
2684+
26692685
def test_illegal_decoder(self):
26702686
# Issue #17106
26712687
# Bypass the early encoding check added in issue 20404

0 commit comments

Comments
 (0)
0