Commit 9d2ac227 authored by Walter Dörwald's avatar Walter Dörwald

Fix io.StringIO: String are stored encoded (using "unicode-internal" as the

encoding) which makes the buffer mutable. Strings are encoded on the way in
and decoded on the way out.

Use io.StringIO in test_codecs.py.

Fix the base64_codec test in test_codecs.py.
parent c9e363c2
...@@ -581,10 +581,10 @@ class BytesIO(_MemoryIOMixin): ...@@ -581,10 +581,10 @@ class BytesIO(_MemoryIOMixin):
# XXX More docs # XXX More docs
def __init__(self, inital_bytes=None): def __init__(self, initial_bytes=None):
buffer = b"" buffer = b""
if inital_bytes is not None: if initial_bytes is not None:
buffer += inital_bytes buffer += initial_bytes
_MemoryIOMixin.__init__(self, buffer) _MemoryIOMixin.__init__(self, buffer)
...@@ -595,21 +595,36 @@ class StringIO(_MemoryIOMixin): ...@@ -595,21 +595,36 @@ class StringIO(_MemoryIOMixin):
# XXX More docs # XXX More docs
# Reuses the same code as BytesIO, just with a string rather that # Reuses the same code as BytesIO, but encode strings on the way in
# bytes as the _buffer value. # and decode them on the way out.
# XXX This doesn't work; _MemoryIOMixin's write() and truncate() def __init__(self, initial_string=None):
# methods assume the buffer is mutable. Simply redefining those if initial_string is not None:
# to use slice concatenation will make it awfully slow (in fact, buffer = initial_string.encode("unicode-internal")
# quadratic in the number of write() calls). Also, there are no else:
# readline() and readlines() methods. Etc., etc. buffer = b""
def __init__(self, inital_string=None):
buffer = ""
if inital_string is not None:
buffer += inital_string
_MemoryIOMixin.__init__(self, buffer) _MemoryIOMixin.__init__(self, buffer)
def getvalue(self):
return self._buffer.encode("unicode-internal")
def read(self, n=-1):
return super(StringIO, self).read(n*2).decode("unicode-internal")
def write(self, s):
return super(StringIO, self).write(s.encode("unicode-internal"))//2
def seek(self, pos, whence=0):
return super(StringIO, self).seek(2*pos, whence)//2
def tell(self):
return super(StringIO, self).tell()//2
def truncate(self, pos=None):
if pos is not None:
pos *= 2
return super(StringIO, self).truncate(pos)//2
def readinto(self, b: bytes) -> int: def readinto(self, b: bytes) -> int:
self._unsupported("readinto") self._unsupported("readinto")
......
...@@ -2,7 +2,6 @@ from test import test_support ...@@ -2,7 +2,6 @@ from test import test_support
import unittest import unittest
import codecs import codecs
import sys, _testcapi, io import sys, _testcapi, io
from StringIO import StringIO
class Queue(object): class Queue(object):
""" """
...@@ -493,7 +492,7 @@ class EscapeDecodeTest(unittest.TestCase): ...@@ -493,7 +492,7 @@ class EscapeDecodeTest(unittest.TestCase):
class RecodingTest(unittest.TestCase): class RecodingTest(unittest.TestCase):
def test_recoding(self): def test_recoding(self):
f = StringIO() f = io.StringIO()
f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8") f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
f2.write("a") f2.write("a")
f2.close() f2.close()
...@@ -991,14 +990,14 @@ class Str2StrTest(unittest.TestCase): ...@@ -991,14 +990,14 @@ class Str2StrTest(unittest.TestCase):
def test_read(self): def test_read(self):
sin = "\x80".encode("base64_codec") sin = "\x80".encode("base64_codec")
reader = codecs.getreader("base64_codec")(StringIO(sin)) reader = codecs.getreader("base64_codec")(io.BytesIO(sin))
sout = reader.read() sout = reader.read()
self.assertEqual(sout, "\x80") self.assertEqual(sout, "\x80")
self.assert_(isinstance(sout, str)) self.assert_(isinstance(sout, str))
def test_readline(self): def test_readline(self):
sin = "\x80".encode("base64_codec") sin = "\x80".encode("base64_codec")
reader = codecs.getreader("base64_codec")(StringIO(sin)) reader = codecs.getreader("base64_codec")(io.BytesIO(sin))
sout = reader.readline() sout = reader.readline()
self.assertEqual(sout, "\x80") self.assertEqual(sout, "\x80")
self.assert_(isinstance(sout, str)) self.assert_(isinstance(sout, str))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment