6
lib = CDLL(_ctypes_test.__file__)
8
class StringPtrTestCase(unittest.TestCase):
10
def test__POINTER_c_char(self):
12
_fields_ = [("str", POINTER(c_char))]
16
self.assertRaises(ValueError, getattr, x.str, "contents")
17
b = c_buffer("Hello, World")
18
from sys import getrefcount as grc
19
self.failUnlessEqual(grc(b), 2)
21
self.failUnlessEqual(grc(b), 3)
23
# POINTER(c_char) and Python string is NOT compatible
24
# POINTER(c_char) and c_buffer() is compatible
25
for i in range(len(b)):
26
self.failUnlessEqual(b[i], x.str[i])
28
self.assertRaises(TypeError, setattr, x, "str", "Hello, World")
30
def test__c_char_p(self):
32
_fields_ = [("str", c_char_p)]
35
# c_char_p and Python string is compatible
36
# c_char_p and c_buffer is NOT compatible
37
self.failUnlessEqual(x.str, None)
38
x.str = "Hello, World"
39
self.failUnlessEqual(x.str, "Hello, World")
40
b = c_buffer("Hello, World")
41
self.failUnlessRaises(TypeError, setattr, x, "str", b)
44
def test_functions(self):
45
strchr = lib.my_strchr
46
strchr.restype = c_char_p
48
# c_char_p and Python string is compatible
49
# c_char_p and c_buffer are now compatible
50
strchr.argtypes = c_char_p, c_char
51
self.failUnlessEqual(strchr("abcdef", "c"), "cdef")
52
self.failUnlessEqual(strchr(c_buffer("abcdef"), "c"), "cdef")
54
# POINTER(c_char) and Python string is NOT compatible
55
# POINTER(c_char) and c_buffer() is compatible
56
strchr.argtypes = POINTER(c_char), c_char
57
buf = c_buffer("abcdef")
58
self.failUnlessEqual(strchr(buf, "c"), "cdef")
59
self.failUnlessEqual(strchr("abcdef", "c"), "cdef")
61
# XXX These calls are dangerous, because the first argument
62
# to strchr is no longer valid after the function returns!
63
# So we must keep a reference to buf separately
65
strchr.restype = POINTER(c_char)
66
buf = c_buffer("abcdef")
68
x = r[0], r[1], r[2], r[3], r[4]
69
self.failUnlessEqual(x, (b"c", b"d", b"e", b"f", b"\000"))
71
# x1 will NOT be the same as x, usually:
72
x1 = r[0], r[1], r[2], r[3], r[4]
74
if __name__ == '__main__':