<s> import os , unittest <EOL> from ctypes import * <EOL> try : <EOL> WINFUNCTYPE <EOL> except NameError : <EOL> WINFUNCTYPE = CFUNCTYPE <EOL> import _ctypes_test <EOL> lib = CDLL ( _ctypes_test . __file__ ) <EOL> class CFuncPtrTestCase ( unittest . TestCase ) : <EOL> def test_basic ( self ) : <EOL> X = WINFUNCTYPE ( c_int , c_int , c_int ) <EOL> def func ( * args ) : <EOL> return len ( args ) <EOL> x = X ( func ) <EOL> self . assertEqual ( x . restype , c_int ) <EOL> self . assertEqual ( x . argtypes , ( c_int , c_int ) ) <EOL> self . assertEqual ( sizeof ( x ) , sizeof ( c_voidp ) ) <EOL> self . assertEqual ( sizeof ( X ) , sizeof ( c_voidp ) ) <EOL> def test_first ( self ) : <EOL> StdCallback = WINFUNCTYPE ( c_int , c_int , c_int ) <EOL> CdeclCallback = CFUNCTYPE ( c_int , c_int , c_int ) <EOL> def func ( a , b ) : <EOL> return a + b <EOL> s = StdCallback ( func ) <EOL> c = CdeclCallback ( func ) <EOL> self . assertEqual ( s ( <NUM_LIT:1> , <NUM_LIT:2> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( c ( <NUM_LIT:1> , <NUM_LIT:2> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( c ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) , <NUM_LIT:3> ) <EOL> if not WINFUNCTYPE is CFUNCTYPE and os . name != "<STR_LIT>" : <EOL> self . assertRaises ( TypeError , s , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> def test_structures ( self ) : <EOL> WNDPROC = WINFUNCTYPE ( c_long , c_int , c_int , c_int , c_int ) <EOL> def wndproc ( hwnd , msg , wParam , lParam ) : <EOL> return hwnd + msg + wParam + lParam <EOL> HINSTANCE = c_int <EOL> HICON = c_int <EOL> HCURSOR = c_int <EOL> LPCTSTR = c_char_p <EOL> class WNDCLASS ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , c_uint ) , <EOL> ( "<STR_LIT>" , WNDPROC ) , <EOL> ( "<STR_LIT>" , c_int ) , <EOL> ( "<STR_LIT>" , c_int ) , <EOL> ( "<STR_LIT>" , HINSTANCE ) , <EOL> ( "<STR_LIT>" , HICON ) , <EOL> ( "<STR_LIT>" , HCURSOR ) , <EOL> ( "<STR_LIT>" , LPCTSTR ) , <EOL> ( "<STR_LIT>" , LPCTSTR ) ] <EOL> wndclass = WNDCLASS ( ) <EOL> wndclass . lpfnWndProc = WNDPROC ( wndproc ) <EOL> WNDPROC_2 = WINFUNCTYPE ( c_long , c_int , c_int , c_int , c_int ) <EOL> self . assertIs ( WNDPROC , WNDPROC_2 ) <EOL> self . assertEqual ( wndclass . lpfnWndProc ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) , <NUM_LIT:10> ) <EOL> f = wndclass . lpfnWndProc <EOL> del wndclass <EOL> del wndproc <EOL> self . assertEqual ( f ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT> ) , <NUM_LIT> ) <EOL> def test_dllfunctions ( self ) : <EOL> def NoNullHandle ( value ) : <EOL> if not value : <EOL> raise WinError ( ) <EOL> return value <EOL> strchr = lib . my_strchr <EOL> strchr . restype = c_char_p <EOL> strchr . argtypes = ( c_char_p , c_char ) <EOL> self . assertEqual ( strchr ( b"<STR_LIT>" , b"<STR_LIT:b>" ) , b"<STR_LIT>" ) <EOL> self . assertEqual ( strchr ( b"<STR_LIT>" , b"<STR_LIT:x>" ) , None ) <EOL> strtok = lib . my_strtok <EOL> strtok . restype = c_char_p <EOL> def c_string ( init ) : <EOL> size = len ( init ) + <NUM_LIT:1> <EOL> return ( c_char * size ) ( * init ) <EOL> s = b"<STR_LIT>" <EOL> b = c_string ( s ) <EOL> self . assertEqual ( strtok ( b , b"<STR_LIT:\n>" ) , b"<STR_LIT:a>" ) <EOL> self . assertEqual ( strtok ( None , b"<STR_LIT:\n>" ) , b"<STR_LIT:b>" ) <EOL> self . assertEqual ( strtok ( None , b"<STR_LIT:\n>" ) , b"<STR_LIT:c>" ) <EOL> self . assertEqual ( strtok ( None , b"<STR_LIT:\n>" ) , None ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
