Issue #17106: Fix a segmentation fault in io.TextIOWrapper when an underlying

stream or a decoder produces data of an unexpected type (i.e. when
io.TextIOWrapper initialized with text stream or use bytes-to-bytes codec).
This commit is contained in:
Serhiy Storchaka 2013-02-03 17:03:31 +02:00
parent 028915e6ea
commit 94dc6736bd
3 changed files with 72 additions and 18 deletions

View file

@ -2481,6 +2481,30 @@ class TextIOWrapperTest(unittest.TestCase):
txt.write('5')
self.assertEqual(b''.join(raw._write_stack), b'123\n45')
def test_read_nonbytes(self):
# Issue #17106
# Crash when underlying read() returns non-bytes
t = self.TextIOWrapper(self.StringIO('a'))
self.assertRaises(TypeError, t.read, 1)
t = self.TextIOWrapper(self.StringIO('a'))
self.assertRaises(TypeError, t.readline)
t = self.TextIOWrapper(self.StringIO('a'))
self.assertRaises(TypeError, t.read)
def test_illegal_decoder(self):
# Issue #17106
# Crash when decoder returns non-string
t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n',
encoding='quopri_codec')
self.assertRaises(TypeError, t.read, 1)
t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n',
encoding='quopri_codec')
self.assertRaises(TypeError, t.readline)
t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n',
encoding='quopri_codec')
self.assertRaises(TypeError, t.read)
class CTextIOWrapperTest(TextIOWrapperTest):
def test_initialization(self):