Restore Python 2.1 StringIO.py behaviour: support concatenating
Unicode string snippets to larger Unicode strings. This fix should also go into Python 2.2.1.
This commit is contained in:
parent
23105d5c24
commit
f853be980e
@ -28,7 +28,7 @@ Notes:
|
|||||||
bytes that occupy space in the buffer.
|
bytes that occupy space in the buffer.
|
||||||
- There's a simple test set (see end of this file).
|
- There's a simple test set (see end of this file).
|
||||||
"""
|
"""
|
||||||
|
import types
|
||||||
try:
|
try:
|
||||||
from errno import EINVAL
|
from errno import EINVAL
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -38,8 +38,10 @@ __all__ = ["StringIO"]
|
|||||||
|
|
||||||
class StringIO:
|
class StringIO:
|
||||||
def __init__(self, buf = ''):
|
def __init__(self, buf = ''):
|
||||||
# Force self.buf to be a string
|
# Force self.buf to be a string or unicode
|
||||||
self.buf = str(buf)
|
if type(buf) is not types.UnicodeType:
|
||||||
|
buf = str(buf)
|
||||||
|
self.buf = buf
|
||||||
self.len = len(buf)
|
self.len = len(buf)
|
||||||
self.buflist = []
|
self.buflist = []
|
||||||
self.pos = 0
|
self.pos = 0
|
||||||
@ -135,7 +137,8 @@ class StringIO:
|
|||||||
if self.closed:
|
if self.closed:
|
||||||
raise ValueError, "I/O operation on closed file"
|
raise ValueError, "I/O operation on closed file"
|
||||||
if not s: return
|
if not s: return
|
||||||
# Force s to be a string
|
# Force s to be a string or unicode
|
||||||
|
if type(s) is not types.UnicodeType:
|
||||||
s = str(s)
|
s = str(s)
|
||||||
if self.pos > self.len:
|
if self.pos > self.len:
|
||||||
self.buflist.append('\0'*(self.pos - self.len))
|
self.buflist.append('\0'*(self.pos - self.len))
|
||||||
|
@ -71,6 +71,21 @@ class TestGenericStringIO(unittest.TestCase):
|
|||||||
class TestStringIO(TestGenericStringIO):
|
class TestStringIO(TestGenericStringIO):
|
||||||
MODULE = StringIO
|
MODULE = StringIO
|
||||||
|
|
||||||
|
def test_unicode(self):
|
||||||
|
|
||||||
|
# The StringIO module also supports concatenating Unicode
|
||||||
|
# snippets to larger Unicode strings. This is tested by this
|
||||||
|
# method. Note that cStringIO does not support this extension.
|
||||||
|
|
||||||
|
f = self.MODULE.StringIO()
|
||||||
|
f.write(self._line[:6])
|
||||||
|
f.seek(3)
|
||||||
|
f.write(unicode(self._line[20:26]))
|
||||||
|
f.write(unicode(self._line[52]))
|
||||||
|
s = f.getvalue()
|
||||||
|
self.assertEqual(s, unicode('abcuvwxyz!'))
|
||||||
|
self.assertEqual(type(s), types.UnicodeType)
|
||||||
|
|
||||||
class TestcStringIO(TestGenericStringIO):
|
class TestcStringIO(TestGenericStringIO):
|
||||||
MODULE = cStringIO
|
MODULE = cStringIO
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user