PageRenderTime 384ms CodeModel.GetById 211ms app.highlight 5ms RepoModel.GetById 166ms app.codeStats 0ms

/Lib/ctypes/test/test_returnfuncptrs.py

http://unladen-swallow.googlecode.com/
Python | 35 lines | 25 code | 5 blank | 5 comment | 1 complexity | 597b7359290e78f360311c0cc57c73f5 MD5 | raw file
 1import unittest
 2from ctypes import *
 3
 4import _ctypes_test
 5
 6class ReturnFuncPtrTestCase(unittest.TestCase):
 7
 8    def test_with_prototype(self):
 9        # The _ctypes_test shared lib/dll exports quite some functions for testing.
10        # The get_strchr function returns a *pointer* to the C strchr function.
11        dll = CDLL(_ctypes_test.__file__)
12        get_strchr = dll.get_strchr
13        get_strchr.restype = CFUNCTYPE(c_char_p, c_char_p, c_char)
14        strchr = get_strchr()
15        self.failUnlessEqual(strchr("abcdef", "b"), "bcdef")
16        self.failUnlessEqual(strchr("abcdef", "x"), None)
17        self.assertRaises(ArgumentError, strchr, "abcdef", 3)
18        self.assertRaises(TypeError, strchr, "abcdef")
19
20    def test_without_prototype(self):
21        dll = CDLL(_ctypes_test.__file__)
22        get_strchr = dll.get_strchr
23        # the default 'c_int' would not work on systems where sizeof(int) != sizeof(void *)
24        get_strchr.restype = c_void_p
25        addr = get_strchr()
26        # _CFuncPtr instances are now callable with an integer argument
27        # which denotes a function address:
28        strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(addr)
29        self.failUnless(strchr("abcdef", "b"), "bcdef")
30        self.failUnlessEqual(strchr("abcdef", "x"), None)
31        self.assertRaises(ArgumentError, strchr, "abcdef", 3)
32        self.assertRaises(TypeError, strchr, "abcdef")
33
34if __name__ == "__main__":
35    unittest.main()