diff options
author | Marc-André Lemburg <mal@egenix.com> | 2002-01-06 17:15:05 (GMT) |
---|---|---|
committer | Marc-André Lemburg <mal@egenix.com> | 2002-01-06 17:15:05 (GMT) |
commit | f853be980ee54e8f61fa8d70a7b4724e26ac2035 (patch) | |
tree | dd0f3960724eb8991eff5fd5c9afb1a1ae6d93c2 /Lib | |
parent | 23105d5c24579ae0278ed4dcd3905565e15f34a3 (diff) | |
download | cpython-f853be980ee54e8f61fa8d70a7b4724e26ac2035.zip cpython-f853be980ee54e8f61fa8d70a7b4724e26ac2035.tar.gz cpython-f853be980ee54e8f61fa8d70a7b4724e26ac2035.tar.bz2 |
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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/StringIO.py | 13 | ||||
-rw-r--r-- | Lib/test/test_StringIO.py | 15 |
2 files changed, 23 insertions, 5 deletions
diff --git a/Lib/StringIO.py b/Lib/StringIO.py index e7c8e04..1840bad 100644 --- a/Lib/StringIO.py +++ b/Lib/StringIO.py @@ -28,7 +28,7 @@ Notes: bytes that occupy space in the buffer. - There's a simple test set (see end of this file). """ - +import types try: from errno import EINVAL except ImportError: @@ -38,8 +38,10 @@ __all__ = ["StringIO"] class StringIO: def __init__(self, buf = ''): - # Force self.buf to be a string - self.buf = str(buf) + # Force self.buf to be a string or unicode + if type(buf) is not types.UnicodeType: + buf = str(buf) + self.buf = buf self.len = len(buf) self.buflist = [] self.pos = 0 @@ -135,8 +137,9 @@ class StringIO: if self.closed: raise ValueError, "I/O operation on closed file" if not s: return - # Force s to be a string - s = str(s) + # Force s to be a string or unicode + if type(s) is not types.UnicodeType: + s = str(s) if self.pos > self.len: self.buflist.append('\0'*(self.pos - self.len)) self.len = self.pos diff --git a/Lib/test/test_StringIO.py b/Lib/test/test_StringIO.py index 8b934ff..bf3640c 100644 --- a/Lib/test/test_StringIO.py +++ b/Lib/test/test_StringIO.py @@ -71,6 +71,21 @@ class TestGenericStringIO(unittest.TestCase): class TestStringIO(TestGenericStringIO): 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): MODULE = cStringIO |