| 1 | n/a | import unittest |
|---|
| 2 | n/a | from test import support |
|---|
| 3 | n/a | from ctypes import * |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import _ctypes_test |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | lib = CDLL(_ctypes_test.__file__) |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | class StringPtrTestCase(unittest.TestCase): |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | @support.refcount_test |
|---|
| 12 | n/a | def test__POINTER_c_char(self): |
|---|
| 13 | n/a | class X(Structure): |
|---|
| 14 | n/a | _fields_ = [("str", POINTER(c_char))] |
|---|
| 15 | n/a | x = X() |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | # NULL pointer access |
|---|
| 18 | n/a | self.assertRaises(ValueError, getattr, x.str, "contents") |
|---|
| 19 | n/a | b = c_buffer(b"Hello, World") |
|---|
| 20 | n/a | from sys import getrefcount as grc |
|---|
| 21 | n/a | self.assertEqual(grc(b), 2) |
|---|
| 22 | n/a | x.str = b |
|---|
| 23 | n/a | self.assertEqual(grc(b), 3) |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | # POINTER(c_char) and Python string is NOT compatible |
|---|
| 26 | n/a | # POINTER(c_char) and c_buffer() is compatible |
|---|
| 27 | n/a | for i in range(len(b)): |
|---|
| 28 | n/a | self.assertEqual(b[i], x.str[i]) |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | self.assertRaises(TypeError, setattr, x, "str", "Hello, World") |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | def test__c_char_p(self): |
|---|
| 33 | n/a | class X(Structure): |
|---|
| 34 | n/a | _fields_ = [("str", c_char_p)] |
|---|
| 35 | n/a | x = X() |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | # c_char_p and Python string is compatible |
|---|
| 38 | n/a | # c_char_p and c_buffer is NOT compatible |
|---|
| 39 | n/a | self.assertEqual(x.str, None) |
|---|
| 40 | n/a | x.str = b"Hello, World" |
|---|
| 41 | n/a | self.assertEqual(x.str, b"Hello, World") |
|---|
| 42 | n/a | b = c_buffer(b"Hello, World") |
|---|
| 43 | n/a | self.assertRaises(TypeError, setattr, x, b"str", b) |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | def test_functions(self): |
|---|
| 47 | n/a | strchr = lib.my_strchr |
|---|
| 48 | n/a | strchr.restype = c_char_p |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | # c_char_p and Python string is compatible |
|---|
| 51 | n/a | # c_char_p and c_buffer are now compatible |
|---|
| 52 | n/a | strchr.argtypes = c_char_p, c_char |
|---|
| 53 | n/a | self.assertEqual(strchr(b"abcdef", b"c"), b"cdef") |
|---|
| 54 | n/a | self.assertEqual(strchr(c_buffer(b"abcdef"), b"c"), b"cdef") |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | # POINTER(c_char) and Python string is NOT compatible |
|---|
| 57 | n/a | # POINTER(c_char) and c_buffer() is compatible |
|---|
| 58 | n/a | strchr.argtypes = POINTER(c_char), c_char |
|---|
| 59 | n/a | buf = c_buffer(b"abcdef") |
|---|
| 60 | n/a | self.assertEqual(strchr(buf, b"c"), b"cdef") |
|---|
| 61 | n/a | self.assertEqual(strchr(b"abcdef", b"c"), b"cdef") |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | # XXX These calls are dangerous, because the first argument |
|---|
| 64 | n/a | # to strchr is no longer valid after the function returns! |
|---|
| 65 | n/a | # So we must keep a reference to buf separately |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | strchr.restype = POINTER(c_char) |
|---|
| 68 | n/a | buf = c_buffer(b"abcdef") |
|---|
| 69 | n/a | r = strchr(buf, b"c") |
|---|
| 70 | n/a | x = r[0], r[1], r[2], r[3], r[4] |
|---|
| 71 | n/a | self.assertEqual(x, (b"c", b"d", b"e", b"f", b"\000")) |
|---|
| 72 | n/a | del buf |
|---|
| 73 | n/a | # x1 will NOT be the same as x, usually: |
|---|
| 74 | n/a | x1 = r[0], r[1], r[2], r[3], r[4] |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | if __name__ == '__main__': |
|---|
| 77 | n/a | unittest.main() |
|---|