]> granicus.if.org Git - python/commitdiff
Revert the fix for #1548891, it broke backwards compatibility with arbitrary read...
authorGeorg Brandl <georg@python.org>
Wed, 8 Aug 2007 13:03:45 +0000 (13:03 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 8 Aug 2007 13:03:45 +0000 (13:03 +0000)
Fixes #1730114.
 (backport from rev. 56830)

Doc/lib/libstringio.tex
Lib/test/test_StringIO.py
Misc/NEWS
Modules/cStringIO.c

index 24312518f37279d38c235798798c1d61a69ef3d4..73ff0e4c3c27e5778fc229fff6c63101d1a85d7f 100644 (file)
@@ -78,6 +78,10 @@ Unlike the memory files implemented by the \refmodule{StringIO}
 module, those provided by this module are not able to accept Unicode
 strings that cannot be encoded as plain \ASCII{} strings.
 
+Calling \function{StringIO()} with a Unicode string parameter populates
+the object with the buffer representation of the Unicode string, instead of
+encoding the string.
+
 Another difference from the \refmodule{StringIO} module is that calling
 \function{StringIO()} with a string parameter creates a read-only object.
 Unlike an object created without a string parameter, it does not have
index aa36b098443c336dd2b7e777ff82f81b4fd4e088..cc3367fed8d16bdfa9a4b0c68c8b0832576fbf32 100644 (file)
@@ -120,28 +120,6 @@ 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
index 3d5221cd43ab6193ab3b52ed3437ac6a6c1ffa2c..0e2985c9cb784181aae79b4d8b3aa324996527ec 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,9 @@ Core and builtins
 Library
 -------
 
+- Reverted the fix for bug #1548891 because it broke compatibility with
+  arbitrary read buffers. Added a note in the documentation.
+
 - GB18030 codec now can encode additional two-byte characters that
   are missing in GBK.
 
index 06bc6cbae36682ec1d08f97d799a8aded282812b..2d8a6abd76bcd49420aa035a225140b570adc129 100644 (file)
@@ -665,8 +665,11 @@ newIobject(PyObject *s) {
   char *buf;
   Py_ssize_t size;
 
-  if (PyObject_AsCharBuffer(s, (const char **)&buf, &size) != 0)
-      return NULL;
+  if (PyObject_AsReadBuffer(s, (const char **)&buf, &size)) {
+    PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
+                 s->ob_type->tp_name);
+    return NULL;
+  }
 
   self = PyObject_New(Iobject, &Itype);
   if (!self) return NULL;