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:
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
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
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