[Python-3000-checkins] r55195 - in python/branches/py3k-struni: Lib/test/test_StringIO.py Objects/bufferobject.c

guido.van.rossum python-3000-checkins at python.org
Wed May 9 01:08:37 CEST 2007


Author: guido.van.rossum
Date: Wed May  9 01:08:31 2007
New Revision: 55195

Modified:
   python/branches/py3k-struni/Lib/test/test_StringIO.py
   python/branches/py3k-struni/Objects/bufferobject.c
Log:
Make the StringIO test pass.
The buffer object now special-cases Unicode when concatenating.  Sigh.


Modified: python/branches/py3k-struni/Lib/test/test_StringIO.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_StringIO.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_StringIO.py	Wed May  9 01:08:31 2007
@@ -1,13 +1,13 @@
 # Tests StringIO and cStringIO
 
+import sys
 import unittest
 import StringIO
 import cStringIO
-import types
 from test import test_support
 
 
-class TestGenericStringIO(unittest.TestCase):
+class TestGenericStringIO:
     # use a class variable MODULE to define which module is being tested
 
     # Line of data to test as string
@@ -71,7 +71,7 @@
         self.assertEqual(f.closed, False)
         f.close()
         self.assertEqual(f.closed, True)
-        f = self.MODULE.StringIO("abc")
+        f = self.MODULE.StringIO(self.constructor("abc"))
         self.assertEqual(f.closed, False)
         f.close()
         self.assertEqual(f.closed, True)
@@ -98,7 +98,7 @@
         self._fp.close()
         self.assertRaises(ValueError, next, self._fp)
 
-class TestStringIO(TestGenericStringIO):
+class TestStringIO(TestGenericStringIO, unittest.TestCase):
     MODULE = StringIO
 
     def test_unicode(self):
@@ -116,10 +116,11 @@
         f.write(str(self._line[52]))
         s = f.getvalue()
         self.assertEqual(s, str('abcuvwxyz!'))
-        self.assertEqual(type(s), types.UnicodeType)
+        self.assertEqual(type(s), str)
 
-class TestcStringIO(TestGenericStringIO):
+class TestcStringIO(TestGenericStringIO, unittest.TestCase):
     MODULE = cStringIO
+    constructor = str8
 
     def test_unicode(self):
 
@@ -133,36 +134,39 @@
         f.write(str(self._line[:5]))
         s = f.getvalue()
         self.assertEqual(s, 'abcde')
-        self.assertEqual(type(s), types.StringType)
+        self.assertEqual(type(s), str8)
 
         f = self.MODULE.StringIO(str(self._line[:5]))
         s = f.getvalue()
         self.assertEqual(s, 'abcde')
-        self.assertEqual(type(s), types.StringType)
-
-        self.assertRaises(UnicodeEncodeError, self.MODULE.StringIO,
-                          str('\xf4', 'latin-1'))
+        self.assertEqual(type(s), str8)
 
-import sys
-if sys.platform.startswith('java'):
-    # Jython doesn't have a buffer object, so we just do a useless
-    # fake of the buffer tests.
-    buffer = str
+        # XXX This no longer fails -- the default encoding is always UTF-8.
+        ##self.assertRaises(UnicodeDecodeError, self.MODULE.StringIO, '\xf4')
 
 class TestBufferStringIO(TestStringIO):
-    constructor = buffer
+
+    def constructor(self, s):
+        return buffer(str8(s))
 
 class TestBuffercStringIO(TestcStringIO):
-    constructor = buffer
+
+    def constructor(self, s):
+        return buffer(str8(s))
 
 
 def test_main():
-    test_support.run_unittest(
+    classes = [
         TestStringIO,
         TestcStringIO,
+        ]
+    if not sys.platform.startswith('java'):
+        classes.extend([
         TestBufferStringIO,
         TestBuffercStringIO
-    )
+        ])
+    test_support.run_unittest(*classes)
+
 
 if __name__ == '__main__':
-    test_main()
+    unittest.main()

Modified: python/branches/py3k-struni/Objects/bufferobject.c
==============================================================================
--- python/branches/py3k-struni/Objects/bufferobject.c	(original)
+++ python/branches/py3k-struni/Objects/bufferobject.c	Wed May  9 01:08:31 2007
@@ -424,15 +424,24 @@
  		return NULL;
  
 	/* optimize special case */
+        /* XXX bad idea type-wise */
 	if ( size == 0 )
 	{
 	    Py_INCREF(other);
 	    return other;
 	}
 
-	if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 )
-		return NULL;
+        if (PyUnicode_Check(other)) {
+		/* XXX HACK */
+		if ( (count = (*pb->bf_getcharbuffer)(other, 0, &ptr2)) < 0 )
+			return NULL;
+	}
+	else {
+		if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 )
+			return NULL;
+	}
 
+        /* XXX Should return a bytes object, really */
  	ob = PyString_FromStringAndSize(NULL, size + count);
 	if ( ob == NULL )
 		return NULL;


More information about the Python-3000-checkins mailing list