summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2006-10-12 09:47:12 (GMT)
committerGeorg Brandl <georg@python.org>2006-10-12 09:47:12 (GMT)
commit5597e261b2e46494d59bfbd7c6f2758f341ad910 (patch)
tree584016b9969d29b28266128276a63e9d4974bd2e
parentb2e81e307dc7e7d8a552619b6defddb06e028613 (diff)
downloadcpython-5597e261b2e46494d59bfbd7c6f2758f341ad910.zip
cpython-5597e261b2e46494d59bfbd7c6f2758f341ad910.tar.gz
cpython-5597e261b2e46494d59bfbd7c6f2758f341ad910.tar.bz2
Bug #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.
-rw-r--r--Lib/test/test_StringIO.py22
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/cStringIO.c6
3 files changed, 28 insertions, 4 deletions
diff --git a/Lib/test/test_StringIO.py b/Lib/test/test_StringIO.py
index cc3367f..aa36b09 100644
--- a/Lib/test/test_StringIO.py
+++ b/Lib/test/test_StringIO.py
@@ -120,6 +120,28 @@ class TestStringIO(TestGenericStringIO):
class TestcStringIO(TestGenericStringIO):
MODULE = cStringIO
+ 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(unicode(self._line[:5]))
+ s = f.getvalue()
+ self.assertEqual(s, 'abcde')
+ self.assertEqual(type(s), types.StringType)
+
+ f = self.MODULE.StringIO(unicode(self._line[:5]))
+ s = f.getvalue()
+ self.assertEqual(s, 'abcde')
+ self.assertEqual(type(s), types.StringType)
+
+ self.assertRaises(UnicodeEncodeError, self.MODULE.StringIO,
+ unicode('\xf4', 'latin-1'))
+
import sys
if sys.platform.startswith('java'):
# Jython doesn't have a buffer object, so we just do a useless
diff --git a/Misc/NEWS b/Misc/NEWS
index 4a19fda..605b1d8 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -131,6 +131,10 @@ Library
Extension Modules
-----------------
+- Bug #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.
+
- Patch #1572724: fix typo ('=' instead of '==') in _msi.c.
- Bug #1572832: fix a bug in ISO-2022 codecs which may cause segfault
diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c
index 4debb72..03ef461 100644
--- a/Modules/cStringIO.c
+++ b/Modules/cStringIO.c
@@ -657,11 +657,9 @@ newIobject(PyObject *s) {
char *buf;
Py_ssize_t size;
- if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
- PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
- s->ob_type->tp_name);
+ if (PyObject_AsCharBuffer(s, (const void **)&buf, &size) != 0)
return NULL;
- }
+
self = PyObject_New(Iobject, &Itype);
if (!self) return NULL;
Py_INCREF(s);