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.
 (backport from rev. 52301)
This commit is contained in:
Georg Brandl 2006-10-12 09:47:17 +00:00
parent fcf6696255
commit 3c48709e3a
3 changed files with 28 additions and 4 deletions

View File

@ -120,6 +120,28 @@ class TestStringIO(TestGenericStringIO):
class TestcStringIO(TestGenericStringIO): class TestcStringIO(TestGenericStringIO):
MODULE = cStringIO 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 import sys
if sys.platform.startswith('java'): if sys.platform.startswith('java'):
# Jython doesn't have a buffer object, so we just do a useless # Jython doesn't have a buffer object, so we just do a useless

View File

@ -49,6 +49,10 @@ Core and builtins
Extension Modules 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.
- Bug #1565150: Fix subsecond processing for os.utime on Windows. - Bug #1565150: Fix subsecond processing for os.utime on Windows.
- Patch #1572724: fix typo ('=' instead of '==') in _msi.c. - Patch #1572724: fix typo ('=' instead of '==') in _msi.c.

View File

@ -657,11 +657,9 @@ newIobject(PyObject *s) {
char *buf; char *buf;
Py_ssize_t size; Py_ssize_t size;
if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) { if (PyObject_AsCharBuffer(s, (const void **)&buf, &size) != 0)
PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
s->ob_type->tp_name);
return NULL; return NULL;
}
self = PyObject_New(Iobject, &Itype); self = PyObject_New(Iobject, &Itype);
if (!self) return NULL; if (!self) return NULL;
Py_INCREF(s); Py_INCREF(s);