diff Lib/test/test_StringIO.py @ 73054:27ae7d4e1983 2.7

Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode arguments with the system default encoding just like the write() method does, instead of converting it to a raw buffer.
author Antoine Pitrou <solipsis@pitrou.net>
date Sat, 22 Oct 2011 21:26:01 +0200
parents d30482d51c25
children a025b04332fe
line wrap: on
line diff
--- a/Lib/test/test_StringIO.py	Fri Oct 21 22:22:43 2011 +0200
+++ b/Lib/test/test_StringIO.py	Sat Oct 22 21:26:01 2011 +0200
@@ -134,6 +134,27 @@
         f = self.MODULE.StringIO(a)
         self.assertEqual(f.getvalue(), '\x00\x01\x02')
 
+    def test_unicode(self):
+
+        if not test_support.have_unicode: return
+
+        # The cStringIO module converts Unicode strings to character
+        # strings when writing them to cStringIO objects.
+        # Check that this works.
+
+        f = self.MODULE.StringIO()
+        f.write(u'abcde')
+        s = f.getvalue()
+        self.assertEqual(s, 'abcde')
+        self.assertEqual(type(s), str)
+
+        f = self.MODULE.StringIO(u'abcde')
+        s = f.getvalue()
+        self.assertEqual(s, 'abcde')
+        self.assertEqual(type(s), str)
+
+        self.assertRaises(UnicodeEncodeError, self.MODULE.StringIO, u'\xf4')
+
 
 import sys
 if sys.platform.startswith('java'):