Skip to content

Commit

Permalink
Bug #1548891: The cStringIO.StringIO() constructor now encodes unicode
Browse files Browse the repository at this point in the history
arguments with the system default encoding just like the write()
method does, instead of converting it to a raw buffer.


git-svn-id: http://svn.python.org/projects/python/trunk@52301 6015fed2-1504-0410-9fe1-9d1591cc4771
  • Loading branch information
georg.brandl committed Oct 12, 2006
1 parent 37b33fa commit 206a912
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
22 changes: 22 additions & 0 deletions Lib/test/test_StringIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ def test_unicode(self):
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
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions Modules/cStringIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 206a912

Please sign in to comment.