Issue #6241: Better type checking for the arguments of io.StringIO.
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index bbf65bc..5458f99 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -1924,8 +1924,10 @@
# C version, even under Windows.
if newline is None:
self._writetranslate = False
- if initial_value:
+ if initial_value is not None:
if not isinstance(initial_value, str):
+ raise TypeError("initial_value must be str or None, not {0}"
+ .format(type(initial_value).__name__))
initial_value = str(initial_value)
self.write(initial_value)
self.seek(0)
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 0b25283..670dab9 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -140,6 +140,7 @@
self.assertEqual(memio.getvalue(), buf * 2)
memio.__init__(buf)
self.assertEqual(memio.getvalue(), buf)
+ self.assertRaises(TypeError, memio.__init__, [])
def test_read(self):
buf = self.buftype("1234567890")
@@ -530,6 +531,13 @@
memio = self.ioclass("a\r\nb\r\n", newline=None)
self.assertEqual(memio.read(5), "a\nb\n")
+ def test_newline_argument(self):
+ self.assertRaises(TypeError, self.ioclass, newline=b"\n")
+ self.assertRaises(ValueError, self.ioclass, newline="error")
+ # These should not raise an error
+ for newline in (None, "", "\n", "\r", "\r\n"):
+ self.ioclass(newline=newline)
+
class CBytesIOTest(PyBytesIOTest):
ioclass = io.BytesIO