<s> import ctypes , ctypes . util , operator , sys <EOL> from . import model <EOL> if sys . version_info < ( <NUM_LIT:3> , ) : <EOL> bytechr = chr <EOL> else : <EOL> unicode = str <EOL> long = int <EOL> xrange = range <EOL> bytechr = lambda num : bytes ( [ num ] ) <EOL> class CTypesType ( type ) : <EOL> pass <EOL> class CTypesData ( object ) : <EOL> __metaclass__ = CTypesType <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> __name__ = '<STR_LIT>' <EOL> def __init__ ( self , * args ) : <EOL> raise TypeError ( "<STR_LIT>" % ( self . __class__ , ) ) <EOL> @ classmethod <EOL> def _newp ( cls , init ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % ( cls . _get_c_name ( ) , ) ) <EOL> @ staticmethod <EOL> def _to_ctypes ( value ) : <EOL> raise TypeError <EOL> @ classmethod <EOL> def _arg_to_ctypes ( cls , * value ) : <EOL> try : <EOL> ctype = cls . _ctype <EOL> except AttributeError : <EOL> raise TypeError ( "<STR_LIT>" % ( cls , ) ) <EOL> if value : <EOL> res = cls . _to_ctypes ( * value ) <EOL> if not isinstance ( res , ctype ) : <EOL> res = cls . _ctype ( res ) <EOL> else : <EOL> res = cls . _ctype ( ) <EOL> return res <EOL> @ classmethod <EOL> def _create_ctype_obj ( cls , init ) : <EOL> if init is None : <EOL> return cls . _arg_to_ctypes ( ) <EOL> else : <EOL> return cls . _arg_to_ctypes ( init ) <EOL> @ staticmethod <EOL> def _from_ctypes ( ctypes_value ) : <EOL> raise TypeError <EOL> @ classmethod <EOL> def _get_c_name ( cls , replace_with = '<STR_LIT>' ) : <EOL> return cls . _reftypename . replace ( '<STR_LIT>' , replace_with ) <EOL> @ classmethod <EOL> def _fix_class ( cls ) : <EOL> cls . __name__ = '<STR_LIT>' % ( cls . _get_c_name ( ) , ) <EOL> cls . __qualname__ = '<STR_LIT>' % ( cls . _get_c_name ( ) , ) <EOL> cls . __module__ = '<STR_LIT>' <EOL> def _get_own_repr ( self ) : <EOL> raise NotImplementedError <EOL> def _addr_repr ( self , address ) : <EOL> if address == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> if address < <NUM_LIT:0> : <EOL> address += <NUM_LIT:1> << ( <NUM_LIT:8> * ctypes . sizeof ( ctypes . c_void_p ) ) <EOL> return '<STR_LIT>' % address <EOL> def __repr__ ( self , c_name = None ) : <EOL> own = self . _get_own_repr ( ) <EOL> return '<STR_LIT>' % ( c_name or self . _get_c_name ( ) , own ) <EOL> def _convert_to_address ( self , BClass ) : <EOL> if BClass is None : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> self . _get_c_name ( ) , ) ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> self . _get_c_name ( ) , BClass . _get_c_name ( ) ) ) <EOL> @ classmethod <EOL> def _get_size ( cls ) : <EOL> return ctypes . sizeof ( cls . _ctype ) <EOL> def _get_size_of_instance ( self ) : <EOL> return ctypes . sizeof ( self . _ctype ) <EOL> @ classmethod <EOL> def _cast_from ( cls , source ) : <EOL> raise TypeError ( "<STR_LIT>" % ( cls . _get_c_name ( ) , ) ) <EOL> def _cast_to_integer ( self ) : <EOL> return self . _convert_to_address ( None ) <EOL> @ classmethod <EOL> def _alignment ( cls ) : <EOL> return ctypes . alignment ( cls . _ctype ) <EOL> def __iter__ ( self ) : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> self . _get_c_name ( ) ) , ) <EOL> def _make_cmp ( name ) : <EOL> cmpfunc = getattr ( operator , name ) <EOL> def cmp ( self , other ) : <EOL> if isinstance ( other , CTypesData ) : <EOL> return cmpfunc ( self . _convert_to_address ( None ) , <EOL> other . _convert_to_address ( None ) ) <EOL> else : <EOL> return NotImplemented <EOL> cmp . func_name = name <EOL> return cmp <EOL> __eq__ = _make_cmp ( '<STR_LIT>' ) <EOL> __ne__ = _make_cmp ( '<STR_LIT>' ) <EOL> __lt__ = _make_cmp ( '<STR_LIT>' ) <EOL> __le__ = _make_cmp ( '<STR_LIT>' ) <EOL> __gt__ = _make_cmp ( '<STR_LIT>' ) <EOL> __ge__ = _make_cmp ( '<STR_LIT>' ) <EOL> def __hash__ ( self ) : <EOL> return hash ( type ( self ) ) ^ hash ( self . _convert_to_address ( None ) ) <EOL> def _to_string ( self , maxlen ) : <EOL> raise TypeError ( "<STR_LIT>" % ( self , ) ) <EOL> class CTypesGenericPrimitive ( CTypesData ) : <EOL> __slots__ = [ ] <EOL> def __eq__ ( self , other ) : <EOL> return self is other <EOL> def __ne__ ( self , other ) : <EOL> return self is not other <EOL> def __hash__ ( self ) : <EOL> return object . __hash__ ( self ) <EOL> def _get_own_repr ( self ) : <EOL> return repr ( self . _from_ctypes ( self . _value ) ) <EOL> class CTypesGenericArray ( CTypesData ) : <EOL> __slots__ = [ ] <EOL> @ classmethod <EOL> def _newp ( cls , init ) : <EOL> return cls ( init ) <EOL> def __iter__ ( self ) : <EOL> for i in xrange ( len ( self ) ) : <EOL> yield self [ i ] <EOL> def _get_own_repr ( self ) : <EOL> return self . _addr_repr ( ctypes . addressof ( self . _blob ) ) <EOL> class CTypesGenericPtr ( CTypesData ) : <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> _automatic_casts = False <EOL> kind = "<STR_LIT>" <EOL> @ classmethod <EOL> def _newp ( cls , init ) : <EOL> return cls ( init ) <EOL> @ classmethod <EOL> def _cast_from ( cls , source ) : <EOL> if source is None : <EOL> address = <NUM_LIT:0> <EOL> elif isinstance ( source , CTypesData ) : <EOL> address = source . _cast_to_integer ( ) <EOL> elif isinstance ( source , ( int , long ) ) : <EOL> address = source <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( cls , type ( source ) . __name__ ) ) <EOL> return cls . _new_pointer_at ( address ) <EOL> @ classmethod <EOL> def _new_pointer_at ( cls , address ) : <EOL> self = cls . __new__ ( cls ) <EOL> self . _address = address <EOL> self . _as_ctype_ptr = ctypes . cast ( address , cls . _ctype ) <EOL> return self <EOL> def _get_own_repr ( self ) : <EOL> try : <EOL> return self . _addr_repr ( self . _address ) <EOL> except AttributeError : <EOL> return '<STR_LIT>' <EOL> def _cast_to_integer ( self ) : <EOL> return self . _address <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _address ) <EOL> def __bool__ ( self ) : <EOL> return bool ( self . _address ) <EOL> @ classmethod <EOL> def _to_ctypes ( cls , value ) : <EOL> if not isinstance ( value , CTypesData ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( value ) . __name__ ) <EOL> address = value . _convert_to_address ( cls ) <EOL> return ctypes . cast ( address , cls . _ctype ) <EOL> @ classmethod <EOL> def _from_ctypes ( cls , ctypes_ptr ) : <EOL> address = ctypes . cast ( ctypes_ptr , ctypes . c_void_p ) . value or <NUM_LIT:0> <EOL> return cls . _new_pointer_at ( address ) <EOL> @ classmethod <EOL> def _initialize ( cls , ctypes_ptr , value ) : <EOL> if value : <EOL> ctypes_ptr . contents = cls . _to_ctypes ( value ) . contents <EOL> def _convert_to_address ( self , BClass ) : <EOL> if ( BClass in ( self . __class__ , None ) or BClass . _automatic_casts <EOL> or self . _automatic_casts ) : <EOL> return self . _address <EOL> else : <EOL> return CTypesData . _convert_to_address ( self , BClass ) <EOL> class CTypesBaseStructOrUnion ( CTypesData ) : <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> @ classmethod <EOL> def _create_ctype_obj ( cls , init ) : <EOL> raise TypeError ( "<STR_LIT>" % ( cls , ) ) <EOL> def _get_own_repr ( self ) : <EOL> return self . _addr_repr ( ctypes . addressof ( self . _blob ) ) <EOL> @ classmethod <EOL> def _offsetof ( cls , fieldname ) : <EOL> return getattr ( cls . _ctype , fieldname ) . offset <EOL> def _convert_to_address ( self , BClass ) : <EOL> if getattr ( BClass , '<STR_LIT>' , None ) is self . __class__ : <EOL> return ctypes . addressof ( self . _blob ) <EOL> else : <EOL> return CTypesData . _convert_to_address ( self , BClass ) <EOL> @ classmethod <EOL> def _from_ctypes ( cls , ctypes_struct_or_union ) : <EOL> self = cls . __new__ ( cls ) <EOL> self . _blob = ctypes_struct_or_union <EOL> return self <EOL> @ classmethod <EOL> def _to_ctypes ( cls , value ) : <EOL> return value . _blob <EOL> def __repr__ ( self , c_name = None ) : <EOL> return CTypesData . __repr__ ( self , c_name or self . _get_c_name ( '<STR_LIT>' ) ) <EOL> class CTypesBackend ( object ) : <EOL> PRIMITIVE_TYPES = { <EOL> '<STR_LIT>' : ctypes . c_char , <EOL> '<STR_LIT>' : ctypes . c_short , <EOL> '<STR_LIT:int>' : ctypes . c_int , <EOL> '<STR_LIT>' : ctypes . c_long , <EOL> '<STR_LIT>' : ctypes . c_longlong , <EOL> '<STR_LIT>' : ctypes . c_byte , <EOL> '<STR_LIT>' : ctypes . c_ubyte , <EOL> '<STR_LIT>' : ctypes . c_ushort , <EOL> '<STR_LIT>' : ctypes . c_uint , <EOL> '<STR_LIT>' : ctypes . c_ulong , <EOL> '<STR_LIT>' : ctypes . c_ulonglong , <EOL> '<STR_LIT:float>' : ctypes . c_float , <EOL> '<STR_LIT>' : ctypes . c_double , <EOL> '<STR_LIT>' : ctypes . c_bool , <EOL> } <EOL> for _name in [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> _size = ctypes . sizeof ( PRIMITIVE_TYPES [ _name ] ) <EOL> PRIMITIVE_TYPES [ '<STR_LIT>' % ( <NUM_LIT:8> * _size ) ] = PRIMITIVE_TYPES [ _name ] <EOL> if _size == ctypes . sizeof ( ctypes . c_void_p ) : <EOL> PRIMITIVE_TYPES [ '<STR_LIT>' ] = PRIMITIVE_TYPES [ _name ] <EOL> if _size == ctypes . sizeof ( ctypes . c_size_t ) : <EOL> PRIMITIVE_TYPES [ '<STR_LIT>' ] = PRIMITIVE_TYPES [ _name ] <EOL> for _name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:int>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> _size = ctypes . sizeof ( PRIMITIVE_TYPES [ _name ] ) <EOL> PRIMITIVE_TYPES [ '<STR_LIT>' % ( <NUM_LIT:8> * _size ) ] = PRIMITIVE_TYPES [ _name ] <EOL> if _size == ctypes . sizeof ( ctypes . c_void_p ) : <EOL> PRIMITIVE_TYPES [ '<STR_LIT>' ] = PRIMITIVE_TYPES [ _name ] <EOL> PRIMITIVE_TYPES [ '<STR_LIT>' ] = PRIMITIVE_TYPES [ _name ] <EOL> if _size == ctypes . sizeof ( ctypes . c_size_t ) : <EOL> PRIMITIVE_TYPES [ '<STR_LIT>' ] = PRIMITIVE_TYPES [ _name ] <EOL> def __init__ ( self ) : <EOL> self . RTLD_LAZY = <NUM_LIT:0> <EOL> self . RTLD_NOW = <NUM_LIT:0> <EOL> self . RTLD_GLOBAL = ctypes . RTLD_GLOBAL <EOL> self . RTLD_LOCAL = ctypes . RTLD_LOCAL <EOL> def set_ffi ( self , ffi ) : <EOL> self . ffi = ffi <EOL> def _get_types ( self ) : <EOL> return CTypesData , CTypesType <EOL> def load_library ( self , path , flags = <NUM_LIT:0> ) : <EOL> cdll = ctypes . CDLL ( path , flags ) <EOL> return CTypesLibrary ( self , cdll ) <EOL> def new_void_type ( self ) : <EOL> class CTypesVoid ( CTypesData ) : <EOL> __slots__ = [ ] <EOL> _reftypename = '<STR_LIT>' <EOL> @ staticmethod <EOL> def _from_ctypes ( novalue ) : <EOL> return None <EOL> @ staticmethod <EOL> def _to_ctypes ( novalue ) : <EOL> if novalue is not None : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( type ( novalue ) . __name__ , ) ) <EOL> return None <EOL> CTypesVoid . _fix_class ( ) <EOL> return CTypesVoid <EOL> def new_primitive_type ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> raise NotImplementedError ( name ) <EOL> ctype = self . PRIMITIVE_TYPES [ name ] <EOL> if name == '<STR_LIT>' : <EOL> kind = '<STR_LIT>' <EOL> elif name in ( '<STR_LIT:float>' , '<STR_LIT>' ) : <EOL> kind = '<STR_LIT:float>' <EOL> else : <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> kind = '<STR_LIT>' <EOL> elif name == '<STR_LIT>' : <EOL> kind = '<STR_LIT:bool>' <EOL> else : <EOL> kind = '<STR_LIT:int>' <EOL> is_signed = ( ctype ( - <NUM_LIT:1> ) . value == - <NUM_LIT:1> ) <EOL> def _cast_source_to_int ( source ) : <EOL> if isinstance ( source , ( int , long , float ) ) : <EOL> source = int ( source ) <EOL> elif isinstance ( source , CTypesData ) : <EOL> source = source . _cast_to_integer ( ) <EOL> elif isinstance ( source , bytes ) : <EOL> source = ord ( source ) <EOL> elif source is None : <EOL> source = <NUM_LIT:0> <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( CTypesPrimitive , type ( source ) . __name__ ) ) <EOL> return source <EOL> kind1 = kind <EOL> class CTypesPrimitive ( CTypesGenericPrimitive ) : <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> _ctype = ctype <EOL> _reftypename = '<STR_LIT>' % name <EOL> kind = kind1 <EOL> def __init__ ( self , value ) : <EOL> self . _value = value <EOL> @ staticmethod <EOL> def _create_ctype_obj ( init ) : <EOL> if init is None : <EOL> return ctype ( ) <EOL> return ctype ( CTypesPrimitive . _to_ctypes ( init ) ) <EOL> if kind == '<STR_LIT:int>' or kind == '<STR_LIT>' : <EOL> @ classmethod <EOL> def _cast_from ( cls , source ) : <EOL> source = _cast_source_to_int ( source ) <EOL> source = ctype ( source ) . value <EOL> return cls ( source ) <EOL> def __int__ ( self ) : <EOL> return self . _value <EOL> if kind == '<STR_LIT:bool>' : <EOL> @ classmethod <EOL> def _cast_from ( cls , source ) : <EOL> if not isinstance ( source , ( int , long , float ) ) : <EOL> source = _cast_source_to_int ( source ) <EOL> return cls ( bool ( source ) ) <EOL> def __int__ ( self ) : <EOL> return self . _value <EOL> if kind == '<STR_LIT>' : <EOL> @ classmethod <EOL> def _cast_from ( cls , source ) : <EOL> source = _cast_source_to_int ( source ) <EOL> source = bytechr ( source & <NUM_LIT> ) <EOL> return cls ( source ) <EOL> def __int__ ( self ) : <EOL> return ord ( self . _value ) <EOL> if kind == '<STR_LIT:float>' : <EOL> @ classmethod <EOL> def _cast_from ( cls , source ) : <EOL> if isinstance ( source , float ) : <EOL> pass <EOL> elif isinstance ( source , CTypesGenericPrimitive ) : <EOL> if hasattr ( source , '<STR_LIT>' ) : <EOL> source = float ( source ) <EOL> else : <EOL> source = int ( source ) <EOL> else : <EOL> source = _cast_source_to_int ( source ) <EOL> source = ctype ( source ) . value <EOL> return cls ( source ) <EOL> def __int__ ( self ) : <EOL> return int ( self . _value ) <EOL> def __float__ ( self ) : <EOL> return self . _value <EOL> _cast_to_integer = __int__ <EOL> if kind == '<STR_LIT:int>' or kind == '<STR_LIT>' or kind == '<STR_LIT:bool>' : <EOL> @ staticmethod <EOL> def _to_ctypes ( x ) : <EOL> if not isinstance ( x , ( int , long ) ) : <EOL> if isinstance ( x , CTypesData ) : <EOL> x = int ( x ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( x ) . __name__ ) <EOL> if ctype ( x ) . value != x : <EOL> if not is_signed and x < <NUM_LIT:0> : <EOL> raise OverflowError ( "<STR_LIT>" % name ) <EOL> else : <EOL> raise OverflowError ( "<STR_LIT>" <EOL> % name ) <EOL> return x <EOL> if kind == '<STR_LIT>' : <EOL> @ staticmethod <EOL> def _to_ctypes ( x ) : <EOL> if isinstance ( x , bytes ) and len ( x ) == <NUM_LIT:1> : <EOL> return x <EOL> if isinstance ( x , CTypesPrimitive ) : <EOL> return x . _value <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( x ) . __name__ ) <EOL> if kind == '<STR_LIT:float>' : <EOL> @ staticmethod <EOL> def _to_ctypes ( x ) : <EOL> if not isinstance ( x , ( int , long , float , CTypesData ) ) : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( x ) . __name__ ) <EOL> return ctype ( x ) . value <EOL> @ staticmethod <EOL> def _from_ctypes ( value ) : <EOL> return getattr ( value , '<STR_LIT:value>' , value ) <EOL> @ staticmethod <EOL> def _initialize ( blob , init ) : <EOL> blob . value = CTypesPrimitive . _to_ctypes ( init ) <EOL> if kind == '<STR_LIT>' : <EOL> def _to_string ( self , maxlen ) : <EOL> return self . _value <EOL> if kind == '<STR_LIT>' : <EOL> def _to_string ( self , maxlen ) : <EOL> return chr ( self . _value & <NUM_LIT> ) <EOL> CTypesPrimitive . _fix_class ( ) <EOL> return CTypesPrimitive <EOL> def new_pointer_type ( self , BItem ) : <EOL> getbtype = self . ffi . _get_cached_btype <EOL> if BItem is getbtype ( model . PrimitiveType ( '<STR_LIT>' ) ) : <EOL> kind = '<STR_LIT>' <EOL> elif BItem in ( getbtype ( model . PrimitiveType ( '<STR_LIT>' ) ) , <EOL> getbtype ( model . PrimitiveType ( '<STR_LIT>' ) ) ) : <EOL> kind = '<STR_LIT>' <EOL> elif BItem is getbtype ( model . void_type ) : <EOL> kind = '<STR_LIT>' <EOL> else : <EOL> kind = '<STR_LIT>' <EOL> class CTypesPtr ( CTypesGenericPtr ) : <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> if kind == '<STR_LIT>' : <EOL> __slots__ += [ '<STR_LIT>' ] <EOL> _BItem = BItem <EOL> if hasattr ( BItem , '<STR_LIT>' ) : <EOL> _ctype = ctypes . POINTER ( BItem . _ctype ) <EOL> _bitem_size = ctypes . sizeof ( BItem . _ctype ) <EOL> else : <EOL> _ctype = ctypes . c_void_p <EOL> if issubclass ( BItem , CTypesGenericArray ) : <EOL> _reftypename = BItem . _get_c_name ( '<STR_LIT>' ) <EOL> else : <EOL> _reftypename = BItem . _get_c_name ( '<STR_LIT>' ) <EOL> def __init__ ( self , init ) : <EOL> ctypeobj = BItem . _create_ctype_obj ( init ) <EOL> if kind == '<STR_LIT>' : <EOL> self . __as_strbuf = ctypes . create_string_buffer ( <EOL> ctypeobj . value + b'<STR_LIT:\x00>' ) <EOL> self . _as_ctype_ptr = ctypes . cast ( <EOL> self . __as_strbuf , self . _ctype ) <EOL> else : <EOL> self . _as_ctype_ptr = ctypes . pointer ( ctypeobj ) <EOL> self . _address = ctypes . cast ( self . _as_ctype_ptr , <EOL> ctypes . c_void_p ) . value <EOL> self . _own = True <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , ( int , long ) ) : <EOL> return self . _new_pointer_at ( self . _address + <EOL> other * self . _bitem_size ) <EOL> else : <EOL> return NotImplemented <EOL> def __sub__ ( self , other ) : <EOL> if isinstance ( other , ( int , long ) ) : <EOL> return self . _new_pointer_at ( self . _address - <EOL> other * self . _bitem_size ) <EOL> elif type ( self ) is type ( other ) : <EOL> return ( self . _address - other . _address ) // self . _bitem_size <EOL> else : <EOL> return NotImplemented <EOL> def __getitem__ ( self , index ) : <EOL> if getattr ( self , '<STR_LIT>' , False ) and index != <NUM_LIT:0> : <EOL> raise IndexError <EOL> return BItem . _from_ctypes ( self . _as_ctype_ptr [ index ] ) <EOL> def __setitem__ ( self , index , value ) : <EOL> self . _as_ctype_ptr [ index ] = BItem . _to_ctypes ( value ) <EOL> if kind == '<STR_LIT>' or kind == '<STR_LIT>' : <EOL> @ classmethod <EOL> def _arg_to_ctypes ( cls , * value ) : <EOL> if value and isinstance ( value [ <NUM_LIT:0> ] , bytes ) : <EOL> return ctypes . c_char_p ( value [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return super ( CTypesPtr , cls ) . _arg_to_ctypes ( * value ) <EOL> if kind == '<STR_LIT>' or kind == '<STR_LIT>' : <EOL> def _to_string ( self , maxlen ) : <EOL> if maxlen < <NUM_LIT:0> : <EOL> maxlen = sys . maxsize <EOL> p = ctypes . cast ( self . _as_ctype_ptr , <EOL> ctypes . POINTER ( ctypes . c_char ) ) <EOL> n = <NUM_LIT:0> <EOL> while n < maxlen and p [ n ] != b'<STR_LIT:\x00>' : <EOL> n += <NUM_LIT:1> <EOL> return b'<STR_LIT>' . join ( [ p [ i ] for i in range ( n ) ] ) <EOL> def _get_own_repr ( self ) : <EOL> if getattr ( self , '<STR_LIT>' , False ) : <EOL> return '<STR_LIT>' % ( <EOL> ctypes . sizeof ( self . _as_ctype_ptr . contents ) , ) <EOL> return super ( CTypesPtr , self ) . _get_own_repr ( ) <EOL> if ( BItem is self . ffi . _get_cached_btype ( model . void_type ) or <EOL> BItem is self . ffi . _get_cached_btype ( model . PrimitiveType ( '<STR_LIT>' ) ) ) : <EOL> CTypesPtr . _automatic_casts = True <EOL> CTypesPtr . _fix_class ( ) <EOL> return CTypesPtr <EOL> def new_array_type ( self , CTypesPtr , length ) : <EOL> if length is None : <EOL> brackets = '<STR_LIT>' <EOL> else : <EOL> brackets = '<STR_LIT>' % length <EOL> BItem = CTypesPtr . _BItem <EOL> getbtype = self . ffi . _get_cached_btype <EOL> if BItem is getbtype ( model . PrimitiveType ( '<STR_LIT>' ) ) : <EOL> kind = '<STR_LIT>' <EOL> elif BItem in ( getbtype ( model . PrimitiveType ( '<STR_LIT>' ) ) , <EOL> getbtype ( model . PrimitiveType ( '<STR_LIT>' ) ) ) : <EOL> kind = '<STR_LIT>' <EOL> else : <EOL> kind = '<STR_LIT>' <EOL> class CTypesArray ( CTypesGenericArray ) : <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if length is not None : <EOL> _ctype = BItem . _ctype * length <EOL> else : <EOL> __slots__ . append ( '<STR_LIT>' ) <EOL> _reftypename = BItem . _get_c_name ( brackets ) <EOL> _declared_length = length <EOL> _CTPtr = CTypesPtr <EOL> def __init__ ( self , init ) : <EOL> if length is None : <EOL> if isinstance ( init , ( int , long ) ) : <EOL> len1 = init <EOL> init = None <EOL> elif kind == '<STR_LIT>' and isinstance ( init , bytes ) : <EOL> len1 = len ( init ) + <NUM_LIT:1> <EOL> else : <EOL> init = tuple ( init ) <EOL> len1 = len ( init ) <EOL> self . _ctype = BItem . _ctype * len1 <EOL> self . _blob = self . _ctype ( ) <EOL> self . _own = True <EOL> if init is not None : <EOL> self . _initialize ( self . _blob , init ) <EOL> @ staticmethod <EOL> def _initialize ( blob , init ) : <EOL> if isinstance ( init , bytes ) : <EOL> init = [ init [ i : i + <NUM_LIT:1> ] for i in range ( len ( init ) ) ] <EOL> else : <EOL> init = tuple ( init ) <EOL> if len ( init ) > len ( blob ) : <EOL> raise IndexError ( "<STR_LIT>" ) <EOL> addr = ctypes . cast ( blob , ctypes . c_void_p ) . value <EOL> PTR = ctypes . POINTER ( BItem . _ctype ) <EOL> itemsize = ctypes . sizeof ( BItem . _ctype ) <EOL> for i , value in enumerate ( init ) : <EOL> p = ctypes . cast ( addr + i * itemsize , PTR ) <EOL> BItem . _initialize ( p . contents , value ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _blob ) <EOL> def __getitem__ ( self , index ) : <EOL> if not ( <NUM_LIT:0> <= index < len ( self . _blob ) ) : <EOL> raise IndexError <EOL> return BItem . _from_ctypes ( self . _blob [ index ] ) <EOL> def __setitem__ ( self , index , value ) : <EOL> if not ( <NUM_LIT:0> <= index < len ( self . _blob ) ) : <EOL> raise IndexError <EOL> self . _blob [ index ] = BItem . _to_ctypes ( value ) <EOL> if kind == '<STR_LIT>' or kind == '<STR_LIT>' : <EOL> def _to_string ( self , maxlen ) : <EOL> if maxlen < <NUM_LIT:0> : <EOL> maxlen = len ( self . _blob ) <EOL> p = ctypes . cast ( self . _blob , <EOL> ctypes . POINTER ( ctypes . c_char ) ) <EOL> n = <NUM_LIT:0> <EOL> while n < maxlen and p [ n ] != b'<STR_LIT:\x00>' : <EOL> n += <NUM_LIT:1> <EOL> return b'<STR_LIT>' . join ( [ p [ i ] for i in range ( n ) ] ) <EOL> def _get_own_repr ( self ) : <EOL> if getattr ( self , '<STR_LIT>' , False ) : <EOL> return '<STR_LIT>' % ( ctypes . sizeof ( self . _blob ) , ) <EOL> return super ( CTypesArray , self ) . _get_own_repr ( ) <EOL> def _convert_to_address ( self , BClass ) : <EOL> if BClass in ( CTypesPtr , None ) or BClass . _automatic_casts : <EOL> return ctypes . addressof ( self . _blob ) <EOL> else : <EOL> return CTypesData . _convert_to_address ( self , BClass ) <EOL> @ staticmethod <EOL> def _from_ctypes ( ctypes_array ) : <EOL> self = CTypesArray . __new__ ( CTypesArray ) <EOL> self . _blob = ctypes_array <EOL> return self <EOL> @ staticmethod <EOL> def _arg_to_ctypes ( value ) : <EOL> return CTypesPtr . _arg_to_ctypes ( value ) <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , ( int , long ) ) : <EOL> return CTypesPtr . _new_pointer_at ( <EOL> ctypes . addressof ( self . _blob ) + <EOL> other * ctypes . sizeof ( BItem . _ctype ) ) <EOL> else : <EOL> return NotImplemented <EOL> @ classmethod <EOL> def _cast_from ( cls , source ) : <EOL> raise NotImplementedError ( "<STR_LIT>" % ( <EOL> cls . _get_c_name ( ) , ) ) <EOL> CTypesArray . _fix_class ( ) <EOL> return CTypesArray <EOL> def _new_struct_or_union ( self , kind , name , base_ctypes_class ) : <EOL> class struct_or_union ( base_ctypes_class ) : <EOL> pass <EOL> struct_or_union . __name__ = '<STR_LIT>' % ( kind , name ) <EOL> kind1 = kind <EOL> class CTypesStructOrUnion ( CTypesBaseStructOrUnion ) : <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> _ctype = struct_or_union <EOL> _reftypename = '<STR_LIT>' % ( name , ) <EOL> _kind = kind = kind1 <EOL> CTypesStructOrUnion . _fix_class ( ) <EOL> return CTypesStructOrUnion <EOL> def new_struct_type ( self , name ) : <EOL> return self . _new_struct_or_union ( '<STR_LIT>' , name , ctypes . Structure ) <EOL> def new_union_type ( self , name ) : <EOL> return self . _new_struct_or_union ( '<STR_LIT>' , name , ctypes . Union ) <EOL> def complete_struct_or_union ( self , CTypesStructOrUnion , fields , tp , <EOL> totalsize = - <NUM_LIT:1> , totalalignment = - <NUM_LIT:1> , sflags = <NUM_LIT:0> ) : <EOL> if totalsize >= <NUM_LIT:0> or totalalignment >= <NUM_LIT:0> : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> struct_or_union = CTypesStructOrUnion . _ctype <EOL> fnames = [ fname for ( fname , BField , bitsize ) in fields ] <EOL> btypes = [ BField for ( fname , BField , bitsize ) in fields ] <EOL> bitfields = [ bitsize for ( fname , BField , bitsize ) in fields ] <EOL> bfield_types = { } <EOL> cfields = [ ] <EOL> for ( fname , BField , bitsize ) in fields : <EOL> if bitsize < <NUM_LIT:0> : <EOL> cfields . append ( ( fname , BField . _ctype ) ) <EOL> bfield_types [ fname ] = BField <EOL> else : <EOL> cfields . append ( ( fname , BField . _ctype , bitsize ) ) <EOL> bfield_types [ fname ] = Ellipsis <EOL> if sflags & <NUM_LIT:8> : <EOL> struct_or_union . _pack_ = <NUM_LIT:1> <EOL> struct_or_union . _fields_ = cfields <EOL> CTypesStructOrUnion . _bfield_types = bfield_types <EOL> @ staticmethod <EOL> def _create_ctype_obj ( init ) : <EOL> result = struct_or_union ( ) <EOL> if init is not None : <EOL> initialize ( result , init ) <EOL> return result <EOL> CTypesStructOrUnion . _create_ctype_obj = _create_ctype_obj <EOL> def initialize ( blob , init ) : <EOL> if is_union : <EOL> if len ( init ) > <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( len ( init ) , ) ) <EOL> if not isinstance ( init , dict ) : <EOL> if isinstance ( init , ( bytes , unicode ) ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> init = tuple ( init ) <EOL> if len ( init ) > len ( fnames ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> CTypesStructOrUnion . _get_c_name ( ) ) <EOL> init = dict ( zip ( fnames , init ) ) <EOL> addr = ctypes . addressof ( blob ) <EOL> for fname , value in init . items ( ) : <EOL> BField , bitsize = name2fieldtype [ fname ] <EOL> assert bitsize < <NUM_LIT:0> , "<STR_LIT>" <EOL> offset = CTypesStructOrUnion . _offsetof ( fname ) <EOL> PTR = ctypes . POINTER ( BField . _ctype ) <EOL> p = ctypes . cast ( addr + offset , PTR ) <EOL> BField . _initialize ( p . contents , value ) <EOL> is_union = CTypesStructOrUnion . _kind == '<STR_LIT>' <EOL> name2fieldtype = dict ( zip ( fnames , zip ( btypes , bitfields ) ) ) <EOL> for fname , BField , bitsize in fields : <EOL> if fname == '<STR_LIT>' : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if hasattr ( CTypesStructOrUnion , fname ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % fname ) <EOL> if bitsize < <NUM_LIT:0> : <EOL> def getter ( self , fname = fname , BField = BField , <EOL> offset = CTypesStructOrUnion . _offsetof ( fname ) , <EOL> PTR = ctypes . POINTER ( BField . _ctype ) ) : <EOL> addr = ctypes . addressof ( self . _blob ) <EOL> p = ctypes . cast ( addr + offset , PTR ) <EOL> return BField . _from_ctypes ( p . contents ) <EOL> def setter ( self , value , fname = fname , BField = BField ) : <EOL> setattr ( self . _blob , fname , BField . _to_ctypes ( value ) ) <EOL> if issubclass ( BField , CTypesGenericArray ) : <EOL> setter = None <EOL> if BField . _declared_length == <NUM_LIT:0> : <EOL> def getter ( self , fname = fname , BFieldPtr = BField . _CTPtr , <EOL> offset = CTypesStructOrUnion . _offsetof ( fname ) , <EOL> PTR = ctypes . POINTER ( BField . _ctype ) ) : <EOL> addr = ctypes . addressof ( self . _blob ) <EOL> p = ctypes . cast ( addr + offset , PTR ) <EOL> return BFieldPtr . _from_ctypes ( p ) <EOL> else : <EOL> def getter ( self , fname = fname , BField = BField ) : <EOL> return BField . _from_ctypes ( getattr ( self . _blob , fname ) ) <EOL> def setter ( self , value , fname = fname , BField = BField ) : <EOL> value = BField . _to_ctypes ( value ) <EOL> oldvalue = getattr ( self . _blob , fname ) <EOL> setattr ( self . _blob , fname , value ) <EOL> if value != getattr ( self . _blob , fname ) : <EOL> setattr ( self . _blob , fname , oldvalue ) <EOL> raise OverflowError ( "<STR_LIT>" ) <EOL> setattr ( CTypesStructOrUnion , fname , property ( getter , setter ) ) <EOL> CTypesPtr = self . ffi . _get_cached_btype ( model . PointerType ( tp ) ) <EOL> for fname in fnames : <EOL> if hasattr ( CTypesPtr , fname ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % fname ) <EOL> def getter ( self , fname = fname ) : <EOL> return getattr ( self [ <NUM_LIT:0> ] , fname ) <EOL> def setter ( self , value , fname = fname ) : <EOL> setattr ( self [ <NUM_LIT:0> ] , fname , value ) <EOL> setattr ( CTypesPtr , fname , property ( getter , setter ) ) <EOL> def new_function_type ( self , BArgs , BResult , has_varargs ) : <EOL> nameargs = [ BArg . _get_c_name ( ) for BArg in BArgs ] <EOL> if has_varargs : <EOL> nameargs . append ( '<STR_LIT>' ) <EOL> nameargs = '<STR_LIT:U+002CU+0020>' . join ( nameargs ) <EOL> class CTypesFunctionPtr ( CTypesGenericPtr ) : <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> _ctype = ctypes . CFUNCTYPE ( getattr ( BResult , '<STR_LIT>' , None ) , <EOL> * [ BArg . _ctype for BArg in BArgs ] , <EOL> use_errno = True ) <EOL> _reftypename = BResult . _get_c_name ( '<STR_LIT>' % ( nameargs , ) ) <EOL> def __init__ ( self , init , error = None ) : <EOL> import traceback <EOL> assert not has_varargs , "<STR_LIT>" <EOL> if getattr ( BResult , '<STR_LIT>' , None ) is not None : <EOL> error = BResult . _from_ctypes ( <EOL> BResult . _create_ctype_obj ( error ) ) <EOL> else : <EOL> error = None <EOL> def callback ( * args ) : <EOL> args2 = [ ] <EOL> for arg , BArg in zip ( args , BArgs ) : <EOL> args2 . append ( BArg . _from_ctypes ( arg ) ) <EOL> try : <EOL> res2 = init ( * args2 ) <EOL> res2 = BResult . _to_ctypes ( res2 ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> res2 = error <EOL> if issubclass ( BResult , CTypesGenericPtr ) : <EOL> if res2 : <EOL> res2 = ctypes . cast ( res2 , ctypes . c_void_p ) . value <EOL> else : <EOL> res2 = None <EOL> return res2 <EOL> if issubclass ( BResult , CTypesGenericPtr ) : <EOL> callback_ctype = ctypes . CFUNCTYPE ( <EOL> ctypes . c_void_p , <EOL> * [ BArg . _ctype for BArg in BArgs ] , <EOL> use_errno = True ) <EOL> else : <EOL> callback_ctype = CTypesFunctionPtr . _ctype <EOL> self . _as_ctype_ptr = callback_ctype ( callback ) <EOL> self . _address = ctypes . cast ( self . _as_ctype_ptr , <EOL> ctypes . c_void_p ) . value <EOL> self . _own_callback = init <EOL> @ staticmethod <EOL> def _initialize ( ctypes_ptr , value ) : <EOL> if value : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __repr__ ( self ) : <EOL> c_name = getattr ( self , '<STR_LIT>' , None ) <EOL> if c_name : <EOL> i = self . _reftypename . index ( '<STR_LIT>' ) <EOL> if self . _reftypename [ i - <NUM_LIT:1> ] not in '<STR_LIT>' : <EOL> c_name = '<STR_LIT:U+0020>' + c_name <EOL> c_name = self . _reftypename . replace ( '<STR_LIT>' , c_name ) <EOL> return CTypesData . __repr__ ( self , c_name ) <EOL> def _get_own_repr ( self ) : <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None : <EOL> return '<STR_LIT>' % ( self . _own_callback , ) <EOL> return super ( CTypesFunctionPtr , self ) . _get_own_repr ( ) <EOL> def __call__ ( self , * args ) : <EOL> if has_varargs : <EOL> assert len ( args ) >= len ( BArgs ) <EOL> extraargs = args [ len ( BArgs ) : ] <EOL> args = args [ : len ( BArgs ) ] <EOL> else : <EOL> assert len ( args ) == len ( BArgs ) <EOL> ctypes_args = [ ] <EOL> for arg , BArg in zip ( args , BArgs ) : <EOL> ctypes_args . append ( BArg . _arg_to_ctypes ( arg ) ) <EOL> if has_varargs : <EOL> for i , arg in enumerate ( extraargs ) : <EOL> if arg is None : <EOL> ctypes_args . append ( ctypes . c_void_p ( <NUM_LIT:0> ) ) <EOL> continue <EOL> if not isinstance ( arg , CTypesData ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( <NUM_LIT:1> + len ( BArgs ) + i , type ( arg ) . __name__ ) ) <EOL> ctypes_args . append ( arg . _arg_to_ctypes ( arg ) ) <EOL> result = self . _as_ctype_ptr ( * ctypes_args ) <EOL> return BResult . _from_ctypes ( result ) <EOL> CTypesFunctionPtr . _fix_class ( ) <EOL> return CTypesFunctionPtr <EOL> def new_enum_type ( self , name , enumerators , enumvalues , CTypesInt ) : <EOL> assert isinstance ( name , str ) <EOL> reverse_mapping = dict ( zip ( reversed ( enumvalues ) , <EOL> reversed ( enumerators ) ) ) <EOL> class CTypesEnum ( CTypesInt ) : <EOL> __slots__ = [ ] <EOL> _reftypename = '<STR_LIT>' % name <EOL> def _get_own_repr ( self ) : <EOL> value = self . _value <EOL> try : <EOL> return '<STR_LIT>' % ( value , reverse_mapping [ value ] ) <EOL> except KeyError : <EOL> return str ( value ) <EOL> def _to_string ( self , maxlen ) : <EOL> value = self . _value <EOL> try : <EOL> return reverse_mapping [ value ] <EOL> except KeyError : <EOL> return str ( value ) <EOL> CTypesEnum . _fix_class ( ) <EOL> return CTypesEnum <EOL> def get_errno ( self ) : <EOL> return ctypes . get_errno ( ) <EOL> def set_errno ( self , value ) : <EOL> ctypes . set_errno ( value ) <EOL> def string ( self , b , maxlen = - <NUM_LIT:1> ) : <EOL> return b . _to_string ( maxlen ) <EOL> def buffer ( self , bptr , size = - <NUM_LIT:1> ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def sizeof ( self , cdata_or_BType ) : <EOL> if isinstance ( cdata_or_BType , CTypesData ) : <EOL> return cdata_or_BType . _get_size_of_instance ( ) <EOL> else : <EOL> assert issubclass ( cdata_or_BType , CTypesData ) <EOL> return cdata_or_BType . _get_size ( ) <EOL> def alignof ( self , BType ) : <EOL> assert issubclass ( BType , CTypesData ) <EOL> return BType . _alignment ( ) <EOL> def newp ( self , BType , source ) : <EOL> if not issubclass ( BType , CTypesData ) : <EOL> raise TypeError <EOL> return BType . _newp ( source ) <EOL> def cast ( self , BType , source ) : <EOL> return BType . _cast_from ( source ) <EOL> def callback ( self , BType , source , error , onerror ) : <EOL> assert onerror is None <EOL> return BType ( source , error ) <EOL> typeof = type <EOL> def getcname ( self , BType , replace_with ) : <EOL> return BType . _get_c_name ( replace_with ) <EOL> def typeoffsetof ( self , BType , fieldname , num = <NUM_LIT:0> ) : <EOL> if isinstance ( fieldname , str ) : <EOL> if num == <NUM_LIT:0> and issubclass ( BType , CTypesGenericPtr ) : <EOL> BType = BType . _BItem <EOL> if not issubclass ( BType , CTypesBaseStructOrUnion ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> BField = BType . _bfield_types [ fieldname ] <EOL> if BField is Ellipsis : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return ( BField , BType . _offsetof ( fieldname ) ) <EOL> elif isinstance ( fieldname , ( int , long ) ) : <EOL> if issubclass ( BType , CTypesGenericArray ) : <EOL> BType = BType . _CTPtr <EOL> if not issubclass ( BType , CTypesGenericPtr ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> BItem = BType . _BItem <EOL> offset = BItem . _get_size ( ) * fieldname <EOL> if offset > sys . maxsize : <EOL> raise OverflowError <EOL> return ( BItem , offset ) <EOL> else : <EOL> raise TypeError ( type ( fieldname ) ) <EOL> def rawaddressof ( self , BTypePtr , cdata , offset = None ) : <EOL> if isinstance ( cdata , CTypesBaseStructOrUnion ) : <EOL> ptr = ctypes . pointer ( type ( cdata ) . _to_ctypes ( cdata ) ) <EOL> elif isinstance ( cdata , CTypesGenericPtr ) : <EOL> if offset is None or not issubclass ( type ( cdata ) . _BItem , <EOL> CTypesBaseStructOrUnion ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> ptr = type ( cdata ) . _to_ctypes ( cdata ) <EOL> elif isinstance ( cdata , CTypesGenericArray ) : <EOL> ptr = type ( cdata ) . _to_ctypes ( cdata ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if offset : <EOL> ptr = ctypes . cast ( <EOL> ctypes . c_void_p ( <EOL> ctypes . cast ( ptr , ctypes . c_void_p ) . value + offset ) , <EOL> type ( ptr ) ) <EOL> return BTypePtr . _from_ctypes ( ptr ) <EOL> class CTypesLibrary ( object ) : <EOL> def __init__ ( self , backend , cdll ) : <EOL> self . backend = backend <EOL> self . cdll = cdll <EOL> def load_function ( self , BType , name ) : <EOL> c_func = getattr ( self . cdll , name ) <EOL> funcobj = BType . _from_ctypes ( c_func ) <EOL> funcobj . _name = name <EOL> return funcobj <EOL> def read_variable ( self , BType , name ) : <EOL> try : <EOL> ctypes_obj = BType . _ctype . in_dll ( self . cdll , name ) <EOL> except AttributeError as e : <EOL> raise NotImplementedError ( e ) <EOL> return BType . _from_ctypes ( ctypes_obj ) <EOL> def write_variable ( self , BType , name , value ) : <EOL> new_ctypes_obj = BType . _to_ctypes ( value ) <EOL> ctypes_obj = BType . _ctype . in_dll ( self . cdll , name ) <EOL> ctypes . memmove ( ctypes . addressof ( ctypes_obj ) , <EOL> ctypes . addressof ( new_ctypes_obj ) , <EOL> ctypes . sizeof ( BType . _ctype ) ) </s>
