| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,10 +82,7 @@ instead. | |||
| 82 | 82 | those cases. | |
| 83 | 83 | ||
| 84 | 84 | 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. | ||
| 89 | 86 | ||
| 90 | 87 | Another difference from the :mod:`StringIO` module is that calling | |
| 91 | 88 | :func:`StringIO` with a string parameter creates a read-only object. Unlike an | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,6 +134,27 @@ def test_array_support(self): | |||
| 134 | 134 | f = self.MODULE.StringIO(a) | |
| 135 | 135 | self.assertEqual(f.getvalue(), '\x00\x01\x02') | |
| 136 | 136 | ||
| 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 | + | ||
| 137 | 158 | ||
| 138 | 159 | import sys | |
| 139 | 160 | if sys.platform.startswith('java'): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -66,6 +66,10 @@ Core and Builtins | |||
| 66 | 66 | Library | |
| 67 | 67 | ------- | |
| 68 | 68 | ||
| 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 | + | ||
| 69 | 73 | - Issue #9168: now smtpd is able to bind privileged port. | |
| 70 | 74 | ||
| 71 | 75 | - Issue #12529: fix cgi.parse_header issue on strings with double-quotes and | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -661,7 +661,11 @@ newIobject(PyObject *s) { | |||
| 661 | 661 | char *buf; | |
| 662 | 662 | Py_ssize_t size; | |
| 663 | 663 | ||
| 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)) { | ||
| 665 | 669 | PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found", | |
| 666 | 670 | s->ob_type->tp_name); | |
| 667 | 671 | return NULL; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments