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
This commit is contained in:
Thomas Heller 2007-08-08 18:47:32 +00:00
parent df5f6b551a
commit 2fb5ac745b
7 changed files with 18 additions and 18 deletions

View File

@ -48,12 +48,12 @@ class ArrayTestCase(unittest.TestCase):
# 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)

View File

@ -7,21 +7,21 @@ class StringBufferTestCase(unittest.TestCase):
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:

View File

@ -78,8 +78,8 @@ class Callbacks(unittest.TestCase):
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

View File

@ -177,11 +177,11 @@ class NumberTestCase(unittest.TestCase):
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):

View File

@ -22,7 +22,7 @@ class ReprTest(unittest.TestCase):
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__":

View File

@ -66,7 +66,7 @@ class StringPtrTestCase(unittest.TestCase):
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]

View File

@ -1156,7 +1156,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
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