[Python-3000-checkins] r56838 - in python/branches/py3k-struni: Lib/ctypes/test/test_arrays.py Lib/ctypes/test/test_buffers.py Lib/ctypes/test/test_callbacks.py Lib/ctypes/test/test_numbers.py Lib/ctypes/test/test_repr.py Lib/ctypes/test/test_stringptr.py Modules/_ctypes/cfield.c

thomas.heller python-3000-checkins at python.org
Wed Aug 8 20:47:33 CEST 2007


Author: thomas.heller
Date: Wed Aug  8 20:47:32 2007
New Revision: 56838

Modified:
   python/branches/py3k-struni/Lib/ctypes/test/test_arrays.py
   python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py
   python/branches/py3k-struni/Lib/ctypes/test/test_callbacks.py
   python/branches/py3k-struni/Lib/ctypes/test/test_numbers.py
   python/branches/py3k-struni/Lib/ctypes/test/test_repr.py
   python/branches/py3k-struni/Lib/ctypes/test/test_stringptr.py
   python/branches/py3k-struni/Modules/_ctypes/cfield.c
Log:
Fix the ctypes tests.  Patch from Victor Stinner.  He writes:

The problem is that ctypes c_char (and c_char_p) creates unicode string 
instead of byte string. I attached a proposition (patch) to change this 
behaviour (use bytes for c_char).

So in next example, it will display 'bytes' and not 'str':
  from ctypes import c_buffer, c_char
  buf = c_buffer("abcdef")
  print (type(buf[0]))

Other behaviour changes:
 - repr(c_char) adds a "b"
   eg. repr(c_char('x')) is "c_char(b'x')" instead of "c_char('x')"
 - bytes is mutable whereas str is not: 
   this may break some modules based on ctypes

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_arrays.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_arrays.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_arrays.py	Wed Aug  8 20:47:32 2007
@@ -48,12 +48,12 @@
         # CharArray("abc")
         self.assertRaises(TypeError, CharArray, "abc")
 
-        self.failUnlessEqual(ca[0], "a")
-        self.failUnlessEqual(ca[1], "b")
-        self.failUnlessEqual(ca[2], "c")
-        self.failUnlessEqual(ca[-3], "a")
-        self.failUnlessEqual(ca[-2], "b")
-        self.failUnlessEqual(ca[-1], "c")
+        self.failUnlessEqual(ca[0], b"a")
+        self.failUnlessEqual(ca[1], b"b")
+        self.failUnlessEqual(ca[2], b"c")
+        self.failUnlessEqual(ca[-3], b"a")
+        self.failUnlessEqual(ca[-2], b"b")
+        self.failUnlessEqual(ca[-1], b"c")
 
         self.failUnlessEqual(len(ca), 3)
 

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py	Wed Aug  8 20:47:32 2007
@@ -7,21 +7,21 @@
         b = create_string_buffer(32)
         self.failUnlessEqual(len(b), 32)
         self.failUnlessEqual(sizeof(b), 32 * sizeof(c_char))
-        self.failUnless(type(b[0]) is str)
+        self.failUnless(type(b[0]) is bytes)
 
         b = create_string_buffer("abc")
         self.failUnlessEqual(len(b), 4) # trailing nul char
         self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
-        self.failUnless(type(b[0]) is str)
-        self.failUnlessEqual(b[0], "a")
+        self.failUnless(type(b[0]) is bytes)
+        self.failUnlessEqual(b[0], b"a")
         self.failUnlessEqual(b[:], "abc\0")
 
     def test_string_conversion(self):
         b = create_string_buffer("abc")
         self.failUnlessEqual(len(b), 4) # trailing nul char
         self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
-        self.failUnless(type(b[0]) is str)
-        self.failUnlessEqual(b[0], "a")
+        self.failUnless(type(b[0]) is bytes)
+        self.failUnlessEqual(b[0], b"a")
         self.failUnlessEqual(b[:], "abc\0")
 
     try:

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_callbacks.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_callbacks.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_callbacks.py	Wed Aug  8 20:47:32 2007
@@ -78,8 +78,8 @@
         self.check_type(c_double, -3.14)
 
     def test_char(self):
-        self.check_type(c_char, "x")
-        self.check_type(c_char, "a")
+        self.check_type(c_char, b"x")
+        self.check_type(c_char, b"a")
 
     # disabled: would now (correctly) raise a RuntimeWarning about
     # a memory leak.  A callback function cannot return a non-integral

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_numbers.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_numbers.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_numbers.py	Wed Aug  8 20:47:32 2007
@@ -177,11 +177,11 @@
         a = array('b', [0])
         a[0] = ord('x')
         v = c_char.from_address(a.buffer_info()[0])
-        self.failUnlessEqual(v.value, 'x')
+        self.failUnlessEqual(v.value, b'x')
         self.failUnless(type(v) is c_char)
 
         a[0] = ord('?')
-        self.failUnlessEqual(v.value, '?')
+        self.failUnlessEqual(v.value, b'?')
 
     # array does not support c_bool / 't'
     # def test_bool_from_address(self):

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_repr.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_repr.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_repr.py	Wed Aug  8 20:47:32 2007
@@ -22,7 +22,7 @@
             self.failUnlessEqual("<X object at", repr(typ(42))[:12])
 
     def test_char(self):
-        self.failUnlessEqual("c_char('x')", repr(c_char('x')))
+        self.failUnlessEqual("c_char(b'x')", repr(c_char('x')))
         self.failUnlessEqual("<X object at", repr(X('x'))[:12])
 
 if __name__ == "__main__":

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_stringptr.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_stringptr.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_stringptr.py	Wed Aug  8 20:47:32 2007
@@ -66,7 +66,7 @@
         buf = c_buffer("abcdef")
         r = strchr(buf, "c")
         x = r[0], r[1], r[2], r[3], r[4]
-        self.failUnlessEqual(x, ("c", "d", "e", "f", "\000"))
+        self.failUnlessEqual(x, (b"c", b"d", b"e", b"f", b"\000"))
         del buf
         # x1 will NOT be the same as x, usually:
         x1 = r[0], r[1], r[2], r[3], r[4]

Modified: python/branches/py3k-struni/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/py3k-struni/Modules/_ctypes/cfield.c	(original)
+++ python/branches/py3k-struni/Modules/_ctypes/cfield.c	Wed Aug  8 20:47:32 2007
@@ -1156,7 +1156,7 @@
 static PyObject *
 c_get(void *ptr, Py_ssize_t size)
 {
-	return PyUnicode_FromStringAndSize((char *)ptr, 1);
+	return PyBytes_FromStringAndSize((char *)ptr, 1);
 }
 
 #ifdef CTYPES_UNICODE


More information about the Python-3000-checkins mailing list