Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_funcptr.CFuncPtrTestCase Class Reference

List of all members.

Public Member Functions

def test_basic
def test_first
def test_structures
def test_dllfunctions

Detailed Description

Definition at line 13 of file test_funcptr.py.


Member Function Documentation

Definition at line 14 of file test_funcptr.py.

00014 
00015     def test_basic(self):
00016         X = WINFUNCTYPE(c_int, c_int, c_int)
00017 
00018         def func(*args):
00019             return len(args)
00020 
00021         x = X(func)
00022         self.assertEqual(x.restype, c_int)
00023         self.assertEqual(x.argtypes, (c_int, c_int))
00024         self.assertEqual(sizeof(x), sizeof(c_voidp))
00025         self.assertEqual(sizeof(X), sizeof(c_voidp))

Here is the call graph for this function:

Definition at line 90 of file test_funcptr.py.

00090 
00091     def test_dllfunctions(self):
00092 
00093         def NoNullHandle(value):
00094             if not value:
00095                 raise WinError()
00096             return value
00097 
00098         strchr = lib.my_strchr
00099         strchr.restype = c_char_p
00100         strchr.argtypes = (c_char_p, c_char)
00101         self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi")
00102         self.assertEqual(strchr(b"abcdefghi", b"x"), None)
00103 
00104 
00105         strtok = lib.my_strtok
00106         strtok.restype = c_char_p
        # Neither of this does work: strtok changes the buffer it is passed

Here is the call graph for this function:

Definition at line 26 of file test_funcptr.py.

00026 
00027     def test_first(self):
00028         StdCallback = WINFUNCTYPE(c_int, c_int, c_int)
00029         CdeclCallback = CFUNCTYPE(c_int, c_int, c_int)
00030 
00031         def func(a, b):
00032             return a + b
00033 
00034         s = StdCallback(func)
00035         c = CdeclCallback(func)
00036 
00037         self.assertEqual(s(1, 2), 3)
00038         self.assertEqual(c(1, 2), 3)
00039         # The following no longer raises a TypeError - it is now
00040         # possible, as in C, to call cdecl functions with more parameters.
00041         #self.assertRaises(TypeError, c, 1, 2, 3)
00042         self.assertEqual(c(1, 2, 3, 4, 5, 6), 3)
00043         if not WINFUNCTYPE is CFUNCTYPE and os.name != "ce":
00044             self.assertRaises(TypeError, s, 1, 2, 3)

Here is the call graph for this function:

Definition at line 45 of file test_funcptr.py.

00045 
00046     def test_structures(self):
00047         WNDPROC = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int)
00048 
00049         def wndproc(hwnd, msg, wParam, lParam):
00050             return hwnd + msg + wParam + lParam
00051 
00052         HINSTANCE = c_int
00053         HICON = c_int
00054         HCURSOR = c_int
00055         LPCTSTR = c_char_p
00056 
00057         class WNDCLASS(Structure):
00058             _fields_ = [("style", c_uint),
00059                         ("lpfnWndProc", WNDPROC),
00060                         ("cbClsExtra", c_int),
00061                         ("cbWndExtra", c_int),
00062                         ("hInstance", HINSTANCE),
00063                         ("hIcon", HICON),
00064                         ("hCursor", HCURSOR),
00065                         ("lpszMenuName", LPCTSTR),
00066                         ("lpszClassName", LPCTSTR)]
00067 
00068         wndclass = WNDCLASS()
00069         wndclass.lpfnWndProc = WNDPROC(wndproc)
00070 
00071         WNDPROC_2 = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int)
00072 
00073         # This is no longer true, now that WINFUNCTYPE caches created types internally.
00074         ## # CFuncPtr subclasses are compared by identity, so this raises a TypeError:
00075         ## self.assertRaises(TypeError, setattr, wndclass,
00076         ##                  "lpfnWndProc", WNDPROC_2(wndproc))
00077         # instead:
00078 
00079         self.assertTrue(WNDPROC is WNDPROC_2)
00080         # 'wndclass.lpfnWndProc' leaks 94 references.  Why?
00081         self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10)
00082 
00083 
00084         f = wndclass.lpfnWndProc
00085 
00086         del wndclass
00087         del wndproc
00088 
00089         self.assertEqual(f(10, 11, 12, 13), 46)

Here is the call graph for this function:


The documentation for this class was generated from the following file: