summaryrefslogtreecommitdiffstats
path: root/Lib/io.py
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2007-05-16 12:47:53 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2007-05-16 12:47:53 (GMT)
commit9d2ac227210fa8c7ba14a581747d1a1836e7274c (patch)
tree59c65cffa19f4429d84b2a2f929447e1a1086c68 /Lib/io.py
parentc9e363c2eb3eed920bcd0d08a2eabdfa939b1270 (diff)
downloadcpython-9d2ac227210fa8c7ba14a581747d1a1836e7274c.zip
cpython-9d2ac227210fa8c7ba14a581747d1a1836e7274c.tar.gz
cpython-9d2ac227210fa8c7ba14a581747d1a1836e7274c.tar.bz2
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.
Diffstat (limited to 'Lib/io.py')
-rw-r--r--Lib/io.py45
1 files changed, 30 insertions, 15 deletions
diff --git a/Lib/io.py b/Lib/io.py
index fdf1299..177526f 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -581,10 +581,10 @@ class BytesIO(_MemoryIOMixin):
# XXX More docs
- def __init__(self, inital_bytes=None):
+ def __init__(self, initial_bytes=None):
buffer = b""
- if inital_bytes is not None:
- buffer += inital_bytes
+ if initial_bytes is not None:
+ buffer += initial_bytes
_MemoryIOMixin.__init__(self, buffer)
@@ -595,21 +595,36 @@ class StringIO(_MemoryIOMixin):
# XXX More docs
- # Reuses the same code as BytesIO, just with a string rather that
- # bytes as the _buffer value.
+ # Reuses the same code as BytesIO, but encode strings on the way in
+ # and decode them on the way out.
- # XXX This doesn't work; _MemoryIOMixin's write() and truncate()
- # methods assume the buffer is mutable. Simply redefining those
- # to use slice concatenation will make it awfully slow (in fact,
- # quadratic in the number of write() calls). Also, there are no
- # readline() and readlines() methods. Etc., etc.
-
- def __init__(self, inital_string=None):
- buffer = ""
- if inital_string is not None:
- buffer += inital_string
+ def __init__(self, initial_string=None):
+ if initial_string is not None:
+ buffer = initial_string.encode("unicode-internal")
+ else:
+ buffer = b""
_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:
self._unsupported("readinto")