mirror of https://github.com/python/cpython
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:
parent
df5f6b551a
commit
2fb5ac745b
|
@ -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)
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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__":
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue