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