<s> '''<STR_LIT:U+0020>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import ctypes as c <EOL> import gzip <EOL> import marshal <EOL> import sys <EOL> class AnonymousStruct ( c . Structure ) : <EOL> __slots__ = ( ) <EOL> _fields_ = ( <EOL> ( '<STR_LIT:foo>' , c . c_int ) , <EOL> ) <EOL> class AnonymousUnion ( c . Union ) : <EOL> __slots__ = ( ) <EOL> _fields_ = ( <EOL> ( '<STR_LIT:foo>' , c . c_int ) , <EOL> ) <EOL> class EnumType ( type ( c . c_int ) ) : <EOL> def __new__ ( metaclass , name , bases , dict ) : <EOL> cls = type ( c . c_int ) . __new__ ( metaclass , name , bases , dict ) <EOL> return cls <EOL> class Enum ( c . c_int ) : <EOL> __metaclass__ = EnumType <EOL> _values_ = ( ) <EOL> @ classmethod <EOL> def from_param ( cls , obj ) : <EOL> assert obj in cls . _values_ <EOL> return cls ( obj ) <EOL> class FFILibrary ( object ) : <EOL> def __init__ ( self , ffi_file , lib ) : <EOL> data = gzip . GzipFile ( mode = '<STR_LIT:r>' , fileobj = ffi_file ) . read ( ) <EOL> self . lib = lib <EOL> self . map = marshal . loads ( data ) <EOL> self . type_map = { } <EOL> self . forward_map = { } <EOL> self . builtin_type_map = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : c . c_char , <EOL> '<STR_LIT>' : c . c_char , <EOL> '<STR_LIT>' : c . c_byte , <EOL> '<STR_LIT>' : c . c_wchar , <EOL> '<STR_LIT>' : c . c_short , <EOL> '<STR_LIT>' : c . c_short , <EOL> '<STR_LIT>' : c . c_ushort , <EOL> '<STR_LIT:int>' : c . c_int , <EOL> '<STR_LIT>' : c . c_int , <EOL> '<STR_LIT>' : c . c_uint , <EOL> '<STR_LIT>' : c . c_long , <EOL> '<STR_LIT>' : c . c_long , <EOL> '<STR_LIT>' : c . c_ulong , <EOL> '<STR_LIT>' : c . c_longlong , <EOL> '<STR_LIT>' : c . c_longlong , <EOL> '<STR_LIT>' : c . c_ulonglong , <EOL> '<STR_LIT:float>' : c . c_float , <EOL> '<STR_LIT>' : c . c_double , <EOL> '<STR_LIT>' : c . c_int , <EOL> } <EOL> def dump ( self , name ) : <EOL> print self . map [ name ] <EOL> def __getattr__ ( self , name ) : <EOL> kind , value = self . map [ name ] <EOL> if kind == '<STR_LIT>' : <EOL> result = value <EOL> elif kind == '<STR_LIT>' : <EOL> result = self . get_type ( value ) <EOL> elif kind == '<STR_LIT>' : <EOL> result = self . get_enum_type ( name , value ) <EOL> elif kind == '<STR_LIT>' : <EOL> result = self . get_class_type ( name , value , c . Structure ) <EOL> elif kind == '<STR_LIT>' : <EOL> result = self . get_class_type ( name , value , c . Union ) <EOL> elif kind == '<STR_LIT>' : <EOL> address = c . addressof ( getattr ( self . lib , name ) ) <EOL> result_type = self . get_type ( value ) <EOL> result = result_type . from_address ( address ) <EOL> elif kind == '<STR_LIT>' : <EOL> address = c . addressof ( getattr ( self . lib , name ) ) <EOL> result_type = self . get_type ( value ) <EOL> result = result_type . from_address ( address ) <EOL> else : <EOL> raise RuntimeError ( kind ) <EOL> setattr ( self , name , result ) <EOL> return result <EOL> def get_type ( self , parts ) : <EOL> try : <EOL> return self . type_map [ parts ] <EOL> except KeyError : <EOL> pass <EOL> result = self . get_base_type ( parts [ <NUM_LIT:0> ] ) <EOL> for part in parts [ <NUM_LIT:1> : ] : <EOL> if part == '<STR_LIT:p>' : <EOL> result = c . POINTER ( result ) <EOL> elif type ( part ) is tuple : <EOL> if part [ <NUM_LIT:0> ] == '<STR_LIT:f>' : <EOL> result = self . get_function_type ( result , part [ <NUM_LIT:1> : ] ) <EOL> elif part [ <NUM_LIT:0> ] == '<STR_LIT:a>' : <EOL> result = self . get_array_type ( result , part [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> raise RuntimeError ( part ) <EOL> else : <EOL> raise RuntimeError ( part ) <EOL> self . type_map [ parts ] = result <EOL> return result <EOL> def get_base_type ( self , name ) : <EOL> try : <EOL> return self . type_map [ name ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> return self . builtin_type_map [ name ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> return self . forward_map [ name ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> kind , value = self . map [ name ] <EOL> except KeyError : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> return AnonymousStruct <EOL> elif name . startswith ( '<STR_LIT>' ) : <EOL> return AnonymousUnion <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' % name ) <EOL> if kind == '<STR_LIT>' : <EOL> return self . get_type ( value ) <EOL> elif kind == '<STR_LIT>' : <EOL> return self . get_enum_type ( name , value ) <EOL> elif kind == '<STR_LIT>' : <EOL> return self . get_class_type ( name , value , c . Structure ) <EOL> elif kind == '<STR_LIT>' : <EOL> return self . get_class_type ( name , value , c . Union ) <EOL> else : <EOL> raise RuntimeError ( kind ) <EOL> def get_enum_type ( self , name , items ) : <EOL> class _FFIEnum ( Enum ) : <EOL> pass <EOL> _FFIEnum . __name__ = name <EOL> for key , value in items : <EOL> setattr ( _FFIEnum , key , value ) <EOL> _FFIEnum . _values_ = tuple ( [ value for key , value in items ] ) <EOL> return _FFIEnum <EOL> def get_class_type ( self , name , fields , base ) : <EOL> slots = tuple ( name for name , _ in fields ) <EOL> class _FFIStruct ( base ) : <EOL> __slots__ = slots <EOL> _FFIStruct . __name__ = name <EOL> self . forward_map [ name ] = _FFIStruct <EOL> _FFIStruct . _fields_ = tuple ( ( name , self . get_type ( type ) ) <EOL> for ( name , type ) in fields ) <EOL> del self . forward_map [ name ] <EOL> return _FFIStruct <EOL> def get_function_type ( self , restype , params ) : <EOL> params = map ( self . get_type , params ) <EOL> return c . CFUNCTYPE ( restype , * params ) <EOL> def get_array_type ( self , element_type , dimensions ) : <EOL> result = element_type <EOL> for d in dimensions : <EOL> result = element_type * d <EOL> return result <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> ffi_filename = sys . argv [ <NUM_LIT:1> ] <EOL> libname = sys . argv [ <NUM_LIT:2> ] <EOL> lib = FFILibrary ( open ( ffi_filename , '<STR_LIT:rb>' ) , c . cdll . LoadLibrary ( libname ) ) <EOL> if len ( sys . argv ) > <NUM_LIT:3> : <EOL> lib . dump ( sys . argv [ <NUM_LIT:3> ] ) <EOL> else : <EOL> for key in lib . map : <EOL> print key , getattr ( lib , key ) <EOL> try : <EOL> getattr ( lib , key ) <EOL> except AttributeError , e : <EOL> print e </s>
