<s> import py <EOL> from ctypes import * <EOL> from support import BaseCTypesTestChecker <EOL> ctype_types = [ c_byte , c_ubyte , c_short , c_ushort , c_int , c_uint , <EOL> c_long , c_ulong , c_longlong , c_ulonglong , c_double , c_float ] <EOL> python_types = [ int , int , int , int , int , long , <EOL> int , long , long , long , float , float ] <EOL> def setup_module ( mod ) : <EOL> import conftest <EOL> mod . _ctypes_test = str ( conftest . sofile ) <EOL> class TestPointers ( BaseCTypesTestChecker ) : <EOL> def test_pointer_crash ( self ) : <EOL> class A ( POINTER ( c_ulong ) ) : <EOL> pass <EOL> POINTER ( c_ulong ) ( c_ulong ( <NUM_LIT> ) ) <EOL> py . test . raises ( TypeError , A , c_ulong ( <NUM_LIT> ) ) <EOL> def test_pass_pointers ( self ) : <EOL> dll = CDLL ( _ctypes_test ) <EOL> func = dll . _testfunc_p_p <EOL> func . restype = c_long <EOL> i = c_int ( <NUM_LIT> ) <EOL> address = func ( byref ( i ) ) <EOL> assert c_int . from_address ( address ) . value == <NUM_LIT> <EOL> func . restype = POINTER ( c_int ) <EOL> res = func ( pointer ( i ) ) <EOL> assert res . contents . value == <NUM_LIT> <EOL> assert res [ <NUM_LIT:0> ] == <NUM_LIT> <EOL> def test_change_pointers ( self ) : <EOL> dll = CDLL ( _ctypes_test ) <EOL> func = dll . _testfunc_p_p <EOL> i = c_int ( <NUM_LIT> ) <EOL> func . restype = POINTER ( c_int ) <EOL> func . argtypes = ( POINTER ( c_int ) , ) <EOL> res = func ( pointer ( i ) ) <EOL> assert res [ <NUM_LIT:0> ] == <NUM_LIT> <EOL> assert res . contents . value == <NUM_LIT> <EOL> res [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> assert i . value == <NUM_LIT> <EOL> res . contents = c_int ( <NUM_LIT> ) <EOL> assert i . value == <NUM_LIT> <EOL> def test_callbacks_with_pointers ( self ) : <EOL> PROTOTYPE = CFUNCTYPE ( c_int , POINTER ( c_int ) ) <EOL> self . result = [ ] <EOL> def func ( arg ) : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> self . result . append ( arg [ i ] ) <EOL> return <NUM_LIT:0> <EOL> callback = PROTOTYPE ( func ) <EOL> dll = CDLL ( _ctypes_test ) <EOL> doit = dll . _testfunc_callback_with_pointer <EOL> doit ( callback ) <EOL> doit ( callback ) <EOL> def test_basics ( self ) : <EOL> from operator import delitem <EOL> for ct , pt in zip ( ctype_types , python_types ) : <EOL> i = ct ( <NUM_LIT> ) <EOL> p = pointer ( i ) <EOL> assert type ( p . contents ) is ct <EOL> py . test . raises ( TypeError , delitem , p , <NUM_LIT:0> ) <EOL> def test_pointer_to_pointer ( self ) : <EOL> x = c_int ( <NUM_LIT:32> ) <EOL> y = c_int ( <NUM_LIT> ) <EOL> p1 = pointer ( x ) <EOL> p2 = pointer ( p1 ) <EOL> assert p2 . contents . contents . value == <NUM_LIT:32> <EOL> p2 . contents . contents = y <EOL> assert p2 . contents . contents . value == <NUM_LIT> <EOL> assert p1 . contents . value == <NUM_LIT> <EOL> def test_from_address ( self ) : <EOL> from array import array <EOL> a = array ( '<STR_LIT:i>' , [ <NUM_LIT:100> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> addr = a . buffer_info ( ) [ <NUM_LIT:0> ] <EOL> p = POINTER ( POINTER ( c_int ) ) <EOL> def test_other ( self ) : <EOL> class Table ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:a>" , c_int ) , <EOL> ( "<STR_LIT:b>" , c_int ) , <EOL> ( "<STR_LIT:c>" , c_int ) ] <EOL> pt = pointer ( Table ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> assert pt . contents . a == <NUM_LIT:1> <EOL> assert pt . contents . b == <NUM_LIT:2> <EOL> assert pt . contents . c == <NUM_LIT:3> <EOL> pt . contents . c = <NUM_LIT> <EOL> from ctypes import _pointer_type_cache <EOL> del _pointer_type_cache [ Table ] <EOL> def test_basic ( self ) : <EOL> p = pointer ( c_int ( <NUM_LIT> ) ) <EOL> py . test . raises ( TypeError , len , p ) <EOL> assert p [ <NUM_LIT:0> ] == <NUM_LIT> <EOL> assert p . contents . value == <NUM_LIT> <EOL> def test_charpp ( self ) : <EOL> """<STR_LIT>""" <EOL> dll = CDLL ( _ctypes_test ) <EOL> func = dll . _testfunc_c_p_p <EOL> func . restype = c_char_p <EOL> argv = ( c_char_p * <NUM_LIT:2> ) ( ) <EOL> argc = c_int ( <NUM_LIT:2> ) <EOL> argv [ <NUM_LIT:0> ] = '<STR_LIT:hello>' <EOL> assert argv [ <NUM_LIT:0> ] == '<STR_LIT:hello>' <EOL> argv [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> result = func ( byref ( argc ) , argv ) <EOL> assert result == '<STR_LIT>' , result <EOL> def test_charpp2 ( self ) : <EOL> """<STR_LIT>""" <EOL> dll = CDLL ( _ctypes_test ) <EOL> func = dll . _testfunc_c_p_p <EOL> func . restype = c_char_p <EOL> argv = ( c_char_p * <NUM_LIT:2> ) ( ) <EOL> argc = c_int ( <NUM_LIT:2> ) <EOL> argv [ <NUM_LIT:0> ] = '<STR_LIT:hello>' <EOL> argv [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> result = func ( byref ( argc ) , byref ( argv ) ) <EOL> assert result == '<STR_LIT>' , result <EOL> def test_charpp3 ( self ) : <EOL> """<STR_LIT>""" <EOL> dll = CDLL ( _ctypes_test ) <EOL> func = dll . _testfunc_c_p_p <EOL> func . argtypes = ( POINTER ( c_int ) , c_char_p * <NUM_LIT:2> ) <EOL> func . restype = c_char_p <EOL> argv = ( c_char_p * <NUM_LIT:2> ) ( ) <EOL> argc = c_int ( <NUM_LIT:2> ) <EOL> argv [ <NUM_LIT:0> ] = '<STR_LIT:hello>' <EOL> argv [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> result = func ( byref ( argc ) , argv ) <EOL> assert result == '<STR_LIT>' , result <EOL> def test_charpp4 ( self ) : <EOL> """<STR_LIT>""" <EOL> dll = CDLL ( _ctypes_test ) <EOL> func = dll . _testfunc_c_p_p <EOL> func . argtypes = ( POINTER ( c_int ) , POINTER ( c_char_p * <NUM_LIT:2> ) ) <EOL> func . restype = c_char_p <EOL> argv = ( c_char_p * <NUM_LIT:2> ) ( ) <EOL> argc = c_int ( <NUM_LIT:2> ) <EOL> argv [ <NUM_LIT:0> ] = '<STR_LIT:hello>' <EOL> argv [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> result = func ( byref ( argc ) , argv ) <EOL> assert result == '<STR_LIT>' , result <EOL> def test_charpp5 ( self ) : <EOL> """<STR_LIT>""" <EOL> dll = CDLL ( _ctypes_test ) <EOL> func = dll . _testfunc_c_p_p <EOL> func . argtypes = ( POINTER ( c_int ) , POINTER ( c_char_p * <NUM_LIT:2> ) ) <EOL> func . restype = c_char_p <EOL> argv = ( c_char_p * <NUM_LIT:2> ) ( ) <EOL> argc = c_int ( <NUM_LIT:2> ) <EOL> argv [ <NUM_LIT:0> ] = '<STR_LIT:hello>' <EOL> argv [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> result = func ( byref ( argc ) , byref ( argv ) ) <EOL> assert result == '<STR_LIT>' , result <EOL> def test_bug_1467852 ( self ) : <EOL> x = c_int ( <NUM_LIT:5> ) <EOL> dummy = [ ] <EOL> for i in range ( <NUM_LIT> ) : <EOL> dummy . append ( c_int ( i ) ) <EOL> y = c_int ( <NUM_LIT:6> ) <EOL> p = pointer ( x ) <EOL> pp = pointer ( p ) <EOL> q = pointer ( y ) <EOL> pp [ <NUM_LIT:0> ] = q <EOL> assert p [ <NUM_LIT:0> ] == <NUM_LIT:6> <EOL> def test_c_void_p ( self ) : <EOL> if sizeof ( c_void_p ) == <NUM_LIT:4> : <EOL> assert c_void_p ( <NUM_LIT> L ) . value == c_void_p ( - <NUM_LIT:1> ) . value <EOL> assert c_void_p ( <NUM_LIT> L ) . value == c_void_p ( - <NUM_LIT:1> ) . value <EOL> elif sizeof ( c_void_p ) == <NUM_LIT:8> : <EOL> assert c_void_p ( <NUM_LIT> L ) . value == <NUM_LIT> L <EOL> assert c_void_p ( <NUM_LIT> L ) . value == c_void_p ( - <NUM_LIT:1> ) . value <EOL> assert c_void_p ( <NUM_LIT> L ) . value == c_void_p ( - <NUM_LIT:1> ) . value <EOL> py . test . raises ( TypeError , c_void_p , <NUM_LIT> ) <EOL> py . test . raises ( TypeError , c_void_p , object ( ) ) <EOL> def test_c_char_p_byref ( self ) : <EOL> dll = CDLL ( _ctypes_test ) <EOL> TwoOutArgs = dll . TwoOutArgs <EOL> TwoOutArgs . restype = None <EOL> TwoOutArgs . argtypes = [ c_int , c_void_p , c_int , c_void_p ] <EOL> a = c_int ( <NUM_LIT:3> ) <EOL> b = c_int ( <NUM_LIT:4> ) <EOL> c = c_int ( <NUM_LIT:5> ) <EOL> d = c_int ( <NUM_LIT:6> ) <EOL> TwoOutArgs ( a , byref ( b ) , c , byref ( d ) ) <EOL> assert b . value == <NUM_LIT:7> <EOL> assert d . value == <NUM_LIT:11> </s>
