<s> import _rawffi <EOL> import weakref <EOL> import sys <EOL> SIMPLE_TYPE_CHARS = "<STR_LIT>" <EOL> from _ctypes . basics import _CData , _CDataMeta , cdata_from_address , CArgObject <EOL> from _ctypes . builtin import ConvMode <EOL> from _ctypes . array import Array <EOL> from _ctypes . pointer import _Pointer <EOL> class NULL ( object ) : <EOL> pass <EOL> NULL = NULL ( ) <EOL> TP_TO_DEFAULT = { <EOL> '<STR_LIT:c>' : <NUM_LIT:0> , <EOL> '<STR_LIT:u>' : <NUM_LIT:0> , <EOL> '<STR_LIT:b>' : <NUM_LIT:0> , <EOL> '<STR_LIT:B>' : <NUM_LIT:0> , <EOL> '<STR_LIT:h>' : <NUM_LIT:0> , <EOL> '<STR_LIT:H>' : <NUM_LIT:0> , <EOL> '<STR_LIT:i>' : <NUM_LIT:0> , <EOL> '<STR_LIT:I>' : <NUM_LIT:0> , <EOL> '<STR_LIT:l>' : <NUM_LIT:0> , <EOL> '<STR_LIT:L>' : <NUM_LIT:0> , <EOL> '<STR_LIT:q>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:f>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT:d>' : <NUM_LIT:0.0> , <EOL> '<STR_LIT:P>' : None , <EOL> '<STR_LIT:O>' : NULL , <EOL> '<STR_LIT:z>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> TP_TO_DEFAULT [ '<STR_LIT:X>' ] = NULL <EOL> TP_TO_DEFAULT [ '<STR_LIT:v>' ] = <NUM_LIT:0> <EOL> DEFAULT_VALUE = object ( ) <EOL> class GlobalPyobjContainer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . objs = [ ] <EOL> def add ( self , obj ) : <EOL> num = len ( self . objs ) <EOL> self . objs . append ( weakref . ref ( obj ) ) <EOL> return num <EOL> def get ( self , num ) : <EOL> return self . objs [ num ] ( ) <EOL> pyobj_container = GlobalPyobjContainer ( ) <EOL> def generic_xxx_p_from_param ( cls , value ) : <EOL> from _ctypes import Array , _Pointer <EOL> if value is None : <EOL> return cls ( None ) <EOL> if isinstance ( value , basestring ) : <EOL> return cls ( value ) <EOL> if isinstance ( value , _SimpleCData ) and type ( value ) . _type_ in '<STR_LIT>' : <EOL> return value <EOL> return None <EOL> def from_param_char_p ( cls , value ) : <EOL> "<STR_LIT>" <EOL> res = generic_xxx_p_from_param ( cls , value ) <EOL> if res is not None : <EOL> return res <EOL> if isinstance ( value , ( Array , _Pointer ) ) : <EOL> from ctypes import c_char , c_byte , c_wchar <EOL> if type ( value ) . _type_ in [ c_char , c_byte , c_wchar ] : <EOL> return value <EOL> def from_param_void_p ( cls , value ) : <EOL> "<STR_LIT>" <EOL> res = generic_xxx_p_from_param ( cls , value ) <EOL> if res is not None : <EOL> return res <EOL> if isinstance ( value , Array ) : <EOL> return value <EOL> if isinstance ( value , _Pointer ) : <EOL> return cls . from_address ( value . _buffer . buffer ) <EOL> FROM_PARAM_BY_TYPE = { <EOL> '<STR_LIT:z>' : from_param_char_p , <EOL> '<STR_LIT>' : from_param_char_p , <EOL> '<STR_LIT:P>' : from_param_void_p , <EOL> } <EOL> class SimpleType ( _CDataMeta ) : <EOL> def __new__ ( self , name , bases , dct ) : <EOL> try : <EOL> tp = dct [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> for base in bases : <EOL> if hasattr ( base , '<STR_LIT>' ) : <EOL> tp = base . _type_ <EOL> break <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> if ( not isinstance ( tp , str ) or <EOL> not len ( tp ) == <NUM_LIT:1> or <EOL> tp not in SIMPLE_TYPE_CHARS ) : <EOL> raise ValueError ( '<STR_LIT>' % ( tp ) ) <EOL> default = TP_TO_DEFAULT [ tp ] <EOL> ffiarray = _rawffi . Array ( tp ) <EOL> result = type . __new__ ( self , name , bases , dct ) <EOL> result . _ffiargshape = tp <EOL> result . _ffishape = tp <EOL> result . _fficompositesize = None <EOL> result . _ffiarray = ffiarray <EOL> if tp == '<STR_LIT:z>' : <EOL> from _ctypes import Array , _Pointer <EOL> def _getvalue ( self ) : <EOL> addr = self . _buffer [ <NUM_LIT:0> ] <EOL> if addr == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> return _rawffi . charp2string ( addr ) <EOL> def _setvalue ( self , value ) : <EOL> if isinstance ( value , basestring ) : <EOL> if isinstance ( value , unicode ) : <EOL> value = value . encode ( ConvMode . encoding , <EOL> ConvMode . errors ) <EOL> array = _rawffi . Array ( '<STR_LIT:c>' ) ( len ( value ) + <NUM_LIT:1> , value ) <EOL> value = array . buffer <EOL> self . _objects = { '<STR_LIT:0>' : CArgObject ( array ) } <EOL> elif value is None : <EOL> value = <NUM_LIT:0> <EOL> self . _buffer [ <NUM_LIT:0> ] = value <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> elif tp == '<STR_LIT>' : <EOL> from _ctypes import Array , _Pointer , _wstring_at_addr <EOL> def _getvalue ( self ) : <EOL> addr = self . _buffer [ <NUM_LIT:0> ] <EOL> if addr == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> return _wstring_at_addr ( addr , - <NUM_LIT:1> ) <EOL> def _setvalue ( self , value ) : <EOL> if isinstance ( value , basestring ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . decode ( ConvMode . encoding , <EOL> ConvMode . errors ) <EOL> array = _rawffi . Array ( '<STR_LIT:u>' ) ( len ( value ) + <NUM_LIT:1> , value ) <EOL> value = array . buffer <EOL> self . _objects = { '<STR_LIT:0>' : CArgObject ( array ) } <EOL> elif value is None : <EOL> value = <NUM_LIT:0> <EOL> self . _buffer [ <NUM_LIT:0> ] = value <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> elif tp == '<STR_LIT:P>' : <EOL> def _getvalue ( self ) : <EOL> addr = self . _buffer [ <NUM_LIT:0> ] <EOL> if addr == <NUM_LIT:0> : <EOL> return None <EOL> return addr <EOL> def _setvalue ( self , value ) : <EOL> if isinstance ( value , str ) : <EOL> array = _rawffi . Array ( '<STR_LIT:c>' ) ( len ( value ) + <NUM_LIT:1> , value ) <EOL> value = array . buffer <EOL> self . _objects = { '<STR_LIT:0>' : CArgObject ( array ) } <EOL> elif value is None : <EOL> value = <NUM_LIT:0> <EOL> self . _buffer [ <NUM_LIT:0> ] = value <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> elif tp == '<STR_LIT:u>' : <EOL> def _setvalue ( self , val ) : <EOL> if isinstance ( val , str ) : <EOL> val = val . decode ( ConvMode . encoding , ConvMode . errors ) <EOL> if val : <EOL> self . _buffer [ <NUM_LIT:0> ] = val <EOL> def _getvalue ( self ) : <EOL> return self . _buffer [ <NUM_LIT:0> ] <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> elif tp == '<STR_LIT:c>' : <EOL> def _setvalue ( self , val ) : <EOL> if isinstance ( val , unicode ) : <EOL> val = val . encode ( ConvMode . encoding , ConvMode . errors ) <EOL> if val : <EOL> self . _buffer [ <NUM_LIT:0> ] = val <EOL> def _getvalue ( self ) : <EOL> return self . _buffer [ <NUM_LIT:0> ] <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> elif tp == '<STR_LIT:O>' : <EOL> def _setvalue ( self , val ) : <EOL> num = pyobj_container . add ( val ) <EOL> self . _buffer [ <NUM_LIT:0> ] = num <EOL> def _getvalue ( self ) : <EOL> return pyobj_container . get ( self . _buffer [ <NUM_LIT:0> ] ) <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> elif tp == '<STR_LIT:X>' : <EOL> from ctypes import windll <EOL> SysAllocStringLen = windll . oleaut32 . SysAllocStringLen <EOL> SysStringLen = windll . oleaut32 . SysStringLen <EOL> SysFreeString = windll . oleaut32 . SysFreeString <EOL> from _ctypes import _wstring_at_addr <EOL> def _getvalue ( self ) : <EOL> addr = self . _buffer [ <NUM_LIT:0> ] <EOL> if addr == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> size = SysStringLen ( addr ) <EOL> return _wstring_at_addr ( addr , size ) <EOL> def _setvalue ( self , value ) : <EOL> if isinstance ( value , basestring ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . decode ( ConvMode . encoding , <EOL> ConvMode . errors ) <EOL> array = _rawffi . Array ( '<STR_LIT:u>' ) ( len ( value ) + <NUM_LIT:1> , value ) <EOL> value = SysAllocStringLen ( array . buffer , len ( value ) ) <EOL> elif value is None : <EOL> value = <NUM_LIT:0> <EOL> if self . _buffer [ <NUM_LIT:0> ] : <EOL> SysFreeString ( self . _buffer [ <NUM_LIT:0> ] ) <EOL> self . _buffer [ <NUM_LIT:0> ] = value <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> elif tp == '<STR_LIT:v>' : <EOL> def _getvalue ( self ) : <EOL> return bool ( self . _buffer [ <NUM_LIT:0> ] ) <EOL> def _setvalue ( self , value ) : <EOL> if value : <EOL> self . _buffer [ <NUM_LIT:0> ] = - <NUM_LIT:1> <EOL> else : <EOL> self . _buffer [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> result . value = property ( _getvalue , _setvalue ) <EOL> return result <EOL> from_address = cdata_from_address <EOL> def from_param ( self , value ) : <EOL> from_param_f = FROM_PARAM_BY_TYPE . get ( self . _type_ ) <EOL> if from_param_f : <EOL> res = from_param_f ( self , value ) <EOL> if res is not None : <EOL> return res <EOL> if isinstance ( value , self ) : <EOL> return value <EOL> try : <EOL> return self ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> return super ( SimpleType , self ) . from_param ( value ) <EOL> def _CData_output ( self , resbuffer , base = None , index = - <NUM_LIT:1> ) : <EOL> output = super ( SimpleType , self ) . _CData_output ( resbuffer , base , index ) <EOL> return output . value <EOL> def _sizeofinstances ( self ) : <EOL> return _rawffi . sizeof ( self . _type_ ) <EOL> def _alignmentofinstances ( self ) : <EOL> return _rawffi . alignment ( self . _type_ ) <EOL> def _is_pointer_like ( self ) : <EOL> return self . _type_ in "<STR_LIT>" <EOL> class _SimpleCData ( _CData ) : <EOL> __metaclass__ = SimpleType <EOL> _type_ = '<STR_LIT:i>' <EOL> def __init__ ( self , value = DEFAULT_VALUE ) : <EOL> self . _buffer = self . _ffiarray ( <NUM_LIT:1> , autofree = True ) <EOL> if value is not DEFAULT_VALUE : <EOL> self . value = value <EOL> def _ensure_objects ( self ) : <EOL> return None <EOL> def _getvalue ( self ) : <EOL> return self . _buffer [ <NUM_LIT:0> ] <EOL> def _setvalue ( self , value ) : <EOL> self . _buffer [ <NUM_LIT:0> ] = value <EOL> value = property ( _getvalue , _setvalue ) <EOL> del _getvalue , _setvalue <EOL> def __ctypes_from_outparam__ ( self ) : <EOL> meta = type ( type ( self ) ) <EOL> if issubclass ( meta , SimpleType ) and meta != SimpleType : <EOL> return self <EOL> return self . value <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( type ( self ) . __name__ , self . value ) <EOL> def __nonzero__ ( self ) : <EOL> return self . _buffer [ <NUM_LIT:0> ] not in ( <NUM_LIT:0> , '<STR_LIT:\x00>' ) </s>
