FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode · python-emscripten/cpython@5a77fe9 · GitHub

Repository navigation

Commit 5a77fe9

Browse files
committed
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.
1 parent f678e82 commit 5a77fe9

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎Doc/library/stringio.rst‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ instead.
8282
those cases.
8383

8484
Unlike the :mod:`StringIO` module, this module is not able to accept Unicode
85-
strings that cannot be encoded as plain ASCII strings. Calling
86-
:func:`StringIO` with a Unicode string parameter populates the object with
87-
the buffer representation of the Unicode string instead of encoding the
88-
string.
85+
strings that cannot be encoded as plain ASCII strings.
8986

9087
Another difference from the :mod:`StringIO` module is that calling
9188
:func:`StringIO` with a string parameter creates a read-only object. Unlike an

‎Lib/test/test_StringIO.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,27 @@ def test_array_support(self):
134134
f = self.MODULE.StringIO(a)
135135
self.assertEqual(f.getvalue(), '\x00\x01\x02')
136136

137+
def test_unicode(self):
138+
139+
if not test_support.have_unicode: return
140+
141+
# The cStringIO module converts Unicode strings to character
142+
# strings when writing them to cStringIO objects.
143+
# Check that this works.
144+
145+
f = self.MODULE.StringIO()
146+
f.write(u'abcde')
147+
s = f.getvalue()
148+
self.assertEqual(s, 'abcde')
149+
self.assertEqual(type(s), str)
150+
151+
f = self.MODULE.StringIO(u'abcde')
152+
s = f.getvalue()
153+
self.assertEqual(s, 'abcde')
154+
self.assertEqual(type(s), str)
155+
156+
self.assertRaises(UnicodeEncodeError, self.MODULE.StringIO, u'\xf4')
157+
137158

138159
import sys
139160
if sys.platform.startswith('java'):

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Core and Builtins
6666
Library
6767
-------
6868

69+
- Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
70+
arguments with the system default encoding just like the write() method
71+
does, instead of converting it to a raw buffer.
72+
6973
- Issue #9168: now smtpd is able to bind privileged port.
7074

7175
- Issue #12529: fix cgi.parse_header issue on strings with double-quotes and

‎Modules/cStringIO.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,11 @@ newIobject(PyObject *s) {
661661
char *buf;
662662
Py_ssize_t size;
663663

664-
if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
664+
if (PyUnicode_Check(s)) {
665+
if (PyObject_AsCharBuffer(s, (const char **)&buf, &size) != 0)
666+
return NULL;
667+
}
668+
else if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
665669
PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
666670
s->ob_type->tp_name);
667671
return NULL;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL