diff options
author | Thomas Heller <theller@ctypes.org> | 2007-08-08 18:47:32 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2007-08-08 18:47:32 (GMT) |
commit | 2fb5ac745bf2edd03261ca8144eb73a1f6e54af3 (patch) | |
tree | 411516f988845a5cc4a25c97262dcdab7f1171e4 | |
parent | df5f6b551ad89257a579682114ae9f2ab1d3755e (diff) | |
download | cpython-2fb5ac745bf2edd03261ca8144eb73a1f6e54af3.zip cpython-2fb5ac745bf2edd03261ca8144eb73a1f6e54af3.tar.gz cpython-2fb5ac745bf2edd03261ca8144eb73a1f6e54af3.tar.bz2 |
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
-rw-r--r-- | Lib/ctypes/test/test_arrays.py | 12 | ||||
-rw-r--r-- | Lib/ctypes/test/test_buffers.py | 10 | ||||
-rw-r--r-- | Lib/ctypes/test/test_callbacks.py | 4 | ||||
-rw-r--r-- | Lib/ctypes/test/test_numbers.py | 4 | ||||
-rw-r--r-- | Lib/ctypes/test/test_repr.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/test/test_stringptr.py | 2 | ||||
-rw-r--r-- | Modules/_ctypes/cfield.c | 2 |
7 files changed, 18 insertions, 18 deletions
diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py index e94e9bd..e01ccfd 100644 --- a/Lib/ctypes/test/test_arrays.py +++ b/Lib/ctypes/test/test_arrays.py @@ -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) diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py index 72cd0dd..35c0510 100644 --- a/Lib/ctypes/test/test_buffers.py +++ b/Lib/ctypes/test/test_buffers.py @@ -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: diff --git a/Lib/ctypes/test/test_callbacks.py b/Lib/ctypes/test/test_callbacks.py index f47fc37..9b2ea8c 100644 --- a/Lib/ctypes/test/test_callbacks.py +++ b/Lib/ctypes/test/test_callbacks.py @@ -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 diff --git a/Lib/ctypes/test/test_numbers.py b/Lib/ctypes/test/test_numbers.py index fda3072..680d29d 100644 --- a/Lib/ctypes/test/test_numbers.py +++ b/Lib/ctypes/test/test_numbers.py @@ -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): diff --git a/Lib/ctypes/test/test_repr.py b/Lib/ctypes/test/test_repr.py index f6f9366..8e69444 100644 --- a/Lib/ctypes/test/test_repr.py +++ b/Lib/ctypes/test/test_repr.py @@ -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__": diff --git a/Lib/ctypes/test/test_stringptr.py b/Lib/ctypes/test/test_stringptr.py index 6ee6ae0..a5a471c 100644 --- a/Lib/ctypes/test/test_stringptr.py +++ b/Lib/ctypes/test/test_stringptr.py @@ -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] diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index cc94850..f60dede 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -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 |