summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_io.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-02-03 15:10:42 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-02-03 15:10:42 (GMT)
commit354d50ee37f2c7e96812a2824b5e899cfd5ee3b1 (patch)
treeb26241769c6009b2df5c6a01631f24d4d2e0e767 /Lib/test/test_io.py
parentcc23cc672ff9164900b717bcc7f6484c03328bc0 (diff)
downloadcpython-354d50ee37f2c7e96812a2824b5e899cfd5ee3b1.zip
cpython-354d50ee37f2c7e96812a2824b5e899cfd5ee3b1.tar.gz
cpython-354d50ee37f2c7e96812a2824b5e899cfd5ee3b1.tar.bz2
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).
Diffstat (limited to 'Lib/test/test_io.py')
-rw-r--r--Lib/test/test_io.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 6132bae..aabc1d2 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -36,6 +36,7 @@ from itertools import cycle, count
from collections import deque
from UserList import UserList
from test import test_support as support
+import contextlib
import codecs
import io # C implementation of io
@@ -2419,6 +2420,39 @@ class TextIOWrapperTest(unittest.TestCase):
with self.assertRaises((AttributeError, TypeError)):
txt.buffer = buf
+ def test_read_nonbytes(self):
+ # Issue #17106
+ # Crash when underlying read() returns non-bytes
+ class NonbytesStream(self.StringIO):
+ read1 = self.StringIO.read
+ class NonbytesStream(self.StringIO):
+ read1 = self.StringIO.read
+ t = self.TextIOWrapper(NonbytesStream('a'))
+ with self.maybeRaises(TypeError):
+ t.read(1)
+ t = self.TextIOWrapper(NonbytesStream('a'))
+ with self.maybeRaises(TypeError):
+ t.readline()
+ t = self.TextIOWrapper(NonbytesStream('a'))
+ self.assertEqual(t.read(), u'a')
+
+ 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')
+ with self.maybeRaises(TypeError):
+ t.read(1)
+ t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n',
+ encoding='quopri_codec')
+ with self.maybeRaises(TypeError):
+ t.readline()
+ t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n',
+ encoding='quopri_codec')
+ with self.maybeRaises(TypeError):
+ t.read()
+
+
class CTextIOWrapperTest(TextIOWrapperTest):
def test_initialization(self):
@@ -2460,9 +2494,13 @@ class CTextIOWrapperTest(TextIOWrapperTest):
t2.buddy = t1
support.gc_collect()
+ maybeRaises = unittest.TestCase.assertRaises
+
class PyTextIOWrapperTest(TextIOWrapperTest):
- pass
+ @contextlib.contextmanager
+ def maybeRaises(self, *args, **kwds):
+ yield
class IncrementalNewlineDecoderTest(unittest.TestCase):